You are here

public function CommentInterfaceTest::testAutoFilledHtmlSubject in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Functional/CommentInterfaceTest.php \Drupal\Tests\comment\Functional\CommentInterfaceTest::testAutoFilledHtmlSubject()

Tests that automatic subject is correctly created from HTML comment text.

This is the same test as in CommentInterfaceTest::testAutoFilledSubject() with the additional check that HTML is stripped appropriately prior to character-counting.

File

core/modules/comment/tests/src/Functional/CommentInterfaceTest.php, line 262

Class

CommentInterfaceTest
Tests comment user interfaces.

Namespace

Drupal\Tests\comment\Functional

Code

public function testAutoFilledHtmlSubject() {

  // Set up two default (i.e. filtered HTML) input formats, because then we
  // can select one of them. Then create a user that can use these formats,
  // log the user in, and then GET the node page on which to test the
  // comments.
  $filtered_html_format = FilterFormat::create([
    'format' => 'filtered_html',
    'name' => 'Filtered HTML',
  ]);
  $filtered_html_format
    ->save();
  $full_html_format = FilterFormat::create([
    'format' => 'full_html',
    'name' => 'Full HTML',
  ]);
  $full_html_format
    ->save();
  $html_user = $this
    ->drupalCreateUser([
    'access comments',
    'post comments',
    'edit own comments',
    'skip comment approval',
    'access content',
    $filtered_html_format
      ->getPermissionName(),
    $full_html_format
      ->getPermissionName(),
  ]);
  $this
    ->drupalLogin($html_user);
  $this
    ->drupalGet('node/' . $this->node
    ->id());

  // HTML should not be included in the character count.
  $body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />';
  $edit1 = [
    'comment_body[0][value]' => $body_text1,
    'comment_body[0][format]' => 'filtered_html',
  ];
  $this
    ->submitForm($edit1, 'Save');
  $this
    ->assertEquals('Hello World', Comment::load(1)
    ->getSubject());

  // If there's nothing other than HTML, the subject should be '(No subject)'.
  $body_text2 = '<span></span><strong> </strong><span> </span><strong></strong> <br />';
  $edit2 = [
    'comment_body[0][value]' => $body_text2,
    'comment_body[0][format]' => 'filtered_html',
  ];
  $this
    ->submitForm($edit2, 'Save');
  $this
    ->assertEquals('(No subject)', Comment::load(2)
    ->getSubject());
}