1.20.x backdrop_web_test_case.php protected BackdropTestCase::prepareDatabasePrefix()

Generates a database prefix for running tests.

The generated database table prefix is used for the Backdrop installation being performed for the test. It is also used as user agent HTTP header value by the cURL-based browser of BackdropWebTestCase, which is sent to the Backdrop installation of the test. During early Backdrop 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

BackdropWebTestCase::curlInitialize()

backdrop_valid_test_ua()

BackdropWebTestCase::setUp()

File

modules/simpletest/backdrop_web_test_case.php, line 172

Class

BackdropTestCase
Base class for Backdrop tests.

Code

protected function prepareDatabasePrefix() {
  // Generate a temporary prefixed database to ensure that tests have a clean
  // starting point and confirm that random prefix isn't already in use.
  db_transaction();
  do {
    $prefix = 'simpletest' . mt_rand(100000, 999999);
    $prefix_exists = db_query("SELECT COUNT(*) FROM {simpletest_prefix} WHERE prefix = :prefix", array(':prefix' => $prefix))->fetchField();
  } while ($prefix_exists);
  $this->databasePrefix = $prefix;
  $this->fileDirectoryName = substr($prefix, 10);

  // 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_insert('simpletest_prefix')
    ->fields(array(
      'test_id' => $this->testId,
      'prefix' => $this->databasePrefix,
    ))
    ->execute();
}