Passed
Pull Request — 4.9 (#10353)
by Guy
13:24
created

NullableFieldTest::testSetValueArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\Forms\NullableField;
8
9
/**
10
 * Tests the NullableField form field class.
11
 *
12
 * @author Pete Bacon Darwin
13
 */
14
class NullableFieldTest extends SapphireTest
15
{
16
17
    /**
18
     * Test that the NullableField works when it wraps a TextField containing actual content
19
     */
20
    public function testWithContent()
21
    {
22
        $a = new NullableField(new TextField("Field1", "Field 1", "abc"));
23
        $this->assertEquals("Field1", $a->getName());
24
        $this->assertEquals("Field 1", $a->Title());
25
        $this->assertSame("abc", $a->Value());
26
        $this->assertSame("abc", $a->dataValue());
27
        $field = $a->Field();
28
        $this->assertTag(
0 ignored issues
show
Bug introduced by
The method assertTag() does not exist on SilverStripe\Forms\Tests\NullableFieldTest. Did you maybe mean assertThat()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->/** @scrutinizer ignore-call */ 
29
               assertTag(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
            [
30
            'tag'=>'input',
31
            'id'=>'Field1',
32
            'attributes'=>['type'=>'text', 'name'=>'Field1', 'value'=>'abc'],
33
            ],
34
            $field
35
        );
36
        $this->assertTag(
37
            [
38
            'tag'=>'input',
39
            'id'=>'Field1_IsNull',
40
            'attributes'=>['type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1'],
41
            ],
42
            $field
43
        );
44
    }
45
    /**
46
     * Test that the NullableField works when it wraps a TextField containing an empty string
47
     */
48
    public function testWithEmpty()
49
    {
50
        $a = new NullableField(new TextField("Field1", "Field 1", ""));
51
        $this->assertEquals("Field1", $a->getName());
52
        $this->assertEquals("Field 1", $a->Title());
53
        $this->assertSame("", $a->Value());
54
        $this->assertSame("", $a->dataValue());
55
        $field = $a->Field();
56
        $this->assertTag(
57
            [
58
            'tag'=>'input',
59
            'id'=>'Field1',
60
            'attributes'=>['type'=>'text', 'name'=>'Field1', 'value'=>''],
61
            ],
62
            $field
63
        );
64
        $this->assertTag(
65
            [
66
            'tag'=>'input',
67
            'id'=>'Field1_IsNull',
68
            'attributes'=>['type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1'],
69
            ],
70
            $field
71
        );
72
    }
73
    /**
74
     * Test that the NullableField works when it wraps a TextField containing a null string
75
     */
76
    public function testWithNull()
77
    {
78
        $a = new NullableField(new TextField("Field1", "Field 1", null));
79
        $this->assertEquals("Field1", $a->getName());
80
        $this->assertEquals("Field 1", $a->Title());
81
        $this->assertSame(null, $a->Value());
82
        $this->assertSame(null, $a->dataValue());
83
        $field = $a->Field();
84
        $this->assertTag(
85
            [
86
            'tag'=>'input',
87
            'id'=>'Field1',
88
            'attributes'=>['type'=>'text', 'name'=>'Field1', 'value'=>''],
89
            ],
90
            $field
91
        );
92
        $this->assertTag(
93
            [
94
            'tag'=>'input',
95
            'id'=>'Field1_IsNull',
96
            'attributes'=>['type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1', 'checked'=>'checked'],
97
            ],
98
            $field
99
        );
100
        unset($a);
101
    }
102
103
    /**
104
     * Test NullableField::setValue works when passed simple values
105
     */
106
    public function testSetValueSimple()
107
    {
108
        $a = new NullableField(new TextField("Field1", "Field 1"));
109
        $a->setValue("abc");
110
        $this->assertSame("abc", $a->dataValue());
111
112
        $a = new NullableField(new TextField("Field1", "Field 1"));
113
        $a->setValue(null);
114
        $this->assertSame(null, $a->dataValue());
115
116
        $a = new NullableField(new TextField("Field1", "Field 1", "abc"));
117
        $a->setValue(null);
118
        $this->assertSame(null, $a->dataValue());
119
120
        $a = new NullableField(new TextField("Field1", "Field 1", "abc"));
121
        $a->setValue("xyz");
122
        $this->assertSame("xyz", $a->dataValue());
123
124
        $a = new NullableField(new TextField("Field1", "Field 1"));
125
        $a->setValue("");
126
        $this->assertSame("", $a->dataValue());
127
128
        $a = new NullableField(new TextField("Field1", "Field 1", "abc"));
129
        $a->setValue("");
130
        $this->assertSame("", $a->dataValue());
131
    }
132
133
    /**
134
     * Test NullableField::setValue works when passed an array values,
135
     * which happens when the form submits.
136
     */
137
    public function testSetValueArray()
138
    {
139
        $a = new NullableField(new TextField("Field1", "Field 1"));
140
        $a->setValue("abc", ["Field1_IsNull"=>false]);
141
        $this->assertSame("abc", $a->dataValue());
142
143
        $a = new NullableField(new TextField("Field1", "Field 1"));
144
        $a->setValue("", ["Field1_IsNull"=>false]);
145
        $this->assertSame("", $a->dataValue());
146
147
        $a = new NullableField(new TextField("Field1", "Field 1"));
148
        $a->setValue("", ["Field1_IsNull"=>true]);
149
        $this->assertSame(null, $a->dataValue());
150
    }
151
}
152