tag.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. <?php
  2. /**
  3. * Base include file for SimpleTest.
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id$
  7. */
  8. /**#@+
  9. * include SimpleTest files
  10. */
  11. require_once(dirname(__FILE__) . '/page.php');
  12. require_once(dirname(__FILE__) . '/encoding.php');
  13. /**#@-*/
  14. /**
  15. * Creates tags and widgets given HTML tag
  16. * attributes.
  17. * @package SimpleTest
  18. * @subpackage WebTester
  19. */
  20. class SimpleTagBuilder {
  21. /**
  22. * Factory for the tag objects. Creates the
  23. * appropriate tag object for the incoming tag name
  24. * and attributes.
  25. * @param string $name HTML tag name.
  26. * @param hash $attributes Element attributes.
  27. * @return SimpleTag Tag object.
  28. * @access public
  29. */
  30. function createTag($name, $attributes) {
  31. static $map = array(
  32. 'a' => 'SimpleAnchorTag',
  33. 'title' => 'SimpleTitleTag',
  34. 'base' => 'SimpleBaseTag',
  35. 'button' => 'SimpleButtonTag',
  36. 'textarea' => 'SimpleTextAreaTag',
  37. 'option' => 'SimpleOptionTag',
  38. 'label' => 'SimpleLabelTag',
  39. 'form' => 'SimpleFormTag',
  40. 'frame' => 'SimpleFrameTag');
  41. $attributes = $this->keysToLowerCase($attributes);
  42. if (array_key_exists($name, $map)) {
  43. $tag_class = $map[$name];
  44. return new $tag_class($attributes);
  45. } elseif ($name == 'select') {
  46. return $this->createSelectionTag($attributes);
  47. } elseif ($name == 'input') {
  48. return $this->createInputTag($attributes);
  49. }
  50. return new SimpleTag($name, $attributes);
  51. }
  52. /**
  53. * Factory for selection fields.
  54. * @param hash $attributes Element attributes.
  55. * @return SimpleTag Tag object.
  56. * @access protected
  57. */
  58. protected function createSelectionTag($attributes) {
  59. if (isset($attributes['multiple'])) {
  60. return new MultipleSelectionTag($attributes);
  61. }
  62. return new SimpleSelectionTag($attributes);
  63. }
  64. /**
  65. * Factory for input tags.
  66. * @param hash $attributes Element attributes.
  67. * @return SimpleTag Tag object.
  68. * @access protected
  69. */
  70. protected function createInputTag($attributes) {
  71. if (! isset($attributes['type'])) {
  72. return new SimpleTextTag($attributes);
  73. }
  74. $type = strtolower(trim($attributes['type']));
  75. $map = array(
  76. 'submit' => 'SimpleSubmitTag',
  77. 'image' => 'SimpleImageSubmitTag',
  78. 'checkbox' => 'SimpleCheckboxTag',
  79. 'radio' => 'SimpleRadioButtonTag',
  80. 'text' => 'SimpleTextTag',
  81. 'hidden' => 'SimpleTextTag',
  82. 'password' => 'SimpleTextTag',
  83. 'file' => 'SimpleUploadTag');
  84. if (array_key_exists($type, $map)) {
  85. $tag_class = $map[$type];
  86. return new $tag_class($attributes);
  87. }
  88. return false;
  89. }
  90. /**
  91. * Make the keys lower case for case insensitive look-ups.
  92. * @param hash $map Hash to convert.
  93. * @return hash Unchanged values, but keys lower case.
  94. * @access private
  95. */
  96. protected function keysToLowerCase($map) {
  97. $lower = array();
  98. foreach ($map as $key => $value) {
  99. $lower[strtolower($key)] = $value;
  100. }
  101. return $lower;
  102. }
  103. }
  104. /**
  105. * HTML or XML tag.
  106. * @package SimpleTest
  107. * @subpackage WebTester
  108. */
  109. class SimpleTag {
  110. private $name;
  111. private $attributes;
  112. private $content;
  113. /**
  114. * Starts with a named tag with attributes only.
  115. * @param string $name Tag name.
  116. * @param hash $attributes Attribute names and
  117. * string values. Note that
  118. * the keys must have been
  119. * converted to lower case.
  120. */
  121. function __construct($name, $attributes) {
  122. $this->name = strtolower(trim($name));
  123. $this->attributes = $attributes;
  124. $this->content = '';
  125. }
  126. /**
  127. * Check to see if the tag can have both start and
  128. * end tags with content in between.
  129. * @return boolean True if content allowed.
  130. * @access public
  131. */
  132. function expectEndTag() {
  133. return true;
  134. }
  135. /**
  136. * The current tag should not swallow all content for
  137. * itself as it's searchable page content. Private
  138. * content tags are usually widgets that contain default
  139. * values.
  140. * @return boolean False as content is available
  141. * to other tags by default.
  142. * @access public
  143. */
  144. function isPrivateContent() {
  145. return false;
  146. }
  147. /**
  148. * Appends string content to the current content.
  149. * @param string $content Additional text.
  150. * @access public
  151. */
  152. function addContent($content) {
  153. $this->content .= (string)$content;
  154. return $this;
  155. }
  156. /**
  157. * Adds an enclosed tag to the content.
  158. * @param SimpleTag $tag New tag.
  159. * @access public
  160. */
  161. function addTag($tag) {
  162. }
  163. /**
  164. * Adds multiple enclosed tags to the content.
  165. * @param array List of SimpleTag objects to be added.
  166. */
  167. function addTags($tags) {
  168. foreach ($tags as $tag) {
  169. $this->addTag($tag);
  170. }
  171. }
  172. /**
  173. * Accessor for tag name.
  174. * @return string Name of tag.
  175. * @access public
  176. */
  177. function getTagName() {
  178. return $this->name;
  179. }
  180. /**
  181. * List of legal child elements.
  182. * @return array List of element names.
  183. * @access public
  184. */
  185. function getChildElements() {
  186. return array();
  187. }
  188. /**
  189. * Accessor for an attribute.
  190. * @param string $label Attribute name.
  191. * @return string Attribute value.
  192. * @access public
  193. */
  194. function getAttribute($label) {
  195. $label = strtolower($label);
  196. if (! isset($this->attributes[$label])) {
  197. return false;
  198. }
  199. return (string)$this->attributes[$label];
  200. }
  201. /**
  202. * Sets an attribute.
  203. * @param string $label Attribute name.
  204. * @return string $value New attribute value.
  205. * @access protected
  206. */
  207. protected function setAttribute($label, $value) {
  208. $this->attributes[strtolower($label)] = $value;
  209. }
  210. /**
  211. * Accessor for the whole content so far.
  212. * @return string Content as big raw string.
  213. * @access public
  214. */
  215. function getContent() {
  216. return $this->content;
  217. }
  218. /**
  219. * Accessor for content reduced to visible text. Acts
  220. * like a text mode browser, normalising space and
  221. * reducing images to their alt text.
  222. * @return string Content as plain text.
  223. * @access public
  224. */
  225. function getText() {
  226. return SimplePage::normalise($this->content);
  227. }
  228. /**
  229. * Test to see if id attribute matches.
  230. * @param string $id ID to test against.
  231. * @return boolean True on match.
  232. * @access public
  233. */
  234. function isId($id) {
  235. return ($this->getAttribute('id') == $id);
  236. }
  237. }
  238. /**
  239. * Base url.
  240. * @package SimpleTest
  241. * @subpackage WebTester
  242. */
  243. class SimpleBaseTag extends SimpleTag {
  244. /**
  245. * Starts with a named tag with attributes only.
  246. * @param hash $attributes Attribute names and
  247. * string values.
  248. */
  249. function __construct($attributes) {
  250. parent::__construct('base', $attributes);
  251. }
  252. /**
  253. * Base tag is not a block tag.
  254. * @return boolean false
  255. * @access public
  256. */
  257. function expectEndTag() {
  258. return false;
  259. }
  260. }
  261. /**
  262. * Page title.
  263. * @package SimpleTest
  264. * @subpackage WebTester
  265. */
  266. class SimpleTitleTag extends SimpleTag {
  267. /**
  268. * Starts with a named tag with attributes only.
  269. * @param hash $attributes Attribute names and
  270. * string values.
  271. */
  272. function __construct($attributes) {
  273. parent::__construct('title', $attributes);
  274. }
  275. }
  276. /**
  277. * Link.
  278. * @package SimpleTest
  279. * @subpackage WebTester
  280. */
  281. class SimpleAnchorTag extends SimpleTag {
  282. /**
  283. * Starts with a named tag with attributes only.
  284. * @param hash $attributes Attribute names and
  285. * string values.
  286. */
  287. function __construct($attributes) {
  288. parent::__construct('a', $attributes);
  289. }
  290. /**
  291. * Accessor for URL as string.
  292. * @return string Coerced as string.
  293. * @access public
  294. */
  295. function getHref() {
  296. $url = $this->getAttribute('href');
  297. if (is_bool($url)) {
  298. $url = '';
  299. }
  300. return $url;
  301. }
  302. }
  303. /**
  304. * Form element.
  305. * @package SimpleTest
  306. * @subpackage WebTester
  307. */
  308. class SimpleWidget extends SimpleTag {
  309. private $value;
  310. private $label;
  311. private $is_set;
  312. /**
  313. * Starts with a named tag with attributes only.
  314. * @param string $name Tag name.
  315. * @param hash $attributes Attribute names and
  316. * string values.
  317. */
  318. function __construct($name, $attributes) {
  319. parent::__construct($name, $attributes);
  320. $this->value = false;
  321. $this->label = false;
  322. $this->is_set = false;
  323. }
  324. /**
  325. * Accessor for name submitted as the key in
  326. * GET/POST privateiables hash.
  327. * @return string Parsed value.
  328. * @access public
  329. */
  330. function getName() {
  331. return $this->getAttribute('name');
  332. }
  333. /**
  334. * Accessor for default value parsed with the tag.
  335. * @return string Parsed value.
  336. * @access public
  337. */
  338. function getDefault() {
  339. return $this->getAttribute('value');
  340. }
  341. /**
  342. * Accessor for currently set value or default if
  343. * none.
  344. * @return string Value set by form or default
  345. * if none.
  346. * @access public
  347. */
  348. function getValue() {
  349. if (! $this->is_set) {
  350. return $this->getDefault();
  351. }
  352. return $this->value;
  353. }
  354. /**
  355. * Sets the current form element value.
  356. * @param string $value New value.
  357. * @return boolean True if allowed.
  358. * @access public
  359. */
  360. function setValue($value) {
  361. $this->value = $value;
  362. $this->is_set = true;
  363. return true;
  364. }
  365. /**
  366. * Resets the form element value back to the
  367. * default.
  368. * @access public
  369. */
  370. function resetValue() {
  371. $this->is_set = false;
  372. }
  373. /**
  374. * Allows setting of a label externally, say by a
  375. * label tag.
  376. * @param string $label Label to attach.
  377. * @access public
  378. */
  379. function setLabel($label) {
  380. $this->label = trim($label);
  381. return $this;
  382. }
  383. /**
  384. * Reads external or internal label.
  385. * @param string $label Label to test.
  386. * @return boolean True is match.
  387. * @access public
  388. */
  389. function isLabel($label) {
  390. return $this->label == trim($label);
  391. }
  392. /**
  393. * Dispatches the value into the form encoded packet.
  394. * @param SimpleEncoding $encoding Form packet.
  395. * @access public
  396. */
  397. function write($encoding) {
  398. if ($this->getName()) {
  399. $encoding->add($this->getName(), $this->getValue());
  400. }
  401. }
  402. }
  403. /**
  404. * Text, password and hidden field.
  405. * @package SimpleTest
  406. * @subpackage WebTester
  407. */
  408. class SimpleTextTag extends SimpleWidget {
  409. /**
  410. * Starts with a named tag with attributes only.
  411. * @param hash $attributes Attribute names and
  412. * string values.
  413. */
  414. function __construct($attributes) {
  415. parent::__construct('input', $attributes);
  416. if ($this->getAttribute('value') === false) {
  417. $this->setAttribute('value', '');
  418. }
  419. }
  420. /**
  421. * Tag contains no content.
  422. * @return boolean False.
  423. * @access public
  424. */
  425. function expectEndTag() {
  426. return false;
  427. }
  428. /**
  429. * Sets the current form element value. Cannot
  430. * change the value of a hidden field.
  431. * @param string $value New value.
  432. * @return boolean True if allowed.
  433. * @access public
  434. */
  435. function setValue($value) {
  436. if ($this->getAttribute('type') == 'hidden') {
  437. return false;
  438. }
  439. return parent::setValue($value);
  440. }
  441. }
  442. /**
  443. * Submit button as input tag.
  444. * @package SimpleTest
  445. * @subpackage WebTester
  446. */
  447. class SimpleSubmitTag extends SimpleWidget {
  448. /**
  449. * Starts with a named tag with attributes only.
  450. * @param hash $attributes Attribute names and
  451. * string values.
  452. */
  453. function __construct($attributes) {
  454. parent::__construct('input', $attributes);
  455. if ($this->getAttribute('value') === false) {
  456. $this->setAttribute('value', 'Submit');
  457. }
  458. }
  459. /**
  460. * Tag contains no end element.
  461. * @return boolean False.
  462. * @access public
  463. */
  464. function expectEndTag() {
  465. return false;
  466. }
  467. /**
  468. * Disables the setting of the button value.
  469. * @param string $value Ignored.
  470. * @return boolean True if allowed.
  471. * @access public
  472. */
  473. function setValue($value) {
  474. return false;
  475. }
  476. /**
  477. * Value of browser visible text.
  478. * @return string Visible label.
  479. * @access public
  480. */
  481. function getLabel() {
  482. return $this->getValue();
  483. }
  484. /**
  485. * Test for a label match when searching.
  486. * @param string $label Label to test.
  487. * @return boolean True on match.
  488. * @access public
  489. */
  490. function isLabel($label) {
  491. return trim($label) == trim($this->getLabel());
  492. }
  493. }
  494. /**
  495. * Image button as input tag.
  496. * @package SimpleTest
  497. * @subpackage WebTester
  498. */
  499. class SimpleImageSubmitTag extends SimpleWidget {
  500. /**
  501. * Starts with a named tag with attributes only.
  502. * @param hash $attributes Attribute names and
  503. * string values.
  504. */
  505. function __construct($attributes) {
  506. parent::__construct('input', $attributes);
  507. }
  508. /**
  509. * Tag contains no end element.
  510. * @return boolean False.
  511. * @access public
  512. */
  513. function expectEndTag() {
  514. return false;
  515. }
  516. /**
  517. * Disables the setting of the button value.
  518. * @param string $value Ignored.
  519. * @return boolean True if allowed.
  520. * @access public
  521. */
  522. function setValue($value) {
  523. return false;
  524. }
  525. /**
  526. * Value of browser visible text.
  527. * @return string Visible label.
  528. * @access public
  529. */
  530. function getLabel() {
  531. if ($this->getAttribute('title')) {
  532. return $this->getAttribute('title');
  533. }
  534. return $this->getAttribute('alt');
  535. }
  536. /**
  537. * Test for a label match when searching.
  538. * @param string $label Label to test.
  539. * @return boolean True on match.
  540. * @access public
  541. */
  542. function isLabel($label) {
  543. return trim($label) == trim($this->getLabel());
  544. }
  545. /**
  546. * Dispatches the value into the form encoded packet.
  547. * @param SimpleEncoding $encoding Form packet.
  548. * @param integer $x X coordinate of click.
  549. * @param integer $y Y coordinate of click.
  550. * @access public
  551. */
  552. function write($encoding, $x = 1, $y = 1) {
  553. if ($this->getName()) {
  554. $encoding->add($this->getName() . '.x', $x);
  555. $encoding->add($this->getName() . '.y', $y);
  556. } else {
  557. $encoding->add('x', $x);
  558. $encoding->add('y', $y);
  559. }
  560. }
  561. }
  562. /**
  563. * Submit button as button tag.
  564. * @package SimpleTest
  565. * @subpackage WebTester
  566. */
  567. class SimpleButtonTag extends SimpleWidget {
  568. /**
  569. * Starts with a named tag with attributes only.
  570. * Defaults are very browser dependent.
  571. * @param hash $attributes Attribute names and
  572. * string values.
  573. */
  574. function __construct($attributes) {
  575. parent::__construct('button', $attributes);
  576. }
  577. /**
  578. * Check to see if the tag can have both start and
  579. * end tags with content in between.
  580. * @return boolean True if content allowed.
  581. * @access public
  582. */
  583. function expectEndTag() {
  584. return true;
  585. }
  586. /**
  587. * Disables the setting of the button value.
  588. * @param string $value Ignored.
  589. * @return boolean True if allowed.
  590. * @access public
  591. */
  592. function setValue($value) {
  593. return false;
  594. }
  595. /**
  596. * Value of browser visible text.
  597. * @return string Visible label.
  598. * @access public
  599. */
  600. function getLabel() {
  601. return $this->getContent();
  602. }
  603. /**
  604. * Test for a label match when searching.
  605. * @param string $label Label to test.
  606. * @return boolean True on match.
  607. * @access public
  608. */
  609. function isLabel($label) {
  610. return trim($label) == trim($this->getLabel());
  611. }
  612. }
  613. /**
  614. * Content tag for text area.
  615. * @package SimpleTest
  616. * @subpackage WebTester
  617. */
  618. class SimpleTextAreaTag extends SimpleWidget {
  619. /**
  620. * Starts with a named tag with attributes only.
  621. * @param hash $attributes Attribute names and
  622. * string values.
  623. */
  624. function __construct($attributes) {
  625. parent::__construct('textarea', $attributes);
  626. }
  627. /**
  628. * Accessor for starting value.
  629. * @return string Parsed value.
  630. * @access public
  631. */
  632. function getDefault() {
  633. return $this->wrap(html_entity_decode($this->getContent(), ENT_QUOTES));
  634. }
  635. /**
  636. * Applies word wrapping if needed.
  637. * @param string $value New value.
  638. * @return boolean True if allowed.
  639. * @access public
  640. */
  641. function setValue($value) {
  642. return parent::setValue($this->wrap($value));
  643. }
  644. /**
  645. * Test to see if text should be wrapped.
  646. * @return boolean True if wrapping on.
  647. * @access private
  648. */
  649. function wrapIsEnabled() {
  650. if ($this->getAttribute('cols')) {
  651. $wrap = $this->getAttribute('wrap');
  652. if (($wrap == 'physical') || ($wrap == 'hard')) {
  653. return true;
  654. }
  655. }
  656. return false;
  657. }
  658. /**
  659. * Performs the formatting that is peculiar to
  660. * this tag. There is strange behaviour in this
  661. * one, including stripping a leading new line.
  662. * Go figure. I am using Firefox as a guide.
  663. * @param string $text Text to wrap.
  664. * @return string Text wrapped with carriage
  665. * returns and line feeds
  666. * @access private
  667. */
  668. protected function wrap($text) {
  669. $text = str_replace("\r\r\n", "\r\n", str_replace("\n", "\r\n", $text));
  670. $text = str_replace("\r\n\n", "\r\n", str_replace("\r", "\r\n", $text));
  671. if (strncmp($text, "\r\n", strlen("\r\n")) == 0) {
  672. $text = substr($text, strlen("\r\n"));
  673. }
  674. if ($this->wrapIsEnabled()) {
  675. return wordwrap(
  676. $text,
  677. (integer)$this->getAttribute('cols'),
  678. "\r\n");
  679. }
  680. return $text;
  681. }
  682. /**
  683. * The content of textarea is not part of the page.
  684. * @return boolean True.
  685. * @access public
  686. */
  687. function isPrivateContent() {
  688. return true;
  689. }
  690. }
  691. /**
  692. * File upload widget.
  693. * @package SimpleTest
  694. * @subpackage WebTester
  695. */
  696. class SimpleUploadTag extends SimpleWidget {
  697. /**
  698. * Starts with attributes only.
  699. * @param hash $attributes Attribute names and
  700. * string values.
  701. */
  702. function __construct($attributes) {
  703. parent::__construct('input', $attributes);
  704. }
  705. /**
  706. * Tag contains no content.
  707. * @return boolean False.
  708. * @access public
  709. */
  710. function expectEndTag() {
  711. return false;
  712. }
  713. /**
  714. * Dispatches the value into the form encoded packet.
  715. * @param SimpleEncoding $encoding Form packet.
  716. * @access public
  717. */
  718. function write($encoding) {
  719. if (! file_exists($this->getValue())) {
  720. return;
  721. }
  722. $encoding->attach(
  723. $this->getName(),
  724. implode('', file($this->getValue())),
  725. basename($this->getValue()));
  726. }
  727. }
  728. /**
  729. * Drop down widget.
  730. * @package SimpleTest
  731. * @subpackage WebTester
  732. */
  733. class SimpleSelectionTag extends SimpleWidget {
  734. private $options;
  735. private $choice;
  736. /**
  737. * Starts with attributes only.
  738. * @param hash $attributes Attribute names and
  739. * string values.
  740. */
  741. function __construct($attributes) {
  742. parent::__construct('select', $attributes);
  743. $this->options = array();
  744. $this->choice = false;
  745. }
  746. /**
  747. * Adds an option tag to a selection field.
  748. * @param SimpleOptionTag $tag New option.
  749. * @access public
  750. */
  751. function addTag($tag) {
  752. if ($tag->getTagName() == 'option') {
  753. $this->options[] = $tag;
  754. }
  755. }
  756. /**
  757. * Text within the selection element is ignored.
  758. * @param string $content Ignored.
  759. * @access public
  760. */
  761. function addContent($content) {
  762. return $this;
  763. }
  764. /**
  765. * Scans options for defaults. If none, then
  766. * the first option is selected.
  767. * @return string Selected field.
  768. * @access public
  769. */
  770. function getDefault() {
  771. for ($i = 0, $count = count($this->options); $i < $count; $i++) {
  772. if ($this->options[$i]->getAttribute('selected') !== false) {
  773. return $this->options[$i]->getDefault();
  774. }
  775. }
  776. if ($count > 0) {
  777. return $this->options[0]->getDefault();
  778. }
  779. return '';
  780. }
  781. /**
  782. * Can only set allowed values.
  783. * @param string $value New choice.
  784. * @return boolean True if allowed.
  785. * @access public
  786. */
  787. function setValue($value) {
  788. for ($i = 0, $count = count($this->options); $i < $count; $i++) {
  789. if ($this->options[$i]->isValue($value)) {
  790. $this->choice = $i;
  791. return true;
  792. }
  793. }
  794. return false;
  795. }
  796. /**
  797. * Accessor for current selection value.
  798. * @return string Value attribute or
  799. * content of opton.
  800. * @access public
  801. */
  802. function getValue() {
  803. if ($this->choice === false) {
  804. return $this->getDefault();
  805. }
  806. return $this->options[$this->choice]->getValue();
  807. }
  808. }
  809. /**
  810. * Drop down widget.
  811. * @package SimpleTest
  812. * @subpackage WebTester
  813. */
  814. class MultipleSelectionTag extends SimpleWidget {
  815. private $options;
  816. private $values;
  817. /**
  818. * Starts with attributes only.
  819. * @param hash $attributes Attribute names and
  820. * string values.
  821. */
  822. function __construct($attributes) {
  823. parent::__construct('select', $attributes);
  824. $this->options = array();
  825. $this->values = false;
  826. }
  827. /**
  828. * Adds an option tag to a selection field.
  829. * @param SimpleOptionTag $tag New option.
  830. * @access public
  831. */
  832. function addTag($tag) {
  833. if ($tag->getTagName() == 'option') {
  834. $this->options[] = &$tag;
  835. }
  836. }
  837. /**
  838. * Text within the selection element is ignored.
  839. * @param string $content Ignored.
  840. * @access public
  841. */
  842. function addContent($content) {
  843. return $this;
  844. }
  845. /**
  846. * Scans options for defaults to populate the
  847. * value array().
  848. * @return array Selected fields.
  849. * @access public
  850. */
  851. function getDefault() {
  852. $default = array();
  853. for ($i = 0, $count = count($this->options); $i < $count; $i++) {
  854. if ($this->options[$i]->getAttribute('selected') !== false) {
  855. $default[] = $this->options[$i]->getDefault();
  856. }
  857. }
  858. return $default;
  859. }
  860. /**
  861. * Can only set allowed values. Any illegal value
  862. * will result in a failure, but all correct values
  863. * will be set.
  864. * @param array $desired New choices.
  865. * @return boolean True if all allowed.
  866. * @access public
  867. */
  868. function setValue($desired) {
  869. $achieved = array();
  870. foreach ($desired as $value) {
  871. $success = false;
  872. for ($i = 0, $count = count($this->options); $i < $count; $i++) {
  873. if ($this->options[$i]->isValue($value)) {
  874. $achieved[] = $this->options[$i]->getValue();
  875. $success = true;
  876. break;
  877. }
  878. }
  879. if (! $success) {
  880. return false;
  881. }
  882. }
  883. $this->values = $achieved;
  884. return true;
  885. }
  886. /**
  887. * Accessor for current selection value.
  888. * @return array List of currently set options.
  889. * @access public
  890. */
  891. function getValue() {
  892. if ($this->values === false) {
  893. return $this->getDefault();
  894. }
  895. return $this->values;
  896. }
  897. }
  898. /**
  899. * Option for selection field.
  900. * @package SimpleTest
  901. * @subpackage WebTester
  902. */
  903. class SimpleOptionTag extends SimpleWidget {
  904. /**
  905. * Stashes the attributes.
  906. */
  907. function __construct($attributes) {
  908. parent::__construct('option', $attributes);
  909. }
  910. /**
  911. * Does nothing.
  912. * @param string $value Ignored.
  913. * @return boolean Not allowed.
  914. * @access public
  915. */
  916. function setValue($value) {
  917. return false;
  918. }
  919. /**
  920. * Test to see if a value matches the option.
  921. * @param string $compare Value to compare with.
  922. * @return boolean True if possible match.
  923. * @access public
  924. */
  925. function isValue($compare) {
  926. $compare = trim($compare);
  927. if (trim($this->getValue()) == $compare) {
  928. return true;
  929. }
  930. return trim(strip_tags($this->getContent())) == $compare;
  931. }
  932. /**
  933. * Accessor for starting value. Will be set to
  934. * the option label if no value exists.
  935. * @return string Parsed value.
  936. * @access public
  937. */
  938. function getDefault() {
  939. if ($this->getAttribute('value') === false) {
  940. return strip_tags($this->getContent());
  941. }
  942. return $this->getAttribute('value');
  943. }
  944. /**
  945. * The content of options is not part of the page.
  946. * @return boolean True.
  947. * @access public
  948. */
  949. function isPrivateContent() {
  950. return true;
  951. }
  952. }
  953. /**
  954. * Radio button.
  955. * @package SimpleTest
  956. * @subpackage WebTester
  957. */
  958. class SimpleRadioButtonTag extends SimpleWidget {
  959. /**
  960. * Stashes the attributes.
  961. * @param array $attributes Hash of attributes.
  962. */
  963. function __construct($attributes) {
  964. parent::__construct('input', $attributes);
  965. if ($this->getAttribute('value') === false) {
  966. $this->setAttribute('value', 'on');
  967. }
  968. }
  969. /**
  970. * Tag contains no content.
  971. * @return boolean False.
  972. * @access public
  973. */
  974. function expectEndTag() {
  975. return false;
  976. }
  977. /**
  978. * The only allowed value sn the one in the
  979. * "value" attribute.
  980. * @param string $value New value.
  981. * @return boolean True if allowed.
  982. * @access public
  983. */
  984. function setValue($value) {
  985. if ($value === false) {
  986. return parent::setValue($value);
  987. }
  988. if ($value != $this->getAttribute('value')) {
  989. return false;
  990. }
  991. return parent::setValue($value);
  992. }
  993. /**
  994. * Accessor for starting value.
  995. * @return string Parsed value.
  996. * @access public
  997. */
  998. function getDefault() {
  999. if ($this->getAttribute('checked') !== false) {
  1000. return $this->getAttribute('value');
  1001. }
  1002. return false;
  1003. }
  1004. }
  1005. /**
  1006. * Checkbox widget.
  1007. * @package SimpleTest
  1008. * @subpackage WebTester
  1009. */
  1010. class SimpleCheckboxTag extends SimpleWidget {
  1011. /**
  1012. * Starts with attributes only.
  1013. * @param hash $attributes Attribute names and
  1014. * string values.
  1015. */
  1016. function __construct($attributes) {
  1017. parent::__construct('input', $attributes);
  1018. if ($this->getAttribute('value') === false) {
  1019. $this->setAttribute('value', 'on');
  1020. }
  1021. }
  1022. /**
  1023. * Tag contains no content.
  1024. * @return boolean False.
  1025. * @access public
  1026. */
  1027. function expectEndTag() {
  1028. return false;
  1029. }
  1030. /**
  1031. * The only allowed value in the one in the
  1032. * "value" attribute. The default for this
  1033. * attribute is "on". If this widget is set to
  1034. * true, then the usual value will be taken.
  1035. * @param string $value New value.
  1036. * @return boolean True if allowed.
  1037. * @access public
  1038. */
  1039. function setValue($value) {
  1040. if ($value === false) {
  1041. return parent::setValue($value);
  1042. }
  1043. if ($value === true) {
  1044. return parent::setValue($this->getAttribute('value'));
  1045. }
  1046. if ($value != $this->getAttribute('value')) {
  1047. return false;
  1048. }
  1049. return parent::setValue($value);
  1050. }
  1051. /**
  1052. * Accessor for starting value. The default
  1053. * value is "on".
  1054. * @return string Parsed value.
  1055. * @access public
  1056. */
  1057. function getDefault() {
  1058. if ($this->getAttribute('checked') !== false) {
  1059. return $this->getAttribute('value');
  1060. }
  1061. return false;
  1062. }
  1063. }
  1064. /**
  1065. * A group of multiple widgets with some shared behaviour.
  1066. * @package SimpleTest
  1067. * @subpackage WebTester
  1068. */
  1069. class SimpleTagGroup {
  1070. private $widgets = array();
  1071. /**
  1072. * Adds a tag to the group.
  1073. * @param SimpleWidget $widget
  1074. * @access public
  1075. */
  1076. function addWidget($widget) {
  1077. $this->widgets[] = $widget;
  1078. }
  1079. /**
  1080. * Accessor to widget set.
  1081. * @return array All widgets.
  1082. * @access protected
  1083. */
  1084. protected function &getWidgets() {
  1085. return $this->widgets;
  1086. }
  1087. /**
  1088. * Accessor for an attribute.
  1089. * @param string $label Attribute name.
  1090. * @return boolean Always false.
  1091. * @access public
  1092. */
  1093. function getAttribute($label) {
  1094. return false;
  1095. }
  1096. /**
  1097. * Fetches the name for the widget from the first
  1098. * member.
  1099. * @return string Name of widget.
  1100. * @access public
  1101. */
  1102. function getName() {
  1103. if (count($this->widgets) > 0) {
  1104. return $this->widgets[0]->getName();
  1105. }
  1106. }
  1107. /**
  1108. * Scans the widgets for one with the appropriate
  1109. * ID field.
  1110. * @param string $id ID value to try.
  1111. * @return boolean True if matched.
  1112. * @access public
  1113. */
  1114. function isId($id) {
  1115. for ($i = 0, $count = count($this->widgets); $i < $count; $i++) {
  1116. if ($this->widgets[$i]->isId($id)) {
  1117. return true;
  1118. }
  1119. }
  1120. return false;
  1121. }
  1122. /**
  1123. * Scans the widgets for one with the appropriate
  1124. * attached label.
  1125. * @param string $label Attached label to try.
  1126. * @return boolean True if matched.
  1127. * @access public
  1128. */
  1129. function isLabel($label) {
  1130. for ($i = 0, $count = count($this->widgets); $i < $count; $i++) {
  1131. if ($this->widgets[$i]->isLabel($label)) {
  1132. return true;
  1133. }
  1134. }
  1135. return false;
  1136. }
  1137. /**
  1138. * Dispatches the value into the form encoded packet.
  1139. * @param SimpleEncoding $encoding Form packet.
  1140. * @access public
  1141. */
  1142. function write($encoding) {
  1143. $encoding->add($this->getName(), $this->getValue());
  1144. }
  1145. }
  1146. /**
  1147. * A group of tags with the same name within a form.
  1148. * @package SimpleTest
  1149. * @subpackage WebTester
  1150. */
  1151. class SimpleCheckboxGroup extends SimpleTagGroup {
  1152. /**
  1153. * Accessor for current selected widget or false
  1154. * if none.
  1155. * @return string/array Widget values or false if none.
  1156. * @access public
  1157. */
  1158. function getValue() {
  1159. $values = array();
  1160. $widgets = $this->getWidgets();
  1161. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1162. if ($widgets[$i]->getValue() !== false) {
  1163. $values[] = $widgets[$i]->getValue();
  1164. }
  1165. }
  1166. return $this->coerceValues($values);
  1167. }
  1168. /**
  1169. * Accessor for starting value that is active.
  1170. * @return string/array Widget values or false if none.
  1171. * @access public
  1172. */
  1173. function getDefault() {
  1174. $values = array();
  1175. $widgets = $this->getWidgets();
  1176. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1177. if ($widgets[$i]->getDefault() !== false) {
  1178. $values[] = $widgets[$i]->getDefault();
  1179. }
  1180. }
  1181. return $this->coerceValues($values);
  1182. }
  1183. /**
  1184. * Accessor for current set values.
  1185. * @param string/array/boolean $values Either a single string, a
  1186. * hash or false for nothing set.
  1187. * @return boolean True if all values can be set.
  1188. * @access public
  1189. */
  1190. function setValue($values) {
  1191. $values = $this->makeArray($values);
  1192. if (! $this->valuesArePossible($values)) {
  1193. return false;
  1194. }
  1195. $widgets = $this->getWidgets();
  1196. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1197. $possible = $widgets[$i]->getAttribute('value');
  1198. if (in_array($widgets[$i]->getAttribute('value'), $values)) {
  1199. $widgets[$i]->setValue($possible);
  1200. } else {
  1201. $widgets[$i]->setValue(false);
  1202. }
  1203. }
  1204. return true;
  1205. }
  1206. /**
  1207. * Tests to see if a possible value set is legal.
  1208. * @param string/array/boolean $values Either a single string, a
  1209. * hash or false for nothing set.
  1210. * @return boolean False if trying to set a
  1211. * missing value.
  1212. * @access private
  1213. */
  1214. protected function valuesArePossible($values) {
  1215. $matches = array();
  1216. $widgets = &$this->getWidgets();
  1217. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1218. $possible = $widgets[$i]->getAttribute('value');
  1219. if (in_array($possible, $values)) {
  1220. $matches[] = $possible;
  1221. }
  1222. }
  1223. return ($values == $matches);
  1224. }
  1225. /**
  1226. * Converts the output to an appropriate format. This means
  1227. * that no values is false, a single value is just that
  1228. * value and only two or more are contained in an array.
  1229. * @param array $values List of values of widgets.
  1230. * @return string/array/boolean Expected format for a tag.
  1231. * @access private
  1232. */
  1233. protected function coerceValues($values) {
  1234. if (count($values) == 0) {
  1235. return false;
  1236. } elseif (count($values) == 1) {
  1237. return $values[0];
  1238. } else {
  1239. return $values;
  1240. }
  1241. }
  1242. /**
  1243. * Converts false or string into array. The opposite of
  1244. * the coercian method.
  1245. * @param string/array/boolean $value A single item is converted
  1246. * to a one item list. False
  1247. * gives an empty list.
  1248. * @return array List of values, possibly empty.
  1249. * @access private
  1250. */
  1251. protected function makeArray($value) {
  1252. if ($value === false) {
  1253. return array();
  1254. }
  1255. if (is_string($value)) {
  1256. return array($value);
  1257. }
  1258. return $value;
  1259. }
  1260. }
  1261. /**
  1262. * A group of tags with the same name within a form.
  1263. * Used for radio buttons.
  1264. * @package SimpleTest
  1265. * @subpackage WebTester
  1266. */
  1267. class SimpleRadioGroup extends SimpleTagGroup {
  1268. /**
  1269. * Each tag is tried in turn until one is
  1270. * successfully set. The others will be
  1271. * unchecked if successful.
  1272. * @param string $value New value.
  1273. * @return boolean True if any allowed.
  1274. * @access public
  1275. */
  1276. function setValue($value) {
  1277. if (! $this->valueIsPossible($value)) {
  1278. return false;
  1279. }
  1280. $index = false;
  1281. $widgets = $this->getWidgets();
  1282. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1283. if (! $widgets[$i]->setValue($value)) {
  1284. $widgets[$i]->setValue(false);
  1285. }
  1286. }
  1287. return true;
  1288. }
  1289. /**
  1290. * Tests to see if a value is allowed.
  1291. * @param string Attempted value.
  1292. * @return boolean True if a valid value.
  1293. * @access private
  1294. */
  1295. protected function valueIsPossible($value) {
  1296. $widgets = $this->getWidgets();
  1297. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1298. if ($widgets[$i]->getAttribute('value') == $value) {
  1299. return true;
  1300. }
  1301. }
  1302. return false;
  1303. }
  1304. /**
  1305. * Accessor for current selected widget or false
  1306. * if none.
  1307. * @return string/boolean Value attribute or
  1308. * content of opton.
  1309. * @access public
  1310. */
  1311. function getValue() {
  1312. $widgets = $this->getWidgets();
  1313. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1314. if ($widgets[$i]->getValue() !== false) {
  1315. return $widgets[$i]->getValue();
  1316. }
  1317. }
  1318. return false;
  1319. }
  1320. /**
  1321. * Accessor for starting value that is active.
  1322. * @return string/boolean Value of first checked
  1323. * widget or false if none.
  1324. * @access public
  1325. */
  1326. function getDefault() {
  1327. $widgets = $this->getWidgets();
  1328. for ($i = 0, $count = count($widgets); $i < $count; $i++) {
  1329. if ($widgets[$i]->getDefault() !== false) {
  1330. return $widgets[$i]->getDefault();
  1331. }
  1332. }
  1333. return false;
  1334. }
  1335. }
  1336. /**
  1337. * Tag to keep track of labels.
  1338. * @package SimpleTest
  1339. * @subpackage WebTester
  1340. */
  1341. class SimpleLabelTag extends SimpleTag {
  1342. /**
  1343. * Starts with a named tag with attributes only.
  1344. * @param hash $attributes Attribute names and
  1345. * string values.
  1346. */
  1347. function __construct($attributes) {
  1348. parent::__construct('label', $attributes);
  1349. }
  1350. /**
  1351. * Access for the ID to attach the label to.
  1352. * @return string For attribute.
  1353. * @access public
  1354. */
  1355. function getFor() {
  1356. return $this->getAttribute('for');
  1357. }
  1358. }
  1359. /**
  1360. * Tag to aid parsing the form.
  1361. * @package SimpleTest
  1362. * @subpackage WebTester
  1363. */
  1364. class SimpleFormTag extends SimpleTag {
  1365. /**
  1366. * Starts with a named tag with attributes only.
  1367. * @param hash $attributes Attribute names and
  1368. * string values.
  1369. */
  1370. function __construct($attributes) {
  1371. parent::__construct('form', $attributes);
  1372. }
  1373. }
  1374. /**
  1375. * Tag to aid parsing the frames in a page.
  1376. * @package SimpleTest
  1377. * @subpackage WebTester
  1378. */
  1379. class SimpleFrameTag extends SimpleTag {
  1380. /**
  1381. * Starts with a named tag with attributes only.
  1382. * @param hash $attributes Attribute names and
  1383. * string values.
  1384. */
  1385. function __construct($attributes) {
  1386. parent::__construct('frame', $attributes);
  1387. }
  1388. /**
  1389. * Tag contains no content.
  1390. * @return boolean False.
  1391. * @access public
  1392. */
  1393. function expectEndTag() {
  1394. return false;
  1395. }
  1396. }
  1397. ?>