Debian Codegraph @

tests/phpunit/includes/title/NamespaceInfoTest.php

raw symbols SCIP
1 <?php
2 /**
3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
5 * @file
6 */
7
8 use MediaWiki\Config\ServiceOptions;
9 use MediaWiki\HookContainer\HookContainer;
10 use MediaWiki\Linker\LinkTarget;
11 use MediaWiki\MainConfigNames;
12 use MediaWiki\Title\NamespaceInfo;
13 use MediaWiki\Title\Title;
14 use MediaWiki\Title\TitleValue;
15
16 class NamespaceInfoTest extends MediaWikiIntegrationTestCase {
17 use TestAllServiceOptionsUsed;
18
19 private const TEST_EXT_NAMESPACES = [ NS_MAIN => 'No effect', NS_TALK => 'No effect', 12345 => 'Extended' ];
20
21 /**********************************************************************************************
22 * Shared code
23 * %{
24 */
25
26 private const DEFAULT_OPTIONS = [
27 MainConfigNames::CanonicalNamespaceNames => [
28 NS_TALK => 'Talk',
29 NS_USER => 'User',
30 NS_USER_TALK => 'User_talk',
31 NS_SPECIAL => 'Special',
32 NS_MEDIA => 'Media',
33 ],
34 MainConfigNames::CapitalLinkOverrides => [],
35 MainConfigNames::CapitalLinks => true,
36 MainConfigNames::ContentNamespaces => [ NS_MAIN ],
37 MainConfigNames::ExtraNamespaces => [],
38 MainConfigNames::ExtraSignatureNamespaces => [],
39 MainConfigNames::NamespaceContentModels => [],
40 MainConfigNames::NamespacesWithSubpages => [
41 NS_TALK => true,
42 NS_USER => true,
43 NS_USER_TALK => true,
44 ],
45 MainConfigNames::NonincludableNamespaces => [],
46 ];
47
48 /**
49 * @return HookContainer
50 */
51 private function getHookContainer() {
52 return $this->getServiceContainer()->getHookContainer();
53 }
54
55 private function newObj( array $options = [], array $extensionNamespaces = [] ): NamespaceInfo {
56 return new NamespaceInfo(
57 new LoggedServiceOptions(
58 self::$serviceOptionsAccessLog,
59 NamespaceInfo::CONSTRUCTOR_OPTIONS,
60 $options, self::DEFAULT_OPTIONS
61 ),
62 $this->getHookContainer(),
63 $extensionNamespaces,
64 []
65 );
66 }
67
68 // %} End shared code
69
70 /**********************************************************************************************
71 * Basic methods
72 * %{
73 */
74
75 /**
76 * @covers \MediaWiki\Title\NamespaceInfo::__construct
77 * @dataProvider provideConstructor
78 * @param ServiceOptions $options
79 * @param string|null $expectedExceptionText
80 */
81 public function testConstructor( ServiceOptions $options, $expectedExceptionText = null ) {
82 if ( $expectedExceptionText !== null ) {
83 $this->expectException( \Wikimedia\Assert\PreconditionException::class );
84 $this->expectExceptionMessage( $expectedExceptionText );
85 }
86 new NamespaceInfo( $options, $this->getHookContainer(), [], [] );
87 $this->assertTrue( true );
88 }
89
90 public static function provideConstructor() {
91 return [
92 [ new ServiceOptions( NamespaceInfo::CONSTRUCTOR_OPTIONS, self::DEFAULT_OPTIONS ) ],
93 [ new ServiceOptions( [], [] ), 'Required options missing: ' ],
94 [ new ServiceOptions(
95 array_merge( NamespaceInfo::CONSTRUCTOR_OPTIONS, [ 'invalid' ] ),
96 self::DEFAULT_OPTIONS,
97 [ 'invalid' => '' ]
98 ), 'Unsupported options passed: invalid' ],
99 ];
100 }
101
102 /**
103 * @dataProvider provideIsMovable
104 * @covers \MediaWiki\Title\NamespaceInfo::isMovable
105 *
106 * @param bool $expected
107 * @param int $ns
108 */
109 public function testIsMovable( $expected, $ns ) {
110 $obj = $this->newObj();
111 $this->assertSame( $expected, $obj->isMovable( $ns ) );
112 }
113
114 public static function provideIsMovable() {
115 return [
116 'Main' => [ true, NS_MAIN ],
117 'Talk' => [ true, NS_TALK ],
118 'Special' => [ false, NS_SPECIAL ],
119 'Nonexistent even namespace' => [ true, 1234 ],
120 'Nonexistent odd namespace' => [ true, 12345 ],
121
122 'Media' => [ false, NS_MEDIA ],
123 'File' => [ true, NS_FILE ],
124 ];
125 }
126
127 /**
128 * @param int $ns
129 * @param bool $expected
130 * @dataProvider provideIsSubject
131 * @covers \MediaWiki\Title\NamespaceInfo::isSubject
132 */
133 public function testIsSubject( $ns, $expected ) {
134 $this->assertSame( $expected, $this->newObj()->isSubject( $ns ) );
135 }
136
137 /**
138 * @param int $ns
139 * @param bool $expected
140 * @dataProvider provideIsSubject
141 * @covers \MediaWiki\Title\NamespaceInfo::isTalk
142 */
143 public function testIsTalk( $ns, $expected ) {
144 $this->assertSame( !$expected, $this->newObj()->isTalk( $ns ) );
145 }
146
147 public static function provideIsSubject() {
148 return [
149 // Special namespaces
150 [ NS_MEDIA, true ],
151 [ NS_SPECIAL, true ],
152
153 // Subject pages
154 [ NS_MAIN, true ],
155 [ NS_USER, true ],
156 [ 100, true ],
157
158 // Talk pages
159 [ NS_TALK, false ],
160 [ NS_USER_TALK, false ],
161 [ 101, false ],
162 ];
163 }
164
165 /**
166 * @covers \MediaWiki\Title\NamespaceInfo::exists
167 * @dataProvider provideExists
168 * @param int $ns
169 * @param bool $expected
170 */
171 public function testExists( $ns, $expected ) {
172 $this->assertSame( $expected, $this->newObj()->exists( $ns ) );
173 }
174
175 public static function provideExists() {
176 return [
177 'Main' => [ NS_MAIN, true ],
178 'Talk' => [ NS_TALK, true ],
179 'Media' => [ NS_MEDIA, true ],
180 'Special' => [ NS_SPECIAL, true ],
181 'Nonexistent' => [ 12345, false ],
182 'Negative nonexistent' => [ -12345, false ],
183 ];
184 }
185
186 /**
187 * Note if we add a namespace registration system with keys like 'MAIN'
188 * we should add tests here for equivalence on things like 'MAIN' == 0
189 * and 'MAIN' == NS_MAIN.
190 * @covers \MediaWiki\Title\NamespaceInfo::equals
191 */
192 public function testEquals() {
193 $obj = $this->newObj();
194 $this->assertTrue( $obj->equals( NS_MAIN, NS_MAIN ) );
195 $this->assertTrue( $obj->equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
196 $this->assertTrue( $obj->equals( NS_USER, NS_USER ) );
197 $this->assertTrue( $obj->equals( NS_USER, 2 ) );
198 $this->assertTrue( $obj->equals( NS_USER_TALK, NS_USER_TALK ) );
199 $this->assertTrue( $obj->equals( NS_SPECIAL, NS_SPECIAL ) );
200 $this->assertFalse( $obj->equals( NS_MAIN, NS_TALK ) );
201 $this->assertFalse( $obj->equals( NS_USER, NS_USER_TALK ) );
202 $this->assertFalse( $obj->equals( NS_PROJECT, NS_TEMPLATE ) );
203 }
204
205 /**
206 * @param int $ns1
207 * @param int $ns2
208 * @param bool $expected
209 * @dataProvider provideSubjectEquals
210 * @covers \MediaWiki\Title\NamespaceInfo::subjectEquals
211 */
212 public function testSubjectEquals( $ns1, $ns2, $expected ) {
213 $this->assertSame( $expected, $this->newObj()->subjectEquals( $ns1, $ns2 ) );
214 }
215
216 public static function provideSubjectEquals() {
217 return [
218 [ NS_MAIN, NS_MAIN, true ],
219 // In case we make NS_MAIN 'MAIN'
220 [ NS_MAIN, 0, true ],
221 [ NS_USER, NS_USER, true ],
222 [ NS_USER, 2, true ],
223 [ NS_USER_TALK, NS_USER_TALK, true ],
224 [ NS_SPECIAL, NS_SPECIAL, true ],
225 [ NS_MAIN, NS_TALK, true ],
226 [ NS_USER, NS_USER_TALK, true ],
227
228 [ NS_PROJECT, NS_TEMPLATE, false ],
229 [ NS_SPECIAL, NS_MAIN, false ],
230 [ NS_MEDIA, NS_SPECIAL, false ],
231 [ NS_SPECIAL, NS_MEDIA, false ],
232 ];
233 }
234
235 /**
236 * @dataProvider provideHasTalkNamespace
237 * @covers \MediaWiki\Title\NamespaceInfo::hasTalkNamespace
238 *
239 * @param int $ns
240 * @param bool $expected
241 */
242 public function testHasTalkNamespace( $ns, $expected ) {
243 $this->assertSame( $expected, $this->newObj()->hasTalkNamespace( $ns ) );
244 }
245
246 public static function provideHasTalkNamespace() {
247 return [
248 [ NS_MEDIA, false ],
249 [ NS_SPECIAL, false ],
250
251 [ NS_MAIN, true ],
252 [ NS_TALK, true ],
253 [ NS_USER, true ],
254 [ NS_USER_TALK, true ],
255
256 [ 100, true ],
257 [ 101, true ],
258 ];
259 }
260
261 /**
262 * @param int $ns
263 * @param bool $expected
264 * @param array $contentNamespaces
265 * @covers \MediaWiki\Title\NamespaceInfo::isContent
266 * @dataProvider provideIsContent
267 */
268 public function testIsContent( $ns, $expected, $contentNamespaces = [ NS_MAIN ] ) {
269 $obj = $this->newObj( [ MainConfigNames::ContentNamespaces => $contentNamespaces ] );
270 $this->assertSame( $expected, $obj->isContent( $ns ) );
271 }
272
273 public static function provideIsContent() {
274 return [
275 [ NS_MAIN, true ],
276 [ NS_MEDIA, false ],
277 [ NS_SPECIAL, false ],
278 [ NS_TALK, false ],
279 [ NS_USER, false ],
280 [ NS_CATEGORY, false ],
281 [ 100, false ],
282 [ 100, true, [ NS_MAIN, 100, 252 ] ],
283 [ 252, true, [ NS_MAIN, 100, 252 ] ],
284 [ NS_MAIN, true, [ NS_MAIN, 100, 252 ] ],
285 // NS_MAIN is always content
286 [ NS_MAIN, true, [] ],
287 ];
288 }
289
290 /**
291 * @dataProvider provideWantSignatures
292 * @covers \MediaWiki\Title\NamespaceInfo::wantSignatures
293 *
294 * @param int $index
295 * @param bool $expected
296 */
297 public function testWantSignatures( $index, $expected ) {
298 $this->assertSame( $expected, $this->newObj()->wantSignatures( $index ) );
299 }
300
301 public static function provideWantSignatures() {
302 return [
303 'Main' => [ NS_MAIN, false ],
304 'Talk' => [ NS_TALK, true ],
305 'User' => [ NS_USER, false ],
306 'User talk' => [ NS_USER_TALK, true ],
307 'Special' => [ NS_SPECIAL, false ],
308 'Media' => [ NS_MEDIA, false ],
309 'Nonexistent talk' => [ 12345, true ],
310 'Nonexistent subject' => [ 123456, false ],
311 'Nonexistent negative odd' => [ -12345, false ],
312 ];
313 }
314
315 /**
316 * @dataProvider provideWantSignatures_ExtraSignatureNamespaces
317 * @covers \MediaWiki\Title\NamespaceInfo::wantSignatures
318 *
319 * @param int $index
320 * @param int $expected
321 */
322 public function testWantSignatures_ExtraSignatureNamespaces( $index, $expected ) {
323 $obj = $this->newObj( [ MainConfigNames::ExtraSignatureNamespaces =>
324 [ NS_MAIN, NS_USER, NS_SPECIAL, NS_MEDIA, 123456, -12345 ] ] );
325 $this->assertSame( $expected, $obj->wantSignatures( $index ) );
326 }
327
328 public function provideWantSignatures_ExtraSignatureNamespaces() {
329 $ret = array_map(
330 static function ( $arr ) {
331 // We've added all these as extra signature namespaces, so expect true
332 return [ $arr[0], true ];
333 },
334 $this->provideWantSignatures()
335 );
336
337 // Add one more that's false
338 $ret['Another nonexistent subject'] = [ 12345678, false ];
339 return $ret;
340 }
341
342 /**
343 * @param int $ns
344 * @param bool $expected
345 * @covers \MediaWiki\Title\NamespaceInfo::isWatchable
346 * @dataProvider provideIsWatchable
347 */
348 public function testIsWatchable( $ns, $expected ) {
349 $this->assertSame( $expected, $this->newObj()->isWatchable( $ns ) );
350 }
351
352 public static function provideIsWatchable() {
353 return [
354 // Specials namespaces are not watchable
355 [ NS_MEDIA, false ],
356 [ NS_SPECIAL, false ],
357
358 // Core defined namespaces are watchables
359 [ NS_MAIN, true ],
360 [ NS_TALK, true ],
361
362 // Additional, user defined namespaces are watchables
363 [ 100, true ],
364 [ 101, true ],
365 ];
366 }
367
368 /**
369 * @param int $ns
370 * @param int $expected
371 * @param array|null $namespacesWithSubpages To pass to constructor
372 * @covers \MediaWiki\Title\NamespaceInfo::hasSubpages
373 * @dataProvider provideHasSubpages
374 */
375 public function testHasSubpages( $ns, $expected, ?array $namespacesWithSubpages = null ) {
376 $obj = $this->newObj( $namespacesWithSubpages
377 ? [ MainConfigNames::NamespacesWithSubpages => $namespacesWithSubpages ]
378 : [] );
379 $this->assertSame( $expected, $obj->hasSubpages( $ns ) );
380 }
381
382 public static function provideHasSubpages() {
383 return [
384 // Special namespaces:
385 [ NS_MEDIA, false ],
386 [ NS_SPECIAL, false ],
387
388 // Namespaces without subpages
389 [ NS_MAIN, false ],
390 [ NS_MAIN, true, [ NS_MAIN => true ] ],
391 [ NS_MAIN, false, [ NS_MAIN => false ] ],
392
393 // Some namespaces with subpages
394 [ NS_TALK, true ],
395 [ NS_USER, true ],
396 [ NS_USER_TALK, true ],
397 ];
398 }
399
400 /**
401 * @dataProvider provideGetContentNamespaces
402 * @covers \MediaWiki\Title\NamespaceInfo::getContentNamespaces
403 */
404 public function testGetContentNamespaces( $contentNamespaces, array $expected ) {
405 $obj = $this->newObj( [ MainConfigNames::ContentNamespaces => $contentNamespaces ] );
406 $this->assertSame( $expected, $obj->getContentNamespaces() );
407 }
408
409 public static function provideGetContentNamespaces() {
410 return [
411 // Non-array
412 [ '', [ NS_MAIN ] ],
413 [ false, [ NS_MAIN ] ],
414 [ null, [ NS_MAIN ] ],
415 [ 5, [ NS_MAIN ] ],
416
417 // Empty array
418 [ [], [ NS_MAIN ] ],
419
420 // NS_MAIN is forced to be content even if unwanted
421 [ [ NS_USER, NS_CATEGORY ], [ NS_MAIN, NS_USER, NS_CATEGORY ] ],
422
423 // In other cases, return as-is
424 [ [ NS_MAIN ], [ NS_MAIN ] ],
425 [ [ NS_MAIN, NS_USER, NS_CATEGORY ], [ NS_MAIN, NS_USER, NS_CATEGORY ] ],
426 ];
427 }
428
429 /**
430 * @covers \MediaWiki\Title\NamespaceInfo::getSubjectNamespaces
431 */
432 public function testGetSubjectNamespaces() {
433 $subjectsNS = $this->newObj()->getSubjectNamespaces();
434 $this->assertContains( NS_MAIN, $subjectsNS,
435 "Talk namespaces should have NS_MAIN" );
436 $this->assertNotContains( NS_TALK, $subjectsNS,
437 "Talk namespaces should have NS_TALK" );
438
439 $this->assertNotContains( NS_MEDIA, $subjectsNS,
440 "Talk namespaces should not have NS_MEDIA" );
441 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
442 "Talk namespaces should not have NS_SPECIAL" );
443 }
444
445 /**
446 * @covers \MediaWiki\Title\NamespaceInfo::getTalkNamespaces
447 */
448 public function testGetTalkNamespaces() {
449 $talkNS = $this->newObj()->getTalkNamespaces();
450 $this->assertContains( NS_TALK, $talkNS,
451 "Subject namespaces should have NS_TALK" );
452 $this->assertNotContains( NS_MAIN, $talkNS,
453 "Subject namespaces should not have NS_MAIN" );
454
455 $this->assertNotContains( NS_MEDIA, $talkNS,
456 "Subject namespaces should not have NS_MEDIA" );
457 $this->assertNotContains( NS_SPECIAL, $talkNS,
458 "Subject namespaces should not have NS_SPECIAL" );
459 }
460
461 /**
462 * @param int $ns
463 * @param bool $expected
464 * @param bool $capitalLinks To pass to constructor
465 * @param array $capitalLinkOverrides To pass to constructor
466 * @dataProvider provideIsCapitalized
467 * @covers \MediaWiki\Title\NamespaceInfo::isCapitalized
468 */
469 public function testIsCapitalized(
470 $ns, $expected, $capitalLinks = true, array $capitalLinkOverrides = []
471 ) {
472 $obj = $this->newObj( [
473 MainConfigNames::CapitalLinks => $capitalLinks,
474 MainConfigNames::CapitalLinkOverrides => $capitalLinkOverrides,
475 ] );
476 $this->assertSame( $expected, $obj->isCapitalized( $ns ) );
477 }
478
479 public static function provideIsCapitalized() {
480 return [
481 // Test default settings
482 [ NS_PROJECT, true ],
483 [ NS_PROJECT_TALK, true ],
484 [ NS_MEDIA, true ],
485 [ NS_FILE, true ],
486
487 // Always capitalized no matter what
488 [ NS_SPECIAL, true, false ],
489 [ NS_USER, true, false ],
490 [ NS_MEDIAWIKI, true, false ],
491
492 // Even with an override too
493 [ NS_SPECIAL, true, false, [ NS_SPECIAL => false ] ],
494 [ NS_USER, true, false, [ NS_USER => false ] ],
495 [ NS_MEDIAWIKI, true, false, [ NS_MEDIAWIKI => false ] ],
496
497 // Overrides work for other namespaces
498 [ NS_PROJECT, false, true, [ NS_PROJECT => false ] ],
499 [ NS_PROJECT, true, false, [ NS_PROJECT => true ] ],
500
501 // NS_MEDIA is treated like NS_FILE, and ignores NS_MEDIA overrides
502 [ NS_MEDIA, false, true, [ NS_FILE => false, NS_MEDIA => true ] ],
503 [ NS_MEDIA, true, false, [ NS_FILE => true, NS_MEDIA => false ] ],
504 [ NS_FILE, false, true, [ NS_FILE => false, NS_MEDIA => true ] ],
505 [ NS_FILE, true, false, [ NS_FILE => true, NS_MEDIA => false ] ],
506 ];
507 }
508
509 /**
510 * @covers \MediaWiki\Title\NamespaceInfo::hasGenderDistinction
511 */
512 public function testHasGenderDistinction() {
513 $obj = $this->newObj();
514
515 // Namespaces with gender distinctions
516 $this->assertTrue( $obj->hasGenderDistinction( NS_USER ) );
517 $this->assertTrue( $obj->hasGenderDistinction( NS_USER_TALK ) );
518
519 // Other ones, "genderless"
520 $this->assertFalse( $obj->hasGenderDistinction( NS_MEDIA ) );
521 $this->assertFalse( $obj->hasGenderDistinction( NS_SPECIAL ) );
522 $this->assertFalse( $obj->hasGenderDistinction( NS_MAIN ) );
523 $this->assertFalse( $obj->hasGenderDistinction( NS_TALK ) );
524 }
525
526 /**
527 * @covers \MediaWiki\Title\NamespaceInfo::isNonincludable
528 */
529 public function testIsNonincludable() {
530 $obj = $this->newObj( [ MainConfigNames::NonincludableNamespaces => [ NS_USER ] ] );
531 $this->assertTrue( $obj->isNonincludable( NS_USER ) );
532 $this->assertFalse( $obj->isNonincludable( NS_TEMPLATE ) );
533 }
534
535 /**
536 * @dataProvider provideGetNamespaceContentModel
537 * @covers \MediaWiki\Title\NamespaceInfo::getNamespaceContentModel
538 *
539 * @param int $ns
540 * @param string $expected
541 */
542 public function testGetNamespaceContentModel( $ns, $expected ) {
543 $obj = $this->newObj( [ MainConfigNames::NamespaceContentModels =>
544 [ NS_USER => CONTENT_MODEL_WIKITEXT, 123 => CONTENT_MODEL_JSON, 1234 => 'abcdef' ],
545 ] );
546 $this->assertSame( $expected, $obj->getNamespaceContentModel( $ns ) );
547 }
548
549 public static function provideGetNamespaceContentModel() {
550 return [
551 [ NS_MAIN, null ],
552 [ NS_TALK, null ],
553 [ NS_USER, CONTENT_MODEL_WIKITEXT ],
554 [ NS_USER_TALK, null ],
555 [ NS_SPECIAL, null ],
556 [ 122, null ],
557 [ 123, CONTENT_MODEL_JSON ],
558 [ 1234, 'abcdef' ],
559 [ 1235, null ],
560 ];
561 }
562
563 /**
564 * @dataProvider provideGetCategoryLinkType
565 * @covers \MediaWiki\Title\NamespaceInfo::getCategoryLinkType
566 *
567 * @param int $ns
568 * @param string $expected
569 */
570 public function testGetCategoryLinkType( $ns, $expected ) {
571 $this->assertSame( $expected, $this->newObj()->getCategoryLinkType( $ns ) );
572 }
573
574 public static function provideGetCategoryLinkType() {
575 return [
576 [ NS_MAIN, 'page' ],
577 [ NS_TALK, 'page' ],
578 [ NS_USER, 'page' ],
579 [ NS_USER_TALK, 'page' ],
580
581 [ NS_FILE, 'file' ],
582 [ NS_FILE_TALK, 'page' ],
583
584 [ NS_CATEGORY, 'subcat' ],
585 [ NS_CATEGORY_TALK, 'page' ],
586
587 [ 100, 'page' ],
588 [ 101, 'page' ],
589 ];
590 }
591
592 // %} End basic methods
593
594 /**********************************************************************************************
595 * getSubject/Talk/Associated
596 * %{
597 */
598
599 /**
600 * @dataProvider provideSubjectTalk
601 * @covers \MediaWiki\Title\NamespaceInfo::getSubject
602 * @covers \MediaWiki\Title\NamespaceInfo::getSubjectPage
603 * @covers \MediaWiki\Title\NamespaceInfo::isMethodValidFor
604 * @covers \MediaWiki\Title\Title::getSubjectPage
605 *
606 * @param int $subject
607 * @param int $talk
608 */
609 public function testGetSubject( $subject, $talk ) {
610 $obj = $this->newObj();
611 $this->assertSame( $subject, $obj->getSubject( $subject ) );
612 $this->assertSame( $subject, $obj->getSubject( $talk ) );
613
614 $subjectTitleVal = new TitleValue( $subject, 'A' );
615 $talkTitleVal = new TitleValue( $talk, 'A' );
616 // Object will be the same one passed in if it's a subject, different but equal object if
617 // it's talk
618 $this->assertSame( $subjectTitleVal, $obj->getSubjectPage( $subjectTitleVal ) );
619 $this->assertEquals( $subjectTitleVal, $obj->getSubjectPage( $talkTitleVal ) );
620
621 $subjectTitle = Title::makeTitle( $subject, 'A' );
622 $talkTitle = Title::makeTitle( $talk, 'A' );
623 $this->assertSame( $subjectTitle, $subjectTitle->getSubjectPage() );
624 $this->assertEquals( $subjectTitle, $talkTitle->getSubjectPage() );
625 }
626
627 /**
628 * @dataProvider provideSpecialNamespaces
629 * @covers \MediaWiki\Title\NamespaceInfo::getSubject
630 * @covers \MediaWiki\Title\NamespaceInfo::getSubjectPage
631 *
632 * @param int $ns
633 */
634 public function testGetSubject_special( $ns ) {
635 $obj = $this->newObj();
636 $this->assertSame( $ns, $obj->getSubject( $ns ) );
637
638 $title = new TitleValue( $ns, 'A' );
639 $this->assertSame( $title, $obj->getSubjectPage( $title ) );
640 }
641
642 /**
643 * @dataProvider provideSubjectTalk
644 * @covers \MediaWiki\Title\NamespaceInfo::getTalk
645 * @covers \MediaWiki\Title\NamespaceInfo::getTalkPage
646 * @covers \MediaWiki\Title\NamespaceInfo::isMethodValidFor
647 * @covers \MediaWiki\Title\Title::getTalkPage
648 *
649 * @param int $subject
650 * @param int $talk
651 */
652 public function testGetTalk( $subject, $talk ) {
653 $obj = $this->newObj();
654 $this->assertSame( $talk, $obj->getTalk( $subject ) );
655 $this->assertSame( $talk, $obj->getTalk( $talk ) );
656
657 $subjectTitleVal = new TitleValue( $subject, 'A' );
658 $talkTitleVal = new TitleValue( $talk, 'A' );
659 // Object will be the same one passed in if it's a talk, different but equal object if it's
660 // subject
661 $this->assertEquals( $talkTitleVal, $obj->getTalkPage( $subjectTitleVal ) );
662 $this->assertSame( $talkTitleVal, $obj->getTalkPage( $talkTitleVal ) );
663
664 $subjectTitle = Title::makeTitle( $subject, 'A' );
665 $talkTitle = Title::makeTitle( $talk, 'A' );
666 $this->assertEquals( $talkTitle, $subjectTitle->getTalkPage() );
667 $this->assertSame( $talkTitle, $talkTitle->getTalkPage() );
668 }
669
670 /**
671 * @dataProvider provideSpecialNamespaces
672 * @covers \MediaWiki\Title\NamespaceInfo::getTalk
673 * @covers \MediaWiki\Title\NamespaceInfo::isMethodValidFor
674 *
675 * @param int $ns
676 */
677 public function testGetTalk_special( $ns ) {
678 $this->expectException( MWException::class );
679 $this->expectExceptionMessage(
680 "NamespaceInfo::getTalk does not make any sense for given namespace $ns"
681 );
682 $this->newObj()->getTalk( $ns );
683 }
684
685 /**
686 * @dataProvider provideSpecialNamespaces
687 * @covers \MediaWiki\Title\NamespaceInfo::getAssociated
688 * @covers \MediaWiki\Title\NamespaceInfo::isMethodValidFor
689 *
690 * @param int $ns
691 */
692 public function testGetAssociated_special( $ns ) {
693 $this->expectException( MWException::class );
694 $this->expectExceptionMessage(
695 "NamespaceInfo::getAssociated does not make any sense for given namespace $ns"
696 );
697 $this->newObj()->getAssociated( $ns );
698 }
699
700 public static function provideCanHaveTalkPage() {
701 return [
702 [ new TitleValue( NS_MAIN, 'Test' ), true ],
703 [ new TitleValue( NS_TALK, 'Test' ), true ],
704 [ new TitleValue( NS_USER, 'Test' ), true ],
705 [ new TitleValue( NS_SPECIAL, 'Test' ), false ],
706 [ new TitleValue( NS_MEDIA, 'Test' ), false ],
707 [ new TitleValue( NS_MAIN, '', 'Kittens' ), false ],
708 [ new TitleValue( NS_MAIN, 'Kittens', '', 'acme' ), false ],
709 ];
710 }
711
712 /**
713 * @dataProvider provideCanHaveTalkPage
714 * @covers \MediaWiki\Title\NamespaceInfo::canHaveTalkPage
715 */
716 public function testCanHaveTalkPage( LinkTarget $t, $expected ) {
717 $actual = $this->newObj()->canHaveTalkPage( $t );
718 $this->assertEquals( $expected, $actual, $t->getDBkey() );
719 }
720
721 public static function provideGetTalkPage_good() {
722 return [
723 [ new TitleValue( NS_MAIN, 'Test' ), new TitleValue( NS_TALK, 'Test' ) ],
724 [ new TitleValue( NS_TALK, 'Test' ), new TitleValue( NS_TALK, 'Test' ) ],
725 [ new TitleValue( NS_USER, 'Test' ), new TitleValue( NS_USER_TALK, 'Test' ) ],
726 ];
727 }
728
729 /**
730 * @dataProvider provideGetTalkPage_good
731 * @covers \MediaWiki\Title\NamespaceInfo::getTalk
732 * @covers \MediaWiki\Title\NamespaceInfo::getTalkPage
733 * @covers \MediaWiki\Title\NamespaceInfo::isMethodValidFor
734 */
735 public function testGetTalkPage_good( LinkTarget $t, LinkTarget $expected ) {
736 $actual = $this->newObj()->getTalkPage( $t );
737 $this->assertEquals( $expected, $actual, $t->getDBkey() );
738 }
739
740 public static function provideGetTalkPage_bad() {
741 return [
742 [ new TitleValue( NS_SPECIAL, 'Test' ) ],
743 [ new TitleValue( NS_MEDIA, 'Test' ) ],
744 [ new TitleValue( NS_MAIN, '', 'Kittens' ) ],
745 [ new TitleValue( NS_MAIN, 'Kittens', '', 'acme' ) ],
746 ];
747 }
748
749 /**
750 * @dataProvider provideGetTalkPage_bad
751 * @covers \MediaWiki\Title\NamespaceInfo::getTalk
752 * @covers \MediaWiki\Title\NamespaceInfo::getTalkPage
753 * @covers \MediaWiki\Title\NamespaceInfo::isMethodValidFor
754 */
755 public function testGetTalkPage_bad( LinkTarget $t ) {
756 $this->expectException( MWException::class );
757 $this->newObj()->getTalkPage( $t );
758 }
759
760 /**
761 * @dataProvider provideGetTalkPage_bad
762 * @covers \MediaWiki\Title\NamespaceInfo::getAssociated
763 * @covers \MediaWiki\Title\NamespaceInfo::getAssociatedPage
764 * @covers \MediaWiki\Title\NamespaceInfo::isMethodValidFor
765 */
766 public function testGetAssociatedPage_bad( LinkTarget $t ) {
767 $this->expectException( MWException::class );
768 $this->newObj()->getAssociatedPage( $t );
769 }
770
771 /**
772 * @dataProvider provideSubjectTalk
773 * @covers \MediaWiki\Title\NamespaceInfo::getAssociated
774 * @covers \MediaWiki\Title\NamespaceInfo::getAssociatedPage
775 * @covers \MediaWiki\Title\Title::getOtherPage
776 *
777 * @param int $subject
778 * @param int $talk
779 */
780 public function testGetAssociated( $subject, $talk ) {
781 $obj = $this->newObj();
782 $this->assertSame( $talk, $obj->getAssociated( $subject ) );
783 $this->assertSame( $subject, $obj->getAssociated( $talk ) );
784
785 $subjectTitle = new TitleValue( $subject, 'A' );
786 $talkTitle = new TitleValue( $talk, 'A' );
787 // Object will not be the same
788 $this->assertEquals( $talkTitle, $obj->getAssociatedPage( $subjectTitle ) );
789 $this->assertEquals( $subjectTitle, $obj->getAssociatedPage( $talkTitle ) );
790
791 $subjectTitle = Title::makeTitle( $subject, 'A' );
792 $talkTitle = Title::makeTitle( $talk, 'A' );
793 $this->assertEquals( $talkTitle, $subjectTitle->getOtherPage() );
794 $this->assertEquals( $subjectTitle, $talkTitle->getOtherPage() );
795 }
796
797 public static function provideSubjectTalk() {
798 return [
799 // Format: [ subject, talk ]
800 'Main/talk' => [ NS_MAIN, NS_TALK ],
801 'User/user talk' => [ NS_USER, NS_USER_TALK ],
802 'Unknown namespaces also supported' => [ 106, 107 ],
803 ];
804 }
805
806 public static function provideSpecialNamespaces() {
807 return [
808 'Special' => [ NS_SPECIAL ],
809 'Media' => [ NS_MEDIA ],
810 'Unknown negative index' => [ -613 ],
811 ];
812 }
813
814 // %} End getSubject/Talk/Associated
815
816 /**********************************************************************************************
817 * Canonical namespaces
818 * %{
819 */
820
821 // Default canonical namespaces
822 // %{
823 private function getDefaultNamespaces() {
824 return [ NS_MAIN => '' ] + self::DEFAULT_OPTIONS['CanonicalNamespaceNames'];
825 }
826
827 /**
828 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalNamespaces
829 */
830 public function testGetCanonicalNamespaces() {
831 $this->assertSame(
832 $this->getDefaultNamespaces(),
833 $this->newObj()->getCanonicalNamespaces()
834 );
835 }
836
837 /**
838 * @dataProvider provideGetCanonicalName
839 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalName
840 *
841 * @param int $index
842 * @param string|bool $expected
843 */
844 public function testGetCanonicalName( $index, $expected ) {
845 $this->assertSame( $expected, $this->newObj()->getCanonicalName( $index ) );
846 }
847
848 public static function provideGetCanonicalName() {
849 return [
850 'Main' => [ NS_MAIN, '' ],
851 'Talk' => [ NS_TALK, 'Talk' ],
852 'With underscore not space' => [ NS_USER_TALK, 'User_talk' ],
853 'Special' => [ NS_SPECIAL, 'Special' ],
854 'Nonexistent' => [ 12345, false ],
855 'Nonexistent negative' => [ -12345, false ],
856 ];
857 }
858
859 /**
860 * @dataProvider provideGetCanonicalIndex
861 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalIndex
862 *
863 * @param string $name
864 * @param int|null $expected
865 */
866 public function testGetCanonicalIndex( $name, $expected ) {
867 $this->assertSame( $expected, $this->newObj()->getCanonicalIndex( $name ) );
868 }
869
870 public static function provideGetCanonicalIndex() {
871 return [
872 'Main' => [ '', NS_MAIN ],
873 'Talk' => [ 'talk', NS_TALK ],
874 'Not lowercase' => [ 'Talk', null ],
875 'With underscore' => [ 'user_talk', NS_USER_TALK ],
876 'Space is not recognized for underscore' => [ 'user talk', null ],
877 '0' => [ '0', null ],
878 ];
879 }
880
881 /**
882 * @covers \MediaWiki\Title\NamespaceInfo::getValidNamespaces
883 */
884 public function testGetValidNamespaces() {
885 $this->assertSame(
886 [ NS_MAIN, NS_TALK, NS_USER, NS_USER_TALK ],
887 $this->newObj()->getValidNamespaces()
888 );
889 }
890
891 // %} End default canonical namespaces
892
893 // No canonical namespace names
894 // %{
895
896 /**
897 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalNamespaces
898 */
899 public function testGetCanonicalNamespaces_NoCanonicalNamespaceNames() {
900 $obj = $this->newObj( [ MainConfigNames::CanonicalNamespaceNames => [] ] );
901
902 $this->assertSame( [ NS_MAIN => '' ], $obj->getCanonicalNamespaces() );
903 }
904
905 /**
906 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalName
907 */
908 public function testGetCanonicalName_NoCanonicalNamespaceNames() {
909 $obj = $this->newObj( [ MainConfigNames::CanonicalNamespaceNames => [] ] );
910
911 $this->assertSame( '', $obj->getCanonicalName( NS_MAIN ) );
912 $this->assertFalse( $obj->getCanonicalName( NS_TALK ) );
913 }
914
915 /**
916 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalIndex
917 */
918 public function testGetCanonicalIndex_NoCanonicalNamespaceNames() {
919 $obj = $this->newObj( [ MainConfigNames::CanonicalNamespaceNames => [] ] );
920
921 $this->assertSame( NS_MAIN, $obj->getCanonicalIndex( '' ) );
922 $this->assertNull( $obj->getCanonicalIndex( 'talk' ) );
923 }
924
925 /**
926 * @covers \MediaWiki\Title\NamespaceInfo::getValidNamespaces
927 */
928 public function testGetValidNamespaces_NoCanonicalNamespaceNames() {
929 $obj = $this->newObj( [ MainConfigNames::CanonicalNamespaceNames => [] ] );
930
931 $this->assertSame( [ NS_MAIN ], $obj->getValidNamespaces() );
932 }
933
934 // %} End no canonical namespace names
935
936 // Test extension namespaces
937 // %{
938
939 /**
940 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalNamespaces
941 */
942 public function testGetCanonicalNamespaces_ExtensionNamespaces() {
943 $this->assertSame(
944 $this->getDefaultNamespaces() + [ 12345 => 'Extended' ],
945 $this->newObj( [], self::TEST_EXT_NAMESPACES )->getCanonicalNamespaces()
946 );
947 }
948
949 /**
950 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalName
951 */
952 public function testGetCanonicalName_ExtensionNamespaces() {
953 $obj = $this->newObj( [], self::TEST_EXT_NAMESPACES );
954
955 $this->assertSame( '', $obj->getCanonicalName( NS_MAIN ) );
956 $this->assertSame( 'Talk', $obj->getCanonicalName( NS_TALK ) );
957 $this->assertSame( 'Extended', $obj->getCanonicalName( 12345 ) );
958 }
959
960 /**
961 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalIndex
962 */
963 public function testGetCanonicalIndex_ExtensionNamespaces() {
964 $obj = $this->newObj( [], self::TEST_EXT_NAMESPACES );
965
966 $this->assertSame( NS_MAIN, $obj->getCanonicalIndex( '' ) );
967 $this->assertSame( NS_TALK, $obj->getCanonicalIndex( 'talk' ) );
968 $this->assertSame( 12345, $obj->getCanonicalIndex( 'extended' ) );
969 }
970
971 /**
972 * @covers \MediaWiki\Title\NamespaceInfo::getValidNamespaces
973 */
974 public function testGetValidNamespaces_ExtensionNamespaces() {
975 $this->assertSame(
976 [ NS_MAIN, NS_TALK, NS_USER, NS_USER_TALK, 12345 ],
977 $this->newObj( [], self::TEST_EXT_NAMESPACES )->getValidNamespaces()
978 );
979 }
980
981 // %} End extension namespaces
982
983 // Hook namespaces
984 // %{
985
986 /**
987 * @return array Expected canonical namespaces
988 */
989 private function setupHookNamespaces() {
990 $callback =
991 static function ( &$canonicalNamespaces ) {
992 $canonicalNamespaces[NS_MAIN] = 'Main';
993 unset( $canonicalNamespaces[NS_MEDIA] );
994 $canonicalNamespaces[123456] = 'Hooked';
995 };
996 $this->setTemporaryHook( 'CanonicalNamespaces', $callback );
997 $expected = $this->getDefaultNamespaces();
998 ( $callback )( $expected );
999 return $expected;
1000 }
1001
1002 /**
1003 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalNamespaces
1004 */
1005 public function testGetCanonicalNamespaces_HookNamespaces() {
1006 $expected = $this->setupHookNamespaces();
1007
1008 $this->assertSame( $expected, $this->newObj()->getCanonicalNamespaces() );
1009 }
1010
1011 /**
1012 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalName
1013 */
1014 public function testGetCanonicalName_HookNamespaces() {
1015 $this->setupHookNamespaces();
1016 $obj = $this->newObj();
1017
1018 $this->assertSame( 'Main', $obj->getCanonicalName( NS_MAIN ) );
1019 $this->assertFalse( $obj->getCanonicalName( NS_MEDIA ) );
1020 $this->assertSame( 'Hooked', $obj->getCanonicalName( 123456 ) );
1021 }
1022
1023 /**
1024 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalIndex
1025 */
1026 public function testGetCanonicalIndex_HookNamespaces() {
1027 $this->setupHookNamespaces();
1028 $obj = $this->newObj();
1029
1030 $this->assertSame( NS_MAIN, $obj->getCanonicalIndex( 'main' ) );
1031 $this->assertNull( $obj->getCanonicalIndex( 'media' ) );
1032 $this->assertSame( 123456, $obj->getCanonicalIndex( 'hooked' ) );
1033 }
1034
1035 /**
1036 * @covers \MediaWiki\Title\NamespaceInfo::getValidNamespaces
1037 */
1038 public function testGetValidNamespaces_HookNamespaces() {
1039 $this->setupHookNamespaces();
1040
1041 $this->assertSame(
1042 [ NS_MAIN, NS_TALK, NS_USER, NS_USER_TALK, 123456 ],
1043 $this->newObj()->getValidNamespaces()
1044 );
1045 }
1046
1047 // %} End hook namespaces
1048
1049 // Extra namespaces
1050 // %{
1051
1052 /**
1053 * @return NamespaceInfo
1054 */
1055 private function setupExtraNamespaces() {
1056 return $this->newObj( [ MainConfigNames::ExtraNamespaces =>
1057 [ NS_MAIN => 'No effect', NS_TALK => 'No effect', 1234567 => 'Extra' ]
1058 ] );
1059 }
1060
1061 /**
1062 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalNamespaces
1063 */
1064 public function testGetCanonicalNamespaces_ExtraNamespaces() {
1065 $this->assertSame(
1066 $this->getDefaultNamespaces() + [ 1234567 => 'Extra' ],
1067 $this->setupExtraNamespaces()->getCanonicalNamespaces()
1068 );
1069 }
1070
1071 /**
1072 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalName
1073 */
1074 public function testGetCanonicalName_ExtraNamespaces() {
1075 $obj = $this->setupExtraNamespaces();
1076
1077 $this->assertSame( '', $obj->getCanonicalName( NS_MAIN ) );
1078 $this->assertSame( 'Talk', $obj->getCanonicalName( NS_TALK ) );
1079 $this->assertSame( 'Extra', $obj->getCanonicalName( 1234567 ) );
1080 }
1081
1082 /**
1083 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalIndex
1084 */
1085 public function testGetCanonicalIndex_ExtraNamespaces() {
1086 $obj = $this->setupExtraNamespaces();
1087
1088 $this->assertNull( $obj->getCanonicalIndex( 'no effect' ) );
1089 $this->assertNull( $obj->getCanonicalIndex( 'no_effect' ) );
1090 $this->assertSame( 1234567, $obj->getCanonicalIndex( 'extra' ) );
1091 }
1092
1093 /**
1094 * @covers \MediaWiki\Title\NamespaceInfo::getValidNamespaces
1095 */
1096 public function testGetValidNamespaces_ExtraNamespaces() {
1097 $this->assertSame(
1098 [ NS_MAIN, NS_TALK, NS_USER, NS_USER_TALK, 1234567 ],
1099 $this->setupExtraNamespaces()->getValidNamespaces()
1100 );
1101 }
1102
1103 // %} End extra namespaces
1104
1105 // Canonical namespace caching
1106 // %{
1107
1108 /**
1109 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalNamespaces
1110 */
1111 public function testGetCanonicalNamespaces_caching() {
1112 $obj = $this->newObj();
1113
1114 // This should cache the values
1115 $obj->getCanonicalNamespaces();
1116
1117 // Now try to alter them through nefarious means
1118 $this->setupHookNamespaces();
1119
1120 // Should have no effect
1121 $this->assertSame( $this->getDefaultNamespaces(), $obj->getCanonicalNamespaces() );
1122 }
1123
1124 /**
1125 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalName
1126 */
1127 public function testGetCanonicalName_caching() {
1128 $obj = $this->newObj();
1129
1130 // This should cache the values
1131 $obj->getCanonicalName( NS_MAIN );
1132
1133 // Now try to alter them through nefarious means
1134 $this->setupHookNamespaces();
1135
1136 // Should have no effect
1137 $this->assertSame( '', $obj->getCanonicalName( NS_MAIN ) );
1138 $this->assertSame( 'Media', $obj->getCanonicalName( NS_MEDIA ) );
1139 $this->assertFalse( $obj->getCanonicalName( 12345 ) );
1140 $this->assertFalse( $obj->getCanonicalName( 123456 ) );
1141 }
1142
1143 /**
1144 * @covers \MediaWiki\Title\NamespaceInfo::getCanonicalIndex
1145 */
1146 public function testGetCanonicalIndex_caching() {
1147 $obj = $this->newObj();
1148
1149 // This should cache the values
1150 $obj->getCanonicalIndex( '' );
1151
1152 // Now try to alter them through nefarious means
1153 $this->setupHookNamespaces();
1154
1155 // Should have no effect
1156 $this->assertSame( NS_MAIN, $obj->getCanonicalIndex( '' ) );
1157 $this->assertSame( NS_MEDIA, $obj->getCanonicalIndex( 'media' ) );
1158 $this->assertNull( $obj->getCanonicalIndex( 'extended' ) );
1159 $this->assertNull( $obj->getCanonicalIndex( 'hooked' ) );
1160 }
1161
1162 /**
1163 * @covers \MediaWiki\Title\NamespaceInfo::getValidNamespaces
1164 */
1165 public function testGetValidNamespaces_caching() {
1166 $obj = $this->newObj();
1167
1168 // This should cache the values
1169 $obj->getValidNamespaces();
1170
1171 // Now try to alter through nefarious means
1172 $this->setupHookNamespaces();
1173
1174 // Should have no effect
1175 $this->assertSame(
1176 [ NS_MAIN, NS_TALK, NS_USER, NS_USER_TALK ],
1177 $obj->getValidNamespaces()
1178 );
1179 }
1180
1181 // %} End canonical namespace caching
1182
1183 // Miscellaneous
1184 // %{
1185
1186 /**
1187 * @dataProvider provideGetValidNamespaces_misc
1188 * @covers \MediaWiki\Title\NamespaceInfo::getValidNamespaces
1189 *
1190 * @param array $namespaces List of namespace indices to return from getCanonicalNamespaces()
1191 * (list is overwritten by a hook, so NS_MAIN doesn't have to be present)
1192 * @param array $expected
1193 */
1194 public function testGetValidNamespaces_misc( array $namespaces, array $expected ) {
1195 // Each namespace's name is just its index
1196 $this->setTemporaryHook( 'CanonicalNamespaces',
1197 static function ( &$canonicalNamespaces ) use ( $namespaces ) {
1198 $canonicalNamespaces = array_combine( $namespaces, $namespaces );
1199 }
1200 );
1201 $this->assertSame( $expected, $this->newObj()->getValidNamespaces() );
1202 }
1203
1204 public static function provideGetValidNamespaces_misc() {
1205 return [
1206 'Out of order (T109137)' => [ [ 1, 0 ], [ 0, 1 ] ],
1207 'Alphabetical order' => [ [ 10, 2 ], [ 2, 10 ] ],
1208 'Negative' => [ [ -1000, -500, -2, 0 ], [ 0 ] ],
1209 ];
1210 }
1211
1212 // %} End miscellaneous
1213 // %} End canonical namespaces
1214
1215 /**
1216 * @coversNothing
1217 */
1218 public function testAllServiceOptionsUsed() {
1219 $this->assertAllServiceOptionsUsed();
1220 }
1221 }
1222
1223 /**
1224 * For really cool vim folding this needs to be at the end:
1225 * vim: foldmarker=%{,%} foldmethod=marker
1226 */