Debian Codegraph @

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

raw
1 <?php
2
3 namespace Test\Parsoid\Utils;
4
5 use PHPUnit\Framework\MockObject\MockObject;
6 use Wikimedia\Bcp47Code\Bcp47CodeValue;
7 use Wikimedia\Parsoid\Mocks\MockSiteConfig;
8 use Wikimedia\Parsoid\Utils\Title;
9 use Wikimedia\Parsoid\Utils\TitleException;
10
11 /**
12 * @coversDefaultClass \Wikimedia\Parsoid\Utils\Title
13 */
14 class TitleTest extends \PHPUnit\Framework\TestCase {
15 private static function newTitle( ...$args ) {
16 // Access non-public constructor
17 $classReflection = new \ReflectionClass( Title::class );
18 $constructor = $classReflection->getConstructor();
19 $constructor->setAccessible( true );
20 $title = $classReflection->newInstanceWithoutConstructor();
21 $constructor->invokeArgs( $title, $args );
22 return $title;
23 }
24
25 /**
26 * @covers ::__construct
27 * @covers ::getKey
28 * @covers ::getPrefixedDBKey
29 * @covers ::getPrefixedText
30 * @covers ::getFragment
31 * @covers ::getNamespace
32 * @dataProvider provideBasics
33 */
34 public function testBasics( $args, $key, $pKey, $pText, $fragment, $ns ) {
35 $title = self::newTitle( '', ...$args );
36 $this->assertSame( $key, $title->getKey() );
37 $this->assertSame( $pKey, $title->getPrefixedDBKey() );
38 $this->assertSame( $pText, $title->getPrefixedText() );
39 $this->assertSame( $fragment, $title->getFragment() ?: null );
40 $this->assertSame( $ns, $title->getNamespace() );
41
42 // LinkTarget tests
43 $this->assertSame( '', $title->getInterwiki() );
44 $this->assertSame( $ns, $title->getNamespace() );
45 $this->assertSame( $key, $title->getDBKey() );
46 $this->assertSame( $fragment ?? '', $title->getFragment() );
47 }
48
49 public function provideBasics() {
50 return [
51 'Basic article' => [ [ 'Basic_page', 0, '' ], 'Basic_page', 'Basic_page', 'Basic page', null, 0 ],
52 'User-namespace page' => [
53 [ 'Basic_page', 2, 'User' ], 'Basic_page', 'User:Basic_page', 'User:Basic page', null, 2
54 ],
55 'With fragment' => [
56 [ 'Basic_page', 0, '', 'frag' ], 'Basic_page', 'Basic_page', 'Basic page', 'frag', 0
57 ],
58 ];
59 }
60
61 /**
62 * @param string $lang
63 *
64 * @return MockObject|MockSiteConfig
65 */
66 private function getMockSiteConfig( string $lang = 'en' ) {
67 $siteConfig = $this->getMockBuilder( MockSiteConfig::class )
68 ->setConstructorArgs( [ [] ] )
69 ->onlyMethods( [ 'langBcp47', 'namespaceCase', 'specialPageLocalName', 'interwikiMap' ] )
70 ->getMock();
71 $siteConfig->method( 'namespaceCase' )->willReturnCallback( static function ( $ns ) {
72 return $ns === 15 ? 'case-sensitive' : 'first-letter';
73 } );
74 $siteConfig->method( 'langBcp47' )->willReturn( new Bcp47CodeValue( $lang ) );
75 $siteConfig->method( 'specialPageLocalName' )->willReturnCallback( static function ( $alias ) {
76 if ( $alias === 'DoesNotExist' ) {
77 return null;
78 }
79 return strtoupper( $alias );
80 } );
81 $siteConfig->method( 'interwikiMap' )->willReturn( [
82 'remotetestiw' => [
83 'prefix' => 'remotetestiw',
84 'url' => 'http://example.com/$1',
85 ],
86 ] );
87
88 return $siteConfig;
89 }
90
91 /**
92 * @covers ::newFromText
93 * @dataProvider provideNewFromText
94 */
95 public function testNewFromText( $args, $key, $ns, $fragment, $lang = null, $interwiki = '', $rawKey = null ) {
96 array_splice( $args, 1, 0, [ $this->getMockSiteConfig( $lang ?? 'en' ) ] );
97 $title = Title::newFromText( ...$args );
98 $this->assertSame( $key, $title->getKey() );
99 $this->assertSame( $ns, $title->getNamespace() );
100 $this->assertSame( $fragment, $title->getFragment() ?: null );
101
102 // LinkTarget tests
103 $this->assertSame( $interwiki, $title->getInterwiki() );
104 $this->assertSame( $ns, $title->getNamespace() );
105 $this->assertSame( $rawKey ?? $key, $title->getDBKey() );
106 $this->assertSame( $fragment ?? '', $title->getFragment() );
107 }
108
109 public function provideNewFromText() {
110 $x255 = str_repeat( 'X', 255 );
111 $x512 = str_repeat( 'X', 512 );
112 $poo63 = str_repeat( '💩', 63 );
113 return [
114 'Basic article' => [ [ 'Basic page' ], 'Basic_page', 0, null ],
115 'User-namespace page' => [ [ 'User:Foo bar' ], 'Foo_bar', 2, null ],
116 'Default NS' => [ [ 'Foo bar', 2 ], 'Foo_bar', 2, null ],
117 'Default NS, overridden' => [ [ 'File:Foo bar', 2 ], 'Foo_bar', 6, null ],
118 'Default NS, overridden to main' => [ [ ':Foo bar', 2 ], 'Foo_bar', 0, null ],
119 'Default NS, overridden to not-main' => [ [ ':File:Foo bar', 2 ], 'Foo_bar', 6, null ],
120 'With fragment' => [ [ 'User:Basic page#frag#ment' ], 'Basic_page', 2, 'frag#ment' ],
121 'Should normalize fragment' => [ [ 'User:Basic page#frag ment' ], 'Basic_page', 2, 'frag_ment' ],
122
123 'Capitalization' => [ [ 'UsEr:foo Bar' ], 'Foo_Bar', 2, null ],
124 'Capitalization, case-sensitive' => [ [ 'foo Bar', 15 ], 'foo_Bar', 15, null ],
125
126 'Trim whitespace' => [ [ ' _ Basic _ page _ ' ], 'Basic_page', 0, null ],
127 'Trim whitespace with namespace and fragment' => [
128 [ ' _ User _ : _ Basic page _ #__ fragment _ ' ], 'Basic_page', 2, '_fragment'
129 ],
130 'Replace whitespace' => [ [ "Bas\u{200E}ic\u{00A0}page" ], 'Basic_page', 0, null ],
131
132 'IPv4 sanitization (1)' => [ [ 'User:192.0.2.0' ], '192.0.2.0', 2, null ],
133 'IPv4 sanitization (2)' => [ [ 'User:000.000.020.000' ], '0.0.20.0', 2, null ],
134 'IPv4 sanitization (3)' => [ [ 'User_talk:000.000.020.000' ], '0.0.20.0', 3, null ],
135 'IPv4 sanitization (4)' => [ [ 'File:000.000.020.000' ], '000.000.020.000', 6, null ],
136 'IPv6 sanitization (1)' => [ [ 'User:2001:db8::' ], '2001:DB8:0:0:0:0:0:0', 2, null ],
137 'IPv6 sanitization (2)' => [
138 [ 'User:2001:0db8::0:000:0010:000' ], '2001:DB8:0:0:0:0:10:0', 2, null
139 ],
140 'IPv6 sanitization (::)' => [ [ 'User:::' ], '0:0:0:0:0:0:0:0', 2, null ],
141 'IPv6 sanitization (::1)' => [ [ 'User:::1' ], '0:0:0:0:0:0:0:1', 2, null ],
142
143 'Special page normalization (1)' => [ [ 'Special:FooBar' ], 'FOOBAR', -1, null ],
144 'Special page normalization (2)' => [ [ 'Special:FooBar/baz' ], 'FOOBAR/baz', -1, null ],
145 'Special page normalization (3)' => [ [ 'Special:DoesNotExist' ], 'DoesNotExist', -1, null ],
146
147 'Allowed percent-sign' => [ [ '100%' ], '100%', 0, null ],
148 'Allowed percent-sign (2)' => [ [ '100%ok' ], '100%ok', 0, null ],
149 'Allowed ampersand' => [ [ 'foo & bar; ok' ], 'Foo_&_bar;_ok', 0, null ],
150 'Allowed periods (1)' => [ [ 'foo . bar' ], 'Foo_._bar', 0, null ],
151 'Allowed periods (2)' => [ [ 'foo ../. ./.. bar' ], 'Foo_../._./.._bar', 0, null ],
152 'Long title' => [ [ "User talk:$x255" ], $x255, 3, null ],
153 'Long title (Unicode)' => [ [ "User talk:$poo63" ], $poo63, 3, null ],
154 'Long special title' => [ [ "Special:$x512" ], $x512, -1, null ],
155
156 // == More tests copied from the JS module ==
157 [ [ 'Sandbox' ], 'Sandbox', 0, null ],
158 [ [ 'A "B"' ], 'A_"B"', 0, null ],
159 [ [ 'A \'B\'' ], 'A_\'B\'', 0, null ],
160 [ [ '.com' ], '.com', 0, null ],
161 [ [ '~' ], '~', 0, null ],
162 // Parsoid had historically differentiated between '' and '#'
163 // as a title, but core does not distinguish between these two
164 // cases of an "empty' title.
165 [ [ '#' ], '', 0, null ],
166 [ [ 'Test#Abc' ], 'Test', 0, 'Abc' ],
167 [ [ '"' ], '"', 0, null ],
168 [ [ '\'' ], '\'', 0, null ],
169 [ [ 'Talk:Sandbox' ], 'Sandbox', 1, null ],
170 [ [ 'Talk:Foo:Sandbox' ], 'Foo:Sandbox', 1, null ],
171 [ [ 'File:Example.svg' ], 'Example.svg', 6, null ],
172 [ [ 'File_talk:Example.svg' ], 'Example.svg', 7, null ],
173 [ [ 'Foo/.../Sandbox' ], 'Foo/.../Sandbox', 0, null ],
174 [ [ 'Sandbox/...' ], 'Sandbox/...', 0, null ],
175 [ [ 'A~~' ], 'A~~', 0, null ],
176 [ [ ':A' ], 'A', 0, null ],
177 [ [ '-' ], '-', 0, null ],
178 [ [ 'aũ' ], 'Aũ', 0, null ],
179 [
180 [ '"Believing_Women"_in_Islam._Unreading_Patriarchal_Interpretations_of_the_Qur\\\'ān' ],
181 '"Believing_Women"_in_Islam._Unreading_Patriarchal_Interpretations_of_the_Qur\\\'ān', 0, null
182 ],
183
184 [ [ 'Test' ], 'Test', 0, null ],
185 [ [ ':Test' ], 'Test', 0, null ],
186 [ [ ': Test' ], 'Test', 0, null ],
187 [ [ ':_Test_' ], 'Test', 0, null ],
188 [ [ 'Test 123 456 789' ], 'Test_123_456_789', 0, null ],
189 [ [ '💩' ], '💩', 0, null ],
190 [ [ 'Foo:bar' ], 'Foo:bar', 0, null ],
191 [ [ 'Talk: foo' ], 'Foo', 1, null ],
192 [ [ 'int:eger' ], 'Int:eger', 0, null ],
193 [ [ 'WP:eger' ], 'Eger', 4, null ],
194 [ [ 'X-Men (film series) #Gambit' ], 'X-Men_(film_series)', 0, 'Gambit' ],
195 [ [ 'Foo _ bar' ], 'Foo_bar', 0, null ],
196 // phpcs:ignore Generic.Files.LineLength.TooLong
197 [ [ "Foo \u{00A0}\u{1680}\u{180E}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000} bar" ], 'Foo_bar', 0, null ],
198 [ [ "Foo\u{200E}\u{200F}\u{202A}\u{202B}\u{202C}\u{202D}\u{202E}bar" ], 'Foobar', 0, null ],
199 // Special handling for `i` first character
200 [ [ 'iTestTest' ], 'İTestTest', 0, null, 'tr' ],
201 [ [ 'iTestTest' ], 'İTestTest', 0, null, 'az' ],
202 [ [ 'iTestTest' ], 'İTestTest', 0, null, 'kk' ],
203 [ [ 'iTestTest' ], 'İTestTest', 0, null, 'kaa' ],
204 // User IP sanitizations
205 [ [ 'User:::1' ], '0:0:0:0:0:0:0:1', 2, null ],
206 [ [ 'User:0:0:0:0:0:0:0:1' ], '0:0:0:0:0:0:0:1', 2, null ],
207 [ [ 'User:127.000.000.001' ], '127.0.0.1', 2, null ],
208 [ [ 'User:0.0.0.0' ], '0.0.0.0', 2, null ],
209 [ [ 'User:00.00.00.00' ], '0.0.0.0', 2, null ],
210 [ [ 'User:000.000.000.000' ], '0.0.0.0', 2, null ],
211 [ [ 'User:141.000.011.253' ], '141.0.11.253', 2, null ],
212 [ [ 'User: 1.2.4.5' ], '1.2.4.5', 2, null ],
213 [ [ 'User:01.02.04.05' ], '1.2.4.5', 2, null ],
214 [ [ 'User:001.002.004.005' ], '1.2.4.5', 2, null ],
215 [ [ 'User:010.0.000.1' ], '10.0.0.1', 2, null ],
216 [ [ 'User:080.072.250.04' ], '80.72.250.4', 2, null ],
217 [ [ 'User:Foo.1000.00' ], 'Foo.1000.00', 2, null ],
218 [ [ 'User:Bar.01' ], 'Bar.01', 2, null ],
219 [ [ 'User:Bar.010' ], 'Bar.010', 2, null ],
220 [ [ 'User:cebc:2004:f::' ], 'CEBC:2004:F:0:0:0:0:0', 2, null ],
221 [ [ 'User:::' ], '0:0:0:0:0:0:0:0', 2, null ],
222 [ [ 'User:0:0:0:1::' ], '0:0:0:1:0:0:0:0', 2, null ],
223 [ [ 'User:3f:535::e:fbb' ], '3F:535:0:0:0:0:E:FBB', 2, null ],
224 [ [ 'User Talk:::1' ], '0:0:0:0:0:0:0:1', 3, null ],
225 [ [ 'User_Talk:::1' ], '0:0:0:0:0:0:0:1', 3, null ],
226 [ [ 'User_talk:::1' ], '0:0:0:0:0:0:0:1', 3, null ],
227 [ [ 'User_talk:::1/24' ], '0:0:0:0:0:0:0:1/24', 3, null ],
228 // Interwiki link, with prefix: note that we put these in the
229 // main namespace, and the "dbkey" contains everything after the
230 // interwiki prefix, including the "namespace"
231 [ [ 'remotetestiw:User:Bar' ], 'remotetestiw:User:Bar', 0, null, null, 'remotetestiw', 'User:Bar' ],
232 [ [ 'remotetestiw:User:Bar#Foo' ], 'remotetestiw:User:Bar', 0, 'Foo', null, 'remotetestiw', 'User:Bar' ],
233 // remotetestiw in user (T329690)
234 [ [ 'remotetestiw:', 2 ], 'remotetestiw:', 2, null, null, 'remotetestiw', '' ],
235 // Colons in talk namespaces (T332903)
236 // phpcs:ignore Generic.Files.LineLength.TooLong
237 [ [ 'Talk:2024:Expressions of Interest/Wikimania 2024 Istanbul, Türkiye' ], '2024:Expressions_of_Interest/Wikimania_2024_Istanbul,_Türkiye', 1, null ],
238 ];
239 }
240
241 /**
242 * @covers ::newFromText
243 * @dataProvider provideNewFromText_errors
244 */
245 public function testNewFromText_errors( $title, $type ) {
246 try {
247 Title::newFromText( $title, $this->getMockSiteConfig() );
248 $this->fail( 'Expected exception not thrown' );
249 } catch ( TitleException $ex ) {
250 $this->assertSame( $type, $ex->type );
251 }
252 }
253
254 public function provideNewFromText_errors() {
255 $x256 = str_repeat( 'X', 256 );
256 $x513 = str_repeat( 'X', 513 );
257 $poo64 = str_repeat( '💩', 64 );
258 return [
259 'Bad UTF-8' => [ "foo\xa0bar", 'title-invalid-utf8' ],
260 'Replacement character' => [ 'foo�bar', 'title-invalid-utf8' ],
261 'Empty' => [ '', 'title-invalid-empty' ],
262 'Only whitespace' => [ ' _ ', 'title-invalid-empty' ],
263 'Invalid talk' => [ 'Talk:User:Foo', 'title-invalid-talk-namespace' ],
264 'Invalid chars' => [ '<foo>', 'title-invalid-characters' ],
265 'Invalid chars (percent-encoding)' => [ 'foo%26bar', 'title-invalid-characters' ],
266 'Invalid chars (HTML entity)' => [ 'foo&amp;bar', 'title-invalid-characters' ],
267 // Can never trigger these, the # is interpreted as starting the fragment
268 // 'Invalid chars (HTML code)' => [ 'foo&#38;bar', 'title-invalid-characters' ],
269 // 'Invalid chars (HTML hex code)' => [ 'foo&#x2A;bar', 'title-invalid-characters' ],
270 'Relative path: .' => [ '.', 'title-invalid-relative' ],
271 'Relative path: ..' => [ '..', 'title-invalid-relative' ],
272 'Relative path: ./' => [ './foo', 'title-invalid-relative' ],
273 'Relative path: ../' => [ '../foo', 'title-invalid-relative' ],
274 'Relative path: /.' => [ 'foo/.', 'title-invalid-relative' ],
275 'Relative path: /..' => [ 'foo/..', 'title-invalid-relative' ],
276 'Relative path: /./' => [ 'foo/./bar', 'title-invalid-relative' ],
277 'Relative path: /../' => [ 'foo/../bar', 'title-invalid-relative' ],
278 'Tildes' => [ 'foo~~~bar', 'title-invalid-magic-tilde' ],
279 'Too long' => [ $x256, 'title-invalid-too-long' ],
280 'Too long (Unicode)' => [ $poo64, 'title-invalid-too-long' ],
281 'Too long (special)' => [ "Special:$x513", 'title-invalid-too-long' ],
282 'Empty except for namespace' => [ 'User:', 'title-invalid-empty' ],
283 'Empty except for namespace+fragment' => [ 'User:#frag', 'title-invalid-empty' ],
284 'Leading colon' => [ 'User::Hiho', 'title-invalid-leading-colon' ],
285
286 // == More tests copied from the JS module ==
287 [ ':', 'title-invalid-empty' ],
288 [ '__ __', 'title-invalid-empty' ],
289 [ ' __ ', 'title-invalid-empty' ],
290 // Bad characters forbidden regardless of wgLegalTitleChars
291 [ 'A [ B', 'title-invalid-characters' ],
292 [ 'A ] B', 'title-invalid-characters' ],
293 [ 'A { B', 'title-invalid-characters' ],
294 [ 'A } B', 'title-invalid-characters' ],
295 [ 'A < B', 'title-invalid-characters' ],
296 [ 'A > B', 'title-invalid-characters' ],
297 [ 'A | B', 'title-invalid-characters' ],
298 // URL encoding
299 [ 'A%20B', 'title-invalid-characters' ],
300 [ 'A%23B', 'title-invalid-characters' ],
301 [ 'A%2523B', 'title-invalid-characters' ],
302 // XML/HTML character entity references
303 // Note: Commented out because they are not marked invalid by the PHP test as
304 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
305 // 'A &eacute; B',
306 // Subject of NS_TALK does not roundtrip to NS_MAIN
307 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
308 // Directory navigation
309 [ './Sandbox', 'title-invalid-relative' ],
310 [ '../Sandbox', 'title-invalid-relative' ],
311 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
312 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
313 [ 'Sandbox/.', 'title-invalid-relative' ],
314 [ 'Sandbox/..', 'title-invalid-relative' ],
315 // Tilde
316 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
317 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
318 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
319 // Namespace prefix without actual title
320 [ 'Talk:', 'title-invalid-empty' ],
321 [ 'Talk:#', 'title-invalid-empty' ],
322 [ 'Category: ', 'title-invalid-empty' ],
323 [ 'Category: #bar', 'title-invalid-empty' ],
324 ];
325 }
326
327 }