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" );
249 private function assertIsContent( $ns ) {
250 $this->assertTrue( $this->obj
->isContent( $ns ) );
253 private function assertIsNotContent( $ns ) {
254 $this->assertFalse( $this->obj
->isContent( $ns ) );
258 * @covers NamespaceInfo::isContent
260 public function testIsContent() {
261 // NS_MAIN is a content namespace per DefaultSettings.php
262 // and per function definition.
264 $this->assertIsContent( NS_MAIN
);
266 // Other namespaces which are not expected to be content
268 $this->assertIsNotContent( NS_MEDIA
);
269 $this->assertIsNotContent( NS_SPECIAL
);
270 $this->assertIsNotContent( NS_TALK
);
271 $this->assertIsNotContent( NS_USER
);
272 $this->assertIsNotContent( NS_CATEGORY
);
273 $this->assertIsNotContent( 100 );
277 * Similar to testIsContent() but alters the $wgContentNamespaces
279 * @covers NamespaceInfo::isContent
281 public function testIsContentAdvanced() {
282 global $wgContentNamespaces;
284 // Test that user defined namespace #252 is not content
285 $this->assertIsNotContent( 252 );
287 // Bless namespace # 252 as a content namespace
288 $wgContentNamespaces[] = 252;
290 $this->assertIsContent( 252 );
292 // Makes sure NS_MAIN was not impacted
293 $this->assertIsContent( NS_MAIN
);
296 private function assertIsWatchable( $ns ) {
297 $this->assertTrue( $this->obj
->isWatchable( $ns ) );
300 private function assertIsNotWatchable( $ns ) {
301 $this->assertFalse( $this->obj
->isWatchable( $ns ) );
305 * @covers NamespaceInfo::isWatchable
307 public function testIsWatchable() {
308 // Specials namespaces are not watchable
309 $this->assertIsNotWatchable( NS_MEDIA
);
310 $this->assertIsNotWatchable( NS_SPECIAL
);
312 // Core defined namespaces are watchables
313 $this->assertIsWatchable( NS_MAIN
);
314 $this->assertIsWatchable( NS_TALK
);
316 // Additional, user defined namespaces are watchables
317 $this->assertIsWatchable( 100 );
318 $this->assertIsWatchable( 101 );
321 private function assertHasSubpages( $ns ) {
322 $this->assertTrue( $this->obj
->hasSubpages( $ns ) );
325 private function assertHasNotSubpages( $ns ) {
326 $this->assertFalse( $this->obj
->hasSubpages( $ns ) );
330 * @covers NamespaceInfo::hasSubpages
332 public function testHasSubpages() {
333 global $wgNamespacesWithSubpages;
335 // Special namespaces:
336 $this->assertHasNotSubpages( NS_MEDIA
);
337 $this->assertHasNotSubpages( NS_SPECIAL
);
339 // Namespaces without subpages
340 $this->assertHasNotSubpages( NS_MAIN
);
342 $wgNamespacesWithSubpages[NS_MAIN
] = true;
343 $this->assertHasSubpages( NS_MAIN
);
345 $wgNamespacesWithSubpages[NS_MAIN
] = false;
346 $this->assertHasNotSubpages( NS_MAIN
);
348 // Some namespaces with subpages
349 $this->assertHasSubpages( NS_TALK
);
350 $this->assertHasSubpages( NS_USER
);
351 $this->assertHasSubpages( NS_USER_TALK
);
355 * @covers NamespaceInfo::getContentNamespaces
357 public function testGetContentNamespaces() {
358 global $wgContentNamespaces;
362 $this->obj
->getContentNamespaces(),
363 '$wgContentNamespaces is an array with only NS_MAIN by default'
366 # test !is_array( $wgcontentNamespaces )
367 $wgContentNamespaces = '';
368 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
370 $wgContentNamespaces = false;
371 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
373 $wgContentNamespaces = null;
374 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
376 $wgContentNamespaces = 5;
377 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
379 # test $wgContentNamespaces === []
380 $wgContentNamespaces = [];
381 $this->assertEquals( [ NS_MAIN
], $this->obj
->getContentNamespaces() );
383 # test !in_array( NS_MAIN, $wgContentNamespaces )
384 $wgContentNamespaces = [ NS_USER
, NS_CATEGORY
];
386 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
387 $this->obj
->getContentNamespaces(),
388 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
391 # test other cases, return $wgcontentNamespaces as is
392 $wgContentNamespaces = [ NS_MAIN
];
395 $this->obj
->getContentNamespaces()
398 $wgContentNamespaces = [ NS_MAIN
, NS_USER
, NS_CATEGORY
];
400 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
401 $this->obj
->getContentNamespaces()
406 * @covers NamespaceInfo::getSubjectNamespaces
408 public function testGetSubjectNamespaces() {
409 $subjectsNS = $this->obj
->getSubjectNamespaces();
410 $this->assertContains( NS_MAIN
, $subjectsNS,
411 "Talk namespaces should have NS_MAIN" );
412 $this->assertNotContains( NS_TALK
, $subjectsNS,
413 "Talk namespaces should have NS_TALK" );
415 $this->assertNotContains( NS_MEDIA
, $subjectsNS,
416 "Talk namespaces should not have NS_MEDIA" );
417 $this->assertNotContains( NS_SPECIAL
, $subjectsNS,
418 "Talk namespaces should not have NS_SPECIAL" );
422 * @covers NamespaceInfo::getTalkNamespaces
424 public function testGetTalkNamespaces() {
425 $talkNS = $this->obj
->getTalkNamespaces();
426 $this->assertContains( NS_TALK
, $talkNS,
427 "Subject namespaces should have NS_TALK" );
428 $this->assertNotContains( NS_MAIN
, $talkNS,
429 "Subject namespaces should not have NS_MAIN" );
431 $this->assertNotContains( NS_MEDIA
, $talkNS,
432 "Subject namespaces should not have NS_MEDIA" );
433 $this->assertNotContains( NS_SPECIAL
, $talkNS,
434 "Subject namespaces should not have NS_SPECIAL" );
437 private function assertIsCapitalized( $ns ) {
438 $this->assertTrue( $this->obj
->isCapitalized( $ns ) );
441 private function assertIsNotCapitalized( $ns ) {
442 $this->assertFalse( $this->obj
->isCapitalized( $ns ) );
446 * Some namespaces are always capitalized per code definition
447 * in NamespaceInfo::$alwaysCapitalizedNamespaces
448 * @covers NamespaceInfo::isCapitalized
450 public function testIsCapitalizedHardcodedAssertions() {
451 // NS_MEDIA and NS_FILE are treated the same
453 $this->obj
->isCapitalized( NS_MEDIA
),
454 $this->obj
->isCapitalized( NS_FILE
),
455 'NS_MEDIA and NS_FILE have same capitalization rendering'
458 // Boths are capitalized by default
459 $this->assertIsCapitalized( NS_MEDIA
);
460 $this->assertIsCapitalized( NS_FILE
);
462 // Always capitalized namespaces
463 // @see NamespaceInfo::$alwaysCapitalizedNamespaces
464 $this->assertIsCapitalized( NS_SPECIAL
);
465 $this->assertIsCapitalized( NS_USER
);
466 $this->assertIsCapitalized( NS_MEDIAWIKI
);
470 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
471 * global $wgCapitalLink setting to have extended coverage.
473 * NamespaceInfo::isCapitalized() rely on two global settings:
474 * $wgCapitalLinkOverrides = []; by default
475 * $wgCapitalLinks = true; by default
476 * This function test $wgCapitalLinks
478 * Global setting correctness is tested against the NS_PROJECT and
479 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
480 * @covers NamespaceInfo::isCapitalized
482 public function testIsCapitalizedWithWgCapitalLinks() {
483 $this->assertIsCapitalized( NS_PROJECT
);
484 $this->assertIsCapitalized( NS_PROJECT_TALK
);
486 $this->setMwGlobals( 'wgCapitalLinks', false );
488 // hardcoded namespaces (see above function) are still capitalized:
489 $this->assertIsCapitalized( NS_SPECIAL
);
490 $this->assertIsCapitalized( NS_USER
);
491 $this->assertIsCapitalized( NS_MEDIAWIKI
);
493 // setting is correctly applied
494 $this->assertIsNotCapitalized( NS_PROJECT
);
495 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
499 * Counter part for NamespaceInfo::testIsCapitalizedWithWgCapitalLinks() now
500 * testing the $wgCapitalLinkOverrides global.
502 * @todo split groups of assertions in autonomous testing functions
503 * @covers NamespaceInfo::isCapitalized
505 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
506 global $wgCapitalLinkOverrides;
508 // Test default settings
509 $this->assertIsCapitalized( NS_PROJECT
);
510 $this->assertIsCapitalized( NS_PROJECT_TALK
);
512 // hardcoded namespaces (see above function) are capitalized:
513 $this->assertIsCapitalized( NS_SPECIAL
);
514 $this->assertIsCapitalized( NS_USER
);
515 $this->assertIsCapitalized( NS_MEDIAWIKI
);
517 // Hardcoded namespaces remains capitalized
518 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
519 $wgCapitalLinkOverrides[NS_USER
] = false;
520 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
522 $this->assertIsCapitalized( NS_SPECIAL
);
523 $this->assertIsCapitalized( NS_USER
);
524 $this->assertIsCapitalized( NS_MEDIAWIKI
);
526 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
527 $this->assertIsNotCapitalized( NS_PROJECT
);
529 $wgCapitalLinkOverrides[NS_PROJECT
] = true;
530 $this->assertIsCapitalized( NS_PROJECT
);
532 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
533 $this->assertIsCapitalized( NS_PROJECT
);
537 * @covers NamespaceInfo::hasGenderDistinction
539 public function testHasGenderDistinction() {
540 // Namespaces with gender distinctions
541 $this->assertTrue( $this->obj
->hasGenderDistinction( NS_USER
) );
542 $this->assertTrue( $this->obj
->hasGenderDistinction( NS_USER_TALK
) );
544 // Other ones, "genderless"
545 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_MEDIA
) );
546 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_SPECIAL
) );
547 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_MAIN
) );
548 $this->assertFalse( $this->obj
->hasGenderDistinction( NS_TALK
) );
552 * @covers NamespaceInfo::isNonincludable
554 public function testIsNonincludable() {
555 global $wgNonincludableNamespaces;
557 $wgNonincludableNamespaces = [ NS_USER
];
559 $this->assertTrue( $this->obj
->isNonincludable( NS_USER
) );
560 $this->assertFalse( $this->obj
->isNonincludable( NS_TEMPLATE
) );
563 private function assertSameSubject( $ns1, $ns2, $msg = '' ) {
564 $this->assertTrue( $this->obj
->subjectEquals( $ns1, $ns2 ), $msg );
567 private function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
568 $this->assertFalse( $this->obj
->subjectEquals( $ns1, $ns2 ), $msg );
571 public function provideGetCategoryLinkType() {
576 [ NS_USER_TALK
, 'page' ],
579 [ NS_FILE_TALK
, 'page' ],
581 [ NS_CATEGORY
, 'subcat' ],
582 [ NS_CATEGORY_TALK
, 'page' ],
590 * @dataProvider provideGetCategoryLinkType
591 * @covers NamespaceInfo::getCategoryLinkType
594 * @param string $expected
596 public function testGetCategoryLinkType( $index, $expected ) {
597 $actual = $this->obj
->getCategoryLinkType( $index );
598 $this->assertSame( $expected, $actual, "NS $index" );