3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
9 * Test class for MWNamespace.
10 * Generated by PHPUnit on 2011-02-20 at 21:01:55.
13 class MWNamespaceTest
extends MediaWikiTestCase
{
14 protected function setUp() {
17 $this->setMwGlobals( array(
18 'wgContentNamespaces' => array( NS_MAIN
),
19 'wgNamespacesWithSubpages' => array(
24 'wgCapitalLinks' => true,
25 'wgCapitalLinkOverrides' => array(),
26 'wgNonincludableNamespaces' => array(),
30 #### START OF TESTS #########################################################
33 * @todo Write more texts, handle $wgAllowImageMoving setting
35 public function testIsMovable() {
36 $this->assertFalse( MWNamespace
::isMovable( NS_CATEGORY
) );
37 # @todo FIXME: Write more tests!!
41 * Please make sure to change testIsTalk() if you change the assertions below
43 public function testIsSubject() {
45 $this->assertIsSubject( NS_MEDIA
);
46 $this->assertIsSubject( NS_SPECIAL
);
49 $this->assertIsSubject( NS_MAIN
);
50 $this->assertIsSubject( NS_USER
);
51 $this->assertIsSubject( 100 ); # user defined
54 $this->assertIsNotSubject( NS_TALK
);
55 $this->assertIsNotSubject( NS_USER_TALK
);
56 $this->assertIsNotSubject( 101 ); # user defined
60 * Reverse of testIsSubject().
61 * Please update testIsSubject() if you change assertions below
63 public function testIsTalk() {
65 $this->assertIsNotTalk( NS_MEDIA
);
66 $this->assertIsNotTalk( NS_SPECIAL
);
69 $this->assertIsNotTalk( NS_MAIN
);
70 $this->assertIsNotTalk( NS_USER
);
71 $this->assertIsNotTalk( 100 ); # user defined
74 $this->assertIsTalk( NS_TALK
);
75 $this->assertIsTalk( NS_USER_TALK
);
76 $this->assertIsTalk( 101 ); # user defined
81 public function testGetSubject() {
82 // Special namespaces are their own subjects
83 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
84 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
86 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
87 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
91 * Regular getTalk() calls
92 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
93 * the function testGetTalkExceptions()
95 public function testGetTalk() {
96 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
97 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_TALK
) );
98 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER
) );
99 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER_TALK
) );
103 * Exceptions with getTalk()
104 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
105 * @expectedException MWException
107 public function testGetTalkExceptionsForNsMedia() {
108 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
112 * Exceptions with getTalk()
113 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
114 * @expectedException MWException
116 public function testGetTalkExceptionsForNsSpecial() {
117 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
121 * Regular getAssociated() calls
122 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
123 * the function testGetAssociatedExceptions()
125 public function testGetAssociated() {
126 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
127 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
131 ### Exceptions with getAssociated()
132 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
133 ### an exception for them.
135 * @expectedException MWException
137 public function testGetAssociatedExceptionsForNsMedia() {
138 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
142 * @expectedException MWException
144 public function testGetAssociatedExceptionsForNsSpecial() {
145 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
149 * @todo Implement testExists().
152 public function testExists() {
153 // Remove the following lines when you implement this test.
154 $this->markTestIncomplete(
155 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
161 * Test MWNamespace::equals
162 * Note if we add a namespace registration system with keys like 'MAIN'
163 * we should add tests here for equivilance on things like 'MAIN' == 0
164 * and 'MAIN' == NS_MAIN.
166 public function testEquals() {
167 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, NS_MAIN
) );
168 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
169 $this->assertTrue( MWNamespace
::equals( NS_USER
, NS_USER
) );
170 $this->assertTrue( MWNamespace
::equals( NS_USER
, 2 ) );
171 $this->assertTrue( MWNamespace
::equals( NS_USER_TALK
, NS_USER_TALK
) );
172 $this->assertTrue( MWNamespace
::equals( NS_SPECIAL
, NS_SPECIAL
) );
173 $this->assertFalse( MWNamespace
::equals( NS_MAIN
, NS_TALK
) );
174 $this->assertFalse( MWNamespace
::equals( NS_USER
, NS_USER_TALK
) );
175 $this->assertFalse( MWNamespace
::equals( NS_PROJECT
, NS_TEMPLATE
) );
179 * Test MWNamespace::subjectEquals
181 public function testSubjectEquals() {
182 $this->assertSameSubject( NS_MAIN
, NS_MAIN
);
183 $this->assertSameSubject( NS_MAIN
, 0 ); // In case we make NS_MAIN 'MAIN'
184 $this->assertSameSubject( NS_USER
, NS_USER
);
185 $this->assertSameSubject( NS_USER
, 2 );
186 $this->assertSameSubject( NS_USER_TALK
, NS_USER_TALK
);
187 $this->assertSameSubject( NS_SPECIAL
, NS_SPECIAL
);
188 $this->assertSameSubject( NS_MAIN
, NS_TALK
);
189 $this->assertSameSubject( NS_USER
, NS_USER_TALK
);
191 $this->assertDifferentSubject( NS_PROJECT
, NS_TEMPLATE
);
192 $this->assertDifferentSubject( NS_SPECIAL
, NS_MAIN
);
195 public function testSpecialAndMediaAreDifferentSubjects() {
196 $this->assertDifferentSubject(
197 NS_MEDIA
, NS_SPECIAL
,
198 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
200 $this->assertDifferentSubject(
201 NS_SPECIAL
, NS_MEDIA
,
202 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
208 * @todo Implement testGetCanonicalNamespaces().
211 public function testGetCanonicalNamespaces() {
212 // Remove the following lines when you implement this test.
213 $this->markTestIncomplete(
214 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
219 * @todo Implement testGetCanonicalName().
222 public function testGetCanonicalName() {
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 testGetCanonicalIndex().
233 public function testGetCanonicalIndex() {
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 testGetValidNamespaces().
244 public function testGetValidNamespaces() {
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 public function testCanTalk() {
254 $this->assertCanNotTalk( NS_MEDIA
);
255 $this->assertCanNotTalk( NS_SPECIAL
);
257 $this->assertCanTalk( NS_MAIN
);
258 $this->assertCanTalk( NS_TALK
);
259 $this->assertCanTalk( NS_USER
);
260 $this->assertCanTalk( NS_USER_TALK
);
262 // User defined namespaces
263 $this->assertCanTalk( 100 );
264 $this->assertCanTalk( 101 );
269 public function testIsContent() {
270 // NS_MAIN is a content namespace per DefaultSettings.php
271 // and per function definition.
273 $this->assertIsContent( NS_MAIN
);
275 // Other namespaces which are not expected to be content
277 $this->assertIsNotContent( NS_MEDIA
);
278 $this->assertIsNotContent( NS_SPECIAL
);
279 $this->assertIsNotContent( NS_TALK
);
280 $this->assertIsNotContent( NS_USER
);
281 $this->assertIsNotContent( NS_CATEGORY
);
282 $this->assertIsNotContent( 100 );
286 * Similar to testIsContent() but alters the $wgContentNamespaces
289 public function testIsContentAdvanced() {
290 global $wgContentNamespaces;
292 // Test that user defined namespace #252 is not content
293 $this->assertIsNotContent( 252 );
295 // Bless namespace # 252 as a content namespace
296 $wgContentNamespaces[] = 252;
298 $this->assertIsContent( 252 );
300 // Makes sure NS_MAIN was not impacted
301 $this->assertIsContent( NS_MAIN
);
304 public function testIsWatchable() {
305 // Specials namespaces are not watchable
306 $this->assertIsNotWatchable( NS_MEDIA
);
307 $this->assertIsNotWatchable( NS_SPECIAL
);
309 // Core defined namespaces are watchables
310 $this->assertIsWatchable( NS_MAIN
);
311 $this->assertIsWatchable( NS_TALK
);
313 // Additional, user defined namespaces are watchables
314 $this->assertIsWatchable( 100 );
315 $this->assertIsWatchable( 101 );
318 public function testHasSubpages() {
319 global $wgNamespacesWithSubpages;
321 // Special namespaces:
322 $this->assertHasNotSubpages( NS_MEDIA
);
323 $this->assertHasNotSubpages( NS_SPECIAL
);
325 // Namespaces without subpages
326 $this->assertHasNotSubpages( NS_MAIN
);
328 $wgNamespacesWithSubpages[NS_MAIN
] = true;
329 $this->assertHasSubpages( NS_MAIN
);
331 $wgNamespacesWithSubpages[NS_MAIN
] = false;
332 $this->assertHasNotSubpages( NS_MAIN
);
334 // Some namespaces with subpages
335 $this->assertHasSubpages( NS_TALK
);
336 $this->assertHasSubpages( NS_USER
);
337 $this->assertHasSubpages( NS_USER_TALK
);
342 public function testGetContentNamespaces() {
343 global $wgContentNamespaces;
347 MWNamespace
::getcontentNamespaces(),
348 '$wgContentNamespaces is an array with only NS_MAIN by default'
352 # test !is_array( $wgcontentNamespaces )
353 $wgContentNamespaces = '';
354 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
356 $wgContentNamespaces = false;
357 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
359 $wgContentNamespaces = null;
360 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
362 $wgContentNamespaces = 5;
363 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
365 # test $wgContentNamespaces === array()
366 $wgContentNamespaces = array();
367 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
369 # test !in_array( NS_MAIN, $wgContentNamespaces )
370 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
372 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
373 MWNamespace
::getcontentNamespaces(),
374 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
377 # test other cases, return $wgcontentNamespaces as is
378 $wgContentNamespaces = array( NS_MAIN
);
381 MWNamespace
::getcontentNamespaces()
384 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
386 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
387 MWNamespace
::getcontentNamespaces()
393 public function testGetSubjectNamespaces() {
394 $subjectsNS = MWNamespace
::getSubjectNamespaces();
395 $this->assertContains( NS_MAIN
, $subjectsNS,
396 "Talk namespaces should have NS_MAIN" );
397 $this->assertNotContains( NS_TALK
, $subjectsNS,
398 "Talk namespaces should have NS_TALK" );
400 $this->assertNotContains( NS_MEDIA
, $subjectsNS,
401 "Talk namespaces should not have NS_MEDIA" );
402 $this->assertNotContains( NS_SPECIAL
, $subjectsNS,
403 "Talk namespaces should not have NS_SPECIAL" );
408 public function testGetTalkNamespaces() {
409 $talkNS = MWNamespace
::getTalkNamespaces();
410 $this->assertContains( NS_TALK
, $talkNS,
411 "Subject namespaces should have NS_TALK" );
412 $this->assertNotContains( NS_MAIN
, $talkNS,
413 "Subject namespaces should not have NS_MAIN" );
415 $this->assertNotContains( NS_MEDIA
, $talkNS,
416 "Subject namespaces should not have NS_MEDIA" );
417 $this->assertNotContains( NS_SPECIAL
, $talkNS,
418 "Subject namespaces should not have NS_SPECIAL" );
422 * Some namespaces are always capitalized per code definition
423 * in MWNamespace::$alwaysCapitalizedNamespaces
425 public function testIsCapitalizedHardcodedAssertions() {
426 // NS_MEDIA and NS_FILE are treated the same
428 MWNamespace
::isCapitalized( NS_MEDIA
),
429 MWNamespace
::isCapitalized( NS_FILE
),
430 'NS_MEDIA and NS_FILE have same capitalization rendering'
433 // Boths are capitalized by default
434 $this->assertIsCapitalized( NS_MEDIA
);
435 $this->assertIsCapitalized( NS_FILE
);
437 // Always capitalized namespaces
438 // @see MWNamespace::$alwaysCapitalizedNamespaces
439 $this->assertIsCapitalized( NS_SPECIAL
);
440 $this->assertIsCapitalized( NS_USER
);
441 $this->assertIsCapitalized( NS_MEDIAWIKI
);
445 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
446 * global $wgCapitalLink setting to have extended coverage.
448 * MWNamespace::isCapitalized() rely on two global settings:
449 * $wgCapitalLinkOverrides = array(); by default
450 * $wgCapitalLinks = true; by default
451 * This function test $wgCapitalLinks
453 * Global setting correctness is tested against the NS_PROJECT and
454 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
456 public function testIsCapitalizedWithWgCapitalLinks() {
457 global $wgCapitalLinks;
459 $this->assertIsCapitalized( NS_PROJECT
);
460 $this->assertIsCapitalized( NS_PROJECT_TALK
);
462 $wgCapitalLinks = false;
464 // hardcoded namespaces (see above function) are still capitalized:
465 $this->assertIsCapitalized( NS_SPECIAL
);
466 $this->assertIsCapitalized( NS_USER
);
467 $this->assertIsCapitalized( NS_MEDIAWIKI
);
469 // setting is correctly applied
470 $this->assertIsNotCapitalized( NS_PROJECT
);
471 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
475 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
476 * testing the $wgCapitalLinkOverrides global.
478 * @todo split groups of assertions in autonomous testing functions
480 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
481 global $wgCapitalLinkOverrides;
483 // Test default settings
484 $this->assertIsCapitalized( NS_PROJECT
);
485 $this->assertIsCapitalized( NS_PROJECT_TALK
);
487 // hardcoded namespaces (see above function) are capitalized:
488 $this->assertIsCapitalized( NS_SPECIAL
);
489 $this->assertIsCapitalized( NS_USER
);
490 $this->assertIsCapitalized( NS_MEDIAWIKI
);
492 // Hardcoded namespaces remains capitalized
493 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
494 $wgCapitalLinkOverrides[NS_USER
] = false;
495 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
497 $this->assertIsCapitalized( NS_SPECIAL
);
498 $this->assertIsCapitalized( NS_USER
);
499 $this->assertIsCapitalized( NS_MEDIAWIKI
);
501 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
502 $this->assertIsNotCapitalized( NS_PROJECT
);
504 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
505 $this->assertIsCapitalized( NS_PROJECT
);
507 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
508 $this->assertIsCapitalized( NS_PROJECT
);
511 public function testHasGenderDistinction() {
512 // Namespaces with gender distinctions
513 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
514 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
516 // Other ones, "genderless"
517 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
518 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
519 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
520 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
524 public function testIsNonincludable() {
525 global $wgNonincludableNamespaces;
527 $wgNonincludableNamespaces = array( NS_USER
);
529 $this->assertTrue( MWNamespace
::isNonincludable( NS_USER
) );
530 $this->assertFalse( MWNamespace
::isNonincludable( NS_TEMPLATE
) );
533 ####### HELPERS ###########################################################
534 function __call( $method, $args ) {
535 // Call the real method if it exists
536 if( method_exists($this, $method ) ) {
537 return $this->$method( $args );
540 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
541 # Interprets arguments:
543 $msg = isset($args[1]) ?
$args[1] : " dummy message";
545 # Forge the namespace constant name:
547 $ns_name = "NS_MAIN";
549 $ns_name = "NS_" . strtoupper( MWNamespace
::getCanonicalName( $ns ) );
551 # ... and the MWNamespace method name
552 $nsMethod = strtolower( $m[1] ) . $m[3];
554 $expect = ($m[2] === '');
555 $expect_name = $expect ?
'TRUE' : 'FALSE';
557 return $this->assertEquals( $expect,
558 MWNamespace
::$nsMethod( $ns, $msg ),
559 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
563 throw new Exception( __METHOD__
. " could not find a method named $method\n" );
566 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
567 $this->assertTrue( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );
569 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
570 $this->assertFalse( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );