Merge "deferred: make DeferredUpdates::attemptUpdate() use callback owners for $fname...
[lhc/web/wiklou.git] / tests / phpunit / includes / title / MediaWikiTitleCodecTest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Daniel Kinzler
20 */
21
22 use MediaWiki\Interwiki\InterwikiLookup;
23 use Wikimedia\TestingAccessWrapper;
24
25 /**
26 * @covers MediaWikiTitleCodec
27 *
28 * @group Title
29 * @group Database
30 * ^--- needed because of global state in
31 */
32 class MediaWikiTitleCodecTest extends MediaWikiTestCase {
33
34 public function setUp() {
35 parent::setUp();
36
37 $this->setMwGlobals( [
38 'wgAllowUserJs' => false,
39 'wgDefaultLanguageVariant' => false,
40 'wgMetaNamespace' => 'Project',
41 'wgLocalInterwikis' => [ 'localtestiw' ],
42 'wgCapitalLinks' => true,
43 ] );
44 $this->setUserLang( 'en' );
45 $this->setContentLang( 'en' );
46 }
47
48 /**
49 * Returns a mock GenderCache that will consider a user "female" if the
50 * first part of the user name ends with "a".
51 *
52 * @return GenderCache
53 */
54 private function getGenderCache() {
55 $genderCache = $this->getMockBuilder( GenderCache::class )
56 ->disableOriginalConstructor()
57 ->getMock();
58
59 $genderCache->expects( $this->any() )
60 ->method( 'getGenderOf' )
61 ->will( $this->returnCallback( function ( $userName ) {
62 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ? 'female' : 'male';
63 } ) );
64
65 return $genderCache;
66 }
67
68 /**
69 * Returns a mock InterwikiLookup that only has an isValidInterwiki() method, which recognizes
70 * 'localtestiw' and 'remotetestiw'. All other methods throw.
71 *
72 * @return InterwikiLookup
73 */
74 private function getInterwikiLookup() : InterwikiLookup {
75 $iwLookup = $this->createMock( InterwikiLookup::class );
76
77 $iwLookup->expects( $this->any() )
78 ->method( 'isValidInterwiki' )
79 ->will( $this->returnCallback( function ( $prefix ) {
80 return $prefix === 'localtestiw' || $prefix === 'remotetestiw';
81 } ) );
82
83 $iwLookup->expects( $this->never() )
84 ->method( $this->callback( function ( $name ) {
85 return $name !== 'isValidInterwiki';
86 } ) );
87
88 return $iwLookup;
89 }
90
91 /**
92 * Returns a mock NamespaceInfo that has only the following methods:
93 *
94 * * exists()
95 * * getCanonicalName()
96 * * hasGenderDistinction()
97 * * isCapitalized()
98 *
99 * All other methods throw. The only namespaces that exist are NS_SPECIAL, NS_MAIN, NS_TALK,
100 * NS_USER, and NS_USER_TALK. NS_USER and NS_USER_TALK have gender distinctions. All namespaces
101 * are capitalized.
102 *
103 * @return NamespaceInfo
104 */
105 private function getNamespaceInfo() : NamespaceInfo {
106 $canonicalNamespaces = [
107 NS_SPECIAL => 'Special',
108 NS_MAIN => '',
109 NS_TALK => 'Talk',
110 NS_USER => 'User',
111 NS_USER_TALK => 'User_talk',
112 ];
113
114 $nsInfo = $this->createMock( NamespaceInfo::class );
115
116 $nsInfo->method( 'exists' )
117 ->will( $this->returnCallback( function ( $ns ) use ( $canonicalNamespaces ) {
118 return isset( $canonicalNamespaces[$ns] );
119 } ) );
120
121 $nsInfo->method( 'getCanonicalName' )
122 ->will( $this->returnCallback( function ( $ns ) use ( $canonicalNamespaces ) {
123 return $canonicalNamespaces[$ns] ?? false;
124 } ) );
125
126 $nsInfo->method( 'hasGenderDistinction' )
127 ->will( $this->returnCallback( function ( $ns ) {
128 return $ns === NS_USER || $ns === NS_USER_TALK;
129 } ) );
130
131 $nsInfo->method( 'isCapitalized' )->willReturn( true );
132
133 $nsInfo->expects( $this->never() )->method( $this->anythingBut(
134 'exists', 'getCanonicalName', 'hasGenderDistinction', 'isCapitalized'
135 ) );
136
137 return $nsInfo;
138 }
139
140 protected function makeCodec( $lang ) {
141 return new MediaWikiTitleCodec(
142 Language::factory( $lang ),
143 $this->getGenderCache(),
144 [ 'localtestiw' ],
145 $this->getInterwikiLookup(),
146 $this->getNamespaceInfo()
147 );
148 }
149
150 public static function provideFormat() {
151 return [
152 [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo Bar' ],
153 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi Maier#stuff and so on' ],
154 [ false, 'Hansi_Maier', '', '', 'en', 'Hansi Maier' ],
155 [
156 NS_USER_TALK,
157 'hansi__maier',
158 '',
159 '',
160 'en',
161 'User talk:hansi maier',
162 'User talk:Hansi maier'
163 ],
164
165 // getGenderCache() provides a mock that considers first
166 // names ending in "a" to be female.
167 [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa Müller' ],
168 [ NS_MAIN, 'FooBar', '', 'remotetestiw', 'en', 'remotetestiw:FooBar' ],
169 ];
170 }
171
172 /**
173 * @dataProvider provideFormat
174 */
175 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
176 $normalized = null
177 ) {
178 if ( $normalized === null ) {
179 $normalized = $expected;
180 }
181
182 $codec = $this->makeCodec( $lang );
183 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
184
185 $this->assertEquals( $expected, $actual, 'formatted' );
186
187 // test round trip
188 $parsed = $codec->parseTitle( $actual, NS_MAIN );
189 $actual2 = $codec->formatTitle(
190 $parsed->getNamespace(),
191 $parsed->getText(),
192 $parsed->getFragment(),
193 $parsed->getInterwiki()
194 );
195
196 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
197 }
198
199 public static function provideGetText() {
200 return [
201 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
202 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
203 ];
204 }
205
206 /**
207 * @dataProvider provideGetText
208 */
209 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
210 $codec = $this->makeCodec( $lang );
211 $title = new TitleValue( $namespace, $dbkey, $fragment );
212
213 $actual = $codec->getText( $title );
214
215 $this->assertEquals( $expected, $actual );
216 }
217
218 public static function provideGetPrefixedText() {
219 return [
220 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
221 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
222
223 // No capitalization or normalization is applied while formatting!
224 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
225
226 // getGenderCache() provides a mock that considers first
227 // names ending in "a" to be female.
228 [ NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
229 [ 1000000, 'Invalid_namespace', '', 'en', 'Special:Badtitle/NS1000000:Invalid namespace' ],
230 ];
231 }
232
233 /**
234 * @dataProvider provideGetPrefixedText
235 */
236 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
237 $codec = $this->makeCodec( $lang );
238 $title = new TitleValue( $namespace, $dbkey, $fragment );
239
240 $actual = $codec->getPrefixedText( $title );
241
242 $this->assertEquals( $expected, $actual );
243 }
244
245 public static function provideGetPrefixedDBkey() {
246 return [
247 [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo_Bar' ],
248 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi_Maier' ],
249
250 // No capitalization or normalization is applied while formatting!
251 [ NS_USER_TALK, 'hansi__maier', '', '', 'en', 'User_talk:hansi__maier' ],
252
253 // getGenderCache() provides a mock that considers first
254 // names ending in "a" to be female.
255 [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ],
256
257 [ NS_MAIN, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ],
258
259 // non-existent namespace
260 [ 10000000, 'Foobar', '', '', 'en', 'Special:Badtitle/NS10000000:Foobar' ],
261 ];
262 }
263
264 /**
265 * @dataProvider provideGetPrefixedDBkey
266 */
267 public function testGetPrefixedDBkey( $namespace, $dbkey, $fragment,
268 $interwiki, $lang, $expected
269 ) {
270 $codec = $this->makeCodec( $lang );
271 $title = new TitleValue( $namespace, $dbkey, $fragment, $interwiki );
272
273 $actual = $codec->getPrefixedDBkey( $title );
274
275 $this->assertEquals( $expected, $actual );
276 }
277
278 public static function provideGetFullText() {
279 return [
280 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
281 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
282
283 // No capitalization or normalization is applied while formatting!
284 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
285 ];
286 }
287
288 /**
289 * @dataProvider provideGetFullText
290 */
291 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
292 $codec = $this->makeCodec( $lang );
293 $title = new TitleValue( $namespace, $dbkey, $fragment );
294
295 $actual = $codec->getFullText( $title );
296
297 $this->assertEquals( $expected, $actual );
298 }
299
300 public static function provideParseTitle() {
301 // TODO: test capitalization and trimming
302 // TODO: test unicode normalization
303
304 return [
305 [ ' : Hansi_Maier _ ', NS_MAIN, 'en',
306 new TitleValue( NS_MAIN, 'Hansi_Maier', '' ) ],
307 [ 'User:::1', NS_MAIN, 'de',
308 new TitleValue( NS_USER, '0:0:0:0:0:0:0:1', '' ) ],
309 [ ' lisa Müller', NS_USER, 'de',
310 new TitleValue( NS_USER, 'Lisa_Müller', '' ) ],
311 [ 'benutzerin:lisa Müller#stuff', NS_MAIN, 'de',
312 new TitleValue( NS_USER, 'Lisa_Müller', 'stuff' ) ],
313
314 [ ':Category:Quux', NS_MAIN, 'en',
315 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
316 [ 'Category:Quux', NS_MAIN, 'en',
317 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
318 [ 'Category:Quux', NS_CATEGORY, 'en',
319 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
320 [ 'Quux', NS_CATEGORY, 'en',
321 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
322 [ ':Quux', NS_CATEGORY, 'en',
323 new TitleValue( NS_MAIN, 'Quux', '' ) ],
324
325 // getGenderCache() provides a mock that considers first
326 // names ending in "a" to be female.
327
328 [ 'a b c', NS_MAIN, 'en',
329 new TitleValue( NS_MAIN, 'A_b_c' ) ],
330 [ ' a b c ', NS_MAIN, 'en',
331 new TitleValue( NS_MAIN, 'A_b_c' ) ],
332 [ ' _ Foo __ Bar_ _', NS_MAIN, 'en',
333 new TitleValue( NS_MAIN, 'Foo_Bar' ) ],
334
335 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
336 [ 'Sandbox', NS_MAIN, 'en', ],
337 [ 'A "B"', NS_MAIN, 'en', ],
338 [ 'A \'B\'', NS_MAIN, 'en', ],
339 [ '.com', NS_MAIN, 'en', ],
340 [ '~', NS_MAIN, 'en', ],
341 [ '"', NS_MAIN, 'en', ],
342 [ '\'', NS_MAIN, 'en', ],
343
344 [ 'Talk:Sandbox', NS_MAIN, 'en',
345 new TitleValue( NS_TALK, 'Sandbox' ) ],
346 [ 'Talk:Foo:Sandbox', NS_MAIN, 'en',
347 new TitleValue( NS_TALK, 'Foo:Sandbox' ) ],
348 [ 'File:Example.svg', NS_MAIN, 'en',
349 new TitleValue( NS_FILE, 'Example.svg' ) ],
350 [ 'File_talk:Example.svg', NS_MAIN, 'en',
351 new TitleValue( NS_FILE_TALK, 'Example.svg' ) ],
352 [ 'Foo/.../Sandbox', NS_MAIN, 'en',
353 'Foo/.../Sandbox' ],
354 [ 'Sandbox/...', NS_MAIN, 'en',
355 'Sandbox/...' ],
356 [ 'A~~', NS_MAIN, 'en',
357 'A~~' ],
358 // Length is 256 total, but only title part matters
359 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN, 'en',
360 new TitleValue( NS_CATEGORY,
361 'X' . str_repeat( 'x', 247 ) ) ],
362 [ str_repeat( 'x', 252 ), NS_MAIN, 'en',
363 'X' . str_repeat( 'x', 251 ) ],
364 // Test decoding and normalization
365 [ '&quot;n&#x303;&#34;', NS_MAIN, 'en', new TitleValue( NS_MAIN, '"ñ"' ) ],
366 [ 'X#n&#x303;', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'ñ' ) ],
367 // target section parsing
368 'empty fragment' => [ 'X#', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X' ) ],
369 'double hash' => [ 'X##', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', '#' ) ],
370 'fragment with hash' => [ 'X#z#z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z#z' ) ],
371 'fragment with space' => [ 'X#z z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z z' ) ],
372 'fragment with percent' => [ 'X#z%z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z%z' ) ],
373 'fragment with amp' => [ 'X#z&z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z&z' ) ],
374 ];
375 }
376
377 /**
378 * @dataProvider provideParseTitle
379 */
380 public function testParseTitle( $text, $ns, $lang, $title = null ) {
381 if ( $title === null ) {
382 $title = str_replace( ' ', '_', trim( $text ) );
383 }
384
385 if ( is_string( $title ) ) {
386 $title = new TitleValue( NS_MAIN, $title, '' );
387 }
388
389 $codec = $this->makeCodec( $lang );
390 $actual = $codec->parseTitle( $text, $ns );
391
392 $this->assertEquals( $title, $actual );
393 }
394
395 public static function provideParseTitle_invalid() {
396 // TODO: test unicode errors
397
398 return [
399 [ '#' ],
400 [ '::' ],
401 [ '::xx' ],
402 [ '::##' ],
403 [ ' :: x' ],
404
405 [ 'Talk:File:Foo.jpg' ],
406 [ 'Talk:localtestiw:Foo' ],
407 [ '::1' ], // only valid in user namespace
408 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
409
410 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
411 [ '' ],
412 [ ':' ],
413 [ '__ __' ],
414 [ ' __ ' ],
415 // Bad characters forbidden regardless of wgLegalTitleChars
416 [ 'A [ B' ],
417 [ 'A ] B' ],
418 [ 'A { B' ],
419 [ 'A } B' ],
420 [ 'A < B' ],
421 [ 'A > B' ],
422 [ 'A | B' ],
423 // URL encoding
424 [ 'A%20B' ],
425 [ 'A%23B' ],
426 [ 'A%2523B' ],
427 // XML/HTML character entity references
428 // Note: Commented out because they are not marked invalid by the PHP test as
429 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
430 // [ 'A &eacute; B' ],
431 // [ 'A &#233; B' ],
432 // [ 'A &#x00E9; B' ],
433 // Subject of NS_TALK does not roundtrip to NS_MAIN
434 [ 'Talk:File:Example.svg' ],
435 // Directory navigation
436 [ '.' ],
437 [ '..' ],
438 [ './Sandbox' ],
439 [ '../Sandbox' ],
440 [ 'Foo/./Sandbox' ],
441 [ 'Foo/../Sandbox' ],
442 [ 'Sandbox/.' ],
443 [ 'Sandbox/..' ],
444 // Tilde
445 [ 'A ~~~ Name' ],
446 [ 'A ~~~~ Signature' ],
447 [ 'A ~~~~~ Timestamp' ],
448 [ str_repeat( 'x', 256 ) ],
449 // Namespace prefix without actual title
450 [ 'Talk:' ],
451 [ 'Category: ' ],
452 [ 'Category: #bar' ]
453 ];
454 }
455
456 /**
457 * @dataProvider provideParseTitle_invalid
458 */
459 public function testParseTitle_invalid( $text ) {
460 $this->setExpectedException( MalformedTitleException::class );
461
462 $codec = $this->makeCodec( 'en' );
463 $codec->parseTitle( $text, NS_MAIN );
464 }
465
466 /**
467 * @dataProvider provideMakeTitleValueSafe
468 * @covers IP::sanitizeIP
469 */
470 public function testMakeTitleValueSafe(
471 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
472 ) {
473 $codec = $this->makeCodec( $lang );
474 $this->assertEquals( $expected,
475 $codec->makeTitleValueSafe( $ns, $text, $fragment, $interwiki ) );
476 }
477
478 /**
479 * @dataProvider provideMakeTitleValueSafe
480 * @covers Title::makeTitleSafe
481 * @covers Title::makeName
482 * @covers Title::secureAndSplit
483 * @covers IP::sanitizeIP
484 */
485 public function testMakeTitleSafe(
486 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
487 ) {
488 $codec = $this->makeCodec( $lang );
489 $this->setService( 'TitleParser', $codec );
490 $this->setService( 'TitleFormatter', $codec );
491
492 $actual = Title::makeTitleSafe( $ns, $text, $fragment, $interwiki );
493 // We need to reset some members so equality testing works
494 if ( $actual ) {
495 $wrapper = TestingAccessWrapper::newFromObject( $actual );
496 $wrapper->mArticleID = $actual->getNamespace() === NS_SPECIAL ? 0 : -1;
497 $wrapper->mLocalInterwiki = false;
498 $wrapper->mUserCaseDBKey = null;
499 }
500 $this->assertEquals( Title::castFromLinkTarget( $expected ), $actual );
501 }
502
503 public static function provideMakeTitleValueSafe() {
504 $ret = [
505 'Nonexistent NS' => [ null, 942929, 'Test' ],
506 'Simple page' => [ new TitleValue( NS_MAIN, 'Test' ), NS_MAIN, 'Test' ],
507
508 // Fragments
509 'Passed fragment' => [
510 new TitleValue( NS_MAIN, 'Test', 'Fragment' ),
511 NS_MAIN, 'Test', 'Fragment'
512 ],
513 'Embedded fragment' => [
514 new TitleValue( NS_MAIN, 'Test', 'Fragment' ),
515 NS_MAIN, 'Test#Fragment'
516 ],
517 'Passed fragment with spaces' => [
518 // XXX Leading space is okay in fragment?
519 new TitleValue( NS_MAIN, 'Test', ' Frag ment' ),
520 NS_MAIN, ' Test ', " Frag_ment "
521 ],
522 'Embedded fragment with spaces' => [
523 // XXX Leading space is okay in fragment?
524 new TitleValue( NS_MAIN, 'Test', ' Frag ment' ),
525 NS_MAIN, " Test # Frag_ment "
526 ],
527 // XXX Is it correct that these aren't normalized to spaces?
528 'Passed fragment with leading tab' => [ null, NS_MAIN, "\tTest\t", "\tFragment" ],
529 'Embedded fragment with leading tab' => [ null, NS_MAIN, "\tTest\t#\tFragment" ],
530 'Passed fragment with trailing tab' => [ null, NS_MAIN, "\tTest\t", "Fragment\t" ],
531 'Embedded fragment with trailing tab' => [ null, NS_MAIN, "\tTest\t#Fragment\t" ],
532 'Passed fragment with interior tab' => [ null, NS_MAIN, "\tTest\t", "Frag\tment" ],
533 'Embedded fragment with interior tab' => [ null, NS_MAIN, "\tTest\t#\tFrag\tment" ],
534
535 // Interwikis
536 'Passed local interwiki' => [
537 new TitleValue( NS_MAIN, 'Test' ),
538 NS_MAIN, 'Test', '', 'localtestiw'
539 ],
540 'Embedded local interwiki' => [
541 new TitleValue( NS_MAIN, 'Test' ),
542 NS_MAIN, 'localtestiw:Test'
543 ],
544 'Passed remote interwiki' => [
545 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
546 NS_MAIN, 'Test', '', 'remotetestiw'
547 ],
548 'Embedded remote interwiki' => [
549 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
550 NS_MAIN, 'remotetestiw:Test'
551 ],
552 // XXX Are these correct? Interwiki prefixes are case-sensitive?
553 'Passed local interwiki with different case' => [
554 new TitleValue( NS_MAIN, 'LocalTestIW:Test' ),
555 NS_MAIN, 'Test', '', 'LocalTestIW'
556 ],
557 'Embedded local interwiki with different case' => [
558 new TitleValue( NS_MAIN, 'LocalTestIW:Test' ),
559 NS_MAIN, 'LocalTestIW:Test'
560 ],
561 'Passed remote interwiki with different case' => [
562 new TitleValue( NS_MAIN, 'RemoteTestIW:Test' ),
563 NS_MAIN, 'Test', '', 'RemoteTestIW'
564 ],
565 'Embedded remote interwiki with different case' => [
566 new TitleValue( NS_MAIN, 'RemoteTestIW:Test' ),
567 NS_MAIN, 'RemoteTestIW:Test'
568 ],
569 'Passed local interwiki with lowercase page name' => [
570 new TitleValue( NS_MAIN, 'Test' ),
571 NS_MAIN, 'test', '', 'localtestiw'
572 ],
573 'Embedded local interwiki with lowercase page name' => [
574 new TitleValue( NS_MAIN, 'Test' ),
575 NS_MAIN, 'localtestiw:test'
576 ],
577 // For remote we don't auto-capitalize
578 'Passed remote interwiki with lowercase page name' => [
579 new TitleValue( NS_MAIN, 'test', '', 'remotetestiw' ),
580 NS_MAIN, 'test', '', 'remotetestiw'
581 ],
582 'Embedded remote interwiki with lowercase page name' => [
583 new TitleValue( NS_MAIN, 'test', '', 'remotetestiw' ),
584 NS_MAIN, 'remotetestiw:test'
585 ],
586
587 // Fragment and interwiki
588 'Fragment and local interwiki' => [
589 new TitleValue( NS_MAIN, 'Test', 'Fragment' ),
590 NS_MAIN, 'Test', 'Fragment', 'localtestiw'
591 ],
592 'Fragment and remote interwiki' => [
593 new TitleValue( NS_MAIN, 'Test', 'Fragment', 'remotetestiw' ),
594 NS_MAIN, 'Test', 'Fragment', 'remotetestiw'
595 ],
596 'Fragment and local interwiki and non-main namespace' => [
597 new TitleValue( NS_TALK, 'Test', 'Fragment' ),
598 NS_TALK, 'Test', 'Fragment', 'localtestiw'
599 ],
600 // We don't know the foreign wiki's namespaces, so it will always be NS_MAIN
601 'Fragment and remote interwiki and non-main namespace' => [
602 new TitleValue( NS_MAIN, 'Talk:Test', 'Fragment', 'remotetestiw' ),
603 NS_TALK, 'Test', 'Fragment', 'remotetestiw'
604 ],
605
606 // Whitespace normalization and Unicode stripping
607 'Name with space' => [
608 new TitleValue( NS_MAIN, 'Test_test' ),
609 NS_MAIN, 'Test test'
610 ],
611 'Unicode bidi override characters' => [
612 new TitleValue( NS_MAIN, 'Test' ),
613 NS_MAIN, "\u{200E}T\u{200F}e\u{202A}s\u{202B}t\u{202C}\u{202D}\u{202E}"
614 ],
615 'Invalid UTF-8 sequence' => [ null, NS_MAIN, "Te\x80\xf0st" ],
616 'Whitespace collapsing' => [
617 new TitleValue( NS_MAIN, 'Test_test' ),
618 NS_MAIN, "Test _\u{00A0}\u{1680}\u{180E}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}" .
619 "\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}" .
620 "\u{205F}\u{3000}test"
621 ],
622 'UTF8_REPLACEMENT' => [ null, NS_MAIN, UtfNormal\Constants::UTF8_REPLACEMENT ],
623
624 // Namespace prefixes
625 'Talk:Test' => [
626 new TitleValue( NS_TALK, 'Test' ),
627 NS_MAIN, 'Talk:Test'
628 ],
629 'Test in talk NS' => [
630 new TitleValue( NS_TALK, 'Test' ),
631 NS_TALK, 'Test'
632 ],
633 'Talkk:Test' => [
634 new TitleValue( NS_MAIN, 'Talkk:Test' ),
635 NS_MAIN, 'Talkk:Test'
636 ],
637 'Talk:Talk:Test' => [ null, NS_MAIN, 'Talk:Talk:Test' ],
638 'Talk:User:Test' => [ null, NS_MAIN, 'Talk:User:Test' ],
639 'User:Talk:Test' => [
640 new TitleValue( NS_USER, 'Talk:Test' ),
641 NS_MAIN, 'User:Talk:Test'
642 ],
643 'User:Test in talk NS' => [ null, NS_TALK, 'User:Test' ],
644 'Talk:Test in talk NS' => [ null, NS_TALK, 'Talk:Test' ],
645 'User:Test in user NS' => [
646 new TitleValue( NS_USER, 'User:Test' ),
647 NS_USER, 'User:Test'
648 ],
649 'Talk:Test in user NS' => [
650 new TitleValue( NS_USER, 'Talk:Test' ),
651 NS_USER, 'Talk:Test'
652 ],
653
654 // Initial colon
655 ':Test' => [
656 new TitleValue( NS_MAIN, 'Test' ),
657 NS_MAIN, ':Test'
658 ],
659 ':Talk:Test' => [
660 new TitleValue( NS_TALK, 'Test' ),
661 NS_MAIN, ':Talk:Test'
662 ],
663 ':localtestiw:Test' => [
664 new TitleValue( NS_MAIN, 'Test' ),
665 NS_MAIN, ':localtestiw:Test'
666 ],
667 ':remotetestiw:Test' => [
668 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
669 NS_MAIN, ':remotetestiw:Test'
670 ],
671 // XXX Is this correct? Why is it different from remote?
672 'localtestiw::Test' => [ null, NS_MAIN, 'localtestiw::Test' ],
673 'remotetestiw::Test' => [
674 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
675 NS_MAIN, 'remotetestiw::Test'
676 ],
677 // XXX Is this correct? Why is it different from remote?
678 'localtestiw:: Test' => [ null, NS_MAIN, 'localtestiw:: Test' ],
679 'remotetestiw:: Test' => [
680 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
681 NS_MAIN, 'remotetestiw:: Test'
682 ],
683
684 // Empty titles
685 'Empty title' => [ null, NS_MAIN, '' ],
686 'Empty title with namespace' => [ null, NS_USER, '' ],
687 'Local interwiki with empty page name' => [
688 new TitleValue( NS_MAIN, 'Main_Page' ),
689 NS_MAIN, 'localtestiw:'
690 ],
691 'Remote interwiki with empty page name' => [
692 // XXX Is this correct? This is supposed to redirect to the main page remotely?
693 new TitleValue( NS_MAIN, '', '', 'remotetestiw' ),
694 NS_MAIN, 'remotetestiw:'
695 ],
696
697 // Whitespace-only titles
698 'Whitespace-only title' => [ null, NS_MAIN, "\t\n" ],
699 'Whitespace-only title with namespace' => [ null, NS_USER, " _ " ],
700 'Local interwiki with whitespace-only page name' => [
701 // XXX Is whitespace-only really supposed to be different from empty?
702 null,
703 NS_MAIN, "localtestiw:_\t"
704 ],
705 'Remote interwiki with whitespace-only page name' => [
706 // XXX Is whitespace-only really supposed to be different from empty?
707 null,
708 NS_MAIN, "remotetestiw:\t_\n\r"
709 ],
710
711 // Namespace and interwiki
712 'Talk:localtestiw:Test' => [ null, NS_MAIN, 'Talk:localtestiw:Test' ],
713 'Talk:remotetestiw:Test' => [ null, NS_MAIN, 'Talk:remotetestiw:Test' ],
714 'User:localtestiw:Test' => [
715 new TitleValue( NS_USER, 'Localtestiw:Test' ),
716 NS_MAIN, 'User:localtestiw:Test'
717 ],
718 'User:remotetestiw:Test' => [
719 new TitleValue( NS_USER, 'Remotetestiw:Test' ),
720 NS_MAIN, 'User:remotetestiw:Test'
721 ],
722 'localtestiw:Test in user namespace' => [
723 new TitleValue( NS_USER, 'Localtestiw:Test' ),
724 NS_USER, 'localtestiw:Test'
725 ],
726 'remotetestiw:Test in user namespace' => [
727 new TitleValue( NS_USER, 'Remotetestiw:Test' ),
728 NS_USER, 'remotetestiw:Test'
729 ],
730 'localtestiw:talk:test' => [
731 new TitleValue( NS_TALK, 'Test' ),
732 NS_MAIN, 'localtestiw:talk:test'
733 ],
734 'remotetestiw:talk:test' => [
735 new TitleValue( NS_MAIN, 'talk:test', '', 'remotetestiw' ),
736 NS_MAIN, 'remotetestiw:talk:test'
737 ],
738
739 // Invalid chars
740 'Test[test' => [ null, NS_MAIN, 'Test[test' ],
741
742 // Long titles
743 '255 chars long' => [
744 new TitleValue( NS_MAIN, str_repeat( 'A', 255 ) ),
745 NS_MAIN, str_repeat( 'A', 255 )
746 ],
747 '255 chars long in user NS' => [
748 new TitleValue( NS_USER, str_repeat( 'A', 255 ) ),
749 NS_USER, str_repeat( 'A', 255 )
750 ],
751 'User:255 chars long' => [
752 new TitleValue( NS_USER, str_repeat( 'A', 255 ) ),
753 NS_MAIN, 'User:' . str_repeat( 'A', 255 )
754 ],
755 '256 chars long' => [ null, NS_MAIN, str_repeat( 'A', 256 ) ],
756 '256 chars long in user NS' => [ null, NS_USER, str_repeat( 'A', 256 ) ],
757 'User:256 chars long' => [ null, NS_MAIN, 'User:' . str_repeat( 'A', 256 ) ],
758
759 '512 chars long in special NS' => [
760 new TitleValue( NS_SPECIAL, str_repeat( 'A', 512 ) ),
761 NS_SPECIAL, str_repeat( 'A', 512 )
762 ],
763 'Special:512 chars long' => [
764 new TitleValue( NS_SPECIAL, str_repeat( 'A', 512 ) ),
765 NS_MAIN, 'Special:' . str_repeat( 'A', 512 )
766 ],
767 '513 chars long in special NS' => [ null, NS_SPECIAL, str_repeat( 'A', 513 ) ],
768 'Special:513 chars long' => [ null, NS_MAIN, 'Special:' . str_repeat( 'A', 513 ) ],
769
770 // IP addresses
771 'User:000.000.000' => [
772 new TitleValue( NS_USER, '000.000.000' ),
773 NS_MAIN, 'User:000.000.000'
774 ],
775 'User:000.000.000.000' => [
776 new TitleValue( NS_USER, '0.0.0.0' ),
777 NS_MAIN, 'User:000.000.000.000'
778 ],
779 '000.000.000.000' => [
780 new TitleValue( NS_MAIN, '000.000.000.000' ),
781 NS_MAIN, '000.000.000.000'
782 ],
783 'User:1.1.256.000' => [
784 new TitleValue( NS_USER, '1.1.256.000' ),
785 NS_MAIN, 'User:1.1.256.000'
786 ],
787 'User:1.1.255.000' => [
788 new TitleValue( NS_USER, '1.1.255.0' ),
789 NS_MAIN, 'User:1.1.255.000'
790 ],
791 // TODO More IP address sanitization tests
792 ];
793
794 // Invalid and valid dots
795 foreach ( [ '.', '..', '...' ] as $dots ) {
796 foreach ( [ '?', '?/', '?/Test', 'Test/?/Test', '/?', 'Test/?', '?Test', 'Test?Test',
797 'Test?' ] as $pattern ) {
798 $test = str_replace( '?', $dots, $pattern );
799 if ( $dots === '...' || in_array( $pattern, [ '?Test', 'Test?Test', 'Test?' ] ) ) {
800 $expectedMain = new TitleValue( NS_MAIN, $test );
801 $expectedUser = new TitleValue( NS_USER, $test );
802 } else {
803 $expectedMain = $expectedUser = null;
804 }
805 $ret[$test] = [ $expectedMain, NS_MAIN, $test ];
806 $ret["$test in user NS"] = [ $expectedUser, NS_USER, $test ];
807 $ret["User:$test"] = [ $expectedUser, NS_MAIN, "User:$test" ];
808 }
809 }
810
811 // Invalid and valid tildes
812 foreach ( [ '~~', '~~~' ] as $tildes ) {
813 foreach ( [ '?', 'Test?', '?Test', 'Test?Test' ] as $pattern ) {
814 $test = str_replace( '?', $tildes, $pattern );
815 if ( $tildes === '~~' ) {
816 $expectedMain = new TitleValue( NS_MAIN, $test );
817 $expectedUser = new TitleValue( NS_USER, $test );
818 } else {
819 $expectedMain = $expectedUser = null;
820 }
821 $ret[$test] = [ $expectedMain, NS_MAIN, $test ];
822 $ret["$test in user NS"] = [ $expectedUser, NS_USER, $test ];
823 $ret["User:$test"] = [ $expectedUser, NS_MAIN, "User:$test" ];
824 }
825 }
826
827 return $ret;
828 }
829
830 public static function provideGetNamespaceName() {
831 return [
832 [ NS_MAIN, 'Foo', 'en', '' ],
833 [ NS_USER, 'Foo', 'en', 'User' ],
834 [ NS_USER, 'Hansi Maier', 'de', 'Benutzer' ],
835
836 // getGenderCache() provides a mock that considers first
837 // names ending in "a" to be female.
838 [ NS_USER, 'Lisa Müller', 'de', 'Benutzerin' ],
839 ];
840 }
841
842 /**
843 * @dataProvider provideGetNamespaceName
844 */
845 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
846 $codec = $this->makeCodec( $lang );
847 $name = $codec->getNamespaceName( $namespace, $text );
848
849 $this->assertEquals( $expected, $name );
850 }
851 }