Merge "Title: Use a more proper way of detecting whether interwikis are local"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Title
6 */
7 class TitleTest extends MediaWikiTestCase {
8 protected function setUp() {
9 parent::setUp();
10
11 $this->setMwGlobals( [
12 'wgAllowUserJs' => false,
13 'wgDefaultLanguageVariant' => false,
14 'wgMetaNamespace' => 'Project',
15 ] );
16 $this->setUserLang( 'en' );
17 $this->setContentLang( 'en' );
18 }
19
20 /**
21 * @covers Title::legalChars
22 */
23 public function testLegalChars() {
24 $titlechars = Title::legalChars();
25
26 foreach ( range( 1, 255 ) as $num ) {
27 $chr = chr( $num );
28 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
29 $this->assertFalse(
30 (bool)preg_match( "/[$titlechars]/", $chr ),
31 "chr($num) = $chr is not a valid titlechar"
32 );
33 } else {
34 $this->assertTrue(
35 (bool)preg_match( "/[$titlechars]/", $chr ),
36 "chr($num) = $chr is a valid titlechar"
37 );
38 }
39 }
40 }
41
42 public static function provideValidSecureAndSplit() {
43 return [
44 [ 'Sandbox' ],
45 [ 'A "B"' ],
46 [ 'A \'B\'' ],
47 [ '.com' ],
48 [ '~' ],
49 [ '#' ],
50 [ '"' ],
51 [ '\'' ],
52 [ 'Talk:Sandbox' ],
53 [ 'Talk:Foo:Sandbox' ],
54 [ 'File:Example.svg' ],
55 [ 'File_talk:Example.svg' ],
56 [ 'Foo/.../Sandbox' ],
57 [ 'Sandbox/...' ],
58 [ 'A~~' ],
59 [ ':A' ],
60 // Length is 256 total, but only title part matters
61 [ 'Category:' . str_repeat( 'x', 248 ) ],
62 [ str_repeat( 'x', 252 ) ],
63 // interwiki prefix
64 [ 'localtestiw: #anchor' ],
65 [ 'localtestiw:' ],
66 [ 'localtestiw:foo' ],
67 [ 'localtestiw: foo # anchor' ],
68 [ 'localtestiw: Talk: Sandbox # anchor' ],
69 [ 'remotetestiw:' ],
70 [ 'remotetestiw: Talk: # anchor' ],
71 [ 'remotetestiw: #bar' ],
72 [ 'remotetestiw: Talk:' ],
73 [ 'remotetestiw: Talk: Foo' ],
74 [ 'localtestiw:remotetestiw:' ],
75 [ 'localtestiw:remotetestiw:foo' ]
76 ];
77 }
78
79 public static function provideInvalidSecureAndSplit() {
80 return [
81 [ '', 'title-invalid-empty' ],
82 [ ':', 'title-invalid-empty' ],
83 [ '__ __', 'title-invalid-empty' ],
84 [ ' __ ', 'title-invalid-empty' ],
85 // Bad characters forbidden regardless of wgLegalTitleChars
86 [ 'A [ B', 'title-invalid-characters' ],
87 [ 'A ] B', 'title-invalid-characters' ],
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 \t B", 'title-invalid-characters' ],
94 [ "A \n B", 'title-invalid-characters' ],
95 // URL encoding
96 [ 'A%20B', 'title-invalid-characters' ],
97 [ 'A%23B', 'title-invalid-characters' ],
98 [ 'A%2523B', 'title-invalid-characters' ],
99 // XML/HTML character entity references
100 // Note: Commented out because they are not marked invalid by the PHP test as
101 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
102 // 'A &eacute; B',
103 // 'A &#233; B',
104 // 'A &#x00E9; B',
105 // Subject of NS_TALK does not roundtrip to NS_MAIN
106 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
107 // Directory navigation
108 [ '.', 'title-invalid-relative' ],
109 [ '..', 'title-invalid-relative' ],
110 [ './Sandbox', 'title-invalid-relative' ],
111 [ '../Sandbox', 'title-invalid-relative' ],
112 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
113 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
114 [ 'Sandbox/.', 'title-invalid-relative' ],
115 [ 'Sandbox/..', 'title-invalid-relative' ],
116 // Tilde
117 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
118 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
119 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
120 // Length
121 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
122 // Namespace prefix without actual title
123 [ 'Talk:', 'title-invalid-empty' ],
124 [ 'Talk:#', 'title-invalid-empty' ],
125 [ 'Category: ', 'title-invalid-empty' ],
126 [ 'Category: #bar', 'title-invalid-empty' ],
127 // interwiki prefix
128 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
129 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
130 ];
131 }
132
133 private function secureAndSplitGlobals() {
134 $this->setMwGlobals( [
135 'wgLocalInterwikis' => [ 'localtestiw' ],
136 'wgHooks' => [
137 'InterwikiLoadPrefix' => [
138 function ( $prefix, &$data ) {
139 if ( $prefix === 'localtestiw' ) {
140 $data = [ 'iw_url' => 'localtestiw' ];
141 } elseif ( $prefix === 'remotetestiw' ) {
142 $data = [ 'iw_url' => 'remotetestiw' ];
143 }
144 return false;
145 }
146 ]
147 ]
148 ] );
149
150 // Reset TitleParser since we modified $wgLocalInterwikis
151 $this->setService( 'TitleParser', new MediaWikiTitleCodec(
152 Language::factory( 'en' ),
153 new GenderCache(),
154 [ 'localtestiw' ]
155 ) );
156 }
157
158 /**
159 * See also mediawiki.Title.test.js
160 * @covers Title::secureAndSplit
161 * @dataProvider provideValidSecureAndSplit
162 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
163 */
164 public function testSecureAndSplitValid( $text ) {
165 $this->secureAndSplitGlobals();
166 $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" );
167 }
168
169 /**
170 * See also mediawiki.Title.test.js
171 * @covers Title::secureAndSplit
172 * @dataProvider provideInvalidSecureAndSplit
173 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
174 */
175 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
176 $this->secureAndSplitGlobals();
177 try {
178 Title::newFromTextThrow( $text ); // should throw
179 $this->assertTrue( false, "Invalid: $text" );
180 } catch ( MalformedTitleException $ex ) {
181 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
182 }
183 }
184
185 public static function provideConvertByteClassToUnicodeClass() {
186 return [
187 [
188 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
189 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
190 ],
191 [
192 'QWERTYf-\\xFF+',
193 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
194 ],
195 [
196 'QWERTY\\x66-\\xFD+',
197 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
198 ],
199 [
200 'QWERTYf-y+',
201 'QWERTYf-y+',
202 ],
203 [
204 'QWERTYf-\\x80+',
205 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
206 ],
207 [
208 'QWERTY\\x66-\\x80+\\x23',
209 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
210 ],
211 [
212 'QWERTY\\x66-\\x80+\\xD3',
213 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
214 ],
215 [
216 '\\\\\\x99',
217 '\\\\\\u0080-\\uFFFF',
218 ],
219 [
220 '-\\x99',
221 '\\-\\u0080-\\uFFFF',
222 ],
223 [
224 'QWERTY\\-\\x99',
225 'QWERTY\\-\\u0080-\\uFFFF',
226 ],
227 [
228 '\\\\x99',
229 '\\\\x99',
230 ],
231 [
232 'A-\\x9F',
233 'A-\\x7F\\u0080-\\uFFFF',
234 ],
235 [
236 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
237 'f-wQWERTYFXZ\\u0080-\\uFFFF',
238 ],
239 [
240 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
241 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
242 ],
243 ];
244 }
245
246 /**
247 * @dataProvider provideConvertByteClassToUnicodeClass
248 * @covers Title::convertByteClassToUnicodeClass
249 */
250 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
251 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
252 }
253
254 /**
255 * @dataProvider provideSpecialNamesWithAndWithoutParameter
256 * @covers Title::fixSpecialName
257 */
258 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
259 $title = Title::newFromText( $text );
260 $fixed = $title->fixSpecialName();
261 $stuff = explode( '/', $fixed->getDBkey(), 2 );
262 if ( count( $stuff ) == 2 ) {
263 $par = $stuff[1];
264 } else {
265 $par = null;
266 }
267 $this->assertEquals(
268 $expectedParam,
269 $par,
270 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
271 );
272 }
273
274 public static function provideSpecialNamesWithAndWithoutParameter() {
275 return [
276 [ 'Special:Version', null ],
277 [ 'Special:Version/', '' ],
278 [ 'Special:Version/param', 'param' ],
279 ];
280 }
281
282 /**
283 * Auth-less test of Title::isValidMoveOperation
284 *
285 * @param string $source
286 * @param string $target
287 * @param array|string|bool $expected Required error
288 * @dataProvider provideTestIsValidMoveOperation
289 * @covers Title::isValidMoveOperation
290 * @covers Title::validateFileMoveOperation
291 */
292 public function testIsValidMoveOperation( $source, $target, $expected ) {
293 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
294 $title = Title::newFromText( $source );
295 $nt = Title::newFromText( $target );
296 $errors = $title->isValidMoveOperation( $nt, false );
297 if ( $expected === true ) {
298 $this->assertTrue( $errors );
299 } else {
300 $errors = $this->flattenErrorsArray( $errors );
301 foreach ( (array)$expected as $error ) {
302 $this->assertContains( $error, $errors );
303 }
304 }
305 }
306
307 public static function provideTestIsValidMoveOperation() {
308 return [
309 // for Title::isValidMoveOperation
310 [ 'Some page', '', 'badtitletext' ],
311 [ 'Test', 'Test', 'selfmove' ],
312 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
313 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
314 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
315 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
316 // for Title::validateFileMoveOperation
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', $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 # - wgContLang (expected in most case)
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 * @dataProvider provideNewFromTitleValue
556 */
557 public function testNewFromTitleValue( TitleValue $value ) {
558 $title = Title::newFromTitleValue( $value );
559
560 $dbkey = str_replace( ' ', '_', $value->getText() );
561 $this->assertEquals( $dbkey, $title->getDBkey() );
562 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
563 $this->assertEquals( $value->getFragment(), $title->getFragment() );
564 }
565
566 public static function provideGetTitleValue() {
567 return [
568 [ 'Foo' ],
569 [ 'Foo#bar' ],
570 [ 'User:Hansi_Maier' ],
571 ];
572 }
573
574 /**
575 * @dataProvider provideGetTitleValue
576 */
577 public function testGetTitleValue( $text ) {
578 $title = Title::newFromText( $text );
579 $value = $title->getTitleValue();
580
581 $dbkey = str_replace( ' ', '_', $value->getText() );
582 $this->assertEquals( $title->getDBkey(), $dbkey );
583 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
584 $this->assertEquals( $title->getFragment(), $value->getFragment() );
585 }
586
587 public static function provideGetFragment() {
588 return [
589 [ 'Foo', '' ],
590 [ 'Foo#bar', 'bar' ],
591 [ 'Foo#bär', 'bär' ],
592
593 // Inner whitespace is normalized
594 [ 'Foo#bar_bar', 'bar bar' ],
595 [ 'Foo#bar bar', 'bar bar' ],
596 [ 'Foo#bar bar', 'bar bar' ],
597
598 // Leading whitespace is kept, trailing whitespace is trimmed.
599 // XXX: Is this really want we want?
600 [ 'Foo#_bar_bar_', ' bar bar' ],
601 [ 'Foo# bar bar ', ' bar bar' ],
602 ];
603 }
604
605 /**
606 * @dataProvider provideGetFragment
607 *
608 * @param string $full
609 * @param string $fragment
610 */
611 public function testGetFragment( $full, $fragment ) {
612 $title = Title::newFromText( $full );
613 $this->assertEquals( $fragment, $title->getFragment() );
614 }
615
616 /**
617 * @covers Title::isAlwaysKnown
618 * @dataProvider provideIsAlwaysKnown
619 * @param string $page
620 * @param bool $isKnown
621 */
622 public function testIsAlwaysKnown( $page, $isKnown ) {
623 $title = Title::newFromText( $page );
624 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
625 }
626
627 public static function provideIsAlwaysKnown() {
628 return [
629 [ 'Some nonexistent page', false ],
630 [ 'UTPage', false ],
631 [ '#test', true ],
632 [ 'Special:BlankPage', true ],
633 [ 'Special:SomeNonexistentSpecialPage', false ],
634 [ 'MediaWiki:Parentheses', true ],
635 [ 'MediaWiki:Some nonexistent message', false ],
636 ];
637 }
638
639 /**
640 * @covers Title::isValid
641 * @dataProvider provideIsValid
642 * @param Title $title
643 * @param bool $isValid
644 */
645 public function testIsValid( Title $title, $isValid ) {
646 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
647 }
648
649 public static function provideIsValid() {
650 return [
651 [ Title::makeTitle( NS_MAIN, '' ), false ],
652 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
653 [ Title::makeTitle( NS_MAIN, '|' ), false ],
654 [ Title::makeTitle( NS_MAIN, '#' ), false ],
655 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
656 [ Title::makeTitle( -33, 'Test' ), false ],
657 [ Title::makeTitle( 77663399, 'Test' ), false ],
658 ];
659 }
660
661 /**
662 * @covers Title::isAlwaysKnown
663 */
664 public function testIsAlwaysKnownOnInterwiki() {
665 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
666 $this->assertTrue( $title->isAlwaysKnown() );
667 }
668
669 /**
670 * @covers Title::exists
671 */
672 public function testExists() {
673 $title = Title::makeTitle( NS_PROJECT, 'New page' );
674 $linkCache = LinkCache::singleton();
675
676 $article = new Article( $title );
677 $page = $article->getPage();
678 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
679
680 // Tell Title it doesn't know whether it exists
681 $title->mArticleID = -1;
682
683 // Tell the link cache it doesn't exists when it really does
684 $linkCache->clearLink( $title );
685 $linkCache->addBadLinkObj( $title );
686
687 $this->assertEquals(
688 false,
689 $title->exists(),
690 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
691 );
692 $this->assertEquals(
693 true,
694 $title->exists( Title::GAID_FOR_UPDATE ),
695 'exists() should re-query database when GAID_FOR_UPDATE is used'
696 );
697 }
698
699 public function provideCanHaveTalkPage() {
700 return [
701 'User page has talk page' => [
702 Title::makeTitle( NS_USER, 'Jane' ), true
703 ],
704 'Talke page has talk page' => [
705 Title::makeTitle( NS_TALK, 'Foo' ), true
706 ],
707 'Special page cannot have talk page' => [
708 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
709 ],
710 'Virtual namespace cannot have talk page' => [
711 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
712 ],
713 ];
714 }
715
716 /**
717 * @dataProvider provideCanHaveTalkPage
718 * @covers Title::canHaveTalkPage
719 *
720 * @param Title $title
721 * @param bool $expected
722 */
723 public function testCanHaveTalkPage( Title $title, $expected ) {
724 $actual = $title->canHaveTalkPage();
725 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
726 }
727
728 /**
729 * @dataProvider provideCanHaveTalkPage
730 * @covers Title::canTalk
731 *
732 * @param Title $title
733 * @param bool $expected
734 */
735 public function testCanTalk( Title $title, $expected ) {
736 $actual = $title->canTalk();
737 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
738 }
739
740 public static function provideGetTalkPage_good() {
741 return [
742 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
743 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
744 ];
745 }
746
747 /**
748 * @dataProvider provideGetTalkPage_good
749 * @covers Title::getTalkPage
750 */
751 public function testGetTalkPage_good( Title $title, Title $expected ) {
752 $talk = $title->getTalkPage();
753 $this->assertSame(
754 $expected->getPrefixedDBKey(),
755 $talk->getPrefixedDBKey(),
756 $title->getPrefixedDBKey()
757 );
758 }
759
760 /**
761 * @dataProvider provideGetTalkPage_good
762 * @covers Title::getTalkPageIfDefined
763 */
764 public function testGetTalkPageIfDefined_good( Title $title ) {
765 $talk = $title->getTalkPageIfDefined();
766 $this->assertInstanceOf(
767 Title::class,
768 $talk,
769 $title->getPrefixedDBKey()
770 );
771 }
772
773 public static function provideGetTalkPage_bad() {
774 return [
775 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
776 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
777 ];
778 }
779
780 /**
781 * @dataProvider provideGetTalkPage_bad
782 * @covers Title::getTalkPageIfDefined
783 */
784 public function testGetTalkPageIfDefined_bad( Title $title ) {
785 $talk = $title->getTalkPageIfDefined();
786 $this->assertNull(
787 $talk,
788 $title->getPrefixedDBKey()
789 );
790 }
791
792 public function provideCreateFragmentTitle() {
793 return [
794 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
795 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
796 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
797 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
798 ];
799 }
800
801 /**
802 * @covers Title::createFragmentTarget
803 * @dataProvider provideCreateFragmentTitle
804 */
805 public function testCreateFragmentTitle( Title $title, $fragment ) {
806 $this->mergeMwGlobalArrayValue( 'wgHooks', [
807 'InterwikiLoadPrefix' => [
808 function ( $prefix, &$iwdata ) {
809 if ( $prefix === 'interwiki' ) {
810 $iwdata = [
811 'iw_url' => 'http://example.com/',
812 'iw_local' => 0,
813 'iw_trans' => 0,
814 ];
815 return false;
816 }
817 },
818 ],
819 ] );
820
821 $fragmentTitle = $title->createFragmentTarget( $fragment );
822
823 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
824 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
825 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
826 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
827 }
828
829 public function provideGetPrefixedText() {
830 return [
831 // ns = 0
832 [
833 Title::makeTitle( NS_MAIN, 'Foo bar' ),
834 'Foo bar'
835 ],
836 // ns = 2
837 [
838 Title::makeTitle( NS_USER, 'Foo bar' ),
839 'User:Foo bar'
840 ],
841 // ns = 3
842 [
843 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
844 'User talk:Foo bar'
845 ],
846 // fragment not included
847 [
848 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
849 'Foo bar'
850 ],
851 // ns = -2
852 [
853 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
854 'Media:Foo bar'
855 ],
856 // non-existent namespace
857 [
858 Title::makeTitle( 100777, 'Foo bar' ),
859 'Special:Badtitle/NS100777:Foo bar'
860 ],
861 ];
862 }
863
864 /**
865 * @covers Title::getPrefixedText
866 * @dataProvider provideGetPrefixedText
867 */
868 public function testGetPrefixedText( Title $title, $expected ) {
869 $this->assertEquals( $expected, $title->getPrefixedText() );
870 }
871
872 public function provideGetPrefixedDBKey() {
873 return [
874 // ns = 0
875 [
876 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
877 'Foo_bar'
878 ],
879 // ns = 2
880 [
881 Title::makeTitle( NS_USER, 'Foo_bar' ),
882 'User:Foo_bar'
883 ],
884 // ns = 3
885 [
886 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
887 'User_talk:Foo_bar'
888 ],
889 // fragment not included
890 [
891 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
892 'Foo_bar'
893 ],
894 // ns = -2
895 [
896 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
897 'Media:Foo_bar'
898 ],
899 // non-existent namespace
900 [
901 Title::makeTitle( 100777, 'Foo_bar' ),
902 'Special:Badtitle/NS100777:Foo_bar'
903 ],
904 ];
905 }
906
907 /**
908 * @covers Title::getPrefixedDBKey
909 * @dataProvider provideGetPrefixedDBKey
910 */
911 public function testGetPrefixedDBKey( Title $title, $expected ) {
912 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
913 }
914
915 /**
916 * @dataProvider provideGetFragmentForURL
917 *
918 * @param string $titleStr
919 * @param string $expected
920 */
921 public function testGetFragmentForURL( $titleStr, $expected ) {
922 $this->setMwGlobals( [
923 'wgFragmentMode' => [ 'html5' ],
924 'wgExternalInterwikiFragmentMode' => 'legacy',
925 ] );
926 $dbw = wfGetDB( DB_MASTER );
927 $dbw->insert( 'interwiki',
928 [
929 [
930 'iw_prefix' => 'de',
931 'iw_url' => 'http://de.wikipedia.org/wiki/',
932 'iw_api' => 'http://de.wikipedia.org/w/api.php',
933 'iw_wikiid' => 'dewiki',
934 'iw_local' => 1,
935 'iw_trans' => 0,
936 ],
937 [
938 'iw_prefix' => 'zz',
939 'iw_url' => 'http://zzwiki.org/wiki/',
940 'iw_api' => 'http://zzwiki.org/w/api.php',
941 'iw_wikiid' => 'zzwiki',
942 'iw_local' => 0,
943 'iw_trans' => 0,
944 ],
945 ],
946 __METHOD__,
947 [ 'IGNORE' ]
948 );
949
950 $title = Title::newFromText( $titleStr );
951 self::assertEquals( $expected, $title->getFragmentForURL() );
952
953 $dbw->delete( 'interwiki', '*', __METHOD__ );
954 }
955
956 public function provideGetFragmentForURL() {
957 return [
958 [ 'Foo', '' ],
959 [ 'Foo#ümlåût', '#ümlåût' ],
960 [ 'de:Foo#Bå®', '#Bå®' ],
961 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
962 ];
963 }
964 }