CommentInterfaceTest.php 10.3 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
use Drupal\comment\CommentInterface;
11
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
12

13
14
15
/**
 * Tests the comment module administrative and end-user-facing interfaces.
 */
16
class CommentInterfaceTest extends CommentTestBase {
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  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);
35
    $this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
36
37
38
39
40
41
    $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);
42
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
43
44
45
46
47
48
49
50
51
52
53
54
55

    // 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);
56
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
57

58
59
60
61
62
63
64
    // Comment as anonymous with preview required.
    $this->drupalLogout();
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content', 'access comments', 'post comments', 'skip comment approval'));
    $anonymous_comment = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
    $this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.');
    $anonymous_comment->delete();

65
    // Check comment display.
66
    $this->drupalLogin($this->web_user);
67
    $this->drupalGet('node/' . $this->node->id());
68
69
    $this->assertText($subject_text, 'Individual comment subject found.');
    $this->assertText($comment_text, 'Individual comment body found.');
70
71
72
73
74
75
76

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

77
    $this->drupalGet('comment/' . $comment->id() . '/edit');
78
    $this->assertTitle(t('Edit comment @title | Drupal', array(
79
      '@title' => $comment->getSubject(),
80
81
82
    )));

    // Test changing the comment author to "Anonymous".
83
84
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('name' => ''));
    $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
85
86
87

    // Test changing the comment author to an unverified user.
    $random_name = $this->randomName();
88
    $this->drupalGet('comment/' . $comment->id() . '/edit');
89
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('name' => $random_name));
90
    $this->drupalGet('node/' . $this->node->id());
91
    $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
92
93

    // Test changing the comment author to a verified user.
94
    $this->drupalGet('comment/' . $comment->id() . '/edit');
95
96
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('name' => $this->web_user->getUsername()));
    $this->assertTrue($comment->getAuthorName() == $this->web_user->getUsername() && $comment->getOwnerId() == $this->web_user->id(), 'Comment author successfully changed to a registered user.');
97
98
99
100
101
102

    $this->drupalLogout();

    // Reply to comment #2 creating comment #3 with optional preview and no
    // subject though field enabled.
    $this->drupalLogin($this->web_user);
103
104
105
106
107
108
    // 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());
109
110
    $this->assertText($subject_text, 'Individual comment-reply subject found.');
    $this->assertText($comment_text, 'Individual comment-reply body found.');
111
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
112
    $reply_loaded = comment_load($reply->id());
113
    $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
114
    $this->assertEqual($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.');
115
    // Check the thread of reply grows correctly.
116
    $this->assertEqual(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread());
117

118
119
    // Second reply to comment #2 creating comment #4.
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
120
    $this->assertText($comment->getSubject(), 'Individual comment-reply subject found.');
121
122
123
124
125
    $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.
126
    $this->assertEqual(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread());
127
128
129

    // Reply to comment #4 creating comment #5.
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
130
    $this->assertText($reply_loaded->getSubject(), 'Individual comment-reply subject found.');
131
    $this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.');
132
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
133
    $reply_loaded = comment_load($reply->id());
134
    $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
135
    // Check the thread of reply to second reply grows correctly.
136
    $this->assertEqual(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread());
137
138

    // Edit reply.
139
    $this->drupalGet('comment/' . $reply->id() . '/edit');
140
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
141
    $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
142
143
144
145

    // 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);
146
    $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
147
    $this->drupalGet('node/' . $this->node->id(), array('query' => array('page' => 2)));
148
    $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
149
150
151
    $this->setCommentsPerPage(50);

    // Attempt to reply to an unpublished comment.
152
    $reply_loaded->setPublished(FALSE);
153
    $reply_loaded->save();
154
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
155
156
157
    $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.
158
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => CommentItemInterface::HIDDEN))));
159
    $this->assertTrue($this->node, 'Article node created.');
160
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
161
162
    $this->assertText('This discussion is closed', 'Posting to node with comments disabled');
    $this->assertNoField('edit-comment', 'Comment body field found.');
163
164

    // Attempt to post to node with read-only comments.
165
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => CommentItemInterface::CLOSED))));
166
    $this->assertTrue($this->node, 'Article node created.');
167
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
168
169
    $this->assertText('This discussion is closed', 'Posting to node with comments read-only');
    $this->assertNoField('edit-comment', 'Comment body field found.');
170
171

    // Attempt to post to node with comments enabled (check field names etc).
172
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => CommentItemInterface::OPEN))));
173
    $this->assertTrue($this->node, 'Article node created.');
174
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
175
    $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
176
    $this->assertField('edit-comment-body-0-value', 'Comment body field found.');
177
178
179
180
181
182
183

    // 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);

184
    $this->drupalGet('node/' . $this->node->id());
185
186
    $this->assertFalse($this->commentExists($comment), 'Comment not found.');
    $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
187
188
189
190
191
192
193
194

    // 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);
195
    $this->drupalGet('node/' . $this->node->id());
196
    $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
197
    $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
198
199
200
201
202
203
204

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