function ErrorHandlerTest::testExceptionHandler

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php \Drupal\Tests\system\Functional\System\ErrorHandlerTest::testExceptionHandler()
  2. 10 core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php \Drupal\Tests\system\Functional\System\ErrorHandlerTest::testExceptionHandler()
  3. 11.x core/modules/system/tests/src/Functional/System/ErrorHandlerTest.php \Drupal\Tests\system\Functional\System\ErrorHandlerTest::testExceptionHandler()

Test 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' => drupal_get_path('module', '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' => drupal_get_path('module', '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' => drupal_get_path('module', '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->assertText($error_pdo_exception['%type'], new FormattableMarkup('Found %type in error page.', $error_pdo_exception));
    // 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->assertRaw($error_details, new FormattableMarkup("Found '@message' in error page.", [
        '@message' => $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->assertNull($this->drupalGetHeader('X-Drupal-Cache'));
    $this->assertSession()
        ->responseHeaderNotContains('Cache-Control', 'public');
    $this->assertSession()
        ->statusCodeEquals(500);
    $this->assertNoErrorMessage($error_exception);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.