function CommentPreviewTest::testCommentPreviewDuplicateSubmission

Same name and namespace in other branches
  1. 11.x core/modules/comment/tests/src/Functional/CommentPreviewTest.php \Drupal\Tests\comment\Functional\CommentPreviewTest::testCommentPreviewDuplicateSubmission()
  2. 10 core/modules/comment/tests/src/Functional/CommentPreviewTest.php \Drupal\Tests\comment\Functional\CommentPreviewTest::testCommentPreviewDuplicateSubmission()
  3. 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 85

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->drupalPostForm('node/' . $this->node
    ->id(), $edit, t('Preview'));
  // Check that the preview is displaying the title and body.
  $this->assertTitle('Preview comment | Drupal');
  $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
  $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
  // Check that the title and body fields are displayed with the correct values.
  $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
  $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
  // Store the content of this page.
  $this->drupalPostForm(NULL, [], 'Save');
  $this->assertText('Your comment has been posted.');
  $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/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->assertText('Your comment has been posted.');
  $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
  $this->assertCount(2, $elements);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.