PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/simpletest/web_tester.php

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

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