You are here

private function TestBase::prepareDatabasePrefix in Drupal 8

Generates a database prefix for running tests.

The database prefix is used by prepareEnvironment() to setup a public files directory for the test to be run, which also contains the PHP error log, which is written to in case of a fatal error. Since that directory is based on the database prefix, all tests (even unit tests) need to have one, in order to access and read the error log.

The generated database table prefix is used for the Drupal installation being performed for the test. It is also used as user agent HTTP header value by the cURL-based browser of WebTestBase, which is sent to the Drupal installation of the test. During early Drupal bootstrap, the user agent HTTP header is parsed, and if it matches, all database queries use the database table prefix that has been generated here.

Overrides TestSetupTrait::prepareDatabasePrefix

See also

TestBase::prepareEnvironment()

WebTestBase::curlInitialize()

drupal_valid_test_ua()

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

File

core/modules/simpletest/src/TestBase.php, line 1036

Class

TestBase
Base class for Drupal tests.

Namespace

Drupal\simpletest

Code

private function prepareDatabasePrefix() {
  $test_db = new TestDatabase();
  $this->siteDirectory = $test_db
    ->getTestSitePath();
  $this->databasePrefix = $test_db
    ->getDatabasePrefix();

  // As soon as the database prefix is set, the test might start to execute.
  // All assertions as well as the SimpleTest batch operations are associated
  // with the testId, so the database prefix has to be associated with it.
  $affected_rows = self::getDatabaseConnection()
    ->update('simpletest_test_id')
    ->fields([
    'last_prefix' => $this->databasePrefix,
  ])
    ->condition('test_id', $this->testId)
    ->execute();
  if (!$affected_rows) {
    throw new \RuntimeException('Failed to set up database prefix.');
  }
}