3 * @author Ashar Voultoiz
4 * @copyright Copyright © 2011, Ashar Voultoiz
9 * Test class for MWNamespace.
10 * Generated by PHPUnit on 2011-02-20 at 21:01:55.
13 class MWNamespaceTest
extends MediaWikiTestCase
{
20 * Sets up the fixture, for example, opens a network connection.
21 * This method is called before a test is executed.
23 protected function setUp() {
24 $this->object = new MWNamespace
;
28 * Tears down the fixture, for example, closes a network connection.
29 * This method is called after a test is executed.
31 protected function tearDown() {
35 #### START OF TESTS #########################################################
38 * @todo Write more texts, handle $wgAllowImageMoving setting
40 public function testIsMovable() {
41 $this->assertFalse( MWNamespace
::isMovable( NS_CATEGORY
) );
42 # @todo FIXME: Write more tests!!
46 * Please make sure to change testIsTalk() if you change the assertions below
48 public function testIsMain() {
50 $this->assertTrue( MWNamespace
::isMain( NS_MEDIA
) );
51 $this->assertTrue( MWNamespace
::isMain( NS_SPECIAL
) );
54 $this->assertTrue( MWNamespace
::isMain( NS_MAIN
) );
55 $this->assertTrue( MWNamespace
::isMain( NS_USER
) );
56 $this->assertTrue( MWNamespace
::isMain( 100 ) ); # user defined
59 $this->assertFalse( MWNamespace
::isMain( NS_TALK
) );
60 $this->assertFalse( MWNamespace
::isMain( NS_USER_TALK
) );
61 $this->assertFalse( MWNamespace
::isMain( 101 ) ); # user defined
65 * Reverse of testIsMain().
66 * Please update testIsMain() if you change assertions below
68 public function testIsTalk() {
70 $this->assertFalse( MWNamespace
::isTalk( NS_MEDIA
) );
71 $this->assertFalse( MWNamespace
::isTalk( NS_SPECIAL
) );
74 $this->assertFalse( MWNamespace
::isTalk( NS_MAIN
) );
75 $this->assertFalse( MWNamespace
::isTalk( NS_USER
) );
76 $this->assertFalse( MWNamespace
::isTalk( 100 ) ); # user defined
79 $this->assertTrue( MWNamespace
::isTalk( NS_TALK
) );
80 $this->assertTrue( MWNamespace
::isTalk( NS_USER_TALK
) );
81 $this->assertTrue( MWNamespace
::isTalk( 101 ) ); # user defined
85 * Regular getTalk() calls
86 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
87 * the function testGetTalkExceptions()
89 public function testGetTalk() {
90 $this->assertEquals( MWNamespace
::getTalk( NS_MAIN
), NS_TALK
);
94 * Exceptions with getTalk()
95 * NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raise an exception for them.
96 * @expectedException MWException
98 public function testGetTalkExceptions() {
99 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
100 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
104 * Regular getAssociated() calls
105 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
106 * the function testGetAssociatedExceptions()
108 public function testGetAssociated() {
109 $this->assertEquals( MWNamespace
::getAssociated( NS_MAIN
), NS_TALK
);
110 $this->assertEquals( MWNamespace
::getAssociated( NS_TALK
), NS_MAIN
);
114 ### Exceptions with getAssociated()
115 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
116 ### an exception for them.
118 * @expectedException MWException
120 public function testGetAssociatedExceptionsForNsMedia() {
121 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
124 * @expectedException MWException
126 public function testGetAssociatedExceptionsForNsSpecial() {
127 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
132 public function testGetSubject() {
133 // Special namespaces are their own subjects
134 $this->assertEquals( MWNamespace
::getSubject( NS_MEDIA
), NS_MEDIA
);
135 $this->assertEquals( MWNamespace
::getSubject( NS_SPECIAL
), NS_SPECIAL
);
137 $this->assertEquals( MWNamespace
::getSubject( NS_TALK
), NS_MAIN
);
138 $this->assertEquals( MWNamespace
::getSubject( NS_USER_TALK
), NS_USER
);
142 * @todo Implement testExists().
144 public function testExists() {
145 // Remove the following lines when you implement this test.
146 $this->markTestIncomplete(
147 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
152 * @todo Implement testGetCanonicalNamespaces().
154 public function testGetCanonicalNamespaces() {
155 // Remove the following lines when you implement this test.
156 $this->markTestIncomplete(
157 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
162 * @todo Implement testGetCanonicalName().
164 public function testGetCanonicalName() {
165 // Remove the following lines when you implement this test.
166 $this->markTestIncomplete(
167 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
172 * @todo Implement testGetCanonicalIndex().
174 public function testGetCanonicalIndex() {
175 // Remove the following lines when you implement this test.
176 $this->markTestIncomplete(
177 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
182 * @todo Implement testGetValidNamespaces().
184 public function testGetValidNamespaces() {
185 // Remove the following lines when you implement this test.
186 $this->markTestIncomplete(
187 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
193 public function testCanTalk() {
194 $this->assertFalse( MWNamespace
::canTalk( NS_MEDIA
) );
195 $this->assertFalse( MWNamespace
::canTalk( NS_SPECIAL
) );
197 $this->assertTrue( MWNamespace
::canTalk( NS_MAIN
) );
198 $this->assertTrue( MWNamespace
::canTalk( NS_TALK
) );
199 $this->assertTrue( MWNamespace
::canTalk( NS_USER
) );
200 $this->assertTrue( MWNamespace
::canTalk( NS_USER_TALK
) );
202 // User defined namespaces
203 $this->assertTrue( MWNamespace
::canTalk( 100 ) );
204 $this->assertTrue( MWNamespace
::canTalk( 101 ) );
209 public function testIsContent() {
210 // NS_MAIN is a content namespace per DefaultSettings.php
211 // and per function definition.
212 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
214 // Other namespaces which are not expected to be content
215 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
216 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
217 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
218 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
219 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
220 // User defined namespace:
221 $this->assertFalse( MWNamespace
::isContent( 100 ) );
225 * Similar to testIsContent() but alters the $wgContentNamespaces
228 public function testIsContentWithAdditionsInWgContentNamespaces() {
229 // NS_MAIN is a content namespace per DefaultSettings.php
230 // and per function definition.
231 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
233 // Tests that user defined namespace #252 is not content:
234 $this->assertFalse( MWNamespace
::isContent( 252 ) );
236 # @todo FIXME: Is global saving really required for PHPUnit?
237 // Bless namespace # 252 as a content namespace
238 global $wgContentNamespaces;
239 $savedGlobal = $wgContentNamespaces;
240 $wgContentNamespaces[] = 252;
241 $this->assertTrue( MWNamespace
::isContent( 252 ) );
243 // Makes sure NS_MAIN was not impacted
244 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
247 $wgContentNamespaces = $savedGlobal;
249 // Verify namespaces after global restauration
250 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
251 $this->assertFalse( MWNamespace
::isContent( 252 ) );
254 public function testIsWatchable() {
255 // Specials namespaces are not watchable
256 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
257 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
259 // Core defined namespaces are watchables
260 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
261 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
263 // Additional, user defined namespaces are watchables
264 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
265 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
268 public function testHasSubpages() {
269 // Special namespaces:
270 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
271 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
273 // namespaces without subpages
274 global $wgNamespacesWithSubpages;
275 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages )
276 && $wgNamespacesWithSubpages[NS_MAIN
] === true
278 $this->markTestSkipped( "Main namespace has subpages enabled" );
280 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
283 // Some namespaces with subpages
284 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
285 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
286 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
291 public function testGetContentNamespaces() {
293 MWNamespace
::getcontentNamespaces(),
295 '$wgContentNamespaces is an array with only NS_MAIN by default'
298 global $wgContentNamespaces;
300 # test !is_array( $wgcontentNamespaces )
301 $wgContentNamespaces = '';
302 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
303 $wgContentNamespaces = false;
304 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
305 $wgContentNamespaces = null;
306 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
307 $wgContentNamespaces = 5;
308 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
310 # test $wgContentNamespaces === array()
311 $wgContentNamespaces = array();
312 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
314 # test !in_array( NS_MAIN, $wgContentNamespaces )
315 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
317 MWNamespace
::getcontentNamespaces(),
318 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
319 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
322 # test other cases, return $wgcontentNamespaces as is
323 $wgContentNamespaces = array( NS_MAIN
);
325 MWNamespace
::getcontentNamespaces(),
329 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
331 MWNamespace
::getcontentNamespaces(),
332 array( NS_MAIN
, NS_USER
, NS_CATEGORY
)
338 * Some namespaces are always capitalized per code definition
339 * in MWNamespace::$alwaysCapitalizedNamespaces
341 public function testIsCapitalizedHardcodedAssertions() {
342 // NS_MEDIA and NS_FILE are treated the same
344 MWNamespace
::isCapitalized( NS_MEDIA
),
345 MWNamespace
::isCapitalized( NS_FILE
),
346 'NS_MEDIA and NS_FILE have same capitalization rendering'
349 // Boths are capitalized by default
350 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
351 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
353 // Always capitalized namespaces
354 // @see MWNamespace::$alwaysCapitalizedNamespaces
355 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
356 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
357 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
361 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
362 * global $wgCapitalLink setting to have extended coverage.
364 * MWNamespace::isCapitalized() rely on two global settings:
365 * $wgCapitalLinkOverrides = array(); by default
366 * $wgCapitalLinks = true; by default
367 * This function test $wgCapitalLinks
369 * Global setting correctness is tested against the NS_PROJECT and
370 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
372 public function testIsCapitalizedWithWgCapitalLinks() {
373 global $wgCapitalLinks;
374 // Save the global to easily reset to MediaWiki default settings
375 $savedGlobal = $wgCapitalLinks;
377 $wgCapitalLinks = true;
378 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
379 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
381 $wgCapitalLinks = false;
382 // hardcoded namespaces (see above function) are still capitalized:
383 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
384 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
385 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
386 // setting is correctly applied
387 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
388 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
390 // reset global state:
391 $wgCapitalLinks = $savedGlobal;
395 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
396 * testing the $wgCapitalLinkOverrides global.
398 * @todo split groups of assertions in autonomous testing functions
400 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
401 global $wgCapitalLinkOverrides;
402 // Save the global to easily reset to MediaWiki default settings
403 $savedGlobal = $wgCapitalLinkOverrides;
405 // Test default settings
406 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
407 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
408 // hardcoded namespaces (see above function) are capitalized:
409 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
410 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
411 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
413 // Hardcoded namespaces remains capitalized
414 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
415 $wgCapitalLinkOverrides[NS_USER
] = false;
416 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
417 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
418 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
419 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
421 $wgCapitalLinkOverrides = $savedGlobal;
422 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
423 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
424 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
425 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
426 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
427 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
429 // reset global state:
430 $wgCapitalLinkOverrides = $savedGlobal;
433 public function testHasGenderDistinction() {
434 // Namespaces with gender distinctions
435 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
436 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
438 // Other ones, "genderless"
439 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
440 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
441 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
442 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );