Passed
Push — master ( 72e173...aab521 )
by Robbie
23:38 queued 15:49
created

GroupedDropdownFieldTest::testEmptyString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 39
nc 1
nop 0
dl 0
loc 64
rs 9.296
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\Forms\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Forms\GroupedDropdownField;
7
use SilverStripe\Forms\RequiredFields;
8
9
class GroupedDropdownFieldTest extends SapphireTest
10
{
11
12
    public function testValidation()
13
    {
14
        $field = GroupedDropdownField::create(
15
            'Test',
16
            'Testing',
17
            [
18
                "1" => "One",
19
                "Group One" => [
20
                    "2" => "Two",
21
                    "3" => "Three"
22
                ],
23
                "Group Two" => [
24
                    "4" => "Four"
25
                ],
26
            ]
27
        );
28
29
        $this->assertEquals(array("1", "2", "3", "4"), $field->getValidValues());
30
31
        $validator = new RequiredFields();
32
33
        $field->setValue("1");
34
        $this->assertTrue($field->validate($validator));
35
36
        //test grouped values
37
        $field->setValue("3");
38
        $this->assertTrue($field->validate($validator));
39
40
        //non-existent value should make the field invalid
41
        $field->setValue("Over 9000");
42
        $this->assertFalse($field->validate($validator));
43
44
        //empty string shouldn't validate
45
        $field->setValue('');
46
        $this->assertFalse($field->validate($validator));
47
48
        //empty field should validate after being set
49
        $field->setEmptyString('Empty String');
50
        $field->setValue('');
51
        $this->assertTrue($field->validate($validator));
52
53
        //disabled items shouldn't validate
54
        $field->setDisabledItems(array('1'));
55
        $field->setValue('1');
56
57
        $this->assertEquals(array("2", "3", "4"), $field->getValidValues());
58
        $this->assertEquals(array("1"), $field->getDisabledItems());
59
60
        $this->assertFalse($field->validate($validator));
61
    }
62
63
    /**
64
     * Test that empty-string values are supported by GroupDropdownTest
65
     */
66
    public function testEmptyString()
67
    {
68
        // Case A: empty value in the top level of the source
69
        $field = GroupedDropdownField::create(
70
            'Test',
71
            'Testing',
72
            [
73
                "" => "(Choose A)",
74
                "1" => "One",
75
                "Group One" => [
76
                    "2" => "Two",
77
                    "3" => "Three"
78
                ],
79
                "Group Two" => [
80
                    "4" => "Four"
81
                ],
82
            ]
83
        );
84
85
        $this->assertRegExp(
86
            '/<option value="" selected="selected" >\(Choose A\)<\/option>/',
87
            preg_replace('/\s+/', ' ', (string)$field->Field())
88
        );
89
90
        // Case B: empty value in the nested level of the source
91
        $field = GroupedDropdownField::create(
92
            'Test',
93
            'Testing',
94
            [
95
                "1" => "One",
96
                "Group One" => [
97
                    "" => "(Choose B)",
98
                    "2" => "Two",
99
                    "3" => "Three"
100
                ],
101
                "Group Two" => [
102
                    "4" => "Four"
103
                ],
104
            ]
105
        );
106
        $this->assertRegExp(
107
            '/<option value="" selected="selected" >\(Choose B\)<\/option>/',
108
            preg_replace('/\s+/', ' ', (string)$field->Field())
109
        );
110
111
        // Case C: setEmptyString
112
        $field = GroupedDropdownField::create(
113
            'Test',
114
            'Testing',
115
            [
116
                "1" => "One",
117
                "Group One" => [
118
                    "2" => "Two",
119
                    "3" => "Three"
120
                ],
121
                "Group Two" => [
122
                    "4" => "Four"
123
                ],
124
            ]
125
        );
126
        $field->setEmptyString('(Choose C)');
127
        $this->assertRegExp(
128
            '/<option value="" selected="selected" >\(Choose C\)<\/option>/',
129
            preg_replace('/\s+/', ' ', (string)$field->Field())
130
        );
131
    }
132
}
133