CommentTypeTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace Drupal\Tests\comment\Functional;
  3. use Drupal\Core\Url;
  4. use Drupal\comment\Entity\Comment;
  5. use Drupal\comment\Entity\CommentType;
  6. use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
  7. use Drupal\field\Entity\FieldStorageConfig;
  8. use Drupal\field\Entity\FieldConfig;
  9. use Drupal\node\Entity\Node;
  10. /**
  11. * Ensures that comment type functions work correctly.
  12. *
  13. * @group comment
  14. */
  15. class CommentTypeTest extends CommentTestBase {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected $defaultTheme = 'stark';
  20. /**
  21. * Admin user.
  22. *
  23. * @var \Drupal\Core\Session\AccountInterface
  24. */
  25. protected $adminUser;
  26. /**
  27. * Permissions to grant admin user.
  28. *
  29. * @var array
  30. */
  31. protected $permissions = [
  32. 'administer comments',
  33. 'administer comment fields',
  34. 'administer comment types',
  35. ];
  36. /**
  37. * Sets the test up.
  38. */
  39. protected function setUp() {
  40. parent::setUp();
  41. $this->drupalPlaceBlock('page_title_block');
  42. $this->adminUser = $this->drupalCreateUser($this->permissions);
  43. }
  44. /**
  45. * Tests creating a comment type programmatically and via a form.
  46. */
  47. public function testCommentTypeCreation() {
  48. // Create a comment type programmatically.
  49. $type = $this->createCommentType('other');
  50. $comment_type = CommentType::load('other');
  51. $this->assertInstanceOf(CommentType::class, $comment_type);
  52. // Log in a test user.
  53. $this->drupalLogin($this->adminUser);
  54. // Ensure that the new comment type admin page can be accessed.
  55. $this->drupalGet('admin/structure/comment/manage/' . $type->id());
  56. $this->assertSession()->statusCodeEquals(200);
  57. // Create a comment type via the user interface.
  58. $edit = [
  59. 'id' => 'foo',
  60. 'label' => 'title for foo',
  61. 'description' => '',
  62. 'target_entity_type_id' => 'node',
  63. ];
  64. $this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save'));
  65. $comment_type = CommentType::load('foo');
  66. $this->assertInstanceOf(CommentType::class, $comment_type);
  67. // Check that the comment type was created in site default language.
  68. $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
  69. $this->assertEqual($comment_type->language()->getId(), $default_langcode);
  70. // Edit the comment-type and ensure that we cannot change the entity-type.
  71. $this->drupalGet('admin/structure/comment/manage/foo');
  72. $this->assertNoField('target_entity_type_id', 'Entity type file not present');
  73. $this->assertText(t('Target entity type'));
  74. // Save the form and ensure the entity-type value is preserved even though
  75. // the field isn't present.
  76. $this->drupalPostForm(NULL, [], t('Save'));
  77. \Drupal::entityTypeManager()->getStorage('comment_type')->resetCache(['foo']);
  78. $comment_type = CommentType::load('foo');
  79. $this->assertEqual($comment_type->getTargetEntityTypeId(), 'node');
  80. }
  81. /**
  82. * Tests editing a comment type using the UI.
  83. */
  84. public function testCommentTypeEditing() {
  85. $this->drupalLogin($this->adminUser);
  86. $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
  87. $this->assertEqual($field->getLabel(), 'Comment', 'Comment body field was found.');
  88. // Change the comment type name.
  89. $this->drupalGet('admin/structure/comment');
  90. $edit = [
  91. 'label' => 'Bar',
  92. ];
  93. $this->drupalPostForm('admin/structure/comment/manage/comment', $edit, t('Save'));
  94. $this->drupalGet('admin/structure/comment');
  95. $this->assertRaw('Bar', 'New name was displayed.');
  96. $this->clickLink('Manage fields');
  97. $this->assertUrl(Url::fromRoute('entity.comment.field_ui_fields', ['comment_type' => 'comment'], ['absolute' => TRUE])->toString(), [], 'Original machine name was used in URL.');
  98. $this->assertCount(1, $this->cssSelect('tr#comment-body'), 'Body field exists.');
  99. // Remove the body field.
  100. $this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete', [], t('Delete'));
  101. // Resave the settings for this type.
  102. $this->drupalPostForm('admin/structure/comment/manage/comment', [], t('Save'));
  103. // Check that the body field doesn't exist.
  104. $this->drupalGet('admin/structure/comment/manage/comment/fields');
  105. $this->assertCount(0, $this->cssSelect('tr#comment-body'), 'Body field does not exist.');
  106. }
  107. /**
  108. * Tests deleting a comment type that still has content.
  109. */
  110. public function testCommentTypeDeletion() {
  111. // Create a comment type programmatically.
  112. $type = $this->createCommentType('foo');
  113. $this->drupalCreateContentType(['type' => 'page']);
  114. $this->addDefaultCommentField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo');
  115. $field_storage = FieldStorageConfig::loadByName('node', 'foo');
  116. $this->drupalLogin($this->adminUser);
  117. // Create a node.
  118. $node = Node::create([
  119. 'type' => 'page',
  120. 'title' => 'foo',
  121. ]);
  122. $node->save();
  123. // Add a new comment of this type.
  124. $comment = Comment::create([
  125. 'comment_type' => 'foo',
  126. 'entity_type' => 'node',
  127. 'field_name' => 'foo',
  128. 'entity_id' => $node->id(),
  129. ]);
  130. $comment->save();
  131. // Attempt to delete the comment type, which should not be allowed.
  132. $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
  133. $this->assertRaw(
  134. t('%label is used by 1 comment on your site. You can not remove this comment type until you have removed all of the %label comments.', ['%label' => $type->label()]),
  135. 'The comment type will not be deleted until all comments of that type are removed.'
  136. );
  137. $this->assertRaw(
  138. t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', [
  139. '%label' => 'foo',
  140. '%field' => 'node.foo',
  141. ]),
  142. 'The comment type will not be deleted until all fields of that type are removed.'
  143. );
  144. $this->assertNoText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is not available.');
  145. // Delete the comment and the field.
  146. $comment->delete();
  147. $field_storage->delete();
  148. // Attempt to delete the comment type, which should now be allowed.
  149. $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
  150. $this->assertRaw(
  151. t('Are you sure you want to delete the comment type %type?', ['%type' => $type->id()]),
  152. 'The comment type is available for deletion.'
  153. );
  154. $this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.');
  155. // Test exception thrown when re-using an existing comment type.
  156. try {
  157. $this->addDefaultCommentField('comment', 'comment', 'bar');
  158. $this->fail('Exception not thrown.');
  159. }
  160. catch (\InvalidArgumentException $e) {
  161. // Expected exception; just continue testing.
  162. }
  163. // Delete the comment type.
  164. $this->drupalPostForm('admin/structure/comment/manage/' . $type->id() . '/delete', [], t('Delete'));
  165. $this->assertNull(CommentType::load($type->id()), 'Comment type deleted.');
  166. $this->assertRaw(t('The comment type %label has been deleted.', ['%label' => $type->label()]));
  167. }
  168. }