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 {
14 * Tests the comment threading.
16 public function testCommentThreading() {
17 // Set comments to have a subject with preview disabled.
18 $this->drupalLogin($this->adminUser);
19 $this->setCommentPreview(DRUPAL_DISABLED);
20 $this->setCommentForm(TRUE);
21 $this->setCommentSubject(TRUE);
22 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
23 $this->drupalLogout();
26 $this->drupalLogin($this->webUser);
27 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
30 $this->drupalLogin($this->webUser);
31 $subject_text = $this->randomMachineName();
32 $comment_text = $this->randomMachineName();
34 $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
35 // Confirm that the comment was created and has the correct threading.
36 $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
37 $this->assertEqual($comment1->getThread(), '01/');
38 // Confirm that there is no reference to a parent comment.
39 $this->assertNoParentLink($comment1->id());
41 // Post comment #2 following the comment #1 to test if it correctly jumps
42 // out the indentation in case there is a thread above.
43 $subject_text = $this->randomMachineName();
44 $comment_text = $this->randomMachineName();
45 $this->postComment($this->node, $comment_text, $subject_text, TRUE);
47 // Reply to comment #1 creating comment #1_3.
48 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
49 $comment1_3 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
51 // Confirm that the comment was created and has the correct threading.
52 $this->assertTrue($this->commentExists($comment1_3, TRUE), 'Comment #1_3. Reply found.');
53 $this->assertEqual($comment1_3->getThread(), '01.00/');
54 // Confirm that there is a link to the parent comment.
55 $this->assertParentLink($comment1_3->id(), $comment1->id());
57 // Reply to comment #1_3 creating comment #1_3_4.
58 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1_3->id());
59 $comment1_3_4 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
61 // Confirm that the comment was created and has the correct threading.
62 $this->assertTrue($this->commentExists($comment1_3_4, TRUE), 'Comment #1_3_4. Second reply found.');
63 $this->assertEqual($comment1_3_4->getThread(), '01.00.00/');
64 // Confirm that there is a link to the parent comment.
65 $this->assertParentLink($comment1_3_4->id(), $comment1_3->id());
67 // Reply to comment #1 creating comment #1_5.
68 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
70 $comment1_5 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
72 // Confirm that the comment was created and has the correct threading.
73 $this->assertTrue($this->commentExists($comment1_5), 'Comment #1_5. Third reply found.');
74 $this->assertEqual($comment1_5->getThread(), '01.01/');
75 // Confirm that there is a link to the parent comment.
76 $this->assertParentLink($comment1_5->id(), $comment1->id());
78 // Post comment #3 overall comment #5.
79 $this->drupalLogin($this->webUser);
80 $subject_text = $this->randomMachineName();
81 $comment_text = $this->randomMachineName();
83 $comment5 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
84 // Confirm that the comment was created and has the correct threading.
85 $this->assertTrue($this->commentExists($comment5), 'Comment #5. Second comment found.');
86 $this->assertEqual($comment5->getThread(), '03/');
87 // Confirm that there is no link to a parent comment.
88 $this->assertNoParentLink($comment5->id());
90 // Reply to comment #5 creating comment #5_6.
91 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
92 $comment5_6 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
94 // Confirm that the comment was created and has the correct threading.
95 $this->assertTrue($this->commentExists($comment5_6, TRUE), 'Comment #6. Reply found.');
96 $this->assertEqual($comment5_6->getThread(), '03.00/');
97 // Confirm that there is a link to the parent comment.
98 $this->assertParentLink($comment5_6->id(), $comment5->id());
100 // Reply to comment #5_6 creating comment #5_6_7.
101 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5_6->id());
102 $comment5_6_7 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
104 // Confirm that the comment was created and has the correct threading.
105 $this->assertTrue($this->commentExists($comment5_6_7, TRUE), 'Comment #5_6_7. Second reply found.');
106 $this->assertEqual($comment5_6_7->getThread(), '03.00.00/');
107 // Confirm that there is a link to the parent comment.
108 $this->assertParentLink($comment5_6_7->id(), $comment5_6->id());
110 // Reply to comment #5 creating comment #5_8.
111 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
112 $comment5_8 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
114 // Confirm that the comment was created and has the correct threading.
115 $this->assertTrue($this->commentExists($comment5_8), 'Comment #5_8. Third reply found.');
116 $this->assertEqual($comment5_8->getThread(), '03.01/');
117 // Confirm that there is a link to the parent comment.
118 $this->assertParentLink($comment5_8->id(), $comment5->id());
122 * Asserts that the link to the specified parent comment is present.
125 * The comment ID to check.
127 * The expected parent comment ID.
129 protected function assertParentLink($cid, $pid) {
130 // This pattern matches a markup structure like:
131 // <a id="comment-2"></a>
133 // <p class="parent">
134 // <a href="...comment-1"></a>
137 $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]//a[contains(@href, 'comment-$pid')]";
139 $this->assertFieldByXpath($pattern, NULL, format_string(
140 'Comment %cid has a link to parent %pid.',
149 * Asserts that the specified comment does not have a link to a parent.
152 * The comment ID to check.
154 protected function assertNoParentLink($cid) {
155 // This pattern matches a markup structure like:
156 // <a id="comment-2"></a>
158 // <p class="parent"></p>
161 $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]";
162 $this->assertNoFieldByXpath($pattern, NULL, format_string(
163 'Comment %cid does not have a link to a parent.',