CommentInterfaceTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace Drupal\Tests\comment\Functional;
  3. use Drupal\Core\Url;
  4. use Drupal\comment\CommentManagerInterface;
  5. use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
  6. use Drupal\comment\Entity\Comment;
  7. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  8. use Drupal\Core\Entity\Entity\EntityViewMode;
  9. use Drupal\user\RoleInterface;
  10. use Drupal\filter\Entity\FilterFormat;
  11. /**
  12. * Tests comment user interfaces.
  13. *
  14. * @group comment
  15. */
  16. class CommentInterfaceTest extends CommentTestBase {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected $defaultTheme = 'classy';
  21. /**
  22. * Set up comments to have subject and preview disabled.
  23. */
  24. protected function setUp() {
  25. parent::setUp();
  26. $this->drupalLogin($this->adminUser);
  27. // Make sure that comment field title is not displayed when there's no
  28. // comments posted.
  29. $this->drupalGet($this->node->toUrl());
  30. $this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@', 'Comments title is not displayed.');
  31. // Set comments to have subject and preview disabled.
  32. $this->setCommentPreview(DRUPAL_DISABLED);
  33. $this->setCommentForm(TRUE);
  34. $this->setCommentSubject(FALSE);
  35. $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
  36. $this->drupalLogout();
  37. }
  38. /**
  39. * Tests the comment interface.
  40. */
  41. public function testCommentInterface() {
  42. // Post comment #1 without subject or preview.
  43. $this->drupalLogin($this->webUser);
  44. $comment_text = $this->randomMachineName();
  45. $comment = $this->postComment($this->node, $comment_text);
  46. $this->assertTrue($this->commentExists($comment), 'Comment found.');
  47. // Test the comment field title is displayed when there's comments.
  48. $this->drupalGet($this->node->toUrl());
  49. $this->assertPattern('@<h2[^>]*>Comments</h2>@');
  50. // Set comments to have subject and preview to required.
  51. $this->drupalLogout();
  52. $this->drupalLogin($this->adminUser);
  53. $this->setCommentSubject(TRUE);
  54. $this->setCommentPreview(DRUPAL_REQUIRED);
  55. $this->drupalLogout();
  56. // Create comment #2 that allows subject and requires preview.
  57. $this->drupalLogin($this->webUser);
  58. $subject_text = $this->randomMachineName();
  59. $comment_text = $this->randomMachineName();
  60. $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
  61. $this->assertTrue($this->commentExists($comment), 'Comment found.');
  62. // Comment as anonymous with preview required.
  63. $this->drupalLogout();
  64. user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content', 'access comments', 'post comments', 'skip comment approval']);
  65. $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
  66. $this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.');
  67. $anonymous_comment->delete();
  68. // Check comment display.
  69. $this->drupalLogin($this->webUser);
  70. $this->drupalGet('node/' . $this->node->id());
  71. $this->assertText($subject_text, 'Individual comment subject found.');
  72. $this->assertText($comment_text, 'Individual comment body found.');
  73. $arguments = [
  74. ':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(),
  75. ];
  76. $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]';
  77. $permalink = $this->xpath($pattern_permalink, $arguments);
  78. $this->assertTrue(!empty($permalink), 'Permalink link found.');
  79. // Set comments to have subject and preview to optional.
  80. $this->drupalLogout();
  81. $this->drupalLogin($this->adminUser);
  82. $this->setCommentSubject(TRUE);
  83. $this->setCommentPreview(DRUPAL_OPTIONAL);
  84. $this->drupalGet('comment/' . $comment->id() . '/edit');
  85. $this->assertTitle('Edit comment ' . $comment->getSubject() . ' | Drupal');
  86. // Test changing the comment author to "Anonymous".
  87. $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']);
  88. $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
  89. // Test changing the comment author to an unverified user.
  90. $random_name = $this->randomMachineName();
  91. $this->drupalGet('comment/' . $comment->id() . '/edit');
  92. $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['name' => $random_name]);
  93. $this->drupalGet('node/' . $this->node->id());
  94. $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
  95. // Test changing the comment author to a verified user.
  96. $this->drupalGet('comment/' . $comment->id() . '/edit');
  97. $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => $this->webUser->getAccountName() . ' (' . $this->webUser->id() . ')']);
  98. $this->assertTrue($comment->getAuthorName() == $this->webUser->getAccountName() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.');
  99. $this->drupalLogout();
  100. // Reply to comment #2 creating comment #3 with optional preview and no
  101. // subject though field enabled.
  102. $this->drupalLogin($this->webUser);
  103. // Deliberately use the wrong url to test
  104. // \Drupal\comment\Controller\CommentController::redirectNode().
  105. $this->drupalGet('comment/' . $this->node->id() . '/reply');
  106. // Verify we were correctly redirected.
  107. $this->assertUrl(Url::fromRoute('comment.reply', ['entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment'], ['absolute' => TRUE])->toString());
  108. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
  109. $this->assertText($subject_text, 'Individual comment-reply subject found.');
  110. $this->assertText($comment_text, 'Individual comment-reply body found.');
  111. $reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
  112. $reply_loaded = Comment::load($reply->id());
  113. $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
  114. $this->assertEqual($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.');
  115. // Check the thread of reply grows correctly.
  116. $this->assertEqual(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread());
  117. // Second reply to comment #2 creating comment #4.
  118. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
  119. $this->assertText($comment->getSubject(), 'Individual comment-reply subject found.');
  120. $this->assertText($comment->comment_body->value, 'Individual comment-reply body found.');
  121. $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
  122. $reply_loaded = Comment::load($reply->id());
  123. $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
  124. // Check the thread of second reply grows correctly.
  125. $this->assertEqual(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread());
  126. // Reply to comment #4 creating comment #5.
  127. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
  128. $this->assertText($reply_loaded->getSubject(), 'Individual comment-reply subject found.');
  129. $this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.');
  130. $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
  131. $reply_loaded = Comment::load($reply->id());
  132. $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
  133. // Check the thread of reply to second reply grows correctly.
  134. $this->assertEqual(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread());
  135. // Edit reply.
  136. $this->drupalGet('comment/' . $reply->id() . '/edit');
  137. $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
  138. $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
  139. // Confirm a new comment is posted to the correct page.
  140. $this->setCommentsPerPage(2);
  141. $comment_new_page = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
  142. $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
  143. $this->drupalGet('node/' . $this->node->id(), ['query' => ['page' => 2]]);
  144. $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
  145. $this->setCommentsPerPage(50);
  146. // Attempt to reply to an unpublished comment.
  147. $reply_loaded->setUnpublished();
  148. $reply_loaded->save();
  149. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
  150. $this->assertSession()->statusCodeEquals(403);
  151. // Attempt to post to node with comments disabled.
  152. $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::HIDDEN]]]);
  153. $this->assertNotNull($this->node, 'Article node created.');
  154. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
  155. $this->assertSession()->statusCodeEquals(403);
  156. $this->assertNoField('edit-comment', 'Comment body field found.');
  157. // Attempt to post to node with read-only comments.
  158. $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::CLOSED]]]);
  159. $this->assertNotNull($this->node, 'Article node created.');
  160. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
  161. $this->assertSession()->statusCodeEquals(403);
  162. $this->assertNoField('edit-comment', 'Comment body field found.');
  163. // Attempt to post to node with comments enabled (check field names etc).
  164. $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::OPEN]]]);
  165. $this->assertNotNull($this->node, 'Article node created.');
  166. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
  167. $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
  168. $this->assertField('edit-comment-body-0-value', 'Comment body field found.');
  169. // Delete comment and make sure that reply is also removed.
  170. $this->drupalLogout();
  171. $this->drupalLogin($this->adminUser);
  172. $this->deleteComment($comment);
  173. $this->deleteComment($comment_new_page);
  174. $this->drupalGet('node/' . $this->node->id());
  175. $this->assertFalse($this->commentExists($comment), 'Comment not found.');
  176. $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
  177. // Enabled comment form on node page.
  178. $this->drupalLogin($this->adminUser);
  179. $this->setCommentForm(TRUE);
  180. $this->drupalLogout();
  181. // Submit comment through node form.
  182. $this->drupalLogin($this->webUser);
  183. $this->drupalGet('node/' . $this->node->id());
  184. $form_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
  185. $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
  186. // Disable comment form on node page.
  187. $this->drupalLogout();
  188. $this->drupalLogin($this->adminUser);
  189. $this->setCommentForm(FALSE);
  190. }
  191. /**
  192. * Test that the subject is automatically filled if disabled or left blank.
  193. *
  194. * When the subject field is blank or disabled, the first 29 characters of the
  195. * comment body are used for the subject. If this would break within a word,
  196. * then the break is put at the previous word boundary instead.
  197. */
  198. public function testAutoFilledSubject() {
  199. $this->drupalLogin($this->webUser);
  200. $this->drupalGet('node/' . $this->node->id());
  201. // Break when there is a word boundary before 29 characters.
  202. $body_text = 'Lorem ipsum Lorem ipsum Loreming ipsum Lorem ipsum';
  203. $comment1 = $this->postComment(NULL, $body_text, '', TRUE);
  204. $this->assertTrue($this->commentExists($comment1), 'Form comment found.');
  205. $this->assertEqual('Lorem ipsum Lorem ipsum…', $comment1->getSubject());
  206. // Break at 29 characters where there's no boundary before that.
  207. $body_text2 = 'LoremipsumloremipsumLoremingipsumLoremipsum';
  208. $comment2 = $this->postComment(NULL, $body_text2, '', TRUE);
  209. $this->assertEqual('LoremipsumloremipsumLoreming…', $comment2->getSubject());
  210. }
  211. /**
  212. * Test that automatic subject is correctly created from HTML comment text.
  213. *
  214. * This is the same test as in CommentInterfaceTest::testAutoFilledSubject()
  215. * with the additional check that HTML is stripped appropriately prior to
  216. * character-counting.
  217. */
  218. public function testAutoFilledHtmlSubject() {
  219. // Set up two default (i.e. filtered HTML) input formats, because then we
  220. // can select one of them. Then create a user that can use these formats,
  221. // log the user in, and then GET the node page on which to test the
  222. // comments.
  223. $filtered_html_format = FilterFormat::create([
  224. 'format' => 'filtered_html',
  225. 'name' => 'Filtered HTML',
  226. ]);
  227. $filtered_html_format->save();
  228. $full_html_format = FilterFormat::create([
  229. 'format' => 'full_html',
  230. 'name' => 'Full HTML',
  231. ]);
  232. $full_html_format->save();
  233. $html_user = $this->drupalCreateUser([
  234. 'access comments',
  235. 'post comments',
  236. 'edit own comments',
  237. 'skip comment approval',
  238. 'access content',
  239. $filtered_html_format->getPermissionName(),
  240. $full_html_format->getPermissionName(),
  241. ]);
  242. $this->drupalLogin($html_user);
  243. $this->drupalGet('node/' . $this->node->id());
  244. // HTML should not be included in the character count.
  245. $body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />';
  246. $edit1 = [
  247. 'comment_body[0][value]' => $body_text1,
  248. 'comment_body[0][format]' => 'filtered_html',
  249. ];
  250. $this->drupalPostForm(NULL, $edit1, t('Save'));
  251. $this->assertEqual('Hello World', Comment::load(1)->getSubject());
  252. // If there's nothing other than HTML, the subject should be '(No subject)'.
  253. $body_text2 = '<span></span><strong> </strong><span> </span><strong></strong> <br />';
  254. $edit2 = [
  255. 'comment_body[0][value]' => $body_text2,
  256. 'comment_body[0][format]' => 'filtered_html',
  257. ];
  258. $this->drupalPostForm(NULL, $edit2, t('Save'));
  259. $this->assertEqual('(No subject)', Comment::load(2)->getSubject());
  260. }
  261. /**
  262. * Tests the comment formatter configured with a custom comment view mode.
  263. */
  264. public function testViewMode() {
  265. $this->drupalLogin($this->webUser);
  266. $this->drupalGet($this->node->toUrl());
  267. $comment_text = $this->randomMachineName();
  268. // Post a comment.
  269. $this->postComment($this->node, $comment_text);
  270. // Comment displayed in 'default' display mode found and has body text.
  271. $comment_element = $this->cssSelect('.comment-wrapper');
  272. $this->assertTrue(!empty($comment_element));
  273. $this->assertRaw('<p>' . $comment_text . '</p>');
  274. // Create a new comment entity view mode.
  275. $mode = mb_strtolower($this->randomMachineName());
  276. EntityViewMode::create([
  277. 'targetEntityType' => 'comment',
  278. 'id' => "comment.$mode",
  279. ])->save();
  280. // Create the corresponding entity view display for article node-type. Note
  281. // that this new view display mode doesn't contain the comment body.
  282. EntityViewDisplay::create([
  283. 'targetEntityType' => 'comment',
  284. 'bundle' => 'comment',
  285. 'mode' => $mode,
  286. ])->setStatus(TRUE)->save();
  287. /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */
  288. $node_display = EntityViewDisplay::load('node.article.default');
  289. $formatter = $node_display->getComponent('comment');
  290. // Change the node comment field formatter to use $mode mode instead of
  291. // 'default' mode.
  292. $formatter['settings']['view_mode'] = $mode;
  293. $node_display
  294. ->setComponent('comment', $formatter)
  295. ->save();
  296. // Reloading the node page to show the same node with its same comment but
  297. // with a different display mode.
  298. $this->drupalGet($this->node->toUrl());
  299. // The comment should exist but without the body text because we used $mode
  300. // mode this time.
  301. $comment_element = $this->cssSelect('.comment-wrapper');
  302. $this->assertTrue(!empty($comment_element));
  303. $this->assertNoRaw('<p>' . $comment_text . '</p>');
  304. }
  305. }