public function CommentPreviewTest::testCommentEditPreviewSave in Drupal 9
Same name and namespace in other branches
- 8 core/modules/comment/tests/src/Functional/CommentPreviewTest.php \Drupal\Tests\comment\Functional\CommentPreviewTest::testCommentEditPreviewSave()
Tests comment edit, preview, and save.
File
- core/
modules/ comment/ tests/ src/ Functional/ CommentPreviewTest.php, line 138
Class
- CommentPreviewTest
- Tests comment preview.
Namespace
Drupal\Tests\comment\FunctionalCode
public function testCommentEditPreviewSave() {
$web_user = $this
->drupalCreateUser([
'access comments',
'post comments',
'skip comment approval',
'edit own comments',
]);
$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.');
$edit = [];
$date = new DrupalDateTime('2008-03-02 17:23');
$edit['subject[0][value]'] = $this
->randomMachineName(8);
$edit['comment_body[0][value]'] = $this
->randomMachineName(16);
$edit['uid'] = $web_user
->getAccountName() . ' (' . $web_user
->id() . ')';
$edit['date[date]'] = $date
->format('Y-m-d');
$edit['date[time]'] = $date
->format('H:i:s');
$raw_date = $date
->getTimestamp();
$expected_text_date = $this->container
->get('date.formatter')
->format($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[0][value]'], $edit['comment_body[0][value]'], TRUE);
$this
->drupalGet('comment/' . $comment
->id() . '/edit');
$this
->submitForm($edit, 'Preview');
// Check that the preview is displaying the subject, comment, author and date correctly.
$this
->assertSession()
->titleEquals('Preview comment | Drupal');
$this
->assertSession()
->pageTextContains($edit['subject[0][value]']);
$this
->assertSession()
->pageTextContains($edit['comment_body[0][value]']);
$this
->assertSession()
->pageTextContains($web_user
->getAccountName());
$this
->assertSession()
->pageTextContains($expected_text_date);
// Check that the subject, comment, author and date 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]']);
$this
->assertSession()
->fieldValueEquals('uid', $edit['uid']);
$this
->assertSession()
->fieldValueEquals('date[date]', $edit['date[date]']);
$this
->assertSession()
->fieldValueEquals('date[time]', $edit['date[time]']);
// Check that saving a comment produces a success message.
$this
->drupalGet('comment/' . $comment
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Your comment has been posted.');
// Check that the comment fields are correct after loading the saved comment.
$this
->drupalGet('comment/' . $comment
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
$this
->assertSession()
->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
$this
->assertSession()
->fieldValueEquals('uid', $edit['uid']);
$this
->assertSession()
->fieldValueEquals('date[date]', $expected_form_date);
$this
->assertSession()
->fieldValueEquals('date[time]', $expected_form_time);
// Submit the form using the displayed values.
$displayed = [];
$displayed['subject[0][value]'] = $this
->assertSession()
->fieldExists('edit-subject-0-value')
->getValue();
$displayed['comment_body[0][value]'] = $this
->assertSession()
->fieldExists('edit-comment-body-0-value')
->getValue();
$displayed['uid'] = $this
->assertSession()
->fieldExists('edit-uid')
->getValue();
$displayed['date[date]'] = $this
->assertSession()
->fieldExists('edit-date-date')
->getValue();
$displayed['date[time]'] = $this
->assertSession()
->fieldExists('edit-date-time')
->getValue();
$this
->drupalGet('comment/' . $comment
->id() . '/edit');
$this
->submitForm($displayed, 'Save');
// Check that the saved comment is still correct.
$comment_storage = \Drupal::entityTypeManager()
->getStorage('comment');
$comment_storage
->resetCache([
$comment
->id(),
]);
/** @var \Drupal\comment\CommentInterface $comment_loaded */
$comment_loaded = Comment::load($comment
->id());
$this
->assertEquals($edit['subject[0][value]'], $comment_loaded
->getSubject(), 'Subject loaded.');
$this
->assertEquals($edit['comment_body[0][value]'], $comment_loaded->comment_body->value, 'Comment body loaded.');
$this
->assertEquals($web_user
->id(), $comment_loaded
->getOwner()
->id(), 'Name loaded.');
$this
->assertEquals($raw_date, $comment_loaded
->getCreatedTime(), 'Date loaded.');
$this
->drupalLogout();
// Check that the date and time of the comment are correct when edited by
// non-admin users.
$user_edit = [];
$expected_created_time = $comment_loaded
->getCreatedTime();
$this
->drupalLogin($web_user);
// Web user cannot change the comment author.
unset($edit['uid']);
$this
->drupalGet('comment/' . $comment
->id() . '/edit');
$this
->submitForm($user_edit, 'Save');
$comment_storage
->resetCache([
$comment
->id(),
]);
$comment_loaded = Comment::load($comment
->id());
$this
->assertEquals($expected_created_time, $comment_loaded
->getCreatedTime(), 'Expected date and time for comment edited.');
$this
->drupalLogout();
}