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. 10 core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testDependencyResolution()
  3. 11.x 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 80

Class

ModuleHandlerTest
Tests ModuleHandler functionality.

Namespace

Drupal\Tests\system\Kernel\Extension

Code

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->moduleHandler()
        ->moduleExists('help'), 'Dependency chain was installed.');
    // 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->assertEqual($module_order, [
        'help',
        'config',
        'color',
    ]);
    // 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.');
    foreach ([
        'color',
        'config',
        'help',
    ] as $module) {
        $this->assertEqual(drupal_get_installed_schema_version($module), SCHEMA_UNINSTALLED, "{$module} module was uninstalled.");
    }
    $uninstalled_modules = \Drupal::state()->get('module_test.uninstall_order') ?: [];
    $this->assertEqual($uninstalled_modules, [
        'color',
        'config',
        'help',
    ], '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->moduleHandler()
        ->moduleExists('help'), 'Dependency chain was installed.');
    // 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->assertIdentical($enable_order, [
        'help',
        'config',
        'color',
    ]);
}

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