3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\CommentManagerInterface;
8 * Tests to make sure the comment number increments properly.
12 class CommentThreadingTest extends CommentTestBase {
15 * Tests the comment threading.
17 public function testCommentThreading() {
18 // Set comments to have a subject with preview disabled.
19 $this->drupalLogin($this->adminUser);
20 $this->setCommentPreview(DRUPAL_DISABLED);
21 $this->setCommentForm(TRUE);
22 $this->setCommentSubject(TRUE);
23 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
24 $this->drupalLogout();
27 $this->drupalLogin($this->webUser);
28 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
31 $this->drupalLogin($this->webUser);
32 $subject_text = $this->randomMachineName();
33 $comment_text = $this->randomMachineName();
35 $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
36 // Confirm that the comment was created and has the correct threading.
37 $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
38 $this->assertEqual($comment1->getThread(), '01/');
39 // Confirm that there is no reference to a parent comment.
40 $this->assertNoParentLink($comment1->id());
42 // Post comment #2 following the comment #1 to test if it correctly jumps
43 // out the indentation in case there is a thread above.
44 $subject_text = $this->randomMachineName();
45 $comment_text = $this->randomMachineName();
46 $this->postComment($this->node, $comment_text, $subject_text, TRUE);
48 // Reply to comment #1 creating comment #1_3.
49 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
50 $comment1_3 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
52 // Confirm that the comment was created and has the correct threading.
53 $this->assertTrue($this->commentExists($comment1_3, TRUE), 'Comment #1_3. Reply found.');
54 $this->assertEqual($comment1_3->getThread(), '01.00/');
55 // Confirm that there is a link to the parent comment.
56 $this->assertParentLink($comment1_3->id(), $comment1->id());
58 // Reply to comment #1_3 creating comment #1_3_4.
59 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1_3->id());
60 $comment1_3_4 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
62 // Confirm that the comment was created and has the correct threading.
63 $this->assertTrue($this->commentExists($comment1_3_4, TRUE), 'Comment #1_3_4. Second reply found.');
64 $this->assertEqual($comment1_3_4->getThread(), '01.00.00/');
65 // Confirm that there is a link to the parent comment.
66 $this->assertParentLink($comment1_3_4->id(), $comment1_3->id());
68 // Reply to comment #1 creating comment #1_5.
69 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
71 $comment1_5 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
73 // Confirm that the comment was created and has the correct threading.
74 $this->assertTrue($this->commentExists($comment1_5), 'Comment #1_5. Third reply found.');
75 $this->assertEqual($comment1_5->getThread(), '01.01/');
76 // Confirm that there is a link to the parent comment.
77 $this->assertParentLink($comment1_5->id(), $comment1->id());
79 // Post comment #3 overall comment #5.
80 $this->drupalLogin($this->webUser);
81 $subject_text = $this->randomMachineName();
82 $comment_text = $this->randomMachineName();
84 $comment5 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
85 // Confirm that the comment was created and has the correct threading.
86 $this->assertTrue($this->commentExists($comment5), 'Comment #5. Second comment found.');
87 $this->assertEqual($comment5->getThread(), '03/');
88 // Confirm that there is no link to a parent comment.
89 $this->assertNoParentLink($comment5->id());
91 // Reply to comment #5 creating comment #5_6.
92 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
93 $comment5_6 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
95 // Confirm that the comment was created and has the correct threading.
96 $this->assertTrue($this->commentExists($comment5_6, TRUE), 'Comment #6. Reply found.');
97 $this->assertEqual($comment5_6->getThread(), '03.00/');
98 // Confirm that there is a link to the parent comment.
99 $this->assertParentLink($comment5_6->id(), $comment5->id());
101 // Reply to comment #5_6 creating comment #5_6_7.
102 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5_6->id());
103 $comment5_6_7 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
105 // Confirm that the comment was created and has the correct threading.
106 $this->assertTrue($this->commentExists($comment5_6_7, TRUE), 'Comment #5_6_7. Second reply found.');
107 $this->assertEqual($comment5_6_7->getThread(), '03.00.00/');
108 // Confirm that there is a link to the parent comment.
109 $this->assertParentLink($comment5_6_7->id(), $comment5_6->id());
111 // Reply to comment #5 creating comment #5_8.
112 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
113 $comment5_8 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
115 // Confirm that the comment was created and has the correct threading.
116 $this->assertTrue($this->commentExists($comment5_8), 'Comment #5_8. Third reply found.');
117 $this->assertEqual($comment5_8->getThread(), '03.01/');
118 // Confirm that there is a link to the parent comment.
119 $this->assertParentLink($comment5_8->id(), $comment5->id());
123 * Asserts that the link to the specified parent comment is present.
126 * The comment ID to check.
128 * The expected parent comment ID.
130 protected function assertParentLink($cid, $pid) {
131 // This pattern matches a markup structure like:
132 // <a id="comment-2"></a>
134 // <p class="parent">
135 // <a href="...comment-1"></a>
138 $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]//a[contains(@href, 'comment-$pid')]";
140 $this->assertFieldByXpath($pattern, NULL, format_string(
141 'Comment %cid has a link to parent %pid.',
150 * Asserts that the specified comment does not have a link to a parent.
153 * The comment ID to check.
155 protected function assertNoParentLink($cid) {
156 // This pattern matches a markup structure like:
157 // <a id="comment-2"></a>
159 // <p class="parent"></p>
162 $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]";
163 $this->assertNoFieldByXpath($pattern, NULL, format_string(
164 'Comment %cid does not have a link to a parent.',