Chris@0: drupalLogin($this->adminUser); Chris@0: // Make sure that comment field title is not displayed when there's no Chris@0: // comments posted. Chris@18: $this->drupalGet($this->node->toUrl()); Chris@18: $this->assertSession()->responseNotMatches('@]*>Comments@', 'Comments title is not displayed.'); Chris@0: Chris@0: // Set comments to have subject and preview disabled. Chris@0: $this->setCommentPreview(DRUPAL_DISABLED); Chris@0: $this->setCommentForm(TRUE); Chris@0: $this->setCommentSubject(FALSE); Chris@0: $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); Chris@0: $this->drupalLogout(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the comment interface. Chris@0: */ Chris@0: public function testCommentInterface() { Chris@0: Chris@0: // Post comment #1 without subject or preview. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $comment_text = $this->randomMachineName(); Chris@0: $comment = $this->postComment($this->node, $comment_text); Chris@0: $this->assertTrue($this->commentExists($comment), 'Comment found.'); Chris@0: Chris@0: // Test the comment field title is displayed when there's comments. Chris@18: $this->drupalGet($this->node->toUrl()); Chris@0: $this->assertPattern('@]*>Comments@', 'Comments title is displayed.'); Chris@0: Chris@0: // Set comments to have subject and preview to required. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->setCommentSubject(TRUE); Chris@0: $this->setCommentPreview(DRUPAL_REQUIRED); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Create comment #2 that allows subject and requires preview. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $subject_text = $this->randomMachineName(); Chris@0: $comment_text = $this->randomMachineName(); Chris@0: $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE); Chris@0: $this->assertTrue($this->commentExists($comment), 'Comment found.'); Chris@0: Chris@0: // Comment as anonymous with preview required. Chris@0: $this->drupalLogout(); Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content', 'access comments', 'post comments', 'skip comment approval']); Chris@0: $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE); Chris@0: $this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.'); Chris@0: $anonymous_comment->delete(); Chris@0: Chris@0: // Check comment display. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $this->assertText($subject_text, 'Individual comment subject found.'); Chris@0: $this->assertText($comment_text, 'Individual comment body found.'); Chris@0: $arguments = [ Chris@0: ':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(), Chris@0: ]; Chris@0: $pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]'; Chris@0: $permalink = $this->xpath($pattern_permalink, $arguments); Chris@0: $this->assertTrue(!empty($permalink), 'Permalink link found.'); Chris@0: Chris@0: // Set comments to have subject and preview to optional. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->setCommentSubject(TRUE); Chris@0: $this->setCommentPreview(DRUPAL_OPTIONAL); Chris@0: Chris@0: $this->drupalGet('comment/' . $comment->id() . '/edit'); Chris@0: $this->assertTitle(t('Edit comment @title | Drupal', [ Chris@0: '@title' => $comment->getSubject(), Chris@0: ])); Chris@0: Chris@0: // Test changing the comment author to "Anonymous". Chris@0: $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']); Chris@0: $this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.'); Chris@0: Chris@0: // Test changing the comment author to an unverified user. Chris@0: $random_name = $this->randomMachineName(); Chris@0: $this->drupalGet('comment/' . $comment->id() . '/edit'); Chris@0: $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['name' => $random_name]); Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.'); Chris@0: Chris@0: // Test changing the comment author to a verified user. Chris@0: $this->drupalGet('comment/' . $comment->id() . '/edit'); Chris@18: $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => $this->webUser->getAccountName() . ' (' . $this->webUser->id() . ')']); Chris@18: $this->assertTrue($comment->getAuthorName() == $this->webUser->getAccountName() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.'); Chris@0: Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Reply to comment #2 creating comment #3 with optional preview and no Chris@0: // subject though field enabled. Chris@0: $this->drupalLogin($this->webUser); Chris@0: // Deliberately use the wrong url to test Chris@0: // \Drupal\comment\Controller\CommentController::redirectNode(). Chris@0: $this->drupalGet('comment/' . $this->node->id() . '/reply'); Chris@0: // Verify we were correctly redirected. Chris@18: $this->assertUrl(Url::fromRoute('comment.reply', ['entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment'], ['absolute' => TRUE])->toString()); Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id()); Chris@0: $this->assertText($subject_text, 'Individual comment-reply subject found.'); Chris@0: $this->assertText($comment_text, 'Individual comment-reply body found.'); Chris@0: $reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE); Chris@0: $reply_loaded = Comment::load($reply->id()); Chris@0: $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.'); Chris@0: $this->assertEqual($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.'); Chris@0: // Check the thread of reply grows correctly. Chris@0: $this->assertEqual(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread()); Chris@0: Chris@0: // Second reply to comment #2 creating comment #4. Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id()); Chris@0: $this->assertText($comment->getSubject(), 'Individual comment-reply subject found.'); Chris@0: $this->assertText($comment->comment_body->value, 'Individual comment-reply body found.'); Chris@0: $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); Chris@0: $reply_loaded = Comment::load($reply->id()); Chris@0: $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.'); Chris@0: // Check the thread of second reply grows correctly. Chris@0: $this->assertEqual(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread()); Chris@0: Chris@0: // Reply to comment #4 creating comment #5. Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id()); Chris@0: $this->assertText($reply_loaded->getSubject(), 'Individual comment-reply subject found.'); Chris@0: $this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.'); Chris@0: $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); Chris@0: $reply_loaded = Comment::load($reply->id()); Chris@0: $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.'); Chris@0: // Check the thread of reply to second reply grows correctly. Chris@0: $this->assertEqual(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread()); Chris@0: Chris@0: // Edit reply. Chris@0: $this->drupalGet('comment/' . $reply->id() . '/edit'); Chris@0: $reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); Chris@0: $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.'); Chris@0: Chris@0: // Confirm a new comment is posted to the correct page. Chris@0: $this->setCommentsPerPage(2); Chris@0: $comment_new_page = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE); Chris@0: $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s'); Chris@0: $this->drupalGet('node/' . $this->node->id(), ['query' => ['page' => 2]]); Chris@0: $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s'); Chris@0: $this->setCommentsPerPage(50); Chris@0: Chris@0: // Attempt to reply to an unpublished comment. Chris@17: $reply_loaded->setUnpublished(); Chris@0: $reply_loaded->save(); Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id()); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Attempt to post to node with comments disabled. Chris@0: $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::HIDDEN]]]); Chris@0: $this->assertTrue($this->node, 'Article node created.'); Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); Chris@0: $this->assertResponse(403); Chris@0: $this->assertNoField('edit-comment', 'Comment body field found.'); Chris@0: Chris@0: // Attempt to post to node with read-only comments. Chris@0: $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::CLOSED]]]); Chris@0: $this->assertTrue($this->node, 'Article node created.'); Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); Chris@0: $this->assertResponse(403); Chris@0: $this->assertNoField('edit-comment', 'Comment body field found.'); Chris@0: Chris@0: // Attempt to post to node with comments enabled (check field names etc). Chris@0: $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'comment' => [['status' => CommentItemInterface::OPEN]]]); Chris@0: $this->assertTrue($this->node, 'Article node created.'); Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); Chris@0: $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled'); Chris@0: $this->assertField('edit-comment-body-0-value', 'Comment body field found.'); Chris@0: Chris@0: // Delete comment and make sure that reply is also removed. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->deleteComment($comment); Chris@0: $this->deleteComment($comment_new_page); Chris@0: Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $this->assertFalse($this->commentExists($comment), 'Comment not found.'); Chris@0: $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.'); Chris@0: Chris@0: // Enabled comment form on node page. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->setCommentForm(TRUE); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Submit comment through node form. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $form_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE); Chris@0: $this->assertTrue($this->commentExists($form_comment), 'Form comment found.'); Chris@0: Chris@0: // Disable comment form on node page. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->setCommentForm(FALSE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that the subject is automatically filled if disabled or left blank. Chris@0: * Chris@0: * When the subject field is blank or disabled, the first 29 characters of the Chris@0: * comment body are used for the subject. If this would break within a word, Chris@0: * then the break is put at the previous word boundary instead. Chris@0: */ Chris@0: public function testAutoFilledSubject() { Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: Chris@0: // Break when there is a word boundary before 29 characters. Chris@0: $body_text = 'Lorem ipsum Lorem ipsum Loreming ipsum Lorem ipsum'; Chris@0: $comment1 = $this->postComment(NULL, $body_text, '', TRUE); Chris@0: $this->assertTrue($this->commentExists($comment1), 'Form comment found.'); Chris@0: $this->assertEqual('Lorem ipsum Lorem ipsum…', $comment1->getSubject()); Chris@0: Chris@0: // Break at 29 characters where there's no boundary before that. Chris@0: $body_text2 = 'LoremipsumloremipsumLoremingipsumLoremipsum'; Chris@0: $comment2 = $this->postComment(NULL, $body_text2, '', TRUE); Chris@0: $this->assertEqual('LoremipsumloremipsumLoreming…', $comment2->getSubject()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that automatic subject is correctly created from HTML comment text. Chris@0: * Chris@0: * This is the same test as in CommentInterfaceTest::testAutoFilledSubject() Chris@0: * with the additional check that HTML is stripped appropriately prior to Chris@0: * character-counting. Chris@0: */ Chris@0: public function testAutoFilledHtmlSubject() { Chris@0: // Set up two default (i.e. filtered HTML) input formats, because then we Chris@0: // can select one of them. Then create a user that can use these formats, Chris@0: // log the user in, and then GET the node page on which to test the Chris@0: // comments. Chris@0: $filtered_html_format = FilterFormat::create([ Chris@0: 'format' => 'filtered_html', Chris@0: 'name' => 'Filtered HTML', Chris@0: ]); Chris@0: $filtered_html_format->save(); Chris@0: $full_html_format = FilterFormat::create([ Chris@0: 'format' => 'full_html', Chris@0: 'name' => 'Full HTML', Chris@0: ]); Chris@0: $full_html_format->save(); Chris@0: $html_user = $this->drupalCreateUser([ Chris@0: 'access comments', Chris@0: 'post comments', Chris@0: 'edit own comments', Chris@0: 'skip comment approval', Chris@0: 'access content', Chris@0: $filtered_html_format->getPermissionName(), Chris@0: $full_html_format->getPermissionName(), Chris@0: ]); Chris@0: $this->drupalLogin($html_user); Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: Chris@0: // HTML should not be included in the character count. Chris@0: $body_text1 = ' Hello World
'; Chris@0: $edit1 = [ Chris@0: 'comment_body[0][value]' => $body_text1, Chris@0: 'comment_body[0][format]' => 'filtered_html', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit1, t('Save')); Chris@0: $this->assertEqual('Hello World', Comment::load(1)->getSubject()); Chris@0: Chris@0: // If there's nothing other than HTML, the subject should be '(No subject)'. Chris@0: $body_text2 = '
'; Chris@0: $edit2 = [ Chris@0: 'comment_body[0][value]' => $body_text2, Chris@0: 'comment_body[0][format]' => 'filtered_html', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit2, t('Save')); Chris@0: $this->assertEqual('(No subject)', Comment::load(2)->getSubject()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the comment formatter configured with a custom comment view mode. Chris@0: */ Chris@0: public function testViewMode() { Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet($this->node->toUrl()); Chris@0: $comment_text = $this->randomMachineName(); Chris@0: // Post a comment. Chris@0: $this->postComment($this->node, $comment_text); Chris@0: Chris@0: // Comment displayed in 'default' display mode found and has body text. Chris@0: $comment_element = $this->cssSelect('.comment-wrapper'); Chris@0: $this->assertTrue(!empty($comment_element)); Chris@0: $this->assertRaw('

' . $comment_text . '

'); Chris@0: Chris@0: // Create a new comment entity view mode. Chris@17: $mode = mb_strtolower($this->randomMachineName()); Chris@0: EntityViewMode::create([ Chris@0: 'targetEntityType' => 'comment', Chris@0: 'id' => "comment.$mode", Chris@0: ])->save(); Chris@0: // Create the corresponding entity view display for article node-type. Note Chris@0: // that this new view display mode doesn't contain the comment body. Chris@0: EntityViewDisplay::create([ Chris@0: 'targetEntityType' => 'comment', Chris@0: 'bundle' => 'comment', Chris@0: 'mode' => $mode, Chris@0: ])->setStatus(TRUE)->save(); Chris@0: Chris@0: /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */ Chris@0: $node_display = EntityViewDisplay::load('node.article.default'); Chris@0: $formatter = $node_display->getComponent('comment'); Chris@0: // Change the node comment field formatter to use $mode mode instead of Chris@0: // 'default' mode. Chris@0: $formatter['settings']['view_mode'] = $mode; Chris@0: $node_display Chris@0: ->setComponent('comment', $formatter) Chris@0: ->save(); Chris@0: Chris@0: // Reloading the node page to show the same node with its same comment but Chris@0: // with a different display mode. Chris@0: $this->drupalGet($this->node->toUrl()); Chris@0: // The comment should exist but without the body text because we used $mode Chris@0: // mode this time. Chris@0: $comment_element = $this->cssSelect('.comment-wrapper'); Chris@0: $this->assertTrue(!empty($comment_element)); Chris@0: $this->assertNoRaw('

' . $comment_text . '

'); Chris@0: } Chris@0: Chris@0: }