View source
<?php
namespace Drupal\Tests\system\Functional\Module;
use Drupal\Core\Cache\Cache;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
class UninstallTest extends BrowserTestBase {
protected static $modules = [
'module_test',
'user',
'views',
'node',
];
protected $defaultTheme = 'stark';
public function testUserPermsUninstalled() {
$this->container
->get('module_installer')
->uninstall([
'module_test',
]);
$this
->assertEmpty(user_roles(FALSE, 'module_test perm'), 'Permissions were all removed.');
}
public function testUninstallPage() {
$account = $this
->drupalCreateUser([
'administer modules',
]);
$this
->drupalLogin($account);
$node_type = NodeType::create([
'type' => 'uninstall_blocker',
'name' => 'Uninstall blocker',
]);
$node_type
->setThirdPartySetting('module_test', 'key', 'value');
$node_type
->save();
$node = Node::create([
'type' => 'uninstall_blocker',
'title' => $this
->randomString(),
]);
$node
->save();
$this
->config('core.extension')
->set('module.system_status_obsolete_test', 0)
->set('module.deprecated_module', 0)
->set('module.experimental_module_test', 0)
->save();
$this
->rebuildAll();
$this
->drupalGet('admin/modules/uninstall');
$this
->assertSession()
->titleEquals('Uninstall | Drupal');
$this
->assertSession()
->elementExists('xpath', "//a[contains(@aria-label, 'View information on the Experimental status of the module Experimental Test')]");
$this
->assertSession()
->elementExists('xpath', "//a[contains(@href, 'https://example.com/experimental')]");
$this
->assertSession()
->elementExists('xpath', "//a[contains(@aria-label, 'View information on the Deprecated status of the module Deprecated module')]");
$this
->assertSession()
->elementExists('xpath', "//a[contains(@href, 'http://example.com/deprecated')]");
$this
->assertSession()
->elementExists('xpath', "//a[contains(@aria-label, 'View information on the Obsolete status of the module System obsolete status test')]");
$this
->assertSession()
->elementExists('xpath', "//a[contains(@href, 'https://example.com/obsolete')]");
foreach (\Drupal::service('extension.list.module')
->getAllInstalledInfo() as $module => $info) {
$field_name = "uninstall[{$module}]";
if (!empty($info['required'])) {
$this
->assertSession()
->fieldNotExists($field_name);
}
else {
$this
->assertSession()
->fieldExists($field_name);
}
}
$this
->assertSession()
->responseContains('<label for="edit-uninstall-node" class="module-name table-filter-text-source">Node</label>');
$this
->assertSession()
->pageTextContains('The following reason prevents Node from being uninstalled:');
$this
->assertSession()
->pageTextContains('There is content for the entity type: Content');
$node
->delete();
$edit = [];
$edit['uninstall[module_test]'] = TRUE;
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->assertSession()
->pageTextNotContains('Configuration deletions');
$this
->assertSession()
->pageTextContains('Configuration updates');
$this
->assertSession()
->pageTextContains($node_type
->label());
$this
->submitForm([], 'Uninstall');
$this
->assertSession()
->pageTextContains('The selected modules have been uninstalled.');
$node_dependencies = \Drupal::service('config.manager')
->findConfigEntityDependenciesAsEntities('module', [
'node',
]);
$edit = [];
$edit['uninstall[node]'] = TRUE;
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->assertSession()
->pageTextContains('Configuration deletions');
$this
->assertSession()
->pageTextNotContains('Configuration updates');
$entity_types = [];
foreach ($node_dependencies as $entity) {
$label = $entity
->label() ?: $entity
->id();
$this
->assertSession()
->pageTextContains($label);
$entity_types[] = $entity
->getEntityTypeId();
}
$entity_types = array_unique($entity_types);
foreach ($entity_types as $entity_type_id) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($entity_type_id);
$this
->assertSession()
->responseContains('<h3>' . $entity_type
->getLabel() . '</h3>');
}
\Drupal::cache()
->set('uninstall_test', 'test_uninstall_page', Cache::PERMANENT);
$cached = \Drupal::cache()
->get('uninstall_test');
$this
->assertEquals('test_uninstall_page', $cached->data, new FormattableMarkup('Cache entry found: @bin', [
'@bin' => $cached->data,
]));
$this
->submitForm([], 'Uninstall');
$this
->assertSession()
->pageTextContains('The selected modules have been uninstalled.');
$this
->assertSession()
->responseNotContains('<label');
$cached = \Drupal::cache()
->get('uninstall_test');
$this
->assertFalse($cached, 'Cache entry not found');
$this
->drupalGet('admin/modules/uninstall/confirm');
$this
->assertSession()
->pageTextContains('The selected modules could not be uninstalled, either due to a website problem or due to the uninstall confirmation form timing out. Please try again.');
$this
->drupalGet('admin/modules/uninstall/confirm');
$this
->assertSession()
->addressEquals('admin/modules/uninstall');
$this
->assertSession()
->titleEquals('Uninstall | Drupal');
$edit = [];
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->assertSession()
->pageTextContains('No modules selected.');
}
public function testFailedInstallStatus() {
$account = $this
->drupalCreateUser([
'administer modules',
]);
$this
->drupalLogin($account);
$message = 'Exception thrown when installing module_installer_config_test with an invalid configuration file.';
try {
$this->container
->get('module_installer')
->install([
'module_installer_config_test',
]);
$this
->fail($message);
} catch (EntityMalformedException $e) {
}
$this
->drupalGet('admin/modules/uninstall');
$this
->assertSession()
->pageTextContains('Module installer config test');
$edit['uninstall[module_installer_config_test]'] = TRUE;
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->assertSession()
->pageTextContains('The selected modules have been uninstalled.');
$this
->assertSession()
->pageTextNotContains('Module installer config test');
}
}