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
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
;
35 * @covers \MediaWiki\Title\MediaWikiTitleCodec
39 * ^--- needed because of global state in
41 class MediaWikiTitleCodecTest
extends MediaWikiIntegrationTestCase
{
42 use DummyServicesTrait
;
44 protected function setUp(): void
{
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',
55 $this->setUserLang( 'en' );
59 * Returns a mock GenderCache that will consider a user "female" if the
60 * first part of the user name ends with "a".
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';
76 * Returns a InterwikiLookup where the only valid interwikis are 'localtestiw' and 'remotetestiw'.
77 * Only `isValidInterwiki` should actually be needed.
79 * @return InterwikiLookup
81 private function getInterwikiLookup(): InterwikiLookup
{
82 return $this->getDummyInterwikiLookup( [ 'localtestiw', 'remotetestiw' ] );
86 * Returns a NamespaceInfo where the only namespaces that exist are NS_SPECIAL, NS_MAIN, NS_TALK,
87 * NS_USER, and NS_USER_TALK. As per the real NamespaceInfo, NS_USER and NS_USER_TALK have
88 * gender distinctions. All namespaces are capitalized.
90 * @return NamespaceInfo
92 private function getNamespaceInfo(): NamespaceInfo
{
93 return $this->getDummyNamespaceInfo( [
94 MainConfigNames
::CanonicalNamespaceNames
=> [
95 NS_SPECIAL
=> 'Special',
99 NS_USER_TALK
=> 'User_talk',
101 MainConfigNames
::CapitalLinks
=> true,
105 protected function makeCodec( $lang ) {
106 return new MediaWikiTitleCodec(
107 $this->getServiceContainer()->getLanguageFactory()->getLanguage( $lang ),
108 $this->getGenderCache(),
110 $this->getInterwikiLookup(),
111 $this->getNamespaceInfo()
115 public static function provideFormat() {
117 [ NS_MAIN
, 'Foo_Bar', '', '', 'en', 'Foo Bar' ],
118 [ NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi Maier#stuff and so on' ],
119 [ false, 'Hansi_Maier', '', '', 'en', 'Hansi Maier' ],
126 'User talk:hansi maier',
127 'User talk:Hansi maier'
130 // getGenderCache() provides a mock that considers first
131 // names ending in "a" to be female.
132 [ NS_USER
, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa Müller' ],
133 [ NS_MAIN
, 'FooBar', '', 'remotetestiw', 'en', 'remotetestiw:FooBar' ],
138 * @dataProvider provideFormat
140 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
143 $normalized ??
= $expected;
145 $codec = $this->makeCodec( $lang );
146 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
148 $this->assertEquals( $expected, $actual, 'formatted' );
151 $parsed = $codec->parseTitle( $actual, NS_MAIN
);
152 $actual2 = $codec->formatTitle(
153 $parsed->getNamespace(),
155 $parsed->getFragment(),
156 $parsed->getInterwiki()
159 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
162 public static function provideGetText() {
163 // $title = new TitleValue( $namespace, $dbkey, $fragment );
165 [ new TitleValue( NS_MAIN
, 'Foo_Bar', '' ), 'en', 'Foo Bar' ],
166 [ new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on' ), 'en', 'Hansi Maier' ],
167 [ new PageIdentityValue( 37, NS_MAIN
, 'Foo_Bar', PageIdentity
::LOCAL
), 'en', 'Foo Bar' ],
168 [ new PageIdentityValue( 37, NS_USER
, 'Hansi_Maier', PageIdentity
::LOCAL
), 'en', 'Hansi Maier' ],
173 * @dataProvider provideGetText
175 public function testGetText( $title, $lang, $expected ) {
176 $codec = $this->makeCodec( $lang );
177 $actual = $codec->getText( $title );
179 $this->assertEquals( $expected, $actual );
182 public static function provideGetPrefixedText() {
184 [ new TitleValue( NS_MAIN
, 'Foo_Bar', '' ), 'en', 'Foo Bar' ],
185 [ new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on' ), 'en', 'User:Hansi Maier' ],
187 // No capitalization or normalization is applied while formatting!
188 [ new TitleValue( NS_USER_TALK
, 'hansi__maier', '' ), 'en', 'User talk:hansi maier' ],
190 // getGenderCache() provides a mock that considers first
191 // names ending in "a" to be female.
193 new TitleValue( NS_USER
, 'Lisa_Müller', '' ),
194 'de', 'Benutzerin:Lisa Müller'
197 new TitleValue( 1000000, 'Invalid_namespace', '' ),
199 'Special:Badtitle/NS1000000:Invalid namespace'
202 new PageIdentityValue( 37, NS_MAIN
, 'Foo_Bar', PageIdentity
::LOCAL
),
207 new PageIdentityValue( 37, NS_USER
, 'Hansi_Maier', PageIdentity
::LOCAL
),
212 new PageIdentityValue( 37, NS_USER_TALK
, 'hansi__maier', PageIdentity
::LOCAL
),
214 'User talk:hansi maier'
217 new PageIdentityValue( 37, NS_USER
, 'Lisa_Müller', PageIdentity
::LOCAL
),
219 'Benutzerin:Lisa Müller'
222 new PageIdentityValue( 37, 1000000, 'Invalid_namespace', PageIdentity
::LOCAL
),
224 'Special:Badtitle/NS1000000:Invalid namespace'
230 * @dataProvider provideGetPrefixedText
232 public function testGetPrefixedText( $title, $lang, $expected ) {
233 $codec = $this->makeCodec( $lang );
234 $actual = $codec->getPrefixedText( $title );
236 $this->assertEquals( $expected, $actual );
239 public static function provideGetPrefixedDBkey() {
241 [ new TitleValue( NS_MAIN
, 'Foo_Bar', '', '' ), 'en', 'Foo_Bar' ],
242 [ new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on', '' ), 'en', 'User:Hansi_Maier' ],
244 // No capitalization or normalization is applied while formatting!
245 [ new TitleValue( NS_USER_TALK
, 'hansi__maier', '', '' ), 'en', 'User_talk:hansi__maier' ],
247 // getGenderCache() provides a mock that considers first
248 // names ending in "a" to be female.
249 [ new TitleValue( NS_USER
, 'Lisa_Müller', '', '' ), 'de', 'Benutzerin:Lisa_Müller' ],
251 [ new TitleValue( NS_MAIN
, 'Remote_page', '', 'remotetestiw' ), 'en', 'remotetestiw:Remote_page' ],
253 // non-existent namespace
254 [ new TitleValue( 10000000, 'Foobar', '', '' ), 'en', 'Special:Badtitle/NS10000000:Foobar' ],
257 new PageIdentityValue( 37, NS_MAIN
, 'Foo_Bar', PageIdentity
::LOCAL
),
262 new PageIdentityValue( 37, NS_USER
, 'Hansi_Maier', PageIdentity
::LOCAL
),
267 new PageIdentityValue( 37, NS_USER_TALK
, 'hansi__maier', PageIdentity
::LOCAL
),
269 'User_talk:hansi__maier'
272 new PageIdentityValue( 37, NS_USER
, 'Lisa_Müller', PageIdentity
::LOCAL
),
274 'Benutzerin:Lisa_Müller'
277 new PageIdentityValue( 37, NS_MAIN
, 'Remote_page', PageIdentity
::LOCAL
),
282 new PageIdentityValue( 37, 10000000, 'Foobar', PageIdentity
::LOCAL
),
284 'Special:Badtitle/NS10000000:Foobar'
290 * @dataProvider provideGetPrefixedDBkey
292 public function testGetPrefixedDBkey( $title, $lang, $expected
294 $codec = $this->makeCodec( $lang );
295 $actual = $codec->getPrefixedDBkey( $title );
297 $this->assertEquals( $expected, $actual );
300 public static function provideGetFullText() {
302 [ new TitleValue( NS_MAIN
, 'Foo_Bar', '' ), 'en', 'Foo Bar' ],
303 [ new TitleValue( NS_USER
, 'Hansi_Maier', 'stuff_and_so_on' ), 'en', 'User:Hansi Maier#stuff and so on' ],
305 // No capitalization or normalization is applied while formatting!
306 [ new TitleValue( NS_USER_TALK
, 'hansi__maier', '' ), 'en', 'User talk:hansi maier' ],
308 [ new TitleValue( NS_MAIN
, 'Foo_Bar' ), 'en', 'Foo Bar' ],
309 [ new TitleValue( NS_USER
, 'Hansi_Maier' ), 'en', 'User:Hansi Maier' ],
312 new PageIdentityValue( 37, NS_MAIN
, 'Foo_Bar', PageIdentity
::LOCAL
),
317 new PageIdentityValue( 37, NS_USER
, 'Hansi_Maier', PageIdentity
::LOCAL
),
322 new PageIdentityValue( 37, NS_USER_TALK
, 'hansi__maier', PageIdentity
::LOCAL
),
324 'User talk:hansi maier'
330 * @dataProvider provideGetFullText
332 public function testGetFullText( $title, $lang, $expected ) {
333 $codec = $this->makeCodec( $lang );
334 $actual = $codec->getFullText( $title );
336 $this->assertEquals( $expected, $actual );
339 public static function provideParseTitle() {
340 // TODO: test capitalization and trimming
341 // TODO: test unicode normalization
344 [ ' : Hansi_Maier _ ', NS_MAIN
, 'en',
345 new TitleValue( NS_MAIN
, 'Hansi_Maier', '' ) ],
346 [ 'User:::1', NS_MAIN
, 'de',
347 new TitleValue( NS_USER
, '0:0:0:0:0:0:0:1', '' ) ],
348 [ ' lisa Müller', NS_USER
, 'de',
349 new TitleValue( NS_USER
, 'Lisa_Müller', '' ) ],
350 [ 'benutzerin:lisa Müller#stuff', NS_MAIN
, 'de',
351 new TitleValue( NS_USER
, 'Lisa_Müller', 'stuff' ) ],
353 [ ':Category:Quux', NS_MAIN
, 'en',
354 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
355 [ 'Category:Quux', NS_MAIN
, 'en',
356 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
357 [ 'Category:Quux', NS_CATEGORY
, 'en',
358 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
359 [ 'Quux', NS_CATEGORY
, 'en',
360 new TitleValue( NS_CATEGORY
, 'Quux', '' ) ],
361 [ ':Quux', NS_CATEGORY
, 'en',
362 new TitleValue( NS_MAIN
, 'Quux', '' ) ],
364 // getGenderCache() provides a mock that considers first
365 // names ending in "a" to be female.
367 [ 'a b c', NS_MAIN
, 'en',
368 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
369 [ ' a b c ', NS_MAIN
, 'en',
370 new TitleValue( NS_MAIN
, 'A_b_c' ) ],
371 [ ' _ Foo __ Bar_ _', NS_MAIN
, 'en',
372 new TitleValue( NS_MAIN
, 'Foo_Bar' ) ],
374 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
375 [ 'Sandbox', NS_MAIN
, 'en', ],
376 [ 'A "B"', NS_MAIN
, 'en', ],
377 [ 'A \'B\'', NS_MAIN
, 'en', ],
378 [ '.com', NS_MAIN
, 'en', ],
379 [ '~', NS_MAIN
, 'en', ],
380 [ '"', NS_MAIN
, 'en', ],
381 [ '\'', NS_MAIN
, 'en', ],
383 [ 'Talk:Sandbox', NS_MAIN
, 'en',
384 new TitleValue( NS_TALK
, 'Sandbox' ) ],
385 [ 'Talk:Foo:Sandbox', NS_MAIN
, 'en',
386 new TitleValue( NS_TALK
, 'Foo:Sandbox' ) ],
387 [ 'File:Example.svg', NS_MAIN
, 'en',
388 new TitleValue( NS_FILE
, 'Example.svg' ) ],
389 [ 'File_talk:Example.svg', NS_MAIN
, 'en',
390 new TitleValue( NS_FILE_TALK
, 'Example.svg' ) ],
391 [ 'Foo/.../Sandbox', NS_MAIN
, 'en',
393 [ 'Sandbox/...', NS_MAIN
, 'en',
395 [ 'A~~', NS_MAIN
, 'en',
397 // Length is 256 total, but only title part matters
398 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN
, 'en',
399 new TitleValue( NS_CATEGORY
,
400 'X' . str_repeat( 'x', 247 ) ) ],
401 [ str_repeat( 'x', 252 ), NS_MAIN
, 'en',
402 'X' . str_repeat( 'x', 251 ) ],
403 // Test decoding and normalization
404 [ '"ñ"', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, '"ñ"' ) ],
405 [ 'X#ñ', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'ñ' ) ],
406 // target section parsing
407 'empty fragment' => [ 'X#', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X' ) ],
408 'only fragment' => [ '#', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, '' ) ],
409 'double hash' => [ 'X##', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', '#' ) ],
410 'fragment with hash' => [ 'X#z#z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z#z' ) ],
411 'fragment with space' => [ 'X#z z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z z' ) ],
412 'fragment with percent' => [ 'X#z%z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z%z' ) ],
413 'fragment with amp' => [ 'X#z&z', NS_MAIN
, 'en', new TitleValue( NS_MAIN
, 'X', 'z&z' ) ],
414 'remotetestiw in user' => [ 'User:remotetestiw:', NS_MAIN
, 'en', new TitleValue( NS_USER
, 'Remotetestiw:' ) ],
419 * @dataProvider provideParseTitle
421 public function testParseTitle( $text, $ns, $lang, $title = null ) {
422 if ( !( $title instanceof TitleValue
) ) {
423 $title ??
= str_replace( ' ', '_', trim( $text ) );
424 $title = new TitleValue( NS_MAIN
, $title, '' );
427 $codec = $this->makeCodec( $lang );
428 $actual = $codec->parseTitle( $text, $ns );
430 $this->assertEquals( $title, $actual );
433 public static function provideParseTitle_invalid() {
441 [ 'Talk:File:Foo.jpg' ],
442 [ 'Talk:localtestiw:Foo' ],
443 [ '::1' ], // only valid in user namespace
444 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
445 [ 'remotetestiw:', NS_USER
],
447 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
452 // Bad characters forbidden regardless of wgLegalTitleChars
464 // XML/HTML character entity references
465 // Note: Commented out because they are not marked invalid by the PHP test as
466 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
467 // [ 'A é B' ],
469 // [ 'A é B' ],
470 // Subject of NS_TALK does not roundtrip to NS_MAIN
471 [ 'Talk:File:Example.svg' ],
472 // Directory navigation
478 [ 'Foo/../Sandbox' ],
483 [ 'A ~~~~ Signature' ],
484 [ 'A ~~~~~ Timestamp' ],
485 [ str_repeat( 'x', 256 ) ],
486 // Namespace prefix without actual title
489 [ 'Category: #bar' ],
491 [ "Apollo\x96Soyuz" ],
492 // Input resulting from invalid Unicode being sanitized somewhere else
493 [ "Apollo\u{FFFD}Soyuz" ],
498 * @dataProvider provideParseTitle_invalid
500 public function testParseTitle_invalid( $text, $ns = NS_MAIN
) {
501 $this->expectException( MalformedTitleException
::class );
503 $codec = $this->makeCodec( 'en' );
504 $codec->parseTitle( $text, $ns );
508 * @dataProvider provideMakeTitleValueSafe
510 public function testMakeTitleValueSafe(
511 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
513 $codec = $this->makeCodec( $lang );
514 $this->assertEquals( $expected,
515 $codec->makeTitleValueSafe( $ns, $text, $fragment, $interwiki ) );
519 * @dataProvider provideMakeTitleValueSafe
520 * @covers \MediaWiki\Title\Title::makeTitleSafe
521 * @covers \MediaWiki\Title\Title::makeName
522 * @covers \MediaWiki\Title\Title::secureAndSplit
524 public function testMakeTitleSafe(
525 $expected, $ns, $text, $fragment = '', $interwiki = '', $lang = 'en'
527 $codec = $this->makeCodec( $lang );
528 $this->setService( 'TitleParser', $codec );
529 $this->setService( 'TitleFormatter', $codec );
531 $actual = Title
::makeTitleSafe( $ns, $text, $fragment, $interwiki );
534 $this->assertNotNull( $actual );
535 $expectedTitle = Title
::newFromLinkTarget( $expected );
536 $this->assertSame( $expectedTitle->getPrefixedDBkey(), $actual->getPrefixedDBkey() );
538 $this->assertNull( $actual );
542 public static function provideMakeTitleValueSafe() {
544 'Nonexistent NS' => [ null, 942929, 'Test' ],
545 'Linebreak in title' => [ null, NS_MAIN
, "Test\nthis" ],
546 'Pipe in title' => [ null, NS_MAIN
, "Test|this" ],
547 'Simple page' => [ new TitleValue( NS_MAIN
, 'Test' ), NS_MAIN
, 'Test' ],
550 'Passed fragment' => [
551 new TitleValue( NS_MAIN
, 'Test', 'Fragment' ),
552 NS_MAIN
, 'Test', 'Fragment'
554 'Embedded fragment' => [
555 new TitleValue( NS_MAIN
, 'Test', 'Fragment' ),
556 NS_MAIN
, 'Test#Fragment'
558 'Passed fragment with spaces' => [
559 // XXX Leading space is okay in fragment?
560 new TitleValue( NS_MAIN
, 'Test', ' Frag ment' ),
561 NS_MAIN
, ' Test ', " Frag_ment "
563 'Embedded fragment with spaces' => [
564 // XXX Leading space is okay in fragment?
565 new TitleValue( NS_MAIN
, 'Test', ' Frag ment' ),
566 NS_MAIN
, " Test # Frag_ment "
568 // XXX Is it correct that these aren't normalized to spaces?
569 'Passed fragment with leading tab' => [ null, NS_MAIN
, "\tTest\t", "\tFragment" ],
570 'Embedded fragment with leading tab' => [ null, NS_MAIN
, "\tTest\t#\tFragment" ],
571 'Passed fragment with trailing tab' => [ null, NS_MAIN
, "\tTest\t", "Fragment\t" ],
572 'Embedded fragment with trailing tab' => [ null, NS_MAIN
, "\tTest\t#Fragment\t" ],
573 'Passed fragment with interior tab' => [ null, NS_MAIN
, "\tTest\t", "Frag\tment" ],
574 'Embedded fragment with interior tab' => [ null, NS_MAIN
, "\tTest\t#\tFrag\tment" ],
577 'Passed local interwiki' => [
578 new TitleValue( NS_MAIN
, 'Test' ),
579 NS_MAIN
, 'Test', '', 'localtestiw'
581 'Embedded local interwiki' => [
582 new TitleValue( NS_MAIN
, 'Test' ),
583 NS_MAIN
, 'localtestiw:Test'
585 'Passed remote interwiki' => [
586 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
587 NS_MAIN
, 'Test', '', 'remotetestiw'
589 'Embedded remote interwiki' => [
590 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
591 NS_MAIN
, 'remotetestiw:Test'
593 // Interwiki prefixes are not case sensitive
594 'Passed local interwiki with different case' => [
595 new TitleValue( NS_MAIN
, 'Test' ),
596 NS_MAIN
, 'Test', '', 'LocalTestIW'
598 'Embedded local interwiki with different case' => [
599 new TitleValue( NS_MAIN
, 'Test' ),
600 NS_MAIN
, 'LocalTestIW:Test'
602 'Passed remote interwiki with different case' => [
603 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
604 NS_MAIN
, 'Test', '', 'RemoteTestIW'
606 'Embedded remote interwiki with different case' => [
607 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
608 NS_MAIN
, 'RemoteTestIW:Test'
610 'Passed local interwiki with lowercase page name' => [
611 new TitleValue( NS_MAIN
, 'Test' ),
612 NS_MAIN
, 'test', '', 'localtestiw'
614 'Embedded local interwiki with lowercase page name' => [
615 new TitleValue( NS_MAIN
, 'Test' ),
616 NS_MAIN
, 'localtestiw:test'
618 // For remote we don't auto-capitalize
619 'Passed remote interwiki with lowercase page name' => [
620 new TitleValue( NS_MAIN
, 'test', '', 'remotetestiw' ),
621 NS_MAIN
, 'test', '', 'remotetestiw'
623 'Embedded remote interwiki with lowercase page name' => [
624 new TitleValue( NS_MAIN
, 'test', '', 'remotetestiw' ),
625 NS_MAIN
, 'remotetestiw:test'
628 // Fragment and interwiki
629 'Fragment and local interwiki' => [
630 new TitleValue( NS_MAIN
, 'Test', 'Fragment' ),
631 NS_MAIN
, 'Test', 'Fragment', 'localtestiw'
633 'Fragment and remote interwiki' => [
634 new TitleValue( NS_MAIN
, 'Test', 'Fragment', 'remotetestiw' ),
635 NS_MAIN
, 'Test', 'Fragment', 'remotetestiw'
637 'Fragment and local interwiki and non-main namespace' => [
638 new TitleValue( NS_TALK
, 'Test', 'Fragment' ),
639 NS_TALK
, 'Test', 'Fragment', 'localtestiw'
641 // We don't know the foreign wiki's namespaces, so it will always be NS_MAIN
642 'Fragment and remote interwiki and non-main namespace' => [
643 new TitleValue( NS_MAIN
, 'Talk:Test', 'Fragment', 'remotetestiw' ),
644 NS_TALK
, 'Test', 'Fragment', 'remotetestiw'
647 // Whitespace normalization and Unicode stripping
648 'Name with space' => [
649 new TitleValue( NS_MAIN
, 'Test_test' ),
652 'Unicode bidi override characters' => [
653 new TitleValue( NS_MAIN
, 'Test' ),
654 NS_MAIN
, "\u{200E}T\u{200F}e\u{202A}s\u{202B}t\u{202C}\u{202D}\u{202E}"
656 'Invalid UTF-8 sequence' => [ null, NS_MAIN
, "Te\x80\xf0st" ],
657 'Whitespace collapsing' => [
658 new TitleValue( NS_MAIN
, 'Test_test' ),
659 NS_MAIN
, "Test _\u{00A0}\u{1680}\u{180E}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}" .
660 "\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}" .
661 "\u{205F}\u{3000}test"
663 'UTF8_REPLACEMENT' => [ null, NS_MAIN
, UtfNormal\Constants
::UTF8_REPLACEMENT
],
665 // Namespace prefixes
667 new TitleValue( NS_TALK
, 'Test' ),
670 'Test in talk NS' => [
671 new TitleValue( NS_TALK
, 'Test' ),
675 new TitleValue( NS_MAIN
, 'Talkk:Test' ),
676 NS_MAIN
, 'Talkk:Test'
678 'Talk:Talk:Test' => [ null, NS_MAIN
, 'Talk:Talk:Test' ],
679 'Talk:User:Test' => [ null, NS_MAIN
, 'Talk:User:Test' ],
680 'User:Talk:Test' => [
681 new TitleValue( NS_USER
, 'Talk:Test' ),
682 NS_MAIN
, 'User:Talk:Test'
684 'User:Test in talk NS' => [ null, NS_TALK
, 'User:Test' ],
685 'Talk:Test in talk NS' => [ null, NS_TALK
, 'Talk:Test' ],
686 'User:Test in user NS' => [
687 new TitleValue( NS_USER
, 'User:Test' ),
690 'Talk:Test in user NS' => [
691 new TitleValue( NS_USER
, 'Talk:Test' ),
697 new TitleValue( NS_MAIN
, 'Test' ),
701 new TitleValue( NS_TALK
, 'Test' ),
702 NS_MAIN
, ':Talk:Test'
704 ':localtestiw:Test' => [
705 new TitleValue( NS_MAIN
, 'Test' ),
706 NS_MAIN
, ':localtestiw:Test'
708 ':remotetestiw:Test' => [
709 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
710 NS_MAIN
, ':remotetestiw:Test'
712 // XXX Is this correct? Why is it different from remote?
713 'localtestiw::Test' => [ null, NS_MAIN
, 'localtestiw::Test' ],
714 'remotetestiw::Test' => [
715 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
716 NS_MAIN
, 'remotetestiw::Test'
718 // XXX Is this correct? Why is it different from remote?
719 'localtestiw:: Test' => [ null, NS_MAIN
, 'localtestiw:: Test' ],
720 'remotetestiw:: Test' => [
721 new TitleValue( NS_MAIN
, 'Test', '', 'remotetestiw' ),
722 NS_MAIN
, 'remotetestiw:: Test'
726 'Empty title' => [ null, NS_MAIN
, '' ],
727 'Empty title with namespace' => [ null, NS_USER
, '' ],
728 'Local interwiki with empty page name' => [
729 new TitleValue( NS_MAIN
, 'Main_Page' ),
730 NS_MAIN
, 'localtestiw:'
732 'Remote interwiki with empty page name' => [
733 // XXX Is this correct? This is supposed to redirect to the main page remotely?
734 new TitleValue( NS_MAIN
, '', '', 'remotetestiw' ),
735 NS_MAIN
, 'remotetestiw:'
738 // Whitespace-only titles
739 'Whitespace-only title' => [ null, NS_MAIN
, "\t\n" ],
740 'Whitespace-only title with namespace' => [ null, NS_USER
, " _ " ],
741 'Local interwiki with whitespace-only page name' => [
742 // XXX Is whitespace-only really supposed to be different from empty?
744 NS_MAIN
, "localtestiw:_\t"
746 'Remote interwiki with whitespace-only page name' => [
747 // XXX Is whitespace-only really supposed to be different from empty?
749 NS_MAIN
, "remotetestiw:\t_\n\r"
752 // Namespace and interwiki
753 'Talk:localtestiw:Test' => [ null, NS_MAIN
, 'Talk:localtestiw:Test' ],
754 'Talk:remotetestiw:Test' => [ null, NS_MAIN
, 'Talk:remotetestiw:Test' ],
755 'User:localtestiw:Test' => [
756 new TitleValue( NS_USER
, 'Localtestiw:Test' ),
757 NS_MAIN
, 'User:localtestiw:Test'
759 'User:remotetestiw:Test' => [
760 new TitleValue( NS_USER
, 'Remotetestiw:Test' ),
761 NS_MAIN
, 'User:remotetestiw:Test'
763 'localtestiw:Test in user namespace' => [
764 new TitleValue( NS_USER
, 'Localtestiw:Test' ),
765 NS_USER
, 'localtestiw:Test'
767 'remotetestiw:Test in user namespace' => [
768 new TitleValue( NS_USER
, 'Remotetestiw:Test' ),
769 NS_USER
, 'remotetestiw:Test'
771 'localtestiw:talk:test' => [
772 new TitleValue( NS_TALK
, 'Test' ),
773 NS_MAIN
, 'localtestiw:talk:test'
775 'remotetestiw:talk:test' => [
776 new TitleValue( NS_MAIN
, 'talk:test', '', 'remotetestiw' ),
777 NS_MAIN
, 'remotetestiw:talk:test'
781 'Test[test' => [ null, NS_MAIN
, 'Test[test' ],
784 '255 chars long' => [
785 new TitleValue( NS_MAIN
, str_repeat( 'A', 255 ) ),
786 NS_MAIN
, str_repeat( 'A', 255 )
788 '255 chars long in user NS' => [
789 new TitleValue( NS_USER
, str_repeat( 'A', 255 ) ),
790 NS_USER
, str_repeat( 'A', 255 )
792 'User:255 chars long' => [
793 new TitleValue( NS_USER
, str_repeat( 'A', 255 ) ),
794 NS_MAIN
, 'User:' . str_repeat( 'A', 255 )
796 '256 chars long' => [ null, NS_MAIN
, str_repeat( 'A', 256 ) ],
797 '256 chars long in user NS' => [ null, NS_USER
, str_repeat( 'A', 256 ) ],
798 'User:256 chars long' => [ null, NS_MAIN
, 'User:' . str_repeat( 'A', 256 ) ],
800 '512 chars long in special NS' => [
801 new TitleValue( NS_SPECIAL
, str_repeat( 'A', 512 ) ),
802 NS_SPECIAL
, str_repeat( 'A', 512 )
804 'Special:512 chars long' => [
805 new TitleValue( NS_SPECIAL
, str_repeat( 'A', 512 ) ),
806 NS_MAIN
, 'Special:' . str_repeat( 'A', 512 )
808 '513 chars long in special NS' => [ null, NS_SPECIAL
, str_repeat( 'A', 513 ) ],
809 'Special:513 chars long' => [ null, NS_MAIN
, 'Special:' . str_repeat( 'A', 513 ) ],
812 'User:000.000.000' => [
813 new TitleValue( NS_USER
, '000.000.000' ),
814 NS_MAIN
, 'User:000.000.000'
816 'User:000.000.000.000' => [
817 new TitleValue( NS_USER
, '0.0.0.0' ),
818 NS_MAIN
, 'User:000.000.000.000'
820 '000.000.000.000' => [
821 new TitleValue( NS_MAIN
, '000.000.000.000' ),
822 NS_MAIN
, '000.000.000.000'
824 'User:1.1.256.000' => [
825 new TitleValue( NS_USER
, '1.1.256.000' ),
826 NS_MAIN
, 'User:1.1.256.000'
828 'User:1.1.255.000' => [
829 new TitleValue( NS_USER
, '1.1.255.0' ),
830 NS_MAIN
, 'User:1.1.255.000'
832 // TODO More IP address sanitization tests
835 // Invalid and valid dots
836 foreach ( [ '.', '..', '...' ] as $dots ) {
837 foreach ( [ '?', '?/', '?/Test', 'Test/?/Test', '/?', 'Test/?', '?Test', 'Test?Test',
838 'Test?' ] as $pattern ) {
839 $test = str_replace( '?', $dots, $pattern );
840 if ( $dots === '...' ||
in_array( $pattern, [ '?Test', 'Test?Test', 'Test?' ] ) ) {
841 $expectedMain = new TitleValue( NS_MAIN
, $test );
842 $expectedUser = new TitleValue( NS_USER
, $test );
844 $expectedMain = $expectedUser = null;
846 $ret[$test] = [ $expectedMain, NS_MAIN
, $test ];
847 $ret["$test in user NS"] = [ $expectedUser, NS_USER
, $test ];
848 $ret["User:$test"] = [ $expectedUser, NS_MAIN
, "User:$test" ];
852 // Invalid and valid tildes
853 foreach ( [ '~~', '~~~' ] as $tildes ) {
854 foreach ( [ '?', 'Test?', '?Test', 'Test?Test' ] as $pattern ) {
855 $test = str_replace( '?', $tildes, $pattern );
856 if ( $tildes === '~~' ) {
857 $expectedMain = new TitleValue( NS_MAIN
, $test );
858 $expectedUser = new TitleValue( NS_USER
, $test );
860 $expectedMain = $expectedUser = null;
862 $ret[$test] = [ $expectedMain, NS_MAIN
, $test ];
863 $ret["$test in user NS"] = [ $expectedUser, NS_USER
, $test ];
864 $ret["User:$test"] = [ $expectedUser, NS_MAIN
, "User:$test" ];
871 public static function provideGetNamespaceName() {
873 [ NS_MAIN
, 'Foo', 'en', '' ],
874 [ NS_USER
, 'Foo', 'en', 'User' ],
875 [ NS_USER
, 'Hansi Maier', 'de', 'Benutzer' ],
877 // getGenderCache() provides a mock that considers first
878 // names ending in "a" to be female.
879 [ NS_USER
, 'Lisa Müller', 'de', 'Benutzerin' ],
884 * @dataProvider provideGetNamespaceName
886 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
887 $codec = $this->makeCodec( $lang );
888 $name = $codec->getNamespaceName( $namespace, $text );
890 $this->assertEquals( $expected, $name );