Same name and namespace in other branches
  1. 8.9.x core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testLineBreakFilter()
  2. 9 core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testLineBreakFilter()

Tests the line break filter.

File

core/modules/filter/tests/src/Kernel/FilterKernelTest.php, line 407

Class

FilterKernelTest
Tests Filter module filters individually.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testLineBreakFilter() {

  // Get FilterAutoP object.
  $filter = $this->filters['filter_autop'];

  // Since the line break filter naturally needs plenty of newlines in test
  // strings and expectations, we're using "\n" instead of regular newlines
  // here.
  // cSpell:disable
  $tests = [
    // Single line breaks should be changed to <br /> tags, while paragraphs
    // separated with double line breaks should be enclosed with <p></p> tags.
    "aaa\nbbb\n\nccc" => [
      "<p>aaa<br />\nbbb</p>\n<p>ccc</p>" => TRUE,
    ],
    // Skip contents of certain block tags entirely.
    "<script>aaa\nbbb\n\nccc</script>\n      <style>aaa\nbbb\n\nccc</style>\n      <pre>aaa\nbbb\n\nccc</pre>\n      <object>aaa\nbbb\n\nccc</object>\n      <iframe>aaa\nbbb\n\nccc</iframe>\n      <svg>aaa\nbbb\n\nccc</svg>" => [
      "<script>aaa\nbbb\n\nccc</script>" => TRUE,
      "<style>aaa\nbbb\n\nccc</style>" => TRUE,
      "<pre>aaa\nbbb\n\nccc</pre>" => TRUE,
      "<object>aaa\nbbb\n\nccc</object>" => TRUE,
      "<iframe>aaa\nbbb\n\nccc</iframe>" => TRUE,
      "<svg>aaa\nbbb\n\nccc</svg>" => TRUE,
    ],
    // Skip comments entirely.
    "One. <!-- comment --> Two.\n<!--\nThree.\n-->\n" => [
      '<!-- comment -->' => TRUE,
      "<!--\nThree.\n-->" => TRUE,
    ],
    // Resulting HTML should produce matching paragraph tags.
    '<p><div>  </div></p>' => [
      "<p>\n<div>  </div>\n</p>" => TRUE,
    ],
    '<div><p>  </p></div>' => [
      "<div>\n</div>" => TRUE,
    ],
    '<blockquote><pre>aaa</pre></blockquote>' => [
      "<blockquote><pre>aaa</pre></blockquote>" => TRUE,
    ],
    "<pre>aaa\nbbb\nccc</pre>\nddd\neee" => [
      "<pre>aaa\nbbb\nccc</pre>" => TRUE,
      "<p>ddd<br />\neee</p>" => TRUE,
    ],
    // Comments remain unchanged and subsequent lines/paragraphs are
    // transformed normally.
    "aaa<!--comment-->\n\nbbb\n\nccc\n\nddd<!--comment\nwith linebreak-->\n\neee\n\nfff" => [
      "<p>aaa</p>\n<!--comment--><p>\nbbb</p>\n<p>ccc</p>\n<p>ddd</p>" => TRUE,
      "<!--comment\nwith linebreak--><p>\neee</p>\n<p>fff</p>" => TRUE,
    ],
    // Check that a comment in a PRE will result that the text after
    // the comment, but still in PRE, is not transformed.
    "<pre>aaa\nbbb<!-- comment -->\n\nccc</pre>\nddd" => [
      "<pre>aaa\nbbb<!-- comment -->\n\nccc</pre>" => TRUE,
    ],
    // Bug 810824, paragraphs were appearing around iframe tags.
    "<iframe>aaa</iframe>\n\n" => [
      "<p><iframe>aaa</iframe></p>" => FALSE,
    ],
    // Bug 3097338, paragraphs were appearing around drupalmedia tags.
    '<drupal-media data-caption=" " data-entity-type="media" data-entity-uuid="dbb16f97-cd11-4357-acde-cd09e19e312b"></drupal-media>' => [
      '<p><drupal-media data-caption=" " data-entity-type="media" data-entity-uuid="dbb16f97-cd11-4357-acde-cd09e19e312b"></drupal-media></p>' => FALSE,
    ],
  ];

  // cSpell:enable
  $this
    ->assertFilteredString($filter, $tests);

  // Very long string hitting PCRE limits.
  $limit = max((int) ini_get('pcre.backtrack_limit'), (int) ini_get('pcre.recursion_limit'));
  $source = $this
    ->randomMachineName($limit);
  $result = _filter_autop($source);
  $this
    ->assertEquals($result, '<p>' . $source . "</p>\n", 'Line break filter can process very long strings.');
}