CommentThreadingTest.php 7.04 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\comment\Tests\CommentThreadingTest.
 */

namespace Drupal\comment\Tests;

10 11
use Drupal\Core\Language\Language;

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**
 * Tests comment threading.
 */
class CommentThreadingTest extends CommentTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Comment Threading',
      'description' => 'Test to make sure the comment number increments properly.',
      'group' => 'Comment',
    );
  }

  /**
   * Tests the comment threading.
   */
  function testCommentThreading() {
28
    $langcode = Language::LANGCODE_NOT_SPECIFIED;
29 30 31 32 33
    // Set comments to have a subject with preview disabled.
    $this->drupalLogin($this->admin_user);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
34
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
35 36 37 38
    $this->drupalLogout();

    // Create a node.
    $this->drupalLogin($this->web_user);
39
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->id()));
40 41 42 43 44

    // Post comment #1.
    $this->drupalLogin($this->web_user);
    $subject_text = $this->randomName();
    $comment_text = $this->randomName();
45 46 47
    $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
48
    $this->assertEqual($comment1->thread->value, '01/');
49
    // Confirm that there is no reference to a parent comment.
50
    $this->assertNoParentLink($comment1->id());
51 52 53

    // Reply to comment #1 creating comment #2.
    $this->drupalLogin($this->web_user);
54
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment1->id());
55 56 57
    $comment2 = $this->postComment(NULL, $this->randomName(), '', TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment2, TRUE), 'Comment #2. Reply found.');
58
    $this->assertEqual($comment2->thread->value, '01.00/');
59
    // Confirm that there is a link to the parent comment.
60
    $this->assertParentLink($comment2->id(), $comment1->id());
61 62

    // Reply to comment #2 creating comment #3.
63
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment2->id());
64 65 66
    $comment3 = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment3, TRUE), 'Comment #3. Second reply found.');
67
    $this->assertEqual($comment3->thread->value, '01.00.00/');
68
    // Confirm that there is a link to the parent comment.
69
    $this->assertParentLink($comment3->id(), $comment2->id());
70 71 72

    // Reply to comment #1 creating comment #4.
    $this->drupalLogin($this->web_user);
73
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment1->id());
74 75 76
    $comment4 = $this->postComment(NULL, $this->randomName(), '', TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment4), 'Comment #4. Third reply found.');
77
    $this->assertEqual($comment4->thread->value, '01.01/');
78
    // Confirm that there is a link to the parent comment.
79
    $this->assertParentLink($comment4->id(), $comment1->id());
80 81 82 83 84

    // Post comment #2 overall comment #5.
    $this->drupalLogin($this->web_user);
    $subject_text = $this->randomName();
    $comment_text = $this->randomName();
85 86 87
    $comment5 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment5), 'Comment #5. Second comment found.');
88
    $this->assertEqual($comment5->thread->value, '02/');
89
    // Confirm that there is no link to a parent comment.
90
    $this->assertNoParentLink($comment5->id());
91 92 93

    // Reply to comment #5 creating comment #6.
    $this->drupalLogin($this->web_user);
94
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment5->id());
95 96 97
    $comment6 = $this->postComment(NULL, $this->randomName(), '', TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment6, TRUE), 'Comment #6. Reply found.');
98
    $this->assertEqual($comment6->thread->value, '02.00/');
99
    // Confirm that there is a link to the parent comment.
100
    $this->assertParentLink($comment6->id(), $comment5->id());
101 102

    // Reply to comment #6 creating comment #7.
103
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment6->id());
104 105 106
    $comment7 = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment7, TRUE), 'Comment #7. Second reply found.');
107
    $this->assertEqual($comment7->thread->value, '02.00.00/');
108
    // Confirm that there is a link to the parent comment.
109
    $this->assertParentLink($comment7->id(), $comment6->id());
110 111 112

    // Reply to comment #5 creating comment #8.
    $this->drupalLogin($this->web_user);
113
    $this->drupalGet('comment/reply/' . $this->node->nid . '/' . $comment5->id());
114 115 116
    $comment8 = $this->postComment(NULL, $this->randomName(), '', TRUE);
    // Confirm that the comment was created and has the correct threading.
    $this->assertTrue($this->commentExists($comment8), 'Comment #8. Third reply found.');
117
    $this->assertEqual($comment8->thread->value, '02.01/');
118
    // Confirm that there is a link to the parent comment.
119
    $this->assertParentLink($comment8->id(), $comment5->id());
120
  }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  /**
   * Asserts that the link to the specified parent comment is present.
   *
   * @parm int $cid
   *   The comment ID to check.
   * @param int $pid
   *   The expected parent comment ID.
   */
  protected function assertParentLink($cid, $pid) {
    // This pattern matches a markup structure like:
    // <a id="comment-2"></a>
    // <article>
    //   <p class="parent">
    //     <a href="...comment-1"></a>
    //   </p>
    //  </article>
    $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]//a[contains(@href, 'comment-$pid')]";

    $this->assertFieldByXpath($pattern, NULL, format_string(
      'Comment %cid has a link to parent %pid.',
      array(
        '%cid' => $cid,
        '%pid' => $pid,
      )
    ));
  }

  /**
   * Asserts that the specified comment does not have a link to a parent.
   *
   * @parm int $cid
   *   The comment ID to check.
   */
  protected function assertNoParentLink($cid) {
    // This pattern matches a markup structure like:
    // <a id="comment-2"></a>
    // <article>
    //   <p class="parent"></p>
    //  </article>

    $pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]";
    $this->assertNoFieldByXpath($pattern, NULL, format_string(
      'Comment %cid does not have a link to a parent.',
      array(
        '%cid' => $cid,
      )
    ));
  }

171
}