CommentThreadingTest.php 7.07 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
28
29
30
31
32
/**
 * 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() {
    // Set comments to have a subject with preview disabled.
    $this->drupalLogin($this->admin_user);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
33
    $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
34
35
36
37
    $this->drupalLogout();

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

    // Post comment #1.
    $this->drupalLogin($this->web_user);
    $subject_text = $this->randomName();
    $comment_text = $this->randomName();
44
45
46
    $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.');
47
    $this->assertEqual($comment1->thread->value, '01/');
48
    // Confirm that there is no reference to a parent comment.
49
    $this->assertNoParentLink($comment1->id());
50
51
52

    // Reply to comment #1 creating comment #2.
    $this->drupalLogin($this->web_user);
53
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
54
55
56
    $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.');
57
    $this->assertEqual($comment2->thread->value, '01.00/');
58
    // Confirm that there is a link to the parent comment.
59
    $this->assertParentLink($comment2->id(), $comment1->id());
60
61

    // Reply to comment #2 creating comment #3.
62
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment2->id());
63
64
65
    $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.');
66
    $this->assertEqual($comment3->thread->value, '01.00.00/');
67
    // Confirm that there is a link to the parent comment.
68
    $this->assertParentLink($comment3->id(), $comment2->id());
69
70
71

    // Reply to comment #1 creating comment #4.
    $this->drupalLogin($this->web_user);
72
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
73
74
75
    $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.');
76
    $this->assertEqual($comment4->thread->value, '01.01/');
77
    // Confirm that there is a link to the parent comment.
78
    $this->assertParentLink($comment4->id(), $comment1->id());
79
80
81
82
83

    // Post comment #2 overall comment #5.
    $this->drupalLogin($this->web_user);
    $subject_text = $this->randomName();
    $comment_text = $this->randomName();
84
85
86
    $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.');
87
    $this->assertEqual($comment5->thread->value, '02/');
88
    // Confirm that there is no link to a parent comment.
89
    $this->assertNoParentLink($comment5->id());
90
91
92

    // Reply to comment #5 creating comment #6.
    $this->drupalLogin($this->web_user);
93
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
94
95
96
    $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.');
97
    $this->assertEqual($comment6->thread->value, '02.00/');
98
    // Confirm that there is a link to the parent comment.
99
    $this->assertParentLink($comment6->id(), $comment5->id());
100
101

    // Reply to comment #6 creating comment #7.
102
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment6->id());
103
104
105
    $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.');
106
    $this->assertEqual($comment7->thread->value, '02.00.00/');
107
    // Confirm that there is a link to the parent comment.
108
    $this->assertParentLink($comment7->id(), $comment6->id());
109
110
111

    // Reply to comment #5 creating comment #8.
    $this->drupalLogin($this->web_user);
112
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
113
114
115
    $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.');
116
    $this->assertEqual($comment8->thread->value, '02.01/');
117
    // Confirm that there is a link to the parent comment.
118
    $this->assertParentLink($comment8->id(), $comment5->id());
119
  }
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

  /**
   * 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,
      )
    ));
  }

170
}