1 | <?php |
||
17 | * Class PartialSubmissionControllerTest |
||
18 | * @package Firesphere\PartialUserforms\Tests |
||
19 | */ |
||
20 | class PartialSubmissionControllerTest extends FunctionalTest |
||
21 | { |
||
22 | protected static $fixture_file = '../fixtures/partialformtest.yml'; |
||
23 | |||
24 | /** |
||
25 | * @var PartialSubmissionController |
||
26 | */ |
||
27 | protected $controller; |
||
28 | |||
29 | public function testClassExists() |
||
30 | { |
||
31 | $this->assertInstanceOf(PartialSubmissionController::class, $this->controller); |
||
32 | } |
||
33 | |||
34 | public function testSavePartialSubmissionExists() |
||
35 | { |
||
36 | $this->assertTrue(method_exists($this->controller, 'savePartialSubmission')); |
||
37 | } |
||
38 | |||
39 | public function testSavePartialSubmissionFormCreated() |
||
40 | { |
||
41 | // If successful, will return the SubmittedFormID from the session |
||
42 | $id = $this->savePartial(['Field1' => 'Value1']); |
||
43 | $this->assertInternalType('int', $id); |
||
44 | |||
45 | $partial = PartialFormSubmission::get()->byID($id); |
||
46 | $this->assertNotNull($partial); |
||
47 | } |
||
48 | |||
49 | public function testSavePartialSubmissionFieldCreated() |
||
50 | { |
||
51 | // If successful, will return the SubmittedFormID from the session |
||
52 | $id = $this->savePartial(['Field1' => 'Value1']); |
||
53 | $this->assertInternalType('int', $id); |
||
54 | |||
55 | $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]); |
||
56 | $this->assertCount(1, $fields); |
||
57 | } |
||
58 | |||
59 | public function testSavePartialSubmissionFileField() |
||
60 | { |
||
61 | $field = $this->objFromFixture(EditableFileField::class, 'filefield1'); |
||
62 | $field->write(); |
||
63 | copy(__DIR__ . '/../fixtures/Hans-fullsize-sqr.png', TEMP_FOLDER . '/tmpfile'); |
||
64 | $multipart = [ |
||
65 | 'name' => 'file1', |
||
66 | 'tmp_name' => TEMP_FOLDER . '/tmpfile', |
||
67 | 'filename' => 'Hans-fullsize-sqr.png', |
||
68 | 'type' => 'image/png', |
||
69 | 'error' => null, |
||
70 | 'size' => filesize(TEMP_FOLDER . '/tmpfile') |
||
71 | ]; |
||
72 | // Disable checking if the file is uploaded through a POST |
||
73 | Upload_Validator::config()->set('use_is_uploaded_file', false); |
||
74 | $this->savePartial(['File' => $multipart]); |
||
75 | $this->assertNotNull(PartialFileFieldSubmission::get()->filter(['Name' => 'File'])); |
||
76 | } |
||
77 | |||
78 | public function testPartialFormSubmissionExists() |
||
79 | { |
||
80 | // If successful, will return the SubmittedFormID from the session |
||
81 | $id = $this->savePartial(['Field1' => 'Value1', 'Field2' => 'Value2']); |
||
82 | |||
83 | // Second submission |
||
84 | $secondId = $this->savePartial(['Field2' => 'Value2']); |
||
85 | |||
86 | $this->assertEquals($id, $secondId); |
||
87 | } |
||
88 | |||
89 | public function testPartialFormSubmissionExistingField() |
||
90 | { |
||
91 | $values = [ |
||
92 | 'Field1' => 'Value1', |
||
93 | 'Field2' => 'Value2', |
||
94 | 'Field3' => 'null' |
||
95 | ]; |
||
96 | $id = $this->savePartial($values); |
||
97 | $field3 = PartialFieldSubmission::get() |
||
98 | ->filter([ |
||
99 | 'Name' => 'Field3', |
||
100 | 'SubmittedFormID' => $id |
||
101 | ]) |
||
102 | ->first(); |
||
103 | |||
104 | $this->assertEquals('null', $field3->Value); |
||
105 | |||
106 | // Update the values |
||
107 | $values['Field3'] = 'Value3'; |
||
108 | $id = $this->savePartial($values); |
||
109 | $field3 = PartialFieldSubmission::get() |
||
110 | ->filter([ |
||
111 | 'Name' => 'Field3', |
||
112 | 'SubmittedFormID' => $id |
||
113 | ]) |
||
114 | ->first(); |
||
115 | $this->assertEquals('Value3', $field3->Value); |
||
116 | } |
||
117 | |||
118 | public function testSubmittedFieldName() |
||
119 | { |
||
120 | $values = [ |
||
121 | 'Field1' => 'Value1', |
||
122 | 'Field2' => 'Value2', |
||
123 | 'Field3' => 'null' |
||
124 | ]; |
||
125 | $id = $this->savePartial($values); |
||
126 | /** @var DataList|PartialFieldSubmission[] $fields */ |
||
127 | $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]); |
||
128 | $this->assertCount(3, $fields); |
||
129 | |||
130 | foreach ($fields as $key => $field) { |
||
131 | $this->assertEquals('Field' . ($key + 1), $field->Name, 'Test field ' . $field->Name); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | public function testParent() |
||
136 | { |
||
137 | $values = [ |
||
138 | 'Field1' => 'Value1', |
||
139 | 'Field2' => 'Value2', |
||
140 | 'Field3' => 'null' |
||
141 | ]; |
||
142 | $id = $this->savePartial($values); |
||
143 | /** @var DataList|PartialFieldSubmission[] $fields */ |
||
144 | $partialForm = PartialFormSubmission::get()->byID($id); |
||
145 | |||
146 | // No parent class |
||
147 | $this->assertEquals(UserDefinedForm::class, $partialForm->ParentClass); |
||
148 | |||
149 | $form = UserDefinedForm::create(['Title' => 'Test']); |
||
150 | $formID = $form->write(); |
||
151 | |||
152 | // With parent class |
||
153 | $partialForm->ParentID = $formID; |
||
154 | $partialForm->ParentClass = get_class($form); |
||
155 | |||
156 | $this->assertEquals(UserDefinedForm::class, $partialForm->ParentClass); |
||
157 | } |
||
158 | |||
159 | public function testUnwantedFields() |
||
160 | { |
||
161 | $values = [ |
||
162 | 'Field1' => 'Value1', |
||
163 | 'Field2' => 'Value2', |
||
164 | 'Field3' => 'null', |
||
165 | 'SecurityID' => '123456789aoeu', |
||
166 | 'action_process' => 'Submit' |
||
167 | ]; |
||
168 | $id = $this->savePartial($values); |
||
169 | /** @var DataList|PartialFieldSubmission[] $fields */ |
||
170 | $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]); |
||
171 | |||
172 | $items = $fields->column('Name'); |
||
173 | $this->assertFalse(in_array('SecurityID', $items, true)); |
||
174 | $this->assertFalse(in_array('action_process', $items, true)); |
||
175 | } |
||
176 | |||
177 | public function testArrayData() |
||
178 | { |
||
179 | $values = [ |
||
180 | 'Field1' => 'Value1', |
||
181 | 'Field2' => 'Value2', |
||
182 | 'Field3' => ['Value1', 'Value2'] |
||
183 | ]; |
||
184 | $id = $this->savePartial($values); |
||
185 | $field3 = PartialFieldSubmission::get() |
||
186 | ->filter([ |
||
187 | 'Name' => 'Field3', |
||
188 | 'SubmittedFormID' => $id |
||
189 | ]) |
||
190 | ->first(); |
||
191 | $this->assertEquals('Value1, Value2', $field3->Value); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @todo Remove skip test after implementation |
||
196 | */ |
||
197 | public function testSaveDataWithExpiredSession() |
||
198 | { |
||
199 | $this->markTestSkipped('Remove skip test once implementation is complete'); |
||
200 | |||
201 | $values = [ |
||
202 | 'Field1' => 'Value1', |
||
203 | 'Field2' => 'Value2', |
||
204 | 'Field3' => ['Value1', 'Value2'] |
||
205 | ]; |
||
206 | |||
207 | $id = $this->savePartial($values); |
||
208 | $this->assertInternalType('int', $id); |
||
209 | |||
210 | $partial = PartialFormSubmission::get()->byID($id); |
||
211 | $this->assertNotNull($partial); |
||
212 | |||
213 | // Now clear session and save |
||
214 | $this->session()->clear(PartialSubmissionController::SESSION_KEY); |
||
215 | $values['Field1'] = 'NEW VALUE'; |
||
216 | $newId = $this->savePartial($values); |
||
217 | $this->assertEquals($id, $newId); |
||
240 |