3 * @author Ashar Voultoiz
4 * @copyright Copyright © 2011, Ashar Voultoiz
8 require_once '/var/www/mediawiki-trunk/includes/Namespace.php';
11 * Test class for MWNamespace.
12 * Generated by PHPUnit on 2011-02-20 at 21:01:55.
15 class MWNamespaceTest
extends PHPUnit_Framework_TestCase
{
22 * Sets up the fixture, for example, opens a network connection.
23 * This method is called before a test is executed.
25 protected function setUp() {
26 $this->object = new MWNamespace
;
30 * Tears down the fixture, for example, closes a network connection.
31 * This method is called after a test is executed.
33 protected function tearDown() {
37 #### START OF TESTS #########################################################
40 * @todo Write more texts, handle $wgAllowImageMoving setting
42 public function testIsMovable() {
43 $this->assertFalse( MWNamespace
::isMovable( NS_CATEGORY
) );
44 # FIXME : write more tests!!
48 * Please make sure to change testIsTalk() if you change the assertions below
50 public function testIsMain() {
52 $this->assertTrue( MWNamespace
::isMain( NS_MEDIA
) );
53 $this->assertTrue( MWNamespace
::isMain( NS_SPECIAL
) );
56 $this->assertTrue( MWNamespace
::isMain( NS_MAIN
) );
57 $this->assertTrue( MWNamespace
::isMain( NS_USER
) );
58 $this->assertTrue( MWNamespace
::isMain( 100 ) ); # user defined
61 $this->assertFalse( MWNamespace
::isMain( NS_TALK
) );
62 $this->assertFalse( MWNamespace
::isMain( NS_USER_TALK
) );
63 $this->assertFalse( MWNamespace
::isMain( 101 ) ); # user defined
67 * Reverse of testIsMain().
68 * Please update testIsMain() if you change assertions below
70 public function testIsTalk() {
72 $this->assertFalse( MWNamespace
::isTalk( NS_MEDIA
) );
73 $this->assertFalse( MWNamespace
::isTalk( NS_SPECIAL
) );
76 $this->assertFalse( MWNamespace
::isTalk( NS_MAIN
) );
77 $this->assertFalse( MWNamespace
::isTalk( NS_USER
) );
78 $this->assertFalse( MWNamespace
::isTalk( 100 ) ); # user defined
81 $this->assertTrue( MWNamespace
::isTalk( NS_TALK
) );
82 $this->assertTrue( MWNamespace
::isTalk( NS_USER_TALK
) );
83 $this->assertTrue( MWNamespace
::isTalk( 101 ) ); # user defined
87 * Regular getTalk() calls
88 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
89 * the function testGetTalkExceptions()
91 public function testGetTalk() {
92 $this->assertEquals( MWNamespace
::getTalk( NS_MAIN
), NS_TALK
);
96 * Exceptions with getTalk()
97 * NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raise an exception for them.
98 * @expectedException MWException
100 public function testGetTalkExceptions() {
101 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
102 $this->assertNull( MWNamespace
::getAssociated( 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( MWNamespace
::getAssociated( NS_MAIN
), NS_TALK
);
112 $this->assertEquals( MWNamespace
::getAssociated( NS_TALK
), NS_MAIN
);
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
) );
126 * @expectedException MWException
128 public function testGetAssociatedExceptionsForNsSpecial() {
129 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
134 public function testGetSubject() {
135 // Special namespaces are their own subjects
136 $this->assertEquals( MWNamespace
::getSubject( NS_MEDIA
), NS_MEDIA
);
137 $this->assertEquals( MWNamespace
::getSubject( NS_SPECIAL
), NS_SPECIAL
);
139 $this->assertEquals( MWNamespace
::getSubject( NS_TALK
), NS_MAIN
);
140 $this->assertEquals( MWNamespace
::getSubject( NS_USER_TALK
), NS_USER
);
144 * @todo Implement testExists().
146 public function testExists() {
147 // Remove the following lines when you implement this test.
148 $this->markTestIncomplete(
149 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
154 * @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().
166 public function testGetCanonicalName() {
167 // Remove the following lines when you implement this test.
168 $this->markTestIncomplete(
169 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
174 * @todo Implement testGetCanonicalIndex().
176 public function testGetCanonicalIndex() {
177 // Remove the following lines when you implement this test.
178 $this->markTestIncomplete(
179 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
184 * @todo Implement testGetValidNamespaces().
186 public function testGetValidNamespaces() {
187 // Remove the following lines when you implement this test.
188 $this->markTestIncomplete(
189 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
195 public function testCanTalk() {
196 $this->assertFalse( MWNamespace
::canTalk( NS_MEDIA
) );
197 $this->assertFalse( MWNamespace
::canTalk( NS_SPECIAL
) );
199 $this->assertTrue( MWNamespace
::canTalk( NS_MAIN
) );
200 $this->assertTrue( MWNamespace
::canTalk( NS_TALK
) );
201 $this->assertTrue( MWNamespace
::canTalk( NS_USER
) );
202 $this->assertTrue( MWNamespace
::canTalk( NS_USER_TALK
) );
204 // User defined namespaces
205 $this->assertTrue( MWNamespace
::canTalk( 100 ) );
206 $this->assertTrue( MWNamespace
::canTalk( 101 ) );
211 public function testIsContent() {
212 // NS_MAIN is a content namespace per DefaultSettings.php
213 // and per function definition.
214 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
216 // Other namespaces which are not expected to be content
217 $this->assertFalse( MWNamespace
::isContent( NS_MEDIA
) );
218 $this->assertFalse( MWNamespace
::isContent( NS_SPECIAL
) );
219 $this->assertFalse( MWNamespace
::isContent( NS_TALK
) );
220 $this->assertFalse( MWNamespace
::isContent( NS_USER
) );
221 $this->assertFalse( MWNamespace
::isContent( NS_CATEGORY
) );
222 // User defined namespace:
223 $this->assertFalse( MWNamespace
::isContent( 100 ) );
227 * Similar to testIsContent() but alters the $wgContentNamespaces
230 public function testIsContentWithAdditionsInWgContentNamespaces() {
231 // NS_MAIN is a content namespace per DefaultSettings.php
232 // and per function definition.
233 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
235 // Tests that user defined namespace #252 is not content:
236 $this->assertFalse( MWNamespace
::isContent( 252 ) );
238 # FIXME: is global saving really required for PHPUnit?
239 // Bless namespace # 252 as a content namespace
240 global $wgContentNamespaces;
241 $savedGlobal = $wgContentNamespaces;
242 $wgContentNamespaces[] = 252;
243 $this->assertTrue( MWNamespace
::isContent( 252 ) );
245 // Makes sure NS_MAIN was not impacted
246 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
249 $wgContentNamespaces = $savedGlobal;
251 // Verify namespaces after global restauration
252 $this->assertTrue( MWNamespace
::isContent( NS_MAIN
) );
253 $this->assertFalse( MWNamespace
::isContent( 252 ) );
256 public function testIsWatchable() {
257 // Specials namespaces are not watchable
258 $this->assertFalse( MWNamespace
::isWatchable( NS_MEDIA
) );
259 $this->assertFalse( MWNamespace
::isWatchable( NS_SPECIAL
) );
261 // Core defined namespaces are watchables
262 $this->assertTrue( MWNamespace
::isWatchable( NS_MAIN
) );
263 $this->assertTrue( MWNamespace
::isWatchable( NS_TALK
) );
265 // Additional, user defined namespaces are watchables
266 $this->assertTrue( MWNamespace
::isWatchable( 100 ) );
267 $this->assertTrue( MWNamespace
::isWatchable( 101 ) );
270 public function testHasSubpages() {
271 // Special namespaces:
272 $this->assertFalse( MWNamespace
::hasSubpages( NS_MEDIA
) );
273 $this->assertFalse( MWNamespace
::hasSubpages( NS_SPECIAL
) );
275 // namespaces without subpages
276 $this->assertFalse( MWNamespace
::hasSubpages( NS_MAIN
) );
278 // Some namespaces with subpages
279 $this->assertTrue( MWNamespace
::hasSubpages( NS_TALK
) );
280 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER
) );
281 $this->assertTrue( MWNamespace
::hasSubpages( NS_USER_TALK
) );
286 public function testGetContentNamespaces() {
288 MWNamespace
::getcontentNamespaces(),
290 '$wgContentNamespaces is an array with only NS_MAIN by default'
293 global $wgContentNamespaces;
295 # test !is_array( $wgcontentNamespaces )
296 $wgContentNamespaces = '';
297 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
298 $wgContentNamespaces = false;
299 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
300 $wgContentNamespaces = null;
301 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
302 $wgContentNamespaces = 5;
303 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
305 # test $wgContentNamespaces === array()
306 $wgContentNamespaces = array();
307 $this->assertEquals( MWNamespace
::getcontentNamespaces(), NS_MAIN
);
309 # test !in_array( NS_MAIN, $wgContentNamespaces )
310 $wgContentNamespaces = array( NS_USER
, NS_CATEGORY
);
312 MWNamespace
::getcontentNamespaces(),
313 array( NS_MAIN
, NS_USER
, NS_CATEGORY
),
314 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
317 # test other cases, return $wgcontentNamespaces as is
318 $wgContentNamespaces = array( NS_MAIN
);
320 MWNamespace
::getcontentNamespaces(),
324 $wgContentNamespaces = array( NS_MAIN
, NS_USER
, NS_CATEGORY
);
326 MWNamespace
::getcontentNamespaces(),
327 array( NS_MAIN
, NS_USER
, NS_CATEGORY
)
333 * Some namespaces are always capitalized per code definition
334 * in MWNamespace::$alwaysCapitalizedNamespaces
336 public function testIsCapitalizedHardcodedAssertions() {
337 // NS_MEDIA and NS_FILE are treated the same
339 MWNamespace
::isCapitalized( NS_MEDIA
),
340 MWNamespace
::isCapitalized( NS_FILE
),
341 'NS_MEDIA and NS_FILE have same capitalization rendering'
344 // Boths are capitalized by default
345 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIA
) );
346 $this->assertTrue( MWNamespace
::isCapitalized( NS_FILE
) );
348 // Always capitalized namespaces
349 // @see MWNamespace::$alwaysCapitalizedNamespaces
350 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
351 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
352 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
356 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
357 * global $wgCapitalLink setting to have extended coverage.
359 * MWNamespace::isCapitalized() rely on two global settings:
360 * $wgCapitalLinkOverrides = array(); by default
361 * $wgCapitalLinks = true; by default
362 * This function test $wgCapitalLinks
364 * Global setting correctness is tested against the NS_PROJECT and
365 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
367 public function testIsCapitalizedWithWgCapitalLinks() {
368 global $wgCapitalLinks;
369 // Save the global to easily reset to MediaWiki default settings
370 $savedGlobal = $wgCapitalLinks;
372 $wgCapitalLinks = true;
373 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
374 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
376 $wgCapitalLinks = false;
377 // hardcoded namespaces (see above function) are still capitalized:
378 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
379 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
380 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
381 // setting is correctly applied
382 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
383 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
385 // reset global state:
386 $wgCapitalLinks = $savedGlobal;
390 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
391 * testing the $wgCapitalLinkOverrides global.
393 * @todo split groups of assertions in autonomous testing functions
395 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
396 global $wgCapitalLinkOverrides;
397 // Save the global to easily reset to MediaWiki default settings
398 $savedGlobal = $wgCapitalLinkOverrides;
400 // Test default settings
401 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
402 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT_TALK
) );
403 // hardcoded namespaces (see above function) are capitalized:
404 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
405 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
406 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
408 // Hardcoded namespaces remains capitalized
409 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
410 $wgCapitalLinkOverrides[NS_USER
] = false;
411 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
412 $this->assertTrue( MWNamespace
::isCapitalized( NS_SPECIAL
) );
413 $this->assertTrue( MWNamespace
::isCapitalized( NS_USER
) );
414 $this->assertTrue( MWNamespace
::isCapitalized( NS_MEDIAWIKI
) );
416 $wgCapitalLinkOverrides = $savedGlobal;
417 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
418 $this->assertFalse( MWNamespace
::isCapitalized( NS_PROJECT
) );
419 $wgCapitalLinkOverrides[NS_PROJECT
] = true ;
420 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
421 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
422 $this->assertTrue( MWNamespace
::isCapitalized( NS_PROJECT
) );
424 // reset global state:
425 $wgCapitalLinkOverrides = $savedGlobal;
428 public function testHasGenderDistinction() {
429 // Namespaces with gender distinctions
430 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
431 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
433 // Other ones, "genderless"
434 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
435 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
436 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
437 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );