3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
9 * Test class for MWNamespace.
10 * @todo FIXME: this test file is a mess
12 class MWNamespaceTest
extends MediaWikiTestCase
{
13 protected function setUp() {
16 $this->setMwGlobals( [
17 'wgContentNamespaces' => [ NS_MAIN
],
18 'wgNamespacesWithSubpages' => [
23 'wgCapitalLinks' => true,
24 'wgCapitalLinkOverrides' => [],
25 'wgNonincludableNamespaces' => [],
29 # ### START OF TESTS #########################################################
32 * @todo Write more texts, handle $wgAllowImageMoving setting
33 * @covers MWNamespace::isMovable
35 public function testIsMovable() {
36 $this->assertFalse( MWNamespace
::isMovable( NS_SPECIAL
) );
37 # @todo FIXME: Write more tests!!
41 * Please make sure to change testIsTalk() if you change the assertions below
42 * @covers MWNamespace::isSubject
44 public function testIsSubject() {
46 $this->assertIsSubject( NS_MEDIA
);
47 $this->assertIsSubject( NS_SPECIAL
);
50 $this->assertIsSubject( NS_MAIN
);
51 $this->assertIsSubject( NS_USER
);
52 $this->assertIsSubject( 100 ); # user defined
55 $this->assertIsNotSubject( NS_TALK
);
56 $this->assertIsNotSubject( NS_USER_TALK
);
57 $this->assertIsNotSubject( 101 ); # user defined
61 * Reverse of testIsSubject().
62 * Please update testIsSubject() if you change assertions below
63 * @covers MWNamespace::isTalk
65 public function testIsTalk() {
67 $this->assertIsNotTalk( NS_MEDIA
);
68 $this->assertIsNotTalk( NS_SPECIAL
);
71 $this->assertIsNotTalk( NS_MAIN
);
72 $this->assertIsNotTalk( NS_USER
);
73 $this->assertIsNotTalk( 100 ); # user defined
76 $this->assertIsTalk( NS_TALK
);
77 $this->assertIsTalk( NS_USER_TALK
);
78 $this->assertIsTalk( 101 ); # user defined
82 * @covers MWNamespace::getSubject
84 public function testGetSubject() {
85 // Special namespaces are their own subjects
86 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
87 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
89 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
90 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
94 * Regular getTalk() calls
95 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
96 * the function testGetTalkExceptions()
97 * @covers MWNamespace::getTalk
99 public function testGetTalk() {
100 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
101 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_TALK
) );
102 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER
) );
103 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER_TALK
) );
107 * Exceptions with getTalk()
108 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
109 * @expectedException MWException
110 * @covers MWNamespace::getTalk
112 public function testGetTalkExceptionsForNsMedia() {
113 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
117 * Exceptions with getTalk()
118 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
119 * @expectedException MWException
120 * @covers MWNamespace::getTalk
122 public function testGetTalkExceptionsForNsSpecial() {
123 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
127 * Regular getAssociated() calls
128 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
129 * the function testGetAssociatedExceptions()
130 * @covers MWNamespace::getAssociated
132 public function testGetAssociated() {
133 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
134 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
137 # ## Exceptions with getAssociated()
138 # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
139 # ## an exception for them.
141 * @expectedException MWException
142 * @covers MWNamespace::getAssociated
144 public function testGetAssociatedExceptionsForNsMedia() {
145 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
149 * @expectedException MWException
150 * @covers MWNamespace::getAssociated
152 public function testGetAssociatedExceptionsForNsSpecial() {
153 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
157 * @todo Implement testExists().
160 public function testExists() {
161 // Remove the following lines when you implement this test.
162 $this->markTestIncomplete(
163 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
169 * Test MWNamespace::equals
170 * Note if we add a namespace registration system with keys like 'MAIN'
171 * we should add tests here for equivilance on things like 'MAIN' == 0
172 * and 'MAIN' == NS_MAIN.
173 * @covers MWNamespace::equals
175 public function testEquals() {
176 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, NS_MAIN
) );
177 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
178 $this->assertTrue( MWNamespace
::equals( NS_USER
, NS_USER
) );
179 $this->assertTrue( MWNamespace
::equals( NS_USER
, 2 ) );
180 $this->assertTrue( MWNamespace
::equals( NS_USER_TALK
, NS_USER_TALK
) );
181 $this->assertTrue( MWNamespace
::equals( NS_SPECIAL
, NS_SPECIAL
) );
182 $this->assertFalse( MWNamespace
::equals( NS_MAIN
, NS_TALK
) );
183 $this->assertFalse( MWNamespace
::equals( NS_USER
, NS_USER_TALK
) );
184 $this->assertFalse( MWNamespace
::equals( NS_PROJECT
, NS_TEMPLATE
) );
188 * @covers MWNamespace::subjectEquals
190 public function testSubjectEquals() {
191 $this->assertSameSubject( NS_MAIN
, NS_MAIN
);
192 $this->assertSameSubject( NS_MAIN
, 0 ); // In case we make NS_MAIN 'MAIN'
193 $this->assertSameSubject( NS_USER
, NS_USER
);
194 $this->assertSameSubject( NS_USER
, 2 );
195 $this->assertSameSubject( NS_USER_TALK
, NS_USER_TALK
);
196 $this->assertSameSubject( NS_SPECIAL
, NS_SPECIAL
);
197 $this->assertSameSubject( NS_MAIN
, NS_TALK
);
198 $this->assertSameSubject( NS_USER
, NS_USER_TALK
);
200 $this->assertDifferentSubject( NS_PROJECT
, NS_TEMPLATE
);
201 $this->assertDifferentSubject( NS_SPECIAL
, NS_MAIN
);
205 * @covers MWNamespace::subjectEquals
207 public function testSpecialAndMediaAreDifferentSubjects() {
208 $this->assertDifferentSubject(
209 NS_MEDIA
, NS_SPECIAL
,
210 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
212 $this->assertDifferentSubject(
213 NS_SPECIAL
, NS_MEDIA
,
214 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
219 * @todo Implement testGetCanonicalNamespaces().
222 public function testGetCanonicalNamespaces() {
223 // Remove the following lines when you implement this test.
224 $this->markTestIncomplete(
225 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
230 * @todo Implement testGetCanonicalName().
233 public function testGetCanonicalName() {
234 // Remove the following lines when you implement this test.
235 $this->markTestIncomplete(
236 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
241 * @todo Implement testGetCanonicalIndex().
244 public function testGetCanonicalIndex() {
245 // Remove the following lines when you implement this test.
246 $this->markTestIncomplete(
247 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
253 * @todo Implement testGetValidNamespaces().
256 public function testGetValidNamespaces() {
257 // Remove the following lines when you implement this test.
258 $this->markTestIncomplete(
259 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
264 public function provideHasTalkNamespace() {
267 [ NS_SPECIAL
, false ],
272 [ NS_USER_TALK
, true ],
280 * @dataProvider provideHasTalkNamespace
281 * @covers MWNamespace::hasTalkNamespace
284 * @param bool $expected
286 public function testHasTalkNamespace( $index, $expected ) {
287 $actual = MWNamespace
::hasTalkNamespace( $index );
288 $this->assertSame( $actual, $expected, "NS $index" );
292 * @dataProvider provideHasTalkNamespace
293 * @covers MWNamespace::canTalk
296 * @param bool $expected
298 public function testCanTalk( $index, $expected ) {
299 $actual = MWNamespace
::canTalk( $index );
300 $this->assertSame( $actual, $expected, "NS $index" );
304 * @covers MWNamespace::isContent
306 public function testIsContent() {
307 // NS_MAIN is a content namespace per DefaultSettings.php
308 // and per function definition.
310 $this->assertIsContent( NS_MAIN
);
312 // Other namespaces which are not expected to be content
314 $this->assertIsNotContent( NS_MEDIA
);
315 $this->assertIsNotContent( NS_SPECIAL
);
316 $this->assertIsNotContent( NS_TALK
);
317 $this->assertIsNotContent( NS_USER
);
318 $this->assertIsNotContent( NS_CATEGORY
);
319 $this->assertIsNotContent( 100 );
323 * Similar to testIsContent() but alters the $wgContentNamespaces
325 * @covers MWNamespace::isContent
327 public function testIsContentAdvanced() {
328 global $wgContentNamespaces;
330 // Test that user defined namespace #252 is not content
331 $this->assertIsNotContent( 252 );
333 // Bless namespace # 252 as a content namespace
334 $wgContentNamespaces[] = 252;
336 $this->assertIsContent( 252 );
338 // Makes sure NS_MAIN was not impacted
339 $this->assertIsContent( NS_MAIN
);
343 * @covers MWNamespace::isWatchable
345 public function testIsWatchable() {
346 // Specials namespaces are not watchable
347 $this->assertIsNotWatchable( NS_MEDIA
);
348 $this->assertIsNotWatchable( NS_SPECIAL
);
350 // Core defined namespaces are watchables
351 $this->assertIsWatchable( NS_MAIN
);
352 $this->assertIsWatchable( NS_TALK
);
354 // Additional, user defined namespaces are watchables
355 $this->assertIsWatchable( 100 );
356 $this->assertIsWatchable( 101 );
360 * @covers MWNamespace::hasSubpages
362 public function testHasSubpages() {
363 global $wgNamespacesWithSubpages;
365 // Special namespaces:
366 $this->assertHasNotSubpages( NS_MEDIA
);
367 $this->assertHasNotSubpages( NS_SPECIAL
);
369 // Namespaces without subpages
370 $this->assertHasNotSubpages( NS_MAIN
);
372 $wgNamespacesWithSubpages[NS_MAIN
] = true;
373 $this->assertHasSubpages( NS_MAIN
);
375 $wgNamespacesWithSubpages[NS_MAIN
] = false;
376 $this->assertHasNotSubpages( NS_MAIN
);
378 // Some namespaces with subpages
379 $this->assertHasSubpages( NS_TALK
);
380 $this->assertHasSubpages( NS_USER
);
381 $this->assertHasSubpages( NS_USER_TALK
);
385 * @covers MWNamespace::getContentNamespaces
387 public function testGetContentNamespaces() {
388 global $wgContentNamespaces;
392 MWNamespace
::getContentNamespaces(),
393 '$wgContentNamespaces is an array with only NS_MAIN by default'
396 # test !is_array( $wgcontentNamespaces )
397 $wgContentNamespaces = '';
398 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
400 $wgContentNamespaces = false;
401 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
403 $wgContentNamespaces = null;
404 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
406 $wgContentNamespaces = 5;
407 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
409 # test $wgContentNamespaces === []
410 $wgContentNamespaces = [];
411 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
413 # test !in_array( NS_MAIN, $wgContentNamespaces )
414 $wgContentNamespaces = [ NS_USER
, NS_CATEGORY
];
416 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
417 MWNamespace
::getContentNamespaces(),
418 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
421 # test other cases, return $wgcontentNamespaces as is
422 $wgContentNamespaces = [ NS_MAIN
];
425 MWNamespace
::getContentNamespaces()
428 $wgContentNamespaces = [ NS_MAIN
, NS_USER
, NS_CATEGORY
];
430 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
431 MWNamespace
::getContentNamespaces()
436 * @covers MWNamespace::getSubjectNamespaces
438 public function testGetSubjectNamespaces() {
439 $subjectsNS = MWNamespace
::getSubjectNamespaces();
440 $this->assertContains( NS_MAIN
, $subjectsNS,
441 "Talk namespaces should have NS_MAIN" );
442 $this->assertNotContains( NS_TALK
, $subjectsNS,
443 "Talk namespaces should have NS_TALK" );
445 $this->assertNotContains( NS_MEDIA
, $subjectsNS,
446 "Talk namespaces should not have NS_MEDIA" );
447 $this->assertNotContains( NS_SPECIAL
, $subjectsNS,
448 "Talk namespaces should not have NS_SPECIAL" );
452 * @covers MWNamespace::getTalkNamespaces
454 public function testGetTalkNamespaces() {
455 $talkNS = MWNamespace
::getTalkNamespaces();
456 $this->assertContains( NS_TALK
, $talkNS,
457 "Subject namespaces should have NS_TALK" );
458 $this->assertNotContains( NS_MAIN
, $talkNS,
459 "Subject namespaces should not have NS_MAIN" );
461 $this->assertNotContains( NS_MEDIA
, $talkNS,
462 "Subject namespaces should not have NS_MEDIA" );
463 $this->assertNotContains( NS_SPECIAL
, $talkNS,
464 "Subject namespaces should not have NS_SPECIAL" );
468 * Some namespaces are always capitalized per code definition
469 * in MWNamespace::$alwaysCapitalizedNamespaces
470 * @covers MWNamespace::isCapitalized
472 public function testIsCapitalizedHardcodedAssertions() {
473 // NS_MEDIA and NS_FILE are treated the same
475 MWNamespace
::isCapitalized( NS_MEDIA
),
476 MWNamespace
::isCapitalized( NS_FILE
),
477 'NS_MEDIA and NS_FILE have same capitalization rendering'
480 // Boths are capitalized by default
481 $this->assertIsCapitalized( NS_MEDIA
);
482 $this->assertIsCapitalized( NS_FILE
);
484 // Always capitalized namespaces
485 // @see MWNamespace::$alwaysCapitalizedNamespaces
486 $this->assertIsCapitalized( NS_SPECIAL
);
487 $this->assertIsCapitalized( NS_USER
);
488 $this->assertIsCapitalized( NS_MEDIAWIKI
);
492 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
493 * global $wgCapitalLink setting to have extended coverage.
495 * MWNamespace::isCapitalized() rely on two global settings:
496 * $wgCapitalLinkOverrides = []; by default
497 * $wgCapitalLinks = true; by default
498 * This function test $wgCapitalLinks
500 * Global setting correctness is tested against the NS_PROJECT and
501 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
502 * @covers MWNamespace::isCapitalized
504 public function testIsCapitalizedWithWgCapitalLinks() {
505 global $wgCapitalLinks;
507 $this->assertIsCapitalized( NS_PROJECT
);
508 $this->assertIsCapitalized( NS_PROJECT_TALK
);
510 $wgCapitalLinks = false;
512 // hardcoded namespaces (see above function) are still capitalized:
513 $this->assertIsCapitalized( NS_SPECIAL
);
514 $this->assertIsCapitalized( NS_USER
);
515 $this->assertIsCapitalized( NS_MEDIAWIKI
);
517 // setting is correctly applied
518 $this->assertIsNotCapitalized( NS_PROJECT
);
519 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
523 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
524 * testing the $wgCapitalLinkOverrides global.
526 * @todo split groups of assertions in autonomous testing functions
527 * @covers MWNamespace::isCapitalized
529 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
530 global $wgCapitalLinkOverrides;
532 // Test default settings
533 $this->assertIsCapitalized( NS_PROJECT
);
534 $this->assertIsCapitalized( NS_PROJECT_TALK
);
536 // hardcoded namespaces (see above function) are capitalized:
537 $this->assertIsCapitalized( NS_SPECIAL
);
538 $this->assertIsCapitalized( NS_USER
);
539 $this->assertIsCapitalized( NS_MEDIAWIKI
);
541 // Hardcoded namespaces remains capitalized
542 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
543 $wgCapitalLinkOverrides[NS_USER
] = false;
544 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
546 $this->assertIsCapitalized( NS_SPECIAL
);
547 $this->assertIsCapitalized( NS_USER
);
548 $this->assertIsCapitalized( NS_MEDIAWIKI
);
550 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
551 $this->assertIsNotCapitalized( NS_PROJECT
);
553 $wgCapitalLinkOverrides[NS_PROJECT
] = true;
554 $this->assertIsCapitalized( NS_PROJECT
);
556 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
557 $this->assertIsCapitalized( NS_PROJECT
);
561 * @covers MWNamespace::hasGenderDistinction
563 public function testHasGenderDistinction() {
564 // Namespaces with gender distinctions
565 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
566 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
568 // Other ones, "genderless"
569 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
570 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
571 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
572 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
576 * @covers MWNamespace::isNonincludable
578 public function testIsNonincludable() {
579 global $wgNonincludableNamespaces;
581 $wgNonincludableNamespaces = [ NS_USER
];
583 $this->assertTrue( MWNamespace
::isNonincludable( NS_USER
) );
584 $this->assertFalse( MWNamespace
::isNonincludable( NS_TEMPLATE
) );
587 # ###### HELPERS ###########################################################
588 function __call( $method, $args ) {
589 // Call the real method if it exists
590 if ( method_exists( $this, $method ) ) {
591 return $this->$method( $args );
595 '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/',
599 # Interprets arguments:
601 $msg = isset( $args[1] ) ?
$args[1] : " dummy message";
603 # Forge the namespace constant name:
605 $ns_name = "NS_MAIN";
607 $ns_name = "NS_" . strtoupper( MWNamespace
::getCanonicalName( $ns ) );
609 # ... and the MWNamespace method name
610 $nsMethod = strtolower( $m[1] ) . $m[3];
612 $expect = ( $m[2] === '' );
613 $expect_name = $expect ?
'TRUE' : 'FALSE';
615 return $this->assertEquals( $expect,
616 MWNamespace
::$nsMethod( $ns, $msg ),
617 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
621 throw new Exception( __METHOD__
. " could not find a method named $method\n" );
624 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
625 $this->assertTrue( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );
628 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
629 $this->assertFalse( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );