// Set "Comments per page" as zero and verify that all comments are appearing
// on the page.
$this->setCommentsPerPage(0); $this->drupalGet('node/' . $node->id()); $this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page.'); $this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page.'); $this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page.');
// Set comments to one per page so that we are able to test paging without
// needing to insert large numbers of comments.
$this->setCommentsPerPage(1);
// Check the first page of the node, and confirm the correct comments are
// shown.
$this->drupalGet('node/' . $node->id()); $this->assertSession()->pageTextContains('next');
$edit['comment_body[0][value]'] = $body; $this->drupalGet($this->node->toUrl()); $this->submitForm($edit, 'Preview'); // Cannot use assertRaw here since both title and body are in the form.
$preview = (string)$this->cssSelect('[data-drupal-selector="edit-comment-preview"]')[0]->getHtml(); $this->assertStringContainsString($title, $preview, 'Anonymous user can preview comment title.'); $this->assertStringContainsString($body, $preview, 'Anonymous user can preview comment body.'); user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
// Post anonymous comment without contact info.
$anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
// Ensure anonymous users cannot post in the name of registered users.
$edit = [ 'name' => $this->adminUser->getAccountName(), 'comment_body[0][value]' => $this->randomMachineName(), ]; $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); $this->submitForm($edit, 'Save'); $this->assertSession()->pageTextContains('The name you used (' . $this->adminUser->getAccountName() . ') belongs to a registered user.');
// Allow contact info.
/**
* Tests the comment interface.
*/ publicfunctiontestCommentInterface(){
// Post comment #1 without subject or preview.
$this->drupalLogin($this->webUser); $comment_text = $this->randomMachineName(); $comment = $this->postComment($this->node, $comment_text); $this->assertTrue($this->commentExists($comment), 'Comment found.');
// Test that using an invalid entity-type does not raise an error.
$this->drupalGet('comment/reply/yeah-this-is-not-an-entity-type/' . $this->node->id() . '/comment/' . $comment->id()); $this->assertSession()->statusCodeEquals(404);
// Test the comment field title is displayed when there's comments.
$this->drupalGet($this->node->toUrl()); $this->assertSession()->responseMatches('@<h2[^>]*>Comments</h2>@');
// Set comments to have subject and preview to required.
$this->drupalLogout();
$comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE); // Confirm that the comment was created and has the correct threading.
$this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.'); $this->assertEquals('01/', $comment1->getThread()); // Confirm that there is no reference to a parent comment.
$this->assertNoParentLink($comment1->id());
// Post comment #2 following the comment #1 to test if it correctly jumps
// out the indentation in case there is a thread above.
$subject_text = $this->randomMachineName(); $comment_text = $this->randomMachineName(); $this->postComment($this->node, $comment_text, $subject_text, TRUE);
// Test breadcrumb on comment add page.
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $this->entity->label());
// Post a comment.
/** @var \Drupal\comment\CommentInterface $comment1 */ $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName()); $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
// Test breadcrumb on comment reply page.
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());
// Test breadcrumb on comment edit page.
$this->drupalGet('comment/' . $comment1->id() . '/edit'); $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());
// Test breadcrumb on comment delete page.
$this->drupalGet('comment/' . $comment1->id() . '/delete');
// Post comment #1 and verify that title is rendered in h3.
$subject_text = $this->randomMachineName(); $comment_text = $this->randomMachineName(); $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
// The entity fields for name and mail have no meaning if the user is not
// Anonymous.
$this->assertNull($comment1->name->value); $this->assertNull($comment1->mail->value);
// Confirm that the comment was created.
$this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.'); // Tests that markup is created for comment with heading.
$this->assertSession()->responseMatches('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|'); // Tests that the comment's title link is the permalink of the comment.
$comment_permalink = $this->cssSelect('.permalink'); $comment_permalink = $comment_permalink[0]->getAttribute('href'); // Tests that the comment's title link contains the URL fragment.
$this->assertStringContainsString('#comment-' . $comment1->id(), $comment_permalink, "The comment's title link contains the url fragment."); $this->assertEquals($comment1->permalink()->toString(), $comment_permalink, "The comment's title has the correct link."); }