ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
HTMLSpecialCharsAsEntitiesTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ValueError;
26 use TypeError;
28 
29 class HTMLSpecialCharsAsEntitiesTest extends TestCase
30 {
31  public function testConstruct(): void
32  {
33  $this->assertInstanceOf(HTMLSpecialCharsAsEntities::class, new HTMLSpecialCharsAsEntities());
34  }
35 
36  #[DataProvider('provideTransformData')]
37  public function testTransform(string $exptected, string $in): void
38  {
39  $this->assertSame($exptected, (new HTMLSpecialCharsAsEntities())->transform($in));
40  }
41 
42  public function testInvalidType(): void
43  {
44  $this->expectException(TypeError::class);
46  }
47 
48  public function testInvalidEncoding(): void
49  {
50  $this->expectException(ValueError::class);
51  (new HTMLSpecialCharsAsEntities())->transform(chr(128));
52  }
53 
54  public static function provideTransformData(): array
55  {
56  $alpha = implode('', range(ord('A'), ord('z')));
57  $numbers = implode('', range(ord('0'), ord('9')));
58 
59  return array_map(fn($s) => [htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8'), $s], [
60  'Empty string' => '',
61  'Single quotes' => '\'',
62  'Different quotes' => '"Hey, y' . "'" . 'all"',
63  'Characters beyond ASCII value 255' => 'Ā',
64  'Characters beyond Unicode BMP' => "\xF0\x90\x80\x80",
65  'UTF-8' => '커피',
66  'Immune chars' => ',.-_',
67  'Alpha' => $alpha,
68  'Numbers' => $numbers,
69  'Basic control characters and null' => "\r\n\t\0",
70  'Named entities' => '<>&"',
71  'Single space' => ' ',
72  'Encode entities' => '&quot;hello&quot;',
73  'Braces' => '{[()]}',
74  ]);
75  }
76 
77  public function htmlSpecialCharsProvider(): array
78  {
79  return [
80  '\'' => ['\'', '&#039;'],
81  '"' => ['"', '&quot;'],
82  '<' => ['<', '&lt;'],
83  '>' => ['>', '&gt;'],
84  '&' => ['&', '&amp;'],
85  ];
86  }
87 }
Inspired by: Laminas escaper: https://github.com/laminas/laminas-escaper.