1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package userforms |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
class EditableFormFieldTest extends FunctionalTest { |
|
|
|
|
8
|
|
|
|
9
|
|
|
static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml'; |
|
|
|
|
10
|
|
|
|
11
|
|
|
function testFormFieldPermissions() { |
|
|
|
|
12
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text'); |
13
|
|
|
|
14
|
|
|
$this->logInWithPermission('ADMIN'); |
15
|
|
|
$this->assertTrue($text->canCreate()); |
|
|
|
|
16
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
17
|
|
|
$this->assertTrue($text->canEdit()); |
|
|
|
|
18
|
|
|
$this->assertTrue($text->canDelete()); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$text->setReadonly(true); |
21
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
22
|
|
|
$this->assertFalse($text->canEdit()); |
|
|
|
|
23
|
|
|
$this->assertFalse($text->canDelete()); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$text->setReadonly(false); |
26
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
27
|
|
|
$this->assertTrue($text->canEdit()); |
|
|
|
|
28
|
|
|
$this->assertTrue($text->canDelete()); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$member = Member::currentUser(); |
31
|
|
|
$member->logout(); |
32
|
|
|
|
33
|
|
|
$this->logInWithPermission('SITETREE_VIEW_ALL'); |
34
|
|
|
$this->assertFalse($text->canCreate()); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$text->setReadonly(false); |
37
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
38
|
|
|
$this->assertFalse($text->canEdit()); |
|
|
|
|
39
|
|
|
$this->assertFalse($text->canDelete()); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$text->setReadonly(true); |
42
|
|
|
$this->assertTrue($text->canView()); |
|
|
|
|
43
|
|
|
$this->assertFalse($text->canEdit()); |
|
|
|
|
44
|
|
|
$this->assertFalse($text->canDelete()); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function testCustomRules() { |
|
|
|
|
48
|
|
|
$this->logInWithPermission('ADMIN'); |
49
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'custom-rules-form'); |
50
|
|
|
|
51
|
|
|
$checkbox = $form->Fields()->find('ClassName', 'EditableCheckbox'); |
|
|
|
|
52
|
|
|
$field = $form->Fields()->find('ClassName', 'EditableTextField'); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$rules = $checkbox->DisplayRules(); |
55
|
|
|
|
56
|
|
|
// form has 2 fields - a checkbox and a text field |
57
|
|
|
// it has 1 rule - when ticked the checkbox hides the text field |
58
|
|
|
$this->assertEquals(1, $rules->Count()); |
|
|
|
|
59
|
|
|
$this->assertEquals($rules, $checkbox->EffectiveDisplayRules()); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$checkboxRule = $rules->First(); |
62
|
|
|
$checkboxRule->ConditionFieldID = $field->ID; |
63
|
|
|
|
64
|
|
|
$this->assertEquals($checkboxRule->Display, 'Hide'); |
|
|
|
|
65
|
|
|
$this->assertEquals($checkboxRule->ConditionOption, 'HasValue'); |
|
|
|
|
66
|
|
|
$this->assertEquals($checkboxRule->FieldValue, '6'); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// If field is required then all custom rules are disabled |
69
|
|
|
$checkbox->Required = true; |
70
|
|
|
$this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count()); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @covers EditableOption::getValue |
75
|
|
|
*/ |
76
|
|
|
public function testEditableOptionEmptyValue() { |
77
|
|
|
$option = $this->objFromFixture('EditableOption', 'option-1'); |
78
|
|
|
$option->Value = ''; |
79
|
|
|
|
80
|
|
|
// Disallow empty values |
81
|
|
|
EditableOption::set_allow_empty_values(false); |
82
|
|
|
$this->assertEquals($option->Title, $option->Value); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$option->Value = 'test'; |
85
|
|
|
$this->assertEquals('test', $option->Value); |
|
|
|
|
86
|
|
|
|
87
|
|
|
// Allow empty values |
88
|
|
|
EditableOption::set_allow_empty_values(true); |
89
|
|
|
$option->Value = ''; |
90
|
|
|
$this->assertEquals('', $option->Value); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
function testEditableDropdownField() { |
|
|
|
|
94
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown'); |
95
|
|
|
|
96
|
|
|
$field = $dropdown->getFormField(); |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->assertThat($field, $this->isInstanceOf('DropdownField')); |
|
|
|
|
100
|
|
|
$values = $field->getSource(); |
101
|
|
|
|
102
|
|
|
$this->assertEquals(array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'), $values); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
function testEditableRadioField() { |
|
|
|
|
106
|
|
|
$radio = $this->objFromFixture('EditableRadioField', 'radio-field'); |
107
|
|
|
|
108
|
|
|
$field = $radio->getFormField(); |
109
|
|
|
|
110
|
|
|
$this->assertThat($field, $this->isInstanceOf('OptionsetField')); |
|
|
|
|
111
|
|
|
$values = $field->getSource(); |
112
|
|
|
|
113
|
|
|
$this->assertEquals(array('Option 5' => 'Option 5', 'Option 6' => 'Option 6'), $values); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
function testMultipleOptionDuplication() { |
|
|
|
|
117
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown','basic-dropdown'); |
118
|
|
|
|
119
|
|
|
$clone = $dropdown->duplicate(); |
120
|
|
|
|
121
|
|
|
$this->assertEquals($clone->Options()->Count(), $dropdown->Options()->Count()); |
|
|
|
|
122
|
|
|
|
123
|
|
|
foreach($clone->Options() as $option) { |
124
|
|
|
$orginal = $dropdown->Options()->find('Title', $option->Title); |
125
|
|
|
|
126
|
|
|
$this->assertEquals($orginal->Sort, $option->Sort); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testFileField() { |
131
|
|
|
$fileField = $this->objFromFixture('EditableFileField', 'file-field'); |
132
|
|
|
$formField = $fileField->getFormField(); |
133
|
|
|
|
134
|
|
|
$this->assertContains('jpg', $formField->getValidator()->getAllowedExtensions()); |
135
|
|
|
$this->assertNotContains('notallowedextension', $formField->getValidator()->getAllowedExtensions()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testFileFieldAllowedExtensionsBlacklist() { |
139
|
|
|
Config::inst()->update('EditableFileField', 'allowed_extensions_blacklist', array('jpg')); |
140
|
|
|
$fileField = $this->objFromFixture('EditableFileField', 'file-field'); |
141
|
|
|
$formField = $fileField->getFormField(); |
142
|
|
|
|
143
|
|
|
$this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Verify that unique names are automatically generated for each formfield |
148
|
|
|
*/ |
149
|
|
|
public function testUniqueName() { |
150
|
|
|
$textfield1 = new EditableTextField(); |
151
|
|
|
$this->assertEmpty($textfield1->Name); |
|
|
|
|
152
|
|
|
|
153
|
|
|
// Write values |
154
|
|
|
$textfield1->write(); |
155
|
|
|
$textfield2 = new EditableTextField(); |
156
|
|
|
$textfield2->write(); |
157
|
|
|
$checkboxField = new EditableCheckbox(); |
158
|
|
|
$checkboxField->write(); |
159
|
|
|
|
160
|
|
|
// Test values are in the expected format |
161
|
|
|
$this->assertRegExp('/^EditableTextField_.+/', $textfield1->Name); |
|
|
|
|
162
|
|
|
$this->assertRegExp('/^EditableTextField_.+/', $textfield2->Name); |
|
|
|
|
163
|
|
|
$this->assertRegExp('/^EditableCheckbox_.+/', $checkboxField->Name); |
|
|
|
|
164
|
|
|
$this->assertNotEquals($textfield1->Name, $textfield2->Name); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testLengthRange() { |
168
|
|
|
/** @var EditableTextField $textField */ |
169
|
|
|
$textField = $this->objFromFixture('EditableTextField', 'basic-text'); |
170
|
|
|
|
171
|
|
|
// Empty range |
172
|
|
|
/** @var TextField $formField */ |
173
|
|
|
$textField->MinLength = 0; |
|
|
|
|
174
|
|
|
$textField->MaxLength = 0; |
|
|
|
|
175
|
|
|
$attributes = $textField->getFormField()->getAttributes(); |
176
|
|
|
$this->assertFalse(isset($attributes['maxLength'])); |
|
|
|
|
177
|
|
|
$this->assertFalse(isset($attributes['data-rule-minlength'])); |
|
|
|
|
178
|
|
|
$this->assertFalse(isset($attributes['data-rule-maxlength'])); |
|
|
|
|
179
|
|
|
|
180
|
|
|
// Test valid range |
181
|
|
|
$textField->MinLength = 10; |
|
|
|
|
182
|
|
|
$textField->MaxLength = 20; |
|
|
|
|
183
|
|
|
$attributes = $textField->getFormField()->getAttributes(); |
184
|
|
|
$this->assertEquals(20, $attributes['maxLength']); |
|
|
|
|
185
|
|
|
$this->assertEquals(20, $attributes['size']); |
|
|
|
|
186
|
|
|
$this->assertEquals(10, $attributes['data-rule-minlength']); |
|
|
|
|
187
|
|
|
$this->assertEquals(20, $attributes['data-rule-maxlength']); |
|
|
|
|
188
|
|
|
|
189
|
|
|
// textarea |
190
|
|
|
$textField->Rows = 3; |
|
|
|
|
191
|
|
|
$attributes = $textField->getFormField()->getAttributes(); |
192
|
|
|
$this->assertFalse(isset($attributes['maxLength'])); |
|
|
|
|
193
|
|
|
$this->assertEquals(10, $attributes['data-rule-minlength']); |
|
|
|
|
194
|
|
|
$this->assertEquals(20, $attributes['data-rule-maxlength']); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
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.