1 error.test BackdropErrorHandlerUnitTest::testErrorHandler()

Test the error handler.

File

core/modules/simpletest/tests/error.test, line 14

Class

BackdropErrorHandlerUnitTest
Tests Backdrop error and exception handlers.

Code

function testErrorHandler() {
  $config = config('system.core');
  $error_notice = array(
    '%type' => 'Notice',
    '!message' => 'Object of class stdClass could not be converted to int',
    '%function' => 'error_test_generate_warnings()',
    '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
  );
  $error_warning = array(
    '%type' => 'Warning',
    '!message' => \PHP_VERSION_ID < 80000 ? 'Invalid argument supplied for foreach()' : 'foreach() argument must be of type array|object, string given',
    '%function' => 'error_test_generate_warnings()',
    '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
  );
  $error_user_notice = array(
    '%type' => 'User warning',
    '!message' => 'Backdrop is awesome',
    '%function' => 'error_test_generate_warnings()',
    '%file' => backdrop_realpath('core/modules/simpletest/tests/error_test.module'),
  );

  // Set error reporting to collect notices.
  $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
  $this->backdropGet('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);

  // Set error reporting to not collect notices.
  $config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save();
  $this->backdropGet('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);

  // Set error reporting to not show any errors.
  $config->set('error_level', ERROR_REPORTING_HIDE)->save();
  $this->backdropGet('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);
}