1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package userforms |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
class UserDefinedFormControllerTest extends FunctionalTest { |
|
|
|
|
8
|
|
|
|
9
|
|
|
static $fixture_file = 'UserDefinedFormTest.yml'; |
|
|
|
|
10
|
|
|
|
11
|
|
|
public function testProcess() { |
12
|
|
|
$form = $this->setupFormFrontend(); |
13
|
|
|
|
14
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form); |
|
|
|
|
15
|
|
|
|
16
|
|
|
$this->autoFollowRedirection = false; |
17
|
|
|
$this->clearEmails(); |
18
|
|
|
|
19
|
|
|
// load the form |
20
|
|
|
$this->get($form->URLSegment); |
21
|
|
|
|
22
|
|
|
$field = $this->objFromFixture('EditableTextField', 'basic-text'); |
23
|
|
|
|
24
|
|
|
$response = $this->submitForm('UserForm_Form', null, array($field->Name => 'Basic Value')); |
25
|
|
|
|
26
|
|
|
// should have a submitted form field now |
27
|
|
|
$submitted = DataObject::get('SubmittedFormField', "\"Name\" = 'basic-text-name'"); |
28
|
|
|
$this->assertDOSAllMatch(array('Name' => 'basic-text-name', 'Value' => 'Basic Value', 'Title' => 'Basic Text Field'), $submitted); |
29
|
|
|
|
30
|
|
|
// check emails |
31
|
|
|
$this->assertEmailSent('[email protected]', '[email protected]', 'Email Subject'); |
32
|
|
|
$email = $this->findEmail('[email protected]', '[email protected]', 'Email Subject'); |
33
|
|
|
|
34
|
|
|
// assert that the email has the field title and the value html email |
35
|
|
|
$parser = new CSSContentParser($email['content']); |
36
|
|
|
$title = $parser->getBySelector('strong'); |
37
|
|
|
|
38
|
|
|
$this->assertEquals('Basic Text Field', (string) $title[0], 'Email contains the field name'); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$value = $parser->getBySelector('dd'); |
41
|
|
|
$this->assertEquals('Basic Value', (string) $value[0], 'Email contains the value'); |
|
|
|
|
42
|
|
|
|
43
|
|
|
// no html |
44
|
|
|
$this->assertEmailSent('[email protected]', '[email protected]', 'Email Subject'); |
45
|
|
|
$nohtml = $this->findEmail('[email protected]', '[email protected]', 'Email Subject'); |
46
|
|
|
|
47
|
|
|
$this->assertContains('Basic Text Field: Basic Value', $nohtml['content'], 'Email contains no html'); |
48
|
|
|
|
49
|
|
|
// no data |
50
|
|
|
$this->assertEmailSent('[email protected]', '[email protected]', 'Email Subject'); |
51
|
|
|
$nodata = $this->findEmail('[email protected]', '[email protected]', 'Email Subject'); |
52
|
|
|
|
53
|
|
|
$parser = new CSSContentParser($nodata['content']); |
54
|
|
|
$list = $parser->getBySelector('dl'); |
55
|
|
|
|
56
|
|
|
$this->assertFalse(isset($list[0]), 'Email contains no fields'); |
|
|
|
|
57
|
|
|
|
58
|
|
|
// check to see if the user was redirected (301) |
59
|
|
|
$this->assertEquals($response->getStatusCode(), 302); |
|
|
|
|
60
|
|
|
$this->assertStringEndsWith('finished#uff', $response->getHeader('Location')); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testValidation() { |
64
|
|
|
$form = $this->setupFormFrontend('email-form'); |
65
|
|
|
|
66
|
|
|
// Post with no fields |
67
|
|
|
$this->get($form->URLSegment); |
68
|
|
|
$response = $this->submitForm('UserForm_Form', null, array()); |
|
|
|
|
69
|
|
|
$this->assertPartialMatchBySelector( |
70
|
|
|
'.field .message', |
71
|
|
|
array('This field is required') |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
// Post with all fields, but invalid email |
75
|
|
|
$this->get($form->URLSegment); |
76
|
|
|
$this->submitForm('UserForm_Form', null, array( |
77
|
|
|
'required-email' => 'invalid', |
78
|
|
|
'required-text' => 'bob' |
79
|
|
|
)); |
80
|
|
|
$this->assertPartialMatchBySelector( |
81
|
|
|
'.field .message', |
82
|
|
|
array('Please enter an email address') |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
// Post with only required |
86
|
|
|
$this->get($form->URLSegment); |
87
|
|
|
$this->submitForm('UserForm_Form', null, array( |
88
|
|
|
'required-text' => 'bob' |
89
|
|
|
)); |
90
|
|
|
$this->assertPartialMatchBySelector( |
91
|
|
|
'p', |
92
|
|
|
array("Thanks, we've received your submission.") |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testFinished() { |
|
|
|
|
97
|
|
|
$form = $this->setupFormFrontend(); |
98
|
|
|
|
99
|
|
|
// set formProcessed and SecurityID to replicate the form being filled out |
100
|
|
|
$this->session()->inst_set('SecurityID', 1); |
101
|
|
|
$this->session()->inst_set('FormProcessed', 1); |
102
|
|
|
|
103
|
|
|
$response = $this->get($form->URLSegment.'/finished'); |
104
|
|
|
|
105
|
|
|
$this->assertContains($form->OnCompleteMessage ,$response->getBody()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
public function testAppendingFinished() { |
|
|
|
|
109
|
|
|
$form = $this->setupFormFrontend(); |
110
|
|
|
|
111
|
|
|
// replicate finished being added to the end of the form URL without the form being filled out |
112
|
|
|
$this->session()->inst_set('SecurityID', 1); |
113
|
|
|
$this->session()->inst_set('FormProcessed', null); |
114
|
|
|
|
115
|
|
|
$response = $this->get($form->URLSegment.'/finished'); |
116
|
|
|
|
117
|
|
|
$this->assertNotContains($form->OnCompleteMessage ,$response->getBody()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testForm() { |
121
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page'); |
122
|
|
|
|
123
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form); |
124
|
|
|
|
125
|
|
|
// test form |
126
|
|
|
$this->assertEquals($controller->Form()->getName(), 'Form', 'The form is referenced as Form'); |
|
|
|
|
127
|
|
|
$this->assertEquals($controller->Form()->Fields()->Count(), 1); // disabled SecurityID token fields |
|
|
|
|
128
|
|
|
$this->assertEquals($controller->Form()->Actions()->Count(), 1); |
|
|
|
|
129
|
|
|
$this->assertEquals(count($controller->Form()->getValidator()->getRequired()), 0); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$requiredForm = $this->objFromFixture('UserDefinedForm', 'validation-form'); |
132
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($requiredForm); |
133
|
|
|
|
134
|
|
|
$this->assertEquals($controller->Form()->Fields()->Count(), 1); // disabled SecurityID token fields |
|
|
|
|
135
|
|
|
$this->assertEquals($controller->Form()->Actions()->Count(), 1); |
|
|
|
|
136
|
|
|
$this->assertEquals(count($controller->Form()->getValidator()->getRequired()), 1); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testGetFormFields() { |
140
|
|
|
// generating the fieldset of fields |
141
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page'); |
142
|
|
|
|
143
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form); |
144
|
|
|
|
145
|
|
|
$formSteps = $controller->Form()->getFormFields(); |
146
|
|
|
$firstStep = $formSteps->first(); |
147
|
|
|
|
148
|
|
|
$this->assertEquals($formSteps->Count(), 1); |
|
|
|
|
149
|
|
|
$this->assertEquals($firstStep->getChildren()->Count(), 1); |
|
|
|
|
150
|
|
|
|
151
|
|
|
// custom error message on a form field |
152
|
|
|
$requiredForm = $this->objFromFixture('UserDefinedForm', 'validation-form'); |
153
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($requiredForm); |
154
|
|
|
|
155
|
|
|
UserDefinedForm::config()->required_identifier = "*"; |
156
|
|
|
|
157
|
|
|
$formSteps = $controller->Form()->getFormFields(); |
158
|
|
|
$firstStep = $formSteps->first(); |
159
|
|
|
$firstField = $firstStep->getChildren()->first(); |
160
|
|
|
|
161
|
|
|
$this->assertEquals('Custom Error Message', $firstField->getCustomValidationMessage()); |
|
|
|
|
162
|
|
|
$this->assertEquals($firstField->Title(), 'Required Text Field <span class=\'required-identifier\'>*</span>'); |
|
|
|
|
163
|
|
|
|
164
|
|
|
// test custom right title |
165
|
|
|
$field = $form->Fields()->limit(1, 1)->First(); |
|
|
|
|
166
|
|
|
$field->RightTitle = 'Right Title'; |
167
|
|
|
$field->write(); |
168
|
|
|
|
169
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form); |
170
|
|
|
$formSteps = $controller->Form()->getFormFields(); |
171
|
|
|
$firstStep = $formSteps->first(); |
172
|
|
|
|
173
|
|
|
$this->assertEquals($firstStep->getChildren()->First()->RightTitle(), "Right Title"); |
|
|
|
|
174
|
|
|
|
175
|
|
|
// test empty form |
176
|
|
|
$emptyForm = $this->objFromFixture('UserDefinedForm', 'empty-form'); |
177
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($emptyForm); |
178
|
|
|
|
179
|
|
|
$this->assertFalse($controller->Form()->getFormFields()->exists()); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testGetFormActions() |
183
|
|
|
{ |
184
|
|
|
// generating the fieldset of actions |
185
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page'); |
186
|
|
|
|
187
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form); |
188
|
|
|
$actions = $controller->Form()->getFormActions(); |
189
|
|
|
|
190
|
|
|
// by default will have 1 submit button which links to process |
191
|
|
|
$expected = new FieldList(new FormAction('process', 'Submit')); |
192
|
|
|
$expected->setForm($controller->Form()); |
193
|
|
|
|
194
|
|
|
$this->assertEquals($actions, $expected); |
|
|
|
|
195
|
|
|
|
196
|
|
|
// the custom popup should have a reset button and a custom text |
197
|
|
|
$custom = $this->objFromFixture('UserDefinedForm', 'form-with-reset-and-custom-action'); |
198
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($custom); |
199
|
|
|
$actions = $controller->Form()->getFormActions(); |
200
|
|
|
|
201
|
|
|
$expected = new FieldList(new FormAction('process', 'Custom Button')); |
202
|
|
|
$expected->push(new ResetFormAction("clearForm", "Clear")); |
203
|
|
|
$expected->setForm($controller->Form()); |
204
|
|
|
|
205
|
|
|
$this->assertEquals($actions, $expected); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
View Code Duplication |
public function testRenderingIntoFormTemplate() { |
|
|
|
|
209
|
|
|
$form = $this->setupFormFrontend(); |
210
|
|
|
|
211
|
|
|
$form->Content = 'This is some content without a form nested between it'; |
212
|
|
|
$form->doPublish(); |
213
|
|
|
|
214
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form); |
215
|
|
|
|
216
|
|
|
// check to see if $Form is replaced to inside the content |
217
|
|
|
$index = new ArrayData($controller->index()); |
218
|
|
|
$parser = new CSSContentParser($index->renderWith(array('UserDefinedFormControllerTest'))); |
219
|
|
|
|
220
|
|
|
$this->checkTemplateIsCorrect($parser); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
View Code Duplication |
public function testRenderingIntoTemplateWithSubstringReplacement() { |
|
|
|
|
224
|
|
|
$form = $this->setupFormFrontend(); |
225
|
|
|
|
226
|
|
|
$controller = new UserDefinedFormControllerTest_Controller($form); |
227
|
|
|
|
228
|
|
|
// check to see if $Form is replaced to inside the content |
229
|
|
|
$index = new ArrayData($controller->index()); |
230
|
|
|
$parser = new CSSContentParser($index->renderWith(array('UserDefinedFormControllerTest'))); |
231
|
|
|
|
232
|
|
|
$this->checkTemplateIsCorrect($parser); |
233
|
|
|
} |
234
|
|
|
/** |
235
|
|
|
* Publish a form for use on the frontend |
236
|
|
|
* |
237
|
|
|
* @param string $fixtureName |
238
|
|
|
* @return UserDefinedForm |
239
|
|
|
*/ |
240
|
|
|
protected function setupFormFrontend($fixtureName = 'basic-form-page') { |
241
|
|
|
$form = $this->objFromFixture('UserDefinedForm', $fixtureName); |
242
|
|
|
$this->logInWithPermission('ADMIN'); |
243
|
|
|
|
244
|
|
|
$form->doPublish(); |
245
|
|
|
|
246
|
|
|
$member = Member::currentUser(); |
247
|
|
|
$member->logOut(); |
248
|
|
|
|
249
|
|
|
return $form; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function checkTemplateIsCorrect($parser) { |
253
|
|
|
$this->assertArrayHasKey(0, $parser->getBySelector('form#UserForm_Form')); |
|
|
|
|
254
|
|
|
|
255
|
|
|
// check for the input |
256
|
|
|
$this->assertArrayHasKey(0, $parser->getBySelector('input.text')); |
|
|
|
|
257
|
|
|
|
258
|
|
|
// check for the label and the text |
259
|
|
|
$label = $parser->getBySelector('label.left'); |
260
|
|
|
$this->assertArrayHasKey(0, $label); |
|
|
|
|
261
|
|
|
|
262
|
|
|
$this->assertEquals((string) $label[0][0], "Basic Text Field", "Label contains correct field name"); |
|
|
|
|
263
|
|
|
|
264
|
|
|
// check for the action |
265
|
|
|
$action = $parser->getBySelector('input.action'); |
266
|
|
|
$this->assertArrayHasKey(0, $action); |
|
|
|
|
267
|
|
|
|
268
|
|
|
$this->assertEquals((string) $action[0]['value'], "Submit", "Submit button has default text"); |
|
|
|
|
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
class UserDefinedFormControllerTest_Controller extends UserDefinedForm_Controller implements TestOnly { |
|
|
|
|
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Overloaded to avoid inconsistencies between 2.4.2 and 2.4.3 (disables all security tokens in unit tests by default) |
276
|
|
|
*/ |
277
|
|
|
public function Form() { |
278
|
|
|
$form = parent::Form(); |
279
|
|
|
|
280
|
|
|
if($form) $form->disableSecurityToken(); |
281
|
|
|
|
282
|
|
|
return $form; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.