3 namespace Drupal\Tests\system\Functional\Module;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
10 * Tests that modules which provide entity types can be uninstalled.
14 class PrepareUninstallTest extends BrowserTestBase {
16 use TaxonomyTestTrait;
19 * An array of node objects.
21 * @var \Drupal\node\NodeInterface[]
26 * An array of taxonomy term objects.
28 * @var \Drupal\taxonomy\TermInterface[]
37 public static $modules = ['node', 'taxonomy', 'entity_test'];
42 public function setUp() {
45 $admin_user = $this->drupalCreateUser(['administer modules']);
46 $this->drupalLogin($admin_user);
49 for ($i = 1; $i <= 5; $i++) {
50 $this->nodes[] = $this->drupalCreateNode(['type' => 'page']);
51 $this->nodes[] = $this->drupalCreateNode(['type' => 'article']);
54 // Create 3 top-level taxonomy terms, each with 11 children.
55 $vocabulary = $this->createVocabulary();
56 for ($i = 1; $i <= 3; $i++) {
57 $term = $this->createTerm($vocabulary);
58 $this->terms[] = $term;
59 for ($j = 1; $j <= 11; $j++) {
60 $this->terms[] = $this->createTerm($vocabulary, ['parent' => ['target_id' => $term->id()]]);
66 * Tests that Node and Taxonomy can be uninstalled.
68 public function testUninstall() {
69 // Check that Taxonomy cannot be uninstalled yet.
70 $this->drupalGet('admin/modules/uninstall');
71 $this->assertText('Remove content items');
72 $this->assertLinkByHref('admin/modules/uninstall/entity/taxonomy_term');
74 // Delete Taxonomy term data.
75 $this->drupalGet('admin/modules/uninstall/entity/taxonomy_term');
76 $term_count = count($this->terms);
77 for ($i = 1; $i < 11; $i++) {
78 $this->assertText($this->terms[$term_count - $i]->label());
80 $term_count = $term_count - 10;
81 $this->assertText("And $term_count more taxonomy term entities.");
82 $this->assertText('This action cannot be undone.');
83 $this->assertText('Make a backup of your database if you want to be able to restore these items.');
84 $this->drupalPostForm(NULL, [], t('Delete all taxonomy term entities'));
86 // Check that we are redirected to the uninstall page and data has been
88 $this->assertUrl('admin/modules/uninstall', []);
89 $this->assertText('All taxonomy term entities have been deleted.');
91 // Check that there is no more data to be deleted, Taxonomy is ready to be
93 $this->assertText('Enables the categorization of content.');
94 $this->assertNoLinkByHref('admin/modules/uninstall/entity/taxonomy_term');
96 // Uninstall the Taxonomy module.
97 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[taxonomy]' => TRUE], t('Uninstall'));
98 $this->drupalPostForm(NULL, [], t('Uninstall'));
99 $this->assertText('The selected modules have been uninstalled.');
100 $this->assertNoText('Enables the categorization of content.');
102 // Check Node cannot be uninstalled yet, there is content to be removed.
103 $this->drupalGet('admin/modules/uninstall');
104 $this->assertText('Remove content items');
105 $this->assertLinkByHref('admin/modules/uninstall/entity/node');
108 $this->drupalGet('admin/modules/uninstall/entity/node');
109 // All 10 nodes should be listed.
110 foreach ($this->nodes as $node) {
111 $this->assertText($node->label());
114 // Ensures there is no more count when not necessary.
115 $this->assertNoText('And 0 more content');
116 $this->assertText('This action cannot be undone.');
117 $this->assertText('Make a backup of your database if you want to be able to restore these items.');
119 // Create another node so we have 11.
120 $this->nodes[] = $this->drupalCreateNode(['type' => 'page']);
121 $this->drupalGet('admin/modules/uninstall/entity/node');
122 // Ensures singular case is used when a single entity is left after listing
123 // the first 10's labels.
124 $this->assertText('And 1 more content item.');
126 // Create another node so we have 12.
127 $this->nodes[] = $this->drupalCreateNode(['type' => 'article']);
128 $this->drupalGet('admin/modules/uninstall/entity/node');
129 // Ensures singular case is used when a single entity is left after listing
130 // the first 10's labels.
131 $this->assertText('And 2 more content items.');
133 $this->drupalPostForm(NULL, [], t('Delete all content items'));
135 // Check we are redirected to the uninstall page and data has been removed.
136 $this->assertUrl('admin/modules/uninstall', []);
137 $this->assertText('All content items have been deleted.');
139 // Check there is no more data to be deleted, Node is ready to be
141 $this->assertText('Allows content to be submitted to the site and displayed on pages.');
142 $this->assertNoLinkByHref('admin/modules/uninstall/entity/node');
144 // Uninstall Node module.
145 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[node]' => TRUE], t('Uninstall'));
146 $this->drupalPostForm(NULL, [], t('Uninstall'));
147 $this->assertText('The selected modules have been uninstalled.');
148 $this->assertNoText('Allows content to be submitted to the site and displayed on pages.');
150 // Ensure the proper response when accessing a non-existent entity type.
151 $this->drupalGet('admin/modules/uninstall/entity/node');
152 $this->assertResponse(404, 'Entity types that do not exist result in a 404.');
154 // Test an entity type which does not have any existing entities.
155 $this->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
156 $this->assertText('There are 0 entity test without label entities to delete.');
157 $button_xpath = '//input[@type="submit"][@value="Delete all entity test without label entities"]';
158 $this->assertNoFieldByXPath($button_xpath, NULL, 'Button with value "Delete all entity test without label entities" not found');
160 // Test an entity type without a label.
161 /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
162 $storage = $this->container->get('entity.manager')
163 ->getStorage('entity_test_no_label');
165 'id' => Unicode::strtolower($this->randomMachineName()),
166 'name' => $this->randomMachineName(),
168 $this->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
169 $this->assertText('This will delete 1 entity test without label.');
170 $this->assertFieldByXPath($button_xpath, NULL, 'Button with value "Delete all entity test without label entities" found');
172 'id' => Unicode::strtolower($this->randomMachineName()),
173 'name' => $this->randomMachineName(),
175 $this->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
176 $this->assertText('This will delete 2 entity test without label entities.');