3 namespace Drupal\Tests\system\Functional\Module;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Component\Utility\SafeMarkup;
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 // Be sure labels are rendered properly.
61 // @see regression https://www.drupal.org/node/2512106
62 $this->assertRaw('<label for="edit-uninstall-node" class="module-name table-filter-text-source">Node</label>');
64 $this->assertText(\Drupal::translation()->translate('The following reason prevents Node from being uninstalled:'));
65 $this->assertText(\Drupal::translation()->translate('There is content for the entity type: Content'));
66 // Delete the node to allow node to be uninstalled.
69 // Uninstall module_test.
71 $edit['uninstall[module_test]'] = TRUE;
72 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
73 $this->assertNoText(\Drupal::translation()->translate('Configuration deletions'), 'No configuration deletions listed on the module install confirmation page.');
74 $this->assertText(\Drupal::translation()->translate('Configuration updates'), 'Configuration updates listed on the module install confirmation page.');
75 $this->assertText($node_type->label());
76 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
77 $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
79 // Uninstall node testing that the configuration that will be deleted is
81 $node_dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('module', ['node']);
83 $edit['uninstall[node]'] = TRUE;
84 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
85 $this->assertText(\Drupal::translation()->translate('Configuration deletions'), 'Configuration deletions listed on the module install confirmation page.');
86 $this->assertNoText(\Drupal::translation()->translate('Configuration updates'), 'No configuration updates listed on the module install confirmation page.');
89 foreach ($node_dependencies as $entity) {
90 $label = $entity->label() ?: $entity->id();
91 $this->assertText($label);
92 $entity_types[] = $entity->getEntityTypeId();
94 $entity_types = array_unique($entity_types);
95 foreach ($entity_types as $entity_type_id) {
96 $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
97 // Add h3's since the entity type label is often repeated in the entity
99 $this->assertRaw('<h3>' . $entity_type->getLabel() . '</h3>');
102 // Set a unique cache entry to be able to test whether all caches are
103 // cleared during the uninstall.
104 \Drupal::cache()->set('uninstall_test', 'test_uninstall_page', Cache::PERMANENT);
105 $cached = \Drupal::cache()->get('uninstall_test');
106 $this->assertEqual($cached->data, 'test_uninstall_page', SafeMarkup::format('Cache entry found: @bin', ['@bin' => $cached->data]));
108 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
109 $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
110 $this->assertNoRaw('<label', 'The page does not have double escaped HTML tags.');
112 // Make sure our unique cache entry is gone.
113 $cached = \Drupal::cache()->get('uninstall_test');
114 $this->assertFalse($cached, 'Cache entry not found');
115 // Make sure we get an error message when we try to confirm uninstallation
116 // of an empty list of modules.
117 $this->drupalGet('admin/modules/uninstall/confirm');
118 $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');
120 // Make sure confirmation page is accessible only during uninstall process.
121 $this->drupalGet('admin/modules/uninstall/confirm');
122 $this->assertUrl('admin/modules/uninstall');
123 $this->assertTitle(t('Uninstall') . ' | Drupal');
125 // Make sure the correct error is shown when no modules are selected.
127 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
128 $this->assertText(t('No modules selected.'), 'No module is selected to uninstall');
132 * Tests that a module which fails to install can still be uninstalled.
134 public function testFailedInstallStatus() {
135 $account = $this->drupalCreateUser(['administer modules']);
136 $this->drupalLogin($account);
138 $message = 'Exception thrown when installing module_installer_config_test with an invalid configuration file.';
140 $this->container->get('module_installer')->install(['module_installer_config_test']);
141 $this->fail($message);
143 catch (EntityMalformedException $e) {
144 $this->pass($message);
147 // Even though the module failed to install properly, its configuration
148 // status is "enabled" and should still be available to uninstall.
149 $this->drupalGet('admin/modules/uninstall');
150 $this->assertText('Module installer config test');
151 $edit['uninstall[module_installer_config_test]'] = TRUE;
152 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
153 $this->drupalPostForm(NULL, NULL, t('Uninstall'));
154 $this->assertText(t('The selected modules have been uninstalled.'));
155 $this->assertNoText('Module installer config test');