UninstallTest.php 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Module\UninstallTest.
 */

namespace Drupal\system\Tests\Module;

10
use Drupal\Core\Cache\Cache;
11
use Drupal\Component\Utility\String;
12 13 14
use Drupal\simpletest\WebTestBase;

/**
15 16 17
 * Tests the uninstallation of modules.
 *
 * @group Module
18 19
 */
class UninstallTest extends WebTestBase {
20 21 22 23 24 25

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = array('module_test', 'user', 'views', 'node');
27

28 29 30 31 32 33
  /**
   * Tests the hook_modules_uninstalled() of the user module.
   */
  function testUserPermsUninstalled() {
    // Uninstalls the module_test module, so hook_modules_uninstalled()
    // is executed.
34
    $this->container->get('module_installer')->uninstall(array('module_test'));
35

36 37
    // Are the perms defined by module_test removed?
    $this->assertFalse(user_roles(FALSE, 'module_test perm'), 'Permissions were all removed.');
38
  }
39 40

  /**
41
   * Tests the Uninstall page and Uninstall confirmation page.
42 43 44 45
   */
  function testUninstallPage() {
    $account = $this->drupalCreateUser(array('administer modules'));
    $this->drupalLogin($account);
46 47 48 49 50 51 52 53

    // Create a node type.
    $node_type = entity_create('node_type', array('type' => 'uninstall_blocker'));
    $node_type->save();
    // Add a node to prevent node from being uninstalled.
    $node = entity_create('node', array('type' => 'uninstall_blocker'));
    $node->save();

54 55
    $this->drupalGet('admin/modules/uninstall');
    $this->assertTitle(t('Uninstall') . ' | Drupal');
56

57 58 59 60 61
    $this->assertText(\Drupal::translation()->translate('The following reasons prevents Node from being uninstalled: There is content for the entity type: Content'), 'Content prevents uninstalling node module.');
    // Delete the node to allow node to be uninstalled.
    $node->delete();
    $node_type->delete();

62 63 64 65
    // Uninstall module_test.
    $edit = array();
    $edit['uninstall[module_test]'] = TRUE;
    $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
66
    $this->assertNoText(\Drupal::translation()->translate('Affected configuration'), 'No configuration deletions listed on the module install confirmation page.');
67 68 69 70 71 72 73 74 75
    $this->drupalPostForm(NULL, NULL, t('Uninstall'));
    $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');

    // Uninstall node testing that the configuration that will be deleted is
    // listed.
    $node_dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('module', array('node'));
    $edit = array();
    $edit['uninstall[node]'] = TRUE;
    $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
76
    $this->assertText(\Drupal::translation()->translate('Affected configuration'), 'Configuration deletions listed on the module install confirmation page.');
77 78 79 80 81 82 83 84 85 86 87 88 89 90

    $entity_types = array();
    foreach ($node_dependencies as $entity) {
      $label = $entity->label();
      $this->assertText($label, String::format('The entity label "!label" found.', array('!label' => $label)));
      $entity_types[] = $entity->getEntityTypeId();
    }
    $entity_types = array_unique($entity_types);
    foreach ($entity_types as $entity_type_id) {
      $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
      // Add h3's since the entity type label is often repeated in the entity
      // labels.
      $this->assertRaw('<h3>' . $entity_type->getLabel() . '</h3>');
    }
91 92 93 94 95 96 97

    // Set a unique cache entry to be able to test whether all caches are
    // cleared during the uninstall.
    \Drupal::cache()->set('uninstall_test', 'test_uninstall_page', Cache::PERMANENT);
    $cached = \Drupal::cache()->get('uninstall_test');
    $this->assertEqual($cached->data, 'test_uninstall_page', String::format('Cache entry found: @bin', array('@bin' => $cached->data)));

98 99
    $this->drupalPostForm(NULL, NULL, t('Uninstall'));
    $this->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
100
    $this->assertNoRaw('&lt;label', 'The page does not have double escaped HTML tags.');
101 102 103 104

    // Make sure our unique cache entry is gone.
    $cached = \Drupal::cache()->get('uninstall_test');
    $this->assertFalse($cached, 'Cache entry not found');
105
  }
106
}