View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentManagerInterface;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\comment\Entity\Comment;
use Drupal\Tests\TestFileCreationTrait;
class CommentPreviewTest extends CommentTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected $profile = 'standard';
public function testCommentPreview() {
$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();
$this
->drupalLogin($this->webUser);
\Drupal::service('module_installer')
->install([
'user_hooks_test',
]);
\Drupal::state()
->set('user_hooks_test_user_format_name_alter', TRUE);
$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'));
$this
->assertEscaped('<em>' . $this->webUser
->id() . '</em>');
\Drupal::state()
->set('user_hooks_test_user_format_name_alter_safe', TRUE);
$this
->drupalPostForm('node/' . $this->node
->id(), $edit, t('Preview'));
$this
->assertInstanceOf(MarkupInterface::class, $this->webUser
->getDisplayName());
$this
->assertNoEscaped('<em>' . $this->webUser
->id() . '</em>');
$this
->assertRaw('<em>' . $this->webUser
->id() . '</em>');
$image = current($this
->drupalGetTestFiles('image'));
$user_edit['files[user_picture_0]'] = \Drupal::service('file_system')
->realpath($image->uri);
$this
->drupalPostForm('user/' . $this->webUser
->id() . '/edit', $user_edit, t('Save'));
$this
->drupalPostForm('node/' . $this->node
->id(), $edit, t('Preview'));
$this
->assertTitle('Preview comment | Drupal');
$this
->assertText($edit['subject[0][value]'], 'Subject displayed.');
$this
->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
$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.');
$this
->assertFieldByXPath("//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
}
public function testCommentPreviewDuplicateSubmission() {
$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();
$this
->drupalLogin($this->webUser);
$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'));
$this
->assertTitle('Preview comment | Drupal');
$this
->assertText($edit['subject[0][value]'], 'Subject displayed.');
$this
->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
$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.');
$this
->drupalPostForm(NULL, [], 'Save');
$this
->assertText('Your comment has been posted.');
$elements = $this
->xpath('//section[contains(@class, "comment-wrapper")]/article');
$this
->assertCount(1, $elements);
$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);
}
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
->drupalPostForm('comment/' . $comment
->id() . '/edit', $edit, t('Preview'));
$this
->assertTitle('Preview comment | Drupal');
$this
->assertText($edit['subject[0][value]'], 'Subject displayed.');
$this
->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
$this
->assertText($web_user
->getAccountName(), 'Author displayed.');
$this
->assertText($expected_text_date, 'Date displayed.');
$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.');
$this
->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
$this
->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
$this
->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
$this
->drupalPostForm('comment/' . $comment
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('Your comment has been posted.'), 'Comment posted.');
$this
->drupalGet('comment/' . $comment
->id() . '/edit');
$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.');
$this
->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
$this
->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
$this
->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
$displayed = [];
$displayed['subject[0][value]'] = current($this
->xpath("//input[@id='edit-subject-0-value']"))
->getValue();
$displayed['comment_body[0][value]'] = current($this
->xpath("//textarea[@id='edit-comment-body-0-value']"))
->getValue();
$displayed['uid'] = current($this
->xpath("//input[@id='edit-uid']"))
->getValue();
$displayed['date[date]'] = current($this
->xpath("//input[@id='edit-date-date']"))
->getValue();
$displayed['date[time]'] = current($this
->xpath("//input[@id='edit-date-time']"))
->getValue();
$this
->drupalPostForm('comment/' . $comment
->id() . '/edit', $displayed, t('Save'));
$comment_storage = \Drupal::entityTypeManager()
->getStorage('comment');
$comment_storage
->resetCache([
$comment
->id(),
]);
$comment_loaded = Comment::load($comment
->id());
$this
->assertEqual($comment_loaded
->getSubject(), $edit['subject[0][value]'], 'Subject loaded.');
$this
->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
$this
->assertEqual($comment_loaded
->getOwner()
->id(), $web_user
->id(), 'Name loaded.');
$this
->assertEqual($comment_loaded
->getCreatedTime(), $raw_date, 'Date loaded.');
$this
->drupalLogout();
$user_edit = [];
$expected_created_time = $comment_loaded
->getCreatedTime();
$this
->drupalLogin($web_user);
unset($edit['uid']);
$this
->drupalPostForm('comment/' . $comment
->id() . '/edit', $user_edit, t('Save'));
$comment_storage
->resetCache([
$comment
->id(),
]);
$comment_loaded = Comment::load($comment
->id());
$this
->assertEqual($comment_loaded
->getCreatedTime(), $expected_created_time, 'Expected date and time for comment edited.');
$this
->drupalLogout();
}
}