View source
<?php
namespace Drupal\Tests\system\Functional\Module;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class PrepareUninstallTest extends BrowserTestBase {
use TaxonomyTestTrait;
protected $defaultTheme = 'stark';
protected $nodes;
protected $terms;
public static $modules = [
'node',
'taxonomy',
'entity_test',
];
public function setUp() {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer modules',
]);
$this
->drupalLogin($admin_user);
for ($i = 1; $i <= 5; $i++) {
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'page',
]);
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'article',
]);
}
$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
->assertText('Remove content items');
$this
->assertLinkByHref('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
->assertText($this->terms[$term_count - $i]
->label());
}
$term_count = $term_count - 10;
$this
->assertText("And {$term_count} more taxonomy terms.");
$this
->assertText('This action cannot be undone.');
$this
->assertText('Make a backup of your database if you want to be able to restore these items.');
$this
->drupalPostForm(NULL, [], t('Delete all taxonomy terms'));
$this
->assertUrl('admin/modules/uninstall', []);
$this
->assertText('All taxonomy terms have been deleted.');
$this
->assertText('Enables the categorization of content.');
$this
->assertNoLinkByHref('admin/modules/uninstall/entity/taxonomy_term');
$this
->drupalPostForm('admin/modules/uninstall', [
'uninstall[taxonomy]' => TRUE,
], t('Uninstall'));
$this
->drupalPostForm(NULL, [], t('Uninstall'));
$this
->assertText('The selected modules have been uninstalled.');
$this
->assertNoText('Enables the categorization of content.');
$this
->drupalGet('admin/modules/uninstall');
$this
->assertText('Remove content items');
$this
->assertLinkByHref('admin/modules/uninstall/entity/node');
$this
->drupalGet('admin/modules/uninstall/entity/node');
foreach ($this->nodes as $node) {
$this
->assertText($node
->label());
}
$this
->assertNoText('And 0 more content');
$this
->assertText('This action cannot be undone.');
$this
->assertText('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
->assertText('And 1 more content item.');
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'article',
]);
$this
->drupalGet('admin/modules/uninstall/entity/node');
$this
->assertText('And 2 more content items.');
$this
->drupalPostForm(NULL, [], t('Delete all content items'));
$this
->assertUrl('admin/modules/uninstall', []);
$this
->assertText('All content items have been deleted.');
$this
->assertText('Allows content to be submitted to the site and displayed on pages.');
$this
->assertNoLinkByHref('admin/modules/uninstall/entity/node');
$this
->drupalPostForm('admin/modules/uninstall', [
'uninstall[node]' => TRUE,
], t('Uninstall'));
$this
->drupalPostForm(NULL, [], t('Uninstall'));
$this
->assertText('The selected modules have been uninstalled.');
$this
->assertNoText('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
->assertText('There are 0 entity test without label entities to delete.');
$button_xpath = '//input[@type="submit"][@value="Delete all entity test without label entities"]';
$this
->assertNoFieldByXPath($button_xpath, NULL, 'Button with value "Delete all entity test without label entities" not found');
$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
->assertText('This will delete 1 entity test without label.');
$this
->assertFieldByXPath($button_xpath, NULL, 'Button with value "Delete all entity test without label entities" found');
$storage
->create([
'id' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
])
->save();
$this
->drupalGet('admin/modules/uninstall/entity/entity_test_no_label');
$this
->assertText('This will delete 2 entity test without label entities.');
}
}