ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilHtmlPurifierCompositeTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
22use PHPUnit\Framework\Attributes\DataProvider;
23
24final class ilHtmlPurifierCompositeTest extends TestCase
25{
26 private const array TO_PURIFY = [
27 'phpunit1',
28 'phpunit2',
29 'phpunit3',
30 ];
31
33 {
34 return new class () implements ilHtmlPurifierInterface {
35 public function purify(string $html): string
36 {
37 return $html . '.';
38 }
39
40 public function purifyArray(array $htmlCollection): array
41 {
42 foreach ($htmlCollection as &$html) {
43 $html .= '.';
44 }
45
46 return $htmlCollection;
47 }
48 };
49 }
50
52 {
53 $purifier = new ilHtmlPurifierComposite();
54
55 $p1 = $this->getFakePurifier();
56 $p2 = clone $p1;
57 $p3 = clone $p1;
58
59 $purifier->addPurifier($p1);
60 $purifier->addPurifier($p1);
61 $purifier->addPurifier($p2);
62 $purifier->addPurifier($p3);
63
64 $this->assertSame('phpunit...', $purifier->purify('phpunit'));
65
66 $purifier->removePurifier($p2);
67
68 $this->assertSame('phpunit..', $purifier->purify('phpunit'));
69 }
70
72 {
73 $purifier = new ilHtmlPurifierComposite();
74
75 $p1 = $this->getFakePurifier();
76 $p2 = clone $p1;
77 $p3 = clone $p1;
78
79 $purifier->addPurifier($p1);
80 $purifier->addPurifier($p1);
81 $purifier->addPurifier($p2);
82 $purifier->addPurifier($p3);
83
84 $this->assertSame(array_map(static function (string $html): string {
85 return $html . '...';
86 }, self::TO_PURIFY), $purifier->purifyArray(self::TO_PURIFY));
87
88 $purifier->removePurifier($p2);
89
90 $this->assertSame(array_map(static function (string $html): string {
91 return $html . '..';
92 }, self::TO_PURIFY), $purifier->purifyArray(self::TO_PURIFY));
93 }
94
98 public static function invalidHtmlDataTypeProvider(): array
99 {
100 return [
101 'integer' => [5],
102 'float' => [0.1],
103 'null' => [null],
104 'array' => [[]],
105 'object' => [new stdClass()],
106 'bool' => [false],
107 'resource' => [fopen('php://memory', 'rb')],
108 ];
109 }
110
111 #[DataProvider('invalidHtmlDataTypeProvider')]
113 {
114 $this->expectException(InvalidArgumentException::class);
115
116 $purifier = new ilHtmlPurifierComposite();
117 $purifier->purifyArray([$element]);
118 }
119}
testExceptionIsRaisedIfNonStringElementsArePassedForHtmlBatchProcessing($element)
Composite for nesting multiple purifiers.
Interface for html sanitizing functionality.