/demo/scalr_newui/app/src/LibWebta/tests/simpletest/form.php

https://github.com/kennethjiang/Wolke · PHP · 351 lines · 188 code · 22 blank · 141 comment · 35 complexity · 0e67d4a851bbb36f143a9c9721ca07b3 MD5 · raw file

  1. <?php
  2. /**
  3. * Base include file for SimpleTest.
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id: form.php,v 1.41 2007/07/16 22:28:39 lastcraft Exp $
  7. */
  8. /**#@+
  9. * include SimpleTest files
  10. */
  11. require_once(dirname(__FILE__) . '/tag.php');
  12. require_once(dirname(__FILE__) . '/encoding.php');
  13. require_once(dirname(__FILE__) . '/selector.php');
  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 $_encoding;
  24. var $_default_target;
  25. var $_id;
  26. var $_buttons;
  27. var $_images;
  28. var $_widgets;
  29. var $_radios;
  30. var $_checkboxes;
  31. /**
  32. * Starts with no held controls/widgets.
  33. * @param SimpleTag $tag Form tag to read.
  34. * @param SimplePage $page Holding page.
  35. */
  36. function SimpleForm($tag, &$page) {
  37. $this->_method = $tag->getAttribute('method');
  38. $this->_action = $this->_createAction($tag->getAttribute('action'), $page);
  39. $this->_encoding = $this->_setEncodingClass($tag);
  40. $this->_default_target = false;
  41. $this->_id = $tag->getAttribute('id');
  42. $this->_buttons = array();
  43. $this->_images = array();
  44. $this->_widgets = array();
  45. $this->_radios = array();
  46. $this->_checkboxes = array();
  47. }
  48. /**
  49. * Creates the request packet to be sent by the form.
  50. * @param SimpleTag $tag Form tag to read.
  51. * @return string Packet class.
  52. * @access private
  53. */
  54. function _setEncodingClass($tag) {
  55. if (strtolower($tag->getAttribute('method')) == 'post') {
  56. if (strtolower($tag->getAttribute('enctype')) == 'multipart/form-data') {
  57. return 'SimpleMultipartEncoding';
  58. }
  59. return 'SimplePostEncoding';
  60. }
  61. return 'SimpleGetEncoding';
  62. }
  63. /**
  64. * Sets the frame target within a frameset.
  65. * @param string $frame Name of frame.
  66. * @access public
  67. */
  68. function setDefaultTarget($frame) {
  69. $this->_default_target = $frame;
  70. }
  71. /**
  72. * Accessor for method of form submission.
  73. * @return string Either get or post.
  74. * @access public
  75. */
  76. function getMethod() {
  77. return ($this->_method ? strtolower($this->_method) : 'get');
  78. }
  79. /**
  80. * Combined action attribute with current location
  81. * to get an absolute form target.
  82. * @param string $action Action attribute from form tag.
  83. * @param SimpleUrl $base Page location.
  84. * @return SimpleUrl Absolute form target.
  85. */
  86. function _createAction($action, &$page) {
  87. if (($action === '') || ($action === false)) {
  88. return $page->expandUrl($page->getUrl());
  89. }
  90. return $page->expandUrl(new SimpleUrl($action));;
  91. }
  92. /**
  93. * Absolute URL of the target.
  94. * @return SimpleUrl URL target.
  95. * @access public
  96. */
  97. function getAction() {
  98. $url = $this->_action;
  99. if ($this->_default_target && ! $url->getTarget()) {
  100. $url->setTarget($this->_default_target);
  101. }
  102. return $url;
  103. }
  104. /**
  105. * Creates the encoding for the current values in the
  106. * form.
  107. * @return SimpleFormEncoding Request to submit.
  108. * @access private
  109. */
  110. function _encode() {
  111. $class = $this->_encoding;
  112. $encoding = new $class();
  113. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  114. $this->_widgets[$i]->write($encoding);
  115. }
  116. return $encoding;
  117. }
  118. /**
  119. * ID field of form for unique identification.
  120. * @return string Unique tag ID.
  121. * @access public
  122. */
  123. function getId() {
  124. return $this->_id;
  125. }
  126. /**
  127. * Adds a tag contents to the form.
  128. * @param SimpleWidget $tag Input tag to add.
  129. * @access public
  130. */
  131. function addWidget(&$tag) {
  132. if (strtolower($tag->getAttribute('type')) == 'submit') {
  133. $this->_buttons[] = &$tag;
  134. } elseif (strtolower($tag->getAttribute('type')) == 'image') {
  135. $this->_images[] = &$tag;
  136. } elseif ($tag->getName()) {
  137. $this->_setWidget($tag);
  138. }
  139. }
  140. /**
  141. * Sets the widget into the form, grouping radio
  142. * buttons if any.
  143. * @param SimpleWidget $tag Incoming form control.
  144. * @access private
  145. */
  146. function _setWidget(&$tag) {
  147. if (strtolower($tag->getAttribute('type')) == 'radio') {
  148. $this->_addRadioButton($tag);
  149. } elseif (strtolower($tag->getAttribute('type')) == 'checkbox') {
  150. $this->_addCheckbox($tag);
  151. } else {
  152. $this->_widgets[] = &$tag;
  153. }
  154. }
  155. /**
  156. * Adds a radio button, building a group if necessary.
  157. * @param SimpleRadioButtonTag $tag Incoming form control.
  158. * @access private
  159. */
  160. function _addRadioButton(&$tag) {
  161. if (! isset($this->_radios[$tag->getName()])) {
  162. $this->_widgets[] = &new SimpleRadioGroup();
  163. $this->_radios[$tag->getName()] = count($this->_widgets) - 1;
  164. }
  165. $this->_widgets[$this->_radios[$tag->getName()]]->addWidget($tag);
  166. }
  167. /**
  168. * Adds a checkbox, making it a group on a repeated name.
  169. * @param SimpleCheckboxTag $tag Incoming form control.
  170. * @access private
  171. */
  172. function _addCheckbox(&$tag) {
  173. if (! isset($this->_checkboxes[$tag->getName()])) {
  174. $this->_widgets[] = &$tag;
  175. $this->_checkboxes[$tag->getName()] = count($this->_widgets) - 1;
  176. } else {
  177. $index = $this->_checkboxes[$tag->getName()];
  178. if (! SimpleTestCompatibility::isA($this->_widgets[$index], 'SimpleCheckboxGroup')) {
  179. $previous = &$this->_widgets[$index];
  180. $this->_widgets[$index] = &new SimpleCheckboxGroup();
  181. $this->_widgets[$index]->addWidget($previous);
  182. }
  183. $this->_widgets[$index]->addWidget($tag);
  184. }
  185. }
  186. /**
  187. * Extracts current value from form.
  188. * @param SimpleSelector $selector Criteria to apply.
  189. * @return string/array Value(s) as string or null
  190. * if not set.
  191. * @access public
  192. */
  193. function getValue($selector) {
  194. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  195. if ($selector->isMatch($this->_widgets[$i])) {
  196. return $this->_widgets[$i]->getValue();
  197. }
  198. }
  199. foreach ($this->_buttons as $button) {
  200. if ($selector->isMatch($button)) {
  201. return $button->getValue();
  202. }
  203. }
  204. return null;
  205. }
  206. /**
  207. * Sets a widget value within the form.
  208. * @param SimpleSelector $selector Criteria to apply.
  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($selector, $value) {
  216. $success = false;
  217. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  218. if ($selector->isMatch($this->_widgets[$i])) {
  219. if ($this->_widgets[$i]->setValue($value)) {
  220. $success = true;
  221. }
  222. }
  223. }
  224. return $success;
  225. }
  226. /**
  227. * Used by the page object to set widgets labels to
  228. * external label tags.
  229. * @param SimpleSelector $selector Criteria to apply.
  230. * @access public
  231. */
  232. function attachLabelBySelector($selector, $label) {
  233. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  234. if ($selector->isMatch($this->_widgets[$i])) {
  235. if (method_exists($this->_widgets[$i], 'setLabel')) {
  236. $this->_widgets[$i]->setLabel($label);
  237. return;
  238. }
  239. }
  240. }
  241. }
  242. /**
  243. * Test to see if a form has a submit button.
  244. * @param SimpleSelector $selector Criteria to apply.
  245. * @return boolean True if present.
  246. * @access public
  247. */
  248. function hasSubmit($selector) {
  249. foreach ($this->_buttons as $button) {
  250. if ($selector->isMatch($button)) {
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. /**
  257. * Test to see if a form has an image control.
  258. * @param SimpleSelector $selector Criteria to apply.
  259. * @return boolean True if present.
  260. * @access public
  261. */
  262. function hasImage($selector) {
  263. foreach ($this->_images as $image) {
  264. if ($selector->isMatch($image)) {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * Gets the submit values for a selected button.
  272. * @param SimpleSelector $selector Criteria to apply.
  273. * @param hash $additional Additional data for the form.
  274. * @return SimpleEncoding Submitted values or false
  275. * if there is no such button
  276. * in the form.
  277. * @access public
  278. */
  279. function submitButton($selector, $additional = false) {
  280. $additional = $additional ? $additional : array();
  281. foreach ($this->_buttons as $button) {
  282. if ($selector->isMatch($button)) {
  283. $encoding = $this->_encode();
  284. $button->write($encoding);
  285. if ($additional) {
  286. $encoding->merge($additional);
  287. }
  288. return $encoding;
  289. }
  290. }
  291. return false;
  292. }
  293. /**
  294. * Gets the submit values for an image.
  295. * @param SimpleSelector $selector Criteria to apply.
  296. * @param integer $x X-coordinate of click.
  297. * @param integer $y Y-coordinate of click.
  298. * @param hash $additional Additional data for the form.
  299. * @return SimpleEncoding Submitted values or false
  300. * if there is no such button in the
  301. * form.
  302. * @access public
  303. */
  304. function submitImage($selector, $x, $y, $additional = false) {
  305. $additional = $additional ? $additional : array();
  306. foreach ($this->_images as $image) {
  307. if ($selector->isMatch($image)) {
  308. $encoding = $this->_encode();
  309. $image->write($encoding, $x, $y);
  310. if ($additional) {
  311. $encoding->merge($additional);
  312. }
  313. return $encoding;
  314. }
  315. }
  316. return false;
  317. }
  318. /**
  319. * Simply submits the form without the submit button
  320. * value. Used when there is only one button or it
  321. * is unimportant.
  322. * @return hash Submitted values.
  323. * @access public
  324. */
  325. function submit() {
  326. return $this->_encode();
  327. }
  328. }
  329. ?>