MediaWiki  REL1_19
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                 // Back compat
00058                 $this->assertTrue( MWNamespace::isMain( NS_MAIN ) == MWNamespace::isSubject( NS_MAIN ) );
00059                 $this->assertTrue( MWNamespace::isMain( NS_USER_TALK ) == MWNamespace::isSubject( NS_USER_TALK ) );
00060         }
00061 
00066         public function testIsTalk() {
00067                 // Special namespaces
00068                 $this->assertIsNotTalk( NS_MEDIA   );
00069                 $this->assertIsNotTalk( NS_SPECIAL );
00070 
00071                 // Subject pages
00072                 $this->assertIsNotTalk( NS_MAIN   );
00073                 $this->assertIsNotTalk( NS_USER   );
00074                 $this->assertIsNotTalk( 100       );  # user defined
00075 
00076                 // Talk pages
00077                 $this->assertIsTalk( NS_TALK      );
00078                 $this->assertIsTalk( NS_USER_TALK );
00079                 $this->assertIsTalk( 101          ); # user defined
00080         }
00081 
00084         public function testGetSubject() {
00085                 // Special namespaces are their own subjects
00086                 $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
00087                 $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
00088 
00089                 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
00090                 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
00091         }
00092 
00098         public function testGetTalk() {
00099                 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
00100                 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) );
00101                 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) );
00102                 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) );
00103         }
00104 
00110         public function testGetTalkExceptionsForNsMedia() {
00111                 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
00112         }
00113 
00119         public function testGetTalkExceptionsForNsSpecial() {
00120                 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
00121         }
00122 
00128         public function testGetAssociated() {
00129                 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
00130                 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
00131 
00132         }
00133 
00134         ### Exceptions with getAssociated()
00135         ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
00136         ### an exception for them.
00137 
00140         public function testGetAssociatedExceptionsForNsMedia() {
00141                 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA   ) );
00142         }
00143 
00147         public function testGetAssociatedExceptionsForNsSpecial() {
00148                 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
00149         }
00150 
00154 /*
00155         public function testExists() {
00156                 // Remove the following lines when you implement this test.
00157                 $this->markTestIncomplete(
00158                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00159                 );
00160         }
00161 */
00162 
00169         public function testEquals() {
00170                 $this->assertTrue( MWNamespace::equals( NS_MAIN, NS_MAIN ) );
00171                 $this->assertTrue( MWNamespace::equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
00172                 $this->assertTrue( MWNamespace::equals( NS_USER, NS_USER ) );
00173                 $this->assertTrue( MWNamespace::equals( NS_USER, 2 ) );
00174                 $this->assertTrue( MWNamespace::equals( NS_USER_TALK, NS_USER_TALK ) );
00175                 $this->assertTrue( MWNamespace::equals( NS_SPECIAL, NS_SPECIAL ) );
00176                 $this->assertFalse( MWNamespace::equals( NS_MAIN, NS_TALK ) );
00177                 $this->assertFalse( MWNamespace::equals( NS_USER, NS_USER_TALK ) );
00178                 $this->assertFalse( MWNamespace::equals( NS_PROJECT, NS_TEMPLATE ) );
00179         }
00180 
00184         public function testSubjectEquals() {
00185                 $this->assertSameSubject( NS_MAIN, NS_MAIN );
00186                 $this->assertSameSubject( NS_MAIN, 0 ); // In case we make NS_MAIN 'MAIN'
00187                 $this->assertSameSubject( NS_USER, NS_USER );
00188                 $this->assertSameSubject( NS_USER, 2 );
00189                 $this->assertSameSubject( NS_USER_TALK, NS_USER_TALK );
00190                 $this->assertSameSubject( NS_SPECIAL, NS_SPECIAL );
00191                 $this->assertSameSubject( NS_MAIN, NS_TALK );
00192                 $this->assertSameSubject( NS_USER, NS_USER_TALK );
00193 
00194                 $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
00195                 $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN     );
00196         }
00197 
00198         public function testSpecialAndMediaAreDifferentSubjects() {
00199                 $this->assertDifferentSubject(
00200                         NS_MEDIA, NS_SPECIAL,
00201                         "NS_MEDIA and NS_SPECIAL are different subject namespaces"
00202                 );
00203                 $this->assertDifferentSubject(
00204                         NS_SPECIAL, NS_MEDIA,
00205                         "NS_SPECIAL and NS_MEDIA are different subject namespaces"
00206                 );
00207 
00208         }
00209 
00213 /*
00214         public function testGetCanonicalNamespaces() {
00215                 // Remove the following lines when you implement this test.
00216                 $this->markTestIncomplete(
00217                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00218                 );
00219         }
00220 */
00224 /*
00225         public function testGetCanonicalName() {
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 testGetCanonicalIndex() {
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 testGetValidNamespaces() {
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 */
00256         public function testCanTalk() {
00257                 $this->assertCanNotTalk( NS_MEDIA   );
00258                 $this->assertCanNotTalk( NS_SPECIAL );
00259 
00260                 $this->assertCanTalk( NS_MAIN      );
00261                 $this->assertCanTalk( NS_TALK      );
00262                 $this->assertCanTalk( NS_USER      );
00263                 $this->assertCanTalk( NS_USER_TALK );
00264 
00265                 // User defined namespaces
00266                 $this->assertCanTalk( 100 );
00267                 $this->assertCanTalk( 101 );
00268         }
00269 
00272         public function testIsContent() {
00273                 // NS_MAIN is a content namespace per DefaultSettings.php
00274                 // and per function definition.
00275                 $this->assertIsContent( NS_MAIN );
00276 
00277                 global $wgContentNamespaces;
00278 
00279                 $saved = $wgContentNamespaces;
00280 
00281                 $wgContentNamespaces[] = NS_MAIN;
00282                 $this->assertIsContent( NS_MAIN );
00283 
00284                 // Other namespaces which are not expected to be content
00285                 if ( isset( $wgContentNamespaces[NS_MEDIA] ) ) {
00286                         unset( $wgContentNamespaces[NS_MEDIA] );
00287                 }
00288                 $this->assertIsNotContent( NS_MEDIA );
00289 
00290                 if ( isset( $wgContentNamespaces[NS_SPECIAL] ) ) {
00291                         unset( $wgContentNamespaces[NS_SPECIAL] );
00292                 }
00293                 $this->assertIsNotContent( NS_SPECIAL );
00294 
00295                 if ( isset( $wgContentNamespaces[NS_TALK] ) ) {
00296                         unset( $wgContentNamespaces[NS_TALK] );
00297                 }
00298                 $this->assertIsNotContent( NS_TALK );
00299 
00300                 if ( isset( $wgContentNamespaces[NS_USER] ) ) {
00301                         unset( $wgContentNamespaces[NS_USER] );
00302                 }
00303                 $this->assertIsNotContent( NS_USER );
00304 
00305                 if ( isset( $wgContentNamespaces[NS_CATEGORY] ) ) {
00306                         unset( $wgContentNamespaces[NS_CATEGORY] );
00307                 }
00308                 $this->assertIsNotContent( NS_CATEGORY );
00309 
00310                 if ( isset( $wgContentNamespaces[100] ) ) {
00311                         unset( $wgContentNamespaces[100] );
00312                 }
00313                 $this->assertIsNotContent( 100 );
00314 
00315                 $wgContentNamespaces = $saved;
00316         }
00317 
00322         public function testIsContentWithAdditionsInWgContentNamespaces() {
00323                 // NS_MAIN is a content namespace per DefaultSettings.php
00324                 // and per function definition.
00325                 $this->assertIsContent( NS_MAIN );
00326 
00327                 // Tests that user defined namespace #252 is not content:
00328                 $this->assertIsNotContent( 252 );
00329 
00330                 # @todo FIXME: Is global saving really required for PHPUnit?
00331                 // Bless namespace # 252 as a content namespace
00332                 global $wgContentNamespaces;
00333                 $savedGlobal = $wgContentNamespaces;
00334                 $wgContentNamespaces[] = 252;
00335                 $this->assertIsContent( 252 );
00336 
00337                 // Makes sure NS_MAIN was not impacted
00338                 $this->assertIsContent( NS_MAIN );
00339 
00340                 // Restore global
00341                 $wgContentNamespaces = $savedGlobal;
00342 
00343                 // Verify namespaces after global restauration
00344                 $this->assertIsContent( NS_MAIN  );
00345                 $this->assertIsNotContent( 252 );
00346         }
00347 
00348         public function testIsWatchable() {
00349                 // Specials namespaces are not watchable
00350                 $this->assertIsNotWatchable( NS_MEDIA   );
00351                 $this->assertIsNotWatchable( NS_SPECIAL );
00352 
00353                 // Core defined namespaces are watchables
00354                 $this->assertIsWatchable( NS_MAIN );
00355                 $this->assertIsWatchable( NS_TALK );
00356 
00357                 // Additional, user defined namespaces are watchables
00358                 $this->assertIsWatchable( 100 );
00359                 $this->assertIsWatchable( 101 );
00360         }
00361 
00362         public function testHasSubpages() {
00363                 // Special namespaces:
00364                 $this->assertHasNotSubpages( NS_MEDIA   );
00365                 $this->assertHasNotSubpages( NS_SPECIAL );
00366 
00367                 // namespaces without subpages
00368                 # save up global
00369                 global $wgNamespacesWithSubpages;
00370                 $saved = null;
00371                 if( array_key_exists( NS_MAIN, $wgNamespacesWithSubpages ) ) {
00372                         $saved = $wgNamespacesWithSubpages[NS_MAIN];
00373                         unset( $wgNamespacesWithSubpages[NS_MAIN] );
00374                 }
00375 
00376                 $this->assertHasNotSubpages( NS_MAIN );
00377 
00378                 $wgNamespacesWithSubpages[NS_MAIN] = true;
00379                 $this->assertHasSubpages( NS_MAIN );
00380                 $wgNamespacesWithSubpages[NS_MAIN] = false;
00381                 $this->assertHasNotSubpages( NS_MAIN );
00382 
00383                 # restore global
00384                 if( $saved !== null ) {
00385                         $wgNamespacesWithSubpages[NS_MAIN] = $saved;
00386                 }
00387 
00388                 // Some namespaces with subpages
00389                 $this->assertHasSubpages( NS_TALK      );
00390                 $this->assertHasSubpages( NS_USER      );
00391                 $this->assertHasSubpages( NS_USER_TALK );
00392         }
00393 
00396         public function testGetContentNamespaces() {
00397                 $this->assertEquals(
00398                         array( NS_MAIN ),
00399                         MWNamespace::getcontentNamespaces(),
00400                         '$wgContentNamespaces is an array with only NS_MAIN by default'
00401                 );
00402 
00403                 global $wgContentNamespaces;
00404 
00405                 $saved = $wgContentNamespaces;
00406                 # test !is_array( $wgcontentNamespaces )
00407                 $wgContentNamespaces = '';
00408                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00409                 $wgContentNamespaces = false;
00410                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00411                 $wgContentNamespaces = null;
00412                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00413                 $wgContentNamespaces = 5;
00414                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00415 
00416                 # test $wgContentNamespaces === array()
00417                 $wgContentNamespaces = array();
00418                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00419 
00420                 # test !in_array( NS_MAIN, $wgContentNamespaces )
00421                 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
00422                 $this->assertEquals(
00423                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00424                         MWNamespace::getcontentNamespaces(),
00425                         'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
00426                 );
00427 
00428                 # test other cases, return $wgcontentNamespaces as is
00429                 $wgContentNamespaces = array( NS_MAIN );
00430                 $this->assertEquals(
00431                         array( NS_MAIN ),
00432                         MWNamespace::getcontentNamespaces()
00433                 );
00434 
00435                 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
00436                 $this->assertEquals(
00437                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00438                         MWNamespace::getcontentNamespaces()
00439                 );
00440 
00441                 $wgContentNamespaces = $saved;
00442         }
00443 
00448         public function testIsCapitalizedHardcodedAssertions() {
00449                 // NS_MEDIA and NS_FILE are treated the same
00450                 $this->assertEquals(
00451                         MWNamespace::isCapitalized( NS_MEDIA ),
00452                         MWNamespace::isCapitalized( NS_FILE  ),
00453                         'NS_MEDIA and NS_FILE have same capitalization rendering'
00454                 );
00455 
00456                 // Boths are capitalized by default
00457                 $this->assertIsCapitalized( NS_MEDIA );
00458                 $this->assertIsCapitalized( NS_FILE  );
00459 
00460                 // Always capitalized namespaces
00461                 // @see MWNamespace::$alwaysCapitalizedNamespaces
00462                 $this->assertIsCapitalized( NS_SPECIAL   );
00463                 $this->assertIsCapitalized( NS_USER      );
00464                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00465         }
00466 
00479         public function testIsCapitalizedWithWgCapitalLinks() {
00480                 global $wgCapitalLinks;
00481                 // Save the global to easily reset to MediaWiki default settings
00482                 $savedGlobal = $wgCapitalLinks;
00483 
00484                 $wgCapitalLinks = true;
00485                 $this->assertIsCapitalized( NS_PROJECT      );
00486                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00487 
00488                 $wgCapitalLinks = false;
00489                 // hardcoded namespaces (see above function) are still capitalized:
00490                 $this->assertIsCapitalized( NS_SPECIAL   );
00491                 $this->assertIsCapitalized( NS_USER      );
00492                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00493                 // setting is correctly applied
00494                 $this->assertIsNotCapitalized( NS_PROJECT      );
00495                 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
00496 
00497                 // reset global state:
00498                 $wgCapitalLinks = $savedGlobal;
00499         }
00500 
00507         public function testIsCapitalizedWithWgCapitalLinkOverrides() {
00508                 global $wgCapitalLinkOverrides;
00509                 // Save the global to easily reset to MediaWiki default settings
00510                 $savedGlobal = $wgCapitalLinkOverrides;
00511 
00512                 // Test default settings
00513                 $this->assertIsCapitalized( NS_PROJECT      );
00514                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00515                 // hardcoded namespaces (see above function) are capitalized:
00516                 $this->assertIsCapitalized( NS_SPECIAL   );
00517                 $this->assertIsCapitalized( NS_USER      );
00518                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00519 
00520                 // Hardcoded namespaces remains capitalized
00521                 $wgCapitalLinkOverrides[NS_SPECIAL]   = false;
00522                 $wgCapitalLinkOverrides[NS_USER]      = false;
00523                 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
00524                 $this->assertIsCapitalized( NS_SPECIAL   );
00525                 $this->assertIsCapitalized( NS_USER      );
00526                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00527 
00528                 $wgCapitalLinkOverrides = $savedGlobal;
00529                 $wgCapitalLinkOverrides[NS_PROJECT] = false;
00530                 $this->assertIsNotCapitalized( NS_PROJECT );
00531                 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
00532                 $this->assertIsCapitalized( NS_PROJECT );
00533                 unset(  $wgCapitalLinkOverrides[NS_PROJECT] );
00534                 $this->assertIsCapitalized( NS_PROJECT );
00535 
00536                 // reset global state:
00537                 $wgCapitalLinkOverrides = $savedGlobal;
00538         }
00539 
00540         public function testHasGenderDistinction() {
00541                 // Namespaces with gender distinctions
00542                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER      ) );
00543                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
00544 
00545                 // Other ones, "genderless"
00546                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA   ) );
00547                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
00548                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN    ) );
00549                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK    ) );
00550 
00551         }
00552 
00553         ####### HELPERS ###########################################################
00554         function __call( $method, $args ) {
00555                 // Call the real method if it exists
00556                 if( method_exists($this, $method ) ) {
00557                         return $this->$method( $args );
00558                 }
00559 
00560                 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
00561                         # Interprets arguments:
00562                         $ns  = $args[0];
00563                         $msg = isset($args[1]) ? $args[1] : " dummy message";
00564 
00565                         # Forge the namespace constant name:
00566                         if( $ns === 0 ) {
00567                                 $ns_name = "NS_MAIN";
00568                         } else {
00569                                 $ns_name = "NS_" . strtoupper(  MWNamespace::getCanonicalName( $ns ) );
00570                         }
00571                         # ... and the MWNamespace method name
00572                         $nsMethod = strtolower( $m[1] ) . $m[3];
00573 
00574                         $expect = ($m[2] === '');
00575                         $expect_name = $expect ? 'TRUE' : 'FALSE';
00576 
00577                         return $this->assertEquals( $expect,
00578                                 MWNamespace::$nsMethod( $ns, $msg ),
00579                                 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
00580                         );
00581                 }
00582 
00583                 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
00584         }
00585 
00586         function assertSameSubject( $ns1, $ns2, $msg = '' ) {
00587                 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00588         }
00589         function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
00590                 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00591         }
00592 }
00593