ListboxFieldTest.php
8.01 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* @package framework
* @subpackage tests
*/
class ListboxFieldTest extends SapphireTest {
protected static $fixture_file = 'ListboxFieldTest.yml';
protected $extraDataObjects = array('ListboxFieldTest_DataObject', 'ListboxFieldTest_Article',
'ListboxFieldTest_Tag');
public function testFieldWithManyManyRelationship() {
$articleWithTags = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag1');
$tag2 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag2');
$tag3 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag3');
$field = new ListboxField("Tags", "Test field", DataObject::get("ListboxFieldTest_Tag")->map()->toArray());
$field->setMultiple(true);
$field->setValue(null, $articleWithTags);
$p = new CSSContentParser($field->Field());
$tag1xml = $p->getByXpath('//option[@value=' . $tag1->ID . ']');
$tag2xml = $p->getByXpath('//option[@value=' . $tag2->ID . ']');
$tag3xml = $p->getByXpath('//option[@value=' . $tag3->ID . ']');
$this->assertEquals('selected', (string)$tag1xml[0]['selected']);
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
$this->assertNull($tag3xml[0]['selected']);
}
public function testFieldWithDisabledItems() {
$articleWithTags = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag1');
$tag2 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag2');
$tag3 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag3');
$field = new ListboxField("Tags", "Test field", DataObject::get("ListboxFieldTest_Tag")->map()->toArray());
$field->setMultiple(true);
$field->setValue(null, $articleWithTags);
$field->setDisabledItems(array($tag1->ID, $tag3->ID));
$p = new CSSContentParser($field->Field());
$tag1xml = $p->getByXpath('//option[@value=' . $tag1->ID . ']');
$tag2xml = $p->getByXpath('//option[@value=' . $tag2->ID . ']');
$tag3xml = $p->getByXpath('//option[@value=' . $tag3->ID . ']');
$this->assertEquals('selected', (string)$tag1xml[0]['selected']);
$this->assertEquals('disabled', (string)$tag1xml[0]['disabled']);
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
$this->assertNull($tag2xml[0]['disabled']);
$this->assertNull($tag3xml[0]['selected']);
$this->assertEquals('disabled', (string)$tag3xml[0]['disabled']);
}
public function testSaveIntoNullValueWithMultipleOff() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = true;
$obj = new ListboxFieldTest_DataObject();
$field->setValue('a');
$field->saveInto($obj);
$field->setValue(null);
$field->saveInto($obj);
$this->assertNull($obj->Choices);
}
public function testSaveIntoNullValueWithMultipleOn() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = true;
$obj = new ListboxFieldTest_DataObject();
$field->setValue('a,c');
$field->saveInto($obj);
$field->setValue('');
$field->saveInto($obj);
$this->assertEquals('', $obj->Choices);
}
public function testSaveInto() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = false;
$obj = new ListboxFieldTest_DataObject();
$field->setValue('a');
$field->saveInto($obj);
$this->assertEquals('a', $obj->Choices);
}
public function testSaveIntoMultiple() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = true;
// As array
$obj1 = new ListboxFieldTest_DataObject();
$field->setValue(array('a', 'c'));
$field->saveInto($obj1);
$this->assertEquals('a,c', $obj1->Choices);
// As string
$obj2 = new ListboxFieldTest_DataObject();
$field->setValue('a,c');
$field->saveInto($obj2);
$this->assertEquals('a,c', $obj2->Choices);
}
public function testSaveIntoManyManyRelation() {
$article = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag1');
$tag2 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag2');
$field = new ListboxField("Tags", "Test field", DataObject::get("ListboxFieldTest_Tag")->map()->toArray());
$field->setMultiple(true);
// Save new relations
$field->setValue(array($tag1->ID,$tag2->ID));
$field->saveInto($article);
$article = Dataobject::get_by_id('ListboxFieldTest_Article', $article->ID, false);
$this->assertEquals(array($tag1->ID, $tag2->ID), $article->Tags()->sort('ID')->column('ID'));
// Remove existing relation
$field->setValue(array($tag1->ID));
$field->saveInto($article);
$article = Dataobject::get_by_id('ListboxFieldTest_Article', $article->ID, false);
$this->assertEquals(array($tag1->ID), $article->Tags()->sort('ID')->column('ID'));
// Set NULL value
$field->setValue(null);
$field->saveInto($article);
$article = Dataobject::get_by_id('ListboxFieldTest_Article', $article->ID, false);
$this->assertEquals(array(), $article->Tags()->sort('ID')->column('ID'));
}
/**
* @expectedException InvalidArgumentException
*/
public function testSetValueFailsOnArrayIfMultipleIsOff() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = false;
// As array (type error)
$failsOnArray = false;
$obj = new ListboxFieldTest_DataObject();
$field->setValue(array('a', 'c'));
}
/**
* @expectedException InvalidArgumentException
*/
public function testSetValueFailsOnStringIfChoiceInvalidAndMultipleIsOff() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = false;
// As string (invalid choice as comma is regarded literal)
$obj = new ListboxFieldTest_DataObject();
$field->setValue('invalid');
}
public function testFieldRenderingMultipleOff() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = true;
$field->setValue('a');
$parser = new CSSContentParser($field->Field());
$optEls = $parser->getBySelector('option');
$this->assertEquals(3, count($optEls));
$this->assertEquals('selected', (string)$optEls[0]['selected']);
$this->assertEquals('', (string)$optEls[1]['selected']);
$this->assertEquals('', (string)$optEls[2]['selected']);
}
public function testFieldRenderingMultipleOn() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');
$field = new ListboxField('Choices', 'Choices', $choices);
$field->multiple = true;
$field->setValue('a,c');
$parser = new CSSContentParser($field->Field());
$optEls = $parser->getBySelector('option');
$this->assertEquals(3, count($optEls));
$this->assertEquals('selected', (string)$optEls[0]['selected']);
$this->assertEquals('', (string)$optEls[1]['selected']);
$this->assertEquals('selected', (string)$optEls[2]['selected']);
}
/**
* @expectedException InvalidArgumentException
*/
public function testCommasInSourceKeys() {
$choices = array('a' => 'a value', 'b,with,comma' => 'b value,with,comma',);
$field = new ListboxField('Choices', 'Choices', $choices);
}
}
class ListboxFieldTest_DataObject extends DataObject implements TestOnly {
private static $db = array(
'Choices' => 'Text'
);
}
class ListboxFieldTest_Article extends DataObject implements TestOnly {
private static $db = array(
"Content" => "Text",
);
private static $many_many = array(
"Tags" => "ListboxFieldTest_Tag",
);
}
class ListboxFieldTest_Tag extends DataObject implements TestOnly {
private static $belongs_many_many = array(
'Articles' => 'ListboxFieldTest_Article'
);
}