using real identity for hashar
[lhc/web/wiklou.git] / tests / phpunit / includes / MWNamespaceTest.php
1 <?php
2 /**
3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
5 * @file
6 */
7
8 /**
9 * Test class for MWNamespace.
10 * Generated by PHPUnit on 2011-02-20 at 21:01:55.
11 *
12 */
13 class MWNamespaceTest extends MediaWikiTestCase {
14 /**
15 * Sets up the fixture, for example, opens a network connection.
16 * This method is called before a test is executed.
17 */
18 protected function setUp() {
19 }
20
21 /**
22 * Tears down the fixture, for example, closes a network connection.
23 * This method is called after a test is executed.
24 */
25 protected function tearDown() {
26 }
27
28
29 #### START OF TESTS #########################################################
30
31 /**
32 * @todo Write more texts, handle $wgAllowImageMoving setting
33 */
34 public function testIsMovable() {
35 $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
36 # @todo FIXME: Write more tests!!
37 }
38
39 /**
40 * Please make sure to change testIsTalk() if you change the assertions below
41 */
42 public function testIsMain() {
43 // Special namespaces
44 $this->assertTrue( MWNamespace::isMain( NS_MEDIA ) );
45 $this->assertTrue( MWNamespace::isMain( NS_SPECIAL ) );
46
47 // Subject pages
48 $this->assertTrue( MWNamespace::isMain( NS_MAIN ) );
49 $this->assertTrue( MWNamespace::isMain( NS_USER ) );
50 $this->assertTrue( MWNamespace::isMain( 100 ) ); # user defined
51
52 // Talk pages
53 $this->assertFalse( MWNamespace::isMain( NS_TALK ) );
54 $this->assertFalse( MWNamespace::isMain( NS_USER_TALK ) );
55 $this->assertFalse( MWNamespace::isMain( 101 ) ); # user defined
56 }
57
58 /**
59 * Reverse of testIsMain().
60 * Please update testIsMain() if you change assertions below
61 */
62 public function testIsTalk() {
63 // Special namespaces
64 $this->assertFalse( MWNamespace::isTalk( NS_MEDIA ) );
65 $this->assertFalse( MWNamespace::isTalk( NS_SPECIAL ) );
66
67 // Subject pages
68 $this->assertFalse( MWNamespace::isTalk( NS_MAIN ) );
69 $this->assertFalse( MWNamespace::isTalk( NS_USER ) );
70 $this->assertFalse( MWNamespace::isTalk( 100 ) ); # user defined
71
72 // Talk pages
73 $this->assertTrue( MWNamespace::isTalk( NS_TALK ) );
74 $this->assertTrue( MWNamespace::isTalk( NS_USER_TALK ) );
75 $this->assertTrue( MWNamespace::isTalk( 101 ) ); # user defined
76 }
77
78 /**
79 * Regular getTalk() calls
80 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
81 * the function testGetTalkExceptions()
82 */
83 public function testGetTalk() {
84 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
85 }
86
87 /**
88 * Exceptions with getTalk()
89 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
90 * @expectedException MWException
91 */
92 public function testGetTalkExceptionsForNsMedia() {
93 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
94 }
95
96 /**
97 * Exceptions with getTalk()
98 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
99 * @expectedException MWException
100 */
101 public function testGetTalkExceptionsForNsSpecial() {
102 $this->assertNull( MWNamespace::getTalk( 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( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
112 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
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 /**
127 * @expectedException MWException
128 */
129 public function testGetAssociatedExceptionsForNsSpecial() {
130 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
131 }
132
133 /**
134 */
135 public function testGetSubject() {
136 // Special namespaces are their own subjects
137 $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
138 $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
139
140 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
141 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
142 }
143
144 /**
145 * @todo Implement testExists().
146 */
147 /*
148 public function testExists() {
149 // Remove the following lines when you implement this test.
150 $this->markTestIncomplete(
151 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
152 );
153 }
154 */
155 /**
156 * @todo Implement testGetCanonicalNamespaces().
157 */
158 /*
159 public function testGetCanonicalNamespaces() {
160 // Remove the following lines when you implement this test.
161 $this->markTestIncomplete(
162 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
163 );
164 }
165 */
166 /**
167 * @todo Implement testGetCanonicalName().
168 */
169 /*
170 public function testGetCanonicalName() {
171 // Remove the following lines when you implement this test.
172 $this->markTestIncomplete(
173 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
174 );
175 }
176 */
177 /**
178 * @todo Implement testGetCanonicalIndex().
179 */
180 /*
181 public function testGetCanonicalIndex() {
182 // Remove the following lines when you implement this test.
183 $this->markTestIncomplete(
184 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
185 );
186 }
187 */
188 /**
189 * @todo Implement testGetValidNamespaces().
190 */
191 /*
192 public function testGetValidNamespaces() {
193 // Remove the following lines when you implement this test.
194 $this->markTestIncomplete(
195 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
196 );
197 }
198 */
199 /**
200 */
201 public function testCanTalk() {
202 $this->assertFalse( MWNamespace::canTalk( NS_MEDIA ) );
203 $this->assertFalse( MWNamespace::canTalk( NS_SPECIAL ) );
204
205 $this->assertTrue( MWNamespace::canTalk( NS_MAIN ) );
206 $this->assertTrue( MWNamespace::canTalk( NS_TALK ) );
207 $this->assertTrue( MWNamespace::canTalk( NS_USER ) );
208 $this->assertTrue( MWNamespace::canTalk( NS_USER_TALK ) );
209
210 // User defined namespaces
211 $this->assertTrue( MWNamespace::canTalk( 100 ) );
212 $this->assertTrue( MWNamespace::canTalk( 101 ) );
213 }
214
215 /**
216 */
217 public function testIsContent() {
218 // NS_MAIN is a content namespace per DefaultSettings.php
219 // and per function definition.
220 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
221
222 global $wgContentNamespaces;
223
224 $saved = $wgContentNamespaces;
225
226 $wgContentNamespaces[] = NS_MAIN;
227 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
228
229 // Other namespaces which are not expected to be content
230 if ( isset( $wgContentNamespaces[NS_MEDIA] ) ) {
231 unset( $wgContentNamespaces[NS_MEDIA] );
232 }
233 $this->assertFalse( MWNamespace::isContent( NS_MEDIA ) );
234
235 if ( isset( $wgContentNamespaces[NS_SPECIAL] ) ) {
236 unset( $wgContentNamespaces[NS_SPECIAL] );
237 }
238 $this->assertFalse( MWNamespace::isContent( NS_SPECIAL ) );
239
240 if ( isset( $wgContentNamespaces[NS_TALK] ) ) {
241 unset( $wgContentNamespaces[NS_TALK] );
242 }
243 $this->assertFalse( MWNamespace::isContent( NS_TALK ) );
244
245 if ( isset( $wgContentNamespaces[NS_USER] ) ) {
246 unset( $wgContentNamespaces[NS_USER] );
247 }
248 $this->assertFalse( MWNamespace::isContent( NS_USER ) );
249
250 if ( isset( $wgContentNamespaces[NS_CATEGORY] ) ) {
251 unset( $wgContentNamespaces[NS_CATEGORY] );
252 }
253 $this->assertFalse( MWNamespace::isContent( NS_CATEGORY ) );
254
255 if ( isset( $wgContentNamespaces[100] ) ) {
256 unset( $wgContentNamespaces[100] );
257 }
258 $this->assertFalse( MWNamespace::isContent( 100 ) );
259
260 $wgContentNamespaces = $saved;
261 }
262
263 /**
264 * Similar to testIsContent() but alters the $wgContentNamespaces
265 * global variable.
266 */
267 public function testIsContentWithAdditionsInWgContentNamespaces() {
268 // NS_MAIN is a content namespace per DefaultSettings.php
269 // and per function definition.
270 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
271
272 // Tests that user defined namespace #252 is not content:
273 $this->assertFalse( MWNamespace::isContent( 252 ) );
274
275 # @todo FIXME: Is global saving really required for PHPUnit?
276 // Bless namespace # 252 as a content namespace
277 global $wgContentNamespaces;
278 $savedGlobal = $wgContentNamespaces;
279 $wgContentNamespaces[] = 252;
280 $this->assertTrue( MWNamespace::isContent( 252 ) );
281
282 // Makes sure NS_MAIN was not impacted
283 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
284
285 // Restore global
286 $wgContentNamespaces = $savedGlobal;
287
288 // Verify namespaces after global restauration
289 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
290 $this->assertFalse( MWNamespace::isContent( 252 ) );
291 }
292
293 public function testIsWatchable() {
294 // Specials namespaces are not watchable
295 $this->assertFalse( MWNamespace::isWatchable( NS_MEDIA ) );
296 $this->assertFalse( MWNamespace::isWatchable( NS_SPECIAL ) );
297
298 // Core defined namespaces are watchables
299 $this->assertTrue( MWNamespace::isWatchable( NS_MAIN ) );
300 $this->assertTrue( MWNamespace::isWatchable( NS_TALK ) );
301
302 // Additional, user defined namespaces are watchables
303 $this->assertTrue( MWNamespace::isWatchable( 100 ) );
304 $this->assertTrue( MWNamespace::isWatchable( 101 ) );
305 }
306
307 public function testHasSubpages() {
308 // Special namespaces:
309 $this->assertFalse( MWNamespace::hasSubpages( NS_MEDIA ) );
310 $this->assertFalse( MWNamespace::hasSubpages( NS_SPECIAL ) );
311
312 // namespaces without subpages
313 # save up global
314 global $wgNamespacesWithSubpages;
315 $saved = null;
316 if( array_key_exists( NS_MAIN, $wgNamespacesWithSubpages ) ) {
317 $saved = $wgNamespacesWithSubpages[NS_MAIN];
318 unset( $wgNamespacesWithSubpages[NS_MAIN] );
319 }
320
321 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
322
323 $wgNamespacesWithSubpages[NS_MAIN] = true;
324 $this->assertTrue( MWNamespace::hasSubpages( NS_MAIN ) );
325 $wgNamespacesWithSubpages[NS_MAIN] = false;
326 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
327
328 # restore global
329 if( $saved !== null ) {
330 $wgNamespacesWithSubpages[NS_MAIN] = $saved;
331 }
332
333 // Some namespaces with subpages
334 $this->assertTrue( MWNamespace::hasSubpages( NS_TALK ) );
335 $this->assertTrue( MWNamespace::hasSubpages( NS_USER ) );
336 $this->assertTrue( MWNamespace::hasSubpages( NS_USER_TALK ) );
337 }
338
339 /**
340 */
341 public function testGetContentNamespaces() {
342 $this->assertEquals(
343 array( NS_MAIN ),
344 MWNamespace::getcontentNamespaces(),
345 '$wgContentNamespaces is an array with only NS_MAIN by default'
346 );
347
348 global $wgContentNamespaces;
349
350 # test !is_array( $wgcontentNamespaces )
351 $wgContentNamespaces = '';
352 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
353 $wgContentNamespaces = false;
354 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
355 $wgContentNamespaces = null;
356 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
357 $wgContentNamespaces = 5;
358 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
359
360 # test $wgContentNamespaces === array()
361 $wgContentNamespaces = array();
362 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
363
364 # test !in_array( NS_MAIN, $wgContentNamespaces )
365 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
366 $this->assertEquals(
367 array( NS_MAIN, NS_USER, NS_CATEGORY ),
368 MWNamespace::getcontentNamespaces(),
369 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
370 );
371
372 # test other cases, return $wgcontentNamespaces as is
373 $wgContentNamespaces = array( NS_MAIN );
374 $this->assertEquals(
375 array( NS_MAIN ),
376 MWNamespace::getcontentNamespaces()
377 );
378
379 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
380 $this->assertEquals(
381 array( NS_MAIN, NS_USER, NS_CATEGORY ),
382 MWNamespace::getcontentNamespaces()
383 );
384
385 }
386
387 /**
388 * Some namespaces are always capitalized per code definition
389 * in MWNamespace::$alwaysCapitalizedNamespaces
390 */
391 public function testIsCapitalizedHardcodedAssertions() {
392 // NS_MEDIA and NS_FILE are treated the same
393 $this->assertEquals(
394 MWNamespace::isCapitalized( NS_MEDIA ),
395 MWNamespace::isCapitalized( NS_FILE ),
396 'NS_MEDIA and NS_FILE have same capitalization rendering'
397 );
398
399 // Boths are capitalized by default
400 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIA ) );
401 $this->assertTrue( MWNamespace::isCapitalized( NS_FILE ) );
402
403 // Always capitalized namespaces
404 // @see MWNamespace::$alwaysCapitalizedNamespaces
405 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
406 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
407 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
408 }
409
410 /**
411 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
412 * global $wgCapitalLink setting to have extended coverage.
413 *
414 * MWNamespace::isCapitalized() rely on two global settings:
415 * $wgCapitalLinkOverrides = array(); by default
416 * $wgCapitalLinks = true; by default
417 * This function test $wgCapitalLinks
418 *
419 * Global setting correctness is tested against the NS_PROJECT and
420 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
421 */
422 public function testIsCapitalizedWithWgCapitalLinks() {
423 global $wgCapitalLinks;
424 // Save the global to easily reset to MediaWiki default settings
425 $savedGlobal = $wgCapitalLinks;
426
427 $wgCapitalLinks = true;
428 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
429 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
430
431 $wgCapitalLinks = false;
432 // hardcoded namespaces (see above function) are still capitalized:
433 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
434 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
435 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
436 // setting is correctly applied
437 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
438 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
439
440 // reset global state:
441 $wgCapitalLinks = $savedGlobal;
442 }
443
444 /**
445 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
446 * testing the $wgCapitalLinkOverrides global.
447 *
448 * @todo split groups of assertions in autonomous testing functions
449 */
450 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
451 global $wgCapitalLinkOverrides;
452 // Save the global to easily reset to MediaWiki default settings
453 $savedGlobal = $wgCapitalLinkOverrides;
454
455 // Test default settings
456 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
457 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
458 // hardcoded namespaces (see above function) are capitalized:
459 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
460 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
461 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
462
463 // Hardcoded namespaces remains capitalized
464 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
465 $wgCapitalLinkOverrides[NS_USER] = false;
466 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
467 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
468 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
469 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
470
471 $wgCapitalLinkOverrides = $savedGlobal;
472 $wgCapitalLinkOverrides[NS_PROJECT] = false;
473 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
474 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
475 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
476 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
477 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
478
479 // reset global state:
480 $wgCapitalLinkOverrides = $savedGlobal;
481 }
482
483 public function testHasGenderDistinction() {
484 // Namespaces with gender distinctions
485 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
486 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
487
488 // Other ones, "genderless"
489 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
490 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
491 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
492 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
493
494 }
495 }
496