diff --git a/core/lib/Drupal/Core/Test/TestSetupTrait.php b/core/lib/Drupal/Core/Test/TestSetupTrait.php index 44409078dbf3854670711937948c404d887beb36..0625691022cd451bde0804482f593089a792f654 100644 --- a/core/lib/Drupal/Core/Test/TestSetupTrait.php +++ b/core/lib/Drupal/Core/Test/TestSetupTrait.php @@ -158,6 +158,9 @@ protected function changeDatabasePrefix() { // If the test is run with argument dburl then use it. $db_url = getenv('SIMPLETEST_DB'); if (!empty($db_url)) { + // Ensure no existing database gets in the way. If a default database + // exists already it must be removed. + Database::removeConnection('default'); $database = Database::convertDbUrlToConnectionInfo($db_url, isset($this->root) ? $this->root : DRUPAL_ROOT); Database::addConnectionInfo('default', 'default', $database); } diff --git a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php index 34b4cc4dbef94ce1c6131e56658745cd48155d12..7abe0efe30a6656c0c42c2bc50c0b8bd0075d9bc 100644 --- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php @@ -161,7 +161,15 @@ protected function setUp() { $kernel = TestRunnerKernel::createFromRequest($request, $autoloader); $kernel->loadLegacyIncludes(); - $this->changeDatabasePrefix(); + // Set the update url. This must be set here rather than in + // self::__construct() or the old URL generator will leak additional test + // sites. + $this->updateUrl = Url::fromRoute('system.db_update'); + + $this->setupBaseUrl(); + + // Install Drupal test site. + $this->prepareEnvironment(); $this->runDbTasks(); // Allow classes to set database dump files. $this->setDatabaseDumpFiles(); @@ -173,15 +181,6 @@ protected function setUp() { parent::setUp(); return; } - // Set the update url. This must be set here rather than in - // self::__construct() or the old URL generator will leak additional test - // sites. - $this->updateUrl = Url::fromRoute('system.db_update'); - - $this->setupBaseUrl(); - - // Install Drupal test site. - $this->prepareEnvironment(); $this->installDrupal(); // Add the config directories to settings.php. diff --git a/core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php b/core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d0b82835666100dec9f39f2944eb4d1c2a59a566 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php @@ -0,0 +1,50 @@ +assertEquals('mysql', Database::getConnectionInfo()['default']['driver']); + $this->assertEquals('localhost', Database::getConnectionInfo()['default']['host']); + + // Create a mock for testing the trait and set a few properties that are + // used to avoid unnecessary set up. + $test_setup = $this->getMockForTrait(TestSetupTrait::class); + $test_setup->databasePrefix = 'testDbPrefix'; + $test_setup->root = ''; + + $method = new \ReflectionMethod(get_class($test_setup), 'changeDatabasePrefix'); + $method->setAccessible(TRUE); + $method->invoke($test_setup); + + // Ensure that SIMPLETEST_DB defines the default database connection after + // calling \Drupal\Core\Test\TestSetupTrait::changeDatabasePrefix(). + $this->assertEquals('pgsql', Database::getConnectionInfo()['default']['driver']); + $this->assertEquals('127.0.0.1', Database::getConnectionInfo()['default']['host']); + } + +}