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