ForumUninstallTest.php 5.44 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\forum\Functional;
4

5
use Drupal\comment\CommentInterface;
6
use Drupal\field\Entity\FieldStorageConfig;
7
use Drupal\node\Entity\NodeType;
8
use Drupal\comment\Entity\Comment;
9
use Drupal\taxonomy\Entity\Term;
10
use Drupal\Tests\BrowserTestBase;
11 12 13

/**
 * Tests forum module uninstallation.
14 15
 *
 * @group forum
16
 */
17
class ForumUninstallTest extends BrowserTestBase {
18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = ['forum'];
25 26 27 28

  /**
   * Tests if forum module uninstallation properly deletes the field.
   */
29 30
  public function testForumUninstallWithField() {
    $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'administer nodes', 'administer modules', 'delete any forum content', 'administer content types']));
31
    // Ensure that the field exists before uninstallation.
32 33
    $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
    $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
34

35
    // Create a taxonomy term.
36
    $term = Term::create([
37
      'name' => t('A term'),
38
      'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
39
      'description' => '',
40
      'parent' => [0],
41 42
      'vid' => 'forums',
      'forum_container' => 0,
43
    ]);
44 45 46
    $term->save();

    // Create a forum node.
47
    $node = $this->drupalCreateNode([
48 49
      'title' => 'A forum post',
      'type' => 'forum',
50 51
      'taxonomy_forums' => [['target_id' => $term->id()]],
    ]);
52 53

    // Create at least one comment against the forum node.
54
    $comment = Comment::create([
55 56 57 58 59
      'entity_id' => $node->nid->value,
      'entity_type' => 'node',
      'field_name' => 'comment_forum',
      'pid' => 0,
      'uid' => 0,
60
      'status' => CommentInterface::PUBLISHED,
61
      'subject' => $this->randomMachineName(),
62
      'hostname' => '127.0.0.1',
63
    ]);
64 65
    $comment->save();

66 67 68
    // Attempt to uninstall forum.
    $this->drupalGet('admin/modules/uninstall');
    // Assert forum is required.
69
    $this->assertSession()->fieldDisabled('uninstall[forum]');
70
    $this->assertText('To uninstall Forum, first delete all Forum content');
71 72

    // Delete the node.
73
    $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
74 75 76 77

    // Attempt to uninstall forum.
    $this->drupalGet('admin/modules/uninstall');
    // Assert forum is still required.
78
    $this->assertSession()->fieldDisabled('uninstall[forum]');
79
    $this->assertText('To uninstall Forum, first delete all Forums terms');
80 81

    // Delete any forum terms.
82
    $vid = $this->config('forum.settings')->get('vocabulary');
83 84 85
    $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $terms = $storage->loadByProperties(['vid' => $vid]);
    $storage->delete($terms);
86 87 88 89 90 91 92 93 94

    // Ensure that the forum node type can not be deleted.
    $this->drupalGet('admin/structure/types/manage/forum');
    $this->assertNoLink(t('Delete'));

    // Now attempt to uninstall forum.
    $this->drupalGet('admin/modules/uninstall');
    // Assert forum is no longer required.
    $this->assertFieldByName('uninstall[forum]');
95
    $this->drupalPostForm('admin/modules/uninstall', [
96
      'uninstall[forum]' => 1,
97
    ], t('Uninstall'));
98
    $this->drupalPostForm(NULL, [], t('Uninstall'));
99

100
    // Check that the field is now deleted.
101 102
    $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
    $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
103

104 105
    // Check that a node type with a machine name of forum can be created after
    // uninstalling the forum module and the node type is not locked.
106
    $edit = [
107 108 109
      'name' => 'Forum',
      'title_label' => 'title for forum',
      'type' => 'forum',
110
    ];
111 112 113 114
    $this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type'));
    $this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.');
    $this->drupalGet('admin/structure/types/manage/forum');
    $this->clickLink(t('Delete'));
115
    $this->drupalPostForm(NULL, [], t('Delete'));
116 117
    $this->assertResponse(200);
    $this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.');
118 119

    // Double check everything by reinstalling the forum module again.
120
    $this->drupalPostForm('admin/modules', ['modules[forum][enable]' => 1], 'Install');
121
    $this->assertText('Module Forum has been enabled.');
122
  }
123 124

  /**
125
   * Tests uninstallation if the field storage has been deleted beforehand.
126
   */
127
  public function testForumUninstallWithoutFieldStorage() {
128
    // Manually delete the taxonomy_forums field before module uninstallation.
129 130 131
    $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
    $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
    $field_storage->delete();
132 133

    // Check that the field is now deleted.
134 135
    $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
    $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
136

137 138
    // Delete all terms in the Forums vocabulary. Uninstalling the forum module
    // will fail unless this is done.
139
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'forums']);
140
    foreach ($terms as $term) {
141 142 143
      $term->delete();
    }

144 145
    // Ensure that uninstallation succeeds even if the field has already been
    // deleted manually beforehand.
146
    $this->container->get('module_installer')->uninstall(['forum']);
147 148 149
  }

}