function ModuleHandlerTest::testDependencyResolution

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testDependencyResolution()
  2. 8.9.x core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testDependencyResolution()
  3. 10 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 90

Class

ModuleHandlerTest
Tests ModuleHandler functionality.

Namespace

Drupal\Tests\system\Kernel\Extension

Code

public function testDependencyResolution() : void {
    $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('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.');
    // Create a missing fake dependency.
    // dblog 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([
            'dblog',
        ]);
        $this->fail('ModuleInstaller::install() throws an exception if dependencies are missing.');
    } catch (MissingDependencyException) {
        // Expected exception; just continue testing.
    }
    $this->assertFalse($this->moduleHandler()
        ->moduleExists('dblog'), 'ModuleInstaller::install() aborts if dependencies are missing.');
    // Fix the missing dependency.
    // dblog module depends on Config. Config depends on Help module.
    \Drupal::state()->set('module_test.dependency', 'dependency');
    $result = $this->moduleInstaller()
        ->install([
        'dblog',
    ]);
    $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('dblog'), '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',
        'dblog',
    ], $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',
        'dblog',
    ]);
    $this->assertTrue($result, 'ModuleInstaller::uninstall() returned TRUE.');
    
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $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.');
    // Enable dblog 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([
        'dblog',
    ]);
    $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('dblog'), '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',
        'dblog',
    ], $enable_order);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.