summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/error.test
diff options
context:
space:
mode:
Diffstat (limited to 'modules/simpletest/tests/error.test')
-rw-r--r--modules/simpletest/tests/error.test115
1 files changed, 115 insertions, 0 deletions
diff --git a/modules/simpletest/tests/error.test b/modules/simpletest/tests/error.test
new file mode 100644
index 000000000..8584e2a40
--- /dev/null
+++ b/modules/simpletest/tests/error.test
@@ -0,0 +1,115 @@
+<?php
+// $Id$
+
+/**
+ * Tests Drupal error and exception handlers.
+ */
+class DrupalErrorHandlerUnitTest extends DrupalWebTestCase {
+ public static 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('error_test');
+ }
+
+ /**
+ * Test the error handler.
+ */
+ function testErrorHandler() {
+ $error_notice = array(
+ '%type' => 'Notice',
+ '%message' => 'Undefined variable: bananas',
+ '%function' => 'error_test_generate_warnings()',
+ '%line' => 44,
+ '%file' => realpath('modules/simpletest/tests/error_test.module'),
+ );
+ $error_warning = array(
+ '%type' => 'Warning',
+ '%message' => 'Division by zero',
+ '%function' => 'error_test_generate_warnings()',
+ '%line' => 46,
+ '%file' => realpath('modules/simpletest/tests/error_test.module'),
+ );
+ $error_user_notice = array(
+ '%type' => 'User notice',
+ '%message' => 'Drupal is awesome',
+ '%function' => 'error_test_generate_warnings()',
+ '%line' => 48,
+ '%file' => realpath('modules/simpletest/tests/error_test.module'),
+ );
+
+ // Set error reporting to collect notices.
+ variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL);
+ $this->drupalGet('error-test/generate-warnings');
+ $this->assertErrorMessage($error_notice);
+ $this->assertErrorMessage($error_warning);
+ $this->assertErrorMessage($error_user_notice);
+
+ // Set error reporting to not collect notices.
+ variable_set('error_level', ERROR_REPORTING_DISPLAY_SOME);
+ $this->drupalGet('error-test/generate-warnings');
+ $this->assertNoErrorMessage($error_notice);
+ $this->assertErrorMessage($error_warning);
+ $this->assertErrorMessage($error_user_notice);
+
+ // Set error reporting to not show any errors.
+ variable_set('error_level', ERROR_REPORTING_HIDE);
+ $this->drupalGet('error-test/generate-warnings');
+ $this->assertNoErrorMessage($error_notice);
+ $this->assertNoErrorMessage($error_warning);
+ $this->assertNoErrorMessage($error_user_notice);
+ }
+
+ /**
+ * Test the exception handler.
+ */
+ function testExceptionHandler() {
+ $error_exception = array(
+ '%type' => 'Exception',
+ '%message' => 'Drupal is awesome',
+ '%function' => 'error_test_trigger_exception()',
+ '%line' => 57,
+ '%file' => realpath('modules/simpletest/tests/error_test.module'),
+ );
+ $error_pdo_exception = array(
+ '%type' => 'PDOException',
+ '%message' => 'SQLSTATE',
+ '%function' => 'error_test_trigger_pdo_exception()',
+ '%line' => 65,
+ '%file' => realpath('modules/simpletest/tests/error_test.module'),
+ );
+
+ $this->drupalGet('error-test/trigger-exception');
+ $this->assertErrorMessage($error_exception);
+
+ $this->drupalGet('error-test/trigger-pdo-exception');
+ // We cannot use assertErrorMessage() since the extact error reported
+ // varies from database to database. Check for the error keyword 'SQLSTATE'.
+ $this->assertText($error_pdo_exception['%type'], t('Found %type in error page.', $error_pdo_exception));
+ $this->assertText($error_pdo_exception['%message'], t('Found %message in error page.', $error_pdo_exception));
+ $error_details = t('in %function (line %line of %file)', $error_pdo_exception);
+ $this->assertRaw($error_details, t("Found '!message' in error page.", array('!message' => $error_details)));
+ }
+
+ /**
+ * Helper function: assert that the error message is found.
+ */
+ function assertErrorMessage(array $error) {
+ $message = t('%type: %message in %function (line %line of %file).', $error);
+ $this->assertRaw($message, t('Error !message found.', array('!message' => $message)));
+ }
+
+ /**
+ * Helper function: assert that the error message is not found.
+ */
+ function assertNoErrorMessage(array $error) {
+ $message = t('%type: %message in %function (line %line of %file).', $error);
+ $this->assertNoRaw($message, t('Error !message not found.', array('!message' => $message)));
+ }
+}
+