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->assertIsSubject( NS_MEDIA
);
45 $this->assertIsSubject( NS_SPECIAL
);
48 $this->assertIsSubject( NS_MAIN
);
49 $this->assertIsSubject( NS_USER
);
50 $this->assertIsSubject( 100 ); # user defined
53 $this->assertIsNotSubject( NS_TALK
);
54 $this->assertIsNotSubject( NS_USER_TALK
);
55 $this->assertIsNotSubject( 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->assertIsNotTalk( NS_MEDIA
);
69 $this->assertIsNotTalk( NS_SPECIAL
);
72 $this->assertIsNotTalk( NS_MAIN
);
73 $this->assertIsNotTalk( NS_USER
);
74 $this->assertIsNotTalk( 100 ); # user defined
77 $this->assertIsTalk( NS_TALK
);
78 $this->assertIsTalk( NS_USER_TALK
);
79 $this->assertIsTalk( 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->assertCanNotTalk( NS_MEDIA
);
244 $this->assertCanNotTalk( NS_SPECIAL
);
246 $this->assertCanTalk( NS_MAIN
);
247 $this->assertCanTalk( NS_TALK
);
248 $this->assertCanTalk( NS_USER
);
249 $this->assertCanTalk( NS_USER_TALK
);
251 // User defined namespaces
252 $this->assertCanTalk( 100 );
253 $this->assertCanTalk( 101 );
258 public function testIsContent() {
259 // NS_MAIN is a content namespace per DefaultSettings.php
260 // and per function definition.
261 $this->assertIsContent( NS_MAIN
);
263 global $wgContentNamespaces;
265 $saved = $wgContentNamespaces;
267 $wgContentNamespaces[] = NS_MAIN
;
268 $this->assertIsContent( 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->assertIsNotContent( NS_MEDIA
);
276 if ( isset( $wgContentNamespaces[NS_SPECIAL
] ) ) {
277 unset( $wgContentNamespaces[NS_SPECIAL
] );
279 $this->assertIsNotContent( NS_SPECIAL
);
281 if ( isset( $wgContentNamespaces[NS_TALK
] ) ) {
282 unset( $wgContentNamespaces[NS_TALK
] );
284 $this->assertIsNotContent( NS_TALK
);
286 if ( isset( $wgContentNamespaces[NS_USER
] ) ) {
287 unset( $wgContentNamespaces[NS_USER
] );
289 $this->assertIsNotContent( NS_USER
);
291 if ( isset( $wgContentNamespaces[NS_CATEGORY
] ) ) {
292 unset( $wgContentNamespaces[NS_CATEGORY
] );
294 $this->assertIsNotContent( NS_CATEGORY
);
296 if ( isset( $wgContentNamespaces[100] ) ) {
297 unset( $wgContentNamespaces[100] );
299 $this->assertIsNotContent( 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->assertIsContent( NS_MAIN
);
313 // Tests that user defined namespace #252 is not content:
314 $this->assertIsNotContent( 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->assertIsContent( 252 );
323 // Makes sure NS_MAIN was not impacted
324 $this->assertIsContent( NS_MAIN
);
327 $wgContentNamespaces = $savedGlobal;
329 // Verify namespaces after global restauration
330 $this->assertIsContent( NS_MAIN
);
331 $this->assertIsNotContent( 252 );
334 public function testIsWatchable() {
335 // Specials namespaces are not watchable
336 $this->assertIsNotWatchable( NS_MEDIA
);
337 $this->assertIsNotWatchable( NS_SPECIAL
);
339 // Core defined namespaces are watchables
340 $this->assertIsWatchable( NS_MAIN
);
341 $this->assertIsWatchable( NS_TALK
);
343 // Additional, user defined namespaces are watchables
344 $this->assertIsWatchable( 100 );
345 $this->assertIsWatchable( 101 );
348 public function testHasSubpages() {
349 // Special namespaces:
350 $this->assertHasNotSubpages( NS_MEDIA
);
351 $this->assertHasNotSubpages( 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->assertHasNotSubpages( NS_MAIN
);
364 $wgNamespacesWithSubpages[NS_MAIN
] = true;
365 $this->assertHasSubpages( NS_MAIN
);
366 $wgNamespacesWithSubpages[NS_MAIN
] = false;
367 $this->assertHasNotSubpages( NS_MAIN
);
370 if( $saved !== null ) {
371 $wgNamespacesWithSubpages[NS_MAIN
] = $saved;
374 // Some namespaces with subpages
375 $this->assertHasSubpages( NS_TALK
);
376 $this->assertHasSubpages( NS_USER
);
377 $this->assertHasSubpages( 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 $saved = $wgContentNamespaces;
392 # test !is_array( $wgcontentNamespaces )
393 $wgContentNamespaces = '';
394 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
395 $wgContentNamespaces = false;
396 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
397 $wgContentNamespaces = null;
398 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
399 $wgContentNamespaces = 5;
400 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
402 # test $wgContentNamespaces === array()
403 $wgContentNamespaces = array();
404 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
406 # test !in_array( NS_MAIN, $wgContentNamespaces )
407 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
409 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
410 MWNamespace
::getcontentNamespaces(),
411 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
414 # test other cases, return $wgcontentNamespaces as is
415 $wgContentNamespaces = array( NS_MAIN
);
418 MWNamespace
::getcontentNamespaces()
421 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
423 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
424 MWNamespace
::getcontentNamespaces()
427 $wgContentNamespaces = $saved;
431 * Some namespaces are always capitalized per code definition
432 * in MWNamespace::$alwaysCapitalizedNamespaces
434 public function testIsCapitalizedHardcodedAssertions() {
435 // NS_MEDIA and NS_FILE are treated the same
437 MWNamespace
::isCapitalized( NS_MEDIA
),
438 MWNamespace
::isCapitalized( NS_FILE
),
439 'NS_MEDIA and NS_FILE have same capitalization rendering'
442 // Boths are capitalized by default
443 $this->assertIsCapitalized( NS_MEDIA
);
444 $this->assertIsCapitalized( NS_FILE
);
446 // Always capitalized namespaces
447 // @see MWNamespace::$alwaysCapitalizedNamespaces
448 $this->assertIsCapitalized( NS_SPECIAL
);
449 $this->assertIsCapitalized( NS_USER
);
450 $this->assertIsCapitalized( NS_MEDIAWIKI
);
454 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
455 * global $wgCapitalLink setting to have extended coverage.
457 * MWNamespace::isCapitalized() rely on two global settings:
458 * $wgCapitalLinkOverrides = array(); by default
459 * $wgCapitalLinks = true; by default
460 * This function test $wgCapitalLinks
462 * Global setting correctness is tested against the NS_PROJECT and
463 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
465 public function testIsCapitalizedWithWgCapitalLinks() {
466 global $wgCapitalLinks;
467 // Save the global to easily reset to MediaWiki default settings
468 $savedGlobal = $wgCapitalLinks;
470 $wgCapitalLinks = true;
471 $this->assertIsCapitalized( NS_PROJECT
);
472 $this->assertIsCapitalized( NS_PROJECT_TALK
);
474 $wgCapitalLinks = false;
475 // hardcoded namespaces (see above function) are still capitalized:
476 $this->assertIsCapitalized( NS_SPECIAL
);
477 $this->assertIsCapitalized( NS_USER
);
478 $this->assertIsCapitalized( NS_MEDIAWIKI
);
479 // setting is correctly applied
480 $this->assertIsNotCapitalized( NS_PROJECT
);
481 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
483 // reset global state:
484 $wgCapitalLinks = $savedGlobal;
488 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
489 * testing the $wgCapitalLinkOverrides global.
491 * @todo split groups of assertions in autonomous testing functions
493 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
494 global $wgCapitalLinkOverrides;
495 // Save the global to easily reset to MediaWiki default settings
496 $savedGlobal = $wgCapitalLinkOverrides;
498 // Test default settings
499 $this->assertIsCapitalized( NS_PROJECT
);
500 $this->assertIsCapitalized( NS_PROJECT_TALK
);
501 // hardcoded namespaces (see above function) are capitalized:
502 $this->assertIsCapitalized( NS_SPECIAL
);
503 $this->assertIsCapitalized( NS_USER
);
504 $this->assertIsCapitalized( NS_MEDIAWIKI
);
506 // Hardcoded namespaces remains capitalized
507 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
508 $wgCapitalLinkOverrides[NS_USER
] = false;
509 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
510 $this->assertIsCapitalized( NS_SPECIAL
);
511 $this->assertIsCapitalized( NS_USER
);
512 $this->assertIsCapitalized( NS_MEDIAWIKI
);
514 $wgCapitalLinkOverrides = $savedGlobal;
515 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
516 $this->assertIsNotCapitalized( NS_PROJECT
);
517 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
518 $this->assertIsCapitalized( NS_PROJECT
);
519 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
520 $this->assertIsCapitalized( NS_PROJECT
);
522 // reset global state:
523 $wgCapitalLinkOverrides = $savedGlobal;
526 public function testHasGenderDistinction() {
527 // Namespaces with gender distinctions
528 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
529 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
531 // Other ones, "genderless"
532 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
533 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
534 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
535 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
539 ####### HELPERS ###########################################################
540 function __call( $method, $args ) {
541 // Call the real method if it exists
542 if( method_exists($this, $method ) ) {
543 return $this->$method( $args );
546 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
547 # Interprets arguments:
549 $msg = isset($args[1]) ?
$args[1] : " dummy message";
551 # Forge the namespace constant name:
553 $ns_name = "NS_MAIN";
555 $ns_name = "NS_" . strtoupper( MWNamespace
::getCanonicalName( $ns ) );
557 # ... and the MWNamespace method name
558 $nsMethod = strtolower( $m[1] ) . $m[3];
560 $expect = ($m[2] === '');
561 $expect_name = $expect ?
'TRUE' : 'FALSE';
563 return $this->assertEquals( $expect,
564 MWNamespace
::$nsMethod( $ns, $msg ),
565 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
569 throw new Exception( __METHOD__
. " could not find a method named $method\n" );