Implement a number of namespace related equals functions:
[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 testIsSubject() {
43 // Special namespaces
44 $this->assertTrue( MWNamespace::isSubject( NS_MEDIA ) );
45 $this->assertTrue( MWNamespace::isSubject( NS_SPECIAL ) );
46
47 // Subject pages
48 $this->assertTrue( MWNamespace::isSubject( NS_MAIN ) );
49 $this->assertTrue( MWNamespace::isSubject( NS_USER ) );
50 $this->assertTrue( MWNamespace::isSubject( 100 ) ); # user defined
51
52 // Talk pages
53 $this->assertFalse( MWNamespace::isSubject( NS_TALK ) );
54 $this->assertFalse( MWNamespace::isSubject( NS_USER_TALK ) );
55 $this->assertFalse( MWNamespace::isSubject( 101 ) ); # user defined
56
57 // Back compat
58 $this->assertTrue( MWNamespace::isMain( NS_MAIN ) == MWNamespace::isSubject( NS_MAIN ) );
59 $this->assertTrue( MWNamespace::isMain( NS_USER_TALK ) == MWNamespace::isSubject( NS_USER_TALK ) );
60 }
61
62 /**
63 * Reverse of testIsSubject().
64 * Please update testIsSubject() if you change assertions below
65 */
66 public function testIsTalk() {
67 // Special namespaces
68 $this->assertFalse( MWNamespace::isTalk( NS_MEDIA ) );
69 $this->assertFalse( MWNamespace::isTalk( NS_SPECIAL ) );
70
71 // Subject pages
72 $this->assertFalse( MWNamespace::isTalk( NS_MAIN ) );
73 $this->assertFalse( MWNamespace::isTalk( NS_USER ) );
74 $this->assertFalse( MWNamespace::isTalk( 100 ) ); # user defined
75
76 // Talk pages
77 $this->assertTrue( MWNamespace::isTalk( NS_TALK ) );
78 $this->assertTrue( MWNamespace::isTalk( NS_USER_TALK ) );
79 $this->assertTrue( MWNamespace::isTalk( 101 ) ); # user defined
80 }
81
82 /**
83 */
84 public function testGetSubject() {
85 // Special namespaces are their own subjects
86 $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
87 $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
88
89 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
90 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
91 }
92
93 /**
94 * Regular getTalk() calls
95 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
96 * the function testGetTalkExceptions()
97 */
98 public function testGetTalk() {
99 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
100 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) );
101 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) );
102 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) );
103 }
104
105 /**
106 * Exceptions with getTalk()
107 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
108 * @expectedException MWException
109 */
110 public function testGetTalkExceptionsForNsMedia() {
111 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
112 }
113
114 /**
115 * Exceptions with getTalk()
116 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
117 * @expectedException MWException
118 */
119 public function testGetTalkExceptionsForNsSpecial() {
120 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
121 }
122
123 /**
124 * Regular getAssociated() calls
125 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
126 * the function testGetAssociatedExceptions()
127 */
128 public function testGetAssociated() {
129 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
130 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
131
132 }
133
134 ### Exceptions with getAssociated()
135 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
136 ### an exception for them.
137 /**
138 * @expectedException MWException
139 */
140 public function testGetAssociatedExceptionsForNsMedia() {
141 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
142 }
143
144 /**
145 * @expectedException MWException
146 */
147 public function testGetAssociatedExceptionsForNsSpecial() {
148 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
149 }
150
151 /**
152 * @todo Implement testExists().
153 */
154 /*
155 public function testExists() {
156 // Remove the following lines when you implement this test.
157 $this->markTestIncomplete(
158 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
159 );
160 }
161 */
162
163 /**
164 * Test MWNamespace::equals
165 * Note if we add a namespace registration system with keys like 'MAIN'
166 * we should add tests here for equivilance on things like 'MAIN' == 0
167 * and 'MAIN' == NS_MAIN.
168 */
169 public function testEquals() {
170 $this->assertTrue( MWNamespace::equals( NS_MAIN, NS_MAIN ) );
171 $this->assertTrue( MWNamespace::equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
172 $this->assertTrue( MWNamespace::equals( NS_USER, NS_USER ) );
173 $this->assertTrue( MWNamespace::equals( NS_USER, 2 ) );
174 $this->assertTrue( MWNamespace::equals( NS_USER_TALK, NS_USER_TALK ) );
175 $this->assertTrue( MWNamespace::equals( NS_SPECIAL, NS_SPECIAL ) );
176 $this->assertFalse( MWNamespace::equals( NS_MAIN, NS_TALK ) );
177 $this->assertFalse( MWNamespace::equals( NS_USER, NS_USER_TALK ) );
178 $this->assertFalse( MWNamespace::equals( NS_PROJECT, NS_TEMPLATE ) );
179 }
180
181 /**
182 * Test MWNamespace::subjectEquals
183 */
184 public function testSubjectEquals() {
185 $this->assertTrue( MWNamespace::subjectEquals( NS_MAIN, NS_MAIN ) );
186 $this->assertTrue( MWNamespace::subjectEquals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
187 $this->assertTrue( MWNamespace::subjectEquals( NS_USER, NS_USER ) );
188 $this->assertTrue( MWNamespace::subjectEquals( NS_USER, 2 ) );
189 $this->assertTrue( MWNamespace::subjectEquals( NS_USER_TALK, NS_USER_TALK ) );
190 $this->assertTrue( MWNamespace::subjectEquals( NS_SPECIAL, NS_SPECIAL ) );
191 $this->assertTrue( MWNamespace::subjectEquals( NS_MAIN, NS_TALK ) );
192 $this->assertTrue( MWNamespace::subjectEquals( NS_USER, NS_USER_TALK ) );
193 $this->assertFalse( MWNamespace::subjectEquals( NS_PROJECT, NS_TEMPLATE ) );
194 }
195
196 /**
197 * @todo Implement testGetCanonicalNamespaces().
198 */
199 /*
200 public function testGetCanonicalNamespaces() {
201 // Remove the following lines when you implement this test.
202 $this->markTestIncomplete(
203 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
204 );
205 }
206 */
207 /**
208 * @todo Implement testGetCanonicalName().
209 */
210 /*
211 public function testGetCanonicalName() {
212 // Remove the following lines when you implement this test.
213 $this->markTestIncomplete(
214 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
215 );
216 }
217 */
218 /**
219 * @todo Implement testGetCanonicalIndex().
220 */
221 /*
222 public function testGetCanonicalIndex() {
223 // Remove the following lines when you implement this test.
224 $this->markTestIncomplete(
225 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
226 );
227 }
228 */
229 /**
230 * @todo Implement testGetValidNamespaces().
231 */
232 /*
233 public function testGetValidNamespaces() {
234 // Remove the following lines when you implement this test.
235 $this->markTestIncomplete(
236 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
237 );
238 }
239 */
240 /**
241 */
242 public function testCanTalk() {
243 $this->assertFalse( MWNamespace::canTalk( NS_MEDIA ) );
244 $this->assertFalse( MWNamespace::canTalk( NS_SPECIAL ) );
245
246 $this->assertTrue( MWNamespace::canTalk( NS_MAIN ) );
247 $this->assertTrue( MWNamespace::canTalk( NS_TALK ) );
248 $this->assertTrue( MWNamespace::canTalk( NS_USER ) );
249 $this->assertTrue( MWNamespace::canTalk( NS_USER_TALK ) );
250
251 // User defined namespaces
252 $this->assertTrue( MWNamespace::canTalk( 100 ) );
253 $this->assertTrue( MWNamespace::canTalk( 101 ) );
254 }
255
256 /**
257 */
258 public function testIsContent() {
259 // NS_MAIN is a content namespace per DefaultSettings.php
260 // and per function definition.
261 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
262
263 global $wgContentNamespaces;
264
265 $saved = $wgContentNamespaces;
266
267 $wgContentNamespaces[] = NS_MAIN;
268 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
269
270 // Other namespaces which are not expected to be content
271 if ( isset( $wgContentNamespaces[NS_MEDIA] ) ) {
272 unset( $wgContentNamespaces[NS_MEDIA] );
273 }
274 $this->assertFalse( MWNamespace::isContent( NS_MEDIA ) );
275
276 if ( isset( $wgContentNamespaces[NS_SPECIAL] ) ) {
277 unset( $wgContentNamespaces[NS_SPECIAL] );
278 }
279 $this->assertFalse( MWNamespace::isContent( NS_SPECIAL ) );
280
281 if ( isset( $wgContentNamespaces[NS_TALK] ) ) {
282 unset( $wgContentNamespaces[NS_TALK] );
283 }
284 $this->assertFalse( MWNamespace::isContent( NS_TALK ) );
285
286 if ( isset( $wgContentNamespaces[NS_USER] ) ) {
287 unset( $wgContentNamespaces[NS_USER] );
288 }
289 $this->assertFalse( MWNamespace::isContent( NS_USER ) );
290
291 if ( isset( $wgContentNamespaces[NS_CATEGORY] ) ) {
292 unset( $wgContentNamespaces[NS_CATEGORY] );
293 }
294 $this->assertFalse( MWNamespace::isContent( NS_CATEGORY ) );
295
296 if ( isset( $wgContentNamespaces[100] ) ) {
297 unset( $wgContentNamespaces[100] );
298 }
299 $this->assertFalse( MWNamespace::isContent( 100 ) );
300
301 $wgContentNamespaces = $saved;
302 }
303
304 /**
305 * Similar to testIsContent() but alters the $wgContentNamespaces
306 * global variable.
307 */
308 public function testIsContentWithAdditionsInWgContentNamespaces() {
309 // NS_MAIN is a content namespace per DefaultSettings.php
310 // and per function definition.
311 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
312
313 // Tests that user defined namespace #252 is not content:
314 $this->assertFalse( MWNamespace::isContent( 252 ) );
315
316 # @todo FIXME: Is global saving really required for PHPUnit?
317 // Bless namespace # 252 as a content namespace
318 global $wgContentNamespaces;
319 $savedGlobal = $wgContentNamespaces;
320 $wgContentNamespaces[] = 252;
321 $this->assertTrue( MWNamespace::isContent( 252 ) );
322
323 // Makes sure NS_MAIN was not impacted
324 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
325
326 // Restore global
327 $wgContentNamespaces = $savedGlobal;
328
329 // Verify namespaces after global restauration
330 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
331 $this->assertFalse( MWNamespace::isContent( 252 ) );
332 }
333
334 public function testIsWatchable() {
335 // Specials namespaces are not watchable
336 $this->assertFalse( MWNamespace::isWatchable( NS_MEDIA ) );
337 $this->assertFalse( MWNamespace::isWatchable( NS_SPECIAL ) );
338
339 // Core defined namespaces are watchables
340 $this->assertTrue( MWNamespace::isWatchable( NS_MAIN ) );
341 $this->assertTrue( MWNamespace::isWatchable( NS_TALK ) );
342
343 // Additional, user defined namespaces are watchables
344 $this->assertTrue( MWNamespace::isWatchable( 100 ) );
345 $this->assertTrue( MWNamespace::isWatchable( 101 ) );
346 }
347
348 public function testHasSubpages() {
349 // Special namespaces:
350 $this->assertFalse( MWNamespace::hasSubpages( NS_MEDIA ) );
351 $this->assertFalse( MWNamespace::hasSubpages( NS_SPECIAL ) );
352
353 // namespaces without subpages
354 # save up global
355 global $wgNamespacesWithSubpages;
356 $saved = null;
357 if( array_key_exists( NS_MAIN, $wgNamespacesWithSubpages ) ) {
358 $saved = $wgNamespacesWithSubpages[NS_MAIN];
359 unset( $wgNamespacesWithSubpages[NS_MAIN] );
360 }
361
362 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
363
364 $wgNamespacesWithSubpages[NS_MAIN] = true;
365 $this->assertTrue( MWNamespace::hasSubpages( NS_MAIN ) );
366 $wgNamespacesWithSubpages[NS_MAIN] = false;
367 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
368
369 # restore global
370 if( $saved !== null ) {
371 $wgNamespacesWithSubpages[NS_MAIN] = $saved;
372 }
373
374 // Some namespaces with subpages
375 $this->assertTrue( MWNamespace::hasSubpages( NS_TALK ) );
376 $this->assertTrue( MWNamespace::hasSubpages( NS_USER ) );
377 $this->assertTrue( MWNamespace::hasSubpages( NS_USER_TALK ) );
378 }
379
380 /**
381 */
382 public function testGetContentNamespaces() {
383 $this->assertEquals(
384 array( NS_MAIN ),
385 MWNamespace::getcontentNamespaces(),
386 '$wgContentNamespaces is an array with only NS_MAIN by default'
387 );
388
389 global $wgContentNamespaces;
390
391 # test !is_array( $wgcontentNamespaces )
392 $wgContentNamespaces = '';
393 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
394 $wgContentNamespaces = false;
395 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
396 $wgContentNamespaces = null;
397 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
398 $wgContentNamespaces = 5;
399 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
400
401 # test $wgContentNamespaces === array()
402 $wgContentNamespaces = array();
403 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
404
405 # test !in_array( NS_MAIN, $wgContentNamespaces )
406 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
407 $this->assertEquals(
408 array( NS_MAIN, NS_USER, NS_CATEGORY ),
409 MWNamespace::getcontentNamespaces(),
410 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
411 );
412
413 # test other cases, return $wgcontentNamespaces as is
414 $wgContentNamespaces = array( NS_MAIN );
415 $this->assertEquals(
416 array( NS_MAIN ),
417 MWNamespace::getcontentNamespaces()
418 );
419
420 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
421 $this->assertEquals(
422 array( NS_MAIN, NS_USER, NS_CATEGORY ),
423 MWNamespace::getcontentNamespaces()
424 );
425
426 }
427
428 /**
429 * Some namespaces are always capitalized per code definition
430 * in MWNamespace::$alwaysCapitalizedNamespaces
431 */
432 public function testIsCapitalizedHardcodedAssertions() {
433 // NS_MEDIA and NS_FILE are treated the same
434 $this->assertEquals(
435 MWNamespace::isCapitalized( NS_MEDIA ),
436 MWNamespace::isCapitalized( NS_FILE ),
437 'NS_MEDIA and NS_FILE have same capitalization rendering'
438 );
439
440 // Boths are capitalized by default
441 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIA ) );
442 $this->assertTrue( MWNamespace::isCapitalized( NS_FILE ) );
443
444 // Always capitalized namespaces
445 // @see MWNamespace::$alwaysCapitalizedNamespaces
446 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
447 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
448 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
449 }
450
451 /**
452 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
453 * global $wgCapitalLink setting to have extended coverage.
454 *
455 * MWNamespace::isCapitalized() rely on two global settings:
456 * $wgCapitalLinkOverrides = array(); by default
457 * $wgCapitalLinks = true; by default
458 * This function test $wgCapitalLinks
459 *
460 * Global setting correctness is tested against the NS_PROJECT and
461 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
462 */
463 public function testIsCapitalizedWithWgCapitalLinks() {
464 global $wgCapitalLinks;
465 // Save the global to easily reset to MediaWiki default settings
466 $savedGlobal = $wgCapitalLinks;
467
468 $wgCapitalLinks = true;
469 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
470 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
471
472 $wgCapitalLinks = false;
473 // hardcoded namespaces (see above function) are still capitalized:
474 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
475 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
476 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
477 // setting is correctly applied
478 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
479 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
480
481 // reset global state:
482 $wgCapitalLinks = $savedGlobal;
483 }
484
485 /**
486 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
487 * testing the $wgCapitalLinkOverrides global.
488 *
489 * @todo split groups of assertions in autonomous testing functions
490 */
491 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
492 global $wgCapitalLinkOverrides;
493 // Save the global to easily reset to MediaWiki default settings
494 $savedGlobal = $wgCapitalLinkOverrides;
495
496 // Test default settings
497 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
498 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
499 // hardcoded namespaces (see above function) are capitalized:
500 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
501 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
502 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
503
504 // Hardcoded namespaces remains capitalized
505 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
506 $wgCapitalLinkOverrides[NS_USER] = false;
507 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
508 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
509 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
510 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
511
512 $wgCapitalLinkOverrides = $savedGlobal;
513 $wgCapitalLinkOverrides[NS_PROJECT] = false;
514 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
515 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
516 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
517 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
518 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
519
520 // reset global state:
521 $wgCapitalLinkOverrides = $savedGlobal;
522 }
523
524 public function testHasGenderDistinction() {
525 // Namespaces with gender distinctions
526 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
527 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
528
529 // Other ones, "genderless"
530 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
531 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
532 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
533 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
534
535 }
536 }
537