UserDefinedFormTest::testRollbackToVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.7333
1
<?php
2
3
namespace SilverStripe\UserForms\Tests\Model;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\Dev\FunctionalTest;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\Forms\GridField\GridField;
13
use SilverStripe\Forms\GridField\GridFieldDataColumns;
14
use SilverStripe\ORM\DB;
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\EditableDropdown;
20
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
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 $usesTransactions = false;
33
34
    protected static $fixture_file = '../UserFormsTest.yml';
35
36
    protected static $required_extensions = [
37
        UserDefinedForm::class => [UserFormFieldEditorExtension::class],
38
    ];
39
40
    protected function setUp()
41
    {
42
        parent::setUp();
43
        Email::config()->update('admin_email', '[email protected]');
44
    }
45
46
    public function testRollbackToVersion()
47
    {
48
        $this->markTestSkipped(
49
            'UserDefinedForm::rollback() has not been implemented completely'
50
        );
51
52
        // @todo
53
        $this->logInWithPermission('ADMIN');
54
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
55
56
        $form->SubmitButtonText = 'Button Text';
57
        $form->write();
58
        $form->publishRecursive();
59
        $origVersion = $form->Version;
60
61
        $form->SubmitButtonText = 'Updated Button Text';
62
        $form->write();
63
        $form->publishRecursive();
64
65
        // check published site
66
        $updated = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
67
        $this->assertEquals($updated->SubmitButtonText, 'Updated Button Text');
68
69
        $form->doRollbackTo($origVersion);
70
71
        $orignal = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
72
        $this->assertEquals($orignal->SubmitButtonText, 'Button Text');
73
    }
74
75
    public function testGetCMSFields()
76
    {
77
        $this->logInWithPermission('ADMIN');
78
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
79
80
        $fields = $form->getCMSFields();
81
82
        $this->assertNotNull($fields->dataFieldByName('Fields'));
83
        $this->assertNotNull($fields->dataFieldByName('EmailRecipients'));
84
        $this->assertNotNull($fields->dataFieldByName('Submissions'));
85
        $this->assertNotNull($fields->dataFieldByName('OnCompleteMessage'));
86
    }
87
88
89
    public function testGetCMSFieldsShowInSummary()
90
    {
91
        $this->logInWithPermission('ADMIN');
92
        $form = $this->objFromFixture(UserDefinedForm::class, 'summary-rules-form');
93
94
        $fields = $form->getCMSFields();
95
96
        $this->assertInstanceOf(GridField::class, $fields->dataFieldByName('Submissions'));
97
98
        $submissionsgrid = $fields->dataFieldByName('Submissions');
99
        $gridFieldDataColumns = $submissionsgrid->getConfig()->getComponentByType(GridFieldDataColumns::class);
100
101
        $summaryFields = $gridFieldDataColumns->getDisplayFields($submissionsgrid);
102
103
        $this->assertContains('SummaryShow', array_keys($summaryFields), 'Summary field not showing displayed field');
104
        $this->assertNotContains('SummaryHide', array_keys($summaryFields), 'Summary field showing displayed field');
105
    }
106
107
    public function testEmailRecipientPopup()
108
    {
109
        $this->logInWithPermission('ADMIN');
110
111
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
112
113
        $popup = new EmailRecipient();
114
        $popup->FormID = $form->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property FormID does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
115
        $popup->FormClass = UserDefinedForm::class;
0 ignored issues
show
Bug Best Practice introduced by
The property FormClass does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
116
        $popup->EmailAddress = '[email protected]';
0 ignored issues
show
Bug Best Practice introduced by
The property EmailAddress does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
117
118
        $fields = $popup->getCMSFields();
119
120
        $this->assertNotNull($fields->dataFieldByName('EmailSubject'));
121
        $this->assertNotNull($fields->dataFieldByName('EmailFrom'));
122
        $this->assertNotNull($fields->dataFieldByName('EmailAddress'));
123
        $this->assertNotNull($fields->dataFieldByName('HideFormData'));
124
        $this->assertNotNull($fields->dataFieldByName('SendPlain'));
125
        $this->assertNotNull($fields->dataFieldByName('EmailBody'));
126
127
        // add an email field, it should now add a or from X address picker
128
        $email = $this->objFromFixture(EditableEmailField::class, 'email-field');
129
        $form->Fields()->add($email);
130
131
        $popup->write();
132
133
        $fields = $popup->getCMSFields();
134
        $this->assertThat($fields->dataFieldByName('SendEmailToFieldID'), $this->isInstanceOf(DropdownField::class));
135
136
        // if the front end has checkboxes or dropdown they can select from that can also be used to send things
137
        $dropdown = $this->objFromFixture(EditableDropdown::class, 'department-dropdown');
138
        $form->Fields()->add($dropdown);
139
140
        $fields = $popup->getCMSFields();
141
        $this->assertTrue($fields->dataFieldByName('SendEmailToFieldID') !== null);
142
143
        $popup->delete();
144
    }
145
146
    public function testGetEmailBodyContent()
147
    {
148
        $recipient = new EmailRecipient();
149
        $recipient->EmailAddress = '[email protected]';
0 ignored issues
show
Bug Best Practice introduced by
The property EmailAddress does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
150
151
        $emailBody = 'not html';
152
        $emailBodyHtml = '<p>html</p>';
153
154
        $recipient->EmailBody = $emailBody;
0 ignored issues
show
Bug Best Practice introduced by
The property EmailBody does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
155
        $recipient->EmailBodyHtml = $emailBodyHtml;
0 ignored issues
show
Bug Best Practice introduced by
The property EmailBodyHtml does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
156
        $recipient->write();
157
158
        $this->assertEquals($recipient->SendPlain, 0);
0 ignored issues
show
Bug Best Practice introduced by
The property SendPlain does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __get, consider adding a @property annotation.
Loading history...
159
        $this->assertEquals($recipient->getEmailBodyContent(), $emailBodyHtml);
160
161
        $recipient->SendPlain = 1;
0 ignored issues
show
Bug Best Practice introduced by
The property SendPlain does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
162
        $recipient->write();
163
164
        $this->assertEquals($recipient->getEmailBodyContent(), $emailBody);
165
166
        $recipient->delete();
167
    }
168
169
    public function testGetEmailTemplateDropdownValues()
170
    {
171
        $page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
172
        $recipient = new EmailRecipient();
173
        $recipient->FormID = $page->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property FormID does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
174
        $recipient->FormClass = UserDefinedForm::class;
0 ignored issues
show
Bug Best Practice introduced by
The property FormClass does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
175
176
        $result = $recipient->getEmailTemplateDropdownValues();
177
178
        // Installation path can be as a project when testing in Travis, so check partial match
179
        $foundKey = false;
180
        foreach (array_keys($result) as $key) {
181
            if (strpos($key, 'email' . DIRECTORY_SEPARATOR . 'SubmittedFormEmail') !== false) {
182
                $foundKey = true;
183
            }
184
        }
185
        $this->assertTrue($foundKey);
186
        $this->assertTrue(in_array('SubmittedFormEmail', array_values($result)));
187
    }
188
189
    public function testEmailTemplateExists()
190
    {
191
        $page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
192
        $recipient = new EmailRecipient();
193
        $recipient->FormID = $page->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property FormID does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
194
        $recipient->FormClass = UserDefinedForm::class;
0 ignored issues
show
Bug Best Practice introduced by
The property FormClass does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
195
        $recipient->EmailAddress = '[email protected]';
0 ignored issues
show
Bug Best Practice introduced by
The property EmailAddress does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
196
197
        // Set the default template
198
        $recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues()));
0 ignored issues
show
Bug Best Practice introduced by
The property EmailTemplate does not exist on SilverStripe\UserForms\M...ecipient\EmailRecipient. Since you implemented __set, consider adding a @property annotation.
Loading history...
199
        $recipient->write();
200
201
        // The default template exists
202
        $this->assertTrue($recipient->emailTemplateExists());
203
204
        // A made up template doesn't exists
205
        $this->assertFalse($recipient->emailTemplateExists('MyTemplateThatsNotThere'));
206
207
        $recipient->delete();
208
    }
209
210
    public function testCanEditAndDeleteRecipient()
211
    {
212
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
213
214
        $this->logInWithPermission('ADMIN');
215
        foreach ($form->EmailRecipients() as $recipient) {
216
            $this->assertTrue($recipient->canEdit());
217
            $this->assertTrue($recipient->canDelete());
218
        }
219
220
        $this->logOut();
221
        $this->logInWithPermission('SITETREE_VIEW_ALL');
222
223
        foreach ($form->EmailRecipients() as $recipient) {
224
            $this->assertFalse($recipient->canEdit());
225
            $this->assertFalse($recipient->canDelete());
226
        }
227
    }
228
229
    public function testPublishing()
230
    {
231
        $this->logInWithPermission('ADMIN');
232
233
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
234
        $form->write();
235
236
        $form->publishRecursive();
237
238
        $live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
239
240
        $this->assertNotNull($live);
241
        $this->assertEquals(2, $live->Fields()->Count()); // one page and one field
242
243
        $dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
244
        $form->Fields()->add($dropdown);
245
246
        $stage = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
247
        $this->assertEquals(3, $stage->Fields()->Count());
248
249
        // should not have published the dropdown
250
        $liveDropdown = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $dropdown->ID");
251
        $this->assertNull($liveDropdown);
252
253
        // when publishing it should have added it
254
        $form->publishRecursive();
255
256
        $live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
257
        $this->assertEquals(3, $live->Fields()->Count());
258
259
        // edit the title
260
        $text = $form->Fields()->limit(1, 1)->First();
261
        $text->Title = 'Edited title';
262
        $text->write();
263
264
        $liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID");
265
        $this->assertFalse($liveText->Title == $text->Title);
266
267
        $form->publishRecursive();
268
269
        $liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID");
270
        $this->assertTrue($liveText->Title == $text->Title);
271
272
        // Add a display rule to the dropdown
273
        $displayRule = new EditableCustomRule();
274
        $displayRule->ParentID = $dropdown->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\UserForms\Model\EditableCustomRule. Since you implemented __set, consider adding a @property annotation.
Loading history...
275
        $displayRule->ConditionFieldID = $text->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ConditionFieldID does not exist on SilverStripe\UserForms\Model\EditableCustomRule. Since you implemented __set, consider adding a @property annotation.
Loading history...
276
        $displayRule->write();
277
        $ruleID = $displayRule->ID;
278
279
        // Not live
280
        $liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
281
        $this->assertEmpty($liveRule);
282
283
        // Publish form, it's now live
284
        $form->publishRecursive();
285
        $liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
286
        $this->assertNotEmpty($liveRule);
287
288
        // Remove rule
289
        $displayRule->delete();
290
291
        // Live rule still exists
292
        $liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
293
        $this->assertNotEmpty($liveRule);
294
295
        // Publish form, it should remove this rule
296
         $form->publishRecursive();
297
         $liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
298
         $this->assertEmpty($liveRule);
299
    }
300
301
    public function testUnpublishing()
302
    {
303
        $this->logInWithPermission('ADMIN');
304
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
305
        $form->write();
306
        $this->assertEquals(0, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
307
        $form->publishRecursive();
308
309
        // assert that it exists and has a field
310
        $live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID", false);
311
312
        $this->assertTrue(isset($live));
313
        $this->assertEquals(2, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
314
315
        // unpublish
316
        $form->doUnpublish();
317
318
        $this->assertNull(Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID", false));
319
        $this->assertEquals(0, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
320
    }
321
322
    public function testDoRevertToLive()
323
    {
324
        $this->logInWithPermission('ADMIN');
325
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
326
        $field = $form->Fields()->First();
327
328
        $field->Title = 'Title';
329
        $field->write();
330
331
        $form->publishRecursive();
332
333
        $field->Title = 'Edited title';
334
        $field->write();
335
336
        // check that the published version is not updated
337
        $live = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $field->ID");
338
        $this->assertInstanceOf(EditableFormField::class, $live);
339
        $this->assertEquals('Title', $live->Title);
340
341
        // revert back to the live data
342
        $form->doRevertToLive();
343
        $form->flushCache();
344
345
        $check = Versioned::get_one_by_stage(EditableFormField::class, 'Stage', "\"EditableFormField\".\"ID\" = $field->ID");
346
347
        $this->assertEquals('Title', $check->Title);
348
    }
349
350
    public function testDuplicatingForm()
351
    {
352
        $this->logInWithPermission('ADMIN');
353
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
354
355
        $duplicate = $form->duplicate();
356
357
        $this->assertEquals($form->Fields()->Count(), $duplicate->Fields()->Count());
358
359
        // can't compare object since the dates/ids change
360
        $this->assertEquals($form->Fields()->First()->Title, $duplicate->Fields()->First()->Title);
361
362
        // Test duplicate with group
363
        $form2 = $this->objFromFixture(UserDefinedForm::class, 'page-with-group');
364
        $form2Validator = new UserFormValidator();
365
        $form2Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList()));
366
        $this->assertTrue($form2Validator->php($form2->toMap()));
367
368
        // Check field groups exist
369
        $form2GroupStart = $form2->Fields()->filter('ClassName', EditableFieldGroup::class)->first();
370
        $form2GroupEnd = $form2->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first();
371
        $this->assertEquals($form2GroupEnd->ID, $form2GroupStart->EndID);
372
373
        // Duplicate this
374
        $form3 = $form2->duplicate();
375
        $form3Validator = new UserFormValidator();
376
        $form3Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList()));
377
        $this->assertTrue($form3Validator->php($form3->toMap()));
378
        // Check field groups exist
379
        $form3GroupStart = $form3->Fields()->filter('ClassName', EditableFieldGroup::class)->first();
380
        $form3GroupEnd = $form3->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first();
381
        $this->assertEquals($form3GroupEnd->ID, $form3GroupStart->EndID);
382
        $this->assertNotEquals($form2GroupEnd->ID, $form3GroupStart->EndID);
383
    }
384
385
    public function testDuplicateFormDuplicatesRecursively()
386
    {
387
        $this->logInWithPermission('ADMIN');
388
        /** @var UserDefinedForm $form */
389
        $form = $this->objFromFixture(UserDefinedForm::class, 'form-with-multioptions');
390
391
        $this->assertGreaterThanOrEqual(1, $form->Fields()->count(), 'Fixtured page has a field');
392
        $this->assertCount(
393
            2,
394
            $form->Fields()->Last()->Options(),
395
            'Fixtured multiple option field has two options'
396
        );
397
398
        $newForm = $form->duplicate();
399
        $this->assertEquals(
400
            $form->Fields()->count(),
401
            $newForm->Fields()->count(),
402
            'Duplicated page has same number of fields'
403
        );
404
        $this->assertEquals(
405
            $form->Fields()->Last()->Options()->count(),
0 ignored issues
show
Bug introduced by
The method count() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as DOMNodeList or Yaf_Config_Simple or Yaf\Session or Threaded or Volatile or SimpleXMLElement or DOMNamedNodeMap or Thread or Yaf_Session or Yaf\Config\Simple or Yaf\Config\Ini or Worker or Yaf_Config_Ini or Stackable or MongoGridFSCursor or ResourceBundle or SilverStripe\ORM\SS_List or SilverStripe\View\ViewableData or PHPUnit_Framework_TestSuite or SilverStripe\ORM\Map or Symfony\Component\Finder\Finder or SebastianBergmann\CodeCoverage\Node\Directory or ArrayObject or SplDoublyLinkedList or HttpMessage or HttpRequestPool or RdKafka\Metadata\Collection or Yaf_Config_Simple or PHP_CodeSniffer\Files\FileList or SplFixedArray or SplObjectStorage or Yaf\Session or SQLiteResult or Imagick or http\Message or Yaf_Session or SplPriorityQueue or Yaf\Config\Simple or Yaf\Config\Ini or MongoCursor or Yaf_Config_Ini or SplHeap or MongoGridFSCursor or CachingIterator or PHP_Token_Stream or Phar or ArrayIterator or GlobIterator or Phar or Phar or RecursiveCachingIterator or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

405
            $form->Fields()->Last()->Options()->/** @scrutinizer ignore-call */ count(),
Loading history...
406
            $newForm->Fields()->Last()->Options()->count(),
407
            'Duplicated dropdown field from duplicated form has duplicated options'
408
        );
409
    }
410
411
    public function testFormOptions()
412
    {
413
        $this->logInWithPermission('ADMIN');
414
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
415
416
        $fields = $form->getFormOptions();
417
        $submit = $fields->fieldByName('SubmitButtonText');
418
        $reset = $fields->fieldByName('ShowClearButton');
419
420
        $this->assertEquals($submit->Title(), 'Text on submit button:');
421
        $this->assertEquals($reset->Title(), 'Show Clear Form Button');
422
    }
423
424
    public function testEmailRecipientFilters()
425
    {
426
        /** @var UserDefinedForm $form */
427
        $form = $this->objFromFixture(UserDefinedForm::class, 'filtered-form-page');
428
429
        // Check unfiltered recipients
430
        $result0 = $form
431
            ->EmailRecipients()
432
            ->sort('EmailAddress')
433
            ->column('EmailAddress');
434
        $this->assertEquals(
435
            [
436
                '[email protected]',
437
                '[email protected]',
438
                '[email protected]'
439
            ],
440
            $result0
441
        );
442
443
        // check filters based on given data
444
        $result1 = $form->FilteredEmailRecipients(
445
            [
446
                'your-name' => 'Value',
447
                'address' => '',
448
                'street' => 'Anything',
449
                'city' => 'Matches Not Equals',
450
                'colours' => ['Red'] // matches 2
451
            ],
452
            null
453
        )
454
            ->sort('EmailAddress')
455
            ->column('EmailAddress');
456
        $this->assertEquals(
457
            [
458
                '[email protected]',
459
                '[email protected]'
460
            ],
461
            $result1
462
        );
463
464
        // Check all positive matches
465
        $result2 = $form->FilteredEmailRecipients(
466
            [
467
                'your-name' => '',
468
                'address' => 'Anything',
469
                'street' => 'Matches Equals',
470
                'city' => 'Anything',
471
                'colours' => ['Red', 'Blue'] // matches 2
472
            ],
473
            null
474
        )
475
            ->sort('EmailAddress')
476
            ->column('EmailAddress');
477
        $this->assertEquals(
478
            [
479
                '[email protected]',
480
                '[email protected]',
481
                '[email protected]'
482
            ],
483
            $result2
484
        );
485
486
        $result3 = $form->FilteredEmailRecipients(
487
            [
488
                'your-name' => 'Should be blank but is not',
489
                'address' => 'Anything',
490
                'street' => 'Matches Equals',
491
                'city' => 'Anything',
492
                'colours' => ['Blue']
493
            ],
494
            null
495
        )->column('EmailAddress');
496
        $this->assertEquals(
497
            [
498
                '[email protected]'
499
            ],
500
            $result3
501
        );
502
503
504
        $result4 = $form->FilteredEmailRecipients(
505
            [
506
                'your-name' => '',
507
                'address' => 'Anything',
508
                'street' => 'Wrong value for this field',
509
                'city' => '',
510
                'colours' => ['Blue', 'Green']
511
            ],
512
            null
513
        )->column('EmailAddress');
514
        $this->assertEquals(
515
            ['[email protected]'],
516
            $result4
517
        );
518
    }
519
520
    public function testIndex()
521
    {
522
        // Test that the $UserDefinedForm is stripped out
523
        $page = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
524
        $page->publish('Stage', 'Live');
525
526
        $result = $this->get($page->Link());
527
        $body = Convert::nl2os($result->getBody(), ''); // strip out newlines
528
        $this->assertFalse($result->isError());
529
        $this->assertContains('<p>Here is my form</p><form', $body);
530
        $this->assertContains('</form><p>Thank you for filling it out</p>', $body);
531
532
        $this->assertNotContains('<p>$UserDefinedForm</p>', $body);
533
        $this->assertNotContains('<p></p>', $body);
534
        $this->assertNotContains('</p><p>Thank you for filling it out</p>', $body);
535
    }
536
537
    public function testEmailAddressValidation()
538
    {
539
        $this->logInWithPermission('ADMIN');
540
541
        // test invalid email addresses fail validation
542
        $recipient = $this->objFromFixture(
543
            EmailRecipient::class,
544
            'invalid-recipient-list'
545
        );
546
        $result = $recipient->validate();
547
        $this->assertFalse($result->isValid());
548
        $this->assertNotEmpty($result->getMessages());
549
        $this->assertContains('filtered.example.com', $result->getMessages()[0]['message']);
550
        $this->assertNotContains('[email protected]', $result->getMessages()[0]['message']);
551
552
        // test valid email addresses pass validation
553
        $recipient = $this->objFromFixture(
554
            EmailRecipient::class,
555
            'valid-recipient-list'
556
        );
557
        $result = $recipient->validate();
558
        $this->assertTrue($result->isValid());
559
        $this->assertEmpty($result->getMessages());
560
    }
561
}
562