root/trunk/tests/element/Basic.php

Revision 260, 8.6 kB (checked in by sfuchs, 3 years ago)

re-commit: element unittests and testpages

Line 
1 <?
2     class Element_Basic extends Element_Base {
3
4         // test getValue()
5
6         public function test_string_SetGet() {
7
8             $form = $this->createForm();
9
10             $expected = 'Sven';
11             $element = $form->getElement('name');
12             $element->setValue($expected);
13             $result = $element->getValue();
14
15             $this->assertTrue($result === $expected);
16         }
17
18         // test getValue() w/ $_GET
19
20         public function test_string_GET_1() {
21
22             // this should work
23             $form = $this->createForm();
24             $expected = $_GET['name'] = 'Sven';
25             $result = $form->getElement('name')->getValue();
26             $this->assertTrue($result === $expected);
27         }
28
29         // test getValue() w/ $_GET
30
31         public function test_string_GET_2() {
32
33             // this should not work, since no namespace is set
34             $form = $this->createForm();
35             $_GET['home']['name'] = 'Sven';
36             $result = $form->getElement('name')->getValue();
37             $this->assertTrue(empty($result));
38         }
39
40         // test name attribute serialization
41
42         public function test_string_serialize() {
43
44             $form = $this->createForm();
45
46             $expected = 'name="name"';
47             $element = $form->getElement('name');
48             $result = $element->serialize();
49
50             $this->assertTrue(strpos($result, $expected) !== false);
51         }
52
53         // COMBOBOX
54
55         public function test_combobox_GET() {
56
57             $form = $this->createForm(array('formname' => 'combobox'));
58
59             $_GET['area'] = 'a01';
60             $element = $form->getElement('area');
61             $expected = 'a01';
62             $result = $element->getValue();
63             $this->assertTrue($result === $expected);
64         }
65
66         public function test_combobox_serialize() {
67
68             $form = $this->createForm(array('formname' => 'combobox'));
69
70             $expected = '<select name="area" size="1"><option value="a01">Very pretty area</option></select><input id="pfo6__free" name="area__free" type="text" value="" />';
71             $element = $form->getElement('area');
72             $result = self::stripElementId($element->serialize());
73
74             $this->assertTrue(strpos($result, $expected) !== false);
75         }
76
77         // ENUM
78
79         public function test_enum_GET() {
80
81             $form = $this->createForm(array('formname' => 'enum'));
82
83             $_GET['area'] = 'a01';
84             $element = $form->getElement('area');
85             $expected = 'a01';
86             $result = $element->getValue();
87             $this->assertTrue($result === $expected);
88         }
89
90         public function test_enum_serialize() {
91
92             $form = $this->createForm(array('formname' => 'enum'));
93
94             $expected = '<select name="area" size="1"><option value="a01">Very pretty area</option></select>';
95             $element = $form->getElement('area');
96             $result = self::stripElementId($element->serialize());
97
98             $this->assertTrue($result === $expected);
99         }
100
101         // GROUP
102
103         public function test_group_GET() {
104
105             $form = $this->createForm(array('formname' => 'group'));
106
107             $areaDefinition = array('values' => array(array('value' => '',),array('value' => 'a01',),),);
108             $el = patForms::createElement('area', 'Enum', $areaDefinition);
109
110             $group = $form->getElement('loginOptions');
111             $group->addElement($el);
112
113             $_GET['area'] = 'a01';
114             $expected = array('area' => 'a01');
115             $result = $group->getValue();
116
117             $this->assertTrue($result === $expected);
118
119         }
120
121         public function test_group_serialize() {
122
123             $form = $this->createForm(array('formname' => 'group'));
124
125             $areaDefinition = array('values' => array(array('value' => '',),array('value' => 'a01',),),);
126             $el = patForms::createElement('area', 'Enum', $areaDefinition);
127
128             $group = $form->getElement('loginOptions');
129             $group->setRenderer(patForms::createRenderer('Array'));
130             $group->addElement($el);
131
132             $expected = '<select name="area" size="1"><option value=""></option><option value="a01"></option></select>';
133             $element = $form->getElement('loginOptions');
134             $result = array_pop($element->serialize());
135             $result = self::stripElementId($result['element']);
136
137             $this->assertTrue($result === $expected);
138         }
139
140         // HIDDEN
141
142         public function test_hidden_GET() {
143
144             $form = $this->createForm(array('formname' => 'hidden'));
145
146             $_GET['area'] = 'a01';
147             $element = $form->getElement('area');
148             $expected = 'a01';
149             $result = $element->getValue();
150             $this->assertTrue($result === $expected);
151         }
152
153         public function test_hidden_serialize() {
154
155             $form = $this->createForm(array('formname' => 'hidden'));
156
157             $expected = '<input type="hidden" name="area" value="a01" />';
158             $element = $form->getElement('area');
159             $result = self::stripElementId($element->serialize());
160
161             $this->assertTrue($result === $expected);
162         }
163
164         // POOL
165
166         public function test_pool_GET() {
167
168             $form = $this->createForm(array('formname' => 'pool'));
169
170             $_GET['mammalians']= array('and', 'deg');
171             $element = $form->getElement('mammalians');
172             $expected = array('and', 'deg');
173             $result = $element->getValue();
174
175             $this->assertTrue($result === $expected);
176         }
177
178         public function test_pool_serialize() {
179
180             $form = $this->createForm(array('formname' => 'pool'));
181
182             $expected = array(
183                 '!name="mammalians" id="mammalians"!',
184                 '!id="candidates_mammalians" name="candidates_mammalians"!',
185                 '!id="members_mammalians" name="members_mammalians"!',
186                 '!javascript:pool_mammalians.add\(\);!',
187                 '!javascript:pool_mammalians.remove\(\);!',
188                 '!pool_mammalians[\s]*=[\s]*new[\s]*pool\([\s]?\'mammalians\'[\s]?\);!',
189                 '!pool_mammalians.addCandidate!',
190                 '!pool_mammalians.init!',
191             );
192             $element = $form->getElement('mammalians');
193             $result = $element->serialize();
194             $result = preg_replace($expected, ':FOUND:', $result);
195
196             $this->assertTrue(substr_count($result, ':FOUND:') == 9);
197         }
198
199         // RADIO
200
201         public function test_radio_GET() {
202
203             $form = $this->createForm(array('formname' => 'radio'));
204
205             $_GET['username'] = 'a02';
206             $element = $form->getElement('username');
207             $expected = 'a02';
208             $result = $element->getValue();
209             $this->assertTrue($result === $expected);
210         }
211
212         public function test_radio_serialize() {
213
214             $form = $this->createForm(array('formname' => 'radio'));
215
216             $expected = 'name="username"';
217             $element = $form->getElement('username');
218             $result = self::stripElementId($element->serialize());
219
220             $this->assertTrue(strpos($result, $expected) !== false);
221         }
222
223         public function test_radio_serialize_labelForId() {
224
225             $form = $this->createForm(array('formname' => 'radio'));
226
227             $expected = 'id="pfo';
228             $element = $form->getElement('username');
229             $result = $element->serialize();
230
231             $this->assertTrue(strpos($result, $expected) !== false);
232         }
233
234         // RADIOGROUP
235
236         public function test_radiogroup_GET() {
237
238             $form = $this->createForm(array('formname' => 'radiogroup'));
239
240             $_GET['username'] = 'a02';
241             $element = $form->getElement('username');
242             $expected = 'a02';
243             $result = $element->getValue();
244             $this->assertTrue($result === $expected);
245         }
246
247         public function test_radiogroup_serialize() {
248
249             $form = $this->createForm(array('formname' => 'radiogroup'));
250
251             $expected = 'name="username"';
252             $element = $form->getElement('username');
253             $result = self::stripElementId($element->serialize());
254
255             $this->assertTrue(strpos($result, $expected) !== false);
256         }
257
258         // SET
259
260         public function test_set_GET() {
261
262             $form = $this->createForm(array('formname' => 'set'));
263
264             $_GET['wishlist']= array('w1', 'w7');
265             $element = $form->getElement('wishlist');
266             $expected = array('w1', 'w7');
267             $result = $element->getValue();
268
269             $this->assertTrue($result === $expected);
270         }
271
272         public function test_set_serialize() {
273
274             $form = $this->createForm(array('formname' => 'set'));
275
276             $expected = 'name="wishlist[]"';
277             $element = $form->getElement('wishlist');
278             $result = $element->serialize();
279
280             $this->assertTrue(strpos($result, $expected) !== false);
281         }
282
283         // SWITCH
284
285         public function test_switch_GET() {
286
287             $form = $this->createForm(array('formname' => 'switch'));
288
289             $_GET['enable'] = 'yes';
290             $element = $form->getElement('enable');
291             $expected = 'yes';
292             $result = $element->getValue();
293             $this->assertTrue($result === $expected);
294         }
295
296         public function test_switch_serialize() {
297
298             $form = $this->createForm(array('formname' => 'switch'));
299
300             $expected = '<input name="enable" value="yes" type="checkbox" />';
301             $element = $form->getElement('enable');
302             $result = self::stripElementId($element->serialize());
303
304             $this->assertTrue($result === $expected);
305         }
306
307         public function test_switch_serialize_labelForId() {
308
309             $form = $this->createForm(array('formname' => 'switch'));
310
311             $expected = 'id="pfo';
312             $element = $form->getElement('enable');
313             $result = $element->serialize();
314
315             $this->assertTrue(strpos($result, $expected) !== false);
316         }
317
318     }
319
320 ?>
Note: See TracBrowser for help on using the browser.