Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\comment\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\CommentManagerInterface;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Tests to make sure the comment number increments properly.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @group comment
|
Chris@0
|
11 */
|
Chris@0
|
12 class CommentThreadingTest extends CommentTestBase {
|
Chris@17
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Tests the comment threading.
|
Chris@0
|
16 */
|
Chris@0
|
17 public function testCommentThreading() {
|
Chris@0
|
18 // Set comments to have a subject with preview disabled.
|
Chris@0
|
19 $this->drupalLogin($this->adminUser);
|
Chris@0
|
20 $this->setCommentPreview(DRUPAL_DISABLED);
|
Chris@0
|
21 $this->setCommentForm(TRUE);
|
Chris@0
|
22 $this->setCommentSubject(TRUE);
|
Chris@0
|
23 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
Chris@0
|
24 $this->drupalLogout();
|
Chris@0
|
25
|
Chris@0
|
26 // Create a node.
|
Chris@0
|
27 $this->drupalLogin($this->webUser);
|
Chris@0
|
28 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
|
Chris@0
|
29
|
Chris@0
|
30 // Post comment #1.
|
Chris@0
|
31 $this->drupalLogin($this->webUser);
|
Chris@0
|
32 $subject_text = $this->randomMachineName();
|
Chris@0
|
33 $comment_text = $this->randomMachineName();
|
Chris@0
|
34
|
Chris@0
|
35 $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
Chris@0
|
36 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
37 $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
|
Chris@0
|
38 $this->assertEqual($comment1->getThread(), '01/');
|
Chris@0
|
39 // Confirm that there is no reference to a parent comment.
|
Chris@0
|
40 $this->assertNoParentLink($comment1->id());
|
Chris@0
|
41
|
Chris@0
|
42 // Post comment #2 following the comment #1 to test if it correctly jumps
|
Chris@0
|
43 // out the indentation in case there is a thread above.
|
Chris@0
|
44 $subject_text = $this->randomMachineName();
|
Chris@0
|
45 $comment_text = $this->randomMachineName();
|
Chris@0
|
46 $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
Chris@0
|
47
|
Chris@0
|
48 // Reply to comment #1 creating comment #1_3.
|
Chris@0
|
49 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
|
Chris@0
|
50 $comment1_3 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
Chris@0
|
51
|
Chris@0
|
52 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
53 $this->assertTrue($this->commentExists($comment1_3, TRUE), 'Comment #1_3. Reply found.');
|
Chris@0
|
54 $this->assertEqual($comment1_3->getThread(), '01.00/');
|
Chris@0
|
55 // Confirm that there is a link to the parent comment.
|
Chris@0
|
56 $this->assertParentLink($comment1_3->id(), $comment1->id());
|
Chris@0
|
57
|
Chris@0
|
58 // Reply to comment #1_3 creating comment #1_3_4.
|
Chris@0
|
59 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1_3->id());
|
Chris@0
|
60 $comment1_3_4 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
Chris@0
|
61
|
Chris@0
|
62 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
63 $this->assertTrue($this->commentExists($comment1_3_4, TRUE), 'Comment #1_3_4. Second reply found.');
|
Chris@0
|
64 $this->assertEqual($comment1_3_4->getThread(), '01.00.00/');
|
Chris@0
|
65 // Confirm that there is a link to the parent comment.
|
Chris@0
|
66 $this->assertParentLink($comment1_3_4->id(), $comment1_3->id());
|
Chris@0
|
67
|
Chris@0
|
68 // Reply to comment #1 creating comment #1_5.
|
Chris@0
|
69 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
|
Chris@0
|
70
|
Chris@0
|
71 $comment1_5 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
Chris@0
|
72
|
Chris@0
|
73 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
74 $this->assertTrue($this->commentExists($comment1_5), 'Comment #1_5. Third reply found.');
|
Chris@0
|
75 $this->assertEqual($comment1_5->getThread(), '01.01/');
|
Chris@0
|
76 // Confirm that there is a link to the parent comment.
|
Chris@0
|
77 $this->assertParentLink($comment1_5->id(), $comment1->id());
|
Chris@0
|
78
|
Chris@0
|
79 // Post comment #3 overall comment #5.
|
Chris@0
|
80 $this->drupalLogin($this->webUser);
|
Chris@0
|
81 $subject_text = $this->randomMachineName();
|
Chris@0
|
82 $comment_text = $this->randomMachineName();
|
Chris@0
|
83
|
Chris@0
|
84 $comment5 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
Chris@0
|
85 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
86 $this->assertTrue($this->commentExists($comment5), 'Comment #5. Second comment found.');
|
Chris@0
|
87 $this->assertEqual($comment5->getThread(), '03/');
|
Chris@0
|
88 // Confirm that there is no link to a parent comment.
|
Chris@0
|
89 $this->assertNoParentLink($comment5->id());
|
Chris@0
|
90
|
Chris@0
|
91 // Reply to comment #5 creating comment #5_6.
|
Chris@0
|
92 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
|
Chris@0
|
93 $comment5_6 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
Chris@0
|
94
|
Chris@0
|
95 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
96 $this->assertTrue($this->commentExists($comment5_6, TRUE), 'Comment #6. Reply found.');
|
Chris@0
|
97 $this->assertEqual($comment5_6->getThread(), '03.00/');
|
Chris@0
|
98 // Confirm that there is a link to the parent comment.
|
Chris@0
|
99 $this->assertParentLink($comment5_6->id(), $comment5->id());
|
Chris@0
|
100
|
Chris@0
|
101 // Reply to comment #5_6 creating comment #5_6_7.
|
Chris@0
|
102 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5_6->id());
|
Chris@0
|
103 $comment5_6_7 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
Chris@0
|
104
|
Chris@0
|
105 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
106 $this->assertTrue($this->commentExists($comment5_6_7, TRUE), 'Comment #5_6_7. Second reply found.');
|
Chris@0
|
107 $this->assertEqual($comment5_6_7->getThread(), '03.00.00/');
|
Chris@0
|
108 // Confirm that there is a link to the parent comment.
|
Chris@0
|
109 $this->assertParentLink($comment5_6_7->id(), $comment5_6->id());
|
Chris@0
|
110
|
Chris@0
|
111 // Reply to comment #5 creating comment #5_8.
|
Chris@0
|
112 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
|
Chris@0
|
113 $comment5_8 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
Chris@0
|
114
|
Chris@0
|
115 // Confirm that the comment was created and has the correct threading.
|
Chris@0
|
116 $this->assertTrue($this->commentExists($comment5_8), 'Comment #5_8. Third reply found.');
|
Chris@0
|
117 $this->assertEqual($comment5_8->getThread(), '03.01/');
|
Chris@0
|
118 // Confirm that there is a link to the parent comment.
|
Chris@0
|
119 $this->assertParentLink($comment5_8->id(), $comment5->id());
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * Asserts that the link to the specified parent comment is present.
|
Chris@0
|
124 *
|
Chris@0
|
125 * @param int $cid
|
Chris@0
|
126 * The comment ID to check.
|
Chris@0
|
127 * @param int $pid
|
Chris@0
|
128 * The expected parent comment ID.
|
Chris@0
|
129 */
|
Chris@0
|
130 protected function assertParentLink($cid, $pid) {
|
Chris@0
|
131 // This pattern matches a markup structure like:
|
Chris@0
|
132 // <a id="comment-2"></a>
|
Chris@0
|
133 // <article>
|
Chris@0
|
134 // <p class="parent">
|
Chris@0
|
135 // <a href="...comment-1"></a>
|
Chris@0
|
136 // </p>
|
Chris@0
|
137 // </article>
|
Chris@18
|
138 $pattern = "//article[@id='comment-$cid']//p[contains(@class, 'parent')]//a[contains(@href, 'comment-$pid')]";
|
Chris@0
|
139
|
Chris@0
|
140 $this->assertFieldByXpath($pattern, NULL, format_string(
|
Chris@0
|
141 'Comment %cid has a link to parent %pid.',
|
Chris@0
|
142 [
|
Chris@0
|
143 '%cid' => $cid,
|
Chris@0
|
144 '%pid' => $pid,
|
Chris@0
|
145 ]
|
Chris@0
|
146 ));
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 /**
|
Chris@0
|
150 * Asserts that the specified comment does not have a link to a parent.
|
Chris@0
|
151 *
|
Chris@0
|
152 * @param int $cid
|
Chris@0
|
153 * The comment ID to check.
|
Chris@0
|
154 */
|
Chris@0
|
155 protected function assertNoParentLink($cid) {
|
Chris@0
|
156 // This pattern matches a markup structure like:
|
Chris@0
|
157 // <a id="comment-2"></a>
|
Chris@0
|
158 // <article>
|
Chris@0
|
159 // <p class="parent"></p>
|
Chris@0
|
160 // </article>
|
Chris@0
|
161
|
Chris@18
|
162 $pattern = "//article[@id='comment-$cid']//p[contains(@class, 'parent')]";
|
Chris@0
|
163 $this->assertNoFieldByXpath($pattern, NULL, format_string(
|
Chris@0
|
164 'Comment %cid does not have a link to a parent.',
|
Chris@0
|
165 [
|
Chris@0
|
166 '%cid' => $cid,
|
Chris@0
|
167 ]
|
Chris@0
|
168 ));
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 }
|