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