You are here

function ErrorHandlerTest::testExceptionHandler in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/System/ErrorHandlerTest.php \Drupal\system\Tests\System\ErrorHandlerTest::testExceptionHandler()

Test the exception handler.

File

core/modules/system/src/Tests/System/ErrorHandlerTest.php, line 128
Contains \Drupal\system\Tests\System\ErrorHandlerTest.

Class

ErrorHandlerTest
Performs tests on the Drupal error and exception handler.

Namespace

Drupal\system\Tests\System

Code

function testExceptionHandler() {

  // Ensure the test error log is empty before these tests.
  $this
    ->assertNoErrorsLogged();
  $error_exception = array(
    '%type' => 'Exception',
    '@message' => 'Drupal & awesome',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->triggerException()',
    '%line' => 56,
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  );
  $error_pdo_exception = array(
    '%type' => 'DatabaseExceptionWrapper',
    '@message' => 'SELECT * FROM bananas_are_awesome',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->triggerPDOException()',
    '%line' => 64,
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  );
  $error_renderer_exception = array(
    '%type' => 'Exception',
    '@message' => 'This is an exception that occurs during rendering',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->Drupal\\error_test\\Controller\\{closure}()',
    '%line' => 82,
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  );
  $this
    ->drupalGet('error-test/trigger-exception');
  $this
    ->assertTrue(strpos($this
    ->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  $this
    ->assertErrorMessage($error_exception);
  $this
    ->drupalGet('error-test/trigger-pdo-exception');
  $this
    ->assertTrue(strpos($this
    ->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');

  // We cannot use assertErrorMessage() since the exact error reported
  // varies from database to database. Check that the SQL string is displayed.
  $this
    ->assertText($error_pdo_exception['%type'], format_string('Found %type in error page.', $error_pdo_exception));
  $this
    ->assertText($error_pdo_exception['@message'], format_string('Found @message in error page.', $error_pdo_exception));
  $error_details = format_string('in %function (line ', $error_pdo_exception);
  $this
    ->assertRaw($error_details, format_string("Found '@message' in error page.", array(
    '@message' => $error_details,
  )));
  $this
    ->drupalGet('error-test/trigger-renderer-exception');
  $this
    ->assertTrue(strpos($this
    ->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  $this
    ->assertErrorMessage($error_renderer_exception);

  // Disable error reporting, ensure that 5xx responses are not cached.
  $this
    ->config('system.logging')
    ->set('error_level', ERROR_REPORTING_HIDE)
    ->save();
  $this
    ->drupalGet('error-test/trigger-exception');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'));
  $this
    ->assertIdentical(strpos($this
    ->drupalGetHeader('Cache-Control'), 'public'), FALSE, 'Received expected HTTP status line.');
  $this
    ->assertTrue(strpos($this
    ->drupalGetHeader(':status'), '500 Service unavailable (with message)'), 'Received expected HTTP status line.');
  $this
    ->assertNoErrorMessage($error_exception);

  // The exceptions are expected. Do not interpret them as a test failure.
  // Not using File API; a potential error must trigger a PHP warning.
  unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
}