diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/simpletest/drupal_web_test_case.php | 3 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 49 | ||||
-rw-r--r-- | modules/simpletest/tests/system_test.info | 1 | ||||
-rw-r--r-- | modules/simpletest/tests/system_test.module | 48 |
4 files changed, 98 insertions, 3 deletions
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php index eba232b86..988f1592c 100644 --- a/modules/simpletest/drupal_web_test_case.php +++ b/modules/simpletest/drupal_web_test_case.php @@ -322,8 +322,7 @@ class DrupalWebTestCase { * @see set_error_handler */ function errorHandler($severity, $message, $file = NULL, $line = NULL) { - $severity = $severity & error_reporting(); - if ($severity) { + if ($severity & error_reporting()) { $error_map = array( E_STRICT => 'Run-time notice', E_WARNING => 'Warning', diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 43e7b0216..0d7ad985c 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -251,3 +251,52 @@ class DrupalSetContentTestCase extends DrupalWebTestCase { } } } + +/** + * Tests Drupal error and exception handlers. + */ +class DrupalErrorHandlerUnitTest extends DrupalWebTestCase { + function getInfo() { + return array( + 'name' => t('Drupal error handlers'), + 'description' => t("Performs tests on the Drupal error and exception handler."), + 'group' => t('System'), + ); + } + + function setUp() { + parent::setUp('system_test'); + } + + /** + * Test the error handler. + */ + function testErrorHandler() { + $this->drupalGet('system-test/generate-warnings'); + + $this->assertErrorMessage('Notice', 'system_test.module', 'system_test_generate_warnings() ', 'Undefined variable'); + $this->assertErrorMessage('Warning', 'system_test.module', 'system_test_generate_warnings() ', 'Division by zero'); + $this->assertErrorMessage('User notice', 'system_test.module', 'system_test_generate_warnings() ', 'Drupal is awesome'); + } + + /** + * Test the exception handler. + */ + function testExceptionHandler() { + $this->drupalGet('system-test/trigger-exception'); + $this->assertErrorMessage('Exception', 'system_test.module', 'system_test_trigger_exception()', 'Drupal is awesome'); + + $this->drupalGet('system-test/trigger-pdo-exception'); + $this->assertErrorMessage('PDOException', 'system_test.module', 'system_test_trigger_pdo_exception()', 'Base table or view not found'); + } + + /** + * Helper function: assert that the logged message is correct. + */ + function assertErrorMessage($type, $file, $function, $message) { + $this->assertText($type, t("Found '%type' in error page.", array('%type' => $type))); + $this->assertText($file, t("Found '%file' in error page.", array('%file' => $file))); + $this->assertText($function, t("Found '%function' in error page.", array('%function' => $function))); + $this->assertText($message, t("Found '%message' in error page.", array('%message' => $message))); + } +} diff --git a/modules/simpletest/tests/system_test.info b/modules/simpletest/tests/system_test.info index f70a1a79f..1910e87ae 100644 --- a/modules/simpletest/tests/system_test.info +++ b/modules/simpletest/tests/system_test.info @@ -5,4 +5,3 @@ package = Testing version = VERSION core = 7.x files[] = system_test.module -hidden = TRUE diff --git a/modules/simpletest/tests/system_test.module b/modules/simpletest/tests/system_test.module index 188facc96..bd3eff6c5 100644 --- a/modules/simpletest/tests/system_test.module +++ b/modules/simpletest/tests/system_test.module @@ -48,6 +48,27 @@ function system_test_menu() { 'type' => MENU_CALLBACK, ); + $items['system-test/generate-warnings'] = array( + 'title' => 'Generate warnings', + 'page callback' => 'system_test_generate_warnings', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + + $items['system-test/trigger-exception'] = array( + 'title' => 'Trigger an exception', + 'page callback' => 'system_test_trigger_exception', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + + $items['system-test/trigger-pdo-exception'] = array( + 'title' => 'Trigger a PDO exception', + 'page callback' => 'system_test_trigger_pdo_exception', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + return $items; } @@ -120,3 +141,30 @@ function system_test_modules_uninstalled($modules) { drupal_set_message(t('hook_modules_uninstalled fired for aggregator')); } } + +/** + * Menu callback; generate warnings to test the error handler. + */ +function system_test_generate_warnings() { + // This will generate a notice. + $monkey_love = $bananas; + // This will generate a warning. + $awesomely_big = 1/0; + // This will generate a user error. + trigger_error("Drupal is awesome", E_USER_NOTICE); + return ""; +} + +/** + * Menu callback; trigger an exception to test the exception handler. + */ +function system_test_trigger_exception() { + throw new Exception("Drupal is awesome"); +} + +/** + * Menu callback; trigger an exception to test the exception handler. + */ +function system_test_trigger_pdo_exception() { + db_query("SELECT * FROM bananas_are_awesome"); +} |