function CommentPreviewTest::testCommentEditPreviewSave

Tests comment edit, preview, and save.

File

drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentPreviewTest.php, line 82
Definition of Drupal\comment\Tests\CommentPreviewTest.

Class

CommentPreviewTest
Tests previewing comments.

Namespace

Drupal\comment\Tests

Code

function testCommentEditPreviewSave() {
  $langcode = Language::LANGCODE_NOT_SPECIFIED;
  $web_user = $this
    ->drupalCreateUser(array(
    'access comments',
    'post comments',
    'skip comment approval',
  ));
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->setCommentPreview(DRUPAL_OPTIONAL);
  $this
    ->setCommentForm(TRUE);
  $this
    ->setCommentSubject(TRUE);
  $this
    ->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
  $edit = array();
  $date = new DrupalDateTime('2008-03-02 17:23');
  $edit['subject'] = $this
    ->randomName(8);
  $edit['comment_body[' . $langcode . '][0][value]'] = $this
    ->randomName(16);
  $edit['name'] = $web_user->name;
  $edit['date[date]'] = $date
    ->format('Y-m-d');
  $edit['date[time]'] = $date
    ->format('H:i:s');
  $raw_date = $date
    ->getTimestamp();
  $expected_text_date = format_date($raw_date);
  $expected_form_date = $date
    ->format('Y-m-d');
  $expected_form_time = $date
    ->format('H:i:s');
  $comment = $this
    ->postComment($this->node, $edit['subject'], $edit['comment_body[' . $langcode . '][0][value]'], TRUE);
  $this
    ->drupalPost('comment/' . $comment
    ->id() . '/edit', $edit, t('Preview'));

  // Check that the preview is displaying the subject, comment, author and date correctly.
  $this
    ->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
  $this
    ->assertText($edit['subject'], 'Subject displayed.');
  $this
    ->assertText($edit['comment_body[' . $langcode . '][0][value]'], 'Comment displayed.');
  $this
    ->assertText($edit['name'], 'Author displayed.');
  $this
    ->assertText($expected_text_date, 'Date displayed.');

  // Check that the subject, comment, author and date fields are displayed with the correct values.
  $this
    ->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
  $this
    ->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], 'Comment field displayed.');
  $this
    ->assertFieldByName('name', $edit['name'], 'Author field displayed.');
  $this
    ->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
  $this
    ->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');

  // Check that saving a comment produces a success message.
  $this
    ->drupalPost('comment/' . $comment
    ->id() . '/edit', $edit, t('Save'));
  $this
    ->assertText(t('Your comment has been posted.'), 'Comment posted.');

  // Check that the comment fields are correct after loading the saved comment.
  $this
    ->drupalGet('comment/' . $comment
    ->id() . '/edit');
  $this
    ->assertFieldByName('subject', $edit['subject'], 'Subject field displayed.');
  $this
    ->assertFieldByName('comment_body[' . $langcode . '][0][value]', $edit['comment_body[' . $langcode . '][0][value]'], 'Comment field displayed.');
  $this
    ->assertFieldByName('name', $edit['name'], 'Author field displayed.');
  $this
    ->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
  $this
    ->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');

  // Submit the form using the displayed values.
  $displayed = array();
  $displayed['subject'] = (string) current($this
    ->xpath("//input[@id='edit-subject']/@value"));
  $displayed['comment_body[' . $langcode . '][0][value]'] = (string) current($this
    ->xpath("//textarea[@id='edit-comment-body-" . $langcode . "-0-value']"));
  $displayed['name'] = (string) current($this
    ->xpath("//input[@id='edit-name']/@value"));
  $displayed['date[date]'] = (string) current($this
    ->xpath("//input[@id='edit-date-date']/@value"));
  $displayed['date[time]'] = (string) current($this
    ->xpath("//input[@id='edit-date-time']/@value"));
  $this
    ->drupalPost('comment/' . $comment
    ->id() . '/edit', $displayed, t('Save'));

  // Check that the saved comment is still correct.
  $comment_loaded = comment_load($comment
    ->id(), TRUE);
  $this
    ->assertEqual($comment_loaded->subject->value, $edit['subject'], 'Subject loaded.');
  $this
    ->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[' . $langcode . '][0][value]'], 'Comment body loaded.');
  $this
    ->assertEqual($comment_loaded->name->value, $edit['name'], 'Name loaded.');
  $this
    ->assertEqual($comment_loaded->created->value, $raw_date, 'Date loaded.');
}