CommentAnonymousTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace Drupal\Tests\comment\Functional;
  3. use Drupal\comment\CommentInterface;
  4. use Drupal\user\RoleInterface;
  5. /**
  6. * Tests anonymous commenting.
  7. *
  8. * @group comment
  9. */
  10. class CommentAnonymousTest extends CommentTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected $defaultTheme = 'classy';
  15. protected function setUp() {
  16. parent::setUp();
  17. // Enable anonymous and authenticated user comments.
  18. user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
  19. 'access comments',
  20. 'post comments',
  21. 'skip comment approval',
  22. ]);
  23. user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
  24. 'access comments',
  25. 'post comments',
  26. 'skip comment approval',
  27. ]);
  28. }
  29. /**
  30. * Tests anonymous comment functionality.
  31. */
  32. public function testAnonymous() {
  33. $this->drupalLogin($this->adminUser);
  34. $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
  35. $this->drupalLogout();
  36. // Preview comments (with `skip comment approval` permission).
  37. $edit = [];
  38. $title = 'comment title with skip comment approval';
  39. $body = 'comment body with skip comment approval';
  40. $edit['subject[0][value]'] = $title;
  41. $edit['comment_body[0][value]'] = $body;
  42. $this->drupalPostForm($this->node->toUrl(), $edit, t('Preview'));
  43. // Cannot use assertRaw here since both title and body are in the form.
  44. $preview = (string) $this->cssSelect('.preview')[0]->getHtml();
  45. $this->assertStringContainsString($title, $preview, 'Anonymous user can preview comment title.');
  46. $this->assertStringContainsString($body, $preview, 'Anonymous user can preview comment body.');
  47. // Preview comments (without `skip comment approval` permission).
  48. user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
  49. $edit = [];
  50. $title = 'comment title without skip comment approval';
  51. $body = 'comment body without skip comment approval';
  52. $edit['subject[0][value]'] = $title;
  53. $edit['comment_body[0][value]'] = $body;
  54. $this->drupalPostForm($this->node->toUrl(), $edit, t('Preview'));
  55. // Cannot use assertRaw here since both title and body are in the form.
  56. $preview = (string) $this->cssSelect('.preview')[0]->getHtml();
  57. $this->assertStringContainsString($title, $preview, 'Anonymous user can preview comment title.');
  58. $this->assertStringContainsString($body, $preview, 'Anonymous user can preview comment body.');
  59. user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
  60. // Post anonymous comment without contact info.
  61. $anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
  62. $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
  63. // Ensure anonymous users cannot post in the name of registered users.
  64. $edit = [
  65. 'name' => $this->adminUser->getAccountName(),
  66. 'comment_body[0][value]' => $this->randomMachineName(),
  67. ];
  68. $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
  69. $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
  70. '%name' => $this->adminUser->getAccountName(),
  71. ]));
  72. // Allow contact info.
  73. $this->drupalLogin($this->adminUser);
  74. $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT);
  75. // Attempt to edit anonymous comment.
  76. $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit');
  77. $edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
  78. $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
  79. $this->drupalLogout();
  80. // Post anonymous comment with contact info (optional).
  81. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
  82. $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
  83. // Check the presence of expected cache tags.
  84. $this->assertCacheTag('config:field.field.node.article.comment');
  85. $this->assertCacheTag('config:user.settings');
  86. $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
  87. $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
  88. // Ensure anonymous users cannot post in the name of registered users.
  89. $edit = [
  90. 'name' => $this->adminUser->getAccountName(),
  91. 'mail' => $this->randomMachineName() . '@example.com',
  92. 'subject[0][value]' => $this->randomMachineName(),
  93. 'comment_body[0][value]' => $this->randomMachineName(),
  94. ];
  95. $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
  96. $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
  97. '%name' => $this->adminUser->getAccountName(),
  98. ]));
  99. // Require contact info.
  100. $this->drupalLogin($this->adminUser);
  101. $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT);
  102. $this->drupalLogout();
  103. // Try to post comment with contact info (required).
  104. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
  105. $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
  106. $anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
  107. // Name should have 'Anonymous' for value by default.
  108. $this->assertText(t('Email field is required.'), 'Email required.');
  109. $this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
  110. // Post comment with contact info (required).
  111. $author_name = $this->randomMachineName();
  112. $author_mail = $this->randomMachineName() . '@example.com';
  113. $anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
  114. $this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
  115. // Make sure the user data appears correctly when editing the comment.
  116. $this->drupalLogin($this->adminUser);
  117. $this->drupalGet('comment/' . $anonymous_comment3->id() . '/edit');
  118. $this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment.");
  119. $this->assertFieldByName('uid', '', 'The author field is empty (i.e. anonymous) when editing the comment.');
  120. $this->assertRaw($author_mail, "The anonymous user's email address is correct when editing the comment.");
  121. // Unpublish comment.
  122. $this->performCommentOperation($anonymous_comment3, 'unpublish');
  123. $this->drupalGet('admin/content/comment/approval');
  124. $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was unpublished.');
  125. // Publish comment.
  126. $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
  127. $this->drupalGet('admin/content/comment');
  128. $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was published.');
  129. // Delete comment.
  130. $this->performCommentOperation($anonymous_comment3, 'delete');
  131. $this->drupalGet('admin/content/comment');
  132. $this->assertNoRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was deleted.');
  133. $this->drupalLogout();
  134. // Comment 3 was deleted.
  135. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment3->id());
  136. $this->assertSession()->statusCodeEquals(403);
  137. // Reset.
  138. user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
  139. 'access comments' => FALSE,
  140. 'post comments' => FALSE,
  141. 'skip comment approval' => FALSE,
  142. ]);
  143. // Attempt to view comments while disallowed.
  144. // NOTE: if authenticated user has permission to post comments, then a
  145. // "Login or register to post comments" type link may be shown.
  146. $this->drupalGet('node/' . $this->node->id());
  147. $this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
  148. $this->assertSession()->linkNotExists('Add new comment', 'Link to add comment was found.');
  149. // Attempt to view node-comment form while disallowed.
  150. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
  151. $this->assertSession()->statusCodeEquals(403);
  152. user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
  153. 'access comments' => TRUE,
  154. 'post comments' => FALSE,
  155. 'skip comment approval' => FALSE,
  156. ]);
  157. $this->drupalGet('node/' . $this->node->id());
  158. // Verify that the comment field title is displayed.
  159. $this->assertPattern('@<h2[^>]*>Comments</h2>@');
  160. $this->assertSession()->linkExists('Log in', 1, 'Link to login was found.');
  161. $this->assertSession()->linkExists('register', 1, 'Link to register was found.');
  162. user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
  163. 'access comments' => FALSE,
  164. 'post comments' => TRUE,
  165. 'skip comment approval' => TRUE,
  166. ]);
  167. $this->drupalGet('node/' . $this->node->id());
  168. $this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
  169. $this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
  170. $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
  171. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id());
  172. $this->assertSession()->statusCodeEquals(403);
  173. }
  174. }