You are here

protected function TestSetupTrait::prepareDatabasePrefix in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Test/TestSetupTrait.php \Drupal\Core\Test\TestSetupTrait::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

\Drupal\Tests\BrowserTestBase::prepareEnvironment()

\Drupal\simpletest\WebTestBase::curlInitialize()

\Drupal\simpletest\TestBase::prepareEnvironment()

drupal_valid_test_ua()

3 calls to TestSetupTrait::prepareDatabasePrefix()
InstallerTestBase::setUp in core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php
SelectProfileFormTest::setUp in core/tests/Drupal/FunctionalJavascriptTests/Core/Installer/Form/SelectProfileFormTest.php
TestSetupTrait::changeDatabasePrefix in core/lib/Drupal/Core/Test/TestSetupTrait.php
Changes the database connection to the prefixed one.
2 methods override TestSetupTrait::prepareDatabasePrefix()
TestBase::prepareDatabasePrefix in core/modules/simpletest/src/TestBase.php
Generates a database prefix for running tests.
TestSiteInstallCommand::prepareDatabasePrefix in core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php
Generates a database prefix for running tests.

File

core/lib/Drupal/Core/Test/TestSetupTrait.php, line 137

Class

TestSetupTrait
Provides a trait for shared test setup functionality.

Namespace

Drupal\Core\Test

Code

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