You are here

public function UncaughtExceptionTest::testLostDatabaseConnection in Drupal 9

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

Tests the case when the database connection is gone.

File

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

Class

UncaughtExceptionTest
Tests kernel panic when things are really messed up.

Namespace

Drupal\FunctionalTests\Bootstrap

Code

public function testLostDatabaseConnection() {
  $incorrect_username = $this
    ->randomMachineName(16);
  switch ($this->container
    ->get('database')
    ->driver()) {
    case 'pgsql':
    case 'mysql':
      $this->expectedExceptionMessage = $incorrect_username;
      break;
    default:

      // We can not carry out this test.
      $this
        ->markTestSkipped('Unable to run \\Drupal\\system\\Tests\\System\\UncaughtExceptionTest::testLostDatabaseConnection for this database type.');
  }

  // We simulate a broken database connection by rewrite settings.php to no
  // longer have the proper data.
  $settings['databases']['default']['default']['username'] = (object) [
    'value' => $incorrect_username,
    'required' => TRUE,
  ];
  $settings['databases']['default']['default']['password'] = (object) [
    'value' => $this
      ->randomMachineName(16),
    'required' => TRUE,
  ];
  $this
    ->writeSettings($settings);
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->statusCodeEquals(500);
  $this
    ->assertSession()
    ->pageTextContains('DatabaseAccessDeniedException');
  $this
    ->assertErrorLogged($this->expectedExceptionMessage);
}