function FilterKernelTest::testHtmlCorrectorFilter

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

Tests the HTML corrector filter.

@todo This test could really use some validity checking function.

File

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

Class

FilterKernelTest
Tests Filter module filters individually.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testHtmlCorrectorFilter() {
    // Tag closing.
    $f = Html::normalize('<p>text');
    $this->assertEquals('<p>text</p>', $f, 'HTML corrector -- tag closing at the end of input.');
    $f = Html::normalize('<p>text<p><p>text');
    $this->assertEquals('<p>text</p><p></p><p>text</p>', $f, 'HTML corrector -- tag closing.');
    $f = Html::normalize("<ul><li>e1<li>e2");
    $this->assertEquals("<ul><li>e1</li><li>e2</li></ul>", $f, 'HTML corrector -- unclosed list tags.');
    $f = Html::normalize('<div id="d">content');
    $this->assertEquals('<div id="d">content</div>', $f, 'HTML corrector -- unclosed tag with attribute.');
    // XHTML slash for empty elements.
    $f = Html::normalize('<hr><br>');
    $this->assertEquals('<hr /><br />', $f, 'HTML corrector -- XHTML closing slash.');
    $f = Html::normalize('<P>test</P>');
    $this->assertEquals('<p>test</p>', $f, 'HTML corrector -- Convert uppercased tags to proper lowercased ones.');
    $f = Html::normalize('<P>test</p>');
    $this->assertEquals('<p>test</p>', $f, 'HTML corrector -- Convert uppercased tags to proper lowercased ones.');
    $f = Html::normalize('test<hr />');
    $this->assertEquals('test<hr />', $f, 'HTML corrector -- Let proper XHTML pass through.');
    $f = Html::normalize('test<hr/>');
    $this->assertEquals('test<hr />', $f, 'HTML corrector -- Let proper XHTML pass through, but ensure there is a single space before the closing slash.');
    $f = Html::normalize('test<hr    />');
    $this->assertEquals('test<hr />', $f, 'HTML corrector -- Let proper XHTML pass through, but ensure there are not too many spaces before the closing slash.');
    $f = Html::normalize('<span class="test" />');
    $this->assertEquals('<span class="test"></span>', $f, 'HTML corrector -- Convert XHTML that is properly formed but that would not be compatible with typical HTML user agents.');
    $f = Html::normalize('test1<br class="test">test2');
    $this->assertEquals('test1<br class="test" />test2', $f, 'HTML corrector -- Automatically close single tags.');
    $f = Html::normalize('line1<hr>line2');
    $this->assertEquals('line1<hr />line2', $f, 'HTML corrector -- Automatically close single tags.');
    $f = Html::normalize('line1<HR>line2');
    $this->assertEquals('line1<hr />line2', $f, 'HTML corrector -- Automatically close single tags.');
    $f = Html::normalize('<img src="http://example.com/test.jpg">test</img>');
    $this->assertEquals('<img src="http://example.com/test.jpg" />test', $f, 'HTML corrector -- Automatically close single tags.');
    $f = Html::normalize('<br></br>');
    $this->assertEquals('<br />', $f, "HTML corrector -- Transform empty tags to a single closed tag if the tag's content model is EMPTY.");
    $f = Html::normalize('<div></div>');
    $this->assertEquals('<div></div>', $f, "HTML corrector -- Do not transform empty tags to a single closed tag if the tag's content model is not EMPTY.");
    $f = Html::normalize('<p>line1<br/><hr/>line2</p>');
    $this->assertEquals('<p>line1<br /></p><hr />line2', $f, 'HTML corrector -- Move non-inline elements outside of inline containers.');
    $f = Html::normalize('<p>line1<div>line2</div></p>');
    $this->assertEquals('<p>line1</p><div>line2</div>', $f, 'HTML corrector -- Move non-inline elements outside of inline containers.');
    $f = Html::normalize('<p>test<p>test</p>\\n');
    $this->assertEquals('<p>test</p><p>test</p>\\n', $f, 'HTML corrector -- Auto-close improperly nested tags.');
    $f = Html::normalize('<p>Line1<br><STRONG>bold stuff</b>');
    $this->assertEquals('<p>Line1<br /><strong>bold stuff</strong></p>', $f, 'HTML corrector -- Properly close unclosed tags, and remove useless closing tags.');
    $f = Html::normalize('test <!-- this is a comment -->');
    $this->assertEquals('test <!-- this is a comment -->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!--this is a comment-->');
    $this->assertEquals('test <!--this is a comment-->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!-- comment <p>another
    <strong>multiple</strong> line
    comment</p> -->');
    $this->assertEquals('test <!-- comment <p>another
    <strong>multiple</strong> line
    comment</p> -->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!-- comment <p>another comment</p> -->');
    $this->assertEquals('test <!-- comment <p>another comment</p> -->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!--break-->');
    $this->assertEquals('test <!--break-->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('<p>test\\n</p>\\n');
    $this->assertEquals('<p>test\\n</p>\\n', $f, 'HTML corrector -- New-lines are accepted and kept as-is.');
    // cSpell:disable
    $f = Html::normalize('<p>دروبال');
    $this->assertEquals('<p>دروبال</p>', $f, 'HTML corrector -- Encoding is correctly kept.');
    // cSpell:enable
    $f = Html::normalize('<script>alert("test")</script>');
    $this->assertEquals('<script>
<!--//--><![CDATA[// ><!--
alert("test")
//--><!]]>
</script>', $f, 'HTML corrector -- CDATA added to script element');
    $f = Html::normalize('<p><script>alert("test")</script></p>');
    $this->assertEquals('<p><script>
<!--//--><![CDATA[// ><!--
alert("test")
//--><!]]>
</script></p>', $f, 'HTML corrector -- CDATA added to a nested script element');
    $f = Html::normalize('<p><style> /* Styling */ body {color:red}</style></p>');
    $this->assertEquals('<p><style>
<!--/*--><![CDATA[/* ><!--*/
 /* Styling */ body {color:red}
/*--><!]]>*/
</style></p>', $f, 'HTML corrector -- CDATA added to a style element.');
    $filtered_data = Html::normalize('<p><style>
/*<![CDATA[*/
/* Styling */
body {color:red}
/*]]>*/
</style></p>');
    $this->assertEquals('<p><style>
<!--/*--><![CDATA[/* ><!--*/

/*<![CDATA[*/
/* Styling */
body {color:red}
/*]]]]><![CDATA[>*/

/*--><!]]>*/
</style></p>', $filtered_data, new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
        '@pattern_name' => '/*<![CDATA[*/',
    ]));
    $filtered_data = Html::normalize('<p><style>
  <!--/*--><![CDATA[/* ><!--*/
  /* Styling */
  body {color:red}
  /*--><!]]>*/
</style></p>');
    $this->assertEquals('<p><style>
<!--/*--><![CDATA[/* ><!--*/

  <!--/*--><![CDATA[/* ><!--*/
  /* Styling */
  body {color:red}
  /*--><!]]]]><![CDATA[>*/

/*--><!]]>*/
</style></p>', $filtered_data, new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
        '@pattern_name' => '<!--/*--><![CDATA[/* ><!--*/',
    ]));
    $filtered_data = Html::normalize('<p><script>
<!--//--><![CDATA[// ><!--
  alert("test");
//--><!]]>
</script></p>');
    $this->assertEquals('<p><script>
<!--//--><![CDATA[// ><!--

<!--//--><![CDATA[// ><!--
  alert("test");
//--><!]]]]><![CDATA[>

//--><!]]>
</script></p>', $filtered_data, new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
        '@pattern_name' => '<!--//--><![CDATA[// ><!--',
    ]));
    $filtered_data = Html::normalize('<p><script>
// <![CDATA[
  alert("test");
// ]]>
</script></p>');
    $this->assertEquals('<p><script>
<!--//--><![CDATA[// ><!--

// <![CDATA[
  alert("test");
// ]]]]><![CDATA[>

//--><!]]>
</script></p>', $filtered_data, new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
        '@pattern_name' => '// <![CDATA[',
    ]));
}

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