function ModuleHandlerTest::testUninstallContentDependency

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

Class

ModuleHandlerTest
Tests ModuleHandler functionality.

Namespace

Drupal\Tests\system\Kernel\Extension

Code

public function testUninstallContentDependency() {
    $this->enableModules([
        '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::entityTypeManager()->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');
    // Create an entity so that the modules can not be disabled.
    $entity = EntityTest::create([
        'name' => $this->randomString(),
    ]);
    $entity->save();
    // Uninstalling entity_test is not possible when there is content.
    try {
        $message = 'ModuleInstaller::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
        $this->moduleInstaller()
            ->uninstall([
            'entity_test',
        ]);
        $this->fail($message);
    } catch (ModuleUninstallValidatorException $e) {
        // Expected exception; just continue testing.
    }
    // Uninstalling help needs entity_test to be un-installable.
    try {
        $message = 'ModuleInstaller::uninstall() throws ModuleUninstallValidatorException upon uninstalling a module which does not pass validation.';
        $this->moduleInstaller()
            ->uninstall([
            'help',
        ]);
        $this->fail($message);
    } catch (ModuleUninstallValidatorException $e) {
        // Expected exception; just continue testing.
    }
    // Deleting the entity.
    $entity->delete();
    $result = $this->moduleInstaller()
        ->uninstall([
        'help',
    ]);
    $this->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
    $this->assertEqual(drupal_get_installed_schema_version('entity_test'), SCHEMA_UNINSTALLED, "entity_test module was uninstalled.");
}

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