|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package userforms |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
class EditableFormFieldTest extends FunctionalTest |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
public static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml'; |
|
11
|
|
|
|
|
12
|
|
|
public function testFormFieldPermissions() |
|
13
|
|
|
{ |
|
14
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text'); |
|
15
|
|
|
|
|
16
|
|
|
$this->logInWithPermission('ADMIN'); |
|
17
|
|
|
$this->assertTrue($text->canCreate()); |
|
|
|
|
|
|
18
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
|
|
19
|
|
|
$this->assertTrue($text->canEdit()); |
|
|
|
|
|
|
20
|
|
|
$this->assertTrue($text->canDelete()); |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
$text->setReadonly(true); |
|
23
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
|
|
24
|
|
|
$this->assertFalse($text->canEdit()); |
|
|
|
|
|
|
25
|
|
|
$this->assertFalse($text->canDelete()); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
$text->setReadonly(false); |
|
28
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
|
|
29
|
|
|
$this->assertTrue($text->canEdit()); |
|
|
|
|
|
|
30
|
|
|
$this->assertTrue($text->canDelete()); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$member = Member::currentUser(); |
|
33
|
|
|
$member->logout(); |
|
34
|
|
|
|
|
35
|
|
|
$this->logInWithPermission('SITETREE_VIEW_ALL'); |
|
36
|
|
|
$this->assertFalse($text->canCreate()); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$text->setReadonly(false); |
|
39
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
|
|
40
|
|
|
$this->assertFalse($text->canEdit()); |
|
|
|
|
|
|
41
|
|
|
$this->assertFalse($text->canDelete()); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$text->setReadonly(true); |
|
44
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
|
|
45
|
|
|
$this->assertFalse($text->canEdit()); |
|
|
|
|
|
|
46
|
|
|
$this->assertFalse($text->canDelete()); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testCustomRules() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->logInWithPermission('ADMIN'); |
|
52
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'custom-rules-form'); |
|
53
|
|
|
|
|
54
|
|
|
$checkbox = $form->Fields()->find('ClassName', 'EditableCheckbox'); |
|
|
|
|
|
|
55
|
|
|
$field = $form->Fields()->find('ClassName', 'EditableTextField'); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$rules = $checkbox->DisplayRules(); |
|
58
|
|
|
|
|
59
|
|
|
// form has 2 fields - a checkbox and a text field |
|
60
|
|
|
// it has 1 rule - when ticked the checkbox hides the text field |
|
61
|
|
|
$this->assertEquals(1, $rules->Count()); |
|
|
|
|
|
|
62
|
|
|
$this->assertEquals($rules, $checkbox->EffectiveDisplayRules()); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$checkboxRule = $rules->First(); |
|
65
|
|
|
$checkboxRule->ConditionFieldID = $field->ID; |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals($checkboxRule->Display, 'Hide'); |
|
|
|
|
|
|
68
|
|
|
$this->assertEquals($checkboxRule->ConditionOption, 'HasValue'); |
|
|
|
|
|
|
69
|
|
|
$this->assertEquals($checkboxRule->FieldValue, '6'); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
// If field is required then all custom rules are disabled |
|
72
|
|
|
$checkbox->Required = true; |
|
73
|
|
|
$this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count()); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @covers EditableOption::getValue |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testEditableOptionEmptyValue() |
|
80
|
|
|
{ |
|
81
|
|
|
$option = $this->objFromFixture('EditableOption', 'option-1'); |
|
82
|
|
|
$option->Value = ''; |
|
83
|
|
|
|
|
84
|
|
|
// Disallow empty values |
|
85
|
|
|
EditableOption::set_allow_empty_values(false); |
|
86
|
|
|
$this->assertEquals($option->Title, $option->Value); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$option->Value = 'test'; |
|
89
|
|
|
$this->assertEquals('test', $option->Value); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
// Allow empty values |
|
92
|
|
|
EditableOption::set_allow_empty_values(true); |
|
93
|
|
|
$option->Value = ''; |
|
94
|
|
|
$this->assertEquals('', $option->Value); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
public function testEditableDropdownField() |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown'); |
|
100
|
|
|
|
|
101
|
|
|
$field = $dropdown->getFormField(); |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$this->assertThat($field, $this->isInstanceOf('DropdownField')); |
|
|
|
|
|
|
105
|
|
|
$values = $field->getSource(); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertEquals(array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'), $values); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
public function testEditableRadioField() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$radio = $this->objFromFixture('EditableRadioField', 'radio-field'); |
|
113
|
|
|
|
|
114
|
|
|
$field = $radio->getFormField(); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertThat($field, $this->isInstanceOf('OptionsetField')); |
|
|
|
|
|
|
117
|
|
|
$values = $field->getSource(); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals(array('Option 5' => 'Option 5', 'Option 6' => 'Option 6'), $values); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testMultipleOptionDuplication() |
|
123
|
|
|
{ |
|
124
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown'); |
|
125
|
|
|
|
|
126
|
|
|
$clone = $dropdown->duplicate(); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertEquals($clone->Options()->Count(), $dropdown->Options()->Count()); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
foreach ($clone->Options() as $option) { |
|
131
|
|
|
$orginal = $dropdown->Options()->find('Title', $option->Title); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertEquals($orginal->Sort, $option->Sort); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testFileField() |
|
138
|
|
|
{ |
|
139
|
|
|
$fileField = $this->objFromFixture('EditableFileField', 'file-field'); |
|
140
|
|
|
$formField = $fileField->getFormField(); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertContains('jpg', $formField->getValidator()->getAllowedExtensions()); |
|
143
|
|
|
$this->assertNotContains('notallowedextension', $formField->getValidator()->getAllowedExtensions()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testFileFieldAllowedExtensionsBlacklist() |
|
147
|
|
|
{ |
|
148
|
|
|
Config::inst()->update('EditableFileField', 'allowed_extensions_blacklist', array('jpg')); |
|
149
|
|
|
$fileField = $this->objFromFixture('EditableFileField', 'file-field'); |
|
150
|
|
|
$formField = $fileField->getFormField(); |
|
151
|
|
|
|
|
152
|
|
|
$this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions()); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Verify that unique names are automatically generated for each formfield |
|
157
|
|
|
*/ |
|
158
|
|
|
public function testUniqueName() |
|
159
|
|
|
{ |
|
160
|
|
|
$textfield1 = new EditableTextField(); |
|
161
|
|
|
$this->assertEmpty($textfield1->Name); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
// Write values |
|
164
|
|
|
$textfield1->write(); |
|
165
|
|
|
$textfield2 = new EditableTextField(); |
|
166
|
|
|
$textfield2->write(); |
|
167
|
|
|
$checkboxField = new EditableCheckbox(); |
|
168
|
|
|
$checkboxField->write(); |
|
169
|
|
|
|
|
170
|
|
|
// Test values are in the expected format |
|
171
|
|
|
$this->assertRegExp('/^EditableTextField_.+/', $textfield1->Name); |
|
|
|
|
|
|
172
|
|
|
$this->assertRegExp('/^EditableTextField_.+/', $textfield2->Name); |
|
|
|
|
|
|
173
|
|
|
$this->assertRegExp('/^EditableCheckbox_.+/', $checkboxField->Name); |
|
|
|
|
|
|
174
|
|
|
$this->assertNotEquals($textfield1->Name, $textfield2->Name); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testLengthRange() |
|
178
|
|
|
{ |
|
179
|
|
|
/** @var EditableTextField $textField */ |
|
180
|
|
|
$textField = $this->objFromFixture('EditableTextField', 'basic-text'); |
|
181
|
|
|
|
|
182
|
|
|
// Empty range |
|
183
|
|
|
/** @var TextField $formField */ |
|
184
|
|
|
$textField->MinLength = 0; |
|
|
|
|
|
|
185
|
|
|
$textField->MaxLength = 0; |
|
|
|
|
|
|
186
|
|
|
$attributes = $textField->getFormField()->getAttributes(); |
|
187
|
|
|
$this->assertFalse(isset($attributes['maxLength'])); |
|
|
|
|
|
|
188
|
|
|
$this->assertFalse(isset($attributes['data-rule-minlength'])); |
|
|
|
|
|
|
189
|
|
|
$this->assertFalse(isset($attributes['data-rule-maxlength'])); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
// Test valid range |
|
192
|
|
|
$textField->MinLength = 10; |
|
|
|
|
|
|
193
|
|
|
$textField->MaxLength = 20; |
|
|
|
|
|
|
194
|
|
|
$attributes = $textField->getFormField()->getAttributes(); |
|
195
|
|
|
$this->assertEquals(20, $attributes['maxLength']); |
|
|
|
|
|
|
196
|
|
|
$this->assertEquals(20, $attributes['size']); |
|
|
|
|
|
|
197
|
|
|
$this->assertEquals(10, $attributes['data-rule-minlength']); |
|
|
|
|
|
|
198
|
|
|
$this->assertEquals(20, $attributes['data-rule-maxlength']); |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
// textarea |
|
201
|
|
|
$textField->Rows = 3; |
|
|
|
|
|
|
202
|
|
|
$attributes = $textField->getFormField()->getAttributes(); |
|
203
|
|
|
$this->assertFalse(isset($attributes['maxLength'])); |
|
|
|
|
|
|
204
|
|
|
$this->assertEquals(10, $attributes['data-rule-minlength']); |
|
|
|
|
|
|
205
|
|
|
$this->assertEquals(20, $attributes['data-rule-maxlength']); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function testFormatDisplayRules() |
|
209
|
|
|
{ |
|
210
|
|
|
$field = $this->objFromFixture('EditableFormField', 'irdNumberField'); |
|
211
|
|
|
$displayRules = $field->formatDisplayRules(); |
|
212
|
|
|
$this->assertNotNull($displayRules); |
|
|
|
|
|
|
213
|
|
|
$this->assertCount(1, $displayRules['operations']); |
|
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
// Field is initially visible, so the "view" method should be to hide it |
|
216
|
|
|
$this->assertSame('addClass("hide")', $displayRules['view']); |
|
|
|
|
|
|
217
|
|
|
// The opposite method should be to return it to its original state, i.e. show it again |
|
218
|
|
|
$this->assertSame('removeClass("hide")', $displayRules['opposite']); |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Tests that certain elements are rendered with custom templates |
|
223
|
|
|
*/ |
|
224
|
|
|
public function testCustomTemplates() |
|
225
|
|
|
{ |
|
226
|
|
|
$radio = $this->objFromFixture('EditableRadioField', 'radio-field'); |
|
227
|
|
|
$this->assertEquals('UserFormsOptionSetField', $radio->getFormField()->getTemplate()); |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
$checkboxGroup = $this->objFromFixture('EditableCheckboxGroupField', 'checkbox-group'); |
|
230
|
|
|
$this->assertEquals('UserFormsCheckboxSetField', $checkboxGroup->getFormField()->getTemplate()); |
|
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.