3 namespace Drupal\Tests\forum\Functional;
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;
13 * Tests forum module uninstallation.
17 class ForumUninstallTest extends BrowserTestBase {
24 public static $modules = ['forum'];
27 * Tests if forum module uninstallation properly deletes the field.
29 public function testForumUninstallWithField() {
30 $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 $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
33 $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
35 // Create a taxonomy term.
36 $term = Term::create([
37 'name' => t('A term'),
38 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
42 'forum_container' => 0,
46 // Create a forum node.
47 $node = $this->drupalCreateNode([
48 'title' => 'A forum post',
50 'taxonomy_forums' => [['target_id' => $term->id()]],
53 // Create at least one comment against the forum node.
54 $comment = Comment::create([
55 'entity_id' => $node->nid->value,
56 'entity_type' => 'node',
57 'field_name' => 'comment_forum',
60 'status' => CommentInterface::PUBLISHED,
61 'subject' => $this->randomMachineName(),
62 'hostname' => '127.0.0.1',
66 // Attempt to uninstall forum.
67 $this->drupalGet('admin/modules/uninstall');
68 // Assert forum is required.
69 $this->assertSession()->fieldDisabled('uninstall[forum]');
70 $this->assertText('To uninstall Forum, first delete all Forum content');
73 $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
75 // Attempt to uninstall forum.
76 $this->drupalGet('admin/modules/uninstall');
77 // Assert forum is still required.
78 $this->assertSession()->fieldDisabled('uninstall[forum]');
79 $this->assertText('To uninstall Forum, first delete all Forums terms');
81 // Delete any forum terms.
82 $vid = $this->config('forum.settings')->get('vocabulary');
83 $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]);
84 foreach ($terms as $term) {
88 // Ensure that the forum node type can not be deleted.
89 $this->drupalGet('admin/structure/types/manage/forum');
90 $this->assertNoLink(t('Delete'));
92 // Now attempt to uninstall forum.
93 $this->drupalGet('admin/modules/uninstall');
94 // Assert forum is no longer required.
95 $this->assertFieldByName('uninstall[forum]');
96 $this->drupalPostForm('admin/modules/uninstall', [
97 'uninstall[forum]' => 1,
99 $this->drupalPostForm(NULL, [], t('Uninstall'));
101 // Check that the field is now deleted.
102 $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
103 $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
105 // Check that a node type with a machine name of forum can be created after
106 // uninstalling the forum module and the node type is not locked.
109 'title_label' => 'title for forum',
112 $this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type'));
113 $this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.');
114 $this->drupalGet('admin/structure/types/manage/forum');
115 $this->clickLink(t('Delete'));
116 $this->drupalPostForm(NULL, [], t('Delete'));
117 $this->assertResponse(200);
118 $this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.');
120 // Double check everything by reinstalling the forum module again.
121 $this->drupalPostForm('admin/modules', ['modules[forum][enable]' => 1], 'Install');
122 $this->assertText('Module Forum has been enabled.');
126 * Tests uninstallation if the field storage has been deleted beforehand.
128 public function testForumUninstallWithoutFieldStorage() {
129 // Manually delete the taxonomy_forums field before module uninstallation.
130 $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
131 $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
132 $field_storage->delete();
134 // Check that the field is now deleted.
135 $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
136 $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
138 // Delete all terms in the Forums vocabulary. Uninstalling the forum module
139 // will fail unless this is done.
140 $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => 'forums']);
141 foreach ($terms as $term) {
145 // Ensure that uninstallation succeeds even if the field has already been
146 // deleted manually beforehand.
147 $this->container->get('module_installer')->uninstall(['forum']);