Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Module;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Render\FormattableMarkup;
|
Chris@0
|
6 use Drupal\Core\Logger\RfcLogLevel;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Install/uninstall core module and confirm table creation/deletion.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group Module
|
Chris@0
|
12 */
|
Chris@0
|
13 class InstallUninstallTest extends ModuleTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * {@inheritdoc}
|
Chris@0
|
17 */
|
Chris@0
|
18 public static $modules = ['system_test', 'dblog', 'taxonomy', 'update_test_postupdate'];
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Tests that a fixed set of modules can be installed and uninstalled.
|
Chris@0
|
22 */
|
Chris@0
|
23 public function testInstallUninstall() {
|
Chris@0
|
24 // Set a variable so that the hook implementations in system_test.module
|
Chris@0
|
25 // will display messages via drupal_set_message().
|
Chris@0
|
26 $this->container->get('state')->set('system_test.verbose_module_hooks', TRUE);
|
Chris@0
|
27
|
Chris@0
|
28 // Install and uninstall module_test to ensure hook_preinstall_module and
|
Chris@0
|
29 // hook_preuninstall_module are fired as expected.
|
Chris@0
|
30 $this->container->get('module_installer')->install(['module_test']);
|
Chris@0
|
31 $this->assertEqual($this->container->get('state')->get('system_test_preinstall_module'), 'module_test');
|
Chris@0
|
32 $this->container->get('module_installer')->uninstall(['module_test']);
|
Chris@0
|
33 $this->assertEqual($this->container->get('state')->get('system_test_preuninstall_module'), 'module_test');
|
Chris@0
|
34 $this->resetAll();
|
Chris@0
|
35
|
Chris@0
|
36 $all_modules = system_rebuild_module_data();
|
Chris@0
|
37
|
Chris@0
|
38 // Test help on required modules, but do not test uninstalling.
|
Chris@0
|
39 $required_modules = array_filter($all_modules, function ($module) {
|
Chris@0
|
40 if (!empty($module->info['required']) || $module->status == TRUE) {
|
Chris@0
|
41 if ($module->info['package'] != 'Testing' && empty($module->info['hidden'])) {
|
Chris@0
|
42 return TRUE;
|
Chris@0
|
43 }
|
Chris@0
|
44 }
|
Chris@0
|
45 return FALSE;
|
Chris@0
|
46 });
|
Chris@0
|
47
|
Chris@0
|
48 $required_modules['help'] = $all_modules['help'];
|
Chris@0
|
49
|
Chris@0
|
50 // Test uninstalling without hidden, required, and already enabled modules.
|
Chris@0
|
51 $all_modules = array_filter($all_modules, function ($module) {
|
Chris@0
|
52 if (!empty($module->info['hidden']) || !empty($module->info['required']) || $module->status == TRUE || $module->info['package'] == 'Testing') {
|
Chris@0
|
53 return FALSE;
|
Chris@0
|
54 }
|
Chris@0
|
55 return TRUE;
|
Chris@0
|
56 });
|
Chris@0
|
57
|
Chris@0
|
58 // Install the Help module, and verify it installed successfully.
|
Chris@0
|
59 unset($all_modules['help']);
|
Chris@0
|
60 $this->assertModuleNotInstalled('help');
|
Chris@0
|
61 $edit = [];
|
Chris@0
|
62 $edit["modules[help][enable]"] = TRUE;
|
Chris@0
|
63 $this->drupalPostForm('admin/modules', $edit, t('Install'));
|
Chris@0
|
64 $this->assertText('has been enabled', 'Modules status has been updated.');
|
Chris@0
|
65 $this->assertText(t('hook_modules_installed fired for help'));
|
Chris@0
|
66 $this->assertModuleSuccessfullyInstalled('help');
|
Chris@0
|
67
|
Chris@0
|
68 // Test help for the required modules.
|
Chris@0
|
69 foreach ($required_modules as $name => $module) {
|
Chris@0
|
70 $this->assertHelp($name, $module->info['name']);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 // Go through each module in the list and try to install and uninstall
|
Chris@0
|
74 // it with its dependencies.
|
Chris@0
|
75 while (list($name, $module) = each($all_modules)) {
|
Chris@0
|
76 $was_installed_list = \Drupal::moduleHandler()->getModuleList();
|
Chris@0
|
77
|
Chris@0
|
78 // Start a list of modules that we expect to be installed this time.
|
Chris@0
|
79 $modules_to_install = [$name];
|
Chris@0
|
80 foreach (array_keys($module->requires) as $dependency) {
|
Chris@0
|
81 if (isset($all_modules[$dependency])) {
|
Chris@0
|
82 $modules_to_install[] = $dependency;
|
Chris@0
|
83 }
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 // Check that each module is not yet enabled and does not have any
|
Chris@0
|
87 // database tables yet.
|
Chris@0
|
88 foreach ($modules_to_install as $module_to_install) {
|
Chris@0
|
89 $this->assertModuleNotInstalled($module_to_install);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 // Install the module.
|
Chris@0
|
93 $edit = [];
|
Chris@0
|
94 $package = $module->info['package'];
|
Chris@0
|
95 $edit['modules[' . $name . '][enable]'] = TRUE;
|
Chris@0
|
96 $this->drupalPostForm('admin/modules', $edit, t('Install'));
|
Chris@0
|
97
|
Chris@0
|
98 // Handle experimental modules, which require a confirmation screen.
|
Chris@0
|
99 if ($package == 'Core (Experimental)') {
|
Chris@0
|
100 $this->assertText('Are you sure you wish to enable experimental modules?');
|
Chris@0
|
101 if (count($modules_to_install) > 1) {
|
Chris@0
|
102 // When there are experimental modules, needed dependencies do not
|
Chris@0
|
103 // result in the same page title, but there will be expected text
|
Chris@0
|
104 // indicating they need to be enabled.
|
Chris@0
|
105 $this->assertText('You must enable');
|
Chris@0
|
106 }
|
Chris@0
|
107 $this->drupalPostForm(NULL, [], t('Continue'));
|
Chris@0
|
108 }
|
Chris@0
|
109 // Handle the case where modules were installed along with this one and
|
Chris@0
|
110 // where we therefore hit a confirmation screen.
|
Chris@0
|
111 elseif (count($modules_to_install) > 1) {
|
Chris@0
|
112 // Verify that we are on the correct form and that the expected text
|
Chris@0
|
113 // about enabling dependencies appears.
|
Chris@0
|
114 $this->assertText('Some required modules must be enabled');
|
Chris@0
|
115 $this->assertText('You must enable');
|
Chris@0
|
116 $this->drupalPostForm(NULL, [], t('Continue'));
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 // List the module display names to check the confirmation message.
|
Chris@0
|
120 $module_names = [];
|
Chris@0
|
121 foreach ($modules_to_install as $module_to_install) {
|
Chris@0
|
122 $module_names[] = $all_modules[$module_to_install]->info['name'];
|
Chris@0
|
123 }
|
Chris@0
|
124 $expected_text = \Drupal::translation()->formatPlural(count($module_names), 'Module @name has been enabled.', '@count modules have been enabled: @names.', [
|
Chris@0
|
125 '@name' => $module_names[0],
|
Chris@0
|
126 '@names' => implode(', ', $module_names),
|
Chris@0
|
127 ]);
|
Chris@0
|
128 $this->assertText($expected_text, 'Modules status has been updated.');
|
Chris@0
|
129
|
Chris@0
|
130 // Check that hook_modules_installed() was invoked with the expected list
|
Chris@0
|
131 // of modules, that each module's database tables now exist, and that
|
Chris@0
|
132 // appropriate messages appear in the logs.
|
Chris@0
|
133 foreach ($modules_to_install as $module_to_install) {
|
Chris@0
|
134 $this->assertText(t('hook_modules_installed fired for @module', ['@module' => $module_to_install]));
|
Chris@0
|
135 $this->assertLogMessage('system', "%module module installed.", ['%module' => $module_to_install], RfcLogLevel::INFO);
|
Chris@0
|
136 $this->assertInstallModuleUpdates($module_to_install);
|
Chris@0
|
137 $this->assertModuleSuccessfullyInstalled($module_to_install);
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 // Verify the help page.
|
Chris@0
|
141 $this->assertHelp($name, $module->info['name']);
|
Chris@0
|
142
|
Chris@0
|
143 // Uninstall the original module, plus everything else that was installed
|
Chris@0
|
144 // with it.
|
Chris@0
|
145 if ($name == 'forum') {
|
Chris@0
|
146 // Forum has an extra step to be able to uninstall it.
|
Chris@0
|
147 $this->preUninstallForum();
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 $now_installed_list = \Drupal::moduleHandler()->getModuleList();
|
Chris@0
|
151 $added_modules = array_diff(array_keys($now_installed_list), array_keys($was_installed_list));
|
Chris@0
|
152 while ($added_modules) {
|
Chris@0
|
153 $initial_count = count($added_modules);
|
Chris@0
|
154 foreach ($added_modules as $to_uninstall) {
|
Chris@0
|
155 // See if we can currently uninstall this module (if its dependencies
|
Chris@0
|
156 // have been uninstalled), and do so if we can.
|
Chris@0
|
157 $this->drupalGet('admin/modules/uninstall');
|
Chris@0
|
158 $field_name = "uninstall[$to_uninstall]";
|
Chris@0
|
159 $has_checkbox = $this->xpath('//input[@type="checkbox" and @name="' . $field_name . '"]');
|
Chris@0
|
160 $disabled = $this->xpath('//input[@type="checkbox" and @name="' . $field_name . '" and @disabled="disabled"]');
|
Chris@0
|
161
|
Chris@0
|
162 if (!empty($has_checkbox) && empty($disabled)) {
|
Chris@0
|
163 // This one is eligible for being uninstalled.
|
Chris@0
|
164 $package = $all_modules[$to_uninstall]->info['package'];
|
Chris@0
|
165 $this->assertSuccessfulUninstall($to_uninstall, $package);
|
Chris@0
|
166 $added_modules = array_diff($added_modules, [$to_uninstall]);
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 // If we were not able to find a module to uninstall, fail and exit the
|
Chris@0
|
171 // loop.
|
Chris@0
|
172 $final_count = count($added_modules);
|
Chris@0
|
173 if ($initial_count == $final_count) {
|
Chris@0
|
174 $this->fail('Remaining modules could not be uninstalled for ' . $name);
|
Chris@0
|
175 break;
|
Chris@0
|
176 }
|
Chris@0
|
177 }
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 // Uninstall the help module and put it back into the list of modules.
|
Chris@0
|
181 $all_modules['help'] = $required_modules['help'];
|
Chris@0
|
182 $this->assertSuccessfulUninstall('help', $required_modules['help']->info['package']);
|
Chris@0
|
183
|
Chris@0
|
184 // Now that all modules have been tested, go back and try to enable them
|
Chris@0
|
185 // all again at once. This tests two things:
|
Chris@0
|
186 // - That each module can be successfully enabled again after being
|
Chris@0
|
187 // uninstalled.
|
Chris@0
|
188 // - That enabling more than one module at the same time does not lead to
|
Chris@0
|
189 // any errors.
|
Chris@0
|
190 $edit = [];
|
Chris@0
|
191 $experimental = FALSE;
|
Chris@0
|
192 foreach ($all_modules as $name => $module) {
|
Chris@0
|
193 $edit['modules[' . $name . '][enable]'] = TRUE;
|
Chris@0
|
194 // Track whether there is at least one experimental module.
|
Chris@0
|
195 if ($module->info['package'] == 'Core (Experimental)') {
|
Chris@0
|
196 $experimental = TRUE;
|
Chris@0
|
197 }
|
Chris@0
|
198 }
|
Chris@0
|
199 $this->drupalPostForm('admin/modules', $edit, t('Install'));
|
Chris@0
|
200
|
Chris@0
|
201 // If there are experimental modules, click the confirm form.
|
Chris@0
|
202 if ($experimental) {
|
Chris@0
|
203 $this->assertText('Are you sure you wish to enable experimental modules?');
|
Chris@0
|
204 $this->drupalPostForm(NULL, [], t('Continue'));
|
Chris@0
|
205 }
|
Chris@0
|
206 // The string tested here is translatable but we are only using a part of it
|
Chris@0
|
207 // so using a translated string is wrong. Doing so would create a new string
|
Chris@0
|
208 // to translate.
|
Chris@0
|
209 $this->assertText(new FormattableMarkup('@count modules have been enabled: ', ['@count' => count($all_modules)]), 'Modules status has been updated.');
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * Asserts that a module is not yet installed.
|
Chris@0
|
214 *
|
Chris@0
|
215 * @param string $name
|
Chris@0
|
216 * Name of the module to check.
|
Chris@0
|
217 */
|
Chris@0
|
218 protected function assertModuleNotInstalled($name) {
|
Chris@0
|
219 $this->assertModules([$name], FALSE);
|
Chris@0
|
220 $this->assertModuleTablesDoNotExist($name);
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 /**
|
Chris@0
|
224 * Asserts that a module was successfully installed.
|
Chris@0
|
225 *
|
Chris@0
|
226 * @param string $name
|
Chris@0
|
227 * Name of the module to check.
|
Chris@0
|
228 */
|
Chris@0
|
229 protected function assertModuleSuccessfullyInstalled($name) {
|
Chris@0
|
230 $this->assertModules([$name], TRUE);
|
Chris@0
|
231 $this->assertModuleTablesExist($name);
|
Chris@0
|
232 $this->assertModuleConfig($name);
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 /**
|
Chris@0
|
236 * Uninstalls a module and asserts that it was done correctly.
|
Chris@0
|
237 *
|
Chris@0
|
238 * @param string $module
|
Chris@0
|
239 * The name of the module to uninstall.
|
Chris@0
|
240 * @param string $package
|
Chris@0
|
241 * (optional) The package of the module to uninstall. Defaults
|
Chris@0
|
242 * to 'Core'.
|
Chris@0
|
243 */
|
Chris@0
|
244 protected function assertSuccessfulUninstall($module, $package = 'Core') {
|
Chris@0
|
245 $edit = [];
|
Chris@0
|
246 $edit['uninstall[' . $module . ']'] = TRUE;
|
Chris@0
|
247 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
|
Chris@0
|
248 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
|
Chris@0
|
249 $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
|
Chris@0
|
250 $this->assertModules([$module], FALSE);
|
Chris@0
|
251
|
Chris@0
|
252 // Check that the appropriate hook was fired and the appropriate log
|
Chris@0
|
253 // message appears. (But don't check for the log message if the dblog
|
Chris@0
|
254 // module was just uninstalled, since the {watchdog} table won't be there
|
Chris@0
|
255 // anymore.)
|
Chris@0
|
256 $this->assertText(t('hook_modules_uninstalled fired for @module', ['@module' => $module]));
|
Chris@0
|
257 $this->assertLogMessage('system', "%module module uninstalled.", ['%module' => $module], RfcLogLevel::INFO);
|
Chris@0
|
258
|
Chris@0
|
259 // Check that the module's database tables no longer exist.
|
Chris@0
|
260 $this->assertModuleTablesDoNotExist($module);
|
Chris@0
|
261 // Check that the module's config files no longer exist.
|
Chris@0
|
262 $this->assertNoModuleConfig($module);
|
Chris@0
|
263 $this->assertUninstallModuleUpdates($module);
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * Asserts the module post update functions after install.
|
Chris@0
|
268 *
|
Chris@0
|
269 * @param string $module
|
Chris@0
|
270 * The module that got installed.
|
Chris@0
|
271 */
|
Chris@0
|
272 protected function assertInstallModuleUpdates($module) {
|
Chris@0
|
273 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
|
Chris@0
|
274 $post_update_registry = \Drupal::service('update.post_update_registry');
|
Chris@0
|
275 $all_update_functions = $post_update_registry->getPendingUpdateFunctions();
|
Chris@0
|
276 $empty_result = TRUE;
|
Chris@0
|
277 foreach ($all_update_functions as $function) {
|
Chris@0
|
278 list($function_module,) = explode('_post_update_', $function);
|
Chris@0
|
279 if ($module === $function_module) {
|
Chris@0
|
280 $empty_result = FALSE;
|
Chris@0
|
281 break;
|
Chris@0
|
282 }
|
Chris@0
|
283 }
|
Chris@0
|
284 $this->assertTrue($empty_result, 'Ensures that no pending post update functions are available.');
|
Chris@0
|
285
|
Chris@0
|
286 $existing_updates = \Drupal::keyValue('post_update')->get('existing_updates', []);
|
Chris@0
|
287 switch ($module) {
|
Chris@0
|
288 case 'block':
|
Chris@0
|
289 $this->assertFalse(array_diff(['block_post_update_disable_blocks_with_missing_contexts'], $existing_updates));
|
Chris@0
|
290 break;
|
Chris@0
|
291 case 'update_test_postupdate':
|
Chris@0
|
292 $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
|
293 break;
|
Chris@0
|
294 }
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Asserts the module post update functions after uninstall.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @param string $module
|
Chris@0
|
301 * The module that got installed.
|
Chris@0
|
302 */
|
Chris@0
|
303 protected function assertUninstallModuleUpdates($module) {
|
Chris@0
|
304 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
|
Chris@0
|
305 $post_update_registry = \Drupal::service('update.post_update_registry');
|
Chris@0
|
306 $all_update_functions = $post_update_registry->getPendingUpdateFunctions();
|
Chris@0
|
307
|
Chris@0
|
308 switch ($module) {
|
Chris@0
|
309 case 'block':
|
Chris@0
|
310 $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
|
311
|
Chris@0
|
312 $existing_updates = \Drupal::keyValue('post_update')->get('existing_updates', []);
|
Chris@0
|
313 $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
|
314 break;
|
Chris@0
|
315 }
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * Verifies a module's help.
|
Chris@0
|
320 *
|
Chris@0
|
321 * Verifies that the module help page from hook_help() exists and can be
|
Chris@0
|
322 * displayed, and that it contains the phrase "Foo Bar module", where "Foo
|
Chris@0
|
323 * Bar" is the name of the module from the .info.yml file.
|
Chris@0
|
324 *
|
Chris@0
|
325 * @param string $module
|
Chris@0
|
326 * Machine name of the module to verify.
|
Chris@0
|
327 * @param string $name
|
Chris@0
|
328 * Human-readable name of the module to verify.
|
Chris@0
|
329 */
|
Chris@0
|
330 protected function assertHelp($module, $name) {
|
Chris@0
|
331 $this->drupalGet('admin/help/' . $module);
|
Chris@0
|
332 $this->assertResponse(200, "Help for $module displayed successfully");
|
Chris@0
|
333 $this->assertText($name . ' module', "'$name module' is on the help page for $module");
|
Chris@0
|
334 $this->assertLink('online documentation for the ' . $name . ' module', 0, "Correct online documentation link is in the help page for $module");
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 /**
|
Chris@0
|
338 * Deletes forum taxonomy terms, so Forum can be uninstalled.
|
Chris@0
|
339 */
|
Chris@0
|
340 protected function preUninstallForum() {
|
Chris@0
|
341 // There only should be a 'General discussion' term in the 'forums'
|
Chris@0
|
342 // vocabulary, but just delete any terms there in case the name changes.
|
Chris@0
|
343 $query = \Drupal::entityQuery('taxonomy_term');
|
Chris@0
|
344 $query->condition('vid', 'forums');
|
Chris@0
|
345 $ids = $query->execute();
|
Chris@0
|
346 $storage = \Drupal::entityManager()->getStorage('taxonomy_term');
|
Chris@0
|
347 $terms = $storage->loadMultiple($ids);
|
Chris@0
|
348 $storage->delete($terms);
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 }
|