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