annotate core/tests/Drupal/KernelTests/KernelTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\KernelTests;
Chris@0 4
Chris@0 5 use Drupal\Component\FileCache\ApcuFileCacheBackend;
Chris@0 6 use Drupal\Component\FileCache\FileCache;
Chris@0 7 use Drupal\Component\FileCache\FileCacheFactory;
Chris@0 8 use Drupal\Component\Utility\Html;
Chris@17 9 use Drupal\Component\Render\FormattableMarkup;
Chris@0 10 use Drupal\Core\Config\Development\ConfigSchemaChecker;
Chris@0 11 use Drupal\Core\Database\Database;
Chris@0 12 use Drupal\Core\DependencyInjection\ContainerBuilder;
Chris@0 13 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
Chris@0 14 use Drupal\Core\DrupalKernel;
Chris@0 15 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
Chris@0 16 use Drupal\Core\Extension\ExtensionDiscovery;
Chris@0 17 use Drupal\Core\Language\Language;
Chris@0 18 use Drupal\Core\Site\Settings;
Chris@0 19 use Drupal\Core\Test\TestDatabase;
Chris@0 20 use Drupal\Tests\AssertHelperTrait;
Chris@0 21 use Drupal\Tests\ConfigTestTrait;
Chris@12 22 use Drupal\Tests\PhpunitCompatibilityTrait;
Chris@0 23 use Drupal\Tests\RandomGeneratorTrait;
Chris@0 24 use Drupal\Tests\TestRequirementsTrait;
Chris@0 25 use PHPUnit\Framework\TestCase;
Chris@0 26 use Symfony\Component\DependencyInjection\Reference;
Chris@0 27 use Symfony\Component\HttpFoundation\Request;
Chris@0 28 use org\bovigo\vfs\vfsStream;
Chris@0 29 use org\bovigo\vfs\visitor\vfsStreamPrintVisitor;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Base class for functional integration tests.
Chris@0 33 *
Chris@0 34 * This base class should be useful for testing some types of integrations which
Chris@0 35 * don't require the overhead of a fully-installed Drupal instance, but which
Chris@0 36 * have many dependencies on parts of Drupal which can't or shouldn't be mocked.
Chris@0 37 *
Chris@0 38 * This base class partially boots a fixture Drupal. The state of the fixture
Chris@0 39 * Drupal is comparable to the state of a system during the early part of the
Chris@0 40 * installation process.
Chris@0 41 *
Chris@0 42 * Tests extending this base class can access services and the database, but the
Chris@0 43 * system is initially empty. This Drupal runs in a minimal mocked filesystem
Chris@0 44 * which operates within vfsStream.
Chris@0 45 *
Chris@0 46 * Modules specified in the $modules property are added to the service container
Chris@0 47 * for each test. The module/hook system is functional. Additional modules
Chris@0 48 * needed in a test should override $modules. Modules specified in this way will
Chris@0 49 * be added to those specified in superclasses.
Chris@0 50 *
Chris@0 51 * Unlike \Drupal\Tests\BrowserTestBase, the modules are not installed. They are
Chris@0 52 * loaded such that their services and hooks are available, but the install
Chris@0 53 * process has not been performed.
Chris@0 54 *
Chris@0 55 * Other modules can be made available in this way using
Chris@0 56 * KernelTestBase::enableModules().
Chris@0 57 *
Chris@0 58 * Some modules can be brought into a fully-installed state using
Chris@0 59 * KernelTestBase::installConfig(), KernelTestBase::installSchema(), and
Chris@0 60 * KernelTestBase::installEntitySchema(). Alternately, tests which need modules
Chris@0 61 * to be fully installed could inherit from \Drupal\Tests\BrowserTestBase.
Chris@0 62 *
Chris@0 63 * @see \Drupal\Tests\KernelTestBase::$modules
Chris@0 64 * @see \Drupal\Tests\KernelTestBase::enableModules()
Chris@0 65 * @see \Drupal\Tests\KernelTestBase::installConfig()
Chris@0 66 * @see \Drupal\Tests\KernelTestBase::installEntitySchema()
Chris@0 67 * @see \Drupal\Tests\KernelTestBase::installSchema()
Chris@0 68 * @see \Drupal\Tests\BrowserTestBase
Chris@18 69 *
Chris@18 70 * @ingroup testing
Chris@0 71 */
Chris@0 72 abstract class KernelTestBase extends TestCase implements ServiceProviderInterface {
Chris@0 73
Chris@0 74 use AssertLegacyTrait;
Chris@0 75 use AssertContentTrait;
Chris@0 76 use AssertHelperTrait;
Chris@0 77 use RandomGeneratorTrait;
Chris@0 78 use ConfigTestTrait;
Chris@0 79 use TestRequirementsTrait;
Chris@12 80 use PhpunitCompatibilityTrait;
Chris@0 81
Chris@0 82 /**
Chris@0 83 * {@inheritdoc}
Chris@0 84 *
Chris@0 85 * Back up and restore any global variables that may be changed by tests.
Chris@0 86 *
Chris@0 87 * @see self::runTestInSeparateProcess
Chris@0 88 */
Chris@0 89 protected $backupGlobals = TRUE;
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 *
Chris@0 94 * Kernel tests are run in separate processes because they allow autoloading
Chris@0 95 * of code from extensions. Running the test in a separate process isolates
Chris@0 96 * this behavior from other tests. Subclasses should not override this
Chris@0 97 * property.
Chris@0 98 */
Chris@0 99 protected $runTestInSeparateProcess = TRUE;
Chris@0 100
Chris@0 101 /**
Chris@0 102 * {@inheritdoc}
Chris@0 103 *
Chris@0 104 * Back up and restore static class properties that may be changed by tests.
Chris@0 105 *
Chris@0 106 * @see self::runTestInSeparateProcess
Chris@0 107 */
Chris@0 108 protected $backupStaticAttributes = TRUE;
Chris@0 109
Chris@0 110 /**
Chris@0 111 * {@inheritdoc}
Chris@0 112 *
Chris@0 113 * Contains a few static class properties for performance.
Chris@0 114 */
Chris@0 115 protected $backupStaticAttributesBlacklist = [
Chris@0 116 // Ignore static discovery/parser caches to speed up tests.
Chris@0 117 'Drupal\Component\Discovery\YamlDiscovery' => ['parsedFiles'],
Chris@0 118 'Drupal\Core\DependencyInjection\YamlFileLoader' => ['yaml'],
Chris@0 119 'Drupal\Core\Extension\ExtensionDiscovery' => ['files'],
Chris@0 120 'Drupal\Core\Extension\InfoParser' => ['parsedInfos'],
Chris@0 121 // Drupal::$container cannot be serialized.
Chris@0 122 'Drupal' => ['container'],
Chris@0 123 // Settings cannot be serialized.
Chris@0 124 'Drupal\Core\Site\Settings' => ['instance'],
Chris@0 125 ];
Chris@0 126
Chris@0 127 /**
Chris@0 128 * {@inheritdoc}
Chris@0 129 *
Chris@0 130 * Do not forward any global state from the parent process to the processes
Chris@0 131 * that run the actual tests.
Chris@0 132 *
Chris@0 133 * @see self::runTestInSeparateProcess
Chris@0 134 */
Chris@0 135 protected $preserveGlobalState = FALSE;
Chris@0 136
Chris@0 137 /**
Chris@0 138 * @var \Composer\Autoload\Classloader
Chris@0 139 */
Chris@0 140 protected $classLoader;
Chris@0 141
Chris@0 142 /**
Chris@0 143 * @var string
Chris@0 144 */
Chris@0 145 protected $siteDirectory;
Chris@0 146
Chris@0 147 /**
Chris@0 148 * @var string
Chris@0 149 */
Chris@0 150 protected $databasePrefix;
Chris@0 151
Chris@0 152 /**
Chris@0 153 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
Chris@0 154 */
Chris@0 155 protected $container;
Chris@0 156
Chris@0 157 /**
Chris@0 158 * Modules to enable.
Chris@0 159 *
Chris@0 160 * The test runner will merge the $modules lists from this class, the class
Chris@0 161 * it extends, and so on up the class hierarchy. It is not necessary to
Chris@0 162 * include modules in your list that a parent class has already declared.
Chris@0 163 *
Chris@0 164 * @see \Drupal\Tests\KernelTestBase::enableModules()
Chris@0 165 * @see \Drupal\Tests\KernelTestBase::bootKernel()
Chris@0 166 *
Chris@0 167 * @var array
Chris@0 168 */
Chris@0 169 protected static $modules = [];
Chris@0 170
Chris@0 171 /**
Chris@0 172 * The virtual filesystem root directory.
Chris@0 173 *
Chris@0 174 * @var \org\bovigo\vfs\vfsStreamDirectory
Chris@0 175 */
Chris@0 176 protected $vfsRoot;
Chris@0 177
Chris@0 178 /**
Chris@0 179 * @todo Move into Config test base class.
Chris@0 180 * @var \Drupal\Core\Config\ConfigImporter
Chris@0 181 */
Chris@0 182 protected $configImporter;
Chris@0 183
Chris@0 184 /**
Chris@0 185 * The app root.
Chris@0 186 *
Chris@0 187 * @var string
Chris@0 188 */
Chris@0 189 protected $root;
Chris@0 190
Chris@0 191 /**
Chris@0 192 * Set to TRUE to strict check all configuration saved.
Chris@0 193 *
Chris@0 194 * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
Chris@0 195 *
Chris@0 196 * @var bool
Chris@0 197 */
Chris@0 198 protected $strictConfigSchema = TRUE;
Chris@0 199
Chris@0 200 /**
Chris@0 201 * An array of config object names that are excluded from schema checking.
Chris@0 202 *
Chris@0 203 * @var string[]
Chris@0 204 */
Chris@0 205 protected static $configSchemaCheckerExclusions = [
Chris@0 206 // Following are used to test lack of or partial schema. Where partial
Chris@0 207 // schema is provided, that is explicitly tested in specific tests.
Chris@0 208 'config_schema_test.noschema',
Chris@0 209 'config_schema_test.someschema',
Chris@0 210 'config_schema_test.schema_data_types',
Chris@0 211 'config_schema_test.no_schema_data_types',
Chris@0 212 // Used to test application of schema to filtering of configuration.
Chris@0 213 'config_test.dynamic.system',
Chris@0 214 ];
Chris@0 215
Chris@0 216 /**
Chris@0 217 * {@inheritdoc}
Chris@0 218 */
Chris@0 219 public static function setUpBeforeClass() {
Chris@0 220 parent::setUpBeforeClass();
Chris@0 221
Chris@0 222 // Change the current dir to DRUPAL_ROOT.
Chris@0 223 chdir(static::getDrupalRoot());
Chris@0 224 }
Chris@0 225
Chris@0 226 /**
Chris@0 227 * {@inheritdoc}
Chris@0 228 */
Chris@0 229 protected function setUp() {
Chris@0 230 parent::setUp();
Chris@0 231
Chris@0 232 $this->root = static::getDrupalRoot();
Chris@0 233 $this->initFileCache();
Chris@0 234 $this->bootEnvironment();
Chris@0 235 $this->bootKernel();
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * Bootstraps a basic test environment.
Chris@0 240 *
Chris@0 241 * Should not be called by tests. Only visible for DrupalKernel integration
Chris@0 242 * tests.
Chris@0 243 *
Chris@14 244 * @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
Chris@0 245 * @internal
Chris@0 246 */
Chris@0 247 protected function bootEnvironment() {
Chris@0 248 $this->streamWrappers = [];
Chris@0 249 \Drupal::unsetContainer();
Chris@0 250
Chris@0 251 $this->classLoader = require $this->root . '/autoload.php';
Chris@0 252
Chris@0 253 require_once $this->root . '/core/includes/bootstrap.inc';
Chris@0 254
Chris@0 255 // Set up virtual filesystem.
Chris@0 256 Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
Chris@0 257 $test_db = new TestDatabase();
Chris@0 258 $this->siteDirectory = $test_db->getTestSitePath();
Chris@0 259
Chris@0 260 // Ensure that all code that relies on drupal_valid_test_ua() can still be
Chris@0 261 // safely executed. This primarily affects the (test) site directory
Chris@0 262 // resolution (used by e.g. LocalStream and PhpStorage).
Chris@0 263 $this->databasePrefix = $test_db->getDatabasePrefix();
Chris@0 264 drupal_valid_test_ua($this->databasePrefix);
Chris@0 265
Chris@0 266 $settings = [
Chris@0 267 'hash_salt' => get_class($this),
Chris@0 268 'file_public_path' => $this->siteDirectory . '/files',
Chris@0 269 // Disable Twig template caching/dumping.
Chris@0 270 'twig_cache' => FALSE,
Chris@0 271 // @see \Drupal\KernelTests\KernelTestBase::register()
Chris@0 272 ];
Chris@0 273 new Settings($settings);
Chris@0 274
Chris@0 275 $this->setUpFilesystem();
Chris@0 276
Chris@0 277 foreach (Database::getAllConnectionInfo() as $key => $targets) {
Chris@0 278 Database::removeConnection($key);
Chris@0 279 }
Chris@0 280 Database::addConnectionInfo('default', 'default', $this->getDatabaseConnectionInfo()['default']);
Chris@0 281 }
Chris@0 282
Chris@0 283 /**
Chris@0 284 * Sets up the filesystem, so things like the file directory.
Chris@0 285 */
Chris@0 286 protected function setUpFilesystem() {
Chris@0 287 $test_db = new TestDatabase($this->databasePrefix);
Chris@0 288 $test_site_path = $test_db->getTestSitePath();
Chris@0 289
Chris@0 290 $this->vfsRoot = vfsStream::setup('root');
Chris@0 291 $this->vfsRoot->addChild(vfsStream::newDirectory($test_site_path));
Chris@0 292 $this->siteDirectory = vfsStream::url('root/' . $test_site_path);
Chris@0 293
Chris@0 294 mkdir($this->siteDirectory . '/files', 0775);
Chris@0 295 mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
Chris@0 296
Chris@0 297 $settings = Settings::getInstance() ? Settings::getAll() : [];
Chris@0 298 $settings['file_public_path'] = $this->siteDirectory . '/files';
Chris@0 299 new Settings($settings);
Chris@0 300
Chris@0 301 $GLOBALS['config_directories'] = [
Chris@0 302 CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
Chris@0 303 ];
Chris@0 304 }
Chris@0 305
Chris@0 306 /**
Chris@0 307 * @return string
Chris@0 308 */
Chris@0 309 public function getDatabasePrefix() {
Chris@0 310 return $this->databasePrefix;
Chris@0 311 }
Chris@0 312
Chris@0 313 /**
Chris@0 314 * Bootstraps a kernel for a test.
Chris@0 315 */
Chris@0 316 private function bootKernel() {
Chris@0 317 $this->setSetting('container_yamls', []);
Chris@0 318 // Allow for test-specific overrides.
Chris@12 319 $settings_services_file = $this->root . '/sites/default/testing.services.yml';
Chris@0 320 if (file_exists($settings_services_file)) {
Chris@0 321 // Copy the testing-specific service overrides in place.
Chris@12 322 $testing_services_file = $this->siteDirectory . '/services.yml';
Chris@0 323 copy($settings_services_file, $testing_services_file);
Chris@0 324 $this->setSetting('container_yamls', [$testing_services_file]);
Chris@0 325 }
Chris@0 326
Chris@0 327 // Allow for global test environment overrides.
Chris@0 328 if (file_exists($test_env = $this->root . '/sites/default/testing.services.yml')) {
Chris@0 329 $GLOBALS['conf']['container_yamls']['testing'] = $test_env;
Chris@0 330 }
Chris@0 331 // Add this test class as a service provider.
Chris@0 332 $GLOBALS['conf']['container_service_providers']['test'] = $this;
Chris@0 333
Chris@0 334 $modules = self::getModulesToEnable(get_class($this));
Chris@0 335
Chris@0 336 // Bootstrap the kernel. Do not use createFromRequest() to retain Settings.
Chris@0 337 $kernel = new DrupalKernel('testing', $this->classLoader, FALSE);
Chris@0 338 $kernel->setSitePath($this->siteDirectory);
Chris@0 339 // Boot a new one-time container from scratch. Ensure to set the module list
Chris@0 340 // upfront to avoid a subsequent rebuild.
Chris@0 341 if ($modules && $extensions = $this->getExtensionsForModules($modules)) {
Chris@0 342 $kernel->updateModules($extensions, $extensions);
Chris@0 343 }
Chris@0 344 // DrupalKernel::boot() is not sufficient as it does not invoke preHandle(),
Chris@0 345 // which is required to initialize legacy global variables.
Chris@0 346 $request = Request::create('/');
Chris@0 347 $kernel->prepareLegacyRequest($request);
Chris@0 348
Chris@0 349 // register() is only called if a new container was built/compiled.
Chris@0 350 $this->container = $kernel->getContainer();
Chris@0 351
Chris@0 352 // Ensure database tasks have been run.
Chris@0 353 require_once __DIR__ . '/../../../includes/install.inc';
Chris@0 354 $connection = Database::getConnection();
Chris@0 355 $errors = db_installer_object($connection->driver())->runTasks();
Chris@0 356 if (!empty($errors)) {
Chris@0 357 $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
Chris@0 358 }
Chris@0 359
Chris@0 360 if ($modules) {
Chris@0 361 $this->container->get('module_handler')->loadAll();
Chris@0 362 }
Chris@0 363
Chris@0 364 $this->container->get('request_stack')->push($request);
Chris@0 365
Chris@0 366 // Setup the destion to the be frontpage by default.
Chris@0 367 \Drupal::destination()->set('/');
Chris@0 368
Chris@0 369 // Write the core.extension configuration.
Chris@0 370 // Required for ConfigInstaller::installDefaultConfig() to work.
Chris@0 371 $this->container->get('config.storage')->write('core.extension', [
Chris@0 372 'module' => array_fill_keys($modules, 0),
Chris@0 373 'theme' => [],
Chris@0 374 'profile' => '',
Chris@0 375 ]);
Chris@0 376
Chris@0 377 $settings = Settings::getAll();
Chris@0 378 $settings['php_storage']['default'] = [
Chris@0 379 'class' => '\Drupal\Component\PhpStorage\FileStorage',
Chris@0 380 ];
Chris@0 381 new Settings($settings);
Chris@0 382
Chris@0 383 // Manually configure the test mail collector implementation to prevent
Chris@0 384 // tests from sending out emails and collect them in state instead.
Chris@0 385 // While this should be enforced via settings.php prior to installation,
Chris@0 386 // some tests expect to be able to test mail system implementations.
Chris@0 387 $GLOBALS['config']['system.mail']['interface']['default'] = 'test_mail_collector';
Chris@0 388
Chris@0 389 // Manually configure the default file scheme so that modules that use file
Chris@0 390 // functions don't have to install system and its configuration.
Chris@0 391 // @see file_default_scheme()
Chris@0 392 $GLOBALS['config']['system.file']['default_scheme'] = 'public';
Chris@0 393 }
Chris@0 394
Chris@0 395 /**
Chris@0 396 * Configuration accessor for tests. Returns non-overridden configuration.
Chris@0 397 *
Chris@0 398 * @param string $name
Chris@0 399 * The configuration name.
Chris@0 400 *
Chris@0 401 * @return \Drupal\Core\Config\Config
Chris@0 402 * The configuration object with original configuration data.
Chris@0 403 */
Chris@0 404 protected function config($name) {
Chris@0 405 return $this->container->get('config.factory')->getEditable($name);
Chris@0 406 }
Chris@0 407
Chris@0 408 /**
Chris@0 409 * Returns the Database connection info to be used for this test.
Chris@0 410 *
Chris@0 411 * This method only exists for tests of the Database component itself, because
Chris@0 412 * they require multiple database connections. Each SQLite :memory: connection
Chris@0 413 * creates a new/separate database in memory. A shared-memory SQLite file URI
Chris@0 414 * triggers PHP open_basedir/allow_url_fopen/allow_url_include restrictions.
Chris@0 415 * Due to that, Database tests are running against a SQLite database that is
Chris@0 416 * located in an actual file in the system's temporary directory.
Chris@0 417 *
Chris@0 418 * Other tests should not override this method.
Chris@0 419 *
Chris@0 420 * @return array
Chris@0 421 * A Database connection info array.
Chris@0 422 *
Chris@0 423 * @internal
Chris@0 424 */
Chris@0 425 protected function getDatabaseConnectionInfo() {
Chris@0 426 // If the test is run with argument dburl then use it.
Chris@0 427 $db_url = getenv('SIMPLETEST_DB');
Chris@0 428 if (empty($db_url)) {
Chris@0 429 throw new \Exception('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. See https://www.drupal.org/node/2116263#skipped-tests for more information.');
Chris@0 430 }
Chris@0 431 else {
Chris@0 432 $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root);
Chris@0 433 Database::addConnectionInfo('default', 'default', $database);
Chris@0 434 }
Chris@0 435
Chris@0 436 // Clone the current connection and replace the current prefix.
Chris@0 437 $connection_info = Database::getConnectionInfo('default');
Chris@0 438 if (!empty($connection_info)) {
Chris@0 439 Database::renameConnection('default', 'simpletest_original_default');
Chris@0 440 foreach ($connection_info as $target => $value) {
Chris@0 441 // Replace the full table prefix definition to ensure that no table
Chris@0 442 // prefixes of the test runner leak into the test.
Chris@0 443 $connection_info[$target]['prefix'] = [
Chris@17 444 'default' => $this->databasePrefix,
Chris@0 445 ];
Chris@0 446 }
Chris@0 447 }
Chris@0 448 return $connection_info;
Chris@0 449 }
Chris@0 450
Chris@0 451 /**
Chris@0 452 * Initializes the FileCache component.
Chris@0 453 *
Chris@0 454 * We can not use the Settings object in a component, that's why we have to do
Chris@0 455 * it here instead of \Drupal\Component\FileCache\FileCacheFactory.
Chris@0 456 */
Chris@0 457 protected function initFileCache() {
Chris@0 458 $configuration = Settings::get('file_cache');
Chris@0 459
Chris@0 460 // Provide a default configuration, if not set.
Chris@0 461 if (!isset($configuration['default'])) {
Chris@0 462 // @todo Use extension_loaded('apcu') for non-testbot
Chris@0 463 // https://www.drupal.org/node/2447753.
Chris@0 464 if (function_exists('apcu_fetch')) {
Chris@0 465 $configuration['default']['cache_backend_class'] = ApcuFileCacheBackend::class;
Chris@0 466 }
Chris@0 467 }
Chris@0 468 FileCacheFactory::setConfiguration($configuration);
Chris@0 469 FileCacheFactory::setPrefix(Settings::getApcuPrefix('file_cache', $this->root));
Chris@0 470 }
Chris@0 471
Chris@0 472 /**
Chris@0 473 * Returns Extension objects for $modules to enable.
Chris@0 474 *
Chris@0 475 * @param string[] $modules
Chris@0 476 * The list of modules to enable.
Chris@0 477 *
Chris@0 478 * @return \Drupal\Core\Extension\Extension[]
Chris@0 479 * Extension objects for $modules, keyed by module name.
Chris@0 480 *
Chris@0 481 * @throws \PHPUnit_Framework_Exception
Chris@0 482 * If a module is not available.
Chris@0 483 *
Chris@0 484 * @see \Drupal\Tests\KernelTestBase::enableModules()
Chris@0 485 * @see \Drupal\Core\Extension\ModuleHandler::add()
Chris@0 486 */
Chris@0 487 private function getExtensionsForModules(array $modules) {
Chris@0 488 $extensions = [];
Chris@0 489 $discovery = new ExtensionDiscovery($this->root);
Chris@0 490 $discovery->setProfileDirectories([]);
Chris@0 491 $list = $discovery->scan('module');
Chris@0 492 foreach ($modules as $name) {
Chris@0 493 if (!isset($list[$name])) {
Chris@0 494 throw new \PHPUnit_Framework_Exception("Unavailable module: '$name'. If this module needs to be downloaded separately, annotate the test class with '@requires module $name'.");
Chris@0 495 }
Chris@0 496 $extensions[$name] = $list[$name];
Chris@0 497 }
Chris@0 498 return $extensions;
Chris@0 499 }
Chris@0 500
Chris@0 501 /**
Chris@0 502 * Registers test-specific services.
Chris@0 503 *
Chris@0 504 * Extend this method in your test to register additional services. This
Chris@0 505 * method is called whenever the kernel is rebuilt.
Chris@0 506 *
Chris@0 507 * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
Chris@0 508 * The service container to enhance.
Chris@0 509 *
Chris@0 510 * @see \Drupal\Tests\KernelTestBase::bootKernel()
Chris@0 511 */
Chris@0 512 public function register(ContainerBuilder $container) {
Chris@0 513 // Keep the container object around for tests.
Chris@0 514 $this->container = $container;
Chris@0 515
Chris@0 516 $container
Chris@0 517 ->register('flood', 'Drupal\Core\Flood\MemoryBackend')
Chris@0 518 ->addArgument(new Reference('request_stack'));
Chris@0 519 $container
Chris@0 520 ->register('lock', 'Drupal\Core\Lock\NullLockBackend');
Chris@0 521 $container
Chris@0 522 ->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');
Chris@0 523 $container
Chris@0 524 ->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory')
Chris@0 525 // Must persist container rebuilds, or all data would vanish otherwise.
Chris@0 526 ->addTag('persist');
Chris@0 527 $container
Chris@0 528 ->setAlias('keyvalue', 'keyvalue.memory');
Chris@0 529
Chris@0 530 // Set the default language on the minimal container.
Chris@0 531 $container->setParameter('language.default_values', Language::$defaultValues);
Chris@0 532
Chris@0 533 if ($this->strictConfigSchema) {
Chris@0 534 $container
Chris@0 535 ->register('simpletest.config_schema_checker', ConfigSchemaChecker::class)
Chris@0 536 ->addArgument(new Reference('config.typed'))
Chris@0 537 ->addArgument($this->getConfigSchemaExclusions())
Chris@0 538 ->addTag('event_subscriber');
Chris@0 539 }
Chris@0 540
Chris@0 541 if ($container->hasDefinition('path_processor_alias')) {
Chris@0 542 // Prevent the alias-based path processor, which requires a url_alias db
Chris@0 543 // table, from being registered to the path processor manager. We do this
Chris@0 544 // by removing the tags that the compiler pass looks for. This means the
Chris@0 545 // url generator can safely be used within tests.
Chris@0 546 $container->getDefinition('path_processor_alias')
Chris@0 547 ->clearTag('path_processor_inbound')
Chris@0 548 ->clearTag('path_processor_outbound');
Chris@0 549 }
Chris@0 550
Chris@0 551 if ($container->hasDefinition('password')) {
Chris@0 552 $container->getDefinition('password')
Chris@0 553 ->setArguments([1]);
Chris@0 554 }
Chris@0 555 TestServiceProvider::addRouteProvider($container);
Chris@0 556 }
Chris@0 557
Chris@0 558 /**
Chris@0 559 * Gets the config schema exclusions for this test.
Chris@0 560 *
Chris@0 561 * @return string[]
Chris@0 562 * An array of config object names that are excluded from schema checking.
Chris@0 563 */
Chris@0 564 protected function getConfigSchemaExclusions() {
Chris@0 565 $class = get_class($this);
Chris@0 566 $exceptions = [];
Chris@0 567 while ($class) {
Chris@0 568 if (property_exists($class, 'configSchemaCheckerExclusions')) {
Chris@0 569 $exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
Chris@0 570 }
Chris@0 571 $class = get_parent_class($class);
Chris@0 572 }
Chris@0 573 // Filter out any duplicates.
Chris@0 574 return array_unique($exceptions);
Chris@0 575 }
Chris@0 576
Chris@0 577 /**
Chris@0 578 * {@inheritdoc}
Chris@0 579 */
Chris@0 580 protected function assertPostConditions() {
Chris@0 581 // Execute registered Drupal shutdown functions prior to tearing down.
Chris@0 582 // @see _drupal_shutdown_function()
Chris@0 583 $callbacks = &drupal_register_shutdown_function();
Chris@0 584 while ($callback = array_shift($callbacks)) {
Chris@0 585 call_user_func_array($callback['callback'], $callback['arguments']);
Chris@0 586 }
Chris@0 587
Chris@0 588 // Shut down the kernel (if bootKernel() was called).
Chris@0 589 // @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
Chris@0 590 if ($this->container) {
Chris@0 591 $this->container->get('kernel')->shutdown();
Chris@0 592 }
Chris@0 593
Chris@0 594 // Fail in case any (new) shutdown functions exist.
Chris@0 595 $this->assertCount(0, drupal_register_shutdown_function(), 'Unexpected Drupal shutdown callbacks exist after running shutdown functions.');
Chris@0 596
Chris@0 597 parent::assertPostConditions();
Chris@0 598 }
Chris@0 599
Chris@0 600 /**
Chris@0 601 * {@inheritdoc}
Chris@0 602 */
Chris@0 603 protected function tearDown() {
Chris@0 604 // Destroy the testing kernel.
Chris@0 605 if (isset($this->kernel)) {
Chris@0 606 $this->kernel->shutdown();
Chris@0 607 }
Chris@0 608
Chris@0 609 // Remove all prefixed tables.
Chris@0 610 $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
Chris@0 611 $original_prefix = $original_connection_info['default']['prefix']['default'];
Chris@0 612 $test_connection_info = Database::getConnectionInfo('default');
Chris@0 613 $test_prefix = $test_connection_info['default']['prefix']['default'];
Chris@0 614 if ($original_prefix != $test_prefix) {
Chris@0 615 $tables = Database::getConnection()->schema()->findTables('%');
Chris@0 616 foreach ($tables as $table) {
Chris@0 617 if (Database::getConnection()->schema()->dropTable($table)) {
Chris@0 618 unset($tables[$table]);
Chris@0 619 }
Chris@0 620 }
Chris@0 621 }
Chris@0 622
Chris@0 623 // Free up memory: Own properties.
Chris@0 624 $this->classLoader = NULL;
Chris@0 625 $this->vfsRoot = NULL;
Chris@0 626 $this->configImporter = NULL;
Chris@0 627
Chris@0 628 // Free up memory: Custom test class properties.
Chris@0 629 // Note: Private properties cannot be cleaned up.
Chris@0 630 $rc = new \ReflectionClass(__CLASS__);
Chris@0 631 $blacklist = [];
Chris@0 632 foreach ($rc->getProperties() as $property) {
Chris@0 633 $blacklist[$property->name] = $property->getDeclaringClass()->name;
Chris@0 634 }
Chris@0 635 $rc = new \ReflectionClass($this);
Chris@0 636 foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $property) {
Chris@0 637 if (!$property->isStatic() && !isset($blacklist[$property->name])) {
Chris@0 638 $this->{$property->name} = NULL;
Chris@0 639 }
Chris@0 640 }
Chris@0 641
Chris@0 642 // Clean FileCache cache.
Chris@0 643 FileCache::reset();
Chris@0 644
Chris@0 645 // Clean up statics, container, and settings.
Chris@0 646 if (function_exists('drupal_static_reset')) {
Chris@0 647 drupal_static_reset();
Chris@0 648 }
Chris@0 649 \Drupal::unsetContainer();
Chris@0 650 $this->container = NULL;
Chris@0 651 new Settings([]);
Chris@0 652
Chris@0 653 parent::tearDown();
Chris@0 654 }
Chris@0 655
Chris@0 656 /**
Chris@0 657 * @after
Chris@0 658 *
Chris@0 659 * Additional tear down method to close the connection at the end.
Chris@0 660 */
Chris@0 661 public function tearDownCloseDatabaseConnection() {
Chris@0 662 // Destroy the database connection, which for example removes the memory
Chris@0 663 // from sqlite in memory.
Chris@0 664 foreach (Database::getAllConnectionInfo() as $key => $targets) {
Chris@0 665 Database::removeConnection($key);
Chris@0 666 }
Chris@0 667 }
Chris@0 668
Chris@0 669 /**
Chris@0 670 * Installs default configuration for a given list of modules.
Chris@0 671 *
Chris@0 672 * @param string|string[] $modules
Chris@0 673 * A module or list of modules for which to install default configuration.
Chris@0 674 *
Chris@0 675 * @throws \LogicException
Chris@0 676 * If any module in $modules is not enabled.
Chris@0 677 */
Chris@0 678 protected function installConfig($modules) {
Chris@0 679 foreach ((array) $modules as $module) {
Chris@0 680 if (!$this->container->get('module_handler')->moduleExists($module)) {
Chris@0 681 throw new \LogicException("$module module is not enabled.");
Chris@0 682 }
Chris@0 683 $this->container->get('config.installer')->installDefaultConfig('module', $module);
Chris@0 684 }
Chris@0 685 }
Chris@0 686
Chris@0 687 /**
Chris@0 688 * Installs database tables from a module schema definition.
Chris@0 689 *
Chris@0 690 * @param string $module
Chris@0 691 * The name of the module that defines the table's schema.
Chris@0 692 * @param string|array $tables
Chris@0 693 * The name or an array of the names of the tables to install.
Chris@0 694 *
Chris@0 695 * @throws \LogicException
Chris@0 696 * If $module is not enabled or the table schema cannot be found.
Chris@0 697 */
Chris@0 698 protected function installSchema($module, $tables) {
Chris@0 699 // drupal_get_module_schema() is technically able to install a schema
Chris@0 700 // of a non-enabled module, but its ability to load the module's .install
Chris@0 701 // file depends on many other factors. To prevent differences in test
Chris@0 702 // behavior and non-reproducible test failures, we only allow the schema of
Chris@0 703 // explicitly loaded/enabled modules to be installed.
Chris@0 704 if (!$this->container->get('module_handler')->moduleExists($module)) {
Chris@0 705 throw new \LogicException("$module module is not enabled.");
Chris@0 706 }
Chris@0 707 $tables = (array) $tables;
Chris@0 708 foreach ($tables as $table) {
Chris@0 709 $schema = drupal_get_module_schema($module, $table);
Chris@0 710 if (empty($schema)) {
Chris@0 711 // BC layer to avoid some contrib tests to fail.
Chris@0 712 if ($module == 'system') {
Chris@18 713 @trigger_error('Special handling of system module schemas in \Drupal\KernelTests\KernelTestBase::installSchema has been deprecated in Drupal 8.7.x, remove any calls to this method that use invalid schema names. See https://www.drupal.org/project/drupal/issues/2794347.', E_USER_DEPRECATED);
Chris@0 714 continue;
Chris@0 715 }
Chris@0 716 throw new \LogicException("$module module does not define a schema for table '$table'.");
Chris@0 717 }
Chris@0 718 $this->container->get('database')->schema()->createTable($table, $schema);
Chris@0 719 }
Chris@0 720 }
Chris@0 721
Chris@0 722 /**
Chris@0 723 * Installs the storage schema for a specific entity type.
Chris@0 724 *
Chris@0 725 * @param string $entity_type_id
Chris@0 726 * The ID of the entity type.
Chris@0 727 */
Chris@0 728 protected function installEntitySchema($entity_type_id) {
Chris@18 729 $entity_type_manager = \Drupal::entityTypeManager();
Chris@18 730 $entity_type = $entity_type_manager->getDefinition($entity_type_id);
Chris@18 731 \Drupal::service('entity_type.listener')->onEntityTypeCreate($entity_type);
Chris@0 732
Chris@0 733 // For test runs, the most common storage backend is a SQL database. For
Chris@0 734 // this case, ensure the tables got created.
Chris@18 735 $storage = $entity_type_manager->getStorage($entity_type_id);
Chris@0 736 if ($storage instanceof SqlEntityStorageInterface) {
Chris@0 737 $tables = $storage->getTableMapping()->getTableNames();
Chris@0 738 $db_schema = $this->container->get('database')->schema();
Chris@0 739 $all_tables_exist = TRUE;
Chris@0 740 foreach ($tables as $table) {
Chris@0 741 if (!$db_schema->tableExists($table)) {
Chris@17 742 $this->fail(new FormattableMarkup('Installed entity type table for the %entity_type entity type: %table', [
Chris@0 743 '%entity_type' => $entity_type_id,
Chris@0 744 '%table' => $table,
Chris@0 745 ]));
Chris@0 746 $all_tables_exist = FALSE;
Chris@0 747 }
Chris@0 748 }
Chris@0 749 if ($all_tables_exist) {
Chris@17 750 $this->pass(new FormattableMarkup('Installed entity type tables for the %entity_type entity type: %tables', [
Chris@0 751 '%entity_type' => $entity_type_id,
Chris@0 752 '%tables' => '{' . implode('}, {', $tables) . '}',
Chris@0 753 ]));
Chris@0 754 }
Chris@0 755 }
Chris@0 756 }
Chris@0 757
Chris@0 758 /**
Chris@0 759 * Enables modules for this test.
Chris@0 760 *
Chris@0 761 * This method does not install modules fully. Services and hooks for the
Chris@0 762 * module are available, but the install process is not performed.
Chris@0 763 *
Chris@0 764 * To install test modules outside of the testing environment, add
Chris@0 765 * @code
Chris@0 766 * $settings['extension_discovery_scan_tests'] = TRUE;
Chris@0 767 * @endcode
Chris@0 768 * to your settings.php.
Chris@0 769 *
Chris@0 770 * @param string[] $modules
Chris@0 771 * A list of modules to enable. Dependencies are not resolved; i.e.,
Chris@0 772 * multiple modules have to be specified individually. The modules are only
Chris@0 773 * added to the active module list and loaded; i.e., their database schema
Chris@0 774 * is not installed. hook_install() is not invoked. A custom module weight
Chris@0 775 * is not applied.
Chris@0 776 *
Chris@0 777 * @throws \LogicException
Chris@0 778 * If any module in $modules is already enabled.
Chris@0 779 * @throws \RuntimeException
Chris@0 780 * If a module is not enabled after enabling it.
Chris@0 781 */
Chris@0 782 protected function enableModules(array $modules) {
Chris@0 783 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
Chris@0 784 if ($trace[1]['function'] === 'setUp') {
Chris@0 785 trigger_error('KernelTestBase::enableModules() should not be called from setUp(). Use the $modules property instead.', E_USER_DEPRECATED);
Chris@0 786 }
Chris@0 787 unset($trace);
Chris@0 788
Chris@0 789 // Perform an ExtensionDiscovery scan as this function may receive a
Chris@0 790 // profile that is not the current profile, and we don't yet have a cached
Chris@0 791 // way to receive inactive profile information.
Chris@0 792 // @todo Remove as part of https://www.drupal.org/node/2186491
Chris@17 793 $listing = new ExtensionDiscovery($this->root);
Chris@0 794 $module_list = $listing->scan('module');
Chris@0 795 // In ModuleHandlerTest we pass in a profile as if it were a module.
Chris@0 796 $module_list += $listing->scan('profile');
Chris@0 797
Chris@0 798 // Set the list of modules in the extension handler.
Chris@0 799 $module_handler = $this->container->get('module_handler');
Chris@0 800
Chris@0 801 // Write directly to active storage to avoid early instantiation of
Chris@0 802 // the event dispatcher which can prevent modules from registering events.
Chris@0 803 $active_storage = $this->container->get('config.storage');
Chris@0 804 $extension_config = $active_storage->read('core.extension');
Chris@0 805
Chris@0 806 foreach ($modules as $module) {
Chris@0 807 if ($module_handler->moduleExists($module)) {
Chris@12 808 continue;
Chris@0 809 }
Chris@0 810 $module_handler->addModule($module, $module_list[$module]->getPath());
Chris@0 811 // Maintain the list of enabled modules in configuration.
Chris@0 812 $extension_config['module'][$module] = 0;
Chris@0 813 }
Chris@0 814 $active_storage->write('core.extension', $extension_config);
Chris@0 815
Chris@0 816 // Update the kernel to make their services available.
Chris@0 817 $extensions = $module_handler->getModuleList();
Chris@0 818 $this->container->get('kernel')->updateModules($extensions, $extensions);
Chris@0 819
Chris@0 820 // Ensure isLoaded() is TRUE in order to make
Chris@0 821 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
Chris@0 822 // Note that the kernel has rebuilt the container; this $module_handler is
Chris@0 823 // no longer the $module_handler instance from above.
Chris@0 824 $module_handler = $this->container->get('module_handler');
Chris@0 825 $module_handler->reload();
Chris@0 826 foreach ($modules as $module) {
Chris@0 827 if (!$module_handler->moduleExists($module)) {
Chris@0 828 throw new \RuntimeException("$module module is not enabled after enabling it.");
Chris@0 829 }
Chris@0 830 }
Chris@0 831 }
Chris@0 832
Chris@0 833 /**
Chris@0 834 * Disables modules for this test.
Chris@0 835 *
Chris@0 836 * @param string[] $modules
Chris@0 837 * A list of modules to disable. Dependencies are not resolved; i.e.,
Chris@0 838 * multiple modules have to be specified with dependent modules first.
Chris@0 839 * Code of previously enabled modules is still loaded. The modules are only
Chris@0 840 * removed from the active module list.
Chris@0 841 *
Chris@0 842 * @throws \LogicException
Chris@0 843 * If any module in $modules is already disabled.
Chris@0 844 * @throws \RuntimeException
Chris@0 845 * If a module is not disabled after disabling it.
Chris@0 846 */
Chris@0 847 protected function disableModules(array $modules) {
Chris@0 848 // Unset the list of modules in the extension handler.
Chris@0 849 $module_handler = $this->container->get('module_handler');
Chris@0 850 $module_filenames = $module_handler->getModuleList();
Chris@0 851 $extension_config = $this->config('core.extension');
Chris@0 852 foreach ($modules as $module) {
Chris@0 853 if (!$module_handler->moduleExists($module)) {
Chris@0 854 throw new \LogicException("$module module cannot be disabled because it is not enabled.");
Chris@0 855 }
Chris@0 856 unset($module_filenames[$module]);
Chris@0 857 $extension_config->clear('module.' . $module);
Chris@0 858 }
Chris@0 859 $extension_config->save();
Chris@0 860 $module_handler->setModuleList($module_filenames);
Chris@0 861 $module_handler->resetImplementations();
Chris@0 862 // Update the kernel to remove their services.
Chris@0 863 $this->container->get('kernel')->updateModules($module_filenames, $module_filenames);
Chris@0 864
Chris@0 865 // Ensure isLoaded() is TRUE in order to make
Chris@0 866 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
Chris@0 867 // Note that the kernel has rebuilt the container; this $module_handler is
Chris@0 868 // no longer the $module_handler instance from above.
Chris@0 869 $module_handler = $this->container->get('module_handler');
Chris@0 870 $module_handler->reload();
Chris@0 871 foreach ($modules as $module) {
Chris@0 872 if ($module_handler->moduleExists($module)) {
Chris@0 873 throw new \RuntimeException("$module module is not disabled after disabling it.");
Chris@0 874 }
Chris@0 875 }
Chris@0 876 }
Chris@0 877
Chris@0 878 /**
Chris@0 879 * Renders a render array.
Chris@0 880 *
Chris@0 881 * @param array $elements
Chris@0 882 * The elements to render.
Chris@0 883 *
Chris@0 884 * @return string
Chris@0 885 * The rendered string output (typically HTML).
Chris@0 886 */
Chris@0 887 protected function render(array &$elements) {
Chris@0 888 // \Drupal\Core\Render\BareHtmlPageRenderer::renderBarePage calls out to
Chris@0 889 // system_page_attachments() directly.
Chris@0 890 if (!\Drupal::moduleHandler()->moduleExists('system')) {
Chris@0 891 throw new \Exception(__METHOD__ . ' requires system module to be installed.');
Chris@0 892 }
Chris@0 893
Chris@0 894 // Use the bare HTML page renderer to render our links.
Chris@0 895 $renderer = $this->container->get('bare_html_page_renderer');
Chris@0 896 $response = $renderer->renderBarePage($elements, '', 'maintenance_page');
Chris@0 897
Chris@0 898 // Glean the content from the response object.
Chris@0 899 $content = $response->getContent();
Chris@0 900 $this->setRawContent($content);
Chris@0 901 $this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
Chris@0 902 return $content;
Chris@0 903 }
Chris@0 904
Chris@0 905 /**
Chris@0 906 * Sets an in-memory Settings variable.
Chris@0 907 *
Chris@0 908 * @param string $name
Chris@0 909 * The name of the setting to set.
Chris@0 910 * @param bool|string|int|array|null $value
Chris@0 911 * The value to set. Note that array values are replaced entirely; use
Chris@0 912 * \Drupal\Core\Site\Settings::get() to perform custom merges.
Chris@0 913 */
Chris@0 914 protected function setSetting($name, $value) {
Chris@17 915 if ($name === 'install_profile') {
Chris@17 916 @trigger_error('Use \Drupal\KernelTests\KernelTestBase::setInstallProfile() to set the install profile in kernel tests. See https://www.drupal.org/node/2538996', E_USER_DEPRECATED);
Chris@17 917 $this->setInstallProfile($value);
Chris@17 918 }
Chris@0 919 $settings = Settings::getInstance() ? Settings::getAll() : [];
Chris@0 920 $settings[$name] = $value;
Chris@0 921 new Settings($settings);
Chris@0 922 }
Chris@0 923
Chris@0 924 /**
Chris@17 925 * Sets the install profile and rebuilds the container to update it.
Chris@17 926 *
Chris@17 927 * @param string $profile
Chris@17 928 * The install profile to set.
Chris@17 929 */
Chris@17 930 protected function setInstallProfile($profile) {
Chris@17 931 $this->container->get('config.factory')
Chris@17 932 ->getEditable('core.extension')
Chris@17 933 ->set('profile', $profile)
Chris@17 934 ->save();
Chris@17 935
Chris@17 936 // The installation profile is provided by a container parameter. Saving
Chris@17 937 // the configuration doesn't automatically trigger invalidation
Chris@17 938 $this->container->get('kernel')->rebuildContainer();
Chris@17 939 }
Chris@17 940
Chris@17 941 /**
Chris@0 942 * Stops test execution.
Chris@0 943 */
Chris@0 944 protected function stop() {
Chris@0 945 $this->getTestResultObject()->stop();
Chris@0 946 }
Chris@0 947
Chris@0 948 /**
Chris@0 949 * Dumps the current state of the virtual filesystem to STDOUT.
Chris@0 950 */
Chris@0 951 protected function vfsDump() {
Chris@0 952 vfsStream::inspect(new vfsStreamPrintVisitor());
Chris@0 953 }
Chris@0 954
Chris@0 955 /**
Chris@0 956 * Returns the modules to enable for this test.
Chris@0 957 *
Chris@0 958 * @param string $class
Chris@0 959 * The fully-qualified class name of this test.
Chris@0 960 *
Chris@0 961 * @return array
Chris@0 962 */
Chris@0 963 private static function getModulesToEnable($class) {
Chris@0 964 $modules = [];
Chris@0 965 while ($class) {
Chris@0 966 if (property_exists($class, 'modules')) {
Chris@0 967 // Only add the modules, if the $modules property was not inherited.
Chris@0 968 $rp = new \ReflectionProperty($class, 'modules');
Chris@0 969 if ($rp->class == $class) {
Chris@0 970 $modules[$class] = $class::$modules;
Chris@0 971 }
Chris@0 972 }
Chris@0 973 $class = get_parent_class($class);
Chris@0 974 }
Chris@0 975 // Modules have been collected in reverse class hierarchy order; modules
Chris@0 976 // defined by base classes should be sorted first. Then, merge the results
Chris@0 977 // together.
Chris@0 978 $modules = array_reverse($modules);
Chris@0 979 return call_user_func_array('array_merge_recursive', $modules);
Chris@0 980 }
Chris@0 981
Chris@0 982 /**
Chris@0 983 * {@inheritdoc}
Chris@0 984 */
Chris@0 985 protected function prepareTemplate(\Text_Template $template) {
Chris@0 986 $bootstrap_globals = '';
Chris@0 987
Chris@0 988 // Fix missing bootstrap.php when $preserveGlobalState is FALSE.
Chris@0 989 // @see https://github.com/sebastianbergmann/phpunit/pull/797
Chris@0 990 $bootstrap_globals .= '$__PHPUNIT_BOOTSTRAP = ' . var_export($GLOBALS['__PHPUNIT_BOOTSTRAP'], TRUE) . ";\n";
Chris@0 991
Chris@0 992 // Avoid repetitive test namespace discoveries to improve performance.
Chris@0 993 // @see /core/tests/bootstrap.php
Chris@0 994 $bootstrap_globals .= '$namespaces = ' . var_export($GLOBALS['namespaces'], TRUE) . ";\n";
Chris@0 995
Chris@0 996 $template->setVar([
Chris@0 997 'constants' => '',
Chris@0 998 'included_files' => '',
Chris@0 999 'globals' => $bootstrap_globals,
Chris@0 1000 ]);
Chris@0 1001 }
Chris@0 1002
Chris@0 1003 /**
Chris@0 1004 * Returns whether the current test method is running in a separate process.
Chris@0 1005 *
Chris@0 1006 * Note that KernelTestBase will run in a separate process by default.
Chris@0 1007 *
Chris@0 1008 * @return bool
Chris@0 1009 *
Chris@0 1010 * @see \Drupal\KernelTests\KernelTestBase::$runTestInSeparateProcess
Chris@0 1011 * @see https://github.com/sebastianbergmann/phpunit/pull/1350
Chris@0 1012 *
Chris@0 1013 * @deprecated in Drupal 8.4.x, for removal before the Drupal 9.0.0 release.
Chris@0 1014 * KernelTestBase tests are always run in isolated processes.
Chris@0 1015 */
Chris@0 1016 protected function isTestInIsolation() {
Chris@0 1017 @trigger_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated in Drupal 8.4.x, for removal before the Drupal 9.0.0 release. KernelTestBase tests are always run in isolated processes.', E_USER_DEPRECATED);
Chris@0 1018 return function_exists('__phpunit_run_isolated_test');
Chris@0 1019 }
Chris@0 1020
Chris@0 1021 /**
Chris@0 1022 * BC: Automatically resolve former KernelTestBase class properties.
Chris@0 1023 *
Chris@0 1024 * Test authors should follow the provided instructions and adjust their tests
Chris@0 1025 * accordingly.
Chris@0 1026 *
Chris@16 1027 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
Chris@0 1028 */
Chris@0 1029 public function __get($name) {
Chris@0 1030 if (in_array($name, [
Chris@0 1031 'public_files_directory',
Chris@0 1032 'private_files_directory',
Chris@0 1033 'temp_files_directory',
Chris@0 1034 'translation_files_directory',
Chris@0 1035 ])) {
Chris@0 1036 // @comment it in again.
Chris@0 1037 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use the regular API method to retrieve it instead (e.g., Settings).", $name), E_USER_DEPRECATED);
Chris@0 1038 switch ($name) {
Chris@0 1039 case 'public_files_directory':
Chris@0 1040 return Settings::get('file_public_path', \Drupal::service('site.path') . '/files');
Chris@0 1041
Chris@0 1042 case 'private_files_directory':
Chris@0 1043 return Settings::get('file_private_path');
Chris@0 1044
Chris@0 1045 case 'temp_files_directory':
Chris@0 1046 return file_directory_temp();
Chris@0 1047
Chris@0 1048 case 'translation_files_directory':
Chris@0 1049 return Settings::get('file_public_path', \Drupal::service('site.path') . '/translations');
Chris@0 1050 }
Chris@0 1051 }
Chris@0 1052
Chris@0 1053 if ($name === 'configDirectories') {
Chris@0 1054 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use config_get_config_directory() directly instead.", $name), E_USER_DEPRECATED);
Chris@0 1055 return [
Chris@0 1056 CONFIG_SYNC_DIRECTORY => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
Chris@0 1057 ];
Chris@0 1058 }
Chris@0 1059
Chris@0 1060 $denied = [
Chris@0 1061 // @see \Drupal\simpletest\TestBase
Chris@0 1062 'testId',
Chris@0 1063 'timeLimit',
Chris@0 1064 'results',
Chris@0 1065 'assertions',
Chris@0 1066 'skipClasses',
Chris@0 1067 'verbose',
Chris@0 1068 'verboseId',
Chris@0 1069 'verboseClassName',
Chris@0 1070 'verboseDirectory',
Chris@0 1071 'verboseDirectoryUrl',
Chris@0 1072 'dieOnFail',
Chris@0 1073 'kernel',
Chris@0 1074 // @see \Drupal\simpletest\TestBase::prepareEnvironment()
Chris@0 1075 'generatedTestFiles',
Chris@0 1076 // Properties from the old KernelTestBase class that has been removed.
Chris@0 1077 'keyValueFactory',
Chris@0 1078 ];
Chris@0 1079 if (in_array($name, $denied) || strpos($name, 'original') === 0) {
Chris@0 1080 throw new \RuntimeException(sprintf('TestBase::$%s property no longer exists', $name));
Chris@0 1081 }
Chris@0 1082 }
Chris@0 1083
Chris@0 1084 /**
Chris@0 1085 * Prevents serializing any properties.
Chris@0 1086 *
Chris@0 1087 * Kernel tests are run in a separate process. To do this PHPUnit creates a
Chris@0 1088 * script to run the test. If it fails, the test result object will contain a
Chris@0 1089 * stack trace which includes the test object. It will attempt to serialize
Chris@0 1090 * it. Returning an empty array prevents it from serializing anything it
Chris@0 1091 * should not.
Chris@0 1092 *
Chris@0 1093 * @return array
Chris@0 1094 * An empty array.
Chris@0 1095 *
Chris@0 1096 * @see vendor/phpunit/phpunit/src/Util/PHP/Template/TestCaseMethod.tpl.dist
Chris@0 1097 */
Chris@0 1098 public function __sleep() {
Chris@0 1099 return [];
Chris@0 1100 }
Chris@0 1101
Chris@0 1102 /**
Chris@0 1103 * {@inheritdoc}
Chris@0 1104 */
Chris@0 1105 public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) {
Chris@0 1106 // Cast objects implementing MarkupInterface to string instead of
Chris@0 1107 // relying on PHP casting them to string depending on what they are being
Chris@0 1108 // comparing with.
Chris@0 1109 $expected = static::castSafeStrings($expected);
Chris@0 1110 $actual = static::castSafeStrings($actual);
Chris@0 1111 parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
Chris@0 1112 }
Chris@0 1113
Chris@0 1114 }