Pull merge.
[yaffs-website] / web / core / modules / system / tests / src / Functional / Module / UninstallTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Module;
4
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;
11
12 /**
13  * Tests the uninstallation of modules.
14  *
15  * @group Module
16  */
17 class UninstallTest extends BrowserTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['module_test', 'user', 'views', 'node'];
25
26   /**
27    * Tests the hook_modules_uninstalled() of the user module.
28    */
29   public function testUserPermsUninstalled() {
30     // Uninstalls the module_test module, so hook_modules_uninstalled()
31     // is executed.
32     $this->container->get('module_installer')->uninstall(['module_test']);
33
34     // Are the perms defined by module_test removed?
35     $this->assertFalse(user_roles(FALSE, 'module_test perm'), 'Permissions were all removed.');
36   }
37
38   /**
39    * Tests the Uninstall page and Uninstall confirmation page.
40    */
41   public function testUninstallPage() {
42     $account = $this->drupalCreateUser(['administer modules']);
43     $this->drupalLogin($account);
44
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');
49     $node_type->save();
50     // Add a node to prevent node from being uninstalled.
51     $node = Node::create([
52       'type' => 'uninstall_blocker',
53       'title' => $this->randomString(),
54     ]);
55     $node->save();
56
57     $this->drupalGet('admin/modules/uninstall');
58     $this->assertTitle(t('Uninstall') . ' | Drupal');
59
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);
65       }
66       else {
67         $this->assertSession()->fieldExists($field_name);
68       }
69     }
70
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>');
74
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.
78     $node->delete();
79
80     // Uninstall module_test.
81     $edit = [];
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.');
89
90     // Uninstall node testing that the configuration that will be deleted is
91     // listed.
92     $node_dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('module', ['node']);
93     $edit = [];
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.');
98
99     $entity_types = [];
100     foreach ($node_dependencies as $entity) {
101       $label = $entity->label() ?: $entity->id();
102       $this->assertText($label);
103       $entity_types[] = $entity->getEntityTypeId();
104     }
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
109       // labels.
110       $this->assertRaw('<h3>' . $entity_type->getLabel() . '</h3>');
111     }
112
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]));
118
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('&lt;label', 'The page does not have double escaped HTML tags.');
122
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');
130
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');
135
136     // Make sure the correct error is shown when no modules are selected.
137     $edit = [];
138     $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
139     $this->assertText(t('No modules selected.'), 'No module is selected to uninstall');
140   }
141
142   /**
143    * Tests that a module which fails to install can still be uninstalled.
144    */
145   public function testFailedInstallStatus() {
146     $account = $this->drupalCreateUser(['administer modules']);
147     $this->drupalLogin($account);
148
149     $message = 'Exception thrown when installing module_installer_config_test with an invalid configuration file.';
150     try {
151       $this->container->get('module_installer')->install(['module_installer_config_test']);
152       $this->fail($message);
153     }
154     catch (EntityMalformedException $e) {
155       $this->pass($message);
156     }
157
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');
167   }
168
169 }