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;
442 public function testGetSubjectNamespaces() {
443 $subjectsNS = MWNamespace
::getSubjectNamespaces();
444 $this->assertContains( NS_MAIN
, $subjectsNS,
445 "Talk namespaces should have NS_MAIN" );
446 $this->assertNotContains( NS_TALK
, $subjectsNS,
447 "Talk namespaces should have NS_TALK" );
449 $this->assertNotContains( NS_MEDIA
, $subjectsNS,
450 "Talk namespaces should not have NS_MEDIA" );
451 $this->assertNotContains( NS_SPECIAL
, $subjectsNS,
452 "Talk namespaces should not have NS_SPECIAL" );
457 public function testGetTalkNamespaces() {
458 $talkNS = MWNamespace
::getTalkNamespaces();
459 $this->assertContains( NS_TALK
, $talkNS,
460 "Subject namespaces should have NS_TALK" );
461 $this->assertNotContains( NS_MAIN
, $talkNS,
462 "Subject namespaces should not have NS_MAIN" );
464 $this->assertNotContains( NS_MEDIA
, $talkNS,
465 "Subject namespaces should not have NS_MEDIA" );
466 $this->assertNotContains( NS_SPECIAL
, $talkNS,
467 "Subject namespaces should not have NS_SPECIAL" );
471 * Some namespaces are always capitalized per code definition
472 * in MWNamespace::$alwaysCapitalizedNamespaces
474 public function testIsCapitalizedHardcodedAssertions() {
475 // NS_MEDIA and NS_FILE are treated the same
477 MWNamespace
::isCapitalized( NS_MEDIA
),
478 MWNamespace
::isCapitalized( NS_FILE
),
479 'NS_MEDIA and NS_FILE have same capitalization rendering'
482 // Boths are capitalized by default
483 $this->assertIsCapitalized( NS_MEDIA
);
484 $this->assertIsCapitalized( NS_FILE
);
486 // Always capitalized namespaces
487 // @see MWNamespace::$alwaysCapitalizedNamespaces
488 $this->assertIsCapitalized( NS_SPECIAL
);
489 $this->assertIsCapitalized( NS_USER
);
490 $this->assertIsCapitalized( NS_MEDIAWIKI
);
494 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
495 * global $wgCapitalLink setting to have extended coverage.
497 * MWNamespace::isCapitalized() rely on two global settings:
498 * $wgCapitalLinkOverrides = array(); by default
499 * $wgCapitalLinks = true; by default
500 * This function test $wgCapitalLinks
502 * Global setting correctness is tested against the NS_PROJECT and
503 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
505 public function testIsCapitalizedWithWgCapitalLinks() {
506 global $wgCapitalLinks;
507 // Save the global to easily reset to MediaWiki default settings
508 $savedGlobal = $wgCapitalLinks;
510 $wgCapitalLinks = true;
511 $this->assertIsCapitalized( NS_PROJECT
);
512 $this->assertIsCapitalized( NS_PROJECT_TALK
);
514 $wgCapitalLinks = false;
515 // hardcoded namespaces (see above function) are still capitalized:
516 $this->assertIsCapitalized( NS_SPECIAL
);
517 $this->assertIsCapitalized( NS_USER
);
518 $this->assertIsCapitalized( NS_MEDIAWIKI
);
519 // setting is correctly applied
520 $this->assertIsNotCapitalized( NS_PROJECT
);
521 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
523 // reset global state:
524 $wgCapitalLinks = $savedGlobal;
528 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
529 * testing the $wgCapitalLinkOverrides global.
531 * @todo split groups of assertions in autonomous testing functions
533 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
534 global $wgCapitalLinkOverrides;
535 // Save the global to easily reset to MediaWiki default settings
536 $savedGlobal = $wgCapitalLinkOverrides;
538 // Test default settings
539 $this->assertIsCapitalized( NS_PROJECT
);
540 $this->assertIsCapitalized( NS_PROJECT_TALK
);
541 // hardcoded namespaces (see above function) are capitalized:
542 $this->assertIsCapitalized( NS_SPECIAL
);
543 $this->assertIsCapitalized( NS_USER
);
544 $this->assertIsCapitalized( NS_MEDIAWIKI
);
546 // Hardcoded namespaces remains capitalized
547 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
548 $wgCapitalLinkOverrides[NS_USER
] = false;
549 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
550 $this->assertIsCapitalized( NS_SPECIAL
);
551 $this->assertIsCapitalized( NS_USER
);
552 $this->assertIsCapitalized( NS_MEDIAWIKI
);
554 $wgCapitalLinkOverrides = $savedGlobal;
555 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
556 $this->assertIsNotCapitalized( NS_PROJECT
);
557 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
558 $this->assertIsCapitalized( NS_PROJECT
);
559 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
560 $this->assertIsCapitalized( NS_PROJECT
);
562 // reset global state:
563 $wgCapitalLinkOverrides = $savedGlobal;
566 public function testHasGenderDistinction() {
567 // Namespaces with gender distinctions
568 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
569 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
571 // Other ones, "genderless"
572 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
573 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
574 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
575 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
579 public function testIsNonincludable() {
580 global $wgNonincludableNamespaces;
581 $wgNonincludableNamespaces = array( NS_USER
);
583 $this->assertTrue( MWNamespace
::isNonincludable( NS_USER
) );
585 $this->assertFalse( MWNamespace
::isNonincludable( NS_TEMPLATE
) );
588 ####### HELPERS ###########################################################
589 function __call( $method, $args ) {
590 // Call the real method if it exists
591 if( method_exists($this, $method ) ) {
592 return $this->$method( $args );
595 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
596 # Interprets arguments:
598 $msg = isset($args[1]) ?
$args[1] : " dummy message";
600 # Forge the namespace constant name:
602 $ns_name = "NS_MAIN";
604 $ns_name = "NS_" . strtoupper( MWNamespace
::getCanonicalName( $ns ) );
606 # ... and the MWNamespace method name
607 $nsMethod = strtolower( $m[1] ) . $m[3];
609 $expect = ($m[2] === '');
610 $expect_name = $expect ?
'TRUE' : 'FALSE';
612 return $this->assertEquals( $expect,
613 MWNamespace
::$nsMethod( $ns, $msg ),
614 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
618 throw new Exception( __METHOD__
. " could not find a method named $method\n" );
621 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
622 $this->assertTrue( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );
624 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
625 $this->assertFalse( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );