Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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   /**
15    * Tests the comment threading.
16    */
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();
25
26     // Create a node.
27     $this->drupalLogin($this->webUser);
28     $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
29
30     // Post comment #1.
31     $this->drupalLogin($this->webUser);
32     $subject_text = $this->randomMachineName();
33     $comment_text = $this->randomMachineName();
34
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());
41
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);
47
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);
51
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());
57
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);
61
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());
67
68     // Reply to comment #1 creating comment #1_5.
69     $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
70
71     $comment1_5 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
72
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());
78
79     // Post comment #3 overall comment #5.
80     $this->drupalLogin($this->webUser);
81     $subject_text = $this->randomMachineName();
82     $comment_text = $this->randomMachineName();
83
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());
90
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);
94
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());
100
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);
104
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());
110
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);
114
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());
120   }
121
122   /**
123    * Asserts that the link to the specified parent comment is present.
124    *
125    * @param int $cid
126    *   The comment ID to check.
127    * @param int $pid
128    *   The expected parent comment ID.
129    */
130   protected function assertParentLink($cid, $pid) {
131     // This pattern matches a markup structure like:
132     // <a id="comment-2"></a>
133     // <article>
134     //   <p class="parent">
135     //     <a href="...comment-1"></a>
136     //   </p>
137     //  </article>
138     $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]//a[contains(@href, 'comment-$pid')]";
139
140     $this->assertFieldByXpath($pattern, NULL, format_string(
141       'Comment %cid has a link to parent %pid.',
142       [
143         '%cid' => $cid,
144         '%pid' => $pid,
145       ]
146     ));
147   }
148
149   /**
150    * Asserts that the specified comment does not have a link to a parent.
151    *
152    * @param int $cid
153    *   The comment ID to check.
154    */
155   protected function assertNoParentLink($cid) {
156     // This pattern matches a markup structure like:
157     // <a id="comment-2"></a>
158     // <article>
159     //   <p class="parent"></p>
160     //  </article>
161
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.',
165       [
166         '%cid' => $cid,
167       ]
168     ));
169   }
170
171 }