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