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
{
15 * Sets up the fixture, for example, opens a network connection.
16 * This method is called before a test is executed.
18 protected function setUp() {
22 * Tears down the fixture, for example, closes a network connection.
23 * This method is called after a test is executed.
25 protected function tearDown() {
29 #### START OF TESTS #########################################################
32 * @todo Write more texts, handle $wgAllowImageMoving setting
34 public function testIsMovable() {
35 $this->assertFalse( MWNamespace
::isMovable( NS_CATEGORY
) );
36 # @todo FIXME: Write more tests!!
40 * Please make sure to change testIsTalk() if you change the assertions below
42 public function testIsSubject() {
44 $this->assertIsSubject( NS_MEDIA
);
45 $this->assertIsSubject( NS_SPECIAL
);
48 $this->assertIsSubject( NS_MAIN
);
49 $this->assertIsSubject( NS_USER
);
50 $this->assertIsSubject( 100 ); # user defined
53 $this->assertIsNotSubject( NS_TALK
);
54 $this->assertIsNotSubject( NS_USER_TALK
);
55 $this->assertIsNotSubject( 101 ); # user defined
59 * Reverse of testIsSubject().
60 * Please update testIsSubject() if you change assertions below
62 public function testIsTalk() {
64 $this->assertIsNotTalk( NS_MEDIA
);
65 $this->assertIsNotTalk( NS_SPECIAL
);
68 $this->assertIsNotTalk( NS_MAIN
);
69 $this->assertIsNotTalk( NS_USER
);
70 $this->assertIsNotTalk( 100 ); # user defined
73 $this->assertIsTalk( NS_TALK
);
74 $this->assertIsTalk( NS_USER_TALK
);
75 $this->assertIsTalk( 101 ); # user defined
80 public function testGetSubject() {
81 // Special namespaces are their own subjects
82 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
83 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
85 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
86 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
90 * Regular getTalk() calls
91 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
92 * the function testGetTalkExceptions()
94 public function testGetTalk() {
95 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
96 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_TALK
) );
97 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER
) );
98 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER_TALK
) );
102 * Exceptions with getTalk()
103 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
104 * @expectedException MWException
106 public function testGetTalkExceptionsForNsMedia() {
107 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
111 * Exceptions with getTalk()
112 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
113 * @expectedException MWException
115 public function testGetTalkExceptionsForNsSpecial() {
116 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
120 * Regular getAssociated() calls
121 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
122 * the function testGetAssociatedExceptions()
124 public function testGetAssociated() {
125 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
126 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
130 ### Exceptions with getAssociated()
131 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
132 ### an exception for them.
134 * @expectedException MWException
136 public function testGetAssociatedExceptionsForNsMedia() {
137 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
141 * @expectedException MWException
143 public function testGetAssociatedExceptionsForNsSpecial() {
144 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
148 * @todo Implement testExists().
151 public function testExists() {
152 // Remove the following lines when you implement this test.
153 $this->markTestIncomplete(
154 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
160 * Test MWNamespace::equals
161 * Note if we add a namespace registration system with keys like 'MAIN'
162 * we should add tests here for equivilance on things like 'MAIN' == 0
163 * and 'MAIN' == NS_MAIN.
165 public function testEquals() {
166 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, NS_MAIN
) );
167 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
168 $this->assertTrue( MWNamespace
::equals( NS_USER
, NS_USER
) );
169 $this->assertTrue( MWNamespace
::equals( NS_USER
, 2 ) );
170 $this->assertTrue( MWNamespace
::equals( NS_USER_TALK
, NS_USER_TALK
) );
171 $this->assertTrue( MWNamespace
::equals( NS_SPECIAL
, NS_SPECIAL
) );
172 $this->assertFalse( MWNamespace
::equals( NS_MAIN
, NS_TALK
) );
173 $this->assertFalse( MWNamespace
::equals( NS_USER
, NS_USER_TALK
) );
174 $this->assertFalse( MWNamespace
::equals( NS_PROJECT
, NS_TEMPLATE
) );
178 * Test MWNamespace::subjectEquals
180 public function testSubjectEquals() {
181 $this->assertSameSubject( NS_MAIN
, NS_MAIN
);
182 $this->assertSameSubject( NS_MAIN
, 0 ); // In case we make NS_MAIN 'MAIN'
183 $this->assertSameSubject( NS_USER
, NS_USER
);
184 $this->assertSameSubject( NS_USER
, 2 );
185 $this->assertSameSubject( NS_USER_TALK
, NS_USER_TALK
);
186 $this->assertSameSubject( NS_SPECIAL
, NS_SPECIAL
);
187 $this->assertSameSubject( NS_MAIN
, NS_TALK
);
188 $this->assertSameSubject( NS_USER
, NS_USER_TALK
);
190 $this->assertDifferentSubject( NS_PROJECT
, NS_TEMPLATE
);
191 $this->assertDifferentSubject( NS_SPECIAL
, NS_MAIN
);
194 public function testSpecialAndMediaAreDifferentSubjects() {
195 $this->assertDifferentSubject(
196 NS_MEDIA
, NS_SPECIAL
,
197 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
199 $this->assertDifferentSubject(
200 NS_SPECIAL
, NS_MEDIA
,
201 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
207 * @todo Implement testGetCanonicalNamespaces().
210 public function testGetCanonicalNamespaces() {
211 // Remove the following lines when you implement this test.
212 $this->markTestIncomplete(
213 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
218 * @todo Implement testGetCanonicalName().
221 public function testGetCanonicalName() {
222 // Remove the following lines when you implement this test.
223 $this->markTestIncomplete(
224 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
229 * @todo Implement testGetCanonicalIndex().
232 public function testGetCanonicalIndex() {
233 // Remove the following lines when you implement this test.
234 $this->markTestIncomplete(
235 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
240 * @todo Implement testGetValidNamespaces().
243 public function testGetValidNamespaces() {
244 // Remove the following lines when you implement this test.
245 $this->markTestIncomplete(
246 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
252 public function testCanTalk() {
253 $this->assertCanNotTalk( NS_MEDIA
);
254 $this->assertCanNotTalk( NS_SPECIAL
);
256 $this->assertCanTalk( NS_MAIN
);
257 $this->assertCanTalk( NS_TALK
);
258 $this->assertCanTalk( NS_USER
);
259 $this->assertCanTalk( NS_USER_TALK
);
261 // User defined namespaces
262 $this->assertCanTalk( 100 );
263 $this->assertCanTalk( 101 );
268 public function testIsContent() {
269 // NS_MAIN is a content namespace per DefaultSettings.php
270 // and per function definition.
271 $this->assertIsContent( NS_MAIN
);
273 global $wgContentNamespaces;
275 $saved = $wgContentNamespaces;
277 $wgContentNamespaces[] = NS_MAIN
;
278 $this->assertIsContent( NS_MAIN
);
280 // Other namespaces which are not expected to be content
281 if ( isset( $wgContentNamespaces[NS_MEDIA
] ) ) {
282 unset( $wgContentNamespaces[NS_MEDIA
] );
284 $this->assertIsNotContent( NS_MEDIA
);
286 if ( isset( $wgContentNamespaces[NS_SPECIAL
] ) ) {
287 unset( $wgContentNamespaces[NS_SPECIAL
] );
289 $this->assertIsNotContent( NS_SPECIAL
);
291 if ( isset( $wgContentNamespaces[NS_TALK
] ) ) {
292 unset( $wgContentNamespaces[NS_TALK
] );
294 $this->assertIsNotContent( NS_TALK
);
296 if ( isset( $wgContentNamespaces[NS_USER
] ) ) {
297 unset( $wgContentNamespaces[NS_USER
] );
299 $this->assertIsNotContent( NS_USER
);
301 if ( isset( $wgContentNamespaces[NS_CATEGORY
] ) ) {
302 unset( $wgContentNamespaces[NS_CATEGORY
] );
304 $this->assertIsNotContent( NS_CATEGORY
);
306 if ( isset( $wgContentNamespaces[100] ) ) {
307 unset( $wgContentNamespaces[100] );
309 $this->assertIsNotContent( 100 );
311 $wgContentNamespaces = $saved;
315 * Similar to testIsContent() but alters the $wgContentNamespaces
318 public function testIsContentWithAdditionsInWgContentNamespaces() {
319 // NS_MAIN is a content namespace per DefaultSettings.php
320 // and per function definition.
321 $this->assertIsContent( NS_MAIN
);
323 // Tests that user defined namespace #252 is not content:
324 $this->assertIsNotContent( 252 );
326 # @todo FIXME: Is global saving really required for PHPUnit?
327 // Bless namespace # 252 as a content namespace
328 global $wgContentNamespaces;
329 $savedGlobal = $wgContentNamespaces;
330 $wgContentNamespaces[] = 252;
331 $this->assertIsContent( 252 );
333 // Makes sure NS_MAIN was not impacted
334 $this->assertIsContent( NS_MAIN
);
337 $wgContentNamespaces = $savedGlobal;
339 // Verify namespaces after global restauration
340 $this->assertIsContent( NS_MAIN
);
341 $this->assertIsNotContent( 252 );
344 public function testIsWatchable() {
345 // Specials namespaces are not watchable
346 $this->assertIsNotWatchable( NS_MEDIA
);
347 $this->assertIsNotWatchable( NS_SPECIAL
);
349 // Core defined namespaces are watchables
350 $this->assertIsWatchable( NS_MAIN
);
351 $this->assertIsWatchable( NS_TALK
);
353 // Additional, user defined namespaces are watchables
354 $this->assertIsWatchable( 100 );
355 $this->assertIsWatchable( 101 );
358 public function testHasSubpages() {
359 // Special namespaces:
360 $this->assertHasNotSubpages( NS_MEDIA
);
361 $this->assertHasNotSubpages( NS_SPECIAL
);
363 // namespaces without subpages
365 global $wgNamespacesWithSubpages;
367 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages ) ) {
368 $saved = $wgNamespacesWithSubpages[NS_MAIN
];
369 unset( $wgNamespacesWithSubpages[NS_MAIN
] );
372 $this->assertHasNotSubpages( NS_MAIN
);
374 $wgNamespacesWithSubpages[NS_MAIN
] = true;
375 $this->assertHasSubpages( NS_MAIN
);
376 $wgNamespacesWithSubpages[NS_MAIN
] = false;
377 $this->assertHasNotSubpages( NS_MAIN
);
380 if( $saved !== null ) {
381 $wgNamespacesWithSubpages[NS_MAIN
] = $saved;
384 // Some namespaces with subpages
385 $this->assertHasSubpages( NS_TALK
);
386 $this->assertHasSubpages( NS_USER
);
387 $this->assertHasSubpages( NS_USER_TALK
);
392 public function testGetContentNamespaces() {
395 MWNamespace
::getcontentNamespaces(),
396 '$wgContentNamespaces is an array with only NS_MAIN by default'
399 global $wgContentNamespaces;
401 $saved = $wgContentNamespaces;
402 # test !is_array( $wgcontentNamespaces )
403 $wgContentNamespaces = '';
404 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
405 $wgContentNamespaces = false;
406 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
407 $wgContentNamespaces = null;
408 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
409 $wgContentNamespaces = 5;
410 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
412 # test $wgContentNamespaces === array()
413 $wgContentNamespaces = array();
414 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
416 # test !in_array( NS_MAIN, $wgContentNamespaces )
417 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
419 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
420 MWNamespace
::getcontentNamespaces(),
421 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
424 # test other cases, return $wgcontentNamespaces as is
425 $wgContentNamespaces = array( NS_MAIN
);
428 MWNamespace
::getcontentNamespaces()
431 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
433 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
434 MWNamespace
::getcontentNamespaces()
437 $wgContentNamespaces = $saved;
441 * Some namespaces are always capitalized per code definition
442 * in MWNamespace::$alwaysCapitalizedNamespaces
444 public function testIsCapitalizedHardcodedAssertions() {
445 // NS_MEDIA and NS_FILE are treated the same
447 MWNamespace
::isCapitalized( NS_MEDIA
),
448 MWNamespace
::isCapitalized( NS_FILE
),
449 'NS_MEDIA and NS_FILE have same capitalization rendering'
452 // Boths are capitalized by default
453 $this->assertIsCapitalized( NS_MEDIA
);
454 $this->assertIsCapitalized( NS_FILE
);
456 // Always capitalized namespaces
457 // @see MWNamespace::$alwaysCapitalizedNamespaces
458 $this->assertIsCapitalized( NS_SPECIAL
);
459 $this->assertIsCapitalized( NS_USER
);
460 $this->assertIsCapitalized( NS_MEDIAWIKI
);
464 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
465 * global $wgCapitalLink setting to have extended coverage.
467 * MWNamespace::isCapitalized() rely on two global settings:
468 * $wgCapitalLinkOverrides = array(); by default
469 * $wgCapitalLinks = true; by default
470 * This function test $wgCapitalLinks
472 * Global setting correctness is tested against the NS_PROJECT and
473 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
475 public function testIsCapitalizedWithWgCapitalLinks() {
476 global $wgCapitalLinks;
477 // Save the global to easily reset to MediaWiki default settings
478 $savedGlobal = $wgCapitalLinks;
480 $wgCapitalLinks = true;
481 $this->assertIsCapitalized( NS_PROJECT
);
482 $this->assertIsCapitalized( NS_PROJECT_TALK
);
484 $wgCapitalLinks = false;
485 // hardcoded namespaces (see above function) are still capitalized:
486 $this->assertIsCapitalized( NS_SPECIAL
);
487 $this->assertIsCapitalized( NS_USER
);
488 $this->assertIsCapitalized( NS_MEDIAWIKI
);
489 // setting is correctly applied
490 $this->assertIsNotCapitalized( NS_PROJECT
);
491 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
493 // reset global state:
494 $wgCapitalLinks = $savedGlobal;
498 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
499 * testing the $wgCapitalLinkOverrides global.
501 * @todo split groups of assertions in autonomous testing functions
503 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
504 global $wgCapitalLinkOverrides;
505 // Save the global to easily reset to MediaWiki default settings
506 $savedGlobal = $wgCapitalLinkOverrides;
508 // Test default settings
509 $this->assertIsCapitalized( NS_PROJECT
);
510 $this->assertIsCapitalized( NS_PROJECT_TALK
);
511 // hardcoded namespaces (see above function) are capitalized:
512 $this->assertIsCapitalized( NS_SPECIAL
);
513 $this->assertIsCapitalized( NS_USER
);
514 $this->assertIsCapitalized( NS_MEDIAWIKI
);
516 // Hardcoded namespaces remains capitalized
517 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
518 $wgCapitalLinkOverrides[NS_USER
] = false;
519 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
520 $this->assertIsCapitalized( NS_SPECIAL
);
521 $this->assertIsCapitalized( NS_USER
);
522 $this->assertIsCapitalized( NS_MEDIAWIKI
);
524 $wgCapitalLinkOverrides = $savedGlobal;
525 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
526 $this->assertIsNotCapitalized( NS_PROJECT
);
527 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
528 $this->assertIsCapitalized( NS_PROJECT
);
529 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
530 $this->assertIsCapitalized( NS_PROJECT
);
532 // reset global state:
533 $wgCapitalLinkOverrides = $savedGlobal;
536 public function testHasGenderDistinction() {
537 // Namespaces with gender distinctions
538 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
539 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
541 // Other ones, "genderless"
542 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
543 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
544 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
545 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
549 ####### HELPERS ###########################################################
550 function __call( $method, $args ) {
551 // Call the real method if it exists
552 if( method_exists($this, $method ) ) {
553 return $this->$method( $args );
556 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
557 # Interprets arguments:
559 $msg = isset($args[1]) ?
$args[1] : " dummy message";
561 # Forge the namespace constant name:
563 $ns_name = "NS_MAIN";
565 $ns_name = "NS_" . strtoupper( MWNamespace
::getCanonicalName( $ns ) );
567 # ... and the MWNamespace method name
568 $nsMethod = strtolower( $m[1] ) . $m[3];
570 $expect = ($m[2] === '');
571 $expect_name = $expect ?
'TRUE' : 'FALSE';
573 return $this->assertEquals( $expect,
574 MWNamespace
::$nsMethod( $ns, $msg ),
575 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
579 throw new Exception( __METHOD__
. " could not find a method named $method\n" );
582 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
583 $this->assertTrue( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );
585 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
586 $this->assertFalse( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );