Completed
Push — authenticator-refactor ( 0a18bb...b9e528 )
by Simon
08:12
created

CheckboxFieldTest::testNoAriaRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
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 View Code Duplication
    public function testFieldValueTrue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFieldValueString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFieldValueSettingNull()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testFieldValueSettingFalse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testSavingChecked()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testSavingUnchecked()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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(
0 ignored issues
show
Unused Code introduced by
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
200
            Controller::curr(),
201
            "form",
202
            new FieldList($field),
203
            new FieldList(),
204
            new RequiredFields(["RequiredField"])
205
        );
206
        $this->assertTrue($field->Required());
207
208
        $attributes = $field->getAttributes();
209
        $this->assertFalse(array_key_exists("aria-required", $attributes));
210
    }
211
}
212