86d06e35eef533fce38b1b20e3f08ad8b75431b7
[yaffs-website] / web / core / modules / system / tests / src / Functional / Module / PrepareUninstallTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Module;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
8
9 /**
10  * Tests that modules which provide entity types can be uninstalled.
11  *
12  * @group Module
13  */
14 class PrepareUninstallTest extends BrowserTestBase {
15
16   use TaxonomyTestTrait;
17
18   /**
19    * An array of node objects.
20    *
21    * @var \Drupal\node\NodeInterface[]
22    */
23   protected $nodes;
24
25   /**
26    * An array of taxonomy term objects.
27    *
28    * @var \Drupal\taxonomy\TermInterface[]
29    */
30   protected $terms;
31
32   /**
33    * Modules to enable.
34    *
35    * @var array
36    */
37   public static $modules = ['node', 'taxonomy', 'entity_test'];
38
39   /**
40    * {@inheritdoc}
41    */
42   public function setUp() {
43     parent::setUp();
44
45     $admin_user = $this->drupalCreateUser(['administer modules']);
46     $this->drupalLogin($admin_user);
47
48     // Create 10 nodes.
49     for ($i = 1; $i <= 5; $i++) {
50       $this->nodes[] = $this->drupalCreateNode(['type' => 'page']);
51       $this->nodes[] = $this->drupalCreateNode(['type' => 'article']);
52     }
53
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()]]);
61       }
62     }
63   }
64
65   /**
66    * Tests that Node and Taxonomy can be uninstalled.
67    */
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');
73
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());
79     }
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'));
85
86     // Check that we are redirected to the uninstall page and data has been
87     // removed.
88     $this->assertUrl('admin/modules/uninstall', []);
89     $this->assertText('All taxonomy term entities have been deleted.');
90
91     // Check that there is no more data to be deleted, Taxonomy is ready to be
92     // uninstalled.
93     $this->assertText('Enables the categorization of content.');
94     $this->assertNoLinkByHref('admin/modules/uninstall/entity/taxonomy_term');
95
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.');
101
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');
106
107     // Delete Node data.
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());
112     }
113
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.');
118
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.');
125
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.');
132
133     $this->drupalPostForm(NULL, [], t('Delete all content items'));
134
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.');
138
139     // Check there is no more data to be deleted, Node is ready to be
140     // uninstalled.
141     $this->assertText('Allows content to be submitted to the site and displayed on pages.');
142     $this->assertNoLinkByHref('admin/modules/uninstall/entity/node');
143
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.');
149
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.');
153
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');
159
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');
164     $storage->create([
165       'id' => Unicode::strtolower($this->randomMachineName()),
166       'name' => $this->randomMachineName(),
167     ])->save();
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');
171     $storage->create([
172       'id' => Unicode::strtolower($this->randomMachineName()),
173       'name' => $this->randomMachineName(),
174     ])->save();
175     $this->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
176     $this->assertText('This will delete 2 entity test without label entities.');
177   }
178
179 }