summaryrefslogtreecommitdiff
path: root/tests/UnitTests/simpletest/test/tag_test.php
blob: c18788afd57a4ec20e61d2262970edd9b56ec4d7 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
    // $Id: tag_test.php,v 1.52 2005/02/10 02:44:24 lastcraft Exp $
    
    require_once(dirname(__FILE__) . '/../tag.php');
    
    class TestOfTag extends UnitTestCase {
        
        function testStartValuesWithoutAdditionalContent() {
            $tag = new SimpleTitleTag(array('a' => '1', 'b' => ''));
            $this->assertEqual($tag->getTagName(), 'title');
            $this->assertIdentical($tag->getAttribute('a'), '1');
            $this->assertIdentical($tag->getAttribute('b'), true);
            $this->assertIdentical($tag->getAttribute('c'), false);
            $this->assertIdentical($tag->getContent(), '');
        }
        
        function testTitleContent() {
            $tag = &new SimpleTitleTag(array());
            $this->assertTrue($tag->expectEndTag());
            $tag->addContent('Hello');
            $tag->addContent('World');
            $this->assertEqual($tag->getText(), 'HelloWorld');
        }
        
        function testMessyTitleContent() {
            $tag = &new SimpleTitleTag(array());
            $this->assertTrue($tag->expectEndTag());
            $tag->addContent('<b>Hello</b>');
            $tag->addContent('<em>World</em>');
            $this->assertEqual($tag->getText(), 'HelloWorld');
        }
        
        function testTagWithNoEnd() {
            $tag = &new SimpleTextTag(array());
            $this->assertFalse($tag->expectEndTag());
        }
        
        function testAnchorHref() {
            $tag = &new SimpleAnchorTag(array('href' => 'http://here/'));
            $this->assertEqual($tag->getHref(), 'http://here/');
            
            $tag = &new SimpleAnchorTag(array('href' => ''));
            $this->assertIdentical($tag->getAttribute('href'), true);
            $this->assertIdentical($tag->getHref(), '');
            
            $tag = &new SimpleAnchorTag(array());
            $this->assertIdentical($tag->getAttribute('href'), false);
            $this->assertIdentical($tag->getHref(), '');
        }
        
        function testIsIdMatchesIdAttribute() {
            $tag = &new SimpleAnchorTag(array('href' => 'http://here/', 'id' => 7));
            $this->assertIdentical($tag->getAttribute('id'), '7');
            $this->assertTrue($tag->isId(7));
        }
    }
    
    class TestOfWidget extends UnitTestCase {
        
        function testTextEmptyDefault() {
            $tag = &new SimpleTextTag(array('' => 'text'));
            $this->assertIdentical($tag->getDefault(), '');
            $this->assertIdentical($tag->getValue(), '');
        }
        
        function testTextDefault() {
            $tag = &new SimpleTextTag(array('value' => 'aaa'));
            $this->assertEqual($tag->getDefault(), 'aaa');
            $this->assertEqual($tag->getValue(), 'aaa');
        }
        
        function testSettingTextValue() {
            $tag = &new SimpleTextTag(array('value' => 'aaa'));
            $tag->setValue('bbb');
            $this->assertEqual($tag->getValue(), 'bbb');
            $tag->resetValue();
            $this->assertEqual($tag->getValue(), 'aaa');
        }
        
        function testFailToSetHiddenValue() {
            $tag = &new SimpleTextTag(array('value' => 'aaa', 'type' => 'hidden'));
            $this->assertFalse($tag->setValue('bbb'));
            $this->assertEqual($tag->getValue(), 'aaa');
        }
        
        function testSubmitDefaults() {
            $tag = &new SimpleSubmitTag(array('type' => 'submit'));
            $this->assertEqual($tag->getName(), 'submit');
            $this->assertEqual($tag->getValue(), 'Submit');
            $this->assertFalse($tag->setValue('Cannot set this'));
            $this->assertEqual($tag->getValue(), 'Submit');
            $this->assertEqual($tag->getLabel(), 'Submit');
            $this->assertEqual($tag->getSubmitValues(), array('submit' => 'Submit'));
        }
        
        function testPopulatedSubmit() {
            $tag = &new SimpleSubmitTag(
                    array('type' => 'submit', 'name' => 's', 'value' => 'Ok!'));
            $this->assertEqual($tag->getName(), 's');
            $this->assertEqual($tag->getValue(), 'Ok!');
            $this->assertEqual($tag->getLabel(), 'Ok!');
            $this->assertEqual($tag->getSubmitValues(), array('s' => 'Ok!'));
        }
        
        function testImageSubmit() {
            $tag = &new SimpleImageSubmitTag(
                    array('type' => 'image', 'name' => 's', 'alt' => 'Label'));
            $this->assertEqual($tag->getName(), 's');
            $this->assertEqual($tag->getLabel(), 'Label');
            $this->assertEqual(
                    $tag->getSubmitValues(20, 30),
                    array('s.x' => 20, 's.y' => 30));
        }
        
        function testImageSubmitTitlePreferredOverAltForLabel() {
            $tag = &new SimpleImageSubmitTag(
                    array('type' => 'image', 'name' => 's', 'alt' => 'Label', 'title' => 'Title'));
            $this->assertEqual($tag->getLabel(), 'Title');
        }
        
        function testButton() {
            $tag = &new SimpleButtonTag(
                    array('type' => 'submit', 'name' => 's', 'value' => 'do'));
            $tag->addContent('I am a button');
            $this->assertEqual($tag->getName(), 's');
            $this->assertEqual($tag->getValue(), 'do');
            $this->assertEqual($tag->getLabel(), 'I am a button');
            $this->assertEqual($tag->getSubmitValues(), array('s' => 'do'));
        }
    }
    
    class TestOfTextArea extends UnitTestCase {
        
        function testDefault() {
            $tag = &new SimpleTextAreaTag(array('name' => 'a'));
            $tag->addContent('Some text');
            $this->assertEqual($tag->getName(), 'a');
            $this->assertEqual($tag->getDefault(), 'Some text');
        }
        
        function testWrapping() {
            $tag = &new SimpleTextAreaTag(array('cols' => '10', 'wrap' => 'physical'));
            $tag->addContent("Lot's of text that should be wrapped");
            $this->assertEqual(
                    $tag->getDefault(),
                    "Lot's of\ntext that\nshould be\nwrapped");
            $tag->setValue("New long text\nwith two lines");
            $this->assertEqual(
                    $tag->getValue(),
                    "New long\ntext\nwith two\nlines");
        }
    }
    
    class TestOfSelection extends UnitTestCase {
        
        function testEmpty() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $this->assertIdentical($tag->getValue(), '');
        }
        
        function testSingle() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $option = &new SimpleOptionTag(array());
            $option->addContent('AAA');
            $tag->addTag($option);
            $this->assertEqual($tag->getValue(), 'AAA');
        }
        
        function testSingleDefault() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $option = &new SimpleOptionTag(array('selected' => ''));
            $option->addContent('AAA');
            $tag->addTag($option);
            $this->assertEqual($tag->getValue(), 'AAA');
        }
        
        function testSingleMappedDefault() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $option = &new SimpleOptionTag(array('selected' => '', 'value' => 'aaa'));
            $option->addContent('AAA');
            $tag->addTag($option);
            $this->assertEqual($tag->getValue(), 'aaa');
        }
        
        function testStartsWithDefault() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $a = &new SimpleOptionTag(array());
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array('selected' => ''));
            $b->addContent('BBB');
            $tag->addTag($b);
            $c = &new SimpleOptionTag(array());
            $c->addContent('CCC');
            $tag->addTag($c);
            $this->assertEqual($tag->getValue(), 'BBB');
        }
        
        function testSettingOption() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $a = &new SimpleOptionTag(array());
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array('selected' => ''));
            $b->addContent('BBB');
            $tag->addTag($b);
            $c = &new SimpleOptionTag(array());
            $c->addContent('CCC');
            $tag->setValue('AAA');
            $this->assertEqual($tag->getValue(), 'AAA');
        }
        
        function testSettingMappedOption() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $a = &new SimpleOptionTag(array('value' => 'aaa'));
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array('value' => 'bbb', 'selected' => ''));
            $b->addContent('BBB');
            $tag->addTag($b);
            $c = &new SimpleOptionTag(array('value' => 'ccc'));
            $c->addContent('CCC');
            $tag->addTag($c);
            $tag->setValue('AAA');
            $this->assertEqual($tag->getValue(), 'aaa');
        }
        
        function testSelectionDespiteSpuriousWhitespace() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $a = &new SimpleOptionTag(array());
            $a->addContent(' AAA ');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array('selected' => ''));
            $b->addContent(' BBB ');
            $tag->addTag($b);
            $c = &new SimpleOptionTag(array());
            $c->addContent(' CCC ');
            $tag->addTag($c);
            $this->assertEqual($tag->getValue(), ' BBB ');
            $tag->setValue('AAA');
            $this->assertEqual($tag->getValue(), ' AAA ');
        }
        
        function testFailToSetIllegalOption() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $a = &new SimpleOptionTag(array());
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array('selected' => ''));
            $b->addContent('BBB');
            $tag->addTag($b);
            $c = &new SimpleOptionTag(array());
            $c->addContent('CCC');
            $tag->addTag($c);
            $this->assertFalse($tag->setValue('Not present'));
            $this->assertEqual($tag->getValue(), 'BBB');
        }
        
        function testNastyOptionValuesThatLookLikeFalse() {
            $tag = &new SimpleSelectionTag(array('name' => 'a'));
            $a = &new SimpleOptionTag(array('value' => '1'));
            $a->addContent('One');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array('value' => '0'));
            $b->addContent('Zero');
            $tag->addTag($b);
            $this->assertIdentical($tag->getValue(), '1');
            $tag->setValue('Zero');
            $this->assertIdentical($tag->getValue(), '0');
        }
        
        function testBlankOption() {
            $tag = &new SimpleSelectionTag(array('name' => 'A'));
            $a = &new SimpleOptionTag(array());
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array());
            $b->addContent('b');
            $tag->addTag($b);
            $this->assertIdentical($tag->getValue(), '');
            $tag->setValue('b');
            $this->assertIdentical($tag->getValue(), 'b');
            $tag->setValue('');
            $this->assertIdentical($tag->getValue(), '');
        }
        
        function testMultipleDefaultWithNoSelections() {
            $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
            $a = &new SimpleOptionTag(array());
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array());
            $b->addContent('BBB');
            $tag->addTag($b);
            $this->assertIdentical($tag->getDefault(), array());
            $this->assertIdentical($tag->getValue(), array());
        }
        
        function testMultipleDefaultWithSelections() {
            $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
            $a = &new SimpleOptionTag(array('selected' => ''));
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array('selected' => ''));
            $b->addContent('BBB');
            $tag->addTag($b);
            $this->assertIdentical($tag->getDefault(), array('AAA', 'BBB'));
            $this->assertIdentical($tag->getValue(), array('AAA', 'BBB'));
        }
        
        function testSettingMultiple() {
            $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
            $a = &new SimpleOptionTag(array('selected' => ''));
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array());
            $b->addContent('BBB');
            $tag->addTag($b);
            $c = &new SimpleOptionTag(array('selected' => ''));
            $c->addContent('CCC');
            $tag->addTag($c);
            $this->assertIdentical($tag->getDefault(), array('AAA', 'CCC'));
            $this->assertTrue($tag->setValue(array('BBB', 'CCC')));
            $this->assertIdentical($tag->getValue(), array('BBB', 'CCC'));
            $this->assertTrue($tag->setValue(array()));
            $this->assertIdentical($tag->getValue(), array());
        }
        
        function testFailToSetIllegalOptionsInMultiple() {
            $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
            $a = &new SimpleOptionTag(array('selected' => ''));
            $a->addContent('AAA');
            $tag->addTag($a);
            $b = &new SimpleOptionTag(array());
            $b->addContent('BBB');
            $tag->addTag($b);
            $this->assertFalse($tag->setValue(array('CCC')));
            $this->assertTrue($tag->setValue(array('AAA', 'BBB')));
            $this->assertFalse($tag->setValue(array('AAA', 'CCC')));
        }
    }
    
    class TestOfRadioGroup extends UnitTestCase {
        
        function testEmptyGroup() {
            $group = &new SimpleRadioGroup();
            $this->assertIdentical($group->getDefault(), false);
            $this->assertIdentical($group->getValue(), false);
            $this->assertFalse($group->setValue('a'));
        }
        
        function testReadingSingleButtonGroup() {
            $group = &new SimpleRadioGroup();
            $group->addWidget(new SimpleRadioButtonTag(
                    array('value' => 'A', 'checked' => '')));
            $this->assertIdentical($group->getDefault(), 'A');
            $this->assertIdentical($group->getValue(), 'A');
        }
        
        function testReadingMultipleButtonGroup() {
            $group = &new SimpleRadioGroup();
            $group->addWidget(new SimpleRadioButtonTag(
                    array('value' => 'A')));
            $group->addWidget(new SimpleRadioButtonTag(
                    array('value' => 'B', 'checked' => '')));
            $this->assertIdentical($group->getDefault(), 'B');
            $this->assertIdentical($group->getValue(), 'B');
        }
        
        function testFailToSetUnlistedValue() {
            $group = &new SimpleRadioGroup();
            $group->addWidget(new SimpleRadioButtonTag(array('value' => 'z')));
            $this->assertFalse($group->setValue('a'));
            $this->assertIdentical($group->getValue(), false);
        }
        
        function testSettingNewValueClearsTheOldOne() {
            $group = &new SimpleRadioGroup();
            $group->addWidget(new SimpleRadioButtonTag(
                    array('value' => 'A')));
            $group->addWidget(new SimpleRadioButtonTag(
                    array('value' => 'B', 'checked' => '')));
            $this->assertTrue($group->setValue('A'));
            $this->assertIdentical($group->getValue(), 'A');
        }
        
        function testIsIdMatchesAnyWidgetInSet() {
            $group = &new SimpleRadioGroup();
            $group->addWidget(new SimpleRadioButtonTag(
                    array('value' => 'A', 'id' => 'i1')));
            $group->addWidget(new SimpleRadioButtonTag(
                    array('value' => 'B', 'id' => 'i2')));
            $this->assertFalse($group->isId('i0'));
            $this->assertTrue($group->isId('i1'));
            $this->assertTrue($group->isId('i2'));
        }
    }
    
    class TestOfTagGroup extends UnitTestCase {
        
        function testReadingMultipleCheckboxGroup() {
            $group = &new SimpleCheckboxGroup();
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
            $group->addWidget(new SimpleCheckboxTag(
                    array('value' => 'B', 'checked' => '')));
            $this->assertIdentical($group->getDefault(), 'B');
            $this->assertIdentical($group->getValue(), 'B');
        }
        
        function testReadingMultipleUncheckedItems() {
            $group = &new SimpleCheckboxGroup();
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));            
            $this->assertIdentical($group->getDefault(), false);
            $this->assertIdentical($group->getValue(), false);
        }
        
        function testReadingMultipleCheckedItems() {
            $group = &new SimpleCheckboxGroup();
            $group->addWidget(new SimpleCheckboxTag(
                    array('value' => 'A', 'checked' => '')));
            $group->addWidget(new SimpleCheckboxTag(
                    array('value' => 'B', 'checked' => '')));
            $this->assertIdentical($group->getDefault(), array('A', 'B'));
            $this->assertIdentical($group->getValue(), array('A', 'B'));
        }
        
        function testSettingSingleValue() {
            $group = &new SimpleCheckboxGroup();
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));
            $this->assertTrue($group->setValue('A'));
            $this->assertIdentical($group->getValue(), 'A');
            $this->assertTrue($group->setValue('B'));
            $this->assertIdentical($group->getValue(), 'B');
        }
        
        function testSettingMultipleValues() {
            $group = &new SimpleCheckboxGroup();
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));
            $this->assertTrue($group->setValue(array('A', 'B')));
            $this->assertIdentical($group->getValue(), array('A', 'B'));
        }
        
        function testSettingNoValue() {
            $group = &new SimpleCheckboxGroup();
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
            $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));
            $this->assertTrue($group->setValue(false));
            $this->assertIdentical($group->getValue(), false);
        }
        
        function testIsIdMatchesAnyIdInSet() {
            $group = &new SimpleCheckboxGroup();
            $group->addWidget(new SimpleCheckboxTag(array('id' => 1, 'value' => 'A')));
            $group->addWidget(new SimpleCheckboxTag(array('id' => 2, 'value' => 'B')));
            $this->assertFalse($group->isId(0));
            $this->assertTrue($group->isId(1));
            $this->assertTrue($group->isId(2));
        }
    }
?>