Version 1
[yaffs-website] / web / core / modules / forum / tests / src / Functional / ForumUninstallTest.php
1 <?php
2
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    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['forum'];
25
26   /**
27    * Tests if forum module uninstallation properly deletes the field.
28    */
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.');
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       'vid' => 'forums',
42       'forum_container' => 0,
43     ]);
44     $term->save();
45
46     // Create a forum node.
47     $node = $this->drupalCreateNode([
48       'title' => 'A forum post',
49       'type' => 'forum',
50       'taxonomy_forums' => [['target_id' => $term->id()]],
51     ]);
52
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',
58       'pid' => 0,
59       'uid' => 0,
60       'status' => CommentInterface::PUBLISHED,
61       'subject' => $this->randomMachineName(),
62       'hostname' => '127.0.0.1',
63     ]);
64     $comment->save();
65
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');
71
72     // Delete the node.
73     $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
74
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');
80
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) {
85       $term->delete();
86     }
87
88     // Ensure that the forum node type can not be deleted.
89     $this->drupalGet('admin/structure/types/manage/forum');
90     $this->assertNoLink(t('Delete'));
91
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,
98     ], t('Uninstall'));
99     $this->drupalPostForm(NULL, [], t('Uninstall'));
100
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.');
104
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.
107     $edit = [
108       'name' => 'Forum',
109       'title_label' => 'title for forum',
110       'type' => 'forum',
111     ];
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.');
119
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.');
123   }
124
125   /**
126    * Tests uninstallation if the field storage has been deleted beforehand.
127    */
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();
133
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.');
137
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) {
142       $term->delete();
143     }
144
145     // Ensure that uninstallation succeeds even if the field has already been
146     // deleted manually beforehand.
147     $this->container->get('module_installer')->uninstall(['forum']);
148   }
149
150 }