1 comment.test CommentPreviewTest::testCommentEditPreviewSave()

Tests comment edit, preview, and save.

File

core/modules/comment/tests/comment.test, line 1072
Tests for the Comment module.

Class

CommentPreviewTest
Tests previewing comments.

Code

function testCommentEditPreviewSave() {
  $langcode = LANGUAGE_NONE;
  $web_user = $this->backdropCreateUser(array('access comments', 'post comments', 'skip comment approval'));
  $this->backdropLogin($this->admin_user);
  $this->setCommentPreview(BACKDROP_OPTIONAL);
  $this->setCommentForm(TRUE);
  $this->setCommentSubject(TRUE);
  $this->setCommentSettings('comment_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');

  $edit = array();
  $edit['subject'] = $this->randomName(8);
  $edit['comment_body[' . $langcode . '][0][value]'] = $this->randomName(16);
  $edit['name'] = $web_user->name;
  $edit['date'] = '2008-03-02 17:23 +0300';
  $raw_date = strtotime($edit['date']);
  $expected_text_date = format_date($raw_date, 'medium', NULL, 'UTC');
  $expected_form_date = format_date($raw_date, 'custom', 'Y-m-d H:i O', 'UTC');
  $comment = $this->postComment($this->node, $edit['comment_body[' . $langcode . '][0][value]'], '', TRUE);
  $this->backdropPost('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 | Backdrop CMS'), 'Page title is "Preview comment".');
  $this->assertText($edit['subject'], 'Title 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'], 'Title 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', $edit['date'], 'Date field displayed.');

  // Check that saving a comment produces a success message.
  $this->backdropPost('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->backdropGet('comment/' . $comment->id . '/edit');
  $this->assertFieldByName('subject', $edit['subject'], 'Title 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', $expected_form_date, 'Date 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'] = (string) current($this->xpath("//input[@id='edit-date']/@value"));
  $this->backdropPost('comment/' . $comment->id . '/edit', $displayed, t('Save'));

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

}