3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7 use Drupal\comment\Entity\Comment;
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9 use Drupal\Core\Entity\Entity\EntityViewMode;
10 use Drupal\user\RoleInterface;
11 use Drupal\filter\Entity\FilterFormat;
14 * Tests comment user interfaces.
18 class CommentInterfaceTest extends CommentTestBase {
21 * Set up comments to have subject and preview disabled.
23 protected function setUp() {
25 $this->drupalLogin($this->adminUser);
26 // Make sure that comment field title is not displayed when there's no
28 $this->drupalGet($this->node->urlInfo());
29 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is not displayed.');
31 // Set comments to have subject and preview disabled.
32 $this->setCommentPreview(DRUPAL_DISABLED);
33 $this->setCommentForm(TRUE);
34 $this->setCommentSubject(FALSE);
35 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
36 $this->drupalLogout();
40 * Tests the comment interface.
42 public function testCommentInterface() {
44 // Post comment #1 without subject or preview.
45 $this->drupalLogin($this->webUser);
46 $comment_text = $this->randomMachineName();
47 $comment = $this->postComment($this->node, $comment_text);
48 $this->assertTrue($this->commentExists($comment), 'Comment found.');
50 // Test the comment field title is displayed when there's comments.
51 $this->drupalGet($this->node->urlInfo());
52 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is displayed.');
54 // Set comments to have subject and preview to required.
55 $this->drupalLogout();
56 $this->drupalLogin($this->adminUser);
57 $this->setCommentSubject(TRUE);
58 $this->setCommentPreview(DRUPAL_REQUIRED);
59 $this->drupalLogout();
61 // Create comment #2 that allows subject and requires preview.
62 $this->drupalLogin($this->webUser);
63 $subject_text = $this->randomMachineName();
64 $comment_text = $this->randomMachineName();
65 $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
66 $this->assertTrue($this->commentExists($comment), 'Comment found.');
68 // Comment as anonymous with preview required.
69 $this->drupalLogout();
70 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content', 'access comments', 'post comments', 'skip comment approval']);
71 $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
72 $this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.');
73 $anonymous_comment->delete();
75 // Check comment display.
76 $this->drupalLogin($this->webUser);
77 $this->drupalGet('node/' . $this->node->id());
78 $this->assertText($subject_text, 'Individual comment subject found.');
79 $this->assertText($comment_text, 'Individual comment body found.');
81 ':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(),
83 $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]';
84 $permalink = $this->xpath($pattern_permalink, $arguments);
85 $this->assertTrue(!empty($permalink), 'Permalink link found.');
87 // Set comments to have subject and preview to optional.
88 $this->drupalLogout();
89 $this->drupalLogin($this->adminUser);
90 $this->setCommentSubject(TRUE);
91 $this->setCommentPreview(DRUPAL_OPTIONAL);
93 $this->drupalGet('comment/' . $comment->id() . '/edit');
94 $this->assertTitle(t('Edit comment @title | Drupal', [
95 '@title' => $comment->getSubject(),
98 // Test changing the comment author to "Anonymous".
99 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']);
100 $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
102 // Test changing the comment author to an unverified user.
103 $random_name = $this->randomMachineName();
104 $this->drupalGet('comment/' . $comment->id() . '/edit');
105 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['name' => $random_name]);
106 $this->drupalGet('node/' . $this->node->id());
107 $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
109 // Test changing the comment author to a verified user.
110 $this->drupalGet('comment/' . $comment->id() . '/edit');
111 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => $this->webUser->getUsername() . ' (' . $this->webUser->id() . ')']);
112 $this->assertTrue($comment->getAuthorName() == $this->webUser->getUsername() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.');
114 $this->drupalLogout();
116 // Reply to comment #2 creating comment #3 with optional preview and no
117 // subject though field enabled.
118 $this->drupalLogin($this->webUser);
119 // Deliberately use the wrong url to test
120 // \Drupal\comment\Controller\CommentController::redirectNode().
121 $this->drupalGet('comment/' . $this->node->id() . '/reply');
122 // Verify we were correctly redirected.
123 $this->assertUrl(\Drupal::url('comment.reply', ['entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment'], ['absolute' => TRUE]));
124 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
125 $this->assertText($subject_text, 'Individual comment-reply subject found.');
126 $this->assertText($comment_text, 'Individual comment-reply body found.');
127 $reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
128 $reply_loaded = Comment::load($reply->id());
129 $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
130 $this->assertEqual($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.');
131 // Check the thread of reply grows correctly.
132 $this->assertEqual(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread());
134 // Second reply to comment #2 creating comment #4.
135 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
136 $this->assertText($comment->getSubject(), 'Individual comment-reply subject found.');
137 $this->assertText($comment->comment_body->value, 'Individual comment-reply body found.');
138 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
139 $reply_loaded = Comment::load($reply->id());
140 $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
141 // Check the thread of second reply grows correctly.
142 $this->assertEqual(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread());
144 // Reply to comment #4 creating comment #5.
145 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
146 $this->assertText($reply_loaded->getSubject(), 'Individual comment-reply subject found.');
147 $this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.');
148 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
149 $reply_loaded = Comment::load($reply->id());
150 $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
151 // Check the thread of reply to second reply grows correctly.
152 $this->assertEqual(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread());
155 $this->drupalGet('comment/' . $reply->id() . '/edit');
156 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
157 $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
159 // Confirm a new comment is posted to the correct page.
160 $this->setCommentsPerPage(2);
161 $comment_new_page = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
162 $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
163 $this->drupalGet('node/' . $this->node->id(), ['query' => ['page' => 2]]);
164 $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
165 $this->setCommentsPerPage(50);
167 // Attempt to reply to an unpublished comment.
168 $reply_loaded->setUnpublished();
169 $reply_loaded->save();
170 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
171 $this->assertResponse(403);
173 // Attempt to post to node with comments disabled.
174 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::HIDDEN]]]);
175 $this->assertTrue($this->node, 'Article node created.');
176 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
177 $this->assertResponse(403);
178 $this->assertNoField('edit-comment', 'Comment body field found.');
180 // Attempt to post to node with read-only comments.
181 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::CLOSED]]]);
182 $this->assertTrue($this->node, 'Article node created.');
183 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
184 $this->assertResponse(403);
185 $this->assertNoField('edit-comment', 'Comment body field found.');
187 // Attempt to post to node with comments enabled (check field names etc).
188 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::OPEN]]]);
189 $this->assertTrue($this->node, 'Article node created.');
190 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
191 $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
192 $this->assertField('edit-comment-body-0-value', 'Comment body field found.');
194 // Delete comment and make sure that reply is also removed.
195 $this->drupalLogout();
196 $this->drupalLogin($this->adminUser);
197 $this->deleteComment($comment);
198 $this->deleteComment($comment_new_page);
200 $this->drupalGet('node/' . $this->node->id());
201 $this->assertFalse($this->commentExists($comment), 'Comment not found.');
202 $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
204 // Enabled comment form on node page.
205 $this->drupalLogin($this->adminUser);
206 $this->setCommentForm(TRUE);
207 $this->drupalLogout();
209 // Submit comment through node form.
210 $this->drupalLogin($this->webUser);
211 $this->drupalGet('node/' . $this->node->id());
212 $form_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
213 $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
215 // Disable comment form on node page.
216 $this->drupalLogout();
217 $this->drupalLogin($this->adminUser);
218 $this->setCommentForm(FALSE);
222 * Test that the subject is automatically filled if disabled or left blank.
224 * When the subject field is blank or disabled, the first 29 characters of the
225 * comment body are used for the subject. If this would break within a word,
226 * then the break is put at the previous word boundary instead.
228 public function testAutoFilledSubject() {
229 $this->drupalLogin($this->webUser);
230 $this->drupalGet('node/' . $this->node->id());
232 // Break when there is a word boundary before 29 characters.
233 $body_text = 'Lorem ipsum Lorem ipsum Loreming ipsum Lorem ipsum';
234 $comment1 = $this->postComment(NULL, $body_text, '', TRUE);
235 $this->assertTrue($this->commentExists($comment1), 'Form comment found.');
236 $this->assertEqual('Lorem ipsum Lorem ipsum…', $comment1->getSubject());
238 // Break at 29 characters where there's no boundary before that.
239 $body_text2 = 'LoremipsumloremipsumLoremingipsumLoremipsum';
240 $comment2 = $this->postComment(NULL, $body_text2, '', TRUE);
241 $this->assertEqual('LoremipsumloremipsumLoreming…', $comment2->getSubject());
245 * Test that automatic subject is correctly created from HTML comment text.
247 * This is the same test as in CommentInterfaceTest::testAutoFilledSubject()
248 * with the additional check that HTML is stripped appropriately prior to
249 * character-counting.
251 public function testAutoFilledHtmlSubject() {
252 // Set up two default (i.e. filtered HTML) input formats, because then we
253 // can select one of them. Then create a user that can use these formats,
254 // log the user in, and then GET the node page on which to test the
256 $filtered_html_format = FilterFormat::create([
257 'format' => 'filtered_html',
258 'name' => 'Filtered HTML',
260 $filtered_html_format->save();
261 $full_html_format = FilterFormat::create([
262 'format' => 'full_html',
263 'name' => 'Full HTML',
265 $full_html_format->save();
266 $html_user = $this->drupalCreateUser([
270 'skip comment approval',
272 $filtered_html_format->getPermissionName(),
273 $full_html_format->getPermissionName(),
275 $this->drupalLogin($html_user);
276 $this->drupalGet('node/' . $this->node->id());
278 // HTML should not be included in the character count.
279 $body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />';
281 'comment_body[0][value]' => $body_text1,
282 'comment_body[0][format]' => 'filtered_html',
284 $this->drupalPostForm(NULL, $edit1, t('Save'));
285 $this->assertEqual('Hello World', Comment::load(1)->getSubject());
287 // If there's nothing other than HTML, the subject should be '(No subject)'.
288 $body_text2 = '<span></span><strong> </strong><span> </span><strong></strong> <br />';
290 'comment_body[0][value]' => $body_text2,
291 'comment_body[0][format]' => 'filtered_html',
293 $this->drupalPostForm(NULL, $edit2, t('Save'));
294 $this->assertEqual('(No subject)', Comment::load(2)->getSubject());
298 * Tests the comment formatter configured with a custom comment view mode.
300 public function testViewMode() {
301 $this->drupalLogin($this->webUser);
302 $this->drupalGet($this->node->toUrl());
303 $comment_text = $this->randomMachineName();
305 $this->postComment($this->node, $comment_text);
307 // Comment displayed in 'default' display mode found and has body text.
308 $comment_element = $this->cssSelect('.comment-wrapper');
309 $this->assertTrue(!empty($comment_element));
310 $this->assertRaw('<p>' . $comment_text . '</p>');
312 // Create a new comment entity view mode.
313 $mode = mb_strtolower($this->randomMachineName());
314 EntityViewMode::create([
315 'targetEntityType' => 'comment',
316 'id' => "comment.$mode",
318 // Create the corresponding entity view display for article node-type. Note
319 // that this new view display mode doesn't contain the comment body.
320 EntityViewDisplay::create([
321 'targetEntityType' => 'comment',
322 'bundle' => 'comment',
324 ])->setStatus(TRUE)->save();
326 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */
327 $node_display = EntityViewDisplay::load('node.article.default');
328 $formatter = $node_display->getComponent('comment');
329 // Change the node comment field formatter to use $mode mode instead of
331 $formatter['settings']['view_mode'] = $mode;
333 ->setComponent('comment', $formatter)
336 // Reloading the node page to show the same node with its same comment but
337 // with a different display mode.
338 $this->drupalGet($this->node->toUrl());
339 // The comment should exist but without the body text because we used $mode
341 $comment_element = $this->cssSelect('.comment-wrapper');
342 $this->assertTrue(!empty($comment_element));
343 $this->assertNoRaw('<p>' . $comment_text . '</p>');