Merge "objectcache: make BagOStuff::getMulti() preserve order and omit keys with...
[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 ];
558 }
559
560 /**
561 * @todo Handle $wgNamespacesWithSubpages cases
562 * @dataProvider provideSubpageTitleCases
563 * @covers Title::getSubpageText
564 */
565 public function testGetSubpageText( $title, $expected, $msg = '' ) {
566 $title = Title::newFromText( $title );
567 $this->assertEquals( $expected,
568 $title->getSubpageText(),
569 $msg
570 );
571 }
572
573 public static function provideSubpageTitleCases() {
574 return [
575 # Title, expected base, optional message
576 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
577 [ 'User:John_Doe/subOne', 'subOne' ],
578 ];
579 }
580
581 public function provideSubpage() {
582 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
583 return [
584 [ 'Foo', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
585 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
586 [ 'User:Foo', 'x', new TitleValue( NS_USER, 'Foo/x' ) ],
587 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN, 'User:Foo/x', '', 'wiki' ) ],
588 ];
589 }
590
591 /**
592 * @dataProvider provideSubpage
593 * @covers Title::getSubpage
594 */
595 public function testSubpage( $title, $sub, LinkTarget $expected ) {
596 $interwikiLookup = $this->getMock( InterwikiLookup::class );
597 $interwikiLookup->expects( $this->any() )
598 ->method( 'isValidInterwiki' )
599 ->willReturnCallback(
600 function ( $prefix ) {
601 return $prefix == 'wiki';
602 }
603 );
604
605 $this->setService( 'InterwikiLookup', $interwikiLookup );
606
607 $title = Title::newFromText( $title );
608 $expected = Title::newFromLinkTarget( $expected );
609 $actual = $title->getSubpage( $sub );
610
611 // NOTE: convert to string for comparison
612 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
613 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
614 }
615
616 public static function provideNewFromTitleValue() {
617 return [
618 [ new TitleValue( NS_MAIN, 'Foo' ) ],
619 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
620 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
621 ];
622 }
623
624 /**
625 * @covers Title::newFromTitleValue
626 * @dataProvider provideNewFromTitleValue
627 */
628 public function testNewFromTitleValue( TitleValue $value ) {
629 $title = Title::newFromTitleValue( $value );
630
631 $dbkey = str_replace( ' ', '_', $value->getText() );
632 $this->assertEquals( $dbkey, $title->getDBkey() );
633 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
634 $this->assertEquals( $value->getFragment(), $title->getFragment() );
635 }
636
637 /**
638 * @covers Title::newFromLinkTarget
639 * @dataProvider provideNewFromTitleValue
640 */
641 public function testNewFromLinkTarget( LinkTarget $value ) {
642 $title = Title::newFromLinkTarget( $value );
643
644 $dbkey = str_replace( ' ', '_', $value->getText() );
645 $this->assertEquals( $dbkey, $title->getDBkey() );
646 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
647 $this->assertEquals( $value->getFragment(), $title->getFragment() );
648 }
649
650 /**
651 * @covers Title::newFromLinkTarget
652 */
653 public function testNewFromLinkTarget_clone() {
654 $title = Title::newFromText( __METHOD__ );
655 $this->assertSame( $title, Title::newFromLinkTarget( $title ) );
656
657 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
658 $clone = Title::newFromLinkTarget( $title, Title::NEW_CLONE );
659 $this->assertNotSame( $title, $clone );
660 $this->assertTrue( $clone->equals( $title ) );
661 }
662
663 public function provideCastFromLinkTarget() {
664 return array_merge( [ [ null ] ], self::provideNewFromTitleValue() );
665 }
666
667 /**
668 * @covers Title::castFromLinkTarget
669 * @dataProvider provideCastFromLinkTarget
670 */
671 public function testCastFromLinkTarget( $value ) {
672 $title = Title::castFromLinkTarget( $value );
673
674 if ( $value === null ) {
675 $this->assertNull( $title );
676 } else {
677 $dbkey = str_replace( ' ', '_', $value->getText() );
678 $this->assertSame( $dbkey, $title->getDBkey() );
679 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
680 $this->assertSame( $value->getFragment(), $title->getFragment() );
681 }
682 }
683
684 public static function provideGetTitleValue() {
685 return [
686 [ 'Foo' ],
687 [ 'Foo#bar' ],
688 [ 'User:Hansi_Maier' ],
689 ];
690 }
691
692 /**
693 * @covers Title::getTitleValue
694 * @dataProvider provideGetTitleValue
695 */
696 public function testGetTitleValue( $text ) {
697 $title = Title::newFromText( $text );
698 $value = $title->getTitleValue();
699
700 $dbkey = str_replace( ' ', '_', $value->getText() );
701 $this->assertEquals( $title->getDBkey(), $dbkey );
702 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
703 $this->assertEquals( $title->getFragment(), $value->getFragment() );
704 }
705
706 public static function provideGetFragment() {
707 return [
708 [ 'Foo', '' ],
709 [ 'Foo#bar', 'bar' ],
710 [ 'Foo#bär', 'bär' ],
711
712 // Inner whitespace is normalized
713 [ 'Foo#bar_bar', 'bar bar' ],
714 [ 'Foo#bar bar', 'bar bar' ],
715 [ 'Foo#bar bar', 'bar bar' ],
716
717 // Leading whitespace is kept, trailing whitespace is trimmed.
718 // XXX: Is this really want we want?
719 [ 'Foo#_bar_bar_', ' bar bar' ],
720 [ 'Foo# bar bar ', ' bar bar' ],
721 ];
722 }
723
724 /**
725 * @covers Title::getFragment
726 * @dataProvider provideGetFragment
727 *
728 * @param string $full
729 * @param string $fragment
730 */
731 public function testGetFragment( $full, $fragment ) {
732 $title = Title::newFromText( $full );
733 $this->assertEquals( $fragment, $title->getFragment() );
734 }
735
736 /**
737 * @covers Title::isAlwaysKnown
738 * @dataProvider provideIsAlwaysKnown
739 * @param string $page
740 * @param bool $isKnown
741 */
742 public function testIsAlwaysKnown( $page, $isKnown ) {
743 $title = Title::newFromText( $page );
744 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
745 }
746
747 public static function provideIsAlwaysKnown() {
748 return [
749 [ 'Some nonexistent page', false ],
750 [ 'UTPage', false ],
751 [ '#test', true ],
752 [ 'Special:BlankPage', true ],
753 [ 'Special:SomeNonexistentSpecialPage', false ],
754 [ 'MediaWiki:Parentheses', true ],
755 [ 'MediaWiki:Some nonexistent message', false ],
756 ];
757 }
758
759 /**
760 * @covers Title::isValid
761 * @dataProvider provideIsValid
762 * @param Title $title
763 * @param bool $isValid
764 */
765 public function testIsValid( Title $title, $isValid ) {
766 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
767 }
768
769 public static function provideIsValid() {
770 return [
771 [ Title::makeTitle( NS_MAIN, '' ), false ],
772 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
773 [ Title::makeTitle( NS_MAIN, '|' ), false ],
774 [ Title::makeTitle( NS_MAIN, '#' ), false ],
775 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
776 [ Title::makeTitle( NS_MAIN, ' Test' ), false ],
777 [ Title::makeTitle( NS_MAIN, '_Test' ), false ],
778 [ Title::makeTitle( NS_MAIN, 'Test ' ), false ],
779 [ Title::makeTitle( NS_MAIN, 'Test_' ), false ],
780 [ Title::makeTitle( NS_MAIN, "Test\nthis" ), false ],
781 [ Title::makeTitle( NS_MAIN, "Test\tthis" ), false ],
782 [ Title::makeTitle( -33, 'Test' ), false ],
783 [ Title::makeTitle( 77663399, 'Test' ), false ],
784 ];
785 }
786
787 /**
788 * @covers Title::isAlwaysKnown
789 */
790 public function testIsAlwaysKnownOnInterwiki() {
791 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
792 $this->assertTrue( $title->isAlwaysKnown() );
793 }
794
795 /**
796 * @covers Title::exists
797 */
798 public function testExists() {
799 $title = Title::makeTitle( NS_PROJECT, 'New page' );
800 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
801
802 $article = new Article( $title );
803 $page = $article->getPage();
804 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
805
806 // Tell Title it doesn't know whether it exists
807 $title->mArticleID = -1;
808
809 // Tell the link cache it doesn't exists when it really does
810 $linkCache->clearLink( $title );
811 $linkCache->addBadLinkObj( $title );
812
813 $this->assertEquals(
814 false,
815 $title->exists(),
816 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
817 );
818 $this->assertEquals(
819 true,
820 $title->exists( Title::GAID_FOR_UPDATE ),
821 'exists() should re-query database when GAID_FOR_UPDATE is used'
822 );
823 }
824
825 public function provideCanHaveTalkPage() {
826 return [
827 'User page has talk page' => [
828 Title::makeTitle( NS_USER, 'Jane' ), true
829 ],
830 'Talke page has talk page' => [
831 Title::makeTitle( NS_TALK, 'Foo' ), true
832 ],
833 'Special page cannot have talk page' => [
834 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
835 ],
836 'Virtual namespace cannot have talk page' => [
837 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
838 ],
839 'Relative link has no talk page' => [
840 Title::makeTitle( NS_MAIN, '', 'Kittens' ), false
841 ],
842 'Interwiki link has no talk page' => [
843 Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ), false
844 ],
845 ];
846 }
847
848 public function provideIsWatchable() {
849 return [
850 'User page is watchable' => [
851 Title::makeTitle( NS_USER, 'Jane' ), true
852 ],
853 'Talke page is watchable' => [
854 Title::makeTitle( NS_TALK, 'Foo' ), true
855 ],
856 'Special page is not watchable' => [
857 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
858 ],
859 'Virtual namespace is not watchable' => [
860 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
861 ],
862 'Relative link is not watchable' => [
863 Title::makeTitle( NS_MAIN, '', 'Kittens' ), false
864 ],
865 'Interwiki link is not watchable' => [
866 Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ), false
867 ],
868 ];
869 }
870
871 public static function provideGetTalkPage_good() {
872 return [
873 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
874 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
875 ];
876 }
877
878 public static function provideGetTalkPage_bad() {
879 return [
880 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
881 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
882 [ Title::makeTitle( NS_MAIN, '', 'Kittens' ) ],
883 [ Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ) ],
884 ];
885 }
886
887 public static function provideGetSubjectPage_good() {
888 return [
889 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
890 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
891 ];
892 }
893
894 public static function provideGetOtherPage_good() {
895 return [
896 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
897 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
898 ];
899 }
900
901 /**
902 * @dataProvider provideCanHaveTalkPage
903 * @covers Title::canHaveTalkPage
904 *
905 * @param Title $title
906 * @param bool $expected
907 */
908 public function testCanHaveTalkPage( Title $title, $expected ) {
909 $actual = $title->canHaveTalkPage();
910 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
911 }
912
913 /**
914 * @dataProvider provideIsWatchable
915 * @covers Title::isWatchable
916 *
917 * @param Title $title
918 * @param bool $expected
919 */
920 public function testIsWatchable( Title $title, $expected ) {
921 $actual = $title->canHaveTalkPage();
922 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
923 }
924
925 /**
926 * @dataProvider provideGetTalkPage_good
927 * @covers Title::getTalkPageIfDefined
928 */
929 public function testGetTalkPage_good( Title $title, Title $expected ) {
930 $actual = $title->getTalkPage();
931 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
932 }
933
934 /**
935 * @dataProvider provideGetTalkPage_bad
936 * @covers Title::getTalkPageIfDefined
937 */
938 public function testGetTalkPage_bad( Title $title ) {
939 $this->setExpectedException( MWException::class );
940 $title->getTalkPage();
941 }
942
943 /**
944 * @dataProvider provideGetTalkPage_good
945 * @covers Title::getTalkPageIfDefined
946 */
947 public function testGetTalkPageIfDefined_good( Title $title, Title $expected ) {
948 $actual = $title->getTalkPageIfDefined();
949 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
950 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
951 }
952
953 /**
954 * @dataProvider provideGetTalkPage_bad
955 * @covers Title::getTalkPageIfDefined
956 */
957 public function testGetTalkPageIfDefined_bad( Title $title ) {
958 $talk = $title->getTalkPageIfDefined();
959 $this->assertNull(
960 $talk,
961 $title->getPrefixedDBkey()
962 );
963 }
964
965 /**
966 * @dataProvider provideGetSubjectPage_good
967 * @covers Title::getSubjectPage
968 */
969 public function testGetSubjectPage_good( Title $title, Title $expected ) {
970 $actual = $title->getSubjectPage();
971 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
972 }
973
974 /**
975 * @dataProvider provideGetOtherPage_good
976 * @covers Title::getOtherPage
977 */
978 public function testGetOtherPage_good( Title $title, Title $expected ) {
979 $actual = $title->getOtherPage();
980 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
981 }
982
983 /**
984 * @dataProvider provideGetTalkPage_bad
985 * @covers Title::getOtherPage
986 */
987 public function testGetOtherPage_bad( Title $title ) {
988 $this->setExpectedException( MWException::class );
989 $title->getOtherPage();
990 }
991
992 public function provideCreateFragmentTitle() {
993 return [
994 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
995 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
996 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
997 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
998 ];
999 }
1000
1001 /**
1002 * @covers Title::createFragmentTarget
1003 * @dataProvider provideCreateFragmentTitle
1004 */
1005 public function testCreateFragmentTitle( Title $title, $fragment ) {
1006 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1007 'InterwikiLoadPrefix' => [
1008 function ( $prefix, &$iwdata ) {
1009 if ( $prefix === 'interwiki' ) {
1010 $iwdata = [
1011 'iw_url' => 'http://example.com/',
1012 'iw_local' => 0,
1013 'iw_trans' => 0,
1014 ];
1015 return false;
1016 }
1017 },
1018 ],
1019 ] );
1020
1021 $fragmentTitle = $title->createFragmentTarget( $fragment );
1022
1023 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1024 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1025 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1026 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1027 }
1028
1029 public function provideGetPrefixedText() {
1030 return [
1031 // ns = 0
1032 [
1033 Title::makeTitle( NS_MAIN, 'Foo bar' ),
1034 'Foo bar'
1035 ],
1036 // ns = 2
1037 [
1038 Title::makeTitle( NS_USER, 'Foo bar' ),
1039 'User:Foo bar'
1040 ],
1041 // ns = 3
1042 [
1043 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
1044 'User talk:Foo bar'
1045 ],
1046 // fragment not included
1047 [
1048 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
1049 'Foo bar'
1050 ],
1051 // ns = -2
1052 [
1053 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
1054 'Media:Foo bar'
1055 ],
1056 // non-existent namespace
1057 [
1058 Title::makeTitle( 100777, 'Foo bar' ),
1059 'Special:Badtitle/NS100777:Foo bar'
1060 ],
1061 ];
1062 }
1063
1064 /**
1065 * @covers Title::getPrefixedText
1066 * @dataProvider provideGetPrefixedText
1067 */
1068 public function testGetPrefixedText( Title $title, $expected ) {
1069 $this->assertEquals( $expected, $title->getPrefixedText() );
1070 }
1071
1072 public function provideGetPrefixedDBKey() {
1073 return [
1074 // ns = 0
1075 [
1076 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
1077 'Foo_bar'
1078 ],
1079 // ns = 2
1080 [
1081 Title::makeTitle( NS_USER, 'Foo_bar' ),
1082 'User:Foo_bar'
1083 ],
1084 // ns = 3
1085 [
1086 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
1087 'User_talk:Foo_bar'
1088 ],
1089 // fragment not included
1090 [
1091 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
1092 'Foo_bar'
1093 ],
1094 // ns = -2
1095 [
1096 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
1097 'Media:Foo_bar'
1098 ],
1099 // non-existent namespace
1100 [
1101 Title::makeTitle( 100777, 'Foo_bar' ),
1102 'Special:Badtitle/NS100777:Foo_bar'
1103 ],
1104 ];
1105 }
1106
1107 /**
1108 * @covers Title::getPrefixedDBKey
1109 * @dataProvider provideGetPrefixedDBKey
1110 */
1111 public function testGetPrefixedDBKey( Title $title, $expected ) {
1112 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1113 }
1114
1115 /**
1116 * @covers Title::getFragmentForURL
1117 * @dataProvider provideGetFragmentForURL
1118 *
1119 * @param string $titleStr
1120 * @param string $expected
1121 */
1122 public function testGetFragmentForURL( $titleStr, $expected ) {
1123 $this->setMwGlobals( [
1124 'wgFragmentMode' => [ 'html5' ],
1125 'wgExternalInterwikiFragmentMode' => 'legacy',
1126 ] );
1127 $dbw = wfGetDB( DB_MASTER );
1128 $dbw->insert( 'interwiki',
1129 [
1130 [
1131 'iw_prefix' => 'de',
1132 'iw_url' => 'http://de.wikipedia.org/wiki/',
1133 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1134 'iw_wikiid' => 'dewiki',
1135 'iw_local' => 1,
1136 'iw_trans' => 0,
1137 ],
1138 [
1139 'iw_prefix' => 'zz',
1140 'iw_url' => 'http://zzwiki.org/wiki/',
1141 'iw_api' => 'http://zzwiki.org/w/api.php',
1142 'iw_wikiid' => 'zzwiki',
1143 'iw_local' => 0,
1144 'iw_trans' => 0,
1145 ],
1146 ],
1147 __METHOD__,
1148 [ 'IGNORE' ]
1149 );
1150
1151 $title = Title::newFromText( $titleStr );
1152 self::assertEquals( $expected, $title->getFragmentForURL() );
1153
1154 $dbw->delete( 'interwiki', '*', __METHOD__ );
1155 }
1156
1157 public function provideGetFragmentForURL() {
1158 return [
1159 [ 'Foo', '' ],
1160 [ 'Foo#ümlåût', '#ümlåût' ],
1161 [ 'de:Foo#Bå®', '#Bå®' ],
1162 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1163 ];
1164 }
1165
1166 /**
1167 * @covers Title::isRawHtmlMessage
1168 * @dataProvider provideIsRawHtmlMessage
1169 */
1170 public function testIsRawHtmlMessage( $textForm, $expected ) {
1171 $this->setMwGlobals( 'wgRawHtmlMessages', [
1172 'foobar',
1173 'foo_bar',
1174 'foo-bar',
1175 ] );
1176
1177 $title = Title::newFromText( $textForm );
1178 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1179 }
1180
1181 public function provideIsRawHtmlMessage() {
1182 return [
1183 [ 'MediaWiki:Foobar', true ],
1184 [ 'MediaWiki:Foo bar', true ],
1185 [ 'MediaWiki:Foo-bar', true ],
1186 [ 'MediaWiki:foo bar', true ],
1187 [ 'MediaWiki:foo-bar', true ],
1188 [ 'MediaWiki:foobar', true ],
1189 [ 'MediaWiki:some-other-message', false ],
1190 [ 'Main Page', false ],
1191 ];
1192 }
1193
1194 public function provideEquals() {
1195 yield [
1196 Title::newFromText( 'Main Page' ),
1197 Title::newFromText( 'Main Page' ),
1198 true
1199 ];
1200 yield [
1201 Title::newFromText( 'Main Page' ),
1202 Title::newFromText( 'Not The Main Page' ),
1203 false
1204 ];
1205 yield [
1206 Title::newFromText( 'Main Page' ),
1207 Title::newFromText( 'Project:Main Page' ),
1208 false
1209 ];
1210 yield [
1211 Title::newFromText( 'File:Example.png' ),
1212 Title::newFromText( 'Image:Example.png' ),
1213 true
1214 ];
1215 yield [
1216 Title::newFromText( 'Special:Version' ),
1217 Title::newFromText( 'Special:Version' ),
1218 true
1219 ];
1220 yield [
1221 Title::newFromText( 'Special:Version' ),
1222 Title::newFromText( 'Special:Recentchanges' ),
1223 false
1224 ];
1225 yield [
1226 Title::newFromText( 'Special:Version' ),
1227 Title::newFromText( 'Main Page' ),
1228 false
1229 ];
1230 yield [
1231 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1232 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1233 true
1234 ];
1235 yield [
1236 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1237 Title::makeTitle( NS_MAIN, 'Bar', '', '' ),
1238 false
1239 ];
1240 yield [
1241 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1242 Title::makeTitle( NS_TALK, 'Foo', '', '' ),
1243 false
1244 ];
1245 yield [
1246 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1247 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1248 true
1249 ];
1250 yield [
1251 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1252 Title::makeTitle( NS_MAIN, 'Foo', 'Baz', '' ),
1253 true
1254 ];
1255 yield [
1256 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1257 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1258 true
1259 ];
1260 yield [
1261 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1262 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1263 true
1264 ];
1265 yield [
1266 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1267 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1268 false
1269 ];
1270 }
1271
1272 /**
1273 * @covers Title::equals
1274 * @dataProvider provideEquals
1275 */
1276 public function testEquals( Title $firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1277 $this->assertSame(
1278 $expectedSame,
1279 $firstValue->equals( $secondValue )
1280 );
1281 }
1282
1283 /**
1284 * @covers Title::newMainPage
1285 */
1286 public function testNewMainPage() {
1287 $msgCache = TestingAccessWrapper::newFromClass( MessageCache::class );
1288 $msgCache->instance = $this->createMock( MessageCache::class );
1289 $msgCache->instance->method( 'get' )->willReturn( 'Foresheet' );
1290 $msgCache->instance->method( 'transform' )->willReturn( 'Foresheet' );
1291
1292 $this->assertSame(
1293 'Foresheet',
1294 Title::newMainPage()->getText()
1295 );
1296 }
1297
1298 /**
1299 * @covers Title::newMainPage
1300 */
1301 public function testNewMainPageWithLocal() {
1302 $local = $this->createMock( MessageLocalizer::class );
1303 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1304
1305 $this->assertSame(
1306 'Prime Article',
1307 Title::newMainPage( $local )->getText()
1308 );
1309 }
1310 }