View source
<?php
namespace Drupal\Tests\system\Functional\Module;
use Drupal\Core\Extension\ExtensionLifecycle;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\workspaces\Entity\Workspace;
class InstallUninstallTest extends ModuleTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'system_test',
'dblog',
'taxonomy',
'update_test_postupdate',
];
public function testInstallUninstall() {
$this->container
->get('state')
->set('system_test.verbose_module_hooks', TRUE);
$this->container
->get('module_installer')
->install([
'module_test',
]);
$this
->assertEquals('module_test', $this->container
->get('state')
->get('system_test_preinstall_module'));
$this->container
->get('module_installer')
->uninstall([
'module_test',
]);
$this
->assertEquals('module_test', $this->container
->get('state')
->get('system_test_preuninstall_module'));
$this
->resetAll();
$all_modules = $this->container
->get('extension.list.module')
->getList();
$required_modules = array_filter($all_modules, function ($module) {
if (!empty($module->info['required']) || $module->status == TRUE) {
if ($module->info['package'] != 'Testing' && empty($module->info['hidden'])) {
return TRUE;
}
}
return FALSE;
});
$required_modules['help'] = $all_modules['help'];
$all_modules = array_filter($all_modules, function ($module) {
if (!empty($module->info['hidden']) || !empty($module->info['required']) || $module->status == TRUE || $module->info['package'] == 'Testing') {
return FALSE;
}
return TRUE;
});
unset($all_modules['help']);
$this
->assertModuleNotInstalled('help');
$edit = [];
$edit["modules[help][enable]"] = TRUE;
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$this
->assertSession()
->pageTextContains('has been enabled');
$this
->assertSession()
->pageTextContains('hook_modules_installed fired for help');
$this
->assertModuleSuccessfullyInstalled('help');
foreach ($required_modules as $name => $module) {
$this
->assertHelp($name, $module->info['name']);
}
foreach ($all_modules as $name => $module) {
$was_installed_list = \Drupal::moduleHandler()
->getModuleList();
$modules_to_install = [
$name,
];
foreach (array_keys($module->requires) as $dependency) {
if (isset($all_modules[$dependency])) {
$modules_to_install[] = $dependency;
}
}
foreach ($modules_to_install as $module_to_install) {
$this
->assertModuleNotInstalled($module_to_install);
}
$edit = [];
$lifecycle = $module->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER];
$edit['modules[' . $name . '][enable]'] = TRUE;
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
if ($lifecycle === ExtensionLifecycle::EXPERIMENTAL) {
$this
->assertSession()
->pageTextContains('Are you sure you wish to enable experimental modules?');
if (count($modules_to_install) > 1) {
$this
->assertSession()
->pageTextContains('You must enable');
}
$this
->submitForm([], 'Continue');
}
elseif (count($modules_to_install) > 1) {
$this
->assertSession()
->pageTextContains('Some required modules must be enabled');
$this
->assertSession()
->pageTextContains('You must enable');
$this
->submitForm([], 'Continue');
}
$module_names = [];
foreach ($modules_to_install as $module_to_install) {
$module_names[] = $all_modules[$module_to_install]->info['name'];
}
if (count($modules_to_install) > 1) {
$this
->assertSession()
->pageTextContains(count($module_names) . ' modules have been enabled: ' . implode(', ', $module_names));
}
else {
$this
->assertSession()
->pageTextContains('Module ' . $module_names[0] . ' has been enabled.');
}
foreach ($modules_to_install as $module_to_install) {
$this
->assertSession()
->pageTextContains('hook_modules_installed fired for ' . $module_to_install);
$this
->assertLogMessage('system', "%module module installed.", [
'%module' => $module_to_install,
], RfcLogLevel::INFO);
$this
->assertInstallModuleUpdates($module_to_install);
$this
->assertModuleSuccessfullyInstalled($module_to_install);
}
$this
->assertHelp($name, $module->info['name']);
if ($name == 'forum') {
$this
->preUninstallForum();
}
if ($name == 'workspaces') {
$workspaces = Workspace::loadMultiple();
\Drupal::entityTypeManager()
->getStorage('workspace')
->delete($workspaces);
}
$now_installed_list = \Drupal::moduleHandler()
->getModuleList();
$added_modules = array_diff(array_keys($now_installed_list), array_keys($was_installed_list));
while ($added_modules) {
$initial_count = count($added_modules);
foreach ($added_modules as $to_uninstall) {
$this
->drupalGet('admin/modules/uninstall');
$checkbox = $this
->assertSession()
->fieldExists("uninstall[{$to_uninstall}]");
if (!$checkbox
->hasAttribute('disabled')) {
$package = $all_modules[$to_uninstall]->info['package'];
$this
->assertSuccessfulUninstall($to_uninstall, $package);
$added_modules = array_diff($added_modules, [
$to_uninstall,
]);
}
}
$final_count = count($added_modules);
if ($initial_count == $final_count) {
$this
->fail('Remaining modules could not be uninstalled for ' . $name);
break;
}
}
}
$all_modules['help'] = $required_modules['help'];
$this
->assertSuccessfulUninstall('help', $required_modules['help']->info['package']);
$edit = [];
$experimental = FALSE;
foreach ($all_modules as $name => $module) {
$edit['modules[' . $name . '][enable]'] = TRUE;
if ($module->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::EXPERIMENTAL) {
$experimental = TRUE;
}
}
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
if ($experimental) {
$this
->assertSession()
->pageTextContains('Are you sure you wish to enable experimental modules?');
$this
->submitForm([], 'Continue');
}
$this
->assertSession()
->pageTextContains(count($all_modules) . ' modules have been enabled: ');
}
protected function assertModuleNotInstalled($name) {
$this
->assertModules([
$name,
], FALSE);
$this
->assertModuleTablesDoNotExist($name);
}
protected function assertModuleSuccessfullyInstalled($name) {
$this
->assertModules([
$name,
], TRUE);
$this
->assertModuleTablesExist($name);
$this
->assertModuleConfig($name);
}
protected function assertSuccessfulUninstall($module, $package = 'Core') {
$edit = [];
$edit['uninstall[' . $module . ']'] = TRUE;
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->assertSession()
->pageTextContains('The selected modules have been uninstalled.');
$this
->assertModules([
$module,
], FALSE);
$this
->assertSession()
->pageTextContains('hook_modules_uninstalled fired for ' . $module);
$this
->assertLogMessage('system', "%module module uninstalled.", [
'%module' => $module,
], RfcLogLevel::INFO);
$this
->assertModuleTablesDoNotExist($module);
$this
->assertNoModuleConfig($module);
$this
->assertUninstallModuleUpdates($module);
}
protected function assertInstallModuleUpdates($module) {
$post_update_registry = \Drupal::service('update.post_update_registry');
$all_update_functions = $post_update_registry
->getPendingUpdateFunctions();
$empty_result = TRUE;
foreach ($all_update_functions as $function) {
list($function_module, ) = explode('_post_update_', $function);
if ($module === $function_module) {
$empty_result = FALSE;
break;
}
}
$this
->assertTrue($empty_result, 'Ensures that no pending post update functions are available.');
$existing_updates = \Drupal::keyValue('post_update')
->get('existing_updates', []);
switch ($module) {
case 'update_test_postupdate':
$expected = [
'update_test_postupdate_post_update_first',
'update_test_postupdate_post_update_second',
'update_test_postupdate_post_update_test1',
'update_test_postupdate_post_update_test0',
'update_test_postupdate_post_update_foo',
'update_test_postupdate_post_update_bar',
'update_test_postupdate_post_update_baz',
];
$this
->assertSame($expected, $existing_updates);
break;
}
}
protected function assertUninstallModuleUpdates($module) {
$post_update_registry = \Drupal::service('update.post_update_registry');
$all_update_functions = $post_update_registry
->getPendingUpdateFunctions();
switch ($module) {
case 'update_test_postupdate':
$this
->assertEmpty(array_intersect([
'update_test_postupdate_post_update_first',
], $all_update_functions), 'Asserts that no pending post update functions are available.');
$existing_updates = \Drupal::keyValue('post_update')
->get('existing_updates', []);
$this
->assertEmpty(array_intersect([
'update_test_postupdate_post_update_first',
], $existing_updates), 'Asserts that no post update functions are stored in keyvalue store.');
break;
}
}
protected function assertHelp($module, $name) {
$this
->drupalGet('admin/help/' . $module);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($name . ' module');
$this
->assertSession()
->linkExists('online documentation for the ' . $name . ' module', 0, "Correct online documentation link is in the help page for {$module}");
}
protected function preUninstallForum() {
$query = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE);
$query
->condition('vid', 'forums');
$ids = $query
->execute();
$storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$terms = $storage
->loadMultiple($ids);
$storage
->delete($terms);
}
}