You are here

public function UncaughtExceptionTest::testLoggerException in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php \Drupal\FunctionalTests\Bootstrap\UncaughtExceptionTest::testLoggerException()
  2. 10 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 250

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
    ->assertSession()
    ->statusCodeEquals(500);
  $this
    ->assertSession()
    ->pageTextContains('The website encountered an unexpected error. Please try again later.');
  $this
    ->assertSession()
    ->pageTextContains($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 = 61;
  $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');
}