0337d704 |
1 | <?php |
2 | /** |
3 | * Base include file for SimpleTest. |
4 | * @package SimpleTest |
5 | * @subpackage WebTester |
6 | * @version $Id: form.php,v 1.4 2004/08/19 00:05:52 lastcraft Exp $ |
7 | */ |
8 | |
9 | /**#@+ |
10 | * include SimpleTest files |
11 | */ |
12 | require_once(dirname(__FILE__) . '/tag.php'); |
13 | /**#@-*/ |
14 | |
15 | /** |
16 | * Form tag class to hold widget values. |
17 | * @package SimpleTest |
18 | * @subpackage WebTester |
19 | */ |
20 | class SimpleForm { |
21 | var $_method; |
22 | var $_action; |
23 | var $_default_target; |
24 | var $_id; |
25 | var $_buttons; |
26 | var $_images; |
27 | var $_widgets; |
28 | |
29 | /** |
30 | * Starts with no held controls/widgets. |
31 | * @param SimpleTag $tag Form tag to read. |
32 | * @param SimpleUrl $url Location of holding page. |
33 | */ |
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(); |
42 | } |
43 | |
44 | /** |
45 | * Sets the frame target within a frameset. |
46 | * @param string $frame Name of frame. |
47 | * @access public |
48 | */ |
49 | function setDefaultTarget($frame) { |
50 | $this->_default_target = $frame; |
51 | } |
52 | |
53 | /** |
54 | * Accessor for form action. |
55 | * @return string Either get or post. |
56 | * @access public |
57 | */ |
58 | function getMethod() { |
59 | return ($this->_method ? strtolower($this->_method) : 'get'); |
60 | } |
61 | |
62 | /** |
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. |
68 | */ |
69 | function _createAction($action, $base) { |
70 | if ($action === false) { |
71 | return $base; |
72 | } |
73 | if ($action === true) { |
74 | $url = new SimpleUrl(''); |
75 | } else { |
76 | $url = new SimpleUrl($action); |
77 | } |
78 | return $url->makeAbsolute($base); |
79 | } |
80 | |
81 | /** |
82 | * Absolute URL of the target. |
83 | * @return SimpleUrl URL target. |
84 | * @access public |
85 | */ |
86 | function getAction() { |
87 | $url = $this->_action; |
88 | if ($this->_default_target && ! $url->getTarget()) { |
89 | $url->setTarget($this->_default_target); |
90 | } |
91 | return $url; |
92 | } |
93 | |
94 | /** |
95 | * ID field of form for unique identification. |
96 | * @return string Unique tag ID. |
97 | * @access public |
98 | */ |
99 | function getId() { |
100 | return $this->_id; |
101 | } |
102 | |
103 | /** |
104 | * Adds a tag contents to the form. |
105 | * @param SimpleWidget $tag Input tag to add. |
106 | * @access public |
107 | */ |
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; |
113 | } else { |
114 | if ($tag->getName()) { |
115 | $this->_setWidget($tag); |
116 | } |
117 | } |
118 | } |
119 | |
120 | /** |
121 | * Sets the widget into the form, grouping radio |
122 | * buttons if any. |
123 | * @param SimpleWidget $tag Incoming form control. |
124 | * @access private |
125 | */ |
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); |
131 | } else { |
132 | $this->_widgets[$tag->getName()] = &$tag; |
133 | } |
134 | } |
135 | |
136 | /** |
137 | * Adds a radio button, building a group if necessary. |
138 | * @param SimpleRadioButtonTag $tag Incoming form control. |
139 | * @access private |
140 | */ |
141 | function _addRadioButton($tag) { |
142 | if (! isset($this->_widgets[$tag->getName()])) { |
143 | $this->_widgets[$tag->getName()] = &new SimpleRadioGroup(); |
144 | } |
145 | $this->_widgets[$tag->getName()]->addWidget($tag); |
146 | } |
147 | |
148 | /** |
149 | * Adds a checkbox, making it a group on a repeated name. |
150 | * @param SimpleCheckboxTag $tag Incoming form control. |
151 | * @access private |
152 | */ |
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); |
161 | } else { |
162 | $this->_widgets[$tag->getName()]->addWidget($tag); |
163 | } |
164 | } |
165 | |
166 | /** |
167 | * Extracts current value from form. |
168 | * @param string $name Keyed by widget name. |
169 | * @return string Value as string or null |
170 | * if not set. |
171 | * @access public |
172 | */ |
173 | function getValue($name) { |
174 | if (isset($this->_widgets[$name])) { |
175 | return $this->_widgets[$name]->getValue(); |
176 | } |
177 | foreach ($this->_buttons as $button) { |
178 | if ($button->getName() == $name) { |
179 | return $button->getValue(); |
180 | } |
181 | } |
182 | return null; |
183 | } |
184 | |
185 | /** |
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 |
189 | * if not set. |
190 | * @access public |
191 | */ |
192 | function getValueById($id) { |
193 | foreach ($this->_widgets as $widget) { |
194 | if ($widget->getAttribute('id') == $id) { |
195 | return $widget->getValue(); |
196 | } |
197 | } |
198 | foreach ($this->_buttons as $button) { |
199 | if ($button->getAttribute('id') == $id) { |
200 | return $button->getValue(); |
201 | } |
202 | } |
203 | return null; |
204 | } |
205 | |
206 | /** |
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. |
213 | * @access public |
214 | */ |
215 | function setField($name, $value) { |
216 | if (isset($this->_widgets[$name])) { |
217 | return $this->_widgets[$name]->setValue($value); |
218 | } |
219 | return false; |
220 | } |
221 | |
222 | /** |
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. |
229 | * @access public |
230 | */ |
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); |
235 | } |
236 | } |
237 | return false; |
238 | } |
239 | |
240 | /** |
241 | * Reads the current form values as a hash |
242 | * of submitted parameters. Repeated parameters |
243 | * appear as a list. |
244 | * @return hash Submitted values. |
245 | * @access public |
246 | */ |
247 | function getValues() { |
248 | $values = array(); |
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; |
255 | } |
256 | } |
257 | return $values; |
258 | } |
259 | |
260 | /** |
261 | * Test to see if a form has a submit button with this |
262 | * name attribute. |
263 | * @param string $name Name to look for. |
264 | * @return boolean True if present. |
265 | * @access public |
266 | */ |
267 | function hasSubmitName($name) { |
268 | foreach ($this->_buttons as $button) { |
269 | if ($button->getName() == $name) { |
270 | return true; |
271 | } |
272 | } |
273 | return false; |
274 | } |
275 | |
276 | /** |
277 | * Test to see if a form has a submit button with this |
278 | * value attribute. |
279 | * @param string $label Button label to search for. |
280 | * @return boolean True if present. |
281 | * @access public |
282 | */ |
283 | function hasSubmitLabel($label) { |
284 | foreach ($this->_buttons as $button) { |
285 | if ($button->getLabel() == $label) { |
286 | return true; |
287 | } |
288 | } |
289 | return false; |
290 | } |
291 | |
292 | /** |
293 | * Test to see if a form has a submit button with this |
294 | * ID attribute. |
295 | * @param string $id Button ID attribute to search for. |
296 | * @return boolean True if present. |
297 | * @access public |
298 | */ |
299 | function hasSubmitId($id) { |
300 | foreach ($this->_buttons as $button) { |
301 | if ($button->getAttribute('id') == $id) { |
302 | return true; |
303 | } |
304 | } |
305 | return false; |
306 | } |
307 | |
308 | /** |
309 | * Test to see if a form has a submit button with this |
310 | * name attribute. |
311 | * @param string $label Button alt attribute to search for |
312 | * or nearest equivalent. |
313 | * @return boolean True if present. |
314 | * @access public |
315 | */ |
316 | function hasImageLabel($label) { |
317 | foreach ($this->_images as $image) { |
318 | if ($image->getLabel() == $label) { |
319 | return true; |
320 | } |
321 | } |
322 | return false; |
323 | } |
324 | |
325 | /** |
326 | * Test to see if a form has a submittable image with this |
327 | * field name. |
328 | * @param string $name Image name to search for. |
329 | * @return boolean True if present. |
330 | * @access public |
331 | */ |
332 | function hasImageName($name) { |
333 | foreach ($this->_images as $image) { |
334 | if ($image->getName() == $name) { |
335 | return true; |
336 | } |
337 | } |
338 | return false; |
339 | } |
340 | |
341 | /** |
342 | * Test to see if a form has a submittable image with this |
343 | * ID attribute. |
344 | * @param string $id Button ID attribute to search for. |
345 | * @return boolean True if present. |
346 | * @access public |
347 | */ |
348 | function hasImageId($id) { |
349 | foreach ($this->_images as $image) { |
350 | if ($image->getAttribute('id') == $id) { |
351 | return true; |
352 | } |
353 | } |
354 | return false; |
355 | } |
356 | |
357 | /** |
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 |
362 | * form. |
363 | * @access public |
364 | */ |
365 | function submitButtonByName($name) { |
366 | foreach ($this->_buttons as $button) { |
367 | if ($button->getName() == $name) { |
368 | return array_merge( |
369 | $button->getSubmitValues(), |
370 | $this->getValues()); |
371 | } |
372 | } |
373 | return false; |
374 | } |
375 | |
376 | /** |
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 |
381 | * form. |
382 | * @access public |
383 | */ |
384 | function submitButtonByLabel($label) { |
385 | foreach ($this->_buttons as $button) { |
386 | if ($button->getLabel() == $label) { |
387 | return array_merge( |
388 | $button->getSubmitValues(), |
389 | $this->getValues()); |
390 | } |
391 | } |
392 | return false; |
393 | } |
394 | |
395 | /** |
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 |
400 | * form. |
401 | * @access public |
402 | */ |
403 | function submitButtonById($id) { |
404 | foreach ($this->_buttons as $button) { |
405 | if ($button->getAttribute('id') == $id) { |
406 | return array_merge( |
407 | $button->getSubmitValues(), |
408 | $this->getValues()); |
409 | } |
410 | } |
411 | return false; |
412 | } |
413 | |
414 | /** |
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 |
422 | * form. |
423 | * @access public |
424 | */ |
425 | function submitImageByLabel($label, $x, $y) { |
426 | foreach ($this->_images as $image) { |
427 | if ($image->getAttribute('alt') == $label) { |
428 | return array_merge( |
429 | $image->getSubmitValues($x, $y), |
430 | $this->getValues()); |
431 | } |
432 | } |
433 | return false; |
434 | } |
435 | |
436 | /** |
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 |
443 | * form. |
444 | * @access public |
445 | */ |
446 | function submitImageByName($name, $x, $y) { |
447 | foreach ($this->_images as $image) { |
448 | if ($image->getName() == $name) { |
449 | return array_merge( |
450 | $image->getSubmitValues($x, $y), |
451 | $this->getValues()); |
452 | } |
453 | } |
454 | return false; |
455 | } |
456 | |
457 | /** |
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 |
464 | * form. |
465 | * @access public |
466 | */ |
467 | function submitImageById($id, $x, $y) { |
468 | foreach ($this->_images as $image) { |
469 | if ($image->getAttribute('id') == $id) { |
470 | return array_merge( |
471 | $image->getSubmitValues($x, $y), |
472 | $this->getValues()); |
473 | } |
474 | } |
475 | return false; |
476 | } |
477 | |
478 | /** |
479 | * Simply submits the form without the submit button |
480 | * value. Used when there is only one button or it |
481 | * is unimportant. |
482 | * @return hash Submitted values. |
483 | * @access public |
484 | */ |
485 | function submit() { |
486 | return $this->getValues(); |
487 | } |
488 | } |
489 | ?> |