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().
144 public function testExists() {
145 // Remove the following lines when you implement this test.
146 $this->markTestIncomplete(
147 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
152 * @todo Implement testGetCanonicalNamespaces().
154 public function testGetCanonicalNamespaces() {
155 // Remove the following lines when you implement this test.
156 $this->markTestIncomplete(
157 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
162 * @todo Implement testGetCanonicalName().
164 public function testGetCanonicalName() {
165 // Remove the following lines when you implement this test.
166 $this->markTestIncomplete(
167 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
172 * @todo Implement testGetCanonicalIndex().
174 public function testGetCanonicalIndex() {
175 // Remove the following lines when you implement this test.
176 $this->markTestIncomplete(
177 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
182 * @todo Implement testGetValidNamespaces().
184 public function testGetValidNamespaces() {
185 // Remove the following lines when you implement this test.
186 $this->markTestIncomplete(
187 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
193 public function testCanTalk() {
194 $this->assertFalse( MWNamespace
::canTalk( NS_MEDIA
) );
195 $this->assertFalse( MWNamespace
::canTalk( NS_SPECIAL
) );
197 $this->assertTrue( MWNamespace
::canTalk( NS_MAIN
) );
198 $this->assertTrue( MWNamespace
::canTalk( NS_TALK
) );
199 $this->assertTrue( MWNamespace
::canTalk( NS_USER
) );
200 $this->assertTrue( MWNamespace
::canTalk( NS_USER_TALK
) );
202 // User defined namespaces
203 $this->assertTrue( MWNamespace
::canTalk( 100 ) );
204 $this->assertTrue( MWNamespace
::canTalk( 101 ) );
209 public function testIsContent() {
210 // NS_MAIN is a content namespace per DefaultSettings.php
211 // and per function definition.
212 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
214 // Other namespaces which are not expected to be content
215 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
216 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
217 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
218 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
219 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
220 // User defined namespace:
221 $this->assertFalse( MWNamespace
::isContent( 100 ) );
225 * Similar to testIsContent() but alters the $wgContentNamespaces
228 public function testIsContentWithAdditionsInWgContentNamespaces() {
229 // NS_MAIN is a content namespace per DefaultSettings.php
230 // and per function definition.
231 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
233 // Tests that user defined namespace #252 is not content:
234 $this->assertFalse( MWNamespace
::isContent( 252 ) );
236 # @todo FIXME: Is global saving really required for PHPUnit?
237 // Bless namespace # 252 as a content namespace
238 global $wgContentNamespaces;
239 $savedGlobal = $wgContentNamespaces;
240 $wgContentNamespaces[] = 252;
241 $this->assertTrue( MWNamespace
::isContent( 252 ) );
243 // Makes sure NS_MAIN was not impacted
244 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
247 $wgContentNamespaces = $savedGlobal;
249 // Verify namespaces after global restauration
250 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
251 $this->assertFalse( MWNamespace
::isContent( 252 ) );
254 public function testIsWatchable() {
255 // Specials namespaces are not watchable
256 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
257 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
259 // Core defined namespaces are watchables
260 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
261 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
263 // Additional, user defined namespaces are watchables
264 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
265 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
268 public function testHasSubpages() {
269 // Special namespaces:
270 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
271 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
273 // namespaces without subpages
274 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
276 // Some namespaces with subpages
277 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
278 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
279 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
284 public function testGetContentNamespaces() {
286 MWNamespace
::getcontentNamespaces(),
288 '$wgContentNamespaces is an array with only NS_MAIN by default'
291 global $wgContentNamespaces;
293 # test !is_array( $wgcontentNamespaces )
294 $wgContentNamespaces = '';
295 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
296 $wgContentNamespaces = false;
297 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
298 $wgContentNamespaces = null;
299 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
300 $wgContentNamespaces = 5;
301 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
303 # test $wgContentNamespaces === array()
304 $wgContentNamespaces = array();
305 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
307 # test !in_array( NS_MAIN, $wgContentNamespaces )
308 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
310 MWNamespace
::getcontentNamespaces(),
311 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
312 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
315 # test other cases, return $wgcontentNamespaces as is
316 $wgContentNamespaces = array( NS_MAIN
);
318 MWNamespace
::getcontentNamespaces(),
322 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
324 MWNamespace
::getcontentNamespaces(),
325 array( NS_MAIN
, NS_USER
, NS_CATEGORY
)
331 * Some namespaces are always capitalized per code definition
332 * in MWNamespace::$alwaysCapitalizedNamespaces
334 public function testIsCapitalizedHardcodedAssertions() {
335 // NS_MEDIA and NS_FILE are treated the same
337 MWNamespace
::isCapitalized( NS_MEDIA
),
338 MWNamespace
::isCapitalized( NS_FILE
),
339 'NS_MEDIA and NS_FILE have same capitalization rendering'
342 // Boths are capitalized by default
343 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
344 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
346 // Always capitalized namespaces
347 // @see MWNamespace::$alwaysCapitalizedNamespaces
348 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
349 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
350 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
354 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
355 * global $wgCapitalLink setting to have extended coverage.
357 * MWNamespace::isCapitalized() rely on two global settings:
358 * $wgCapitalLinkOverrides = array(); by default
359 * $wgCapitalLinks = true; by default
360 * This function test $wgCapitalLinks
362 * Global setting correctness is tested against the NS_PROJECT and
363 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
365 public function testIsCapitalizedWithWgCapitalLinks() {
366 global $wgCapitalLinks;
367 // Save the global to easily reset to MediaWiki default settings
368 $savedGlobal = $wgCapitalLinks;
370 $wgCapitalLinks = true;
371 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
372 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
374 $wgCapitalLinks = false;
375 // hardcoded namespaces (see above function) are still capitalized:
376 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
377 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
378 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
379 // setting is correctly applied
380 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
381 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
383 // reset global state:
384 $wgCapitalLinks = $savedGlobal;
388 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
389 * testing the $wgCapitalLinkOverrides global.
391 * @todo split groups of assertions in autonomous testing functions
393 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
394 global $wgCapitalLinkOverrides;
395 // Save the global to easily reset to MediaWiki default settings
396 $savedGlobal = $wgCapitalLinkOverrides;
398 // Test default settings
399 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
400 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
401 // hardcoded namespaces (see above function) are capitalized:
402 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
403 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
404 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
406 // Hardcoded namespaces remains capitalized
407 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
408 $wgCapitalLinkOverrides[NS_USER
] = false;
409 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
410 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
411 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
412 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
414 $wgCapitalLinkOverrides = $savedGlobal;
415 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
416 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
417 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
418 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
419 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
420 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
422 // reset global state:
423 $wgCapitalLinkOverrides = $savedGlobal;
426 public function testHasGenderDistinction() {
427 // Namespaces with gender distinctions
428 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
429 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
431 // Other ones, "genderless"
432 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
433 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
434 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
435 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );