public function ModuleHandlerTest::testUninstallProfileDependency in Drupal 9
Same name and namespace in other branches
- 8 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 161
Class
- ModuleHandlerTest
- Tests ModuleHandler functionality.
Namespace
Drupal\Tests\system\Kernel\ExtensionCode
public function testUninstallProfileDependency() {
$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.
$this
->expectException(ModuleUninstallValidatorException::class);
$this
->expectExceptionMessage('The following reasons prevent the modules from being uninstalled: The Testing install profile dependencies module is required');
$this
->moduleInstaller()
->uninstall([
$dependency,
]);
}