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
{
15 * Sets up the fixture, for example, opens a network connection.
16 * This method is called before a test is executed.
18 protected function setUp() {
22 * Tears down the fixture, for example, closes a network connection.
23 * This method is called after a test is executed.
25 protected function tearDown() {
29 #### START OF TESTS #########################################################
32 * @todo Write more texts, handle $wgAllowImageMoving setting
34 public function testIsMovable() {
35 $this->assertFalse( MWNamespace
::isMovable( NS_CATEGORY
) );
36 # @todo FIXME: Write more tests!!
40 * Please make sure to change testIsTalk() if you change the assertions below
42 public function testIsMain() {
44 $this->assertTrue( MWNamespace
::isMain( NS_MEDIA
) );
45 $this->assertTrue( MWNamespace
::isMain( NS_SPECIAL
) );
48 $this->assertTrue( MWNamespace
::isMain( NS_MAIN
) );
49 $this->assertTrue( MWNamespace
::isMain( NS_USER
) );
50 $this->assertTrue( MWNamespace
::isMain( 100 ) ); # user defined
53 $this->assertFalse( MWNamespace
::isMain( NS_TALK
) );
54 $this->assertFalse( MWNamespace
::isMain( NS_USER_TALK
) );
55 $this->assertFalse( MWNamespace
::isMain( 101 ) ); # user defined
59 * Reverse of testIsMain().
60 * Please update testIsMain() if you change assertions below
62 public function testIsTalk() {
64 $this->assertFalse( MWNamespace
::isTalk( NS_MEDIA
) );
65 $this->assertFalse( MWNamespace
::isTalk( NS_SPECIAL
) );
68 $this->assertFalse( MWNamespace
::isTalk( NS_MAIN
) );
69 $this->assertFalse( MWNamespace
::isTalk( NS_USER
) );
70 $this->assertFalse( MWNamespace
::isTalk( 100 ) ); # user defined
73 $this->assertTrue( MWNamespace
::isTalk( NS_TALK
) );
74 $this->assertTrue( MWNamespace
::isTalk( NS_USER_TALK
) );
75 $this->assertTrue( MWNamespace
::isTalk( 101 ) ); # user defined
79 * Regular getTalk() calls
80 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
81 * the function testGetTalkExceptions()
83 public function testGetTalk() {
84 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
88 * Exceptions with getTalk()
89 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
90 * @expectedException MWException
92 public function testGetTalkExceptionsForNsMedia() {
93 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
97 * Exceptions with getTalk()
98 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
99 * @expectedException MWException
101 public function testGetTalkExceptionsForNsSpecial() {
102 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
106 * Regular getAssociated() calls
107 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
108 * the function testGetAssociatedExceptions()
110 public function testGetAssociated() {
111 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
112 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
116 ### Exceptions with getAssociated()
117 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
118 ### an exception for them.
120 * @expectedException MWException
122 public function testGetAssociatedExceptionsForNsMedia() {
123 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
127 * @expectedException MWException
129 public function testGetAssociatedExceptionsForNsSpecial() {
130 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
135 public function testGetSubject() {
136 // Special namespaces are their own subjects
137 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
138 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
140 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
141 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
145 * @todo Implement testExists().
148 public function testExists() {
149 // Remove the following lines when you implement this test.
150 $this->markTestIncomplete(
151 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
156 * @todo Implement testGetCanonicalNamespaces().
159 public function testGetCanonicalNamespaces() {
160 // Remove the following lines when you implement this test.
161 $this->markTestIncomplete(
162 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
167 * @todo Implement testGetCanonicalName().
170 public function testGetCanonicalName() {
171 // Remove the following lines when you implement this test.
172 $this->markTestIncomplete(
173 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
178 * @todo Implement testGetCanonicalIndex().
181 public function testGetCanonicalIndex() {
182 // Remove the following lines when you implement this test.
183 $this->markTestIncomplete(
184 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
189 * @todo Implement testGetValidNamespaces().
192 public function testGetValidNamespaces() {
193 // Remove the following lines when you implement this test.
194 $this->markTestIncomplete(
195 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
201 public function testCanTalk() {
202 $this->assertFalse( MWNamespace
::canTalk( NS_MEDIA
) );
203 $this->assertFalse( MWNamespace
::canTalk( NS_SPECIAL
) );
205 $this->assertTrue( MWNamespace
::canTalk( NS_MAIN
) );
206 $this->assertTrue( MWNamespace
::canTalk( NS_TALK
) );
207 $this->assertTrue( MWNamespace
::canTalk( NS_USER
) );
208 $this->assertTrue( MWNamespace
::canTalk( NS_USER_TALK
) );
210 // User defined namespaces
211 $this->assertTrue( MWNamespace
::canTalk( 100 ) );
212 $this->assertTrue( MWNamespace
::canTalk( 101 ) );
217 public function testIsContent() {
218 // NS_MAIN is a content namespace per DefaultSettings.php
219 // and per function definition.
220 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
222 global $wgContentNamespaces;
224 $saved = $wgContentNamespaces;
226 $wgContentNamespaces[] = NS_MAIN
;
227 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
229 // Other namespaces which are not expected to be content
230 if ( isset( $wgContentNamespaces[NS_MEDIA
] ) ) {
231 unset( $wgContentNamespaces[NS_MEDIA
] );
233 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
235 if ( isset( $wgContentNamespaces[NS_SPECIAL
] ) ) {
236 unset( $wgContentNamespaces[NS_SPECIAL
] );
238 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
240 if ( isset( $wgContentNamespaces[NS_TALK
] ) ) {
241 unset( $wgContentNamespaces[NS_TALK
] );
243 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
245 if ( isset( $wgContentNamespaces[NS_USER
] ) ) {
246 unset( $wgContentNamespaces[NS_USER
] );
248 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
250 if ( isset( $wgContentNamespaces[NS_CATEGORY
] ) ) {
251 unset( $wgContentNamespaces[NS_CATEGORY
] );
253 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
255 if ( isset( $wgContentNamespaces[100] ) ) {
256 unset( $wgContentNamespaces[100] );
258 $this->assertFalse( MWNamespace
::isContent( 100 ) );
260 $wgContentNamespaces = saved
;
264 * Similar to testIsContent() but alters the $wgContentNamespaces
267 public function testIsContentWithAdditionsInWgContentNamespaces() {
268 // NS_MAIN is a content namespace per DefaultSettings.php
269 // and per function definition.
270 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
272 // Tests that user defined namespace #252 is not content:
273 $this->assertFalse( MWNamespace
::isContent( 252 ) );
275 # @todo FIXME: Is global saving really required for PHPUnit?
276 // Bless namespace # 252 as a content namespace
277 global $wgContentNamespaces;
278 $savedGlobal = $wgContentNamespaces;
279 $wgContentNamespaces[] = 252;
280 $this->assertTrue( MWNamespace
::isContent( 252 ) );
282 // Makes sure NS_MAIN was not impacted
283 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
286 $wgContentNamespaces = $savedGlobal;
288 // Verify namespaces after global restauration
289 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
290 $this->assertFalse( MWNamespace
::isContent( 252 ) );
293 public function testIsWatchable() {
294 // Specials namespaces are not watchable
295 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
296 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
298 // Core defined namespaces are watchables
299 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
300 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
302 // Additional, user defined namespaces are watchables
303 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
304 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
307 public function testHasSubpages() {
308 // Special namespaces:
309 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
310 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
312 // namespaces without subpages
314 global $wgNamespacesWithSubpages;
316 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages ) ) {
317 $saved = $wgNamespacesWithSubpages[NS_MAIN
];
318 unset( $wgNamespacesWithSubpages[NS_MAIN
] );
321 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
323 $wgNamespacesWithSubpages[NS_MAIN
] = true;
324 $this->assertTrue( MWNamespace
::hasSubpages( NS_MAIN
) );
325 $wgNamespacesWithSubpages[NS_MAIN
] = false;
326 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
329 if( $saved !== null ) {
330 $wgNamespacesWithSubpages[NS_MAIN
] = $saved;
333 // Some namespaces with subpages
334 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
335 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
336 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
341 public function testGetContentNamespaces() {
344 MWNamespace
::getcontentNamespaces(),
345 '$wgContentNamespaces is an array with only NS_MAIN by default'
348 global $wgContentNamespaces;
350 # test !is_array( $wgcontentNamespaces )
351 $wgContentNamespaces = '';
352 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
353 $wgContentNamespaces = false;
354 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
355 $wgContentNamespaces = null;
356 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
357 $wgContentNamespaces = 5;
358 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
360 # test $wgContentNamespaces === array()
361 $wgContentNamespaces = array();
362 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
364 # test !in_array( NS_MAIN, $wgContentNamespaces )
365 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
367 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
368 MWNamespace
::getcontentNamespaces(),
369 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
372 # test other cases, return $wgcontentNamespaces as is
373 $wgContentNamespaces = array( NS_MAIN
);
376 MWNamespace
::getcontentNamespaces()
379 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
381 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
382 MWNamespace
::getcontentNamespaces()
388 * Some namespaces are always capitalized per code definition
389 * in MWNamespace::$alwaysCapitalizedNamespaces
391 public function testIsCapitalizedHardcodedAssertions() {
392 // NS_MEDIA and NS_FILE are treated the same
394 MWNamespace
::isCapitalized( NS_MEDIA
),
395 MWNamespace
::isCapitalized( NS_FILE
),
396 'NS_MEDIA and NS_FILE have same capitalization rendering'
399 // Boths are capitalized by default
400 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
401 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
403 // Always capitalized namespaces
404 // @see MWNamespace::$alwaysCapitalizedNamespaces
405 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
406 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
407 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
411 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
412 * global $wgCapitalLink setting to have extended coverage.
414 * MWNamespace::isCapitalized() rely on two global settings:
415 * $wgCapitalLinkOverrides = array(); by default
416 * $wgCapitalLinks = true; by default
417 * This function test $wgCapitalLinks
419 * Global setting correctness is tested against the NS_PROJECT and
420 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
422 public function testIsCapitalizedWithWgCapitalLinks() {
423 global $wgCapitalLinks;
424 // Save the global to easily reset to MediaWiki default settings
425 $savedGlobal = $wgCapitalLinks;
427 $wgCapitalLinks = true;
428 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
429 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
431 $wgCapitalLinks = false;
432 // hardcoded namespaces (see above function) are still capitalized:
433 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
434 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
435 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
436 // setting is correctly applied
437 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
438 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
440 // reset global state:
441 $wgCapitalLinks = $savedGlobal;
445 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
446 * testing the $wgCapitalLinkOverrides global.
448 * @todo split groups of assertions in autonomous testing functions
450 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
451 global $wgCapitalLinkOverrides;
452 // Save the global to easily reset to MediaWiki default settings
453 $savedGlobal = $wgCapitalLinkOverrides;
455 // Test default settings
456 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
457 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
458 // hardcoded namespaces (see above function) are capitalized:
459 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
460 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
461 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
463 // Hardcoded namespaces remains capitalized
464 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
465 $wgCapitalLinkOverrides[NS_USER
] = false;
466 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
467 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
468 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
469 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
471 $wgCapitalLinkOverrides = $savedGlobal;
472 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
473 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
474 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
475 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
476 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
477 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
479 // reset global state:
480 $wgCapitalLinkOverrides = $savedGlobal;
483 public function testHasGenderDistinction() {
484 // Namespaces with gender distinctions
485 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
486 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
488 // Other ones, "genderless"
489 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
490 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
491 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
492 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );