TestSetupTrait.php 6.1 KB

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