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 and NS_SPECIAL do not have talk pages. MediaWiki raise an exception for them.
90 * @expectedException MWException
92 public function testGetTalkExceptions() {
93 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
94 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
98 * Regular getAssociated() calls
99 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
100 * the function testGetAssociatedExceptions()
102 public function testGetAssociated() {
103 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
104 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
108 ### Exceptions with getAssociated()
109 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
110 ### an exception for them.
112 * @expectedException MWException
114 public function testGetAssociatedExceptionsForNsMedia() {
115 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
118 * @expectedException MWException
120 public function testGetAssociatedExceptionsForNsSpecial() {
121 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
126 public function testGetSubject() {
127 // Special namespaces are their own subjects
128 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
129 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
131 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
132 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
136 * @todo Implement testExists().
139 public function testExists() {
140 // Remove the following lines when you implement this test.
141 $this->markTestIncomplete(
142 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
147 * @todo Implement testGetCanonicalNamespaces().
150 public function testGetCanonicalNamespaces() {
151 // Remove the following lines when you implement this test.
152 $this->markTestIncomplete(
153 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
158 * @todo Implement testGetCanonicalName().
161 public function testGetCanonicalName() {
162 // Remove the following lines when you implement this test.
163 $this->markTestIncomplete(
164 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
169 * @todo Implement testGetCanonicalIndex().
172 public function testGetCanonicalIndex() {
173 // Remove the following lines when you implement this test.
174 $this->markTestIncomplete(
175 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
180 * @todo Implement testGetValidNamespaces().
183 public function testGetValidNamespaces() {
184 // Remove the following lines when you implement this test.
185 $this->markTestIncomplete(
186 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
192 public function testCanTalk() {
193 $this->assertFalse( MWNamespace
::canTalk( NS_MEDIA
) );
194 $this->assertFalse( MWNamespace
::canTalk( NS_SPECIAL
) );
196 $this->assertTrue( MWNamespace
::canTalk( NS_MAIN
) );
197 $this->assertTrue( MWNamespace
::canTalk( NS_TALK
) );
198 $this->assertTrue( MWNamespace
::canTalk( NS_USER
) );
199 $this->assertTrue( MWNamespace
::canTalk( NS_USER_TALK
) );
201 // User defined namespaces
202 $this->assertTrue( MWNamespace
::canTalk( 100 ) );
203 $this->assertTrue( MWNamespace
::canTalk( 101 ) );
208 public function testIsContent() {
209 // NS_MAIN is a content namespace per DefaultSettings.php
210 // and per function definition.
211 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
213 // Other namespaces which are not expected to be content
214 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
215 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
216 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
217 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
218 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
219 // User defined namespace:
220 $this->assertFalse( MWNamespace
::isContent( 100 ) );
224 * Similar to testIsContent() but alters the $wgContentNamespaces
227 public function testIsContentWithAdditionsInWgContentNamespaces() {
228 // NS_MAIN is a content namespace per DefaultSettings.php
229 // and per function definition.
230 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
232 // Tests that user defined namespace #252 is not content:
233 $this->assertFalse( MWNamespace
::isContent( 252 ) );
235 # @todo FIXME: Is global saving really required for PHPUnit?
236 // Bless namespace # 252 as a content namespace
237 global $wgContentNamespaces;
238 $savedGlobal = $wgContentNamespaces;
239 $wgContentNamespaces[] = 252;
240 $this->assertTrue( MWNamespace
::isContent( 252 ) );
242 // Makes sure NS_MAIN was not impacted
243 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
246 $wgContentNamespaces = $savedGlobal;
248 // Verify namespaces after global restauration
249 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
250 $this->assertFalse( MWNamespace
::isContent( 252 ) );
253 public function testIsWatchable() {
254 // Specials namespaces are not watchable
255 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
256 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
258 // Core defined namespaces are watchables
259 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
260 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
262 // Additional, user defined namespaces are watchables
263 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
264 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
267 public function testHasSubpages() {
268 // Special namespaces:
269 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
270 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
272 // namespaces without subpages
274 global $wgNamespacesWithSubpages;
276 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages ) ) {
277 $saved = $wgNamespacesWithSubpages[NS_MAIN
];
278 unset( $wgNamespacesWithSubpages[NS_MAIN
] );
281 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
283 $wgNamespacesWithSubpages[NS_MAIN
] = true;
284 $this->assertTrue( MWNamespace
::hasSubpages( NS_MAIN
) );
285 $wgNamespacesWithSubpages[NS_MAIN
] = false;
286 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
289 if( $saved !== null ) {
290 $wgNamespacesWithSubpages[NS_MAIN
] = $saved;
293 // Some namespaces with subpages
294 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
295 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
296 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
301 public function testGetContentNamespaces() {
304 MWNamespace
::getcontentNamespaces(),
305 '$wgContentNamespaces is an array with only NS_MAIN by default'
308 global $wgContentNamespaces;
310 # test !is_array( $wgcontentNamespaces )
311 $wgContentNamespaces = '';
312 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
313 $wgContentNamespaces = false;
314 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
315 $wgContentNamespaces = null;
316 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
317 $wgContentNamespaces = 5;
318 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
320 # test $wgContentNamespaces === array()
321 $wgContentNamespaces = array();
322 $this->assertEquals( NS_MAIN
, MWNamespace
::getcontentNamespaces() );
324 # test !in_array( NS_MAIN, $wgContentNamespaces )
325 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
327 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
328 MWNamespace
::getcontentNamespaces(),
329 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
332 # test other cases, return $wgcontentNamespaces as is
333 $wgContentNamespaces = array( NS_MAIN
);
336 MWNamespace
::getcontentNamespaces()
339 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
341 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
342 MWNamespace
::getcontentNamespaces()
348 * Some namespaces are always capitalized per code definition
349 * in MWNamespace::$alwaysCapitalizedNamespaces
351 public function testIsCapitalizedHardcodedAssertions() {
352 // NS_MEDIA and NS_FILE are treated the same
354 MWNamespace
::isCapitalized( NS_MEDIA
),
355 MWNamespace
::isCapitalized( NS_FILE
),
356 'NS_MEDIA and NS_FILE have same capitalization rendering'
359 // Boths are capitalized by default
360 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
361 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
363 // Always capitalized namespaces
364 // @see MWNamespace::$alwaysCapitalizedNamespaces
365 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
366 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
367 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
371 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
372 * global $wgCapitalLink setting to have extended coverage.
374 * MWNamespace::isCapitalized() rely on two global settings:
375 * $wgCapitalLinkOverrides = array(); by default
376 * $wgCapitalLinks = true; by default
377 * This function test $wgCapitalLinks
379 * Global setting correctness is tested against the NS_PROJECT and
380 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
382 public function testIsCapitalizedWithWgCapitalLinks() {
383 global $wgCapitalLinks;
384 // Save the global to easily reset to MediaWiki default settings
385 $savedGlobal = $wgCapitalLinks;
387 $wgCapitalLinks = true;
388 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
389 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
391 $wgCapitalLinks = false;
392 // hardcoded namespaces (see above function) are still capitalized:
393 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
394 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
395 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
396 // setting is correctly applied
397 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
398 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
400 // reset global state:
401 $wgCapitalLinks = $savedGlobal;
405 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
406 * testing the $wgCapitalLinkOverrides global.
408 * @todo split groups of assertions in autonomous testing functions
410 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
411 global $wgCapitalLinkOverrides;
412 // Save the global to easily reset to MediaWiki default settings
413 $savedGlobal = $wgCapitalLinkOverrides;
415 // Test default settings
416 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
417 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
418 // hardcoded namespaces (see above function) are capitalized:
419 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
420 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
421 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
423 // Hardcoded namespaces remains capitalized
424 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
425 $wgCapitalLinkOverrides[NS_USER
] = false;
426 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
427 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
428 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
429 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
431 $wgCapitalLinkOverrides = $savedGlobal;
432 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
433 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
434 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
435 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
436 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
437 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
439 // reset global state:
440 $wgCapitalLinkOverrides = $savedGlobal;
443 public function testHasGenderDistinction() {
444 // Namespaces with gender distinctions
445 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
446 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
448 // Other ones, "genderless"
449 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
450 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
451 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
452 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );