3 * Base include file for SimpleTest.
5 * @subpackage WebTester
6 * @version $Id: form.php,v 1.4 2004/08/19 00:05:52 lastcraft Exp $
10 * include SimpleTest files
12 require_once(dirname(__FILE__
) . '/tag.php');
16 * Form tag class to hold widget values.
18 * @subpackage WebTester
30 * Starts with no held controls/widgets.
31 * @param SimpleTag $tag Form tag to read.
32 * @param SimpleUrl $url Location of holding page.
34 function SimpleForm($tag, $url) {
35 $this->_method
= $tag->getAttribute('method');
36 $this->_action
= $this->_createAction($tag->getAttribute('action'), $url);
37 $this->_default_target
= false
;
38 $this->_id
= $tag->getAttribute('id');
39 $this->_buttons
= array();
40 $this->_images
= array();
41 $this->_widgets
= array();
45 * Sets the frame target within a frameset.
46 * @param string $frame Name of frame.
49 function setDefaultTarget($frame) {
50 $this->_default_target
= $frame;
54 * Accessor for form action.
55 * @return string Either get or post.
58 function getMethod() {
59 return ($this->_method ?
strtolower($this->_method
) : 'get');
63 * Combined action attribute with current location
64 * to get an absolute form target.
65 * @param string $action Action attribute from form tag.
66 * @param SimpleUrl $base Page location.
67 * @return SimpleUrl Absolute form target.
69 function _createAction($action, $base) {
70 if ($action === false
) {
73 if ($action === true
) {
74 $url = new SimpleUrl('');
76 $url = new SimpleUrl($action);
78 return $url->makeAbsolute($base);
82 * Absolute URL of the target.
83 * @return SimpleUrl URL target.
86 function getAction() {
87 $url = $this->_action
;
88 if ($this->_default_target
&& ! $url->getTarget()) {
89 $url->setTarget($this->_default_target
);
95 * ID field of form for unique identification.
96 * @return string Unique tag ID.
104 * Adds a tag contents to the form.
105 * @param SimpleWidget $tag Input tag to add.
108 function addWidget($tag) {
109 if (strtolower($tag->getAttribute('type')) == 'submit') {
110 $this->_buttons
[] = &$tag;
111 } elseif (strtolower($tag->getAttribute('type')) == 'image') {
112 $this->_images
[] = &$tag;
114 if ($tag->getName()) {
115 $this->_setWidget($tag);
121 * Sets the widget into the form, grouping radio
123 * @param SimpleWidget $tag Incoming form control.
126 function _setWidget($tag) {
127 if (strtolower($tag->getAttribute('type')) == 'radio') {
128 $this->_addRadioButton($tag);
129 } elseif (strtolower($tag->getAttribute('type')) == 'checkbox') {
130 $this->_addCheckbox($tag);
132 $this->_widgets
[$tag->getName()] = &$tag;
137 * Adds a radio button, building a group if necessary.
138 * @param SimpleRadioButtonTag $tag Incoming form control.
141 function _addRadioButton($tag) {
142 if (! isset($this->_widgets
[$tag->getName()])) {
143 $this->_widgets
[$tag->getName()] = &new SimpleRadioGroup();
145 $this->_widgets
[$tag->getName()]->addWidget($tag);
149 * Adds a checkbox, making it a group on a repeated name.
150 * @param SimpleCheckboxTag $tag Incoming form control.
153 function _addCheckbox($tag) {
154 if (! isset($this->_widgets
[$tag->getName()])) {
155 $this->_widgets
[$tag->getName()] = &$tag;
156 } elseif (! SimpleTestCompatibility
::isA($this->_widgets
[$tag->getName()], 'SimpleCheckboxGroup')) {
157 $previous = &$this->_widgets
[$tag->getName()];
158 $this->_widgets
[$tag->getName()] = &new SimpleCheckboxGroup();
159 $this->_widgets
[$tag->getName()]->addWidget($previous);
160 $this->_widgets
[$tag->getName()]->addWidget($tag);
162 $this->_widgets
[$tag->getName()]->addWidget($tag);
167 * Extracts current value from form.
168 * @param string $name Keyed by widget name.
169 * @return string Value as string or null
173 function getValue($name) {
174 if (isset($this->_widgets
[$name])) {
175 return $this->_widgets
[$name]->getValue();
177 foreach ($this->_buttons
as $button) {
178 if ($button->getName() == $name) {
179 return $button->getValue();
186 * Extracts current value from form by the ID.
187 * @param string/integer $id Keyed by widget ID attribute.
188 * @return string Value as string or null
192 function getValueById($id) {
193 foreach ($this->_widgets
as $widget) {
194 if ($widget->getAttribute('id') == $id) {
195 return $widget->getValue();
198 foreach ($this->_buttons
as $button) {
199 if ($button->getAttribute('id') == $id) {
200 return $button->getValue();
207 * Sets a widget value within the form.
208 * @param string $name Name of widget tag.
209 * @param string $value Value to input into the widget.
210 * @return boolean True if value is legal, false
211 * otherwise. If the field is not
212 * present, nothing will be set.
215 function setField($name, $value) {
216 if (isset($this->_widgets
[$name])) {
217 return $this->_widgets
[$name]->setValue($value);
223 * Sets a widget value within the form by using the ID.
224 * @param string/integer $id Name of widget tag.
225 * @param string $value Value to input into the widget.
226 * @return boolean True if value is legal, false
227 * otherwise. If the field is not
228 * present, nothing will be set.
231 function setFieldById($id, $value) {
232 foreach (array_keys($this->_widgets
) as $name) {
233 if ($this->_widgets
[$name]->getAttribute('id') == $id) {
234 return $this->setField($name, $value);
241 * Reads the current form values as a hash
242 * of submitted parameters. Repeated parameters
244 * @return hash Submitted values.
247 function getValues() {
249 foreach (array_keys($this->_widgets
) as $name) {
250 $new = $this->_widgets
[$name]->getValue();
251 if (is_string($new)) {
252 $values[$name] = $new;
253 } elseif (is_array($new)) {
254 $values[$name] = $new;
261 * Test to see if a form has a submit button with this
263 * @param string $name Name to look for.
264 * @return boolean True if present.
267 function hasSubmitName($name) {
268 foreach ($this->_buttons
as $button) {
269 if ($button->getName() == $name) {
277 * Test to see if a form has a submit button with this
279 * @param string $label Button label to search for.
280 * @return boolean True if present.
283 function hasSubmitLabel($label) {
284 foreach ($this->_buttons
as $button) {
285 if ($button->getLabel() == $label) {
293 * Test to see if a form has a submit button with this
295 * @param string $id Button ID attribute to search for.
296 * @return boolean True if present.
299 function hasSubmitId($id) {
300 foreach ($this->_buttons
as $button) {
301 if ($button->getAttribute('id') == $id) {
309 * Test to see if a form has a submit button with this
311 * @param string $label Button alt attribute to search for
312 * or nearest equivalent.
313 * @return boolean True if present.
316 function hasImageLabel($label) {
317 foreach ($this->_images
as $image) {
318 if ($image->getLabel() == $label) {
326 * Test to see if a form has a submittable image with this
328 * @param string $name Image name to search for.
329 * @return boolean True if present.
332 function hasImageName($name) {
333 foreach ($this->_images
as $image) {
334 if ($image->getName() == $name) {
342 * Test to see if a form has a submittable image with this
344 * @param string $id Button ID attribute to search for.
345 * @return boolean True if present.
348 function hasImageId($id) {
349 foreach ($this->_images
as $image) {
350 if ($image->getAttribute('id') == $id) {
358 * Gets the submit values for a named button.
359 * @param string $name Button label to search for.
360 * @return hash Submitted values or false
361 * if there is no such button in the
365 function submitButtonByName($name) {
366 foreach ($this->_buttons
as $button) {
367 if ($button->getName() == $name) {
369 $button->getSubmitValues(),
377 * Gets the submit values for a named button.
378 * @param string $label Button label to search for.
379 * @return hash Submitted values or false
380 * if there is no such button in the
384 function submitButtonByLabel($label) {
385 foreach ($this->_buttons
as $button) {
386 if ($button->getLabel() == $label) {
388 $button->getSubmitValues(),
396 * Gets the submit values for a button identified by the ID.
397 * @param string $id Button ID attribute to search for.
398 * @return hash Submitted values or false
399 * if there is no such button in the
403 function submitButtonById($id) {
404 foreach ($this->_buttons
as $button) {
405 if ($button->getAttribute('id') == $id) {
407 $button->getSubmitValues(),
415 * Gets the submit values for an image identified by the alt
416 * tag or nearest equivalent.
417 * @param string $label Button label to search for.
418 * @param integer $x X-coordinate of click.
419 * @param integer $y Y-coordinate of click.
420 * @return hash Submitted values or false
421 * if there is no such button in the
425 function submitImageByLabel($label, $x, $y) {
426 foreach ($this->_images
as $image) {
427 if ($image->getAttribute('alt') == $label) {
429 $image->getSubmitValues($x, $y),
437 * Gets the submit values for an image identified by the ID.
438 * @param string $name Image name to search for.
439 * @param integer $x X-coordinate of click.
440 * @param integer $y Y-coordinate of click.
441 * @return hash Submitted values or false
442 * if there is no such button in the
446 function submitImageByName($name, $x, $y) {
447 foreach ($this->_images
as $image) {
448 if ($image->getName() == $name) {
450 $image->getSubmitValues($x, $y),
458 * Gets the submit values for an image identified by the ID.
459 * @param string/integer $id Button ID attribute to search for.
460 * @param integer $x X-coordinate of click.
461 * @param integer $y Y-coordinate of click.
462 * @return hash Submitted values or false
463 * if there is no such button in the
467 function submitImageById($id, $x, $y) {
468 foreach ($this->_images
as $image) {
469 if ($image->getAttribute('id') == $id) {
471 $image->getSubmitValues($x, $y),
479 * Simply submits the form without the submit button
480 * value. Used when there is only one button or it
482 * @return hash Submitted values.
486 return $this->getValues();