You are here

protected function KernelTestBase::enableModules in Zircon Profile 8.0

Same name in this branch
  1. 8.0 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::enableModules()
  2. 8.0 core/modules/simpletest/src/KernelTestBase.php \Drupal\simpletest\KernelTestBase::enableModules()
Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::enableModules()

Enables modules for this test.

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.

8 calls to KernelTestBase::enableModules()
KernelTestBaseTest::testCompiledContainer in core/tests/Drupal/KernelTests/KernelTestBaseTest.php
@covers ::getCompiledContainerBuilder
KernelTestBaseTest::testCompiledContainerIsDestructed in core/tests/Drupal/KernelTests/KernelTestBaseTest.php
@covers ::getCompiledContainerBuilder @depends testCompiledContainer
KernelTestBaseTest::testRegister in core/tests/Drupal/KernelTests/KernelTestBaseTest.php
@covers ::register
KernelTestBaseTest::testRender in core/tests/Drupal/KernelTests/KernelTestBaseTest.php
@covers ::render
KernelTestBaseTest::testRenderWithTheme in core/tests/Drupal/KernelTests/KernelTestBaseTest.php
@covers ::render

... See full list

File

core/tests/Drupal/KernelTests/KernelTestBase.php, line 802
Contains \Drupal\KernelTests\KernelTestBase.

Class

KernelTestBase
Base class for functional integration tests.

Namespace

Drupal\KernelTests

Code

protected function enableModules(array $modules) {
  $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  if ($trace[1]['function'] === 'setUp') {
    trigger_error('KernelTestBase::enableModules() should not be called from setUp(). Use the $modules property instead.', E_USER_DEPRECATED);
  }
  unset($trace);

  // 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(\Drupal::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)) {
      throw new \LogicException("{$module} module is already enabled.");
    }
    $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.");
    }
  }
}