Merge "Force user id and actor id to 0 when loading from remote wikis"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 use MediaWiki\Linker\LinkTarget;
4 use MediaWiki\MediaWikiServices;
5 use Wikimedia\TestingAccessWrapper;
6
7 /**
8 * @group Database
9 * @group Title
10 */
11 class TitleTest extends MediaWikiTestCase {
12 protected function setUp() {
13 parent::setUp();
14
15 $this->setMwGlobals( [
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
18 'wgMetaNamespace' => 'Project',
19 ] );
20 $this->setUserLang( 'en' );
21 $this->setContentLang( 'en' );
22 }
23
24 protected function tearDown() {
25 // For testNewMainPage
26 MessageCache::destroyInstance();
27 parent::tearDown();
28 }
29
30 /**
31 * @covers Title::legalChars
32 */
33 public function testLegalChars() {
34 $titlechars = Title::legalChars();
35
36 foreach ( range( 1, 255 ) as $num ) {
37 $chr = chr( $num );
38 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
39 $this->assertFalse(
40 (bool)preg_match( "/[$titlechars]/", $chr ),
41 "chr($num) = $chr is not a valid titlechar"
42 );
43 } else {
44 $this->assertTrue(
45 (bool)preg_match( "/[$titlechars]/", $chr ),
46 "chr($num) = $chr is a valid titlechar"
47 );
48 }
49 }
50 }
51
52 public static function provideValidSecureAndSplit() {
53 return [
54 [ 'Sandbox' ],
55 [ 'A "B"' ],
56 [ 'A \'B\'' ],
57 [ '.com' ],
58 [ '~' ],
59 [ '#' ],
60 [ '"' ],
61 [ '\'' ],
62 [ 'Talk:Sandbox' ],
63 [ 'Talk:Foo:Sandbox' ],
64 [ 'File:Example.svg' ],
65 [ 'File_talk:Example.svg' ],
66 [ 'Foo/.../Sandbox' ],
67 [ 'Sandbox/...' ],
68 [ 'A~~' ],
69 [ ':A' ],
70 // Length is 256 total, but only title part matters
71 [ 'Category:' . str_repeat( 'x', 248 ) ],
72 [ str_repeat( 'x', 252 ) ],
73 // interwiki prefix
74 [ 'localtestiw: #anchor' ],
75 [ 'localtestiw:' ],
76 [ 'localtestiw:foo' ],
77 [ 'localtestiw: foo # anchor' ],
78 [ 'localtestiw: Talk: Sandbox # anchor' ],
79 [ 'remotetestiw:' ],
80 [ 'remotetestiw: Talk: # anchor' ],
81 [ 'remotetestiw: #bar' ],
82 [ 'remotetestiw: Talk:' ],
83 [ 'remotetestiw: Talk: Foo' ],
84 [ 'localtestiw:remotetestiw:' ],
85 [ 'localtestiw:remotetestiw:foo' ]
86 ];
87 }
88
89 public static function provideInvalidSecureAndSplit() {
90 return [
91 [ '', 'title-invalid-empty' ],
92 [ ':', 'title-invalid-empty' ],
93 [ '__ __', 'title-invalid-empty' ],
94 [ ' __ ', 'title-invalid-empty' ],
95 // Bad characters forbidden regardless of wgLegalTitleChars
96 [ 'A [ B', 'title-invalid-characters' ],
97 [ 'A ] B', 'title-invalid-characters' ],
98 [ 'A { B', 'title-invalid-characters' ],
99 [ 'A } B', 'title-invalid-characters' ],
100 [ 'A < B', 'title-invalid-characters' ],
101 [ 'A > B', 'title-invalid-characters' ],
102 [ 'A | B', 'title-invalid-characters' ],
103 [ "A \t B", 'title-invalid-characters' ],
104 [ "A \n B", 'title-invalid-characters' ],
105 // URL encoding
106 [ 'A%20B', 'title-invalid-characters' ],
107 [ 'A%23B', 'title-invalid-characters' ],
108 [ 'A%2523B', 'title-invalid-characters' ],
109 // XML/HTML character entity references
110 // Note: Commented out because they are not marked invalid by the PHP test as
111 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
112 // 'A &eacute; B',
113 // 'A &#233; B',
114 // 'A &#x00E9; B',
115 // Subject of NS_TALK does not roundtrip to NS_MAIN
116 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
117 // Directory navigation
118 [ '.', 'title-invalid-relative' ],
119 [ '..', 'title-invalid-relative' ],
120 [ './Sandbox', 'title-invalid-relative' ],
121 [ '../Sandbox', 'title-invalid-relative' ],
122 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
123 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
124 [ 'Sandbox/.', 'title-invalid-relative' ],
125 [ 'Sandbox/..', 'title-invalid-relative' ],
126 // Tilde
127 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
128 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
129 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
130 // Length
131 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
132 // Namespace prefix without actual title
133 [ 'Talk:', 'title-invalid-empty' ],
134 [ 'Talk:#', 'title-invalid-empty' ],
135 [ 'Category: ', 'title-invalid-empty' ],
136 [ 'Category: #bar', 'title-invalid-empty' ],
137 // interwiki prefix
138 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
139 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
140 ];
141 }
142
143 private function secureAndSplitGlobals() {
144 $this->setMwGlobals( [
145 'wgLocalInterwikis' => [ 'localtestiw' ],
146 'wgHooks' => [
147 'InterwikiLoadPrefix' => [
148 function ( $prefix, &$data ) {
149 if ( $prefix === 'localtestiw' ) {
150 $data = [ 'iw_url' => 'localtestiw' ];
151 } elseif ( $prefix === 'remotetestiw' ) {
152 $data = [ 'iw_url' => 'remotetestiw' ];
153 }
154 return false;
155 }
156 ]
157 ]
158 ] );
159
160 // Reset services since we modified $wgLocalInterwikis
161 $this->overrideMwServices();
162 }
163
164 /**
165 * See also mediawiki.Title.test.js
166 * @covers Title::secureAndSplit
167 * @dataProvider provideValidSecureAndSplit
168 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
169 */
170 public function testSecureAndSplitValid( $text ) {
171 $this->secureAndSplitGlobals();
172 $this->assertInstanceOf( Title::class, Title::newFromText( $text ), "Valid: $text" );
173 }
174
175 /**
176 * See also mediawiki.Title.test.js
177 * @covers Title::secureAndSplit
178 * @dataProvider provideInvalidSecureAndSplit
179 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
180 */
181 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
182 $this->secureAndSplitGlobals();
183 try {
184 Title::newFromTextThrow( $text ); // should throw
185 $this->assertTrue( false, "Invalid: $text" );
186 } catch ( MalformedTitleException $ex ) {
187 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
188 }
189 }
190
191 public static function provideConvertByteClassToUnicodeClass() {
192 return [
193 [
194 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
195 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
196 ],
197 [
198 'QWERTYf-\\xFF+',
199 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
200 ],
201 [
202 'QWERTY\\x66-\\xFD+',
203 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
204 ],
205 [
206 'QWERTYf-y+',
207 'QWERTYf-y+',
208 ],
209 [
210 'QWERTYf-\\x80+',
211 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
212 ],
213 [
214 'QWERTY\\x66-\\x80+\\x23',
215 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
216 ],
217 [
218 'QWERTY\\x66-\\x80+\\xD3',
219 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
220 ],
221 [
222 '\\\\\\x99',
223 '\\\\\\u0080-\\uFFFF',
224 ],
225 [
226 '-\\x99',
227 '\\-\\u0080-\\uFFFF',
228 ],
229 [
230 'QWERTY\\-\\x99',
231 'QWERTY\\-\\u0080-\\uFFFF',
232 ],
233 [
234 '\\\\x99',
235 '\\\\x99',
236 ],
237 [
238 'A-\\x9F',
239 'A-\\x7F\\u0080-\\uFFFF',
240 ],
241 [
242 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
243 'f-wQWERTYFXZ\\u0080-\\uFFFF',
244 ],
245 [
246 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
247 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
248 ],
249 ];
250 }
251
252 /**
253 * @dataProvider provideConvertByteClassToUnicodeClass
254 * @covers Title::convertByteClassToUnicodeClass
255 */
256 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
257 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
258 }
259
260 /**
261 * @dataProvider provideSpecialNamesWithAndWithoutParameter
262 * @covers Title::fixSpecialName
263 */
264 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
265 $title = Title::newFromText( $text );
266 $fixed = $title->fixSpecialName();
267 $stuff = explode( '/', $fixed->getDBkey(), 2 );
268 if ( count( $stuff ) == 2 ) {
269 $par = $stuff[1];
270 } else {
271 $par = null;
272 }
273 $this->assertEquals(
274 $expectedParam,
275 $par,
276 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
277 );
278 }
279
280 public static function provideSpecialNamesWithAndWithoutParameter() {
281 return [
282 [ 'Special:Version', null ],
283 [ 'Special:Version/', '' ],
284 [ 'Special:Version/param', 'param' ],
285 ];
286 }
287
288 /**
289 * Auth-less test of Title::isValidMoveOperation
290 *
291 * @param string $source
292 * @param string $target
293 * @param array|string|bool $expected Required error
294 * @dataProvider provideTestIsValidMoveOperation
295 * @covers Title::isValidMoveOperation
296 */
297 public function testIsValidMoveOperation( $source, $target, $expected ) {
298 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
299 $title = Title::newFromText( $source );
300 $nt = Title::newFromText( $target );
301 $errors = $title->isValidMoveOperation( $nt, false );
302 if ( $expected === true ) {
303 $this->assertTrue( $errors );
304 } else {
305 $errors = $this->flattenErrorsArray( $errors );
306 foreach ( (array)$expected as $error ) {
307 $this->assertContains( $error, $errors );
308 }
309 }
310 }
311
312 public static function provideTestIsValidMoveOperation() {
313 return [
314 // for Title::isValidMoveOperation
315 [ 'Some page', '', 'badtitletext' ],
316 [ 'Test', 'Test', 'selfmove' ],
317 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
318 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
319 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
320 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
321 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
322 ];
323 }
324
325 /**
326 * Auth-less test of Title::userCan
327 *
328 * @param array $whitelistRegexp
329 * @param string $source
330 * @param string $action
331 * @param array|string|bool $expected Required error
332 *
333 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
334 * @dataProvider dataWgWhitelistReadRegexp
335 */
336 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
337 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
338 // usually have only one regex, it is more concise to write the lonely regex
339 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
340 // type requisite.
341 if ( is_string( $whitelistRegexp ) ) {
342 $whitelistRegexp = [ $whitelistRegexp ];
343 }
344
345 $this->setMwGlobals( [
346 // So User::isEveryoneAllowed( 'read' ) === false
347 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
348 'wgWhitelistRead' => [ 'some random non sense title' ],
349 'wgWhitelistReadRegexp' => $whitelistRegexp,
350 ] );
351
352 $title = Title::newFromDBkey( $source );
353
354 // New anonymous user with no rights
355 $user = new User;
356 $user->mRights = [];
357 $errors = $title->userCan( $action, $user );
358
359 if ( is_bool( $expected ) ) {
360 # Forge the assertion message depending on the assertion expectation
361 $allowableness = $expected
362 ? " should be allowed"
363 : " should NOT be allowed";
364 $this->assertEquals(
365 $expected,
366 $errors,
367 "User action '$action' on [[$source]] $allowableness."
368 );
369 } else {
370 $errors = $this->flattenErrorsArray( $errors );
371 foreach ( (array)$expected as $error ) {
372 $this->assertContains( $error, $errors );
373 }
374 }
375 }
376
377 /**
378 * Provides test parameter values for testWgWhitelistReadRegexp()
379 */
380 public function dataWgWhitelistReadRegexp() {
381 $ALLOWED = true;
382 $DISALLOWED = false;
383
384 return [
385 // Everything, if this doesn't work, we're really in trouble
386 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
387 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
388
389 // We validate against the title name, not the db key
390 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
391 // Main page
392 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
393 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
394 // With spaces
395 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
396 // Unicode multibyte
397 // ...without unicode modifier
398 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
399 // ...with unicode modifier
400 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
401 // Case insensitive
402 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
403 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
404
405 // From DefaultSettings.php:
406 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
407 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
408
409 // With namespaces:
410 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
411 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
412
413 ];
414 }
415
416 public function flattenErrorsArray( $errors ) {
417 $result = [];
418 foreach ( $errors as $error ) {
419 $result[] = $error[0];
420 }
421
422 return $result;
423 }
424
425 /**
426 * @dataProvider provideGetPageViewLanguage
427 * @covers Title::getPageViewLanguage
428 */
429 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
430 $lang, $variant, $msg = ''
431 ) {
432 // Setup environnement for this test
433 $this->setMwGlobals( [
434 'wgDefaultLanguageVariant' => $variant,
435 'wgAllowUserJs' => true,
436 ] );
437 $this->setUserLang( $lang );
438 $this->setContentLang( $contLang );
439
440 $title = Title::newFromText( $titleText );
441 $this->assertInstanceOf( Title::class, $title,
442 "Test must be passed a valid title text, you gave '$titleText'"
443 );
444 $this->assertEquals( $expected,
445 $title->getPageViewLanguage()->getCode(),
446 $msg
447 );
448 }
449
450 public static function provideGetPageViewLanguage() {
451 # Format:
452 # - expected
453 # - Title name
454 # - content language (expected in most cases)
455 # - wgLang (on some specific pages)
456 # - wgDefaultLanguageVariant
457 # - Optional message
458 return [
459 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
460 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
461 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
462
463 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
470 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
471
472 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
481 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
482
483 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
484 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
485
486 ];
487 }
488
489 /**
490 * @dataProvider provideBaseTitleCases
491 * @covers Title::getBaseText
492 */
493 public function testGetBaseText( $title, $expected, $msg = '' ) {
494 $title = Title::newFromText( $title );
495 $this->assertEquals( $expected,
496 $title->getBaseText(),
497 $msg
498 );
499 }
500
501 public static function provideBaseTitleCases() {
502 return [
503 # Title, expected base, optional message
504 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
505 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
506 ];
507 }
508
509 /**
510 * @dataProvider provideRootTitleCases
511 * @covers Title::getRootText
512 */
513 public function testGetRootText( $title, $expected, $msg = '' ) {
514 $title = Title::newFromText( $title );
515 $this->assertEquals( $expected,
516 $title->getRootText(),
517 $msg
518 );
519 }
520
521 public static function provideRootTitleCases() {
522 return [
523 # Title, expected base, optional message
524 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
525 [ 'User:Foo/Bar/Baz', 'Foo' ],
526 ];
527 }
528
529 /**
530 * @todo Handle $wgNamespacesWithSubpages cases
531 * @dataProvider provideSubpageTitleCases
532 * @covers Title::getSubpageText
533 */
534 public function testGetSubpageText( $title, $expected, $msg = '' ) {
535 $title = Title::newFromText( $title );
536 $this->assertEquals( $expected,
537 $title->getSubpageText(),
538 $msg
539 );
540 }
541
542 public static function provideSubpageTitleCases() {
543 return [
544 # Title, expected base, optional message
545 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
546 [ 'User:John_Doe/subOne', 'subOne' ],
547 ];
548 }
549
550 public static function provideNewFromTitleValue() {
551 return [
552 [ new TitleValue( NS_MAIN, 'Foo' ) ],
553 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
554 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
555 ];
556 }
557
558 /**
559 * @covers Title::newFromTitleValue
560 * @dataProvider provideNewFromTitleValue
561 */
562 public function testNewFromTitleValue( TitleValue $value ) {
563 $title = Title::newFromTitleValue( $value );
564
565 $dbkey = str_replace( ' ', '_', $value->getText() );
566 $this->assertEquals( $dbkey, $title->getDBkey() );
567 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
568 $this->assertEquals( $value->getFragment(), $title->getFragment() );
569 }
570
571 /**
572 * @covers Title::newFromLinkTarget
573 * @dataProvider provideNewFromTitleValue
574 */
575 public function testNewFromLinkTarget( LinkTarget $value ) {
576 $title = Title::newFromLinkTarget( $value );
577
578 $dbkey = str_replace( ' ', '_', $value->getText() );
579 $this->assertEquals( $dbkey, $title->getDBkey() );
580 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
581 $this->assertEquals( $value->getFragment(), $title->getFragment() );
582 }
583
584 /**
585 * @covers Title::newFromLinkTarget
586 */
587 public function testNewFromLinkTarget_clone() {
588 $title = Title::newFromText( __METHOD__ );
589 $this->assertSame( $title, Title::newFromLinkTarget( $title ) );
590
591 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
592 $clone = Title::newFromLinkTarget( $title, Title::NEW_CLONE );
593 $this->assertNotSame( $title, $clone );
594 $this->assertTrue( $clone->equals( $title ) );
595 }
596
597 public function provideCastFromLinkTarget() {
598 return array_merge( [ [ null ] ], self::provideNewFromTitleValue() );
599 }
600
601 /**
602 * @covers Title::castFromLinkTarget
603 * @dataProvider provideCastFromLinkTarget
604 */
605 public function testCastFromLinkTarget( $value ) {
606 $title = Title::castFromLinkTarget( $value );
607
608 if ( $value === null ) {
609 $this->assertNull( $title );
610 } else {
611 $dbkey = str_replace( ' ', '_', $value->getText() );
612 $this->assertSame( $dbkey, $title->getDBkey() );
613 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
614 $this->assertSame( $value->getFragment(), $title->getFragment() );
615 }
616 }
617
618 public static function provideGetTitleValue() {
619 return [
620 [ 'Foo' ],
621 [ 'Foo#bar' ],
622 [ 'User:Hansi_Maier' ],
623 ];
624 }
625
626 /**
627 * @covers Title::getTitleValue
628 * @dataProvider provideGetTitleValue
629 */
630 public function testGetTitleValue( $text ) {
631 $title = Title::newFromText( $text );
632 $value = $title->getTitleValue();
633
634 $dbkey = str_replace( ' ', '_', $value->getText() );
635 $this->assertEquals( $title->getDBkey(), $dbkey );
636 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
637 $this->assertEquals( $title->getFragment(), $value->getFragment() );
638 }
639
640 public static function provideGetFragment() {
641 return [
642 [ 'Foo', '' ],
643 [ 'Foo#bar', 'bar' ],
644 [ 'Foo#bär', 'bär' ],
645
646 // Inner whitespace is normalized
647 [ 'Foo#bar_bar', 'bar bar' ],
648 [ 'Foo#bar bar', 'bar bar' ],
649 [ 'Foo#bar bar', 'bar bar' ],
650
651 // Leading whitespace is kept, trailing whitespace is trimmed.
652 // XXX: Is this really want we want?
653 [ 'Foo#_bar_bar_', ' bar bar' ],
654 [ 'Foo# bar bar ', ' bar bar' ],
655 ];
656 }
657
658 /**
659 * @covers Title::getFragment
660 * @dataProvider provideGetFragment
661 *
662 * @param string $full
663 * @param string $fragment
664 */
665 public function testGetFragment( $full, $fragment ) {
666 $title = Title::newFromText( $full );
667 $this->assertEquals( $fragment, $title->getFragment() );
668 }
669
670 /**
671 * @covers Title::isAlwaysKnown
672 * @dataProvider provideIsAlwaysKnown
673 * @param string $page
674 * @param bool $isKnown
675 */
676 public function testIsAlwaysKnown( $page, $isKnown ) {
677 $title = Title::newFromText( $page );
678 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
679 }
680
681 public static function provideIsAlwaysKnown() {
682 return [
683 [ 'Some nonexistent page', false ],
684 [ 'UTPage', false ],
685 [ '#test', true ],
686 [ 'Special:BlankPage', true ],
687 [ 'Special:SomeNonexistentSpecialPage', false ],
688 [ 'MediaWiki:Parentheses', true ],
689 [ 'MediaWiki:Some nonexistent message', false ],
690 ];
691 }
692
693 /**
694 * @covers Title::isValid
695 * @dataProvider provideIsValid
696 * @param Title $title
697 * @param bool $isValid
698 */
699 public function testIsValid( Title $title, $isValid ) {
700 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
701 }
702
703 public static function provideIsValid() {
704 return [
705 [ Title::makeTitle( NS_MAIN, '' ), false ],
706 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
707 [ Title::makeTitle( NS_MAIN, '|' ), false ],
708 [ Title::makeTitle( NS_MAIN, '#' ), false ],
709 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
710 [ Title::makeTitle( -33, 'Test' ), false ],
711 [ Title::makeTitle( 77663399, 'Test' ), false ],
712 ];
713 }
714
715 /**
716 * @covers Title::isAlwaysKnown
717 */
718 public function testIsAlwaysKnownOnInterwiki() {
719 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
720 $this->assertTrue( $title->isAlwaysKnown() );
721 }
722
723 /**
724 * @covers Title::exists
725 */
726 public function testExists() {
727 $title = Title::makeTitle( NS_PROJECT, 'New page' );
728 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
729
730 $article = new Article( $title );
731 $page = $article->getPage();
732 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
733
734 // Tell Title it doesn't know whether it exists
735 $title->mArticleID = -1;
736
737 // Tell the link cache it doesn't exists when it really does
738 $linkCache->clearLink( $title );
739 $linkCache->addBadLinkObj( $title );
740
741 $this->assertEquals(
742 false,
743 $title->exists(),
744 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
745 );
746 $this->assertEquals(
747 true,
748 $title->exists( Title::GAID_FOR_UPDATE ),
749 'exists() should re-query database when GAID_FOR_UPDATE is used'
750 );
751 }
752
753 public function provideCanHaveTalkPage() {
754 return [
755 'User page has talk page' => [
756 Title::makeTitle( NS_USER, 'Jane' ), true
757 ],
758 'Talke page has talk page' => [
759 Title::makeTitle( NS_TALK, 'Foo' ), true
760 ],
761 'Special page cannot have talk page' => [
762 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
763 ],
764 'Virtual namespace cannot have talk page' => [
765 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
766 ],
767 ];
768 }
769
770 /**
771 * @dataProvider provideCanHaveTalkPage
772 * @covers Title::canHaveTalkPage
773 *
774 * @param Title $title
775 * @param bool $expected
776 */
777 public function testCanHaveTalkPage( Title $title, $expected ) {
778 $actual = $title->canHaveTalkPage();
779 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
780 }
781
782 public static function provideGetTalkPage_good() {
783 return [
784 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
785 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
786 ];
787 }
788
789 /**
790 * @dataProvider provideGetTalkPage_good
791 * @covers Title::getTalkPage
792 */
793 public function testGetTalkPage_good( Title $title, Title $expected ) {
794 $talk = $title->getTalkPage();
795 $this->assertSame(
796 $expected->getPrefixedDBKey(),
797 $talk->getPrefixedDBKey(),
798 $title->getPrefixedDBKey()
799 );
800 }
801
802 /**
803 * @dataProvider provideGetTalkPage_good
804 * @covers Title::getTalkPageIfDefined
805 */
806 public function testGetTalkPageIfDefined_good( Title $title ) {
807 $talk = $title->getTalkPageIfDefined();
808 $this->assertInstanceOf(
809 Title::class,
810 $talk,
811 $title->getPrefixedDBKey()
812 );
813 }
814
815 public static function provideGetTalkPage_bad() {
816 return [
817 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
818 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
819 ];
820 }
821
822 /**
823 * @dataProvider provideGetTalkPage_bad
824 * @covers Title::getTalkPageIfDefined
825 */
826 public function testGetTalkPageIfDefined_bad( Title $title ) {
827 $talk = $title->getTalkPageIfDefined();
828 $this->assertNull(
829 $talk,
830 $title->getPrefixedDBKey()
831 );
832 }
833
834 public function provideCreateFragmentTitle() {
835 return [
836 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
837 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
838 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
839 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
840 ];
841 }
842
843 /**
844 * @covers Title::createFragmentTarget
845 * @dataProvider provideCreateFragmentTitle
846 */
847 public function testCreateFragmentTitle( Title $title, $fragment ) {
848 $this->mergeMwGlobalArrayValue( 'wgHooks', [
849 'InterwikiLoadPrefix' => [
850 function ( $prefix, &$iwdata ) {
851 if ( $prefix === 'interwiki' ) {
852 $iwdata = [
853 'iw_url' => 'http://example.com/',
854 'iw_local' => 0,
855 'iw_trans' => 0,
856 ];
857 return false;
858 }
859 },
860 ],
861 ] );
862
863 $fragmentTitle = $title->createFragmentTarget( $fragment );
864
865 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
866 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
867 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
868 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
869 }
870
871 public function provideGetPrefixedText() {
872 return [
873 // ns = 0
874 [
875 Title::makeTitle( NS_MAIN, 'Foo bar' ),
876 'Foo bar'
877 ],
878 // ns = 2
879 [
880 Title::makeTitle( NS_USER, 'Foo bar' ),
881 'User:Foo bar'
882 ],
883 // ns = 3
884 [
885 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
886 'User talk:Foo bar'
887 ],
888 // fragment not included
889 [
890 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
891 'Foo bar'
892 ],
893 // ns = -2
894 [
895 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
896 'Media:Foo bar'
897 ],
898 // non-existent namespace
899 [
900 Title::makeTitle( 100777, 'Foo bar' ),
901 'Special:Badtitle/NS100777:Foo bar'
902 ],
903 ];
904 }
905
906 /**
907 * @covers Title::getPrefixedText
908 * @dataProvider provideGetPrefixedText
909 */
910 public function testGetPrefixedText( Title $title, $expected ) {
911 $this->assertEquals( $expected, $title->getPrefixedText() );
912 }
913
914 public function provideGetPrefixedDBKey() {
915 return [
916 // ns = 0
917 [
918 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
919 'Foo_bar'
920 ],
921 // ns = 2
922 [
923 Title::makeTitle( NS_USER, 'Foo_bar' ),
924 'User:Foo_bar'
925 ],
926 // ns = 3
927 [
928 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
929 'User_talk:Foo_bar'
930 ],
931 // fragment not included
932 [
933 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
934 'Foo_bar'
935 ],
936 // ns = -2
937 [
938 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
939 'Media:Foo_bar'
940 ],
941 // non-existent namespace
942 [
943 Title::makeTitle( 100777, 'Foo_bar' ),
944 'Special:Badtitle/NS100777:Foo_bar'
945 ],
946 ];
947 }
948
949 /**
950 * @covers Title::getPrefixedDBKey
951 * @dataProvider provideGetPrefixedDBKey
952 */
953 public function testGetPrefixedDBKey( Title $title, $expected ) {
954 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
955 }
956
957 /**
958 * @covers Title::getFragmentForURL
959 * @dataProvider provideGetFragmentForURL
960 *
961 * @param string $titleStr
962 * @param string $expected
963 */
964 public function testGetFragmentForURL( $titleStr, $expected ) {
965 $this->setMwGlobals( [
966 'wgFragmentMode' => [ 'html5' ],
967 'wgExternalInterwikiFragmentMode' => 'legacy',
968 ] );
969 $dbw = wfGetDB( DB_MASTER );
970 $dbw->insert( 'interwiki',
971 [
972 [
973 'iw_prefix' => 'de',
974 'iw_url' => 'http://de.wikipedia.org/wiki/',
975 'iw_api' => 'http://de.wikipedia.org/w/api.php',
976 'iw_wikiid' => 'dewiki',
977 'iw_local' => 1,
978 'iw_trans' => 0,
979 ],
980 [
981 'iw_prefix' => 'zz',
982 'iw_url' => 'http://zzwiki.org/wiki/',
983 'iw_api' => 'http://zzwiki.org/w/api.php',
984 'iw_wikiid' => 'zzwiki',
985 'iw_local' => 0,
986 'iw_trans' => 0,
987 ],
988 ],
989 __METHOD__,
990 [ 'IGNORE' ]
991 );
992
993 $title = Title::newFromText( $titleStr );
994 self::assertEquals( $expected, $title->getFragmentForURL() );
995
996 $dbw->delete( 'interwiki', '*', __METHOD__ );
997 }
998
999 public function provideGetFragmentForURL() {
1000 return [
1001 [ 'Foo', '' ],
1002 [ 'Foo#ümlåût', '#ümlåût' ],
1003 [ 'de:Foo#Bå®', '#Bå®' ],
1004 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1005 ];
1006 }
1007
1008 /**
1009 * @covers Title::isRawHtmlMessage
1010 * @dataProvider provideIsRawHtmlMessage
1011 */
1012 public function testIsRawHtmlMessage( $textForm, $expected ) {
1013 $this->setMwGlobals( 'wgRawHtmlMessages', [
1014 'foobar',
1015 'foo_bar',
1016 'foo-bar',
1017 ] );
1018
1019 $title = Title::newFromText( $textForm );
1020 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1021 }
1022
1023 public function provideIsRawHtmlMessage() {
1024 return [
1025 [ 'MediaWiki:Foobar', true ],
1026 [ 'MediaWiki:Foo bar', true ],
1027 [ 'MediaWiki:Foo-bar', true ],
1028 [ 'MediaWiki:foo bar', true ],
1029 [ 'MediaWiki:foo-bar', true ],
1030 [ 'MediaWiki:foobar', true ],
1031 [ 'MediaWiki:some-other-message', false ],
1032 [ 'Main Page', false ],
1033 ];
1034 }
1035
1036 public function provideEquals() {
1037 yield [
1038 Title::newFromText( 'Main Page' ),
1039 Title::newFromText( 'Main Page' ),
1040 true
1041 ];
1042 yield [
1043 Title::newFromText( 'Main Page' ),
1044 Title::newFromText( 'Not The Main Page' ),
1045 false
1046 ];
1047 yield [
1048 Title::newFromText( 'Main Page' ),
1049 Title::newFromText( 'Project:Main Page' ),
1050 false
1051 ];
1052 yield [
1053 Title::newFromText( 'File:Example.png' ),
1054 Title::newFromText( 'Image:Example.png' ),
1055 true
1056 ];
1057 yield [
1058 Title::newFromText( 'Special:Version' ),
1059 Title::newFromText( 'Special:Version' ),
1060 true
1061 ];
1062 yield [
1063 Title::newFromText( 'Special:Version' ),
1064 Title::newFromText( 'Special:Recentchanges' ),
1065 false
1066 ];
1067 yield [
1068 Title::newFromText( 'Special:Version' ),
1069 Title::newFromText( 'Main Page' ),
1070 false
1071 ];
1072 yield [
1073 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1074 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1075 true
1076 ];
1077 yield [
1078 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1079 Title::makeTitle( NS_MAIN, 'Bar', '', '' ),
1080 false
1081 ];
1082 yield [
1083 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1084 Title::makeTitle( NS_TALK, 'Foo', '', '' ),
1085 false
1086 ];
1087 yield [
1088 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1089 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1090 true
1091 ];
1092 yield [
1093 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1094 Title::makeTitle( NS_MAIN, 'Foo', 'Baz', '' ),
1095 true
1096 ];
1097 yield [
1098 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1099 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1100 true
1101 ];
1102 yield [
1103 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1104 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1105 true
1106 ];
1107 yield [
1108 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1109 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1110 false
1111 ];
1112 }
1113
1114 /**
1115 * @covers Title::equals
1116 * @dataProvider provideEquals
1117 */
1118 public function testEquals( Title $firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1119 $this->assertSame(
1120 $expectedSame,
1121 $firstValue->equals( $secondValue )
1122 );
1123 }
1124
1125 /**
1126 * @covers Title::newMainPage
1127 */
1128 public function testNewMainPage() {
1129 $msgCache = TestingAccessWrapper::newFromClass( MessageCache::class );
1130 $msgCache->instance = $this->createMock( MessageCache::class );
1131 $msgCache->instance->method( 'get' )->willReturn( 'Foresheet' );
1132 $msgCache->instance->method( 'transform' )->willReturn( 'Foresheet' );
1133
1134 $this->assertSame(
1135 'Foresheet',
1136 Title::newMainPage()->getText()
1137 );
1138 }
1139
1140 /**
1141 * @covers Title::newMainPage
1142 */
1143 public function testNewMainPageWithLocal() {
1144 $local = $this->createMock( MessageLocalizer::class );
1145 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1146
1147 $this->assertSame(
1148 'Prime Article',
1149 Title::newMainPage( $local )->getText()
1150 );
1151 }
1152 }