improve namespace related methods
[lhc/web/wiklou.git] / tests / phpunit / includes / MWNamespaceTest.php
1 <?php
2 /**
3 * @author Ashar Voultoiz
4 * @copyright Copyright © 2011, Ashar Voultoiz
5 * @file
6 */
7
8 require_once '/var/www/mediawiki-trunk/includes/Namespace.php';
9
10 /**
11 * Test class for MWNamespace.
12 * Generated by PHPUnit on 2011-02-20 at 21:01:55.
13 *
14 */
15 class MWNamespaceTest extends PHPUnit_Framework_TestCase {
16 /**
17 * @var MWNamespace
18 */
19 protected $object;
20
21 /**
22 * Sets up the fixture, for example, opens a network connection.
23 * This method is called before a test is executed.
24 */
25 protected function setUp() {
26 $this->object = new MWNamespace;
27 }
28
29 /**
30 * Tears down the fixture, for example, closes a network connection.
31 * This method is called after a test is executed.
32 */
33 protected function tearDown() {
34 }
35
36
37 #### START OF TESTS #########################################################
38
39 /**
40 * @todo Write more texts, handle $wgAllowImageMoving setting
41 */
42 public function testIsMovable() {
43 $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
44 # FIXME : write more tests!!
45 }
46
47 /**
48 * Please make sure to change testIsTalk() if you change the assertions below
49 */
50 public function testIsMain() {
51 // Special namespaces
52 $this->assertTrue( MWNamespace::isMain( NS_MEDIA ) );
53 $this->assertTrue( MWNamespace::isMain( NS_SPECIAL ) );
54
55 // Subject pages
56 $this->assertTrue( MWNamespace::isMain( NS_MAIN ) );
57 $this->assertTrue( MWNamespace::isMain( NS_USER ) );
58 $this->assertTrue( MWNamespace::isMain( 100 ) ); # user defined
59
60 // Talk pages
61 $this->assertFalse( MWNamespace::isMain( NS_TALK ) );
62 $this->assertFalse( MWNamespace::isMain( NS_USER_TALK ) );
63 $this->assertFalse( MWNamespace::isMain( 101 ) ); # user defined
64 }
65
66 /**
67 * Reverse of testIsMain().
68 * Please update testIsMain() if you change assertions below
69 */
70 public function testIsTalk() {
71 // Special namespaces
72 $this->assertFalse( MWNamespace::isTalk( NS_MEDIA ) );
73 $this->assertFalse( MWNamespace::isTalk( NS_SPECIAL ) );
74
75 // Subject pages
76 $this->assertFalse( MWNamespace::isTalk( NS_MAIN ) );
77 $this->assertFalse( MWNamespace::isTalk( NS_USER ) );
78 $this->assertFalse( MWNamespace::isTalk( 100 ) ); # user defined
79
80 // Talk pages
81 $this->assertTrue( MWNamespace::isTalk( NS_TALK ) );
82 $this->assertTrue( MWNamespace::isTalk( NS_USER_TALK ) );
83 $this->assertTrue( MWNamespace::isTalk( 101 ) ); # user defined
84 }
85
86 /**
87 * Regular getTalk() calls
88 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
89 * the function testGetTalkExceptions()
90 */
91 public function testGetTalk() {
92 $this->assertEquals( MWNamespace::getTalk( NS_MAIN), NS_TALK );
93 }
94
95 /**
96 * Exceptions with getTalk()
97 * NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raise an exception for them.
98 * @expectedException MWException
99 */
100 public function testGetTalkExceptions() {
101 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
102 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
103 }
104
105 /**
106 * Regular getAssociated() calls
107 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
108 * the function testGetAssociatedExceptions()
109 */
110 public function testGetAssociated() {
111 $this->assertEquals( MWNamespace::getAssociated( NS_MAIN ), NS_TALK );
112 $this->assertEquals( MWNamespace::getAssociated( NS_TALK ), NS_MAIN );
113
114 }
115
116 ### Exceptions with getAssociated()
117 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
118 ### an exception for them.
119 /**
120 * @expectedException MWException
121 */
122 public function testGetAssociatedExceptionsForNsMedia() {
123 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
124 }
125 /**
126 * @expectedException MWException
127 */
128 public function testGetAssociatedExceptionsForNsSpecial() {
129 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
130 }
131
132 /**
133 */
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 );
138
139 $this->assertEquals( MWNamespace::getSubject( NS_TALK ), NS_MAIN );
140 $this->assertEquals( MWNamespace::getSubject( NS_USER_TALK ), NS_USER );
141 }
142
143 /**
144 * @todo Implement testExists().
145 */
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.'
150 );
151 }
152
153 /**
154 * @todo Implement testGetCanonicalNamespaces().
155 */
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.'
160 );
161 }
162
163 /**
164 * @todo Implement testGetCanonicalName().
165 */
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.'
170 );
171 }
172
173 /**
174 * @todo Implement testGetCanonicalIndex().
175 */
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.'
180 );
181 }
182
183 /**
184 * @todo Implement testGetValidNamespaces().
185 */
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.'
190 );
191 }
192
193 /**
194 */
195 public function testCanTalk() {
196 $this->assertFalse( MWNamespace::canTalk( NS_MEDIA ) );
197 $this->assertFalse( MWNamespace::canTalk( NS_SPECIAL ) );
198
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 ) );
203
204 // User defined namespaces
205 $this->assertTrue( MWNamespace::canTalk( 100 ) );
206 $this->assertTrue( MWNamespace::canTalk( 101 ) );
207 }
208
209 /**
210 */
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 ) );
215
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 ) );
224 }
225
226 /**
227 * Similar to testIsContent() but alters the $wgContentNamespaces
228 * global variable.
229 */
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 ) );
234
235 // Tests that user defined namespace #252 is not content:
236 $this->assertFalse( MWNamespace::isContent( 252 ) );
237
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 ) );
244
245 // Makes sure NS_MAIN was not impacted
246 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
247
248 // Restore global
249 $wgContentNamespaces = $savedGlobal;
250
251 // Verify namespaces after global restauration
252 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
253 $this->assertFalse( MWNamespace::isContent( 252 ) );
254 }
255
256 public function testIsWatchable() {
257 // Specials namespaces are not watchable
258 $this->assertFalse( MWNamespace::isWatchable( NS_MEDIA ) );
259 $this->assertFalse( MWNamespace::isWatchable( NS_SPECIAL ) );
260
261 // Core defined namespaces are watchables
262 $this->assertTrue( MWNamespace::isWatchable( NS_MAIN ) );
263 $this->assertTrue( MWNamespace::isWatchable( NS_TALK ) );
264
265 // Additional, user defined namespaces are watchables
266 $this->assertTrue( MWNamespace::isWatchable( 100 ) );
267 $this->assertTrue( MWNamespace::isWatchable( 101 ) );
268 }
269
270 public function testHasSubpages() {
271 // Special namespaces:
272 $this->assertFalse( MWNamespace::hasSubpages( NS_MEDIA ) );
273 $this->assertFalse( MWNamespace::hasSubpages( NS_SPECIAL ) );
274
275 // namespaces without subpages
276 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
277
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 ) );
282 }
283
284 /**
285 */
286 public function testGetContentNamespaces() {
287 $this->assertEquals(
288 MWNamespace::getcontentNamespaces(),
289 array( NS_MAIN),
290 '$wgContentNamespaces is an array with only NS_MAIN by default'
291 );
292
293 global $wgContentNamespaces;
294
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 );
304
305 # test $wgContentNamespaces === array()
306 $wgContentNamespaces = array();
307 $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN );
308
309 # test !in_array( NS_MAIN, $wgContentNamespaces )
310 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
311 $this->assertEquals(
312 MWNamespace::getcontentNamespaces(),
313 array( NS_MAIN, NS_USER, NS_CATEGORY ),
314 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
315 );
316
317 # test other cases, return $wgcontentNamespaces as is
318 $wgContentNamespaces = array( NS_MAIN );
319 $this->assertEquals(
320 MWNamespace::getcontentNamespaces(),
321 array( NS_MAIN )
322 );
323
324 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
325 $this->assertEquals(
326 MWNamespace::getcontentNamespaces(),
327 array( NS_MAIN, NS_USER, NS_CATEGORY )
328 );
329
330 }
331
332 /**
333 * Some namespaces are always capitalized per code definition
334 * in MWNamespace::$alwaysCapitalizedNamespaces
335 */
336 public function testIsCapitalizedHardcodedAssertions() {
337 // NS_MEDIA and NS_FILE are treated the same
338 $this->assertEquals(
339 MWNamespace::isCapitalized( NS_MEDIA ),
340 MWNamespace::isCapitalized( NS_FILE ),
341 'NS_MEDIA and NS_FILE have same capitalization rendering'
342 );
343
344 // Boths are capitalized by default
345 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIA ) );
346 $this->assertTrue( MWNamespace::isCapitalized( NS_FILE ) );
347
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 ) );
353 }
354
355 /**
356 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
357 * global $wgCapitalLink setting to have extended coverage.
358 *
359 * MWNamespace::isCapitalized() rely on two global settings:
360 * $wgCapitalLinkOverrides = array(); by default
361 * $wgCapitalLinks = true; by default
362 * This function test $wgCapitalLinks
363 *
364 * Global setting correctness is tested against the NS_PROJECT and
365 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
366 */
367 public function testIsCapitalizedWithWgCapitalLinks() {
368 global $wgCapitalLinks;
369 // Save the global to easily reset to MediaWiki default settings
370 $savedGlobal = $wgCapitalLinks;
371
372 $wgCapitalLinks = true;
373 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
374 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
375
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 ) );
384
385 // reset global state:
386 $wgCapitalLinks = $savedGlobal;
387 }
388
389 /**
390 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
391 * testing the $wgCapitalLinkOverrides global.
392 *
393 * @todo split groups of assertions in autonomous testing functions
394 */
395 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
396 global $wgCapitalLinkOverrides;
397 // Save the global to easily reset to MediaWiki default settings
398 $savedGlobal = $wgCapitalLinkOverrides;
399
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 ) );
407
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 ) );
415
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 ) );
423
424 // reset global state:
425 $wgCapitalLinkOverrides = $savedGlobal;
426 }
427
428 public function testHasGenderDistinction() {
429 // Namespaces with gender distinctions
430 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
431 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
432
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 ) );
438
439 }
440 }
441 ?>