/modules/forum/tests/src/Functional/ForumUninstallTest.php

https://github.com/drupal/core · PHP · 160 lines · 95 code · 23 blank · 42 comment · 0 complexity · 1cb270d3ef0e65e076d9cd0c1287d053 MD5 · raw file

  1. <?php
  2. namespace Drupal\Tests\forum\Functional;
  3. use Drupal\comment\CommentInterface;
  4. use Drupal\field\Entity\FieldStorageConfig;
  5. use Drupal\node\Entity\NodeType;
  6. use Drupal\comment\Entity\Comment;
  7. use Drupal\taxonomy\Entity\Term;
  8. use Drupal\Tests\BrowserTestBase;
  9. /**
  10. * Tests forum module uninstallation.
  11. *
  12. * @group forum
  13. */
  14. class ForumUninstallTest extends BrowserTestBase {
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['forum'];
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected $defaultTheme = 'stark';
  25. /**
  26. * Tests if forum module uninstallation properly deletes the field.
  27. */
  28. public function testForumUninstallWithField() {
  29. $this->drupalLogin($this->drupalCreateUser([
  30. 'administer taxonomy',
  31. 'administer nodes',
  32. 'administer modules',
  33. 'delete any forum content',
  34. 'administer content types',
  35. ]));
  36. // Ensure that the field exists before uninstallation.
  37. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
  38. $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
  39. // Create a taxonomy term.
  40. $term = Term::create([
  41. 'name' => t('A term'),
  42. 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
  43. 'description' => '',
  44. 'parent' => [0],
  45. 'vid' => 'forums',
  46. 'forum_container' => 0,
  47. ]);
  48. $term->save();
  49. // Create a forum node.
  50. $node = $this->drupalCreateNode([
  51. 'title' => 'A forum post',
  52. 'type' => 'forum',
  53. 'taxonomy_forums' => [['target_id' => $term->id()]],
  54. ]);
  55. // Create at least one comment against the forum node.
  56. $comment = Comment::create([
  57. 'entity_id' => $node->nid->value,
  58. 'entity_type' => 'node',
  59. 'field_name' => 'comment_forum',
  60. 'pid' => 0,
  61. 'uid' => 0,
  62. 'status' => CommentInterface::PUBLISHED,
  63. 'subject' => $this->randomMachineName(),
  64. 'hostname' => '127.0.0.1',
  65. ]);
  66. $comment->save();
  67. // Attempt to uninstall forum.
  68. $this->drupalGet('admin/modules/uninstall');
  69. // Assert forum is required.
  70. $this->assertSession()->fieldDisabled('uninstall[forum]');
  71. $this->assertText('To uninstall Forum, first delete all Forum content');
  72. // Delete the node.
  73. $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
  74. // Attempt to uninstall forum.
  75. $this->drupalGet('admin/modules/uninstall');
  76. // Assert forum is still required.
  77. $this->assertSession()->fieldDisabled('uninstall[forum]');
  78. $this->assertText('To uninstall Forum, first delete all Forums terms');
  79. // Delete any forum terms.
  80. $vid = $this->config('forum.settings')->get('vocabulary');
  81. $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  82. $terms = $storage->loadByProperties(['vid' => $vid]);
  83. $storage->delete($terms);
  84. // Ensure that the forum node type can not be deleted.
  85. $this->drupalGet('admin/structure/types/manage/forum');
  86. $this->assertSession()->linkNotExists(t('Delete'));
  87. // Now attempt to uninstall forum.
  88. $this->drupalGet('admin/modules/uninstall');
  89. // Assert forum is no longer required.
  90. $this->assertFieldByName('uninstall[forum]');
  91. $this->drupalPostForm('admin/modules/uninstall', [
  92. 'uninstall[forum]' => 1,
  93. ], t('Uninstall'));
  94. $this->drupalPostForm(NULL, [], t('Uninstall'));
  95. // Check that the field is now deleted.
  96. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
  97. $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
  98. // Check that a node type with a machine name of forum can be created after
  99. // uninstalling the forum module and the node type is not locked.
  100. $edit = [
  101. 'name' => 'Forum',
  102. 'title_label' => 'title for forum',
  103. 'type' => 'forum',
  104. ];
  105. $this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type'));
  106. $this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.');
  107. $this->drupalGet('admin/structure/types/manage/forum');
  108. $this->clickLink(t('Delete'));
  109. $this->drupalPostForm(NULL, [], t('Delete'));
  110. $this->assertSession()->statusCodeEquals(200);
  111. $this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.');
  112. // Double check everything by reinstalling the forum module again.
  113. $this->drupalPostForm('admin/modules', ['modules[forum][enable]' => 1], 'Install');
  114. $this->assertText('Module Forum has been enabled.');
  115. }
  116. /**
  117. * Tests uninstallation if the field storage has been deleted beforehand.
  118. */
  119. public function testForumUninstallWithoutFieldStorage() {
  120. // Manually delete the taxonomy_forums field before module uninstallation.
  121. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
  122. $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
  123. $field_storage->delete();
  124. // Check that the field is now deleted.
  125. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
  126. $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
  127. // Delete all terms in the Forums vocabulary. Uninstalling the forum module
  128. // will fail unless this is done.
  129. $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'forums']);
  130. foreach ($terms as $term) {
  131. $term->delete();
  132. }
  133. // Ensure that uninstallation succeeds even if the field has already been
  134. // deleted manually beforehand.
  135. $this->container->get('module_installer')->uninstall(['forum']);
  136. }
  137. }