annotate core/modules/comment/tests/src/Functional/CommentThreadingTest.php @ 0:c75dbcec494b

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