You are here

public function UncaughtExceptionTest::testLoggerException in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php \Drupal\FunctionalTests\Bootstrap\UncaughtExceptionTest::testLoggerException()

Tests fallback to PHP error log when an exception is thrown while logging.

File

core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php, line 268

Class

UncaughtExceptionTest
Tests kernel panic when things are really messed up.

Namespace

Drupal\FunctionalTests\Bootstrap

Code

public function testLoggerException() {

  // Ensure the test error log is empty before these tests.
  $this
    ->assertNoErrorsLogged();
  $this->expectedExceptionMessage = 'Deforestation';
  \Drupal::state()
    ->set('error_service_test.break_logger', TRUE);
  $this
    ->drupalGet('');
  $this
    ->assertResponse(500);
  $this
    ->assertText('The website encountered an unexpected error. Please try again later.');
  $this
    ->assertRaw($this->expectedExceptionMessage);

  // Find fatal error logged to the error.log
  $errors = file(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
  $this
    ->assertCount(8, $errors, 'The error + the error that the logging service is broken has been written to the error log.');
  $this
    ->assertStringContainsString('Failed to log error', $errors[0], 'The error handling logs when an error could not be logged to the logger.');
  $expected_path = \Drupal::root() . '/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php';
  $expected_line = 59;
  $expected_entry = "Failed to log error: Exception: Deforestation in Drupal\\error_service_test\\MonkeysInTheControlRoom->handle() (line {$expected_line} of {$expected_path})";
  $this
    ->assertStringContainsString($expected_entry, $errors[0], 'Original error logged to the PHP error log when an exception is thrown by a logger');

  // The exception is expected. Do not interpret it as a test failure. Not
  // using File API; a potential error must trigger a PHP warning.
  unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
}