You are here

public function CommentPagerTest::testCommentNewPageIndicator in Drupal 9

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

Tests calculation of first page with new comment.

File

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

Class

CommentPagerTest
Tests paging of comments and their settings.

Namespace

Drupal\Tests\comment\Functional

Code

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

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

  // 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);

  // 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);

  // Post a reply to the second comment.
  $this
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment/' . $comments[1]
    ->id());
  $comments[] = $this
    ->postComment(NULL, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);

  // Post a reply to the first comment.
  $this
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment/' . $comments[0]
    ->id());
  $comments[] = $this
    ->postComment(NULL, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);

  // Post a reply to the last comment.
  $this
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment/' . $comments[2]
    ->id());
  $comments[] = $this
    ->postComment(NULL, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);

  // At this point, the comment tree is:
  // - 0
  //   - 4
  // - 1
  //   - 3
  // - 2
  //   - 5
  $this
    ->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
  $expected_pages = [
    // Page of comment 5
    1 => 5,
    // Page of comment 4
    2 => 4,
    // Page of comment 3
    3 => 3,
    // Page of comment 2
    4 => 2,
    // Page of comment 1
    5 => 1,
    // Page of comment 0
    6 => 0,
  ];
  $node = Node::load($node
    ->id());
  foreach ($expected_pages as $new_replies => $expected_page) {
    $returned_page = \Drupal::entityTypeManager()
      ->getStorage('comment')
      ->getNewCommentPageNumber($node
      ->get('comment')->comment_count, $new_replies, $node, 'comment');
    $this
      ->assertSame($expected_page, $returned_page, new FormattableMarkup('Flat mode, @new replies: expected page @expected, returned page @returned.', [
      '@new' => $new_replies,
      '@expected' => $expected_page,
      '@returned' => $returned_page,
    ]));
  }
  $this
    ->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
  $expected_pages = [
    // Page of comment 5
    1 => 5,
    // Page of comment 4
    2 => 1,
    // Page of comment 4
    3 => 1,
    // Page of comment 4
    4 => 1,
    // Page of comment 4
    5 => 1,
    // Page of comment 0
    6 => 0,
  ];
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = Node::load($node
    ->id());
  foreach ($expected_pages as $new_replies => $expected_page) {
    $returned_page = \Drupal::entityTypeManager()
      ->getStorage('comment')
      ->getNewCommentPageNumber($node
      ->get('comment')->comment_count, $new_replies, $node, 'comment');
    $this
      ->assertEquals($expected_page, $returned_page, new FormattableMarkup('Threaded mode, @new replies: expected page @expected, returned page @returned.', [
      '@new' => $new_replies,
      '@expected' => $expected_page,
      '@returned' => $returned_page,
    ]));
  }
}