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

OptionsetFieldTest::testNoAriaRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 18
loc 18
rs 9.4285
c 2
b 0
f 0
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,
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
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...
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 &amp; Five &amp; Six')
95
            )
96
        );
97
        $fieldHTML = (string)$field1->Field();
98
        $this->assertContains('One', $fieldHTML);
99
        $this->assertContains('Two &amp; Three', $fieldHTML);
100
        $this->assertNotContains('Two & Three', $fieldHTML);
101
        $this->assertContains('Four &amp; Five &amp; 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()
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...
109
    {
110
        $field = new OptionsetField('RequiredField', 'myRequiredField');
111
112
        $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...
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