Upgraded drupal core with security updates
[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\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;
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     // 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>');
63
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.
67     $node->delete();
68
69     // Uninstall module_test.
70     $edit = [];
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.');
78
79     // Uninstall node testing that the configuration that will be deleted is
80     // listed.
81     $node_dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('module', ['node']);
82     $edit = [];
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.');
87
88     $entity_types = [];
89     foreach ($node_dependencies as $entity) {
90       $label = $entity->label() ?: $entity->id();
91       $this->assertText($label);
92       $entity_types[] = $entity->getEntityTypeId();
93     }
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
98       // labels.
99       $this->assertRaw('<h3>' . $entity_type->getLabel() . '</h3>');
100     }
101
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]));
107
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('&lt;label', 'The page does not have double escaped HTML tags.');
111
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');
119
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');
124
125     // Make sure the correct error is shown when no modules are selected.
126     $edit = [];
127     $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
128     $this->assertText(t('No modules selected.'), 'No module is selected to uninstall');
129   }
130
131   /**
132    * Tests that a module which fails to install can still be uninstalled.
133    */
134   public function testFailedInstallStatus() {
135     $account = $this->drupalCreateUser(['administer modules']);
136     $this->drupalLogin($account);
137
138     $message = 'Exception thrown when installing module_installer_config_test with an invalid configuration file.';
139     try {
140       $this->container->get('module_installer')->install(['module_installer_config_test']);
141       $this->fail($message);
142     }
143     catch (EntityMalformedException $e) {
144       $this->pass($message);
145     }
146
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');
156   }
157
158 }