annotate core/lib/Drupal/Core/Test/TestSetupTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Test;
Chris@0 4
Chris@0 5 use Drupal\Core\Database\Database;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides a trait for shared test setup functionality.
Chris@0 9 */
Chris@0 10 trait TestSetupTrait {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * An array of config object names that are excluded from schema checking.
Chris@0 14 *
Chris@0 15 * @var string[]
Chris@0 16 */
Chris@0 17 protected static $configSchemaCheckerExclusions = [
Chris@0 18 // Following are used to test lack of or partial schema. Where partial
Chris@0 19 // schema is provided, that is explicitly tested in specific tests.
Chris@0 20 'config_schema_test.noschema',
Chris@0 21 'config_schema_test.someschema',
Chris@0 22 'config_schema_test.schema_data_types',
Chris@0 23 'config_schema_test.no_schema_data_types',
Chris@0 24 // Used to test application of schema to filtering of configuration.
Chris@0 25 'config_test.dynamic.system',
Chris@0 26 ];
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The dependency injection container used in the test.
Chris@0 30 *
Chris@0 31 * @var \Symfony\Component\DependencyInjection\ContainerInterface
Chris@0 32 */
Chris@0 33 protected $container;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The site directory of this test run.
Chris@0 37 *
Chris@0 38 * @var string
Chris@0 39 */
Chris@0 40 protected $siteDirectory = NULL;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * The public file directory for the test environment.
Chris@0 44 *
Chris@0 45 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
Chris@0 46 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
Chris@0 47 *
Chris@0 48 * @var string
Chris@0 49 */
Chris@0 50 protected $publicFilesDirectory;
Chris@0 51
Chris@0 52 /**
Chris@0 53 * The site directory of the original parent site.
Chris@0 54 *
Chris@0 55 * @var string
Chris@0 56 */
Chris@0 57 protected $originalSite;
Chris@0 58
Chris@0 59 /**
Chris@0 60 * The private file directory for the test environment.
Chris@0 61 *
Chris@0 62 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
Chris@0 63 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
Chris@0 64 *
Chris@0 65 * @var string
Chris@0 66 */
Chris@0 67 protected $privateFilesDirectory;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * The original installation profile.
Chris@0 71 *
Chris@0 72 * @var string
Chris@0 73 */
Chris@0 74 protected $originalProfile;
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Set to TRUE to strict check all configuration saved.
Chris@0 78 *
Chris@0 79 * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
Chris@0 80 *
Chris@0 81 * @var bool
Chris@0 82 */
Chris@0 83 protected $strictConfigSchema = TRUE;
Chris@0 84
Chris@0 85 /**
Chris@0 86 * The DrupalKernel instance used in the test.
Chris@0 87 *
Chris@0 88 * @var \Drupal\Core\DrupalKernel
Chris@0 89 */
Chris@0 90 protected $kernel;
Chris@0 91
Chris@0 92 /**
Chris@0 93 * The temporary file directory for the test environment.
Chris@0 94 *
Chris@0 95 * This value has to match the temporary directory created in
Chris@0 96 * install_base_system() for test installs.
Chris@0 97 *
Chris@0 98 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
Chris@0 99 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
Chris@0 100 * @see install_base_system()
Chris@0 101 *
Chris@0 102 * @var string
Chris@0 103 */
Chris@0 104 protected $tempFilesDirectory;
Chris@0 105
Chris@0 106 /**
Chris@0 107 * The test run ID.
Chris@0 108 *
Chris@0 109 * @var string
Chris@0 110 */
Chris@0 111 protected $testId;
Chris@0 112
Chris@0 113 /**
Chris@0 114 * Returns the database connection to the site running Simpletest.
Chris@0 115 *
Chris@0 116 * @return \Drupal\Core\Database\Connection
Chris@0 117 * The database connection to use for inserting assertions.
Chris@0 118 */
Chris@0 119 public static function getDatabaseConnection() {
Chris@0 120 return TestDatabase::getConnection();
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Generates a database prefix for running tests.
Chris@0 125 *
Chris@0 126 * The database prefix is used by prepareEnvironment() to setup a public files
Chris@0 127 * directory for the test to be run, which also contains the PHP error log,
Chris@0 128 * which is written to in case of a fatal error. Since that directory is based
Chris@0 129 * on the database prefix, all tests (even unit tests) need to have one, in
Chris@0 130 * order to access and read the error log.
Chris@0 131 *
Chris@0 132 * The generated database table prefix is used for the Drupal installation
Chris@0 133 * being performed for the test. It is also used as user agent HTTP header
Chris@0 134 * value by the cURL-based browser of WebTestBase, which is sent to the Drupal
Chris@0 135 * installation of the test. During early Drupal bootstrap, the user agent
Chris@0 136 * HTTP header is parsed, and if it matches, all database queries use the
Chris@0 137 * database table prefix that has been generated here.
Chris@0 138 *
Chris@0 139 * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
Chris@0 140 * @see \Drupal\simpletest\WebTestBase::curlInitialize()
Chris@0 141 * @see \Drupal\simpletest\TestBase::prepareEnvironment()
Chris@0 142 * @see drupal_valid_test_ua()
Chris@0 143 */
Chris@0 144 protected function prepareDatabasePrefix() {
Chris@0 145 $test_db = new TestDatabase();
Chris@0 146 $this->siteDirectory = $test_db->getTestSitePath();
Chris@0 147 $this->databasePrefix = $test_db->getDatabasePrefix();
Chris@0 148 }
Chris@0 149
Chris@0 150 /**
Chris@0 151 * Changes the database connection to the prefixed one.
Chris@0 152 */
Chris@0 153 protected function changeDatabasePrefix() {
Chris@0 154 if (empty($this->databasePrefix)) {
Chris@0 155 $this->prepareDatabasePrefix();
Chris@0 156 }
Chris@0 157
Chris@0 158 // If the test is run with argument dburl then use it.
Chris@0 159 $db_url = getenv('SIMPLETEST_DB');
Chris@0 160 if (!empty($db_url)) {
Chris@17 161 // Ensure no existing database gets in the way. If a default database
Chris@17 162 // exists already it must be removed.
Chris@17 163 Database::removeConnection('default');
Chris@0 164 $database = Database::convertDbUrlToConnectionInfo($db_url, isset($this->root) ? $this->root : DRUPAL_ROOT);
Chris@0 165 Database::addConnectionInfo('default', 'default', $database);
Chris@0 166 }
Chris@0 167
Chris@0 168 // Clone the current connection and replace the current prefix.
Chris@0 169 $connection_info = Database::getConnectionInfo('default');
Chris@0 170 if (is_null($connection_info)) {
Chris@0 171 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.');
Chris@0 172 }
Chris@0 173 else {
Chris@0 174 Database::renameConnection('default', 'simpletest_original_default');
Chris@0 175 foreach ($connection_info as $target => $value) {
Chris@0 176 // Replace the full table prefix definition to ensure that no table
Chris@0 177 // prefixes of the test runner leak into the test.
Chris@0 178 $connection_info[$target]['prefix'] = [
Chris@0 179 'default' => $value['prefix']['default'] . $this->databasePrefix,
Chris@0 180 ];
Chris@0 181 }
Chris@0 182 Database::addConnectionInfo('default', 'default', $connection_info['default']);
Chris@0 183 }
Chris@0 184 }
Chris@0 185
Chris@0 186 /**
Chris@0 187 * Gets the config schema exclusions for this test.
Chris@0 188 *
Chris@0 189 * @return string[]
Chris@0 190 * An array of config object names that are excluded from schema checking.
Chris@0 191 */
Chris@0 192 protected function getConfigSchemaExclusions() {
Chris@0 193 $class = get_class($this);
Chris@0 194 $exceptions = [];
Chris@0 195 while ($class) {
Chris@0 196 if (property_exists($class, 'configSchemaCheckerExclusions')) {
Chris@0 197 $exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
Chris@0 198 }
Chris@0 199 $class = get_parent_class($class);
Chris@0 200 }
Chris@0 201 // Filter out any duplicates.
Chris@0 202 return array_unique($exceptions);
Chris@0 203 }
Chris@0 204
Chris@0 205 }