Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / filter / tests / src / Unit / FilterHtmlTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\filter\Plugin\Filter\FilterHtml;
7
8 /**
9  * @coversDefaultClass \Drupal\filter\Plugin\Filter\FilterHtml
10  * @group filter
11  */
12 class FilterHtmlTest extends UnitTestCase {
13
14   /**
15    * @var \Drupal\filter\Plugin\Filter\FilterHtml
16    */
17   protected $filter;
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::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,
28     ];
29     $this->filter = new FilterHtml($configuration, 'filter_html', ['provider' => 'test']);
30     $this->filter->setStringTranslation($this->getStringTranslationStub());
31   }
32
33   /**
34    * @covers ::filterAttributes
35    *
36    * @dataProvider providerFilterAttributes
37    *
38    * @param string $html
39    *   Input HTML.
40    * @param array $expected
41    *   The expected output string.
42    */
43   public function testfilterAttributes($html, $expected) {
44     $this->assertSame($expected, $this->filter->filterAttributes($html));
45   }
46
47   /**
48    * Provides data for testfilterAttributes.
49    *
50    * @return array
51    *   An array of test data.
52    */
53   public function providerFilterAttributes() {
54     return [
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>'],
79     ];
80   }
81
82   /**
83    * @covers ::setConfiguration
84    */
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,
91     ];
92     $filter = new FilterHtml($configuration, 'filter_html', ['provider' => 'test']);
93     $this->assertSame('<a> <br> <p>', $filter->getConfiguration()['settings']['allowed_html']);
94   }
95
96 }