function CommentPagerTest::testTwoPagers

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentPagerTest.php \Drupal\Tests\comment\Functional\CommentPagerTest::testTwoPagers()
  2. 8.9.x core/modules/comment/tests/src/Functional/CommentPagerTest.php \Drupal\Tests\comment\Functional\CommentPagerTest::testTwoPagers()
  3. 11.x core/modules/comment/tests/src/Functional/CommentPagerTest.php \Drupal\Tests\comment\Functional\CommentPagerTest::testTwoPagers()

Confirms comment paging works correctly with two pagers.

File

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

Class

CommentPagerTest
Tests paging of comments and their settings.

Namespace

Drupal\Tests\comment\Functional

Code

public function testTwoPagers() : void {
  // Add another field to article content-type.
  $this->addDefaultCommentField('node', 'article', 'comment_2');
  // Set default to display comment list with unique pager id.
  \Drupal::service('entity_display.repository')->getViewDisplay('node', 'article')
    ->setComponent('comment_2', [
    'label' => 'hidden',
    'type' => 'comment_default',
    'weight' => 30,
    'settings' => [
      'pager_id' => 1,
      'view_mode' => 'default',
    ],
  ])
    ->save();
  // Make sure pager appears in formatter summary and settings form.
  $account = $this->drupalCreateUser([
    'administer node display',
  ]);
  $this->drupalLogin($account);
  $this->drupalGet('admin/structure/types/manage/article/display');
  // No summary for standard pager.
  $this->assertSession()
    ->pageTextNotContains('Pager ID: 0');
  $this->assertSession()
    ->pageTextContains('Pager ID: 1');
  $this->submitForm([], 'comment_settings_edit');
  // Change default pager to 2.
  $this->submitForm([
    'fields[comment][settings_edit_form][settings][pager_id]' => 2,
  ], 'Save');
  $this->assertSession()
    ->pageTextContains('Pager ID: 2');
  // Revert the changes.
  $this->submitForm([], 'comment_settings_edit');
  $this->submitForm([
    'fields[comment][settings_edit_form][settings][pager_id]' => 0,
  ], 'Save');
  // No summary for standard pager.
  $this->assertSession()
    ->pageTextNotContains('Pager ID: 0');
  $this->drupalLogin($this->adminUser);
  // Add a new node with both comment fields open.
  $node = $this->drupalCreateNode([
    'type' => 'article',
    'promote' => 1,
    'uid' => $this->webUser
      ->id(),
  ]);
  // Set comment options.
  $comments = [];
  foreach ([
    'comment',
    'comment_2',
  ] as $field_name) {
    $this->setCommentForm(TRUE, $field_name);
    $this->setCommentPreview(DRUPAL_OPTIONAL, $field_name);
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name);
    // 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, $field_name);
    for ($i = 1; $i <= 4; $i++) {
      $comment = "Comment {$i} on field {$field_name}";
      $comments[] = $this->postComment($node, $comment, $comment, TRUE, $field_name);
    }
  }
  // Check the first page of the node, and confirm the correct comments are
  // shown.
  $this->drupalGet('node/' . $node->id());
  $this->assertSession()
    ->pageTextContains('next');
  $this->assertSession()
    ->pageTextContains('Comment 1 on field comment');
  $this->assertSession()
    ->pageTextContains('Comment 1 on field comment_2');
  // Navigate to next page of field 1.
  $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [
    ':label' => 'Comment 1 on field comment',
  ]);
  // Check only one pager updated.
  $this->assertSession()
    ->pageTextContains('Comment 2 on field comment');
  $this->assertSession()
    ->pageTextContains('Comment 1 on field comment_2');
  // Return to page 1.
  $this->drupalGet('node/' . $node->id());
  // Navigate to next page of field 2.
  $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [
    ':label' => 'Comment 1 on field comment_2',
  ]);
  // Check only one pager updated.
  $this->assertSession()
    ->pageTextContains('Comment 1 on field comment');
  $this->assertSession()
    ->pageTextContains('Comment 2 on field comment_2');
  // Navigate to next page of field 1.
  $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [
    ':label' => 'Comment 1 on field comment',
  ]);
  // Check only one pager updated.
  $this->assertSession()
    ->pageTextContains('Comment 2 on field comment');
  $this->assertSession()
    ->pageTextContains('Comment 2 on field comment_2');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.