tag.php 37 KB

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