You are here

function ModuleHandlerTest::testUninstallContentDependency in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testUninstallContentDependency()

Tests uninstalling a module that has content.

File

core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php, line 221
Contains \Drupal\system\Tests\Extension\ModuleHandlerTest.

Class

ModuleHandlerTest
Tests ModuleHandler functionality.

Namespace

Drupal\Tests\system\Kernel\Extension

Code

function testUninstallContentDependency() {
  $this
    ->enableModules(array(
    'module_test',
    'entity_test',
    'text',
    'user',
    'help',
  ));
  $this
    ->assertTrue($this
    ->moduleHandler()
    ->moduleExists('entity_test'), 'Test module is enabled.');
  $this
    ->assertTrue($this
    ->moduleHandler()
    ->moduleExists('module_test'), 'Test module is enabled.');
  $this
    ->installSchema('user', 'users_data');
  $entity_types = \Drupal::entityManager()
    ->getDefinitions();
  foreach ($entity_types as $entity_type) {
    if ('entity_test' == $entity_type
      ->getProvider()) {
      $this
        ->installEntitySchema($entity_type
        ->id());
    }
  }

  // Create a fake dependency.
  // entity_test will depend on help. This way help can not be uninstalled
  // when there is test content preventing entity_test from being uninstalled.
  \Drupal::state()
    ->set('module_test.dependency', 'dependency');
  drupal_static_reset('system_rebuild_module_data');

  // Create an entity so that the modules can not be disabled.
  $entity = entity_create('entity_test', array(
    'name' => $this
      ->randomString(),
  ));
  $entity
    ->save();

  // Uninstalling entity_test is not possible when there is content.
  try {
    $message = 'ModuleHandler::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
    $this
      ->moduleInstaller()
      ->uninstall(array(
      'entity_test',
    ));
    $this
      ->fail($message);
  } catch (ModuleUninstallValidatorException $e) {
    $this
      ->pass(get_class($e) . ': ' . $e
      ->getMessage());
  }

  // Uninstalling help needs entity_test to be un-installable.
  try {
    $message = 'ModuleHandler::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
    $this
      ->moduleInstaller()
      ->uninstall(array(
      'help',
    ));
    $this
      ->fail($message);
  } catch (ModuleUninstallValidatorException $e) {
    $this
      ->pass(get_class($e) . ': ' . $e
      ->getMessage());
  }

  // Deleting the entity.
  $entity
    ->delete();
  $result = $this
    ->moduleInstaller()
    ->uninstall(array(
    'help',
  ));
  $this
    ->assertTrue($result, 'ModuleHandler::uninstall() returns TRUE.');
  $this
    ->assertEqual(drupal_get_installed_schema_version('entity_test'), SCHEMA_UNINSTALLED, "entity_test module was uninstalled.");
}