Debian Codegraph @

tests/phpunit/includes/title/TitleTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Cache\BacklinkCache;
4 use MediaWiki\Content\WikitextContent;
5 use MediaWiki\Language\RawMessage;
6 use MediaWiki\Linker\LinkTarget;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Message\Message;
9 use MediaWiki\Page\PageIdentity;
10 use MediaWiki\Page\PageIdentityValue;
11 use MediaWiki\Tests\Unit\DummyServicesTrait;
12 use MediaWiki\Title\MalformedTitleException;
13 use MediaWiki\Title\Title;
14 use MediaWiki\Title\TitleValue;
15 use MediaWiki\Utils\MWTimestamp;
16 use Wikimedia\Assert\PreconditionException;
17 use Wikimedia\Rdbms\IDBAccessObject;
18
19 /**
20 * @group Database
21 * @group Title
22 */
23 class TitleTest extends MediaWikiIntegrationTestCase {
24 use DummyServicesTrait;
25 use LinkCacheTestTrait;
26
27 protected function setUp(): void {
28 parent::setUp();
29
30 $this->mergeMwGlobalArrayValue(
31 'wgExtraNamespaces',
32 [
33 12302 => 'TEST-JS',
34 12303 => 'TEST-JS_TALK',
35 ]
36 );
37 $this->mergeMwGlobalArrayValue(
38 'wgNamespaceContentModels',
39 [
40 12302 => CONTENT_MODEL_JAVASCRIPT,
41 ]
42 );
43
44 $this->overrideConfigValues( [
45 MainConfigNames::AllowUserJs => false,
46 MainConfigNames::DefaultLanguageVariant => false,
47 MainConfigNames::MetaNamespace => 'Project',
48 MainConfigNames::Server => 'https://example.org',
49 MainConfigNames::CanonicalServer => 'https://example.org',
50 MainConfigNames::ScriptPath => '/w',
51 MainConfigNames::Script => '/w/index.php',
52 MainConfigNames::LanguageCode => 'en',
53 // For testSecureAndSplitValid, testSecureAndSplitInvalid
54 MainConfigNames::LocalInterwikis => [ 'localtestiw' ],
55 ] );
56 $this->setUserLang( 'en' );
57
58 // Define valid interwiki prefixes and their configuration
59 $interwikiLookup = $this->getDummyInterwikiLookup( [
60 // testSecureAndSplitValid, testSecureAndSplitInvalid
61 [ 'iw_prefix' => 'localtestiw', 'iw_url' => 'localtestiw' ],
62 [ 'iw_prefix' => 'remotetestiw', 'iw_url' => 'remotetestiw' ],
63
64 // testSubpages
65 'wiki',
66
67 // testIsValid
68 'wikipedia',
69
70 // testIsValidRedirectTarget
71 'acme',
72
73 // testGetFragmentForURL
74 [ 'iw_prefix' => 'de', 'iw_local' => 1 ],
75 [ 'iw_prefix' => 'zz', 'iw_local' => 0 ],
76
77 // Some tests use interwikis - define valid prefixes and their configuration
78 [ 'iw_prefix' => 'acme', 'iw_url' => 'https://acme.test/$1' ],
79 [ 'iw_prefix' => 'yy', 'iw_url' => 'https://yy.wiki.test/wiki/$1', 'iw_local' => true ]
80 ] );
81 $this->setService( 'InterwikiLookup', $interwikiLookup );
82 }
83
84 protected function tearDown(): void {
85 parent::tearDown();
86 // delete dummy pages
87 $this->getNonexistingTestPage( 'UTest1' );
88 $this->getNonexistingTestPage( 'UTest2' );
89 }
90
91 public static function provideInNamespace() {
92 return [
93 [ 'Main Page', NS_MAIN, true ],
94 [ 'Main Page', NS_TALK, false ],
95 [ 'Main Page', NS_USER, false ],
96 [ 'User:Foo', NS_USER, true ],
97 [ 'User:Foo', NS_USER_TALK, false ],
98 [ 'User:Foo', NS_TEMPLATE, false ],
99 [ 'User_talk:Foo', NS_USER_TALK, true ],
100 [ 'User_talk:Foo', NS_USER, false ],
101 ];
102 }
103
104 /**
105 * @dataProvider provideInNamespace
106 * @covers \MediaWiki\Title\Title::inNamespace
107 */
108 public function testInNamespace( $title, $ns, $expectedBool ) {
109 $title = Title::newFromText( $title );
110 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
111 }
112
113 /**
114 * @covers \MediaWiki\Title\Title::inNamespaces
115 */
116 public function testInNamespaces() {
117 $mainpage = Title::newFromText( 'Main Page' );
118 $this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) );
119 $this->assertTrue( $mainpage->inNamespaces( [ NS_MAIN, NS_USER ] ) );
120 $this->assertTrue( $mainpage->inNamespaces( [ NS_USER, NS_MAIN ] ) );
121 $this->assertFalse( $mainpage->inNamespaces( [ NS_PROJECT, NS_TEMPLATE ] ) );
122 }
123
124 public static function provideHasSubjectNamespace() {
125 return [
126 [ 'Main Page', NS_MAIN, true ],
127 [ 'Main Page', NS_TALK, true ],
128 [ 'Main Page', NS_USER, false ],
129 [ 'User:Foo', NS_USER, true ],
130 [ 'User:Foo', NS_USER_TALK, true ],
131 [ 'User:Foo', NS_TEMPLATE, false ],
132 [ 'User_talk:Foo', NS_USER_TALK, true ],
133 [ 'User_talk:Foo', NS_USER, true ],
134 ];
135 }
136
137 /**
138 * @dataProvider provideHasSubjectNamespace
139 * @covers \MediaWiki\Title\Title::hasSubjectNamespace
140 */
141 public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
142 $title = Title::newFromText( $title );
143 $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
144 }
145
146 public function dataGetContentModel() {
147 return [
148 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
149 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
150 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
151 [ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
152 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
153 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
154 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
155 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
156 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
157 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
158 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
159 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
160 [ 'MediaWiki:Foo/bar.css', CONTENT_MODEL_CSS ],
161 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
162 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
163 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
164 [ 'TEST-JS:Foo', CONTENT_MODEL_JAVASCRIPT ],
165 [ 'TEST-JS:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
166 [ 'TEST-JS:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
167 [ 'TEST-JS_TALK:Foo.js', CONTENT_MODEL_WIKITEXT ],
168 ];
169 }
170
171 /**
172 * @dataProvider dataGetContentModel
173 * @covers \MediaWiki\Title\Title::getContentModel
174 */
175 public function testGetContentModel( $title, $expectedModelId ) {
176 $title = Title::newFromText( $title );
177 $this->assertEquals( $expectedModelId, $title->getContentModel() );
178 }
179
180 /**
181 * @dataProvider dataGetContentModel
182 * @covers \MediaWiki\Title\Title::hasContentModel
183 */
184 public function testHasContentModel( $title, $expectedModelId ) {
185 $title = Title::newFromText( $title );
186 $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
187 }
188
189 public static function provideIsSiteConfigPage() {
190 return [
191 [ 'Help:Foo', false ],
192 [ 'Help:Foo.js', false ],
193 [ 'Help:Foo/bar.js', false ],
194 [ 'User:Foo', false ],
195 [ 'User:Foo.js', false ],
196 [ 'User:Foo/bar.js', false ],
197 [ 'User:Foo/bar.json', false ],
198 [ 'User:Foo/bar.css', false ],
199 [ 'User:Foo/bar.JS', false ],
200 [ 'User:Foo/bar.JSON', false ],
201 [ 'User:Foo/bar.CSS', false ],
202 [ 'User talk:Foo/bar.css', false ],
203 [ 'User:Foo/bar.js.xxx', false ],
204 [ 'User:Foo/bar.xxx', false ],
205 [ 'MediaWiki:Foo.js', 'javascript' ],
206 [ 'MediaWiki:Foo.json', 'json' ],
207 [ 'MediaWiki:Foo.css', 'css' ],
208 [ 'MediaWiki:Foo.JS', false ],
209 [ 'MediaWiki:Foo.JSON', false ],
210 [ 'MediaWiki:Foo.CSS', false ],
211 [ 'MediaWiki:Foo/bar.css', 'css' ],
212 [ 'MediaWiki:Foo.css.xxx', false ],
213 [ 'TEST-JS:Foo', false ],
214 [ 'TEST-JS:Foo.js', false ],
215 ];
216 }
217
218 /**
219 * @dataProvider provideIsSiteConfigPage
220 * @covers \MediaWiki\Title\Title::isSiteConfigPage
221 * @covers \MediaWiki\Title\Title::isSiteJsConfigPage
222 * @covers \MediaWiki\Title\Title::isSiteJsonConfigPage
223 * @covers \MediaWiki\Title\Title::isSiteCssConfigPage
224 */
225 public function testSiteConfigPage( $title, $expected ) {
226 $title = Title::newFromText( $title );
227
228 // $expected is either false or the relevant type ('javascript', 'json', 'css')
229 $this->assertSame(
230 $expected !== false,
231 $title->isSiteConfigPage()
232 );
233 $this->assertSame(
234 $expected === 'javascript',
235 $title->isSiteJsConfigPage()
236 );
237 $this->assertSame(
238 $expected === 'json',
239 $title->isSiteJsonConfigPage()
240 );
241 $this->assertSame(
242 $expected === 'css',
243 $title->isSiteCssConfigPage()
244 );
245 }
246
247 public static function provideIsUserConfigPage() {
248 return [
249 [ 'Help:Foo', false ],
250 [ 'Help:Foo.js', false ],
251 [ 'Help:Foo/bar.js', false ],
252 [ 'User:Foo', false ],
253 [ 'User:Foo.js', false ],
254 [ 'User:Foo/bar.js', 'javascript' ],
255 [ 'User:Foo/bar.JS', false ],
256 [ 'User:Foo/bar.json', 'json' ],
257 [ 'User:Foo/bar.JSON', false ],
258 [ 'User:Foo/bar.css', 'css' ],
259 [ 'User:Foo/bar.CSS', false ],
260 [ 'User talk:Foo/bar.css', false ],
261 [ 'User:Foo/bar.js.xxx', false ],
262 [ 'User:Foo/bar.xxx', false ],
263 [ 'MediaWiki:Foo.js', false ],
264 [ 'MediaWiki:Foo.json', false ],
265 [ 'MediaWiki:Foo.css', false ],
266 [ 'MediaWiki:Foo.JS', false ],
267 [ 'MediaWiki:Foo.JSON', false ],
268 [ 'MediaWiki:Foo.CSS', false ],
269 [ 'MediaWiki:Foo.css.xxx', false ],
270 [ 'TEST-JS:Foo', false ],
271 [ 'TEST-JS:Foo.js', false ],
272 ];
273 }
274
275 /**
276 * @dataProvider provideIsUserConfigPage
277 * @covers \MediaWiki\Title\Title::isUserConfigPage
278 * @covers \MediaWiki\Title\Title::isUserJsConfigPage
279 * @covers \MediaWiki\Title\Title::isUserJsonConfigPage
280 * @covers \MediaWiki\Title\Title::isUserCssConfigPage
281 */
282 public function testIsUserConfigPage( $title, $expected ) {
283 $title = Title::newFromText( $title );
284
285 // $expected is either false or the relevant type ('javascript', 'json', 'css')
286 $this->assertSame(
287 $expected !== false,
288 $title->isUserConfigPage()
289 );
290 $this->assertSame(
291 $expected === 'javascript',
292 $title->isUserJsConfigPage()
293 );
294 $this->assertSame(
295 $expected === 'json',
296 $title->isUserJsonConfigPage()
297 );
298 $this->assertSame(
299 $expected === 'css',
300 $title->isUserCssConfigPage()
301 );
302 }
303
304 public static function provideIsWikitextPage() {
305 return [
306 [ 'Help:Foo', true ],
307 [ 'Help:Foo.js', true ],
308 [ 'Help:Foo/bar.js', true ],
309 [ 'User:Foo', true ],
310 [ 'User:Foo.js', true ],
311 [ 'User:Foo/bar.js', false ],
312 [ 'User:Foo/bar.json', false ],
313 [ 'User:Foo/bar.css', false ],
314 [ 'User talk:Foo/bar.css', true ],
315 [ 'User:Foo/bar.js.xxx', true ],
316 [ 'User:Foo/bar.xxx', true ],
317 [ 'MediaWiki:Foo.js', false ],
318 [ 'User:Foo/bar.JS', true ],
319 [ 'User:Foo/bar.JSON', true ],
320 [ 'User:Foo/bar.CSS', true ],
321 [ 'MediaWiki:Foo.json', false ],
322 [ 'MediaWiki:Foo.css', false ],
323 [ 'MediaWiki:Foo.JS', true ],
324 [ 'MediaWiki:Foo.JSON', true ],
325 [ 'MediaWiki:Foo.CSS', true ],
326 [ 'MediaWiki:Foo.css.xxx', true ],
327 [ 'TEST-JS:Foo', false ],
328 [ 'TEST-JS:Foo.js', false ],
329 ];
330 }
331
332 /**
333 * @dataProvider provideIsWikitextPage
334 * @covers \MediaWiki\Title\Title::isWikitextPage
335 */
336 public function testIsWikitextPage( $title, $expectedBool ) {
337 $title = Title::newFromText( $title );
338 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
339 }
340
341 public static function provideGetOtherPage() {
342 return [
343 [ 'Main Page', 'Talk:Main Page' ],
344 [ 'Talk:Main Page', 'Main Page' ],
345 [ 'Help:Main Page', 'Help talk:Main Page' ],
346 [ 'Help talk:Main Page', 'Help:Main Page' ],
347 [ 'Special:FooBar', null ],
348 [ 'Media:File.jpg', null ],
349 ];
350 }
351
352 /**
353 * @dataProvider provideGetOtherpage
354 * @covers \MediaWiki\Title\Title::getOtherPage
355 *
356 * @param string $text
357 * @param string|null $expected
358 */
359 public function testGetOtherPage( $text, $expected ) {
360 if ( $expected === null ) {
361 $this->expectException( MWException::class );
362 }
363
364 $title = Title::newFromText( $text );
365 $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
366 }
367
368 /**
369 * @covers \MediaWiki\Title\Title::clearCaches
370 */
371 public function testClearCaches() {
372 $linkCache = $this->getServiceContainer()->getLinkCache();
373
374 $title1 = Title::newFromText( 'Foo' );
375 $this->addGoodLinkObject( 23, $title1 );
376
377 Title::clearCaches();
378
379 $title2 = Title::newFromText( 'Foo' );
380 $this->assertNotSame( $title1, $title2, 'title cache should be empty' );
381 $this->assertSame( 0, $linkCache->getGoodLinkID( 'Foo' ), 'link cache should be empty' );
382 }
383
384 /**
385 * @covers \MediaWiki\Title\Title::getFieldFromPageStore
386 */
387 public function testUseCaches() {
388 $title1 = Title::makeTitle( NS_MAIN, __METHOD__ . '998724352' );
389 $this->addGoodLinkObject( 23, $title1, 7, 0, 42 );
390
391 // Ensure that getLatestRevID uses the LinkCache even after
392 // the article ID is known (T296063#7520023).
393 $this->assertSame( 23, $title1->getArticleID() );
394 $this->assertSame( 42, $title1->getLatestRevID() );
395 }
396
397 public static function provideGetLinkURL() {
398 yield 'Simple' => [
399 '/wiki/Goats',
400 NS_MAIN,
401 'Goats'
402 ];
403
404 yield 'Fragment' => [
405 '/wiki/Goats#Goatificatiön',
406 NS_MAIN,
407 'Goats',
408 'Goatificatiön'
409 ];
410
411 yield 'Fragment only (query is ignored)' => [
412 '#Goatificatiön',
413 NS_MAIN,
414 '',
415 'Goatificatiön',
416 '',
417 [
418 'a' => 1,
419 ]
420 ];
421
422 yield 'Unknown interwiki with fragment' => [
423 'https://xx.wiki.test/wiki/xyzzy:Goats#Goatificatiön',
424 NS_MAIN,
425 'Goats',
426 'Goatificatiön',
427 'xyzzy'
428 ];
429
430 yield 'Interwiki with fragment' => [
431 'https://acme.test/Goats#Goatificati.C3.B6n',
432 NS_MAIN,
433 'Goats',
434 'Goatificatiön',
435 'acme'
436 ];
437
438 yield 'Interwiki with query' => [
439 'https://acme.test/Goats?a=1&b=blank+blank#Goatificati.C3.B6n',
440 NS_MAIN,
441 'Goats',
442 'Goatificatiön',
443 'acme',
444 [
445 'a' => 1,
446 'b' => 'blank blank'
447 ]
448 ];
449
450 yield 'Local interwiki with fragment' => [
451 'https://yy.wiki.test/wiki/Goats#Goatificatiön',
452 NS_MAIN,
453 'Goats',
454 'Goatificatiön',
455 'yy'
456 ];
457 }
458
459 /**
460 * @dataProvider provideGetLinkURL
461 *
462 * @covers \MediaWiki\Title\Title::getLinkURL
463 * @covers \MediaWiki\Title\Title::getFullURL
464 * @covers \MediaWiki\Title\Title::getLocalURL
465 * @covers \MediaWiki\Title\Title::getFragmentForURL
466 */
467 public function testGetLinkURL(
468 $expected,
469 $ns,
470 $title,
471 $fragment = '',
472 $interwiki = '',
473 $query = '',
474 $query2 = false,
475 $proto = false
476 ) {
477 $this->overrideConfigValues( [
478 MainConfigNames::Server => 'https://xx.wiki.test',
479 MainConfigNames::ExternalInterwikiFragmentMode => 'legacy',
480 MainConfigNames::FragmentMode => [ 'html5', 'legacy' ]
481 ] );
482
483 $title = Title::makeTitle( $ns, $title, $fragment, $interwiki );
484 $this->assertSame( $expected, $title->getLinkURL( $query, $query2, $proto ) );
485 }
486
487 public static function provideProperPage() {
488 return [
489 [ NS_MAIN, 'Test' ],
490 [ NS_MAIN, 'User' ],
491 ];
492 }
493
494 /**
495 * @dataProvider provideProperPage
496 * @covers \MediaWiki\Title\Title::toPageIdentity
497 */
498 public function testToPageIdentity( $ns, $text ) {
499 $title = Title::makeTitle( $ns, $text );
500
501 $page = $title->toPageIdentity();
502
503 $this->assertNotSame( $title, $page );
504 $this->assertSame( $title->getId(), $page->getId() );
505 $this->assertSame( $title->getNamespace(), $page->getNamespace() );
506 $this->assertSame( $title->getDBkey(), $page->getDBkey() );
507 $this->assertSame( $title->getWikiId(), $page->getWikiId() );
508 }
509
510 /**
511 * @dataProvider provideProperPage
512 * @covers \MediaWiki\Title\Title::toPageRecord
513 */
514 public function testToPageRecord( $ns, $text ) {
515 $title = Title::makeTitle( $ns, $text );
516 $wikiPage = $this->getExistingTestPage( $title );
517
518 $record = $title->toPageRecord();
519
520 $this->assertNotSame( $title, $record );
521 $this->assertNotSame( $title, $wikiPage );
522
523 $this->assertSame( $title->getId(), $record->getId() );
524 $this->assertSame( $title->getNamespace(), $record->getNamespace() );
525 $this->assertSame( $title->getDBkey(), $record->getDBkey() );
526 $this->assertSame( $title->getWikiId(), $record->getWikiId() );
527
528 $this->assertSame( $title->getLatestRevID(), $record->getLatest() );
529 $this->assertSame( MWTimestamp::convert( TS_MW, $title->getTouched() ), $record->getTouched() );
530 $this->assertSame( $title->isNewPage(), $record->isNew() );
531 $this->assertSame( $title->isRedirect(), $record->isRedirect() );
532 $this->assertSame( $title->getTouched(), $record->getTouched() );
533 }
534
535 /**
536 * @dataProvider provideImproperPage
537 * @covers \MediaWiki\Title\Title::toPageRecord
538 */
539 public function testToPageRecord_fail( $ns, $text, $fragment = '', $interwiki = '' ) {
540 $title = Title::makeTitle( $ns, $text, $fragment, $interwiki );
541
542 $this->expectException( PreconditionException::class );
543 $title->toPageRecord();
544 }
545
546 public static function provideImproperPage() {
547 return [
548 [ NS_MAIN, '' ],
549 [ NS_MAIN, '<>' ],
550 [ NS_MAIN, '|' ],
551 [ NS_MAIN, '#' ],
552 [ NS_PROJECT, '#test' ],
553 [ NS_MAIN, '', 'test', 'acme' ],
554 [ NS_MAIN, ' Test' ],
555 [ NS_MAIN, '_Test' ],
556 [ NS_MAIN, 'Test ' ],
557 [ NS_MAIN, 'Test_' ],
558 [ NS_MAIN, "Test\nthis" ],
559 [ NS_MAIN, "Test\tthis" ],
560 [ -33, 'Test' ],
561 [ 77663399, 'Test' ],
562
563 // Valid but can't exist
564 [ NS_MAIN, '', 'test' ],
565 [ NS_SPECIAL, 'Test' ],
566 [ NS_MAIN, 'Test', '', 'acme' ],
567 ];
568 }
569
570 /**
571 * @dataProvider provideImproperPage
572 * @covers \MediaWiki\Title\Title::getId
573 */
574 public function testGetId_fail( $ns, $text, $fragment = '', $interwiki = '' ) {
575 $title = Title::makeTitle( $ns, $text, $fragment, $interwiki );
576
577 $this->expectException( PreconditionException::class );
578 $title->getId();
579 }
580
581 /**
582 * @dataProvider provideImproperPage
583 * @covers \MediaWiki\Title\Title::getId
584 */
585 public function testGetId_fragment() {
586 $title = Title::makeTitle( NS_MAIN, 'Test', 'References' );
587
588 // should not throw
589 $this->assertIsInt( $title->getId() );
590 }
591
592 /**
593 * @dataProvider provideImproperPage
594 * @covers \MediaWiki\Title\Title::toPageIdentity
595 */
596 public function testToPageIdentity_fail( $ns, $text, $fragment = '', $interwiki = '' ) {
597 $title = Title::makeTitle( $ns, $text, $fragment, $interwiki );
598
599 $this->expectException( PreconditionException::class );
600 $title->toPageIdentity();
601 }
602
603 public static function provideMakeTitle() {
604 yield 'main namespace' => [ 'Foo', NS_MAIN, 'Foo' ];
605 yield 'user namespace' => [ 'User:Foo', NS_USER, 'Foo' ];
606 yield 'fragment' => [ 'Foo#Section', NS_MAIN, 'Foo', 'Section' ];
607 yield 'only fragment' => [ '#Section', NS_MAIN, '', 'Section' ];
608 yield 'interwiki' => [ 'acme:Foo', NS_MAIN, 'Foo', '', 'acme' ];
609 yield 'normalized underscores' => [ 'Foo Bar', NS_MAIN, 'Foo_Bar' ];
610 }
611
612 /**
613 * @dataProvider provideMakeTitle
614 * @covers \MediaWiki\Title\Title::makeTitle
615 */
616 public function testMakeTitle( $expected, $ns, $text, $fragment = '', $interwiki = '' ) {
617 $title = Title::makeTitle( $ns, $text, $fragment, $interwiki );
618
619 $this->assertTrue( $title->isValid() );
620 $this->assertSame( $expected, $title->getFullText() );
621 }
622
623 public static function provideMakeTitle_invalid() {
624 yield 'bad namespace' => [ 'Special:Badtitle/NS-1234:Foo', -1234, 'Foo' ];
625 yield 'lower case' => [ 'User:foo', NS_USER, 'foo' ];
626 yield 'empty' => [ '', NS_MAIN, '' ];
627 yield 'bad character' => [ 'Foo|Bar', NS_MAIN, 'Foo|Bar' ];
628 yield 'bad interwiki' => [ 'qwerty:Foo', NS_MAIN, 'Foo', '', 'qwerty' ];
629 }
630
631 /**
632 * @dataProvider provideMakeTitle_invalid
633 * @covers \MediaWiki\Title\Title::makeTitle
634 */
635 public function testMakeTitle_invalid( $expected, $ns, $text, $fragment = '', $interwiki = '' ) {
636 $title = Title::makeTitle( $ns, $text, $fragment, $interwiki );
637
638 $this->assertFalse( $title->isValid() );
639 $this->assertSame( $expected, $title->getFullText() );
640 }
641
642 public static function provideMakeName() {
643 yield 'main namespace' => [ 'Foo', NS_MAIN, 'Foo' ];
644 yield 'user namespace' => [ 'User:Foo', NS_USER, 'Foo' ];
645 yield 'fragment' => [ 'Foo#Section', NS_MAIN, 'Foo', 'Section' ];
646 yield 'only fragment' => [ '#Section', NS_MAIN, '', 'Section' ];
647 yield 'interwiki' => [ 'acme:Foo', NS_MAIN, 'Foo', '', 'acme' ];
648 yield 'bad namespace' => [ 'Special:Badtitle/NS-1234:Foo', -1234, 'Foo' ];
649 yield 'lower case' => [ 'User:foo', NS_USER, 'foo' ];
650 yield 'empty' => [ '', NS_MAIN, '' ];
651 yield 'bad character' => [ 'Foo|Bar', NS_MAIN, 'Foo|Bar' ];
652 yield 'bad interwiki' => [ 'qwerty:Foo', NS_MAIN, 'Foo', '', 'qwerty' ];
653 }
654
655 /**
656 * @dataProvider provideMakeName
657 * @covers \MediaWiki\Title\Title::makeName
658 */
659 public function testMakeName( $expected, $ns, $text, $fragment = '', $interwiki = '' ) {
660 $titleName = Title::makeName( $ns, $text, $fragment, $interwiki );
661
662 $this->assertSame( $expected, $titleName );
663 }
664
665 public static function provideMakeTitleSafe() {
666 yield 'main namespace' => [ 'Foo', NS_MAIN, 'Foo' ];
667 yield 'user namespace' => [ 'User:Foo', NS_USER, 'Foo' ];
668 yield 'fragment' => [ 'Foo#Section', NS_MAIN, 'Foo', 'Section' ];
669 yield 'only fragment' => [ '#Section', NS_MAIN, '', 'Section' ];
670 yield 'interwiki' => [ 'acme:Foo', NS_MAIN, 'Foo', '', 'acme' ];
671
672 // Normalize
673 yield 'normalized underscores' => [ 'Foo Bar', NS_MAIN, 'Foo_Bar' ];
674 yield 'lower case' => [ 'User:Foo', NS_USER, 'foo' ];
675
676 // Bad interwiki becomes part of the title text. Is this intentional?
677 yield 'bad interwiki' => [ 'Qwerty:Foo', NS_MAIN, 'Foo', '', 'qwerty' ];
678 }
679
680 /**
681 * @dataProvider provideMakeTitleSafe
682 * @covers \MediaWiki\Title\Title::makeTitleSafe
683 */
684 public function testMakeTitleSafe( $expected, $ns, $text, $fragment = '', $interwiki = '' ) {
685 $title = Title::makeTitleSafe( $ns, $text, $fragment, $interwiki );
686
687 $this->assertTrue( $title->isValid() );
688 $this->assertSame( $expected, $title->getFullText() );
689 }
690
691 public static function provideMakeTitleSafe_invalid() {
692 yield 'bad namespace' => [ -1234, 'Foo' ];
693 yield 'empty' => [ '', NS_MAIN, '' ];
694 yield 'bad character' => [ NS_MAIN, 'Foo|Bar' ];
695 }
696
697 /**
698 * @dataProvider provideMakeTitleSafe_invalid
699 * @covers \MediaWiki\Title\Title::makeTitleSafe
700 */
701 public function testMakeTitleSafe_invalid( $ns, $text, $fragment = '', $interwiki = '' ) {
702 $title = Title::makeTitleSafe( $ns, $text, $fragment, $interwiki );
703
704 $this->assertNull( $title );
705 }
706
707 /**
708 * @covers \MediaWiki\Title\Title::getContentModel
709 * @covers \MediaWiki\Title\Title::setContentModel
710 * @covers \MediaWiki\Title\Title::uncache
711 */
712 public function testSetContentModel() {
713 // NOTE: must use newFromText to test behavior of internal instance cache (T281337)
714 $title = Title::newFromText( 'Foo' );
715
716 $title->setContentModel( CONTENT_MODEL_UNKNOWN );
717 $this->assertSame( CONTENT_MODEL_UNKNOWN, $title->getContentModel() );
718
719 // Ensure that the instance we get back from newFromText isn't the modified one.
720 $title = Title::newFromText( 'Foo' );
721 $this->assertNotSame( CONTENT_MODEL_UNKNOWN, $title->getContentModel() );
722 }
723
724 /**
725 * @covers \MediaWiki\Title\Title::newFromID
726 * @covers \MediaWiki\Title\Title::newFromRow
727 */
728 public function testNewFromId() {
729 // First id
730 $existingPage1 = $this->getExistingTestPage( 'UTest1' );
731 $existingTitle1 = $existingPage1->getTitle();
732 $existingId1 = $existingTitle1->getId();
733
734 $this->assertGreaterThan( 0, $existingId1, 'Existing test page should have a positive id' );
735
736 $newFromId1 = Title::newFromID( $existingId1 );
737 $this->assertInstanceOf( Title::class, $newFromId1, 'newFromID returns a title for an existing id' );
738 $this->assertTrue(
739 $newFromId1->equals( $existingTitle1 ),
740 'newFromID returns the correct title'
741 );
742
743 // Second id
744 $existingPage2 = $this->getExistingTestPage( 'UTest2' );
745 $existingTitle2 = $existingPage2->getTitle();
746 $existingId2 = $existingTitle2->getId();
747
748 $this->assertGreaterThan( 0, $existingId2, 'Existing test page should have a positive id' );
749
750 $newFromId2 = Title::newFromID( $existingId2 );
751 $this->assertInstanceOf( Title::class, $newFromId2, 'newFromID returns a title for an existing id' );
752 $this->assertTrue(
753 $newFromId2->equals( $existingTitle2 ),
754 'newFromID returns the correct title'
755 );
756 }
757
758 /**
759 * @covers \MediaWiki\Title\Title::newFromID
760 */
761 public function testNewFromMissingId() {
762 // Testing return of null for an id that does not exist
763 $maxPageId = (int)$this->getDb()->newSelectQueryBuilder()
764 ->select( 'max(page_id)' )
765 ->from( 'page' )
766 ->caller( __METHOD__ )->fetchField();
767 $res = Title::newFromID( $maxPageId + 1 );
768 $this->assertNull( $res, 'newFromID returns null for missing ids' );
769 }
770
771 public static function provideValidSecureAndSplit() {
772 return [
773 [ 'Sandbox' ],
774 [ 'A "B"' ],
775 [ 'A \'B\'' ],
776 [ '.com' ],
777 [ '~' ],
778 [ '#' ],
779 [ '"' ],
780 [ '\'' ],
781 [ 'Talk:Sandbox' ],
782 [ 'Talk:Foo:Sandbox' ],
783 [ 'File:Example.svg' ],
784 [ 'File_talk:Example.svg' ],
785 [ 'Foo/.../Sandbox' ],
786 [ 'Sandbox/...' ],
787 [ 'A~~' ],
788 [ ':A' ],
789 // Length is 256 total, but only title part matters
790 [ 'Category:' . str_repeat( 'x', 248 ) ],
791 [ str_repeat( 'x', 252 ) ],
792 // interwiki prefix
793 [ 'localtestiw: #anchor' ],
794 [ 'localtestiw:' ],
795 [ 'localtestiw:foo' ],
796 [ 'localtestiw: foo # anchor' ],
797 [ 'localtestiw: Talk: Sandbox # anchor' ],
798 [ 'remotetestiw:' ],
799 [ 'remotetestiw: Talk: # anchor' ],
800 [ 'remotetestiw: #bar' ],
801 [ 'remotetestiw: Talk:' ],
802 [ 'remotetestiw: Talk: Foo' ],
803 [ 'localtestiw:remotetestiw:' ],
804 [ 'localtestiw:remotetestiw:foo' ]
805 ];
806 }
807
808 public static function provideInvalidSecureAndSplit() {
809 return [
810 [ '', 'title-invalid-empty' ],
811 [ ':', 'title-invalid-empty' ],
812 [ '__ __', 'title-invalid-empty' ],
813 [ ' __ ', 'title-invalid-empty' ],
814 // Bad characters forbidden regardless of wgLegalTitleChars
815 [ 'A [ B', 'title-invalid-characters' ],
816 [ 'A ] B', 'title-invalid-characters' ],
817 [ 'A { B', 'title-invalid-characters' ],
818 [ 'A } B', 'title-invalid-characters' ],
819 [ 'A < B', 'title-invalid-characters' ],
820 [ 'A > B', 'title-invalid-characters' ],
821 [ 'A | B', 'title-invalid-characters' ],
822 [ "A \t B", 'title-invalid-characters' ],
823 [ "A \n B", 'title-invalid-characters' ],
824 // URL encoding
825 [ 'A%20B', 'title-invalid-characters' ],
826 [ 'A%23B', 'title-invalid-characters' ],
827 [ 'A%2523B', 'title-invalid-characters' ],
828 // XML/HTML character entity references
829 // Note: Commented out because they are not marked invalid by the PHP test as
830 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
831 // 'A &eacute; B',
832 // Subject of NS_TALK does not roundtrip to NS_MAIN
833 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
834 // Directory navigation
835 [ '.', 'title-invalid-relative' ],
836 [ '..', 'title-invalid-relative' ],
837 [ './Sandbox', 'title-invalid-relative' ],
838 [ '../Sandbox', 'title-invalid-relative' ],
839 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
840 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
841 [ 'Sandbox/.', 'title-invalid-relative' ],
842 [ 'Sandbox/..', 'title-invalid-relative' ],
843 // Tilde
844 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
845 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
846 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
847 // Length
848 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
849 // Namespace prefix without actual title
850 [ 'Talk:', 'title-invalid-empty' ],
851 [ 'Talk:#', 'title-invalid-empty' ],
852 [ 'Category: ', 'title-invalid-empty' ],
853 [ 'Category: #bar', 'title-invalid-empty' ],
854 // interwiki prefix
855 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
856 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
857 ];
858 }
859
860 /**
861 * See also mediawiki.Title.test.js
862 * @covers \MediaWiki\Title\Title::secureAndSplit
863 * @dataProvider provideValidSecureAndSplit
864 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
865 */
866 public function testSecureAndSplitValid( $text ) {
867 $this->assertInstanceOf( Title::class, Title::newFromText( $text ), "Valid: $text" );
868 }
869
870 /**
871 * See also mediawiki.Title.test.js
872 * @covers \MediaWiki\Title\Title::secureAndSplit
873 * @dataProvider provideInvalidSecureAndSplit
874 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
875 */
876 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
877 try {
878 Title::newFromTextThrow( $text ); // should throw
879 $this->fail( "Title::newFromTextThrow should have thrown with $text" );
880 } catch ( MalformedTitleException $ex ) {
881 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
882 }
883 }
884
885 public static function provideSpecialNamesWithAndWithoutParameter() {
886 return [
887 [ 'Special:Version', null ],
888 [ 'Special:Version/', '' ],
889 [ 'Special:Version/param', 'param' ],
890 ];
891 }
892
893 /**
894 * @dataProvider provideSpecialNamesWithAndWithoutParameter
895 * @covers \MediaWiki\Title\Title::fixSpecialName
896 */
897 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
898 $title = Title::newFromText( $text );
899 $fixed = $title->fixSpecialName();
900 $stuff = explode( '/', $fixed->getDBkey(), 2 );
901 $par = $stuff[1] ?? null;
902 $this->assertEquals(
903 $expectedParam,
904 $par,
905 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
906 );
907 }
908
909 public function flattenErrorsArray( $errors ) {
910 $result = [];
911 foreach ( $errors as $error ) {
912 $result[] = $error[0];
913 }
914
915 return $result;
916 }
917
918 public static function provideGetPageViewLanguage() {
919 # Format:
920 # - expected
921 # - Title name
922 # - content language (expected in most cases)
923 # - wgLang (on some specific pages)
924 # - wgDefaultLanguageVariant
925 return [
926 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
927 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
928 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
929
930 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
931 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
932 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
933 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
934 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
935 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
936 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
937 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
938
939 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
940 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
941 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
942 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
943 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
944 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
945 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
946 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
947 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
948 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
949
950 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
951 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
952
953 ];
954 }
955
956 /**
957 * Superseded by OutputPageTest::testGetJsVarsAboutPageLang
958 *
959 * @dataProvider provideGetPageViewLanguage
960 * @covers \MediaWiki\Title\Title::getPageViewLanguage
961 */
962 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
963 $lang, $variant, $msg = ''
964 ) {
965 // Setup environment for this test
966 $this->overrideConfigValues( [
967 MainConfigNames::DefaultLanguageVariant => $variant,
968 MainConfigNames::AllowUserJs => true,
969 ] );
970 $this->setUserLang( $lang );
971 $this->overrideConfigValue( MainConfigNames::LanguageCode, $contLang );
972
973 $title = Title::newFromText( $titleText );
974 $this->assertInstanceOf( Title::class, $title,
975 "Test must be passed a valid title text, you gave '$titleText'"
976 );
977 $this->hideDeprecated( Title::class . '::getPageViewLanguage' );
978 $this->assertEquals( $expected,
979 $title->getPageViewLanguage()->getCode(),
980 $msg
981 );
982 }
983
984 public static function provideSubpage() {
985 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
986 return [
987 [ 'Foo', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
988 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
989 [ 'User:Foo', 'x', new TitleValue( NS_USER, 'Foo/x' ) ],
990 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN, 'User:Foo/x', '', 'wiki' ) ],
991 ];
992 }
993
994 /**
995 * @dataProvider provideSubpage
996 * @covers \MediaWiki\Title\Title::getSubpage
997 */
998 public function testSubpage( $title, $sub, LinkTarget $expected ) {
999 $title = Title::newFromText( $title );
1000 $expected = Title::newFromLinkTarget( $expected );
1001 $actual = $title->getSubpage( $sub );
1002
1003 // NOTE: convert to string for comparison
1004 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
1005 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
1006 }
1007
1008 public static function provideIsAlwaysKnown() {
1009 return [
1010 [ 'Some nonexistent page' . wfRandomString(), false ],
1011 [ 'Some existent page', false, true ],
1012 [ '#test', true ],
1013 [ 'Special:BlankPage', true ],
1014 [ 'Special:SomeNonexistentSpecialPage', false ],
1015 [ 'MediaWiki:Parentheses', true ],
1016 [ 'MediaWiki:Some nonexistent message', false ],
1017 ];
1018 }
1019
1020 /**
1021 * @covers \MediaWiki\Title\Title::isAlwaysKnown
1022 * @dataProvider provideIsAlwaysKnown
1023 * @param string $page
1024 * @param bool $isKnown
1025 * @param bool $createIfNotExists
1026 */
1027 public function testIsAlwaysKnown( $page, $isKnown, bool $createIfNotExists = false ) {
1028 if ( $createIfNotExists ) {
1029 $this->getExistingTestPage( $page );
1030 }
1031 $title = Title::newFromText( $page );
1032 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
1033 }
1034
1035 public static function provideIsValid() {
1036 return [
1037 [ Title::makeTitle( NS_MAIN, '' ), false ],
1038 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
1039 [ Title::makeTitle( NS_MAIN, '|' ), false ],
1040 [ Title::makeTitle( NS_MAIN, '#' ), false ],
1041 [ Title::makeTitle( NS_PROJECT, '#' ), false ],
1042 [ Title::makeTitle( NS_MAIN, '', 'test' ), true ],
1043 [ Title::makeTitle( NS_PROJECT, '#test' ), false ],
1044 [ Title::makeTitle( NS_MAIN, '', 'test', 'wikipedia' ), true ],
1045 [ Title::makeTitle( NS_MAIN, 'Test', '', 'wikipedia' ), true ],
1046 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
1047 [ Title::makeTitle( NS_SPECIAL, 'Test' ), true ],
1048 [ Title::makeTitle( NS_MAIN, ' Test' ), false ],
1049 [ Title::makeTitle( NS_MAIN, '_Test' ), false ],
1050 [ Title::makeTitle( NS_MAIN, 'Test ' ), false ],
1051 [ Title::makeTitle( NS_MAIN, 'Test_' ), false ],
1052 [ Title::makeTitle( NS_MAIN, "Test\nthis" ), false ],
1053 [ Title::makeTitle( NS_MAIN, "Test\tthis" ), false ],
1054 [ Title::makeTitle( -33, 'Test' ), false ],
1055 [ Title::makeTitle( 77663399, 'Test' ), false ],
1056 ];
1057 }
1058
1059 /**
1060 * @covers \MediaWiki\Title\Title::isValid
1061 * @dataProvider provideIsValid
1062 * @param Title $title
1063 * @param bool $isValid
1064 */
1065 public function testIsValid( Title $title, $isValid ) {
1066 $this->assertEquals( $isValid, $title->isValid(), $title->getFullText() );
1067 }
1068
1069 public static function provideIsValidRedirectTarget() {
1070 return [
1071 [ Title::makeTitle( NS_MAIN, '' ), false ],
1072 [ Title::makeTitle( NS_MAIN, '', 'test' ), false ],
1073 [ Title::makeTitle( NS_MAIN, 'Foo', 'test' ), true ],
1074 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
1075 [ Title::makeTitle( NS_MAIN, '_' ), false ],
1076 [ Title::makeTitle( NS_MAIN, 'Test', '', 'acme' ), true ],
1077 [ Title::makeTitle( NS_SPECIAL, 'UserLogout' ), false ],
1078 [ Title::makeTitle( NS_SPECIAL, 'RecentChanges' ), true ],
1079 ];
1080 }
1081
1082 /**
1083 * @covers \MediaWiki\Title\Title::isValidRedirectTarget
1084 * @dataProvider provideIsValidRedirectTarget
1085 * @param Title $title
1086 * @param bool $isValid
1087 */
1088 public function testIsValidRedirectTarget( Title $title, $isValid ) {
1089 // InterwikiLookup is configured in setUp()
1090 $this->assertEquals( $isValid, $title->isValidRedirectTarget(), $title->getFullText() );
1091 }
1092
1093 public static function provideCanExist() {
1094 return [
1095 [ Title::makeTitle( NS_MAIN, '' ), false ],
1096 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
1097 [ Title::makeTitle( NS_MAIN, '|' ), false ],
1098 [ Title::makeTitle( NS_MAIN, '#' ), false ],
1099 [ Title::makeTitle( NS_PROJECT, '#test' ), false ],
1100 [ Title::makeTitle( NS_MAIN, '', 'test', 'acme' ), false ],
1101 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
1102 [ Title::makeTitle( NS_MAIN, ' Test' ), false ],
1103 [ Title::makeTitle( NS_MAIN, '_Test' ), false ],
1104 [ Title::makeTitle( NS_MAIN, 'Test ' ), false ],
1105 [ Title::makeTitle( NS_MAIN, 'Test_' ), false ],
1106 [ Title::makeTitle( NS_MAIN, "Test\nthis" ), false ],
1107 [ Title::makeTitle( NS_MAIN, "Test\tthis" ), false ],
1108 [ Title::makeTitle( -33, 'Test' ), false ],
1109 [ Title::makeTitle( 77663399, 'Test' ), false ],
1110
1111 // Valid but can't exist
1112 [ Title::makeTitle( NS_MAIN, '', 'test' ), false ],
1113 [ Title::makeTitle( NS_SPECIAL, 'Test' ), false ],
1114 [ Title::makeTitle( NS_MAIN, 'Test', '', 'acme' ), false ],
1115 ];
1116 }
1117
1118 /**
1119 * @covers \MediaWiki\Title\Title::canExist
1120 * @dataProvider provideCanExist
1121 * @param Title $title
1122 * @param bool $canExist
1123 */
1124 public function testCanExist( Title $title, $canExist ) {
1125 $this->assertEquals( $canExist, $title->canExist(), $title->getFullText() );
1126 }
1127
1128 /**
1129 * @covers \MediaWiki\Title\Title::isAlwaysKnown
1130 */
1131 public function testIsAlwaysKnownOnInterwiki() {
1132 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
1133 $this->assertTrue( $title->isAlwaysKnown() );
1134 }
1135
1136 public static function provideGetSkinFromConfigSubpage() {
1137 return [
1138 [ 'User:Foo', '' ],
1139 [ 'User:Foo.css', '' ],
1140 [ 'User:Foo/', '' ],
1141 [ 'User:Foo/bar', '' ],
1142 [ 'User:Foo./bar', '' ],
1143 [ 'User:Foo/bar.', 'bar' ],
1144 [ 'User:Foo/bar.css', 'bar' ],
1145 [ '/bar.css', '' ],
1146 [ '//bar.css', 'bar' ],
1147 [ '.css', '' ],
1148 ];
1149 }
1150
1151 /**
1152 * @dataProvider provideGetSkinFromConfigSubpage
1153 * @covers \MediaWiki\Title\Title::getSkinFromConfigSubpage
1154 */
1155 public function testGetSkinFromConfigSubpage( $title, $expected ) {
1156 $title = Title::newFromText( $title );
1157 $this->assertSame( $expected, $title->getSkinFromConfigSubpage() );
1158 }
1159
1160 /**
1161 * @covers \MediaWiki\Title\Title::getWikiId
1162 */
1163 public function testGetWikiId() {
1164 $title = Title::newFromText( 'Foo' );
1165 $this->assertFalse( $title->getWikiId() );
1166 }
1167
1168 /**
1169 * @covers \MediaWiki\Title\Title::getFragment
1170 * @covers \MediaWiki\Title\Title::getFragment
1171 * @covers \MediaWiki\Title\Title::uncache
1172 */
1173 public function testSetFragment() {
1174 // NOTE: must use newFromText to test behavior of internal instance cache (T281337)
1175 $title = Title::newFromText( 'Foo' );
1176
1177 $title->setFragment( '#Xyzzy' );
1178 $this->assertSame( 'Xyzzy', $title->getFragment() );
1179
1180 // Ensure that the instance we get back from newFromText isn't the modified one.
1181 $title = Title::newFromText( 'Foo' );
1182 $this->assertNotSame( 'Xyzzy', $title->getFragment() );
1183 }
1184
1185 /**
1186 * @covers \MediaWiki\Title\Title::__clone
1187 */
1188 public function testClone() {
1189 // NOTE: must use newFromText to test behavior of internal instance cache (T281337)
1190 $title = Title::newFromText( 'Foo' );
1191
1192 $clone = clone $title;
1193 $clone->setFragment( '#Xyzzy' );
1194
1195 // Ensure that the instance we get back from newFromText is the original one
1196 $title2 = Title::newFromText( 'Foo' );
1197 $this->assertSame( $title, $title2 );
1198 }
1199
1200 public static function provideBaseTitleCases() {
1201 return [
1202 # Namespace, Title text, expected base
1203 [ NS_USER, 'John_Doe', 'John Doe' ],
1204 [ NS_USER, 'John_Doe/subOne/subTwo', 'John Doe/subOne' ],
1205 [ NS_USER, 'Foo / Bar / Baz', 'Foo / Bar ' ],
1206 [ NS_USER, 'Foo/', 'Foo' ],
1207 [ NS_USER, 'Foo/Bar/', 'Foo/Bar' ],
1208 [ NS_USER, '/', '/' ],
1209 [ NS_USER, '//', '/' ],
1210 [ NS_USER, '/oops/', '/oops' ],
1211 [ NS_USER, '/indeed', '/indeed' ],
1212 [ NS_USER, '//indeed', '/' ],
1213 [ NS_USER, '/Ramba/Zamba/Mamba/', '/Ramba/Zamba/Mamba' ],
1214 [ NS_USER, '//x//y//z//', '//x//y//z/' ],
1215 ];
1216 }
1217
1218 /**
1219 * @dataProvider provideBaseTitleCases
1220 * @covers \MediaWiki\Title\Title::getBaseText
1221 */
1222 public function testGetBaseText( $namespace, $title, $expected ) {
1223 $title = Title::makeTitle( $namespace, $title );
1224 $this->assertSame( $expected, $title->getBaseText() );
1225 }
1226
1227 /**
1228 * @dataProvider provideBaseTitleCases
1229 * @covers \MediaWiki\Title\Title::getBaseTitle
1230 */
1231 public function testGetBaseTitle( $namespace, $title, $expected ) {
1232 $title = Title::makeTitle( $namespace, $title );
1233 $base = $title->getBaseTitle();
1234 $this->assertTrue( $base->isValid() );
1235 $this->assertTrue(
1236 $base->equals( Title::makeTitleSafe( $title->getNamespace(), $expected ) )
1237 );
1238 }
1239
1240 /**
1241 * Don't explode on invalid titles (T290194).
1242 * @covers \MediaWiki\Title\Title::getBaseTitle
1243 */
1244 public function testGetBaseTitle_invalid() {
1245 $title = Title::makeTitle( -23, 'Test' );
1246 $base = $title->getBaseTitle();
1247 $this->assertSame( $title, $base );
1248 }
1249
1250 public static function provideRootTitleCases() {
1251 return [
1252 # Namespace, Title, expected base
1253 [ NS_USER, 'John_Doe', 'John Doe' ],
1254 [ NS_USER, 'John_Doe/subOne/subTwo', 'John Doe' ],
1255 [ NS_USER, 'Foo / Bar / Baz', 'Foo ' ],
1256 [ NS_USER, 'Foo/', 'Foo' ],
1257 [ NS_USER, 'Foo/Bar/', 'Foo' ],
1258 [ NS_USER, '/', '/' ],
1259 [ NS_USER, '//', '/' ],
1260 [ NS_USER, '/oops/', '/oops' ],
1261 [ NS_USER, '/Ramba/Zamba/Mamba/', '/Ramba' ],
1262 [ NS_USER, '//x//y//z//', '//x' ],
1263 [ NS_TALK, '////', '///' ],
1264 [ NS_TEMPLATE, '////', '///' ],
1265 [ NS_TEMPLATE, 'Foo////', 'Foo' ],
1266 [ NS_TEMPLATE, 'Foo////Bar', 'Foo' ],
1267 ];
1268 }
1269
1270 /**
1271 * @dataProvider provideRootTitleCases
1272 * @covers \MediaWiki\Title\Title::getRootText
1273 */
1274 public function testGetRootText( $namespace, $title, $expected ) {
1275 $title = Title::makeTitle( $namespace, $title );
1276 $this->assertEquals( $expected, $title->getRootText() );
1277 }
1278
1279 /**
1280 * @dataProvider provideRootTitleCases
1281 * @covers \MediaWiki\Title\Title::getRootTitle
1282 */
1283 public function testGetRootTitle( $namespace, $title, $expected ) {
1284 $title = Title::makeTitle( $namespace, $title );
1285 $root = $title->getRootTitle();
1286 $this->assertTrue( $root->isValid() );
1287 $this->assertTrue(
1288 $root->equals( Title::makeTitleSafe( $title->getNamespace(), $expected ) )
1289 );
1290 }
1291
1292 /**
1293 * Don't explode on invalid titles (T290194).
1294 * @covers \MediaWiki\Title\Title::getRootTitle
1295 */
1296 public function testGetRootTitle_invalid() {
1297 $title = Title::makeTitle( -23, 'Test' );
1298 $base = $title->getRootTitle();
1299 $this->assertSame( $title, $base );
1300 }
1301
1302 public static function provideSubpageTitleCases() {
1303 return [
1304 # Namespace, Title, expected base
1305 [ NS_USER, 'John_Doe', 'John Doe' ],
1306 [ NS_USER, 'John_Doe/subOne/subTwo', 'subTwo' ],
1307 [ NS_USER, 'John_Doe/subOne', 'subOne' ],
1308 [ NS_USER, '/', '/' ],
1309 [ NS_USER, '//', '' ],
1310 [ NS_USER, '/oops/', '' ],
1311 [ NS_USER, '/indeed', '/indeed' ],
1312 [ NS_USER, '//indeed', 'indeed' ],
1313 [ NS_USER, '/Ramba/Zamba/Mamba/', '' ],
1314 [ NS_USER, '//x//y//z//', '' ],
1315 [ NS_TEMPLATE, 'Foo', 'Foo' ],
1316 [ NS_CATEGORY, 'Foo', 'Foo' ],
1317 [ NS_MAIN, 'Bar', 'Bar' ],
1318 ];
1319 }
1320
1321 /**
1322 * @dataProvider provideSubpageTitleCases
1323 * @covers \MediaWiki\Title\Title::getSubpageText
1324 */
1325 public function testGetSubpageText( $namespace, $title, $expected ) {
1326 $title = Title::makeTitle( $namespace, $title );
1327 $this->assertEquals( $expected, $title->getSubpageText() );
1328 }
1329
1330 public static function provideGetTitleValue() {
1331 return [
1332 [ 'Foo' ],
1333 [ 'Foo#bar' ],
1334 [ 'User:Hansi_Maier' ],
1335 ];
1336 }
1337
1338 /**
1339 * @covers \MediaWiki\Title\Title::getTitleValue
1340 * @dataProvider provideGetTitleValue
1341 */
1342 public function testGetTitleValue( $text ) {
1343 $title = Title::newFromText( $text );
1344 $value = $title->getTitleValue();
1345
1346 $dbkey = str_replace( ' ', '_', $value->getText() );
1347 $this->assertEquals( $title->getDBkey(), $dbkey );
1348 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
1349 $this->assertEquals( $title->getFragment(), $value->getFragment() );
1350 }
1351
1352 public static function provideGetFragment() {
1353 return [
1354 [ 'Foo', '' ],
1355 [ 'Foo#bar', 'bar' ],
1356 [ 'Foo#bär', 'bär' ],
1357
1358 // Inner whitespace is normalized
1359 [ 'Foo#bar_bar', 'bar bar' ],
1360 [ 'Foo#bar bar', 'bar bar' ],
1361 [ 'Foo#bar bar', 'bar bar' ],
1362
1363 // Leading whitespace is kept, trailing whitespace is trimmed.
1364 // XXX: Is this really want we want?
1365 [ 'Foo#_bar_bar_', ' bar bar' ],
1366 [ 'Foo# bar bar ', ' bar bar' ],
1367 ];
1368 }
1369
1370 /**
1371 * @covers \MediaWiki\Title\Title::getFragment
1372 * @dataProvider provideGetFragment
1373 *
1374 * @param string $full
1375 * @param string $fragment
1376 */
1377 public function testGetFragment( $full, $fragment ) {
1378 $title = Title::newFromText( $full );
1379 $this->assertEquals( $fragment, $title->getFragment() );
1380 }
1381
1382 /**
1383 * @covers \MediaWiki\Title\Title::exists
1384 */
1385 public function testExists() {
1386 $title = Title::makeTitle( NS_PROJECT, 'New page' );
1387 $linkCache = $this->getServiceContainer()->getLinkCache();
1388
1389 $article = new Article( $title );
1390 $page = $article->getPage();
1391 $page->doUserEditContent(
1392 new WikitextContent( 'Some [[link]]' ),
1393 $this->getTestSysop()->getUser(),
1394 'summary'
1395 );
1396
1397 // Tell Title it doesn't know whether it exists
1398 $title->mArticleID = -1;
1399
1400 // Tell the link cache it doesn't exist when it really does
1401 $linkCache->clearLink( $title );
1402 $linkCache->addBadLinkObj( $title );
1403
1404 $this->assertFalse(
1405 $title->exists(),
1406 'exists() should rely on link cache unless READ_LATEST is used'
1407 );
1408 $this->assertTrue(
1409 $title->exists( IDBAccessObject::READ_LATEST ),
1410 'exists() should re-query database when READ_LATEST is used'
1411 );
1412 }
1413
1414 /**
1415 * @covers \MediaWiki\Title\Title::getArticleID
1416 * @covers \MediaWiki\Title\Title::getId
1417 */
1418 public function testGetArticleID() {
1419 $title = Title::makeTitle( NS_PROJECT, __METHOD__ );
1420 $this->assertSame( 0, $title->getArticleID() );
1421 $this->assertSame( $title->getArticleID(), $title->getId() );
1422
1423 $article = new Article( $title );
1424 $page = $article->getPage();
1425 $page->doUserEditContent(
1426 new WikitextContent( 'Some [[link]]' ),
1427 $this->getTestSysop()->getUser(),
1428 'summary'
1429 );
1430
1431 $this->assertGreaterThan( 0, $title->getArticleID() );
1432 $this->assertSame( $title->getArticleID(), $title->getId() );
1433 }
1434
1435 public static function provideNonProperTitles() {
1436 return [
1437 'section link' => [ Title::makeTitle( NS_MAIN, '', 'Section' ) ],
1438 'empty' => [ Title::makeTitle( NS_MAIN, '' ) ],
1439 'bad chars' => [ Title::makeTitle( NS_MAIN, '_|_' ) ],
1440 'empty in namspace' => [ Title::makeTitle( NS_USER, '' ) ],
1441 'special' => [ Title::makeTitle( NS_SPECIAL, 'RecentChanges' ) ],
1442 'interwiki' => [ Title::makeTitle( NS_MAIN, 'Test', '', 'acme' ) ],
1443 ];
1444 }
1445
1446 /**
1447 * @dataProvider provideNonProperTitles
1448 * @covers \MediaWiki\Title\Title::getArticleID
1449 */
1450 public function testGetArticleIDFromNonProperTitle( $title ) {
1451 // make sure nothing explodes
1452 $this->assertSame( 0, $title->getArticleID() );
1453 }
1454
1455 public static function provideCanHaveTalkPage() {
1456 return [
1457 'User page has talk page' => [
1458 Title::makeTitle( NS_USER, 'Jane' ), true
1459 ],
1460 'Talke page has talk page' => [
1461 Title::makeTitle( NS_TALK, 'Foo' ), true
1462 ],
1463 'Special page cannot have talk page' => [
1464 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
1465 ],
1466 'Virtual namespace cannot have talk page' => [
1467 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
1468 ],
1469 'Relative link has no talk page' => [
1470 Title::makeTitle( NS_MAIN, '', 'Kittens' ), false
1471 ],
1472 'Interwiki link has no talk page' => [
1473 Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ), false
1474 ],
1475 ];
1476 }
1477
1478 public static function provideGetTalkPage_good() {
1479 return [
1480 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
1481 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
1482 ];
1483 }
1484
1485 public static function provideGetTalkPage_bad() {
1486 return [
1487 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
1488 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
1489 ];
1490 }
1491
1492 public static function provideGetTalkPage_broken() {
1493 // These cases *should* be bad, but are not treated as bad, for backwards compatibility.
1494 // See discussion on T227817.
1495 return [
1496 [
1497 Title::makeTitle( NS_MAIN, '', 'Kittens' ),
1498 Title::makeTitle( NS_TALK, '' ), // Section is lost!
1499 false,
1500 ],
1501 [
1502 Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ),
1503 Title::makeTitle( NS_TALK, 'Kittens', '' ), // Interwiki prefix is lost!
1504 true,
1505 ],
1506 ];
1507 }
1508
1509 public static function provideGetSubjectPage_good() {
1510 return [
1511 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
1512 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
1513 ];
1514 }
1515
1516 public static function provideGetOtherPage_good() {
1517 return [
1518 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
1519 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_MAIN, 'Test' ) ],
1520 ];
1521 }
1522
1523 /**
1524 * @dataProvider provideCanHaveTalkPage
1525 * @covers \MediaWiki\Title\Title::canHaveTalkPage
1526 *
1527 * @param Title $title
1528 * @param bool $expected
1529 */
1530 public function testCanHaveTalkPage( Title $title, $expected ) {
1531 $actual = $title->canHaveTalkPage();
1532 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
1533 }
1534
1535 /**
1536 * @dataProvider provideGetTalkPage_good
1537 * @covers \MediaWiki\Title\Title::getTalkPageIfDefined
1538 */
1539 public function testGetTalkPage_good( Title $title, Title $expected ) {
1540 $actual = $title->getTalkPage();
1541 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
1542 }
1543
1544 /**
1545 * @dataProvider provideGetTalkPage_bad
1546 * @covers \MediaWiki\Title\Title::getTalkPageIfDefined
1547 */
1548 public function testGetTalkPage_bad( Title $title ) {
1549 $this->expectException( MWException::class );
1550 $title->getTalkPage();
1551 }
1552
1553 /**
1554 * @dataProvider provideGetTalkPage_broken
1555 * @covers \MediaWiki\Title\Title::getTalkPageIfDefined
1556 */
1557 public function testGetTalkPage_broken( Title $title, Title $expected, $valid ) {
1558 // NOTE: Eventually we want to throw in this case. But while there is still code that
1559 // calls this method without checking, we want to avoid fatal errors.
1560 // See discussion on T227817.
1561 $result = @$title->getTalkPage();
1562 $this->assertTrue( $expected->equals( $result ) );
1563 $this->assertSame( $valid, $result->isValid() );
1564 }
1565
1566 /**
1567 * @dataProvider provideGetTalkPage_good
1568 * @covers \MediaWiki\Title\Title::getTalkPageIfDefined
1569 */
1570 public function testGetTalkPageIfDefined_good( Title $title, Title $expected ) {
1571 $actual = $title->getTalkPageIfDefined();
1572 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
1573 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
1574 }
1575
1576 /**
1577 * @dataProvider provideGetTalkPage_bad
1578 * @covers \MediaWiki\Title\Title::getTalkPageIfDefined
1579 */
1580 public function testGetTalkPageIfDefined_bad( Title $title ) {
1581 $talk = $title->getTalkPageIfDefined();
1582 $this->assertNull(
1583 $talk,
1584 $title->getPrefixedDBkey()
1585 );
1586 }
1587
1588 /**
1589 * @dataProvider provideGetSubjectPage_good
1590 * @covers \MediaWiki\Title\Title::getSubjectPage
1591 */
1592 public function testGetSubjectPage_good( Title $title, Title $expected ) {
1593 $actual = $title->getSubjectPage();
1594 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
1595 }
1596
1597 /**
1598 * @dataProvider provideGetOtherPage_good
1599 * @covers \MediaWiki\Title\Title::getOtherPage
1600 */
1601 public function testGetOtherPage_good( Title $title, Title $expected ) {
1602 $actual = $title->getOtherPage();
1603 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
1604 }
1605
1606 /**
1607 * @dataProvider provideGetTalkPage_bad
1608 * @covers \MediaWiki\Title\Title::getOtherPage
1609 */
1610 public function testGetOtherPage_bad( Title $title ) {
1611 $this->expectException( MWException::class );
1612 $title->getOtherPage();
1613 }
1614
1615 public static function provideIsMovable() {
1616 return [
1617 'Simple title' => [ 'Foo', true ],
1618 // @todo Should these next two really be true?
1619 'Empty name' => [ Title::makeTitle( NS_MAIN, '' ), true ],
1620 'Invalid name' => [ Title::makeTitle( NS_MAIN, '<' ), true ],
1621 'Interwiki' => [ Title::makeTitle( NS_MAIN, 'Test', '', 'otherwiki' ), false ],
1622 'Special page' => [ 'Special:FooBar', false ],
1623 'Aborted by hook' => [ 'Hooked in place', false,
1624 static function ( Title $title, &$result ) {
1625 $result = false;
1626 }
1627 ],
1628 ];
1629 }
1630
1631 /**
1632 * @dataProvider provideIsMovable
1633 * @covers \MediaWiki\Title\Title::isMovable
1634 *
1635 * @param string|Title $title
1636 * @param bool $expected
1637 * @param callable|null $hookCallback For TitleIsMovable
1638 */
1639 public function testIsMovable( $title, $expected, $hookCallback = null ) {
1640 if ( $hookCallback ) {
1641 $this->setTemporaryHook( 'TitleIsMovable', $hookCallback );
1642 }
1643 if ( is_string( $title ) ) {
1644 $title = Title::newFromText( $title );
1645 }
1646
1647 $this->assertSame( $expected, $title->isMovable() );
1648 }
1649
1650 public static function provideGetPrefixedText() {
1651 return [
1652 // ns = 0
1653 [
1654 Title::makeTitle( NS_MAIN, 'Foo bar' ),
1655 'Foo bar'
1656 ],
1657 // ns = 2
1658 [
1659 Title::makeTitle( NS_USER, 'Foo bar' ),
1660 'User:Foo bar'
1661 ],
1662 // ns = 3
1663 [
1664 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
1665 'User talk:Foo bar'
1666 ],
1667 // fragment not included
1668 [
1669 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
1670 'Foo bar'
1671 ],
1672 // ns = -2
1673 [
1674 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
1675 'Media:Foo bar'
1676 ],
1677 // non-existent namespace
1678 [
1679 Title::makeTitle( 100777, 'Foo bar' ),
1680 'Special:Badtitle/NS100777:Foo bar'
1681 ],
1682 ];
1683 }
1684
1685 /**
1686 * @covers \MediaWiki\Title\Title::getPrefixedText
1687 * @dataProvider provideGetPrefixedText
1688 */
1689 public function testGetPrefixedText( Title $title, $expected ) {
1690 $this->assertEquals( $expected, $title->getPrefixedText() );
1691 }
1692
1693 public static function provideGetPrefixedDBKey() {
1694 return [
1695 // ns = 0
1696 [
1697 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
1698 'Foo_bar'
1699 ],
1700 // ns = 2
1701 [
1702 Title::makeTitle( NS_USER, 'Foo_bar' ),
1703 'User:Foo_bar'
1704 ],
1705 // ns = 3
1706 [
1707 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
1708 'User_talk:Foo_bar'
1709 ],
1710 // fragment not included
1711 [
1712 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
1713 'Foo_bar'
1714 ],
1715 // ns = -2
1716 [
1717 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
1718 'Media:Foo_bar'
1719 ],
1720 // non-existent namespace
1721 [
1722 Title::makeTitle( 100777, 'Foo_bar' ),
1723 'Special:Badtitle/NS100777:Foo_bar'
1724 ],
1725 ];
1726 }
1727
1728 /**
1729 * @covers \MediaWiki\Title\Title::getPrefixedDBKey
1730 * @dataProvider provideGetPrefixedDBKey
1731 */
1732 public function testGetPrefixedDBKey( Title $title, $expected ) {
1733 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1734 }
1735
1736 public static function provideGetFragmentForURL() {
1737 return [
1738 [ 'Foo', '' ],
1739 [ 'Foo#ümlåût', '#ümlåût' ],
1740 [ 'de:Foo#Bå®', '#Bå®' ],
1741 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1742 ];
1743 }
1744
1745 /**
1746 * @covers \MediaWiki\Title\Title::getFragmentForURL
1747 * @dataProvider provideGetFragmentForURL
1748 *
1749 * @param string $titleStr
1750 * @param string $expected
1751 */
1752 public function testGetFragmentForURL( $titleStr, $expected ) {
1753 $this->overrideConfigValues( [
1754 MainConfigNames::FragmentMode => [ 'html5' ],
1755 MainConfigNames::ExternalInterwikiFragmentMode => 'legacy',
1756 ] );
1757 // InterwikiLookup is configured in setUp()
1758
1759 $title = Title::newFromText( $titleStr );
1760 self::assertEquals( $expected, $title->getFragmentForURL() );
1761 }
1762
1763 public static function provideIsRawHtmlMessage() {
1764 return [
1765 [ 'MediaWiki:Foobar', true ],
1766 [ 'MediaWiki:Foo bar', true ],
1767 [ 'MediaWiki:Foo-bar', true ],
1768 [ 'MediaWiki:foo bar', true ],
1769 [ 'MediaWiki:foo-bar', true ],
1770 [ 'MediaWiki:foobar', true ],
1771 [ 'MediaWiki:some-other-message', false ],
1772 [ 'Main Page', false ],
1773 ];
1774 }
1775
1776 /**
1777 * @covers \MediaWiki\Title\Title::isRawHtmlMessage
1778 * @dataProvider provideIsRawHtmlMessage
1779 */
1780 public function testIsRawHtmlMessage( $textForm, $expected ) {
1781 $this->overrideConfigValue(
1782 MainConfigNames::RawHtmlMessages,
1783 [
1784 'foobar',
1785 'foo_bar',
1786 'foo-bar',
1787 ]
1788 );
1789
1790 $title = Title::newFromText( $textForm );
1791 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1792 }
1793
1794 /**
1795 * @covers \MediaWiki\Title\Title::newMainPage
1796 */
1797 public function testNewMainPage() {
1798 $mock = $this->createMock( MessageCache::class );
1799 $mock->method( 'get' )->willReturn( 'Foresheet' );
1800 $mock->method( 'transform' )->willReturn( 'Foresheet' );
1801
1802 $this->setService( 'MessageCache', $mock );
1803
1804 $this->assertSame(
1805 'Foresheet',
1806 Title::newMainPage()->getText()
1807 );
1808 }
1809
1810 /**
1811 * Regression test for T297571
1812 *
1813 * @covers \MediaWiki\Title\Title::newMainPage
1814 */
1815 public function testNewMainPageNoRecursion() {
1816 $mock = $this->createMock( MessageCache::class );
1817 $mock->method( 'get' )->willReturn( 'localtestiw:' );
1818 $mock->method( 'transform' )->willReturn( 'localtestiw:' );
1819 $this->setService( 'MessageCache', $mock );
1820
1821 $this->assertSame(
1822 'Main Page',
1823 Title::newMainPage()->getPrefixedText()
1824 );
1825 }
1826
1827 /**
1828 * @covers \MediaWiki\Title\Title::newMainPage
1829 */
1830 public function testNewMainPageWithLocal() {
1831 $local = $this->createMock( MessageLocalizer::class );
1832 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1833
1834 $this->assertSame(
1835 'Prime Article',
1836 Title::newMainPage( $local )->getText()
1837 );
1838 }
1839
1840 /**
1841 * @covers \MediaWiki\Title\Title::getTitleProtection
1842 */
1843 public function testGetTitleProtection() {
1844 $title = $this->getNonexistingTestPage( 'UTest1' )->getTitle();
1845 $this->hideDeprecated( Title::class . '::getTitleProtection' );
1846 $this->assertFalse( $title->getTitleProtection() );
1847 }
1848
1849 /**
1850 * @covers \MediaWiki\Title\Title::deleteTitleProtection
1851 */
1852 public function testDeleteTitleProtection() {
1853 $title = $this->getExistingTestPage( 'UTest1' )->getTitle();
1854 $this->hideDeprecated( Title::class . '::getTitleProtection' );
1855 $this->assertFalse( $title->getTitleProtection() );
1856 }
1857
1858 /**
1859 * @covers \MediaWiki\Page\PageStore::getSubpages
1860 */
1861 public function testGetSubpages() {
1862 $existingPage = $this->getExistingTestPage();
1863 $title = $existingPage->getTitle();
1864
1865 $this->overrideConfigValue(
1866 MainConfigNames::NamespacesWithSubpages,
1867 [ $title->getNamespace() => true ]
1868 );
1869
1870 $this->getExistingTestPage( $title->getSubpage( 'A' ) );
1871 $this->getExistingTestPage( $title->getSubpage( 'B' ) );
1872
1873 $notQuiteSubpageTitle = $title->getPrefixedDBkey() . 'X'; // no slash!
1874 $this->getExistingTestPage( $notQuiteSubpageTitle );
1875
1876 $subpages = iterator_to_array( $title->getSubpages() );
1877
1878 $this->assertCount( 2, $subpages );
1879 $this->assertCount( 1, $title->getSubpages( 1 ) );
1880 }
1881
1882 /**
1883 * @covers \MediaWiki\Page\PageStore::getSubpages
1884 */
1885 public function testGetSubpages_disabled() {
1886 $this->overrideConfigValue( MainConfigNames::NamespacesWithSubpages, [] );
1887
1888 $existingPage = $this->getExistingTestPage();
1889 $title = $existingPage->getTitle();
1890
1891 $this->getExistingTestPage( $title->getSubpage( 'A' ) );
1892 $this->getExistingTestPage( $title->getSubpage( 'B' ) );
1893
1894 $this->assertSame( [], $title->getSubpages() );
1895 }
1896
1897 public static function provideNamespaces() {
1898 // For ->isExternal() code path, construct a title with interwiki
1899 $title = Title::makeTitle( NS_FILE, 'test', 'frag', 'meta' );
1900 return [
1901 [ NS_MAIN, '' ],
1902 [ NS_FILE, 'File' ],
1903 [ NS_MEDIA, 'Media' ],
1904 [ NS_TALK, 'Talk' ],
1905 [ NS_CATEGORY, 'Category' ],
1906 [ $title, 'File' ],
1907 ];
1908 }
1909
1910 /**
1911 * @covers \MediaWiki\Title\Title::getNsText
1912 * @dataProvider provideNamespaces
1913 */
1914 public function testGetNsText( $namespace, $expected ) {
1915 if ( $namespace instanceof Title ) {
1916 $this->assertSame( $expected, $namespace->getNsText() );
1917 } else {
1918 $actual = Title::makeTitle( $namespace, 'Title_test' )->getNsText();
1919 $this->assertSame( $expected, $actual );
1920 }
1921 }
1922
1923 public static function providePagesWithSubjects() {
1924 return [
1925 [ Title::makeTitle( NS_USER_TALK, 'User_test' ), 'User' ],
1926 [ Title::makeTitle( NS_PROJECT, 'Test' ), 'Project' ],
1927 [ Title::makeTitle( NS_MAIN, 'Test' ), '' ],
1928 [ Title::makeTitle( NS_CATEGORY, 'Cat_test' ), 'Category' ],
1929 ];
1930 }
1931
1932 /**
1933 * @covers \MediaWiki\Title\Title::getSubjectNsText
1934 * @dataProvider providePagesWithSubjects
1935 */
1936 public function testGetSubjectNsText( Title $title, $expected ) {
1937 $actual = $title->getSubjectNsText();
1938 $this->assertSame( $expected, $actual );
1939 }
1940
1941 public static function provideTitlesWithTalkPages() {
1942 return [
1943 [ Title::makeTitle( NS_HELP, 'Help page' ), 'Help_talk' ],
1944 [ Title::newMainPage(), 'Talk' ],
1945 [ Title::makeTitle( NS_PROJECT, 'Test' ), 'Project_talk' ],
1946 ];
1947 }
1948
1949 /**
1950 * @covers \MediaWiki\Title\Title::getTalkNsText
1951 * @dataProvider provideTitlesWithTalkPages
1952 */
1953 public function testGetTalkNsText( Title $title, $expected ) {
1954 $actual = $title->getTalkNsText();
1955 $this->assertSame( $expected, $actual );
1956 }
1957
1958 /**
1959 * @covers \MediaWiki\Title\Title::isSpecial
1960 */
1961 public function testIsSpecial() {
1962 $title = Title::makeTitle( NS_SPECIAL, 'Recentchanges/Subpage' );
1963 $this->assertTrue( $title->isSpecial( 'Recentchanges' ) );
1964 }
1965
1966 /**
1967 * @covers \MediaWiki\Title\Title::isSpecial
1968 */
1969 public function testIsNotSpecial() {
1970 $title = Title::newFromText( 'NotSpecialPage/Subpage', NS_SPECIAL );
1971 $this->assertFalse( $title->isSpecial( 'NotSpecialPage' ) );
1972 }
1973
1974 /**
1975 * @covers \MediaWiki\Title\Title::isTalkPage
1976 */
1977 public function testIsTalkPage() {
1978 $title = Title::newFromText( 'Talk page', NS_TALK );
1979 $this->assertTrue( $title->isTalkPage() );
1980
1981 $titleNotInTalkNs = Title::makeTitle( NS_HELP, 'Test' );
1982 $this->assertFalse( $titleNotInTalkNs->isTalkPage() );
1983 }
1984
1985 /**
1986 * @coversNothing
1987 */
1988 public function testGetBacklinkCache() {
1989 $blcFactory = $this->getServiceContainer()->getBacklinkCacheFactory();
1990 $backlinkCache = $blcFactory->getBacklinkCache( Title::makeTitle( NS_FILE, 'Test' ) );
1991 $this->assertInstanceOf( BacklinkCache::class, $backlinkCache );
1992 }
1993
1994 public static function provideNsWithSubpagesSupport() {
1995 return [
1996 [ NS_HELP, 'Mainhelp', 'Mainhelp/Subhelp' ],
1997 [ NS_USER, 'Mainuser', 'Mainuser/Subuser' ],
1998 [ NS_TALK, 'Maintalk', 'Maintalk/Subtalk' ],
1999 [ NS_PROJECT, 'Mainproject', 'Mainproject/Subproject' ],
2000 ];
2001 }
2002
2003 /**
2004 * @covers \MediaWiki\Title\Title::isSubpage
2005 * @covers \MediaWiki\Title\Title::isSubpageOf
2006 * @dataProvider provideNsWithSubpagesSupport
2007 */
2008 public function testIsSubpageOfWithNamespacesSubpages( $namespace, $pageName, $subpageName ) {
2009 $page = Title::makeTitle( $namespace, $pageName, '', 'meta' );
2010 $subPage = Title::makeTitle( $namespace, $subpageName, '', 'meta' );
2011
2012 $this->assertTrue( $subPage->isSubpageOf( $page ) );
2013 $this->assertTrue( $subPage->isSubpage() );
2014 }
2015
2016 public static function provideNsWithNoSubpages() {
2017 return [
2018 [ NS_CATEGORY, 'Maincat', 'Maincat/Subcat' ],
2019 [ NS_MAIN, 'Mainpage', 'Mainpage/Subpage' ]
2020 ];
2021 }
2022
2023 /**
2024 * @covers \MediaWiki\Title\Title::isSubpage
2025 * @covers \MediaWiki\Title\Title::isSubpageOf
2026 * @dataProvider provideNsWithNoSubpages
2027 */
2028 public function testIsSubpageOfWithoutNamespacesSubpages( $namespace, $pageName, $subpageName ) {
2029 $page = Title::makeTitle( $namespace, $pageName, '', 'meta' );
2030 $subPage = Title::makeTitle( $namespace, $subpageName, '', 'meta' );
2031
2032 $this->assertFalse( $page->isSubpageOf( $page ) );
2033 $this->assertFalse( $subPage->isSubpage() );
2034 }
2035
2036 public static function provideTitleEditURLs() {
2037 return [
2038 [ Title::makeTitle( NS_MAIN, 'Title' ), '/w/index.php?title=Title&action=edit' ],
2039 [ Title::makeTitle( NS_HELP, 'Test', '', 'mw' ), '' ],
2040 [ Title::makeTitle( NS_HELP, 'Test' ), '/w/index.php?title=Help:Test&action=edit' ],
2041 ];
2042 }
2043
2044 /**
2045 * @covers \MediaWiki\Title\Title::getEditURL
2046 * @dataProvider provideTitleEditURLs
2047 */
2048 public function testGetEditURL( Title $title, $expected ) {
2049 $actual = $title->getEditURL();
2050 $this->assertSame( $expected, $actual );
2051 }
2052
2053 public static function provideTitleEditURLsWithActionPaths() {
2054 return [
2055 [ Title::newFromText( 'Title', NS_MAIN ), '/wiki/edit/Title' ],
2056 [ Title::makeTitle( NS_HELP, 'Test', '', 'mw' ), '' ],
2057 [ Title::newFromText( 'Test', NS_HELP ), '/wiki/edit/Help:Test' ],
2058 ];
2059 }
2060
2061 /**
2062 * @covers \MediaWiki\Title\Title::getEditURL
2063 * @dataProvider provideTitleEditURLsWithActionPaths
2064 */
2065 public function testGetEditUrlWithActionPaths( Title $title, $expected ) {
2066 $this->overrideConfigValue( MainConfigNames::ActionPaths, [ 'edit' => '/wiki/edit/$1' ] );
2067 $actual = $title->getEditURL();
2068 $this->assertSame( $expected, $actual );
2069 }
2070
2071 /**
2072 * @covers \MediaWiki\Title\Title::isMainPage
2073 * @covers \MediaWiki\Title\Title::equals
2074 */
2075 public function testIsMainPage() {
2076 $this->assertTrue( Title::newMainPage()->isMainPage() );
2077 }
2078
2079 /**
2080 * @covers \MediaWiki\Title\Title::isMainPage
2081 * @covers \MediaWiki\Title\Title::equals
2082 * @dataProvider provideMainPageTitles
2083 */
2084 public function testIsNotMainPage( Title $title, $expected ) {
2085 $this->assertSame( $expected, $title->isMainPage() );
2086 }
2087
2088 public static function provideMainPageTitles() {
2089 return [
2090 [ Title::makeTitle( NS_MAIN, 'Test' ), false ],
2091 [ Title::makeTitle( NS_CATEGORY, 'mw:Category' ), false ],
2092 ];
2093 }
2094
2095 /**
2096 * @covers \MediaWiki\Title\Title::getPrefixedURL
2097 * @covers \MediaWiki\Title\Title::prefix
2098 * @dataProvider provideDataForTestGetPrefixedURL
2099 */
2100 public function testGetPrefixedURL( Title $title, $expected ) {
2101 $actual = $title->getPrefixedURL();
2102
2103 $this->assertSame( $expected, $actual );
2104 }
2105
2106 public static function provideDataForTestGetPrefixedURL() {
2107 return [
2108 [ Title::makeTitle( NS_FILE, 'Title' ), 'File:Title' ],
2109 [ Title::makeTitle( NS_MEDIA, 'Title' ), 'Media:Title' ],
2110 [ Title::makeTitle( NS_CATEGORY, 'Title' ), 'Category:Title' ],
2111 [ Title::makeTitle( NS_FILE, 'Title with spaces' ), 'File:Title_with_spaces' ],
2112 [
2113 Title::makeTitle( NS_FILE, 'Title with spaces', '', 'mw' ),
2114 'mw:File:Title_with_spaces'
2115 ],
2116 ];
2117 }
2118
2119 /**
2120 * @covers \MediaWiki\Title\Title::__toString
2121 */
2122 public function testToString() {
2123 $title = Title::makeTitle( NS_USER, 'User test' );
2124
2125 $this->assertSame( 'User:User test', (string)$title );
2126 }
2127
2128 /**
2129 * @covers \MediaWiki\Title\Title::getFullText
2130 * @dataProvider provideDataForTestGetFullText
2131 */
2132 public function testGetFullText( Title $title, $expected ) {
2133 $actual = $title->getFullText();
2134
2135 $this->assertSame( $expected, $actual );
2136 }
2137
2138 public static function provideDataForTestGetFullText() {
2139 return [
2140 [ Title::makeTitle( NS_TALK, 'Test' ), 'Talk:Test' ],
2141 [ Title::makeTitle( NS_HELP, 'Test', 'frag' ), 'Help:Test#frag' ],
2142 [ Title::makeTitle( NS_TALK, 'Test', 'frag', 'phab' ), 'phab:Talk:Test#frag' ],
2143 ];
2144 }
2145
2146 public static function provideIsSamePageAs() {
2147 $title = Title::makeTitle( 0, 'Foo' );
2148 $title->resetArticleID( 1 );
2149 yield '(PageIdentityValue) same text, title has ID 0' => [
2150 $title,
2151 new PageIdentityValue( 1, 0, 'Foo', PageIdentity::LOCAL ),
2152 true
2153 ];
2154
2155 $title = Title::makeTitle( 1, 'Bar_Baz' );
2156 $title->resetArticleID( 0 );
2157 yield '(PageIdentityValue) same text, PageIdentityValue has ID 0' => [
2158 $title,
2159 new PageIdentityValue( 0, 1, 'Bar_Baz', PageIdentity::LOCAL ),
2160 true
2161 ];
2162
2163 $title = Title::makeTitle( 0, 'Foo' );
2164 $title->resetArticleID( 0 );
2165 yield '(PageIdentityValue) different text, both IDs are 0' => [
2166 $title,
2167 new PageIdentityValue( 0, 0, 'Foozz', PageIdentity::LOCAL ),
2168 false
2169 ];
2170
2171 $title = Title::makeTitle( 0, 'Foo' );
2172 $title->resetArticleID( 0 );
2173 yield '(PageIdentityValue) different namespace' => [
2174 $title,
2175 new PageIdentityValue( 0, 1, 'Foo', PageIdentity::LOCAL ),
2176 false
2177 ];
2178
2179 $title = Title::makeTitle( 0, 'Foo', '' );
2180 $title->resetArticleID( 1 );
2181 yield '(PageIdentityValue) different wiki, different ID' => [
2182 $title,
2183 new PageIdentityValue( 1, 0, 'Foo', 'bar' ),
2184 false
2185 ];
2186
2187 $title = Title::makeTitle( 0, 'Foo', '' );
2188 $title->resetArticleID( 0 );
2189 yield '(PageIdentityValue) different wiki, both IDs are 0' => [
2190 $title,
2191 new PageIdentityValue( 0, 0, 'Foo', 'bar' ),
2192 false
2193 ];
2194 }
2195
2196 /**
2197 * @covers \MediaWiki\Title\Title::isSamePageAs
2198 * @dataProvider provideIsSamePageAs
2199 */
2200 public function testIsSamePageAs( Title $firstValue, $secondValue, $expectedSame ) {
2201 $this->assertSame(
2202 $expectedSame,
2203 $firstValue->isSamePageAs( $secondValue )
2204 );
2205 }
2206
2207 /**
2208 * @covers \MediaWiki\Title\Title::getArticleID
2209 * @covers \MediaWiki\Title\Title::getId
2210 * @covers \MediaWiki\Title\Title::getLength
2211 * @covers \MediaWiki\Title\Title::getLatestRevID
2212 * @covers \MediaWiki\Title\Title::exists
2213 * @covers \MediaWiki\Title\Title::isNewPage
2214 * @covers \MediaWiki\Title\Title::isRedirect
2215 * @covers \MediaWiki\Title\Title::getTouched
2216 * @covers \MediaWiki\Title\Title::getContentModel
2217 * @covers \MediaWiki\Title\Title::getFieldFromPageStore
2218 */
2219 public function testGetFieldsOfNonExistingPage() {
2220 $title = Title::makeTitle( NS_MAIN, 'ThisDoesNotExist-92347852349' );
2221
2222 $this->assertSame( 0, $title->getArticleID() );
2223 $this->assertSame( 0, $title->getId() );
2224 $this->assertSame( 0, $title->getLength() );
2225 $this->assertSame( 0, $title->getLatestRevID() );
2226 $this->assertFalse( $title->exists() );
2227 $this->assertFalse( $title->isNewPage() );
2228 $this->assertFalse( $title->isRedirect() );
2229 $this->assertFalse( $title->getTouched() );
2230 $this->assertNotEmpty( $title->getContentModel() );
2231 }
2232
2233 /**
2234 * @covers \MediaWiki\Title\Title::getDefaultSystemMessage
2235 */
2236 public function testGetDefaultSystemMessage() {
2237 $title = Title::makeTitle( NS_MEDIAWIKI, 'Logouttext' );
2238
2239 $this->assertInstanceOf( Message::class, $title->getDefaultSystemMessage() );
2240 $this->assertStringContainsString( 'You are now logged out', $title->getDefaultMessageText() );
2241 }
2242
2243 /**
2244 * @covers \MediaWiki\Title\Title::getDefaultSystemMessage
2245 */
2246 public function testGetDefaultSystemMessageReturnsNull() {
2247 $title = Title::makeTitle( NS_MAIN, 'Some title' );
2248
2249 $this->assertNull( $title->getDefaultSystemMessage() );
2250 }
2251
2252 }