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
{
20 * Sets up the fixture, for example, opens a network connection.
21 * This method is called before a test is executed.
23 protected function setUp() {
24 $this->object = new MWNamespace
;
28 * Tears down the fixture, for example, closes a network connection.
29 * This method is called after a test is executed.
31 protected function tearDown() {
35 #### START OF TESTS #########################################################
38 * @todo Write more texts, handle $wgAllowImageMoving setting
40 public function testIsMovable() {
41 $this->assertFalse( MWNamespace
::isMovable( NS_CATEGORY
) );
42 # @todo FIXME: Write more tests!!
46 * Please make sure to change testIsTalk() if you change the assertions below
48 public function testIsMain() {
50 $this->assertTrue( MWNamespace
::isMain( NS_MEDIA
) );
51 $this->assertTrue( MWNamespace
::isMain( NS_SPECIAL
) );
54 $this->assertTrue( MWNamespace
::isMain( NS_MAIN
) );
55 $this->assertTrue( MWNamespace
::isMain( NS_USER
) );
56 $this->assertTrue( MWNamespace
::isMain( 100 ) ); # user defined
59 $this->assertFalse( MWNamespace
::isMain( NS_TALK
) );
60 $this->assertFalse( MWNamespace
::isMain( NS_USER_TALK
) );
61 $this->assertFalse( MWNamespace
::isMain( 101 ) ); # user defined
65 * Reverse of testIsMain().
66 * Please update testIsMain() if you change assertions below
68 public function testIsTalk() {
70 $this->assertFalse( MWNamespace
::isTalk( NS_MEDIA
) );
71 $this->assertFalse( MWNamespace
::isTalk( NS_SPECIAL
) );
74 $this->assertFalse( MWNamespace
::isTalk( NS_MAIN
) );
75 $this->assertFalse( MWNamespace
::isTalk( NS_USER
) );
76 $this->assertFalse( MWNamespace
::isTalk( 100 ) ); # user defined
79 $this->assertTrue( MWNamespace
::isTalk( NS_TALK
) );
80 $this->assertTrue( MWNamespace
::isTalk( NS_USER_TALK
) );
81 $this->assertTrue( MWNamespace
::isTalk( 101 ) ); # user defined
85 * Regular getTalk() calls
86 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
87 * the function testGetTalkExceptions()
89 public function testGetTalk() {
90 $this->assertEquals( MWNamespace
::getTalk( NS_MAIN
), NS_TALK
);
94 * Exceptions with getTalk()
95 * NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raise an exception for them.
96 * @expectedException MWException
98 public function testGetTalkExceptions() {
99 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
100 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
104 * Regular getAssociated() calls
105 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
106 * the function testGetAssociatedExceptions()
108 public function testGetAssociated() {
109 $this->assertEquals( MWNamespace
::getAssociated( NS_MAIN
), NS_TALK
);
110 $this->assertEquals( MWNamespace
::getAssociated( NS_TALK
), NS_MAIN
);
114 ### Exceptions with getAssociated()
115 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
116 ### an exception for them.
118 * @expectedException MWException
120 public function testGetAssociatedExceptionsForNsMedia() {
121 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
124 * @expectedException MWException
126 public function testGetAssociatedExceptionsForNsSpecial() {
127 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
132 public function testGetSubject() {
133 // Special namespaces are their own subjects
134 $this->assertEquals( MWNamespace
::getSubject( NS_MEDIA
), NS_MEDIA
);
135 $this->assertEquals( MWNamespace
::getSubject( NS_SPECIAL
), NS_SPECIAL
);
137 $this->assertEquals( MWNamespace
::getSubject( NS_TALK
), NS_MAIN
);
138 $this->assertEquals( MWNamespace
::getSubject( NS_USER_TALK
), NS_USER
);
142 * @todo Implement testExists().
145 public function testExists() {
146 // Remove the following lines when you implement this test.
147 $this->markTestIncomplete(
148 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
153 * @todo Implement testGetCanonicalNamespaces().
156 public function testGetCanonicalNamespaces() {
157 // Remove the following lines when you implement this test.
158 $this->markTestIncomplete(
159 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
164 * @todo Implement testGetCanonicalName().
167 public function testGetCanonicalName() {
168 // Remove the following lines when you implement this test.
169 $this->markTestIncomplete(
170 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
175 * @todo Implement testGetCanonicalIndex().
178 public function testGetCanonicalIndex() {
179 // Remove the following lines when you implement this test.
180 $this->markTestIncomplete(
181 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
186 * @todo Implement testGetValidNamespaces().
189 public function testGetValidNamespaces() {
190 // Remove the following lines when you implement this test.
191 $this->markTestIncomplete(
192 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
198 public function testCanTalk() {
199 $this->assertFalse( MWNamespace
::canTalk( NS_MEDIA
) );
200 $this->assertFalse( MWNamespace
::canTalk( NS_SPECIAL
) );
202 $this->assertTrue( MWNamespace
::canTalk( NS_MAIN
) );
203 $this->assertTrue( MWNamespace
::canTalk( NS_TALK
) );
204 $this->assertTrue( MWNamespace
::canTalk( NS_USER
) );
205 $this->assertTrue( MWNamespace
::canTalk( NS_USER_TALK
) );
207 // User defined namespaces
208 $this->assertTrue( MWNamespace
::canTalk( 100 ) );
209 $this->assertTrue( MWNamespace
::canTalk( 101 ) );
214 public function testIsContent() {
215 // NS_MAIN is a content namespace per DefaultSettings.php
216 // and per function definition.
217 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
219 // Other namespaces which are not expected to be content
220 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
221 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
222 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
223 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
224 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
225 // User defined namespace:
226 $this->assertFalse( MWNamespace
::isContent( 100 ) );
230 * Similar to testIsContent() but alters the $wgContentNamespaces
233 public function testIsContentWithAdditionsInWgContentNamespaces() {
234 // NS_MAIN is a content namespace per DefaultSettings.php
235 // and per function definition.
236 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
238 // Tests that user defined namespace #252 is not content:
239 $this->assertFalse( MWNamespace
::isContent( 252 ) );
241 # @todo FIXME: Is global saving really required for PHPUnit?
242 // Bless namespace # 252 as a content namespace
243 global $wgContentNamespaces;
244 $savedGlobal = $wgContentNamespaces;
245 $wgContentNamespaces[] = 252;
246 $this->assertTrue( MWNamespace
::isContent( 252 ) );
248 // Makes sure NS_MAIN was not impacted
249 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
252 $wgContentNamespaces = $savedGlobal;
254 // Verify namespaces after global restauration
255 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
256 $this->assertFalse( MWNamespace
::isContent( 252 ) );
259 public function testIsWatchable() {
260 // Specials namespaces are not watchable
261 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
262 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
264 // Core defined namespaces are watchables
265 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
266 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
268 // Additional, user defined namespaces are watchables
269 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
270 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
273 public function testHasSubpages() {
274 // Special namespaces:
275 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
276 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
278 // namespaces without subpages
279 global $wgNamespacesWithSubpages;
280 if( array_key_exists( NS_MAIN
, $wgNamespacesWithSubpages )
281 && $wgNamespacesWithSubpages[NS_MAIN
] === true
283 $this->markTestSkipped( "Main namespace has subpages enabled" );
285 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
288 // Some namespaces with subpages
289 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
290 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
291 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
296 public function testGetContentNamespaces() {
298 MWNamespace
::getcontentNamespaces(),
300 '$wgContentNamespaces is an array with only NS_MAIN by default'
303 global $wgContentNamespaces;
305 # test !is_array( $wgcontentNamespaces )
306 $wgContentNamespaces = '';
307 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
308 $wgContentNamespaces = false;
309 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
310 $wgContentNamespaces = null;
311 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
312 $wgContentNamespaces = 5;
313 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
315 # test $wgContentNamespaces === array()
316 $wgContentNamespaces = array();
317 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
319 # test !in_array( NS_MAIN, $wgContentNamespaces )
320 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
322 MWNamespace
::getcontentNamespaces(),
323 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
324 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
327 # test other cases, return $wgcontentNamespaces as is
328 $wgContentNamespaces = array( NS_MAIN
);
330 MWNamespace
::getcontentNamespaces(),
334 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
336 MWNamespace
::getcontentNamespaces(),
337 array( NS_MAIN
, NS_USER
, NS_CATEGORY
)
343 * Some namespaces are always capitalized per code definition
344 * in MWNamespace::$alwaysCapitalizedNamespaces
346 public function testIsCapitalizedHardcodedAssertions() {
347 // NS_MEDIA and NS_FILE are treated the same
349 MWNamespace
::isCapitalized( NS_MEDIA
),
350 MWNamespace
::isCapitalized( NS_FILE
),
351 'NS_MEDIA and NS_FILE have same capitalization rendering'
354 // Boths are capitalized by default
355 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
356 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
358 // Always capitalized namespaces
359 // @see MWNamespace::$alwaysCapitalizedNamespaces
360 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
361 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
362 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
366 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
367 * global $wgCapitalLink setting to have extended coverage.
369 * MWNamespace::isCapitalized() rely on two global settings:
370 * $wgCapitalLinkOverrides = array(); by default
371 * $wgCapitalLinks = true; by default
372 * This function test $wgCapitalLinks
374 * Global setting correctness is tested against the NS_PROJECT and
375 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
377 public function testIsCapitalizedWithWgCapitalLinks() {
378 global $wgCapitalLinks;
379 // Save the global to easily reset to MediaWiki default settings
380 $savedGlobal = $wgCapitalLinks;
382 $wgCapitalLinks = true;
383 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
384 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
386 $wgCapitalLinks = false;
387 // hardcoded namespaces (see above function) are still capitalized:
388 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
389 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
390 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
391 // setting is correctly applied
392 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
393 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
395 // reset global state:
396 $wgCapitalLinks = $savedGlobal;
400 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
401 * testing the $wgCapitalLinkOverrides global.
403 * @todo split groups of assertions in autonomous testing functions
405 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
406 global $wgCapitalLinkOverrides;
407 // Save the global to easily reset to MediaWiki default settings
408 $savedGlobal = $wgCapitalLinkOverrides;
410 // Test default settings
411 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
412 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
413 // hardcoded namespaces (see above function) are capitalized:
414 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
415 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
416 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
418 // Hardcoded namespaces remains capitalized
419 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
420 $wgCapitalLinkOverrides[NS_USER
] = false;
421 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
422 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
423 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
424 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
426 $wgCapitalLinkOverrides = $savedGlobal;
427 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
428 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
429 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
430 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
431 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
432 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
434 // reset global state:
435 $wgCapitalLinkOverrides = $savedGlobal;
438 public function testHasGenderDistinction() {
439 // Namespaces with gender distinctions
440 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
441 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
443 // Other ones, "genderless"
444 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
445 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
446 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
447 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );