You are here

function ModuleHandlerTest::testDependencyResolution in Zircon Profile 8.0

Same name and namespace in other branches
  1. 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 109
Contains \Drupal\system\Tests\Extension\ModuleHandlerTest.

Class

ModuleHandlerTest
Tests ModuleHandler functionality.

Namespace

Drupal\Tests\system\Kernel\Extension

Code

function testDependencyResolution() {
  $this
    ->enableModules(array(
    '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');
  drupal_static_reset('system_rebuild_module_data');
  try {
    $result = $this
      ->moduleInstaller()
      ->install(array(
      'color',
    ));
    $this
      ->fail(t('ModuleInstaller::install() throws an exception if dependencies are missing.'));
  } catch (\Drupal\Core\Extension\MissingDependencyException $e) {
    $this
      ->pass(t('ModuleInstaller::install() throws an exception if dependencies are missing.'));
  }
  $this
    ->assertFalse($this
    ->moduleHandler()
    ->moduleExists('color'), 'ModuleHandler::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');
  drupal_static_reset('system_rebuild_module_data');
  $result = $this
    ->moduleInstaller()
    ->install(array(
    'color',
  ));
  $this
    ->assertTrue($result, 'ModuleHandler::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') ?: array();
  $this
    ->assertEqual($module_order, array(
    'help',
    'config',
    'color',
  ));

  // Uninstall all three modules explicitly, but in the incorrect order,
  // and make sure that ModuleHandler::uninstall() uninstalled them in the
  // correct sequence.
  $result = $this
    ->moduleInstaller()
    ->uninstall(array(
    'config',
    'help',
    'color',
  ));
  $this
    ->assertTrue($result, 'ModuleHandler::uninstall() returned TRUE.');
  foreach (array(
    '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') ?: array();
  $this
    ->assertEqual($uninstalled_modules, array(
    '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\ModuleHandler::install() still works.
  \Drupal::state()
    ->set('module_test.dependency', 'version dependency');
  drupal_static_reset('system_rebuild_module_data');
  $result = $this
    ->moduleInstaller()
    ->install(array(
    'color',
  ));
  $this
    ->assertTrue($result, 'ModuleHandler::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') ?: array();
  $this
    ->assertIdentical($enable_order, array(
    'help',
    'config',
    'color',
  ));
}