MediaWiki  REL1_20
MWNamespaceTest.php
Go to the documentation of this file.
00001 <?php
00013 class MWNamespaceTest extends MediaWikiTestCase {
00018         protected function setUp() {
00019         }
00020 
00025         protected function tearDown() {
00026         }
00027 
00028 
00029 #### START OF TESTS #########################################################
00030 
00034         public function testIsMovable() {
00035                 $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
00036                 # @todo FIXME: Write more tests!!
00037         }
00038 
00042         public function testIsSubject() {
00043                 // Special namespaces
00044                 $this->assertIsSubject( NS_MEDIA   );
00045                 $this->assertIsSubject( NS_SPECIAL );
00046 
00047                 // Subject pages
00048                 $this->assertIsSubject( NS_MAIN );
00049                 $this->assertIsSubject( NS_USER );
00050                 $this->assertIsSubject( 100     );  # user defined
00051 
00052                 // Talk pages
00053                 $this->assertIsNotSubject( NS_TALK      );
00054                 $this->assertIsNotSubject( NS_USER_TALK );
00055                 $this->assertIsNotSubject( 101          ); # user defined
00056         }
00057 
00062         public function testIsTalk() {
00063                 // Special namespaces
00064                 $this->assertIsNotTalk( NS_MEDIA   );
00065                 $this->assertIsNotTalk( NS_SPECIAL );
00066 
00067                 // Subject pages
00068                 $this->assertIsNotTalk( NS_MAIN   );
00069                 $this->assertIsNotTalk( NS_USER   );
00070                 $this->assertIsNotTalk( 100       );  # user defined
00071 
00072                 // Talk pages
00073                 $this->assertIsTalk( NS_TALK      );
00074                 $this->assertIsTalk( NS_USER_TALK );
00075                 $this->assertIsTalk( 101          ); # user defined
00076         }
00077 
00080         public function testGetSubject() {
00081                 // Special namespaces are their own subjects
00082                 $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
00083                 $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
00084 
00085                 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
00086                 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
00087         }
00088 
00094         public function testGetTalk() {
00095                 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
00096                 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) );
00097                 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) );
00098                 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) );
00099         }
00100 
00106         public function testGetTalkExceptionsForNsMedia() {
00107                 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
00108         }
00109 
00115         public function testGetTalkExceptionsForNsSpecial() {
00116                 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
00117         }
00118 
00124         public function testGetAssociated() {
00125                 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
00126                 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
00127 
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         }
00205 
00209 /*
00210         public function testGetCanonicalNamespaces() {
00211                 // Remove the following lines when you implement this test.
00212                 $this->markTestIncomplete(
00213                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00214                 );
00215         }
00216 */
00220 /*
00221         public function testGetCanonicalName() {
00222                 // Remove the following lines when you implement this test.
00223                 $this->markTestIncomplete(
00224                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00225                 );
00226         }
00227 */
00231 /*
00232         public function testGetCanonicalIndex() {
00233                 // Remove the following lines when you implement this test.
00234                 $this->markTestIncomplete(
00235                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
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 */
00252         public function testCanTalk() {
00253                 $this->assertCanNotTalk( NS_MEDIA   );
00254                 $this->assertCanNotTalk( NS_SPECIAL );
00255 
00256                 $this->assertCanTalk( NS_MAIN      );
00257                 $this->assertCanTalk( NS_TALK      );
00258                 $this->assertCanTalk( NS_USER      );
00259                 $this->assertCanTalk( NS_USER_TALK );
00260 
00261                 // User defined namespaces
00262                 $this->assertCanTalk( 100 );
00263                 $this->assertCanTalk( 101 );
00264         }
00265 
00268         public function testIsContent() {
00269                 // NS_MAIN is a content namespace per DefaultSettings.php
00270                 // and per function definition.
00271                 $this->assertIsContent( NS_MAIN );
00272 
00273                 global $wgContentNamespaces;
00274 
00275                 $saved = $wgContentNamespaces;
00276 
00277                 $wgContentNamespaces[] = NS_MAIN;
00278                 $this->assertIsContent( NS_MAIN );
00279 
00280                 // Other namespaces which are not expected to be content
00281                 if ( isset( $wgContentNamespaces[NS_MEDIA] ) ) {
00282                         unset( $wgContentNamespaces[NS_MEDIA] );
00283                 }
00284                 $this->assertIsNotContent( NS_MEDIA );
00285 
00286                 if ( isset( $wgContentNamespaces[NS_SPECIAL] ) ) {
00287                         unset( $wgContentNamespaces[NS_SPECIAL] );
00288                 }
00289                 $this->assertIsNotContent( NS_SPECIAL );
00290 
00291                 if ( isset( $wgContentNamespaces[NS_TALK] ) ) {
00292                         unset( $wgContentNamespaces[NS_TALK] );
00293                 }
00294                 $this->assertIsNotContent( NS_TALK );
00295 
00296                 if ( isset( $wgContentNamespaces[NS_USER] ) ) {
00297                         unset( $wgContentNamespaces[NS_USER] );
00298                 }
00299                 $this->assertIsNotContent( NS_USER );
00300 
00301                 if ( isset( $wgContentNamespaces[NS_CATEGORY] ) ) {
00302                         unset( $wgContentNamespaces[NS_CATEGORY] );
00303                 }
00304                 $this->assertIsNotContent( NS_CATEGORY );
00305 
00306                 if ( isset( $wgContentNamespaces[100] ) ) {
00307                         unset( $wgContentNamespaces[100] );
00308                 }
00309                 $this->assertIsNotContent( 100 );
00310 
00311                 $wgContentNamespaces = $saved;
00312         }
00313 
00318         public function testIsContentWithAdditionsInWgContentNamespaces() {
00319                 // NS_MAIN is a content namespace per DefaultSettings.php
00320                 // and per function definition.
00321                 $this->assertIsContent( NS_MAIN );
00322 
00323                 // Tests that user defined namespace #252 is not content:
00324                 $this->assertIsNotContent( 252 );
00325 
00326                 # @todo FIXME: Is global saving really required for PHPUnit?
00327                 // Bless namespace # 252 as a content namespace
00328                 global $wgContentNamespaces;
00329                 $savedGlobal = $wgContentNamespaces;
00330                 $wgContentNamespaces[] = 252;
00331                 $this->assertIsContent( 252 );
00332 
00333                 // Makes sure NS_MAIN was not impacted
00334                 $this->assertIsContent( NS_MAIN );
00335 
00336                 // Restore global
00337                 $wgContentNamespaces = $savedGlobal;
00338 
00339                 // Verify namespaces after global restauration
00340                 $this->assertIsContent( NS_MAIN  );
00341                 $this->assertIsNotContent( 252 );
00342         }
00343 
00344         public function testIsWatchable() {
00345                 // Specials namespaces are not watchable
00346                 $this->assertIsNotWatchable( NS_MEDIA   );
00347                 $this->assertIsNotWatchable( NS_SPECIAL );
00348 
00349                 // Core defined namespaces are watchables
00350                 $this->assertIsWatchable( NS_MAIN );
00351                 $this->assertIsWatchable( NS_TALK );
00352 
00353                 // Additional, user defined namespaces are watchables
00354                 $this->assertIsWatchable( 100 );
00355                 $this->assertIsWatchable( 101 );
00356         }
00357 
00358         public function testHasSubpages() {
00359                 // Special namespaces:
00360                 $this->assertHasNotSubpages( NS_MEDIA   );
00361                 $this->assertHasNotSubpages( NS_SPECIAL );
00362 
00363                 // namespaces without subpages
00364                 # save up global
00365                 global $wgNamespacesWithSubpages;
00366                 $saved = null;
00367                 if( array_key_exists( NS_MAIN, $wgNamespacesWithSubpages ) ) {
00368                         $saved = $wgNamespacesWithSubpages[NS_MAIN];
00369                         unset( $wgNamespacesWithSubpages[NS_MAIN] );
00370                 }
00371 
00372                 $this->assertHasNotSubpages( NS_MAIN );
00373 
00374                 $wgNamespacesWithSubpages[NS_MAIN] = true;
00375                 $this->assertHasSubpages( NS_MAIN );
00376                 $wgNamespacesWithSubpages[NS_MAIN] = false;
00377                 $this->assertHasNotSubpages( NS_MAIN );
00378 
00379                 # restore global
00380                 if( $saved !== null ) {
00381                         $wgNamespacesWithSubpages[NS_MAIN] = $saved;
00382                 }
00383 
00384                 // Some namespaces with subpages
00385                 $this->assertHasSubpages( NS_TALK      );
00386                 $this->assertHasSubpages( NS_USER      );
00387                 $this->assertHasSubpages( NS_USER_TALK );
00388         }
00389 
00392         public function testGetContentNamespaces() {
00393                 $this->assertEquals(
00394                         array( NS_MAIN ),
00395                         MWNamespace::getcontentNamespaces(),
00396                         '$wgContentNamespaces is an array with only NS_MAIN by default'
00397                 );
00398 
00399                 global $wgContentNamespaces;
00400 
00401                 $saved = $wgContentNamespaces;
00402                 # test !is_array( $wgcontentNamespaces )
00403                 $wgContentNamespaces = '';
00404                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00405                 $wgContentNamespaces = false;
00406                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00407                 $wgContentNamespaces = null;
00408                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00409                 $wgContentNamespaces = 5;
00410                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00411 
00412                 # test $wgContentNamespaces === array()
00413                 $wgContentNamespaces = array();
00414                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00415 
00416                 # test !in_array( NS_MAIN, $wgContentNamespaces )
00417                 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
00418                 $this->assertEquals(
00419                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00420                         MWNamespace::getcontentNamespaces(),
00421                         'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
00422                 );
00423 
00424                 # test other cases, return $wgcontentNamespaces as is
00425                 $wgContentNamespaces = array( NS_MAIN );
00426                 $this->assertEquals(
00427                         array( NS_MAIN ),
00428                         MWNamespace::getcontentNamespaces()
00429                 );
00430 
00431                 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
00432                 $this->assertEquals(
00433                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00434                         MWNamespace::getcontentNamespaces()
00435                 );
00436 
00437                 $wgContentNamespaces = $saved;
00438         }
00439 
00442         public function testGetSubjectNamespaces() {
00443                 $subjectsNS = MWNamespace::getSubjectNamespaces();
00444                 $this->assertContains(    NS_MAIN, $subjectsNS,
00445                         "Talk namespaces should have NS_MAIN" );
00446                 $this->assertNotContains( NS_TALK, $subjectsNS,
00447                         "Talk namespaces should have NS_TALK" );
00448 
00449                 $this->assertNotContains( NS_MEDIA, $subjectsNS,
00450                         "Talk namespaces should not have NS_MEDIA" );
00451                 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
00452                         "Talk namespaces should not have NS_SPECIAL" );
00453         }
00454 
00457         public function testGetTalkNamespaces() {
00458                 $talkNS = MWNamespace::getTalkNamespaces();
00459                 $this->assertContains(    NS_TALK, $talkNS,
00460                         "Subject namespaces should have NS_TALK" );
00461                 $this->assertNotContains( NS_MAIN, $talkNS,
00462                         "Subject namespaces should not have NS_MAIN" );
00463 
00464                 $this->assertNotContains( NS_MEDIA, $talkNS,
00465                         "Subject namespaces should not have NS_MEDIA" );
00466                 $this->assertNotContains( NS_SPECIAL, $talkNS,
00467                         "Subject namespaces should not have NS_SPECIAL" );
00468         }
00469 
00474         public function testIsCapitalizedHardcodedAssertions() {
00475                 // NS_MEDIA and NS_FILE are treated the same
00476                 $this->assertEquals(
00477                         MWNamespace::isCapitalized( NS_MEDIA ),
00478                         MWNamespace::isCapitalized( NS_FILE  ),
00479                         'NS_MEDIA and NS_FILE have same capitalization rendering'
00480                 );
00481 
00482                 // Boths are capitalized by default
00483                 $this->assertIsCapitalized( NS_MEDIA );
00484                 $this->assertIsCapitalized( NS_FILE  );
00485 
00486                 // Always capitalized namespaces
00487                 // @see MWNamespace::$alwaysCapitalizedNamespaces
00488                 $this->assertIsCapitalized( NS_SPECIAL   );
00489                 $this->assertIsCapitalized( NS_USER      );
00490                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00491         }
00492 
00505         public function testIsCapitalizedWithWgCapitalLinks() {
00506                 global $wgCapitalLinks;
00507                 // Save the global to easily reset to MediaWiki default settings
00508                 $savedGlobal = $wgCapitalLinks;
00509 
00510                 $wgCapitalLinks = true;
00511                 $this->assertIsCapitalized( NS_PROJECT      );
00512                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00513 
00514                 $wgCapitalLinks = false;
00515                 // hardcoded namespaces (see above function) are still capitalized:
00516                 $this->assertIsCapitalized( NS_SPECIAL   );
00517                 $this->assertIsCapitalized( NS_USER      );
00518                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00519                 // setting is correctly applied
00520                 $this->assertIsNotCapitalized( NS_PROJECT      );
00521                 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
00522 
00523                 // reset global state:
00524                 $wgCapitalLinks = $savedGlobal;
00525         }
00526 
00533         public function testIsCapitalizedWithWgCapitalLinkOverrides() {
00534                 global $wgCapitalLinkOverrides;
00535                 // Save the global to easily reset to MediaWiki default settings
00536                 $savedGlobal = $wgCapitalLinkOverrides;
00537 
00538                 // Test default settings
00539                 $this->assertIsCapitalized( NS_PROJECT      );
00540                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00541                 // hardcoded namespaces (see above function) are capitalized:
00542                 $this->assertIsCapitalized( NS_SPECIAL   );
00543                 $this->assertIsCapitalized( NS_USER      );
00544                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00545 
00546                 // Hardcoded namespaces remains capitalized
00547                 $wgCapitalLinkOverrides[NS_SPECIAL]   = false;
00548                 $wgCapitalLinkOverrides[NS_USER]      = false;
00549                 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
00550                 $this->assertIsCapitalized( NS_SPECIAL   );
00551                 $this->assertIsCapitalized( NS_USER      );
00552                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00553 
00554                 $wgCapitalLinkOverrides = $savedGlobal;
00555                 $wgCapitalLinkOverrides[NS_PROJECT] = false;
00556                 $this->assertIsNotCapitalized( NS_PROJECT );
00557                 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
00558                 $this->assertIsCapitalized( NS_PROJECT );
00559                 unset(  $wgCapitalLinkOverrides[NS_PROJECT] );
00560                 $this->assertIsCapitalized( NS_PROJECT );
00561 
00562                 // reset global state:
00563                 $wgCapitalLinkOverrides = $savedGlobal;
00564         }
00565 
00566         public function testHasGenderDistinction() {
00567                 // Namespaces with gender distinctions
00568                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER      ) );
00569                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
00570 
00571                 // Other ones, "genderless"
00572                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA   ) );
00573                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
00574                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN    ) );
00575                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK    ) );
00576 
00577         }
00578 
00579         public function testIsNonincludable() {
00580                 global $wgNonincludableNamespaces;
00581                 $wgNonincludableNamespaces = array( NS_USER );
00582 
00583                 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
00584 
00585                 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
00586         }
00587 
00588         ####### HELPERS ###########################################################
00589         function __call( $method, $args ) {
00590                 // Call the real method if it exists
00591                 if( method_exists($this, $method ) ) {
00592                         return $this->$method( $args );
00593                 }
00594 
00595                 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
00596                         # Interprets arguments:
00597                         $ns  = $args[0];
00598                         $msg = isset($args[1]) ? $args[1] : " dummy message";
00599 
00600                         # Forge the namespace constant name:
00601                         if( $ns === 0 ) {
00602                                 $ns_name = "NS_MAIN";
00603                         } else {
00604                                 $ns_name = "NS_" . strtoupper(  MWNamespace::getCanonicalName( $ns ) );
00605                         }
00606                         # ... and the MWNamespace method name
00607                         $nsMethod = strtolower( $m[1] ) . $m[3];
00608 
00609                         $expect = ($m[2] === '');
00610                         $expect_name = $expect ? 'TRUE' : 'FALSE';
00611 
00612                         return $this->assertEquals( $expect,
00613                                 MWNamespace::$nsMethod( $ns, $msg ),
00614                                 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
00615                         );
00616                 }
00617 
00618                 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
00619         }
00620 
00621         function assertSameSubject( $ns1, $ns2, $msg = '' ) {
00622                 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00623         }
00624         function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
00625                 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00626         }
00627 }
00628