You are here

public function CommentPagerTest::testCommentPaging in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentPagerTest.php \Drupal\Tests\comment\Functional\CommentPagerTest::testCommentPaging()

Confirms comment paging works correctly with flat and threaded comments.

File

core/modules/comment/tests/src/Functional/CommentPagerTest.php, line 24

Class

CommentPagerTest
Tests paging of comments and their settings.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentPaging() {
  $this
    ->drupalLogin($this->adminUser);

  // Set comment variables.
  $this
    ->setCommentForm(TRUE);
  $this
    ->setCommentSubject(TRUE);
  $this
    ->setCommentPreview(DRUPAL_DISABLED);

  // Create a node and three comments.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'promote' => 1,
  ]);
  $comments = [];
  $comments[] = $this
    ->postComment($node, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);
  $comments[] = $this
    ->postComment($node, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);
  $comments[] = $this
    ->postComment($node, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);
  $this
    ->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');

  // Set comments to one per page so that we are able to test paging without
  // needing to insert large numbers of comments.
  $this
    ->setCommentsPerPage(1);

  // Check the first page of the node, and confirm the correct comments are
  // shown.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertRaw(t('next'), 'Paging links found.');
  $this
    ->assertTrue($this
    ->commentExists($comments[0]), 'Comment 1 appears on page 1.');
  $this
    ->assertFalse($this
    ->commentExists($comments[1]), 'Comment 2 does not appear on page 1.');
  $this
    ->assertFalse($this
    ->commentExists($comments[2]), 'Comment 3 does not appear on page 1.');

  // Check the second page.
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'query' => [
      'page' => 1,
    ],
  ]);
  $this
    ->assertTrue($this
    ->commentExists($comments[1]), 'Comment 2 appears on page 2.');
  $this
    ->assertFalse($this
    ->commentExists($comments[0]), 'Comment 1 does not appear on page 2.');
  $this
    ->assertFalse($this
    ->commentExists($comments[2]), 'Comment 3 does not appear on page 2.');

  // Check the third page.
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'query' => [
      'page' => 2,
    ],
  ]);
  $this
    ->assertTrue($this
    ->commentExists($comments[2]), 'Comment 3 appears on page 3.');
  $this
    ->assertFalse($this
    ->commentExists($comments[0]), 'Comment 1 does not appear on page 3.');
  $this
    ->assertFalse($this
    ->commentExists($comments[1]), 'Comment 2 does not appear on page 3.');

  // Post a reply to the oldest comment and test again.
  $oldest_comment = reset($comments);
  $this
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment/' . $oldest_comment
    ->id());
  $reply = $this
    ->postComment(NULL, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);
  $this
    ->setCommentsPerPage(2);

  // We are still in flat view - the replies should not be on the first page,
  // even though they are replies to the oldest comment.
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'query' => [
      'page' => 0,
    ],
  ]);
  $this
    ->assertFalse($this
    ->commentExists($reply, TRUE), 'In flat mode, reply does not appear on page 1.');

  // If we switch to threaded mode, the replies on the oldest comment
  // should be bumped to the first page and comment 6 should be bumped
  // to the second page.
  $this
    ->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'query' => [
      'page' => 0,
    ],
  ]);
  $this
    ->assertTrue($this
    ->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.');
  $this
    ->assertFalse($this
    ->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.');

  // If (# replies > # comments per page) in threaded expanded view,
  // the overage should be bumped.
  $reply2 = $this
    ->postComment(NULL, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'query' => [
      'page' => 0,
    ],
  ]);
  $this
    ->assertFalse($this
    ->commentExists($reply2, TRUE), 'In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.');

  // Test that the page build process does not somehow generate errors when
  // # comments per page is set to 0.
  $this
    ->setCommentsPerPage(0);
  $this
    ->drupalGet('node/' . $node
    ->id(), [
    'query' => [
      'page' => 0,
    ],
  ]);
  $this
    ->assertFalse($this
    ->commentExists($reply2, TRUE), 'Threaded mode works correctly when comments per page is 0.');
  $this
    ->drupalLogout();
}