View source
<?php
namespace Drupal\Tests\system\Functional\Module;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class PrepareUninstallTest extends BrowserTestBase {
use TaxonomyTestTrait;
protected $defaultTheme = 'stark';
protected $nodes;
protected $terms;
protected static $modules = [
'node',
'taxonomy',
'entity_test',
'node_access_test',
];
public function setUp() : void {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer modules',
]);
$this
->drupalLogin($admin_user);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
node_access_rebuild();
node_access_test_add_field(NodeType::load('article'));
\Drupal::state()
->set('node_access_test.private', TRUE);
for ($i = 1; $i <= 5; $i++) {
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'page',
]);
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'article',
'uid' => 0,
'private' => TRUE,
]);
}
$vocabulary = $this
->createVocabulary();
for ($i = 1; $i <= 3; $i++) {
$term = $this
->createTerm($vocabulary);
$this->terms[] = $term;
for ($j = 1; $j <= 11; $j++) {
$this->terms[] = $this
->createTerm($vocabulary, [
'parent' => [
'target_id' => $term
->id(),
],
]);
}
}
}
public function testUninstall() {
$this
->drupalGet('admin/modules/uninstall');
$this
->assertSession()
->pageTextContains('Remove content items');
$this
->assertSession()
->linkByHrefExists('admin/modules/uninstall/entity/taxonomy_term');
$this
->drupalGet('admin/modules/uninstall/entity/taxonomy_term');
$term_count = count($this->terms);
for ($i = 1; $i < 11; $i++) {
$this
->assertSession()
->pageTextContains($this->terms[$term_count - $i]
->label());
}
$term_count = $term_count - 10;
$this
->assertSession()
->pageTextContains("And {$term_count} more taxonomy terms.");
$this
->assertSession()
->pageTextContains('This action cannot be undone.');
$this
->assertSession()
->pageTextContains('Make a backup of your database if you want to be able to restore these items.');
$this
->submitForm([], 'Delete all taxonomy terms');
$this
->assertSession()
->addressEquals('admin/modules/uninstall');
$this
->assertSession()
->pageTextContains('All taxonomy terms have been deleted.');
$this
->assertSession()
->pageTextContains('Enables the categorization of content.');
$this
->assertSession()
->linkByHrefNotExists('admin/modules/uninstall/entity/taxonomy_term');
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm([
'uninstall[taxonomy]' => TRUE,
], 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->assertSession()
->pageTextContains('The selected modules have been uninstalled.');
$this
->assertSession()
->pageTextNotContains('Enables the categorization of content.');
$this
->drupalGet('admin/modules/uninstall');
$this
->assertSession()
->pageTextContains('Remove content items');
$this
->assertSession()
->linkByHrefExists('admin/modules/uninstall/entity/node');
$this
->drupalGet('admin/modules/uninstall/entity/node');
foreach ($this->nodes as $node) {
if ($node
->bundle() === 'page') {
$this
->assertSession()
->pageTextContains($node
->label());
}
else {
$node
->set('private', FALSE)
->save();
}
}
$this
->assertSession()
->pageTextContains('And 5 more content items.');
$this
->drupalGet('admin/modules/uninstall/entity/node');
foreach ($this->nodes as $node) {
$this
->assertSession()
->pageTextContains($node
->label());
}
$this
->assertSession()
->pageTextNotContains('And 0 more content');
$this
->assertSession()
->pageTextContains('This action cannot be undone.');
$this
->assertSession()
->pageTextContains('Make a backup of your database if you want to be able to restore these items.');
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'page',
]);
$this
->drupalGet('admin/modules/uninstall/entity/node');
$this
->assertSession()
->pageTextContains('And 1 more content item.');
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'article',
'private' => TRUE,
]);
$this
->drupalGet('admin/modules/uninstall/entity/node');
$this
->assertSession()
->pageTextContains('And 2 more content items.');
$this
->submitForm([], 'Delete all content items');
$this
->assertSession()
->addressEquals('admin/modules/uninstall');
$this
->assertSession()
->pageTextContains('All content items have been deleted.');
$this
->assertSession()
->pageTextContains('Allows content to be submitted to the site and displayed on pages.');
$this
->assertSession()
->linkByHrefNotExists('admin/modules/uninstall/entity/node');
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm([
'uninstall[node]' => TRUE,
], 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->assertSession()
->pageTextContains('The selected modules have been uninstalled.');
$this
->assertSession()
->pageTextNotContains('Allows content to be submitted to the site and displayed on pages.');
$this
->drupalGet('admin/modules/uninstall/entity/node');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
$this
->assertSession()
->pageTextContains('There are 0 entity test without label entities to delete.');
$this
->assertSession()
->buttonNotExists("Delete all entity test without label entities");
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test_no_label');
$storage
->create([
'id' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
])
->save();
$this
->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
$this
->assertSession()
->pageTextContains('This will delete 1 entity test without label.');
$this
->assertSession()
->buttonExists("Delete all entity test without label entities");
$storage
->create([
'id' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
])
->save();
$this
->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
$this
->assertSession()
->pageTextContains('This will delete 2 entity test without label entities.');
}
}