Merge "Improve "selfmove" message's wording"
[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 * @group Database
286 * @param string $source
287 * @param string $target
288 * @param array|string|bool $expected Required error
289 * @dataProvider provideTestIsValidMoveOperation
290 * @covers Title::isValidMoveOperation
291 * @covers Title::validateFileMoveOperation
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 // for Title::validateFileMoveOperation
318 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
319 ];
320 }
321
322 /**
323 * Auth-less test of Title::userCan
324 *
325 * @param array $whitelistRegexp
326 * @param string $source
327 * @param string $action
328 * @param array|string|bool $expected Required error
329 *
330 * @covers Title::checkReadPermissions
331 * @dataProvider dataWgWhitelistReadRegexp
332 */
333 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
334 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
335 // usually have only one regex, it is more concise to write the lonely regex
336 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
337 // type requisite.
338 if ( is_string( $whitelistRegexp ) ) {
339 $whitelistRegexp = [ $whitelistRegexp ];
340 }
341
342 $this->setMwGlobals( [
343 // So User::isEveryoneAllowed( 'read' ) === false
344 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
345 'wgWhitelistRead' => [ 'some random non sense title' ],
346 'wgWhitelistReadRegexp' => $whitelistRegexp,
347 ] );
348
349 $title = Title::newFromDBkey( $source );
350
351 // New anonymous user with no rights
352 $user = new User;
353 $user->mRights = [];
354 $errors = $title->userCan( $action, $user );
355
356 if ( is_bool( $expected ) ) {
357 # Forge the assertion message depending on the assertion expectation
358 $allowableness = $expected
359 ? " should be allowed"
360 : " should NOT be allowed";
361 $this->assertEquals(
362 $expected,
363 $errors,
364 "User action '$action' on [[$source]] $allowableness."
365 );
366 } else {
367 $errors = $this->flattenErrorsArray( $errors );
368 foreach ( (array)$expected as $error ) {
369 $this->assertContains( $error, $errors );
370 }
371 }
372 }
373
374 /**
375 * Provides test parameter values for testWgWhitelistReadRegexp()
376 */
377 public function dataWgWhitelistReadRegexp() {
378 $ALLOWED = true;
379 $DISALLOWED = false;
380
381 return [
382 // Everything, if this doesn't work, we're really in trouble
383 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
384 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
385
386 // We validate against the title name, not the db key
387 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
388 // Main page
389 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
390 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
391 // With spaces
392 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
393 // Unicode multibyte
394 // ...without unicode modifier
395 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
396 // ...with unicode modifier
397 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
398 // Case insensitive
399 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
400 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
401
402 // From DefaultSettings.php:
403 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
404 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
405
406 // With namespaces:
407 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
408 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
409
410 ];
411 }
412
413 public function flattenErrorsArray( $errors ) {
414 $result = [];
415 foreach ( $errors as $error ) {
416 $result[] = $error[0];
417 }
418
419 return $result;
420 }
421
422 /**
423 * @dataProvider provideGetPageViewLanguage
424 * @covers Title::getPageViewLanguage
425 */
426 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
427 $lang, $variant, $msg = ''
428 ) {
429 // Setup environnement for this test
430 $this->setMwGlobals( [
431 'wgDefaultLanguageVariant' => $variant,
432 'wgAllowUserJs' => true,
433 ] );
434 $this->setUserLang( $lang );
435 $this->setContentLang( $contLang );
436
437 $title = Title::newFromText( $titleText );
438 $this->assertInstanceOf( 'Title', $title,
439 "Test must be passed a valid title text, you gave '$titleText'"
440 );
441 $this->assertEquals( $expected,
442 $title->getPageViewLanguage()->getCode(),
443 $msg
444 );
445 }
446
447 public static function provideGetPageViewLanguage() {
448 # Format:
449 # - expected
450 # - Title name
451 # - wgContLang (expected in most case)
452 # - wgLang (on some specific pages)
453 # - wgDefaultLanguageVariant
454 # - Optional message
455 return [
456 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
457 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
458 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
459
460 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
461 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
462 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
468
469 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
470 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
479
480 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
481 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
482
483 ];
484 }
485
486 /**
487 * @dataProvider provideBaseTitleCases
488 * @covers Title::getBaseText
489 */
490 public function testGetBaseText( $title, $expected, $msg = '' ) {
491 $title = Title::newFromText( $title );
492 $this->assertEquals( $expected,
493 $title->getBaseText(),
494 $msg
495 );
496 }
497
498 public static function provideBaseTitleCases() {
499 return [
500 # Title, expected base, optional message
501 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
502 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
503 ];
504 }
505
506 /**
507 * @dataProvider provideRootTitleCases
508 * @covers Title::getRootText
509 */
510 public function testGetRootText( $title, $expected, $msg = '' ) {
511 $title = Title::newFromText( $title );
512 $this->assertEquals( $expected,
513 $title->getRootText(),
514 $msg
515 );
516 }
517
518 public static function provideRootTitleCases() {
519 return [
520 # Title, expected base, optional message
521 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
522 [ 'User:Foo/Bar/Baz', 'Foo' ],
523 ];
524 }
525
526 /**
527 * @todo Handle $wgNamespacesWithSubpages cases
528 * @dataProvider provideSubpageTitleCases
529 * @covers Title::getSubpageText
530 */
531 public function testGetSubpageText( $title, $expected, $msg = '' ) {
532 $title = Title::newFromText( $title );
533 $this->assertEquals( $expected,
534 $title->getSubpageText(),
535 $msg
536 );
537 }
538
539 public static function provideSubpageTitleCases() {
540 return [
541 # Title, expected base, optional message
542 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
543 [ 'User:John_Doe/subOne', 'subOne' ],
544 ];
545 }
546
547 public static function provideNewFromTitleValue() {
548 return [
549 [ new TitleValue( NS_MAIN, 'Foo' ) ],
550 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
551 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
552 ];
553 }
554
555 /**
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 * @dataProvider provideGetTitleValue
577 */
578 public function testGetTitleValue( $text ) {
579 $title = Title::newFromText( $text );
580 $value = $title->getTitleValue();
581
582 $dbkey = str_replace( ' ', '_', $value->getText() );
583 $this->assertEquals( $title->getDBkey(), $dbkey );
584 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
585 $this->assertEquals( $title->getFragment(), $value->getFragment() );
586 }
587
588 public static function provideGetFragment() {
589 return [
590 [ 'Foo', '' ],
591 [ 'Foo#bar', 'bar' ],
592 [ 'Foo#bär', 'bär' ],
593
594 // Inner whitespace is normalized
595 [ 'Foo#bar_bar', 'bar bar' ],
596 [ 'Foo#bar bar', 'bar bar' ],
597 [ 'Foo#bar bar', 'bar bar' ],
598
599 // Leading whitespace is kept, trailing whitespace is trimmed.
600 // XXX: Is this really want we want?
601 [ 'Foo#_bar_bar_', ' bar bar' ],
602 [ 'Foo# bar bar ', ' bar bar' ],
603 ];
604 }
605
606 /**
607 * @dataProvider provideGetFragment
608 *
609 * @param string $full
610 * @param string $fragment
611 */
612 public function testGetFragment( $full, $fragment ) {
613 $title = Title::newFromText( $full );
614 $this->assertEquals( $fragment, $title->getFragment() );
615 }
616
617 /**
618 * @covers Title::isAlwaysKnown
619 * @dataProvider provideIsAlwaysKnown
620 * @param string $page
621 * @param bool $isKnown
622 */
623 public function testIsAlwaysKnown( $page, $isKnown ) {
624 $title = Title::newFromText( $page );
625 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
626 }
627
628 public static function provideIsAlwaysKnown() {
629 return [
630 [ 'Some nonexistent page', false ],
631 [ 'UTPage', false ],
632 [ '#test', true ],
633 [ 'Special:BlankPage', true ],
634 [ 'Special:SomeNonexistentSpecialPage', false ],
635 [ 'MediaWiki:Parentheses', true ],
636 [ 'MediaWiki:Some nonexistent message', false ],
637 ];
638 }
639
640 /**
641 * @covers Title::isValid
642 * @dataProvider provideIsValid
643 * @param Title $title
644 * @param bool $isValid
645 */
646 public function testIsValid( Title $title, $isValid ) {
647 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
648 }
649
650 public static function provideIsValid() {
651 return [
652 [ Title::makeTitle( NS_MAIN, '' ), false ],
653 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
654 [ Title::makeTitle( NS_MAIN, '|' ), false ],
655 [ Title::makeTitle( NS_MAIN, '#' ), false ],
656 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
657 [ Title::makeTitle( -33, 'Test' ), false ],
658 [ Title::makeTitle( 77663399, 'Test' ), false ],
659 ];
660 }
661
662 /**
663 * @covers Title::isAlwaysKnown
664 */
665 public function testIsAlwaysKnownOnInterwiki() {
666 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
667 $this->assertTrue( $title->isAlwaysKnown() );
668 }
669
670 /**
671 * @covers Title::exists
672 */
673 public function testExists() {
674 $title = Title::makeTitle( NS_PROJECT, 'New page' );
675 $linkCache = LinkCache::singleton();
676
677 $article = new Article( $title );
678 $page = $article->getPage();
679 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
680
681 // Tell Title it doesn't know whether it exists
682 $title->mArticleID = -1;
683
684 // Tell the link cache it doesn't exists when it really does
685 $linkCache->clearLink( $title );
686 $linkCache->addBadLinkObj( $title );
687
688 $this->assertEquals(
689 false,
690 $title->exists(),
691 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
692 );
693 $this->assertEquals(
694 true,
695 $title->exists( Title::GAID_FOR_UPDATE ),
696 'exists() should re-query database when GAID_FOR_UPDATE is used'
697 );
698 }
699
700 public function provideCanHaveTalkPage() {
701 return [
702 'User page has talk page' => [
703 Title::makeTitle( NS_USER, 'Jane' ), true
704 ],
705 'Talke page has talk page' => [
706 Title::makeTitle( NS_TALK, 'Foo' ), true
707 ],
708 'Special page cannot have talk page' => [
709 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
710 ],
711 'Virtual namespace cannot have talk page' => [
712 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
713 ],
714 ];
715 }
716
717 /**
718 * @dataProvider provideCanHaveTalkPage
719 * @covers Title::canHaveTalkPage
720 *
721 * @param Title $title
722 * @param bool $expected
723 */
724 public function testCanHaveTalkPage( Title $title, $expected ) {
725 $actual = $title->canHaveTalkPage();
726 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
727 }
728
729 /**
730 * @dataProvider provideCanHaveTalkPage
731 * @covers Title::canTalk
732 *
733 * @param Title $title
734 * @param bool $expected
735 */
736 public function testCanTalk( Title $title, $expected ) {
737 $actual = $title->canTalk();
738 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
739 }
740
741 public static function provideGetTalkPage_good() {
742 return [
743 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
744 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
745 ];
746 }
747
748 /**
749 * @dataProvider provideGetTalkPage_good
750 * @covers Title::getTalkPage
751 */
752 public function testGetTalkPage_good( Title $title, Title $expected ) {
753 $talk = $title->getTalkPage();
754 $this->assertSame(
755 $expected->getPrefixedDBKey(),
756 $talk->getPrefixedDBKey(),
757 $title->getPrefixedDBKey()
758 );
759 }
760
761 /**
762 * @dataProvider provideGetTalkPage_good
763 * @covers Title::getTalkPageIfDefined
764 */
765 public function testGetTalkPageIfDefined_good( Title $title ) {
766 $talk = $title->getTalkPageIfDefined();
767 $this->assertInstanceOf(
768 Title::class,
769 $talk,
770 $title->getPrefixedDBKey()
771 );
772 }
773
774 public static function provideGetTalkPage_bad() {
775 return [
776 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
777 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
778 ];
779 }
780
781 /**
782 * @dataProvider provideGetTalkPage_bad
783 * @covers Title::getTalkPageIfDefined
784 */
785 public function testGetTalkPageIfDefined_bad( Title $title ) {
786 $talk = $title->getTalkPageIfDefined();
787 $this->assertNull(
788 $talk,
789 $title->getPrefixedDBKey()
790 );
791 }
792
793 public function provideCreateFragmentTitle() {
794 return [
795 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
796 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
797 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
798 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
799 ];
800 }
801
802 /**
803 * @covers Title::createFragmentTarget
804 * @dataProvider provideCreateFragmentTitle
805 */
806 public function testCreateFragmentTitle( Title $title, $fragment ) {
807 $this->mergeMwGlobalArrayValue( 'wgHooks', [
808 'InterwikiLoadPrefix' => [
809 function ( $prefix, &$iwdata ) {
810 if ( $prefix === 'interwiki' ) {
811 $iwdata = [
812 'iw_url' => 'http://example.com/',
813 'iw_local' => 0,
814 'iw_trans' => 0,
815 ];
816 return false;
817 }
818 },
819 ],
820 ] );
821
822 $fragmentTitle = $title->createFragmentTarget( $fragment );
823
824 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
825 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
826 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
827 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
828 }
829
830 public function provideGetPrefixedText() {
831 return [
832 // ns = 0
833 [
834 Title::makeTitle( NS_MAIN, 'Foo bar' ),
835 'Foo bar'
836 ],
837 // ns = 2
838 [
839 Title::makeTitle( NS_USER, 'Foo bar' ),
840 'User:Foo bar'
841 ],
842 // ns = 3
843 [
844 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
845 'User talk:Foo bar'
846 ],
847 // fragment not included
848 [
849 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
850 'Foo bar'
851 ],
852 // ns = -2
853 [
854 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
855 'Media:Foo bar'
856 ],
857 // non-existent namespace
858 [
859 Title::makeTitle( 100777, 'Foo bar' ),
860 'Special:Badtitle/NS100777:Foo bar'
861 ],
862 ];
863 }
864
865 /**
866 * @covers Title::getPrefixedText
867 * @dataProvider provideGetPrefixedText
868 */
869 public function testGetPrefixedText( Title $title, $expected ) {
870 $this->assertEquals( $expected, $title->getPrefixedText() );
871 }
872
873 public function provideGetPrefixedDBKey() {
874 return [
875 // ns = 0
876 [
877 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
878 'Foo_bar'
879 ],
880 // ns = 2
881 [
882 Title::makeTitle( NS_USER, 'Foo_bar' ),
883 'User:Foo_bar'
884 ],
885 // ns = 3
886 [
887 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
888 'User_talk:Foo_bar'
889 ],
890 // fragment not included
891 [
892 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
893 'Foo_bar'
894 ],
895 // ns = -2
896 [
897 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
898 'Media:Foo_bar'
899 ],
900 // non-existent namespace
901 [
902 Title::makeTitle( 100777, 'Foo_bar' ),
903 'Special:Badtitle/NS100777:Foo_bar'
904 ],
905 ];
906 }
907
908 /**
909 * @covers Title::getPrefixedDBKey
910 * @dataProvider provideGetPrefixedDBKey
911 */
912 public function testGetPrefixedDBKey( Title $title, $expected ) {
913 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
914 }
915 }