premier commit
[bazdig.git] / test / simpletest / form.php
blob208392d5a5745ea80a151da98e0f8cd1c6f9baca
1 <?php
2 /**
3 * Base include file for SimpleTest.
4 * @package SimpleTest
5 * @subpackage WebTester
6 * @version $Id: form.php,v 1.40 2006/01/24 01:11:55 lastcraft Exp $
7 */
9 /**#@+
10 * include SimpleTest files
12 require_once(dirname(__FILE__) . '/tag.php');
13 require_once(dirname(__FILE__) . '/encoding.php');
14 require_once(dirname(__FILE__) . '/selector.php');
15 /**#@-*/
17 /**
18 * Form tag class to hold widget values.
19 * @package SimpleTest
20 * @subpackage WebTester
22 class SimpleForm {
23 var $_method;
24 var $_action;
25 var $_encoding;
26 var $_default_target;
27 var $_id;
28 var $_buttons;
29 var $_images;
30 var $_widgets;
31 var $_radios;
32 var $_checkboxes;
34 /**
35 * Starts with no held controls/widgets.
36 * @param SimpleTag $tag Form tag to read.
37 * @param SimpleUrl $url Location of holding page.
39 function SimpleForm($tag, $url) {
40 $this->_method = $tag->getAttribute('method');
41 $this->_action = $this->_createAction($tag->getAttribute('action'), $url);
42 $this->_encoding = $this->_setEncodingClass($tag);
43 $this->_default_target = false;
44 $this->_id = $tag->getAttribute('id');
45 $this->_buttons = array();
46 $this->_images = array();
47 $this->_widgets = array();
48 $this->_radios = array();
49 $this->_checkboxes = array();
52 /**
53 * Creates the request packet to be sent by the form.
54 * @param SimpleTag $tag Form tag to read.
55 * @return string Packet class.
56 * @access private
58 function _setEncodingClass($tag) {
59 if (strtolower($tag->getAttribute('method')) == 'post') {
60 if (strtolower($tag->getAttribute('enctype')) == 'multipart/form-data') {
61 return 'SimpleMultipartEncoding';
63 return 'SimplePostEncoding';
65 return 'SimpleGetEncoding';
68 /**
69 * Sets the frame target within a frameset.
70 * @param string $frame Name of frame.
71 * @access public
73 function setDefaultTarget($frame) {
74 $this->_default_target = $frame;
77 /**
78 * Accessor for method of form submission.
79 * @return string Either get or post.
80 * @access public
82 function getMethod() {
83 return ($this->_method ? strtolower($this->_method) : 'get');
86 /**
87 * Combined action attribute with current location
88 * to get an absolute form target.
89 * @param string $action Action attribute from form tag.
90 * @param SimpleUrl $base Page location.
91 * @return SimpleUrl Absolute form target.
93 function _createAction($action, $base) {
94 if (($action === '') || ($action === false)) {
95 return $base;
97 $url = new SimpleUrl($action);
98 return $url->makeAbsolute($base);
102 * Absolute URL of the target.
103 * @return SimpleUrl URL target.
104 * @access public
106 function getAction() {
107 $url = $this->_action;
108 if ($this->_default_target && ! $url->getTarget()) {
109 $url->setTarget($this->_default_target);
111 return $url;
115 * Creates the encoding for the current values in the
116 * form.
117 * @return SimpleFormEncoding Request to submit.
118 * @access private
120 function _encode() {
121 $class = $this->_encoding;
122 $encoding = new $class();
123 for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
124 $this->_widgets[$i]->write($encoding);
126 return $encoding;
130 * ID field of form for unique identification.
131 * @return string Unique tag ID.
132 * @access public
134 function getId() {
135 return $this->_id;
139 * Adds a tag contents to the form.
140 * @param SimpleWidget $tag Input tag to add.
141 * @access public
143 function addWidget(&$tag) {
144 if (strtolower($tag->getAttribute('type')) == 'submit') {
145 $this->_buttons[] = &$tag;
146 } elseif (strtolower($tag->getAttribute('type')) == 'image') {
147 $this->_images[] = &$tag;
148 } elseif ($tag->getName()) {
149 $this->_setWidget($tag);
154 * Sets the widget into the form, grouping radio
155 * buttons if any.
156 * @param SimpleWidget $tag Incoming form control.
157 * @access private
159 function _setWidget(&$tag) {
160 if (strtolower($tag->getAttribute('type')) == 'radio') {
161 $this->_addRadioButton($tag);
162 } elseif (strtolower($tag->getAttribute('type')) == 'checkbox') {
163 $this->_addCheckbox($tag);
164 } else {
165 $this->_widgets[] = &$tag;
170 * Adds a radio button, building a group if necessary.
171 * @param SimpleRadioButtonTag $tag Incoming form control.
172 * @access private
174 function _addRadioButton(&$tag) {
175 if (! isset($this->_radios[$tag->getName()])) {
176 $this->_widgets[] = &new SimpleRadioGroup();
177 $this->_radios[$tag->getName()] = count($this->_widgets) - 1;
179 $this->_widgets[$this->_radios[$tag->getName()]]->addWidget($tag);
183 * Adds a checkbox, making it a group on a repeated name.
184 * @param SimpleCheckboxTag $tag Incoming form control.
185 * @access private
187 function _addCheckbox(&$tag) {
188 if (! isset($this->_checkboxes[$tag->getName()])) {
189 $this->_widgets[] = &$tag;
190 $this->_checkboxes[$tag->getName()] = count($this->_widgets) - 1;
191 } else {
192 $index = $this->_checkboxes[$tag->getName()];
193 if (! SimpleTestCompatibility::isA($this->_widgets[$index], 'SimpleCheckboxGroup')) {
194 $previous = &$this->_widgets[$index];
195 $this->_widgets[$index] = &new SimpleCheckboxGroup();
196 $this->_widgets[$index]->addWidget($previous);
198 $this->_widgets[$index]->addWidget($tag);
203 * Extracts current value from form.
204 * @param SimpleSelector $selector Criteria to apply.
205 * @return string/array Value(s) as string or null
206 * if not set.
207 * @access public
209 function getValue($selector) {
210 for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
211 if ($selector->isMatch($this->_widgets[$i])) {
212 return $this->_widgets[$i]->getValue();
215 foreach ($this->_buttons as $button) {
216 if ($selector->isMatch($button)) {
217 return $button->getValue();
220 return null;
224 * Sets a widget value within the form.
225 * @param SimpleSelector $selector Criteria to apply.
226 * @param string $value Value to input into the widget.
227 * @return boolean True if value is legal, false
228 * otherwise. If the field is not
229 * present, nothing will be set.
230 * @access public
232 function setField($selector, $value) {
233 $success = false;
234 for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
235 if ($selector->isMatch($this->_widgets[$i])) {
236 if ($this->_widgets[$i]->setValue($value)) {
237 $success = true;
241 return $success;
245 * Used by the page object to set widgets labels to
246 * external label tags.
247 * @param SimpleSelector $selector Criteria to apply.
248 * @access public
250 function attachLabelBySelector($selector, $label) {
251 for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
252 if ($selector->isMatch($this->_widgets[$i])) {
253 if (method_exists($this->_widgets[$i], 'setLabel')) {
254 $this->_widgets[$i]->setLabel($label);
255 return;
262 * Test to see if a form has a submit button.
263 * @param SimpleSelector $selector Criteria to apply.
264 * @return boolean True if present.
265 * @access public
267 function hasSubmit($selector) {
268 foreach ($this->_buttons as $button) {
269 if ($selector->isMatch($button)) {
270 return true;
273 return false;
277 * Test to see if a form has an image control.
278 * @param SimpleSelector $selector Criteria to apply.
279 * @return boolean True if present.
280 * @access public
282 function hasImage($selector) {
283 foreach ($this->_images as $image) {
284 if ($selector->isMatch($image)) {
285 return true;
288 return false;
292 * Gets the submit values for a selected button.
293 * @param SimpleSelector $selector Criteria to apply.
294 * @param hash $additional Additional data for the form.
295 * @return SimpleEncoding Submitted values or false
296 * if there is no such button
297 * in the form.
298 * @access public
300 function submitButton($selector, $additional = false) {
301 $additional = $additional ? $additional : array();
302 foreach ($this->_buttons as $button) {
303 if ($selector->isMatch($button)) {
304 $encoding = $this->_encode();
305 $button->write($encoding);
306 if ($additional) {
307 $encoding->merge($additional);
309 return $encoding;
312 return false;
316 * Gets the submit values for an image.
317 * @param SimpleSelector $selector Criteria to apply.
318 * @param integer $x X-coordinate of click.
319 * @param integer $y Y-coordinate of click.
320 * @param hash $additional Additional data for the form.
321 * @return SimpleEncoding Submitted values or false
322 * if there is no such button in the
323 * form.
324 * @access public
326 function submitImage($selector, $x, $y, $additional = false) {
327 $additional = $additional ? $additional : array();
328 foreach ($this->_images as $image) {
329 if ($selector->isMatch($image)) {
330 $encoding = $this->_encode();
331 $image->write($encoding, $x, $y);
332 if ($additional) {
333 $encoding->merge($additional);
335 return $encoding;
338 return false;
342 * Simply submits the form without the submit button
343 * value. Used when there is only one button or it
344 * is unimportant.
345 * @return hash Submitted values.
346 * @access public
348 function submit() {
349 return $this->_encode();