Merge "mediawiki.content.json: Remove file and author annotations"
[mediawiki.git] / tests / phpunit / includes / title / MediaWikiTitleCodecTest.php
blob664f12d4821d27f48de8ebf4ab66faf97b42a66e
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.
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.
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
18 * @file
19 * @author Daniel Kinzler
22 use MediaWiki\Cache\GenderCache;
23 use MediaWiki\Interwiki\InterwikiLookup;
24 use MediaWiki\MainConfigNames;
25 use MediaWiki\Page\PageIdentity;
26 use MediaWiki\Page\PageIdentityValue;
27 use MediaWiki\Tests\Unit\DummyServicesTrait;
28 use MediaWiki\Title\MalformedTitleException;
29 use MediaWiki\Title\MediaWikiTitleCodec;
30 use MediaWiki\Title\NamespaceInfo;
31 use MediaWiki\Title\Title;
32 use MediaWiki\Title\TitleValue;
34 /**
35 * @covers \MediaWiki\Title\MediaWikiTitleCodec
37 * @group Title
38 * @group Database
39 * ^--- needed because of global state in
41 class MediaWikiTitleCodecTest extends MediaWikiIntegrationTestCase {
42 use DummyServicesTrait;
44 protected function setUp(): void {
45 parent::setUp();
47 $this->overrideConfigValues( [
48 MainConfigNames::AllowUserJs => false,
49 MainConfigNames::DefaultLanguageVariant => false,
50 MainConfigNames::MetaNamespace => 'Project',
51 MainConfigNames::LocalInterwikis => [ 'localtestiw' ],
52 MainConfigNames::CapitalLinks => true,
53 MainConfigNames::LanguageCode => 'en',
54 ] );
55 $this->setUserLang( 'en' );
58 /**
59 * Returns a mock GenderCache that will consider a user "female" if the
60 * first part of the user name ends with "a".
62 * @return GenderCache
64 private function getGenderCache() {
65 $genderCache = $this->createMock( GenderCache::class );
67 $genderCache->method( 'getGenderOf' )
68 ->willReturnCallback( static function ( $userName ) {
69 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ? 'female' : 'male';
70 } );
72 return $genderCache;
75 /**
76 * Returns a InterwikiLookup where the only valid interwikis are 'localtestiw' and 'remotetestiw'.
77 * Only `isValidInterwiki` should actually be needed.
79 private function getInterwikiLookup(): InterwikiLookup {
80 return $this->getDummyInterwikiLookup( [ 'localtestiw', 'remotetestiw' ] );
83 /**
84 * Returns a NamespaceInfo where the only namespaces that exist are NS_SPECIAL, NS_MAIN, NS_TALK,
85 * NS_USER, and NS_USER_TALK. As per the real NamespaceInfo, NS_USER and NS_USER_TALK have
86 * gender distinctions. All namespaces are capitalized.
88 private function getNamespaceInfo(): NamespaceInfo {
89 return $this->getDummyNamespaceInfo( [
90 MainConfigNames::CanonicalNamespaceNames => [
91 NS_SPECIAL => 'Special',
92 NS_MAIN => '',
93 NS_TALK => 'Talk',
94 NS_USER => 'User',
95 NS_USER_TALK => 'User_talk',
97 MainConfigNames::CapitalLinks => true,
98 ] );
101 protected function makeCodec( $lang ) {
102 return new MediaWikiTitleCodec(
103 $this->getServiceContainer()->getLanguageFactory()->getLanguage( $lang ),
104 $this->getGenderCache(),
105 [ 'localtestiw' ],
106 $this->getInterwikiLookup(),
107 $this->getNamespaceInfo()
111 public static function provideFormat() {
112 return [
113 [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo Bar' ],
114 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi Maier#stuff and so on' ],
115 [ false, 'Hansi_Maier', '', '', 'en', 'Hansi Maier' ],
117 NS_USER_TALK,
118 'hansi__maier',
121 'en',
122 'User talk:hansi maier',
123 'User talk:Hansi maier'
126 // getGenderCache() provides a mock that considers first
127 // names ending in "a" to be female.
128 [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa Müller' ],
129 [ NS_MAIN, 'FooBar', '', 'remotetestiw', 'en', 'remotetestiw:FooBar' ],
134 * @dataProvider provideFormat
136 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
137 $normalized = null
139 $normalized ??= $expected;
141 $codec = $this->makeCodec( $lang );
142 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
144 $this->assertEquals( $expected, $actual, 'formatted' );
146 // test round trip
147 $parsed = $codec->parseTitle( $actual, NS_MAIN );
148 $actual2 = $codec->formatTitle(
149 $parsed->getNamespace(),
150 $parsed->getText(),
151 $parsed->getFragment(),
152 $parsed->getInterwiki()
155 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
158 public static function provideGetText() {
159 // $title = new TitleValue( $namespace, $dbkey, $fragment );
160 return [
161 [ new TitleValue( NS_MAIN, 'Foo_Bar', '' ), 'en', 'Foo Bar' ],
162 [ new TitleValue( NS_USER, 'Hansi_Maier', 'stuff_and_so_on' ), 'en', 'Hansi Maier' ],
163 [ new PageIdentityValue( 37, NS_MAIN, 'Foo_Bar', PageIdentity::LOCAL ), 'en', 'Foo Bar' ],
164 [ new PageIdentityValue( 37, NS_USER, 'Hansi_Maier', PageIdentity::LOCAL ), 'en', 'Hansi Maier' ],
169 * @dataProvider provideGetText
171 public function testGetText( $title, $lang, $expected ) {
172 $codec = $this->makeCodec( $lang );
173 $actual = $codec->getText( $title );
175 $this->assertEquals( $expected, $actual );
178 public static function provideGetPrefixedText() {
179 return [
180 [ new TitleValue( NS_MAIN, 'Foo_Bar', '' ), 'en', 'Foo Bar' ],
181 [ new TitleValue( NS_USER, 'Hansi_Maier', 'stuff_and_so_on' ), 'en', 'User:Hansi Maier' ],
183 // No capitalization or normalization is applied while formatting!
184 [ new TitleValue( NS_USER_TALK, 'hansi__maier', '' ), 'en', 'User talk:hansi maier' ],
186 // getGenderCache() provides a mock that considers first
187 // names ending in "a" to be female.
189 new TitleValue( NS_USER, 'Lisa_Müller', '' ),
190 'de', 'Benutzerin:Lisa Müller'
193 new TitleValue( 1000000, 'Invalid_namespace', '' ),
194 'en',
195 'Special:Badtitle/NS1000000:Invalid namespace'
198 new PageIdentityValue( 37, NS_MAIN, 'Foo_Bar', PageIdentity::LOCAL ),
199 'en',
200 'Foo Bar'
203 new PageIdentityValue( 37, NS_USER, 'Hansi_Maier', PageIdentity::LOCAL ),
204 'en',
205 'User:Hansi Maier'
208 new PageIdentityValue( 37, NS_USER_TALK, 'hansi__maier', PageIdentity::LOCAL ),
209 'en',
210 'User talk:hansi maier'
213 new PageIdentityValue( 37, NS_USER, 'Lisa_Müller', PageIdentity::LOCAL ),
214 'de',
215 'Benutzerin:Lisa Müller'
218 new PageIdentityValue( 37, 1000000, 'Invalid_namespace', PageIdentity::LOCAL ),
219 'en',
220 'Special:Badtitle/NS1000000:Invalid namespace'
226 * @dataProvider provideGetPrefixedText
228 public function testGetPrefixedText( $title, $lang, $expected ) {
229 $codec = $this->makeCodec( $lang );
230 $actual = $codec->getPrefixedText( $title );
232 $this->assertEquals( $expected, $actual );
235 public static function provideGetPrefixedDBkey() {
236 return [
237 [ new TitleValue( NS_MAIN, 'Foo_Bar', '', '' ), 'en', 'Foo_Bar' ],
238 [ new TitleValue( NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '' ), 'en', 'User:Hansi_Maier' ],
240 // No capitalization or normalization is applied while formatting!
241 [ new TitleValue( NS_USER_TALK, 'hansi__maier', '', '' ), 'en', 'User_talk:hansi__maier' ],
243 // getGenderCache() provides a mock that considers first
244 // names ending in "a" to be female.
245 [ new TitleValue( NS_USER, 'Lisa_Müller', '', '' ), 'de', 'Benutzerin:Lisa_Müller' ],
247 [ new TitleValue( NS_MAIN, 'Remote_page', '', 'remotetestiw' ), 'en', 'remotetestiw:Remote_page' ],
249 // non-existent namespace
250 [ new TitleValue( 10000000, 'Foobar', '', '' ), 'en', 'Special:Badtitle/NS10000000:Foobar' ],
253 new PageIdentityValue( 37, NS_MAIN, 'Foo_Bar', PageIdentity::LOCAL ),
254 'en',
255 'Foo_Bar'
258 new PageIdentityValue( 37, NS_USER, 'Hansi_Maier', PageIdentity::LOCAL ),
259 'en',
260 'User:Hansi_Maier'
263 new PageIdentityValue( 37, NS_USER_TALK, 'hansi__maier', PageIdentity::LOCAL ),
264 'en',
265 'User_talk:hansi__maier'
268 new PageIdentityValue( 37, NS_USER, 'Lisa_Müller', PageIdentity::LOCAL ),
269 'de',
270 'Benutzerin:Lisa_Müller'
273 new PageIdentityValue( 37, NS_MAIN, 'Remote_page', PageIdentity::LOCAL ),
274 'en',
275 'Remote_page'
278 new PageIdentityValue( 37, 10000000, 'Foobar', PageIdentity::LOCAL ),
279 'en',
280 'Special:Badtitle/NS10000000:Foobar'
286 * @dataProvider provideGetPrefixedDBkey
288 public function testGetPrefixedDBkey( $title, $lang, $expected
290 $codec = $this->makeCodec( $lang );
291 $actual = $codec->getPrefixedDBkey( $title );
293 $this->assertEquals( $expected, $actual );
296 public static function provideGetFullText() {
297 return [
298 [ new TitleValue( NS_MAIN, 'Foo_Bar', '' ), 'en', 'Foo Bar' ],
299 [ new TitleValue( NS_USER, 'Hansi_Maier', 'stuff_and_so_on' ), 'en', 'User:Hansi Maier#stuff and so on' ],
301 // No capitalization or normalization is applied while formatting!
302 [ new TitleValue( NS_USER_TALK, 'hansi__maier', '' ), 'en', 'User talk:hansi maier' ],
304 [ new TitleValue( NS_MAIN, 'Foo_Bar' ), 'en', 'Foo Bar' ],
305 [ new TitleValue( NS_USER, 'Hansi_Maier' ), 'en', 'User:Hansi Maier' ],
308 new PageIdentityValue( 37, NS_MAIN, 'Foo_Bar', PageIdentity::LOCAL ),
309 'en',
310 'Foo Bar'
313 new PageIdentityValue( 37, NS_USER, 'Hansi_Maier', PageIdentity::LOCAL ),
314 'en',
315 'User:Hansi Maier'
318 new PageIdentityValue( 37, NS_USER_TALK, 'hansi__maier', PageIdentity::LOCAL ),
319 'en',
320 'User talk:hansi maier'
326 * @dataProvider provideGetFullText
328 public function testGetFullText( $title, $lang, $expected ) {
329 $codec = $this->makeCodec( $lang );
330 $actual = $codec->getFullText( $title );
332 $this->assertEquals( $expected, $actual );
335 public static function provideParseTitle() {
336 // TODO: test capitalization and trimming
337 // TODO: test unicode normalization
339 return [
340 [ ' : Hansi_Maier _ ', NS_MAIN, 'en',
341 new TitleValue( NS_MAIN, 'Hansi_Maier', '' ) ],
342 [ 'User:::1', NS_MAIN, 'de',
343 new TitleValue( NS_USER, '0:0:0:0:0:0:0:1', '' ) ],
344 [ ' lisa Müller', NS_USER, 'de',
345 new TitleValue( NS_USER, 'Lisa_Müller', '' ) ],
346 [ 'benutzerin:lisa Müller#stuff', NS_MAIN, 'de',
347 new TitleValue( NS_USER, 'Lisa_Müller', 'stuff' ) ],
349 [ ':Category:Quux', NS_MAIN, 'en',
350 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
351 [ 'Category:Quux', NS_MAIN, 'en',
352 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
353 [ 'Category:Quux', NS_CATEGORY, 'en',
354 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
355 [ 'Quux', NS_CATEGORY, 'en',
356 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
357 [ ':Quux', NS_CATEGORY, 'en',
358 new TitleValue( NS_MAIN, 'Quux', '' ) ],
360 // getGenderCache() provides a mock that considers first
361 // names ending in "a" to be female.
363 [ 'a b c', NS_MAIN, 'en',
364 new TitleValue( NS_MAIN, 'A_b_c' ) ],
365 [ ' a b c ', NS_MAIN, 'en',
366 new TitleValue( NS_MAIN, 'A_b_c' ) ],
367 [ ' _ Foo __ Bar_ _', NS_MAIN, 'en',
368 new TitleValue( NS_MAIN, 'Foo_Bar' ) ],
370 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
371 [ 'Sandbox', NS_MAIN, 'en', ],
372 [ 'A "B"', NS_MAIN, 'en', ],
373 [ 'A \'B\'', NS_MAIN, 'en', ],
374 [ '.com', NS_MAIN, 'en', ],
375 [ '~', NS_MAIN, 'en', ],
376 [ '"', NS_MAIN, 'en', ],
377 [ '\'', NS_MAIN, 'en', ],
379 [ 'Talk:Sandbox', NS_MAIN, 'en',
380 new TitleValue( NS_TALK, 'Sandbox' ) ],
381 [ 'Talk:Foo:Sandbox', NS_MAIN, 'en',
382 new TitleValue( NS_TALK, 'Foo:Sandbox' ) ],
383 [ 'File:Example.svg', NS_MAIN, 'en',
384 new TitleValue( NS_FILE, 'Example.svg' ) ],
385 [ 'File_talk:Example.svg', NS_MAIN, 'en',
386 new TitleValue( NS_FILE_TALK, 'Example.svg' ) ],
387 [ 'Foo/.../Sandbox', NS_MAIN, 'en',
388 'Foo/.../Sandbox' ],
389 [ 'Sandbox/...', NS_MAIN, 'en',
390 'Sandbox/...' ],
391 [ 'A~~', NS_MAIN, 'en',
392 'A~~' ],
393 // Length is 256 total, but only title part matters
394 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN, 'en',
395 new TitleValue( NS_CATEGORY,
396 'X' . str_repeat( 'x', 247 ) ) ],
397 [ str_repeat( 'x', 252 ), NS_MAIN, 'en',
398 'X' . str_repeat( 'x', 251 ) ],
399 // Test decoding and normalization
400 [ '&quot;n&#x303;&#34;', NS_MAIN, 'en', new TitleValue( NS_MAIN, '"ñ"' ) ],
401 [ 'X#n&#x303;', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'ñ' ) ],
402 // target section parsing
403 'empty fragment' => [ 'X#', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X' ) ],
404 'only fragment' => [ '#', NS_MAIN, 'en', new TitleValue( NS_MAIN, '' ) ],
405 'double hash' => [ 'X##', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', '#' ) ],
406 'fragment with hash' => [ 'X#z#z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z#z' ) ],
407 'fragment with space' => [ 'X#z z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z z' ) ],
408 'fragment with percent' => [ 'X#z%z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z%z' ) ],
409 'fragment with amp' => [ 'X#z&z', NS_MAIN, 'en', new TitleValue( NS_MAIN, 'X', 'z&z' ) ],
410 'remotetestiw in user' => [ 'User:remotetestiw:', NS_MAIN, 'en', new TitleValue( NS_USER, 'Remotetestiw:' ) ],
415 * @dataProvider provideParseTitle
417 public function testParseTitle( $text, $ns, $lang, $title = null ) {
418 if ( !( $title instanceof TitleValue ) ) {
419 $title ??= str_replace( ' ', '_', trim( $text ) );
420 $title = new TitleValue( NS_MAIN, $title, '' );
423 $codec = $this->makeCodec( $lang );
424 $actual = $codec->parseTitle( $text, $ns );
426 $this->assertEquals( $title, $actual );
429 public static function provideParseTitle_invalid() {
430 return [
431 [ 'User:#' ],
432 [ '::' ],
433 [ '::xx' ],
434 [ '::##' ],
435 [ ' :: x' ],
437 [ 'Talk:File:Foo.jpg' ],
438 [ 'Talk:localtestiw:Foo' ],
439 [ '::1' ], // only valid in user namespace
440 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
441 [ 'remotetestiw:', NS_USER ],
443 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
444 [ '' ],
445 [ ':' ],
446 [ '__ __' ],
447 [ ' __ ' ],
448 // Bad characters forbidden regardless of wgLegalTitleChars
449 [ 'A [ B' ],
450 [ 'A ] B' ],
451 [ 'A { B' ],
452 [ 'A } B' ],
453 [ 'A < B' ],
454 [ 'A > B' ],
455 [ 'A | B' ],
456 // URL encoding
457 [ 'A%20B' ],
458 [ 'A%23B' ],
459 [ 'A%2523B' ],
460 // XML/HTML character entity references
461 // Note: Commented out because they are not marked invalid by the PHP test as
462 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
463 // [ 'A &eacute; B' ],
464 // [ 'A &#233; B' ],
465 // [ 'A &#x00E9; B' ],
466 // Subject of NS_TALK does not roundtrip to NS_MAIN
467 [ 'Talk:File:Example.svg' ],
468 // Directory navigation
469 [ '.' ],
470 [ '..' ],
471 [ './Sandbox' ],
472 [ '../Sandbox' ],
473 [ 'Foo/./Sandbox' ],
474 [ 'Foo/../Sandbox' ],
475 [ 'Sandbox/.' ],
476 [ 'Sandbox/..' ],
477 // Tilde
478 [ 'A ~~~ Name' ],
479 [ 'A ~~~~ Signature' ],
480 [ 'A ~~~~~ Timestamp' ],
481 [ str_repeat( 'x', 256 ) ],
482 // Namespace prefix without actual title
483 [ 'Talk:' ],
484 [ 'Category: ' ],
485 [ 'Category: #bar' ],
486 // Invalid Unicode
487 [ "Apollo\x96Soyuz" ],
488 // Input resulting from invalid Unicode being sanitized somewhere else
489 [ "Apollo\u{FFFD}Soyuz" ],
494 * @dataProvider provideParseTitle_invalid
496 public function testParseTitle_invalid( $text, $ns = NS_MAIN ) {
497 $this->expectException( MalformedTitleException::class );
499 $codec = $this->makeCodec( 'en' );
500 $codec->parseTitle( $text, $ns );
504 * @dataProvider provideMakeTitleValueSafe
506 public function testMakeTitleValueSafe(
507 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
509 $codec = $this->makeCodec( $lang );
510 $this->assertEquals( $expected,
511 $codec->makeTitleValueSafe( $ns, $text, $fragment, $interwiki ) );
515 * @dataProvider provideMakeTitleValueSafe
516 * @covers \MediaWiki\Title\Title::makeTitleSafe
517 * @covers \MediaWiki\Title\Title::makeName
518 * @covers \MediaWiki\Title\Title::secureAndSplit
520 public function testMakeTitleSafe(
521 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
523 $codec = $this->makeCodec( $lang );
524 $this->setService( 'TitleParser', $codec );
525 $this->setService( 'TitleFormatter', $codec );
527 $actual = Title::makeTitleSafe( $ns, $text, $fragment, $interwiki );
529 if ( $expected ) {
530 $this->assertNotNull( $actual );
531 $expectedTitle = Title::newFromLinkTarget( $expected );
532 $this->assertSame( $expectedTitle->getPrefixedDBkey(), $actual->getPrefixedDBkey() );
533 } else {
534 $this->assertNull( $actual );
538 public static function provideMakeTitleValueSafe() {
539 $ret = [
540 'Nonexistent NS' => [ null, 942929, 'Test' ],
541 'Linebreak in title' => [ null, NS_MAIN, "Test\nthis" ],
542 'Pipe in title' => [ null, NS_MAIN, "Test|this" ],
543 'Simple page' => [ new TitleValue( NS_MAIN, 'Test' ), NS_MAIN, 'Test' ],
545 // Fragments
546 'Passed fragment' => [
547 new TitleValue( NS_MAIN, 'Test', 'Fragment' ),
548 NS_MAIN, 'Test', 'Fragment'
550 'Embedded fragment' => [
551 new TitleValue( NS_MAIN, 'Test', 'Fragment' ),
552 NS_MAIN, 'Test#Fragment'
554 'Passed fragment with spaces' => [
555 // XXX Leading space is okay in fragment?
556 new TitleValue( NS_MAIN, 'Test', ' Frag ment' ),
557 NS_MAIN, ' Test ', " Frag_ment "
559 'Embedded fragment with spaces' => [
560 // XXX Leading space is okay in fragment?
561 new TitleValue( NS_MAIN, 'Test', ' Frag ment' ),
562 NS_MAIN, " Test # Frag_ment "
564 // XXX Is it correct that these aren't normalized to spaces?
565 'Passed fragment with leading tab' => [ null, NS_MAIN, "\tTest\t", "\tFragment" ],
566 'Embedded fragment with leading tab' => [ null, NS_MAIN, "\tTest\t#\tFragment" ],
567 'Passed fragment with trailing tab' => [ null, NS_MAIN, "\tTest\t", "Fragment\t" ],
568 'Embedded fragment with trailing tab' => [ null, NS_MAIN, "\tTest\t#Fragment\t" ],
569 'Passed fragment with interior tab' => [ null, NS_MAIN, "\tTest\t", "Frag\tment" ],
570 'Embedded fragment with interior tab' => [ null, NS_MAIN, "\tTest\t#\tFrag\tment" ],
572 // Interwikis
573 'Passed local interwiki' => [
574 new TitleValue( NS_MAIN, 'Test' ),
575 NS_MAIN, 'Test', '', 'localtestiw'
577 'Embedded local interwiki' => [
578 new TitleValue( NS_MAIN, 'Test' ),
579 NS_MAIN, 'localtestiw:Test'
581 'Passed remote interwiki' => [
582 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
583 NS_MAIN, 'Test', '', 'remotetestiw'
585 'Embedded remote interwiki' => [
586 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
587 NS_MAIN, 'remotetestiw:Test'
589 // Interwiki prefixes are not case sensitive
590 'Passed local interwiki with different case' => [
591 new TitleValue( NS_MAIN, 'Test' ),
592 NS_MAIN, 'Test', '', 'LocalTestIW'
594 'Embedded local interwiki with different case' => [
595 new TitleValue( NS_MAIN, 'Test' ),
596 NS_MAIN, 'LocalTestIW:Test'
598 'Passed remote interwiki with different case' => [
599 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
600 NS_MAIN, 'Test', '', 'RemoteTestIW'
602 'Embedded remote interwiki with different case' => [
603 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
604 NS_MAIN, 'RemoteTestIW:Test'
606 'Passed local interwiki with lowercase page name' => [
607 new TitleValue( NS_MAIN, 'Test' ),
608 NS_MAIN, 'test', '', 'localtestiw'
610 'Embedded local interwiki with lowercase page name' => [
611 new TitleValue( NS_MAIN, 'Test' ),
612 NS_MAIN, 'localtestiw:test'
614 // For remote we don't auto-capitalize
615 'Passed remote interwiki with lowercase page name' => [
616 new TitleValue( NS_MAIN, 'test', '', 'remotetestiw' ),
617 NS_MAIN, 'test', '', 'remotetestiw'
619 'Embedded remote interwiki with lowercase page name' => [
620 new TitleValue( NS_MAIN, 'test', '', 'remotetestiw' ),
621 NS_MAIN, 'remotetestiw:test'
624 // Fragment and interwiki
625 'Fragment and local interwiki' => [
626 new TitleValue( NS_MAIN, 'Test', 'Fragment' ),
627 NS_MAIN, 'Test', 'Fragment', 'localtestiw'
629 'Fragment and remote interwiki' => [
630 new TitleValue( NS_MAIN, 'Test', 'Fragment', 'remotetestiw' ),
631 NS_MAIN, 'Test', 'Fragment', 'remotetestiw'
633 'Fragment and local interwiki and non-main namespace' => [
634 new TitleValue( NS_TALK, 'Test', 'Fragment' ),
635 NS_TALK, 'Test', 'Fragment', 'localtestiw'
637 // We don't know the foreign wiki's namespaces, so it will always be NS_MAIN
638 'Fragment and remote interwiki and non-main namespace' => [
639 new TitleValue( NS_MAIN, 'Talk:Test', 'Fragment', 'remotetestiw' ),
640 NS_TALK, 'Test', 'Fragment', 'remotetestiw'
643 // Whitespace normalization and Unicode stripping
644 'Name with space' => [
645 new TitleValue( NS_MAIN, 'Test_test' ),
646 NS_MAIN, 'Test test'
648 'Unicode bidi override characters' => [
649 new TitleValue( NS_MAIN, 'Test' ),
650 NS_MAIN, "\u{200E}T\u{200F}e\u{202A}s\u{202B}t\u{202C}\u{202D}\u{202E}"
652 'Invalid UTF-8 sequence' => [ null, NS_MAIN, "Te\x80\xf0st" ],
653 'Whitespace collapsing' => [
654 new TitleValue( NS_MAIN, 'Test_test' ),
655 NS_MAIN, "Test _\u{00A0}\u{1680}\u{180E}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}" .
656 "\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}" .
657 "\u{205F}\u{3000}test"
659 'UTF8_REPLACEMENT' => [ null, NS_MAIN, UtfNormal\Constants::UTF8_REPLACEMENT ],
661 // Namespace prefixes
662 'Talk:Test' => [
663 new TitleValue( NS_TALK, 'Test' ),
664 NS_MAIN, 'Talk:Test'
666 'Test in talk NS' => [
667 new TitleValue( NS_TALK, 'Test' ),
668 NS_TALK, 'Test'
670 'Talkk:Test' => [
671 new TitleValue( NS_MAIN, 'Talkk:Test' ),
672 NS_MAIN, 'Talkk:Test'
674 'Talk:Talk:Test' => [ null, NS_MAIN, 'Talk:Talk:Test' ],
675 'Talk:User:Test' => [ null, NS_MAIN, 'Talk:User:Test' ],
676 'User:Talk:Test' => [
677 new TitleValue( NS_USER, 'Talk:Test' ),
678 NS_MAIN, 'User:Talk:Test'
680 'User:Test in talk NS' => [ null, NS_TALK, 'User:Test' ],
681 'Talk:Test in talk NS' => [ null, NS_TALK, 'Talk:Test' ],
682 'User:Test in user NS' => [
683 new TitleValue( NS_USER, 'User:Test' ),
684 NS_USER, 'User:Test'
686 'Talk:Test in user NS' => [
687 new TitleValue( NS_USER, 'Talk:Test' ),
688 NS_USER, 'Talk:Test'
691 // Initial colon
692 ':Test' => [
693 new TitleValue( NS_MAIN, 'Test' ),
694 NS_MAIN, ':Test'
696 ':Talk:Test' => [
697 new TitleValue( NS_TALK, 'Test' ),
698 NS_MAIN, ':Talk:Test'
700 ':localtestiw:Test' => [
701 new TitleValue( NS_MAIN, 'Test' ),
702 NS_MAIN, ':localtestiw:Test'
704 ':remotetestiw:Test' => [
705 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
706 NS_MAIN, ':remotetestiw:Test'
708 // XXX Is this correct? Why is it different from remote?
709 'localtestiw::Test' => [ null, NS_MAIN, 'localtestiw::Test' ],
710 'remotetestiw::Test' => [
711 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
712 NS_MAIN, 'remotetestiw::Test'
714 // XXX Is this correct? Why is it different from remote?
715 'localtestiw:: Test' => [ null, NS_MAIN, 'localtestiw:: Test' ],
716 'remotetestiw:: Test' => [
717 new TitleValue( NS_MAIN, 'Test', '', 'remotetestiw' ),
718 NS_MAIN, 'remotetestiw:: Test'
721 // Empty titles
722 'Empty title' => [ null, NS_MAIN, '' ],
723 'Empty title with namespace' => [ null, NS_USER, '' ],
724 'Local interwiki with empty page name' => [
725 new TitleValue( NS_MAIN, 'Main_Page' ),
726 NS_MAIN, 'localtestiw:'
728 'Remote interwiki with empty page name' => [
729 // XXX Is this correct? This is supposed to redirect to the main page remotely?
730 new TitleValue( NS_MAIN, '', '', 'remotetestiw' ),
731 NS_MAIN, 'remotetestiw:'
734 // Whitespace-only titles
735 'Whitespace-only title' => [ null, NS_MAIN, "\t\n" ],
736 'Whitespace-only title with namespace' => [ null, NS_USER, " _ " ],
737 'Local interwiki with whitespace-only page name' => [
738 // XXX Is whitespace-only really supposed to be different from empty?
739 null,
740 NS_MAIN, "localtestiw:_\t"
742 'Remote interwiki with whitespace-only page name' => [
743 // XXX Is whitespace-only really supposed to be different from empty?
744 null,
745 NS_MAIN, "remotetestiw:\t_\n\r"
748 // Namespace and interwiki
749 'Talk:localtestiw:Test' => [ null, NS_MAIN, 'Talk:localtestiw:Test' ],
750 'Talk:remotetestiw:Test' => [ null, NS_MAIN, 'Talk:remotetestiw:Test' ],
751 'User:localtestiw:Test' => [
752 new TitleValue( NS_USER, 'Localtestiw:Test' ),
753 NS_MAIN, 'User:localtestiw:Test'
755 'User:remotetestiw:Test' => [
756 new TitleValue( NS_USER, 'Remotetestiw:Test' ),
757 NS_MAIN, 'User:remotetestiw:Test'
759 'localtestiw:Test in user namespace' => [
760 new TitleValue( NS_USER, 'Localtestiw:Test' ),
761 NS_USER, 'localtestiw:Test'
763 'remotetestiw:Test in user namespace' => [
764 new TitleValue( NS_USER, 'Remotetestiw:Test' ),
765 NS_USER, 'remotetestiw:Test'
767 'localtestiw:talk:test' => [
768 new TitleValue( NS_TALK, 'Test' ),
769 NS_MAIN, 'localtestiw:talk:test'
771 'remotetestiw:talk:test' => [
772 new TitleValue( NS_MAIN, 'talk:test', '', 'remotetestiw' ),
773 NS_MAIN, 'remotetestiw:talk:test'
776 // Invalid chars
777 'Test[test' => [ null, NS_MAIN, 'Test[test' ],
779 // Long titles
780 '255 chars long' => [
781 new TitleValue( NS_MAIN, str_repeat( 'A', 255 ) ),
782 NS_MAIN, str_repeat( 'A', 255 )
784 '255 chars long in user NS' => [
785 new TitleValue( NS_USER, str_repeat( 'A', 255 ) ),
786 NS_USER, str_repeat( 'A', 255 )
788 'User:255 chars long' => [
789 new TitleValue( NS_USER, str_repeat( 'A', 255 ) ),
790 NS_MAIN, 'User:' . str_repeat( 'A', 255 )
792 '256 chars long' => [ null, NS_MAIN, str_repeat( 'A', 256 ) ],
793 '256 chars long in user NS' => [ null, NS_USER, str_repeat( 'A', 256 ) ],
794 'User:256 chars long' => [ null, NS_MAIN, 'User:' . str_repeat( 'A', 256 ) ],
796 '512 chars long in special NS' => [
797 new TitleValue( NS_SPECIAL, str_repeat( 'A', 512 ) ),
798 NS_SPECIAL, str_repeat( 'A', 512 )
800 'Special:512 chars long' => [
801 new TitleValue( NS_SPECIAL, str_repeat( 'A', 512 ) ),
802 NS_MAIN, 'Special:' . str_repeat( 'A', 512 )
804 '513 chars long in special NS' => [ null, NS_SPECIAL, str_repeat( 'A', 513 ) ],
805 'Special:513 chars long' => [ null, NS_MAIN, 'Special:' . str_repeat( 'A', 513 ) ],
807 // IP addresses
808 'User:000.000.000' => [
809 new TitleValue( NS_USER, '000.000.000' ),
810 NS_MAIN, 'User:000.000.000'
812 'User:000.000.000.000' => [
813 new TitleValue( NS_USER, '0.0.0.0' ),
814 NS_MAIN, 'User:000.000.000.000'
816 '000.000.000.000' => [
817 new TitleValue( NS_MAIN, '000.000.000.000' ),
818 NS_MAIN, '000.000.000.000'
820 'User:1.1.256.000' => [
821 new TitleValue( NS_USER, '1.1.256.000' ),
822 NS_MAIN, 'User:1.1.256.000'
824 'User:1.1.255.000' => [
825 new TitleValue( NS_USER, '1.1.255.0' ),
826 NS_MAIN, 'User:1.1.255.000'
828 // TODO More IP address sanitization tests
831 // Invalid and valid dots
832 foreach ( [ '.', '..', '...' ] as $dots ) {
833 foreach ( [ '?', '?/', '?/Test', 'Test/?/Test', '/?', 'Test/?', '?Test', 'Test?Test',
834 'Test?' ] as $pattern ) {
835 $test = str_replace( '?', $dots, $pattern );
836 if ( $dots === '...' || in_array( $pattern, [ '?Test', 'Test?Test', 'Test?' ] ) ) {
837 $expectedMain = new TitleValue( NS_MAIN, $test );
838 $expectedUser = new TitleValue( NS_USER, $test );
839 } else {
840 $expectedMain = $expectedUser = null;
842 $ret[$test] = [ $expectedMain, NS_MAIN, $test ];
843 $ret["$test in user NS"] = [ $expectedUser, NS_USER, $test ];
844 $ret["User:$test"] = [ $expectedUser, NS_MAIN, "User:$test" ];
848 // Invalid and valid tildes
849 foreach ( [ '~~', '~~~' ] as $tildes ) {
850 foreach ( [ '?', 'Test?', '?Test', 'Test?Test' ] as $pattern ) {
851 $test = str_replace( '?', $tildes, $pattern );
852 if ( $tildes === '~~' ) {
853 $expectedMain = new TitleValue( NS_MAIN, $test );
854 $expectedUser = new TitleValue( NS_USER, $test );
855 } else {
856 $expectedMain = $expectedUser = null;
858 $ret[$test] = [ $expectedMain, NS_MAIN, $test ];
859 $ret["$test in user NS"] = [ $expectedUser, NS_USER, $test ];
860 $ret["User:$test"] = [ $expectedUser, NS_MAIN, "User:$test" ];
864 return $ret;
867 public static function provideGetNamespaceName() {
868 return [
869 [ NS_MAIN, 'Foo', 'en', '' ],
870 [ NS_USER, 'Foo', 'en', 'User' ],
871 [ NS_USER, 'Hansi Maier', 'de', 'Benutzer' ],
873 // getGenderCache() provides a mock that considers first
874 // names ending in "a" to be female.
875 [ NS_USER, 'Lisa Müller', 'de', 'Benutzerin' ],
880 * @dataProvider provideGetNamespaceName
882 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
883 $codec = $this->makeCodec( $lang );
884 $name = $codec->getNamespaceName( $namespace, $text );
886 $this->assertEquals( $expected, $name );