3 namespace Drupal\comment\Tests;
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\node\Entity\Node;
10 * Tests paging of comments and their settings.
14 class CommentPagerTest extends CommentTestBase {
16 * Confirms comment paging works correctly with flat and threaded comments.
18 public function testCommentPaging() {
19 $this->drupalLogin($this->adminUser);
21 // Set comment variables.
22 $this->setCommentForm(TRUE);
23 $this->setCommentSubject(TRUE);
24 $this->setCommentPreview(DRUPAL_DISABLED);
26 // Create a node and three comments.
27 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
29 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
30 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
31 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
33 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
35 // Set comments to one per page so that we are able to test paging without
36 // needing to insert large numbers of comments.
37 $this->setCommentsPerPage(1);
39 // Check the first page of the node, and confirm the correct comments are
41 $this->drupalGet('node/' . $node->id());
42 $this->assertRaw(t('next'), 'Paging links found.');
43 $this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page 1.');
44 $this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 1.');
45 $this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 1.');
47 // Check the second page.
48 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 1]]);
49 $this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page 2.');
50 $this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 2.');
51 $this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 2.');
53 // Check the third page.
54 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 2]]);
55 $this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page 3.');
56 $this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 3.');
57 $this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 3.');
59 // Post a reply to the oldest comment and test again.
60 $oldest_comment = reset($comments);
61 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $oldest_comment->id());
62 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
64 $this->setCommentsPerPage(2);
65 // We are still in flat view - the replies should not be on the first page,
66 // even though they are replies to the oldest comment.
67 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
68 $this->assertFalse($this->commentExists($reply, TRUE), 'In flat mode, reply does not appear on page 1.');
70 // If we switch to threaded mode, the replies on the oldest comment
71 // should be bumped to the first page and comment 6 should be bumped
72 // to the second page.
73 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
74 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
75 $this->assertTrue($this->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.');
76 $this->assertFalse($this->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.');
78 // If (# replies > # comments per page) in threaded expanded view,
79 // the overage should be bumped.
80 $reply2 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
81 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
82 $this->assertFalse($this->commentExists($reply2, TRUE), 'In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.');
84 // Test that the page build process does not somehow generate errors when
85 // # comments per page is set to 0.
86 $this->setCommentsPerPage(0);
87 $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
88 $this->assertFalse($this->commentExists($reply2, TRUE), 'Threaded mode works correctly when comments per page is 0.');
90 $this->drupalLogout();
94 * Confirms comment paging works correctly with flat and threaded comments.
96 public function testCommentPermalink() {
97 $this->drupalLogin($this->adminUser);
99 // Set comment variables.
100 $this->setCommentForm(TRUE);
101 $this->setCommentSubject(TRUE);
102 $this->setCommentPreview(DRUPAL_DISABLED);
104 // Create a node and three comments.
105 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
107 $comments[] = $this->postComment($node, 'comment 1: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
108 $comments[] = $this->postComment($node, 'comment 2: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
109 $comments[] = $this->postComment($node, 'comment 3: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
111 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
113 // Set comments to one per page so that we are able to test paging without
114 // needing to insert large numbers of comments.
115 $this->setCommentsPerPage(1);
117 // Navigate to each comment permalink as anonymous and assert it appears on
119 foreach ($comments as $index => $comment) {
120 $this->drupalGet($comment->toUrl());
121 $this->assertTrue($this->commentExists($comment), sprintf('Comment %d appears on page %d.', $index + 1, $index + 1));
126 * Tests comment ordering and threading.
128 public function testCommentOrderingThreading() {
129 $this->drupalLogin($this->adminUser);
131 // Set comment variables.
132 $this->setCommentForm(TRUE);
133 $this->setCommentSubject(TRUE);
134 $this->setCommentPreview(DRUPAL_DISABLED);
136 // Display all the comments on the same page.
137 $this->setCommentsPerPage(1000);
139 // Create a node and three comments.
140 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
142 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
143 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
144 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
146 // Post a reply to the second comment.
147 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id());
148 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
150 // Post a reply to the first comment.
151 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id());
152 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
154 // Post a reply to the last comment.
155 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id());
156 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
158 // Post a reply to the second comment.
159 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[3]->id());
160 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
162 // At this point, the comment tree is:
171 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
182 $this->drupalGet('node/' . $node->id());
183 $this->assertCommentOrder($comments, $expected_order);
185 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
196 $this->drupalGet('node/' . $node->id());
197 $this->assertCommentOrder($comments, $expected_order);
201 * Asserts that the comments are displayed in the correct order.
203 * @param \Drupal\comment\CommentInterface[] $comments
204 * An array of comments, must be of the type CommentInterface.
205 * @param array $expected_order
206 * An array of keys from $comments describing the expected order.
208 public function assertCommentOrder(array $comments, array $expected_order) {
211 // First, rekey the expected order by cid.
212 foreach ($expected_order as $key) {
213 $expected_cids[] = $comments[$key]->id();
216 $comment_anchors = $this->xpath('//a[starts-with(@id,"comment-")]');
218 foreach ($comment_anchors as $anchor) {
219 $result_order[] = substr($anchor['id'], 8);
221 return $this->assertEqual($expected_cids, $result_order, format_string('Comment order: expected @expected, returned @returned.', ['@expected' => implode(',', $expected_cids), '@returned' => implode(',', $result_order)]));
225 * Tests calculation of first page with new comment.
227 public function testCommentNewPageIndicator() {
228 $this->drupalLogin($this->adminUser);
230 // Set comment variables.
231 $this->setCommentForm(TRUE);
232 $this->setCommentSubject(TRUE);
233 $this->setCommentPreview(DRUPAL_DISABLED);
235 // Set comments to one per page so that we are able to test paging without
236 // needing to insert large numbers of comments.
237 $this->setCommentsPerPage(1);
239 // Create a node and three comments.
240 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
242 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
243 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
244 $comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
246 // Post a reply to the second comment.
247 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id());
248 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
250 // Post a reply to the first comment.
251 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id());
252 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
254 // Post a reply to the last comment.
255 $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id());
256 $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
258 // At this point, the comment tree is:
266 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
283 $node = Node::load($node->id());
284 foreach ($expected_pages as $new_replies => $expected_page) {
285 $returned_page = \Drupal::entityManager()->getStorage('comment')
286 ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
287 $this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page]));
290 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
307 \Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]);
308 $node = Node::load($node->id());
309 foreach ($expected_pages as $new_replies => $expected_page) {
310 $returned_page = \Drupal::entityManager()->getStorage('comment')
311 ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
312 $this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page]));
317 * Confirms comment paging works correctly with two pagers.
319 public function testTwoPagers() {
320 // Add another field to article content-type.
321 $this->addDefaultCommentField('node', 'article', 'comment_2');
322 // Set default to display comment list with unique pager id.
323 entity_get_display('node', 'article', 'default')
324 ->setComponent('comment_2', [
326 'type' => 'comment_default',
330 'view_mode' => 'default',
335 // Make sure pager appears in formatter summary and settings form.
336 $account = $this->drupalCreateUser(['administer node display']);
337 $this->drupalLogin($account);
338 $this->drupalGet('admin/structure/types/manage/article/display');
339 $this->assertNoText(t('Pager ID: @id', ['@id' => 0]), 'No summary for standard pager');
340 $this->assertText(t('Pager ID: @id', ['@id' => 1]));
341 $this->drupalPostAjaxForm(NULL, [], 'comment_settings_edit');
342 // Change default pager to 2.
343 $this->drupalPostForm(NULL, ['fields[comment][settings_edit_form][settings][pager_id]' => 2], t('Save'));
344 $this->assertText(t('Pager ID: @id', ['@id' => 2]));
345 // Revert the changes.
346 $this->drupalPostAjaxForm(NULL, [], 'comment_settings_edit');
347 $this->drupalPostForm(NULL, ['fields[comment][settings_edit_form][settings][pager_id]' => 0], t('Save'));
348 $this->assertNoText(t('Pager ID: @id', ['@id' => 0]), 'No summary for standard pager');
350 $this->drupalLogin($this->adminUser);
352 // Add a new node with both comment fields open.
353 $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
354 // Set comment options.
356 foreach (['comment', 'comment_2'] as $field_name) {
357 $this->setCommentForm(TRUE, $field_name);
358 $this->setCommentPreview(DRUPAL_OPTIONAL, $field_name);
359 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name);
361 // Set comments to one per page so that we are able to test paging without
362 // needing to insert large numbers of comments.
363 $this->setCommentsPerPage(1, $field_name);
364 for ($i = 0; $i < 3; $i++) {
365 $comment = t('Comment @count on field @field', [
367 '@field' => $field_name,
369 $comments[] = $this->postComment($node, $comment, $comment, TRUE, $field_name);
373 // Check the first page of the node, and confirm the correct comments are
375 $this->drupalGet('node/' . $node->id());
376 $this->assertRaw(t('next'), 'Paging links found.');
377 $this->assertRaw('Comment 1 on field comment');
378 $this->assertRaw('Comment 1 on field comment_2');
379 // Navigate to next page of field 1.
380 $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']);
381 // Check only one pager updated.
382 $this->assertRaw('Comment 2 on field comment');
383 $this->assertRaw('Comment 1 on field comment_2');
385 $this->drupalGet('node/' . $node->id());
386 // Navigate to next page of field 2.
387 $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment_2']);
388 // Check only one pager updated.
389 $this->assertRaw('Comment 1 on field comment');
390 $this->assertRaw('Comment 2 on field comment_2');
391 // Navigate to next page of field 1.
392 $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']);
393 // Check only one pager updated.
394 $this->assertRaw('Comment 2 on field comment');
395 $this->assertRaw('Comment 2 on field comment_2');
399 * Follows a link found at a give xpath query.
401 * Will click the first link found with the given xpath query by default,
402 * or a later one if an index is given.
404 * If the link is discovered and clicked, the test passes. Fail otherwise.
406 * @param string $xpath
407 * Xpath query that targets an anchor tag, or set of anchor tags.
408 * @param array $arguments
409 * An array of arguments with keys in the form ':name' matching the
410 * placeholders in the query. The values may be either strings or numeric
413 * Link position counting from zero.
415 * @return string|false
416 * Page contents on success, or FALSE on failure.
418 * @see WebTestBase::clickLink()
420 protected function clickLinkWithXPath($xpath, $arguments = [], $index = 0) {
421 $url_before = $this->getUrl();
422 $urls = $this->xpath($xpath, $arguments);
423 if (isset($urls[$index])) {
424 $url_target = $this->getAbsoluteUrl($urls[$index]['href']);
425 $this->pass(SafeMarkup::format('Clicked link %label (@url_target) from @url_before', ['%label' => $xpath, '@url_target' => $url_target, '@url_before' => $url_before]), 'Browser');
426 return $this->drupalGet($url_target);
428 $this->fail(SafeMarkup::format('Link %label does not exist on @url_before', ['%label' => $xpath, '@url_before' => $url_before]), 'Browser');