PageRenderTime 97ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/limb/tests_runner/lib/simpletest/form.php

https://github.com/limb-php-framework/limb-app-buildman
PHP | 352 lines | 189 code | 22 blank | 141 comment | 35 complexity | d4dcdec32e08427db0b878e01c714d43 MD5 | raw file
  1. <?php
  2. /**
  3. * Base include file for SimpleTest.
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id: form.php 4378 2006-10-27 10:04:53Z pachanga $
  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 SimpleUrl $url Location of holding page.
  35. */
  36. function SimpleForm($tag, $url) {
  37. $this->_method = $tag->getAttribute('method');
  38. $this->_action = $this->_createAction($tag->getAttribute('action'), $url);
  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, $base) {
  87. if (($action === '') || ($action === false)) {
  88. return $base;
  89. }
  90. $url = new SimpleUrl($action);
  91. return $url->makeAbsolute($base);
  92. }
  93. /**
  94. * Absolute URL of the target.
  95. * @return SimpleUrl URL target.
  96. * @access public
  97. */
  98. function getAction() {
  99. $url = $this->_action;
  100. if ($this->_default_target && ! $url->getTarget()) {
  101. $url->setTarget($this->_default_target);
  102. }
  103. return $url;
  104. }
  105. /**
  106. * Creates the encoding for the current values in the
  107. * form.
  108. * @return SimpleFormEncoding Request to submit.
  109. * @access private
  110. */
  111. function _encode() {
  112. $class = $this->_encoding;
  113. $encoding = new $class();
  114. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  115. $this->_widgets[$i]->write($encoding);
  116. }
  117. return $encoding;
  118. }
  119. /**
  120. * ID field of form for unique identification.
  121. * @return string Unique tag ID.
  122. * @access public
  123. */
  124. function getId() {
  125. return $this->_id;
  126. }
  127. /**
  128. * Adds a tag contents to the form.
  129. * @param SimpleWidget $tag Input tag to add.
  130. * @access public
  131. */
  132. function addWidget(&$tag) {
  133. if (strtolower($tag->getAttribute('type')) == 'submit') {
  134. $this->_buttons[] = &$tag;
  135. } elseif (strtolower($tag->getAttribute('type')) == 'image') {
  136. $this->_images[] = &$tag;
  137. } elseif ($tag->getName()) {
  138. $this->_setWidget($tag);
  139. }
  140. }
  141. /**
  142. * Sets the widget into the form, grouping radio
  143. * buttons if any.
  144. * @param SimpleWidget $tag Incoming form control.
  145. * @access private
  146. */
  147. function _setWidget(&$tag) {
  148. if (strtolower($tag->getAttribute('type')) == 'radio') {
  149. $this->_addRadioButton($tag);
  150. } elseif (strtolower($tag->getAttribute('type')) == 'checkbox') {
  151. $this->_addCheckbox($tag);
  152. } else {
  153. $this->_widgets[] = &$tag;
  154. }
  155. }
  156. /**
  157. * Adds a radio button, building a group if necessary.
  158. * @param SimpleRadioButtonTag $tag Incoming form control.
  159. * @access private
  160. */
  161. function _addRadioButton(&$tag) {
  162. if (! isset($this->_radios[$tag->getName()])) {
  163. $this->_widgets[] = &new SimpleRadioGroup();
  164. $this->_radios[$tag->getName()] = count($this->_widgets) - 1;
  165. }
  166. $this->_widgets[$this->_radios[$tag->getName()]]->addWidget($tag);
  167. }
  168. /**
  169. * Adds a checkbox, making it a group on a repeated name.
  170. * @param SimpleCheckboxTag $tag Incoming form control.
  171. * @access private
  172. */
  173. function _addCheckbox(&$tag) {
  174. if (! isset($this->_checkboxes[$tag->getName()])) {
  175. $this->_widgets[] = &$tag;
  176. $this->_checkboxes[$tag->getName()] = count($this->_widgets) - 1;
  177. } else {
  178. $index = $this->_checkboxes[$tag->getName()];
  179. if (! SimpleTestCompatibility::isA($this->_widgets[$index], 'SimpleCheckboxGroup')) {
  180. $previous = &$this->_widgets[$index];
  181. $this->_widgets[$index] = &new SimpleCheckboxGroup();
  182. $this->_widgets[$index]->addWidget($previous);
  183. }
  184. $this->_widgets[$index]->addWidget($tag);
  185. }
  186. }
  187. /**
  188. * Extracts current value from form.
  189. * @param SimpleSelector $selector Criteria to apply.
  190. * @return string/array Value(s) as string or null
  191. * if not set.
  192. * @access public
  193. */
  194. function getValue($selector) {
  195. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  196. if ($selector->isMatch($this->_widgets[$i])) {
  197. return $this->_widgets[$i]->getValue();
  198. }
  199. }
  200. foreach ($this->_buttons as $button) {
  201. if ($selector->isMatch($button)) {
  202. return $button->getValue();
  203. }
  204. }
  205. return null;
  206. }
  207. /**
  208. * Sets a widget value within the form.
  209. * @param SimpleSelector $selector Criteria to apply.
  210. * @param string $value Value to input into the widget.
  211. * @return boolean True if value is legal, false
  212. * otherwise. If the field is not
  213. * present, nothing will be set.
  214. * @access public
  215. */
  216. function setField($selector, $value) {
  217. $success = false;
  218. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  219. if ($selector->isMatch($this->_widgets[$i])) {
  220. if ($this->_widgets[$i]->setValue($value)) {
  221. $success = true;
  222. }
  223. }
  224. }
  225. return $success;
  226. }
  227. /**
  228. * Used by the page object to set widgets labels to
  229. * external label tags.
  230. * @param SimpleSelector $selector Criteria to apply.
  231. * @access public
  232. */
  233. function attachLabelBySelector($selector, $label) {
  234. for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
  235. if ($selector->isMatch($this->_widgets[$i])) {
  236. if (method_exists($this->_widgets[$i], 'setLabel')) {
  237. $this->_widgets[$i]->setLabel($label);
  238. return;
  239. }
  240. }
  241. }
  242. }
  243. /**
  244. * Test to see if a form has a submit button.
  245. * @param SimpleSelector $selector Criteria to apply.
  246. * @return boolean True if present.
  247. * @access public
  248. */
  249. function hasSubmit($selector) {
  250. foreach ($this->_buttons as $button) {
  251. if ($selector->isMatch($button)) {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. /**
  258. * Test to see if a form has an image control.
  259. * @param SimpleSelector $selector Criteria to apply.
  260. * @return boolean True if present.
  261. * @access public
  262. */
  263. function hasImage($selector) {
  264. foreach ($this->_images as $image) {
  265. if ($selector->isMatch($image)) {
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * Gets the submit values for a selected button.
  273. * @param SimpleSelector $selector Criteria to apply.
  274. * @param hash $additional Additional data for the form.
  275. * @return SimpleEncoding Submitted values or false
  276. * if there is no such button
  277. * in the form.
  278. * @access public
  279. */
  280. function submitButton($selector, $additional = false) {
  281. $additional = $additional ? $additional : array();
  282. foreach ($this->_buttons as $button) {
  283. if ($selector->isMatch($button)) {
  284. $encoding = $this->_encode();
  285. $button->write($encoding);
  286. if ($additional) {
  287. $encoding->merge($additional);
  288. }
  289. return $encoding;
  290. }
  291. }
  292. return false;
  293. }
  294. /**
  295. * Gets the submit values for an image.
  296. * @param SimpleSelector $selector Criteria to apply.
  297. * @param integer $x X-coordinate of click.
  298. * @param integer $y Y-coordinate of click.
  299. * @param hash $additional Additional data for the form.
  300. * @return SimpleEncoding Submitted values or false
  301. * if there is no such button in the
  302. * form.
  303. * @access public
  304. */
  305. function submitImage($selector, $x, $y, $additional = false) {
  306. $additional = $additional ? $additional : array();
  307. foreach ($this->_images as $image) {
  308. if ($selector->isMatch($image)) {
  309. $encoding = $this->_encode();
  310. $image->write($encoding, $x, $y);
  311. if ($additional) {
  312. $encoding->merge($additional);
  313. }
  314. return $encoding;
  315. }
  316. }
  317. return false;
  318. }
  319. /**
  320. * Simply submits the form without the submit button
  321. * value. Used when there is only one button or it
  322. * is unimportant.
  323. * @return hash Submitted values.
  324. * @access public
  325. */
  326. function submit() {
  327. return $this->_encode();
  328. }
  329. }
  330. ?>