You are here

function CommentPreviewTest::testCommentPreview in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/comment/src/Tests/CommentPreviewTest.php \Drupal\comment\Tests\CommentPreviewTest::testCommentPreview()

Tests comment preview.

File

core/modules/comment/src/Tests/CommentPreviewTest.php, line 34
Contains \Drupal\comment\Tests\CommentPreviewTest.

Class

CommentPreviewTest
Tests comment preview.

Namespace

Drupal\comment\Tests

Code

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();

  // Login 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 = array();
  $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
    ->assertTrue(SafeMarkup::isSafe($this->webUser
    ->getDisplayName()), 'Username is marked safe');
  $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_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(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
  $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.');
}