Completed
Push — master ( 79d3cf...7bc817 )
by Damian
09:08
created

OptionsetFieldTest::testSafelyCast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
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\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
    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 &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
    public function testNoAriaRequired()
109
    {
110
        $field = new OptionsetField('RequiredField', 'myRequiredField');
111
112
        $form = new Form(
113
            Controller::curr(), "form", new FieldList($field), new FieldList(),
114
            new RequiredFields(["RequiredField"])
115
        );
116
        $this->assertTrue($field->Required());
117
118
        $attributes = $field->getAttributes();
119
        $this->assertFalse(array_key_exists("name", $attributes));
120
        $this->assertFalse(array_key_exists("required", $attributes));
121
        $this->assertTrue(array_key_exists("role", $attributes));
122
    }
123
}
124