CommentPagerTest.php 18.4 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\comment\Tests;

5
use Drupal\comment\CommentManagerInterface;
6
use Drupal\Component\Render\FormattableMarkup;
7
use Drupal\node\Entity\Node;
8

9
/**
10 11 12
 * Tests paging of comments and their settings.
 *
 * @group comment
13 14
 */
class CommentPagerTest extends CommentTestBase {
15

16 17 18
  /**
   * Confirms comment paging works correctly with flat and threaded comments.
   */
19
  public function testCommentPaging() {
20
    $this->drupalLogin($this->adminUser);
21 22 23 24 25 26 27

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

    // Create a node and three comments.
28 29
    $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $comments = [];
30 31 32
    $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);
33

34
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
35 36 37 38 39 40 41

    // 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.
42
    $this->drupalGet('node/' . $node->id());
43 44 45 46
    $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.');
47 48

    // Check the second page.
49
    $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 1]]);
50 51 52
    $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.');
53 54

    // Check the third page.
55
    $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 2]]);
56 57 58
    $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.');
59 60 61

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

    $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.
68
    $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
69
    $this->assertFalse($this->commentExists($reply, TRUE), 'In flat mode, reply does not appear on page 1.');
70 71 72 73

    // 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.
74
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
75
    $this->drupalGet('node/' . $node->id(), ['query' => ['page' => 0]]);
76 77
    $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.');
78 79 80

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

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

91 92 93
    $this->drupalLogout();
  }

94 95 96
  /**
   * Confirms comment paging works correctly with flat and threaded comments.
   */
97
  public function testCommentPermalink() {
98 99 100 101 102 103 104 105
    $this->drupalLogin($this->adminUser);

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

    // Create a node and three comments.
106 107
    $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $comments = [];
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    $comments[] = $this->postComment($node, 'comment 1: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($node, 'comment 2: ' . $this->randomMachineName(), $this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($node, 'comment 3: ' . $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);

    // Navigate to each comment permalink as anonymous and assert it appears on
    // the page.
    foreach ($comments as $index => $comment) {
      $this->drupalGet($comment->toUrl());
      $this->assertTrue($this->commentExists($comment), sprintf('Comment %d appears on page %d.', $index + 1, $index + 1));
    }
  }

126 127 128
  /**
   * Tests comment ordering and threading.
   */
129
  public function testCommentOrderingThreading() {
130
    $this->drupalLogin($this->adminUser);
131 132 133 134 135 136 137 138 139 140

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

    // Display all the comments on the same page.
    $this->setCommentsPerPage(1000);

    // Create a node and three comments.
141 142
    $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $comments = [];
143 144 145
    $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);
146 147

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

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

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

    // Post a reply to the second comment.
160
    $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[3]->id());
161
    $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
162 163 164 165 166 167 168 169 170 171

    // At this point, the comment tree is:
    // - 0
    //   - 4
    // - 1
    //   - 3
    //     - 6
    // - 2
    //   - 5

172
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
173

174
    $expected_order = [
175 176 177 178 179 180 181
      0,
      1,
      2,
      3,
      4,
      5,
      6,
182
    ];
183
    $this->drupalGet('node/' . $node->id());
184 185
    $this->assertCommentOrder($comments, $expected_order);

186
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
187

188
    $expected_order = [
189 190 191 192 193 194 195
      0,
      4,
      1,
      3,
      6,
      2,
      5,
196
    ];
197
    $this->drupalGet('node/' . $node->id());
198 199 200 201 202 203
    $this->assertCommentOrder($comments, $expected_order);
  }

  /**
   * Asserts that the comments are displayed in the correct order.
   *
204 205 206
   * @param \Drupal\comment\CommentInterface[] $comments
   *   An array of comments, must be of the type CommentInterface.
   * @param array $expected_order
207 208
   *   An array of keys from $comments describing the expected order.
   */
209
  public function assertCommentOrder(array $comments, array $expected_order) {
210
    $expected_cids = [];
211 212 213

    // First, rekey the expected order by cid.
    foreach ($expected_order as $key) {
214
      $expected_cids[] = $comments[$key]->id();
215 216 217
    }

    $comment_anchors = $this->xpath('//a[starts-with(@id,"comment-")]');
218
    $result_order = [];
219 220 221
    foreach ($comment_anchors as $anchor) {
      $result_order[] = substr($anchor['id'], 8);
    }
222
    return $this->assertEqual($expected_cids, $result_order, format_string('Comment order: expected @expected, returned @returned.', ['@expected' => implode(',', $expected_cids), '@returned' => implode(',', $result_order)]));
223 224 225
  }

  /**
226
   * Tests calculation of first page with new comment.
227
   */
228
  public function testCommentNewPageIndicator() {
229
    $this->drupalLogin($this->adminUser);
230 231 232 233 234 235 236 237 238 239 240

    // 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.
241 242
    $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $comments = [];
243 244 245
    $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);
246 247

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

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

    // Post a reply to the last comment.
256
    $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id());
257
    $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
258 259 260 261 262 263 264 265 266

    // At this point, the comment tree is:
    // - 0
    //   - 4
    // - 1
    //   - 3
    // - 2
    //   - 5

267
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
268

269
    $expected_pages = [
270 271 272 273 274 275 276 277 278 279 280 281
      // 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,
282
    ];
283

284
    $node = Node::load($node->id());
285
    foreach ($expected_pages as $new_replies => $expected_page) {
286
      $returned_page = \Drupal::entityManager()->getStorage('comment')
287
        ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
288
      $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]));
289 290
    }

291
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
292

293
    $expected_pages = [
294 295 296 297 298 299 300 301 302 303 304 305
      // 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,
306
    ];
307

308
    \Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]);
309
    $node = Node::load($node->id());
310
    foreach ($expected_pages as $new_replies => $expected_page) {
311
      $returned_page = \Drupal::entityManager()->getStorage('comment')
312
        ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
313
      $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]));
314 315
    }
  }
316 317 318 319

  /**
   * Confirms comment paging works correctly with two pagers.
   */
320
  public function testTwoPagers() {
321
    // Add another field to article content-type.
322
    $this->addDefaultCommentField('node', 'article', 'comment_2');
323 324
    // Set default to display comment list with unique pager id.
    entity_get_display('node', 'article', 'default')
325
      ->setComponent('comment_2', [
326 327
        'label' => 'hidden',
        'type' => 'comment_default',
328
        'weight' => 30,
329
        'settings' => [
330
          'pager_id' => 1,
331
          'view_mode' => 'default',
332 333
        ]
      ])
334
      ->save();
335 336

    // Make sure pager appears in formatter summary and settings form.
337
    $account = $this->drupalCreateUser(['administer node display']);
338 339
    $this->drupalLogin($account);
    $this->drupalGet('admin/structure/types/manage/article/display');
340 341 342
    $this->assertNoText(t('Pager ID: @id', ['@id' => 0]), 'No summary for standard pager');
    $this->assertText(t('Pager ID: @id', ['@id' => 1]));
    $this->drupalPostAjaxForm(NULL, [], 'comment_settings_edit');
343
    // Change default pager to 2.
344 345
    $this->drupalPostForm(NULL, ['fields[comment][settings_edit_form][settings][pager_id]' => 2], t('Save'));
    $this->assertText(t('Pager ID: @id', ['@id' => 2]));
346
    // Revert the changes.
347 348 349
    $this->drupalPostAjaxForm(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');
350

351
    $this->drupalLogin($this->adminUser);
352

353
    // Add a new node with both comment fields open.
354
    $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
355
    // Set comment options.
356 357
    $comments = [];
    foreach (['comment', 'comment_2'] as $field_name) {
358 359
      $this->setCommentForm(TRUE, $field_name);
      $this->setCommentPreview(DRUPAL_OPTIONAL, $field_name);
360
      $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name);
361 362 363 364 365

      // 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++) {
366
        $comment = t('Comment @count on field @field', [
367 368
          '@count' => $i + 1,
          '@field' => $field_name,
369
        ]);
370 371 372 373 374 375 376 377 378 379 380
        $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.
381
    $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']);
382 383 384 385 386 387
    // 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.
388
    $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment_2']);
389 390 391 392
    // 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.
393
    $this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', [':label' => 'Comment 1 on field comment']);
394 395 396 397
    // Check only one pager updated.
    $this->assertRaw('Comment 2 on field comment');
    $this->assertRaw('Comment 2 on field comment_2');
  }
398

399 400 401 402 403 404 405 406 407 408
  /**
   * Follows a link found at a give xpath query.
   *
   * Will click the first link found with the given xpath query by default,
   * or a later one if an index is given.
   *
   * If the link is discovered and clicked, the test passes. Fail otherwise.
   *
   * @param string $xpath
   *   Xpath query that targets an anchor tag, or set of anchor tags.
409 410 411 412
   * @param array $arguments
   *   An array of arguments with keys in the form ':name' matching the
   *   placeholders in the query. The values may be either strings or numeric
   *   values.
413 414 415 416 417 418 419 420
   * @param int $index
   *   Link position counting from zero.
   *
   * @return string|false
   *   Page contents on success, or FALSE on failure.
   *
   * @see WebTestBase::clickLink()
   */
421
  protected function clickLinkWithXPath($xpath, $arguments = [], $index = 0) {
422
    $url_before = $this->getUrl();
423
    $urls = $this->xpath($xpath, $arguments);
424 425
    if (isset($urls[$index])) {
      $url_target = $this->getAbsoluteUrl($urls[$index]['href']);
426
      $this->pass(new FormattableMarkup('Clicked link %label (@url_target) from @url_before', ['%label' => $xpath, '@url_target' => $url_target, '@url_before' => $url_before]), 'Browser');
427 428
      return $this->drupalGet($url_target);
    }
429
    $this->fail(new FormattableMarkup('Link %label does not exist on @url_before', ['%label' => $xpath, '@url_before' => $url_before]), 'Browser');
430 431 432
    return FALSE;
  }

433
}