3 * Base include file for SimpleTest.
5 * @subpackage WebTester
6 * @version $Id: tag.php,v 1.65 2004/08/18 23:10:19 lastcraft Exp $
10 * include SimpleTest files
12 require_once(dirname(__FILE__
) . '/options.php');
18 * @subpackage WebTester
26 * Starts with a named tag with attributes only.
27 * @param string $name Tag name.
28 * @param hash $attributes Attribute names and
31 function SimpleTag($name, $attributes) {
33 $this->_attributes
= $this->_keysToLowerCase($attributes);
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.
43 function _keysToLowerCase($map) {
45 foreach ($map as $key => $value) {
46 $lower[strtolower($key)] = $value;
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.
57 function expectEndTag() {
62 * Appends string content to the current content.
63 * @param string $content Additional text.
66 function addContent($content) {
67 $this->_content
.= (string)$content;
71 * Adds an enclosed tag to the content.
72 * @param SimpleTag $tag New tag.
75 function addTag(&$tag) {
79 * Accessor for tag name.
80 * @return string Name of tag.
83 function getTagName() {
88 * List oflegal child elements.
89 * @return array List of element names.
92 function getChildElements() {
97 * Accessor for an attribute.
98 * @param string $label Attribute name.
99 * @return string Attribute value.
102 function getAttribute($label) {
103 $label = strtolower($label);
104 if (! isset($this->_attributes
[$label])) {
107 if ($this->_attributes
[$label] === '') {
110 return (string)$this->_attributes
[$label];
115 * @param string $label Attribute name.
116 * @return string $value New attribute value.
119 function _setAttribute($label, $value) {
120 $this->_attributes
[strtolower($label)] = $value;
124 * Accessor for the whole content so far.
125 * @return string Content as big string.
128 function getContent() {
129 return $this->_content
;
135 * @package SimpleTest
136 * @subpackage WebTester
138 class SimpleTitleTag
extends SimpleTag
{
141 * Starts with a named tag with attributes only.
142 * @param hash $attributes Attribute names and
145 function SimpleTitleTag($attributes) {
146 $this->SimpleTag('title', $attributes);
152 * @package SimpleTest
153 * @subpackage WebTester
155 class SimpleAnchorTag
extends SimpleTag
{
158 * Starts with a named tag with attributes only.
159 * @param hash $attributes Attribute names and
162 function SimpleAnchorTag($attributes) {
163 $this->SimpleTag('a', $attributes);
167 * Accessor for URL as string.
168 * @return string Coerced as string.
172 $url = $this->getAttribute('href');
182 * @package SimpleTest
183 * @subpackage WebTester
185 class SimpleWidget
extends SimpleTag
{
190 * Starts with a named tag with attributes only.
191 * @param string $name Tag name.
192 * @param hash $attributes Attribute names and
195 function SimpleWidget($name, $attributes) {
196 $this->SimpleTag($name, $attributes);
197 $this->_value
= false
;
198 $this->_is_set
= false
;
202 * Accessor for name submitted as the key in
203 * GET/POST variables hash.
204 * @return string Parsed value.
208 return $this->getAttribute('name');
212 * Accessor for default value parsed with the tag.
213 * @return string Parsed value.
216 function getDefault() {
217 $default = $this->getAttribute('value');
218 if ($default === true
) {
221 if ($default === false
) {
228 * Accessor for currently set value or default if
230 * @return string Value set by form or default
234 function getValue() {
235 if (! $this->_is_set
) {
236 return $this->getDefault();
238 return $this->_value
;
242 * Sets the current form element value.
243 * @param string $value New value.
244 * @return boolean True if allowed.
247 function setValue($value) {
248 $this->_value
= $value;
249 $this->_is_set
= true
;
254 * Resets the form element value back to the
258 function resetValue() {
259 $this->_is_set
= false
;
264 * Text, password and hidden field.
265 * @package SimpleTest
266 * @subpackage WebTester
268 class SimpleTextTag
extends SimpleWidget
{
271 * Starts with a named tag with attributes only.
272 * @param hash $attributes Attribute names and
275 function SimpleTextTag($attributes) {
276 $this->SimpleWidget('input', $attributes);
277 if ($this->getAttribute('value') === false
) {
278 $this->_setAttribute('value', '');
283 * Tag contains no content.
284 * @return boolean False.
287 function expectEndTag() {
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.
298 function setValue($value) {
299 if ($this->getAttribute('type') == 'hidden') {
302 return parent
::setValue($value);
307 * Submit button as input tag.
308 * @package SimpleTest
309 * @subpackage WebTester
311 class SimpleSubmitTag
extends SimpleWidget
{
314 * Starts with a named tag with attributes only.
315 * @param hash $attributes Attribute names and
318 function SimpleSubmitTag($attributes) {
319 $this->SimpleWidget('input', $attributes);
320 if ($this->getAttribute('name') === false
) {
321 $this->_setAttribute('name', 'submit');
323 if ($this->getAttribute('value') === false
) {
324 $this->_setAttribute('value', 'Submit');
329 * Tag contains no end element.
330 * @return boolean False.
333 function expectEndTag() {
338 * Disables the setting of the button value.
339 * @param string $value Ignored.
340 * @return boolean True if allowed.
343 function setValue($value) {
348 * Value of browser visible text.
349 * @return string Visible label.
352 function getLabel() {
353 return $this->getValue();
357 * Gets the values submitted as a form.
358 * @return array Hash of name and values.
361 function getSubmitValues() {
362 return array($this->getName() => $this->getValue());
367 * Image button as input tag.
368 * @package SimpleTest
369 * @subpackage WebTester
371 class SimpleImageSubmitTag
extends SimpleWidget
{
374 * Starts with a named tag with attributes only.
375 * @param hash $attributes Attribute names and
378 function SimpleImageSubmitTag($attributes) {
379 $this->SimpleWidget('input', $attributes);
383 * Tag contains no end element.
384 * @return boolean False.
387 function expectEndTag() {
392 * Disables the setting of the button value.
393 * @param string $value Ignored.
394 * @return boolean True if allowed.
397 function setValue($value) {
402 * Value of browser visible text.
403 * @return string Visible label.
406 function getLabel() {
407 if ($this->getAttribute('title')) {
408 return $this->getAttribute('title');
410 return $this->getAttribute('alt');
414 * Gets the values submitted as a form.
415 * @return array Hash of name and values.
418 function getSubmitValues($x, $y) {
420 $this->getName() . '.x' => $x,
421 $this->getName() . '.y' => $y);
426 * Submit button as button tag.
427 * @package SimpleTest
428 * @subpackage WebTester
430 class SimpleButtonTag
extends SimpleWidget
{
433 * Starts with a named tag with attributes only.
434 * Defaults are very browser dependent.
435 * @param hash $attributes Attribute names and
438 function SimpleButtonTag($attributes) {
439 $this->SimpleWidget('button', $attributes);
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.
448 function expectEndTag() {
453 * Disables the setting of the button value.
454 * @param string $value Ignored.
455 * @return boolean True if allowed.
458 function setValue($value) {
463 * Value of browser visible text.
464 * @return string Visible label.
467 function getLabel() {
468 return $this->getContent();
472 * Gets the values submitted as a form. Gone
473 * for the Mozilla defaults values.
474 * @return array Hash of name and values.
477 function getSubmitValues() {
478 if ($this->getAttribute('name') === false
) {
481 if ($this->getAttribute('value') === false
) {
482 return array($this->getName() => '');
484 return array($this->getName() => $this->getValue());
489 * Content tag for text area.
490 * @package SimpleTest
491 * @subpackage WebTester
493 class SimpleTextAreaTag
extends SimpleWidget
{
496 * Starts with a named tag with attributes only.
497 * @param hash $attributes Attribute names and
500 function SimpleTextAreaTag($attributes) {
501 $this->SimpleWidget('textarea', $attributes);
505 * Accessor for starting value.
506 * @return string Parsed value.
509 function getDefault() {
510 if ($this->_wrapIsEnabled()) {
513 (integer)$this->getAttribute('cols'),
516 return $this->getContent();
520 * Applies word wrapping if needed.
521 * @param string $value New value.
522 * @return boolean True if allowed.
525 function setValue($value) {
526 if ($this->_wrapIsEnabled()) {
529 (integer)$this->getAttribute('cols'),
532 return parent
::setValue($value);
536 * Test to see if text should be wrapped.
537 * @return boolean True if wrapping on.
540 function _wrapIsEnabled() {
541 if ($this->getAttribute('cols')) {
542 $wrap = $this->getAttribute('wrap');
543 if (($wrap == 'physical') ||
($wrap == 'hard')) {
553 * @package SimpleTest
554 * @subpackage WebTester
556 class SimpleCheckboxTag
extends SimpleWidget
{
559 * Starts with attributes only.
560 * @param hash $attributes Attribute names and
563 function SimpleCheckboxTag($attributes) {
564 $this->SimpleWidget('input', $attributes);
565 if ($this->getAttribute('value') === false
) {
566 $this->_setAttribute('value', 'on');
571 * Tag contains no content.
572 * @return boolean False.
575 function expectEndTag() {
580 * The only allowed value in the one in the
581 * "value" attribute. The default for this
583 * @param string $value New value.
584 * @return boolean True if allowed.
587 function setValue($value) {
588 if ($value === false
) {
589 return parent
::setValue($value);
591 if ($value != $this->getAttribute('value')) {
594 return parent
::setValue($value);
598 * Accessor for starting value. The default
600 * @return string Parsed value.
603 function getDefault() {
604 if ($this->getAttribute('checked')) {
605 return $this->getAttribute('value');
613 * @package SimpleTest
614 * @subpackage WebTester
616 class SimpleSelectionTag
extends SimpleWidget
{
621 * Starts with attributes only.
622 * @param hash $attributes Attribute names and
625 function SimpleSelectionTag($attributes) {
626 $this->SimpleWidget('select', $attributes);
627 $this->_options
= array();
628 $this->_choice
= false
;
632 * Adds an option tag to a selection field.
633 * @param SimpleOptionTag $tag New option.
636 function addTag(&$tag) {
637 if ($tag->getTagName() == 'option') {
638 $this->_options
[] = &$tag;
643 * Text within the selection element is ignored.
644 * @param string $content Ignored.
647 function addContent($content) {
651 * Scans options for defaults. If none, then
652 * the first option is selected.
653 * @return string Selected field.
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();
662 if (count($this->_options
) > 0) {
663 return $this->_options
[0]->getDefault();
669 * Can only set allowed values.
670 * @param string $value New choice.
671 * @return boolean True if allowed.
674 function setValue($value) {
675 for ($i = 0; $i < count($this->_options
); $i++
) {
676 if ($this->_options
[$i]->getContent() === $value) {
685 * Accessor for current selection value.
686 * @return string Value attribute or
690 function getValue() {
691 if ($this->_choice
=== false
) {
692 return $this->getDefault();
694 return $this->_options
[$this->_choice
]->getValue();
700 * @package SimpleTest
701 * @subpackage WebTester
703 class MultipleSelectionTag
extends SimpleWidget
{
708 * Starts with attributes only.
709 * @param hash $attributes Attribute names and
712 function MultipleSelectionTag($attributes) {
713 $this->SimpleWidget('select', $attributes);
714 $this->_options
= array();
715 $this->_values
= false
;
719 * Adds an option tag to a selection field.
720 * @param SimpleOptionTag $tag New option.
723 function addTag(&$tag) {
724 if ($tag->getTagName() == 'option') {
725 $this->_options
[] = &$tag;
730 * Text within the selection element is ignored.
731 * @param string $content Ignored.
734 function addContent($content) {
738 * Scans options for defaults to populate the
740 * @return array Selected fields.
743 function getDefault() {
745 for ($i = 0; $i < count($this->_options
); $i++
) {
746 if ($this->_options
[$i]->getAttribute('selected')) {
747 $default[] = $this->_options
[$i]->getDefault();
754 * Can only set allowed values.
755 * @param array $values New choices.
756 * @return boolean True if allowed.
759 function setValue($values) {
760 foreach ($values as $value) {
762 for ($i = 0; $i < count($this->_options
); $i++
) {
763 if ($this->_options
[$i]->getContent() == $value) {
772 $this->_values
= $values;
777 * Accessor for current selection value.
778 * @return array List of currently set options.
781 function getValue() {
782 if ($this->_values
=== false
) {
783 return $this->getDefault();
785 return $this->_values
;
790 * Option for selection field.
791 * @package SimpleTest
792 * @subpackage WebTester
794 class SimpleOptionTag
extends SimpleWidget
{
797 * Stashes the attributes.
799 function SimpleOptionTag($attributes) {
800 $this->SimpleWidget('option', $attributes);
805 * @param string $value Ignored.
806 * @return boolean Not allowed.
809 function setValue($value) {
814 * Accessor for starting value. Will be set to
815 * the option label if no value exists.
816 * @return string Parsed value.
819 function getDefault() {
820 if ($this->getAttribute('value') === false
) {
821 return $this->getContent();
823 return $this->getAttribute('value');
829 * @package SimpleTest
830 * @subpackage WebTester
832 class SimpleRadioButtonTag
extends SimpleWidget
{
835 * Stashes the attributes.
837 function SimpleRadioButtonTag($attributes) {
838 $this->SimpleWidget('input', $attributes);
839 if ($this->getAttribute('value') === false
) {
840 $this->_setAttribute('value', 'on');
845 * Tag contains no content.
846 * @return boolean False.
849 function expectEndTag() {
854 * The only allowed value in the one in the
856 * @param string $value New value.
857 * @return boolean True if allowed.
860 function setValue($value) {
861 if ($value === false
) {
862 return parent
::setValue($value);
864 if ($value != $this->getAttribute('value')) {
867 return parent
::setValue($value);
871 * Accessor for starting value.
872 * @return string Parsed value.
875 function getDefault() {
876 if ($this->getAttribute('checked')) {
877 return $this->getAttribute('value');
884 * A group of tags with the same name within a form.
885 * @package SimpleTest
886 * @subpackage WebTester
888 class SimpleCheckboxGroup
{
895 function SimpleCheckboxGroup() {
896 $this->_widgets
= array();
900 * Adds a tag to the group.
901 * @param SimpleWidget $widget
904 function addWidget(&$widget) {
905 $this->_widgets
[] = &$widget;
909 * Accessor for current selected widget or false
911 * @return string/array Widget values or false if none.
914 function getValue() {
916 for ($i = 0; $i < count($this->_widgets
); $i++
) {
917 if ($this->_widgets
[$i]->getValue()) {
918 $values[] = $this->_widgets
[$i]->getValue();
921 return $this->_coerceValues($values);
925 * Accessor for starting value that is active.
926 * @return string/array Widget values or false if none.
929 function getDefault() {
931 for ($i = 0; $i < count($this->_widgets
); $i++
) {
932 if ($this->_widgets
[$i]->getDefault()) {
933 $values[] = $this->_widgets
[$i]->getDefault();
936 return $this->_coerceValues($values);
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.
946 function setValue($values) {
947 $values = $this->_makeArray($values);
948 if (! $this->_valuesArePossible($values)) {
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);
956 $this->_widgets
[$i]->setValue(false
);
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
970 function _valuesArePossible($values) {
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;
978 return ($values == $matches);
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.
989 function _coerceValues($values) {
990 if (count($values) == 0) {
992 } elseif (count($values) == 1) {
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.
1008 function _makeArray($value) {
1009 if ($value === false
) {
1012 if (is_string($value)) {
1013 return array($value);
1020 * A group of tags with the same name within a form.
1021 * Used for radio buttons.
1022 * @package SimpleTest
1023 * @subpackage WebTester
1025 class SimpleRadioGroup
{
1032 function SimpleRadioGroup() {
1033 $this->_widgets
= array();
1037 * Adds a tag to the group.
1038 * @param SimpleWidget $widget
1041 function addWidget(&$widget) {
1042 $this->_widgets
[] = &$widget;
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.
1053 function setValue($value) {
1054 if (! $this->_valueIsPossible($value)) {
1058 for ($i = 0; $i < count($this->_widgets
); $i++
) {
1059 if (! $this->_widgets
[$i]->setValue($value)) {
1060 $this->_widgets
[$i]->setValue(false
);
1067 * Tests to see if a value is allowed.
1068 * @param string Attempted value.
1069 * @return boolean True if a valid value.
1072 function _valueIsPossible($value) {
1073 for ($i = 0; $i < count($this->_widgets
); $i++
) {
1074 if ($this->_widgets
[$i]->getAttribute('value') == $value) {
1082 * Accessor for current selected widget or false
1084 * @return string/boolean Value attribute or
1088 function getValue() {
1089 for ($i = 0; $i < count($this->_widgets
); $i++
) {
1090 if ($this->_widgets
[$i]->getValue()) {
1091 return $this->_widgets
[$i]->getValue();
1098 * Accessor for starting value that is active.
1099 * @return string/boolean Value of first checked
1100 * widget or false if none.
1103 function getDefault() {
1104 for ($i = 0; $i < count($this->_widgets
); $i++
) {
1105 if ($this->_widgets
[$i]->getDefault()) {
1106 return $this->_widgets
[$i]->getDefault();
1114 * Tag to aid parsing the form.
1115 * @package SimpleTest
1116 * @subpackage WebTester
1118 class SimpleFormTag
extends SimpleTag
{
1121 * Starts with a named tag with attributes only.
1122 * @param hash $attributes Attribute names and
1125 function SimpleFormTag($attributes) {
1126 $this->SimpleTag('form', $attributes);
1131 * Tag to aid parsing the frames in a page.
1132 * @package SimpleTest
1133 * @subpackage WebTester
1135 class SimpleFrameTag
extends SimpleTag
{
1138 * Starts with a named tag with attributes only.
1139 * @param hash $attributes Attribute names and
1142 function SimpleFrameTag($attributes) {
1143 $this->SimpleTag('frame', $attributes);
1147 * Tag contains no content.
1148 * @return boolean False.
1151 function expectEndTag() {