Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TestSetupTrait.php

function 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 it is also used in the user agent HTTP header value by BrowserTestBase, which is sent to the Drupal installation of the test. During early Drupal all bootstrap, the user agent HTTP header is parsed, and if it matches, database queries use the database table prefix that has been generated here.

See also

\Drupal\Tests\BrowserTestBase::prepareEnvironment()

drupal_valid_test_ua()

1 call to TestSetupTrait::prepareDatabasePrefix()
TestSetupTrait::changeDatabasePrefix in core/lib/Drupal/Core/Test/TestSetupTrait.php
Changes the database connection to the prefixed one.

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();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal