You are here

function ErrorHandlerTest::testErrorHandler in Zircon Profile 8.0

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

Test the error handler.

File

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

Class

ErrorHandlerTest
Performs tests on the Drupal error and exception handler.

Namespace

Drupal\system\Tests\System

Code

function testErrorHandler() {
  $config = $this
    ->config('system.logging');
  $error_notice = array(
    '%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 = array(
    '%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 = array(
    '%type' => 'User warning',
    '@message' => 'Drupal & awesome',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->generateWarnings()',
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  );
  $fatal_error = array(
    '%type' => 'Recoverable fatal error',
    '%function' => 'Drupal\\error_test\\Controller\\ErrorTestController->Drupal\\error_test\\Controller\\{closure}()',
    '@message' => 'Argument 1 passed to Drupal\\error_test\\Controller\\ErrorTestController::Drupal\\error_test\\Controller\\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66 and defined',
  );
  if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) {

    // In PHP 7, instead of a recoverable fatal error we get a TypeError.
    $fatal_error['%type'] = 'TypeError';

    // The error message also changes in PHP 7.
    $fatal_error['@message'] = 'Argument 1 passed to Drupal\\error_test\\Controller\\ErrorTestController::Drupal\\error_test\\Controller\\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66';
  }

  // 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
    ->assertResponse(200, 'Received expected HTTP status code.');
  $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();
  $this
    ->drupalGet('error-test/generate-fatals');
  $this
    ->assertResponse(500, 'Received expected HTTP status code.');
  $this
    ->assertErrorMessage($fatal_error);
  $this
    ->assertRaw('<pre class="backtrace">', 'Found pre element with backtrace class.');

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

  // Remove the recoverable fatal error from the assertions, it's wanted here.
  // Ensure that we just remove this one recoverable fatal error (in PHP 7 this
  // is a TypeError).
  foreach ($this->assertions as $key => $assertion) {
    if (in_array($assertion['message_group'], [
      'Recoverable fatal error',
      'TypeError',
    ]) && strpos($assertion['message'], 'Argument 1 passed to Drupal\\error_test\\Controller\\ErrorTestController::Drupal\\error_test\\Controller\\{closure}() must be of the type array, string given, called in') !== FALSE) {
      unset($this->assertions[$key]);
      $this
        ->deleteAssert($assertion['message_id']);
    }
  }

  // Drop the single exception.
  $this->results['#exception']--;

  // Set error reporting to collect notices.
  $config
    ->set('error_level', ERROR_REPORTING_DISPLAY_ALL)
    ->save();
  $this
    ->drupalGet('error-test/generate-warnings');
  $this
    ->assertResponse(200, 'Received expected HTTP status code.');
  $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.');
  $this
    ->assertErrorLogged($fatal_error['@message']);

  // Set error reporting to not collect notices.
  $config
    ->set('error_level', ERROR_REPORTING_DISPLAY_SOME)
    ->save();
  $this
    ->drupalGet('error-test/generate-warnings');
  $this
    ->assertResponse(200, 'Received expected HTTP status code.');
  $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
    ->assertResponse(200, 'Received expected HTTP status code.');
  $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.');
}