Completed
Push — master ( 016a0b...25fc98 )
by Franco
12s
created

UserDefinedFormTest::testPublishing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 9.0335
c 0
b 0
f 0
cc 1
eloc 38
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property FormID does not exist on object<SilverStripe\User...cipient\EmailRecipient>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property EmailBody does not exist on object<SilverStripe\User...cipient\EmailRecipient>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
145
        $recipient->EmailBodyHtml = $emailBodyHtml;
0 ignored issues
show
Documentation introduced by
The property EmailBodyHtml does not exist on object<SilverStripe\User...cipient\EmailRecipient>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
146
        $recipient->write();
147
148
        $this->assertEquals($recipient->SendPlain, 0);
0 ignored issues
show
Documentation introduced by
The property SendPlain does not exist on object<SilverStripe\User...cipient\EmailRecipient>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149
        $this->assertEquals($recipient->getEmailBodyContent(), $emailBodyHtml);
150
151
        $recipient->SendPlain = 1;
0 ignored issues
show
Documentation introduced by
The property SendPlain does not exist on object<SilverStripe\User...cipient\EmailRecipient>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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()));
0 ignored issues
show
Documentation introduced by
The property EmailTemplate does not exist on object<SilverStripe\User...cipient\EmailRecipient>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
218
219
        $dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
220
        $form->Fields()->add($dropdown);
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
221
222
        $stage = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
223
        $this->assertEquals(3, $stage->Fields()->Count());
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
234
235
        // edit the title
236
        $text = $form->Fields()->limit(1, 1)->First();
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<SilverStripe\User...del\EditableCustomRule>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
251
        $displayRule->ConditionFieldID = $text->ID;
0 ignored issues
show
Documentation introduced by
The property ConditionFieldID does not exist on object<SilverStripe\User...del\EditableCustomRule>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
276
        // $liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
277
        // $this->assertEmpty($liveRule);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
302
    {
303
        $this->logInWithPermission('ADMIN');
304
        $form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
305
        $field = $form->Fields()->First();
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
337
338
        // can't compare object since the dates/ids change
339
        $this->assertEquals($form->Fields()->First()->Title, $duplicate->Fields()->First()->Title);
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
349
        $form2GroupEnd = $form2->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first();
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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