ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HTMLAttributeValueTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
25use ValueError;
26use TypeError;
27use PHPUnit\Framework\Attributes\DataProvider;
28
29require_once __DIR__ . '/ProvideUTF8CodepointRange.php';
30
31class HTMLAttributeValueTest extends TestCase
32{
33 use ProvideUTF8CodepointRange;
34
35 public function testConstruct(): void
36 {
37 $this->assertInstanceOf(HTMLAttributeValue::class, new HTMLAttributeValue());
38 }
39
40 #[DataProvider('provideTransformData')]
41 public function testTransform(string $exptected, string $in, string $method): void
42 {
43 $this->$method($exptected, (new HTMLAttributeValue())->transform($in));
44 }
45
46 public function testInvalidEncoding(): void
47 {
48 $this->expectException(ValueError::class);
49 (new HTMLAttributeValue())->transform(chr(128));
50 }
51
52 public function testInvalidType(): void
53 {
54 $this->expectException(TypeError::class);
55 (new HTMLAttributeValue())->transform(8);
56 }
57
58 public static function provideTransformData(): array
59 {
60 return [
61 'Empty string' => ['', '', 'assertSame'],
62 'Single quote' => ['&#x27;', '\'', 'assertSame'],
63 'Different quotes' => ['&quot;Hey,&#x20;y&#x27;all&quot;', '"Hey, y' . "'" . 'all"', 'assertSame'],
64 'UTF-8' => ['&#xAE40;&#xCE58;', '김치', 'assertSame'],
65 'Characters beyond ASCII value 255' => ['&#x0100;', 'Ā', 'assertSame'],
66 'Characters beyond Unicode BMP' => ['&#x10000;', "\xF0\x90\x80\x80", 'assertSame'],
67 'Printable control characters' => ['&#x0D;&#x0A;&#x09;', "\r\n\t", 'assertSame'],
68 'Unprintable control characters' => ['&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;', "\0\1\x1F\x7F", 'assertSame'],
69 'Named entities' => ['&lt;&gt;&amp;&quot;', '<>&"', 'assertSame'],
70 'Single space' => ['&#x20;', ' ', 'assertSame'],
71 'Encode entities' => ['&amp;quot&#x3B;hello&amp;quot&#x3B;', '&quot;hello&quot;', 'assertSame'],
72 'Braces' => ['&#x7B;&#x5B;&#x28;&#x29;&#x5D;&#x7D;', '{[()]}', 'assertSame'],
73 ...self::oneByteRangeExcept([',', '.', '-', '_']),
74 ];
75 }
76}
Inspired by: Laminas escaper: https://github.com/laminas/laminas-escaper.
testTransform(string $exptected, string $in, string $method)