diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-12-09 11:09:26 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-12-09 11:09:26 +0000 |
commit | 6f859fdd8fa5272a3491074792c0c0015051cee9 (patch) | |
tree | 43a6c4533f005d70ff1092382c56bae884ac9922 /modules | |
parent | c50651f7fb348ec2dc6e29cdf462f22d64045002 (diff) | |
download | brdo-6f859fdd8fa5272a3491074792c0c0015051cee9.tar.gz brdo-6f859fdd8fa5272a3491074792c0c0015051cee9.tar.bz2 |
- Patch #328781 by Damien Tournoud and Dave Reid: fixed simpletest error reporting.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/simpletest/drupal_web_test_case.php | 8 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 78 | ||||
-rw-r--r-- | modules/simpletest/tests/system_test.module | 17 |
3 files changed, 92 insertions, 11 deletions
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php index 926d97f08..679d51a4d 100644 --- a/modules/simpletest/drupal_web_test_case.php +++ b/modules/simpletest/drupal_web_test_case.php @@ -128,15 +128,15 @@ class DrupalWebTestCase { * @param $message * The message string. * @param $group - * WHich group this assert belongs to. + * Which group this assert belongs to. * @param $caller - * By default, the assert comes from a function which names start with + * By default, the assert comes from a function whose name starts with * 'test'. Instead, you can specify where this assert originates from * by passing in an associative array as $caller. Key 'file' is * the name of the source file, 'line' is the line number and 'function' * is the caller function itself. */ - private function assert($status, $message = '', $group = 'Other', $caller = NULL) { + private function assert($status, $message = '', $group = 'Other', array $caller = NULL) { global $db_prefix; // Convert boolean status to string status. @@ -374,7 +374,7 @@ class DrupalWebTestCase { * @return * FALSE. */ - protected function error($message = '', $group = 'Other', $caller = NULL) { + protected function error($message = '', $group = 'Other', array $caller = NULL) { return $this->assert('exception', $message, $group, $caller); } diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 437f5dbd6..b10b0be05 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -287,7 +287,7 @@ class DrupalSetContentTestCase extends DrupalWebTestCase { function getInfo() { return array( 'name' => t('Drupal set/get content'), - 'description' => t("Performs tests on setting and retrieiving content from theme regions."), + 'description' => t('Performs tests on setting and retrieiving content from theme regions.'), 'group' => t('System') ); } @@ -338,7 +338,7 @@ class JavaScriptTestCase extends DrupalWebTestCase { function getInfo() { return array( 'name' => t('JavaScript'), - 'description' => t("Tests the JavaScript system."), + 'description' => t('Tests the JavaScript system.'), 'group' => t('System') ); } @@ -479,7 +479,7 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase { function getInfo() { return array( 'name' => t('Drupal error handlers'), - 'description' => t("Performs tests on the Drupal error and exception handler."), + 'description' => t('Performs tests on the Drupal error and exception handler.'), 'group' => t('System'), ); } @@ -521,3 +521,75 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase { $this->assertText($message, t("Found '%message' in error page.", array('%message' => $message))); } } + +/** + * Tests Simpletest error and exception collecter. + */ +class DrupalErrorCollectionUnitTest extends DrupalWebTestCase { + + /** + * Errors triggered during the test. + * + * Errors are intercepted by the overriden implementation + * of DrupalWebTestCase::error below. + * + * @var Array + */ + protected $collectedErrors = array(); + + function getInfo() { + return array( + 'name' => t('SimpleTest error collecter'), + 'description' => t('Performs tests on the Simpletest error and exception collecter.'), + 'group' => t('SimpleTest'), + ); + } + + function setUp() { + parent::setUp('system_test'); + } + + /** + * Test that simpletest collects errors from the tested site. + */ + function testErrorCollect() { + $this->collectedErrors = array(); + $this->drupalGet('system-test/generate-warnings-with-report'); + + $this->assertEqual(count($this->collectedErrors), 3, t('Three errors were collected')); + + if (count($this->collectedErrors) == 3) { + $this->assertError($this->collectedErrors[0], 'Notice', 'system_test_generate_warnings()', 'system_test.module', 'Undefined variable: bananas'); + $this->assertError($this->collectedErrors[1], 'Warning', 'system_test_generate_warnings()', 'system_test.module', 'Division by zero'); + $this->assertError($this->collectedErrors[2], 'User notice', 'system_test_generate_warnings()', 'system_test.module', 'Drupal is awesome'); + } + else { + // Give back the errors to the log report. + foreach ($this->collectedErrors as $error) { + parent::error($error['message'], $error['group'], $error['caller']); + } + } + } + + protected function error($message = '', $group = 'Other', array $caller = NULL) { + // This function overiddes DrupalWebTestCase::error(). We collect an error... + $this->collectedErrors[] = array( + 'message' => $message, + 'group' => $group, + 'caller' => $caller + ); + // ... and ignore it. + } + + /** + * Assert that a collected error matches what we are expecting. + */ + function assertError($error, $group, $function, $file, $message = NULL) { + $this->assertEqual($error['group'], $group, t("Group was %group", array('%group' => $group))); + $this->assertEqual($error['caller']['function'], $function, t("Function was %function", array('%function' => $function))); + $this->assertEqual(basename($error['caller']['file']), $file, t("File was %file", array('%file' => $file))); + if (isset($message)) { + $this->assertEqual($error['message'], $message, t("Message was %message", array('%message' => $message))); + } + } +}
\ No newline at end of file diff --git a/modules/simpletest/tests/system_test.module b/modules/simpletest/tests/system_test.module index dfe25bbf8..df13b2440 100644 --- a/modules/simpletest/tests/system_test.module +++ b/modules/simpletest/tests/system_test.module @@ -55,6 +55,14 @@ function system_test_menu() { 'type' => MENU_CALLBACK, ); + $items['system-test/generate-warnings-with-report'] = array( + 'title' => 'Generate warnings with Simpletest reporting', + 'page callback' => 'system_test_generate_warnings', + 'page arguments' => array(TRUE), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['system-test/trigger-exception'] = array( 'title' => 'Trigger an exception', 'page callback' => 'system_test_trigger_exception', @@ -169,8 +177,9 @@ function system_test_exit() { /** * Menu callback; generate warnings to test the error handler. */ -function system_test_generate_warnings() { - define('SIMPLETEST_DONT_COLLECT_ERRORS', TRUE); +function system_test_generate_warnings($collect_errors = FALSE) { + // Tell Drupal error reporter to send errors to Simpletest or not. + define('SIMPLETEST_COLLECT_ERRORS', $collect_errors); // This will generate a notice. $monkey_love = $bananas; // This will generate a warning. @@ -184,7 +193,7 @@ function system_test_generate_warnings() { * Menu callback; trigger an exception to test the exception handler. */ function system_test_trigger_exception() { - define('SIMPLETEST_DONT_COLLECT_ERRORS', TRUE); + define('SIMPLETEST_COLLECT_ERRORS', FALSE); throw new Exception("Drupal is awesome"); } @@ -192,6 +201,6 @@ function system_test_trigger_exception() { * Menu callback; trigger an exception to test the exception handler. */ function system_test_trigger_pdo_exception() { - define('SIMPLETEST_DONT_COLLECT_ERRORS', TRUE); + define('SIMPLETEST_COLLECT_ERRORS', FALSE); db_query("SELECT * FROM bananas_are_awesome"); } |