Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Utils/UtilsTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Utils;
4
5 use Wikimedia\Parsoid\Utils\Utils;
6
7 /**
8 * @coversDefaultClass \Wikimedia\Parsoid\Utils\Utils
9 */
10 class UtilsTest extends \PHPUnit\Framework\TestCase {
11 /**
12 * @covers ::decodeWtEntities
13 * @dataProvider provideDecodeWtEntities
14 */
15 public function testDecodeWtEntities( $wt, $expected ) {
16 $actual = Utils::decodeWtEntities( $wt );
17 $this->assertEquals( $expected, $actual );
18 }
19
20 public function provideDecodeWtEntities() {
21 // Easy cases...
22 yield "Lowercase &" => [ '&amp;', '&' ];
23 yield "Uppercase &" => [ '&AMP;', '&' ];
24 // Ensure that HTML5 "semicolon-less" entities are not
25 // decoded -- these are decoded in HTML5 but not in wikitext!
26 yield "Semicolon-less &" => [ '&ampamp;', '&ampamp;' ];
27 yield "Semicolon-less query" => [ '&prod=foo', '&prod=foo' ];
28 // Exhaustive test against Sanitizer::validateCodePoint()
29 $checkEntity = function ( int $cp, string $s ) {
30 $expected = self::validateCodePoint( $cp ) ?
31 mb_chr( $cp, "utf-8" ) : $s;
32 yield $s => [ $s, $expected ];
33 };
34 $check = static function ( int $cp ) use ( $checkEntity ) {
35 yield from $checkEntity( $cp, "&#$cp;" );
36 yield from $checkEntity( $cp, strtolower( "&#x" . dechex( $cp ) . ";" ) );
37 yield from $checkEntity( $cp, strtoupper( "&#x" . dechex( $cp ) . ";" ) );
38 };
39 for ( $cp = 0; $cp < 0x100; $cp++ ) {
40 yield from $check( $cp );
41 }
42 for ( $cp = 0xD700; $cp < 0xE010; $cp++ ) {
43 yield from $check( $cp );
44 }
45 for ( $cp = 0xFFF0; $cp < 0x10010; $cp++ ) {
46 yield from $check( $cp );
47 }
48 for ( $cp = 0x10FFF0; $cp < 0x110010; $cp++ ) {
49 yield from $check( $cp );
50 }
51 }
52
53 /**
54 * This is a direct copy of Sanitizer::validateCodepoint; when/if
55 * Parsoid is integrated into MediaWiki we could replace this with
56 * a direct call.
57 *
58 * @param int $codepoint Unicode codepoint.
59 *
60 * @return bool
61 */
62 private static function validateCodePoint( int $codepoint ): bool {
63 # U+000C is valid in HTML5 but not allowed in XML.
64 # U+000D is valid in XML but not allowed in HTML5.
65 # U+007F - U+009F are disallowed in HTML5 (control characters).
66 return $codepoint == 0x09
67 || $codepoint == 0x0a
68 || ( $codepoint >= 0x20 && $codepoint <= 0x7e )
69 || ( $codepoint >= 0xa0 && $codepoint <= 0xd7ff )
70 || ( $codepoint >= 0xe000 && $codepoint <= 0xfffd )
71 || ( $codepoint >= 0x10000 && $codepoint <= 0x10ffff );
72 }
73
74 /**
75 * @covers ::escapeWtEntities
76 * @dataProvider provideEscapeWtEntities
77 */
78 public function testEscapeWtEntities( $text, $expected ) {
79 $actual = Utils::escapeWtEntities( $text );
80 $this->assertEquals( $expected, $actual );
81 }
82
83 public function provideEscapeWtEntities() {
84 return [
85 [ '&amp;', '&amp;amp;' ],
86 // Not all entities, just those that are valid
87 [ 'hello&goodbye', 'hello&goodbye' ],
88 [ 'hello&goodbye;', 'hello&goodbye;' ],
89 ];
90 }
91
92 /**
93 * @covers ::escapeHtml
94 * @dataProvider provideEscapeHtml
95 */
96 public function testEscapeHtml( $text, $expected ) {
97 $actual = Utils::escapeHtml( $text );
98 $this->assertEquals( $expected, $actual );
99 }
100
101 public function provideEscapeHtml() {
102 return [
103 [ 'only 5 characters escaped', 'only 5 characters escaped' ],
104 [ '<>&"\'', '&lt;&gt;&amp;&quot;&apos;' ],
105 ];
106 }
107
108 /**
109 * @covers ::entityEncodeAll
110 * @dataProvider provideEntityEncodeAll
111 */
112 public function testEntityEncodeAll( $text, $expected ) {
113 $actual = Utils::entityEncodeAll( $text );
114 $this->assertEquals( $expected, $actual );
115 }
116
117 public function provideEntityEncodeAll() {
118 return [
119 [ 'Even ASCII', '&#x45;&#x76;&#x65;&#x6E;&#x20;&#x41;&#x53;&#x43;&#x49;&#x49;' ],
120 [ 'is encoded', '&#x69;&#x73;&#x20;&#x65;&#x6E;&#x63;&#x6F;&#x64;&#x65;&#x64;' ],
121 [ "&>", '&#x26;&#x3E;' ],
122 // Some entities use special forms
123 [ "\u{00A0}", '&nbsp;' ],
124 [ "\u{000D}", '&#x0D;' ],
125 // Even Unicode characters in the "Astral plane" work correctly
126 [ "\u{1F4A9}", '&#x1F4A9;' ],
127 ];
128 }
129
130 /**
131 * @dataProvider provideDecodeURI
132 * @covers ::decodeURI
133 * @covers ::decodeURIComponent
134 */
135 public function testDecodeURI( $input, $expect1, $expect2 = null ) {
136 $this->assertSame( $expect1, Utils::decodeURIComponent( $input ) );
137 $this->assertSame( $expect2 ?? $expect1, Utils::decodeURI( $input ) );
138 }
139
140 public function provideDecodeURI() {
141 return [
142 'Simple example' => [ 'abc %66%6f%6f%c2%a0%62%61%72', "abc foo\xc2\xa0bar" ],
143 'Non-BMP example' => [ '%66%6f%6f%f0%9f%92%a9%62%61%72', 'foo💩bar' ],
144 'Reserved chars (lowercase hex)' => [
145 '%22%23%24%25%26%27%28%29%2a%2b%2c%2d%2e%2f%30%31%32%33%34%35%36%37%38%39%3a%3b%3c%3d%3e%3f%40',
146 '"#$%&\'()*+,-./0123456789:;<=>?@',
147 '"%23%24%%26\'()*%2b%2c-.%2f0123456789%3a%3b<%3d>%3f%40',
148 ],
149 'Reserved chars (uppercase hex)' => [
150 '%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F%30%31%32%33%34%35%36%37%38%39%3A%3B%3C%3D%3E%3F%40',
151 '"#$%&\'()*+,-./0123456789:;<=>?@',
152 '"%23%24%%26\'()*%2B%2C-.%2F0123456789%3A%3B<%3D>%3F%40',
153 ],
154 'Reserved chars (literals aren\'t encoded)' => [
155 '"#$&\'()*+,-./0123456789:;<=>?@',
156 '"#$&\'()*+,-./0123456789:;<=>?@',
157 ],
158 'Invalid byte' => [ '%66%6f%6f%aA%62%61%72', 'foo%aAbar' ],
159 'Overlong sequence' => [ '%66%6f%6f%c1%98%62%61%72', 'foo%c1%98bar' ],
160 'Out of range sequence' => [ '%66%6f%6f%f4%90%80%80%62%61%72', 'foo%f4%90%80%80bar' ],
161 'Truncated sequence' => [ '%66%6f%6f%f0%9f%92%c9%62%61%72', 'foo%f0%9f%92%c9bar' ],
162 'Too many continuation bytes' => [ '%66%6f%6f%c2%a0%a0%62%61%72', "foo\xc2\xa0%a0bar" ],
163 'Invalid percent-sequence' => [ '%66%6f%6f%aG%%62%61%72', 'foo%aG%bar' ],
164 'Truncated percent-sequence' => [ '%66%6f%6', 'fo%6' ],
165 'Truncated percent-sequence (2)' => [ '%66%6f%', 'fo%' ],
166 ];
167 }
168
169 }