function CommentPagerTest::testCommentOrderingThreading

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

Tests comment ordering and threading.

File

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

Class

CommentPagerTest
Tests paging of comments and their settings.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentOrderingThreading() : void {
    $this->drupalLogin($this->adminUser);
    // 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.
    $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);
    // Post a reply to the second comment.
    $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[3]->id());
    $comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
    // At this point, the comment tree is:
    // - 0
    //   - 4
    // - 1
    //   - 3
    //     - 6
    // - 2
    //   - 5
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
    $expected_order = [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
    ];
    $this->drupalGet('node/' . $node->id());
    $this->assertCommentOrder($comments, $expected_order);
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
    $expected_order = [
        0,
        4,
        1,
        3,
        6,
        2,
        5,
    ];
    $this->drupalGet('node/' . $node->id());
    $this->assertCommentOrder($comments, $expected_order);
}

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