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

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

namespace Drupal\comment\Tests;

10
11
12
/**
 * Tests the comment module administrative and end-user-facing interfaces.
 */
13
class CommentInterfaceTest extends CommentTestBase {
14

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  public static function getInfo() {
    return array(
      'name' => 'Comment interface',
      'description' => 'Test comment user interfaces.',
      'group' => 'Comment',
    );
  }

  /**
   * Tests the comment interface.
   */
  function testCommentInterface() {
    // Set comments to have subject and preview disabled.
    $this->drupalLogin($this->admin_user);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(FALSE);
32
    $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
33
34
35
36
37
38
    $this->drupalLogout();

    // Post comment #1 without subject or preview.
    $this->drupalLogin($this->web_user);
    $comment_text = $this->randomName();
    $comment = $this->postComment($this->node, $comment_text);
39
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
40
41
42
43
44
45
46
47
48
49
50
51
52

    // Set comments to have subject and preview to required.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->setCommentSubject(TRUE);
    $this->setCommentPreview(DRUPAL_REQUIRED);
    $this->drupalLogout();

    // Create comment #2 that allows subject and requires preview.
    $this->drupalLogin($this->web_user);
    $subject_text = $this->randomName();
    $comment_text = $this->randomName();
    $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
53
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
54
55

    // Check comment display.
56
    $this->drupalGet('node/' . $this->node->id());
57
58
    $this->assertText($subject_text, 'Individual comment subject found.');
    $this->assertText($comment_text, 'Individual comment body found.');
59
60
61
62
63
64
65

    // Set comments to have subject and preview to optional.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->setCommentSubject(TRUE);
    $this->setCommentPreview(DRUPAL_OPTIONAL);

66
    $this->drupalGet('comment/' . $comment->id() . '/edit');
67
68
69
70
71
    $this->assertTitle(t('Edit comment @title | Drupal', array(
      '@title' => $comment->subject->value,
    )));

    // Test changing the comment author to "Anonymous".
72
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => ''));
73
    $this->assertTrue(empty($comment->name->value) && $comment->uid->target_id == 0, 'Comment author successfully changed to anonymous.');
74
75
76

    // Test changing the comment author to an unverified user.
    $random_name = $this->randomName();
77
78
    $this->drupalGet('comment/' . $comment->id() . '/edit');
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => $random_name));
79
    $this->drupalGet('node/' . $this->node->id());
80
    $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
81
82

    // Test changing the comment author to a verified user.
83
    $this->drupalGet('comment/' . $comment->id() . '/edit');
84
85
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => $this->web_user->getUsername()));
    $this->assertTrue($comment->name->value == $this->web_user->getUsername() && $comment->uid->target_id == $this->web_user->id(), 'Comment author successfully changed to a registered user.');
86
87
88
89
90
91

    $this->drupalLogout();

    // Reply to comment #2 creating comment #3 with optional preview and no
    // subject though field enabled.
    $this->drupalLogin($this->web_user);
92
93
94
95
96
97
    // Deliberately use the wrong url to test
    // \Drupal\comment\Controller\CommentController::redirectNode().
    $this->drupalGet('comment/' . $this->node->id() . '/reply');
    // Verify we were correctly redirected.
    $this->assertUrl(url('comment/reply/node/' . $this->node->id() . '/comment', array('absolute' => TRUE)));
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
98
99
    $this->assertText($subject_text, 'Individual comment-reply subject found.');
    $this->assertText($comment_text, 'Individual comment-reply body found.');
100
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
101
    $reply_loaded = comment_load($reply->id());
102
    $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
103
    $this->assertEqual($comment->id(), $reply_loaded->pid->target_id, 'Pid of a reply to a comment is set correctly.');
104
105
    // Check the thread of reply grows correctly.
    $this->assertEqual(rtrim($comment->thread->value, '/') . '.00/', $reply_loaded->thread->value);
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
    // Second reply to comment #2 creating comment #4.
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
    $this->assertText($comment->subject->value, 'Individual comment-reply subject found.');
    $this->assertText($comment->comment_body->value, 'Individual comment-reply body found.');
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
    $reply_loaded = comment_load($reply->id());
    $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
    // Check the thread of second reply grows correctly.
    $this->assertEqual(rtrim($comment->thread->value, '/') . '.01/', $reply_loaded->thread->value);

    // Reply to comment #4 creating comment #5.
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
    $this->assertText($reply_loaded->subject->value, 'Individual comment-reply subject found.');
    $this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.');
121
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
122
    $reply_loaded = comment_load($reply->id());
123
    $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
124
125
    // Check the thread of reply to second reply grows correctly.
    $this->assertEqual(rtrim($comment->thread->value, '/') . '.01.00/', $reply_loaded->thread->value);
126
127

    // Edit reply.
128
    $this->drupalGet('comment/' . $reply->id() . '/edit');
129
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
130
    $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
131
132
133
134

    // Confirm a new comment is posted to the correct page.
    $this->setCommentsPerPage(2);
    $comment_new_page = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
135
    $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
136
    $this->drupalGet('node/' . $this->node->id(), array('query' => array('page' => 2)));
137
    $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
138
139
140
    $this->setCommentsPerPage(50);

    // Attempt to reply to an unpublished comment.
141
    $reply_loaded->status->value = COMMENT_NOT_PUBLISHED;
142
    $reply_loaded->save();
143
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
144
145
146
    $this->assertText(t('The comment you are replying to does not exist.'), 'Replying to an unpublished comment');

    // Attempt to post to node with comments disabled.
147
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => COMMENT_HIDDEN))));
148
    $this->assertTrue($this->node, 'Article node created.');
149
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
150
151
    $this->assertText('This discussion is closed', 'Posting to node with comments disabled');
    $this->assertNoField('edit-comment', 'Comment body field found.');
152
153

    // Attempt to post to node with read-only comments.
154
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => COMMENT_CLOSED))));
155
    $this->assertTrue($this->node, 'Article node created.');
156
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
157
158
    $this->assertText('This discussion is closed', 'Posting to node with comments read-only');
    $this->assertNoField('edit-comment', 'Comment body field found.');
159
160

    // Attempt to post to node with comments enabled (check field names etc).
161
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => COMMENT_OPEN))));
162
    $this->assertTrue($this->node, 'Article node created.');
163
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
164
    $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
165
    $this->assertField('edit-comment-body-0-value', 'Comment body field found.');
166
167
168
169
170
171
172

    // Delete comment and make sure that reply is also removed.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->deleteComment($comment);
    $this->deleteComment($comment_new_page);

173
    $this->drupalGet('node/' . $this->node->id());
174
175
    $this->assertFalse($this->commentExists($comment), 'Comment not found.');
    $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
176
177
178
179
180
181
182
183

    // Enabled comment form on node page.
    $this->drupalLogin($this->admin_user);
    $this->setCommentForm(TRUE);
    $this->drupalLogout();

    // Submit comment through node form.
    $this->drupalLogin($this->web_user);
184
    $this->drupalGet('node/' . $this->node->id());
185
    $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
186
    $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
187
188
189
190
191
192
193

    // Disable comment form on node page.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->setCommentForm(FALSE);
  }
}