You are here

public function ErrorHandlerTest::testErrorHandler in Drupal 8

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

Test the error handler.

File

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

Class

ErrorHandlerTest
Performs tests on the Drupal error and exception handler.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testErrorHandler() {
  $config = $this
    ->config('system.logging');
  $error_notice = [
    '%type' => 'Notice',
    '@message' => 'Undefined variable: bananas',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->generateWarnings()',
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  ];
  $error_warning = [
    '%type' => 'Warning',
    '@message' => 'Division by zero',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->generateWarnings()',
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  ];
  $error_user_notice = [
    '%type' => 'User warning',
    '@message' => 'Drupal & awesome',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->generateWarnings()',
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  ];

  // Set error reporting to display verbose notices.
  $this
    ->config('system.logging')
    ->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)
    ->save();
  $this
    ->drupalGet('error-test/generate-warnings');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertErrorMessage($error_notice);
  $this
    ->assertErrorMessage($error_warning);
  $this
    ->assertErrorMessage($error_user_notice);
  $this
    ->assertRaw('<pre class="backtrace">', 'Found pre element with backtrace class.');

  // Ensure we are escaping but not double escaping.
  $this
    ->assertRaw('&amp;');
  $this
    ->assertNoRaw('&amp;amp;');

  // Set error reporting to display verbose notices.
  $this
    ->config('system.logging')
    ->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)
    ->save();

  // Set error reporting to collect notices.
  $config
    ->set('error_level', ERROR_REPORTING_DISPLAY_ALL)
    ->save();
  $this
    ->drupalGet('error-test/generate-warnings');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertErrorMessage($error_notice);
  $this
    ->assertErrorMessage($error_warning);
  $this
    ->assertErrorMessage($error_user_notice);
  $this
    ->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');

  // Set error reporting to not collect notices.
  $config
    ->set('error_level', ERROR_REPORTING_DISPLAY_SOME)
    ->save();
  $this
    ->drupalGet('error-test/generate-warnings');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoErrorMessage($error_notice);
  $this
    ->assertErrorMessage($error_warning);
  $this
    ->assertErrorMessage($error_user_notice);
  $this
    ->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');

  // Set error reporting to not show any errors.
  $config
    ->set('error_level', ERROR_REPORTING_HIDE)
    ->save();
  $this
    ->drupalGet('error-test/generate-warnings');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoErrorMessage($error_notice);
  $this
    ->assertNoErrorMessage($error_warning);
  $this
    ->assertNoErrorMessage($error_user_notice);
  $this
    ->assertNoMessages();
  $this
    ->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
}