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\Utility\SafeMarkup;
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\simpletest\AssertContentTrait;
21 use Drupal\simpletest\AssertHelperTrait;
22 use Drupal\Tests\ConfigTestTrait;
23 use Drupal\Tests\RandomGeneratorTrait;
24 use Drupal\simpletest\TestServiceProvider;
25 use Symfony\Component\DependencyInjection\Reference;
26 use Symfony\Component\HttpFoundation\Request;
27 use org\bovigo\vfs\vfsStream;
28 use org\bovigo\vfs\visitor\vfsStreamPrintVisitor;
31 * Base class for functional integration tests.
33 * Tests extending this base class can access files and the database, but the
34 * entire environment is initially empty. Drupal runs in a minimal mocked
35 * environment, comparable to the one in the early installer.
37 * Unlike \Drupal\Tests\UnitTestCase, modules specified in the $modules
38 * property are automatically added to the service container for each test.
39 * The module/hook system is functional and operates on a fixed module list.
40 * Additional modules needed in a test may be loaded and added to the fixed
43 * Unlike \Drupal\simpletest\WebTestBase, the modules are only loaded, but not
44 * installed. Modules have to be installed manually, if needed.
46 * @see \Drupal\Tests\KernelTestBase::$modules
47 * @see \Drupal\Tests\KernelTestBase::enableModules()
49 * @todo Extend ::setRequirementsFromAnnotation() and ::checkRequirements() to
50 * account for '@requires module'.
52 abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements ServiceProviderInterface {
54 use AssertLegacyTrait;
55 use AssertContentTrait;
56 use AssertHelperTrait;
57 use RandomGeneratorTrait;
63 * Back up and restore any global variables that may be changed by tests.
65 * @see self::runTestInSeparateProcess
67 protected $backupGlobals = TRUE;
72 * Kernel tests are run in separate processes because they allow autoloading
73 * of code from extensions. Running the test in a separate process isolates
74 * this behavior from other tests. Subclasses should not override this
77 protected $runTestInSeparateProcess = TRUE;
82 * Back up and restore static class properties that may be changed by tests.
84 * @see self::runTestInSeparateProcess
86 protected $backupStaticAttributes = TRUE;
91 * Contains a few static class properties for performance.
93 protected $backupStaticAttributesBlacklist = [
94 // Ignore static discovery/parser caches to speed up tests.
95 'Drupal\Component\Discovery\YamlDiscovery' => ['parsedFiles'],
96 'Drupal\Core\DependencyInjection\YamlFileLoader' => ['yaml'],
97 'Drupal\Core\Extension\ExtensionDiscovery' => ['files'],
98 'Drupal\Core\Extension\InfoParser' => ['parsedInfos'],
99 // Drupal::$container cannot be serialized.
100 'Drupal' => ['container'],
101 // Settings cannot be serialized.
102 'Drupal\Core\Site\Settings' => ['instance'],
108 * Do not forward any global state from the parent process to the processes
109 * that run the actual tests.
111 * @see self::runTestInSeparateProcess
113 protected $preserveGlobalState = FALSE;
116 * @var \Composer\Autoload\Classloader
118 protected $classLoader;
123 protected $siteDirectory;
128 protected $databasePrefix;
131 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
133 protected $container;
138 * The test runner will merge the $modules lists from this class, the class
139 * it extends, and so on up the class hierarchy. It is not necessary to
140 * include modules in your list that a parent class has already declared.
142 * @see \Drupal\Tests\KernelTestBase::enableModules()
143 * @see \Drupal\Tests\KernelTestBase::bootKernel()
147 protected static $modules = [];
150 * The virtual filesystem root directory.
152 * @var \org\bovigo\vfs\vfsStreamDirectory
159 protected $expectedLogSeverity;
164 protected $expectedLogMessage;
167 * @todo Move into Config test base class.
168 * @var \Drupal\Core\Config\ConfigImporter
170 protected $configImporter;
180 * Set to TRUE to strict check all configuration saved.
182 * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
186 protected $strictConfigSchema = TRUE;
189 * An array of config object names that are excluded from schema checking.
193 protected static $configSchemaCheckerExclusions = [
194 // Following are used to test lack of or partial schema. Where partial
195 // schema is provided, that is explicitly tested in specific tests.
196 'config_schema_test.noschema',
197 'config_schema_test.someschema',
198 'config_schema_test.schema_data_types',
199 'config_schema_test.no_schema_data_types',
200 // Used to test application of schema to filtering of configuration.
201 'config_test.dynamic.system',
207 public static function setUpBeforeClass() {
208 parent::setUpBeforeClass();
210 // Change the current dir to DRUPAL_ROOT.
211 chdir(static::getDrupalRoot());
215 * Returns the drupal root directory.
219 protected static function getDrupalRoot() {
220 return dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
226 protected function setUp() {
229 $this->root = static::getDrupalRoot();
230 $this->initFileCache();
231 $this->bootEnvironment();
236 * Bootstraps a basic test environment.
238 * Should not be called by tests. Only visible for DrupalKernel integration
241 * @see \Drupal\system\Tests\DrupalKernel\DrupalKernelTest
244 protected function bootEnvironment() {
245 $this->streamWrappers = [];
246 \Drupal::unsetContainer();
248 $this->classLoader = require $this->root . '/autoload.php';
250 require_once $this->root . '/core/includes/bootstrap.inc';
252 // Set up virtual filesystem.
253 Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
254 $test_db = new TestDatabase();
255 $this->siteDirectory = $test_db->getTestSitePath();
257 // Ensure that all code that relies on drupal_valid_test_ua() can still be
258 // safely executed. This primarily affects the (test) site directory
259 // resolution (used by e.g. LocalStream and PhpStorage).
260 $this->databasePrefix = $test_db->getDatabasePrefix();
261 drupal_valid_test_ua($this->databasePrefix);
264 'hash_salt' => get_class($this),
265 'file_public_path' => $this->siteDirectory . '/files',
266 // Disable Twig template caching/dumping.
267 'twig_cache' => FALSE,
268 // @see \Drupal\KernelTests\KernelTestBase::register()
270 new Settings($settings);
272 $this->setUpFilesystem();
274 foreach (Database::getAllConnectionInfo() as $key => $targets) {
275 Database::removeConnection($key);
277 Database::addConnectionInfo('default', 'default', $this->getDatabaseConnectionInfo()['default']);
281 * Sets up the filesystem, so things like the file directory.
283 protected function setUpFilesystem() {
284 $test_db = new TestDatabase($this->databasePrefix);
285 $test_site_path = $test_db->getTestSitePath();
287 $this->vfsRoot = vfsStream::setup('root');
288 $this->vfsRoot->addChild(vfsStream::newDirectory($test_site_path));
289 $this->siteDirectory = vfsStream::url('root/' . $test_site_path);
291 mkdir($this->siteDirectory . '/files', 0775);
292 mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
294 $settings = Settings::getInstance() ? Settings::getAll() : [];
295 $settings['file_public_path'] = $this->siteDirectory . '/files';
296 new Settings($settings);
298 $GLOBALS['config_directories'] = [
299 CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
306 public function getDatabasePrefix() {
307 return $this->databasePrefix;
311 * Bootstraps a kernel for a test.
313 private function bootKernel() {
314 $this->setSetting('container_yamls', []);
315 // Allow for test-specific overrides.
316 $settings_services_file = $this->root . '/sites/default' . '/testing.services.yml';
317 if (file_exists($settings_services_file)) {
318 // Copy the testing-specific service overrides in place.
319 $testing_services_file = $this->root . '/' . $this->siteDirectory . '/services.yml';
320 copy($settings_services_file, $testing_services_file);
321 $this->setSetting('container_yamls', [$testing_services_file]);
324 // Allow for global test environment overrides.
325 if (file_exists($test_env = $this->root . '/sites/default/testing.services.yml')) {
326 $GLOBALS['conf']['container_yamls']['testing'] = $test_env;
328 // Add this test class as a service provider.
329 $GLOBALS['conf']['container_service_providers']['test'] = $this;
331 $modules = self::getModulesToEnable(get_class($this));
333 // Prepare a precompiled container for all tests of this class.
334 // Substantially improves performance, since ContainerBuilder::compile()
335 // is very expensive. Encourages testing best practices (small tests).
336 // Normally a setUpBeforeClass() operation, but object scope is required to
337 // inject $this test class instance as a service provider (see above).
338 $rc = new \ReflectionClass(get_class($this));
339 $test_method_count = count(array_filter($rc->getMethods(), function ($method) {
340 // PHPUnit's @test annotations are intentionally ignored/not supported.
341 return strpos($method->getName(), 'test') === 0;
344 // Bootstrap the kernel. Do not use createFromRequest() to retain Settings.
345 $kernel = new DrupalKernel('testing', $this->classLoader, FALSE);
346 $kernel->setSitePath($this->siteDirectory);
347 // Boot a new one-time container from scratch. Ensure to set the module list
348 // upfront to avoid a subsequent rebuild.
349 if ($modules && $extensions = $this->getExtensionsForModules($modules)) {
350 $kernel->updateModules($extensions, $extensions);
352 // DrupalKernel::boot() is not sufficient as it does not invoke preHandle(),
353 // which is required to initialize legacy global variables.
354 $request = Request::create('/');
355 $kernel->prepareLegacyRequest($request);
357 // register() is only called if a new container was built/compiled.
358 $this->container = $kernel->getContainer();
360 // Ensure database tasks have been run.
361 require_once __DIR__ . '/../../../includes/install.inc';
362 $connection = Database::getConnection();
363 $errors = db_installer_object($connection->driver())->runTasks();
364 if (!empty($errors)) {
365 $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
369 $this->container->get('module_handler')->loadAll();
372 $this->container->get('request_stack')->push($request);
374 // Setup the destion to the be frontpage by default.
375 \Drupal::destination()->set('/');
377 // Write the core.extension configuration.
378 // Required for ConfigInstaller::installDefaultConfig() to work.
379 $this->container->get('config.storage')->write('core.extension', [
380 'module' => array_fill_keys($modules, 0),
385 $settings = Settings::getAll();
386 $settings['php_storage']['default'] = [
387 'class' => '\Drupal\Component\PhpStorage\FileStorage',
389 new Settings($settings);
391 // Manually configure the test mail collector implementation to prevent
392 // tests from sending out emails and collect them in state instead.
393 // While this should be enforced via settings.php prior to installation,
394 // some tests expect to be able to test mail system implementations.
395 $GLOBALS['config']['system.mail']['interface']['default'] = 'test_mail_collector';
397 // Manually configure the default file scheme so that modules that use file
398 // functions don't have to install system and its configuration.
399 // @see file_default_scheme()
400 $GLOBALS['config']['system.file']['default_scheme'] = 'public';
404 * Configuration accessor for tests. Returns non-overridden configuration.
406 * @param string $name
407 * The configuration name.
409 * @return \Drupal\Core\Config\Config
410 * The configuration object with original configuration data.
412 protected function config($name) {
413 return $this->container->get('config.factory')->getEditable($name);
417 * Returns the Database connection info to be used for this test.
419 * This method only exists for tests of the Database component itself, because
420 * they require multiple database connections. Each SQLite :memory: connection
421 * creates a new/separate database in memory. A shared-memory SQLite file URI
422 * triggers PHP open_basedir/allow_url_fopen/allow_url_include restrictions.
423 * Due to that, Database tests are running against a SQLite database that is
424 * located in an actual file in the system's temporary directory.
426 * Other tests should not override this method.
429 * A Database connection info array.
433 protected function getDatabaseConnectionInfo() {
434 // If the test is run with argument dburl then use it.
435 $db_url = getenv('SIMPLETEST_DB');
436 if (empty($db_url)) {
437 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.');
440 $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root);
441 Database::addConnectionInfo('default', 'default', $database);
444 // Clone the current connection and replace the current prefix.
445 $connection_info = Database::getConnectionInfo('default');
446 if (!empty($connection_info)) {
447 Database::renameConnection('default', 'simpletest_original_default');
448 foreach ($connection_info as $target => $value) {
449 // Replace the full table prefix definition to ensure that no table
450 // prefixes of the test runner leak into the test.
451 $connection_info[$target]['prefix'] = [
452 'default' => $value['prefix']['default'] . $this->databasePrefix,
456 return $connection_info;
460 * Initializes the FileCache component.
462 * We can not use the Settings object in a component, that's why we have to do
463 * it here instead of \Drupal\Component\FileCache\FileCacheFactory.
465 protected function initFileCache() {
466 $configuration = Settings::get('file_cache');
468 // Provide a default configuration, if not set.
469 if (!isset($configuration['default'])) {
470 // @todo Use extension_loaded('apcu') for non-testbot
471 // https://www.drupal.org/node/2447753.
472 if (function_exists('apcu_fetch')) {
473 $configuration['default']['cache_backend_class'] = ApcuFileCacheBackend::class;
476 FileCacheFactory::setConfiguration($configuration);
477 FileCacheFactory::setPrefix(Settings::getApcuPrefix('file_cache', $this->root));
481 * Returns Extension objects for $modules to enable.
483 * @param string[] $modules
484 * The list of modules to enable.
486 * @return \Drupal\Core\Extension\Extension[]
487 * Extension objects for $modules, keyed by module name.
489 * @throws \PHPUnit_Framework_Exception
490 * If a module is not available.
492 * @see \Drupal\Tests\KernelTestBase::enableModules()
493 * @see \Drupal\Core\Extension\ModuleHandler::add()
495 private function getExtensionsForModules(array $modules) {
497 $discovery = new ExtensionDiscovery($this->root);
498 $discovery->setProfileDirectories([]);
499 $list = $discovery->scan('module');
500 foreach ($modules as $name) {
501 if (!isset($list[$name])) {
502 throw new \PHPUnit_Framework_Exception("Unavailable module: '$name'. If this module needs to be downloaded separately, annotate the test class with '@requires module $name'.");
504 $extensions[$name] = $list[$name];
510 * Registers test-specific services.
512 * Extend this method in your test to register additional services. This
513 * method is called whenever the kernel is rebuilt.
515 * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
516 * The service container to enhance.
518 * @see \Drupal\Tests\KernelTestBase::bootKernel()
520 public function register(ContainerBuilder $container) {
521 // Keep the container object around for tests.
522 $this->container = $container;
525 ->register('flood', 'Drupal\Core\Flood\MemoryBackend')
526 ->addArgument(new Reference('request_stack'));
528 ->register('lock', 'Drupal\Core\Lock\NullLockBackend');
530 ->register('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory');
532 ->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory')
533 // Must persist container rebuilds, or all data would vanish otherwise.
536 ->setAlias('keyvalue', 'keyvalue.memory');
538 // Set the default language on the minimal container.
539 $container->setParameter('language.default_values', Language::$defaultValues);
541 if ($this->strictConfigSchema) {
543 ->register('simpletest.config_schema_checker', ConfigSchemaChecker::class)
544 ->addArgument(new Reference('config.typed'))
545 ->addArgument($this->getConfigSchemaExclusions())
546 ->addTag('event_subscriber');
549 if ($container->hasDefinition('path_processor_alias')) {
550 // Prevent the alias-based path processor, which requires a url_alias db
551 // table, from being registered to the path processor manager. We do this
552 // by removing the tags that the compiler pass looks for. This means the
553 // url generator can safely be used within tests.
554 $container->getDefinition('path_processor_alias')
555 ->clearTag('path_processor_inbound')
556 ->clearTag('path_processor_outbound');
559 if ($container->hasDefinition('password')) {
560 $container->getDefinition('password')
563 TestServiceProvider::addRouteProvider($container);
567 * Gets the config schema exclusions for this test.
570 * An array of config object names that are excluded from schema checking.
572 protected function getConfigSchemaExclusions() {
573 $class = get_class($this);
576 if (property_exists($class, 'configSchemaCheckerExclusions')) {
577 $exceptions = array_merge($exceptions, $class::$configSchemaCheckerExclusions);
579 $class = get_parent_class($class);
581 // Filter out any duplicates.
582 return array_unique($exceptions);
588 protected function assertPostConditions() {
589 // Execute registered Drupal shutdown functions prior to tearing down.
590 // @see _drupal_shutdown_function()
591 $callbacks = &drupal_register_shutdown_function();
592 while ($callback = array_shift($callbacks)) {
593 call_user_func_array($callback['callback'], $callback['arguments']);
596 // Shut down the kernel (if bootKernel() was called).
597 // @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest
598 if ($this->container) {
599 $this->container->get('kernel')->shutdown();
602 // Fail in case any (new) shutdown functions exist.
603 $this->assertCount(0, drupal_register_shutdown_function(), 'Unexpected Drupal shutdown callbacks exist after running shutdown functions.');
605 parent::assertPostConditions();
611 protected function tearDown() {
612 // Destroy the testing kernel.
613 if (isset($this->kernel)) {
614 $this->kernel->shutdown();
617 // Remove all prefixed tables.
618 $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
619 $original_prefix = $original_connection_info['default']['prefix']['default'];
620 $test_connection_info = Database::getConnectionInfo('default');
621 $test_prefix = $test_connection_info['default']['prefix']['default'];
622 if ($original_prefix != $test_prefix) {
623 $tables = Database::getConnection()->schema()->findTables('%');
624 foreach ($tables as $table) {
625 if (Database::getConnection()->schema()->dropTable($table)) {
626 unset($tables[$table]);
631 // Free up memory: Own properties.
632 $this->classLoader = NULL;
633 $this->vfsRoot = NULL;
634 $this->configImporter = NULL;
636 // Free up memory: Custom test class properties.
637 // Note: Private properties cannot be cleaned up.
638 $rc = new \ReflectionClass(__CLASS__);
640 foreach ($rc->getProperties() as $property) {
641 $blacklist[$property->name] = $property->getDeclaringClass()->name;
643 $rc = new \ReflectionClass($this);
644 foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $property) {
645 if (!$property->isStatic() && !isset($blacklist[$property->name])) {
646 $this->{$property->name} = NULL;
650 // Clean FileCache cache.
653 // Clean up statics, container, and settings.
654 if (function_exists('drupal_static_reset')) {
655 drupal_static_reset();
657 \Drupal::unsetContainer();
658 $this->container = NULL;
667 * Additional tear down method to close the connection at the end.
669 public function tearDownCloseDatabaseConnection() {
670 // Destroy the database connection, which for example removes the memory
671 // from sqlite in memory.
672 foreach (Database::getAllConnectionInfo() as $key => $targets) {
673 Database::removeConnection($key);
678 * Installs default configuration for a given list of modules.
680 * @param string|string[] $modules
681 * A list of modules for which to install default configuration.
683 * @throws \LogicException
684 * If any module in $modules is not enabled.
686 protected function installConfig($modules) {
687 foreach ((array) $modules as $module) {
688 if (!$this->container->get('module_handler')->moduleExists($module)) {
689 throw new \LogicException("$module module is not enabled.");
691 $this->container->get('config.installer')->installDefaultConfig('module', $module);
696 * Installs database tables from a module schema definition.
698 * @param string $module
699 * The name of the module that defines the table's schema.
700 * @param string|array $tables
701 * The name or an array of the names of the tables to install.
703 * @throws \LogicException
704 * If $module is not enabled or the table schema cannot be found.
706 protected function installSchema($module, $tables) {
707 // drupal_get_module_schema() is technically able to install a schema
708 // of a non-enabled module, but its ability to load the module's .install
709 // file depends on many other factors. To prevent differences in test
710 // behavior and non-reproducible test failures, we only allow the schema of
711 // explicitly loaded/enabled modules to be installed.
712 if (!$this->container->get('module_handler')->moduleExists($module)) {
713 throw new \LogicException("$module module is not enabled.");
715 $tables = (array) $tables;
716 foreach ($tables as $table) {
717 $schema = drupal_get_module_schema($module, $table);
718 if (empty($schema)) {
719 // BC layer to avoid some contrib tests to fail.
720 // @todo Remove the BC layer before 8.1.x release.
721 // @see https://www.drupal.org/node/2670360
722 // @see https://www.drupal.org/node/2670454
723 if ($module == 'system') {
726 throw new \LogicException("$module module does not define a schema for table '$table'.");
728 $this->container->get('database')->schema()->createTable($table, $schema);
733 * Installs the storage schema for a specific entity type.
735 * @param string $entity_type_id
736 * The ID of the entity type.
738 protected function installEntitySchema($entity_type_id) {
739 /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
740 $entity_manager = $this->container->get('entity.manager');
741 $entity_type = $entity_manager->getDefinition($entity_type_id);
742 $entity_manager->onEntityTypeCreate($entity_type);
744 // For test runs, the most common storage backend is a SQL database. For
745 // this case, ensure the tables got created.
746 $storage = $entity_manager->getStorage($entity_type_id);
747 if ($storage instanceof SqlEntityStorageInterface) {
748 $tables = $storage->getTableMapping()->getTableNames();
749 $db_schema = $this->container->get('database')->schema();
750 $all_tables_exist = TRUE;
751 foreach ($tables as $table) {
752 if (!$db_schema->tableExists($table)) {
753 $this->fail(SafeMarkup::format('Installed entity type table for the %entity_type entity type: %table', [
754 '%entity_type' => $entity_type_id,
757 $all_tables_exist = FALSE;
760 if ($all_tables_exist) {
761 $this->pass(SafeMarkup::format('Installed entity type tables for the %entity_type entity type: %tables', [
762 '%entity_type' => $entity_type_id,
763 '%tables' => '{' . implode('}, {', $tables) . '}',
770 * Enables modules for this test.
772 * To install test modules outside of the testing environment, add
774 * $settings['extension_discovery_scan_tests'] = TRUE;
776 * to your settings.php.
778 * @param string[] $modules
779 * A list of modules to enable. Dependencies are not resolved; i.e.,
780 * multiple modules have to be specified individually. The modules are only
781 * added to the active module list and loaded; i.e., their database schema
782 * is not installed. hook_install() is not invoked. A custom module weight
785 * @throws \LogicException
786 * If any module in $modules is already enabled.
787 * @throws \RuntimeException
788 * If a module is not enabled after enabling it.
790 protected function enableModules(array $modules) {
791 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
792 if ($trace[1]['function'] === 'setUp') {
793 trigger_error('KernelTestBase::enableModules() should not be called from setUp(). Use the $modules property instead.', E_USER_DEPRECATED);
797 // Perform an ExtensionDiscovery scan as this function may receive a
798 // profile that is not the current profile, and we don't yet have a cached
799 // way to receive inactive profile information.
800 // @todo Remove as part of https://www.drupal.org/node/2186491
801 $listing = new ExtensionDiscovery(\Drupal::root());
802 $module_list = $listing->scan('module');
803 // In ModuleHandlerTest we pass in a profile as if it were a module.
804 $module_list += $listing->scan('profile');
806 // Set the list of modules in the extension handler.
807 $module_handler = $this->container->get('module_handler');
809 // Write directly to active storage to avoid early instantiation of
810 // the event dispatcher which can prevent modules from registering events.
811 $active_storage = $this->container->get('config.storage');
812 $extension_config = $active_storage->read('core.extension');
814 foreach ($modules as $module) {
815 if ($module_handler->moduleExists($module)) {
816 throw new \LogicException("$module module is already enabled.");
818 $module_handler->addModule($module, $module_list[$module]->getPath());
819 // Maintain the list of enabled modules in configuration.
820 $extension_config['module'][$module] = 0;
822 $active_storage->write('core.extension', $extension_config);
824 // Update the kernel to make their services available.
825 $extensions = $module_handler->getModuleList();
826 $this->container->get('kernel')->updateModules($extensions, $extensions);
828 // Ensure isLoaded() is TRUE in order to make
829 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
830 // Note that the kernel has rebuilt the container; this $module_handler is
831 // no longer the $module_handler instance from above.
832 $module_handler = $this->container->get('module_handler');
833 $module_handler->reload();
834 foreach ($modules as $module) {
835 if (!$module_handler->moduleExists($module)) {
836 throw new \RuntimeException("$module module is not enabled after enabling it.");
842 * Disables modules for this test.
844 * @param string[] $modules
845 * A list of modules to disable. Dependencies are not resolved; i.e.,
846 * multiple modules have to be specified with dependent modules first.
847 * Code of previously enabled modules is still loaded. The modules are only
848 * removed from the active module list.
850 * @throws \LogicException
851 * If any module in $modules is already disabled.
852 * @throws \RuntimeException
853 * If a module is not disabled after disabling it.
855 protected function disableModules(array $modules) {
856 // Unset the list of modules in the extension handler.
857 $module_handler = $this->container->get('module_handler');
858 $module_filenames = $module_handler->getModuleList();
859 $extension_config = $this->config('core.extension');
860 foreach ($modules as $module) {
861 if (!$module_handler->moduleExists($module)) {
862 throw new \LogicException("$module module cannot be disabled because it is not enabled.");
864 unset($module_filenames[$module]);
865 $extension_config->clear('module.' . $module);
867 $extension_config->save();
868 $module_handler->setModuleList($module_filenames);
869 $module_handler->resetImplementations();
870 // Update the kernel to remove their services.
871 $this->container->get('kernel')->updateModules($module_filenames, $module_filenames);
873 // Ensure isLoaded() is TRUE in order to make
874 // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
875 // Note that the kernel has rebuilt the container; this $module_handler is
876 // no longer the $module_handler instance from above.
877 $module_handler = $this->container->get('module_handler');
878 $module_handler->reload();
879 foreach ($modules as $module) {
880 if ($module_handler->moduleExists($module)) {
881 throw new \RuntimeException("$module module is not disabled after disabling it.");
887 * Renders a render array.
889 * @param array $elements
890 * The elements to render.
893 * The rendered string output (typically HTML).
895 protected function render(array &$elements) {
896 // \Drupal\Core\Render\BareHtmlPageRenderer::renderBarePage calls out to
897 // system_page_attachments() directly.
898 if (!\Drupal::moduleHandler()->moduleExists('system')) {
899 throw new \Exception(__METHOD__ . ' requires system module to be installed.');
902 // Use the bare HTML page renderer to render our links.
903 $renderer = $this->container->get('bare_html_page_renderer');
904 $response = $renderer->renderBarePage($elements, '', 'maintenance_page');
906 // Glean the content from the response object.
907 $content = $response->getContent();
908 $this->setRawContent($content);
909 $this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
914 * Sets an in-memory Settings variable.
916 * @param string $name
917 * The name of the setting to set.
918 * @param bool|string|int|array|null $value
919 * The value to set. Note that array values are replaced entirely; use
920 * \Drupal\Core\Site\Settings::get() to perform custom merges.
922 protected function setSetting($name, $value) {
923 $settings = Settings::getInstance() ? Settings::getAll() : [];
924 $settings[$name] = $value;
925 new Settings($settings);
929 * Stops test execution.
931 protected function stop() {
932 $this->getTestResultObject()->stop();
936 * Dumps the current state of the virtual filesystem to STDOUT.
938 protected function vfsDump() {
939 vfsStream::inspect(new vfsStreamPrintVisitor());
943 * Returns the modules to enable for this test.
945 * @param string $class
946 * The fully-qualified class name of this test.
950 private static function getModulesToEnable($class) {
953 if (property_exists($class, 'modules')) {
954 // Only add the modules, if the $modules property was not inherited.
955 $rp = new \ReflectionProperty($class, 'modules');
956 if ($rp->class == $class) {
957 $modules[$class] = $class::$modules;
960 $class = get_parent_class($class);
962 // Modules have been collected in reverse class hierarchy order; modules
963 // defined by base classes should be sorted first. Then, merge the results
965 $modules = array_reverse($modules);
966 return call_user_func_array('array_merge_recursive', $modules);
972 protected function prepareTemplate(\Text_Template $template) {
973 $bootstrap_globals = '';
975 // Fix missing bootstrap.php when $preserveGlobalState is FALSE.
976 // @see https://github.com/sebastianbergmann/phpunit/pull/797
977 $bootstrap_globals .= '$__PHPUNIT_BOOTSTRAP = ' . var_export($GLOBALS['__PHPUNIT_BOOTSTRAP'], TRUE) . ";\n";
979 // Avoid repetitive test namespace discoveries to improve performance.
980 // @see /core/tests/bootstrap.php
981 $bootstrap_globals .= '$namespaces = ' . var_export($GLOBALS['namespaces'], TRUE) . ";\n";
985 'included_files' => '',
986 'globals' => $bootstrap_globals,
991 * Returns whether the current test method is running in a separate process.
993 * Note that KernelTestBase will run in a separate process by default.
997 * @see \Drupal\KernelTests\KernelTestBase::$runTestInSeparateProcess
998 * @see https://github.com/sebastianbergmann/phpunit/pull/1350
1000 * @deprecated in Drupal 8.4.x, for removal before the Drupal 9.0.0 release.
1001 * KernelTestBase tests are always run in isolated processes.
1003 protected function isTestInIsolation() {
1004 @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);
1005 return function_exists('__phpunit_run_isolated_test');
1009 * BC: Automatically resolve former KernelTestBase class properties.
1011 * Test authors should follow the provided instructions and adjust their tests
1014 * @deprecated in Drupal 8.0.x, will be removed before Drupal 8.2.0.
1016 public function __get($name) {
1017 if (in_array($name, [
1018 'public_files_directory',
1019 'private_files_directory',
1020 'temp_files_directory',
1021 'translation_files_directory',
1023 // @comment it in again.
1024 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use the regular API method to retrieve it instead (e.g., Settings).", $name), E_USER_DEPRECATED);
1026 case 'public_files_directory':
1027 return Settings::get('file_public_path', \Drupal::service('site.path') . '/files');
1029 case 'private_files_directory':
1030 return Settings::get('file_private_path');
1032 case 'temp_files_directory':
1033 return file_directory_temp();
1035 case 'translation_files_directory':
1036 return Settings::get('file_public_path', \Drupal::service('site.path') . '/translations');
1040 if ($name === 'configDirectories') {
1041 trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use config_get_config_directory() directly instead.", $name), E_USER_DEPRECATED);
1043 CONFIG_SYNC_DIRECTORY => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
1048 // @see \Drupal\simpletest\TestBase
1058 'verboseDirectoryUrl',
1061 // @see \Drupal\simpletest\TestBase::prepareEnvironment()
1062 'generatedTestFiles',
1063 // Properties from the old KernelTestBase class that has been removed.
1066 if (in_array($name, $denied) || strpos($name, 'original') === 0) {
1067 throw new \RuntimeException(sprintf('TestBase::$%s property no longer exists', $name));
1072 * Prevents serializing any properties.
1074 * Kernel tests are run in a separate process. To do this PHPUnit creates a
1075 * script to run the test. If it fails, the test result object will contain a
1076 * stack trace which includes the test object. It will attempt to serialize
1077 * it. Returning an empty array prevents it from serializing anything it
1083 * @see vendor/phpunit/phpunit/src/Util/PHP/Template/TestCaseMethod.tpl.dist
1085 public function __sleep() {
1092 public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) {
1093 // Cast objects implementing MarkupInterface to string instead of
1094 // relying on PHP casting them to string depending on what they are being
1096 $expected = static::castSafeStrings($expected);
1097 $actual = static::castSafeStrings($actual);
1098 parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);