You are here

public function CommentPreviewTest::testCommentPreviewDuplicateSubmission in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Functional/CommentPreviewTest.php \Drupal\Tests\comment\Functional\CommentPreviewTest::testCommentPreviewDuplicateSubmission()
  2. 9 core/modules/comment/tests/src/Functional/CommentPreviewTest.php \Drupal\Tests\comment\Functional\CommentPreviewTest::testCommentPreviewDuplicateSubmission()

Tests comment preview.

File

core/modules/comment/tests/src/Functional/CommentPreviewTest.php, line 93

Class

CommentPreviewTest
Tests comment preview.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentPreviewDuplicateSubmission() {

  // As admin user, configure comment settings.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->setCommentPreview(DRUPAL_OPTIONAL);
  $this
    ->setCommentForm(TRUE);
  $this
    ->setCommentSubject(TRUE);
  $this
    ->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
  $this
    ->drupalLogout();

  // Log in as web user.
  $this
    ->drupalLogin($this->webUser);

  // As the web user, fill in the comment form and preview the comment.
  $edit = [];
  $edit['subject[0][value]'] = $this
    ->randomMachineName(8);
  $edit['comment_body[0][value]'] = $this
    ->randomMachineName(16);
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $this
    ->submitForm($edit, 'Preview');

  // Check that the preview is displaying the title and body.
  $this
    ->assertSession()
    ->titleEquals('Preview comment | Drupal');
  $this
    ->assertSession()
    ->pageTextContains($edit['subject[0][value]']);
  $this
    ->assertSession()
    ->pageTextContains($edit['comment_body[0][value]']);

  // Check that the title and body fields are displayed with the correct values.
  $this
    ->assertSession()
    ->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
  $this
    ->assertSession()
    ->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);

  // Store the content of this page.
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your comment has been posted.');
  $elements = $this
    ->xpath('//section[contains(@class, "comments")]/article');
  $this
    ->assertCount(1, $elements);

  // Go back and re-submit the form.
  $this
    ->getSession()
    ->getDriver()
    ->back();
  $submit_button = $this
    ->assertSession()
    ->buttonExists('Save');
  $submit_button
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('Your comment has been posted.');
  $this
    ->assertSession()
    ->elementsCount('xpath', '//section[contains(@class, "comments")]/article', 2);
}