3 namespace Drupal\Tests\filter\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\filter\Plugin\Filter\FilterHtml;
9 * @coversDefaultClass \Drupal\filter\Plugin\Filter\FilterHtml
12 class FilterHtmlTest extends UnitTestCase {
15 * @var \Drupal\filter\Plugin\Filter\FilterHtml
22 protected function setUp() {
24 $configuration['settings'] = [
25 'allowed_html' => '<a href> <p> <em> <strong> <cite> <blockquote> <code class="pretty boring align-*"> <ul alpaca-*="wooly-* strong"> <ol llama-*> <li> <dl> <dt> <dd> <br> <h3 id>',
26 'filter_html_help' => 1,
27 'filter_html_nofollow' => 0,
29 $this->filter = new FilterHtml($configuration, 'filter_html', ['provider' => 'test']);
30 $this->filter->setStringTranslation($this->getStringTranslationStub());
34 * @covers ::filterAttributes
36 * @dataProvider providerFilterAttributes
40 * @param array $expected
41 * The expected output string.
43 public function testfilterAttributes($html, $expected) {
44 $this->assertSame($expected, $this->filter->filterAttributes($html));
48 * Provides data for testfilterAttributes.
51 * An array of test data.
53 public function providerFilterAttributes() {
55 ['<a href="/blog" title="Blog">Blog</a>', '<a href="/blog">Blog</a>'],
56 ['<p dir="rtl" />', '<p dir="rtl"></p>'],
57 ['<p dir="bogus" />', '<p></p>'],
58 ['<p id="first" />', '<p></p>'],
59 // The addition of xml:lang isn't especially desired, but is still valid
60 // HTML5. See https://www.drupal.org/node/1333730.
61 ['<p id="first" lang="en">text</p>', '<p lang="en" xml:lang="en">text</p>'],
62 ['<p style="display: none;" />', '<p></p>'],
63 ['<code class="pretty invalid">foreach ($a as $b) {}</code>', '<code class="pretty">foreach ($a as $b) {}</code>'],
64 ['<code class="boring pretty">foreach ($a as $b) {}</code>', '<code class="boring pretty">foreach ($a as $b) {}</code>'],
65 ['<code class="boring pretty ">foreach ($a as $b) {}</code>', '<code class="boring pretty">foreach ($a as $b) {}</code>'],
66 ['<code class="invalid alpaca">foreach ($a as $b) {}</code>', '<code>foreach ($a as $b) {}</code>'],
67 ['<h3 class="big">a heading</h3>', '<h3>a heading</h3>'],
68 ['<h3 id="first">a heading</h3>', '<h3 id="first">a heading</h3>'],
69 // Wildcard value. Case matters, so upper case doesn't match.
70 ['<code class="align-left bold">foreach ($a as $b) {}</code>', '<code class="align-left">foreach ($a as $b) {}</code>'],
71 ['<code class="align-right ">foreach ($a as $b) {}</code>', '<code class="align-right">foreach ($a as $b) {}</code>'],
72 ['<code class="Align-right ">foreach ($a as $b) {}</code>', '<code>foreach ($a as $b) {}</code>'],
73 // Wildcard name, case is ignored.
74 ['<ol style="display: none;" llama-wim="noble majestic"></ol>', '<ol llama-wim="noble majestic"></ol>'],
75 ['<ol style="display: none;" LlamA-Wim="majestic"></ol>', '<ol llama-wim="majestic"></ol>'],
76 ['<ol style="display: none;" llama-="noble majestic"></ol>', '<ol llama-="noble majestic"></ol>'],
77 // Both wildcard names and values.
78 ['<ul style="display: none;" alpaca-wool="wooly-warm strong majestic"></ul>', '<ul alpaca-wool="wooly-warm strong"></ul>'],
83 * @covers ::setConfiguration
85 public function testSetConfiguration() {
86 $configuration['settings'] = [
87 // New lines and spaces are replaced with a single space.
88 'allowed_html' => "<a> <br>\r\n <p>",
89 'filter_html_help' => 1,
90 'filter_html_nofollow' => 0,
92 $filter = new FilterHtml($configuration, 'filter_html', ['provider' => 'test']);
93 $this->assertSame('<a> <br> <p>', $filter->getConfiguration()['settings']['allowed_html']);