annotate core/modules/comment/tests/src/Functional/CommentInterfaceTest.php @ 19:fa3358dc1485 tip

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