Merge "Add Parser::resetOutput() and getStripState()"
[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 global $wgMultiContentRevisionSchemaMigrationStage;
300
301 $this->hideDeprecated( 'Title::isValidMoveOperation' );
302
303 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD ) {
304 // We can only set this to false with the old schema
305 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
306 }
307
308 $title = Title::newFromText( $source );
309 $nt = Title::newFromText( $target );
310 $errors = $title->isValidMoveOperation( $nt, false );
311 if ( $expected === true ) {
312 $this->assertTrue( $errors );
313 } else {
314 $errors = $this->flattenErrorsArray( $errors );
315 foreach ( (array)$expected as $error ) {
316 $this->assertContains( $error, $errors );
317 }
318 }
319 }
320
321 public static function provideTestIsValidMoveOperation() {
322 global $wgMultiContentRevisionSchemaMigrationStage;
323 $ret = [
324 // for Title::isValidMoveOperation
325 [ 'Some page', '', 'badtitletext' ],
326 [ 'Test', 'Test', 'selfmove' ],
327 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
328 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
329 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
330 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
331 ];
332 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD ) {
333 // The error can only occur if $wgContentHandlerUseDB is false, which doesn't work with
334 // the new schema, so omit the test in that case
335 array_push( $ret,
336 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ]
337 );
338 }
339 return $ret;
340 }
341
342 /**
343 * Auth-less test of Title::userCan
344 *
345 * @param array $whitelistRegexp
346 * @param string $source
347 * @param string $action
348 * @param array|string|bool $expected Required error
349 *
350 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
351 * @dataProvider dataWgWhitelistReadRegexp
352 */
353 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
354 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
355 // usually have only one regex, it is more concise to write the lonely regex
356 // as a string. Thus we cast to a [] to honor $wgWhitelistReadRegexp
357 // type requisite.
358 if ( is_string( $whitelistRegexp ) ) {
359 $whitelistRegexp = [ $whitelistRegexp ];
360 }
361
362 $this->setMwGlobals( [
363 // So User::isEveryoneAllowed( 'read' ) === false
364 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
365 'wgWhitelistRead' => [ 'some random non sense title' ],
366 'wgWhitelistReadRegexp' => $whitelistRegexp,
367 ] );
368
369 $title = Title::newFromDBkey( $source );
370
371 // New anonymous user with no rights
372 $user = new User;
373 $this->overrideUserPermissions( $user, [] );
374 $errors = $title->userCan( $action, $user );
375
376 if ( is_bool( $expected ) ) {
377 # Forge the assertion message depending on the assertion expectation
378 $allowableness = $expected
379 ? " should be allowed"
380 : " should NOT be allowed";
381 $this->assertEquals(
382 $expected,
383 $errors,
384 "User action '$action' on [[$source]] $allowableness."
385 );
386 } else {
387 $errors = $this->flattenErrorsArray( $errors );
388 foreach ( (array)$expected as $error ) {
389 $this->assertContains( $error, $errors );
390 }
391 }
392 }
393
394 /**
395 * Provides test parameter values for testWgWhitelistReadRegexp()
396 */
397 public function dataWgWhitelistReadRegexp() {
398 $ALLOWED = true;
399 $DISALLOWED = false;
400
401 return [
402 // Everything, if this doesn't work, we're really in trouble
403 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
404 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
405
406 // We validate against the title name, not the db key
407 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
408 // Main page
409 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
410 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
411 // With spaces
412 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
413 // Unicode multibyte
414 // ...without unicode modifier
415 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
416 // ...with unicode modifier
417 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
418 // Case insensitive
419 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
420 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
421
422 // From DefaultSettings.php:
423 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
424 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
425
426 // With namespaces:
427 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
428 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
429
430 ];
431 }
432
433 public function flattenErrorsArray( $errors ) {
434 $result = [];
435 foreach ( $errors as $error ) {
436 $result[] = $error[0];
437 }
438
439 return $result;
440 }
441
442 /**
443 * @dataProvider provideGetPageViewLanguage
444 * @covers Title::getPageViewLanguage
445 */
446 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
447 $lang, $variant, $msg = ''
448 ) {
449 // Setup environnement for this test
450 $this->setMwGlobals( [
451 'wgDefaultLanguageVariant' => $variant,
452 'wgAllowUserJs' => true,
453 ] );
454 $this->setUserLang( $lang );
455 $this->setContentLang( $contLang );
456
457 $title = Title::newFromText( $titleText );
458 $this->assertInstanceOf( Title::class, $title,
459 "Test must be passed a valid title text, you gave '$titleText'"
460 );
461 $this->assertEquals( $expected,
462 $title->getPageViewLanguage()->getCode(),
463 $msg
464 );
465 }
466
467 public static function provideGetPageViewLanguage() {
468 # Format:
469 # - expected
470 # - Title name
471 # - content language (expected in most cases)
472 # - wgLang (on some specific pages)
473 # - wgDefaultLanguageVariant
474 # - Optional message
475 return [
476 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
477 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
478 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
479
480 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
481 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
482 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
483 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
484 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
485 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
486 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
487 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
488
489 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
490 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
491 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
492 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
493 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
494 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
495 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
496 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
497 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
498 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
499
500 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
501 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
502
503 ];
504 }
505
506 /**
507 * @dataProvider provideBaseTitleCases
508 * @covers Title::getBaseText
509 */
510 public function testGetBaseText( $title, $expected, $msg = '' ) {
511 $title = Title::newFromText( $title );
512 $this->assertSame( $expected,
513 $title->getBaseText(),
514 $msg
515 );
516 }
517
518 /**
519 * @dataProvider provideBaseTitleCases
520 * @covers Title::getBaseTitle
521 */
522 public function testGetBaseTitle( $title, $expected, $msg = '' ) {
523 $title = Title::newFromText( $title );
524 $base = $title->getBaseTitle();
525 $this->assertTrue( $base->isValid(), $msg );
526 $this->assertTrue(
527 $base->equals( Title::makeTitleSafe( $title->getNamespace(), $expected ) ),
528 $msg
529 );
530 }
531
532 public static function provideBaseTitleCases() {
533 return [
534 # Title, expected base, optional message
535 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
536 [ 'User:Foo / Bar / Baz', 'Foo / Bar ' ],
537 ];
538 }
539
540 /**
541 * @dataProvider provideRootTitleCases
542 * @covers Title::getRootText
543 */
544 public function testGetRootText( $title, $expected, $msg = '' ) {
545 $title = Title::newFromText( $title );
546 $this->assertEquals( $expected,
547 $title->getRootText(),
548 $msg
549 );
550 }
551
552 /**
553 * @dataProvider provideRootTitleCases
554 * @covers Title::getRootTitle
555 */
556 public function testGetRootTitle( $title, $expected, $msg = '' ) {
557 $title = Title::newFromText( $title );
558 $root = $title->getRootTitle();
559 $this->assertTrue( $root->isValid(), $msg );
560 $this->assertTrue(
561 $root->equals( Title::makeTitleSafe( $title->getNamespace(), $expected ) ),
562 $msg
563 );
564 }
565
566 public static function provideRootTitleCases() {
567 return [
568 # Title, expected base, optional message
569 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
570 [ 'User:Foo / Bar / Baz', 'Foo ' ],
571 [ 'Talk:////', '////' ],
572 [ 'Template:////', '////' ],
573 [ 'Template:Foo////', 'Foo' ],
574 [ 'Template:Foo////Bar', 'Foo' ],
575 ];
576 }
577
578 /**
579 * @todo Handle $wgNamespacesWithSubpages cases
580 * @dataProvider provideSubpageTitleCases
581 * @covers Title::getSubpageText
582 */
583 public function testGetSubpageText( $title, $expected, $msg = '' ) {
584 $title = Title::newFromText( $title );
585 $this->assertEquals( $expected,
586 $title->getSubpageText(),
587 $msg
588 );
589 }
590
591 public static function provideSubpageTitleCases() {
592 return [
593 # Title, expected base, optional message
594 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
595 [ 'User:John_Doe/subOne', 'subOne' ],
596 ];
597 }
598
599 public function provideSubpage() {
600 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
601 return [
602 [ 'Foo', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
603 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
604 [ 'User:Foo', 'x', new TitleValue( NS_USER, 'Foo/x' ) ],
605 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN, 'User:Foo/x', '', 'wiki' ) ],
606 ];
607 }
608
609 /**
610 * @dataProvider provideSubpage
611 * @covers Title::getSubpage
612 */
613 public function testSubpage( $title, $sub, LinkTarget $expected ) {
614 $interwikiLookup = $this->getMock( InterwikiLookup::class );
615 $interwikiLookup->expects( $this->any() )
616 ->method( 'isValidInterwiki' )
617 ->willReturnCallback(
618 function ( $prefix ) {
619 return $prefix == 'wiki';
620 }
621 );
622
623 $this->setService( 'InterwikiLookup', $interwikiLookup );
624
625 $title = Title::newFromText( $title );
626 $expected = Title::newFromLinkTarget( $expected );
627 $actual = $title->getSubpage( $sub );
628
629 // NOTE: convert to string for comparison
630 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
631 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
632 }
633
634 public static function provideNewFromTitleValue() {
635 return [
636 [ new TitleValue( NS_MAIN, 'Foo' ) ],
637 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
638 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
639 ];
640 }
641
642 /**
643 * @covers Title::newFromTitleValue
644 * @dataProvider provideNewFromTitleValue
645 */
646 public function testNewFromTitleValue( TitleValue $value ) {
647 $title = Title::newFromTitleValue( $value );
648
649 $dbkey = str_replace( ' ', '_', $value->getText() );
650 $this->assertEquals( $dbkey, $title->getDBkey() );
651 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
652 $this->assertEquals( $value->getFragment(), $title->getFragment() );
653 }
654
655 /**
656 * @covers Title::newFromLinkTarget
657 * @dataProvider provideNewFromTitleValue
658 */
659 public function testNewFromLinkTarget( LinkTarget $value ) {
660 $title = Title::newFromLinkTarget( $value );
661
662 $dbkey = str_replace( ' ', '_', $value->getText() );
663 $this->assertEquals( $dbkey, $title->getDBkey() );
664 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
665 $this->assertEquals( $value->getFragment(), $title->getFragment() );
666 }
667
668 /**
669 * @covers Title::newFromLinkTarget
670 */
671 public function testNewFromLinkTarget_clone() {
672 $title = Title::newFromText( __METHOD__ );
673 $this->assertSame( $title, Title::newFromLinkTarget( $title ) );
674
675 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
676 $clone = Title::newFromLinkTarget( $title, Title::NEW_CLONE );
677 $this->assertNotSame( $title, $clone );
678 $this->assertTrue( $clone->equals( $title ) );
679 }
680
681 public function provideCastFromLinkTarget() {
682 return array_merge( [ [ null ] ], self::provideNewFromTitleValue() );
683 }
684
685 /**
686 * @covers Title::castFromLinkTarget
687 * @dataProvider provideCastFromLinkTarget
688 */
689 public function testCastFromLinkTarget( $value ) {
690 $title = Title::castFromLinkTarget( $value );
691
692 if ( $value === null ) {
693 $this->assertNull( $title );
694 } else {
695 $dbkey = str_replace( ' ', '_', $value->getText() );
696 $this->assertSame( $dbkey, $title->getDBkey() );
697 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
698 $this->assertSame( $value->getFragment(), $title->getFragment() );
699 }
700 }
701
702 public static function provideGetTitleValue() {
703 return [
704 [ 'Foo' ],
705 [ 'Foo#bar' ],
706 [ 'User:Hansi_Maier' ],
707 ];
708 }
709
710 /**
711 * @covers Title::getTitleValue
712 * @dataProvider provideGetTitleValue
713 */
714 public function testGetTitleValue( $text ) {
715 $title = Title::newFromText( $text );
716 $value = $title->getTitleValue();
717
718 $dbkey = str_replace( ' ', '_', $value->getText() );
719 $this->assertEquals( $title->getDBkey(), $dbkey );
720 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
721 $this->assertEquals( $title->getFragment(), $value->getFragment() );
722 }
723
724 public static function provideGetFragment() {
725 return [
726 [ 'Foo', '' ],
727 [ 'Foo#bar', 'bar' ],
728 [ 'Foo#bär', 'bär' ],
729
730 // Inner whitespace is normalized
731 [ 'Foo#bar_bar', 'bar bar' ],
732 [ 'Foo#bar bar', 'bar bar' ],
733 [ 'Foo#bar bar', 'bar bar' ],
734
735 // Leading whitespace is kept, trailing whitespace is trimmed.
736 // XXX: Is this really want we want?
737 [ 'Foo#_bar_bar_', ' bar bar' ],
738 [ 'Foo# bar bar ', ' bar bar' ],
739 ];
740 }
741
742 /**
743 * @covers Title::getFragment
744 * @dataProvider provideGetFragment
745 *
746 * @param string $full
747 * @param string $fragment
748 */
749 public function testGetFragment( $full, $fragment ) {
750 $title = Title::newFromText( $full );
751 $this->assertEquals( $fragment, $title->getFragment() );
752 }
753
754 /**
755 * @covers Title::isAlwaysKnown
756 * @dataProvider provideIsAlwaysKnown
757 * @param string $page
758 * @param bool $isKnown
759 */
760 public function testIsAlwaysKnown( $page, $isKnown ) {
761 $title = Title::newFromText( $page );
762 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
763 }
764
765 public static function provideIsAlwaysKnown() {
766 return [
767 [ 'Some nonexistent page', false ],
768 [ 'UTPage', false ],
769 [ '#test', true ],
770 [ 'Special:BlankPage', true ],
771 [ 'Special:SomeNonexistentSpecialPage', false ],
772 [ 'MediaWiki:Parentheses', true ],
773 [ 'MediaWiki:Some nonexistent message', false ],
774 ];
775 }
776
777 /**
778 * @covers Title::isValid
779 * @dataProvider provideIsValid
780 * @param Title $title
781 * @param bool $isValid
782 */
783 public function testIsValid( Title $title, $isValid ) {
784 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
785 }
786
787 public static function provideIsValid() {
788 return [
789 [ Title::makeTitle( NS_MAIN, '' ), false ],
790 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
791 [ Title::makeTitle( NS_MAIN, '|' ), false ],
792 [ Title::makeTitle( NS_MAIN, '#' ), false ],
793 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
794 [ Title::makeTitle( NS_MAIN, ' Test' ), false ],
795 [ Title::makeTitle( NS_MAIN, '_Test' ), false ],
796 [ Title::makeTitle( NS_MAIN, 'Test ' ), false ],
797 [ Title::makeTitle( NS_MAIN, 'Test_' ), false ],
798 [ Title::makeTitle( NS_MAIN, "Test\nthis" ), false ],
799 [ Title::makeTitle( NS_MAIN, "Test\tthis" ), false ],
800 [ Title::makeTitle( -33, 'Test' ), false ],
801 [ Title::makeTitle( 77663399, 'Test' ), false ],
802 ];
803 }
804
805 /**
806 * @covers Title::isAlwaysKnown
807 */
808 public function testIsAlwaysKnownOnInterwiki() {
809 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
810 $this->assertTrue( $title->isAlwaysKnown() );
811 }
812
813 /**
814 * @covers Title::exists
815 */
816 public function testExists() {
817 $title = Title::makeTitle( NS_PROJECT, 'New page' );
818 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
819
820 $article = new Article( $title );
821 $page = $article->getPage();
822 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
823
824 // Tell Title it doesn't know whether it exists
825 $title->mArticleID = -1;
826
827 // Tell the link cache it doesn't exist when it really does
828 $linkCache->clearLink( $title );
829 $linkCache->addBadLinkObj( $title );
830
831 $this->assertEquals(
832 false,
833 $title->exists(),
834 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
835 );
836 $this->assertEquals(
837 true,
838 $title->exists( Title::GAID_FOR_UPDATE ),
839 'exists() should re-query database when GAID_FOR_UPDATE is used'
840 );
841 }
842
843 public function provideCanHaveTalkPage() {
844 return [
845 'User page has talk page' => [
846 Title::makeTitle( NS_USER, 'Jane' ), true
847 ],
848 'Talke page has talk page' => [
849 Title::makeTitle( NS_TALK, 'Foo' ), true
850 ],
851 'Special page cannot have talk page' => [
852 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
853 ],
854 'Virtual namespace cannot have talk page' => [
855 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
856 ],
857 'Relative link has no talk page' => [
858 Title::makeTitle( NS_MAIN, '', 'Kittens' ), false
859 ],
860 'Interwiki link has no talk page' => [
861 Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ), false
862 ],
863 ];
864 }
865
866 public function provideIsWatchable() {
867 return [
868 'User page is watchable' => [
869 Title::makeTitle( NS_USER, 'Jane' ), true
870 ],
871 'Talke page is watchable' => [
872 Title::makeTitle( NS_TALK, 'Foo' ), true
873 ],
874 'Special page is not watchable' => [
875 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
876 ],
877 'Virtual namespace is not watchable' => [
878 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
879 ],
880 'Relative link is not watchable' => [
881 Title::makeTitle( NS_MAIN, '', 'Kittens' ), false
882 ],
883 'Interwiki link is not watchable' => [
884 Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ), false
885 ],
886 ];
887 }
888
889 public static function provideGetTalkPage_good() {
890 return [
891 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
892 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
893 ];
894 }
895
896 public static function provideGetTalkPage_bad() {
897 return [
898 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
899 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
900 [ Title::makeTitle( NS_MAIN, '', 'Kittens' ) ],
901 [ Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ) ],
902 ];
903 }
904
905 public static function provideGetSubjectPage_good() {
906 return [
907 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
908 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
909 ];
910 }
911
912 public static function provideGetOtherPage_good() {
913 return [
914 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
915 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
916 ];
917 }
918
919 /**
920 * @dataProvider provideCanHaveTalkPage
921 * @covers Title::canHaveTalkPage
922 *
923 * @param Title $title
924 * @param bool $expected
925 */
926 public function testCanHaveTalkPage( Title $title, $expected ) {
927 $actual = $title->canHaveTalkPage();
928 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
929 }
930
931 /**
932 * @dataProvider provideIsWatchable
933 * @covers Title::isWatchable
934 *
935 * @param Title $title
936 * @param bool $expected
937 */
938 public function testIsWatchable( Title $title, $expected ) {
939 $actual = $title->canHaveTalkPage();
940 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
941 }
942
943 /**
944 * @dataProvider provideGetTalkPage_good
945 * @covers Title::getTalkPageIfDefined
946 */
947 public function testGetTalkPage_good( Title $title, Title $expected ) {
948 $actual = $title->getTalkPage();
949 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
950 }
951
952 /**
953 * @dataProvider provideGetTalkPage_bad
954 * @covers Title::getTalkPageIfDefined
955 */
956 public function testGetTalkPage_bad( Title $title ) {
957 $this->setExpectedException( MWException::class );
958 $title->getTalkPage();
959 }
960
961 /**
962 * @dataProvider provideGetTalkPage_good
963 * @covers Title::getTalkPageIfDefined
964 */
965 public function testGetTalkPageIfDefined_good( Title $title, Title $expected ) {
966 $actual = $title->getTalkPageIfDefined();
967 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
968 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
969 }
970
971 /**
972 * @dataProvider provideGetTalkPage_bad
973 * @covers Title::getTalkPageIfDefined
974 */
975 public function testGetTalkPageIfDefined_bad( Title $title ) {
976 $talk = $title->getTalkPageIfDefined();
977 $this->assertNull(
978 $talk,
979 $title->getPrefixedDBkey()
980 );
981 }
982
983 /**
984 * @dataProvider provideGetSubjectPage_good
985 * @covers Title::getSubjectPage
986 */
987 public function testGetSubjectPage_good( Title $title, Title $expected ) {
988 $actual = $title->getSubjectPage();
989 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
990 }
991
992 /**
993 * @dataProvider provideGetOtherPage_good
994 * @covers Title::getOtherPage
995 */
996 public function testGetOtherPage_good( Title $title, Title $expected ) {
997 $actual = $title->getOtherPage();
998 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
999 }
1000
1001 /**
1002 * @dataProvider provideGetTalkPage_bad
1003 * @covers Title::getOtherPage
1004 */
1005 public function testGetOtherPage_bad( Title $title ) {
1006 $this->setExpectedException( MWException::class );
1007 $title->getOtherPage();
1008 }
1009
1010 public function provideCreateFragmentTitle() {
1011 return [
1012 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
1013 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
1014 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
1015 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
1016 ];
1017 }
1018
1019 /**
1020 * @covers Title::createFragmentTarget
1021 * @dataProvider provideCreateFragmentTitle
1022 */
1023 public function testCreateFragmentTitle( Title $title, $fragment ) {
1024 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1025 'InterwikiLoadPrefix' => [
1026 function ( $prefix, &$iwdata ) {
1027 if ( $prefix === 'interwiki' ) {
1028 $iwdata = [
1029 'iw_url' => 'http://example.com/',
1030 'iw_local' => 0,
1031 'iw_trans' => 0,
1032 ];
1033 return false;
1034 }
1035 },
1036 ],
1037 ] );
1038
1039 $fragmentTitle = $title->createFragmentTarget( $fragment );
1040
1041 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1042 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1043 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1044 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1045 }
1046
1047 public function provideGetPrefixedText() {
1048 return [
1049 // ns = 0
1050 [
1051 Title::makeTitle( NS_MAIN, 'Foo bar' ),
1052 'Foo bar'
1053 ],
1054 // ns = 2
1055 [
1056 Title::makeTitle( NS_USER, 'Foo bar' ),
1057 'User:Foo bar'
1058 ],
1059 // ns = 3
1060 [
1061 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
1062 'User talk:Foo bar'
1063 ],
1064 // fragment not included
1065 [
1066 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
1067 'Foo bar'
1068 ],
1069 // ns = -2
1070 [
1071 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
1072 'Media:Foo bar'
1073 ],
1074 // non-existent namespace
1075 [
1076 Title::makeTitle( 100777, 'Foo bar' ),
1077 'Special:Badtitle/NS100777:Foo bar'
1078 ],
1079 ];
1080 }
1081
1082 /**
1083 * @covers Title::getPrefixedText
1084 * @dataProvider provideGetPrefixedText
1085 */
1086 public function testGetPrefixedText( Title $title, $expected ) {
1087 $this->assertEquals( $expected, $title->getPrefixedText() );
1088 }
1089
1090 public function provideGetPrefixedDBKey() {
1091 return [
1092 // ns = 0
1093 [
1094 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
1095 'Foo_bar'
1096 ],
1097 // ns = 2
1098 [
1099 Title::makeTitle( NS_USER, 'Foo_bar' ),
1100 'User:Foo_bar'
1101 ],
1102 // ns = 3
1103 [
1104 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
1105 'User_talk:Foo_bar'
1106 ],
1107 // fragment not included
1108 [
1109 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
1110 'Foo_bar'
1111 ],
1112 // ns = -2
1113 [
1114 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
1115 'Media:Foo_bar'
1116 ],
1117 // non-existent namespace
1118 [
1119 Title::makeTitle( 100777, 'Foo_bar' ),
1120 'Special:Badtitle/NS100777:Foo_bar'
1121 ],
1122 ];
1123 }
1124
1125 /**
1126 * @covers Title::getPrefixedDBKey
1127 * @dataProvider provideGetPrefixedDBKey
1128 */
1129 public function testGetPrefixedDBKey( Title $title, $expected ) {
1130 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1131 }
1132
1133 /**
1134 * @covers Title::getFragmentForURL
1135 * @dataProvider provideGetFragmentForURL
1136 *
1137 * @param string $titleStr
1138 * @param string $expected
1139 */
1140 public function testGetFragmentForURL( $titleStr, $expected ) {
1141 $this->setMwGlobals( [
1142 'wgFragmentMode' => [ 'html5' ],
1143 'wgExternalInterwikiFragmentMode' => 'legacy',
1144 ] );
1145 $dbw = wfGetDB( DB_MASTER );
1146 $dbw->insert( 'interwiki',
1147 [
1148 [
1149 'iw_prefix' => 'de',
1150 'iw_url' => 'http://de.wikipedia.org/wiki/',
1151 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1152 'iw_wikiid' => 'dewiki',
1153 'iw_local' => 1,
1154 'iw_trans' => 0,
1155 ],
1156 [
1157 'iw_prefix' => 'zz',
1158 'iw_url' => 'http://zzwiki.org/wiki/',
1159 'iw_api' => 'http://zzwiki.org/w/api.php',
1160 'iw_wikiid' => 'zzwiki',
1161 'iw_local' => 0,
1162 'iw_trans' => 0,
1163 ],
1164 ],
1165 __METHOD__,
1166 [ 'IGNORE' ]
1167 );
1168
1169 $title = Title::newFromText( $titleStr );
1170 self::assertEquals( $expected, $title->getFragmentForURL() );
1171
1172 $dbw->delete( 'interwiki', '*', __METHOD__ );
1173 }
1174
1175 public function provideGetFragmentForURL() {
1176 return [
1177 [ 'Foo', '' ],
1178 [ 'Foo#ümlåût', '#ümlåût' ],
1179 [ 'de:Foo#Bå®', '#Bå®' ],
1180 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1181 ];
1182 }
1183
1184 /**
1185 * @covers Title::isRawHtmlMessage
1186 * @dataProvider provideIsRawHtmlMessage
1187 */
1188 public function testIsRawHtmlMessage( $textForm, $expected ) {
1189 $this->setMwGlobals( 'wgRawHtmlMessages', [
1190 'foobar',
1191 'foo_bar',
1192 'foo-bar',
1193 ] );
1194
1195 $title = Title::newFromText( $textForm );
1196 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1197 }
1198
1199 public function provideIsRawHtmlMessage() {
1200 return [
1201 [ 'MediaWiki:Foobar', true ],
1202 [ 'MediaWiki:Foo bar', true ],
1203 [ 'MediaWiki:Foo-bar', true ],
1204 [ 'MediaWiki:foo bar', true ],
1205 [ 'MediaWiki:foo-bar', true ],
1206 [ 'MediaWiki:foobar', true ],
1207 [ 'MediaWiki:some-other-message', false ],
1208 [ 'Main Page', false ],
1209 ];
1210 }
1211
1212 public function provideEquals() {
1213 yield [
1214 Title::newFromText( 'Main Page' ),
1215 Title::newFromText( 'Main Page' ),
1216 true
1217 ];
1218 yield [
1219 Title::newFromText( 'Main Page' ),
1220 Title::newFromText( 'Not The Main Page' ),
1221 false
1222 ];
1223 yield [
1224 Title::newFromText( 'Main Page' ),
1225 Title::newFromText( 'Project:Main Page' ),
1226 false
1227 ];
1228 yield [
1229 Title::newFromText( 'File:Example.png' ),
1230 Title::newFromText( 'Image:Example.png' ),
1231 true
1232 ];
1233 yield [
1234 Title::newFromText( 'Special:Version' ),
1235 Title::newFromText( 'Special:Version' ),
1236 true
1237 ];
1238 yield [
1239 Title::newFromText( 'Special:Version' ),
1240 Title::newFromText( 'Special:Recentchanges' ),
1241 false
1242 ];
1243 yield [
1244 Title::newFromText( 'Special:Version' ),
1245 Title::newFromText( 'Main Page' ),
1246 false
1247 ];
1248 yield [
1249 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1250 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1251 true
1252 ];
1253 yield [
1254 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1255 Title::makeTitle( NS_MAIN, 'Bar', '', '' ),
1256 false
1257 ];
1258 yield [
1259 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1260 Title::makeTitle( NS_TALK, 'Foo', '', '' ),
1261 false
1262 ];
1263 yield [
1264 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1265 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1266 true
1267 ];
1268 yield [
1269 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1270 Title::makeTitle( NS_MAIN, 'Foo', 'Baz', '' ),
1271 true
1272 ];
1273 yield [
1274 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1275 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1276 true
1277 ];
1278 yield [
1279 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1280 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1281 true
1282 ];
1283 yield [
1284 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1285 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1286 false
1287 ];
1288 }
1289
1290 /**
1291 * @covers Title::equals
1292 * @dataProvider provideEquals
1293 */
1294 public function testEquals( Title $firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1295 $this->assertSame(
1296 $expectedSame,
1297 $firstValue->equals( $secondValue )
1298 );
1299 }
1300
1301 /**
1302 * @covers Title::newMainPage
1303 */
1304 public function testNewMainPage() {
1305 $msgCache = TestingAccessWrapper::newFromClass( MessageCache::class );
1306 $msgCache->instance = $this->createMock( MessageCache::class );
1307 $msgCache->instance->method( 'get' )->willReturn( 'Foresheet' );
1308 $msgCache->instance->method( 'transform' )->willReturn( 'Foresheet' );
1309
1310 $this->assertSame(
1311 'Foresheet',
1312 Title::newMainPage()->getText()
1313 );
1314 }
1315
1316 /**
1317 * @covers Title::newMainPage
1318 */
1319 public function testNewMainPageWithLocal() {
1320 $local = $this->createMock( MessageLocalizer::class );
1321 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1322
1323 $this->assertSame(
1324 'Prime Article',
1325 Title::newMainPage( $local )->getText()
1326 );
1327 }
1328 }