rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / TitleTest.php
blob7770cbc2c3a18f58c2a0fd02c77455e5575fe6cc
1 <?php
3 /**
4 * @group Database
5 * @group Title
6 */
7 class TitleTest extends MediaWikiTestCase {
8 protected function setUp() {
9 parent::setUp();
11 $this->setMwGlobals( [
12 'wgAllowUserJs' => false,
13 'wgDefaultLanguageVariant' => false,
14 'wgMetaNamespace' => 'Project',
15 ] );
16 $this->setUserLang( 'en' );
17 $this->setContentLang( 'en' );
20 /**
21 * @covers Title::legalChars
23 public function testLegalChars() {
24 $titlechars = Title::legalChars();
26 foreach ( range( 1, 255 ) as $num ) {
27 $chr = chr( $num );
28 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
29 $this->assertFalse(
30 (bool)preg_match( "/[$titlechars]/", $chr ),
31 "chr($num) = $chr is not a valid titlechar"
33 } else {
34 $this->assertTrue(
35 (bool)preg_match( "/[$titlechars]/", $chr ),
36 "chr($num) = $chr is a valid titlechar"
42 public static function provideValidSecureAndSplit() {
43 return [
44 [ 'Sandbox' ],
45 [ 'A "B"' ],
46 [ 'A \'B\'' ],
47 [ '.com' ],
48 [ '~' ],
49 [ '#' ],
50 [ '"' ],
51 [ '\'' ],
52 [ 'Talk:Sandbox' ],
53 [ 'Talk:Foo:Sandbox' ],
54 [ 'File:Example.svg' ],
55 [ 'File_talk:Example.svg' ],
56 [ 'Foo/.../Sandbox' ],
57 [ 'Sandbox/...' ],
58 [ 'A~~' ],
59 [ ':A' ],
60 // Length is 256 total, but only title part matters
61 [ 'Category:' . str_repeat( 'x', 248 ) ],
62 [ str_repeat( 'x', 252 ) ],
63 // interwiki prefix
64 [ 'localtestiw: #anchor' ],
65 [ 'localtestiw:' ],
66 [ 'localtestiw:foo' ],
67 [ 'localtestiw: foo # anchor' ],
68 [ 'localtestiw: Talk: Sandbox # anchor' ],
69 [ 'remotetestiw:' ],
70 [ 'remotetestiw: Talk: # anchor' ],
71 [ 'remotetestiw: #bar' ],
72 [ 'remotetestiw: Talk:' ],
73 [ 'remotetestiw: Talk: Foo' ],
74 [ 'localtestiw:remotetestiw:' ],
75 [ 'localtestiw:remotetestiw:foo' ]
79 public static function provideInvalidSecureAndSplit() {
80 return [
81 [ '', 'title-invalid-empty' ],
82 [ ':', 'title-invalid-empty' ],
83 [ '__ __', 'title-invalid-empty' ],
84 [ ' __ ', 'title-invalid-empty' ],
85 // Bad characters forbidden regardless of wgLegalTitleChars
86 [ 'A [ B', 'title-invalid-characters' ],
87 [ 'A ] B', 'title-invalid-characters' ],
88 [ 'A { B', 'title-invalid-characters' ],
89 [ 'A } B', 'title-invalid-characters' ],
90 [ 'A < B', 'title-invalid-characters' ],
91 [ 'A > B', 'title-invalid-characters' ],
92 [ 'A | B', 'title-invalid-characters' ],
93 [ "A \t B", 'title-invalid-characters' ],
94 [ "A \n B", 'title-invalid-characters' ],
95 // URL encoding
96 [ 'A%20B', 'title-invalid-characters' ],
97 [ 'A%23B', 'title-invalid-characters' ],
98 [ 'A%2523B', 'title-invalid-characters' ],
99 // XML/HTML character entity references
100 // Note: Commented out because they are not marked invalid by the PHP test as
101 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
102 // 'A &eacute; B',
103 // 'A &#233; B',
104 // 'A &#x00E9; B',
105 // Subject of NS_TALK does not roundtrip to NS_MAIN
106 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
107 // Directory navigation
108 [ '.', 'title-invalid-relative' ],
109 [ '..', 'title-invalid-relative' ],
110 [ './Sandbox', 'title-invalid-relative' ],
111 [ '../Sandbox', 'title-invalid-relative' ],
112 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
113 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
114 [ 'Sandbox/.', 'title-invalid-relative' ],
115 [ 'Sandbox/..', 'title-invalid-relative' ],
116 // Tilde
117 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
118 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
119 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
120 // Length
121 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
122 // Namespace prefix without actual title
123 [ 'Talk:', 'title-invalid-empty' ],
124 [ 'Talk:#', 'title-invalid-empty' ],
125 [ 'Category: ', 'title-invalid-empty' ],
126 [ 'Category: #bar', 'title-invalid-empty' ],
127 // interwiki prefix
128 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
129 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
133 private function secureAndSplitGlobals() {
134 $this->setMwGlobals( [
135 'wgLocalInterwikis' => [ 'localtestiw' ],
136 'wgHooks' => [
137 'InterwikiLoadPrefix' => [
138 function ( $prefix, &$data ) {
139 if ( $prefix === 'localtestiw' ) {
140 $data = [ 'iw_url' => 'localtestiw' ];
141 } elseif ( $prefix === 'remotetestiw' ) {
142 $data = [ 'iw_url' => 'remotetestiw' ];
144 return false;
148 ] );
150 // Reset TitleParser since we modified $wgLocalInterwikis
151 $this->setService( 'TitleParser', new MediaWikiTitleCodec(
152 Language::factory( 'en' ),
153 new GenderCache(),
154 [ 'localtestiw' ]
155 ) );
159 * See also mediawiki.Title.test.js
160 * @covers Title::secureAndSplit
161 * @dataProvider provideValidSecureAndSplit
162 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
164 public function testSecureAndSplitValid( $text ) {
165 $this->secureAndSplitGlobals();
166 $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" );
170 * See also mediawiki.Title.test.js
171 * @covers Title::secureAndSplit
172 * @dataProvider provideInvalidSecureAndSplit
173 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
175 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
176 $this->secureAndSplitGlobals();
177 try {
178 Title::newFromTextThrow( $text ); // should throw
179 $this->assertTrue( false, "Invalid: $text" );
180 } catch ( MalformedTitleException $ex ) {
181 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
185 public static function provideConvertByteClassToUnicodeClass() {
186 return [
188 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
189 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
192 'QWERTYf-\\xFF+',
193 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
196 'QWERTY\\x66-\\xFD+',
197 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
200 'QWERTYf-y+',
201 'QWERTYf-y+',
204 'QWERTYf-\\x80+',
205 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
208 'QWERTY\\x66-\\x80+\\x23',
209 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
212 'QWERTY\\x66-\\x80+\\xD3',
213 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
216 '\\\\\\x99',
217 '\\\\\\u0080-\\uFFFF',
220 '-\\x99',
221 '\\-\\u0080-\\uFFFF',
224 'QWERTY\\-\\x99',
225 'QWERTY\\-\\u0080-\\uFFFF',
228 '\\\\x99',
229 '\\\\x99',
232 'A-\\x9F',
233 'A-\\x7F\\u0080-\\uFFFF',
236 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
237 'f-wQWERTYFXZ\\u0080-\\uFFFF',
240 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
241 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
247 * @dataProvider provideConvertByteClassToUnicodeClass
248 * @covers Title::convertByteClassToUnicodeClass
250 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
251 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
255 * @dataProvider provideSpecialNamesWithAndWithoutParameter
256 * @covers Title::fixSpecialName
258 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
259 $title = Title::newFromText( $text );
260 $fixed = $title->fixSpecialName();
261 $stuff = explode( '/', $fixed->getDBkey(), 2 );
262 if ( count( $stuff ) == 2 ) {
263 $par = $stuff[1];
264 } else {
265 $par = null;
267 $this->assertEquals(
268 $expectedParam,
269 $par,
270 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
274 public static function provideSpecialNamesWithAndWithoutParameter() {
275 return [
276 [ 'Special:Version', null ],
277 [ 'Special:Version/', '' ],
278 [ 'Special:Version/param', 'param' ],
283 * Auth-less test of Title::isValidMoveOperation
285 * @group Database
286 * @param string $source
287 * @param string $target
288 * @param array|string|bool $expected Required error
289 * @dataProvider provideTestIsValidMoveOperation
290 * @covers Title::isValidMoveOperation
291 * @covers Title::validateFileMoveOperation
293 public function testIsValidMoveOperation( $source, $target, $expected ) {
294 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
295 $title = Title::newFromText( $source );
296 $nt = Title::newFromText( $target );
297 $errors = $title->isValidMoveOperation( $nt, false );
298 if ( $expected === true ) {
299 $this->assertTrue( $errors );
300 } else {
301 $errors = $this->flattenErrorsArray( $errors );
302 foreach ( (array)$expected as $error ) {
303 $this->assertContains( $error, $errors );
308 public static function provideTestIsValidMoveOperation() {
309 return [
310 // for Title::isValidMoveOperation
311 [ 'Some page', '', 'badtitletext' ],
312 [ 'Test', 'Test', 'selfmove' ],
313 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
314 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
315 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
316 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
317 // for Title::validateFileMoveOperation
318 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
323 * Auth-less test of Title::userCan
325 * @param array $whitelistRegexp
326 * @param string $source
327 * @param string $action
328 * @param array|string|bool $expected Required error
330 * @covers Title::checkReadPermissions
331 * @dataProvider dataWgWhitelistReadRegexp
333 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
334 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
335 // usually have only one regex, it is more concise to write the lonely regex
336 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
337 // type requisite.
338 if ( is_string( $whitelistRegexp ) ) {
339 $whitelistRegexp = [ $whitelistRegexp ];
342 $this->setMwGlobals( [
343 // So User::isEveryoneAllowed( 'read' ) === false
344 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
345 'wgWhitelistRead' => [ 'some random non sense title' ],
346 'wgWhitelistReadRegexp' => $whitelistRegexp,
347 ] );
349 $title = Title::newFromDBkey( $source );
351 // New anonymous user with no rights
352 $user = new User;
353 $user->mRights = [];
354 $errors = $title->userCan( $action, $user );
356 if ( is_bool( $expected ) ) {
357 # Forge the assertion message depending on the assertion expectation
358 $allowableness = $expected
359 ? " should be allowed"
360 : " should NOT be allowed";
361 $this->assertEquals(
362 $expected,
363 $errors,
364 "User action '$action' on [[$source]] $allowableness."
366 } else {
367 $errors = $this->flattenErrorsArray( $errors );
368 foreach ( (array)$expected as $error ) {
369 $this->assertContains( $error, $errors );
375 * Provides test parameter values for testWgWhitelistReadRegexp()
377 public function dataWgWhitelistReadRegexp() {
378 $ALLOWED = true;
379 $DISALLOWED = false;
381 return [
382 // Everything, if this doesn't work, we're really in trouble
383 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
384 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
386 // We validate against the title name, not the db key
387 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
388 // Main page
389 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
390 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
391 // With spaces
392 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
393 // Unicode multibyte
394 // ...without unicode modifier
395 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
396 // ...with unicode modifier
397 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
398 // Case insensitive
399 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
400 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
402 // From DefaultSettings.php:
403 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
404 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
406 // With namespaces:
407 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
408 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
413 public function flattenErrorsArray( $errors ) {
414 $result = [];
415 foreach ( $errors as $error ) {
416 $result[] = $error[0];
419 return $result;
423 * @dataProvider provideGetPageViewLanguage
424 * @covers Title::getPageViewLanguage
426 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
427 $lang, $variant, $msg = ''
429 // Setup environnement for this test
430 $this->setMwGlobals( [
431 'wgDefaultLanguageVariant' => $variant,
432 'wgAllowUserJs' => true,
433 ] );
434 $this->setUserLang( $lang );
435 $this->setContentLang( $contLang );
437 $title = Title::newFromText( $titleText );
438 $this->assertInstanceOf( 'Title', $title,
439 "Test must be passed a valid title text, you gave '$titleText'"
441 $this->assertEquals( $expected,
442 $title->getPageViewLanguage()->getCode(),
443 $msg
447 public static function provideGetPageViewLanguage() {
448 # Format:
449 # - expected
450 # - Title name
451 # - wgContLang (expected in most case)
452 # - wgLang (on some specific pages)
453 # - wgDefaultLanguageVariant
454 # - Optional message
455 return [
456 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
457 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
458 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
460 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
461 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
462 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
470 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
481 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
487 * @dataProvider provideBaseTitleCases
488 * @covers Title::getBaseText
490 public function testGetBaseText( $title, $expected, $msg = '' ) {
491 $title = Title::newFromText( $title );
492 $this->assertEquals( $expected,
493 $title->getBaseText(),
494 $msg
498 public static function provideBaseTitleCases() {
499 return [
500 # Title, expected base, optional message
501 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
502 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
507 * @dataProvider provideRootTitleCases
508 * @covers Title::getRootText
510 public function testGetRootText( $title, $expected, $msg = '' ) {
511 $title = Title::newFromText( $title );
512 $this->assertEquals( $expected,
513 $title->getRootText(),
514 $msg
518 public static function provideRootTitleCases() {
519 return [
520 # Title, expected base, optional message
521 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
522 [ 'User:Foo/Bar/Baz', 'Foo' ],
527 * @todo Handle $wgNamespacesWithSubpages cases
528 * @dataProvider provideSubpageTitleCases
529 * @covers Title::getSubpageText
531 public function testGetSubpageText( $title, $expected, $msg = '' ) {
532 $title = Title::newFromText( $title );
533 $this->assertEquals( $expected,
534 $title->getSubpageText(),
535 $msg
539 public static function provideSubpageTitleCases() {
540 return [
541 # Title, expected base, optional message
542 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
543 [ 'User:John_Doe/subOne', 'subOne' ],
547 public static function provideNewFromTitleValue() {
548 return [
549 [ new TitleValue( NS_MAIN, 'Foo' ) ],
550 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
551 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
556 * @dataProvider provideNewFromTitleValue
558 public function testNewFromTitleValue( TitleValue $value ) {
559 $title = Title::newFromTitleValue( $value );
561 $dbkey = str_replace( ' ', '_', $value->getText() );
562 $this->assertEquals( $dbkey, $title->getDBkey() );
563 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
564 $this->assertEquals( $value->getFragment(), $title->getFragment() );
567 public static function provideGetTitleValue() {
568 return [
569 [ 'Foo' ],
570 [ 'Foo#bar' ],
571 [ 'User:Hansi_Maier' ],
576 * @dataProvider provideGetTitleValue
578 public function testGetTitleValue( $text ) {
579 $title = Title::newFromText( $text );
580 $value = $title->getTitleValue();
582 $dbkey = str_replace( ' ', '_', $value->getText() );
583 $this->assertEquals( $title->getDBkey(), $dbkey );
584 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
585 $this->assertEquals( $title->getFragment(), $value->getFragment() );
588 public static function provideGetFragment() {
589 return [
590 [ 'Foo', '' ],
591 [ 'Foo#bar', 'bar' ],
592 [ 'Foo#bär', 'bär' ],
594 // Inner whitespace is normalized
595 [ 'Foo#bar_bar', 'bar bar' ],
596 [ 'Foo#bar bar', 'bar bar' ],
597 [ 'Foo#bar bar', 'bar bar' ],
599 // Leading whitespace is kept, trailing whitespace is trimmed.
600 // XXX: Is this really want we want?
601 [ 'Foo#_bar_bar_', ' bar bar' ],
602 [ 'Foo# bar bar ', ' bar bar' ],
607 * @dataProvider provideGetFragment
609 * @param string $full
610 * @param string $fragment
612 public function testGetFragment( $full, $fragment ) {
613 $title = Title::newFromText( $full );
614 $this->assertEquals( $fragment, $title->getFragment() );
618 * @covers Title::isAlwaysKnown
619 * @dataProvider provideIsAlwaysKnown
620 * @param string $page
621 * @param bool $isKnown
623 public function testIsAlwaysKnown( $page, $isKnown ) {
624 $title = Title::newFromText( $page );
625 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
628 public static function provideIsAlwaysKnown() {
629 return [
630 [ 'Some nonexistent page', false ],
631 [ 'UTPage', false ],
632 [ '#test', true ],
633 [ 'Special:BlankPage', true ],
634 [ 'Special:SomeNonexistentSpecialPage', false ],
635 [ 'MediaWiki:Parentheses', true ],
636 [ 'MediaWiki:Some nonexistent message', false ],
641 * @covers Title::isAlwaysKnown
643 public function testIsAlwaysKnownOnInterwiki() {
644 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
645 $this->assertTrue( $title->isAlwaysKnown() );
649 * @covers Title::exists
651 public function testExists() {
652 $title = Title::makeTitle( NS_PROJECT, 'New page' );
653 $linkCache = LinkCache::singleton();
655 $article = new Article( $title );
656 $page = $article->getPage();
657 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
659 // Tell Title it doesn't know whether it exists
660 $title->mArticleID = -1;
662 // Tell the link cache it doesn't exists when it really does
663 $linkCache->clearLink( $title );
664 $linkCache->addBadLinkObj( $title );
666 $this->assertEquals(
667 false,
668 $title->exists(),
669 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
671 $this->assertEquals(
672 true,
673 $title->exists( Title::GAID_FOR_UPDATE ),
674 'exists() should re-query database when GAID_FOR_UPDATE is used'
678 public function provideCanHaveTalkPage() {
679 return [
680 'User page has talk page' => [
681 Title::makeTitle( NS_USER, 'Jane' ), true
683 'Talke page has talk page' => [
684 Title::makeTitle( NS_TALK, 'Foo' ), true
686 'Special page cannot have talk page' => [
687 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
689 'Virtual namespace cannot have talk page' => [
690 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
696 * @dataProvider provideCanHaveTalkPage
697 * @covers Title::canHaveTalkPage
699 * @param Title $title
700 * @param bool $expected
702 public function testCanHaveTalkPage( Title $title, $expected ) {
703 $actual = $title->canHaveTalkPage();
704 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
708 * @dataProvider provideCanHaveTalkPage
709 * @covers Title::canTalk
711 * @param Title $title
712 * @param bool $expected
714 public function testCanTalk( Title $title, $expected ) {
715 $actual = $title->canTalk();
716 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
719 public static function provideGetTalkPage_good() {
720 return [
721 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
722 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
727 * @dataProvider provideGetTalkPage_good
728 * @covers Title::getTalkPage
730 public function testGetTalkPage_good( Title $title, Title $expected ) {
731 $talk = $title->getTalkPage();
732 $this->assertSame(
733 $expected->getPrefixedDBKey(),
734 $talk->getPrefixedDBKey(),
735 $title->getPrefixedDBKey()
740 * @dataProvider provideGetTalkPage_good
741 * @covers Title::getTalkPageIfDefined
743 public function testGetTalkPageIfDefined_good( Title $title ) {
744 $talk = $title->getTalkPageIfDefined();
745 $this->assertInstanceOf(
746 Title::class,
747 $talk,
748 $title->getPrefixedDBKey()
752 public static function provideGetTalkPage_bad() {
753 return [
754 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
755 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
760 * @dataProvider provideGetTalkPage_bad
761 * @covers Title::getTalkPageIfDefined
763 public function testGetTalkPageIfDefined_bad( Title $title ) {
764 $talk = $title->getTalkPageIfDefined();
765 $this->assertNull(
766 $talk,
767 $title->getPrefixedDBKey()
771 public function provideCreateFragmentTitle() {
772 return [
773 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
774 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
775 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
776 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
781 * @covers Title::createFragmentTarget
782 * @dataProvider provideCreateFragmentTitle
784 public function testCreateFragmentTitle( Title $title, $fragment ) {
785 $this->mergeMwGlobalArrayValue( 'wgHooks', [
786 'InterwikiLoadPrefix' => [
787 function ( $prefix, &$iwdata ) {
788 if ( $prefix === 'interwiki' ) {
789 $iwdata = [
790 'iw_url' => 'http://example.com/',
791 'iw_local' => 0,
792 'iw_trans' => 0,
794 return false;
798 ] );
800 $fragmentTitle = $title->createFragmentTarget( $fragment );
802 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
803 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
804 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
805 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
808 public function provideGetPrefixedText() {
809 return [
810 // ns = 0
812 Title::makeTitle( NS_MAIN, 'Foo bar' ),
813 'Foo bar'
815 // ns = 2
817 Title::makeTitle( NS_USER, 'Foo bar' ),
818 'User:Foo bar'
820 // ns = 3
822 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
823 'User talk:Foo bar'
825 // fragment not included
827 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
828 'Foo bar'
830 // ns = -2
832 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
833 'Media:Foo bar'
835 // non-existent namespace
837 Title::makeTitle( 100777, 'Foo bar' ),
838 'Special:Badtitle/NS100777:Foo bar'
844 * @covers Title::getPrefixedText
845 * @dataProvider provideGetPrefixedText
847 public function testGetPrefixedText( Title $title, $expected ) {
848 $this->assertEquals( $expected, $title->getPrefixedText() );
851 public function provideGetPrefixedDBKey() {
852 return [
853 // ns = 0
855 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
856 'Foo_bar'
858 // ns = 2
860 Title::makeTitle( NS_USER, 'Foo_bar' ),
861 'User:Foo_bar'
863 // ns = 3
865 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
866 'User_talk:Foo_bar'
868 // fragment not included
870 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
871 'Foo_bar'
873 // ns = -2
875 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
876 'Media:Foo_bar'
878 // non-existent namespace
880 Title::makeTitle( 100777, 'Foo_bar' ),
881 'Special:Badtitle/NS100777:Foo_bar'
887 * @covers Title::getPrefixedDBKey
888 * @dataProvider provideGetPrefixedDBKey
890 public function testGetPrefixedDBKey( Title $title, $expected ) {
891 $this->assertEquals( $expected, $title->getPrefixedDBkey() );