MDL-32250 import latest SimpleTest 1.1.0
This commit is contained in:
+106
-100
@@ -3,9 +3,9 @@
|
||||
* Base include file for SimpleTest.
|
||||
* @package SimpleTest
|
||||
* @subpackage WebTester
|
||||
* @version $Id$
|
||||
* @version $Id: form.php 2013 2011-04-29 09:29:45Z pp11 $
|
||||
*/
|
||||
|
||||
|
||||
/**#@+
|
||||
* include SimpleTest files
|
||||
*/
|
||||
@@ -20,42 +20,42 @@ require_once(dirname(__FILE__) . '/selector.php');
|
||||
* @subpackage WebTester
|
||||
*/
|
||||
class SimpleForm {
|
||||
var $_method;
|
||||
var $_action;
|
||||
var $_encoding;
|
||||
var $_default_target;
|
||||
var $_id;
|
||||
var $_buttons;
|
||||
var $_images;
|
||||
var $_widgets;
|
||||
var $_radios;
|
||||
var $_checkboxes;
|
||||
|
||||
private $method;
|
||||
private $action;
|
||||
private $encoding;
|
||||
private $default_target;
|
||||
private $id;
|
||||
private $buttons;
|
||||
private $images;
|
||||
private $widgets;
|
||||
private $radios;
|
||||
private $checkboxes;
|
||||
|
||||
/**
|
||||
* Starts with no held controls/widgets.
|
||||
* @param SimpleTag $tag Form tag to read.
|
||||
* @param SimplePage $page Holding page.
|
||||
*/
|
||||
function SimpleForm($tag, &$page) {
|
||||
$this->_method = $tag->getAttribute('method');
|
||||
$this->_action = $this->_createAction($tag->getAttribute('action'), $page);
|
||||
$this->_encoding = $this->_setEncodingClass($tag);
|
||||
$this->_default_target = false;
|
||||
$this->_id = $tag->getAttribute('id');
|
||||
$this->_buttons = array();
|
||||
$this->_images = array();
|
||||
$this->_widgets = array();
|
||||
$this->_radios = array();
|
||||
$this->_checkboxes = array();
|
||||
function __construct($tag, $page) {
|
||||
$this->method = $tag->getAttribute('method');
|
||||
$this->action = $this->createAction($tag->getAttribute('action'), $page);
|
||||
$this->encoding = $this->setEncodingClass($tag);
|
||||
$this->default_target = false;
|
||||
$this->id = $tag->getAttribute('id');
|
||||
$this->buttons = array();
|
||||
$this->images = array();
|
||||
$this->widgets = array();
|
||||
$this->radios = array();
|
||||
$this->checkboxes = array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates the request packet to be sent by the form.
|
||||
* @param SimpleTag $tag Form tag to read.
|
||||
* @return string Packet class.
|
||||
* @access private
|
||||
*/
|
||||
function _setEncodingClass($tag) {
|
||||
protected function setEncodingClass($tag) {
|
||||
if (strtolower($tag->getAttribute('method')) == 'post') {
|
||||
if (strtolower($tag->getAttribute('enctype')) == 'multipart/form-data') {
|
||||
return 'SimpleMultipartEncoding';
|
||||
@@ -64,25 +64,25 @@ class SimpleForm {
|
||||
}
|
||||
return 'SimpleGetEncoding';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the frame target within a frameset.
|
||||
* @param string $frame Name of frame.
|
||||
* @access public
|
||||
*/
|
||||
function setDefaultTarget($frame) {
|
||||
$this->_default_target = $frame;
|
||||
$this->default_target = $frame;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Accessor for method of form submission.
|
||||
* @return string Either get or post.
|
||||
* @access public
|
||||
*/
|
||||
function getMethod() {
|
||||
return ($this->_method ? strtolower($this->_method) : 'get');
|
||||
return ($this->method ? strtolower($this->method) : 'get');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Combined action attribute with current location
|
||||
* to get an absolute form target.
|
||||
@@ -90,114 +90,116 @@ class SimpleForm {
|
||||
* @param SimpleUrl $base Page location.
|
||||
* @return SimpleUrl Absolute form target.
|
||||
*/
|
||||
function _createAction($action, &$page) {
|
||||
protected function createAction($action, $page) {
|
||||
if (($action === '') || ($action === false)) {
|
||||
return $page->expandUrl($page->getUrl());
|
||||
}
|
||||
return $page->expandUrl(new SimpleUrl($action));;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Absolute URL of the target.
|
||||
* @return SimpleUrl URL target.
|
||||
* @access public
|
||||
*/
|
||||
function getAction() {
|
||||
$url = $this->_action;
|
||||
if ($this->_default_target && ! $url->getTarget()) {
|
||||
$url->setTarget($this->_default_target);
|
||||
$url = $this->action;
|
||||
if ($this->default_target && ! $url->getTarget()) {
|
||||
$url->setTarget($this->default_target);
|
||||
}
|
||||
if ($this->getMethod() == 'get') {
|
||||
$url->clearRequest();
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates the encoding for the current values in the
|
||||
* form.
|
||||
* @return SimpleFormEncoding Request to submit.
|
||||
* @access private
|
||||
*/
|
||||
function _encode() {
|
||||
$class = $this->_encoding;
|
||||
protected function encode() {
|
||||
$class = $this->encoding;
|
||||
$encoding = new $class();
|
||||
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
|
||||
$this->_widgets[$i]->write($encoding);
|
||||
for ($i = 0, $count = count($this->widgets); $i < $count; $i++) {
|
||||
$this->widgets[$i]->write($encoding);
|
||||
}
|
||||
return $encoding;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ID field of form for unique identification.
|
||||
* @return string Unique tag ID.
|
||||
* @access public
|
||||
*/
|
||||
function getId() {
|
||||
return $this->_id;
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a tag contents to the form.
|
||||
* @param SimpleWidget $tag Input tag to add.
|
||||
* @access public
|
||||
*/
|
||||
function addWidget(&$tag) {
|
||||
function addWidget($tag) {
|
||||
if (strtolower($tag->getAttribute('type')) == 'submit') {
|
||||
$this->_buttons[] = &$tag;
|
||||
$this->buttons[] = $tag;
|
||||
} elseif (strtolower($tag->getAttribute('type')) == 'image') {
|
||||
$this->_images[] = &$tag;
|
||||
$this->images[] = $tag;
|
||||
} elseif ($tag->getName()) {
|
||||
$this->_setWidget($tag);
|
||||
$this->setWidget($tag);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the widget into the form, grouping radio
|
||||
* buttons if any.
|
||||
* @param SimpleWidget $tag Incoming form control.
|
||||
* @access private
|
||||
*/
|
||||
function _setWidget(&$tag) {
|
||||
protected function setWidget($tag) {
|
||||
if (strtolower($tag->getAttribute('type')) == 'radio') {
|
||||
$this->_addRadioButton($tag);
|
||||
$this->addRadioButton($tag);
|
||||
} elseif (strtolower($tag->getAttribute('type')) == 'checkbox') {
|
||||
$this->_addCheckbox($tag);
|
||||
$this->addCheckbox($tag);
|
||||
} else {
|
||||
$this->_widgets[] = &$tag;
|
||||
$this->widgets[] = &$tag;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a radio button, building a group if necessary.
|
||||
* @param SimpleRadioButtonTag $tag Incoming form control.
|
||||
* @access private
|
||||
*/
|
||||
function _addRadioButton(&$tag) {
|
||||
if (! isset($this->_radios[$tag->getName()])) {
|
||||
$this->_widgets[] = new SimpleRadioGroup();
|
||||
$this->_radios[$tag->getName()] = count($this->_widgets) - 1;
|
||||
protected function addRadioButton($tag) {
|
||||
if (! isset($this->radios[$tag->getName()])) {
|
||||
$this->widgets[] = new SimpleRadioGroup();
|
||||
$this->radios[$tag->getName()] = count($this->widgets) - 1;
|
||||
}
|
||||
$this->_widgets[$this->_radios[$tag->getName()]]->addWidget($tag);
|
||||
$this->widgets[$this->radios[$tag->getName()]]->addWidget($tag);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a checkbox, making it a group on a repeated name.
|
||||
* @param SimpleCheckboxTag $tag Incoming form control.
|
||||
* @access private
|
||||
*/
|
||||
function _addCheckbox(&$tag) {
|
||||
if (! isset($this->_checkboxes[$tag->getName()])) {
|
||||
$this->_widgets[] = &$tag;
|
||||
$this->_checkboxes[$tag->getName()] = count($this->_widgets) - 1;
|
||||
protected function addCheckbox($tag) {
|
||||
if (! isset($this->checkboxes[$tag->getName()])) {
|
||||
$this->widgets[] = $tag;
|
||||
$this->checkboxes[$tag->getName()] = count($this->widgets) - 1;
|
||||
} else {
|
||||
$index = $this->_checkboxes[$tag->getName()];
|
||||
if (! SimpleTestCompatibility::isA($this->_widgets[$index], 'SimpleCheckboxGroup')) {
|
||||
$previous = &$this->_widgets[$index];
|
||||
$this->_widgets[$index] = new SimpleCheckboxGroup();
|
||||
$this->_widgets[$index]->addWidget($previous);
|
||||
$index = $this->checkboxes[$tag->getName()];
|
||||
if (! SimpleTestCompatibility::isA($this->widgets[$index], 'SimpleCheckboxGroup')) {
|
||||
$previous = $this->widgets[$index];
|
||||
$this->widgets[$index] = new SimpleCheckboxGroup();
|
||||
$this->widgets[$index]->addWidget($previous);
|
||||
}
|
||||
$this->_widgets[$index]->addWidget($tag);
|
||||
$this->widgets[$index]->addWidget($tag);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts current value from form.
|
||||
* @param SimpleSelector $selector Criteria to apply.
|
||||
@@ -206,19 +208,19 @@ class SimpleForm {
|
||||
* @access public
|
||||
*/
|
||||
function getValue($selector) {
|
||||
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
|
||||
if ($selector->isMatch($this->_widgets[$i])) {
|
||||
return $this->_widgets[$i]->getValue();
|
||||
for ($i = 0, $count = count($this->widgets); $i < $count; $i++) {
|
||||
if ($selector->isMatch($this->widgets[$i])) {
|
||||
return $this->widgets[$i]->getValue();
|
||||
}
|
||||
}
|
||||
foreach ($this->_buttons as $button) {
|
||||
foreach ($this->buttons as $button) {
|
||||
if ($selector->isMatch($button)) {
|
||||
return $button->getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a widget value within the form.
|
||||
* @param SimpleSelector $selector Criteria to apply.
|
||||
@@ -231,11 +233,11 @@ class SimpleForm {
|
||||
function setField($selector, $value, $position=false) {
|
||||
$success = false;
|
||||
$_position = 0;
|
||||
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
|
||||
if ($selector->isMatch($this->_widgets[$i])) {
|
||||
for ($i = 0, $count = count($this->widgets); $i < $count; $i++) {
|
||||
if ($selector->isMatch($this->widgets[$i])) {
|
||||
$_position++;
|
||||
if ($position === false or $_position === (int)$position) {
|
||||
if ($this->_widgets[$i]->setValue($value)) {
|
||||
if ($this->widgets[$i]->setValue($value)) {
|
||||
$success = true;
|
||||
}
|
||||
}
|
||||
@@ -243,7 +245,7 @@ class SimpleForm {
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used by the page object to set widgets labels to
|
||||
* external label tags.
|
||||
@@ -251,16 +253,16 @@ class SimpleForm {
|
||||
* @access public
|
||||
*/
|
||||
function attachLabelBySelector($selector, $label) {
|
||||
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
|
||||
if ($selector->isMatch($this->_widgets[$i])) {
|
||||
if (method_exists($this->_widgets[$i], 'setLabel')) {
|
||||
$this->_widgets[$i]->setLabel($label);
|
||||
for ($i = 0, $count = count($this->widgets); $i < $count; $i++) {
|
||||
if ($selector->isMatch($this->widgets[$i])) {
|
||||
if (method_exists($this->widgets[$i], 'setLabel')) {
|
||||
$this->widgets[$i]->setLabel($label);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test to see if a form has a submit button.
|
||||
* @param SimpleSelector $selector Criteria to apply.
|
||||
@@ -268,14 +270,14 @@ class SimpleForm {
|
||||
* @access public
|
||||
*/
|
||||
function hasSubmit($selector) {
|
||||
foreach ($this->_buttons as $button) {
|
||||
foreach ($this->buttons as $button) {
|
||||
if ($selector->isMatch($button)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test to see if a form has an image control.
|
||||
* @param SimpleSelector $selector Criteria to apply.
|
||||
@@ -283,14 +285,14 @@ class SimpleForm {
|
||||
* @access public
|
||||
*/
|
||||
function hasImage($selector) {
|
||||
foreach ($this->_images as $image) {
|
||||
foreach ($this->images as $image) {
|
||||
if ($selector->isMatch($image)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the submit values for a selected button.
|
||||
* @param SimpleSelector $selector Criteria to apply.
|
||||
@@ -302,19 +304,19 @@ class SimpleForm {
|
||||
*/
|
||||
function submitButton($selector, $additional = false) {
|
||||
$additional = $additional ? $additional : array();
|
||||
foreach ($this->_buttons as $button) {
|
||||
foreach ($this->buttons as $button) {
|
||||
if ($selector->isMatch($button)) {
|
||||
$encoding = $this->_encode();
|
||||
$encoding = $this->encode();
|
||||
$button->write($encoding);
|
||||
if ($additional) {
|
||||
$encoding->merge($additional);
|
||||
}
|
||||
return $encoding;
|
||||
return $encoding;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the submit values for an image.
|
||||
* @param SimpleSelector $selector Criteria to apply.
|
||||
@@ -328,19 +330,19 @@ class SimpleForm {
|
||||
*/
|
||||
function submitImage($selector, $x, $y, $additional = false) {
|
||||
$additional = $additional ? $additional : array();
|
||||
foreach ($this->_images as $image) {
|
||||
foreach ($this->images as $image) {
|
||||
if ($selector->isMatch($image)) {
|
||||
$encoding = $this->_encode();
|
||||
$encoding = $this->encode();
|
||||
$image->write($encoding, $x, $y);
|
||||
if ($additional) {
|
||||
$encoding->merge($additional);
|
||||
}
|
||||
return $encoding;
|
||||
return $encoding;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simply submits the form without the submit button
|
||||
* value. Used when there is only one button or it
|
||||
@@ -348,8 +350,12 @@ class SimpleForm {
|
||||
* @return hash Submitted values.
|
||||
* @access public
|
||||
*/
|
||||
function submit() {
|
||||
return $this->_encode();
|
||||
function submit($additional = false) {
|
||||
$encoding = $this->encode();
|
||||
if ($additional) {
|
||||
$encoding->merge($additional);
|
||||
}
|
||||
return $encoding;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user