1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_Error; |
6
|
|
|
use SilverStripe\Dev\CSSContentParser; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\Forms\CompositeField; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\RequiredFields; |
12
|
|
|
use SilverStripe\Forms\TextField; |
13
|
|
|
|
14
|
|
|
class CompositeFieldTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public function testFieldPosition() |
18
|
|
|
{ |
19
|
|
|
$compositeOuter = new CompositeField( |
20
|
|
|
new TextField('A'), |
21
|
|
|
new TextField('B'), |
22
|
|
|
$compositeInner = new CompositeField( |
23
|
|
|
new TextField('C1'), |
24
|
|
|
new TextField('C2') |
25
|
|
|
), |
26
|
|
|
new TextField('D') |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$this->assertEquals(0, $compositeOuter->fieldPosition('A')); |
30
|
|
|
$this->assertEquals(1, $compositeOuter->fieldPosition('B')); |
31
|
|
|
$this->assertEquals(3, $compositeOuter->fieldPosition('D')); |
32
|
|
|
|
33
|
|
|
$this->assertEquals(0, $compositeInner->fieldPosition('C1')); |
34
|
|
|
$this->assertEquals(1, $compositeInner->fieldPosition('C2')); |
35
|
|
|
|
36
|
|
|
$compositeOuter->insertBefore('B', new TextField('AB')); |
37
|
|
|
$this->assertEquals(0, $compositeOuter->fieldPosition('A')); |
38
|
|
|
$this->assertEquals(1, $compositeOuter->fieldPosition('AB')); |
39
|
|
|
$this->assertEquals(2, $compositeOuter->fieldPosition('B')); |
40
|
|
|
|
41
|
|
|
$this->assertFalse($compositeOuter->fieldPosition(null), 'Falsy input should return false'); |
|
|
|
|
42
|
|
|
$this->assertFalse($compositeOuter->fieldPosition('FOO'), 'Non-exitent child should return false'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testTag() |
46
|
|
|
{ |
47
|
|
|
$div = new CompositeField( |
48
|
|
|
new TextField('A'), |
49
|
|
|
new TextField('B') |
50
|
|
|
); |
51
|
|
|
$this->assertStringStartsWith('<div', trim($div->FieldHolder())); |
52
|
|
|
$this->assertStringEndsWith('/div>', trim($div->FieldHolder())); |
53
|
|
|
|
54
|
|
|
$fieldset = new CompositeField(); |
55
|
|
|
$fieldset->setTag('fieldset'); |
56
|
|
|
|
57
|
|
|
$this->assertStringStartsWith('<fieldset', trim($fieldset->FieldHolder())); |
58
|
|
|
$this->assertStringEndsWith('/fieldset>', trim($fieldset->FieldHolder())); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testPushAndUnshift() |
62
|
|
|
{ |
63
|
|
|
$composite = new CompositeField( |
64
|
|
|
new TextField('Middle') |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$composite->push(new TextField('End')); |
68
|
|
|
/* There are now 2 fields in the set */ |
69
|
|
|
$this->assertEquals(2, $composite->getChildren()->Count()); |
70
|
|
|
/* The most recently added field is at the end of the set */ |
71
|
|
|
$this->assertEquals('End', $composite->getChildren()->Last()->getName()); |
72
|
|
|
|
73
|
|
|
$composite->unshift(new TextField('Beginning')); |
74
|
|
|
/* There are now 3 fields in the set */ |
75
|
|
|
$this->assertEquals(3, $composite->getChildren()->Count()); |
76
|
|
|
/* The most recently added field is at the beginning of the set */ |
77
|
|
|
$this->assertEquals('Beginning', $composite->getChildren()->First()->getName()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testLegend() |
81
|
|
|
{ |
82
|
|
|
$composite = new CompositeField( |
83
|
|
|
new TextField('A'), |
84
|
|
|
new TextField('B') |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$composite->setTag('fieldset'); |
88
|
|
|
$composite->setLegend('My legend'); |
89
|
|
|
|
90
|
|
|
$parser = new CSSContentParser($composite->FieldHolder()); |
91
|
|
|
$root = $parser->getBySelector('fieldset.composite'); |
|
|
|
|
92
|
|
|
$legend = $parser->getBySelector('fieldset.composite legend'); |
93
|
|
|
|
94
|
|
|
$this->assertNotNull($legend); |
95
|
|
|
$this->assertEquals('My legend', (string)$legend[0]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testValidation() |
99
|
|
|
{ |
100
|
|
|
$field = CompositeField::create( |
101
|
|
|
$fieldOne = DropdownField::create('A', '', [ 'value' => 'value' ]), |
102
|
|
|
$fieldTwo = TextField::create('B') |
103
|
|
|
); |
104
|
|
|
$validator = new RequiredFields(); |
105
|
|
|
$this->assertFalse( |
106
|
|
|
$field->validate($validator), |
107
|
|
|
"Validation fails when child is invalid" |
108
|
|
|
); |
109
|
|
|
$fieldOne->setEmptyString('empty'); |
110
|
|
|
$this->assertTrue( |
111
|
|
|
$field->validate($validator), |
112
|
|
|
"Validates when children are valid" |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testChildren() |
117
|
|
|
{ |
118
|
|
|
$field = CompositeField::create(); |
119
|
|
|
|
120
|
|
|
$this->assertInstanceOf(FieldList::class, $field->getChildren()); |
121
|
|
|
$this->assertEquals($field, $field->getChildren()->getContainerField()); |
122
|
|
|
|
123
|
|
|
$expectedChildren = FieldList::create( |
124
|
|
|
$fieldOne = DropdownField::create('A', '', [ 'value' => 'value' ]), |
125
|
|
|
$fieldTwo = TextField::create('B') |
126
|
|
|
); |
127
|
|
|
$field->setChildren($expectedChildren); |
128
|
|
|
$this->assertEquals($expectedChildren, $field->getChildren()); |
129
|
|
|
$this->assertEquals($field, $expectedChildren->getContainerField()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testExtraClass() |
133
|
|
|
{ |
134
|
|
|
$field = CompositeField::create(); |
135
|
|
|
$field->setColumnCount(3); |
136
|
|
|
$result = $field->extraClass(); |
137
|
|
|
|
138
|
|
|
$this->assertContains('field', $result, 'Default class was not added'); |
139
|
|
|
$this->assertContains('CompositeField', $result, 'Default class was not added'); |
140
|
|
|
$this->assertContains('multicolumn', $result, 'Multi column field did not have extra class added'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testGetAttributes() |
144
|
|
|
{ |
145
|
|
|
$field = CompositeField::create(); |
146
|
|
|
$field->setLegend('test'); |
147
|
|
|
$result = $field->getAttributes(); |
148
|
|
|
|
149
|
|
|
$this->assertNull($result['tabindex']); |
150
|
|
|
$this->assertNull($result['type']); |
151
|
|
|
$this->assertNull($result['value']); |
152
|
|
|
$this->assertSame('test', $result['title']); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testGetAttributesReturnsEmptyTitleForFieldSets() |
156
|
|
|
{ |
157
|
|
|
$field = CompositeField::create(); |
158
|
|
|
$field->setLegend('not used'); |
159
|
|
|
$field->setTag('fieldset'); |
160
|
|
|
$result = $field->getAttributes(); |
161
|
|
|
$this->assertNull($result['title']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @expectedException PHPUnit_Framework_Error |
166
|
|
|
* @expectedExceptionMessageRegExp /a field called 'Test' appears twice in your form.*TextField.*TextField/ |
167
|
|
|
*/ |
168
|
|
|
public function testCollateDataFieldsThrowsErrorOnDuplicateChildren() |
169
|
|
|
{ |
170
|
|
|
$field = CompositeField::create( |
171
|
|
|
TextField::create('Test'), |
172
|
|
|
TextField::create('Test') |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$list = []; |
176
|
|
|
$field->collateDataFields($list); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testCollateDataFieldsWithSaveableOnly() |
180
|
|
|
{ |
181
|
|
|
$field = CompositeField::create( |
182
|
|
|
TextField::create('Test') |
183
|
|
|
->setReadonly(false) |
184
|
|
|
->setDisabled(true) |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$list = []; |
188
|
|
|
$field->collateDataFields($list, true); |
189
|
|
|
$this->assertEmpty($list, 'Unsaveable fields should not be collated when $saveableOnly = true'); |
190
|
|
|
|
191
|
|
|
$field->collateDataFields($list, false); |
192
|
|
|
$this->assertNotEmpty($list, 'Unsavable fields should be collated when $saveableOnly = false'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testSetDisabledPropagatesToChildren() |
196
|
|
|
{ |
197
|
|
|
$field = CompositeField::create( |
198
|
|
|
$testField = TextField::create('Test') |
199
|
|
|
->setDisabled(false) |
200
|
|
|
)->setDisabled(true); |
201
|
|
|
$this->assertTrue($field->fieldByName('Test')->isDisabled(), 'Children should also be set to disabled'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testIsComposite() |
205
|
|
|
{ |
206
|
|
|
$this->assertTrue(CompositeField::create()->isComposite()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testMakeFieldReadonlyPassedFieldName() |
210
|
|
|
{ |
211
|
|
|
$field = CompositeField::create( |
212
|
|
|
TextField::create('Test')->setDisabled(false) |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
$this->assertFalse($field->fieldByName('Test')->isReadonly()); |
216
|
|
|
$this->assertTrue($field->makeFieldReadonly('Test'), 'makeFieldReadonly should return true'); |
217
|
|
|
$this->assertTrue($field->fieldByName('Test')->isReadonly(), 'Named child field should be made readonly'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testMakeFieldReadonlyPassedFormField() |
221
|
|
|
{ |
222
|
|
|
$field = CompositeField::create( |
223
|
|
|
$testField = TextField::create('Test')->setDisabled(false) |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
$this->assertFalse($field->fieldByName('Test')->isReadonly()); |
227
|
|
|
$this->assertTrue($field->makeFieldReadonly($testField), 'makeFieldReadonly should return true'); |
228
|
|
|
$this->assertTrue($field->fieldByName('Test')->isReadonly(), 'Named child field should be made readonly'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testMakeFieldReadonlyWithNestedCompositeFields() |
232
|
|
|
{ |
233
|
|
|
$field = CompositeField::create( |
234
|
|
|
CompositeField::create( |
235
|
|
|
TextField::create('Test')->setDisabled(false) |
236
|
|
|
) |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$this->assertFalse($field->getChildren()->first()->fieldByName('Test')->isReadonly()); |
240
|
|
|
$this->assertTrue($field->makeFieldReadonly('Test'), 'makeFieldReadonly should return true'); |
241
|
|
|
$this->assertTrue( |
242
|
|
|
$field->getChildren()->first()->fieldByName('Test')->isReadonly(), |
243
|
|
|
'Named child field should be made readonly' |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testMakeFieldReadonlyReturnsFalseWhenFieldNotFound() |
248
|
|
|
{ |
249
|
|
|
$field = CompositeField::create( |
250
|
|
|
CompositeField::create( |
251
|
|
|
TextField::create('Test') |
252
|
|
|
) |
253
|
|
|
); |
254
|
|
|
|
255
|
|
|
$this->assertFalse( |
256
|
|
|
$field->makeFieldReadonly('NonExistent'), |
257
|
|
|
'makeFieldReadonly should return false when field is not found' |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function testDebug() |
262
|
|
|
{ |
263
|
|
|
$field = new CompositeField( |
264
|
|
|
new TextField('TestTextField') |
265
|
|
|
); |
266
|
|
|
$field->setName('TestComposite'); |
267
|
|
|
|
268
|
|
|
$result = $field->debug(); |
269
|
|
|
$this->assertContains(CompositeField::class . ' (TestComposite)', $result); |
270
|
|
|
$this->assertContains('TestTextField', $result); |
271
|
|
|
$this->assertContains('<ul', $result, 'Result should be formatted as a <ul>'); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|