PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/simpletest/web_tester.php

https://github.com/chregu/fluxcms
PHP | 1404 lines | 520 code | 108 blank | 776 comment | 45 complexity | 3fd9ee0140b7bc3125bec0200e92a700 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Base include file for SimpleTest.
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id: web_tester.php,v 1.111 2005/09/11 01:34:06 lastcraft Exp $
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/test_case.php');
  12. require_once(dirname(__FILE__) . '/browser.php');
  13. require_once(dirname(__FILE__) . '/page.php');
  14. require_once(dirname(__FILE__) . '/expectation.php');
  15. /**#@-*/
  16. /**
  17. * Test for an HTML widget value match.
  18. * @package SimpleTest
  19. * @subpackage WebTester
  20. */
  21. class FieldExpectation extends SimpleExpectation {
  22. var $_value;
  23. /**
  24. * Sets the field value to compare against.
  25. * @param mixed $value Test value to match. Can be an
  26. * expectation for say pattern matching.
  27. * @access public
  28. */
  29. function FieldExpectation($value, $message = '%s') {
  30. $this->SimpleExpectation($message);
  31. if (is_array($value)) {
  32. sort($value);
  33. }
  34. $this->_value = $value;
  35. }
  36. /**
  37. * Tests the expectation. True if it matches
  38. * a string value or an array value in any order.
  39. * @param mixed $compare Comparison value. False for
  40. * an unset field.
  41. * @return boolean True if correct.
  42. * @access public
  43. */
  44. function test($compare) {
  45. if ($this->_value === false) {
  46. return ($compare === false);
  47. }
  48. if ($this->_isSingle($this->_value)) {
  49. return $this->_testSingle($compare);
  50. }
  51. if (is_array($this->_value)) {
  52. return $this->_testMultiple($compare);
  53. }
  54. return false;
  55. }
  56. /**
  57. * Tests for valid field comparisons with a single option.
  58. * @param mixed $value Value to type check.
  59. * @return boolean True if integer, string or float.
  60. * @access private
  61. */
  62. function _isSingle($value) {
  63. return is_string($value) || is_integer($value) || is_float($value);
  64. }
  65. /**
  66. * String comparison for simple field with a single option.
  67. * @param mixed $compare String to test against.
  68. * @returns boolean True if matching.
  69. * @access private
  70. */
  71. function _testSingle($compare) {
  72. if (is_array($compare) && count($compare) == 1) {
  73. $compare = $compare[0];
  74. }
  75. if (! $this->_isSingle($compare)) {
  76. return false;
  77. }
  78. return ($this->_value == $compare);
  79. }
  80. /**
  81. * List comparison for multivalue field.
  82. * @param mixed $compare List in any order to test against.
  83. * @returns boolean True if matching.
  84. * @access private
  85. */
  86. function _testMultiple($compare) {
  87. if (is_string($compare)) {
  88. $compare = array($compare);
  89. }
  90. if (! is_array($compare)) {
  91. return false;
  92. }
  93. sort($compare);
  94. return ($this->_value === $compare);
  95. }
  96. /**
  97. * Returns a human readable test message.
  98. * @param mixed $compare Comparison value.
  99. * @return string Description of success
  100. * or failure.
  101. * @access public
  102. */
  103. function testMessage($compare) {
  104. $dumper = &$this->_getDumper();
  105. if (is_array($compare)) {
  106. sort($compare);
  107. }
  108. if ($this->test($compare)) {
  109. return "Field expectation [" . $dumper->describeValue($this->_value) . "]";
  110. } else {
  111. return "Field expectation [" . $dumper->describeValue($this->_value) .
  112. "] fails with [" .
  113. $this->_dumper->describeValue($compare) . "] " .
  114. $this->_dumper->describeDifference($this->_value, $compare);
  115. }
  116. }
  117. }
  118. /**
  119. * Test for a specific HTTP header within a header block.
  120. * @package SimpleTest
  121. * @subpackage WebTester
  122. */
  123. class HttpHeaderExpectation extends SimpleExpectation {
  124. var $_expected_header;
  125. var $_expected_value;
  126. /**
  127. * Sets the field and value to compare against.
  128. * @param string $header Case insenstive trimmed header name.
  129. * @param mixed $valu e Optional value to compare. If not
  130. * given then any value will match. If
  131. * an expectation object then that will
  132. * be used instead.
  133. */
  134. function HttpHeaderExpectation($header, $value = false) {
  135. $this->_expected_header = $this->_normaliseHeader($header);
  136. $this->_expected_value = $value;
  137. }
  138. /**
  139. * Accessor for subclases.
  140. * @return mixed Expectation set in constructor.
  141. * @access protected
  142. */
  143. function _getExpectation() {
  144. return $this->_expected_value;
  145. }
  146. /**
  147. * Removes whitespace at ends and case variations.
  148. * @param string $header Name of header.
  149. * @param string Trimmed and lowecased header
  150. * name.
  151. * @access private
  152. */
  153. function _normaliseHeader($header) {
  154. return strtolower(trim($header));
  155. }
  156. /**
  157. * Tests the expectation. True if it matches
  158. * a string value or an array value in any order.
  159. * @param mixed $compare Raw header block to search.
  160. * @return boolean True if header present.
  161. * @access public
  162. */
  163. function test($compare) {
  164. return is_string($this->_findHeader($compare));
  165. }
  166. /**
  167. * Searches the incoming result. Will extract the matching
  168. * line as text.
  169. * @param mixed $compare Raw header block to search.
  170. * @return string Matching header line.
  171. * @access protected
  172. */
  173. function _findHeader($compare) {
  174. $lines = split("\r\n", $compare);
  175. foreach ($lines as $line) {
  176. if ($this->_testHeaderLine($line)) {
  177. return $line;
  178. }
  179. }
  180. return false;
  181. }
  182. /**
  183. * Compares a single header line against the expectation.
  184. * @param string $line A single line to compare.
  185. * @return boolean True if matched.
  186. * @access private
  187. */
  188. function _testHeaderLine($line) {
  189. if (count($parsed = split(':', $line, 2)) < 2) {
  190. return false;
  191. }
  192. list($header, $value) = $parsed;
  193. if ($this->_normaliseHeader($header) != $this->_expected_header) {
  194. return false;
  195. }
  196. return $this->_testHeaderValue($value, $this->_expected_value);
  197. }
  198. /**
  199. * Tests the value part of the header.
  200. * @param string $value Value to test.
  201. * @param mixed $expected Value to test against.
  202. * @return boolean True if matched.
  203. * @access protected
  204. */
  205. function _testHeaderValue($value, $expected) {
  206. if ($expected === false) {
  207. return true;
  208. }
  209. if (SimpleExpectation::isExpectation($expected)) {
  210. return $expected->test(trim($value));
  211. }
  212. return (trim($value) == trim($expected));
  213. }
  214. /**
  215. * Returns a human readable test message.
  216. * @param mixed $compare Raw header block to search.
  217. * @return string Description of success
  218. * or failure.
  219. * @access public
  220. */
  221. function testMessage($compare) {
  222. if (SimpleExpectation::isExpectation($this->_expected_value)) {
  223. $message = $this->_expected_value->testMessage($compare);
  224. } else {
  225. $message = $this->_expected_header .
  226. ($this->_expected_value ? ': ' . $this->_expected_value : '');
  227. }
  228. if (is_string($line = $this->_findHeader($compare))) {
  229. return "Searching for header [$message] found [$line]";
  230. } else {
  231. return "Failed to find header [$message]";
  232. }
  233. }
  234. }
  235. /**
  236. * Test for a specific HTTP header within a header block that
  237. * should not be found.
  238. * @package SimpleTest
  239. * @subpackage WebTester
  240. */
  241. class NoHttpHeaderExpectation extends HttpHeaderExpectation {
  242. var $_expected_header;
  243. var $_expected_value;
  244. /**
  245. * Sets the field and value to compare against.
  246. * @param string $unwanted Case insenstive trimmed header name.
  247. */
  248. function NoHttpHeaderExpectation($unwanted) {
  249. $this->HttpHeaderExpectation($unwanted);
  250. }
  251. /**
  252. * Tests that the unwanted header is not found.
  253. * @param mixed $compare Raw header block to search.
  254. * @return boolean True if header present.
  255. * @access public
  256. */
  257. function test($compare) {
  258. return ($this->_findHeader($compare) === false);
  259. }
  260. /**
  261. * Returns a human readable test message.
  262. * @param mixed $compare Raw header block to search.
  263. * @return string Description of success
  264. * or failure.
  265. * @access public
  266. */
  267. function testMessage($compare) {
  268. $expectation = $this->_getExpectation();
  269. if (is_string($line = $this->_findHeader($compare))) {
  270. return "Found unwanted header [$expectation] with [$line]";
  271. } else {
  272. return "Did not find unwanted header [$expectation]";
  273. }
  274. }
  275. }
  276. /**
  277. * Test for a text substring.
  278. * @package SimpleTest
  279. * @subpackage UnitTester
  280. */
  281. class TextExpectation extends SimpleExpectation {
  282. var $_substring;
  283. /**
  284. * Sets the value to compare against.
  285. * @param string $substring Text to search for.
  286. * @param string $message Customised message on failure.
  287. * @access public
  288. */
  289. function TextExpectation($substring, $message = '%s') {
  290. $this->SimpleExpectation($message);
  291. $this->_substring = $substring;
  292. }
  293. /**
  294. * Accessor for the substring.
  295. * @return string Text to match.
  296. * @access protected
  297. */
  298. function _getSubstring() {
  299. return $this->_substring;
  300. }
  301. /**
  302. * Tests the expectation. True if the text contains the
  303. * substring.
  304. * @param string $compare Comparison value.
  305. * @return boolean True if correct.
  306. * @access public
  307. */
  308. function test($compare) {
  309. return (strpos($compare, $this->_substring) !== false);
  310. }
  311. /**
  312. * Returns a human readable test message.
  313. * @param mixed $compare Comparison value.
  314. * @return string Description of success
  315. * or failure.
  316. * @access public
  317. */
  318. function testMessage($compare) {
  319. if ($this->test($compare)) {
  320. return $this->_describeTextMatch($this->_getSubstring(), $compare);
  321. } else {
  322. $dumper = &$this->_getDumper();
  323. return "Text [" . $this->_getSubstring() .
  324. "] not detected in [" .
  325. $dumper->describeValue($compare) . "]";
  326. }
  327. }
  328. /**
  329. * Describes a pattern match including the string
  330. * found and it's position.
  331. * @param string $substring Text to search for.
  332. * @param string $subject Subject to search.
  333. * @access protected
  334. */
  335. function _describeTextMatch($substring, $subject) {
  336. $position = strpos($subject, $substring);
  337. $dumper = &$this->_getDumper();
  338. return "Text [$substring] detected at character [$position] in [" .
  339. $dumper->describeValue($subject) . "] in region [" .
  340. $dumper->clipString($subject, 100, $position) . "]";
  341. }
  342. }
  343. /**
  344. * Fail if a substring is detected within the
  345. * comparison text.
  346. * @package SimpleTest
  347. * @subpackage UnitTester
  348. */
  349. class NoTextExpectation extends TextExpectation {
  350. /**
  351. * Sets the reject pattern
  352. * @param string $substring Text to search for.
  353. * @param string $message Customised message on failure.
  354. * @access public
  355. */
  356. function NoTextExpectation($substring, $message = '%s') {
  357. $this->TextExpectation($substring, $message);
  358. }
  359. /**
  360. * Tests the expectation. False if the substring appears
  361. * in the text.
  362. * @param string $compare Comparison value.
  363. * @return boolean True if correct.
  364. * @access public
  365. */
  366. function test($compare) {
  367. return ! parent::test($compare);
  368. }
  369. /**
  370. * Returns a human readable test message.
  371. * @param string $compare Comparison value.
  372. * @return string Description of success
  373. * or failure.
  374. * @access public
  375. */
  376. function testMessage($compare) {
  377. if ($this->test($compare)) {
  378. $dumper = &$this->_getDumper();
  379. return "Text [" . $this->_getSubstring() .
  380. "] not detected in [" .
  381. $dumper->describeValue($compare) . "]";
  382. } else {
  383. return $this->_describeTextMatch($this->_getSubstring(), $compare);
  384. }
  385. }
  386. }
  387. /**
  388. * Test case for testing of web pages. Allows
  389. * fetching of pages, parsing of HTML and
  390. * submitting forms.
  391. * @package SimpleTest
  392. * @subpackage WebTester
  393. */
  394. class WebTestCase extends SimpleTestCase {
  395. var $_browser;
  396. /**
  397. * Creates an empty test case. Should be subclassed
  398. * with test methods for a functional test case.
  399. * @param string $label Name of test case. Will use
  400. * the class name if none specified.
  401. * @access public
  402. */
  403. function WebTestCase($label = false) {
  404. $this->SimpleTestCase($label);
  405. }
  406. /**
  407. * Announces the start of the test.
  408. * @param string $method Test method just started.
  409. * @access public
  410. */
  411. function before($method) {
  412. parent::before($method);
  413. $this->setBrowser($this->createBrowser());
  414. }
  415. /**
  416. * Announces the end of the test. Includes private clean up.
  417. * @param string $method Test method just finished.
  418. * @access public
  419. */
  420. function after($method) {
  421. $this->unsetBrowser();
  422. parent::after($method);
  423. }
  424. /**
  425. * Gets a current browser reference for setting
  426. * special expectations or for detailed
  427. * examination of page fetches.
  428. * @return SimpleBrowser Current test browser object.
  429. * @access public
  430. */
  431. function &getBrowser() {
  432. return $this->_browser;
  433. }
  434. /**
  435. * Gets a current browser reference for setting
  436. * special expectations or for detailed
  437. * examination of page fetches.
  438. * @param SimpleBrowser $browser New test browser object.
  439. * @access public
  440. */
  441. function setBrowser(&$browser) {
  442. return $this->_browser = &$browser;
  443. }
  444. /**
  445. * Clears the current browser reference to help the
  446. * PHP garbage collector.
  447. * @access public
  448. */
  449. function unsetBrowser() {
  450. unset($this->_browser);
  451. }
  452. /**
  453. * Creates a new default web browser object.
  454. * Will be cleared at the end of the test method.
  455. * @return TestBrowser New browser.
  456. * @access public
  457. */
  458. function &createBrowser() {
  459. $browser = &new SimpleBrowser();
  460. return $browser;
  461. }
  462. /**
  463. * Gets the last response error.
  464. * @return string Last low level HTTP error.
  465. * @access public
  466. */
  467. function getTransportError() {
  468. return $this->_browser->getTransportError();
  469. }
  470. /**
  471. * Accessor for the currently selected URL.
  472. * @return string Current location or false if
  473. * no page yet fetched.
  474. * @access public
  475. */
  476. function getUrl() {
  477. return $this->_browser->getUrl();
  478. }
  479. /**
  480. * Dumps the current request for debugging.
  481. * @access public
  482. */
  483. function showRequest() {
  484. $this->dump($this->_browser->getRequest());
  485. }
  486. /**
  487. * Dumps the current HTTP headers for debugging.
  488. * @access public
  489. */
  490. function showHeaders() {
  491. $this->dump($this->_browser->getHeaders());
  492. }
  493. /**
  494. * Dumps the current HTML source for debugging.
  495. * @access public
  496. */
  497. function showSource() {
  498. $this->dump($this->_browser->getContent());
  499. }
  500. /**
  501. * Dumps the visible text only for debugging.
  502. * @access public
  503. */
  504. function showText() {
  505. $this->dump(wordwrap($this->_browser->getContentAsText(), 80));
  506. }
  507. /**
  508. * Simulates the closing and reopening of the browser.
  509. * Temporary cookies will be discarded and timed
  510. * cookies will be expired if later than the
  511. * specified time.
  512. * @param string/integer $date Time when session restarted.
  513. * If ommitted then all persistent
  514. * cookies are kept. Time is either
  515. * Cookie format string or timestamp.
  516. * @access public
  517. */
  518. function restart($date = false) {
  519. if ($date === false) {
  520. $date = time();
  521. }
  522. $this->_browser->restart($date);
  523. }
  524. /**
  525. * Moves cookie expiry times back into the past.
  526. * Useful for testing timeouts and expiries.
  527. * @param integer $interval Amount to age in seconds.
  528. * @access public
  529. */
  530. function ageCookies($interval) {
  531. $this->_browser->ageCookies($interval);
  532. }
  533. /**
  534. * Disables frames support. Frames will not be fetched
  535. * and the frameset page will be used instead.
  536. * @access public
  537. */
  538. function ignoreFrames() {
  539. $this->_browser->ignoreFrames();
  540. }
  541. /**
  542. * Adds a header to every fetch.
  543. * @param string $header Header line to add to every
  544. * request until cleared.
  545. * @access public
  546. */
  547. function addHeader($header) {
  548. $this->_browser->addHeader($header);
  549. }
  550. /**
  551. * Sets the maximum number of redirects before
  552. * the web page is loaded regardless.
  553. * @param integer $max Maximum hops.
  554. * @access public
  555. */
  556. function setMaximumRedirects($max) {
  557. if (! $this->_browser) {
  558. trigger_error(
  559. 'Can only set maximum redirects in a test method, setUp() or tearDown()');
  560. }
  561. $this->_browser->setMaximumRedirects($max);
  562. }
  563. /**
  564. * Sets the socket timeout for opening a connection and
  565. * receiving at least one byte of information.
  566. * @param integer $timeout Maximum time in seconds.
  567. * @access public
  568. */
  569. function setConnectionTimeout($timeout) {
  570. $this->_browser->setConnectionTimeout($timeout);
  571. }
  572. /**
  573. * Sets proxy to use on all requests for when
  574. * testing from behind a firewall. Set URL
  575. * to false to disable.
  576. * @param string $proxy Proxy URL.
  577. * @param string $username Proxy username for authentication.
  578. * @param string $password Proxy password for authentication.
  579. * @access public
  580. */
  581. function useProxy($proxy, $username = false, $password = false) {
  582. $this->_browser->useProxy($proxy, $username, $password);
  583. }
  584. /**
  585. * Fetches a page into the page buffer. If
  586. * there is no base for the URL then the
  587. * current base URL is used. After the fetch
  588. * the base URL reflects the new location.
  589. * @param string $url URL to fetch.
  590. * @param hash $parameters Optional additional GET data.
  591. * @return boolean/string Raw page on success.
  592. * @access public
  593. */
  594. function get($url, $parameters = false) {
  595. return $this->_browser->get($url, $parameters);
  596. }
  597. /**
  598. * Fetches a page by POST into the page buffer.
  599. * If there is no base for the URL then the
  600. * current base URL is used. After the fetch
  601. * the base URL reflects the new location.
  602. * @param string $url URL to fetch.
  603. * @param hash $parameters Optional additional GET data.
  604. * @return boolean/string Raw page on success.
  605. * @access public
  606. */
  607. function post($url, $parameters = false) {
  608. return $this->_browser->post($url, $parameters);
  609. }
  610. /**
  611. * Does a HTTP HEAD fetch, fetching only the page
  612. * headers. The current base URL is unchanged by this.
  613. * @param string $url URL to fetch.
  614. * @param hash $parameters Optional additional GET data.
  615. * @return boolean True on success.
  616. * @access public
  617. */
  618. function head($url, $parameters = false) {
  619. return $this->_browser->head($url, $parameters);
  620. }
  621. /**
  622. * Equivalent to hitting the retry button on the
  623. * browser. Will attempt to repeat the page fetch.
  624. * @return boolean True if fetch succeeded.
  625. * @access public
  626. */
  627. function retry() {
  628. return $this->_browser->retry();
  629. }
  630. /**
  631. * Equivalent to hitting the back button on the
  632. * browser.
  633. * @return boolean True if history entry and
  634. * fetch succeeded.
  635. * @access public
  636. */
  637. function back() {
  638. return $this->_browser->back();
  639. }
  640. /**
  641. * Equivalent to hitting the forward button on the
  642. * browser.
  643. * @return boolean True if history entry and
  644. * fetch succeeded.
  645. * @access public
  646. */
  647. function forward() {
  648. return $this->_browser->forward();
  649. }
  650. /**
  651. * Retries a request after setting the authentication
  652. * for the current realm.
  653. * @param string $username Username for realm.
  654. * @param string $password Password for realm.
  655. * @return boolean True if successful fetch. Note
  656. * that authentication may still have
  657. * failed.
  658. * @access public
  659. */
  660. function authenticate($username, $password) {
  661. return $this->_browser->authenticate($username, $password);
  662. }
  663. /**
  664. * Gets the cookie value for the current browser context.
  665. * @param string $name Name of cookie.
  666. * @return string Value of cookie or false if unset.
  667. * @access public
  668. */
  669. function getCookie($name) {
  670. return $this->_browser->getCurrentCookieValue($name);
  671. }
  672. /**
  673. * Sets a cookie in the current browser.
  674. * @param string $name Name of cookie.
  675. * @param string $value Cookie value.
  676. * @param string $host Host upon which the cookie is valid.
  677. * @param string $path Cookie path if not host wide.
  678. * @param string $expiry Expiry date.
  679. * @access public
  680. */
  681. function setCookie($name, $value, $host = false, $path = "/", $expiry = false) {
  682. $this->_browser->setCookie($name, $value, $host, $path, $expiry);
  683. }
  684. /**
  685. * Accessor for current frame focus. Will be
  686. * false if no frame has focus.
  687. * @return integer/string/boolean Label if any, otherwise
  688. * the position in the frameset
  689. * or false if none.
  690. * @access public
  691. */
  692. function getFrameFocus() {
  693. return $this->_browser->getFrameFocus();
  694. }
  695. /**
  696. * Sets the focus by index. The integer index starts from 1.
  697. * @param integer $choice Chosen frame.
  698. * @return boolean True if frame exists.
  699. * @access public
  700. */
  701. function setFrameFocusByIndex($choice) {
  702. return $this->_browser->setFrameFocusByIndex($choice);
  703. }
  704. /**
  705. * Sets the focus by name.
  706. * @param string $name Chosen frame.
  707. * @return boolean True if frame exists.
  708. * @access public
  709. */
  710. function setFrameFocus($name) {
  711. return $this->_browser->setFrameFocus($name);
  712. }
  713. /**
  714. * Clears the frame focus. All frames will be searched
  715. * for content.
  716. * @access public
  717. */
  718. function clearFrameFocus() {
  719. return $this->_browser->clearFrameFocus();
  720. }
  721. /**
  722. * Clicks a visible text item. Will first try buttons,
  723. * then links and then images.
  724. * @param string $label Visible text or alt text.
  725. * @return string/boolean Raw page or false.
  726. * @access public
  727. */
  728. function click($label) {
  729. return $this->_browser->click($label);
  730. }
  731. /**
  732. * Clicks the submit button by label. The owning
  733. * form will be submitted by this.
  734. * @param string $label Button label. An unlabeled
  735. * button can be triggered by 'Submit'.
  736. * @param hash $additional Additional form values.
  737. * @return boolean/string Page on success, else false.
  738. * @access public
  739. */
  740. function clickSubmit($label = 'Submit', $additional = false) {
  741. return $this->_browser->clickSubmit($label, $additional);
  742. }
  743. /**
  744. * Clicks the submit button by name attribute. The owning
  745. * form will be submitted by this.
  746. * @param string $name Name attribute of button.
  747. * @param hash $additional Additional form values.
  748. * @return boolean/string Page on success.
  749. * @access public
  750. */
  751. function clickSubmitByName($name, $additional = false) {
  752. return $this->_browser->clickSubmitByName($name, $additional);
  753. }
  754. /**
  755. * Clicks the submit button by ID attribute. The owning
  756. * form will be submitted by this.
  757. * @param string $id ID attribute of button.
  758. * @param hash $additional Additional form values.
  759. * @return boolean/string Page on success.
  760. * @access public
  761. */
  762. function clickSubmitById($id, $additional = false) {
  763. return $this->_browser->clickSubmitById($id, $additional);
  764. }
  765. /**
  766. * Clicks the submit image by some kind of label. Usually
  767. * the alt tag or the nearest equivalent. The owning
  768. * form will be submitted by this. Clicking outside of
  769. * the boundary of the coordinates will result in
  770. * a failure.
  771. * @param string $label Alt attribute of button.
  772. * @param integer $x X-coordinate of imaginary click.
  773. * @param integer $y Y-coordinate of imaginary click.
  774. * @param hash $additional Additional form values.
  775. * @return boolean/string Page on success.
  776. * @access public
  777. */
  778. function clickImage($label, $x = 1, $y = 1, $additional = false) {
  779. return $this->_browser->clickImage($label, $x, $y, $additional);
  780. }
  781. /**
  782. * Clicks the submit image by the name. Usually
  783. * the alt tag or the nearest equivalent. The owning
  784. * form will be submitted by this. Clicking outside of
  785. * the boundary of the coordinates will result in
  786. * a failure.
  787. * @param string $name Name attribute of button.
  788. * @param integer $x X-coordinate of imaginary click.
  789. * @param integer $y Y-coordinate of imaginary click.
  790. * @param hash $additional Additional form values.
  791. * @return boolean/string Page on success.
  792. * @access public
  793. */
  794. function clickImageByName($name, $x = 1, $y = 1, $additional = false) {
  795. return $this->_browser->clickImageByName($name, $x, $y, $additional);
  796. }
  797. /**
  798. * Clicks the submit image by ID attribute. The owning
  799. * form will be submitted by this. Clicking outside of
  800. * the boundary of the coordinates will result in
  801. * a failure.
  802. * @param integer/string $id ID attribute of button.
  803. * @param integer $x X-coordinate of imaginary click.
  804. * @param integer $y Y-coordinate of imaginary click.
  805. * @param hash $additional Additional form values.
  806. * @return boolean/string Page on success.
  807. * @access public
  808. */
  809. function clickImageById($id, $x = 1, $y = 1, $additional = false) {
  810. return $this->_browser->clickImageById($id, $x, $y, $additional);
  811. }
  812. /**
  813. * Submits a form by the ID.
  814. * @param string $id Form ID. No button information
  815. * is submitted this way.
  816. * @return boolean/string Page on success.
  817. * @access public
  818. */
  819. function submitFormById($id) {
  820. return $this->_browser->submitFormById($id);
  821. }
  822. /**
  823. * Follows a link by name. Will click the first link
  824. * found with this link text by default, or a later
  825. * one if an index is given. Match is case insensitive
  826. * with normalised space.
  827. * @param string $label Text between the anchor tags.
  828. * @param integer $index Link position counting from zero.
  829. * @return boolean/string Page on success.
  830. * @access public
  831. */
  832. function clickLink($label, $index = 0) {
  833. return $this->_browser->clickLink($label, $index);
  834. }
  835. /**
  836. * Follows a link by id attribute.
  837. * @param string $id ID attribute value.
  838. * @return boolean/string Page on success.
  839. * @access public
  840. */
  841. function clickLinkById($id) {
  842. return $this->_browser->clickLinkById($id);
  843. }
  844. /**
  845. * Will trigger a pass if the two parameters have
  846. * the same value only. Otherwise a fail. This
  847. * is for testing hand extracted text, etc.
  848. * @param mixed $first Value to compare.
  849. * @param mixed $second Value to compare.
  850. * @param string $message Message to display.
  851. * @return boolean True on pass
  852. * @access public
  853. */
  854. function assertEqual($first, $second, $message = "%s") {
  855. return $this->assert(
  856. new EqualExpectation($first),
  857. $second,
  858. $message);
  859. }
  860. /**
  861. * Will trigger a pass if the two parameters have
  862. * a different value. Otherwise a fail. This
  863. * is for testing hand extracted text, etc.
  864. * @param mixed $first Value to compare.
  865. * @param mixed $second Value to compare.
  866. * @param string $message Message to display.
  867. * @return boolean True on pass
  868. * @access public
  869. */
  870. function assertNotEqual($first, $second, $message = "%s") {
  871. return $this->assert(
  872. new NotEqualExpectation($first),
  873. $second,
  874. $message);
  875. }
  876. /**
  877. * Tests for the presence of a link label. Match is
  878. * case insensitive with normalised space.
  879. * @param string $label Text between the anchor tags.
  880. * @param string $message Message to display. Default
  881. * can be embedded with %s.
  882. * @return boolean True if link present.
  883. * @access public
  884. */
  885. function assertLink($label, $message = "%s") {
  886. return $this->assertTrue(
  887. $this->_browser->isLink($label),
  888. sprintf($message, "Link [$label] should exist"));
  889. }
  890. /**
  891. * Tests for the non-presence of a link label. Match is
  892. * case insensitive with normalised space.
  893. * @param string/integer $label Text between the anchor tags
  894. * or ID attribute.
  895. * @param string $message Message to display. Default
  896. * can be embedded with %s.
  897. * @return boolean True if link missing.
  898. * @access public
  899. */
  900. function assertNoLink($label, $message = "%s") {
  901. return $this->assertFalse(
  902. $this->_browser->isLink($label),
  903. sprintf($message, "Link [$label] should not exist"));
  904. }
  905. /**
  906. * Tests for the presence of a link id attribute.
  907. * @param string $id Id attribute value.
  908. * @param string $message Message to display. Default
  909. * can be embedded with %s.
  910. * @return boolean True if link present.
  911. * @access public
  912. */
  913. function assertLinkById($id, $message = "%s") {
  914. return $this->assertTrue(
  915. $this->_browser->isLinkById($id),
  916. sprintf($message, "Link ID [$id] should exist"));
  917. }
  918. /**
  919. * Tests for the non-presence of a link label. Match is
  920. * case insensitive with normalised space.
  921. * @param string $id Id attribute value.
  922. * @param string $message Message to display. Default
  923. * can be embedded with %s.
  924. * @return boolean True if link missing.
  925. * @access public
  926. */
  927. function assertNoLinkById($id, $message = "%s") {
  928. return $this->assertFalse(
  929. $this->_browser->isLinkById($id),
  930. sprintf($message, "Link ID [$id] should not exist"));
  931. }
  932. /**
  933. * Sets all form fields with that label, or name if there
  934. * is no label attached.
  935. * @param string $name Name of field in forms.
  936. * @param string $value New value of field.
  937. * @return boolean True if field exists, otherwise false.
  938. * @access public
  939. */
  940. function setField($label, $value) {
  941. return $this->_browser->setField($label, $value);
  942. }
  943. /**
  944. * Sets all form fields with that name.
  945. * @param string $name Name of field in forms.
  946. * @param string $value New value of field.
  947. * @return boolean True if field exists, otherwise false.
  948. * @access public
  949. */
  950. function setFieldByName($name, $value) {
  951. return $this->_browser->setFieldByName($name, $value);
  952. }
  953. /**
  954. * Sets all form fields with that name.
  955. * @param string/integer $id Id of field in forms.
  956. * @param string $value New value of field.
  957. * @return boolean True if field exists, otherwise false.
  958. * @access public
  959. */
  960. function setFieldById($id, $value) {
  961. return $this->_browser->setFieldById($id, $value);
  962. }
  963. /**
  964. * Confirms that the form element is currently set
  965. * to the expected value. A missing form will always
  966. * fail. If no value is given then only the existence
  967. * of the field is checked.
  968. * @param string $name Name of field in forms.
  969. * @param mixed $expected Expected string/array value or
  970. * false for unset fields.
  971. * @param string $message Message to display. Default
  972. * can be embedded with %s.
  973. * @return boolean True if pass.
  974. * @access public
  975. */
  976. function assertField($label, $expected = true, $message = '%s') {
  977. $value = $this->_browser->getField($label);
  978. return $this->_assertFieldValue($label, $value, $expected, $message);
  979. }
  980. /**
  981. * Confirms that the form element is currently set
  982. * to the expected value. A missing form element will always
  983. * fail. If no value is given then only the existence
  984. * of the field is checked.
  985. * @param string $name Name of field in forms.
  986. * @param mixed $expected Expected string/array value or
  987. * false for unset fields.
  988. * @param string $message Message to display. Default
  989. * can be embedded with %s.
  990. * @return boolean True if pass.
  991. * @access public
  992. */
  993. function assertFieldByName($name, $expected = true, $message = '%s') {
  994. $value = $this->_browser->getFieldByName($name);
  995. return $this->_assertFieldValue($name, $value, $expected, $message);
  996. }
  997. /**
  998. * Confirms that the form element is currently set
  999. * to the expected value. A missing form will always
  1000. * fail. If no ID is given then only the existence
  1001. * of the field is checked.
  1002. * @param string/integer $id Name of field in forms.
  1003. * @param mixed $expected Expected string/array value or
  1004. * false for unset fields.
  1005. * @param string $message Message to display. Default
  1006. * can be embedded with %s.
  1007. * @return boolean True if pass.
  1008. * @access public
  1009. */
  1010. function assertFieldById($id, $expected = true, $message = '%s') {
  1011. $value = $this->_browser->getFieldById($id);
  1012. return $this->_assertFieldValue($id, $value, $expected, $message);
  1013. }
  1014. /**
  1015. * Tests the field value against the expectation.
  1016. * @param string $identifier Name, ID or label.
  1017. * @param mixed $value Current field value.
  1018. * @param mixed $expected Expected value to match.
  1019. * @param string $message Failure message.
  1020. * @return boolean True if pass
  1021. * @access protected
  1022. */
  1023. function _assertFieldValue($identifier, $value, $expected, $message) {
  1024. if ($expected === true) {
  1025. return $this->assertTrue(
  1026. isset($value),
  1027. sprintf($message, "Field [$identifier] should exist"));
  1028. }
  1029. if (! SimpleExpectation::isExpectation($expected)) {
  1030. $identifier = str_replace('%', '%%', $identifier);
  1031. $expected = new FieldExpectation(
  1032. $expected,
  1033. "Field [$identifier] should match with [%s]");
  1034. }
  1035. return $this->assert($expected, $value, $message);
  1036. }
  1037. /**
  1038. * Checks the response code against a list
  1039. * of possible values.
  1040. * @param array $responses Possible responses for a pass.
  1041. * @param string $message Message to display. Default
  1042. * can be embedded with %s.
  1043. * @return boolean True if pass.
  1044. * @access public
  1045. */
  1046. function assertResponse($responses, $message = '%s') {
  1047. $responses = (is_array($responses) ? $responses : array($responses));
  1048. $code = $this->_browser->getResponseCode();
  1049. $message = sprintf($message, "Expecting response in [" .
  1050. implode(", ", $responses) . "] got [$code]");
  1051. return $this->assertTrue(in_array($code, $responses), $message);
  1052. }
  1053. /**
  1054. * Checks the mime type against a list
  1055. * of possible values.
  1056. * @param array $types Possible mime types for a pass.
  1057. * @param string $message Message to display.
  1058. * @return boolean True if pass.
  1059. * @access public
  1060. */
  1061. function assertMime($types, $message = '%s') {
  1062. $types = (is_array($types) ? $types : array($types));
  1063. $type = $this->_browser->getMimeType();
  1064. $message = sprintf($message, "Expecting mime type in [" .
  1065. implode(", ", $types) . "] got [$type]");
  1066. return $this->assertTrue(in_array($type, $types), $message);
  1067. }
  1068. /**
  1069. * Attempt to match the authentication type within
  1070. * the security realm we are currently matching.
  1071. * @param string $authentication Usually basic.
  1072. * @param string $message Message to display.
  1073. * @return boolean True if pass.
  1074. * @access public
  1075. */
  1076. function assertAuthentication($authentication = false, $message = '%s') {
  1077. if (! $authentication) {
  1078. $message = sprintf($message, "Expected any authentication type, got [" .
  1079. $this->_browser->getAuthentication() . "]");
  1080. return $this->assertTrue(
  1081. $this->_browser->getAuthentication(),
  1082. $message);
  1083. } else {
  1084. $message = sprintf($message, "Expected authentication [$authentication] got [" .
  1085. $this->_browser->getAuthentication() . "]");
  1086. return $this->assertTrue(
  1087. strtolower($this->_browser->getAuthentication()) == strtolower($authentication),
  1088. $message);
  1089. }
  1090. }
  1091. /**
  1092. * Checks that no authentication is necessary to view
  1093. * the desired page.
  1094. * @param string $message Message to display.
  1095. * @return boolean True if pass.
  1096. * @access public
  1097. */
  1098. function assertNoAuthentication($message = '%s') {
  1099. $message = sprintf($message, "Expected no authentication type, got [" .
  1100. $this->_browser->getAuthentication() . "]");
  1101. return $this->assertFalse($this->_browser->getAuthentication(), $message);
  1102. }
  1103. /**
  1104. * Attempts to match the current security realm.
  1105. * @param string $realm Name of security realm.
  1106. * @param string $message Message to display.
  1107. * @return boolean True if pass.
  1108. * @access public
  1109. */
  1110. function assertRealm($realm, $message = '%s') {
  1111. if (! SimpleExpectation::isExpectation($realm)) {
  1112. $realm = new EqualExpectation($realm);
  1113. }
  1114. return $this->assert(
  1115. $realm,
  1116. $this->_browser->getRealm(),
  1117. "Expected realm -> $message");
  1118. }
  1119. /**
  1120. * Checks each header line for the required value. If no
  1121. * value is given then only an existence check is made.
  1122. * @param string $header Case insensitive header name.
  1123. * @param mixed $value Case sensitive trimmed string to
  1124. * match against. An expectation object
  1125. * can be used for pattern matching.
  1126. * @return boolean True if pass.
  1127. * @access public
  1128. */
  1129. function assertHeader($header, $value = false, $message = '%s') {
  1130. return $this->assert(
  1131. new HttpHeaderExpectation($header, $value),
  1132. $this->_browser->getHeaders(),
  1133. $message);
  1134. }
  1135. /**
  1136. * @deprecated
  1137. */
  1138. function assertHeaderPattern($header, $pattern, $message = '%s') {
  1139. return $this->assert(
  1140. new HttpHeaderExpectation($header, new PatternExpectation($pattern)),
  1141. $this->_browser->getHeaders(),
  1142. $message);
  1143. }
  1144. /**
  1145. * Confirms that the header type has not been received.
  1146. * Only the landing page is checked. If you want to check
  1147. * redirect pages, then you should limit redirects so
  1148. * as to capture the page you want.
  1149. * @param string $header Case insensitive header name.
  1150. * @return boolean True if pass.
  1151. * @access public
  1152. */
  1153. function assertNoHeader($header, $message = '%s') {
  1154. return $this->assert(
  1155. new NoHttpHeaderExpectation($header),
  1156. $this->_browser->getHeaders(),
  1157. $message);
  1158. }
  1159. /**
  1160. * @deprecated
  1161. */
  1162. function assertNoUnwantedHeader($header, $message = '%s') {
  1163. return $this->assertNoHeader($header, $message);
  1164. }
  1165. /**
  1166. * Tests the text between the title tags.
  1167. * @param string $title Expected title.
  1168. * @param string $message Message to display.
  1169. * @return boolean True if pass.
  1170. * @access public
  1171. */
  1172. function assertTitle($title = false, $message = '%s') {
  1173. if (! SimpleExpectation::isExpectation($title)) {
  1174. $title = new EqualExpectation($title);
  1175. }
  1176. return $this->assert($title, $this->_browser->getTitle(), $message);
  1177. }
  1178. /**
  1179. * Will trigger a pass if the text is found in the plain
  1180. * text form of the page.
  1181. * @param string $text Text to look for.
  1182. * @param string $message Message to display.
  1183. * @return boolean True if pass.

Large files files are truncated, but you can click here to view the full file