3 namespace Drupal\Core\Test;
5 use Drupal\Core\Database\Database;
8 * Provides a trait for shared test setup functionality.
10 trait TestSetupTrait {
13 * An array of config object names that are excluded from schema checking.
17 protected static $configSchemaCheckerExclusions = [
18 // Following are used to test lack of or partial schema. Where partial
19 // schema is provided, that is explicitly tested in specific tests.
20 'config_schema_test.noschema',
21 'config_schema_test.someschema',
22 'config_schema_test.schema_data_types',
23 'config_schema_test.no_schema_data_types',
24 // Used to test application of schema to filtering of configuration.
25 'config_test.dynamic.system',
29 * The dependency injection container used in the test.
31 * @var \Symfony\Component\DependencyInjection\ContainerInterface
36 * The site directory of this test run.
40 protected $siteDirectory = NULL;
43 * The public file directory for the test environment.
45 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
46 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
50 protected $publicFilesDirectory;
53 * The site directory of the original parent site.
57 protected $originalSite;
60 * The private file directory for the test environment.
62 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
63 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
67 protected $privateFilesDirectory;
70 * The original installation profile.
74 protected $originalProfile;
77 * Set to TRUE to strict check all configuration saved.
79 * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
83 protected $strictConfigSchema = TRUE;
86 * The DrupalKernel instance used in the test.
88 * @var \Drupal\Core\DrupalKernel
93 * The temporary file directory for the test environment.
95 * This value has to match the temporary directory created in
96 * install_base_system() for test installs.
98 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
99 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
100 * @see install_base_system()
104 protected $tempFilesDirectory;
114 * Returns the database connection to the site running Simpletest.
116 * @return \Drupal\Core\Database\Connection
117 * The database connection to use for inserting assertions.
119 public static function getDatabaseConnection() {
120 return TestDatabase::getConnection();
124 * Generates a database prefix for running tests.
126 * The database prefix is used by prepareEnvironment() to setup a public files
127 * directory for the test to be run, which also contains the PHP error log,
128 * which is written to in case of a fatal error. Since that directory is based
129 * on the database prefix, all tests (even unit tests) need to have one, in
130 * order to access and read the error log.
132 * The generated database table prefix is used for the Drupal installation
133 * being performed for the test. It is also used as user agent HTTP header
134 * value by the cURL-based browser of WebTestBase, which is sent to the Drupal
135 * installation of the test. During early Drupal bootstrap, the user agent
136 * HTTP header is parsed, and if it matches, all database queries use the
137 * database table prefix that has been generated here.
139 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
140 * @see \Drupal\simpletest\WebTestBase::curlInitialize()
141 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
142 * @see drupal_valid_test_ua()
144 protected function prepareDatabasePrefix() {
145 $test_db = new TestDatabase();
146 $this->siteDirectory = $test_db->getTestSitePath();
147 $this->databasePrefix = $test_db->getDatabasePrefix();
151 * Changes the database connection to the prefixed one.
153 protected function changeDatabasePrefix() {
154 if (empty($this->databasePrefix)) {
155 $this->prepareDatabasePrefix();
158 // If the test is run with argument dburl then use it.
159 $db_url = getenv('SIMPLETEST_DB');
160 if (!empty($db_url)) {
161 $database = Database::convertDbUrlToConnectionInfo($db_url, isset($this->root) ? $this->root : DRUPAL_ROOT);
162 Database::addConnectionInfo('default', 'default', $database);
165 // Clone the current connection and replace the current prefix.
166 $connection_info = Database::getConnectionInfo('default');
167 if (is_null($connection_info)) {
168 throw new \InvalidArgumentException('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh.');
171 Database::renameConnection('default', 'simpletest_original_default');
172 foreach ($connection_info as $target => $value) {
173 // Replace the full table prefix definition to ensure that no table
174 // prefixes of the test runner leak into the test.
175 $connection_info[$target]['prefix'] = [
176 'default' => $value['prefix']['default'] . $this->databasePrefix,
179 Database::addConnectionInfo('default', 'default', $connection_info['default']);
184 * Gets the config schema exclusions for this test.
187 * An array of config object names that are excluded from schema checking.
189 protected function getConfigSchemaExclusions() {
190 $class = get_class($this);
193 if (property_exists($class, 'configSchemaCheckerExclusions')) {
194 $exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
196 $class = get_parent_class($class);
198 // Filter out any duplicates.
199 return array_unique($exceptions);