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