MediaWiki  REL1_23
MWNamespaceTest.php
Go to the documentation of this file.
00001 <?php
00015 class MWNamespaceTest extends MediaWikiTestCase {
00016     protected function setUp() {
00017         parent::setUp();
00018 
00019         $this->setMwGlobals( array(
00020             'wgContentNamespaces' => array( NS_MAIN ),
00021             'wgNamespacesWithSubpages' => array(
00022                 NS_TALK => true,
00023                 NS_USER => true,
00024                 NS_USER_TALK => true,
00025             ),
00026             'wgCapitalLinks' => true,
00027             'wgCapitalLinkOverrides' => array(),
00028             'wgNonincludableNamespaces' => array(),
00029         ) );
00030     }
00031 
00032 #### START OF TESTS #########################################################
00033 
00038     public function testIsMovable() {
00039         $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
00040         # @todo FIXME: Write more tests!!
00041     }
00042 
00047     public function testIsSubject() {
00048         // Special namespaces
00049         $this->assertIsSubject( NS_MEDIA );
00050         $this->assertIsSubject( NS_SPECIAL );
00051 
00052         // Subject pages
00053         $this->assertIsSubject( NS_MAIN );
00054         $this->assertIsSubject( NS_USER );
00055         $this->assertIsSubject( 100 ); # user defined
00056 
00057         // Talk pages
00058         $this->assertIsNotSubject( NS_TALK );
00059         $this->assertIsNotSubject( NS_USER_TALK );
00060         $this->assertIsNotSubject( 101 ); # user defined
00061     }
00062 
00068     public function testIsTalk() {
00069         // Special namespaces
00070         $this->assertIsNotTalk( NS_MEDIA );
00071         $this->assertIsNotTalk( NS_SPECIAL );
00072 
00073         // Subject pages
00074         $this->assertIsNotTalk( NS_MAIN );
00075         $this->assertIsNotTalk( NS_USER );
00076         $this->assertIsNotTalk( 100 ); # user defined
00077 
00078         // Talk pages
00079         $this->assertIsTalk( NS_TALK );
00080         $this->assertIsTalk( NS_USER_TALK );
00081         $this->assertIsTalk( 101 ); # user defined
00082     }
00083 
00087     public function testGetSubject() {
00088         // Special namespaces are their own subjects
00089         $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
00090         $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
00091 
00092         $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
00093         $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
00094     }
00095 
00102     public function testGetTalk() {
00103         $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
00104         $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) );
00105         $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) );
00106         $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) );
00107     }
00108 
00115     public function testGetTalkExceptionsForNsMedia() {
00116         $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
00117     }
00118 
00125     public function testGetTalkExceptionsForNsSpecial() {
00126         $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
00127     }
00128 
00135     public function testGetAssociated() {
00136         $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
00137         $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
00138     }
00139 
00140     ### Exceptions with getAssociated()
00141     ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
00142     ### an exception for them.
00143 
00147     public function testGetAssociatedExceptionsForNsMedia() {
00148         $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
00149     }
00150 
00155     public function testGetAssociatedExceptionsForNsSpecial() {
00156         $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
00157     }
00158 
00162     /*
00163     public function testExists() {
00164         // Remove the following lines when you implement this test.
00165         $this->markTestIncomplete(
00166           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00167         );
00168     }
00169     */
00170 
00178     public function testEquals() {
00179         $this->assertTrue( MWNamespace::equals( NS_MAIN, NS_MAIN ) );
00180         $this->assertTrue( MWNamespace::equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
00181         $this->assertTrue( MWNamespace::equals( NS_USER, NS_USER ) );
00182         $this->assertTrue( MWNamespace::equals( NS_USER, 2 ) );
00183         $this->assertTrue( MWNamespace::equals( NS_USER_TALK, NS_USER_TALK ) );
00184         $this->assertTrue( MWNamespace::equals( NS_SPECIAL, NS_SPECIAL ) );
00185         $this->assertFalse( MWNamespace::equals( NS_MAIN, NS_TALK ) );
00186         $this->assertFalse( MWNamespace::equals( NS_USER, NS_USER_TALK ) );
00187         $this->assertFalse( MWNamespace::equals( NS_PROJECT, NS_TEMPLATE ) );
00188     }
00189 
00193     public function testSubjectEquals() {
00194         $this->assertSameSubject( NS_MAIN, NS_MAIN );
00195         $this->assertSameSubject( NS_MAIN, 0 ); // In case we make NS_MAIN 'MAIN'
00196         $this->assertSameSubject( NS_USER, NS_USER );
00197         $this->assertSameSubject( NS_USER, 2 );
00198         $this->assertSameSubject( NS_USER_TALK, NS_USER_TALK );
00199         $this->assertSameSubject( NS_SPECIAL, NS_SPECIAL );
00200         $this->assertSameSubject( NS_MAIN, NS_TALK );
00201         $this->assertSameSubject( NS_USER, NS_USER_TALK );
00202 
00203         $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
00204         $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN );
00205     }
00206 
00210     public function testSpecialAndMediaAreDifferentSubjects() {
00211         $this->assertDifferentSubject(
00212             NS_MEDIA, NS_SPECIAL,
00213             "NS_MEDIA and NS_SPECIAL are different subject namespaces"
00214         );
00215         $this->assertDifferentSubject(
00216             NS_SPECIAL, NS_MEDIA,
00217             "NS_SPECIAL and NS_MEDIA are different subject namespaces"
00218         );
00219     }
00220 
00224     /*
00225     public function testGetCanonicalNamespaces() {
00226         // Remove the following lines when you implement this test.
00227         $this->markTestIncomplete(
00228           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00229         );
00230     }
00231     */
00235     /*
00236         public function testGetCanonicalName() {
00237             // Remove the following lines when you implement this test.
00238             $this->markTestIncomplete(
00239               'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00240             );
00241         }
00242     */
00246     /*
00247     public function testGetCanonicalIndex() {
00248         // Remove the following lines when you implement this test.
00249         $this->markTestIncomplete(
00250           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00251         );
00252     }
00253     */
00254 
00258     /*
00259     public function testGetValidNamespaces() {
00260         // Remove the following lines when you implement this test.
00261         $this->markTestIncomplete(
00262           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00263         );
00264     }
00265     */
00266 
00270     public function testCanTalk() {
00271         $this->assertCanNotTalk( NS_MEDIA );
00272         $this->assertCanNotTalk( NS_SPECIAL );
00273 
00274         $this->assertCanTalk( NS_MAIN );
00275         $this->assertCanTalk( NS_TALK );
00276         $this->assertCanTalk( NS_USER );
00277         $this->assertCanTalk( NS_USER_TALK );
00278 
00279         // User defined namespaces
00280         $this->assertCanTalk( 100 );
00281         $this->assertCanTalk( 101 );
00282     }
00283 
00287     public function testIsContent() {
00288         // NS_MAIN is a content namespace per DefaultSettings.php
00289         // and per function definition.
00290 
00291         $this->assertIsContent( NS_MAIN );
00292 
00293         // Other namespaces which are not expected to be content
00294 
00295         $this->assertIsNotContent( NS_MEDIA );
00296         $this->assertIsNotContent( NS_SPECIAL );
00297         $this->assertIsNotContent( NS_TALK );
00298         $this->assertIsNotContent( NS_USER );
00299         $this->assertIsNotContent( NS_CATEGORY );
00300         $this->assertIsNotContent( 100 );
00301     }
00302 
00308     public function testIsContentAdvanced() {
00309         global $wgContentNamespaces;
00310 
00311         // Test that user defined namespace #252 is not content
00312         $this->assertIsNotContent( 252 );
00313 
00314         // Bless namespace # 252 as a content namespace
00315         $wgContentNamespaces[] = 252;
00316 
00317         $this->assertIsContent( 252 );
00318 
00319         // Makes sure NS_MAIN was not impacted
00320         $this->assertIsContent( NS_MAIN );
00321     }
00322 
00326     public function testIsWatchable() {
00327         // Specials namespaces are not watchable
00328         $this->assertIsNotWatchable( NS_MEDIA );
00329         $this->assertIsNotWatchable( NS_SPECIAL );
00330 
00331         // Core defined namespaces are watchables
00332         $this->assertIsWatchable( NS_MAIN );
00333         $this->assertIsWatchable( NS_TALK );
00334 
00335         // Additional, user defined namespaces are watchables
00336         $this->assertIsWatchable( 100 );
00337         $this->assertIsWatchable( 101 );
00338     }
00339 
00343     public function testHasSubpages() {
00344         global $wgNamespacesWithSubpages;
00345 
00346         // Special namespaces:
00347         $this->assertHasNotSubpages( NS_MEDIA );
00348         $this->assertHasNotSubpages( NS_SPECIAL );
00349 
00350         // Namespaces without subpages
00351         $this->assertHasNotSubpages( NS_MAIN );
00352 
00353         $wgNamespacesWithSubpages[NS_MAIN] = true;
00354         $this->assertHasSubpages( NS_MAIN );
00355 
00356         $wgNamespacesWithSubpages[NS_MAIN] = false;
00357         $this->assertHasNotSubpages( NS_MAIN );
00358 
00359         // Some namespaces with subpages
00360         $this->assertHasSubpages( NS_TALK );
00361         $this->assertHasSubpages( NS_USER );
00362         $this->assertHasSubpages( NS_USER_TALK );
00363     }
00364 
00368     public function testGetContentNamespaces() {
00369         global $wgContentNamespaces;
00370 
00371         $this->assertEquals(
00372             array( NS_MAIN ),
00373             MWNamespace::getContentNamespaces(),
00374             '$wgContentNamespaces is an array with only NS_MAIN by default'
00375         );
00376 
00377         # test !is_array( $wgcontentNamespaces )
00378         $wgContentNamespaces = '';
00379         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00380 
00381         $wgContentNamespaces = false;
00382         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00383 
00384         $wgContentNamespaces = null;
00385         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00386 
00387         $wgContentNamespaces = 5;
00388         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00389 
00390         # test $wgContentNamespaces === array()
00391         $wgContentNamespaces = array();
00392         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00393 
00394         # test !in_array( NS_MAIN, $wgContentNamespaces )
00395         $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
00396         $this->assertEquals(
00397             array( NS_MAIN, NS_USER, NS_CATEGORY ),
00398             MWNamespace::getContentNamespaces(),
00399             'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
00400         );
00401 
00402         # test other cases, return $wgcontentNamespaces as is
00403         $wgContentNamespaces = array( NS_MAIN );
00404         $this->assertEquals(
00405             array( NS_MAIN ),
00406             MWNamespace::getContentNamespaces()
00407         );
00408 
00409         $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
00410         $this->assertEquals(
00411             array( NS_MAIN, NS_USER, NS_CATEGORY ),
00412             MWNamespace::getContentNamespaces()
00413         );
00414     }
00415 
00419     public function testGetSubjectNamespaces() {
00420         $subjectsNS = MWNamespace::getSubjectNamespaces();
00421         $this->assertContains( NS_MAIN, $subjectsNS,
00422             "Talk namespaces should have NS_MAIN" );
00423         $this->assertNotContains( NS_TALK, $subjectsNS,
00424             "Talk namespaces should have NS_TALK" );
00425 
00426         $this->assertNotContains( NS_MEDIA, $subjectsNS,
00427             "Talk namespaces should not have NS_MEDIA" );
00428         $this->assertNotContains( NS_SPECIAL, $subjectsNS,
00429             "Talk namespaces should not have NS_SPECIAL" );
00430     }
00431 
00435     public function testGetTalkNamespaces() {
00436         $talkNS = MWNamespace::getTalkNamespaces();
00437         $this->assertContains( NS_TALK, $talkNS,
00438             "Subject namespaces should have NS_TALK" );
00439         $this->assertNotContains( NS_MAIN, $talkNS,
00440             "Subject namespaces should not have NS_MAIN" );
00441 
00442         $this->assertNotContains( NS_MEDIA, $talkNS,
00443             "Subject namespaces should not have NS_MEDIA" );
00444         $this->assertNotContains( NS_SPECIAL, $talkNS,
00445             "Subject namespaces should not have NS_SPECIAL" );
00446     }
00447 
00453     public function testIsCapitalizedHardcodedAssertions() {
00454         // NS_MEDIA and NS_FILE are treated the same
00455         $this->assertEquals(
00456             MWNamespace::isCapitalized( NS_MEDIA ),
00457             MWNamespace::isCapitalized( NS_FILE ),
00458             'NS_MEDIA and NS_FILE have same capitalization rendering'
00459         );
00460 
00461         // Boths are capitalized by default
00462         $this->assertIsCapitalized( NS_MEDIA );
00463         $this->assertIsCapitalized( NS_FILE );
00464 
00465         // Always capitalized namespaces
00466         // @see MWNamespace::$alwaysCapitalizedNamespaces
00467         $this->assertIsCapitalized( NS_SPECIAL );
00468         $this->assertIsCapitalized( NS_USER );
00469         $this->assertIsCapitalized( NS_MEDIAWIKI );
00470     }
00471 
00485     public function testIsCapitalizedWithWgCapitalLinks() {
00486         global $wgCapitalLinks;
00487 
00488         $this->assertIsCapitalized( NS_PROJECT );
00489         $this->assertIsCapitalized( NS_PROJECT_TALK );
00490 
00491         $wgCapitalLinks = false;
00492 
00493         // hardcoded namespaces (see above function) are still capitalized:
00494         $this->assertIsCapitalized( NS_SPECIAL );
00495         $this->assertIsCapitalized( NS_USER );
00496         $this->assertIsCapitalized( NS_MEDIAWIKI );
00497 
00498         // setting is correctly applied
00499         $this->assertIsNotCapitalized( NS_PROJECT );
00500         $this->assertIsNotCapitalized( NS_PROJECT_TALK );
00501     }
00502 
00510     public function testIsCapitalizedWithWgCapitalLinkOverrides() {
00511         global $wgCapitalLinkOverrides;
00512 
00513         // Test default settings
00514         $this->assertIsCapitalized( NS_PROJECT );
00515         $this->assertIsCapitalized( NS_PROJECT_TALK );
00516 
00517         // hardcoded namespaces (see above function) are capitalized:
00518         $this->assertIsCapitalized( NS_SPECIAL );
00519         $this->assertIsCapitalized( NS_USER );
00520         $this->assertIsCapitalized( NS_MEDIAWIKI );
00521 
00522         // Hardcoded namespaces remains capitalized
00523         $wgCapitalLinkOverrides[NS_SPECIAL] = false;
00524         $wgCapitalLinkOverrides[NS_USER] = false;
00525         $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
00526 
00527         $this->assertIsCapitalized( NS_SPECIAL );
00528         $this->assertIsCapitalized( NS_USER );
00529         $this->assertIsCapitalized( NS_MEDIAWIKI );
00530 
00531         $wgCapitalLinkOverrides[NS_PROJECT] = false;
00532         $this->assertIsNotCapitalized( NS_PROJECT );
00533 
00534         $wgCapitalLinkOverrides[NS_PROJECT] = true;
00535         $this->assertIsCapitalized( NS_PROJECT );
00536 
00537         unset( $wgCapitalLinkOverrides[NS_PROJECT] );
00538         $this->assertIsCapitalized( NS_PROJECT );
00539     }
00540 
00544     public function testHasGenderDistinction() {
00545         // Namespaces with gender distinctions
00546         $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
00547         $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
00548 
00549         // Other ones, "genderless"
00550         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
00551         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
00552         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
00553         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
00554     }
00555 
00559     public function testIsNonincludable() {
00560         global $wgNonincludableNamespaces;
00561 
00562         $wgNonincludableNamespaces = array( NS_USER );
00563 
00564         $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
00565         $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
00566     }
00567 
00568     ####### HELPERS ###########################################################
00569     function __call( $method, $args ) {
00570         // Call the real method if it exists
00571         if ( method_exists( $this, $method ) ) {
00572             return $this->$method( $args );
00573         }
00574 
00575         if ( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
00576             # Interprets arguments:
00577             $ns = $args[0];
00578             $msg = isset( $args[1] ) ? $args[1] : " dummy message";
00579 
00580             # Forge the namespace constant name:
00581             if ( $ns === 0 ) {
00582                 $ns_name = "NS_MAIN";
00583             } else {
00584                 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
00585             }
00586             # ... and the MWNamespace method name
00587             $nsMethod = strtolower( $m[1] ) . $m[3];
00588 
00589             $expect = ( $m[2] === '' );
00590             $expect_name = $expect ? 'TRUE' : 'FALSE';
00591 
00592             return $this->assertEquals( $expect,
00593                 MWNamespace::$nsMethod( $ns, $msg ),
00594                 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
00595             );
00596         }
00597 
00598         throw new Exception( __METHOD__ . " could not find a method named $method\n" );
00599     }
00600 
00601     function assertSameSubject( $ns1, $ns2, $msg = '' ) {
00602         $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00603     }
00604 
00605     function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
00606         $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00607     }
00608 }