You are here

protected function KernelTestBase::enableModules in Drupal 8

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

57 calls to KernelTestBase::enableModules()
CacheableMetadataCalculationTest::testCacheableMetadataCalculation in core/modules/views/tests/src/Kernel/CacheableMetadataCalculationTest.php
Tests that cacheability metadata is only calculated when needed.
CKEditorPluginManagerTest::testCssFiles in core/modules/ckeditor/tests/src/Kernel/CKEditorPluginManagerTest.php
Tests the iframe instance CSS files of plugins.
CKEditorPluginManagerTest::testEnabledPlugins in core/modules/ckeditor/tests/src/Kernel/CKEditorPluginManagerTest.php
Tests the enabling of plugins.
CKEditorTest::testBuildContentsCssJSSetting in core/modules/ckeditor/tests/src/Kernel/CKEditorTest.php
Tests CKEditor::buildContentsCssJSSetting().
CKEditorTest::testBuildToolbarJSSetting in core/modules/ckeditor/tests/src/Kernel/CKEditorTest.php
Tests CKEditor::buildToolbarJSSetting().

... See full list

File

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

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($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.");
    }
  }
}