Completed
Pull Request — master (#16)
by Simon
01:18
created

PartialUserFormControllerTest::testArrayData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\PartialUserforms\Tests;
4
5
use Firesphere\PartialUserforms\Controllers\PartialUserFormController;
6
use Firesphere\PartialUserforms\Models\PartialFieldSubmission;
7
use Firesphere\PartialUserforms\Models\PartialFileFieldSubmission;
8
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Control\Session;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\ORM\DataList;
15
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
16
use SilverStripe\UserForms\Model\UserDefinedForm;
17
18
class PartialUserFormControllerTest extends SapphireTest
19
{
20
    protected static $fixture_file = '../fixtures/partialformtest.yml';
21
22
    /**
23
     * @var PartialUserFormController
24
     */
25
    protected $controller;
26
27
    public function setUp()
28
    {
29
        $this->controller = Injector::inst()->get(PartialUserFormController::class);
30
        parent::setUp();
31
    }
32
33
    public function testClassExists()
34
    {
35
        $this->assertInstanceOf(PartialUserFormController::class, $this->controller);
36
    }
37
38
    public function testSavePartialSubmissionExists()
39
    {
40
        $this->assertTrue(method_exists($this->controller, 'savePartialSubmission'));
41
    }
42
43 View Code Duplication
    public function testSavePartialSubmissionFormCreated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field1' => 'Value1']);
46
        $session = new Session(['hi' => 'bye']);
47
        $request->setSession($session);
48
49
        $id = $this->controller->savePartialSubmission($request);
50
51
        $this->assertInternalType('numeric', $id->getBody());
52
53
        $form = PartialFormSubmission::get()->byID($id->getBody());
54
55
        $this->assertInstanceOf(PartialFormSubmission::class, $form);
56
    }
57
58
    public function testSavePartialSubmissionFileField()
59
    {
60
        $form = $this->objFromFixture(UserDefinedForm::class, 'form1');
61
        $form->publishRecursive();
62
        $field = $this->objFromFixture(EditableFileField::class, 'filefield1');
63
        $field->write();
64
        $multipart = [
65
            'name'     => 'file1',
66
            'contents' => fopen(__DIR__ . '/../fixtures/Hans-fullsize-sqr.png', 'rb'),
67
            'filename' => 'Hans-fullsize-sqr.png'
68
        ];
69
70
        $response = Director::test('/partialuserform/save', ['File' => $multipart], [], 'POST');
71
        $this->assertNotNull(PartialFileFieldSubmission::get()->filter(['Name' => 'File']));
72
        $this->assertEquals(201, $response->getStatusCode());
73
    }
74
75 View Code Duplication
    public function testSavePartialSubmissionFieldCreated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field1' => 'Value1']);
78
        $session = new Session(['hi' => 'bye']);
79
        $request->setSession($session);
80
81
        $id = $this->controller->savePartialSubmission($request);
82
83
        $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id->getBody()]);
84
85
        $this->assertEquals(1, $fields->count());
86
    }
87
88
    public function testPartialFormSubmissionExists()
89
    {
90
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field1' => 'Value1', 'Field2' => 'Value2']);
91
        $session = new Session(['hi' => 'bye']);
92
        $request->setSession($session);
93
94
        $id = $this->controller->savePartialSubmission($request);
95
96
        $session = $request->getSession();
97
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field2' => 'Value2']);
98
        $request->setSession($session);
99
100
        $secondId = $this->controller->savePartialSubmission($request);
101
102
        $this->assertEquals($id, $secondId);
103
    }
104
105
    public function testPartialFormSubmissionExistingField()
106
    {
107
        $values = [
108
            'Field1' => 'Value1',
109
            'Field2' => 'Value2',
110
            'Field3' => 'null'
111
        ];
112
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
113
        $session = new Session(['hi' => 'bye']);
114
        $request->setSession($session);
115
116
        $this->controller->savePartialSubmission($request);
117
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
118
        $field3 = PartialFieldSubmission::get()
119
            ->filter([
120
                'Name'            => 'Field3',
121
                'SubmittedFormID' => $sessionKey
122
            ])
123
            ->first();
124
125
        $this->assertEquals('null', $field3->Value);
126
        // Update the values
127
        $values['Field3'] = 'Value3';
128
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
129
        $request->setSession($session);
130
        $this->controller->savePartialSubmission($request);
131
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
132
133
        $field3 = PartialFieldSubmission::get()
134
            ->filter([
135
                'Name'            => 'Field3',
136
                'SubmittedFormID' => $sessionKey
137
            ])
138
            ->first();
139
        $this->assertEquals('Value3', $field3->Value);
140
    }
141
142
    public function testSubmittedFieldTitle()
143
    {
144
        $values = [
145
            'Field1' => 'Value1',
146
            'Field2' => 'Value2',
147
            'Field3' => 'null'
148
        ];
149
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
150
        $session = new Session(['hi' => 'bye']);
151
        $request->setSession($session);
152
153
        $this->controller->savePartialSubmission($request);
154
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
155
        /** @var DataList|PartialFieldSubmission[] $fields */
156
        $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $sessionKey]);
157
158
        foreach ($fields as $key => $field) {
159
            $this->assertEquals('Field ' . ($key + 1), $field->Title, 'Test field ' . $key);
160
        }
161
    }
162
163
    public function testParent()
164
    {
165
        $values = [
166
            'Field1' => 'Value1',
167
            'Field2' => 'Value2',
168
            'Field3' => 'null'
169
        ];
170
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
171
        $session = new Session(['hi' => 'bye']);
172
        $request->setSession($session);
173
174
        $this->controller->savePartialSubmission($request);
175
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
176
        /** @var DataList|PartialFieldSubmission[] $fields */
177
        $partialForm = PartialFormSubmission::get()->byID($sessionKey);
178
179
        $this->assertEquals('SilverStripe\UserForms\Model\UserDefinedForm', $partialForm->ParentClass);
180
    }
181
182
    public function testUnwantedFields()
183
    {
184
        $values = [
185
            'Field1'         => 'Value1',
186
            'Field2'         => 'Value2',
187
            'Field3'         => 'null',
188
            'SecurityID'     => '123456789aoeu',
189
            'action_process' => 'Submit'
190
        ];
191
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
192
        $session = new Session(['hi' => 'bye']);
193
        $request->setSession($session);
194
195
        $this->controller->savePartialSubmission($request);
196
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
197
        /** @var DataList|PartialFieldSubmission[] $fields */
198
        $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $sessionKey]);
199
200
        $items = $fields->column('Name');
201
        $this->assertFalse(in_array('SecurityID', $items, true));
202
        $this->assertFalse(in_array('action_process', $items, true));
203
    }
204
205
    public function testArrayData()
206
    {
207
        $values = [
208
            'Field1' => 'Value1',
209
            'Field2' => 'Value2',
210
            'Field3' => ['Value1', 'Value2']
211
        ];
212
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
213
        $session = new Session(['hi' => 'bye']);
214
        $request->setSession($session);
215
216
        $this->controller->savePartialSubmission($request);
217
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
218
        $field3 = PartialFieldSubmission::get()
219
            ->filter([
220
                'Name'            => 'Field3',
221
                'SubmittedFormID' => $sessionKey
222
            ])
223
            ->first();
224
        $this->assertEquals('Value1, Value2', $field3->Value);
225
    }
226
}
227