add some namespaces equality tests
[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 $this->assertFalse( MWNamespace::subjectEquals( NS_SPECIAL, NS_MAIN ) );
195 }
196
197 public function testSpecialAndMediaAreDifferentSubjects() {
198 $this->assertFalse( MWNamespace::subjectEquals(
199 NS_MEDIA, NS_SPECIAL
200 ), "NS_MEDIA and NS_SPECIAL are different subhects" );
201 $this->assertFalse( MWNamespace::subjectEquals(
202 NS_SPECIAL, NS_MEDIA
203 ), "NS_SPECIAL and NS_MEDIA are different subhects" );
204
205 }
206
207 /**
208 * @todo Implement testGetCanonicalNamespaces().
209 */
210 /*
211 public function testGetCanonicalNamespaces() {
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 testGetCanonicalName().
220 */
221 /*
222 public function testGetCanonicalName() {
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 testGetCanonicalIndex().
231 */
232 /*
233 public function testGetCanonicalIndex() {
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 * @todo Implement testGetValidNamespaces().
242 */
243 /*
244 public function testGetValidNamespaces() {
245 // Remove the following lines when you implement this test.
246 $this->markTestIncomplete(
247 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
248 );
249 }
250 */
251 /**
252 */
253 public function testCanTalk() {
254 $this->assertCanNotTalk( NS_MEDIA );
255 $this->assertCanNotTalk( NS_SPECIAL );
256
257 $this->assertCanTalk( NS_MAIN );
258 $this->assertCanTalk( NS_TALK );
259 $this->assertCanTalk( NS_USER );
260 $this->assertCanTalk( NS_USER_TALK );
261
262 // User defined namespaces
263 $this->assertCanTalk( 100 );
264 $this->assertCanTalk( 101 );
265 }
266
267 /**
268 */
269 public function testIsContent() {
270 // NS_MAIN is a content namespace per DefaultSettings.php
271 // and per function definition.
272 $this->assertIsContent( NS_MAIN );
273
274 global $wgContentNamespaces;
275
276 $saved = $wgContentNamespaces;
277
278 $wgContentNamespaces[] = NS_MAIN;
279 $this->assertIsContent( NS_MAIN );
280
281 // Other namespaces which are not expected to be content
282 if ( isset( $wgContentNamespaces[NS_MEDIA] ) ) {
283 unset( $wgContentNamespaces[NS_MEDIA] );
284 }
285 $this->assertIsNotContent( NS_MEDIA );
286
287 if ( isset( $wgContentNamespaces[NS_SPECIAL] ) ) {
288 unset( $wgContentNamespaces[NS_SPECIAL] );
289 }
290 $this->assertIsNotContent( NS_SPECIAL );
291
292 if ( isset( $wgContentNamespaces[NS_TALK] ) ) {
293 unset( $wgContentNamespaces[NS_TALK] );
294 }
295 $this->assertIsNotContent( NS_TALK );
296
297 if ( isset( $wgContentNamespaces[NS_USER] ) ) {
298 unset( $wgContentNamespaces[NS_USER] );
299 }
300 $this->assertIsNotContent( NS_USER );
301
302 if ( isset( $wgContentNamespaces[NS_CATEGORY] ) ) {
303 unset( $wgContentNamespaces[NS_CATEGORY] );
304 }
305 $this->assertIsNotContent( NS_CATEGORY );
306
307 if ( isset( $wgContentNamespaces[100] ) ) {
308 unset( $wgContentNamespaces[100] );
309 }
310 $this->assertIsNotContent( 100 );
311
312 $wgContentNamespaces = $saved;
313 }
314
315 /**
316 * Similar to testIsContent() but alters the $wgContentNamespaces
317 * global variable.
318 */
319 public function testIsContentWithAdditionsInWgContentNamespaces() {
320 // NS_MAIN is a content namespace per DefaultSettings.php
321 // and per function definition.
322 $this->assertIsContent( NS_MAIN );
323
324 // Tests that user defined namespace #252 is not content:
325 $this->assertIsNotContent( 252 );
326
327 # @todo FIXME: Is global saving really required for PHPUnit?
328 // Bless namespace # 252 as a content namespace
329 global $wgContentNamespaces;
330 $savedGlobal = $wgContentNamespaces;
331 $wgContentNamespaces[] = 252;
332 $this->assertIsContent( 252 );
333
334 // Makes sure NS_MAIN was not impacted
335 $this->assertIsContent( NS_MAIN );
336
337 // Restore global
338 $wgContentNamespaces = $savedGlobal;
339
340 // Verify namespaces after global restauration
341 $this->assertIsContent( NS_MAIN );
342 $this->assertIsNotContent( 252 );
343 }
344
345 public function testIsWatchable() {
346 // Specials namespaces are not watchable
347 $this->assertIsNotWatchable( NS_MEDIA );
348 $this->assertIsNotWatchable( NS_SPECIAL );
349
350 // Core defined namespaces are watchables
351 $this->assertIsWatchable( NS_MAIN );
352 $this->assertIsWatchable( NS_TALK );
353
354 // Additional, user defined namespaces are watchables
355 $this->assertIsWatchable( 100 );
356 $this->assertIsWatchable( 101 );
357 }
358
359 public function testHasSubpages() {
360 // Special namespaces:
361 $this->assertHasNotSubpages( NS_MEDIA );
362 $this->assertHasNotSubpages( NS_SPECIAL );
363
364 // namespaces without subpages
365 # save up global
366 global $wgNamespacesWithSubpages;
367 $saved = null;
368 if( array_key_exists( NS_MAIN, $wgNamespacesWithSubpages ) ) {
369 $saved = $wgNamespacesWithSubpages[NS_MAIN];
370 unset( $wgNamespacesWithSubpages[NS_MAIN] );
371 }
372
373 $this->assertHasNotSubpages( NS_MAIN );
374
375 $wgNamespacesWithSubpages[NS_MAIN] = true;
376 $this->assertHasSubpages( NS_MAIN );
377 $wgNamespacesWithSubpages[NS_MAIN] = false;
378 $this->assertHasNotSubpages( NS_MAIN );
379
380 # restore global
381 if( $saved !== null ) {
382 $wgNamespacesWithSubpages[NS_MAIN] = $saved;
383 }
384
385 // Some namespaces with subpages
386 $this->assertHasSubpages( NS_TALK );
387 $this->assertHasSubpages( NS_USER );
388 $this->assertHasSubpages( NS_USER_TALK );
389 }
390
391 /**
392 */
393 public function testGetContentNamespaces() {
394 $this->assertEquals(
395 array( NS_MAIN ),
396 MWNamespace::getcontentNamespaces(),
397 '$wgContentNamespaces is an array with only NS_MAIN by default'
398 );
399
400 global $wgContentNamespaces;
401
402 $saved = $wgContentNamespaces;
403 # test !is_array( $wgcontentNamespaces )
404 $wgContentNamespaces = '';
405 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
406 $wgContentNamespaces = false;
407 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
408 $wgContentNamespaces = null;
409 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
410 $wgContentNamespaces = 5;
411 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
412
413 # test $wgContentNamespaces === array()
414 $wgContentNamespaces = array();
415 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
416
417 # test !in_array( NS_MAIN, $wgContentNamespaces )
418 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
419 $this->assertEquals(
420 array( NS_MAIN, NS_USER, NS_CATEGORY ),
421 MWNamespace::getcontentNamespaces(),
422 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
423 );
424
425 # test other cases, return $wgcontentNamespaces as is
426 $wgContentNamespaces = array( NS_MAIN );
427 $this->assertEquals(
428 array( NS_MAIN ),
429 MWNamespace::getcontentNamespaces()
430 );
431
432 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
433 $this->assertEquals(
434 array( NS_MAIN, NS_USER, NS_CATEGORY ),
435 MWNamespace::getcontentNamespaces()
436 );
437
438 $wgContentNamespaces = $saved;
439 }
440
441 /**
442 * Some namespaces are always capitalized per code definition
443 * in MWNamespace::$alwaysCapitalizedNamespaces
444 */
445 public function testIsCapitalizedHardcodedAssertions() {
446 // NS_MEDIA and NS_FILE are treated the same
447 $this->assertEquals(
448 MWNamespace::isCapitalized( NS_MEDIA ),
449 MWNamespace::isCapitalized( NS_FILE ),
450 'NS_MEDIA and NS_FILE have same capitalization rendering'
451 );
452
453 // Boths are capitalized by default
454 $this->assertIsCapitalized( NS_MEDIA );
455 $this->assertIsCapitalized( NS_FILE );
456
457 // Always capitalized namespaces
458 // @see MWNamespace::$alwaysCapitalizedNamespaces
459 $this->assertIsCapitalized( NS_SPECIAL );
460 $this->assertIsCapitalized( NS_USER );
461 $this->assertIsCapitalized( NS_MEDIAWIKI );
462 }
463
464 /**
465 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
466 * global $wgCapitalLink setting to have extended coverage.
467 *
468 * MWNamespace::isCapitalized() rely on two global settings:
469 * $wgCapitalLinkOverrides = array(); by default
470 * $wgCapitalLinks = true; by default
471 * This function test $wgCapitalLinks
472 *
473 * Global setting correctness is tested against the NS_PROJECT and
474 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
475 */
476 public function testIsCapitalizedWithWgCapitalLinks() {
477 global $wgCapitalLinks;
478 // Save the global to easily reset to MediaWiki default settings
479 $savedGlobal = $wgCapitalLinks;
480
481 $wgCapitalLinks = true;
482 $this->assertIsCapitalized( NS_PROJECT );
483 $this->assertIsCapitalized( NS_PROJECT_TALK );
484
485 $wgCapitalLinks = false;
486 // hardcoded namespaces (see above function) are still capitalized:
487 $this->assertIsCapitalized( NS_SPECIAL );
488 $this->assertIsCapitalized( NS_USER );
489 $this->assertIsCapitalized( NS_MEDIAWIKI );
490 // setting is correctly applied
491 $this->assertIsNotCapitalized( NS_PROJECT );
492 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
493
494 // reset global state:
495 $wgCapitalLinks = $savedGlobal;
496 }
497
498 /**
499 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
500 * testing the $wgCapitalLinkOverrides global.
501 *
502 * @todo split groups of assertions in autonomous testing functions
503 */
504 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
505 global $wgCapitalLinkOverrides;
506 // Save the global to easily reset to MediaWiki default settings
507 $savedGlobal = $wgCapitalLinkOverrides;
508
509 // Test default settings
510 $this->assertIsCapitalized( NS_PROJECT );
511 $this->assertIsCapitalized( NS_PROJECT_TALK );
512 // hardcoded namespaces (see above function) are capitalized:
513 $this->assertIsCapitalized( NS_SPECIAL );
514 $this->assertIsCapitalized( NS_USER );
515 $this->assertIsCapitalized( NS_MEDIAWIKI );
516
517 // Hardcoded namespaces remains capitalized
518 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
519 $wgCapitalLinkOverrides[NS_USER] = false;
520 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
521 $this->assertIsCapitalized( NS_SPECIAL );
522 $this->assertIsCapitalized( NS_USER );
523 $this->assertIsCapitalized( NS_MEDIAWIKI );
524
525 $wgCapitalLinkOverrides = $savedGlobal;
526 $wgCapitalLinkOverrides[NS_PROJECT] = false;
527 $this->assertIsNotCapitalized( NS_PROJECT );
528 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
529 $this->assertIsCapitalized( NS_PROJECT );
530 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
531 $this->assertIsCapitalized( NS_PROJECT );
532
533 // reset global state:
534 $wgCapitalLinkOverrides = $savedGlobal;
535 }
536
537 public function testHasGenderDistinction() {
538 // Namespaces with gender distinctions
539 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
540 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
541
542 // Other ones, "genderless"
543 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
544 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
545 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
546 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
547
548 }
549
550 ####### HELPERS ###########################################################
551 function __call( $method, $args ) {
552 // Call the real method if it exists
553 if( method_exists($this, $method ) ) {
554 return $this->$method( $args );
555 }
556
557 if( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
558 # Interprets arguments:
559 $ns = $args[0];
560 $msg = isset($args[1]) ? $args[1] : " dummy message";
561
562 # Forge the namespace constant name:
563 if( $ns === 0 ) {
564 $ns_name = "NS_MAIN";
565 } else {
566 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
567 }
568 # ... and the MWNamespace method name
569 $nsMethod = strtolower( $m[1] ) . $m[3];
570
571 $expect = ($m[2] === '');
572 $expect_name = $expect ? 'TRUE' : 'FALSE';
573
574 return $this->assertEquals( $expect,
575 MWNamespace::$nsMethod( $ns, $msg ),
576 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
577 );
578 }
579
580 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
581 }
582
583 }
584