private function TestBase::prepareDatabasePrefix in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/simpletest/src/TestBase.php \Drupal\simpletest\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 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.
See also
TestBase::prepareEnvironment()
2 calls to TestBase::prepareDatabasePrefix()
- TestBase::changeDatabasePrefix in core/
modules/ simpletest/ src/ TestBase.php - Changes the database connection to the prefixed one.
- 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 1129 - Contains \Drupal\simpletest\TestBase.
Class
- TestBase
- Base class for Drupal tests.
Namespace
Drupal\simpletestCode
private function prepareDatabasePrefix() {
// Ensure that the generated test site directory does not exist already,
// which may happen with a large amount of concurrent threads and
// long-running tests.
do {
$suffix = mt_rand(100000, 999999);
$this->siteDirectory = 'sites/simpletest/' . $suffix;
$this->databasePrefix = 'simpletest' . $suffix;
} while (is_dir(DRUPAL_ROOT . '/' . $this->siteDirectory));
// 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(array(
'last_prefix' => $this->databasePrefix,
))
->condition('test_id', $this->testId)
->execute();
if (!$affected_rows) {
throw new \RuntimeException('Failed to set up database prefix.');
}
}