You are here

function ModuleHandlerTest::testDependencyResolution in Service Container 7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/service_container/Tests/ModuleHandlerTest.php \Drupal\service_container\Tests\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://drupal.org/files/issues/dep.gv__0.png

File

lib/Drupal/service_container/Tests/ModuleHandlerTest.php, line 131
Contains \Drupal\service_container\Tests\ModuleHandlerTest.

Class

ModuleHandlerTest
Tests the module_handler implementation of the service_container.

Namespace

Drupal\service_container\Tests

Code

function testDependencyResolution() {
  return;
  $this
    ->moduleInstaller()
    ->install(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');
  $result = $this
    ->moduleInstaller()
    ->install(array(
    'color',
  ));
  $this
    ->assertFalse($result, 'ModuleHandler::install() returns FALSE 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',
  ));
}