public function ModuleHandlerTest::testDependencyResolution in Drupal 9
Same name and namespace in other branches
- 8 core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testDependencyResolution()
Tests dependency resolution.
Intentionally using fake dependencies added via hook_system_info_alter() for modules that normally do not have any dependencies.
To simplify things further, all of the manipulated modules are either purely UI-facing or live at the "bottom" of all dependency chains.
See also
module_test_system_info_alter()
https://www.drupal.org/files/issues/dep.gv__0.png
File
- core/
modules/ system/ tests/ src/ Kernel/ Extension/ ModuleHandlerTest.php, line 81
Class
- ModuleHandlerTest
- Tests ModuleHandler functionality.
Namespace
Drupal\Tests\system\Kernel\ExtensionCode
public function testDependencyResolution() {
$this
->enableModules([
'module_test',
]);
$this
->assertTrue($this
->moduleHandler()
->moduleExists('module_test'), 'Test module is enabled.');
// Ensure that modules are not enabled.
$this
->assertFalse($this
->moduleHandler()
->moduleExists('color'), 'Color module is disabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('config'), 'Config module is disabled.');
$this
->assertFalse($this
->moduleHandler()
->moduleExists('help'), 'Help module is disabled.');
// Create a missing fake dependency.
// Color will depend on Config, which depends on a non-existing module Foo.
// Nothing should be installed.
\Drupal::state()
->set('module_test.dependency', 'missing dependency');
try {
$result = $this
->moduleInstaller()
->install([
'color',
]);
$this
->fail('ModuleInstaller::install() throws an exception if dependencies are missing.');
} catch (MissingDependencyException $e) {
// Expected exception; just continue testing.
}
$this
->assertFalse($this
->moduleHandler()
->moduleExists('color'), 'ModuleInstaller::install() aborts if dependencies are missing.');
// Fix the missing dependency.
// Color module depends on Config. Config depends on Help module.
\Drupal::state()
->set('module_test.dependency', 'dependency');
$result = $this
->moduleInstaller()
->install([
'color',
]);
$this
->assertTrue($result, 'ModuleInstaller::install() returns the correct value.');
// Verify that the fake dependency chain was installed.
$this
->assertTrue($this
->moduleHandler()
->moduleExists('config'));
$this
->assertTrue($this
->moduleHandler()
->moduleExists('help'));
// Verify that the original module was installed.
$this
->assertTrue($this
->moduleHandler()
->moduleExists('color'), 'Module installation with dependencies succeeded.');
// Verify that the modules were enabled in the correct order.
$module_order = \Drupal::state()
->get('module_test.install_order', []);
$this
->assertEquals([
'help',
'config',
'color',
], $module_order);
// Uninstall all three modules explicitly, but in the incorrect order,
// and make sure that ModuleInstaller::uninstall() uninstalled them in the
// correct sequence.
$result = $this
->moduleInstaller()
->uninstall([
'config',
'help',
'color',
]);
$this
->assertTrue($result, 'ModuleInstaller::uninstall() returned TRUE.');
/** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
$update_registry = \Drupal::service('update.update_hook_registry');
foreach ([
'color',
'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([
'color',
'config',
'help',
], $uninstalled_modules, 'Modules were uninstalled in the correct order.');
// Enable Color module again, which should enable both the Config module and
// Help module. But, this time do it with Config module declaring a
// dependency on a specific version of Help module in its info file. Make
// sure that Drupal\Core\Extension\ModuleInstaller::install() still works.
\Drupal::state()
->set('module_test.dependency', 'version dependency');
$result = $this
->moduleInstaller()
->install([
'color',
]);
$this
->assertTrue($result, 'ModuleInstaller::install() returns the correct value.');
// Verify that the fake dependency chain was installed.
$this
->assertTrue($this
->moduleHandler()
->moduleExists('config'));
$this
->assertTrue($this
->moduleHandler()
->moduleExists('help'));
// Verify that the original module was installed.
$this
->assertTrue($this
->moduleHandler()
->moduleExists('color'), 'Module installation with version dependencies succeeded.');
// Finally, verify that the modules were enabled in the correct order.
$enable_order = \Drupal::state()
->get('module_test.install_order', []);
$this
->assertSame([
'help',
'config',
'color',
], $enable_order);
}