function ModuleHandlerTest::testUninstallProfileDependency

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testUninstallProfileDependency()
  2. 8.9.x core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testUninstallProfileDependency()
  3. 10 core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php \Drupal\Tests\system\Kernel\Extension\ModuleHandlerTest::testUninstallProfileDependency()

Tests uninstalling a module installed by a profile.

File

core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php, line 170

Class

ModuleHandlerTest
Tests ModuleHandler functionality.

Namespace

Drupal\Tests\system\Kernel\Extension

Code

public function testUninstallProfileDependency() : void {
    $profile = 'testing_install_profile_dependencies';
    $dependency = 'dblog';
    $non_dependency = 'ban';
    $this->setInstallProfile($profile);
    // Prime the \Drupal\Core\Extension\ExtensionList::getPathname() static
    // cache with the location of the testing_install_profile_dependencies
    // profile as it is not the currently active profile and we don't yet have
    // any cached way to retrieve its location.
    // @todo Remove as part of https://www.drupal.org/node/2186491
    $profile_list = \Drupal::service('extension.list.profile');
    assert($profile_list instanceof ProfileExtensionList);
    $profile_list->setPathname($profile, 'core/profiles/' . $profile . '/' . $profile . '.info.yml');
    $this->enableModules([
        'module_test',
        $profile,
    ]);
    $data = \Drupal::service('extension.list.module')->reset()
        ->getList();
    $this->assertArrayHasKey($dependency, $data[$profile]->requires);
    $this->assertArrayNotHasKey($non_dependency, $data[$profile]->requires);
    $this->moduleInstaller()
        ->install([
        $dependency,
        $non_dependency,
    ]);
    $this->assertTrue($this->moduleHandler()
        ->moduleExists($dependency));
    // Uninstall the profile module that is not a dependent.
    
    /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
    $update_registry = \Drupal::service('update.update_hook_registry');
    $result = $this->moduleInstaller()
        ->uninstall([
        $non_dependency,
    ]);
    $this->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
    $this->assertFalse($this->moduleHandler()
        ->moduleExists($non_dependency));
    $this->assertEquals($update_registry::SCHEMA_UNINSTALLED, $update_registry->getInstalledVersion($non_dependency), "{$non_dependency} module was uninstalled.");
    // Verify that the installation profile itself was not uninstalled.
    $uninstalled_modules = \Drupal::state()->get('module_test.uninstall_order', []);
    $this->assertContains($non_dependency, $uninstalled_modules, "{$non_dependency} module is in the list of uninstalled modules.");
    $this->assertNotContains($profile, $uninstalled_modules, 'The installation profile is not in the list of uninstalled modules.');
    // Try uninstalling the required module.
    try {
        $this->moduleInstaller()
            ->uninstall([
            $dependency,
        ]);
        $this->fail('Expected ModuleUninstallValidatorException not thrown');
    } catch (ModuleUninstallValidatorException $e) {
        $this->assertEquals("The following reasons prevent the modules from being uninstalled: The 'Testing install profile dependencies' install profile requires 'Database Logging'", $e->getMessage());
    }
    // Try uninstalling the install profile.
    $this->assertSame('testing_install_profile_dependencies', $this->container
        ->getParameter('install_profile'));
    $result = $this->moduleInstaller()
        ->uninstall([
        $profile,
    ]);
    $this->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
    $this->assertFalse($this->moduleHandler()
        ->moduleExists($profile));
    $this->assertFalse($this->container
        ->getParameter('install_profile'));
    // Try uninstalling the required module again.
    $result = $this->moduleInstaller()
        ->uninstall([
        $dependency,
    ]);
    $this->assertTrue($result, 'ModuleInstaller::uninstall() returns TRUE.');
    $this->assertFalse($this->moduleHandler()
        ->moduleExists($dependency));
}

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