You are here

public function ErrorHandlerTest::testExceptionHandler in Drupal 9

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

Tests the exception handler.

File

core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php, line 98

Class

ErrorHandlerTest
Performs tests on the Drupal error and exception handler.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testExceptionHandler() {
  $error_exception = [
    '%type' => 'Exception',
    '@message' => 'Drupal & awesome',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->triggerException()',
    '%line' => 56,
    '%file' => $this
      ->getModulePath('error_test') . '/error_test.module',
  ];
  $error_pdo_exception = [
    '%type' => 'DatabaseExceptionWrapper',
    '@message' => 'SELECT "b".* FROM {bananas_are_awesome} "b"',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->triggerPDOException()',
    '%line' => 64,
    '%file' => $this
      ->getModulePath('error_test') . '/error_test.module',
  ];
  $error_renderer_exception = [
    '%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' => $this
      ->getModulePath('error_test') . '/error_test.module',
  ];
  $this
    ->drupalGet('error-test/trigger-exception');
  $this
    ->assertSession()
    ->statusCodeEquals(500);
  $this
    ->assertErrorMessage($error_exception);
  $this
    ->drupalGet('error-test/trigger-pdo-exception');
  $this
    ->assertSession()
    ->statusCodeEquals(500);

  // We cannot use assertErrorMessage() since the exact error reported
  // varies from database to database. Check that the SQL string is displayed.
  $this
    ->assertSession()
    ->pageTextContains($error_pdo_exception['%type']);

  // Assert statement improved since static queries adds table alias in the
  // error message.
  $this
    ->assertSession()
    ->pageTextContains($error_pdo_exception['@message']);
  $error_details = new FormattableMarkup('in %function (line ', $error_pdo_exception);
  $this
    ->assertSession()
    ->responseContains($error_details);
  $this
    ->drupalGet('error-test/trigger-renderer-exception');
  $this
    ->assertSession()
    ->statusCodeEquals(500);
  $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
    ->assertSession()
    ->responseHeaderDoesNotExist('X-Drupal-Cache');
  $this
    ->assertSession()
    ->responseHeaderNotContains('Cache-Control', 'public');
  $this
    ->assertSession()
    ->statusCodeEquals(500);
  $this
    ->assertNoErrorMessage($error_exception);
}