|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\Forms\Form; |
|
8
|
|
|
use SilverStripe\Forms\Tests\CheckboxFieldtest\Article; |
|
9
|
|
|
use SilverStripe\ORM\DB; |
|
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
11
|
|
|
use SilverStripe\Forms\CheckboxField; |
|
12
|
|
|
use SilverStripe\Forms\RequiredFields; |
|
13
|
|
|
|
|
14
|
|
|
class CheckboxFieldTest extends SapphireTest |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
protected $usesDatabase = true; |
|
18
|
|
|
|
|
19
|
|
|
protected static $extra_dataobjects = array( |
|
20
|
|
|
Article::class, |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
public function testFieldValueTrue() |
|
24
|
|
|
{ |
|
25
|
|
|
/* Create the field, and set the value as boolean true */ |
|
26
|
|
|
$field = new CheckboxField('IsChecked', 'Checked'); |
|
27
|
|
|
$field->setValue(true); |
|
28
|
|
|
|
|
29
|
|
|
/* dataValue() for the field is 1 */ |
|
30
|
|
|
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1'); |
|
31
|
|
|
|
|
32
|
|
|
/* Value() returns 1 as well */ |
|
33
|
|
|
$this->assertEquals($field->Value(), 1, 'Value() returns a 1'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testFieldValueString() |
|
37
|
|
|
{ |
|
38
|
|
|
/* Create the field, and set the value as "on" (raw request field value from DOM) */ |
|
39
|
|
|
$field = new CheckboxField('IsChecked', 'Checked'); |
|
40
|
|
|
$field->setValue('on'); |
|
41
|
|
|
|
|
42
|
|
|
/* dataValue() for the field is 1 */ |
|
43
|
|
|
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1'); |
|
44
|
|
|
|
|
45
|
|
|
/* Value() returns 1 as well */ |
|
46
|
|
|
$this->assertEquals($field->Value(), 1, 'Value() returns a 1'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testFieldValueSettingNull() |
|
50
|
|
|
{ |
|
51
|
|
|
/* Create the field, and set the value as NULL */ |
|
52
|
|
|
$field = new CheckboxField('IsChecked', 'Checked'); |
|
53
|
|
|
$field->setValue(null); |
|
54
|
|
|
|
|
55
|
|
|
/* dataValue() for the field is null */ |
|
56
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0'); |
|
57
|
|
|
|
|
58
|
|
|
/* Value() returns 0 as well */ |
|
59
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testFieldValueSettingFalse() |
|
63
|
|
|
{ |
|
64
|
|
|
/* Create the field, and set the value as NULL */ |
|
65
|
|
|
$field = new CheckboxField('IsChecked', 'Checked'); |
|
66
|
|
|
$field->setValue(false); |
|
67
|
|
|
|
|
68
|
|
|
/* dataValue() for the field is null */ |
|
69
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0'); |
|
70
|
|
|
|
|
71
|
|
|
/* Value() returns 0 as well */ |
|
72
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testFieldValueWithoutSettingValue() |
|
76
|
|
|
{ |
|
77
|
|
|
/* Create the field, but don't set any value on it */ |
|
78
|
|
|
$field = new CheckboxField('IsChecked', 'Checked'); |
|
79
|
|
|
|
|
80
|
|
|
/* dataValue() for the field is null */ |
|
81
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0'); |
|
82
|
|
|
|
|
83
|
|
|
/* Value() returns 0 as well */ |
|
84
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testSavingChecked() |
|
88
|
|
|
{ |
|
89
|
|
|
/* Create a new test data record */ |
|
90
|
|
|
$article = new Article(); |
|
91
|
|
|
|
|
92
|
|
|
/* Create a field, with a value of 1 */ |
|
93
|
|
|
$field = new CheckboxField('IsChecked', 'Checked', 1); |
|
94
|
|
|
|
|
95
|
|
|
/* Save the field into our Article object */ |
|
96
|
|
|
$field->saveInto($article); |
|
97
|
|
|
|
|
98
|
|
|
/* Write the record to the test database */ |
|
99
|
|
|
$article->write(); |
|
100
|
|
|
|
|
101
|
|
|
/* Check that IsChecked column contains a 1 */ |
|
102
|
|
|
$this->assertEquals( |
|
103
|
|
|
DB::query("SELECT \"IsChecked\" FROM \"CheckboxFieldTest_Article\"")->value(), |
|
104
|
|
|
1, |
|
105
|
|
|
'We have a 1 set in the database, because the field saved into as a 1' |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
/* Delete the record we tested */ |
|
109
|
|
|
$article->delete(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testSavingUnchecked() |
|
113
|
|
|
{ |
|
114
|
|
|
/* Create a new test data record */ |
|
115
|
|
|
$article = new Article(); |
|
116
|
|
|
|
|
117
|
|
|
/* Create a field, with no value */ |
|
118
|
|
|
$field = new CheckboxField('IsChecked', 'Checked'); |
|
119
|
|
|
|
|
120
|
|
|
/* Save the field into our Article object */ |
|
121
|
|
|
$field->saveInto($article); |
|
122
|
|
|
|
|
123
|
|
|
/* Write the record to the test database */ |
|
124
|
|
|
$article->write(); |
|
125
|
|
|
|
|
126
|
|
|
/* Check that IsChecked column contains a 0 */ |
|
127
|
|
|
$this->assertEquals( |
|
128
|
|
|
DB::query("SELECT \"IsChecked\" FROM \"CheckboxFieldTest_Article\"")->value(), |
|
129
|
|
|
0, |
|
130
|
|
|
'We have a 0 set in the database, because the field saved into as a 0' |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
/* Delete the record we tested */ |
|
134
|
|
|
$article->delete(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testReadonlyCheckboxField() |
|
138
|
|
|
{ |
|
139
|
|
|
// Test 1: a checked checkbox goes to "Yes" |
|
140
|
|
|
$field1 = new CheckboxField('IsChecked', 'Checked'); |
|
141
|
|
|
$field1->setValue('on'); |
|
142
|
|
|
$this->assertEquals( |
|
143
|
|
|
_t('SilverStripe\\Forms\\CheckboxField.YESANSWER', 'Yes'), |
|
144
|
|
|
trim(strip_tags($field1->performReadonlyTransformation()->Field())) |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
// Test 2: an checkbox with the value set to false to "No" |
|
148
|
|
|
$field2 = new CheckboxField('IsChecked', 'Checked'); |
|
149
|
|
|
$field2->setValue(false); |
|
150
|
|
|
$this->assertEquals( |
|
151
|
|
|
_t('SilverStripe\\Forms\\CheckboxField.NOANSWER', 'No'), |
|
152
|
|
|
trim(strip_tags($field2->performReadonlyTransformation()->Field())) |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
// Test 3: an checkbox with no value ever set goes to "No" |
|
156
|
|
|
$field3 = new CheckboxField('IsChecked', 'Checked'); |
|
157
|
|
|
$this->assertEquals( |
|
158
|
|
|
_t('SilverStripe\\Forms\\CheckboxField.NOANSWER', 'No'), |
|
159
|
|
|
trim(strip_tags($field3->performReadonlyTransformation()->Field())) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function testValidation() |
|
164
|
|
|
{ |
|
165
|
|
|
$field = CheckboxField::create('Test', 'Testing'); |
|
166
|
|
|
$validator = new RequiredFields(); |
|
167
|
|
|
$field->setValue(1); |
|
168
|
|
|
$this->assertTrue( |
|
169
|
|
|
$field->validate($validator), |
|
170
|
|
|
'Field correctly validates integers as allowed' |
|
171
|
|
|
); |
|
172
|
|
|
//string value should validate |
|
173
|
|
|
$field->setValue("test"); |
|
174
|
|
|
$this->assertTrue( |
|
175
|
|
|
$field->validate($validator), |
|
176
|
|
|
'Field correctly validates words as allowed' |
|
177
|
|
|
); |
|
178
|
|
|
//empty string should validate |
|
179
|
|
|
$field->setValue(''); |
|
180
|
|
|
$this->assertTrue( |
|
181
|
|
|
$field->validate($validator), |
|
182
|
|
|
'Field correctly validates empty strings as allowed' |
|
183
|
|
|
); |
|
184
|
|
|
//null should validate |
|
185
|
|
|
$field->setValue(null); |
|
186
|
|
|
$this->assertTrue( |
|
187
|
|
|
$field->validate($validator), |
|
188
|
|
|
'Field correct validates null as allowed' |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* #2939 CheckboxField creates invalid HTML when required |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testNoAriaRequired() |
|
196
|
|
|
{ |
|
197
|
|
|
$field = new CheckboxField('RequiredField', 'myRequiredField'); |
|
198
|
|
|
|
|
199
|
|
|
$form = new Form( |
|
200
|
|
|
Controller::curr(), "form", new FieldList($field), new FieldList(), |
|
201
|
|
|
new RequiredFields(["RequiredField"]) |
|
202
|
|
|
); |
|
203
|
|
|
$this->assertTrue($field->Required()); |
|
204
|
|
|
|
|
205
|
|
|
$attributes = $field->getAttributes(); |
|
206
|
|
|
$this->assertFalse(array_key_exists("aria-required", $attributes)); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|