Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / tests / src / Functional / CommentThreadingTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional;
4
5 use Drupal\comment\CommentManagerInterface;
6
7 /**
8  * Tests to make sure the comment number increments properly.
9  *
10  * @group comment
11  */
12 class CommentThreadingTest extends CommentTestBase {
13   /**
14    * Tests the comment threading.
15    */
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();
24
25     // Create a node.
26     $this->drupalLogin($this->webUser);
27     $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
28
29     // Post comment #1.
30     $this->drupalLogin($this->webUser);
31     $subject_text = $this->randomMachineName();
32     $comment_text = $this->randomMachineName();
33
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());
40
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);
46
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);
50
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());
56
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);
60
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());
66
67     // Reply to comment #1 creating comment #1_5.
68     $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
69
70     $comment1_5 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
71
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());
77
78     // Post comment #3 overall comment #5.
79     $this->drupalLogin($this->webUser);
80     $subject_text = $this->randomMachineName();
81     $comment_text = $this->randomMachineName();
82
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());
89
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);
93
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());
99
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);
103
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());
109
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);
113
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());
119   }
120
121   /**
122    * Asserts that the link to the specified parent comment is present.
123    *
124    * @param int $cid
125    *   The comment ID to check.
126    * @param int $pid
127    *   The expected parent comment ID.
128    */
129   protected function assertParentLink($cid, $pid) {
130     // This pattern matches a markup structure like:
131     // <a id="comment-2"></a>
132     // <article>
133     //   <p class="parent">
134     //     <a href="...comment-1"></a>
135     //   </p>
136     //  </article>
137     $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]//a[contains(@href, 'comment-$pid')]";
138
139     $this->assertFieldByXpath($pattern, NULL, format_string(
140       'Comment %cid has a link to parent %pid.',
141       [
142         '%cid' => $cid,
143         '%pid' => $pid,
144       ]
145     ));
146   }
147
148   /**
149    * Asserts that the specified comment does not have a link to a parent.
150    *
151    * @param int $cid
152    *   The comment ID to check.
153    */
154   protected function assertNoParentLink($cid) {
155     // This pattern matches a markup structure like:
156     // <a id="comment-2"></a>
157     // <article>
158     //   <p class="parent"></p>
159     //  </article>
160
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.',
164       [
165         '%cid' => $cid,
166       ]
167     ));
168   }
169
170 }