Completed
Push — master ( 077d14...bcadba )
by
unknown
12s
created

EditableFormFieldTest::testGetIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\UserForms\Tests\Model;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Dev\FunctionalTest;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\OptionsetField;
9
use SilverStripe\UserForms\Model\EditableFormField;
10
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox;
11
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
12
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
13
use SilverStripe\UserForms\Model\EditableFormField\EditableOption;
14
use SilverStripe\UserForms\Model\EditableFormField\EditableRadioField;
15
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
16
use SilverStripe\UserForms\Model\UserDefinedForm;
17
18
/**
19
 * @package userforms
20
 */
21
class EditableFormFieldTest extends FunctionalTest
22
{
23
    protected static $fixture_file = 'EditableFormFieldTest.yml';
24
25
    public function testFormFieldPermissions()
26
    {
27
        $text = $this->objFromFixture(EditableTextField::class, 'basic-text');
28
29
        $this->logInWithPermission('ADMIN');
30
31
        $this->assertTrue($text->canCreate());
32
        $this->assertTrue($text->canView());
33
        $this->assertTrue($text->canEdit());
34
        $this->assertTrue($text->canDelete());
35
36
        $text->setReadonly(true);
37
        $this->assertTrue($text->canView());
38
        $this->assertFalse($text->canEdit());
39
        $this->assertFalse($text->canDelete());
40
41
        $text->setReadonly(false);
42
        $this->assertTrue($text->canView());
43
        $this->assertTrue($text->canEdit());
44
        $this->assertTrue($text->canDelete());
45
46
        $this->logOut();
47
        $this->logInWithPermission('SITETREE_VIEW_ALL');
48
49
        $this->assertFalse($text->canCreate());
50
51
        $text->setReadonly(false);
52
        $this->assertTrue($text->canView());
53
        $this->assertFalse($text->canEdit());
54
        $this->assertFalse($text->canDelete());
55
56
        $text->setReadonly(true);
57
        $this->assertTrue($text->canView());
58
        $this->assertFalse($text->canEdit());
59
        $this->assertFalse($text->canDelete());
60
    }
61
62
    public function testCustomRules()
63
    {
64
        $this->logInWithPermission('ADMIN');
65
        $form = $this->objFromFixture(UserDefinedForm::class, 'custom-rules-form');
66
67
        $checkbox = $form->Fields()->find('ClassName', EditableCheckbox::class);
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean getQueriedDatabaseFields()?

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...
68
        $field = $form->Fields()->find('ClassName', EditableTextField::class);
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean getQueriedDatabaseFields()?

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...
69
70
        $rules = $checkbox->DisplayRules();
71
72
        // form has 2 fields - a checkbox and a text field
73
        // it has 1 rule - when ticked the checkbox hides the text field
74
        $this->assertEquals(1, $rules->Count());
75
        $this->assertEquals($rules, $checkbox->EffectiveDisplayRules());
76
77
        $checkboxRule = $rules->First();
78
        $checkboxRule->ConditionFieldID = $field->ID;
79
80
        $this->assertEquals($checkboxRule->Display, 'Hide');
81
        $this->assertEquals($checkboxRule->ConditionOption, 'HasValue');
82
        $this->assertEquals($checkboxRule->FieldValue, '6');
83
84
        // If field is required then all custom rules are disabled
85
        $checkbox->Required = true;
86
        $this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count());
87
    }
88
89
    /**
90
     * @covers EditableOption::getValue
91
     */
92
    public function testEditableOptionEmptyValue()
93
    {
94
        $option = $this->objFromFixture(EditableOption::class, 'option-1');
95
        $option->Value = '';
96
97
        // Disallow empty values
98
        EditableOption::set_allow_empty_values(false);
99
        $this->assertEquals($option->Title, $option->Value);
100
101
        $option->Value = 'test';
102
        $this->assertEquals('test', $option->Value);
103
104
        // Allow empty values
105
        EditableOption::set_allow_empty_values(true);
106
        $option->Value = '';
107
        $this->assertEquals('', $option->Value);
108
    }
109
110 View Code Duplication
    public function testEditableDropdownField()
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...
111
    {
112
        $dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
113
114
        $field = $dropdown->getFormField();
115
116
        $this->assertThat($field, $this->isInstanceOf(DropdownField::class));
117
        $values = $field->getSource();
118
119
        $this->assertEquals(['Option 1' => 'Option 1', 'Option 2' => 'Option 2'], $values);
120
    }
121
122 View Code Duplication
    public function testEditableRadioField()
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...
123
    {
124
        $radio = $this->objFromFixture(EditableRadioField::class, 'radio-field');
125
126
        $field = $radio->getFormField();
127
128
        $this->assertThat($field, $this->isInstanceOf(OptionsetField::class));
129
        $values = $field->getSource();
130
131
        $this->assertEquals(['Option 5' => 'Option 5', 'Option 6' => 'Option 6'], $values);
132
    }
133
134
    public function testMultipleOptionDuplication()
135
    {
136
        $dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
137
138
        $clone = $dropdown->duplicate();
139
140
        $this->assertEquals($dropdown->Options()->Count(), $clone->Options()->Count());
141
142
        foreach ($clone->Options() as $option) {
143
            $original = $dropdown->Options()->find('Title', $option->Title);
144
145
            $this->assertEquals($original->Sort, $option->Sort);
146
        }
147
    }
148
149
    public function testFileField()
150
    {
151
        $fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
152
        $formField = $fileField->getFormField();
153
154
        $this->assertContains('jpg', $formField->getValidator()->getAllowedExtensions());
155
        $this->assertNotContains('notallowedextension', $formField->getValidator()->getAllowedExtensions());
156
    }
157
158
    public function testFileFieldAllowedExtensionsBlacklist()
159
    {
160
        Config::modify()->merge(EditableFileField::class, 'allowed_extensions_blacklist', ['jpg']);
161
        $fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
162
        $formField = $fileField->getFormField();
163
164
        $this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions());
165
    }
166
167
    /**
168
     * Verify that unique names are automatically generated for each formfield
169
     */
170
    public function testUniqueName()
171
    {
172
        $textfield1 = new EditableTextField();
173
        $this->assertEmpty($textfield1->Name);
174
175
        // Write values
176
        $textfield1->write();
177
        $textfield2 = new EditableTextField();
178
        $textfield2->write();
179
        $checkboxField = new EditableCheckbox();
180
        $checkboxField->write();
181
182
        // Test values are in the expected format
183
        $this->assertRegExp('/^EditableTextField_.+/', $textfield1->Name);
184
        $this->assertRegExp('/^EditableTextField_.+/', $textfield2->Name);
185
        $this->assertRegExp('/^EditableCheckbox_.+/', $checkboxField->Name);
186
        $this->assertNotEquals($textfield1->Name, $textfield2->Name);
187
    }
188
189
    public function testLengthRange()
190
    {
191
        /** @var EditableTextField $textField */
192
        $textField = $this->objFromFixture(EditableTextField::class, 'basic-text');
193
194
        // Empty range
195
        /** @var TextField $formField */
196
        $textField->MinLength = 0;
0 ignored issues
show
Documentation introduced by
The property MinLength does not exist on object<SilverStripe\User...ield\EditableTextField>. 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...
197
        $textField->MaxLength = 0;
0 ignored issues
show
Documentation introduced by
The property MaxLength does not exist on object<SilverStripe\User...ield\EditableTextField>. 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...
198
        $attributes = $textField->getFormField()->getAttributes();
199
        $this->assertFalse(isset($attributes['maxLength']));
200
        $this->assertFalse(isset($attributes['data-rule-minlength']));
201
        $this->assertFalse(isset($attributes['data-rule-maxlength']));
202
203
        // Test valid range
204
        $textField->MinLength = 10;
0 ignored issues
show
Documentation introduced by
The property MinLength does not exist on object<SilverStripe\User...ield\EditableTextField>. 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...
205
        $textField->MaxLength = 20;
0 ignored issues
show
Documentation introduced by
The property MaxLength does not exist on object<SilverStripe\User...ield\EditableTextField>. 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...
206
        $attributes = $textField->getFormField()->getAttributes();
207
        $this->assertEquals(20, $attributes['maxLength']);
208
        $this->assertEquals(20, $attributes['size']);
209
        $this->assertEquals(10, $attributes['data-rule-minlength']);
210
        $this->assertEquals(20, $attributes['data-rule-maxlength']);
211
212
        // textarea
213
        $textField->Rows = 3;
0 ignored issues
show
Documentation introduced by
The property Rows does not exist on object<SilverStripe\User...ield\EditableTextField>. 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...
214
        $attributes = $textField->getFormField()->getAttributes();
215
        $this->assertFalse(isset($attributes['maxLength']));
216
        $this->assertEquals(10, $attributes['data-rule-minlength']);
217
        $this->assertEquals(20, $attributes['data-rule-maxlength']);
218
    }
219
220
    public function testFormatDisplayRules()
221
    {
222
        $field = $this->objFromFixture(EditableTextField::class, 'irdNumberField');
223
        $displayRules = $field->formatDisplayRules();
224
        $this->assertNotNull($displayRules);
225
        $this->assertCount(1, $displayRules['operations']);
226
227
        // Field is initially visible, so the "view" method should be to hide it
228
        $this->assertSame('addClass("hide")', $displayRules['view']);
229
        // The opposite method should be to return it to its original state, i.e. show it again
230
        $this->assertSame('removeClass("hide")', $displayRules['opposite']);
231
    }
232
233
    public function testGetIcon()
234
    {
235
        $field = new EditableTextField;
236
237
        $this->assertContains('/images/editabletextfield.png', $field->getIcon());
238
    }
239
}
240