View source
<?php
namespace Drupal\system\Tests\Module;
use Drupal\Core\Logger\RfcLogLevel;
class InstallUninstallTest extends ModuleTestBase {
public static $modules = array(
'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(array(
'module_test',
));
$this
->assertEqual($this->container
->get('state')
->get('system_test_preinstall_module'), 'module_test');
$this->container
->get('module_installer')
->uninstall(array(
'module_test',
));
$this
->assertEqual($this->container
->get('state')
->get('system_test_preuninstall_module'), 'module_test');
$this
->resetAll();
$all_modules = system_rebuild_module_data();
$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 = array();
$package = $required_modules['help']->info['package'];
$edit["modules[{$package}][help][enable]"] = TRUE;
$this
->drupalPostForm('admin/modules', $edit, t('Install'));
$this
->assertText('has been enabled', 'Modules status has been updated.');
$this
->assertText(t('hook_modules_installed fired for help'));
$this
->assertModuleSuccessfullyInstalled('help');
foreach ($required_modules as $name => $module) {
$this
->assertHelp($name, $module->info['name']);
}
while (list($name, $module) = each($all_modules)) {
$was_installed_list = \Drupal::moduleHandler()
->getModuleList();
$modules_to_install = array(
$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 = array();
$package = $module->info['package'];
$edit["modules[{$package}][{$name}][enable]"] = TRUE;
$this
->drupalPostForm('admin/modules', $edit, t('Install'));
if (count($modules_to_install) > 1) {
$this
->drupalPostForm(NULL, array(), t('Continue'));
}
$module_names = array();
foreach ($modules_to_install as $module_to_install) {
$module_names[] = $all_modules[$module_to_install]->info['name'];
}
$expected_text = \Drupal::translation()
->formatPlural(count($module_names), 'Module @name has been enabled.', '@count modules have been enabled: @names.', array(
'@name' => $module_names[0],
'@names' => implode(', ', $module_names),
));
$this
->assertText($expected_text, 'Modules status has been updated.');
foreach ($modules_to_install as $module_to_install) {
$this
->assertText(t('hook_modules_installed fired for @module', array(
'@module' => $module_to_install,
)));
$this
->assertLogMessage('system', "%module module installed.", array(
'%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();
}
$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');
$field_name = "uninstall[{$to_uninstall}]";
$has_checkbox = $this
->xpath('//input[@type="checkbox" and @name="' . $field_name . '"]');
$disabled = $this
->xpath('//input[@type="checkbox" and @name="' . $field_name . '" and @disabled="disabled"]');
if (!empty($has_checkbox) && empty($disabled)) {
$package = $all_modules[$to_uninstall]->info['package'];
$this
->assertSuccessfulUninstall($to_uninstall, $package);
$added_modules = array_diff($added_modules, array(
$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 = array();
foreach ($all_modules as $name => $module) {
$edit['modules[' . $module->info['package'] . '][' . $name . '][enable]'] = TRUE;
}
$this
->drupalPostForm('admin/modules', $edit, t('Install'));
$this
->assertText(t('@count modules have been enabled: ', array(
'@count' => count($all_modules),
)), 'Modules status has been updated.');
}
protected function assertModuleNotInstalled($name) {
$this
->assertModules(array(
$name,
), FALSE);
$this
->assertModuleTablesDoNotExist($name);
}
protected function assertModuleSuccessfullyInstalled($name) {
$this
->assertModules(array(
$name,
), TRUE);
$this
->assertModuleTablesExist($name);
$this
->assertModuleConfig($name);
}
protected function assertSuccessfulUninstall($module, $package = 'Core') {
$edit = array();
$edit['uninstall[' . $module . ']'] = TRUE;
$this
->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
$this
->drupalPostForm(NULL, NULL, t('Uninstall'));
$this
->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
$this
->assertModules(array(
$module,
), FALSE);
$this
->assertText(t('hook_modules_uninstalled fired for @module', array(
'@module' => $module,
)));
$this
->assertLogMessage('system', "%module module uninstalled.", array(
'%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 'block':
$this
->assertFalse(array_diff([
'block_post_update_disable_blocks_with_missing_contexts',
], $existing_updates));
break;
case 'update_test_postupdate':
$this
->assertFalse(array_diff([
'update_test_postupdate_post_update_first',
'update_test_postupdate_post_update_second',
'update_test_postupdate_post_update_test1',
'update_test_postupdate_post_update_test0',
], $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 'block':
$this
->assertFalse(array_intersect([
'block_post_update_disable_blocks_with_missing_contexts',
], $all_update_functions), 'Asserts that no pending post update functions are available.');
$existing_updates = \Drupal::keyValue('post_update')
->get('existing_updates', []);
$this
->assertFalse(array_intersect([
'block_post_update_disable_blocks_with_missing_contexts',
], $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
->assertResponse(200, "Help for {$module} displayed successfully");
$this
->assertText($name . ' module', "'{$name} module' is on the help page for {$module}");
$this
->assertLink('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');
$query
->condition('vid', 'forums');
$ids = $query
->execute();
$storage = \Drupal::entityManager()
->getStorage('taxonomy_term');
$terms = $storage
->loadMultiple($ids);
$storage
->delete($terms);
}
}