1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
7
|
|
|
use SilverStripe\Dev\CSSContentParser; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\Forms\OptionsetField; |
10
|
|
|
use SilverStripe\Forms\RequiredFields; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
|
14
|
|
|
class OptionsetFieldTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
public function testSetDisabledItems() |
17
|
|
|
{ |
18
|
|
|
$f = new OptionsetField( |
19
|
|
|
'Test', |
20
|
|
|
false, |
|
|
|
|
21
|
|
|
array(0 => 'Zero', 1 => 'One') |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
$f->setDisabledItems(array(0)); |
25
|
|
|
$p = new CSSContentParser($f->Field()); |
26
|
|
|
$item0 = $p->getBySelector('#Test_0'); |
27
|
|
|
$item1 = $p->getBySelector('#Test_1'); |
28
|
|
|
$this->assertEquals( |
29
|
|
|
(string)$item0[0]['disabled'], |
30
|
|
|
'disabled' |
31
|
|
|
); |
32
|
|
|
$this->assertEquals( |
33
|
|
|
(string)$item1[0]['disabled'], |
34
|
|
|
'' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @skipUpgrade |
40
|
|
|
*/ |
41
|
|
|
public function testValidation() |
42
|
|
|
{ |
43
|
|
|
$field = OptionsetField::create( |
44
|
|
|
'Test', |
45
|
|
|
'Testing', |
46
|
|
|
array( |
47
|
|
|
"One" => "One", |
48
|
|
|
"Two" => "Two", |
49
|
|
|
"Five" => "Five" |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
$validator = new RequiredFields('Test'); |
53
|
|
|
$form = new Form(null, 'Form', new FieldList($field), new FieldList(), $validator); |
54
|
|
|
|
55
|
|
|
$field->setValue("One"); |
56
|
|
|
$this->assertTrue($field->validate($validator)); |
57
|
|
|
|
58
|
|
|
//non-existent value should make the field invalid |
59
|
|
|
$field->setValue("Three"); |
60
|
|
|
$this->assertFalse($field->validate($validator)); |
61
|
|
|
|
62
|
|
|
//empty string should pass field-level validation... |
63
|
|
|
$field->setValue(''); |
64
|
|
|
$this->assertTrue($field->validate($validator)); |
65
|
|
|
|
66
|
|
|
// ... but should not pass "RequiredFields" validation |
67
|
|
|
$this->assertFalse($form->validationResult()->isValid()); |
68
|
|
|
|
69
|
|
|
//disabled items shouldn't validate |
70
|
|
|
$field->setDisabledItems(array('Five')); |
71
|
|
|
$field->setValue('Five'); |
72
|
|
|
$this->assertFalse($field->validate($validator)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testReadonlyField() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$sourceArray = array(0 => 'No', 1 => 'Yes'); |
78
|
|
|
$field = new OptionsetField('FeelingOk', 'are you feeling ok?', $sourceArray, 1); |
79
|
|
|
$field->setEmptyString('(Select one)'); |
80
|
|
|
$field->setValue(1); |
81
|
|
|
$readonlyField = $field->performReadonlyTransformation(); |
82
|
|
|
preg_match('/Yes/', $readonlyField->Field(), $matches); |
83
|
|
|
$this->assertEquals($matches[0], 'Yes'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testSafelyCast() |
87
|
|
|
{ |
88
|
|
|
$field1 = new OptionsetField( |
89
|
|
|
'Options', |
90
|
|
|
'Options', |
91
|
|
|
array( |
92
|
|
|
1 => 'One', |
93
|
|
|
2 => 'Two & Three', |
94
|
|
|
3 => DBField::create_field('HTMLText', 'Four & Five & Six') |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
$fieldHTML = (string)$field1->Field(); |
98
|
|
|
$this->assertContains('One', $fieldHTML); |
99
|
|
|
$this->assertContains('Two & Three', $fieldHTML); |
100
|
|
|
$this->assertNotContains('Two & Three', $fieldHTML); |
101
|
|
|
$this->assertContains('Four & Five & Six', $fieldHTML); |
102
|
|
|
$this->assertNotContains('Four & Five & Six', $fieldHTML); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* #2939 OptionSetField creates invalid HTML when required |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function testNoAriaRequired() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$field = new OptionsetField('RequiredField', 'myRequiredField'); |
111
|
|
|
|
112
|
|
|
$form = new Form( |
|
|
|
|
113
|
|
|
Controller::curr(), |
114
|
|
|
"form", |
115
|
|
|
new FieldList($field), |
116
|
|
|
new FieldList(), |
117
|
|
|
new RequiredFields(["RequiredField"]) |
118
|
|
|
); |
119
|
|
|
$this->assertTrue($field->Required()); |
120
|
|
|
|
121
|
|
|
$attributes = $field->getAttributes(); |
122
|
|
|
$this->assertFalse(array_key_exists("name", $attributes)); |
123
|
|
|
$this->assertFalse(array_key_exists("required", $attributes)); |
124
|
|
|
$this->assertTrue(array_key_exists("role", $attributes)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: