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
58 $this->assertTrue( MWNamespace
::isMain( NS_MAIN
) == MWNamespace
::isSubject( NS_MAIN
) );
59 $this->assertTrue( MWNamespace
::isMain( NS_USER_TALK
) == MWNamespace
::isSubject( NS_USER_TALK
) );
63 * Reverse of testIsSubject().
64 * Please update testIsSubject() if you change assertions below
66 public function testIsTalk() {
68 $this->assertIsNotTalk( NS_MEDIA
);
69 $this->assertIsNotTalk( NS_SPECIAL
);
72 $this->assertIsNotTalk( NS_MAIN
);
73 $this->assertIsNotTalk( NS_USER
);
74 $this->assertIsNotTalk( 100 ); # user defined
77 $this->assertIsTalk( NS_TALK
);
78 $this->assertIsTalk( NS_USER_TALK
);
79 $this->assertIsTalk( 101 ); # user defined
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()
98 public function testGetTalk() {
99 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
100 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_TALK
) );
101 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER
) );
102 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER_TALK
) );
106 * Exceptions with getTalk()
107 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
108 * @expectedException MWException
110 public function testGetTalkExceptionsForNsMedia() {
111 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
115 * Exceptions with getTalk()
116 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
117 * @expectedException MWException
119 public function testGetTalkExceptionsForNsSpecial() {
120 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
124 * Regular getAssociated() calls
125 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
126 * the function testGetAssociatedExceptions()
128 public function testGetAssociated() {
129 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
130 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
134 ### Exceptions with getAssociated()
135 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
136 ### an exception for them.
138 * @expectedException MWException
140 public function testGetAssociatedExceptionsForNsMedia() {
141 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
145 * @expectedException MWException
147 public function testGetAssociatedExceptionsForNsSpecial() {
148 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
152 * @todo Implement testExists().
155 public function testExists() {
156 // Remove the following lines when you implement this test.
157 $this->markTestIncomplete(
158 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
164 * Test MWNamespace::equals
165 * Note if we add a namespace registration system with keys like 'MAIN'
166 * we should add tests here for equivilance on things like 'MAIN' == 0
167 * and 'MAIN' == NS_MAIN.
169 public function testEquals() {
170 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, NS_MAIN
) );
171 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
172 $this->assertTrue( MWNamespace
::equals( NS_USER
, NS_USER
) );
173 $this->assertTrue( MWNamespace
::equals( NS_USER
, 2 ) );
174 $this->assertTrue( MWNamespace
::equals( NS_USER_TALK
, NS_USER_TALK
) );
175 $this->assertTrue( MWNamespace
::equals( NS_SPECIAL
, NS_SPECIAL
) );
176 $this->assertFalse( MWNamespace
::equals( NS_MAIN
, NS_TALK
) );
177 $this->assertFalse( MWNamespace
::equals( NS_USER
, NS_USER_TALK
) );
178 $this->assertFalse( MWNamespace
::equals( NS_PROJECT
, NS_TEMPLATE
) );
182 * Test MWNamespace::subjectEquals
184 public function testSubjectEquals() {
185 $this->assertTrue( MWNamespace
::subjectEquals( NS_MAIN
, NS_MAIN
) );
186 $this->assertTrue( MWNamespace
::subjectEquals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
187 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER
, NS_USER
) );
188 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER
, 2 ) );
189 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER_TALK
, NS_USER_TALK
) );
190 $this->assertTrue( MWNamespace
::subjectEquals( NS_SPECIAL
, NS_SPECIAL
) );
191 $this->assertTrue( MWNamespace
::subjectEquals( NS_MAIN
, NS_TALK
) );
192 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER
, NS_USER_TALK
) );
193 $this->assertFalse( MWNamespace
::subjectEquals( NS_PROJECT
, NS_TEMPLATE
) );
194 $this->assertFalse( MWNamespace
::subjectEquals( NS_SPECIAL
, NS_MAIN
) );
197 public function testSpecialAndMediaAreDifferentSubjects() {
198 $this->assertFalse( MWNamespace
::subjectEquals(
200 ), "NS_MEDIA and NS_SPECIAL are different subhects" );
201 $this->assertFalse( MWNamespace
::subjectEquals(
203 ), "NS_SPECIAL and NS_MEDIA are different subhects" );
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.
272 $this->assertIsContent( NS_MAIN
);
274 global $wgContentNamespaces;
276 $saved = $wgContentNamespaces;
278 $wgContentNamespaces[] = NS_MAIN
;
279 $this->assertIsContent( NS_MAIN
);
281 // Other namespaces which are not expected to be content
282 if ( isset( $wgContentNamespaces[NS_MEDIA
] ) ) {
283 unset( $wgContentNamespaces[NS_MEDIA
] );
285 $this->assertIsNotContent( NS_MEDIA
);
287 if ( isset( $wgContentNamespaces[NS_SPECIAL
] ) ) {
288 unset( $wgContentNamespaces[NS_SPECIAL
] );
290 $this->assertIsNotContent( NS_SPECIAL
);
292 if ( isset( $wgContentNamespaces[NS_TALK
] ) ) {
293 unset( $wgContentNamespaces[NS_TALK
] );
295 $this->assertIsNotContent( NS_TALK
);
297 if ( isset( $wgContentNamespaces[NS_USER
] ) ) {
298 unset( $wgContentNamespaces[NS_USER
] );
300 $this->assertIsNotContent( NS_USER
);
302 if ( isset( $wgContentNamespaces[NS_CATEGORY
] ) ) {
303 unset( $wgContentNamespaces[NS_CATEGORY
] );
305 $this->assertIsNotContent( NS_CATEGORY
);
307 if ( isset( $wgContentNamespaces[100] ) ) {
308 unset( $wgContentNamespaces[100] );
310 $this->assertIsNotContent( 100 );
312 $wgContentNamespaces = $saved;
316 * Similar to testIsContent() but alters the $wgContentNamespaces
319 public function testIsContentWithAdditionsInWgContentNamespaces() {
320 // NS_MAIN is a content namespace per DefaultSettings.php
321 // and per function definition.
322 $this->assertIsContent( NS_MAIN
);
324 // Tests that user defined namespace #252 is not content:
325 $this->assertIsNotContent( 252 );
327 # @todo FIXME: Is global saving really required for PHPUnit?
328 // Bless namespace # 252 as a content namespace
329 global $wgContentNamespaces;
330 $savedGlobal = $wgContentNamespaces;
331 $wgContentNamespaces[] = 252;
332 $this->assertIsContent( 252 );
334 // Makes sure NS_MAIN was not impacted
335 $this->assertIsContent( NS_MAIN
);
338 $wgContentNamespaces = $savedGlobal;
340 // Verify namespaces after global restauration
341 $this->assertIsContent( NS_MAIN
);
342 $this->assertIsNotContent( 252 );
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 );
359 public function testHasSubpages() {
360 // Special namespaces:
361 $this->assertHasNotSubpages( NS_MEDIA
);
362 $this->assertHasNotSubpages( NS_SPECIAL
);
364 // namespaces without subpages
366 global $wgNamespacesWithSubpages;
368 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages ) ) {
369 $saved = $wgNamespacesWithSubpages[NS_MAIN
];
370 unset( $wgNamespacesWithSubpages[NS_MAIN
] );
373 $this->assertHasNotSubpages( NS_MAIN
);
375 $wgNamespacesWithSubpages[NS_MAIN
] = true;
376 $this->assertHasSubpages( NS_MAIN
);
377 $wgNamespacesWithSubpages[NS_MAIN
] = false;
378 $this->assertHasNotSubpages( NS_MAIN
);
381 if( $saved !== null ) {
382 $wgNamespacesWithSubpages[NS_MAIN
] = $saved;
385 // Some namespaces with subpages
386 $this->assertHasSubpages( NS_TALK
);
387 $this->assertHasSubpages( NS_USER
);
388 $this->assertHasSubpages( NS_USER_TALK
);
393 public function testGetContentNamespaces() {
396 MWNamespace
::getcontentNamespaces(),
397 '$wgContentNamespaces is an array with only NS_MAIN by default'
400 global $wgContentNamespaces;
402 $saved = $wgContentNamespaces;
403 # test !is_array( $wgcontentNamespaces )
404 $wgContentNamespaces = '';
405 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
406 $wgContentNamespaces = false;
407 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
408 $wgContentNamespaces = null;
409 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
410 $wgContentNamespaces = 5;
411 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
413 # test $wgContentNamespaces === array()
414 $wgContentNamespaces = array();
415 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
417 # test !in_array( NS_MAIN, $wgContentNamespaces )
418 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
420 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
421 MWNamespace
::getcontentNamespaces(),
422 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
425 # test other cases, return $wgcontentNamespaces as is
426 $wgContentNamespaces = array( NS_MAIN
);
429 MWNamespace
::getcontentNamespaces()
432 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
434 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
435 MWNamespace
::getcontentNamespaces()
438 $wgContentNamespaces = $saved;
442 * Some namespaces are always capitalized per code definition
443 * in MWNamespace::$alwaysCapitalizedNamespaces
445 public function testIsCapitalizedHardcodedAssertions() {
446 // NS_MEDIA and NS_FILE are treated the same
448 MWNamespace
::isCapitalized( NS_MEDIA
),
449 MWNamespace
::isCapitalized( NS_FILE
),
450 'NS_MEDIA and NS_FILE have same capitalization rendering'
453 // Boths are capitalized by default
454 $this->assertIsCapitalized( NS_MEDIA
);
455 $this->assertIsCapitalized( NS_FILE
);
457 // Always capitalized namespaces
458 // @see MWNamespace::$alwaysCapitalizedNamespaces
459 $this->assertIsCapitalized( NS_SPECIAL
);
460 $this->assertIsCapitalized( NS_USER
);
461 $this->assertIsCapitalized( NS_MEDIAWIKI
);
465 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
466 * global $wgCapitalLink setting to have extended coverage.
468 * MWNamespace::isCapitalized() rely on two global settings:
469 * $wgCapitalLinkOverrides = array(); by default
470 * $wgCapitalLinks = true; by default
471 * This function test $wgCapitalLinks
473 * Global setting correctness is tested against the NS_PROJECT and
474 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
476 public function testIsCapitalizedWithWgCapitalLinks() {
477 global $wgCapitalLinks;
478 // Save the global to easily reset to MediaWiki default settings
479 $savedGlobal = $wgCapitalLinks;
481 $wgCapitalLinks = true;
482 $this->assertIsCapitalized( NS_PROJECT
);
483 $this->assertIsCapitalized( NS_PROJECT_TALK
);
485 $wgCapitalLinks = false;
486 // hardcoded namespaces (see above function) are still capitalized:
487 $this->assertIsCapitalized( NS_SPECIAL
);
488 $this->assertIsCapitalized( NS_USER
);
489 $this->assertIsCapitalized( NS_MEDIAWIKI
);
490 // setting is correctly applied
491 $this->assertIsNotCapitalized( NS_PROJECT
);
492 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
494 // reset global state:
495 $wgCapitalLinks = $savedGlobal;
499 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
500 * testing the $wgCapitalLinkOverrides global.
502 * @todo split groups of assertions in autonomous testing functions
504 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
505 global $wgCapitalLinkOverrides;
506 // Save the global to easily reset to MediaWiki default settings
507 $savedGlobal = $wgCapitalLinkOverrides;
509 // Test default settings
510 $this->assertIsCapitalized( NS_PROJECT
);
511 $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;
521 $this->assertIsCapitalized( NS_SPECIAL
);
522 $this->assertIsCapitalized( NS_USER
);
523 $this->assertIsCapitalized( NS_MEDIAWIKI
);
525 $wgCapitalLinkOverrides = $savedGlobal;
526 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
527 $this->assertIsNotCapitalized( NS_PROJECT
);
528 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
529 $this->assertIsCapitalized( NS_PROJECT
);
530 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
531 $this->assertIsCapitalized( NS_PROJECT
);
533 // reset global state:
534 $wgCapitalLinkOverrides = $savedGlobal;
537 public function testHasGenderDistinction() {
538 // Namespaces with gender distinctions
539 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
540 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
542 // Other ones, "genderless"
543 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
544 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
545 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
546 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
550 ####### HELPERS ###########################################################
551 function __call( $method, $args ) {
552 // Call the real method if it exists
553 if( method_exists($this, $method ) ) {
554 return $this->$method( $args );
557 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
558 # Interprets arguments:
560 $msg = isset($args[1]) ?
$args[1] : " dummy message";
562 # Forge the namespace constant name:
564 $ns_name = "NS_MAIN";
566 $ns_name = "NS_" . strtoupper( MWNamespace
::getCanonicalName( $ns ) );
568 # ... and the MWNamespace method name
569 $nsMethod = strtolower( $m[1] ) . $m[3];
571 $expect = ($m[2] === '');
572 $expect_name = $expect ?
'TRUE' : 'FALSE';
574 return $this->assertEquals( $expect,
575 MWNamespace
::$nsMethod( $ns, $msg ),
576 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
580 throw new Exception( __METHOD__
. " could not find a method named $method\n" );