You are here

public function CommentPagerTest::testTwoPagers 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::testTwoPagers()
  2. 10 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 325

Class

CommentPagerTest
Tests paging of comments and their settings.

Namespace

Drupal\Tests\comment\Functional

Code

public function testTwoPagers() {

  // 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');
  $this
    ->assertNoText(t('Pager ID: @id', [
    '@id' => 0,
  ]), 'No summary for standard pager');
  $this
    ->assertText(t('Pager ID: @id', [
    '@id' => 1,
  ]));
  $this
    ->drupalPostForm(NULL, [], 'comment_settings_edit');

  // Change default pager to 2.
  $this
    ->drupalPostForm(NULL, [
    'fields[comment][settings_edit_form][settings][pager_id]' => 2,
  ], t('Save'));
  $this
    ->assertText(t('Pager ID: @id', [
    '@id' => 2,
  ]));

  // Revert the changes.
  $this
    ->drupalPostForm(NULL, [], 'comment_settings_edit');
  $this
    ->drupalPostForm(NULL, [
    'fields[comment][settings_edit_form][settings][pager_id]' => 0,
  ], t('Save'));
  $this
    ->assertNoText(t('Pager ID: @id', [
    '@id' => 0,
  ]), 'No summary for standard pager');
  $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 = 0; $i < 3; $i++) {
      $comment = t('Comment @count on field @field', [
        '@count' => $i + 1,
        '@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
    ->assertRaw(t('next'), 'Paging links found.');
  $this
    ->assertRaw('Comment 1 on field comment');
  $this
    ->assertRaw('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
    ->assertRaw('Comment 2 on field comment');
  $this
    ->assertRaw('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
    ->assertRaw('Comment 1 on field comment');
  $this
    ->assertRaw('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
    ->assertRaw('Comment 2 on field comment');
  $this
    ->assertRaw('Comment 2 on field comment_2');
}