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 // Other namespaces which are not expected to be content
223 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
224 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
225 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
226 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
227 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
228 // User defined namespace:
229 $this->assertFalse( MWNamespace
::isContent( 100 ) );
233 * Similar to testIsContent() but alters the $wgContentNamespaces
236 public function testIsContentWithAdditionsInWgContentNamespaces() {
237 // NS_MAIN is a content namespace per DefaultSettings.php
238 // and per function definition.
239 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
241 // Tests that user defined namespace #252 is not content:
242 $this->assertFalse( MWNamespace
::isContent( 252 ) );
244 # @todo FIXME: Is global saving really required for PHPUnit?
245 // Bless namespace # 252 as a content namespace
246 global $wgContentNamespaces;
247 $savedGlobal = $wgContentNamespaces;
248 $wgContentNamespaces[] = 252;
249 $this->assertTrue( MWNamespace
::isContent( 252 ) );
251 // Makes sure NS_MAIN was not impacted
252 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
255 $wgContentNamespaces = $savedGlobal;
257 // Verify namespaces after global restauration
258 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
259 $this->assertFalse( MWNamespace
::isContent( 252 ) );
262 public function testIsWatchable() {
263 // Specials namespaces are not watchable
264 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
265 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
267 // Core defined namespaces are watchables
268 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
269 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
271 // Additional, user defined namespaces are watchables
272 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
273 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
276 public function testHasSubpages() {
277 // Special namespaces:
278 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
279 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
281 // namespaces without subpages
283 global $wgNamespacesWithSubpages;
285 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages ) ) {
286 $saved = $wgNamespacesWithSubpages[NS_MAIN
];
287 unset( $wgNamespacesWithSubpages[NS_MAIN
] );
290 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
292 $wgNamespacesWithSubpages[NS_MAIN
] = true;
293 $this->assertTrue( MWNamespace
::hasSubpages( NS_MAIN
) );
294 $wgNamespacesWithSubpages[NS_MAIN
] = false;
295 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
298 if( $saved !== null ) {
299 $wgNamespacesWithSubpages[NS_MAIN
] = $saved;
302 // Some namespaces with subpages
303 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
304 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
305 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
310 public function testGetContentNamespaces() {
313 MWNamespace
::getcontentNamespaces(),
314 '$wgContentNamespaces is an array with only NS_MAIN by default'
317 global $wgContentNamespaces;
319 # test !is_array( $wgcontentNamespaces )
320 $wgContentNamespaces = '';
321 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
322 $wgContentNamespaces = false;
323 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
324 $wgContentNamespaces = null;
325 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
326 $wgContentNamespaces = 5;
327 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
329 # test $wgContentNamespaces === array()
330 $wgContentNamespaces = array();
331 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
333 # test !in_array( NS_MAIN, $wgContentNamespaces )
334 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
336 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
337 MWNamespace
::getcontentNamespaces(),
338 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
341 # test other cases, return $wgcontentNamespaces as is
342 $wgContentNamespaces = array( NS_MAIN
);
345 MWNamespace
::getcontentNamespaces()
348 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
350 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
351 MWNamespace
::getcontentNamespaces()
357 * Some namespaces are always capitalized per code definition
358 * in MWNamespace::$alwaysCapitalizedNamespaces
360 public function testIsCapitalizedHardcodedAssertions() {
361 // NS_MEDIA and NS_FILE are treated the same
363 MWNamespace
::isCapitalized( NS_MEDIA
),
364 MWNamespace
::isCapitalized( NS_FILE
),
365 'NS_MEDIA and NS_FILE have same capitalization rendering'
368 // Boths are capitalized by default
369 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
370 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
372 // Always capitalized namespaces
373 // @see MWNamespace::$alwaysCapitalizedNamespaces
374 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
375 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
376 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
380 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
381 * global $wgCapitalLink setting to have extended coverage.
383 * MWNamespace::isCapitalized() rely on two global settings:
384 * $wgCapitalLinkOverrides = array(); by default
385 * $wgCapitalLinks = true; by default
386 * This function test $wgCapitalLinks
388 * Global setting correctness is tested against the NS_PROJECT and
389 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
391 public function testIsCapitalizedWithWgCapitalLinks() {
392 global $wgCapitalLinks;
393 // Save the global to easily reset to MediaWiki default settings
394 $savedGlobal = $wgCapitalLinks;
396 $wgCapitalLinks = true;
397 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
398 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
400 $wgCapitalLinks = false;
401 // hardcoded namespaces (see above function) are still capitalized:
402 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
403 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
404 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
405 // setting is correctly applied
406 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
407 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
409 // reset global state:
410 $wgCapitalLinks = $savedGlobal;
414 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
415 * testing the $wgCapitalLinkOverrides global.
417 * @todo split groups of assertions in autonomous testing functions
419 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
420 global $wgCapitalLinkOverrides;
421 // Save the global to easily reset to MediaWiki default settings
422 $savedGlobal = $wgCapitalLinkOverrides;
424 // Test default settings
425 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
426 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
427 // hardcoded namespaces (see above function) are capitalized:
428 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
429 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
430 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
432 // Hardcoded namespaces remains capitalized
433 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
434 $wgCapitalLinkOverrides[NS_USER
] = false;
435 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
436 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
437 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
438 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
440 $wgCapitalLinkOverrides = $savedGlobal;
441 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
442 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
443 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
444 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
445 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
446 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
448 // reset global state:
449 $wgCapitalLinkOverrides = $savedGlobal;
452 public function testHasGenderDistinction() {
453 // Namespaces with gender distinctions
454 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
455 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
457 // Other ones, "genderless"
458 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
459 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
460 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
461 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );