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\Component\Utility\Unicode;
9 use Drupal\Core\Entity\Entity\EntityViewDisplay;
10 use Drupal\Core\Entity\Entity\EntityViewMode;
11 use Drupal\user\RoleInterface;
12 use Drupal\filter\Entity\FilterFormat;
15 * Tests comment user interfaces.
19 class CommentInterfaceTest extends CommentTestBase {
22 * Set up comments to have subject and preview disabled.
24 protected function setUp() {
26 $this->drupalLogin($this->adminUser);
27 // Make sure that comment field title is not displayed when there's no
29 $this->drupalGet($this->node->urlInfo());
30 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is not displayed.');
32 // Set comments to have subject and preview disabled.
33 $this->setCommentPreview(DRUPAL_DISABLED);
34 $this->setCommentForm(TRUE);
35 $this->setCommentSubject(FALSE);
36 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
37 $this->drupalLogout();
41 * Tests the comment interface.
43 public function testCommentInterface() {
45 // Post comment #1 without subject or preview.
46 $this->drupalLogin($this->webUser);
47 $comment_text = $this->randomMachineName();
48 $comment = $this->postComment($this->node, $comment_text);
49 $this->assertTrue($this->commentExists($comment), 'Comment found.');
51 // Test the comment field title is displayed when there's comments.
52 $this->drupalGet($this->node->urlInfo());
53 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is displayed.');
55 // Set comments to have subject and preview to required.
56 $this->drupalLogout();
57 $this->drupalLogin($this->adminUser);
58 $this->setCommentSubject(TRUE);
59 $this->setCommentPreview(DRUPAL_REQUIRED);
60 $this->drupalLogout();
62 // Create comment #2 that allows subject and requires preview.
63 $this->drupalLogin($this->webUser);
64 $subject_text = $this->randomMachineName();
65 $comment_text = $this->randomMachineName();
66 $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
67 $this->assertTrue($this->commentExists($comment), 'Comment found.');
69 // Comment as anonymous with preview required.
70 $this->drupalLogout();
71 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content', 'access comments', 'post comments', 'skip comment approval']);
72 $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
73 $this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.');
74 $anonymous_comment->delete();
76 // Check comment display.
77 $this->drupalLogin($this->webUser);
78 $this->drupalGet('node/' . $this->node->id());
79 $this->assertText($subject_text, 'Individual comment subject found.');
80 $this->assertText($comment_text, 'Individual comment body found.');
82 ':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(),
84 $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]';
85 $permalink = $this->xpath($pattern_permalink, $arguments);
86 $this->assertTrue(!empty($permalink), 'Permalink link found.');
88 // Set comments to have subject and preview to optional.
89 $this->drupalLogout();
90 $this->drupalLogin($this->adminUser);
91 $this->setCommentSubject(TRUE);
92 $this->setCommentPreview(DRUPAL_OPTIONAL);
94 $this->drupalGet('comment/' . $comment->id() . '/edit');
95 $this->assertTitle(t('Edit comment @title | Drupal', [
96 '@title' => $comment->getSubject(),
99 // Test changing the comment author to "Anonymous".
100 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']);
101 $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
103 // Test changing the comment author to an unverified user.
104 $random_name = $this->randomMachineName();
105 $this->drupalGet('comment/' . $comment->id() . '/edit');
106 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['name' => $random_name]);
107 $this->drupalGet('node/' . $this->node->id());
108 $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
110 // Test changing the comment author to a verified user.
111 $this->drupalGet('comment/' . $comment->id() . '/edit');
112 $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => $this->webUser->getUsername() . ' (' . $this->webUser->id() . ')']);
113 $this->assertTrue($comment->getAuthorName() == $this->webUser->getUsername() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.');
115 $this->drupalLogout();
117 // Reply to comment #2 creating comment #3 with optional preview and no
118 // subject though field enabled.
119 $this->drupalLogin($this->webUser);
120 // Deliberately use the wrong url to test
121 // \Drupal\comment\Controller\CommentController::redirectNode().
122 $this->drupalGet('comment/' . $this->node->id() . '/reply');
123 // Verify we were correctly redirected.
124 $this->assertUrl(\Drupal::url('comment.reply', ['entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment'], ['absolute' => TRUE]));
125 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
126 $this->assertText($subject_text, 'Individual comment-reply subject found.');
127 $this->assertText($comment_text, 'Individual comment-reply body found.');
128 $reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
129 $reply_loaded = Comment::load($reply->id());
130 $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
131 $this->assertEqual($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.');
132 // Check the thread of reply grows correctly.
133 $this->assertEqual(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread());
135 // Second reply to comment #2 creating comment #4.
136 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
137 $this->assertText($comment->getSubject(), 'Individual comment-reply subject found.');
138 $this->assertText($comment->comment_body->value, 'Individual comment-reply body found.');
139 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
140 $reply_loaded = Comment::load($reply->id());
141 $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
142 // Check the thread of second reply grows correctly.
143 $this->assertEqual(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread());
145 // Reply to comment #4 creating comment #5.
146 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
147 $this->assertText($reply_loaded->getSubject(), 'Individual comment-reply subject found.');
148 $this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.');
149 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
150 $reply_loaded = Comment::load($reply->id());
151 $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
152 // Check the thread of reply to second reply grows correctly.
153 $this->assertEqual(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread());
156 $this->drupalGet('comment/' . $reply->id() . '/edit');
157 $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
158 $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
160 // Confirm a new comment is posted to the correct page.
161 $this->setCommentsPerPage(2);
162 $comment_new_page = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
163 $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
164 $this->drupalGet('node/' . $this->node->id(), ['query' => ['page' => 2]]);
165 $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
166 $this->setCommentsPerPage(50);
168 // Attempt to reply to an unpublished comment.
169 $reply_loaded->setPublished(FALSE);
170 $reply_loaded->save();
171 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
172 $this->assertResponse(403);
174 // Attempt to post to node with comments disabled.
175 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::HIDDEN]]]);
176 $this->assertTrue($this->node, 'Article node created.');
177 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
178 $this->assertResponse(403);
179 $this->assertNoField('edit-comment', 'Comment body field found.');
181 // Attempt to post to node with read-only comments.
182 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::CLOSED]]]);
183 $this->assertTrue($this->node, 'Article node created.');
184 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
185 $this->assertResponse(403);
186 $this->assertNoField('edit-comment', 'Comment body field found.');
188 // Attempt to post to node with comments enabled (check field names etc).
189 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::OPEN]]]);
190 $this->assertTrue($this->node, 'Article node created.');
191 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
192 $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
193 $this->assertField('edit-comment-body-0-value', 'Comment body field found.');
195 // Delete comment and make sure that reply is also removed.
196 $this->drupalLogout();
197 $this->drupalLogin($this->adminUser);
198 $this->deleteComment($comment);
199 $this->deleteComment($comment_new_page);
201 $this->drupalGet('node/' . $this->node->id());
202 $this->assertFalse($this->commentExists($comment), 'Comment not found.');
203 $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
205 // Enabled comment form on node page.
206 $this->drupalLogin($this->adminUser);
207 $this->setCommentForm(TRUE);
208 $this->drupalLogout();
210 // Submit comment through node form.
211 $this->drupalLogin($this->webUser);
212 $this->drupalGet('node/' . $this->node->id());
213 $form_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
214 $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
216 // Disable comment form on node page.
217 $this->drupalLogout();
218 $this->drupalLogin($this->adminUser);
219 $this->setCommentForm(FALSE);
223 * Test that the subject is automatically filled if disabled or left blank.
225 * When the subject field is blank or disabled, the first 29 characters of the
226 * comment body are used for the subject. If this would break within a word,
227 * then the break is put at the previous word boundary instead.
229 public function testAutoFilledSubject() {
230 $this->drupalLogin($this->webUser);
231 $this->drupalGet('node/' . $this->node->id());
233 // Break when there is a word boundary before 29 characters.
234 $body_text = 'Lorem ipsum Lorem ipsum Loreming ipsum Lorem ipsum';
235 $comment1 = $this->postComment(NULL, $body_text, '', TRUE);
236 $this->assertTrue($this->commentExists($comment1), 'Form comment found.');
237 $this->assertEqual('Lorem ipsum Lorem ipsum…', $comment1->getSubject());
239 // Break at 29 characters where there's no boundary before that.
240 $body_text2 = 'LoremipsumloremipsumLoremingipsumLoremipsum';
241 $comment2 = $this->postComment(NULL, $body_text2, '', TRUE);
242 $this->assertEqual('LoremipsumloremipsumLoreming…', $comment2->getSubject());
246 * Test that automatic subject is correctly created from HTML comment text.
248 * This is the same test as in CommentInterfaceTest::testAutoFilledSubject()
249 * with the additional check that HTML is stripped appropriately prior to
250 * character-counting.
252 public function testAutoFilledHtmlSubject() {
253 // Set up two default (i.e. filtered HTML) input formats, because then we
254 // can select one of them. Then create a user that can use these formats,
255 // log the user in, and then GET the node page on which to test the
257 $filtered_html_format = FilterFormat::create([
258 'format' => 'filtered_html',
259 'name' => 'Filtered HTML',
261 $filtered_html_format->save();
262 $full_html_format = FilterFormat::create([
263 'format' => 'full_html',
264 'name' => 'Full HTML',
266 $full_html_format->save();
267 $html_user = $this->drupalCreateUser([
271 'skip comment approval',
273 $filtered_html_format->getPermissionName(),
274 $full_html_format->getPermissionName(),
276 $this->drupalLogin($html_user);
277 $this->drupalGet('node/' . $this->node->id());
279 // HTML should not be included in the character count.
280 $body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />';
282 'comment_body[0][value]' => $body_text1,
283 'comment_body[0][format]' => 'filtered_html',
285 $this->drupalPostForm(NULL, $edit1, t('Save'));
286 $this->assertEqual('Hello World', Comment::load(1)->getSubject());
288 // If there's nothing other than HTML, the subject should be '(No subject)'.
289 $body_text2 = '<span></span><strong> </strong><span> </span><strong></strong> <br />';
291 'comment_body[0][value]' => $body_text2,
292 'comment_body[0][format]' => 'filtered_html',
294 $this->drupalPostForm(NULL, $edit2, t('Save'));
295 $this->assertEqual('(No subject)', Comment::load(2)->getSubject());
299 * Tests the comment formatter configured with a custom comment view mode.
301 public function testViewMode() {
302 $this->drupalLogin($this->webUser);
303 $this->drupalGet($this->node->toUrl());
304 $comment_text = $this->randomMachineName();
306 $this->postComment($this->node, $comment_text);
308 // Comment displayed in 'default' display mode found and has body text.
309 $comment_element = $this->cssSelect('.comment-wrapper');
310 $this->assertTrue(!empty($comment_element));
311 $this->assertRaw('<p>' . $comment_text . '</p>');
313 // Create a new comment entity view mode.
314 $mode = Unicode::strtolower($this->randomMachineName());
315 EntityViewMode::create([
316 'targetEntityType' => 'comment',
317 'id' => "comment.$mode",
319 // Create the corresponding entity view display for article node-type. Note
320 // that this new view display mode doesn't contain the comment body.
321 EntityViewDisplay::create([
322 'targetEntityType' => 'comment',
323 'bundle' => 'comment',
325 ])->setStatus(TRUE)->save();
327 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */
328 $node_display = EntityViewDisplay::load('node.article.default');
329 $formatter = $node_display->getComponent('comment');
330 // Change the node comment field formatter to use $mode mode instead of
332 $formatter['settings']['view_mode'] = $mode;
334 ->setComponent('comment', $formatter)
337 // Reloading the node page to show the same node with its same comment but
338 // with a different display mode.
339 $this->drupalGet($this->node->toUrl());
340 // The comment should exist but without the body text because we used $mode
342 $comment_element = $this->cssSelect('.comment-wrapper');
343 $this->assertTrue(!empty($comment_element));
344 $this->assertNoRaw('<p>' . $comment_text . '</p>');