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