View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentManagerInterface;
class CommentThreadingTest extends CommentTestBase {
protected $defaultTheme = 'stark';
public function testCommentThreading() {
$this
->drupalLogin($this->adminUser);
$this
->setCommentPreview(DRUPAL_DISABLED);
$this
->setCommentForm(TRUE);
$this
->setCommentSubject(TRUE);
$this
->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this->node = $this
->drupalCreateNode([
'type' => 'article',
'promote' => 1,
'uid' => $this->webUser
->id(),
]);
$this
->drupalLogin($this->webUser);
$subject_text = $this
->randomMachineName();
$comment_text = $this
->randomMachineName();
$comment1 = $this
->postComment($this->node, $comment_text, $subject_text, TRUE);
$this
->assertTrue($this
->commentExists($comment1), 'Comment #1. Comment found.');
$this
->assertEquals('01/', $comment1
->getThread());
$this
->assertNoParentLink($comment1
->id());
$subject_text = $this
->randomMachineName();
$comment_text = $this
->randomMachineName();
$this
->postComment($this->node, $comment_text, $subject_text, TRUE);
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $comment1
->id());
$comment1_3 = $this
->postComment(NULL, $this
->randomMachineName(), '', TRUE);
$this
->assertTrue($this
->commentExists($comment1_3, TRUE), 'Comment #1_3. Reply found.');
$this
->assertEquals('01.00/', $comment1_3
->getThread());
$this
->assertParentLink($comment1_3
->id(), $comment1
->id());
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $comment1_3
->id());
$comment1_3_4 = $this
->postComment(NULL, $this
->randomMachineName(), $this
->randomMachineName(), TRUE);
$this
->assertTrue($this
->commentExists($comment1_3_4, TRUE), 'Comment #1_3_4. Second reply found.');
$this
->assertEquals('01.00.00/', $comment1_3_4
->getThread());
$this
->assertParentLink($comment1_3_4
->id(), $comment1_3
->id());
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $comment1
->id());
$comment1_5 = $this
->postComment(NULL, $this
->randomMachineName(), '', TRUE);
$this
->assertTrue($this
->commentExists($comment1_5), 'Comment #1_5. Third reply found.');
$this
->assertEquals('01.01/', $comment1_5
->getThread());
$this
->assertParentLink($comment1_5
->id(), $comment1
->id());
$this
->drupalLogin($this->webUser);
$subject_text = $this
->randomMachineName();
$comment_text = $this
->randomMachineName();
$comment5 = $this
->postComment($this->node, $comment_text, $subject_text, TRUE);
$this
->assertTrue($this
->commentExists($comment5), 'Comment #5. Second comment found.');
$this
->assertEquals('03/', $comment5
->getThread());
$this
->assertNoParentLink($comment5
->id());
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $comment5
->id());
$comment5_6 = $this
->postComment(NULL, $this
->randomMachineName(), '', TRUE);
$this
->assertTrue($this
->commentExists($comment5_6, TRUE), 'Comment #6. Reply found.');
$this
->assertEquals('03.00/', $comment5_6
->getThread());
$this
->assertParentLink($comment5_6
->id(), $comment5
->id());
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $comment5_6
->id());
$comment5_6_7 = $this
->postComment(NULL, $this
->randomMachineName(), $this
->randomMachineName(), TRUE);
$this
->assertTrue($this
->commentExists($comment5_6_7, TRUE), 'Comment #5_6_7. Second reply found.');
$this
->assertEquals('03.00.00/', $comment5_6_7
->getThread());
$this
->assertParentLink($comment5_6_7
->id(), $comment5_6
->id());
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $comment5
->id());
$comment5_8 = $this
->postComment(NULL, $this
->randomMachineName(), '', TRUE);
$this
->assertTrue($this
->commentExists($comment5_8), 'Comment #5_8. Third reply found.');
$this
->assertEquals('03.01/', $comment5_8
->getThread());
$this
->assertParentLink($comment5_8
->id(), $comment5
->id());
}
protected function assertParentLink(int $cid, int $pid) : void {
$pattern = "//article[@id='comment-{$cid}']//p/a[contains(@href, 'comment-{$pid}')]";
$this
->assertSession()
->elementExists('xpath', $pattern);
$pattern = "//article[@id='comment-{$cid}']";
$this
->assertSession()
->elementTextContains('xpath', $pattern, 'In reply to');
}
protected function assertNoParentLink(int $cid) : void {
$pattern = "//article[@id='comment-{$cid}']";
$this
->assertSession()
->elementTextNotContains('xpath', $pattern, 'In reply to');
}
}