0337d704 |
1 | <?php |
2 | /** |
3 | * Base include file for SimpleTest. |
4 | * @package SimpleTest |
5 | * @subpackage WebTester |
6 | * @version $Id: tag.php,v 1.65 2004/08/18 23:10:19 lastcraft Exp $ |
7 | */ |
8 | |
9 | /**#@+ |
10 | * include SimpleTest files |
11 | */ |
12 | require_once(dirname(__FILE__) . '/options.php'); |
13 | /**#@-*/ |
14 | |
15 | /** |
16 | * HTML or XML tag. |
17 | * @package SimpleTest |
18 | * @subpackage WebTester |
19 | */ |
20 | class SimpleTag { |
21 | var $_name; |
22 | var $_attributes; |
23 | var $_content; |
24 | |
25 | /** |
26 | * Starts with a named tag with attributes only. |
27 | * @param string $name Tag name. |
28 | * @param hash $attributes Attribute names and |
29 | * string values. |
30 | */ |
31 | function SimpleTag($name, $attributes) { |
32 | $this->_name = $name; |
33 | $this->_attributes = $this->_keysToLowerCase($attributes); |
34 | $this->_content = ''; |
35 | } |
36 | |
37 | /** |
38 | * Make the keys lower case for case insensitive look-ups. |
39 | * @param hash $map Hash to convert. |
40 | * @return hash Unchanged values, but keys lower case. |
41 | * @access private |
42 | */ |
43 | function _keysToLowerCase($map) { |
44 | $lower = array(); |
45 | foreach ($map as $key => $value) { |
46 | $lower[strtolower($key)] = $value; |
47 | } |
48 | return $lower; |
49 | } |
50 | |
51 | /** |
52 | * Check to see if the tag can have both start and |
53 | * end tags with content in between. |
54 | * @return boolean True if content allowed. |
55 | * @access public |
56 | */ |
57 | function expectEndTag() { |
58 | return true; |
59 | } |
60 | |
61 | /** |
62 | * Appends string content to the current content. |
63 | * @param string $content Additional text. |
64 | * @access public |
65 | */ |
66 | function addContent($content) { |
67 | $this->_content .= (string)$content; |
68 | } |
69 | |
70 | /** |
71 | * Adds an enclosed tag to the content. |
72 | * @param SimpleTag $tag New tag. |
73 | * @access public |
74 | */ |
75 | function addTag(&$tag) { |
76 | } |
77 | |
78 | /** |
79 | * Accessor for tag name. |
80 | * @return string Name of tag. |
81 | * @access public |
82 | */ |
83 | function getTagName() { |
84 | return $this->_name; |
85 | } |
86 | |
87 | /** |
88 | * List oflegal child elements. |
89 | * @return array List of element names. |
90 | * @access public |
91 | */ |
92 | function getChildElements() { |
93 | return array(); |
94 | } |
95 | |
96 | /** |
97 | * Accessor for an attribute. |
98 | * @param string $label Attribute name. |
99 | * @return string Attribute value. |
100 | * @access public |
101 | */ |
102 | function getAttribute($label) { |
103 | $label = strtolower($label); |
104 | if (! isset($this->_attributes[$label])) { |
105 | return false; |
106 | } |
107 | if ($this->_attributes[$label] === '') { |
108 | return true; |
109 | } |
110 | return (string)$this->_attributes[$label]; |
111 | } |
112 | |
113 | /** |
114 | * Sets an attribute. |
115 | * @param string $label Attribute name. |
116 | * @return string $value New attribute value. |
117 | * @access protected |
118 | */ |
119 | function _setAttribute($label, $value) { |
120 | $this->_attributes[strtolower($label)] = $value; |
121 | } |
122 | |
123 | /** |
124 | * Accessor for the whole content so far. |
125 | * @return string Content as big string. |
126 | * @access public |
127 | */ |
128 | function getContent() { |
129 | return $this->_content; |
130 | } |
131 | } |
132 | |
133 | /** |
134 | * Page title. |
135 | * @package SimpleTest |
136 | * @subpackage WebTester |
137 | */ |
138 | class SimpleTitleTag extends SimpleTag { |
139 | |
140 | /** |
141 | * Starts with a named tag with attributes only. |
142 | * @param hash $attributes Attribute names and |
143 | * string values. |
144 | */ |
145 | function SimpleTitleTag($attributes) { |
146 | $this->SimpleTag('title', $attributes); |
147 | } |
148 | } |
149 | |
150 | /** |
151 | * Link. |
152 | * @package SimpleTest |
153 | * @subpackage WebTester |
154 | */ |
155 | class SimpleAnchorTag extends SimpleTag { |
156 | |
157 | /** |
158 | * Starts with a named tag with attributes only. |
159 | * @param hash $attributes Attribute names and |
160 | * string values. |
161 | */ |
162 | function SimpleAnchorTag($attributes) { |
163 | $this->SimpleTag('a', $attributes); |
164 | } |
165 | |
166 | /** |
167 | * Accessor for URL as string. |
168 | * @return string Coerced as string. |
169 | * @access public |
170 | */ |
171 | function getHref() { |
172 | $url = $this->getAttribute('href'); |
173 | if (is_bool($url)) { |
174 | $url = ''; |
175 | } |
176 | return $url; |
177 | } |
178 | } |
179 | |
180 | /** |
181 | * Form element. |
182 | * @package SimpleTest |
183 | * @subpackage WebTester |
184 | */ |
185 | class SimpleWidget extends SimpleTag { |
186 | var $_value; |
187 | var $_is_set; |
188 | |
189 | /** |
190 | * Starts with a named tag with attributes only. |
191 | * @param string $name Tag name. |
192 | * @param hash $attributes Attribute names and |
193 | * string values. |
194 | */ |
195 | function SimpleWidget($name, $attributes) { |
196 | $this->SimpleTag($name, $attributes); |
197 | $this->_value = false; |
198 | $this->_is_set = false; |
199 | } |
200 | |
201 | /** |
202 | * Accessor for name submitted as the key in |
203 | * GET/POST variables hash. |
204 | * @return string Parsed value. |
205 | * @access public |
206 | */ |
207 | function getName() { |
208 | return $this->getAttribute('name'); |
209 | } |
210 | |
211 | /** |
212 | * Accessor for default value parsed with the tag. |
213 | * @return string Parsed value. |
214 | * @access public |
215 | */ |
216 | function getDefault() { |
217 | $default = $this->getAttribute('value'); |
218 | if ($default === true) { |
219 | $default = ''; |
220 | } |
221 | if ($default === false) { |
222 | $default = ''; |
223 | } |
224 | return $default; |
225 | } |
226 | |
227 | /** |
228 | * Accessor for currently set value or default if |
229 | * none. |
230 | * @return string Value set by form or default |
231 | * if none. |
232 | * @access public |
233 | */ |
234 | function getValue() { |
235 | if (! $this->_is_set) { |
236 | return $this->getDefault(); |
237 | } |
238 | return $this->_value; |
239 | } |
240 | |
241 | /** |
242 | * Sets the current form element value. |
243 | * @param string $value New value. |
244 | * @return boolean True if allowed. |
245 | * @access public |
246 | */ |
247 | function setValue($value) { |
248 | $this->_value = $value; |
249 | $this->_is_set = true; |
250 | return true; |
251 | } |
252 | |
253 | /** |
254 | * Resets the form element value back to the |
255 | * default. |
256 | * @access public |
257 | */ |
258 | function resetValue() { |
259 | $this->_is_set = false; |
260 | } |
261 | } |
262 | |
263 | /** |
264 | * Text, password and hidden field. |
265 | * @package SimpleTest |
266 | * @subpackage WebTester |
267 | */ |
268 | class SimpleTextTag extends SimpleWidget { |
269 | |
270 | /** |
271 | * Starts with a named tag with attributes only. |
272 | * @param hash $attributes Attribute names and |
273 | * string values. |
274 | */ |
275 | function SimpleTextTag($attributes) { |
276 | $this->SimpleWidget('input', $attributes); |
277 | if ($this->getAttribute('value') === false) { |
278 | $this->_setAttribute('value', ''); |
279 | } |
280 | } |
281 | |
282 | /** |
283 | * Tag contains no content. |
284 | * @return boolean False. |
285 | * @access public |
286 | */ |
287 | function expectEndTag() { |
288 | return false; |
289 | } |
290 | |
291 | /** |
292 | * Sets the current form element value. Cannot |
293 | * change the value of a hidden field. |
294 | * @param string $value New value. |
295 | * @return boolean True if allowed. |
296 | * @access public |
297 | */ |
298 | function setValue($value) { |
299 | if ($this->getAttribute('type') == 'hidden') { |
300 | return false; |
301 | } |
302 | return parent::setValue($value); |
303 | } |
304 | } |
305 | |
306 | /** |
307 | * Submit button as input tag. |
308 | * @package SimpleTest |
309 | * @subpackage WebTester |
310 | */ |
311 | class SimpleSubmitTag extends SimpleWidget { |
312 | |
313 | /** |
314 | * Starts with a named tag with attributes only. |
315 | * @param hash $attributes Attribute names and |
316 | * string values. |
317 | */ |
318 | function SimpleSubmitTag($attributes) { |
319 | $this->SimpleWidget('input', $attributes); |
320 | if ($this->getAttribute('name') === false) { |
321 | $this->_setAttribute('name', 'submit'); |
322 | } |
323 | if ($this->getAttribute('value') === false) { |
324 | $this->_setAttribute('value', 'Submit'); |
325 | } |
326 | } |
327 | |
328 | /** |
329 | * Tag contains no end element. |
330 | * @return boolean False. |
331 | * @access public |
332 | */ |
333 | function expectEndTag() { |
334 | return false; |
335 | } |
336 | |
337 | /** |
338 | * Disables the setting of the button value. |
339 | * @param string $value Ignored. |
340 | * @return boolean True if allowed. |
341 | * @access public |
342 | */ |
343 | function setValue($value) { |
344 | return false; |
345 | } |
346 | |
347 | /** |
348 | * Value of browser visible text. |
349 | * @return string Visible label. |
350 | * @access public |
351 | */ |
352 | function getLabel() { |
353 | return $this->getValue(); |
354 | } |
355 | |
356 | /** |
357 | * Gets the values submitted as a form. |
358 | * @return array Hash of name and values. |
359 | * @access public |
360 | */ |
361 | function getSubmitValues() { |
362 | return array($this->getName() => $this->getValue()); |
363 | } |
364 | } |
365 | |
366 | /** |
367 | * Image button as input tag. |
368 | * @package SimpleTest |
369 | * @subpackage WebTester |
370 | */ |
371 | class SimpleImageSubmitTag extends SimpleWidget { |
372 | |
373 | /** |
374 | * Starts with a named tag with attributes only. |
375 | * @param hash $attributes Attribute names and |
376 | * string values. |
377 | */ |
378 | function SimpleImageSubmitTag($attributes) { |
379 | $this->SimpleWidget('input', $attributes); |
380 | } |
381 | |
382 | /** |
383 | * Tag contains no end element. |
384 | * @return boolean False. |
385 | * @access public |
386 | */ |
387 | function expectEndTag() { |
388 | return false; |
389 | } |
390 | |
391 | /** |
392 | * Disables the setting of the button value. |
393 | * @param string $value Ignored. |
394 | * @return boolean True if allowed. |
395 | * @access public |
396 | */ |
397 | function setValue($value) { |
398 | return false; |
399 | } |
400 | |
401 | /** |
402 | * Value of browser visible text. |
403 | * @return string Visible label. |
404 | * @access public |
405 | */ |
406 | function getLabel() { |
407 | if ($this->getAttribute('title')) { |
408 | return $this->getAttribute('title'); |
409 | } |
410 | return $this->getAttribute('alt'); |
411 | } |
412 | |
413 | /** |
414 | * Gets the values submitted as a form. |
415 | * @return array Hash of name and values. |
416 | * @access public |
417 | */ |
418 | function getSubmitValues($x, $y) { |
419 | return array( |
420 | $this->getName() . '.x' => $x, |
421 | $this->getName() . '.y' => $y); |
422 | } |
423 | } |
424 | |
425 | /** |
426 | * Submit button as button tag. |
427 | * @package SimpleTest |
428 | * @subpackage WebTester |
429 | */ |
430 | class SimpleButtonTag extends SimpleWidget { |
431 | |
432 | /** |
433 | * Starts with a named tag with attributes only. |
434 | * Defaults are very browser dependent. |
435 | * @param hash $attributes Attribute names and |
436 | * string values. |
437 | */ |
438 | function SimpleButtonTag($attributes) { |
439 | $this->SimpleWidget('button', $attributes); |
440 | } |
441 | |
442 | /** |
443 | * Check to see if the tag can have both start and |
444 | * end tags with content in between. |
445 | * @return boolean True if content allowed. |
446 | * @access public |
447 | */ |
448 | function expectEndTag() { |
449 | return true; |
450 | } |
451 | |
452 | /** |
453 | * Disables the setting of the button value. |
454 | * @param string $value Ignored. |
455 | * @return boolean True if allowed. |
456 | * @access public |
457 | */ |
458 | function setValue($value) { |
459 | return false; |
460 | } |
461 | |
462 | /** |
463 | * Value of browser visible text. |
464 | * @return string Visible label. |
465 | * @access public |
466 | */ |
467 | function getLabel() { |
468 | return $this->getContent(); |
469 | } |
470 | |
471 | /** |
472 | * Gets the values submitted as a form. Gone |
473 | * for the Mozilla defaults values. |
474 | * @return array Hash of name and values. |
475 | * @access public |
476 | */ |
477 | function getSubmitValues() { |
478 | if ($this->getAttribute('name') === false) { |
479 | return array(); |
480 | } |
481 | if ($this->getAttribute('value') === false) { |
482 | return array($this->getName() => ''); |
483 | } |
484 | return array($this->getName() => $this->getValue()); |
485 | } |
486 | } |
487 | |
488 | /** |
489 | * Content tag for text area. |
490 | * @package SimpleTest |
491 | * @subpackage WebTester |
492 | */ |
493 | class SimpleTextAreaTag extends SimpleWidget { |
494 | |
495 | /** |
496 | * Starts with a named tag with attributes only. |
497 | * @param hash $attributes Attribute names and |
498 | * string values. |
499 | */ |
500 | function SimpleTextAreaTag($attributes) { |
501 | $this->SimpleWidget('textarea', $attributes); |
502 | } |
503 | |
504 | /** |
505 | * Accessor for starting value. |
506 | * @return string Parsed value. |
507 | * @access public |
508 | */ |
509 | function getDefault() { |
510 | if ($this->_wrapIsEnabled()) { |
511 | return wordwrap( |
512 | $this->getContent(), |
513 | (integer)$this->getAttribute('cols'), |
514 | "\n"); |
515 | } |
516 | return $this->getContent(); |
517 | } |
518 | |
519 | /** |
520 | * Applies word wrapping if needed. |
521 | * @param string $value New value. |
522 | * @return boolean True if allowed. |
523 | * @access public |
524 | */ |
525 | function setValue($value) { |
526 | if ($this->_wrapIsEnabled()) { |
527 | $value = wordwrap( |
528 | $value, |
529 | (integer)$this->getAttribute('cols'), |
530 | "\n"); |
531 | } |
532 | return parent::setValue($value); |
533 | } |
534 | |
535 | /** |
536 | * Test to see if text should be wrapped. |
537 | * @return boolean True if wrapping on. |
538 | * @access private |
539 | */ |
540 | function _wrapIsEnabled() { |
541 | if ($this->getAttribute('cols')) { |
542 | $wrap = $this->getAttribute('wrap'); |
543 | if (($wrap == 'physical') || ($wrap == 'hard')) { |
544 | return true; |
545 | } |
546 | } |
547 | return false; |
548 | } |
549 | } |
550 | |
551 | /** |
552 | * Checkbox widget. |
553 | * @package SimpleTest |
554 | * @subpackage WebTester |
555 | */ |
556 | class SimpleCheckboxTag extends SimpleWidget { |
557 | |
558 | /** |
559 | * Starts with attributes only. |
560 | * @param hash $attributes Attribute names and |
561 | * string values. |
562 | */ |
563 | function SimpleCheckboxTag($attributes) { |
564 | $this->SimpleWidget('input', $attributes); |
565 | if ($this->getAttribute('value') === false) { |
566 | $this->_setAttribute('value', 'on'); |
567 | } |
568 | } |
569 | |
570 | /** |
571 | * Tag contains no content. |
572 | * @return boolean False. |
573 | * @access public |
574 | */ |
575 | function expectEndTag() { |
576 | return false; |
577 | } |
578 | |
579 | /** |
580 | * The only allowed value in the one in the |
581 | * "value" attribute. The default for this |
582 | * attribute is "on". |
583 | * @param string $value New value. |
584 | * @return boolean True if allowed. |
585 | * @access public |
586 | */ |
587 | function setValue($value) { |
588 | if ($value === false) { |
589 | return parent::setValue($value); |
590 | } |
591 | if ($value != $this->getAttribute('value')) { |
592 | return false; |
593 | } |
594 | return parent::setValue($value); |
595 | } |
596 | |
597 | /** |
598 | * Accessor for starting value. The default |
599 | * value is "on". |
600 | * @return string Parsed value. |
601 | * @access public |
602 | */ |
603 | function getDefault() { |
604 | if ($this->getAttribute('checked')) { |
605 | return $this->getAttribute('value'); |
606 | } |
607 | return false; |
608 | } |
609 | } |
610 | |
611 | /** |
612 | * Drop down widget. |
613 | * @package SimpleTest |
614 | * @subpackage WebTester |
615 | */ |
616 | class SimpleSelectionTag extends SimpleWidget { |
617 | var $_options; |
618 | var $_choice; |
619 | |
620 | /** |
621 | * Starts with attributes only. |
622 | * @param hash $attributes Attribute names and |
623 | * string values. |
624 | */ |
625 | function SimpleSelectionTag($attributes) { |
626 | $this->SimpleWidget('select', $attributes); |
627 | $this->_options = array(); |
628 | $this->_choice = false; |
629 | } |
630 | |
631 | /** |
632 | * Adds an option tag to a selection field. |
633 | * @param SimpleOptionTag $tag New option. |
634 | * @access public |
635 | */ |
636 | function addTag(&$tag) { |
637 | if ($tag->getTagName() == 'option') { |
638 | $this->_options[] = &$tag; |
639 | } |
640 | } |
641 | |
642 | /** |
643 | * Text within the selection element is ignored. |
644 | * @param string $content Ignored. |
645 | * @access public |
646 | */ |
647 | function addContent($content) { |
648 | } |
649 | |
650 | /** |
651 | * Scans options for defaults. If none, then |
652 | * the first option is selected. |
653 | * @return string Selected field. |
654 | * @access public |
655 | */ |
656 | function getDefault() { |
657 | for ($i = 0; $i < count($this->_options); $i++) { |
658 | if ($this->_options[$i]->getAttribute('selected')) { |
659 | return $this->_options[$i]->getDefault(); |
660 | } |
661 | } |
662 | if (count($this->_options) > 0) { |
663 | return $this->_options[0]->getDefault(); |
664 | } |
665 | return ''; |
666 | } |
667 | |
668 | /** |
669 | * Can only set allowed values. |
670 | * @param string $value New choice. |
671 | * @return boolean True if allowed. |
672 | * @access public |
673 | */ |
674 | function setValue($value) { |
675 | for ($i = 0; $i < count($this->_options); $i++) { |
676 | if ($this->_options[$i]->getContent() === $value) { |
677 | $this->_choice = $i; |
678 | return true; |
679 | } |
680 | } |
681 | return false; |
682 | } |
683 | |
684 | /** |
685 | * Accessor for current selection value. |
686 | * @return string Value attribute or |
687 | * content of opton. |
688 | * @access public |
689 | */ |
690 | function getValue() { |
691 | if ($this->_choice === false) { |
692 | return $this->getDefault(); |
693 | } |
694 | return $this->_options[$this->_choice]->getValue(); |
695 | } |
696 | } |
697 | |
698 | /** |
699 | * Drop down widget. |
700 | * @package SimpleTest |
701 | * @subpackage WebTester |
702 | */ |
703 | class MultipleSelectionTag extends SimpleWidget { |
704 | var $_options; |
705 | var $_values; |
706 | |
707 | /** |
708 | * Starts with attributes only. |
709 | * @param hash $attributes Attribute names and |
710 | * string values. |
711 | */ |
712 | function MultipleSelectionTag($attributes) { |
713 | $this->SimpleWidget('select', $attributes); |
714 | $this->_options = array(); |
715 | $this->_values = false; |
716 | } |
717 | |
718 | /** |
719 | * Adds an option tag to a selection field. |
720 | * @param SimpleOptionTag $tag New option. |
721 | * @access public |
722 | */ |
723 | function addTag(&$tag) { |
724 | if ($tag->getTagName() == 'option') { |
725 | $this->_options[] = &$tag; |
726 | } |
727 | } |
728 | |
729 | /** |
730 | * Text within the selection element is ignored. |
731 | * @param string $content Ignored. |
732 | * @access public |
733 | */ |
734 | function addContent($content) { |
735 | } |
736 | |
737 | /** |
738 | * Scans options for defaults to populate the |
739 | * value array(). |
740 | * @return array Selected fields. |
741 | * @access public |
742 | */ |
743 | function getDefault() { |
744 | $default = array(); |
745 | for ($i = 0; $i < count($this->_options); $i++) { |
746 | if ($this->_options[$i]->getAttribute('selected')) { |
747 | $default[] = $this->_options[$i]->getDefault(); |
748 | } |
749 | } |
750 | return $default; |
751 | } |
752 | |
753 | /** |
754 | * Can only set allowed values. |
755 | * @param array $values New choices. |
756 | * @return boolean True if allowed. |
757 | * @access public |
758 | */ |
759 | function setValue($values) { |
760 | foreach ($values as $value) { |
761 | $is_option = false; |
762 | for ($i = 0; $i < count($this->_options); $i++) { |
763 | if ($this->_options[$i]->getContent() == $value) { |
764 | $is_option = true; |
765 | break; |
766 | } |
767 | } |
768 | if (! $is_option) { |
769 | return false; |
770 | } |
771 | } |
772 | $this->_values = $values; |
773 | return true; |
774 | } |
775 | |
776 | /** |
777 | * Accessor for current selection value. |
778 | * @return array List of currently set options. |
779 | * @access public |
780 | */ |
781 | function getValue() { |
782 | if ($this->_values === false) { |
783 | return $this->getDefault(); |
784 | } |
785 | return $this->_values; |
786 | } |
787 | } |
788 | |
789 | /** |
790 | * Option for selection field. |
791 | * @package SimpleTest |
792 | * @subpackage WebTester |
793 | */ |
794 | class SimpleOptionTag extends SimpleWidget { |
795 | |
796 | /** |
797 | * Stashes the attributes. |
798 | */ |
799 | function SimpleOptionTag($attributes) { |
800 | $this->SimpleWidget('option', $attributes); |
801 | } |
802 | |
803 | /** |
804 | * Does nothing. |
805 | * @param string $value Ignored. |
806 | * @return boolean Not allowed. |
807 | * @access public |
808 | */ |
809 | function setValue($value) { |
810 | return false; |
811 | } |
812 | |
813 | /** |
814 | * Accessor for starting value. Will be set to |
815 | * the option label if no value exists. |
816 | * @return string Parsed value. |
817 | * @access public |
818 | */ |
819 | function getDefault() { |
820 | if ($this->getAttribute('value') === false) { |
821 | return $this->getContent(); |
822 | } |
823 | return $this->getAttribute('value'); |
824 | } |
825 | } |
826 | |
827 | /** |
828 | * Radio button. |
829 | * @package SimpleTest |
830 | * @subpackage WebTester |
831 | */ |
832 | class SimpleRadioButtonTag extends SimpleWidget { |
833 | |
834 | /** |
835 | * Stashes the attributes. |
836 | */ |
837 | function SimpleRadioButtonTag($attributes) { |
838 | $this->SimpleWidget('input', $attributes); |
839 | if ($this->getAttribute('value') === false) { |
840 | $this->_setAttribute('value', 'on'); |
841 | } |
842 | } |
843 | |
844 | /** |
845 | * Tag contains no content. |
846 | * @return boolean False. |
847 | * @access public |
848 | */ |
849 | function expectEndTag() { |
850 | return false; |
851 | } |
852 | |
853 | /** |
854 | * The only allowed value in the one in the |
855 | * "value" attribute. |
856 | * @param string $value New value. |
857 | * @return boolean True if allowed. |
858 | * @access public |
859 | */ |
860 | function setValue($value) { |
861 | if ($value === false) { |
862 | return parent::setValue($value); |
863 | } |
864 | if ($value != $this->getAttribute('value')) { |
865 | return false; |
866 | } |
867 | return parent::setValue($value); |
868 | } |
869 | |
870 | /** |
871 | * Accessor for starting value. |
872 | * @return string Parsed value. |
873 | * @access public |
874 | */ |
875 | function getDefault() { |
876 | if ($this->getAttribute('checked')) { |
877 | return $this->getAttribute('value'); |
878 | } |
879 | return false; |
880 | } |
881 | } |
882 | |
883 | /** |
884 | * A group of tags with the same name within a form. |
885 | * @package SimpleTest |
886 | * @subpackage WebTester |
887 | */ |
888 | class SimpleCheckboxGroup { |
889 | var $_widgets; |
890 | |
891 | /** |
892 | * Starts empty. |
893 | * @access public |
894 | */ |
895 | function SimpleCheckboxGroup() { |
896 | $this->_widgets = array(); |
897 | } |
898 | |
899 | /** |
900 | * Adds a tag to the group. |
901 | * @param SimpleWidget $widget |
902 | * @access public |
903 | */ |
904 | function addWidget(&$widget) { |
905 | $this->_widgets[] = &$widget; |
906 | } |
907 | |
908 | /** |
909 | * Accessor for current selected widget or false |
910 | * if none. |
911 | * @return string/array Widget values or false if none. |
912 | * @access public |
913 | */ |
914 | function getValue() { |
915 | $values = array(); |
916 | for ($i = 0; $i < count($this->_widgets); $i++) { |
917 | if ($this->_widgets[$i]->getValue()) { |
918 | $values[] = $this->_widgets[$i]->getValue(); |
919 | } |
920 | } |
921 | return $this->_coerceValues($values); |
922 | } |
923 | |
924 | /** |
925 | * Accessor for starting value that is active. |
926 | * @return string/array Widget values or false if none. |
927 | * @access public |
928 | */ |
929 | function getDefault() { |
930 | $values = array(); |
931 | for ($i = 0; $i < count($this->_widgets); $i++) { |
932 | if ($this->_widgets[$i]->getDefault()) { |
933 | $values[] = $this->_widgets[$i]->getDefault(); |
934 | } |
935 | } |
936 | return $this->_coerceValues($values); |
937 | } |
938 | |
939 | /** |
940 | * Accessor for current set values. |
941 | * @param string/array/boolean $values Either a single string, a |
942 | * hash or false for nothing set. |
943 | * @return boolean True if all values can be set. |
944 | * @access public |
945 | */ |
946 | function setValue($values) { |
947 | $values = $this->_makeArray($values); |
948 | if (! $this->_valuesArePossible($values)) { |
949 | return false; |
950 | } |
951 | for ($i = 0; $i < count($this->_widgets); $i++) { |
952 | $possible = $this->_widgets[$i]->getAttribute('value'); |
953 | if (in_array($this->_widgets[$i]->getAttribute('value'), $values)) { |
954 | $this->_widgets[$i]->setValue($possible); |
955 | } else { |
956 | $this->_widgets[$i]->setValue(false); |
957 | } |
958 | } |
959 | return true; |
960 | } |
961 | |
962 | /** |
963 | * Tests to see if a possible value set is legal. |
964 | * @param string/array/boolean $values Either a single string, a |
965 | * hash or false for nothing set. |
966 | * @return boolean False if trying to set a |
967 | * missing value. |
968 | * @access private |
969 | */ |
970 | function _valuesArePossible($values) { |
971 | $matches = array(); |
972 | for ($i = 0; $i < count($this->_widgets); $i++) { |
973 | $possible = $this->_widgets[$i]->getAttribute('value'); |
974 | if (in_array($possible, $values)) { |
975 | $matches[] = $possible; |
976 | } |
977 | } |
978 | return ($values == $matches); |
979 | } |
980 | |
981 | /** |
982 | * Converts the output to an appropriate format. This means |
983 | * that no values is false, a single value is just that |
984 | * value and only two or more are contained in an array. |
985 | * @param array $values List of values of widgets. |
986 | * @return string/array/boolean Expected format for a tag. |
987 | * @access private |
988 | */ |
989 | function _coerceValues($values) { |
990 | if (count($values) == 0) { |
991 | return false; |
992 | } elseif (count($values) == 1) { |
993 | return $values[0]; |
994 | } else { |
995 | return $values; |
996 | } |
997 | } |
998 | |
999 | /** |
1000 | * Converts false or string into array. The opposite of |
1001 | * the coercian method. |
1002 | * @param string/array/boolean $value A single item is converted |
1003 | * to a one item list. False |
1004 | * gives an empty list. |
1005 | * @return array List of values, possibly empty. |
1006 | * @access private |
1007 | */ |
1008 | function _makeArray($value) { |
1009 | if ($value === false) { |
1010 | return array(); |
1011 | } |
1012 | if (is_string($value)) { |
1013 | return array($value); |
1014 | } |
1015 | return $value; |
1016 | } |
1017 | } |
1018 | |
1019 | /** |
1020 | * A group of tags with the same name within a form. |
1021 | * Used for radio buttons. |
1022 | * @package SimpleTest |
1023 | * @subpackage WebTester |
1024 | */ |
1025 | class SimpleRadioGroup { |
1026 | var $_widgets; |
1027 | |
1028 | /** |
1029 | * Starts empty. |
1030 | * @access public |
1031 | */ |
1032 | function SimpleRadioGroup() { |
1033 | $this->_widgets = array(); |
1034 | } |
1035 | |
1036 | /** |
1037 | * Adds a tag to the group. |
1038 | * @param SimpleWidget $widget |
1039 | * @access public |
1040 | */ |
1041 | function addWidget(&$widget) { |
1042 | $this->_widgets[] = &$widget; |
1043 | } |
1044 | |
1045 | /** |
1046 | * Each tag is tried in turn until one is |
1047 | * successfully set. The others will be |
1048 | * unchecked if successful. |
1049 | * @param string $value New value. |
1050 | * @return boolean True if any allowed. |
1051 | * @access public |
1052 | */ |
1053 | function setValue($value) { |
1054 | if (! $this->_valueIsPossible($value)) { |
1055 | return false; |
1056 | } |
1057 | $index = false; |
1058 | for ($i = 0; $i < count($this->_widgets); $i++) { |
1059 | if (! $this->_widgets[$i]->setValue($value)) { |
1060 | $this->_widgets[$i]->setValue(false); |
1061 | } |
1062 | } |
1063 | return true; |
1064 | } |
1065 | |
1066 | /** |
1067 | * Tests to see if a value is allowed. |
1068 | * @param string Attempted value. |
1069 | * @return boolean True if a valid value. |
1070 | * @access private |
1071 | */ |
1072 | function _valueIsPossible($value) { |
1073 | for ($i = 0; $i < count($this->_widgets); $i++) { |
1074 | if ($this->_widgets[$i]->getAttribute('value') == $value) { |
1075 | return true; |
1076 | } |
1077 | } |
1078 | return false; |
1079 | } |
1080 | |
1081 | /** |
1082 | * Accessor for current selected widget or false |
1083 | * if none. |
1084 | * @return string/boolean Value attribute or |
1085 | * content of opton. |
1086 | * @access public |
1087 | */ |
1088 | function getValue() { |
1089 | for ($i = 0; $i < count($this->_widgets); $i++) { |
1090 | if ($this->_widgets[$i]->getValue()) { |
1091 | return $this->_widgets[$i]->getValue(); |
1092 | } |
1093 | } |
1094 | return false; |
1095 | } |
1096 | |
1097 | /** |
1098 | * Accessor for starting value that is active. |
1099 | * @return string/boolean Value of first checked |
1100 | * widget or false if none. |
1101 | * @access public |
1102 | */ |
1103 | function getDefault() { |
1104 | for ($i = 0; $i < count($this->_widgets); $i++) { |
1105 | if ($this->_widgets[$i]->getDefault()) { |
1106 | return $this->_widgets[$i]->getDefault(); |
1107 | } |
1108 | } |
1109 | return false; |
1110 | } |
1111 | } |
1112 | |
1113 | /** |
1114 | * Tag to aid parsing the form. |
1115 | * @package SimpleTest |
1116 | * @subpackage WebTester |
1117 | */ |
1118 | class SimpleFormTag extends SimpleTag { |
1119 | |
1120 | /** |
1121 | * Starts with a named tag with attributes only. |
1122 | * @param hash $attributes Attribute names and |
1123 | * string values. |
1124 | */ |
1125 | function SimpleFormTag($attributes) { |
1126 | $this->SimpleTag('form', $attributes); |
1127 | } |
1128 | } |
1129 | |
1130 | /** |
1131 | * Tag to aid parsing the frames in a page. |
1132 | * @package SimpleTest |
1133 | * @subpackage WebTester |
1134 | */ |
1135 | class SimpleFrameTag extends SimpleTag { |
1136 | |
1137 | /** |
1138 | * Starts with a named tag with attributes only. |
1139 | * @param hash $attributes Attribute names and |
1140 | * string values. |
1141 | */ |
1142 | function SimpleFrameTag($attributes) { |
1143 | $this->SimpleTag('frame', $attributes); |
1144 | } |
1145 | |
1146 | /** |
1147 | * Tag contains no content. |
1148 | * @return boolean False. |
1149 | * @access public |
1150 | */ |
1151 | function expectEndTag() { |
1152 | return false; |
1153 | } |
1154 | } |
1155 | ?> |