1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Tests; |
4
|
|
|
|
5
|
|
|
use Firesphere\PartialUserforms\Controllers\PartialSubmissionController; |
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\Core\Injector\Injector; |
11
|
|
|
use SilverStripe\Dev\FunctionalTest; |
12
|
|
|
use SilverStripe\ORM\DataList; |
13
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField; |
14
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm; |
15
|
|
|
|
16
|
|
|
/** |
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
|
|
|
$multipart = [ |
64
|
|
|
'name' => 'file1', |
65
|
|
|
'contents' => fopen(__DIR__ . '/../fixtures/Hans-fullsize-sqr.png', 'rb'), |
66
|
|
|
'filename' => 'Hans-fullsize-sqr.png' |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$response = $this->savePartial(['File' => $multipart]); |
70
|
|
|
$this->assertNotNull(PartialFileFieldSubmission::get()->filter(['Name' => 'File'])); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testPartialFormSubmissionExists() |
74
|
|
|
{ |
75
|
|
|
// If successful, will return the SubmittedFormID from the session |
76
|
|
|
$id = $this->savePartial(['Field1' => 'Value1', 'Field2' => 'Value2']); |
77
|
|
|
|
78
|
|
|
// Second submission |
79
|
|
|
$secondId = $this->savePartial(['Field2' => 'Value2']); |
80
|
|
|
|
81
|
|
|
$this->assertEquals($id, $secondId); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testPartialFormSubmissionExistingField() |
85
|
|
|
{ |
86
|
|
|
$values = [ |
87
|
|
|
'Field1' => 'Value1', |
88
|
|
|
'Field2' => 'Value2', |
89
|
|
|
'Field3' => 'null' |
90
|
|
|
]; |
91
|
|
|
$id = $this->savePartial($values); |
92
|
|
|
$field3 = PartialFieldSubmission::get() |
93
|
|
|
->filter([ |
94
|
|
|
'Name' => 'Field3', |
95
|
|
|
'SubmittedFormID' => $id |
96
|
|
|
]) |
97
|
|
|
->first(); |
98
|
|
|
|
99
|
|
|
$this->assertEquals('null', $field3->Value); |
100
|
|
|
|
101
|
|
|
// Update the values |
102
|
|
|
$values['Field3'] = 'Value3'; |
103
|
|
|
$id = $this->savePartial($values); |
104
|
|
|
$field3 = PartialFieldSubmission::get() |
105
|
|
|
->filter([ |
106
|
|
|
'Name' => 'Field3', |
107
|
|
|
'SubmittedFormID' => $id |
108
|
|
|
]) |
109
|
|
|
->first(); |
110
|
|
|
$this->assertEquals('Value3', $field3->Value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testSubmittedFieldName() |
114
|
|
|
{ |
115
|
|
|
$values = [ |
116
|
|
|
'Field1' => 'Value1', |
117
|
|
|
'Field2' => 'Value2', |
118
|
|
|
'Field3' => 'null' |
119
|
|
|
]; |
120
|
|
|
$id = $this->savePartial($values); |
121
|
|
|
/** @var DataList|PartialFieldSubmission[] $fields */ |
122
|
|
|
$fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]); |
123
|
|
|
$this->assertCount(3, $fields); |
124
|
|
|
|
125
|
|
|
foreach ($fields as $key => $field) { |
126
|
|
|
$this->assertEquals('Field' . ($key + 1), $field->Name, 'Test field ' . $field->Name); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testParent() |
131
|
|
|
{ |
132
|
|
|
$values = [ |
133
|
|
|
'Field1' => 'Value1', |
134
|
|
|
'Field2' => 'Value2', |
135
|
|
|
'Field3' => 'null' |
136
|
|
|
]; |
137
|
|
|
$id = $this->savePartial($values); |
138
|
|
|
/** @var DataList|PartialFieldSubmission[] $fields */ |
139
|
|
|
$partialForm = PartialFormSubmission::get()->byID($id); |
140
|
|
|
|
141
|
|
|
// No parent class |
142
|
|
|
$this->assertEquals(UserDefinedForm::class, $partialForm->ParentClass); |
143
|
|
|
|
144
|
|
|
$form = UserDefinedForm::create(['Title' => 'Test']); |
145
|
|
|
$formID = $form->write(); |
146
|
|
|
|
147
|
|
|
// With parent class |
148
|
|
|
$partialForm->ParentID = $formID; |
149
|
|
|
$partialForm->ParentClass = get_class($form); |
150
|
|
|
|
151
|
|
|
$this->assertEquals(UserDefinedForm::class, $partialForm->ParentClass); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testUnwantedFields() |
155
|
|
|
{ |
156
|
|
|
$values = [ |
157
|
|
|
'Field1' => 'Value1', |
158
|
|
|
'Field2' => 'Value2', |
159
|
|
|
'Field3' => 'null', |
160
|
|
|
'SecurityID' => '123456789aoeu', |
161
|
|
|
'action_process' => 'Submit' |
162
|
|
|
]; |
163
|
|
|
$id = $this->savePartial($values); |
164
|
|
|
/** @var DataList|PartialFieldSubmission[] $fields */ |
165
|
|
|
$fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]); |
166
|
|
|
|
167
|
|
|
$items = $fields->column('Name'); |
168
|
|
|
$this->assertFalse(in_array('SecurityID', $items, true)); |
169
|
|
|
$this->assertFalse(in_array('action_process', $items, true)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testArrayData() |
173
|
|
|
{ |
174
|
|
|
$values = [ |
175
|
|
|
'Field1' => 'Value1', |
176
|
|
|
'Field2' => 'Value2', |
177
|
|
|
'Field3' => ['Value1', 'Value2'] |
178
|
|
|
]; |
179
|
|
|
$id = $this->savePartial($values); |
180
|
|
|
$field3 = PartialFieldSubmission::get() |
181
|
|
|
->filter([ |
182
|
|
|
'Name' => 'Field3', |
183
|
|
|
'SubmittedFormID' => $id |
184
|
|
|
]) |
185
|
|
|
->first(); |
186
|
|
|
$this->assertEquals('Value1, Value2', $field3->Value); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @todo Remove skip test after implementation |
191
|
|
|
*/ |
192
|
|
|
public function testSaveDataWithExpiredSession() |
193
|
|
|
{ |
194
|
|
|
$this->markTestSkipped('Remove skip test once implementation is complete'); |
195
|
|
|
|
196
|
|
|
$values = [ |
197
|
|
|
'Field1' => 'Value1', |
198
|
|
|
'Field2' => 'Value2', |
199
|
|
|
'Field3' => ['Value1', 'Value2'] |
200
|
|
|
]; |
201
|
|
|
|
202
|
|
|
$id = $this->savePartial($values); |
203
|
|
|
$this->assertInternalType('int', $id); |
204
|
|
|
|
205
|
|
|
$partial = PartialFormSubmission::get()->byID($id); |
206
|
|
|
$this->assertNotNull($partial); |
207
|
|
|
|
208
|
|
|
// Now clear session and save |
209
|
|
|
$this->session()->clear(PartialSubmissionController::SESSION_KEY); |
210
|
|
|
$values['Field1'] = 'NEW VALUE'; |
211
|
|
|
$newId = $this->savePartial($values); |
212
|
|
|
$this->assertEquals($id, $newId); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Helper function for saving partial submission |
217
|
|
|
* @param array $values |
218
|
|
|
* @return mixed Returns SubmittedFormID on success, otherwise null |
219
|
|
|
*/ |
220
|
|
|
private function savePartial($values) |
221
|
|
|
{ |
222
|
|
|
$response = $this->post('/partialuserform/save', $values); |
223
|
|
|
$this->assertEquals(201, $response->getStatusCode()); |
224
|
|
|
|
225
|
|
|
return $this->session()->get(PartialSubmissionController::SESSION_KEY); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function setUp() |
229
|
|
|
{ |
230
|
|
|
parent::setUp(); |
231
|
|
|
$this->objFromFixture(UserDefinedForm::class, 'form1')->publishRecursive(); |
232
|
|
|
$this->controller = Injector::inst()->get(PartialSubmissionController::class); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|