function ErrorHandlerTest::testErrorHandler

Test the error handler.

File

drupal/core/modules/system/lib/Drupal/system/Tests/System/ErrorHandlerTest.php, line 35
Definition of Drupal\system\Tests\System\ErrorHandlerTest.

Class

ErrorHandlerTest
Tests error and exception handlers.

Namespace

Drupal\system\Tests\System

Code

function testErrorHandler() {
  $config = config('system.logging');
  $error_notice = array(
    '%type' => 'Notice',
    '!message' => 'Undefined variable: bananas',
    '%function' => 'error_test_generate_warnings()',
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  );
  $error_warning = array(
    '%type' => 'Warning',
    '!message' => 'Division by zero',
    '%function' => 'error_test_generate_warnings()',
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  );
  $error_user_notice = array(
    '%type' => 'User warning',
    '!message' => 'Drupal is awesome',
    '%function' => 'error_test_generate_warnings()',
    '%file' => drupal_get_path('module', 'error_test') . '/error_test.module',
  );

  // Set error reporting to display verbose notices.
  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.');

  // 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.');

  // 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
    ->assertNoRaw('<pre class="backtrace">', 'Did not find pre element with backtrace class.');
}