View source
<?php
namespace Drupal\Tests\system\Kernel\Extension;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Extension\MissingDependencyException;
use Drupal\Core\Extension\ModuleUninstallValidatorException;
use Drupal\Core\Extension\ProfileExtensionList;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
class ModuleHandlerTest extends KernelTestBase {
protected static $modules = [
'system',
];
public function testModuleList() {
$module_list = [
'system',
];
$database_module = \Drupal::database()
->getProvider();
if ($database_module !== 'core') {
$module_list[] = $database_module;
}
sort($module_list);
$this
->assertModuleList($module_list, 'Initial');
$this
->moduleInstaller()
->install([
'ban',
]);
$module_list[] = 'ban';
sort($module_list);
$this
->assertModuleList($module_list, 'After adding a module');
module_set_weight('ban', 20);
unset($module_list[array_search('ban', $module_list)]);
$module_list[] = 'ban';
$this
->assertModuleList($module_list, 'After changing weights');
$fixed_list = [
'system' => 'core/modules/system/system.module',
'menu' => 'core/modules/menu/menu.module',
];
$this
->moduleHandler()
->setModuleList($fixed_list);
$new_module_list = array_combine(array_keys($fixed_list), array_keys($fixed_list));
$this
->assertModuleList($new_module_list, 'When using a fixed list');
}
protected function assertModuleList(array $expected_values, string $condition) : void {
$expected_values = array_values(array_unique($expected_values));
$enabled_modules = array_keys($this->container
->get('module_handler')
->getModuleList());
$this
->assertEquals($expected_values, $enabled_modules, new FormattableMarkup('@condition: extension handler returns correct results', [
'@condition' => $condition,
]));
}
public function testDependencyResolution() {
$this
->enableModules([
'module_test',
]);
$this
->assertTrue($this
->moduleHandler()
->moduleExists('module_test'), 'Test module is enabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('dblog'), 'dblog module is disabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('config'), 'Config module is disabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('help'), 'Help module is disabled.');
\Drupal::state()
->set('module_test.dependency', 'missing dependency');
try {
$result = $this
->moduleInstaller()
->install([
'dblog',
]);
$this
->fail('ModuleInstaller::install() throws an exception if dependencies are missing.');
} catch (MissingDependencyException $e) {
}
$this
->assertFalse($this
->moduleHandler()
->moduleExists('dblog'), 'ModuleInstaller::install() aborts if dependencies are missing.');
\Drupal::state()
->set('module_test.dependency', 'dependency');
$result = $this
->moduleInstaller()
->install([
'dblog',
]);
$this
->assertTrue($result, 'ModuleInstaller::install() returns the correct value.');
$this
->assertTrue($this
->moduleHandler()
->moduleExists('config'));
$this
->assertTrue($this
->moduleHandler()
->moduleExists('help'));
$this
->assertTrue($this
->moduleHandler()
->moduleExists('dblog'), 'Module installation with dependencies succeeded.');
$module_order = \Drupal::state()
->get('module_test.install_order', []);
$this
->assertEquals([
'help',
'config',
'dblog',
], $module_order);
$result = $this
->moduleInstaller()
->uninstall([
'config',
'help',
'dblog',
]);
$this
->assertTrue($result, 'ModuleInstaller::uninstall() returned TRUE.');
$update_registry = \Drupal::service('update.update_hook_registry');
foreach ([
'dblog',
'config',
'help',
] as $module) {
$this
->assertEquals($update_registry::SCHEMA_UNINSTALLED, $update_registry
->getInstalledVersion($module), "{$module} module was uninstalled.");
}
$uninstalled_modules = \Drupal::state()
->get('module_test.uninstall_order', []);
$this
->assertEquals([
'dblog',
'config',
'help',
], $uninstalled_modules, 'Modules were uninstalled in the correct order.');
\Drupal::state()
->set('module_test.dependency', 'version dependency');
$result = $this
->moduleInstaller()
->install([
'dblog',
]);
$this
->assertTrue($result, 'ModuleInstaller::install() returns the correct value.');
$this
->assertTrue($this
->moduleHandler()
->moduleExists('config'));
$this
->assertTrue($this
->moduleHandler()
->moduleExists('help'));
$this
->assertTrue($this
->moduleHandler()
->moduleExists('dblog'), 'Module installation with version dependencies succeeded.');
$enable_order = \Drupal::state()
->get('module_test.install_order', []);
$this
->assertSame([
'help',
'config',
'dblog',
], $enable_order);
}
public function testUninstallProfileDependency() {
$profile = 'testing_install_profile_dependencies';
$dependency = 'dblog';
$non_dependency = 'ban';
$this
->setInstallProfile($profile);
$profile_list = \Drupal::service('extension.list.profile');
assert($profile_list instanceof ProfileExtensionList);
$profile_list
->setPathname($profile, 'core/profiles/' . $profile . '/' . $profile . '.info.yml');
$this
->enableModules([
'module_test',
$profile,
]);
$data = \Drupal::service('extension.list.module')
->reset()
->getList();
$this
->assertArrayHasKey($dependency, $data[$profile]->requires);
$this
->assertArrayNotHasKey($non_dependency, $data[$profile]->requires);
$this
->moduleInstaller()
->install([
$dependency,
$non_dependency,
]);
$this
->assertTrue($this
->moduleHandler()
->moduleExists($dependency));
$update_registry = \Drupal::service('update.update_hook_registry');
$result = $this
->moduleInstaller()
->uninstall([
$non_dependency,
]);
$this
->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists($non_dependency));
$this
->assertEquals($update_registry::SCHEMA_UNINSTALLED, $update_registry
->getInstalledVersion($non_dependency), "{$non_dependency} module was uninstalled.");
$uninstalled_modules = \Drupal::state()
->get('module_test.uninstall_order', []);
$this
->assertContains($non_dependency, $uninstalled_modules, "{$non_dependency} module is in the list of uninstalled modules.");
$this
->assertNotContains($profile, $uninstalled_modules, 'The installation profile is not in the list of uninstalled modules.');
$this
->expectException(ModuleUninstallValidatorException::class);
$this
->expectExceptionMessage('The following reasons prevent the modules from being uninstalled: The Testing install profile dependencies module is required');
$this
->moduleInstaller()
->uninstall([
$dependency,
]);
}
public function testProfileAllDependencies() {
$profile = 'testing_install_profile_all_dependencies';
$dependencies = [
'dblog',
'ban',
];
$this
->setInstallProfile($profile);
$profile_list = \Drupal::service('extension.list.profile');
assert($profile_list instanceof ProfileExtensionList);
$profile_list
->setPathname($profile, 'core/profiles/' . $profile . '/' . $profile . '.info.yml');
$this
->enableModules([
'module_test',
$profile,
]);
$data = \Drupal::service('extension.list.module')
->reset()
->getList();
foreach ($dependencies as $dependency) {
$this
->assertArrayHasKey($dependency, $data[$profile]->requires);
}
$this
->moduleInstaller()
->install($dependencies);
foreach ($dependencies as $dependency) {
$this
->assertTrue($this
->moduleHandler()
->moduleExists($dependency));
}
$this
->expectException(ModuleUninstallValidatorException::class);
$this
->expectExceptionMessage('The following reasons prevent the modules from being uninstalled: The Testing install profile all dependencies module is required');
$this
->moduleInstaller()
->uninstall($dependencies);
}
public function testUninstallContentDependency() {
$this
->enableModules([
'module_test',
'entity_test',
'text',
'user',
'help',
]);
$this
->assertTrue($this
->moduleHandler()
->moduleExists('entity_test'), 'Test module is enabled.');
$this
->assertTrue($this
->moduleHandler()
->moduleExists('module_test'), 'Test module is enabled.');
$this
->installSchema('user', 'users_data');
$entity_types = \Drupal::entityTypeManager()
->getDefinitions();
foreach ($entity_types as $entity_type) {
if ('entity_test' == $entity_type
->getProvider()) {
$this
->installEntitySchema($entity_type
->id());
}
}
\Drupal::state()
->set('module_test.dependency', 'dependency');
$entity = EntityTest::create([
'name' => $this
->randomString(),
]);
$entity
->save();
try {
$message = 'ModuleInstaller::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
$this
->moduleInstaller()
->uninstall([
'entity_test',
]);
$this
->fail($message);
} catch (ModuleUninstallValidatorException $e) {
}
try {
$message = 'ModuleInstaller::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
$this
->moduleInstaller()
->uninstall([
'help',
]);
$this
->fail($message);
} catch (ModuleUninstallValidatorException $e) {
}
$entity
->delete();
$update_registry = \Drupal::service('update.update_hook_registry');
$result = $this
->moduleInstaller()
->uninstall([
'help',
]);
$this
->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
$this
->assertEquals($update_registry::SCHEMA_UNINSTALLED, $update_registry
->getInstalledVersion('entity_test'), "entity_test module was uninstalled.");
}
public function testModuleMetaData() {
$modules = $this->container
->get('extension.list.module')
->getList();
$this
->assertNotEmpty($modules['system']->info['mtime'], 'The system.info.yml file modification time field is present.');
$test_mtime = !empty($modules['system']->info['mtime']) ? $modules['system']->info['mtime'] : 0;
$this
->assertIsNumeric($test_mtime);
$this
->assertGreaterThan(0, $test_mtime);
}
public function testModuleStreamWrappers() {
$stream_wrappers = \Drupal::service('stream_wrapper_manager')
->getWrappers();
$this
->assertFalse(isset($stream_wrappers['dummy']));
$this
->moduleInstaller()
->install([
'file_test',
]);
file_exists('dummy://');
$stream_wrappers = \Drupal::service('stream_wrapper_manager')
->getWrappers();
$this
->assertTrue(isset($stream_wrappers['dummy']));
}
public function testThemeMetaData() {
$themes = \Drupal::service('theme_handler')
->rebuildThemeData();
$this
->assertNotEmpty($themes['olivero']->info['mtime'], 'The olivero.info.yml file modification time field is present.');
$test_mtime = !empty($themes['olivero']->info['mtime']) ? $themes['olivero']->info['mtime'] : 0;
$this
->assertIsNumeric($test_mtime);
$this
->assertGreaterThan(0, $test_mtime);
}
protected function moduleHandler() {
return $this->container
->get('module_handler');
}
protected function moduleInstaller() {
return $this->container
->get('module_installer');
}
}