NullableFieldTests.php
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
 * Tests the NullableField form field class.
 * @package framework
 * @subpackage tests
 * @author Pete Bacon Darwin
 *
 */
class NullableFieldTests extends SapphireTest {
	
	/**
	 * Test that the NullableField works when it wraps a TextField containing actual content
	 */
	public function testWithContent() {
		$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
		$this->assertEquals("Field1", $a->getName());
		$this->assertEquals("Field 1", $a->Title());
		$this->assertSame("abc", $a->Value());
		$this->assertSame("abc", $a->dataValue());
		$field = $a->Field();
		$this->assertTag(array(
			'tag'=>'input',
			'id'=>'Field1',
			'attributes'=>array('type'=>'text', 'name'=>'Field1', 'value'=>'abc'),
		), $field);
		$this->assertTag(array(
			'tag'=>'input',
			'id'=>'Field1_IsNull',
			'attributes'=>array('type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1'),
		), $field);
	}
	/**
	 * Test that the NullableField works when it wraps a TextField containing an empty string
	 */
	public function testWithEmpty() {
		$a = new NullableField(new TextField("Field1", "Field 1", ""));
		$this->assertEquals("Field1", $a->getName());
		$this->assertEquals("Field 1", $a->Title());
		$this->assertSame("", $a->Value());
		$this->assertSame("", $a->dataValue());
		$field = $a->Field();
		$this->assertTag(array(
			'tag'=>'input',
			'id'=>'Field1',
			'attributes'=>array('type'=>'text', 'name'=>'Field1', 'value'=>''),
		), $field);
		$this->assertTag(array(
			'tag'=>'input',
			'id'=>'Field1_IsNull',
			'attributes'=>array('type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1'),
		), $field);
	}
	/**
	 * Test that the NullableField works when it wraps a TextField containing a null string
	 */
	public function testWithNull() {
		$a = new NullableField(new TextField("Field1", "Field 1", null));
		$this->assertEquals("Field1", $a->getName());
		$this->assertEquals("Field 1", $a->Title());
		$this->assertSame(null, $a->Value());
		$this->assertSame(null, $a->dataValue());
		$field = $a->Field();
		$this->assertTag(array(
			'tag'=>'input',
			'id'=>'Field1',
			'attributes'=>array('type'=>'text', 'name'=>'Field1', 'value'=>''),
		), $field);
		$this->assertTag(array(
			'tag'=>'input',
			'id'=>'Field1_IsNull',
			'attributes'=>array('type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1', 'checked'=>'checked'),
		), $field);
		unset($a);
	}
	
	/**
	 * Test NullableField::setValue works when passed simple values
	 */
	public function testSetValueSimple() {
		$a = new NullableField(new TextField("Field1", "Field 1"));
		$a->setValue("abc");
		$this->assertSame("abc", $a->dataValue());
		
		$a = new NullableField(new TextField("Field1", "Field 1"));
		$a->setValue(null);
		$this->assertSame(null, $a->dataValue());
		
		$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
		$a->setValue(null);
		$this->assertSame(null, $a->dataValue());
		$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
		$a->setValue("xyz");
		$this->assertSame("xyz", $a->dataValue());
		
		$a = new NullableField(new TextField("Field1", "Field 1"));
		$a->setValue("");
		$this->assertSame("", $a->dataValue());
		
		$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
		$a->setValue("");
		$this->assertSame("", $a->dataValue());
	}
	
	/**
	 * Test NullableField::setValue works when passed an array values,
	 * which happens when the form submits.
	 */
	public function testSetValueArray() {
		$a = new NullableField(new TextField("Field1", "Field 1"));
		$a->setValue("abc", array("Field1_IsNull"=>false));
		$this->assertSame("abc", $a->dataValue());
		$a = new NullableField(new TextField("Field1", "Field 1"));
		$a->setValue("", array("Field1_IsNull"=>false));
		$this->assertSame("", $a->dataValue());
		$a = new NullableField(new TextField("Field1", "Field 1"));
		$a->setValue("", array("Field1_IsNull"=>true));
		$this->assertSame(null, $a->dataValue());
	}
}