You are here

private function BrowserTestBase::changeDatabasePrefix in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/BrowserTestBase.php \Drupal\simpletest\BrowserTestBase::changeDatabasePrefix()

Changes the database connection to the prefixed one.

See also

BrowserTestBase::prepareEnvironment()

1 call to BrowserTestBase::changeDatabasePrefix()
BrowserTestBase::prepareEnvironment in core/modules/simpletest/src/BrowserTestBase.php
Prepares the current environment for running the test.

File

core/modules/simpletest/src/BrowserTestBase.php, line 1042
Contains \Drupal\simpletest\BrowserTestBase.

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\simpletest

Code

private function changeDatabasePrefix() {
  if (empty($this->databasePrefix)) {
    $this
      ->prepareDatabasePrefix();
  }

  // If the test is run with argument dburl then use it.
  $db_url = getenv('SIMPLETEST_DB');
  if (!empty($db_url)) {
    $database = Database::convertDbUrlToConnectionInfo($db_url, DRUPAL_ROOT);
    Database::addConnectionInfo('default', 'default', $database);
  }

  // Clone the current connection and replace the current prefix.
  $connection_info = Database::getConnectionInfo('default');
  if (is_null($connection_info)) {
    throw new \InvalidArgumentException('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh.');
  }
  else {
    Database::renameConnection('default', 'simpletest_original_default');
    foreach ($connection_info as $target => $value) {

      // Replace the full table prefix definition to ensure that no table
      // prefixes of the test runner leak into the test.
      $connection_info[$target]['prefix'] = array(
        'default' => $value['prefix']['default'] . $this->databasePrefix,
      );
    }
    Database::addConnectionInfo('default', 'default', $connection_info['default']);
  }
}