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 | ]; |
||
71 | // Disable checking if the file is uploaded through a POST |
||
72 | Upload_Validator::config()->set('use_is_uploaded_file', false); |
||
73 | $this->savePartial(['File' => $multipart]); |
||
74 | $this->assertNotNull(PartialFileFieldSubmission::get()->filter(['Name' => 'File'])); |
||
75 | } |
||
76 | |||
77 | public function testPartialFormSubmissionExists() |
||
78 | { |
||
79 | // If successful, will return the SubmittedFormID from the session |
||
80 | $id = $this->savePartial(['Field1' => 'Value1', 'Field2' => 'Value2']); |
||
81 | |||
82 | // Second submission |
||
83 | $secondId = $this->savePartial(['Field2' => 'Value2']); |
||
84 | |||
85 | $this->assertEquals($id, $secondId); |
||
86 | } |
||
87 | |||
88 | public function testPartialFormSubmissionExistingField() |
||
89 | { |
||
90 | $values = [ |
||
91 | 'Field1' => 'Value1', |
||
92 | 'Field2' => 'Value2', |
||
93 | 'Field3' => 'null' |
||
94 | ]; |
||
95 | $id = $this->savePartial($values); |
||
96 | $field3 = PartialFieldSubmission::get() |
||
97 | ->filter([ |
||
98 | 'Name' => 'Field3', |
||
99 | 'SubmittedFormID' => $id |
||
100 | ]) |
||
101 | ->first(); |
||
102 | |||
103 | $this->assertEquals('null', $field3->Value); |
||
104 | |||
105 | // Update the values |
||
106 | $values['Field3'] = 'Value3'; |
||
107 | $id = $this->savePartial($values); |
||
108 | $field3 = PartialFieldSubmission::get() |
||
109 | ->filter([ |
||
110 | 'Name' => 'Field3', |
||
111 | 'SubmittedFormID' => $id |
||
112 | ]) |
||
113 | ->first(); |
||
114 | $this->assertEquals('Value3', $field3->Value); |
||
115 | } |
||
116 | |||
117 | public function testSubmittedFieldName() |
||
118 | { |
||
119 | $values = [ |
||
120 | 'Field1' => 'Value1', |
||
121 | 'Field2' => 'Value2', |
||
122 | 'Field3' => 'null' |
||
123 | ]; |
||
124 | $id = $this->savePartial($values); |
||
125 | /** @var DataList|PartialFieldSubmission[] $fields */ |
||
126 | $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]); |
||
127 | $this->assertCount(3, $fields); |
||
128 | |||
129 | foreach ($fields as $key => $field) { |
||
130 | $this->assertEquals('Field' . ($key + 1), $field->Name, 'Test field ' . $field->Name); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public function testParent() |
||
135 | { |
||
136 | $values = [ |
||
137 | 'Field1' => 'Value1', |
||
138 | 'Field2' => 'Value2', |
||
139 | 'Field3' => 'null' |
||
140 | ]; |
||
141 | $id = $this->savePartial($values); |
||
142 | /** @var DataList|PartialFieldSubmission[] $fields */ |
||
143 | $partialForm = PartialFormSubmission::get()->byID($id); |
||
144 | |||
145 | // No parent class |
||
146 | $this->assertEquals(UserDefinedForm::class, $partialForm->ParentClass); |
||
147 | |||
148 | $form = UserDefinedForm::create(['Title' => 'Test']); |
||
149 | $formID = $form->write(); |
||
150 | |||
151 | // With parent class |
||
152 | $partialForm->ParentID = $formID; |
||
153 | $partialForm->ParentClass = get_class($form); |
||
154 | |||
155 | $this->assertEquals(UserDefinedForm::class, $partialForm->ParentClass); |
||
156 | } |
||
157 | |||
158 | public function testUnwantedFields() |
||
159 | { |
||
160 | $values = [ |
||
161 | 'Field1' => 'Value1', |
||
162 | 'Field2' => 'Value2', |
||
163 | 'Field3' => 'null', |
||
164 | 'SecurityID' => '123456789aoeu', |
||
165 | 'action_process' => 'Submit' |
||
166 | ]; |
||
167 | $id = $this->savePartial($values); |
||
168 | /** @var DataList|PartialFieldSubmission[] $fields */ |
||
169 | $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]); |
||
170 | |||
171 | $items = $fields->column('Name'); |
||
172 | $this->assertFalse(in_array('SecurityID', $items, true)); |
||
173 | $this->assertFalse(in_array('action_process', $items, true)); |
||
174 | } |
||
175 | |||
176 | public function testArrayData() |
||
177 | { |
||
178 | $values = [ |
||
179 | 'Field1' => 'Value1', |
||
180 | 'Field2' => 'Value2', |
||
181 | 'Field3' => ['Value1', 'Value2'] |
||
182 | ]; |
||
183 | $id = $this->savePartial($values); |
||
184 | $field3 = PartialFieldSubmission::get() |
||
185 | ->filter([ |
||
186 | 'Name' => 'Field3', |
||
187 | 'SubmittedFormID' => $id |
||
188 | ]) |
||
189 | ->first(); |
||
190 | $this->assertEquals('Value1, Value2', $field3->Value); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @todo Remove skip test after implementation |
||
195 | */ |
||
196 | public function testSaveDataWithExpiredSession() |
||
197 | { |
||
198 | $this->markTestSkipped('Remove skip test once implementation is complete'); |
||
199 | |||
200 | $values = [ |
||
201 | 'Field1' => 'Value1', |
||
202 | 'Field2' => 'Value2', |
||
203 | 'Field3' => ['Value1', 'Value2'] |
||
204 | ]; |
||
205 | |||
206 | $id = $this->savePartial($values); |
||
207 | $this->assertInternalType('int', $id); |
||
208 | |||
209 | $partial = PartialFormSubmission::get()->byID($id); |
||
210 | $this->assertNotNull($partial); |
||
211 | |||
212 | // Now clear session and save |
||
213 | $this->session()->clear(PartialSubmissionController::SESSION_KEY); |
||
214 | $values['Field1'] = 'NEW VALUE'; |
||
215 | $newId = $this->savePartial($values); |
||
216 | $this->assertEquals($id, $newId); |
||
217 | } |
||
239 |