3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
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 testIsSubject() {
44 $this->assertTrue( MWNamespace
::isSubject( NS_MEDIA
) );
45 $this->assertTrue( MWNamespace
::isSubject( NS_SPECIAL
) );
48 $this->assertTrue( MWNamespace
::isSubject( NS_MAIN
) );
49 $this->assertTrue( MWNamespace
::isSubject( NS_USER
) );
50 $this->assertTrue( MWNamespace
::isSubject( 100 ) ); # user defined
53 $this->assertFalse( MWNamespace
::isSubject( NS_TALK
) );
54 $this->assertFalse( MWNamespace
::isSubject( NS_USER_TALK
) );
55 $this->assertFalse( MWNamespace
::isSubject( 101 ) ); # user defined
58 $this->assertTrue( MWNamespace
::isMain( NS_MAIN
) == MWNamespace
::isSubject( NS_MAIN
) );
59 $this->assertTrue( MWNamespace
::isMain( NS_USER_TALK
) == MWNamespace
::isSubject( NS_USER_TALK
) );
63 * Reverse of testIsSubject().
64 * Please update testIsSubject() if you change assertions below
66 public function testIsTalk() {
68 $this->assertFalse( MWNamespace
::isTalk( NS_MEDIA
) );
69 $this->assertFalse( MWNamespace
::isTalk( NS_SPECIAL
) );
72 $this->assertFalse( MWNamespace
::isTalk( NS_MAIN
) );
73 $this->assertFalse( MWNamespace
::isTalk( NS_USER
) );
74 $this->assertFalse( MWNamespace
::isTalk( 100 ) ); # user defined
77 $this->assertTrue( MWNamespace
::isTalk( NS_TALK
) );
78 $this->assertTrue( MWNamespace
::isTalk( NS_USER_TALK
) );
79 $this->assertTrue( MWNamespace
::isTalk( 101 ) ); # user defined
84 public function testGetSubject() {
85 // Special namespaces are their own subjects
86 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
87 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
89 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
90 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
94 * Regular getTalk() calls
95 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
96 * the function testGetTalkExceptions()
98 public function testGetTalk() {
99 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
100 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_TALK
) );
101 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER
) );
102 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER_TALK
) );
106 * Exceptions with getTalk()
107 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
108 * @expectedException MWException
110 public function testGetTalkExceptionsForNsMedia() {
111 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
115 * Exceptions with getTalk()
116 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
117 * @expectedException MWException
119 public function testGetTalkExceptionsForNsSpecial() {
120 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
124 * Regular getAssociated() calls
125 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
126 * the function testGetAssociatedExceptions()
128 public function testGetAssociated() {
129 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
130 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
134 ### Exceptions with getAssociated()
135 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
136 ### an exception for them.
138 * @expectedException MWException
140 public function testGetAssociatedExceptionsForNsMedia() {
141 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
145 * @expectedException MWException
147 public function testGetAssociatedExceptionsForNsSpecial() {
148 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
152 * @todo Implement testExists().
155 public function testExists() {
156 // Remove the following lines when you implement this test.
157 $this->markTestIncomplete(
158 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
164 * Test MWNamespace::equals
165 * Note if we add a namespace registration system with keys like 'MAIN'
166 * we should add tests here for equivilance on things like 'MAIN' == 0
167 * and 'MAIN' == NS_MAIN.
169 public function testEquals() {
170 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, NS_MAIN
) );
171 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
172 $this->assertTrue( MWNamespace
::equals( NS_USER
, NS_USER
) );
173 $this->assertTrue( MWNamespace
::equals( NS_USER
, 2 ) );
174 $this->assertTrue( MWNamespace
::equals( NS_USER_TALK
, NS_USER_TALK
) );
175 $this->assertTrue( MWNamespace
::equals( NS_SPECIAL
, NS_SPECIAL
) );
176 $this->assertFalse( MWNamespace
::equals( NS_MAIN
, NS_TALK
) );
177 $this->assertFalse( MWNamespace
::equals( NS_USER
, NS_USER_TALK
) );
178 $this->assertFalse( MWNamespace
::equals( NS_PROJECT
, NS_TEMPLATE
) );
182 * Test MWNamespace::subjectEquals
184 public function testSubjectEquals() {
185 $this->assertTrue( MWNamespace
::subjectEquals( NS_MAIN
, NS_MAIN
) );
186 $this->assertTrue( MWNamespace
::subjectEquals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
187 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER
, NS_USER
) );
188 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER
, 2 ) );
189 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER_TALK
, NS_USER_TALK
) );
190 $this->assertTrue( MWNamespace
::subjectEquals( NS_SPECIAL
, NS_SPECIAL
) );
191 $this->assertTrue( MWNamespace
::subjectEquals( NS_MAIN
, NS_TALK
) );
192 $this->assertTrue( MWNamespace
::subjectEquals( NS_USER
, NS_USER_TALK
) );
193 $this->assertFalse( MWNamespace
::subjectEquals( NS_PROJECT
, NS_TEMPLATE
) );
197 * @todo Implement testGetCanonicalNamespaces().
200 public function testGetCanonicalNamespaces() {
201 // Remove the following lines when you implement this test.
202 $this->markTestIncomplete(
203 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
208 * @todo Implement testGetCanonicalName().
211 public function testGetCanonicalName() {
212 // Remove the following lines when you implement this test.
213 $this->markTestIncomplete(
214 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
219 * @todo Implement testGetCanonicalIndex().
222 public function testGetCanonicalIndex() {
223 // Remove the following lines when you implement this test.
224 $this->markTestIncomplete(
225 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
230 * @todo Implement testGetValidNamespaces().
233 public function testGetValidNamespaces() {
234 // Remove the following lines when you implement this test.
235 $this->markTestIncomplete(
236 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
242 public function testCanTalk() {
243 $this->assertFalse( MWNamespace
::canTalk( NS_MEDIA
) );
244 $this->assertFalse( MWNamespace
::canTalk( NS_SPECIAL
) );
246 $this->assertTrue( MWNamespace
::canTalk( NS_MAIN
) );
247 $this->assertTrue( MWNamespace
::canTalk( NS_TALK
) );
248 $this->assertTrue( MWNamespace
::canTalk( NS_USER
) );
249 $this->assertTrue( MWNamespace
::canTalk( NS_USER_TALK
) );
251 // User defined namespaces
252 $this->assertTrue( MWNamespace
::canTalk( 100 ) );
253 $this->assertTrue( MWNamespace
::canTalk( 101 ) );
258 public function testIsContent() {
259 // NS_MAIN is a content namespace per DefaultSettings.php
260 // and per function definition.
261 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
263 global $wgContentNamespaces;
265 $saved = $wgContentNamespaces;
267 $wgContentNamespaces[] = NS_MAIN
;
268 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
270 // Other namespaces which are not expected to be content
271 if ( isset( $wgContentNamespaces[NS_MEDIA
] ) ) {
272 unset( $wgContentNamespaces[NS_MEDIA
] );
274 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
276 if ( isset( $wgContentNamespaces[NS_SPECIAL
] ) ) {
277 unset( $wgContentNamespaces[NS_SPECIAL
] );
279 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
281 if ( isset( $wgContentNamespaces[NS_TALK
] ) ) {
282 unset( $wgContentNamespaces[NS_TALK
] );
284 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
286 if ( isset( $wgContentNamespaces[NS_USER
] ) ) {
287 unset( $wgContentNamespaces[NS_USER
] );
289 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
291 if ( isset( $wgContentNamespaces[NS_CATEGORY
] ) ) {
292 unset( $wgContentNamespaces[NS_CATEGORY
] );
294 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
296 if ( isset( $wgContentNamespaces[100] ) ) {
297 unset( $wgContentNamespaces[100] );
299 $this->assertFalse( MWNamespace
::isContent( 100 ) );
301 $wgContentNamespaces = $saved;
305 * Similar to testIsContent() but alters the $wgContentNamespaces
308 public function testIsContentWithAdditionsInWgContentNamespaces() {
309 // NS_MAIN is a content namespace per DefaultSettings.php
310 // and per function definition.
311 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
313 // Tests that user defined namespace #252 is not content:
314 $this->assertFalse( MWNamespace
::isContent( 252 ) );
316 # @todo FIXME: Is global saving really required for PHPUnit?
317 // Bless namespace # 252 as a content namespace
318 global $wgContentNamespaces;
319 $savedGlobal = $wgContentNamespaces;
320 $wgContentNamespaces[] = 252;
321 $this->assertTrue( MWNamespace
::isContent( 252 ) );
323 // Makes sure NS_MAIN was not impacted
324 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
327 $wgContentNamespaces = $savedGlobal;
329 // Verify namespaces after global restauration
330 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
331 $this->assertFalse( MWNamespace
::isContent( 252 ) );
334 public function testIsWatchable() {
335 // Specials namespaces are not watchable
336 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
337 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
339 // Core defined namespaces are watchables
340 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
341 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
343 // Additional, user defined namespaces are watchables
344 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
345 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
348 public function testHasSubpages() {
349 // Special namespaces:
350 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
351 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
353 // namespaces without subpages
355 global $wgNamespacesWithSubpages;
357 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages ) ) {
358 $saved = $wgNamespacesWithSubpages[NS_MAIN
];
359 unset( $wgNamespacesWithSubpages[NS_MAIN
] );
362 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
364 $wgNamespacesWithSubpages[NS_MAIN
] = true;
365 $this->assertTrue( MWNamespace
::hasSubpages( NS_MAIN
) );
366 $wgNamespacesWithSubpages[NS_MAIN
] = false;
367 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
370 if( $saved !== null ) {
371 $wgNamespacesWithSubpages[NS_MAIN
] = $saved;
374 // Some namespaces with subpages
375 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
376 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
377 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
382 public function testGetContentNamespaces() {
385 MWNamespace
::getcontentNamespaces(),
386 '$wgContentNamespaces is an array with only NS_MAIN by default'
389 global $wgContentNamespaces;
391 # test !is_array( $wgcontentNamespaces )
392 $wgContentNamespaces = '';
393 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
394 $wgContentNamespaces = false;
395 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
396 $wgContentNamespaces = null;
397 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
398 $wgContentNamespaces = 5;
399 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
401 # test $wgContentNamespaces === array()
402 $wgContentNamespaces = array();
403 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
405 # test !in_array( NS_MAIN, $wgContentNamespaces )
406 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
408 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
409 MWNamespace
::getcontentNamespaces(),
410 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
413 # test other cases, return $wgcontentNamespaces as is
414 $wgContentNamespaces = array( NS_MAIN
);
417 MWNamespace
::getcontentNamespaces()
420 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
422 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
423 MWNamespace
::getcontentNamespaces()
429 * Some namespaces are always capitalized per code definition
430 * in MWNamespace::$alwaysCapitalizedNamespaces
432 public function testIsCapitalizedHardcodedAssertions() {
433 // NS_MEDIA and NS_FILE are treated the same
435 MWNamespace
::isCapitalized( NS_MEDIA
),
436 MWNamespace
::isCapitalized( NS_FILE
),
437 'NS_MEDIA and NS_FILE have same capitalization rendering'
440 // Boths are capitalized by default
441 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
442 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
444 // Always capitalized namespaces
445 // @see MWNamespace::$alwaysCapitalizedNamespaces
446 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
447 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
448 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
452 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
453 * global $wgCapitalLink setting to have extended coverage.
455 * MWNamespace::isCapitalized() rely on two global settings:
456 * $wgCapitalLinkOverrides = array(); by default
457 * $wgCapitalLinks = true; by default
458 * This function test $wgCapitalLinks
460 * Global setting correctness is tested against the NS_PROJECT and
461 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
463 public function testIsCapitalizedWithWgCapitalLinks() {
464 global $wgCapitalLinks;
465 // Save the global to easily reset to MediaWiki default settings
466 $savedGlobal = $wgCapitalLinks;
468 $wgCapitalLinks = true;
469 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
470 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
472 $wgCapitalLinks = false;
473 // hardcoded namespaces (see above function) are still capitalized:
474 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
475 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
476 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
477 // setting is correctly applied
478 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
479 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
481 // reset global state:
482 $wgCapitalLinks = $savedGlobal;
486 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
487 * testing the $wgCapitalLinkOverrides global.
489 * @todo split groups of assertions in autonomous testing functions
491 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
492 global $wgCapitalLinkOverrides;
493 // Save the global to easily reset to MediaWiki default settings
494 $savedGlobal = $wgCapitalLinkOverrides;
496 // Test default settings
497 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
498 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
499 // hardcoded namespaces (see above function) are capitalized:
500 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
501 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
502 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
504 // Hardcoded namespaces remains capitalized
505 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
506 $wgCapitalLinkOverrides[NS_USER
] = false;
507 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
508 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
509 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
510 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
512 $wgCapitalLinkOverrides = $savedGlobal;
513 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
514 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
515 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
516 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
517 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
518 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
520 // reset global state:
521 $wgCapitalLinkOverrides = $savedGlobal;
524 public function testHasGenderDistinction() {
525 // Namespaces with gender distinctions
526 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
527 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
529 // Other ones, "genderless"
530 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
531 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
532 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
533 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );