function ModuleHandlerTest::getModuleHandler
Get a module handler object to test.
Since we have to run these tests in separate processes, we have to use test objects which are serializable. Since ModuleHandler will populate itself with Extension objects, and since Extension objects will try to access DRUPAL_ROOT when they're unserialized, we can't store our mocked ModuleHandler objects as a property in unit tests. They must be generated by the test method by calling this method.
Parameters
list<string, string> $modules: Module paths by module name.
array<string, array<callable-string, string>> $implementations: Module names by function name implementing hook_hook().
array<string, array<string, string>> $includes: Include files per hook.
array<string, array<string, string>> $group_includes: Group include files per hook.
bool $loadAll: TRUE to call ModuleHandler->loadAll() on the new module handler.
Return value
\Drupal\Core\Extension\ModuleHandler The module handler to test.
12 calls to ModuleHandlerTest::getModuleHandler()
- ModuleHandlerTest::testGetModuleDirectories in core/
tests/ Drupal/ Tests/ Core/ Extension/ ModuleHandlerTest.php - Tests get module directories.
- ModuleHandlerTest::testGroupIncludes in core/
tests/ Drupal/ Tests/ Core/ Extension/ ModuleHandlerTest.php - Tests group includes.
- ModuleHandlerTest::testHasImplementations in core/
tests/ Drupal/ Tests/ Core/ Extension/ ModuleHandlerTest.php - Tests hasImplementations.
- ModuleHandlerTest::testImplementsHookModuleEnabled in core/
tests/ Drupal/ Tests/ Core/ Extension/ ModuleHandlerTest.php - Tests implementations methods when module is enabled.
- ModuleHandlerTest::testInvokeAll in core/
tests/ Drupal/ Tests/ Core/ Extension/ ModuleHandlerTest.php - Tests invoke all.
File
-
core/
tests/ Drupal/ Tests/ Core/ Extension/ ModuleHandlerTest.php, line 57
Class
Namespace
Drupal\Tests\Core\ExtensionCode
protected function getModuleHandler($modules = [], $implementations = [], array $includes = [], array $group_includes = [], $loadAll = TRUE) {
// This only works if there's a single $hook.
$modules['module_handler_test'] = 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test';
$moduleList = [];
foreach ($modules as $module => $path) {
$filename = "{$module}.module";
$moduleList[$module] = [
'type' => 'module',
'pathname' => "{$path}/{$module}.info.yml",
'filename' => file_exists("{$this->root}/{$path}/{$filename}") ? $filename : NULL,
];
}
$keyvalue = new KeyValueMemoryFactory();
$cache = new NullBackend('bootstrap');
$keyvalue->get('hook_data')
->set('hook_list', $implementations);
$keyvalue->get('hook_data')
->set('includes', $includes);
$keyvalue->get('hook_data')
->set('group_includes', $group_includes);
$callableResolver = $this->createMock(CallableResolver::class);
$callableResolver->expects($this->any())
->method('getCallableFromDefinition')
->willReturnCallback(fn($definition) => $definition);
$moduleHandler = new ModuleHandler($this->root, $moduleList, $keyvalue, $callableResolver, $cache);
if ($loadAll) {
$moduleHandler->loadAll();
}
return $moduleHandler;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.