use MWNamespaceTest helpers based on __call()
[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->assertIsSubject( NS_MEDIA );
45 $this->assertIsSubject( NS_SPECIAL );
46
47 // Subject pages
48 $this->assertIsSubject( NS_MAIN );
49 $this->assertIsSubject( NS_USER );
50 $this->assertIsSubject( 100 ); # user defined
51
52 // Talk pages
53 $this->assertIsNotSubject( NS_TALK );
54 $this->assertIsNotSubject( NS_USER_TALK );
55 $this->assertIsNotSubject( 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->assertIsNotTalk( NS_MEDIA );
69 $this->assertIsNotTalk( NS_SPECIAL );
70
71 // Subject pages
72 $this->assertIsNotTalk( NS_MAIN );
73 $this->assertIsNotTalk( NS_USER );
74 $this->assertIsNotTalk( 100 ); # user defined
75
76 // Talk pages
77 $this->assertIsTalk( NS_TALK );
78 $this->assertIsTalk( NS_USER_TALK );
79 $this->assertIsTalk( 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->assertCanNotTalk( NS_MEDIA );
244 $this->assertCanNotTalk( NS_SPECIAL );
245
246 $this->assertCanTalk( NS_MAIN );
247 $this->assertCanTalk( NS_TALK );
248 $this->assertCanTalk( NS_USER );
249 $this->assertCanTalk( NS_USER_TALK );
250
251 // User defined namespaces
252 $this->assertCanTalk( 100 );
253 $this->assertCanTalk( 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->assertIsContent( NS_MAIN );
262
263 global $wgContentNamespaces;
264
265 $saved = $wgContentNamespaces;
266
267 $wgContentNamespaces[] = NS_MAIN;
268 $this->assertIsContent( 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->assertIsNotContent( NS_MEDIA );
275
276 if ( isset( $wgContentNamespaces[NS_SPECIAL] ) ) {
277 unset( $wgContentNamespaces[NS_SPECIAL] );
278 }
279 $this->assertIsNotContent( NS_SPECIAL );
280
281 if ( isset( $wgContentNamespaces[NS_TALK] ) ) {
282 unset( $wgContentNamespaces[NS_TALK] );
283 }
284 $this->assertIsNotContent( NS_TALK );
285
286 if ( isset( $wgContentNamespaces[NS_USER] ) ) {
287 unset( $wgContentNamespaces[NS_USER] );
288 }
289 $this->assertIsNotContent( NS_USER );
290
291 if ( isset( $wgContentNamespaces[NS_CATEGORY] ) ) {
292 unset( $wgContentNamespaces[NS_CATEGORY] );
293 }
294 $this->assertIsNotContent( NS_CATEGORY );
295
296 if ( isset( $wgContentNamespaces[100] ) ) {
297 unset( $wgContentNamespaces[100] );
298 }
299 $this->assertIsNotContent( 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->assertIsContent( NS_MAIN );
312
313 // Tests that user defined namespace #252 is not content:
314 $this->assertIsNotContent( 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->assertIsContent( 252 );
322
323 // Makes sure NS_MAIN was not impacted
324 $this->assertIsContent( NS_MAIN );
325
326 // Restore global
327 $wgContentNamespaces = $savedGlobal;
328
329 // Verify namespaces after global restauration
330 $this->assertIsContent( NS_MAIN );
331 $this->assertIsNotContent( 252 );
332 }
333
334 public function testIsWatchable() {
335 // Specials namespaces are not watchable
336 $this->assertIsNotWatchable( NS_MEDIA );
337 $this->assertIsNotWatchable( NS_SPECIAL );
338
339 // Core defined namespaces are watchables
340 $this->assertIsWatchable( NS_MAIN );
341 $this->assertIsWatchable( NS_TALK );
342
343 // Additional, user defined namespaces are watchables
344 $this->assertIsWatchable( 100 );
345 $this->assertIsWatchable( 101 );
346 }
347
348 public function testHasSubpages() {
349 // Special namespaces:
350 $this->assertHasNotSubpages( NS_MEDIA );
351 $this->assertHasNotSubpages( 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->assertHasNotSubpages( NS_MAIN );
363
364 $wgNamespacesWithSubpages[NS_MAIN] = true;
365 $this->assertHasSubpages( NS_MAIN );
366 $wgNamespacesWithSubpages[NS_MAIN] = false;
367 $this->assertHasNotSubpages( NS_MAIN );
368
369 # restore global
370 if( $saved !== null ) {
371 $wgNamespacesWithSubpages[NS_MAIN] = $saved;
372 }
373
374 // Some namespaces with subpages
375 $this->assertHasSubpages( NS_TALK );
376 $this->assertHasSubpages( NS_USER );
377 $this->assertHasSubpages( 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 $saved = $wgContentNamespaces;
392 # test !is_array( $wgcontentNamespaces )
393 $wgContentNamespaces = '';
394 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
395 $wgContentNamespaces = false;
396 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
397 $wgContentNamespaces = null;
398 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
399 $wgContentNamespaces = 5;
400 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
401
402 # test $wgContentNamespaces === array()
403 $wgContentNamespaces = array();
404 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
405
406 # test !in_array( NS_MAIN, $wgContentNamespaces )
407 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
408 $this->assertEquals(
409 array( NS_MAIN, NS_USER, NS_CATEGORY ),
410 MWNamespace::getcontentNamespaces(),
411 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
412 );
413
414 # test other cases, return $wgcontentNamespaces as is
415 $wgContentNamespaces = array( NS_MAIN );
416 $this->assertEquals(
417 array( NS_MAIN ),
418 MWNamespace::getcontentNamespaces()
419 );
420
421 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
422 $this->assertEquals(
423 array( NS_MAIN, NS_USER, NS_CATEGORY ),
424 MWNamespace::getcontentNamespaces()
425 );
426
427 $wgContentNamespaces = $saved;
428 }
429
430 /**
431 * Some namespaces are always capitalized per code definition
432 * in MWNamespace::$alwaysCapitalizedNamespaces
433 */
434 public function testIsCapitalizedHardcodedAssertions() {
435 // NS_MEDIA and NS_FILE are treated the same
436 $this->assertEquals(
437 MWNamespace::isCapitalized( NS_MEDIA ),
438 MWNamespace::isCapitalized( NS_FILE ),
439 'NS_MEDIA and NS_FILE have same capitalization rendering'
440 );
441
442 // Boths are capitalized by default
443 $this->assertIsCapitalized( NS_MEDIA );
444 $this->assertIsCapitalized( NS_FILE );
445
446 // Always capitalized namespaces
447 // @see MWNamespace::$alwaysCapitalizedNamespaces
448 $this->assertIsCapitalized( NS_SPECIAL );
449 $this->assertIsCapitalized( NS_USER );
450 $this->assertIsCapitalized( NS_MEDIAWIKI );
451 }
452
453 /**
454 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
455 * global $wgCapitalLink setting to have extended coverage.
456 *
457 * MWNamespace::isCapitalized() rely on two global settings:
458 * $wgCapitalLinkOverrides = array(); by default
459 * $wgCapitalLinks = true; by default
460 * This function test $wgCapitalLinks
461 *
462 * Global setting correctness is tested against the NS_PROJECT and
463 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
464 */
465 public function testIsCapitalizedWithWgCapitalLinks() {
466 global $wgCapitalLinks;
467 // Save the global to easily reset to MediaWiki default settings
468 $savedGlobal = $wgCapitalLinks;
469
470 $wgCapitalLinks = true;
471 $this->assertIsCapitalized( NS_PROJECT );
472 $this->assertIsCapitalized( NS_PROJECT_TALK );
473
474 $wgCapitalLinks = false;
475 // hardcoded namespaces (see above function) are still capitalized:
476 $this->assertIsCapitalized( NS_SPECIAL );
477 $this->assertIsCapitalized( NS_USER );
478 $this->assertIsCapitalized( NS_MEDIAWIKI );
479 // setting is correctly applied
480 $this->assertIsNotCapitalized( NS_PROJECT );
481 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
482
483 // reset global state:
484 $wgCapitalLinks = $savedGlobal;
485 }
486
487 /**
488 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
489 * testing the $wgCapitalLinkOverrides global.
490 *
491 * @todo split groups of assertions in autonomous testing functions
492 */
493 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
494 global $wgCapitalLinkOverrides;
495 // Save the global to easily reset to MediaWiki default settings
496 $savedGlobal = $wgCapitalLinkOverrides;
497
498 // Test default settings
499 $this->assertIsCapitalized( NS_PROJECT );
500 $this->assertIsCapitalized( NS_PROJECT_TALK );
501 // hardcoded namespaces (see above function) are capitalized:
502 $this->assertIsCapitalized( NS_SPECIAL );
503 $this->assertIsCapitalized( NS_USER );
504 $this->assertIsCapitalized( NS_MEDIAWIKI );
505
506 // Hardcoded namespaces remains capitalized
507 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
508 $wgCapitalLinkOverrides[NS_USER] = false;
509 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
510 $this->assertIsCapitalized( NS_SPECIAL );
511 $this->assertIsCapitalized( NS_USER );
512 $this->assertIsCapitalized( NS_MEDIAWIKI );
513
514 $wgCapitalLinkOverrides = $savedGlobal;
515 $wgCapitalLinkOverrides[NS_PROJECT] = false;
516 $this->assertIsNotCapitalized( NS_PROJECT );
517 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
518 $this->assertIsCapitalized( NS_PROJECT );
519 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
520 $this->assertIsCapitalized( NS_PROJECT );
521
522 // reset global state:
523 $wgCapitalLinkOverrides = $savedGlobal;
524 }
525
526 public function testHasGenderDistinction() {
527 // Namespaces with gender distinctions
528 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
529 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
530
531 // Other ones, "genderless"
532 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
533 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
534 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
535 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
536
537 }
538
539 ####### HELPERS ###########################################################
540 function __call( $method, $args ) {
541 // Call the real method if it exists
542 if( method_exists($this, $method ) ) {
543 return $this->$method( $args );
544 }
545
546 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
547 # Interprets arguments:
548 $ns = $args[0];
549 $msg = isset($args[1]) ? $args[1] : " dummy message";
550
551 # Forge the namespace constant name:
552 if( $ns === 0 ) {
553 $ns_name = "NS_MAIN";
554 } else {
555 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
556 }
557 # ... and the MWNamespace method name
558 $nsMethod = strtolower( $m[1] ) . $m[3];
559
560 $expect = ($m[2] === '');
561 $expect_name = $expect ? 'TRUE' : 'FALSE';
562
563 return $this->assertEquals( $expect,
564 MWNamespace::$nsMethod( $ns, $msg ),
565 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
566 );
567 }
568
569 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
570 }
571
572 }
573