MediaWiki  REL1_22
MWNamespaceTest.php
Go to the documentation of this file.
00001 <?php
00013 class MWNamespaceTest extends MediaWikiTestCase {
00014     protected function setUp() {
00015         parent::setUp();
00016 
00017         $this->setMwGlobals( array(
00018             'wgContentNamespaces' => array( NS_MAIN ),
00019             'wgNamespacesWithSubpages' => array(
00020                 NS_TALK => true,
00021                 NS_USER => true,
00022                 NS_USER_TALK => true,
00023             ),
00024             'wgCapitalLinks' => true,
00025             'wgCapitalLinkOverrides' => array(),
00026             'wgNonincludableNamespaces' => array(),
00027         ) );
00028     }
00029 
00030 #### START OF TESTS #########################################################
00031 
00035     public function testIsMovable() {
00036         $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
00037         # @todo FIXME: Write more tests!!
00038     }
00039 
00043     public function testIsSubject() {
00044         // Special namespaces
00045         $this->assertIsSubject( NS_MEDIA );
00046         $this->assertIsSubject( NS_SPECIAL );
00047 
00048         // Subject pages
00049         $this->assertIsSubject( NS_MAIN );
00050         $this->assertIsSubject( NS_USER );
00051         $this->assertIsSubject( 100 ); # user defined
00052 
00053         // Talk pages
00054         $this->assertIsNotSubject( NS_TALK );
00055         $this->assertIsNotSubject( NS_USER_TALK );
00056         $this->assertIsNotSubject( 101 ); # user defined
00057     }
00058 
00063     public function testIsTalk() {
00064         // Special namespaces
00065         $this->assertIsNotTalk( NS_MEDIA );
00066         $this->assertIsNotTalk( NS_SPECIAL );
00067 
00068         // Subject pages
00069         $this->assertIsNotTalk( NS_MAIN );
00070         $this->assertIsNotTalk( NS_USER );
00071         $this->assertIsNotTalk( 100 ); # user defined
00072 
00073         // Talk pages
00074         $this->assertIsTalk( NS_TALK );
00075         $this->assertIsTalk( NS_USER_TALK );
00076         $this->assertIsTalk( 101 ); # user defined
00077     }
00078 
00081     public function testGetSubject() {
00082         // Special namespaces are their own subjects
00083         $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
00084         $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
00085 
00086         $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
00087         $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
00088     }
00089 
00095     public function testGetTalk() {
00096         $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
00097         $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) );
00098         $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) );
00099         $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) );
00100     }
00101 
00107     public function testGetTalkExceptionsForNsMedia() {
00108         $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
00109     }
00110 
00116     public function testGetTalkExceptionsForNsSpecial() {
00117         $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
00118     }
00119 
00125     public function testGetAssociated() {
00126         $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
00127         $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
00128     }
00129 
00130     ### Exceptions with getAssociated()
00131     ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
00132     ### an exception for them.
00133 
00136     public function testGetAssociatedExceptionsForNsMedia() {
00137         $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
00138     }
00139 
00143     public function testGetAssociatedExceptionsForNsSpecial() {
00144         $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
00145     }
00146 
00150     /*
00151     public function testExists() {
00152         // Remove the following lines when you implement this test.
00153         $this->markTestIncomplete(
00154           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00155         );
00156     }
00157     */
00158 
00165     public function testEquals() {
00166         $this->assertTrue( MWNamespace::equals( NS_MAIN, NS_MAIN ) );
00167         $this->assertTrue( MWNamespace::equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
00168         $this->assertTrue( MWNamespace::equals( NS_USER, NS_USER ) );
00169         $this->assertTrue( MWNamespace::equals( NS_USER, 2 ) );
00170         $this->assertTrue( MWNamespace::equals( NS_USER_TALK, NS_USER_TALK ) );
00171         $this->assertTrue( MWNamespace::equals( NS_SPECIAL, NS_SPECIAL ) );
00172         $this->assertFalse( MWNamespace::equals( NS_MAIN, NS_TALK ) );
00173         $this->assertFalse( MWNamespace::equals( NS_USER, NS_USER_TALK ) );
00174         $this->assertFalse( MWNamespace::equals( NS_PROJECT, NS_TEMPLATE ) );
00175     }
00176 
00180     public function testSubjectEquals() {
00181         $this->assertSameSubject( NS_MAIN, NS_MAIN );
00182         $this->assertSameSubject( NS_MAIN, 0 ); // In case we make NS_MAIN 'MAIN'
00183         $this->assertSameSubject( NS_USER, NS_USER );
00184         $this->assertSameSubject( NS_USER, 2 );
00185         $this->assertSameSubject( NS_USER_TALK, NS_USER_TALK );
00186         $this->assertSameSubject( NS_SPECIAL, NS_SPECIAL );
00187         $this->assertSameSubject( NS_MAIN, NS_TALK );
00188         $this->assertSameSubject( NS_USER, NS_USER_TALK );
00189 
00190         $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
00191         $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN );
00192     }
00193 
00194     public function testSpecialAndMediaAreDifferentSubjects() {
00195         $this->assertDifferentSubject(
00196             NS_MEDIA, NS_SPECIAL,
00197             "NS_MEDIA and NS_SPECIAL are different subject namespaces"
00198         );
00199         $this->assertDifferentSubject(
00200             NS_SPECIAL, NS_MEDIA,
00201             "NS_SPECIAL and NS_MEDIA are different subject namespaces"
00202         );
00203     }
00204 
00208     /*
00209     public function testGetCanonicalNamespaces() {
00210         // Remove the following lines when you implement this test.
00211         $this->markTestIncomplete(
00212           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00213         );
00214     }
00215     */
00219     /*
00220         public function testGetCanonicalName() {
00221             // Remove the following lines when you implement this test.
00222             $this->markTestIncomplete(
00223               'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00224             );
00225         }
00226     */
00230     /*
00231     public function testGetCanonicalIndex() {
00232         // Remove the following lines when you implement this test.
00233         $this->markTestIncomplete(
00234           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00235         );
00236     }
00237     */
00238 
00242     /*
00243     public function testGetValidNamespaces() {
00244         // Remove the following lines when you implement this test.
00245         $this->markTestIncomplete(
00246           'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00247         );
00248     }
00249     */
00250 
00253     public function testCanTalk() {
00254         $this->assertCanNotTalk( NS_MEDIA );
00255         $this->assertCanNotTalk( NS_SPECIAL );
00256 
00257         $this->assertCanTalk( NS_MAIN );
00258         $this->assertCanTalk( NS_TALK );
00259         $this->assertCanTalk( NS_USER );
00260         $this->assertCanTalk( NS_USER_TALK );
00261 
00262         // User defined namespaces
00263         $this->assertCanTalk( 100 );
00264         $this->assertCanTalk( 101 );
00265     }
00266 
00269     public function testIsContent() {
00270         // NS_MAIN is a content namespace per DefaultSettings.php
00271         // and per function definition.
00272 
00273         $this->assertIsContent( NS_MAIN );
00274 
00275         // Other namespaces which are not expected to be content
00276 
00277         $this->assertIsNotContent( NS_MEDIA );
00278         $this->assertIsNotContent( NS_SPECIAL );
00279         $this->assertIsNotContent( NS_TALK );
00280         $this->assertIsNotContent( NS_USER );
00281         $this->assertIsNotContent( NS_CATEGORY );
00282         $this->assertIsNotContent( 100 );
00283     }
00284 
00289     public function testIsContentAdvanced() {
00290         global $wgContentNamespaces;
00291 
00292         // Test that user defined namespace #252 is not content
00293         $this->assertIsNotContent( 252 );
00294 
00295         // Bless namespace # 252 as a content namespace
00296         $wgContentNamespaces[] = 252;
00297 
00298         $this->assertIsContent( 252 );
00299 
00300         // Makes sure NS_MAIN was not impacted
00301         $this->assertIsContent( NS_MAIN );
00302     }
00303 
00304     public function testIsWatchable() {
00305         // Specials namespaces are not watchable
00306         $this->assertIsNotWatchable( NS_MEDIA );
00307         $this->assertIsNotWatchable( NS_SPECIAL );
00308 
00309         // Core defined namespaces are watchables
00310         $this->assertIsWatchable( NS_MAIN );
00311         $this->assertIsWatchable( NS_TALK );
00312 
00313         // Additional, user defined namespaces are watchables
00314         $this->assertIsWatchable( 100 );
00315         $this->assertIsWatchable( 101 );
00316     }
00317 
00318     public function testHasSubpages() {
00319         global $wgNamespacesWithSubpages;
00320 
00321         // Special namespaces:
00322         $this->assertHasNotSubpages( NS_MEDIA );
00323         $this->assertHasNotSubpages( NS_SPECIAL );
00324 
00325         // Namespaces without subpages
00326         $this->assertHasNotSubpages( NS_MAIN );
00327 
00328         $wgNamespacesWithSubpages[NS_MAIN] = true;
00329         $this->assertHasSubpages( NS_MAIN );
00330 
00331         $wgNamespacesWithSubpages[NS_MAIN] = false;
00332         $this->assertHasNotSubpages( NS_MAIN );
00333 
00334         // Some namespaces with subpages
00335         $this->assertHasSubpages( NS_TALK );
00336         $this->assertHasSubpages( NS_USER );
00337         $this->assertHasSubpages( NS_USER_TALK );
00338     }
00339 
00342     public function testGetContentNamespaces() {
00343         global $wgContentNamespaces;
00344 
00345         $this->assertEquals(
00346             array( NS_MAIN ),
00347             MWNamespace::getContentNamespaces(),
00348             '$wgContentNamespaces is an array with only NS_MAIN by default'
00349         );
00350 
00351         # test !is_array( $wgcontentNamespaces )
00352         $wgContentNamespaces = '';
00353         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00354 
00355         $wgContentNamespaces = false;
00356         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00357 
00358         $wgContentNamespaces = null;
00359         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00360 
00361         $wgContentNamespaces = 5;
00362         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00363 
00364         # test $wgContentNamespaces === array()
00365         $wgContentNamespaces = array();
00366         $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
00367 
00368         # test !in_array( NS_MAIN, $wgContentNamespaces )
00369         $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
00370         $this->assertEquals(
00371             array( NS_MAIN, NS_USER, NS_CATEGORY ),
00372             MWNamespace::getContentNamespaces(),
00373             'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
00374         );
00375 
00376         # test other cases, return $wgcontentNamespaces as is
00377         $wgContentNamespaces = array( NS_MAIN );
00378         $this->assertEquals(
00379             array( NS_MAIN ),
00380             MWNamespace::getContentNamespaces()
00381         );
00382 
00383         $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
00384         $this->assertEquals(
00385             array( NS_MAIN, NS_USER, NS_CATEGORY ),
00386             MWNamespace::getContentNamespaces()
00387         );
00388     }
00389 
00392     public function testGetSubjectNamespaces() {
00393         $subjectsNS = MWNamespace::getSubjectNamespaces();
00394         $this->assertContains( NS_MAIN, $subjectsNS,
00395             "Talk namespaces should have NS_MAIN" );
00396         $this->assertNotContains( NS_TALK, $subjectsNS,
00397             "Talk namespaces should have NS_TALK" );
00398 
00399         $this->assertNotContains( NS_MEDIA, $subjectsNS,
00400             "Talk namespaces should not have NS_MEDIA" );
00401         $this->assertNotContains( NS_SPECIAL, $subjectsNS,
00402             "Talk namespaces should not have NS_SPECIAL" );
00403     }
00404 
00407     public function testGetTalkNamespaces() {
00408         $talkNS = MWNamespace::getTalkNamespaces();
00409         $this->assertContains( NS_TALK, $talkNS,
00410             "Subject namespaces should have NS_TALK" );
00411         $this->assertNotContains( NS_MAIN, $talkNS,
00412             "Subject namespaces should not have NS_MAIN" );
00413 
00414         $this->assertNotContains( NS_MEDIA, $talkNS,
00415             "Subject namespaces should not have NS_MEDIA" );
00416         $this->assertNotContains( NS_SPECIAL, $talkNS,
00417             "Subject namespaces should not have NS_SPECIAL" );
00418     }
00419 
00424     public function testIsCapitalizedHardcodedAssertions() {
00425         // NS_MEDIA and NS_FILE are treated the same
00426         $this->assertEquals(
00427             MWNamespace::isCapitalized( NS_MEDIA ),
00428             MWNamespace::isCapitalized( NS_FILE ),
00429             'NS_MEDIA and NS_FILE have same capitalization rendering'
00430         );
00431 
00432         // Boths are capitalized by default
00433         $this->assertIsCapitalized( NS_MEDIA );
00434         $this->assertIsCapitalized( NS_FILE );
00435 
00436         // Always capitalized namespaces
00437         // @see MWNamespace::$alwaysCapitalizedNamespaces
00438         $this->assertIsCapitalized( NS_SPECIAL );
00439         $this->assertIsCapitalized( NS_USER );
00440         $this->assertIsCapitalized( NS_MEDIAWIKI );
00441     }
00442 
00455     public function testIsCapitalizedWithWgCapitalLinks() {
00456         global $wgCapitalLinks;
00457 
00458         $this->assertIsCapitalized( NS_PROJECT );
00459         $this->assertIsCapitalized( NS_PROJECT_TALK );
00460 
00461         $wgCapitalLinks = false;
00462 
00463         // hardcoded namespaces (see above function) are still capitalized:
00464         $this->assertIsCapitalized( NS_SPECIAL );
00465         $this->assertIsCapitalized( NS_USER );
00466         $this->assertIsCapitalized( NS_MEDIAWIKI );
00467 
00468         // setting is correctly applied
00469         $this->assertIsNotCapitalized( NS_PROJECT );
00470         $this->assertIsNotCapitalized( NS_PROJECT_TALK );
00471     }
00472 
00479     public function testIsCapitalizedWithWgCapitalLinkOverrides() {
00480         global $wgCapitalLinkOverrides;
00481 
00482         // Test default settings
00483         $this->assertIsCapitalized( NS_PROJECT );
00484         $this->assertIsCapitalized( NS_PROJECT_TALK );
00485 
00486         // hardcoded namespaces (see above function) are capitalized:
00487         $this->assertIsCapitalized( NS_SPECIAL );
00488         $this->assertIsCapitalized( NS_USER );
00489         $this->assertIsCapitalized( NS_MEDIAWIKI );
00490 
00491         // Hardcoded namespaces remains capitalized
00492         $wgCapitalLinkOverrides[NS_SPECIAL] = false;
00493         $wgCapitalLinkOverrides[NS_USER] = false;
00494         $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
00495 
00496         $this->assertIsCapitalized( NS_SPECIAL );
00497         $this->assertIsCapitalized( NS_USER );
00498         $this->assertIsCapitalized( NS_MEDIAWIKI );
00499 
00500         $wgCapitalLinkOverrides[NS_PROJECT] = false;
00501         $this->assertIsNotCapitalized( NS_PROJECT );
00502 
00503         $wgCapitalLinkOverrides[NS_PROJECT] = true;
00504         $this->assertIsCapitalized( NS_PROJECT );
00505 
00506         unset( $wgCapitalLinkOverrides[NS_PROJECT] );
00507         $this->assertIsCapitalized( NS_PROJECT );
00508     }
00509 
00510     public function testHasGenderDistinction() {
00511         // Namespaces with gender distinctions
00512         $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
00513         $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
00514 
00515         // Other ones, "genderless"
00516         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
00517         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
00518         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
00519         $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
00520     }
00521 
00522     public function testIsNonincludable() {
00523         global $wgNonincludableNamespaces;
00524 
00525         $wgNonincludableNamespaces = array( NS_USER );
00526 
00527         $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
00528         $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
00529     }
00530 
00531     ####### HELPERS ###########################################################
00532     function __call( $method, $args ) {
00533         // Call the real method if it exists
00534         if ( method_exists( $this, $method ) ) {
00535             return $this->$method( $args );
00536         }
00537 
00538         if ( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
00539             # Interprets arguments:
00540             $ns = $args[0];
00541             $msg = isset( $args[1] ) ? $args[1] : " dummy message";
00542 
00543             # Forge the namespace constant name:
00544             if ( $ns === 0 ) {
00545                 $ns_name = "NS_MAIN";
00546             } else {
00547                 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
00548             }
00549             # ... and the MWNamespace method name
00550             $nsMethod = strtolower( $m[1] ) . $m[3];
00551 
00552             $expect = ( $m[2] === '' );
00553             $expect_name = $expect ? 'TRUE' : 'FALSE';
00554 
00555             return $this->assertEquals( $expect,
00556                 MWNamespace::$nsMethod( $ns, $msg ),
00557                 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
00558             );
00559         }
00560 
00561         throw new Exception( __METHOD__ . " could not find a method named $method\n" );
00562     }
00563 
00564     function assertSameSubject( $ns1, $ns2, $msg = '' ) {
00565         $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00566     }
00567 
00568     function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
00569         $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00570     }
00571 }