summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrancesco Placella2018-08-07 10:09:45 (GMT)
committerFrancesco Placella2018-08-09 13:15:24 (GMT)
commite3458a701a997b21e3005a47d44cadf8a7a1104b (patch)
tree8799644268ca2f4263cc7b1398138d5ea104a10f
parent4523ef6850d61f01239830ec9021f8f1bc701eac (diff)
Issue #2987124 by alexpott, mondrake: Functional tests don't use SIMPLETEST_DB environmental variable as expected
(cherry picked from commit bacd6578d3fd5e7342f25b0a408382be16e152e4)
-rw-r--r--core/lib/Drupal/Core/Test/TestSetupTrait.php3
-rw-r--r--core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php19
-rw-r--r--core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php50
3 files changed, 62 insertions, 10 deletions
diff --git a/core/lib/Drupal/Core/Test/TestSetupTrait.php b/core/lib/Drupal/Core/Test/TestSetupTrait.php
index 4440907..0625691 100644
--- a/core/lib/Drupal/Core/Test/TestSetupTrait.php
+++ b/core/lib/Drupal/Core/Test/TestSetupTrait.php
@@ -158,6 +158,9 @@ trait TestSetupTrait {
// 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 34b4cc4..7abe0ef 100644
--- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php
+++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php
@@ -161,7 +161,15 @@ abstract class UpdatePathTestBase extends BrowserTestBase {
$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 @@ abstract class UpdatePathTestBase extends BrowserTestBase {
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 0000000..d0b8283
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Drupal\Tests\Core\Test;
+
+use Drupal\Core\Database\Database;
+use Drupal\Core\Test\TestSetupTrait;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Tests the TestSetupTrait trait.
+ *
+ * @coversDefaultClass \Drupal\Core\Test\TestSetupTrait
+ * @group Testing
+ *
+ * Run in a separate process as this test involves Database statics and
+ * environment variables.
+ * @runTestsInSeparateProcesses
+ * @preserveGlobalState disabled
+ */
+class TestSetupTraitTest extends UnitTestCase {
+
+ /**
+ * Tests the SIMPLETEST_DB environment variable is used.
+ *
+ * @covers ::changeDatabasePrefix
+ */
+ public function testChangeDatabasePrefix() {
+ putenv('SIMPLETEST_DB=pgsql://user:pass@127.0.0.1/db');
+ $connection_info = Database::convertDbUrlToConnectionInfo('mysql://user:pass@localhost/db', '');
+ Database::addConnectionInfo('default', 'default', $connection_info);
+ $this->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']);
+ }
+
+}