3 namespace Drupal\KernelTests;
5 use Drupal\Component\FileCache\ApcuFileCacheBackend;
6 use Drupal\Component\FileCache\FileCache;
7 use Drupal\Component\FileCache\FileCacheFactory;
8 use Drupal\Component\Utility\Html;
9 use Drupal\Component\Render\FormattableMarkup;
10 use Drupal\Core\Config\Development\ConfigSchemaChecker;
11 use Drupal\Core\Database\Database;
12 use Drupal\Core\DependencyInjection\ContainerBuilder;
13 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
14 use Drupal\Core\DrupalKernel;
15 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
16 use Drupal\Core\Extension\ExtensionDiscovery;
17 use Drupal\Core\Language\Language;
18 use Drupal\Core\Site\Settings;
19 use Drupal\Core\Test\TestDatabase;
20 use Drupal\Tests\AssertHelperTrait;
21 use Drupal\Tests\ConfigTestTrait;
22 use Drupal\Tests\PhpunitCompatibilityTrait;
23 use Drupal\Tests\RandomGeneratorTrait;
24 use Drupal\Tests\TestRequirementsTrait;
25 use PHPUnit\Framework\TestCase;
26 use Symfony\Component\DependencyInjection\Reference;
27 use Symfony\Component\HttpFoundation\Request;
28 use org\bovigo\vfs\vfsStream;
29 use org\bovigo\vfs\visitor\vfsStreamPrintVisitor;
32 * Base class for functional integration tests.
34 * This base class should be useful for testing some types of integrations which
35 * don't require the overhead of a fully-installed Drupal instance, but which
36 * have many dependencies on parts of Drupal which can't or shouldn't be mocked.
38 * This base class partially boots a fixture Drupal. The state of the fixture
39 * Drupal is comparable to the state of a system during the early part of the
40 * installation process.
42 * Tests extending this base class can access services and the database, but the
43 * system is initially empty. This Drupal runs in a minimal mocked filesystem
44 * which operates within vfsStream.
46 * Modules specified in the $modules property are added to the service container
47 * for each test. The module/hook system is functional. Additional modules
48 * needed in a test should override $modules. Modules specified in this way will
49 * be added to those specified in superclasses.
51 * Unlike \Drupal\Tests\BrowserTestBase, the modules are not installed. They are
52 * loaded such that their services and hooks are available, but the install
53 * process has not been performed.
55 * Other modules can be made available in this way using
56 * KernelTestBase::enableModules().
58 * Some modules can be brought into a fully-installed state using
59 * KernelTestBase::installConfig(), KernelTestBase::installSchema(), and
60 * KernelTestBase::installEntitySchema(). Alternately, tests which need modules
61 * to be fully installed could inherit from \Drupal\Tests\BrowserTestBase.
63 * @see \Drupal\Tests\KernelTestBase::$modules
64 * @see \Drupal\Tests\KernelTestBase::enableModules()
65 * @see \Drupal\Tests\KernelTestBase::installConfig()
66 * @see \Drupal\Tests\KernelTestBase::installEntitySchema()
67 * @see \Drupal\Tests\KernelTestBase::installSchema()
68 * @see \Drupal\Tests\BrowserTestBase
70 abstract class KernelTestBase extends TestCase implements ServiceProviderInterface {
72 use AssertLegacyTrait;
73 use AssertContentTrait;
74 use AssertHelperTrait;
75 use RandomGeneratorTrait;
77 use TestRequirementsTrait;
78 use PhpunitCompatibilityTrait;
83 * Back up and restore any global variables that may be changed by tests.
85 * @see self::runTestInSeparateProcess
87 protected $backupGlobals = TRUE;
92 * Kernel tests are run in separate processes because they allow autoloading
93 * of code from extensions. Running the test in a separate process isolates
94 * this behavior from other tests. Subclasses should not override this
97 protected $runTestInSeparateProcess = TRUE;
102 * Back up and restore static class properties that may be changed by tests.
104 * @see self::runTestInSeparateProcess
106 protected $backupStaticAttributes = TRUE;
111 * Contains a few static class properties for performance.
113 protected $backupStaticAttributesBlacklist = [
114 // Ignore static discovery/parser caches to speed up tests.
115 'Drupal\Component\Discovery\YamlDiscovery' => ['parsedFiles'],
116 'Drupal\Core\DependencyInjection\YamlFileLoader' => ['yaml'],
117 'Drupal\Core\Extension\ExtensionDiscovery' => ['files'],
118 'Drupal\Core\Extension\InfoParser' => ['parsedInfos'],
119 // Drupal::$container cannot be serialized.
120 'Drupal' => ['container'],
121 // Settings cannot be serialized.
122 'Drupal\Core\Site\Settings' => ['instance'],
128 * Do not forward any global state from the parent process to the processes
129 * that run the actual tests.
131 * @see self::runTestInSeparateProcess
133 protected $preserveGlobalState = FALSE;
136 * @var \Composer\Autoload\Classloader
138 protected $classLoader;
143 protected $siteDirectory;
148 protected $databasePrefix;
151 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
153 protected $container;
158 * The test runner will merge the $modules lists from this class, the class
159 * it extends, and so on up the class hierarchy. It is not necessary to
160 * include modules in your list that a parent class has already declared.
162 * @see \Drupal\Tests\KernelTestBase::enableModules()
163 * @see \Drupal\Tests\KernelTestBase::bootKernel()
167 protected static $modules = [];
170 * The virtual filesystem root directory.
172 * @var \org\bovigo\vfs\vfsStreamDirectory
179 protected $expectedLogSeverity;
184 protected $expectedLogMessage;
187 * @todo Move into Config test base class.
188 * @var \Drupal\Core\Config\ConfigImporter
190 protected $configImporter;
200 * Set to TRUE to strict check all configuration saved.
202 * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
206 protected $strictConfigSchema = TRUE;
209 * An array of config object names that are excluded from schema checking.
213 protected static $configSchemaCheckerExclusions = [
214 // Following are used to test lack of or partial schema. Where partial
215 // schema is provided, that is explicitly tested in specific tests.
216 'config_schema_test.noschema',
217 'config_schema_test.someschema',
218 'config_schema_test.schema_data_types',
219 'config_schema_test.no_schema_data_types',
220 // Used to test application of schema to filtering of configuration.
221 'config_test.dynamic.system',
227 public static function setUpBeforeClass() {
228 parent::setUpBeforeClass();
230 // Change the current dir to DRUPAL_ROOT.
231 chdir(static::getDrupalRoot());
237 protected function setUp() {
240 $this->root = static::getDrupalRoot();
241 $this->initFileCache();
242 $this->bootEnvironment();
247 * Bootstraps a basic test environment.
249 * Should not be called by tests. Only visible for DrupalKernel integration
252 * @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
255 protected function bootEnvironment() {
256 $this->streamWrappers = [];
257 \Drupal::unsetContainer();
259 $this->classLoader = require $this->root . '/autoload.php';
261 require_once $this->root . '/core/includes/bootstrap.inc';
263 // Set up virtual filesystem.
264 Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
265 $test_db = new TestDatabase();
266 $this->siteDirectory = $test_db->getTestSitePath();
268 // Ensure that all code that relies on drupal_valid_test_ua() can still be
269 // safely executed. This primarily affects the (test) site directory
270 // resolution (used by e.g. LocalStream and PhpStorage).
271 $this->databasePrefix = $test_db->getDatabasePrefix();
272 drupal_valid_test_ua($this->databasePrefix);
275 'hash_salt' => get_class($this),
276 'file_public_path' => $this->siteDirectory . '/files',
277 // Disable Twig template caching/dumping.
278 'twig_cache' => FALSE,
279 // @see \Drupal\KernelTests\KernelTestBase::register()
281 new Settings($settings);
283 $this->setUpFilesystem();
285 foreach (Database::getAllConnectionInfo() as $key => $targets) {
286 Database::removeConnection($key);
288 Database::addConnectionInfo('default', 'default', $this->getDatabaseConnectionInfo()['default']);
292 * Sets up the filesystem, so things like the file directory.
294 protected function setUpFilesystem() {
295 $test_db = new TestDatabase($this->databasePrefix);
296 $test_site_path = $test_db->getTestSitePath();
298 $this->vfsRoot = vfsStream::setup('root');
299 $this->vfsRoot->addChild(vfsStream::newDirectory($test_site_path));
300 $this->siteDirectory = vfsStream::url('root/' . $test_site_path);
302 mkdir($this->siteDirectory . '/files', 0775);
303 mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
305 $settings = Settings::getInstance() ? Settings::getAll() : [];
306 $settings['file_public_path'] = $this->siteDirectory . '/files';
307 new Settings($settings);
309 $GLOBALS['config_directories'] = [
310 CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
317 public function getDatabasePrefix() {
318 return $this->databasePrefix;
322 * Bootstraps a kernel for a test.
324 private function bootKernel() {
325 $this->setSetting('container_yamls', []);
326 // Allow for test-specific overrides.
327 $settings_services_file = $this->root . '/sites/default/testing.services.yml';
328 if (file_exists($settings_services_file)) {
329 // Copy the testing-specific service overrides in place.
330 $testing_services_file = $this->siteDirectory . '/services.yml';
331 copy($settings_services_file, $testing_services_file);
332 $this->setSetting('container_yamls', [$testing_services_file]);
335 // Allow for global test environment overrides.
336 if (file_exists($test_env = $this->root . '/sites/default/testing.services.yml')) {
337 $GLOBALS['conf']['container_yamls']['testing'] = $test_env;
339 // Add this test class as a service provider.
340 $GLOBALS['conf']['container_service_providers']['test'] = $this;
342 $modules = self::getModulesToEnable(get_class($this));
344 // Prepare a precompiled container for all tests of this class.
345 // Substantially improves performance, since ContainerBuilder::compile()
346 // is very expensive. Encourages testing best practices (small tests).
347 // Normally a setUpBeforeClass() operation, but object scope is required to
348 // inject $this test class instance as a service provider (see above).
349 $rc = new \ReflectionClass(get_class($this));
350 $test_method_count = count(array_filter($rc->getMethods(), function ($method) {
351 // PHPUnit's @test annotations are intentionally ignored/not supported.
352 return strpos($method->getName(), 'test') === 0;
355 // Bootstrap the kernel. Do not use createFromRequest() to retain Settings.
356 $kernel = new DrupalKernel('testing', $this->classLoader, FALSE);
357 $kernel->setSitePath($this->siteDirectory);
358 // Boot a new one-time container from scratch. Ensure to set the module list
359 // upfront to avoid a subsequent rebuild.
360 if ($modules && $extensions = $this->getExtensionsForModules($modules)) {
361 $kernel->updateModules($extensions, $extensions);
363 // DrupalKernel::boot() is not sufficient as it does not invoke preHandle(),
364 // which is required to initialize legacy global variables.
365 $request = Request::create('/');
366 $kernel->prepareLegacyRequest($request);
368 // register() is only called if a new container was built/compiled.
369 $this->container = $kernel->getContainer();
371 // Ensure database tasks have been run.
372 require_once __DIR__ . '/../../../includes/install.inc';
373 $connection = Database::getConnection();
374 $errors = db_installer_object($connection->driver())->runTasks();
375 if (!empty($errors)) {
376 $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
380 $this->container->get('module_handler')->loadAll();
383 $this->container->get('request_stack')->push($request);
385 // Setup the destion to the be frontpage by default.
386 \Drupal::destination()->set('/');
388 // Write the core.extension configuration.
389 // Required for ConfigInstaller::installDefaultConfig() to work.
390 $this->container->get('config.storage')->write('core.extension', [
391 'module' => array_fill_keys($modules, 0),
396 $settings = Settings::getAll();
397 $settings['php_storage']['default'] = [
398 'class' => '\Drupal\Component\PhpStorage\FileStorage',
400 new Settings($settings);
402 // Manually configure the test mail collector implementation to prevent
403 // tests from sending out emails and collect them in state instead.
404 // While this should be enforced via settings.php prior to installation,
405 // some tests expect to be able to test mail system implementations.
406 $GLOBALS['config']['system.mail']['interface']['default'] = 'test_mail_collector';
408 // Manually configure the default file scheme so that modules that use file
409 // functions don't have to install system and its configuration.
410 // @see file_default_scheme()
411 $GLOBALS['config']['system.file']['default_scheme'] = 'public';
415 * Configuration accessor for tests. Returns non-overridden configuration.
417 * @param string $name
418 * The configuration name.
420 * @return \Drupal\Core\Config\Config
421 * The configuration object with original configuration data.
423 protected function config($name) {
424 return $this->container->get('config.factory')->getEditable($name);
428 * Returns the Database connection info to be used for this test.
430 * This method only exists for tests of the Database component itself, because
431 * they require multiple database connections. Each SQLite :memory: connection
432 * creates a new/separate database in memory. A shared-memory SQLite file URI
433 * triggers PHP open_basedir/allow_url_fopen/allow_url_include restrictions.
434 * Due to that, Database tests are running against a SQLite database that is
435 * located in an actual file in the system's temporary directory.
437 * Other tests should not override this method.
440 * A Database connection info array.
444 protected function getDatabaseConnectionInfo() {
445 // If the test is run with argument dburl then use it.
446 $db_url = getenv('SIMPLETEST_DB');
447 if (empty($db_url)) {
448 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.');
451 $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root);
452 Database::addConnectionInfo('default', 'default', $database);
455 // Clone the current connection and replace the current prefix.
456 $connection_info = Database::getConnectionInfo('default');
457 if (!empty($connection_info)) {
458 Database::renameConnection('default', 'simpletest_original_default');
459 foreach ($connection_info as $target => $value) {
460 // Replace the full table prefix definition to ensure that no table
461 // prefixes of the test runner leak into the test.
462 $connection_info[$target]['prefix'] = [
463 'default' => $this->databasePrefix,
467 return $connection_info;
471 * Initializes the FileCache component.
473 * We can not use the Settings object in a component, that's why we have to do
474 * it here instead of \Drupal\Component\FileCache\FileCacheFactory.
476 protected function initFileCache() {
477 $configuration = Settings::get('file_cache');
479 // Provide a default configuration, if not set.
480 if (!isset($configuration['default'])) {
481 // @todo Use extension_loaded('apcu') for non-testbot
482 // https://www.drupal.org/node/2447753.
483 if (function_exists('apcu_fetch')) {
484 $configuration['default']['cache_backend_class'] = ApcuFileCacheBackend::class;
487 FileCacheFactory::setConfiguration($configuration);
488 FileCacheFactory::setPrefix(Settings::getApcuPrefix('file_cache', $this->root));
492 * Returns Extension objects for $modules to enable.
494 * @param string[] $modules
495 * The list of modules to enable.
497 * @return \Drupal\Core\Extension\Extension[]
498 * Extension objects for $modules, keyed by module name.
500 * @throws \PHPUnit_Framework_Exception
501 * If a module is not available.
503 * @see \Drupal\Tests\KernelTestBase::enableModules()
504 * @see \Drupal\Core\Extension\ModuleHandler::add()
506 private function getExtensionsForModules(array $modules) {
508 $discovery = new ExtensionDiscovery($this->root);
509 $discovery->setProfileDirectories([]);
510 $list = $discovery->scan('module');
511 foreach ($modules as $name) {
512 if (!isset($list[$name])) {
513 throw new \PHPUnit_Framework_Exception("Unavailable module: '$name'. If this module needs to be downloaded separately, annotate the test class with '@requires module $name'.");
515 $extensions[$name] = $list[$name];
521 * Registers test-specific services.
523 * Extend this method in your test to register additional services. This
524 * method is called whenever the kernel is rebuilt.
526 * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
527 * The service container to enhance.
529 * @see \Drupal\Tests\KernelTestBase::bootKernel()
531 public function register(ContainerBuilder $container) {
532 // Keep the container object around for tests.
533 $this->container = $container;
536 ->register('flood', 'Drupal\Core\Flood\MemoryBackend')
537 ->addArgument(new Reference('request_stack'));
539 ->register('lock', 'Drupal\Core\Lock\NullLockBackend');
541 ->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');
543 ->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory')
544 // Must persist container rebuilds, or all data would vanish otherwise.
547 ->setAlias('keyvalue', 'keyvalue.memory');
549 // Set the default language on the minimal container.
550 $container->setParameter('language.default_values', Language::$defaultValues);
552 if ($this->strictConfigSchema) {
554 ->register('simpletest.config_schema_checker', ConfigSchemaChecker::class)
555 ->addArgument(new Reference('config.typed'))
556 ->addArgument($this->getConfigSchemaExclusions())
557 ->addTag('event_subscriber');
560 if ($container->hasDefinition('path_processor_alias')) {
561 // Prevent the alias-based path processor, which requires a url_alias db
562 // table, from being registered to the path processor manager. We do this
563 // by removing the tags that the compiler pass looks for. This means the
564 // url generator can safely be used within tests.
565 $container->getDefinition('path_processor_alias')
566 ->clearTag('path_processor_inbound')
567 ->clearTag('path_processor_outbound');
570 if ($container->hasDefinition('password')) {
571 $container->getDefinition('password')
574 TestServiceProvider::addRouteProvider($container);
578 * Gets the config schema exclusions for this test.
581 * An array of config object names that are excluded from schema checking.
583 protected function getConfigSchemaExclusions() {
584 $class = get_class($this);
587 if (property_exists($class, 'configSchemaCheckerExclusions')) {
588 $exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
590 $class = get_parent_class($class);
592 // Filter out any duplicates.
593 return array_unique($exceptions);
599 protected function assertPostConditions() {
600 // Execute registered Drupal shutdown functions prior to tearing down.
601 // @see _drupal_shutdown_function()
602 $callbacks = &drupal_register_shutdown_function();
603 while ($callback = array_shift($callbacks)) {
604 call_user_func_array($callback['callback'], $callback['arguments']);
607 // Shut down the kernel (if bootKernel() was called).
608 // @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
609 if ($this->container) {
610 $this->container->get('kernel')->shutdown();
613 // Fail in case any (new) shutdown functions exist.
614 $this->assertCount(0, drupal_register_shutdown_function(), 'Unexpected Drupal shutdown callbacks exist after running shutdown functions.');
616 parent::assertPostConditions();
622 protected function tearDown() {
623 // Destroy the testing kernel.
624 if (isset($this->kernel)) {
625 $this->kernel->shutdown();
628 // Remove all prefixed tables.
629 $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
630 $original_prefix = $original_connection_info['default']['prefix']['default'];
631 $test_connection_info = Database::getConnectionInfo('default');
632 $test_prefix = $test_connection_info['default']['prefix']['default'];
633 if ($original_prefix != $test_prefix) {
634 $tables = Database::getConnection()->schema()->findTables('%');
635 foreach ($tables as $table) {
636 if (Database::getConnection()->schema()->dropTable($table)) {
637 unset($tables[$table]);
642 // Free up memory: Own properties.
643 $this->classLoader = NULL;
644 $this->vfsRoot = NULL;
645 $this->configImporter = NULL;
647 // Free up memory: Custom test class properties.
648 // Note: Private properties cannot be cleaned up.
649 $rc = new \ReflectionClass(__CLASS__);
651 foreach ($rc->getProperties() as $property) {
652 $blacklist[$property->name] = $property->getDeclaringClass()->name;
654 $rc = new \ReflectionClass($this);
655 foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $property) {
656 if (!$property->isStatic() && !isset($blacklist[$property->name])) {
657 $this->{$property->name} = NULL;
661 // Clean FileCache cache.
664 // Clean up statics, container, and settings.
665 if (function_exists('drupal_static_reset')) {
666 drupal_static_reset();
668 \Drupal::unsetContainer();
669 $this->container = NULL;
678 * Additional tear down method to close the connection at the end.
680 public function tearDownCloseDatabaseConnection() {
681 // Destroy the database connection, which for example removes the memory
682 // from sqlite in memory.
683 foreach (Database::getAllConnectionInfo() as $key => $targets) {
684 Database::removeConnection($key);
689 * Installs default configuration for a given list of modules.
691 * @param string|string[] $modules
692 * A module or list of modules for which to install default configuration.
694 * @throws \LogicException
695 * If any module in $modules is not enabled.
697 protected function installConfig($modules) {
698 foreach ((array) $modules as $module) {
699 if (!$this->container->get('module_handler')->moduleExists($module)) {
700 throw new \LogicException("$module module is not enabled.");
702 $this->container->get('config.installer')->installDefaultConfig('module', $module);
707 * Installs database tables from a module schema definition.
709 * @param string $module
710 * The name of the module that defines the table's schema.
711 * @param string|array $tables
712 * The name or an array of the names of the tables to install.
714 * @throws \LogicException
715 * If $module is not enabled or the table schema cannot be found.
717 protected function installSchema($module, $tables) {
718 // drupal_get_module_schema() is technically able to install a schema
719 // of a non-enabled module, but its ability to load the module's .install
720 // file depends on many other factors. To prevent differences in test
721 // behavior and non-reproducible test failures, we only allow the schema of
722 // explicitly loaded/enabled modules to be installed.
723 if (!$this->container->get('module_handler')->moduleExists($module)) {
724 throw new \LogicException("$module module is not enabled.");
726 $tables = (array) $tables;
727 foreach ($tables as $table) {
728 $schema = drupal_get_module_schema($module, $table);
729 if (empty($schema)) {
730 // BC layer to avoid some contrib tests to fail.
731 // @todo Remove the BC layer before 8.1.x release.
732 // @see https://www.drupal.org/node/2670360
733 // @see https://www.drupal.org/node/2670454
734 if ($module == 'system') {
737 throw new \LogicException("$module module does not define a schema for table '$table'.");
739 $this->container->get('database')->schema()->createTable($table, $schema);
744 * Installs the storage schema for a specific entity type.
746 * @param string $entity_type_id
747 * The ID of the entity type.
749 protected function installEntitySchema($entity_type_id) {
750 /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
751 $entity_manager = $this->container->get('entity.manager');
752 $entity_type = $entity_manager->getDefinition($entity_type_id);
753 $entity_manager->onEntityTypeCreate($entity_type);
755 // For test runs, the most common storage backend is a SQL database. For
756 // this case, ensure the tables got created.
757 $storage = $entity_manager->getStorage($entity_type_id);
758 if ($storage instanceof SqlEntityStorageInterface) {
759 $tables = $storage->getTableMapping()->getTableNames();
760 $db_schema = $this->container->get('database')->schema();
761 $all_tables_exist = TRUE;
762 foreach ($tables as $table) {
763 if (!$db_schema->tableExists($table)) {
764 $this->fail(new FormattableMarkup('Installed entity type table for the %entity_type entity type: %table', [
765 '%entity_type' => $entity_type_id,
768 $all_tables_exist = FALSE;
771 if ($all_tables_exist) {
772 $this->pass(new FormattableMarkup('Installed entity type tables for the %entity_type entity type: %tables', [
773 '%entity_type' => $entity_type_id,
774 '%tables' => '{' . implode('}, {', $tables) . '}',
781 * Enables modules for this test.
783 * This method does not install modules fully. Services and hooks for the
784 * module are available, but the install process is not performed.
786 * To install test modules outside of the testing environment, add
788 * $settings['extension_discovery_scan_tests'] = TRUE;
790 * to your settings.php.
792 * @param string[] $modules
793 * A list of modules to enable. Dependencies are not resolved; i.e.,
794 * multiple modules have to be specified individually. The modules are only
795 * added to the active module list and loaded; i.e., their database schema
796 * is not installed. hook_install() is not invoked. A custom module weight
799 * @throws \LogicException
800 * If any module in $modules is already enabled.
801 * @throws \RuntimeException
802 * If a module is not enabled after enabling it.
804 protected function enableModules(array $modules) {
805 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
806 if ($trace[1]['function'] === 'setUp') {
807 trigger_error('KernelTestBase::enableModules() should not be called from setUp(). Use the $modules property instead.', E_USER_DEPRECATED);
811 // Perform an ExtensionDiscovery scan as this function may receive a
812 // profile that is not the current profile, and we don't yet have a cached
813 // way to receive inactive profile information.
814 // @todo Remove as part of https://www.drupal.org/node/2186491
815 $listing = new ExtensionDiscovery($this->root);
816 $module_list = $listing->scan('module');
817 // In ModuleHandlerTest we pass in a profile as if it were a module.
818 $module_list += $listing->scan('profile');
820 // Set the list of modules in the extension handler.
821 $module_handler = $this->container->get('module_handler');
823 // Write directly to active storage to avoid early instantiation of
824 // the event dispatcher which can prevent modules from registering events.
825 $active_storage = $this->container->get('config.storage');
826 $extension_config = $active_storage->read('core.extension');
828 foreach ($modules as $module) {
829 if ($module_handler->moduleExists($module)) {
832 $module_handler->addModule($module, $module_list[$module]->getPath());
833 // Maintain the list of enabled modules in configuration.
834 $extension_config['module'][$module] = 0;
836 $active_storage->write('core.extension', $extension_config);
838 // Update the kernel to make their services available.
839 $extensions = $module_handler->getModuleList();
840 $this->container->get('kernel')->updateModules($extensions, $extensions);
842 // Ensure isLoaded() is TRUE in order to make
843 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
844 // Note that the kernel has rebuilt the container; this $module_handler is
845 // no longer the $module_handler instance from above.
846 $module_handler = $this->container->get('module_handler');
847 $module_handler->reload();
848 foreach ($modules as $module) {
849 if (!$module_handler->moduleExists($module)) {
850 throw new \RuntimeException("$module module is not enabled after enabling it.");
856 * Disables modules for this test.
858 * @param string[] $modules
859 * A list of modules to disable. Dependencies are not resolved; i.e.,
860 * multiple modules have to be specified with dependent modules first.
861 * Code of previously enabled modules is still loaded. The modules are only
862 * removed from the active module list.
864 * @throws \LogicException
865 * If any module in $modules is already disabled.
866 * @throws \RuntimeException
867 * If a module is not disabled after disabling it.
869 protected function disableModules(array $modules) {
870 // Unset the list of modules in the extension handler.
871 $module_handler = $this->container->get('module_handler');
872 $module_filenames = $module_handler->getModuleList();
873 $extension_config = $this->config('core.extension');
874 foreach ($modules as $module) {
875 if (!$module_handler->moduleExists($module)) {
876 throw new \LogicException("$module module cannot be disabled because it is not enabled.");
878 unset($module_filenames[$module]);
879 $extension_config->clear('module.' . $module);
881 $extension_config->save();
882 $module_handler->setModuleList($module_filenames);
883 $module_handler->resetImplementations();
884 // Update the kernel to remove their services.
885 $this->container->get('kernel')->updateModules($module_filenames, $module_filenames);
887 // Ensure isLoaded() is TRUE in order to make
888 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
889 // Note that the kernel has rebuilt the container; this $module_handler is
890 // no longer the $module_handler instance from above.
891 $module_handler = $this->container->get('module_handler');
892 $module_handler->reload();
893 foreach ($modules as $module) {
894 if ($module_handler->moduleExists($module)) {
895 throw new \RuntimeException("$module module is not disabled after disabling it.");
901 * Renders a render array.
903 * @param array $elements
904 * The elements to render.
907 * The rendered string output (typically HTML).
909 protected function render(array &$elements) {
910 // \Drupal\Core\Render\BareHtmlPageRenderer::renderBarePage calls out to
911 // system_page_attachments() directly.
912 if (!\Drupal::moduleHandler()->moduleExists('system')) {
913 throw new \Exception(__METHOD__ . ' requires system module to be installed.');
916 // Use the bare HTML page renderer to render our links.
917 $renderer = $this->container->get('bare_html_page_renderer');
918 $response = $renderer->renderBarePage($elements, '', 'maintenance_page');
920 // Glean the content from the response object.
921 $content = $response->getContent();
922 $this->setRawContent($content);
923 $this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
928 * Sets an in-memory Settings variable.
930 * @param string $name
931 * The name of the setting to set.
932 * @param bool|string|int|array|null $value
933 * The value to set. Note that array values are replaced entirely; use
934 * \Drupal\Core\Site\Settings::get() to perform custom merges.
936 protected function setSetting($name, $value) {
937 if ($name === 'install_profile') {
938 @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);
939 $this->setInstallProfile($value);
941 $settings = Settings::getInstance() ? Settings::getAll() : [];
942 $settings[$name] = $value;
943 new Settings($settings);
947 * Sets the install profile and rebuilds the container to update it.
949 * @param string $profile
950 * The install profile to set.
952 protected function setInstallProfile($profile) {
953 $this->container->get('config.factory')
954 ->getEditable('core.extension')
955 ->set('profile', $profile)
958 // The installation profile is provided by a container parameter. Saving
959 // the configuration doesn't automatically trigger invalidation
960 $this->container->get('kernel')->rebuildContainer();
964 * Stops test execution.
966 protected function stop() {
967 $this->getTestResultObject()->stop();
971 * Dumps the current state of the virtual filesystem to STDOUT.
973 protected function vfsDump() {
974 vfsStream::inspect(new vfsStreamPrintVisitor());
978 * Returns the modules to enable for this test.
980 * @param string $class
981 * The fully-qualified class name of this test.
985 private static function getModulesToEnable($class) {
988 if (property_exists($class, 'modules')) {
989 // Only add the modules, if the $modules property was not inherited.
990 $rp = new \ReflectionProperty($class, 'modules');
991 if ($rp->class == $class) {
992 $modules[$class] = $class::$modules;
995 $class = get_parent_class($class);
997 // Modules have been collected in reverse class hierarchy order; modules
998 // defined by base classes should be sorted first. Then, merge the results
1000 $modules = array_reverse($modules);
1001 return call_user_func_array('array_merge_recursive', $modules);
1007 protected function prepareTemplate(\Text_Template $template) {
1008 $bootstrap_globals = '';
1010 // Fix missing bootstrap.php when $preserveGlobalState is FALSE.
1011 // @see https://github.com/sebastianbergmann/phpunit/pull/797
1012 $bootstrap_globals .= '$__PHPUNIT_BOOTSTRAP = ' . var_export($GLOBALS['__PHPUNIT_BOOTSTRAP'], TRUE) . ";\n";
1014 // Avoid repetitive test namespace discoveries to improve performance.
1015 // @see /core/tests/bootstrap.php
1016 $bootstrap_globals .= '$namespaces = ' . var_export($GLOBALS['namespaces'], TRUE) . ";\n";
1020 'included_files' => '',
1021 'globals' => $bootstrap_globals,
1026 * Returns whether the current test method is running in a separate process.
1028 * Note that KernelTestBase will run in a separate process by default.
1032 * @see \Drupal\KernelTests\KernelTestBase::$runTestInSeparateProcess
1033 * @see https://github.com/sebastianbergmann/phpunit/pull/1350
1035 * @deprecated in Drupal 8.4.x, for removal before the Drupal 9.0.0 release.
1036 * KernelTestBase tests are always run in isolated processes.
1038 protected function isTestInIsolation() {
1039 @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);
1040 return function_exists('__phpunit_run_isolated_test');
1044 * BC: Automatically resolve former KernelTestBase class properties.
1046 * Test authors should follow the provided instructions and adjust their tests
1049 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
1051 public function __get($name) {
1052 if (in_array($name, [
1053 'public_files_directory',
1054 'private_files_directory',
1055 'temp_files_directory',
1056 'translation_files_directory',
1058 // @comment it in again.
1059 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use the regular API method to retrieve it instead (e.g., Settings).", $name), E_USER_DEPRECATED);
1061 case 'public_files_directory':
1062 return Settings::get('file_public_path', \Drupal::service('site.path') . '/files');
1064 case 'private_files_directory':
1065 return Settings::get('file_private_path');
1067 case 'temp_files_directory':
1068 return file_directory_temp();
1070 case 'translation_files_directory':
1071 return Settings::get('file_public_path', \Drupal::service('site.path') . '/translations');
1075 if ($name === 'configDirectories') {
1076 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use config_get_config_directory() directly instead.", $name), E_USER_DEPRECATED);
1078 CONFIG_SYNC_DIRECTORY => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
1083 // @see \Drupal\simpletest\TestBase
1093 'verboseDirectoryUrl',
1096 // @see \Drupal\simpletest\TestBase::prepareEnvironment()
1097 'generatedTestFiles',
1098 // Properties from the old KernelTestBase class that has been removed.
1101 if (in_array($name, $denied) || strpos($name, 'original') === 0) {
1102 throw new \RuntimeException(sprintf('TestBase::$%s property no longer exists', $name));
1107 * Prevents serializing any properties.
1109 * Kernel tests are run in a separate process. To do this PHPUnit creates a
1110 * script to run the test. If it fails, the test result object will contain a
1111 * stack trace which includes the test object. It will attempt to serialize
1112 * it. Returning an empty array prevents it from serializing anything it
1118 * @see vendor/phpunit/phpunit/src/Util/PHP/Template/TestCaseMethod.tpl.dist
1120 public function __sleep() {
1127 public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) {
1128 // Cast objects implementing MarkupInterface to string instead of
1129 // relying on PHP casting them to string depending on what they are being
1131 $expected = static::castSafeStrings($expected);
1132 $actual = static::castSafeStrings($actual);
1133 parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);