MediaWiki  REL1_21
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         */
00240 
00244         /*
00245         public function testGetValidNamespaces() {
00246                 // Remove the following lines when you implement this test.
00247                 $this->markTestIncomplete(
00248                   'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
00249                 );
00250         }
00251         */
00252 
00255         public function testCanTalk() {
00256                 $this->assertCanNotTalk( NS_MEDIA );
00257                 $this->assertCanNotTalk( NS_SPECIAL );
00258 
00259                 $this->assertCanTalk( NS_MAIN );
00260                 $this->assertCanTalk( NS_TALK );
00261                 $this->assertCanTalk( NS_USER );
00262                 $this->assertCanTalk( NS_USER_TALK );
00263 
00264                 // User defined namespaces
00265                 $this->assertCanTalk( 100 );
00266                 $this->assertCanTalk( 101 );
00267         }
00268 
00271         public function testIsContent() {
00272                 // NS_MAIN is a content namespace per DefaultSettings.php
00273                 // and per function definition.
00274 
00275                 $this->assertIsContent( NS_MAIN );
00276 
00277                 // Other namespaces which are not expected to be content
00278 
00279                 $this->assertIsNotContent( NS_MEDIA );
00280                 $this->assertIsNotContent( NS_SPECIAL );
00281                 $this->assertIsNotContent( NS_TALK );
00282                 $this->assertIsNotContent( NS_USER );
00283                 $this->assertIsNotContent( NS_CATEGORY );
00284                 $this->assertIsNotContent( 100 );
00285         }
00286 
00291         public function testIsContentAdvanced() {
00292                 global $wgContentNamespaces;
00293 
00294                 // Test that user defined namespace #252 is not content
00295                 $this->assertIsNotContent( 252 );
00296 
00297                 // Bless namespace # 252 as a content namespace
00298                 $wgContentNamespaces[] = 252;
00299 
00300                 $this->assertIsContent( 252 );
00301 
00302                 // Makes sure NS_MAIN was not impacted
00303                 $this->assertIsContent( NS_MAIN );
00304         }
00305 
00306         public function testIsWatchable() {
00307                 // Specials namespaces are not watchable
00308                 $this->assertIsNotWatchable( NS_MEDIA );
00309                 $this->assertIsNotWatchable( NS_SPECIAL );
00310 
00311                 // Core defined namespaces are watchables
00312                 $this->assertIsWatchable( NS_MAIN );
00313                 $this->assertIsWatchable( NS_TALK );
00314 
00315                 // Additional, user defined namespaces are watchables
00316                 $this->assertIsWatchable( 100 );
00317                 $this->assertIsWatchable( 101 );
00318         }
00319 
00320         public function testHasSubpages() {
00321                 global $wgNamespacesWithSubpages;
00322 
00323                 // Special namespaces:
00324                 $this->assertHasNotSubpages( NS_MEDIA );
00325                 $this->assertHasNotSubpages( NS_SPECIAL );
00326 
00327                 // Namespaces without subpages
00328                 $this->assertHasNotSubpages( NS_MAIN );
00329 
00330                 $wgNamespacesWithSubpages[NS_MAIN] = true;
00331                 $this->assertHasSubpages( NS_MAIN );
00332 
00333                 $wgNamespacesWithSubpages[NS_MAIN] = false;
00334                 $this->assertHasNotSubpages( NS_MAIN );
00335 
00336                 // Some namespaces with subpages
00337                 $this->assertHasSubpages( NS_TALK );
00338                 $this->assertHasSubpages( NS_USER );
00339                 $this->assertHasSubpages( NS_USER_TALK );
00340         }
00341 
00344         public function testGetContentNamespaces() {
00345                 global $wgContentNamespaces;
00346 
00347                 $this->assertEquals(
00348                         array( NS_MAIN ),
00349                         MWNamespace::getcontentNamespaces(),
00350                         '$wgContentNamespaces is an array with only NS_MAIN by default'
00351                 );
00352 
00353 
00354                 # test !is_array( $wgcontentNamespaces )
00355                 $wgContentNamespaces = '';
00356                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00357 
00358                 $wgContentNamespaces = false;
00359                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00360 
00361                 $wgContentNamespaces = null;
00362                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00363 
00364                 $wgContentNamespaces = 5;
00365                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00366 
00367                 # test $wgContentNamespaces === array()
00368                 $wgContentNamespaces = array();
00369                 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
00370 
00371                 # test !in_array( NS_MAIN, $wgContentNamespaces )
00372                 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
00373                 $this->assertEquals(
00374                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00375                         MWNamespace::getcontentNamespaces(),
00376                         'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
00377                 );
00378 
00379                 # test other cases, return $wgcontentNamespaces as is
00380                 $wgContentNamespaces = array( NS_MAIN );
00381                 $this->assertEquals(
00382                         array( NS_MAIN ),
00383                         MWNamespace::getcontentNamespaces()
00384                 );
00385 
00386                 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
00387                 $this->assertEquals(
00388                         array( NS_MAIN, NS_USER, NS_CATEGORY ),
00389                         MWNamespace::getcontentNamespaces()
00390                 );
00391         }
00392 
00395         public function testGetSubjectNamespaces() {
00396                 $subjectsNS = MWNamespace::getSubjectNamespaces();
00397                 $this->assertContains( NS_MAIN, $subjectsNS,
00398                         "Talk namespaces should have NS_MAIN" );
00399                 $this->assertNotContains( NS_TALK, $subjectsNS,
00400                         "Talk namespaces should have NS_TALK" );
00401 
00402                 $this->assertNotContains( NS_MEDIA, $subjectsNS,
00403                         "Talk namespaces should not have NS_MEDIA" );
00404                 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
00405                         "Talk namespaces should not have NS_SPECIAL" );
00406         }
00407 
00410         public function testGetTalkNamespaces() {
00411                 $talkNS = MWNamespace::getTalkNamespaces();
00412                 $this->assertContains( NS_TALK, $talkNS,
00413                         "Subject namespaces should have NS_TALK" );
00414                 $this->assertNotContains( NS_MAIN, $talkNS,
00415                         "Subject namespaces should not have NS_MAIN" );
00416 
00417                 $this->assertNotContains( NS_MEDIA, $talkNS,
00418                         "Subject namespaces should not have NS_MEDIA" );
00419                 $this->assertNotContains( NS_SPECIAL, $talkNS,
00420                         "Subject namespaces should not have NS_SPECIAL" );
00421         }
00422 
00427         public function testIsCapitalizedHardcodedAssertions() {
00428                 // NS_MEDIA and NS_FILE are treated the same
00429                 $this->assertEquals(
00430                         MWNamespace::isCapitalized( NS_MEDIA ),
00431                         MWNamespace::isCapitalized( NS_FILE ),
00432                         'NS_MEDIA and NS_FILE have same capitalization rendering'
00433                 );
00434 
00435                 // Boths are capitalized by default
00436                 $this->assertIsCapitalized( NS_MEDIA );
00437                 $this->assertIsCapitalized( NS_FILE );
00438 
00439                 // Always capitalized namespaces
00440                 // @see MWNamespace::$alwaysCapitalizedNamespaces
00441                 $this->assertIsCapitalized( NS_SPECIAL );
00442                 $this->assertIsCapitalized( NS_USER );
00443                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00444         }
00445 
00458         public function testIsCapitalizedWithWgCapitalLinks() {
00459                 global $wgCapitalLinks;
00460 
00461                 $this->assertIsCapitalized( NS_PROJECT );
00462                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00463 
00464                 $wgCapitalLinks = false;
00465 
00466                 // hardcoded namespaces (see above function) are still capitalized:
00467                 $this->assertIsCapitalized( NS_SPECIAL );
00468                 $this->assertIsCapitalized( NS_USER );
00469                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00470 
00471                 // setting is correctly applied
00472                 $this->assertIsNotCapitalized( NS_PROJECT );
00473                 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
00474         }
00475 
00482         public function testIsCapitalizedWithWgCapitalLinkOverrides() {
00483                 global $wgCapitalLinkOverrides;
00484 
00485                 // Test default settings
00486                 $this->assertIsCapitalized( NS_PROJECT );
00487                 $this->assertIsCapitalized( NS_PROJECT_TALK );
00488 
00489                 // hardcoded namespaces (see above function) are capitalized:
00490                 $this->assertIsCapitalized( NS_SPECIAL );
00491                 $this->assertIsCapitalized( NS_USER );
00492                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00493 
00494                 // Hardcoded namespaces remains capitalized
00495                 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
00496                 $wgCapitalLinkOverrides[NS_USER] = false;
00497                 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
00498 
00499                 $this->assertIsCapitalized( NS_SPECIAL );
00500                 $this->assertIsCapitalized( NS_USER );
00501                 $this->assertIsCapitalized( NS_MEDIAWIKI );
00502 
00503                 $wgCapitalLinkOverrides[NS_PROJECT] = false;
00504                 $this->assertIsNotCapitalized( NS_PROJECT );
00505 
00506                 $wgCapitalLinkOverrides[NS_PROJECT] = true;
00507                 $this->assertIsCapitalized( NS_PROJECT );
00508 
00509                 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
00510                 $this->assertIsCapitalized( NS_PROJECT );
00511         }
00512 
00513         public function testHasGenderDistinction() {
00514                 // Namespaces with gender distinctions
00515                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
00516                 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
00517 
00518                 // Other ones, "genderless"
00519                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
00520                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
00521                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
00522                 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
00523         }
00524 
00525         public function testIsNonincludable() {
00526                 global $wgNonincludableNamespaces;
00527 
00528                 $wgNonincludableNamespaces = array( NS_USER );
00529 
00530                 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
00531                 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
00532         }
00533 
00534         ####### HELPERS ###########################################################
00535         function __call( $method, $args ) {
00536                 // Call the real method if it exists
00537                 if ( method_exists( $this, $method ) ) {
00538                         return $this->$method( $args );
00539                 }
00540 
00541                 if ( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
00542                         # Interprets arguments:
00543                         $ns = $args[0];
00544                         $msg = isset( $args[1] ) ? $args[1] : " dummy message";
00545 
00546                         # Forge the namespace constant name:
00547                         if ( $ns === 0 ) {
00548                                 $ns_name = "NS_MAIN";
00549                         } else {
00550                                 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
00551                         }
00552                         # ... and the MWNamespace method name
00553                         $nsMethod = strtolower( $m[1] ) . $m[3];
00554 
00555                         $expect = ( $m[2] === '' );
00556                         $expect_name = $expect ? 'TRUE' : 'FALSE';
00557 
00558                         return $this->assertEquals( $expect,
00559                                 MWNamespace::$nsMethod( $ns, $msg ),
00560                                 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
00561                         );
00562                 }
00563 
00564                 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
00565         }
00566 
00567         function assertSameSubject( $ns1, $ns2, $msg = '' ) {
00568                 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00569         }
00570 
00571         function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
00572                 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
00573         }
00574 }