protected function TestBase::prepareDatabasePrefix

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 DrupalWebTestCase, 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.

See also

TestBase::prepareEnvironment()

WebTestBase::curlInitialize()

drupal_valid_test_ua()

WebTestBase::setUp()

4 calls to TestBase::prepareDatabasePrefix()
TestBase::changeDatabasePrefix in drupal/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
Changes the database connection to the prefixed one.
UnitTestBase::setUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
Sets up unit test environment.
UpgradePathTestBase::setUp in drupal/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
Overrides Drupal\simpletest\WebTestBase::setUp() for upgrade testing.
WebTestBase::setUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
Sets up a Drupal site for running functional and integration tests.

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php, line 743
Definition of Drupal\simpletest\TestBase.

Class

TestBase
Base class for Drupal tests.

Namespace

Drupal\simpletest

Code

protected function prepareDatabasePrefix() {
  $this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);

  // 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.
  db_update('simpletest_test_id')
    ->fields(array(
    'last_prefix' => $this->databasePrefix,
  ))
    ->condition('test_id', $this->testId)
    ->execute();
}