3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
8 use MediaWiki\MediaWikiServices
;
10 class NamespaceInfoTest
extends MediaWikiTestCase
{
12 /** @var NamespaceInfo */
15 protected function setUp() {
18 $this->setMwGlobals( [
19 'wgContentNamespaces' => [ NS_MAIN
],
20 'wgNamespacesWithSubpages' => [
25 'wgCapitalLinks' => true,
26 'wgCapitalLinkOverrides' => [],
27 'wgNonincludableNamespaces' => [],
30 $this->obj
= MediaWikiServices
::getInstance()->getNamespaceInfo();
34 * @todo Write more texts, handle $wgAllowImageMoving setting
35 * @covers NamespaceInfo::isMovable
37 public function testIsMovable() {
38 $this->assertFalse( $this->obj
->isMovable( NS_SPECIAL
) );
41 private function assertIsSubject( $ns ) {
42 $this->assertTrue( $this->obj
->isSubject( $ns ) );
45 private function assertIsNotSubject( $ns ) {
46 $this->assertFalse( $this->obj
->isSubject( $ns ) );
50 * Please make sure to change testIsTalk() if you change the assertions below
51 * @covers NamespaceInfo::isSubject
53 public function testIsSubject() {
55 $this->assertIsSubject( NS_MEDIA
);
56 $this->assertIsSubject( NS_SPECIAL
);
59 $this->assertIsSubject( NS_MAIN
);
60 $this->assertIsSubject( NS_USER
);
61 $this->assertIsSubject( 100 ); # user defined
64 $this->assertIsNotSubject( NS_TALK
);
65 $this->assertIsNotSubject( NS_USER_TALK
);
66 $this->assertIsNotSubject( 101 ); # user defined
69 private function assertIsTalk( $ns ) {
70 $this->assertTrue( $this->obj
->isTalk( $ns ) );
73 private function assertIsNotTalk( $ns ) {
74 $this->assertFalse( $this->obj
->isTalk( $ns ) );
78 * Reverse of testIsSubject().
79 * Please update testIsSubject() if you change assertions below
80 * @covers NamespaceInfo::isTalk
82 public function testIsTalk() {
84 $this->assertIsNotTalk( NS_MEDIA
);
85 $this->assertIsNotTalk( NS_SPECIAL
);
88 $this->assertIsNotTalk( NS_MAIN
);
89 $this->assertIsNotTalk( NS_USER
);
90 $this->assertIsNotTalk( 100 ); # user defined
93 $this->assertIsTalk( NS_TALK
);
94 $this->assertIsTalk( NS_USER_TALK
);
95 $this->assertIsTalk( 101 ); # user defined
99 * @covers NamespaceInfo::getSubject
101 public function testGetSubject() {
102 // Special namespaces are their own subjects
103 $this->assertEquals( NS_MEDIA
, $this->obj
->getSubject( NS_MEDIA
) );
104 $this->assertEquals( NS_SPECIAL
, $this->obj
->getSubject( NS_SPECIAL
) );
106 $this->assertEquals( NS_MAIN
, $this->obj
->getSubject( NS_TALK
) );
107 $this->assertEquals( NS_USER
, $this->obj
->getSubject( NS_USER_TALK
) );
111 * Regular getTalk() calls
112 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
113 * the function testGetTalkExceptions()
114 * @covers NamespaceInfo::getTalk
116 public function testGetTalk() {
117 $this->assertEquals( NS_TALK
, $this->obj
->getTalk( NS_MAIN
) );
118 $this->assertEquals( NS_TALK
, $this->obj
->getTalk( NS_TALK
) );
119 $this->assertEquals( NS_USER_TALK
, $this->obj
->getTalk( NS_USER
) );
120 $this->assertEquals( NS_USER_TALK
, $this->obj
->getTalk( NS_USER_TALK
) );
124 * Exceptions with getTalk()
125 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
126 * @expectedException MWException
127 * @covers NamespaceInfo::getTalk
129 public function testGetTalkExceptionsForNsMedia() {
130 $this->assertNull( $this->obj
->getTalk( NS_MEDIA
) );
134 * Exceptions with getTalk()
135 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
136 * @expectedException MWException
137 * @covers NamespaceInfo::getTalk
139 public function testGetTalkExceptionsForNsSpecial() {
140 $this->assertNull( $this->obj
->getTalk( NS_SPECIAL
) );
144 * Regular getAssociated() calls
145 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
146 * the function testGetAssociatedExceptions()
147 * @covers NamespaceInfo::getAssociated
149 public function testGetAssociated() {
150 $this->assertEquals( NS_TALK
, $this->obj
->getAssociated( NS_MAIN
) );
151 $this->assertEquals( NS_MAIN
, $this->obj
->getAssociated( NS_TALK
) );
154 # ## Exceptions with getAssociated()
155 # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
156 # ## an exception for them.
158 * @expectedException MWException
159 * @covers NamespaceInfo::getAssociated
161 public function testGetAssociatedExceptionsForNsMedia() {
162 $this->assertNull( $this->obj
->getAssociated( NS_MEDIA
) );
166 * @expectedException MWException
167 * @covers NamespaceInfo::getAssociated
169 public function testGetAssociatedExceptionsForNsSpecial() {
170 $this->assertNull( $this->obj
->getAssociated( NS_SPECIAL
) );
174 * Note if we add a namespace registration system with keys like 'MAIN'
175 * we should add tests here for equivilance on things like 'MAIN' == 0
176 * and 'MAIN' == NS_MAIN.
177 * @covers NamespaceInfo::equals
179 public function testEquals() {
180 $this->assertTrue( $this->obj
->equals( NS_MAIN
, NS_MAIN
) );
181 $this->assertTrue( $this->obj
->equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
182 $this->assertTrue( $this->obj
->equals( NS_USER
, NS_USER
) );
183 $this->assertTrue( $this->obj
->equals( NS_USER
, 2 ) );
184 $this->assertTrue( $this->obj
->equals( NS_USER_TALK
, NS_USER_TALK
) );
185 $this->assertTrue( $this->obj
->equals( NS_SPECIAL
, NS_SPECIAL
) );
186 $this->assertFalse( $this->obj
->equals( NS_MAIN
, NS_TALK
) );
187 $this->assertFalse( $this->obj
->equals( NS_USER
, NS_USER_TALK
) );
188 $this->assertFalse( $this->obj
->equals( NS_PROJECT
, NS_TEMPLATE
) );
192 * @covers NamespaceInfo::subjectEquals
194 public function testSubjectEquals() {
195 $this->assertSameSubject( NS_MAIN
, NS_MAIN
);
196 $this->assertSameSubject( NS_MAIN
, 0 ); // In case we make NS_MAIN 'MAIN'
197 $this->assertSameSubject( NS_USER
, NS_USER
);
198 $this->assertSameSubject( NS_USER
, 2 );
199 $this->assertSameSubject( NS_USER_TALK
, NS_USER_TALK
);
200 $this->assertSameSubject( NS_SPECIAL
, NS_SPECIAL
);
201 $this->assertSameSubject( NS_MAIN
, NS_TALK
);
202 $this->assertSameSubject( NS_USER
, NS_USER_TALK
);
204 $this->assertDifferentSubject( NS_PROJECT
, NS_TEMPLATE
);
205 $this->assertDifferentSubject( NS_SPECIAL
, NS_MAIN
);
209 * @covers NamespaceInfo::subjectEquals
211 public function testSpecialAndMediaAreDifferentSubjects() {
212 $this->assertDifferentSubject(
213 NS_MEDIA
, NS_SPECIAL
,
214 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
216 $this->assertDifferentSubject(
217 NS_SPECIAL
, NS_MEDIA
,
218 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
222 public function provideHasTalkNamespace() {
225 [ NS_SPECIAL
, false ],
230 [ NS_USER_TALK
, true ],
238 * @dataProvider provideHasTalkNamespace
239 * @covers NamespaceInfo::hasTalkNamespace
242 * @param bool $expected
244 public function testHasTalkNamespace( $index, $expected ) {
245 $actual = $this->obj
->hasTalkNamespace( $index );
246 $this->assertSame( $actual, $expected, "NS $index" );
250 * @dataProvider provideHasTalkNamespace
251 * @covers MWNamespace::canTalk
254 * @param bool $expected
256 public function testCanTalk( $index, $expected ) {
257 $this->hideDeprecated( 'MWNamespace::canTalk' );
258 $actual = MWNamespace
::canTalk( $index );
259 $this->assertSame( $actual, $expected, "NS $index" );
262 private function assertIsContent( $ns ) {
263 $this->assertTrue( $this->obj
->isContent( $ns ) );
266 private function assertIsNotContent( $ns ) {
267 $this->assertFalse( $this->obj
->isContent( $ns ) );
271 * @covers NamespaceInfo::isContent
273 public function testIsContent() {
274 // NS_MAIN is a content namespace per DefaultSettings.php
275 // and per function definition.
277 $this->assertIsContent( NS_MAIN
);
279 // Other namespaces which are not expected to be content
281 $this->assertIsNotContent( NS_MEDIA
);
282 $this->assertIsNotContent( NS_SPECIAL
);
283 $this->assertIsNotContent( NS_TALK
);
284 $this->assertIsNotContent( NS_USER
);
285 $this->assertIsNotContent( NS_CATEGORY
);
286 $this->assertIsNotContent( 100 );
290 * Similar to testIsContent() but alters the $wgContentNamespaces
292 * @covers NamespaceInfo::isContent
294 public function testIsContentAdvanced() {
295 global $wgContentNamespaces;
297 // Test that user defined namespace #252 is not content
298 $this->assertIsNotContent( 252 );
300 // Bless namespace # 252 as a content namespace
301 $wgContentNamespaces[] = 252;
303 $this->assertIsContent( 252 );
305 // Makes sure NS_MAIN was not impacted
306 $this->assertIsContent( NS_MAIN
);
309 private function assertIsWatchable( $ns ) {
310 $this->assertTrue( $this->obj
->isWatchable( $ns ) );
313 private function assertIsNotWatchable( $ns ) {
314 $this->assertFalse( $this->obj
->isWatchable( $ns ) );
318 * @covers NamespaceInfo::isWatchable
320 public function testIsWatchable() {
321 // Specials namespaces are not watchable
322 $this->assertIsNotWatchable( NS_MEDIA
);
323 $this->assertIsNotWatchable( NS_SPECIAL
);
325 // Core defined namespaces are watchables
326 $this->assertIsWatchable( NS_MAIN
);
327 $this->assertIsWatchable( NS_TALK
);
329 // Additional, user defined namespaces are watchables
330 $this->assertIsWatchable( 100 );
331 $this->assertIsWatchable( 101 );
334 private function assertHasSubpages( $ns ) {
335 $this->assertTrue( $this->obj
->hasSubpages( $ns ) );
338 private function assertHasNotSubpages( $ns ) {
339 $this->assertFalse( $this->obj
->hasSubpages( $ns ) );
343 * @covers NamespaceInfo::hasSubpages
345 public function testHasSubpages() {
346 global $wgNamespacesWithSubpages;
348 // Special namespaces:
349 $this->assertHasNotSubpages( NS_MEDIA
);
350 $this->assertHasNotSubpages( NS_SPECIAL
);
352 // Namespaces without subpages
353 $this->assertHasNotSubpages( NS_MAIN
);
355 $wgNamespacesWithSubpages[NS_MAIN
] = true;
356 $this->assertHasSubpages( NS_MAIN
);
358 $wgNamespacesWithSubpages[NS_MAIN
] = false;
359 $this->assertHasNotSubpages( NS_MAIN
);
361 // Some namespaces with subpages
362 $this->assertHasSubpages( NS_TALK
);
363 $this->assertHasSubpages( NS_USER
);
364 $this->assertHasSubpages( NS_USER_TALK
);
368 * @covers NamespaceInfo::getContentNamespaces
370 public function testGetContentNamespaces() {
371 global $wgContentNamespaces;
375 $this->obj
->getContentNamespaces(),
376 '$wgContentNamespaces is an array with only NS_MAIN by default'
379 # test !is_array( $wgcontentNamespaces )
380 $wgContentNamespaces = '';
381 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
383 $wgContentNamespaces = false;
384 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
386 $wgContentNamespaces = null;
387 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
389 $wgContentNamespaces = 5;
390 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
392 # test $wgContentNamespaces === []
393 $wgContentNamespaces = [];
394 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
396 # test !in_array( NS_MAIN, $wgContentNamespaces )
397 $wgContentNamespaces = [ NS_USER
, NS_CATEGORY
];
399 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
400 $this->obj
->getContentNamespaces(),
401 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
404 # test other cases, return $wgcontentNamespaces as is
405 $wgContentNamespaces = [ NS_MAIN
];
408 $this->obj
->getContentNamespaces()
411 $wgContentNamespaces = [ NS_MAIN
, NS_USER
, NS_CATEGORY
];
413 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
414 $this->obj
->getContentNamespaces()
419 * @covers NamespaceInfo::getSubjectNamespaces
421 public function testGetSubjectNamespaces() {
422 $subjectsNS = $this->obj
->getSubjectNamespaces();
423 $this->assertContains( NS_MAIN
, $subjectsNS,
424 "Talk namespaces should have NS_MAIN" );
425 $this->assertNotContains( NS_TALK
, $subjectsNS,
426 "Talk namespaces should have NS_TALK" );
428 $this->assertNotContains( NS_MEDIA
, $subjectsNS,
429 "Talk namespaces should not have NS_MEDIA" );
430 $this->assertNotContains( NS_SPECIAL
, $subjectsNS,
431 "Talk namespaces should not have NS_SPECIAL" );
435 * @covers NamespaceInfo::getTalkNamespaces
437 public function testGetTalkNamespaces() {
438 $talkNS = $this->obj
->getTalkNamespaces();
439 $this->assertContains( NS_TALK
, $talkNS,
440 "Subject namespaces should have NS_TALK" );
441 $this->assertNotContains( NS_MAIN
, $talkNS,
442 "Subject namespaces should not have NS_MAIN" );
444 $this->assertNotContains( NS_MEDIA
, $talkNS,
445 "Subject namespaces should not have NS_MEDIA" );
446 $this->assertNotContains( NS_SPECIAL
, $talkNS,
447 "Subject namespaces should not have NS_SPECIAL" );
450 private function assertIsCapitalized( $ns ) {
451 $this->assertTrue( $this->obj
->isCapitalized( $ns ) );
454 private function assertIsNotCapitalized( $ns ) {
455 $this->assertFalse( $this->obj
->isCapitalized( $ns ) );
459 * Some namespaces are always capitalized per code definition
460 * in NamespaceInfo::$alwaysCapitalizedNamespaces
461 * @covers NamespaceInfo::isCapitalized
463 public function testIsCapitalizedHardcodedAssertions() {
464 // NS_MEDIA and NS_FILE are treated the same
466 $this->obj
->isCapitalized( NS_MEDIA
),
467 $this->obj
->isCapitalized( NS_FILE
),
468 'NS_MEDIA and NS_FILE have same capitalization rendering'
471 // Boths are capitalized by default
472 $this->assertIsCapitalized( NS_MEDIA
);
473 $this->assertIsCapitalized( NS_FILE
);
475 // Always capitalized namespaces
476 // @see NamespaceInfo::$alwaysCapitalizedNamespaces
477 $this->assertIsCapitalized( NS_SPECIAL
);
478 $this->assertIsCapitalized( NS_USER
);
479 $this->assertIsCapitalized( NS_MEDIAWIKI
);
483 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
484 * global $wgCapitalLink setting to have extended coverage.
486 * NamespaceInfo::isCapitalized() rely on two global settings:
487 * $wgCapitalLinkOverrides = []; by default
488 * $wgCapitalLinks = true; by default
489 * This function test $wgCapitalLinks
491 * Global setting correctness is tested against the NS_PROJECT and
492 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
493 * @covers NamespaceInfo::isCapitalized
495 public function testIsCapitalizedWithWgCapitalLinks() {
496 $this->assertIsCapitalized( NS_PROJECT
);
497 $this->assertIsCapitalized( NS_PROJECT_TALK
);
499 $this->setMwGlobals( 'wgCapitalLinks', false );
501 // hardcoded namespaces (see above function) are still capitalized:
502 $this->assertIsCapitalized( NS_SPECIAL
);
503 $this->assertIsCapitalized( NS_USER
);
504 $this->assertIsCapitalized( NS_MEDIAWIKI
);
506 // setting is correctly applied
507 $this->assertIsNotCapitalized( NS_PROJECT
);
508 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
512 * Counter part for NamespaceInfo::testIsCapitalizedWithWgCapitalLinks() now
513 * testing the $wgCapitalLinkOverrides global.
515 * @todo split groups of assertions in autonomous testing functions
516 * @covers NamespaceInfo::isCapitalized
518 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
519 global $wgCapitalLinkOverrides;
521 // Test default settings
522 $this->assertIsCapitalized( NS_PROJECT
);
523 $this->assertIsCapitalized( NS_PROJECT_TALK
);
525 // hardcoded namespaces (see above function) are capitalized:
526 $this->assertIsCapitalized( NS_SPECIAL
);
527 $this->assertIsCapitalized( NS_USER
);
528 $this->assertIsCapitalized( NS_MEDIAWIKI
);
530 // Hardcoded namespaces remains capitalized
531 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
532 $wgCapitalLinkOverrides[NS_USER
] = false;
533 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
535 $this->assertIsCapitalized( NS_SPECIAL
);
536 $this->assertIsCapitalized( NS_USER
);
537 $this->assertIsCapitalized( NS_MEDIAWIKI
);
539 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
540 $this->assertIsNotCapitalized( NS_PROJECT
);
542 $wgCapitalLinkOverrides[NS_PROJECT
] = true;
543 $this->assertIsCapitalized( NS_PROJECT
);
545 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
546 $this->assertIsCapitalized( NS_PROJECT
);
550 * @covers NamespaceInfo::hasGenderDistinction
552 public function testHasGenderDistinction() {
553 // Namespaces with gender distinctions
554 $this->assertTrue( $this->obj
->hasGenderDistinction( NS_USER
) );
555 $this->assertTrue( $this->obj
->hasGenderDistinction( NS_USER_TALK
) );
557 // Other ones, "genderless"
558 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_MEDIA
) );
559 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_SPECIAL
) );
560 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_MAIN
) );
561 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_TALK
) );
565 * @covers NamespaceInfo::isNonincludable
567 public function testIsNonincludable() {
568 global $wgNonincludableNamespaces;
570 $wgNonincludableNamespaces = [ NS_USER
];
572 $this->assertTrue( $this->obj
->isNonincludable( NS_USER
) );
573 $this->assertFalse( $this->obj
->isNonincludable( NS_TEMPLATE
) );
576 private function assertSameSubject( $ns1, $ns2, $msg = '' ) {
577 $this->assertTrue( $this->obj
->subjectEquals( $ns1, $ns2 ), $msg );
580 private function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
581 $this->assertFalse( $this->obj
->subjectEquals( $ns1, $ns2 ), $msg );
584 public function provideGetCategoryLinkType() {
589 [ NS_USER_TALK
, 'page' ],
592 [ NS_FILE_TALK
, 'page' ],
594 [ NS_CATEGORY
, 'subcat' ],
595 [ NS_CATEGORY_TALK
, 'page' ],
603 * @dataProvider provideGetCategoryLinkType
604 * @covers NamespaceInfo::getCategoryLinkType
607 * @param string $expected
609 public function testGetCategoryLinkType( $index, $expected ) {
610 $actual = $this->obj
->getCategoryLinkType( $index );
611 $this->assertSame( $expected, $actual, "NS $index" );