You are here

protected function KernelTestBase::enableModules in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::enableModules()
  2. 9 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.

31 calls to KernelTestBase::enableModules()
CommentDepthTest::testCommentDepth in core/modules/comment/tests/src/Kernel/Views/CommentDepthTest.php
Test the comment depth.
CommentLinksTest::testLinkReply in core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php
Tests the comment reply link.
ConfigInstallTest::testCollectionInstallationCollections in core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php
Tests config objects in collections are installed as expected.
ConfigInstallTest::testCollectionInstallationNoCollections in core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php
Tests that collections are ignored if the event does not return anything.
ConfigInstallTest::testModuleInstallation in core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php
Tests module installation.

... See full list

File

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

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 enabled after enabling it.");
    }
  }
}