annotate core/modules/system/src/Tests/Module/PrepareUninstallTest.php @ 9:1fc0ff908d1f

Add another data file
author Chris Cannam
date Mon, 05 Feb 2018 12:34:32 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Module;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Unicode;
Chris@0 6 use Drupal\simpletest\WebTestBase;
Chris@0 7 use Drupal\taxonomy\Tests\TaxonomyTestTrait;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests that modules which provide entity types can be uninstalled.
Chris@0 11 *
Chris@0 12 * @group Module
Chris@0 13 */
Chris@0 14 class PrepareUninstallTest extends WebTestBase {
Chris@0 15
Chris@0 16 use TaxonomyTestTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * An array of node objects.
Chris@0 20 *
Chris@0 21 * @var \Drupal\node\NodeInterface[]
Chris@0 22 */
Chris@0 23 protected $nodes;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * An array of taxonomy term objects.
Chris@0 27 *
Chris@0 28 * @var \Drupal\taxonomy\TermInterface[]
Chris@0 29 */
Chris@0 30 protected $terms;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Modules to enable.
Chris@0 34 *
Chris@0 35 * @var array
Chris@0 36 */
Chris@0 37 public static $modules = ['node', 'taxonomy', 'entity_test'];
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 public function setUp() {
Chris@0 43 parent::setUp();
Chris@0 44
Chris@0 45 $admin_user = $this->drupalCreateUser(['administer modules']);
Chris@0 46 $this->drupalLogin($admin_user);
Chris@0 47
Chris@0 48 // Create 10 nodes.
Chris@0 49 for ($i = 1; $i <= 5; $i++) {
Chris@0 50 $this->nodes[] = $this->drupalCreateNode(['type' => 'page']);
Chris@0 51 $this->nodes[] = $this->drupalCreateNode(['type' => 'article']);
Chris@0 52 }
Chris@0 53
Chris@0 54 // Create 3 top-level taxonomy terms, each with 11 children.
Chris@0 55 $vocabulary = $this->createVocabulary();
Chris@0 56 for ($i = 1; $i <= 3; $i++) {
Chris@0 57 $term = $this->createTerm($vocabulary);
Chris@0 58 $this->terms[] = $term;
Chris@0 59 for ($j = 1; $j <= 11; $j++) {
Chris@0 60 $this->terms[] = $this->createTerm($vocabulary, ['parent' => ['target_id' => $term->id()]]);
Chris@0 61 }
Chris@0 62 }
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Tests that Node and Taxonomy can be uninstalled.
Chris@0 67 */
Chris@0 68 public function testUninstall() {
Chris@0 69 // Check that Taxonomy cannot be uninstalled yet.
Chris@0 70 $this->drupalGet('admin/modules/uninstall');
Chris@0 71 $this->assertText('Remove content items');
Chris@0 72 $this->assertLinkByHref('admin/modules/uninstall/entity/taxonomy_term');
Chris@0 73
Chris@0 74 // Delete Taxonomy term data.
Chris@0 75 $this->drupalGet('admin/modules/uninstall/entity/taxonomy_term');
Chris@0 76 $term_count = count($this->terms);
Chris@0 77 for ($i = 1; $i < 11; $i++) {
Chris@0 78 $this->assertText($this->terms[$term_count - $i]->label());
Chris@0 79 }
Chris@0 80 $term_count = $term_count - 10;
Chris@0 81 $this->assertText("And $term_count more taxonomy term entities.");
Chris@0 82 $this->assertText('This action cannot be undone.');
Chris@0 83 $this->assertText('Make a backup of your database if you want to be able to restore these items.');
Chris@0 84 $this->drupalPostForm(NULL, [], t('Delete all taxonomy term entities'));
Chris@0 85
Chris@0 86 // Check that we are redirected to the uninstall page and data has been
Chris@0 87 // removed.
Chris@0 88 $this->assertUrl('admin/modules/uninstall', []);
Chris@0 89 $this->assertText('All taxonomy term entities have been deleted.');
Chris@0 90
Chris@0 91 // Check that there is no more data to be deleted, Taxonomy is ready to be
Chris@0 92 // uninstalled.
Chris@0 93 $this->assertText('Enables the categorization of content.');
Chris@0 94 $this->assertNoLinkByHref('admin/modules/uninstall/entity/taxonomy_term');
Chris@0 95
Chris@0 96 // Uninstall the Taxonomy module.
Chris@0 97 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[taxonomy]' => TRUE], t('Uninstall'));
Chris@0 98 $this->drupalPostForm(NULL, [], t('Uninstall'));
Chris@0 99 $this->assertText('The selected modules have been uninstalled.');
Chris@0 100 $this->assertNoText('Enables the categorization of content.');
Chris@0 101
Chris@0 102 // Check Node cannot be uninstalled yet, there is content to be removed.
Chris@0 103 $this->drupalGet('admin/modules/uninstall');
Chris@0 104 $this->assertText('Remove content items');
Chris@0 105 $this->assertLinkByHref('admin/modules/uninstall/entity/node');
Chris@0 106
Chris@0 107 // Delete Node data.
Chris@0 108 $this->drupalGet('admin/modules/uninstall/entity/node');
Chris@0 109 // All 10 nodes should be listed.
Chris@0 110 foreach ($this->nodes as $node) {
Chris@0 111 $this->assertText($node->label());
Chris@0 112 }
Chris@0 113
Chris@0 114 // Ensures there is no more count when not necessary.
Chris@0 115 $this->assertNoText('And 0 more content');
Chris@0 116 $this->assertText('This action cannot be undone.');
Chris@0 117 $this->assertText('Make a backup of your database if you want to be able to restore these items.');
Chris@0 118
Chris@0 119 // Create another node so we have 11.
Chris@0 120 $this->nodes[] = $this->drupalCreateNode(['type' => 'page']);
Chris@0 121 $this->drupalGet('admin/modules/uninstall/entity/node');
Chris@0 122 // Ensures singular case is used when a single entity is left after listing
Chris@0 123 // the first 10's labels.
Chris@0 124 $this->assertText('And 1 more content item.');
Chris@0 125
Chris@0 126 // Create another node so we have 12.
Chris@0 127 $this->nodes[] = $this->drupalCreateNode(['type' => 'article']);
Chris@0 128 $this->drupalGet('admin/modules/uninstall/entity/node');
Chris@0 129 // Ensures singular case is used when a single entity is left after listing
Chris@0 130 // the first 10's labels.
Chris@0 131 $this->assertText('And 2 more content items.');
Chris@0 132
Chris@0 133 $this->drupalPostForm(NULL, [], t('Delete all content items'));
Chris@0 134
Chris@0 135 // Check we are redirected to the uninstall page and data has been removed.
Chris@0 136 $this->assertUrl('admin/modules/uninstall', []);
Chris@0 137 $this->assertText('All content items have been deleted.');
Chris@0 138
Chris@0 139 // Check there is no more data to be deleted, Node is ready to be
Chris@0 140 // uninstalled.
Chris@0 141 $this->assertText('Allows content to be submitted to the site and displayed on pages.');
Chris@0 142 $this->assertNoLinkByHref('admin/modules/uninstall/entity/node');
Chris@0 143
Chris@0 144 // Uninstall Node module.
Chris@0 145 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[node]' => TRUE], t('Uninstall'));
Chris@0 146 $this->drupalPostForm(NULL, [], t('Uninstall'));
Chris@0 147 $this->assertText('The selected modules have been uninstalled.');
Chris@0 148 $this->assertNoText('Allows content to be submitted to the site and displayed on pages.');
Chris@0 149
Chris@0 150 // Ensure the proper response when accessing a non-existent entity type.
Chris@0 151 $this->drupalGet('admin/modules/uninstall/entity/node');
Chris@0 152 $this->assertResponse(404, 'Entity types that do not exist result in a 404.');
Chris@0 153
Chris@0 154 // Test an entity type which does not have any existing entities.
Chris@0 155 $this->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
Chris@0 156 $this->assertText('There are 0 entity test without label entities to delete.');
Chris@0 157 $button_xpath = '//input[@type="submit"][@value="Delete all entity test without label entities"]';
Chris@0 158 $this->assertNoFieldByXPath($button_xpath, NULL, 'Button with value "Delete all entity test without label entities" not found');
Chris@0 159
Chris@0 160 // Test an entity type without a label.
Chris@0 161 /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
Chris@0 162 $storage = $this->container->get('entity.manager')
Chris@0 163 ->getStorage('entity_test_no_label');
Chris@0 164 $storage->create([
Chris@0 165 'id' => Unicode::strtolower($this->randomMachineName()),
Chris@0 166 'name' => $this->randomMachineName(),
Chris@0 167 ])->save();
Chris@0 168 $this->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
Chris@0 169 $this->assertText('This will delete 1 entity test without label.');
Chris@0 170 $this->assertFieldByXPath($button_xpath, NULL, 'Button with value "Delete all entity test without label entities" found');
Chris@0 171 $storage->create([
Chris@0 172 'id' => Unicode::strtolower($this->randomMachineName()),
Chris@0 173 'name' => $this->randomMachineName(),
Chris@0 174 ])->save();
Chris@0 175 $this->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
Chris@0 176 $this->assertText('This will delete 2 entity test without label entities.');
Chris@0 177 }
Chris@0 178
Chris@0 179 }