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