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