Chris@0: container->get('state')->set('system_test.verbose_module_hooks', TRUE); Chris@0: Chris@0: // Install and uninstall module_test to ensure hook_preinstall_module and Chris@0: // hook_preuninstall_module are fired as expected. Chris@0: $this->container->get('module_installer')->install(['module_test']); Chris@0: $this->assertEqual($this->container->get('state')->get('system_test_preinstall_module'), 'module_test'); Chris@0: $this->container->get('module_installer')->uninstall(['module_test']); Chris@0: $this->assertEqual($this->container->get('state')->get('system_test_preuninstall_module'), 'module_test'); Chris@0: $this->resetAll(); Chris@0: Chris@0: $all_modules = system_rebuild_module_data(); Chris@0: Chris@0: // Test help on required modules, but do not test uninstalling. Chris@0: $required_modules = array_filter($all_modules, function ($module) { Chris@0: if (!empty($module->info['required']) || $module->status == TRUE) { Chris@0: if ($module->info['package'] != 'Testing' && empty($module->info['hidden'])) { Chris@0: return TRUE; Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: }); Chris@0: Chris@0: $required_modules['help'] = $all_modules['help']; Chris@0: Chris@0: // Test uninstalling without hidden, required, and already enabled modules. Chris@0: $all_modules = array_filter($all_modules, function ($module) { Chris@0: if (!empty($module->info['hidden']) || !empty($module->info['required']) || $module->status == TRUE || $module->info['package'] == 'Testing') { Chris@0: return FALSE; Chris@0: } Chris@0: return TRUE; Chris@0: }); Chris@0: Chris@0: // Install the Help module, and verify it installed successfully. Chris@0: unset($all_modules['help']); Chris@0: $this->assertModuleNotInstalled('help'); Chris@0: $edit = []; Chris@0: $edit["modules[help][enable]"] = TRUE; Chris@0: $this->drupalPostForm('admin/modules', $edit, t('Install')); Chris@0: $this->assertText('has been enabled', 'Modules status has been updated.'); Chris@0: $this->assertText(t('hook_modules_installed fired for help')); Chris@0: $this->assertModuleSuccessfullyInstalled('help'); Chris@0: Chris@0: // Test help for the required modules. Chris@0: foreach ($required_modules as $name => $module) { Chris@0: $this->assertHelp($name, $module->info['name']); Chris@0: } Chris@0: Chris@0: // Go through each module in the list and try to install and uninstall Chris@0: // it with its dependencies. Chris@0: while (list($name, $module) = each($all_modules)) { Chris@0: $was_installed_list = \Drupal::moduleHandler()->getModuleList(); Chris@0: Chris@0: // Start a list of modules that we expect to be installed this time. Chris@0: $modules_to_install = [$name]; Chris@0: foreach (array_keys($module->requires) as $dependency) { Chris@0: if (isset($all_modules[$dependency])) { Chris@0: $modules_to_install[] = $dependency; Chris@0: } Chris@0: } Chris@0: Chris@0: // Check that each module is not yet enabled and does not have any Chris@0: // database tables yet. Chris@0: foreach ($modules_to_install as $module_to_install) { Chris@0: $this->assertModuleNotInstalled($module_to_install); Chris@0: } Chris@0: Chris@0: // Install the module. Chris@0: $edit = []; Chris@0: $package = $module->info['package']; Chris@0: $edit['modules[' . $name . '][enable]'] = TRUE; Chris@0: $this->drupalPostForm('admin/modules', $edit, t('Install')); Chris@0: Chris@0: // Handle experimental modules, which require a confirmation screen. Chris@0: if ($package == 'Core (Experimental)') { Chris@0: $this->assertText('Are you sure you wish to enable experimental modules?'); Chris@0: if (count($modules_to_install) > 1) { Chris@0: // When there are experimental modules, needed dependencies do not Chris@0: // result in the same page title, but there will be expected text Chris@0: // indicating they need to be enabled. Chris@0: $this->assertText('You must enable'); Chris@0: } Chris@0: $this->drupalPostForm(NULL, [], t('Continue')); Chris@0: } Chris@0: // Handle the case where modules were installed along with this one and Chris@0: // where we therefore hit a confirmation screen. Chris@0: elseif (count($modules_to_install) > 1) { Chris@0: // Verify that we are on the correct form and that the expected text Chris@0: // about enabling dependencies appears. Chris@0: $this->assertText('Some required modules must be enabled'); Chris@0: $this->assertText('You must enable'); Chris@0: $this->drupalPostForm(NULL, [], t('Continue')); Chris@0: } Chris@0: Chris@0: // List the module display names to check the confirmation message. Chris@0: $module_names = []; Chris@0: foreach ($modules_to_install as $module_to_install) { Chris@0: $module_names[] = $all_modules[$module_to_install]->info['name']; Chris@0: } Chris@0: $expected_text = \Drupal::translation()->formatPlural(count($module_names), 'Module @name has been enabled.', '@count modules have been enabled: @names.', [ Chris@0: '@name' => $module_names[0], Chris@0: '@names' => implode(', ', $module_names), Chris@0: ]); Chris@0: $this->assertText($expected_text, 'Modules status has been updated.'); Chris@0: Chris@0: // Check that hook_modules_installed() was invoked with the expected list Chris@0: // of modules, that each module's database tables now exist, and that Chris@0: // appropriate messages appear in the logs. Chris@0: foreach ($modules_to_install as $module_to_install) { Chris@0: $this->assertText(t('hook_modules_installed fired for @module', ['@module' => $module_to_install])); Chris@0: $this->assertLogMessage('system', "%module module installed.", ['%module' => $module_to_install], RfcLogLevel::INFO); Chris@0: $this->assertInstallModuleUpdates($module_to_install); Chris@0: $this->assertModuleSuccessfullyInstalled($module_to_install); Chris@0: } Chris@0: Chris@0: // Verify the help page. Chris@0: $this->assertHelp($name, $module->info['name']); Chris@0: Chris@0: // Uninstall the original module, plus everything else that was installed Chris@0: // with it. Chris@0: if ($name == 'forum') { Chris@0: // Forum has an extra step to be able to uninstall it. Chris@0: $this->preUninstallForum(); Chris@0: } Chris@0: Chris@0: $now_installed_list = \Drupal::moduleHandler()->getModuleList(); Chris@0: $added_modules = array_diff(array_keys($now_installed_list), array_keys($was_installed_list)); Chris@0: while ($added_modules) { Chris@0: $initial_count = count($added_modules); Chris@0: foreach ($added_modules as $to_uninstall) { Chris@0: // See if we can currently uninstall this module (if its dependencies Chris@0: // have been uninstalled), and do so if we can. Chris@0: $this->drupalGet('admin/modules/uninstall'); Chris@0: $field_name = "uninstall[$to_uninstall]"; Chris@0: $has_checkbox = $this->xpath('//input[@type="checkbox" and @name="' . $field_name . '"]'); Chris@0: $disabled = $this->xpath('//input[@type="checkbox" and @name="' . $field_name . '" and @disabled="disabled"]'); Chris@0: Chris@0: if (!empty($has_checkbox) && empty($disabled)) { Chris@0: // This one is eligible for being uninstalled. Chris@0: $package = $all_modules[$to_uninstall]->info['package']; Chris@0: $this->assertSuccessfulUninstall($to_uninstall, $package); Chris@0: $added_modules = array_diff($added_modules, [$to_uninstall]); Chris@0: } Chris@0: } Chris@0: Chris@0: // If we were not able to find a module to uninstall, fail and exit the Chris@0: // loop. Chris@0: $final_count = count($added_modules); Chris@0: if ($initial_count == $final_count) { Chris@0: $this->fail('Remaining modules could not be uninstalled for ' . $name); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Uninstall the help module and put it back into the list of modules. Chris@0: $all_modules['help'] = $required_modules['help']; Chris@0: $this->assertSuccessfulUninstall('help', $required_modules['help']->info['package']); Chris@0: Chris@0: // Now that all modules have been tested, go back and try to enable them Chris@0: // all again at once. This tests two things: Chris@0: // - That each module can be successfully enabled again after being Chris@0: // uninstalled. Chris@0: // - That enabling more than one module at the same time does not lead to Chris@0: // any errors. Chris@0: $edit = []; Chris@0: $experimental = FALSE; Chris@0: foreach ($all_modules as $name => $module) { Chris@0: $edit['modules[' . $name . '][enable]'] = TRUE; Chris@0: // Track whether there is at least one experimental module. Chris@0: if ($module->info['package'] == 'Core (Experimental)') { Chris@0: $experimental = TRUE; Chris@0: } Chris@0: } Chris@0: $this->drupalPostForm('admin/modules', $edit, t('Install')); Chris@0: Chris@0: // If there are experimental modules, click the confirm form. Chris@0: if ($experimental) { Chris@0: $this->assertText('Are you sure you wish to enable experimental modules?'); Chris@0: $this->drupalPostForm(NULL, [], t('Continue')); Chris@0: } Chris@0: // The string tested here is translatable but we are only using a part of it Chris@0: // so using a translated string is wrong. Doing so would create a new string Chris@0: // to translate. Chris@0: $this->assertText(new FormattableMarkup('@count modules have been enabled: ', ['@count' => count($all_modules)]), 'Modules status has been updated.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a module is not yet installed. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of the module to check. Chris@0: */ Chris@0: protected function assertModuleNotInstalled($name) { Chris@0: $this->assertModules([$name], FALSE); Chris@0: $this->assertModuleTablesDoNotExist($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a module was successfully installed. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of the module to check. Chris@0: */ Chris@0: protected function assertModuleSuccessfullyInstalled($name) { Chris@0: $this->assertModules([$name], TRUE); Chris@0: $this->assertModuleTablesExist($name); Chris@0: $this->assertModuleConfig($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Uninstalls a module and asserts that it was done correctly. Chris@0: * Chris@0: * @param string $module Chris@0: * The name of the module to uninstall. Chris@0: * @param string $package Chris@0: * (optional) The package of the module to uninstall. Defaults Chris@0: * to 'Core'. Chris@0: */ Chris@0: protected function assertSuccessfulUninstall($module, $package = 'Core') { Chris@0: $edit = []; Chris@0: $edit['uninstall[' . $module . ']'] = TRUE; Chris@0: $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall')); Chris@0: $this->drupalPostForm(NULL, NULL, t('Uninstall')); Chris@0: $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.'); Chris@0: $this->assertModules([$module], FALSE); Chris@0: Chris@0: // Check that the appropriate hook was fired and the appropriate log Chris@0: // message appears. (But don't check for the log message if the dblog Chris@0: // module was just uninstalled, since the {watchdog} table won't be there Chris@0: // anymore.) Chris@0: $this->assertText(t('hook_modules_uninstalled fired for @module', ['@module' => $module])); Chris@0: $this->assertLogMessage('system', "%module module uninstalled.", ['%module' => $module], RfcLogLevel::INFO); Chris@0: Chris@0: // Check that the module's database tables no longer exist. Chris@0: $this->assertModuleTablesDoNotExist($module); Chris@0: // Check that the module's config files no longer exist. Chris@0: $this->assertNoModuleConfig($module); Chris@0: $this->assertUninstallModuleUpdates($module); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts the module post update functions after install. Chris@0: * Chris@0: * @param string $module Chris@0: * The module that got installed. Chris@0: */ Chris@0: protected function assertInstallModuleUpdates($module) { Chris@0: /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */ Chris@0: $post_update_registry = \Drupal::service('update.post_update_registry'); Chris@0: $all_update_functions = $post_update_registry->getPendingUpdateFunctions(); Chris@0: $empty_result = TRUE; Chris@0: foreach ($all_update_functions as $function) { Chris@0: list($function_module,) = explode('_post_update_', $function); Chris@0: if ($module === $function_module) { Chris@0: $empty_result = FALSE; Chris@0: break; Chris@0: } Chris@0: } Chris@0: $this->assertTrue($empty_result, 'Ensures that no pending post update functions are available.'); Chris@0: Chris@0: $existing_updates = \Drupal::keyValue('post_update')->get('existing_updates', []); Chris@0: switch ($module) { Chris@0: case 'block': Chris@0: $this->assertFalse(array_diff(['block_post_update_disable_blocks_with_missing_contexts'], $existing_updates)); Chris@0: break; Chris@0: case 'update_test_postupdate': Chris@0: $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)); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts the module post update functions after uninstall. Chris@0: * Chris@0: * @param string $module Chris@0: * The module that got installed. Chris@0: */ Chris@0: protected function assertUninstallModuleUpdates($module) { Chris@0: /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */ Chris@0: $post_update_registry = \Drupal::service('update.post_update_registry'); Chris@0: $all_update_functions = $post_update_registry->getPendingUpdateFunctions(); Chris@0: Chris@0: switch ($module) { Chris@0: case 'block': Chris@0: $this->assertFalse(array_intersect(['block_post_update_disable_blocks_with_missing_contexts'], $all_update_functions), 'Asserts that no pending post update functions are available.'); Chris@0: Chris@0: $existing_updates = \Drupal::keyValue('post_update')->get('existing_updates', []); Chris@0: $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.'); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies a module's help. Chris@0: * Chris@0: * Verifies that the module help page from hook_help() exists and can be Chris@0: * displayed, and that it contains the phrase "Foo Bar module", where "Foo Chris@0: * Bar" is the name of the module from the .info.yml file. Chris@0: * Chris@0: * @param string $module Chris@0: * Machine name of the module to verify. Chris@0: * @param string $name Chris@0: * Human-readable name of the module to verify. Chris@0: */ Chris@0: protected function assertHelp($module, $name) { Chris@0: $this->drupalGet('admin/help/' . $module); Chris@0: $this->assertResponse(200, "Help for $module displayed successfully"); Chris@0: $this->assertText($name . ' module', "'$name module' is on the help page for $module"); Chris@0: $this->assertLink('online documentation for the ' . $name . ' module', 0, "Correct online documentation link is in the help page for $module"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes forum taxonomy terms, so Forum can be uninstalled. Chris@0: */ Chris@0: protected function preUninstallForum() { Chris@0: // There only should be a 'General discussion' term in the 'forums' Chris@0: // vocabulary, but just delete any terms there in case the name changes. Chris@0: $query = \Drupal::entityQuery('taxonomy_term'); Chris@0: $query->condition('vid', 'forums'); Chris@0: $ids = $query->execute(); Chris@0: $storage = \Drupal::entityManager()->getStorage('taxonomy_term'); Chris@0: $terms = $storage->loadMultiple($ids); Chris@0: $storage->delete($terms); Chris@0: } Chris@0: Chris@0: }