1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Core\Convert; |
7
|
|
|
use SilverStripe\Dev\FunctionalTest; |
8
|
|
|
use SilverStripe\Forms\DropdownField; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\Form; |
11
|
|
|
use SilverStripe\Forms\GridField\GridField; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
13
|
|
|
use SilverStripe\ORM\DB; |
14
|
|
|
use SilverStripe\Security\Member; |
15
|
|
|
use SilverStripe\UserForms\Extension\UserFormFieldEditorExtension; |
16
|
|
|
use SilverStripe\UserForms\Extension\UserFormValidator; |
17
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule; |
18
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
19
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField; |
20
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown; |
21
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup; |
22
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd; |
23
|
|
|
use SilverStripe\UserForms\Model\Recipient\EmailRecipient; |
24
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm; |
25
|
|
|
use SilverStripe\Versioned\Versioned; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package userforms |
29
|
|
|
*/ |
30
|
|
|
class UserDefinedFormTest extends FunctionalTest |
31
|
|
|
{ |
32
|
|
|
protected static $fixture_file = 'UserDefinedFormTest.yml'; |
33
|
|
|
|
34
|
|
|
protected static $required_extensions = [ |
35
|
|
|
UserDefinedForm::class => [UserFormFieldEditorExtension::class], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testRollbackToVersion() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->markTestSkipped( |
41
|
|
|
'UserDefinedForm::rollback() has not been implemented completely' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
// @todo |
45
|
|
|
$this->logInWithPermission('ADMIN'); |
46
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
47
|
|
|
|
48
|
|
|
$form->SubmitButtonText = 'Button Text'; |
49
|
|
|
$form->write(); |
50
|
|
|
$form->publishRecursive(); |
51
|
|
|
$origVersion = $form->Version; |
52
|
|
|
|
53
|
|
|
$form->SubmitButtonText = 'Updated Button Text'; |
54
|
|
|
$form->write(); |
55
|
|
|
$form->publishRecursive(); |
56
|
|
|
|
57
|
|
|
// check published site |
58
|
|
|
$updated = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID"); |
59
|
|
|
$this->assertEquals($updated->SubmitButtonText, 'Updated Button Text'); |
60
|
|
|
|
61
|
|
|
$form->doRollbackTo($origVersion); |
62
|
|
|
|
63
|
|
|
$orignal = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID"); |
64
|
|
|
$this->assertEquals($orignal->SubmitButtonText, 'Button Text'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testGetCMSFields() |
68
|
|
|
{ |
69
|
|
|
$this->logInWithPermission('ADMIN'); |
70
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
71
|
|
|
|
72
|
|
|
$fields = $form->getCMSFields(); |
73
|
|
|
|
74
|
|
|
$this->assertTrue($fields->dataFieldByName('Fields') !== null); |
75
|
|
|
$this->assertTrue($fields->dataFieldByName('EmailRecipients') != null); |
76
|
|
|
$this->assertTrue($fields->dataFieldByName('Submissions') != null); |
77
|
|
|
$this->assertTrue($fields->dataFieldByName('OnCompleteMessage') != null); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
public function testGetCMSFieldsShowInSummary() |
82
|
|
|
{ |
83
|
|
|
$this->logInWithPermission('ADMIN'); |
84
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'summary-rules-form'); |
85
|
|
|
|
86
|
|
|
$fields = $form->getCMSFields(); |
87
|
|
|
|
88
|
|
|
$this->assertInstanceOf(GridField::class, $fields->dataFieldByName('Submissions')); |
89
|
|
|
|
90
|
|
|
$submissionsgrid = $fields->dataFieldByName('Submissions'); |
91
|
|
|
$gridFieldDataColumns = $submissionsgrid->getConfig()->getComponentByType(GridFieldDataColumns::class); |
92
|
|
|
|
93
|
|
|
$summaryFields = $gridFieldDataColumns->getDisplayFields($submissionsgrid); |
94
|
|
|
|
95
|
|
|
$this->assertContains('SummaryShow', array_keys($summaryFields), 'Summary field not showing displayed field'); |
96
|
|
|
$this->assertNotContains('SummaryHide', array_keys($summaryFields), 'Summary field showing displayed field'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
public function testEmailRecipientPopup() |
101
|
|
|
{ |
102
|
|
|
$this->logInWithPermission('ADMIN'); |
103
|
|
|
|
104
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
105
|
|
|
|
106
|
|
|
$popup = new EmailRecipient(); |
107
|
|
|
$popup->FormID = $form->ID; |
|
|
|
|
108
|
|
|
|
109
|
|
|
$fields = $popup->getCMSFields(); |
110
|
|
|
|
111
|
|
|
$this->assertTrue($fields->dataFieldByName('EmailSubject') !== null); |
112
|
|
|
$this->assertTrue($fields->dataFieldByName('EmailFrom') !== null); |
113
|
|
|
$this->assertTrue($fields->dataFieldByName('EmailAddress') !== null); |
114
|
|
|
$this->assertTrue($fields->dataFieldByName('HideFormData') !== null); |
115
|
|
|
$this->assertTrue($fields->dataFieldByName('SendPlain') !== null); |
116
|
|
|
$this->assertTrue($fields->dataFieldByName('EmailBody') !== null); |
117
|
|
|
|
118
|
|
|
// add an email field, it should now add a or from X address picker |
119
|
|
|
$email = $this->objFromFixture(EditableEmailField::class, 'email-field'); |
120
|
|
|
$form->Fields()->add($email); |
|
|
|
|
121
|
|
|
|
122
|
|
|
$popup->write(); |
123
|
|
|
|
124
|
|
|
$fields = $popup->getCMSFields(); |
125
|
|
|
$this->assertThat($fields->dataFieldByName('SendEmailToFieldID'), $this->isInstanceOf(DropdownField::class)); |
126
|
|
|
|
127
|
|
|
// if the front end has checkboxs or dropdown they can select from that can also be used to send things |
128
|
|
|
$dropdown = $this->objFromFixture(EditableDropdown::class, 'department-dropdown'); |
129
|
|
|
$form->Fields()->add($dropdown); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$fields = $popup->getCMSFields(); |
132
|
|
|
$this->assertTrue($fields->dataFieldByName('SendEmailToFieldID') !== null); |
133
|
|
|
|
134
|
|
|
$popup->delete(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGetEmailBodyContent() |
138
|
|
|
{ |
139
|
|
|
$recipient = new EmailRecipient(); |
140
|
|
|
|
141
|
|
|
$emailBody = 'not html'; |
142
|
|
|
$emailBodyHtml = '<p>html</p>'; |
143
|
|
|
|
144
|
|
|
$recipient->EmailBody = $emailBody; |
|
|
|
|
145
|
|
|
$recipient->EmailBodyHtml = $emailBodyHtml; |
|
|
|
|
146
|
|
|
$recipient->write(); |
147
|
|
|
|
148
|
|
|
$this->assertEquals($recipient->SendPlain, 0); |
|
|
|
|
149
|
|
|
$this->assertEquals($recipient->getEmailBodyContent(), $emailBodyHtml); |
150
|
|
|
|
151
|
|
|
$recipient->SendPlain = 1; |
|
|
|
|
152
|
|
|
$recipient->write(); |
153
|
|
|
|
154
|
|
|
$this->assertEquals($recipient->getEmailBodyContent(), $emailBody); |
155
|
|
|
|
156
|
|
|
$recipient->delete(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testGetEmailTemplateDropdownValues() |
160
|
|
|
{ |
161
|
|
|
$recipient = new EmailRecipient(); |
162
|
|
|
|
163
|
|
|
$defaultValues = ['email/SubmittedFormEmail' => 'SubmittedFormEmail']; |
164
|
|
|
|
165
|
|
|
$this->assertEquals($recipient->getEmailTemplateDropdownValues(), $defaultValues); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testEmailTemplateExists() |
169
|
|
|
{ |
170
|
|
|
$recipient = new EmailRecipient(); |
171
|
|
|
|
172
|
|
|
// Set the default template |
173
|
|
|
$recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues())); |
|
|
|
|
174
|
|
|
$recipient->write(); |
175
|
|
|
|
176
|
|
|
// The default template exists |
177
|
|
|
$this->assertTrue($recipient->emailTemplateExists()); |
178
|
|
|
|
179
|
|
|
// A made up template doesn't exists |
180
|
|
|
$this->assertFalse($recipient->emailTemplateExists('MyTemplateThatsNotThere')); |
181
|
|
|
|
182
|
|
|
$recipient->delete(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testCanEditAndDeleteRecipient() |
186
|
|
|
{ |
187
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
188
|
|
|
|
189
|
|
|
$this->logInWithPermission('ADMIN'); |
190
|
|
|
foreach ($form->EmailRecipients() as $recipient) { |
191
|
|
|
$this->assertTrue($recipient->canEdit()); |
192
|
|
|
$this->assertTrue($recipient->canDelete()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$member = Member::currentUser(); |
|
|
|
|
196
|
|
|
$member->logOut(); |
197
|
|
|
|
198
|
|
|
$this->logInWithPermission('SITETREE_VIEW_ALL'); |
199
|
|
|
foreach ($form->EmailRecipients() as $recipient) { |
200
|
|
|
$this->assertFalse($recipient->canEdit()); |
201
|
|
|
$this->assertFalse($recipient->canDelete()); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testPublishing() |
206
|
|
|
{ |
207
|
|
|
$this->logInWithPermission('ADMIN'); |
208
|
|
|
|
209
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
210
|
|
|
$form->write(); |
211
|
|
|
|
212
|
|
|
$form->publishRecursive(); |
213
|
|
|
|
214
|
|
|
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID"); |
215
|
|
|
|
216
|
|
|
$this->assertNotNull($live); |
217
|
|
|
$this->assertEquals(2, $live->Fields()->Count()); // one page and one field |
|
|
|
|
218
|
|
|
|
219
|
|
|
$dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown'); |
220
|
|
|
$form->Fields()->add($dropdown); |
|
|
|
|
221
|
|
|
|
222
|
|
|
$stage = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID"); |
223
|
|
|
$this->assertEquals(3, $stage->Fields()->Count()); |
|
|
|
|
224
|
|
|
|
225
|
|
|
// should not have published the dropdown |
226
|
|
|
$liveDropdown = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $dropdown->ID"); |
227
|
|
|
$this->assertNull($liveDropdown); |
228
|
|
|
|
229
|
|
|
// when publishing it should have added it |
230
|
|
|
$form->publishRecursive(); |
231
|
|
|
|
232
|
|
|
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID"); |
233
|
|
|
$this->assertEquals(3, $live->Fields()->Count()); |
|
|
|
|
234
|
|
|
|
235
|
|
|
// edit the title |
236
|
|
|
$text = $form->Fields()->limit(1, 1)->First(); |
|
|
|
|
237
|
|
|
$text->Title = 'Edited title'; |
238
|
|
|
$text->write(); |
239
|
|
|
|
240
|
|
|
$liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID"); |
241
|
|
|
$this->assertFalse($liveText->Title == $text->Title); |
242
|
|
|
|
243
|
|
|
$form->publishRecursive(); |
244
|
|
|
|
245
|
|
|
$liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID"); |
246
|
|
|
$this->assertTrue($liveText->Title == $text->Title); |
247
|
|
|
|
248
|
|
|
// Add a display rule to the dropdown |
249
|
|
|
$displayRule = new EditableCustomRule(); |
250
|
|
|
$displayRule->ParentID = $dropdown->ID; |
|
|
|
|
251
|
|
|
$displayRule->ConditionFieldID = $text->ID; |
|
|
|
|
252
|
|
|
$displayRule->write(); |
253
|
|
|
$ruleID = $displayRule->ID; |
254
|
|
|
|
255
|
|
|
// Not live |
256
|
|
|
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID"); |
257
|
|
|
$this->assertEmpty($liveRule); |
258
|
|
|
|
259
|
|
|
// Publish form, it's now live |
260
|
|
|
$form->publishRecursive(); |
261
|
|
|
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID"); |
262
|
|
|
$this->assertNotEmpty($liveRule); |
263
|
|
|
|
264
|
|
|
// Remove rule |
265
|
|
|
$displayRule->delete(); |
266
|
|
|
|
267
|
|
|
// Live rule still exists |
268
|
|
|
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID"); |
269
|
|
|
$this->assertNotEmpty($liveRule); |
270
|
|
|
|
271
|
|
|
// Publish form, it should remove this rule |
272
|
|
|
/** |
273
|
|
|
* @todo Currently failing, revisit once https://github.com/silverstripe/silverstripe-versioned/issues/34 is resolved |
274
|
|
|
*/ |
275
|
|
|
// $form->publishRecursive(); |
|
|
|
|
276
|
|
|
// $liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID"); |
|
|
|
|
277
|
|
|
// $this->assertEmpty($liveRule); |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function testUnpublishing() |
281
|
|
|
{ |
282
|
|
|
$this->logInWithPermission('ADMIN'); |
283
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
284
|
|
|
$form->write(); |
285
|
|
|
$this->assertEquals(0, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value()); |
286
|
|
|
$form->publishRecursive(); |
287
|
|
|
|
288
|
|
|
// assert that it exists and has a field |
289
|
|
|
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID"); |
290
|
|
|
|
291
|
|
|
$this->assertTrue(isset($live)); |
292
|
|
|
$this->assertEquals(2, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value()); |
293
|
|
|
|
294
|
|
|
// unpublish |
295
|
|
|
$form->doUnpublish(); |
296
|
|
|
|
297
|
|
|
$this->assertNull(Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID")); |
298
|
|
|
$this->assertEquals(0, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value()); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
View Code Duplication |
public function testDoRevertToLive() |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
$this->logInWithPermission('ADMIN'); |
304
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
305
|
|
|
$field = $form->Fields()->First(); |
|
|
|
|
306
|
|
|
|
307
|
|
|
$field->Title = 'Title'; |
308
|
|
|
$field->write(); |
309
|
|
|
|
310
|
|
|
$form->publishRecursive(); |
311
|
|
|
|
312
|
|
|
$field->Title = 'Edited title'; |
313
|
|
|
$field->write(); |
314
|
|
|
|
315
|
|
|
// check that the published version is not updated |
316
|
|
|
$live = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $field->ID"); |
317
|
|
|
$this->assertInstanceOf(EditableFormField::class, $live); |
318
|
|
|
$this->assertEquals('Title', $live->Title); |
319
|
|
|
|
320
|
|
|
// revert back to the live data |
321
|
|
|
$form->doRevertToLive(); |
322
|
|
|
$form->flushCache(); |
323
|
|
|
|
324
|
|
|
$check = Versioned::get_one_by_stage(EditableFormField::class, 'Stage', "\"EditableFormField\".\"ID\" = $field->ID"); |
325
|
|
|
|
326
|
|
|
$this->assertEquals('Title', $check->Title); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function testDuplicatingForm() |
330
|
|
|
{ |
331
|
|
|
$this->logInWithPermission('ADMIN'); |
332
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
333
|
|
|
|
334
|
|
|
$duplicate = $form->duplicate(); |
335
|
|
|
|
336
|
|
|
$this->assertEquals($form->Fields()->Count(), $duplicate->Fields()->Count()); |
|
|
|
|
337
|
|
|
|
338
|
|
|
// can't compare object since the dates/ids change |
339
|
|
|
$this->assertEquals($form->Fields()->First()->Title, $duplicate->Fields()->First()->Title); |
|
|
|
|
340
|
|
|
|
341
|
|
|
// Test duplicate with group |
342
|
|
|
$form2 = $this->objFromFixture(UserDefinedForm::class, 'page-with-group'); |
343
|
|
|
$form2Validator = new UserFormValidator(); |
344
|
|
|
$form2Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList())); |
345
|
|
|
$this->assertTrue($form2Validator->php($form2->toMap())); |
346
|
|
|
|
347
|
|
|
// Check field groups exist |
348
|
|
|
$form2GroupStart = $form2->Fields()->filter('ClassName', EditableFieldGroup::class)->first(); |
|
|
|
|
349
|
|
|
$form2GroupEnd = $form2->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first(); |
|
|
|
|
350
|
|
|
$this->assertEquals($form2GroupEnd->ID, $form2GroupStart->EndID); |
351
|
|
|
|
352
|
|
|
// Duplicate this |
353
|
|
|
$form3 = $form2->duplicate(); |
354
|
|
|
$form3Validator = new UserFormValidator(); |
355
|
|
|
$form3Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList())); |
356
|
|
|
$this->assertTrue($form3Validator->php($form3->toMap())); |
357
|
|
|
// Check field groups exist |
358
|
|
|
$form3GroupStart = $form3->Fields()->filter('ClassName', EditableFieldGroup::class)->first(); |
359
|
|
|
$form3GroupEnd = $form3->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first(); |
360
|
|
|
$this->assertEquals($form3GroupEnd->ID, $form3GroupStart->EndID); |
361
|
|
|
$this->assertNotEquals($form2GroupEnd->ID, $form3GroupStart->EndID); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function testFormOptions() |
365
|
|
|
{ |
366
|
|
|
$this->logInWithPermission('ADMIN'); |
367
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
368
|
|
|
|
369
|
|
|
$fields = $form->getFormOptions(); |
370
|
|
|
$submit = $fields->fieldByName('SubmitButtonText'); |
371
|
|
|
$reset = $fields->fieldByName('ShowClearButton'); |
372
|
|
|
|
373
|
|
|
$this->assertEquals($submit->Title(), 'Text on submit button:'); |
374
|
|
|
$this->assertEquals($reset->Title(), 'Show Clear Form Button'); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function testEmailRecipientFilters() |
378
|
|
|
{ |
379
|
|
|
/** @var UserDefinedForm $form */ |
380
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'filtered-form-page'); |
381
|
|
|
|
382
|
|
|
// Check unfiltered recipients |
383
|
|
|
$result0 = $form |
384
|
|
|
->EmailRecipients() |
385
|
|
|
->sort('EmailAddress') |
386
|
|
|
->column('EmailAddress'); |
387
|
|
|
$this->assertEquals( |
388
|
|
|
[ |
389
|
|
|
'[email protected]', |
390
|
|
|
'[email protected]', |
391
|
|
|
'[email protected]' |
392
|
|
|
], |
393
|
|
|
$result0 |
394
|
|
|
); |
395
|
|
|
|
396
|
|
|
// check filters based on given data |
397
|
|
|
$result1 = $form->FilteredEmailRecipients( |
398
|
|
|
[ |
399
|
|
|
'your-name' => 'Value', |
400
|
|
|
'address' => '', |
401
|
|
|
'street' => 'Anything', |
402
|
|
|
'city' => 'Matches Not Equals', |
403
|
|
|
'colours' => ['Red'] // matches 2 |
404
|
|
|
], |
405
|
|
|
null |
406
|
|
|
) |
407
|
|
|
->sort('EmailAddress') |
408
|
|
|
->column('EmailAddress'); |
409
|
|
|
$this->assertEquals( |
410
|
|
|
[ |
411
|
|
|
'[email protected]', |
412
|
|
|
'[email protected]' |
413
|
|
|
], |
414
|
|
|
$result1 |
415
|
|
|
); |
416
|
|
|
|
417
|
|
|
// Check all positive matches |
418
|
|
|
$result2 = $form->FilteredEmailRecipients( |
419
|
|
|
[ |
420
|
|
|
'your-name' => '', |
421
|
|
|
'address' => 'Anything', |
422
|
|
|
'street' => 'Matches Equals', |
423
|
|
|
'city' => 'Anything', |
424
|
|
|
'colours' => ['Red', 'Blue'] // matches 2 |
425
|
|
|
], |
426
|
|
|
null |
427
|
|
|
) |
428
|
|
|
->sort('EmailAddress') |
429
|
|
|
->column('EmailAddress'); |
430
|
|
|
$this->assertEquals( |
431
|
|
|
[ |
432
|
|
|
'[email protected]', |
433
|
|
|
'[email protected]', |
434
|
|
|
'[email protected]' |
435
|
|
|
], |
436
|
|
|
$result2 |
437
|
|
|
); |
438
|
|
|
|
439
|
|
|
$result3 = $form->FilteredEmailRecipients( |
440
|
|
|
[ |
441
|
|
|
'your-name' => 'Should be blank but is not', |
442
|
|
|
'address' => 'Anything', |
443
|
|
|
'street' => 'Matches Equals', |
444
|
|
|
'city' => 'Anything', |
445
|
|
|
'colours' => ['Blue'] |
446
|
|
|
], |
447
|
|
|
null |
448
|
|
|
)->column('EmailAddress'); |
449
|
|
|
$this->assertEquals( |
450
|
|
|
[ |
451
|
|
|
'[email protected]' |
452
|
|
|
], |
453
|
|
|
$result3 |
454
|
|
|
); |
455
|
|
|
|
456
|
|
|
|
457
|
|
|
$result4 = $form->FilteredEmailRecipients( |
458
|
|
|
[ |
459
|
|
|
'your-name' => '', |
460
|
|
|
'address' => 'Anything', |
461
|
|
|
'street' => 'Wrong value for this field', |
462
|
|
|
'city' => '', |
463
|
|
|
'colours' => ['Blue', 'Green'] |
464
|
|
|
], |
465
|
|
|
null |
466
|
|
|
)->column('EmailAddress'); |
467
|
|
|
$this->assertEquals( |
468
|
|
|
['[email protected]'], |
469
|
|
|
$result4 |
470
|
|
|
); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
public function testIndex() |
474
|
|
|
{ |
475
|
|
|
// Test that the $UserDefinedForm is stripped out |
476
|
|
|
$page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page'); |
477
|
|
|
$page->publish('Stage', 'Live'); |
478
|
|
|
|
479
|
|
|
$result = $this->get($page->Link()); |
480
|
|
|
$body = Convert::nl2os($result->getBody(), ''); // strip out newlines |
481
|
|
|
$this->assertFalse($result->isError()); |
482
|
|
|
$this->assertContains('<p>Here is my form</p><form', $body); |
483
|
|
|
$this->assertContains('</form><p>Thank you for filling it out</p>', $body); |
484
|
|
|
|
485
|
|
|
$this->assertNotContains('<p>$UserDefinedForm</p>', $body); |
486
|
|
|
$this->assertNotContains('<p></p>', $body); |
487
|
|
|
$this->assertNotContains('</p><p>Thank you for filling it out</p>', $body); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function testEmailAddressValidation() |
491
|
|
|
{ |
492
|
|
|
$this->logInWithPermission('ADMIN'); |
493
|
|
|
|
494
|
|
|
// test invalid email addresses fail validation |
495
|
|
|
$recipient = $this->objFromFixture( |
496
|
|
|
EmailRecipient::class, |
497
|
|
|
'invalid-recipient-list' |
498
|
|
|
); |
499
|
|
|
$result = $recipient->validate(); |
500
|
|
|
$this->assertFalse($result->isValid()); |
501
|
|
|
$this->assertNotEmpty($result->getMessages()); |
502
|
|
|
$this->assertContains('filtered.example.com', $result->getMessages()[0]['message']); |
503
|
|
|
$this->assertNotContains('[email protected]', $result->getMessages()[0]['message']); |
504
|
|
|
|
505
|
|
|
// test valid email addresses pass validation |
506
|
|
|
$recipient = $this->objFromFixture( |
507
|
|
|
EmailRecipient::class, |
508
|
|
|
'valid-recipient-list' |
509
|
|
|
); |
510
|
|
|
$result = $recipient->validate(); |
511
|
|
|
$this->assertTrue($result->isValid()); |
512
|
|
|
$this->assertEmpty($result->getMessages()); |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.