Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/web_tester.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/test_case.php');
00013 require_once(dirname(__FILE__) . '/browser.php');
00014 require_once(dirname(__FILE__) . '/page.php');
00015 require_once(dirname(__FILE__) . '/expectation.php');
00023 class FieldExpectation extends SimpleExpectation {
00024     var $_value;
00025     
00034     function FieldExpectation($value, $message = '%s') {
00035         $this->SimpleExpectation($message);
00036         if (is_array($value)) {
00037             sort($value);
00038         }
00039         $this->_value = $value;
00040     }
00041     
00050     function test($compare) {
00051         if ($this->_value === false) {
00052             return ($compare === false);
00053         }
00054         if ($this->_isSingle($this->_value)) {
00055             return $this->_testSingle($compare);
00056         }
00057         if (is_array($this->_value)) {
00058             return $this->_testMultiple($compare);
00059         }
00060         return false;
00061     }
00062     
00069     function _isSingle($value) {
00070         return is_string($value) || is_integer($value) || is_float($value);
00071     }
00072     
00079     function _testSingle($compare) {
00080         if (is_array($compare) && count($compare) == 1) {
00081             $compare = $compare[0];
00082         }
00083         if (! $this->_isSingle($compare)) {
00084             return false;
00085         }
00086         return ($this->_value == $compare);
00087     }
00088     
00095     function _testMultiple($compare) {
00096         if (is_string($compare)) {
00097             $compare = array($compare);
00098         }
00099         if (! is_array($compare)) {
00100             return false;
00101         }
00102         sort($compare);
00103         return ($this->_value === $compare);
00104     }
00105     
00113     function testMessage($compare) {
00114         $dumper = &$this->_getDumper();
00115         if (is_array($compare)) {
00116             sort($compare);
00117         }
00118         if ($this->test($compare)) {
00119             return "Field expectation [" . $dumper->describeValue($this->_value) . "]";
00120         } else {
00121             return "Field expectation [" . $dumper->describeValue($this->_value) .
00122                     "] fails with [" .
00123                     $dumper->describeValue($compare) . "] " .
00124                     $dumper->describeDifference($this->_value, $compare);
00125         }
00126     }
00127 }
00128 
00134 class HttpHeaderExpectation extends SimpleExpectation {
00135     var $_expected_header;
00136     var $_expected_value;
00137     
00148     function HttpHeaderExpectation($header, $value = false, $message = '%s') {
00149         $this->SimpleExpectation($message);
00150         $this->_expected_header = $this->_normaliseHeader($header);
00151         $this->_expected_value = $value;
00152     }
00153     
00159     function _getExpectation() {
00160         return $this->_expected_value;
00161     }
00162     
00170     function _normaliseHeader($header) {
00171         return strtolower(trim($header));
00172     }
00173     
00181     function test($compare) {
00182         return is_string($this->_findHeader($compare));
00183     }
00184     
00192     function _findHeader($compare) {
00193         $lines = explode("\r\n", $compare);
00194         foreach ($lines as $line) {
00195             if ($this->_testHeaderLine($line)) {
00196                 return $line;
00197             }
00198         }
00199         return false;
00200     }
00201     
00208     function _testHeaderLine($line) {
00209         if (count($parsed = explode(':', $line, 2)) < 2) {
00210             return false;
00211         }
00212         list($header, $value) = $parsed;
00213         if ($this->_normaliseHeader($header) != $this->_expected_header) {
00214             return false;
00215         }
00216         return $this->_testHeaderValue($value, $this->_expected_value);
00217     }
00218     
00226     function _testHeaderValue($value, $expected) {
00227         if ($expected === false) {
00228             return true;
00229         }
00230         if (SimpleExpectation::isExpectation($expected)) {
00231             return $expected->test(trim($value));
00232         }
00233         return (trim($value) == trim($expected));
00234     }
00235     
00243     function testMessage($compare) {
00244         if (SimpleExpectation::isExpectation($this->_expected_value)) {
00245             $message = $this->_expected_value->overlayMessage($compare, $this->_getDumper());
00246         } else {
00247             $message = $this->_expected_header .
00248                     ($this->_expected_value ? ': ' . $this->_expected_value : '');
00249         }
00250         if (is_string($line = $this->_findHeader($compare))) {
00251             return "Searching for header [$message] found [$line]";
00252         } else {
00253             return "Failed to find header [$message]";
00254         }
00255     }
00256 }
00257     
00264 class NoHttpHeaderExpectation extends HttpHeaderExpectation {
00265     var $_expected_header;
00266     var $_expected_value;
00267     
00274     function NoHttpHeaderExpectation($unwanted, $message = '%s') {
00275         $this->HttpHeaderExpectation($unwanted, false, $message);
00276     }
00277     
00284     function test($compare) {
00285         return ($this->_findHeader($compare) === false);
00286     }
00287     
00295     function testMessage($compare) {
00296         $expectation = $this->_getExpectation();
00297         if (is_string($line = $this->_findHeader($compare))) {
00298             return "Found unwanted header [$expectation] with [$line]";
00299         } else {
00300             return "Did not find unwanted header [$expectation]";
00301         }
00302     }
00303 }
00304 
00310 class TextExpectation extends SimpleExpectation {
00311     var $_substring;
00312     
00319     function TextExpectation($substring, $message = '%s') {
00320         $this->SimpleExpectation($message);
00321         $this->_substring = $substring;
00322     }
00323     
00329     function _getSubstring() {
00330         return $this->_substring;
00331     }
00332     
00340     function test($compare) {
00341         return (strpos($compare, $this->_substring) !== false);
00342     }
00343     
00351     function testMessage($compare) {
00352         if ($this->test($compare)) {
00353             return $this->_describeTextMatch($this->_getSubstring(), $compare);
00354         } else {
00355             $dumper = &$this->_getDumper();
00356             return "Text [" . $this->_getSubstring() .
00357                     "] not detected in [" .
00358                     $dumper->describeValue($compare) . "]";
00359         }
00360     }
00361     
00369     function _describeTextMatch($substring, $subject) {
00370         $position = strpos($subject, $substring);
00371         $dumper = &$this->_getDumper();
00372         return "Text [$substring] detected at character [$position] in [" .
00373                 $dumper->describeValue($subject) . "] in region [" .
00374                 $dumper->clipString($subject, 100, $position) . "]";
00375     }
00376 }
00377 
00384 class NoTextExpectation extends TextExpectation {
00385     
00392     function NoTextExpectation($substring, $message = '%s') {
00393         $this->TextExpectation($substring, $message);
00394     }
00395     
00403     function test($compare) {
00404         return ! parent::test($compare);
00405     }
00406     
00414     function testMessage($compare) {
00415         if ($this->test($compare)) {
00416             $dumper = &$this->_getDumper();
00417             return "Text [" . $this->_getSubstring() .
00418                     "] not detected in [" .
00419                     $dumper->describeValue($compare) . "]";
00420         } else {
00421             return $this->_describeTextMatch($this->_getSubstring(), $compare);
00422         }
00423     }
00424 }
00425 
00433 class WebTestCase extends SimpleTestCase {
00434     var $_browser;
00435     var $_ignore_errors = false;
00436     
00444     function WebTestCase($label = false) {
00445         $this->SimpleTestCase($label);
00446     }
00447     
00453     function before($method) {
00454         parent::before($method);
00455         $this->setBrowser($this->createBrowser());
00456     }
00457 
00463     function after($method) {
00464         $this->unsetBrowser();
00465         parent::after($method);
00466     }
00467     
00475     function &getBrowser() {
00476         return $this->_browser;
00477     }
00478     
00486     function setBrowser(&$browser) {
00487         return $this->_browser = &$browser;
00488     }
00489         
00495     function unsetBrowser() {
00496         unset($this->_browser);
00497     }
00498     
00505     function &createBrowser() {
00506         $browser = new SimpleBrowser();
00507         return $browser;
00508     }
00509     
00515     function getTransportError() {
00516         return $this->_browser->getTransportError();
00517     }
00518         
00525     function getUrl() {
00526         return $this->_browser->getUrl();
00527     }
00528     
00533     function showRequest() {
00534         $this->dump($this->_browser->getRequest());
00535     }
00536     
00541     function showHeaders() {
00542         $this->dump($this->_browser->getHeaders());
00543     }
00544     
00549     function showSource() {
00550         $this->dump($this->_browser->getContent());
00551     }
00552     
00557     function showText() {
00558         $this->dump(wordwrap($this->_browser->getContentAsText(), 80));
00559     }
00560     
00572     function restart($date = false) {
00573         if ($date === false) {
00574             $date = time();
00575         }
00576         $this->_browser->restart($date);
00577     }
00578     
00585     function ageCookies($interval) {
00586         $this->_browser->ageCookies($interval);
00587     }
00588     
00594     function ignoreFrames() {
00595         $this->_browser->ignoreFrames();
00596     }
00597     
00602     function ignoreCookies() {
00603         $this->_browser->ignoreCookies();
00604     }
00605     
00612     function ignoreErrors() {
00613         $this->_ignore_errors = true;
00614     }
00615     
00624     function _failOnError($result) {
00625         if (! $this->_ignore_errors) {
00626             if ($error = $this->_browser->getTransportError()) {
00627                 $this->fail($error);
00628             }
00629         }
00630         $this->_ignore_errors = false;
00631         return $result;
00632     }
00633 
00640     function addHeader($header) {
00641         $this->_browser->addHeader($header);
00642     }
00643     
00650     function setMaximumRedirects($max) {
00651         if (! $this->_browser) {
00652             trigger_error(
00653                     'Can only set maximum redirects in a test method, setUp() or tearDown()');
00654         }
00655         $this->_browser->setMaximumRedirects($max);
00656     }
00657     
00664     function setConnectionTimeout($timeout) {
00665         $this->_browser->setConnectionTimeout($timeout);
00666     }
00667     
00677     function useProxy($proxy, $username = false, $password = false) {
00678         $this->_browser->useProxy($proxy, $username, $password);
00679     }
00680     
00691     function get($url, $parameters = false) {
00692         return $this->_failOnError($this->_browser->get($url, $parameters));
00693     }
00694     
00705     function post($url, $parameters = false) {
00706         return $this->_failOnError($this->_browser->post($url, $parameters));
00707     }
00708     
00717     function head($url, $parameters = false) {
00718         return $this->_failOnError($this->_browser->head($url, $parameters));
00719     }
00720     
00727     function retry() {
00728         return $this->_failOnError($this->_browser->retry());
00729     }
00730     
00738     function back() {
00739         return $this->_failOnError($this->_browser->back());
00740     }
00741     
00749     function forward() {
00750         return $this->_failOnError($this->_browser->forward());
00751     }
00752     
00763     function authenticate($username, $password) {
00764         return $this->_failOnError(
00765                 $this->_browser->authenticate($username, $password));
00766     }
00767     
00774     function getCookie($name) {
00775         return $this->_browser->getCurrentCookieValue($name);
00776     }
00777     
00787     function setCookie($name, $value, $host = false, $path = '/', $expiry = false) {
00788         $this->_browser->setCookie($name, $value, $host, $path, $expiry);
00789     }
00790     
00799     function getFrameFocus() {
00800         return $this->_browser->getFrameFocus();
00801     }
00802     
00809     function setFrameFocusByIndex($choice) {
00810         return $this->_browser->setFrameFocusByIndex($choice);
00811     }
00812     
00819     function setFrameFocus($name) {
00820         return $this->_browser->setFrameFocus($name);
00821     }
00822     
00828     function clearFrameFocus() {
00829         return $this->_browser->clearFrameFocus();
00830     }
00831     
00839     function click($label) {
00840         return $this->_failOnError($this->_browser->click($label));
00841     }
00842     
00849     function assertClickable($label, $message = '%s') {
00850         return $this->assertTrue(
00851                 $this->_browser->isClickable($label),
00852                 sprintf($message, "Click target [$label] should exist"));
00853     }
00854     
00864     function clickSubmit($label = 'Submit', $additional = false) {
00865         return $this->_failOnError(
00866                 $this->_browser->clickSubmit($label, $additional));
00867     }
00868     
00877     function clickSubmitByName($name, $additional = false) {
00878         return $this->_failOnError(
00879                 $this->_browser->clickSubmitByName($name, $additional));
00880     }
00881     
00890     function clickSubmitById($id, $additional = false) {
00891         return $this->_failOnError(
00892                 $this->_browser->clickSubmitById($id, $additional));
00893     }
00894     
00901     function assertSubmit($label, $message = '%s') {
00902         return $this->assertTrue(
00903                 $this->_browser->isSubmit($label),
00904                 sprintf($message, "Submit button [$label] should exist"));
00905     }
00906     
00920     function clickImage($label, $x = 1, $y = 1, $additional = false) {
00921         return $this->_failOnError(
00922                 $this->_browser->clickImage($label, $x, $y, $additional));
00923     }
00924     
00938     function clickImageByName($name, $x = 1, $y = 1, $additional = false) {
00939         return $this->_failOnError(
00940                 $this->_browser->clickImageByName($name, $x, $y, $additional));
00941     }
00942     
00955     function clickImageById($id, $x = 1, $y = 1, $additional = false) {
00956         return $this->_failOnError(
00957                 $this->_browser->clickImageById($id, $x, $y, $additional));
00958     }
00959     
00966     function assertImage($label, $message = '%s') {
00967         return $this->assertTrue(
00968                 $this->_browser->isImage($label),
00969                 sprintf($message, "Image with text [$label] should exist"));
00970     }
00971     
00979     function submitFormById($id) {
00980         return $this->_failOnError($this->_browser->submitFormById($id));
00981     }
00982     
00993     function clickLink($label, $index = 0) {
00994         return $this->_failOnError($this->_browser->clickLink($label, $index));
00995     }
00996     
01003     function clickLinkById($id) {
01004         return $this->_failOnError($this->_browser->clickLinkById($id));
01005     }
01006     
01017     function assertLink($label, $expected = true, $message = '%s') {
01018         $url = $this->_browser->getLink($label);
01019         if ($expected === true || ($expected !== true && $url === false)) {
01020             return $this->assertTrue($url !== false, sprintf($message, "Link [$label] should exist"));
01021         }
01022         if (! SimpleExpectation::isExpectation($expected)) {
01023             $expected = new IdenticalExpectation($expected);
01024         }
01025         return $this->assert($expected, $url->asString(), sprintf($message, "Link [$label] should match"));
01026     }
01027 
01038     function assertNoLink($label, $message = '%s') {
01039         return $this->assertTrue(
01040                 $this->_browser->getLink($label) === false,
01041                 sprintf($message, "Link [$label] should not exist"));
01042     }
01043     
01053     function assertLinkById($id, $expected = true, $message = '%s') {
01054         $url = $this->_browser->getLinkById($id);
01055         if ($expected === true) {
01056             return $this->assertTrue($url !== false, sprintf($message, "Link ID [$id] should exist"));
01057         }
01058         if (! SimpleExpectation::isExpectation($expected)) {
01059             $expected = new IdenticalExpectation($expected);
01060         }
01061         return $this->assert($expected, $url->asString(), sprintf($message, "Link ID [$id] should match"));
01062     }
01063 
01073     function assertNoLinkById($id, $message = '%s') {
01074         return $this->assertTrue(
01075                 $this->_browser->getLinkById($id) === false,
01076                 sprintf($message, "Link ID [$id] should not exist"));
01077     }
01078     
01087     function setField($label, $value, $position=false) {
01088         return $this->_browser->setField($label, $value, $position);
01089     }
01090     
01098     function setFieldByName($name, $value, $position=false) {
01099         return $this->_browser->setFieldByName($name, $value, $position);
01100     }
01101         
01109     function setFieldById($id, $value) {
01110         return $this->_browser->setFieldById($id, $value);
01111     }
01112     
01126     function assertField($label, $expected = true, $message = '%s') {
01127         $value = $this->_browser->getField($label);
01128         return $this->_assertFieldValue($label, $value, $expected, $message);
01129     }
01130     
01144     function assertFieldByName($name, $expected = true, $message = '%s') {
01145         $value = $this->_browser->getFieldByName($name);
01146         return $this->_assertFieldValue($name, $value, $expected, $message);
01147     }
01148         
01162     function assertFieldById($id, $expected = true, $message = '%s') {
01163         $value = $this->_browser->getFieldById($id);
01164         return $this->_assertFieldValue($id, $value, $expected, $message);
01165     }
01166     
01176     function _assertFieldValue($identifier, $value, $expected, $message) {
01177         if ($expected === true) {
01178             return $this->assertTrue(
01179                     isset($value),
01180                     sprintf($message, "Field [$identifier] should exist"));
01181         }
01182         if (! SimpleExpectation::isExpectation($expected)) {
01183             $identifier = str_replace('%', '%%', $identifier);
01184             $expected = new FieldExpectation(
01185                     $expected,
01186                     "Field [$identifier] should match with [%s]");
01187         }
01188         return $this->assert($expected, $value, $message);
01189     }
01190     
01200     function assertResponse($responses, $message = '%s') {
01201         $responses = (is_array($responses) ? $responses : array($responses));
01202         $code = $this->_browser->getResponseCode();
01203         $message = sprintf($message, "Expecting response in [" .
01204                 implode(", ", $responses) . "] got [$code]");
01205         return $this->assertTrue(in_array($code, $responses), $message);
01206     }
01207     
01216     function assertMime($types, $message = '%s') {
01217         $types = (is_array($types) ? $types : array($types));
01218         $type = $this->_browser->getMimeType();
01219         $message = sprintf($message, "Expecting mime type in [" .
01220                 implode(", ", $types) . "] got [$type]");
01221         return $this->assertTrue(in_array($type, $types), $message);
01222     }
01223     
01232     function assertAuthentication($authentication = false, $message = '%s') {
01233         if (! $authentication) {
01234             $message = sprintf($message, "Expected any authentication type, got [" .
01235                     $this->_browser->getAuthentication() . "]");
01236             return $this->assertTrue(
01237                     $this->_browser->getAuthentication(),
01238                     $message);
01239         } else {
01240             $message = sprintf($message, "Expected authentication [$authentication] got [" .
01241                     $this->_browser->getAuthentication() . "]");
01242             return $this->assertTrue(
01243                     strtolower($this->_browser->getAuthentication()) == strtolower($authentication),
01244                     $message);
01245         }
01246     }
01247     
01255     function assertNoAuthentication($message = '%s') {
01256         $message = sprintf($message, "Expected no authentication type, got [" .
01257                 $this->_browser->getAuthentication() . "]");
01258         return $this->assertFalse($this->_browser->getAuthentication(), $message);
01259     }
01260     
01268     function assertRealm($realm, $message = '%s') {
01269         if (! SimpleExpectation::isExpectation($realm)) {
01270             $realm = new EqualExpectation($realm);
01271         }
01272         return $this->assert(
01273                 $realm,
01274                 $this->_browser->getRealm(),
01275                 "Expected realm -> $message");
01276     }
01277     
01288     function assertHeader($header, $value = false, $message = '%s') {
01289         return $this->assert(
01290                 new HttpHeaderExpectation($header, $value),
01291                 $this->_browser->getHeaders(),
01292                 $message);
01293     }
01294         
01298     function assertHeaderPattern($header, $pattern, $message = '%s') {
01299         return $this->assert(
01300                 new HttpHeaderExpectation($header, new PatternExpectation($pattern)),
01301                 $this->_browser->getHeaders(),
01302                 $message);
01303     }
01304 
01314     function assertNoHeader($header, $message = '%s') {
01315         return $this->assert(
01316                 new NoHttpHeaderExpectation($header),
01317                 $this->_browser->getHeaders(),
01318                 $message);
01319     }
01320         
01324     function assertNoUnwantedHeader($header, $message = '%s') {
01325         return $this->assertNoHeader($header, $message);
01326     }
01327     
01335     function assertTitle($title = false, $message = '%s') {
01336         if (! SimpleExpectation::isExpectation($title)) {
01337             $title = new EqualExpectation($title);
01338         }
01339         return $this->assert($title, $this->_browser->getTitle(), $message);
01340     }
01341     
01350     function assertText($text, $message = '%s') {
01351         return $this->assert(
01352                 new TextExpectation($text),
01353                 $this->_browser->getContentAsText(),
01354                 $message);
01355     }
01356     
01360     function assertWantedText($text, $message = '%s') {
01361         return $this->assertText($text, $message);
01362     }
01363     
01372     function assertNoText($text, $message = '%s') {
01373         return $this->assert(
01374                 new NoTextExpectation($text),
01375                 $this->_browser->getContentAsText(),
01376                 $message);
01377     }
01378     
01382     function assertNoUnwantedText($text, $message = '%s') {
01383         return $this->assertNoText($text, $message);
01384     }
01385     
01395     function assertPattern($pattern, $message = '%s') {
01396         return $this->assert(
01397                 new PatternExpectation($pattern),
01398                 $this->_browser->getContent(),
01399                 $message);
01400     }
01401     
01405     function assertWantedPattern($pattern, $message = '%s') {
01406         return $this->assertPattern($pattern, $message);
01407     }
01408     
01418     function assertNoPattern($pattern, $message = '%s') {
01419         return $this->assert(
01420                 new NoPatternExpectation($pattern),
01421                 $this->_browser->getContent(),
01422                 $message);
01423     }
01424     
01428     function assertNoUnwantedPattern($pattern, $message = '%s') {
01429         return $this->assertNoPattern($pattern, $message);
01430     }
01431     
01442     function assertCookie($name, $expected = false, $message = '%s') {
01443         $value = $this->getCookie($name);
01444         if (! $expected) {
01445             return $this->assertTrue(
01446                     $value,
01447                     sprintf($message, "Expecting cookie [$name]"));
01448         }
01449         if (! SimpleExpectation::isExpectation($expected)) {
01450             $expected = new EqualExpectation($expected);
01451         }
01452         return $this->assert($expected, $value, "Expecting cookie [$name] -> $message");
01453     }
01454     
01463     function assertNoCookie($name, $message = '%s') {
01464         return $this->assertTrue(
01465                 $this->getCookie($name) === false,
01466                 sprintf($message, "Not expecting cookie [$name]"));
01467     }
01468 
01478     function assertTrue($result, $message = false) {
01479         return $this->assert(new TrueExpectation(), $result, $message);
01480     }
01481 
01492     function assertFalse($result, $message = '%s') {
01493         return $this->assert(new FalseExpectation(), $result, $message);
01494     }
01495     
01506     function assertEqual($first, $second, $message = '%s') {
01507         return $this->assert(
01508                 new EqualExpectation($first),
01509                 $second,
01510                 $message);
01511     }
01512     
01523     function assertNotEqual($first, $second, $message = '%s') {
01524         return $this->assert(
01525                 new NotEqualExpectation($first),
01526                 $second,
01527                 $message);
01528     }
01529 
01536     function getAssertionLine() {
01537         $trace = new SimpleStackTrace(array('assert', 'click', 'pass', 'fail'));
01538         return $trace->traceMethod();
01539     }
01540 }
01541 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations