Commit bacd6578 authored by plach's avatar plach

Issue #2987124 by alexpott, mondrake: Functional tests don't use SIMPLETEST_DB...

Issue #2987124 by alexpott, mondrake: Functional tests don't use SIMPLETEST_DB environmental variable as expected
parent 0e0105fd
...@@ -158,6 +158,9 @@ protected function changeDatabasePrefix() { ...@@ -158,6 +158,9 @@ protected function changeDatabasePrefix() {
// If the test is run with argument dburl then use it. // If the test is run with argument dburl then use it.
$db_url = getenv('SIMPLETEST_DB'); $db_url = getenv('SIMPLETEST_DB');
if (!empty($db_url)) { 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 = Database::convertDbUrlToConnectionInfo($db_url, isset($this->root) ? $this->root : DRUPAL_ROOT);
Database::addConnectionInfo('default', 'default', $database); Database::addConnectionInfo('default', 'default', $database);
} }
......
...@@ -161,7 +161,15 @@ protected function setUp() { ...@@ -161,7 +161,15 @@ protected function setUp() {
$kernel = TestRunnerKernel::createFromRequest($request, $autoloader); $kernel = TestRunnerKernel::createFromRequest($request, $autoloader);
$kernel->loadLegacyIncludes(); $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(); $this->runDbTasks();
// Allow classes to set database dump files. // Allow classes to set database dump files.
$this->setDatabaseDumpFiles(); $this->setDatabaseDumpFiles();
...@@ -173,15 +181,6 @@ protected function setUp() { ...@@ -173,15 +181,6 @@ protected function setUp() {
parent::setUp(); parent::setUp();
return; 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(); $this->installDrupal();
// Add the config directories to settings.php. // Add the config directories to settings.php.
......
<?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']);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment