annotate core/modules/comment/tests/src/Functional/CommentInterfaceTest.php @ 4:a9cd425dd02b

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