ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HTMLPurifierTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use PHPUnit\Framework\TestCase;
25use org\bovigo\vfs\vfsStreamWrapper;
26use org\bovigo\vfs\vfsStreamDirectory;
27use org\bovigo\vfs\vfsStream;
28use PHPUnit\Framework\Attributes\DataProvider;
29
30class HTMLPurifierTest extends TestCase
31{
32 public function testConstruct(): void
33 {
35 vfsStreamWrapper::register();
36 $root = vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
37 $cache_directory = vfsStream::newDirectory('HTMLPurifier')->at($root);
38 $cache_directory->chmod(0777);
39
40 $this->assertInstanceOf(HTMLPurifier::class, new HTMLPurifier([], vfsStream::url('root/HTMLPurifier')));
41 }
42
43 #[DataProvider('documents')]
44 public function testPurify(string $input, string $expected): void
45 {
47 vfsStreamWrapper::register();
48 $root = vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
49 $cache_directory = vfsStream::newDirectory('HTMLPurifier')->at($root);
50 $cache_directory->chmod(0777);
51
52 $instance = new HTMLPurifier([
53 'a', 'blockquote', 'br', 'cite', 'code', 'dd', 'div', 'dl', 'dt', 'em', 'h1', 'h2', 'h3', 'h4',
54 'h5', 'h6', 'hr', 'img', 'li', 'ol', 'p', 'pre', 'span', 'strong', 'sub', 'sup', 'u', 'ul',
55 ], vfsStream::url('root/HTMLPurifier'));
56
57 $this->assertSame($expected, $instance->purify($input));
58 }
59
60 public static function documents(): array
61 {
62 return [
63 'Simple HTML Elements' => [
64 '<h1><b>This</b> <i>is</i> <u>a</u> <em>Headline</em>!</h1><p>And a<br>paragraph.</p>',
65 '<h1><b>This</b> <i>is</i> <span style="text-decoration:underline;">a</span> <em>Headline</em>!</h1><p>And a<br />paragraph.</p>',
66 ],
67 'Simple HTML Elements with an Invalid Tag' => [
68 '<h1>This is a <a href="mailto:info@ilias.de">Headline</a>!</h1><p>And a paragraph with an invalid element: ILIAS e.V. <info@ilias.de>.</p>',
69 '<h1>This is a <a href="mailto:info@ilias.de">Headline</a>!</h1><p>And a paragraph with an invalid element: ILIAS e.V. .</p>',
70 ],
71 'Block Elements and Nested Lists' => [
72 '<div><ul><li>Php</li></ul><hr><ol><li>Unit</li></ol><dl><dt>Test</dt><dd><code>Success or Failure!</code></dd></dl></div>',
73 '<div><ul><li>Php</li></ul><hr /><ol><li>Unit</li></ol><dl><dt>Test</dt><dd><code>Success or Failure!</code></dd></dl></div>',
74 ],
75 'Blockquote' => [
76 '<pre>Text</pre><blockquote><cite><sup>Q</sup>uote</cite></blockquote>',
77 '<pre>Text</pre><blockquote><p><cite><sup>Q</sup>uote</cite></p></blockquote>',
78 ]
79 ];
80 }
81
82 private function isVsfStreamInstalled(): bool
83 {
84 return class_exists('org\bovigo\vfs\vfsStreamWrapper');
85 }
86
87 private function skipIfvfsStreamNotSupported(): void
88 {
89 if (!$this->isVsfStreamInstalled()) {
90 $this->markTestSkipped('Skipped test, vfsStream (https://github.com/bovigo/vfsStream) required');
91 }
92 }
93}
testPurify(string $input, string $expected)