function CommentPreviewTest::testCommentPreview

Tests comment preview.

File

core/modules/comment/tests/src/Functional/CommentPreviewTest.php, line 34

Class

CommentPreviewTest
Tests comment preview.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentPreview() {
  // 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);
  // Test escaping of the username on the preview form.
  \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>');
  // Add a user picture.
  $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'));
  // As the web user, fill in the comment form and preview the comment.
  $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.');
  // Check that the user picture is displayed.
  $this->assertFieldByXPath("//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
}

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