Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.176 diff -u -p -r1.176 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 2 Dec 2009 19:26:22 -0000 1.176 +++ modules/simpletest/drupal_web_test_case.php 6 Dec 2009 06:26:11 -0000 @@ -1166,6 +1166,9 @@ class DrupalWebTestCase extends DrupalTe protected function tearDown() { global $db_prefix, $user, $language; + // Capture any (remaining) watchdog messages. + $this->assertWatchdogMessages(); + // In case a fatal error occured that was not in the test process read the // log to pick up any fatal errors. $db_prefix_temp = $db_prefix; @@ -1224,6 +1227,59 @@ class DrupalWebTestCase extends DrupalTe } /** + * Assert watchdog messages based on their severity. + * + * This function can be (repeatedly) invoked to assert new watchdog messages. + * All watchdog messages with a higher severity than WATCHDOG_NOTICE are + * considered as fails. + * + * @param $pass_expected + * (optional) Boolean whether a non-severe watchdog messages are expected. + * Defaults to TRUE (no fail expected). If FALSE is passed, the logic for + * assertion messages is flipped, i.e. severe watchdog messages will pass + * and non-severe messages will fail. + * @param $type + * (optional) A log message type to limit the assertion to, e.g. 'user', + * 'page not found', 'access denied', or 'php'. + */ + protected function assertWatchdogMessages($pass_expected = TRUE, $type = NULL) { + static $seen_ids = array(), $table_exists; + + // @todo Implement simpletest_watchdog() to perform independent logging. + if (!isset($table_exists)) { + $table_exists = db_table_exists('watchdog'); + } + + $query = db_select('watchdog', 'w') + ->fields('w') + ->orderBy('timestamp'); + if (isset($type)) { + $query->condition('w.type', $type); + } + if (!empty($seen_ids)) { + $query->condition('w.wid', $seen_ids, 'NOT IN'); + } + foreach ($query->execute() as $dblog) { + // @see _dblog_format_message() + // Legacy messages and user specified text. + if ($dblog->variables === 'N;') { + $message = $dblog->message; + } + // Message to translate with injected variables. + else { + $message = t($dblog->message, unserialize($dblog->variables)); + } + if ($pass_expected ? $dblog->severity >= WATCHDOG_NOTICE : $dblog->severity < WATCHDOG_NOTICE) { + $this->pass(t('%type: !message', array('%type' => $dblog->type, '!message' => $message)), t('Watchdog')); + } + else { + $this->fail(t('%type: !message', array('%type' => $dblog->type, '!message' => $message)), t('Watchdog')); + } + $seen_ids[] = $dblog->wid; + } + } + + /** * Initializes the cURL connection. * * If the simpletest_httpauth_credentials variable is set, this function will @@ -2613,12 +2669,25 @@ class DrupalWebTestCase extends DrupalTe * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. * @param $message * Message to display. + * @param $assert_watchdog + * (optional) Boolean whether to automatically assert expected watchdog + * messages for 403 and 404 codes. + * * @return * Assertion result. */ - protected function assertResponse($code, $message = '') { + protected function assertResponse($code, $message = '', $assert_watchdog = TRUE) { $curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code; + // Automatically assert failing watchdog messages. + if ($assert_watchdog) { + if ($code == 403) { + $this->assertWatchdogMessages(FALSE, 'access denied'); + } + elseif ($code == 404) { + $this->assertWatchdogMessages(FALSE, 'page not found'); + } + } return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser')); }