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