function UncaughtExceptionTest::testLostDatabaseConnection

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php \Drupal\FunctionalTests\Bootstrap\UncaughtExceptionTest::testLostDatabaseConnection()
  2. 8.9.x core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php \Drupal\FunctionalTests\Bootstrap\UncaughtExceptionTest::testLostDatabaseConnection()
  3. 11.x 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 191

Class

UncaughtExceptionTest
Tests kernel panic when things are really messed up.

Namespace

Drupal\FunctionalTests\Bootstrap

Code

public function testLostDatabaseConnection() : void {
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.