Merge "Reset scoped session for upload jobs after deferred updates"
[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 'wgLanguageCode' => 'en',
13 'wgContLang' => Language::factory( 'en' ),
14 // User language
15 'wgLang' => Language::factory( 'en' ),
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
18 'wgMetaNamespace' => 'Project',
19 ] );
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 // 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
151 /**
152 * See also mediawiki.Title.test.js
153 * @covers Title::secureAndSplit
154 * @dataProvider provideValidSecureAndSplit
155 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
156 */
157 public function testSecureAndSplitValid( $text ) {
158 $this->secureAndSplitGlobals();
159 $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" );
160 }
161
162 /**
163 * See also mediawiki.Title.test.js
164 * @covers Title::secureAndSplit
165 * @dataProvider provideInvalidSecureAndSplit
166 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
167 */
168 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
169 $this->secureAndSplitGlobals();
170 try {
171 Title::newFromTextThrow( $text ); // should throw
172 $this->assertTrue( false, "Invalid: $text" );
173 } catch ( MalformedTitleException $ex ) {
174 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
175 }
176 }
177
178 public static function provideConvertByteClassToUnicodeClass() {
179 return [
180 [
181 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
182 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
183 ],
184 [
185 'QWERTYf-\\xFF+',
186 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
187 ],
188 [
189 'QWERTY\\x66-\\xFD+',
190 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
191 ],
192 [
193 'QWERTYf-y+',
194 'QWERTYf-y+',
195 ],
196 [
197 'QWERTYf-\\x80+',
198 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
199 ],
200 [
201 'QWERTY\\x66-\\x80+\\x23',
202 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
203 ],
204 [
205 'QWERTY\\x66-\\x80+\\xD3',
206 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
207 ],
208 [
209 '\\\\\\x99',
210 '\\\\\\u0080-\\uFFFF',
211 ],
212 [
213 '-\\x99',
214 '\\-\\u0080-\\uFFFF',
215 ],
216 [
217 'QWERTY\\-\\x99',
218 'QWERTY\\-\\u0080-\\uFFFF',
219 ],
220 [
221 '\\\\x99',
222 '\\\\x99',
223 ],
224 [
225 'A-\\x9F',
226 'A-\\x7F\\u0080-\\uFFFF',
227 ],
228 [
229 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
230 'f-wQWERTYFXZ\\u0080-\\uFFFF',
231 ],
232 [
233 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
234 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
235 ],
236 ];
237 }
238
239 /**
240 * @dataProvider provideConvertByteClassToUnicodeClass
241 * @covers Title::convertByteClassToUnicodeClass
242 */
243 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
244 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
245 }
246
247 /**
248 * @dataProvider provideSpecialNamesWithAndWithoutParameter
249 * @covers Title::fixSpecialName
250 */
251 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
252 $title = Title::newFromText( $text );
253 $fixed = $title->fixSpecialName();
254 $stuff = explode( '/', $fixed->getDBkey(), 2 );
255 if ( count( $stuff ) == 2 ) {
256 $par = $stuff[1];
257 } else {
258 $par = null;
259 }
260 $this->assertEquals(
261 $expectedParam,
262 $par,
263 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
264 );
265 }
266
267 public static function provideSpecialNamesWithAndWithoutParameter() {
268 return [
269 [ 'Special:Version', null ],
270 [ 'Special:Version/', '' ],
271 [ 'Special:Version/param', 'param' ],
272 ];
273 }
274
275 /**
276 * Auth-less test of Title::isValidMoveOperation
277 *
278 * @group Database
279 * @param string $source
280 * @param string $target
281 * @param array|string|bool $expected Required error
282 * @dataProvider provideTestIsValidMoveOperation
283 * @covers Title::isValidMoveOperation
284 * @covers Title::validateFileMoveOperation
285 */
286 public function testIsValidMoveOperation( $source, $target, $expected ) {
287 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
288 $title = Title::newFromText( $source );
289 $nt = Title::newFromText( $target );
290 $errors = $title->isValidMoveOperation( $nt, false );
291 if ( $expected === true ) {
292 $this->assertTrue( $errors );
293 } else {
294 $errors = $this->flattenErrorsArray( $errors );
295 foreach ( (array)$expected as $error ) {
296 $this->assertContains( $error, $errors );
297 }
298 }
299 }
300
301 public static function provideTestIsValidMoveOperation() {
302 return [
303 // for Title::isValidMoveOperation
304 [ 'Some page', '', 'badtitletext' ],
305 [ 'Test', 'Test', 'selfmove' ],
306 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
307 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
308 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
309 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
310 // for Title::validateFileMoveOperation
311 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
312 ];
313 }
314
315 /**
316 * Auth-less test of Title::userCan
317 *
318 * @param array $whitelistRegexp
319 * @param string $source
320 * @param string $action
321 * @param array|string|bool $expected Required error
322 *
323 * @covers Title::checkReadPermissions
324 * @dataProvider dataWgWhitelistReadRegexp
325 */
326 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
327 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
328 // usually have only one regex, it is more concise to write the lonely regex
329 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
330 // type requisite.
331 if ( is_string( $whitelistRegexp ) ) {
332 $whitelistRegexp = [ $whitelistRegexp ];
333 }
334
335 $this->setMwGlobals( [
336 // So User::isEveryoneAllowed( 'read' ) === false
337 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
338 'wgWhitelistRead' => [ 'some random non sense title' ],
339 'wgWhitelistReadRegexp' => $whitelistRegexp,
340 ] );
341
342 $title = Title::newFromDBkey( $source );
343
344 // New anonymous user with no rights
345 $user = new User;
346 $user->mRights = [];
347 $errors = $title->userCan( $action, $user );
348
349 if ( is_bool( $expected ) ) {
350 # Forge the assertion message depending on the assertion expectation
351 $allowableness = $expected
352 ? " should be allowed"
353 : " should NOT be allowed";
354 $this->assertEquals(
355 $expected,
356 $errors,
357 "User action '$action' on [[$source]] $allowableness."
358 );
359 } else {
360 $errors = $this->flattenErrorsArray( $errors );
361 foreach ( (array)$expected as $error ) {
362 $this->assertContains( $error, $errors );
363 }
364 }
365 }
366
367 /**
368 * Provides test parameter values for testWgWhitelistReadRegexp()
369 */
370 public function dataWgWhitelistReadRegexp() {
371 $ALLOWED = true;
372 $DISALLOWED = false;
373
374 return [
375 // Everything, if this doesn't work, we're really in trouble
376 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
377 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
378
379 // We validate against the title name, not the db key
380 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
381 // Main page
382 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
383 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
384 // With spaces
385 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
386 // Unicode multibyte
387 // ...without unicode modifier
388 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
389 // ...with unicode modifier
390 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
391 // Case insensitive
392 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
393 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
394
395 // From DefaultSettings.php:
396 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
397 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
398
399 // With namespaces:
400 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
401 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
402
403 ];
404 }
405
406 public function flattenErrorsArray( $errors ) {
407 $result = [];
408 foreach ( $errors as $error ) {
409 $result[] = $error[0];
410 }
411
412 return $result;
413 }
414
415 /**
416 * @dataProvider provideGetPageViewLanguage
417 * @covers Title::getPageViewLanguage
418 */
419 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
420 $lang, $variant, $msg = ''
421 ) {
422 // Setup environnement for this test
423 $this->setMwGlobals( [
424 'wgLanguageCode' => $contLang,
425 'wgContLang' => Language::factory( $contLang ),
426 'wgLang' => Language::factory( $lang ),
427 'wgDefaultLanguageVariant' => $variant,
428 'wgAllowUserJs' => true,
429 ] );
430
431 $title = Title::newFromText( $titleText );
432 $this->assertInstanceOf( 'Title', $title,
433 "Test must be passed a valid title text, you gave '$titleText'"
434 );
435 $this->assertEquals( $expected,
436 $title->getPageViewLanguage()->getCode(),
437 $msg
438 );
439 }
440
441 public static function provideGetPageViewLanguage() {
442 # Format:
443 # - expected
444 # - Title name
445 # - wgContLang (expected in most case)
446 # - wgLang (on some specific pages)
447 # - wgDefaultLanguageVariant
448 # - Optional message
449 return [
450 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
451 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
452 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
453
454 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
455 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
456 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
457 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
458 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
459 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
460 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
461 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
462
463 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
464 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
465 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
466 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
467 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
468 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
469 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
470 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
473
474 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
475 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
476
477 ];
478 }
479
480 /**
481 * @dataProvider provideBaseTitleCases
482 * @covers Title::getBaseText
483 */
484 public function testGetBaseText( $title, $expected, $msg = '' ) {
485 $title = Title::newFromText( $title );
486 $this->assertEquals( $expected,
487 $title->getBaseText(),
488 $msg
489 );
490 }
491
492 public static function provideBaseTitleCases() {
493 return [
494 # Title, expected base, optional message
495 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
496 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
497 ];
498 }
499
500 /**
501 * @dataProvider provideRootTitleCases
502 * @covers Title::getRootText
503 */
504 public function testGetRootText( $title, $expected, $msg = '' ) {
505 $title = Title::newFromText( $title );
506 $this->assertEquals( $expected,
507 $title->getRootText(),
508 $msg
509 );
510 }
511
512 public static function provideRootTitleCases() {
513 return [
514 # Title, expected base, optional message
515 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
516 [ 'User:Foo/Bar/Baz', 'Foo' ],
517 ];
518 }
519
520 /**
521 * @todo Handle $wgNamespacesWithSubpages cases
522 * @dataProvider provideSubpageTitleCases
523 * @covers Title::getSubpageText
524 */
525 public function testGetSubpageText( $title, $expected, $msg = '' ) {
526 $title = Title::newFromText( $title );
527 $this->assertEquals( $expected,
528 $title->getSubpageText(),
529 $msg
530 );
531 }
532
533 public static function provideSubpageTitleCases() {
534 return [
535 # Title, expected base, optional message
536 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
537 [ 'User:John_Doe/subOne', 'subOne' ],
538 ];
539 }
540
541 public static function provideNewFromTitleValue() {
542 return [
543 [ new TitleValue( NS_MAIN, 'Foo' ) ],
544 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
545 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
546 ];
547 }
548
549 /**
550 * @dataProvider provideNewFromTitleValue
551 */
552 public function testNewFromTitleValue( TitleValue $value ) {
553 $title = Title::newFromTitleValue( $value );
554
555 $dbkey = str_replace( ' ', '_', $value->getText() );
556 $this->assertEquals( $dbkey, $title->getDBkey() );
557 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
558 $this->assertEquals( $value->getFragment(), $title->getFragment() );
559 }
560
561 public static function provideGetTitleValue() {
562 return [
563 [ 'Foo' ],
564 [ 'Foo#bar' ],
565 [ 'User:Hansi_Maier' ],
566 ];
567 }
568
569 /**
570 * @dataProvider provideGetTitleValue
571 */
572 public function testGetTitleValue( $text ) {
573 $title = Title::newFromText( $text );
574 $value = $title->getTitleValue();
575
576 $dbkey = str_replace( ' ', '_', $value->getText() );
577 $this->assertEquals( $title->getDBkey(), $dbkey );
578 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
579 $this->assertEquals( $title->getFragment(), $value->getFragment() );
580 }
581
582 public static function provideGetFragment() {
583 return [
584 [ 'Foo', '' ],
585 [ 'Foo#bar', 'bar' ],
586 [ 'Foo#bär', 'bär' ],
587
588 // Inner whitespace is normalized
589 [ 'Foo#bar_bar', 'bar bar' ],
590 [ 'Foo#bar bar', 'bar bar' ],
591 [ 'Foo#bar bar', 'bar bar' ],
592
593 // Leading whitespace is kept, trailing whitespace is trimmed.
594 // XXX: Is this really want we want?
595 [ 'Foo#_bar_bar_', ' bar bar' ],
596 [ 'Foo# bar bar ', ' bar bar' ],
597 ];
598 }
599
600 /**
601 * @dataProvider provideGetFragment
602 *
603 * @param string $full
604 * @param string $fragment
605 */
606 public function testGetFragment( $full, $fragment ) {
607 $title = Title::newFromText( $full );
608 $this->assertEquals( $fragment, $title->getFragment() );
609 }
610
611 /**
612 * @covers Title::isAlwaysKnown
613 * @dataProvider provideIsAlwaysKnown
614 * @param string $page
615 * @param bool $isKnown
616 */
617 public function testIsAlwaysKnown( $page, $isKnown ) {
618 $title = Title::newFromText( $page );
619 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
620 }
621
622 public static function provideIsAlwaysKnown() {
623 return [
624 [ 'Some nonexistent page', false ],
625 [ 'UTPage', false ],
626 [ '#test', true ],
627 [ 'Special:BlankPage', true ],
628 [ 'Special:SomeNonexistentSpecialPage', false ],
629 [ 'MediaWiki:Parentheses', true ],
630 [ 'MediaWiki:Some nonexistent message', false ],
631 ];
632 }
633
634 /**
635 * @covers Title::isAlwaysKnown
636 */
637 public function testIsAlwaysKnownOnInterwiki() {
638 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
639 $this->assertTrue( $title->isAlwaysKnown() );
640 }
641
642 /**
643 * @covers Title::exists
644 */
645 public function testExists() {
646 $title = Title::makeTitle( NS_PROJECT, 'New page' );
647 $linkCache = LinkCache::singleton();
648
649 $article = new Article( $title );
650 $page = $article->getPage();
651 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
652
653 // Tell Title it doesn't know whether it exists
654 $title->mArticleID = -1;
655
656 // Tell the link cache it doesn't exists when it really does
657 $linkCache->clearLink( $title );
658 $linkCache->addBadLinkObj( $title );
659
660 $this->assertEquals(
661 false,
662 $title->exists(),
663 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
664 );
665 $this->assertEquals(
666 true,
667 $title->exists( Title::GAID_FOR_UPDATE ),
668 'exists() should re-query database when GAID_FOR_UPDATE is used'
669 );
670 }
671 }