3 namespace Drupal\Tests\system\Functional\Module;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Core\Entity\EntityMalformedException;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\Tests\BrowserTestBase;
13 * Tests the uninstallation of modules.
17 class UninstallTest extends BrowserTestBase {
24 public static $modules = ['module_test', 'user', 'views', 'node'];
27 * Tests the hook_modules_uninstalled() of the user module.
29 public function testUserPermsUninstalled() {
30 // Uninstalls the module_test module, so hook_modules_uninstalled()
32 $this->container->get('module_installer')->uninstall(['module_test']);
34 // Are the perms defined by module_test removed?
35 $this->assertFalse(user_roles(FALSE, 'module_test perm'), 'Permissions were all removed.');
39 * Tests the Uninstall page and Uninstall confirmation page.
41 public function testUninstallPage() {
42 $account = $this->drupalCreateUser(['administer modules']);
43 $this->drupalLogin($account);
45 // Create a node type.
46 $node_type = NodeType::create(['type' => 'uninstall_blocker', 'name' => 'Uninstall blocker']);
47 // Create a dependency that can be fixed.
48 $node_type->setThirdPartySetting('module_test', 'key', 'value');
50 // Add a node to prevent node from being uninstalled.
51 $node = Node::create([
52 'type' => 'uninstall_blocker',
53 'title' => $this->randomString(),
57 $this->drupalGet('admin/modules/uninstall');
58 $this->assertTitle(t('Uninstall') . ' | Drupal');
60 foreach (\Drupal::service('extension.list.module')->getAllInstalledInfo() as $module => $info) {
61 $field_name = "uninstall[$module]";
62 if (!empty($info['required'])) {
63 // A required module should not be listed on the uninstall page.
64 $this->assertSession()->fieldNotExists($field_name);
67 $this->assertSession()->fieldExists($field_name);
71 // Be sure labels are rendered properly.
72 // @see regression https://www.drupal.org/node/2512106
73 $this->assertRaw('<label for="edit-uninstall-node" class="module-name table-filter-text-source">Node</label>');
75 $this->assertText(\Drupal::translation()->translate('The following reason prevents Node from being uninstalled:'));
76 $this->assertText(\Drupal::translation()->translate('There is content for the entity type: Content'));
77 // Delete the node to allow node to be uninstalled.
80 // Uninstall module_test.
82 $edit['uninstall[module_test]'] = TRUE;
83 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
84 $this->assertNoText(\Drupal::translation()->translate('Configuration deletions'), 'No configuration deletions listed on the module install confirmation page.');
85 $this->assertText(\Drupal::translation()->translate('Configuration updates'), 'Configuration updates listed on the module install confirmation page.');
86 $this->assertText($node_type->label());
87 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
88 $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
90 // Uninstall node testing that the configuration that will be deleted is
92 $node_dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('module', ['node']);
94 $edit['uninstall[node]'] = TRUE;
95 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
96 $this->assertText(\Drupal::translation()->translate('Configuration deletions'), 'Configuration deletions listed on the module install confirmation page.');
97 $this->assertNoText(\Drupal::translation()->translate('Configuration updates'), 'No configuration updates listed on the module install confirmation page.');
100 foreach ($node_dependencies as $entity) {
101 $label = $entity->label() ?: $entity->id();
102 $this->assertText($label);
103 $entity_types[] = $entity->getEntityTypeId();
105 $entity_types = array_unique($entity_types);
106 foreach ($entity_types as $entity_type_id) {
107 $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
108 // Add h3's since the entity type label is often repeated in the entity
110 $this->assertRaw('<h3>' . $entity_type->getLabel() . '</h3>');
113 // Set a unique cache entry to be able to test whether all caches are
114 // cleared during the uninstall.
115 \Drupal::cache()->set('uninstall_test', 'test_uninstall_page', Cache::PERMANENT);
116 $cached = \Drupal::cache()->get('uninstall_test');
117 $this->assertEqual($cached->data, 'test_uninstall_page', new FormattableMarkup('Cache entry found: @bin', ['@bin' => $cached->data]));
119 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
120 $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
121 $this->assertNoRaw('<label', 'The page does not have double escaped HTML tags.');
123 // Make sure our unique cache entry is gone.
124 $cached = \Drupal::cache()->get('uninstall_test');
125 $this->assertFalse($cached, 'Cache entry not found');
126 // Make sure we get an error message when we try to confirm uninstallation
127 // of an empty list of modules.
128 $this->drupalGet('admin/modules/uninstall/confirm');
129 $this->assertText(t('The selected modules could not be uninstalled, either due to a website problem or due to the uninstall confirmation form timing out. Please try again.'), 'Module uninstall confirmation form displays error message');
131 // Make sure confirmation page is accessible only during uninstall process.
132 $this->drupalGet('admin/modules/uninstall/confirm');
133 $this->assertUrl('admin/modules/uninstall');
134 $this->assertTitle(t('Uninstall') . ' | Drupal');
136 // Make sure the correct error is shown when no modules are selected.
138 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
139 $this->assertText(t('No modules selected.'), 'No module is selected to uninstall');
143 * Tests that a module which fails to install can still be uninstalled.
145 public function testFailedInstallStatus() {
146 $account = $this->drupalCreateUser(['administer modules']);
147 $this->drupalLogin($account);
149 $message = 'Exception thrown when installing module_installer_config_test with an invalid configuration file.';
151 $this->container->get('module_installer')->install(['module_installer_config_test']);
152 $this->fail($message);
154 catch (EntityMalformedException $e) {
155 $this->pass($message);
158 // Even though the module failed to install properly, its configuration
159 // status is "enabled" and should still be available to uninstall.
160 $this->drupalGet('admin/modules/uninstall');
161 $this->assertText('Module installer config test');
162 $edit['uninstall[module_installer_config_test]'] = TRUE;
163 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
164 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
165 $this->assertText(t('The selected modules have been uninstalled.'));
166 $this->assertNoText('Module installer config test');