3
* Base include file for SimpleTest.
5
* @subpackage WebTester
6
* @version $Id: form.php 1672 2008-03-02 04:47:34Z edwardzyang $
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');
18
* Form tag class to hold widget values.
20
* @subpackage WebTester
35
* Starts with no held controls/widgets.
36
* @param SimpleTag $tag Form tag to read.
37
* @param SimplePage $page Holding page.
39
function SimpleForm($tag, &$page) {
40
$this->_method = $tag->getAttribute('method');
41
$this->_action = $this->_createAction($tag->getAttribute('action'), $page);
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();
53
* Creates the request packet to be sent by the form.
54
* @param SimpleTag $tag Form tag to read.
55
* @return string Packet class.
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';
69
* Sets the frame target within a frameset.
70
* @param string $frame Name of frame.
73
function setDefaultTarget($frame) {
74
$this->_default_target = $frame;
78
* Accessor for method of form submission.
79
* @return string Either get or post.
82
function getMethod() {
83
return ($this->_method ? strtolower($this->_method) : 'get');
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, &$page) {
94
if (($action === '') || ($action === false)) {
95
return $page->expandUrl($page->getUrl());
97
return $page->expandUrl(new SimpleUrl($action));;
101
* Absolute URL of the target.
102
* @return SimpleUrl URL target.
105
function getAction() {
106
$url = $this->_action;
107
if ($this->_default_target && ! $url->getTarget()) {
108
$url->setTarget($this->_default_target);
114
* Creates the encoding for the current values in the
116
* @return SimpleFormEncoding Request to submit.
120
$class = $this->_encoding;
121
$encoding = new $class();
122
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
123
$this->_widgets[$i]->write($encoding);
129
* ID field of form for unique identification.
130
* @return string Unique tag ID.
138
* Adds a tag contents to the form.
139
* @param SimpleWidget $tag Input tag to add.
142
function addWidget(&$tag) {
143
if (strtolower($tag->getAttribute('type')) == 'submit') {
144
$this->_buttons[] = &$tag;
145
} elseif (strtolower($tag->getAttribute('type')) == 'image') {
146
$this->_images[] = &$tag;
147
} elseif ($tag->getName()) {
148
$this->_setWidget($tag);
153
* Sets the widget into the form, grouping radio
155
* @param SimpleWidget $tag Incoming form control.
158
function _setWidget(&$tag) {
159
if (strtolower($tag->getAttribute('type')) == 'radio') {
160
$this->_addRadioButton($tag);
161
} elseif (strtolower($tag->getAttribute('type')) == 'checkbox') {
162
$this->_addCheckbox($tag);
164
$this->_widgets[] = &$tag;
169
* Adds a radio button, building a group if necessary.
170
* @param SimpleRadioButtonTag $tag Incoming form control.
173
function _addRadioButton(&$tag) {
174
if (! isset($this->_radios[$tag->getName()])) {
175
$this->_widgets[] = &new SimpleRadioGroup();
176
$this->_radios[$tag->getName()] = count($this->_widgets) - 1;
178
$this->_widgets[$this->_radios[$tag->getName()]]->addWidget($tag);
182
* Adds a checkbox, making it a group on a repeated name.
183
* @param SimpleCheckboxTag $tag Incoming form control.
186
function _addCheckbox(&$tag) {
187
if (! isset($this->_checkboxes[$tag->getName()])) {
188
$this->_widgets[] = &$tag;
189
$this->_checkboxes[$tag->getName()] = count($this->_widgets) - 1;
191
$index = $this->_checkboxes[$tag->getName()];
192
if (! SimpleTestCompatibility::isA($this->_widgets[$index], 'SimpleCheckboxGroup')) {
193
$previous = &$this->_widgets[$index];
194
$this->_widgets[$index] = &new SimpleCheckboxGroup();
195
$this->_widgets[$index]->addWidget($previous);
197
$this->_widgets[$index]->addWidget($tag);
202
* Extracts current value from form.
203
* @param SimpleSelector $selector Criteria to apply.
204
* @return string/array Value(s) as string or null
208
function getValue($selector) {
209
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
210
if ($selector->isMatch($this->_widgets[$i])) {
211
return $this->_widgets[$i]->getValue();
214
foreach ($this->_buttons as $button) {
215
if ($selector->isMatch($button)) {
216
return $button->getValue();
223
* Sets a widget value within the form.
224
* @param SimpleSelector $selector Criteria to apply.
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 setField($selector, $value, $position=false) {
234
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
235
if ($selector->isMatch($this->_widgets[$i])) {
237
if ($position === false or $_position === (int)$position) {
238
if ($this->_widgets[$i]->setValue($value)) {
248
* Used by the page object to set widgets labels to
249
* external label tags.
250
* @param SimpleSelector $selector Criteria to apply.
253
function attachLabelBySelector($selector, $label) {
254
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
255
if ($selector->isMatch($this->_widgets[$i])) {
256
if (method_exists($this->_widgets[$i], 'setLabel')) {
257
$this->_widgets[$i]->setLabel($label);
265
* Test to see if a form has a submit button.
266
* @param SimpleSelector $selector Criteria to apply.
267
* @return boolean True if present.
270
function hasSubmit($selector) {
271
foreach ($this->_buttons as $button) {
272
if ($selector->isMatch($button)) {
280
* Test to see if a form has an image control.
281
* @param SimpleSelector $selector Criteria to apply.
282
* @return boolean True if present.
285
function hasImage($selector) {
286
foreach ($this->_images as $image) {
287
if ($selector->isMatch($image)) {
295
* Gets the submit values for a selected button.
296
* @param SimpleSelector $selector Criteria to apply.
297
* @param hash $additional Additional data for the form.
298
* @return SimpleEncoding Submitted values or false
299
* if there is no such button
303
function submitButton($selector, $additional = false) {
304
$additional = $additional ? $additional : array();
305
foreach ($this->_buttons as $button) {
306
if ($selector->isMatch($button)) {
307
$encoding = $this->_encode();
308
$button->write($encoding);
310
$encoding->merge($additional);
319
* Gets the submit values for an image.
320
* @param SimpleSelector $selector Criteria to apply.
321
* @param integer $x X-coordinate of click.
322
* @param integer $y Y-coordinate of click.
323
* @param hash $additional Additional data for the form.
324
* @return SimpleEncoding Submitted values or false
325
* if there is no such button in the
329
function submitImage($selector, $x, $y, $additional = false) {
330
$additional = $additional ? $additional : array();
331
foreach ($this->_images as $image) {
332
if ($selector->isMatch($image)) {
333
$encoding = $this->_encode();
334
$image->write($encoding, $x, $y);
336
$encoding->merge($additional);
345
* Simply submits the form without the submit button
346
* value. Used when there is only one button or it
348
* @return hash Submitted values.
352
return $this->_encode();
b'\\ No newline at end of file'