/** * 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 by the cookie value of * SIMPLETEST_USER_AGENT by the Mink controlled browser. During early Drupal * bootstrap, the cookie is parsed, and if it matches, all database queries * use the database table prefix that has been generated here. * * @see drupal_valid_test_ua() * @see BrowserTestBase::prepareEnvironment() */ private function prepareDatabasePrefix() { $test_db = new TestDatabase(); $this->siteDirectory = $test_db->getTestSitePath(); $this->databasePrefix = $test_db->getDatabasePrefix(); }
/** * @covers ::__construct * @covers ::getDatabasePrefix * @covers ::getTestSitePath * * @dataProvider providerTestConstructor */ public function testConstructor($db_prefix, $expected_db_prefix, $expected_site_path) { $test_db = new TestDatabase($db_prefix); $this->assertEquals($expected_db_prefix, $test_db->getDatabasePrefix()); $this->assertEquals($expected_site_path, $test_db->getTestSitePath()); }
/** * Bootstraps a basic test environment. * * Should not be called by tests. Only visible for DrupalKernel integration * tests. * * @see \Drupal\system\Tests\DrupalKernel\DrupalKernelTest * @internal */ protected function bootEnvironment() { $this->streamWrappers = array(); \Drupal::unsetContainer(); $this->classLoader = (require $this->root . '/autoload.php'); require_once $this->root . '/core/includes/bootstrap.inc'; // Set up virtual filesystem. Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']); $test_db = new TestDatabase(); $this->siteDirectory = $test_db->getTestSitePath(); // Ensure that all code that relies on drupal_valid_test_ua() can still be // safely executed. This primarily affects the (test) site directory // resolution (used by e.g. LocalStream and PhpStorage). $this->databasePrefix = $test_db->getDatabasePrefix(); drupal_valid_test_ua($this->databasePrefix); $settings = array('hash_salt' => get_class($this), 'file_public_path' => $this->siteDirectory . '/files', 'twig_cache' => FALSE); new Settings($settings); $this->setUpFilesystem(); foreach (Database::getAllConnectionInfo() as $key => $targets) { Database::removeConnection($key); } Database::addConnectionInfo('default', 'default', $this->getDatabaseConnectionInfo()['default']); }
/** * 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. * * @see TestBase::prepareEnvironment() * * 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 WebTestBase::curlInitialize() * @see drupal_valid_test_ua() */ private function prepareDatabasePrefix() { $test_db = new TestDatabase(); $this->siteDirectory = $test_db->getTestSitePath(); $this->databasePrefix = $test_db->getDatabasePrefix(); // 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.'); } }