3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
8 class MWNamespaceTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgContentNamespaces' => [ NS_MAIN
],
15 'wgNamespacesWithSubpages' => [
20 'wgCapitalLinks' => true,
21 'wgCapitalLinkOverrides' => [],
22 'wgNonincludableNamespaces' => [],
27 * @todo Write more texts, handle $wgAllowImageMoving setting
28 * @covers MWNamespace::isMovable
30 public function testIsMovable() {
31 $this->assertFalse( MWNamespace
::isMovable( NS_SPECIAL
) );
34 private function assertIsSubject( $ns ) {
35 $this->assertTrue( MWNamespace
::isSubject( $ns ) );
38 private function assertIsNotSubject( $ns ) {
39 $this->assertFalse( MWNamespace
::isSubject( $ns ) );
43 * Please make sure to change testIsTalk() if you change the assertions below
44 * @covers MWNamespace::isSubject
46 public function testIsSubject() {
48 $this->assertIsSubject( NS_MEDIA
);
49 $this->assertIsSubject( NS_SPECIAL
);
52 $this->assertIsSubject( NS_MAIN
);
53 $this->assertIsSubject( NS_USER
);
54 $this->assertIsSubject( 100 ); # user defined
57 $this->assertIsNotSubject( NS_TALK
);
58 $this->assertIsNotSubject( NS_USER_TALK
);
59 $this->assertIsNotSubject( 101 ); # user defined
62 private function assertIsTalk( $ns ) {
63 $this->assertTrue( MWNamespace
::isTalk( $ns ) );
66 private function assertIsNotTalk( $ns ) {
67 $this->assertFalse( MWNamespace
::isTalk( $ns ) );
71 * Reverse of testIsSubject().
72 * Please update testIsSubject() if you change assertions below
73 * @covers MWNamespace::isTalk
75 public function testIsTalk() {
77 $this->assertIsNotTalk( NS_MEDIA
);
78 $this->assertIsNotTalk( NS_SPECIAL
);
81 $this->assertIsNotTalk( NS_MAIN
);
82 $this->assertIsNotTalk( NS_USER
);
83 $this->assertIsNotTalk( 100 ); # user defined
86 $this->assertIsTalk( NS_TALK
);
87 $this->assertIsTalk( NS_USER_TALK
);
88 $this->assertIsTalk( 101 ); # user defined
92 * @covers MWNamespace::getSubject
94 public function testGetSubject() {
95 // Special namespaces are their own subjects
96 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
97 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
99 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
100 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
104 * Regular getTalk() calls
105 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
106 * the function testGetTalkExceptions()
107 * @covers MWNamespace::getTalk
109 public function testGetTalk() {
110 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
111 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_TALK
) );
112 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER
) );
113 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER_TALK
) );
117 * Exceptions with getTalk()
118 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
119 * @expectedException MWException
120 * @covers MWNamespace::getTalk
122 public function testGetTalkExceptionsForNsMedia() {
123 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
127 * Exceptions with getTalk()
128 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
129 * @expectedException MWException
130 * @covers MWNamespace::getTalk
132 public function testGetTalkExceptionsForNsSpecial() {
133 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
137 * Regular getAssociated() calls
138 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
139 * the function testGetAssociatedExceptions()
140 * @covers MWNamespace::getAssociated
142 public function testGetAssociated() {
143 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
144 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
147 # ## Exceptions with getAssociated()
148 # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
149 # ## an exception for them.
151 * @expectedException MWException
152 * @covers MWNamespace::getAssociated
154 public function testGetAssociatedExceptionsForNsMedia() {
155 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
159 * @expectedException MWException
160 * @covers MWNamespace::getAssociated
162 public function testGetAssociatedExceptionsForNsSpecial() {
163 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
167 * Test MWNamespace::equals
168 * Note if we add a namespace registration system with keys like 'MAIN'
169 * we should add tests here for equivilance on things like 'MAIN' == 0
170 * and 'MAIN' == NS_MAIN.
171 * @covers MWNamespace::equals
173 public function testEquals() {
174 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, NS_MAIN
) );
175 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
176 $this->assertTrue( MWNamespace
::equals( NS_USER
, NS_USER
) );
177 $this->assertTrue( MWNamespace
::equals( NS_USER
, 2 ) );
178 $this->assertTrue( MWNamespace
::equals( NS_USER_TALK
, NS_USER_TALK
) );
179 $this->assertTrue( MWNamespace
::equals( NS_SPECIAL
, NS_SPECIAL
) );
180 $this->assertFalse( MWNamespace
::equals( NS_MAIN
, NS_TALK
) );
181 $this->assertFalse( MWNamespace
::equals( NS_USER
, NS_USER_TALK
) );
182 $this->assertFalse( MWNamespace
::equals( NS_PROJECT
, NS_TEMPLATE
) );
186 * @covers MWNamespace::subjectEquals
188 public function testSubjectEquals() {
189 $this->assertSameSubject( NS_MAIN
, NS_MAIN
);
190 $this->assertSameSubject( NS_MAIN
, 0 ); // In case we make NS_MAIN 'MAIN'
191 $this->assertSameSubject( NS_USER
, NS_USER
);
192 $this->assertSameSubject( NS_USER
, 2 );
193 $this->assertSameSubject( NS_USER_TALK
, NS_USER_TALK
);
194 $this->assertSameSubject( NS_SPECIAL
, NS_SPECIAL
);
195 $this->assertSameSubject( NS_MAIN
, NS_TALK
);
196 $this->assertSameSubject( NS_USER
, NS_USER_TALK
);
198 $this->assertDifferentSubject( NS_PROJECT
, NS_TEMPLATE
);
199 $this->assertDifferentSubject( NS_SPECIAL
, NS_MAIN
);
203 * @covers MWNamespace::subjectEquals
205 public function testSpecialAndMediaAreDifferentSubjects() {
206 $this->assertDifferentSubject(
207 NS_MEDIA
, NS_SPECIAL
,
208 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
210 $this->assertDifferentSubject(
211 NS_SPECIAL
, NS_MEDIA
,
212 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
216 public function provideHasTalkNamespace() {
219 [ NS_SPECIAL
, false ],
224 [ NS_USER_TALK
, true ],
232 * @dataProvider provideHasTalkNamespace
233 * @covers MWNamespace::hasTalkNamespace
236 * @param bool $expected
238 public function testHasTalkNamespace( $index, $expected ) {
239 $actual = MWNamespace
::hasTalkNamespace( $index );
240 $this->assertSame( $actual, $expected, "NS $index" );
244 * @dataProvider provideHasTalkNamespace
245 * @covers MWNamespace::canTalk
248 * @param bool $expected
250 public function testCanTalk( $index, $expected ) {
251 $this->hideDeprecated( 'MWNamespace::canTalk' );
252 $actual = MWNamespace
::canTalk( $index );
253 $this->assertSame( $actual, $expected, "NS $index" );
256 private function assertIsContent( $ns ) {
257 $this->assertTrue( MWNamespace
::isContent( $ns ) );
260 private function assertIsNotContent( $ns ) {
261 $this->assertFalse( MWNamespace
::isContent( $ns ) );
265 * @covers MWNamespace::isContent
267 public function testIsContent() {
268 // NS_MAIN is a content namespace per DefaultSettings.php
269 // and per function definition.
271 $this->assertIsContent( NS_MAIN
);
273 // Other namespaces which are not expected to be content
275 $this->assertIsNotContent( NS_MEDIA
);
276 $this->assertIsNotContent( NS_SPECIAL
);
277 $this->assertIsNotContent( NS_TALK
);
278 $this->assertIsNotContent( NS_USER
);
279 $this->assertIsNotContent( NS_CATEGORY
);
280 $this->assertIsNotContent( 100 );
284 * Similar to testIsContent() but alters the $wgContentNamespaces
286 * @covers MWNamespace::isContent
288 public function testIsContentAdvanced() {
289 global $wgContentNamespaces;
291 // Test that user defined namespace #252 is not content
292 $this->assertIsNotContent( 252 );
294 // Bless namespace # 252 as a content namespace
295 $wgContentNamespaces[] = 252;
297 $this->assertIsContent( 252 );
299 // Makes sure NS_MAIN was not impacted
300 $this->assertIsContent( NS_MAIN
);
303 private function assertIsWatchable( $ns ) {
304 $this->assertTrue( MWNamespace
::isWatchable( $ns ) );
307 private function assertIsNotWatchable( $ns ) {
308 $this->assertFalse( MWNamespace
::isWatchable( $ns ) );
312 * @covers MWNamespace::isWatchable
314 public function testIsWatchable() {
315 // Specials namespaces are not watchable
316 $this->assertIsNotWatchable( NS_MEDIA
);
317 $this->assertIsNotWatchable( NS_SPECIAL
);
319 // Core defined namespaces are watchables
320 $this->assertIsWatchable( NS_MAIN
);
321 $this->assertIsWatchable( NS_TALK
);
323 // Additional, user defined namespaces are watchables
324 $this->assertIsWatchable( 100 );
325 $this->assertIsWatchable( 101 );
328 private function assertHasSubpages( $ns ) {
329 $this->assertTrue( MWNamespace
::hasSubpages( $ns ) );
332 private function assertHasNotSubpages( $ns ) {
333 $this->assertFalse( MWNamespace
::hasSubpages( $ns ) );
337 * @covers MWNamespace::hasSubpages
339 public function testHasSubpages() {
340 global $wgNamespacesWithSubpages;
342 // Special namespaces:
343 $this->assertHasNotSubpages( NS_MEDIA
);
344 $this->assertHasNotSubpages( NS_SPECIAL
);
346 // Namespaces without subpages
347 $this->assertHasNotSubpages( NS_MAIN
);
349 $wgNamespacesWithSubpages[NS_MAIN
] = true;
350 $this->assertHasSubpages( NS_MAIN
);
352 $wgNamespacesWithSubpages[NS_MAIN
] = false;
353 $this->assertHasNotSubpages( NS_MAIN
);
355 // Some namespaces with subpages
356 $this->assertHasSubpages( NS_TALK
);
357 $this->assertHasSubpages( NS_USER
);
358 $this->assertHasSubpages( NS_USER_TALK
);
362 * @covers MWNamespace::getContentNamespaces
364 public function testGetContentNamespaces() {
365 global $wgContentNamespaces;
369 MWNamespace
::getContentNamespaces(),
370 '$wgContentNamespaces is an array with only NS_MAIN by default'
373 # test !is_array( $wgcontentNamespaces )
374 $wgContentNamespaces = '';
375 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
377 $wgContentNamespaces = false;
378 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
380 $wgContentNamespaces = null;
381 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
383 $wgContentNamespaces = 5;
384 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
386 # test $wgContentNamespaces === []
387 $wgContentNamespaces = [];
388 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
390 # test !in_array( NS_MAIN, $wgContentNamespaces )
391 $wgContentNamespaces = [ NS_USER
, NS_CATEGORY
];
393 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
394 MWNamespace
::getContentNamespaces(),
395 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
398 # test other cases, return $wgcontentNamespaces as is
399 $wgContentNamespaces = [ NS_MAIN
];
402 MWNamespace
::getContentNamespaces()
405 $wgContentNamespaces = [ NS_MAIN
, NS_USER
, NS_CATEGORY
];
407 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
408 MWNamespace
::getContentNamespaces()
413 * @covers MWNamespace::getSubjectNamespaces
415 public function testGetSubjectNamespaces() {
416 $subjectsNS = MWNamespace
::getSubjectNamespaces();
417 $this->assertContains( NS_MAIN
, $subjectsNS,
418 "Talk namespaces should have NS_MAIN" );
419 $this->assertNotContains( NS_TALK
, $subjectsNS,
420 "Talk namespaces should have NS_TALK" );
422 $this->assertNotContains( NS_MEDIA
, $subjectsNS,
423 "Talk namespaces should not have NS_MEDIA" );
424 $this->assertNotContains( NS_SPECIAL
, $subjectsNS,
425 "Talk namespaces should not have NS_SPECIAL" );
429 * @covers MWNamespace::getTalkNamespaces
431 public function testGetTalkNamespaces() {
432 $talkNS = MWNamespace
::getTalkNamespaces();
433 $this->assertContains( NS_TALK
, $talkNS,
434 "Subject namespaces should have NS_TALK" );
435 $this->assertNotContains( NS_MAIN
, $talkNS,
436 "Subject namespaces should not have NS_MAIN" );
438 $this->assertNotContains( NS_MEDIA
, $talkNS,
439 "Subject namespaces should not have NS_MEDIA" );
440 $this->assertNotContains( NS_SPECIAL
, $talkNS,
441 "Subject namespaces should not have NS_SPECIAL" );
444 private function assertIsCapitalized( $ns ) {
445 $this->assertTrue( MWNamespace
::isCapitalized( $ns ) );
448 private function assertIsNotCapitalized( $ns ) {
449 $this->assertFalse( MWNamespace
::isCapitalized( $ns ) );
453 * Some namespaces are always capitalized per code definition
454 * in MWNamespace::$alwaysCapitalizedNamespaces
455 * @covers MWNamespace::isCapitalized
457 public function testIsCapitalizedHardcodedAssertions() {
458 // NS_MEDIA and NS_FILE are treated the same
460 MWNamespace
::isCapitalized( NS_MEDIA
),
461 MWNamespace
::isCapitalized( NS_FILE
),
462 'NS_MEDIA and NS_FILE have same capitalization rendering'
465 // Boths are capitalized by default
466 $this->assertIsCapitalized( NS_MEDIA
);
467 $this->assertIsCapitalized( NS_FILE
);
469 // Always capitalized namespaces
470 // @see MWNamespace::$alwaysCapitalizedNamespaces
471 $this->assertIsCapitalized( NS_SPECIAL
);
472 $this->assertIsCapitalized( NS_USER
);
473 $this->assertIsCapitalized( NS_MEDIAWIKI
);
477 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
478 * global $wgCapitalLink setting to have extended coverage.
480 * MWNamespace::isCapitalized() rely on two global settings:
481 * $wgCapitalLinkOverrides = []; by default
482 * $wgCapitalLinks = true; by default
483 * This function test $wgCapitalLinks
485 * Global setting correctness is tested against the NS_PROJECT and
486 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
487 * @covers MWNamespace::isCapitalized
489 public function testIsCapitalizedWithWgCapitalLinks() {
490 global $wgCapitalLinks;
492 $this->assertIsCapitalized( NS_PROJECT
);
493 $this->assertIsCapitalized( NS_PROJECT_TALK
);
495 $wgCapitalLinks = false;
497 // hardcoded namespaces (see above function) are still capitalized:
498 $this->assertIsCapitalized( NS_SPECIAL
);
499 $this->assertIsCapitalized( NS_USER
);
500 $this->assertIsCapitalized( NS_MEDIAWIKI
);
502 // setting is correctly applied
503 $this->assertIsNotCapitalized( NS_PROJECT
);
504 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
508 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
509 * testing the $wgCapitalLinkOverrides global.
511 * @todo split groups of assertions in autonomous testing functions
512 * @covers MWNamespace::isCapitalized
514 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
515 global $wgCapitalLinkOverrides;
517 // Test default settings
518 $this->assertIsCapitalized( NS_PROJECT
);
519 $this->assertIsCapitalized( NS_PROJECT_TALK
);
521 // hardcoded namespaces (see above function) are capitalized:
522 $this->assertIsCapitalized( NS_SPECIAL
);
523 $this->assertIsCapitalized( NS_USER
);
524 $this->assertIsCapitalized( NS_MEDIAWIKI
);
526 // Hardcoded namespaces remains capitalized
527 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
528 $wgCapitalLinkOverrides[NS_USER
] = false;
529 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
531 $this->assertIsCapitalized( NS_SPECIAL
);
532 $this->assertIsCapitalized( NS_USER
);
533 $this->assertIsCapitalized( NS_MEDIAWIKI
);
535 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
536 $this->assertIsNotCapitalized( NS_PROJECT
);
538 $wgCapitalLinkOverrides[NS_PROJECT
] = true;
539 $this->assertIsCapitalized( NS_PROJECT
);
541 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
542 $this->assertIsCapitalized( NS_PROJECT
);
546 * @covers MWNamespace::hasGenderDistinction
548 public function testHasGenderDistinction() {
549 // Namespaces with gender distinctions
550 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
551 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
553 // Other ones, "genderless"
554 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
555 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
556 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
557 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
561 * @covers MWNamespace::isNonincludable
563 public function testIsNonincludable() {
564 global $wgNonincludableNamespaces;
566 $wgNonincludableNamespaces = [ NS_USER
];
568 $this->assertTrue( MWNamespace
::isNonincludable( NS_USER
) );
569 $this->assertFalse( MWNamespace
::isNonincludable( NS_TEMPLATE
) );
572 private function assertSameSubject( $ns1, $ns2, $msg = '' ) {
573 $this->assertTrue( MWNamespace
::subjectEquals( $ns1, $ns2 ), $msg );
576 private function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
577 $this->assertFalse( MWNamespace
::subjectEquals( $ns1, $ns2 ), $msg );
580 public function provideGetCategoryLinkType() {
585 [ NS_USER_TALK
, 'page' ],
588 [ NS_FILE_TALK
, 'page' ],
590 [ NS_CATEGORY
, 'subcat' ],
591 [ NS_CATEGORY_TALK
, 'page' ],
599 * @dataProvider provideGetCategoryLinkType
600 * @covers MWNamespace::getCategoryLinkType
603 * @param string $expected
605 public function testGetCategoryLinkType( $index, $expected ) {
606 $actual = MWNamespace
::getCategoryLinkType( $index );
607 $this->assertSame( $expected, $actual, "NS $index" );