function KernelTestBase::enableModules

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::enableModules()
  2. 8.9.x core/modules/simpletest/src/KernelTestBase.php \Drupal\simpletest\KernelTestBase::enableModules()
  3. 8.9.x core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::enableModules()
  4. 10 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::enableModules()

Enables modules for this test.

This method does not install modules fully. Services and hooks for the module are available, but the install process is not performed.

To install test modules outside of the testing environment, add

$settings['extension_discovery_scan_tests'] = TRUE;

to your settings.php.

Parameters

string[] $modules: A list of modules to enable. Dependencies are not resolved; i.e., multiple modules have to be specified individually. The modules are only added to the active module list and loaded; i.e., their database schema is not installed. hook_install() is not invoked. A custom module weight is not applied.

Throws

\LogicException If any module in $modules is already enabled.

\RuntimeException If a module is not enabled after enabling it.

82 calls to KernelTestBase::enableModules()
AddItemToToolbarConfigActionTest::testActionRequiresCKEditor5 in core/modules/ckeditor5/tests/src/Kernel/ConfigAction/AddItemToToolbarConfigActionTest.php
AddModerationConfigActionTest::testDeriverAdminLabel in core/modules/content_moderation/tests/src/Kernel/ConfigAction/AddModerationConfigActionTest.php
AddModerationConfigActionTest::testWorkflowMustBeContentModeration in core/modules/content_moderation/tests/src/Kernel/ConfigAction/AddModerationConfigActionTest.php
CacheableMetadataCalculationTest::testCacheableMetadataCalculation in core/modules/views/tests/src/Kernel/CacheableMetadataCalculationTest.php
Tests that cacheability metadata is only calculated when needed.
CKEditor5PluginManagerTest::enableModules in core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php
Enables modules for this test.

... See full list

1 method overrides KernelTestBase::enableModules()
CKEditor5PluginManagerTest::enableModules in core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php
Enables modules for this test.

File

core/tests/Drupal/KernelTests/KernelTestBase.php, line 821

Class

KernelTestBase
Base class for functional integration tests.

Namespace

Drupal\KernelTests

Code

protected function enableModules(array $modules) {
    // Perform an ExtensionDiscovery scan as this function may receive a
    // profile that is not the current profile, and we don't yet have a cached
    // way to receive inactive profile information.
    // @todo Remove as part of https://www.drupal.org/node/2186491
    $listing = new ExtensionDiscovery($this->root);
    $module_list = $listing->scan('module');
    // In ModuleHandlerTest we pass in a profile as if it were a module.
    $module_list += $listing->scan('profile');
    // Set the list of modules in the extension handler.
    $module_handler = $this->container
        ->get('module_handler');
    // Write directly to active storage to avoid early instantiation of
    // the event dispatcher which can prevent modules from registering events.
    $active_storage = $this->container
        ->get('config.storage');
    $extension_config = $active_storage->read('core.extension');
    foreach ($modules as $module) {
        if ($module_handler->moduleExists($module)) {
            continue;
        }
        $module_handler->addModule($module, $module_list[$module]->getPath());
        // Maintain the list of enabled modules in configuration.
        $extension_config['module'][$module] = 0;
    }
    $active_storage->write('core.extension', $extension_config);
    // Update the kernel to make their services available.
    $extensions = $module_handler->getModuleList();
    $this->container
        ->get('kernel')
        ->updateModules($extensions, $extensions);
    // Ensure isLoaded() is TRUE in order to make
    // \Drupal\Core\Theme\ThemeManagerInterface::render() work.
    // Note that the kernel has rebuilt the container; this $module_handler is
    // no longer the $module_handler instance from above.
    $module_handler = $this->container
        ->get('module_handler');
    $module_handler->reload();
    foreach ($modules as $module) {
        if (!$module_handler->moduleExists($module)) {
            throw new \RuntimeException("{$module} module is not installed after installing it.");
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.