MediaWiki  master
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 
00131         ### Exceptions with getAssociated()
00132         ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
00133         ### an exception for them.
00134 
00137         public function testGetAssociatedExceptionsForNsMedia() {
00138                 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA   ) );
00139         }
00140 
00144         public function testGetAssociatedExceptionsForNsSpecial() {
00145                 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
00146         }
00147 
00151 /*
00152         public function testExists() {
00153                 // Remove the following lines when you implement this test.
00154                 $this->markTestIncomplete(
00155                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00156                 );
00157         }
00158 */
00159 
00166         public function testEquals() {
00167                 $this->assertTrue( MWNamespace::equals( NS_MAIN, NS_MAIN ) );
00168                 $this->assertTrue( MWNamespace::equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
00169                 $this->assertTrue( MWNamespace::equals( NS_USER, NS_USER ) );
00170                 $this->assertTrue( MWNamespace::equals( NS_USER, 2 ) );
00171                 $this->assertTrue( MWNamespace::equals( NS_USER_TALK, NS_USER_TALK ) );
00172                 $this->assertTrue( MWNamespace::equals( NS_SPECIAL, NS_SPECIAL ) );
00173                 $this->assertFalse( MWNamespace::equals( NS_MAIN, NS_TALK ) );
00174                 $this->assertFalse( MWNamespace::equals( NS_USER, NS_USER_TALK ) );
00175                 $this->assertFalse( MWNamespace::equals( NS_PROJECT, NS_TEMPLATE ) );
00176         }
00177 
00181         public function testSubjectEquals() {
00182                 $this->assertSameSubject( NS_MAIN, NS_MAIN );
00183                 $this->assertSameSubject( NS_MAIN, 0 ); // In case we make NS_MAIN 'MAIN'
00184                 $this->assertSameSubject( NS_USER, NS_USER );
00185                 $this->assertSameSubject( NS_USER, 2 );
00186                 $this->assertSameSubject( NS_USER_TALK, NS_USER_TALK );
00187                 $this->assertSameSubject( NS_SPECIAL, NS_SPECIAL );
00188                 $this->assertSameSubject( NS_MAIN, NS_TALK );
00189                 $this->assertSameSubject( NS_USER, NS_USER_TALK );
00190 
00191                 $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
00192                 $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN     );
00193         }
00194 
00195         public function testSpecialAndMediaAreDifferentSubjects() {
00196                 $this->assertDifferentSubject(
00197                         NS_MEDIA, NS_SPECIAL,
00198                         "NS_MEDIA and NS_SPECIAL are different subject namespaces"
00199                 );
00200                 $this->assertDifferentSubject(
00201                         NS_SPECIAL, NS_MEDIA,
00202                         "NS_SPECIAL and NS_MEDIA are different subject namespaces"
00203                 );
00204 
00205         }
00206 
00210 /*
00211         public function testGetCanonicalNamespaces() {
00212                 // Remove the following lines when you implement this test.
00213                 $this->markTestIncomplete(
00214                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00215                 );
00216         }
00217 */
00221 /*
00222         public function testGetCanonicalName() {
00223                 // Remove the following lines when you implement this test.
00224                 $this->markTestIncomplete(
00225                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00226                 );
00227         }
00228 */
00232 /*
00233         public function testGetCanonicalIndex() {
00234                 // Remove the following lines when you implement this test.
00235                 $this->markTestIncomplete(
00236                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00237                 );
00238         }
00239 */
00243 /*
00244         public function testGetValidNamespaces() {
00245                 // Remove the following lines when you implement this test.
00246                 $this->markTestIncomplete(
00247                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
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 
00352                 # test !is_array( $wgcontentNamespaces )
00353                 $wgContentNamespaces = '';
00354                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00355 
00356                 $wgContentNamespaces = false;
00357                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00358 
00359                 $wgContentNamespaces = null;
00360                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00361 
00362                 $wgContentNamespaces = 5;
00363                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00364 
00365                 # test $wgContentNamespaces === array()
00366                 $wgContentNamespaces = array();
00367                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00368 
00369                 # test !in_array( NS_MAIN, $wgContentNamespaces )
00370                 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
00371                 $this->assertEquals(
00372                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00373                         MWNamespace::getcontentNamespaces(),
00374                         'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
00375                 );
00376 
00377                 # test other cases, return $wgcontentNamespaces as is
00378                 $wgContentNamespaces = array( NS_MAIN );
00379                 $this->assertEquals(
00380                         array( NS_MAIN ),
00381                         MWNamespace::getcontentNamespaces()
00382                 );
00383 
00384                 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
00385                 $this->assertEquals(
00386                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00387                         MWNamespace::getcontentNamespaces()
00388                 );
00389         }
00390 
00393         public function testGetSubjectNamespaces() {
00394                 $subjectsNS = MWNamespace::getSubjectNamespaces();
00395                 $this->assertContains(    NS_MAIN, $subjectsNS,
00396                         "Talk namespaces should have NS_MAIN" );
00397                 $this->assertNotContains( NS_TALK, $subjectsNS,
00398                         "Talk namespaces should have NS_TALK" );
00399 
00400                 $this->assertNotContains( NS_MEDIA, $subjectsNS,
00401                         "Talk namespaces should not have NS_MEDIA" );
00402                 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
00403                         "Talk namespaces should not have NS_SPECIAL" );
00404         }
00405 
00408         public function testGetTalkNamespaces() {
00409                 $talkNS = MWNamespace::getTalkNamespaces();
00410                 $this->assertContains(    NS_TALK, $talkNS,
00411                         "Subject namespaces should have NS_TALK" );
00412                 $this->assertNotContains( NS_MAIN, $talkNS,
00413                         "Subject namespaces should not have NS_MAIN" );
00414 
00415                 $this->assertNotContains( NS_MEDIA, $talkNS,
00416                         "Subject namespaces should not have NS_MEDIA" );
00417                 $this->assertNotContains( NS_SPECIAL, $talkNS,
00418                         "Subject namespaces should not have NS_SPECIAL" );
00419         }
00420 
00425         public function testIsCapitalizedHardcodedAssertions() {
00426                 // NS_MEDIA and NS_FILE are treated the same
00427                 $this->assertEquals(
00428                         MWNamespace::isCapitalized( NS_MEDIA ),
00429                         MWNamespace::isCapitalized( NS_FILE  ),
00430                         'NS_MEDIA and NS_FILE have same capitalization rendering'
00431                 );
00432 
00433                 // Boths are capitalized by default
00434                 $this->assertIsCapitalized( NS_MEDIA );
00435                 $this->assertIsCapitalized( NS_FILE  );
00436 
00437                 // Always capitalized namespaces
00438                 // @see MWNamespace::$alwaysCapitalizedNamespaces
00439                 $this->assertIsCapitalized( NS_SPECIAL   );
00440                 $this->assertIsCapitalized( NS_USER      );
00441                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00442         }
00443 
00456         public function testIsCapitalizedWithWgCapitalLinks() {
00457                 global $wgCapitalLinks;
00458 
00459                 $this->assertIsCapitalized( NS_PROJECT      );
00460                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00461 
00462                 $wgCapitalLinks = false;
00463 
00464                 // hardcoded namespaces (see above function) are still capitalized:
00465                 $this->assertIsCapitalized( NS_SPECIAL   );
00466                 $this->assertIsCapitalized( NS_USER      );
00467                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00468 
00469                 // setting is correctly applied
00470                 $this->assertIsNotCapitalized( NS_PROJECT      );
00471                 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
00472         }
00473 
00480         public function testIsCapitalizedWithWgCapitalLinkOverrides() {
00481                 global $wgCapitalLinkOverrides;
00482 
00483                 // Test default settings
00484                 $this->assertIsCapitalized( NS_PROJECT      );
00485                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00486 
00487                 // hardcoded namespaces (see above function) are capitalized:
00488                 $this->assertIsCapitalized( NS_SPECIAL   );
00489                 $this->assertIsCapitalized( NS_USER      );
00490                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00491 
00492                 // Hardcoded namespaces remains capitalized
00493                 $wgCapitalLinkOverrides[NS_SPECIAL]   = false;
00494                 $wgCapitalLinkOverrides[NS_USER]      = false;
00495                 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
00496 
00497                 $this->assertIsCapitalized( NS_SPECIAL   );
00498                 $this->assertIsCapitalized( NS_USER      );
00499                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00500 
00501                 $wgCapitalLinkOverrides[NS_PROJECT] = false;
00502                 $this->assertIsNotCapitalized( NS_PROJECT );
00503 
00504                 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
00505                 $this->assertIsCapitalized( NS_PROJECT );
00506 
00507                 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
00508                 $this->assertIsCapitalized( NS_PROJECT );
00509         }
00510 
00511         public function testHasGenderDistinction() {
00512                 // Namespaces with gender distinctions
00513                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER      ) );
00514                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
00515 
00516                 // Other ones, "genderless"
00517                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA   ) );
00518                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
00519                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN    ) );
00520                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK    ) );
00521 
00522         }
00523 
00524         public function testIsNonincludable() {
00525                 global $wgNonincludableNamespaces;
00526 
00527                 $wgNonincludableNamespaces = array( NS_USER );
00528 
00529                 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
00530                 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
00531         }
00532 
00533         ####### HELPERS ###########################################################
00534         function __call( $method, $args ) {
00535                 // Call the real method if it exists
00536                 if( method_exists($this, $method ) ) {
00537                         return $this->$method( $args );
00538                 }
00539 
00540                 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
00541                         # Interprets arguments:
00542                         $ns  = $args[0];
00543                         $msg = isset($args[1]) ? $args[1] : " dummy message";
00544 
00545                         # Forge the namespace constant name:
00546                         if( $ns === 0 ) {
00547                                 $ns_name = "NS_MAIN";
00548                         } else {
00549                                 $ns_name = "NS_" . strtoupper(  MWNamespace::getCanonicalName( $ns ) );
00550                         }
00551                         # ... and the MWNamespace method name
00552                         $nsMethod = strtolower( $m[1] ) . $m[3];
00553 
00554                         $expect = ($m[2] === '');
00555                         $expect_name = $expect ? 'TRUE' : 'FALSE';
00556 
00557                         return $this->assertEquals( $expect,
00558                                 MWNamespace::$nsMethod( $ns, $msg ),
00559                                 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
00560                         );
00561                 }
00562 
00563                 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
00564         }
00565 
00566         function assertSameSubject( $ns1, $ns2, $msg = '' ) {
00567                 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00568         }
00569         function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
00570                 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00571         }
00572 }
00573