Allow moving category pages
[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 * @todo covers tags
12 * @FIXME this test file is a mess
13 *
14 */
15 class MWNamespaceTest extends MediaWikiTestCase {
16 protected function setUp() {
17 parent::setUp();
18
19 $this->setMwGlobals( array(
20 'wgContentNamespaces' => array( NS_MAIN ),
21 'wgNamespacesWithSubpages' => array(
22 NS_TALK => true,
23 NS_USER => true,
24 NS_USER_TALK => true,
25 ),
26 'wgCapitalLinks' => true,
27 'wgCapitalLinkOverrides' => array(),
28 'wgNonincludableNamespaces' => array(),
29 ) );
30 }
31
32 #### START OF TESTS #########################################################
33
34 /**
35 * @todo Write more texts, handle $wgAllowImageMoving setting
36 * @covers MWNamespace::isMovable
37 */
38 public function testIsMovable() {
39 $this->assertFalse( MWNamespace::isMovable( NS_SPECIAL ) );
40 # @todo FIXME: Write more tests!!
41 }
42
43 /**
44 * Please make sure to change testIsTalk() if you change the assertions below
45 * @covers MWNamespace::isSubject
46 */
47 public function testIsSubject() {
48 // Special namespaces
49 $this->assertIsSubject( NS_MEDIA );
50 $this->assertIsSubject( NS_SPECIAL );
51
52 // Subject pages
53 $this->assertIsSubject( NS_MAIN );
54 $this->assertIsSubject( NS_USER );
55 $this->assertIsSubject( 100 ); # user defined
56
57 // Talk pages
58 $this->assertIsNotSubject( NS_TALK );
59 $this->assertIsNotSubject( NS_USER_TALK );
60 $this->assertIsNotSubject( 101 ); # user defined
61 }
62
63 /**
64 * Reverse of testIsSubject().
65 * Please update testIsSubject() if you change assertions below
66 * @covers MWNamespace::isTalk
67 */
68 public function testIsTalk() {
69 // Special namespaces
70 $this->assertIsNotTalk( NS_MEDIA );
71 $this->assertIsNotTalk( NS_SPECIAL );
72
73 // Subject pages
74 $this->assertIsNotTalk( NS_MAIN );
75 $this->assertIsNotTalk( NS_USER );
76 $this->assertIsNotTalk( 100 ); # user defined
77
78 // Talk pages
79 $this->assertIsTalk( NS_TALK );
80 $this->assertIsTalk( NS_USER_TALK );
81 $this->assertIsTalk( 101 ); # user defined
82 }
83
84 /**
85 * @covers MWNamespace::getSubject
86 */
87 public function testGetSubject() {
88 // Special namespaces are their own subjects
89 $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
90 $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
91
92 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
93 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
94 }
95
96 /**
97 * Regular getTalk() calls
98 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
99 * the function testGetTalkExceptions()
100 * @covers MWNamespace::getTalk
101 */
102 public function testGetTalk() {
103 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
104 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) );
105 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) );
106 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) );
107 }
108
109 /**
110 * Exceptions with getTalk()
111 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
112 * @expectedException MWException
113 * @covers MWNamespace::getTalk
114 */
115 public function testGetTalkExceptionsForNsMedia() {
116 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
117 }
118
119 /**
120 * Exceptions with getTalk()
121 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
122 * @expectedException MWException
123 * @covers MWNamespace::getTalk
124 */
125 public function testGetTalkExceptionsForNsSpecial() {
126 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
127 }
128
129 /**
130 * Regular getAssociated() calls
131 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
132 * the function testGetAssociatedExceptions()
133 * @covers MWNamespace::getAssociated
134 */
135 public function testGetAssociated() {
136 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
137 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
138 }
139
140 ### Exceptions with getAssociated()
141 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
142 ### an exception for them.
143 /**
144 * @expectedException MWException
145 * @covers MWNamespace::getAssociated
146 */
147 public function testGetAssociatedExceptionsForNsMedia() {
148 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
149 }
150
151 /**
152 * @expectedException MWException
153 * @covers MWNamespace::getAssociated
154 */
155 public function testGetAssociatedExceptionsForNsSpecial() {
156 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
157 }
158
159 /**
160 * @todo Implement testExists().
161 */
162 /*
163 public function testExists() {
164 // Remove the following lines when you implement this test.
165 $this->markTestIncomplete(
166 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
167 );
168 }
169 */
170
171 /**
172 * Test MWNamespace::equals
173 * Note if we add a namespace registration system with keys like 'MAIN'
174 * we should add tests here for equivilance on things like 'MAIN' == 0
175 * and 'MAIN' == NS_MAIN.
176 * @covers MWNamespace::equals
177 */
178 public function testEquals() {
179 $this->assertTrue( MWNamespace::equals( NS_MAIN, NS_MAIN ) );
180 $this->assertTrue( MWNamespace::equals( NS_MAIN, 0 ) ); // In case we make NS_MAIN 'MAIN'
181 $this->assertTrue( MWNamespace::equals( NS_USER, NS_USER ) );
182 $this->assertTrue( MWNamespace::equals( NS_USER, 2 ) );
183 $this->assertTrue( MWNamespace::equals( NS_USER_TALK, NS_USER_TALK ) );
184 $this->assertTrue( MWNamespace::equals( NS_SPECIAL, NS_SPECIAL ) );
185 $this->assertFalse( MWNamespace::equals( NS_MAIN, NS_TALK ) );
186 $this->assertFalse( MWNamespace::equals( NS_USER, NS_USER_TALK ) );
187 $this->assertFalse( MWNamespace::equals( NS_PROJECT, NS_TEMPLATE ) );
188 }
189
190 /**
191 * @covers MWNamespace::subjectEquals
192 */
193 public function testSubjectEquals() {
194 $this->assertSameSubject( NS_MAIN, NS_MAIN );
195 $this->assertSameSubject( NS_MAIN, 0 ); // In case we make NS_MAIN 'MAIN'
196 $this->assertSameSubject( NS_USER, NS_USER );
197 $this->assertSameSubject( NS_USER, 2 );
198 $this->assertSameSubject( NS_USER_TALK, NS_USER_TALK );
199 $this->assertSameSubject( NS_SPECIAL, NS_SPECIAL );
200 $this->assertSameSubject( NS_MAIN, NS_TALK );
201 $this->assertSameSubject( NS_USER, NS_USER_TALK );
202
203 $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
204 $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN );
205 }
206
207 /**
208 * @covers MWNamespace::subjectEquals
209 */
210 public function testSpecialAndMediaAreDifferentSubjects() {
211 $this->assertDifferentSubject(
212 NS_MEDIA, NS_SPECIAL,
213 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
214 );
215 $this->assertDifferentSubject(
216 NS_SPECIAL, NS_MEDIA,
217 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
218 );
219 }
220
221 /**
222 * @todo Implement testGetCanonicalNamespaces().
223 */
224 /*
225 public function testGetCanonicalNamespaces() {
226 // Remove the following lines when you implement this test.
227 $this->markTestIncomplete(
228 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
229 );
230 }
231 */
232 /**
233 * @todo Implement testGetCanonicalName().
234 */
235 /*
236 public function testGetCanonicalName() {
237 // Remove the following lines when you implement this test.
238 $this->markTestIncomplete(
239 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
240 );
241 }
242 */
243 /**
244 * @todo Implement testGetCanonicalIndex().
245 */
246 /*
247 public function testGetCanonicalIndex() {
248 // Remove the following lines when you implement this test.
249 $this->markTestIncomplete(
250 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
251 );
252 }
253 */
254
255 /**
256 * @todo Implement testGetValidNamespaces().
257 */
258 /*
259 public function testGetValidNamespaces() {
260 // Remove the following lines when you implement this test.
261 $this->markTestIncomplete(
262 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
263 );
264 }
265 */
266
267 /**
268 * @covers MWNamespace::canTalk
269 */
270 public function testCanTalk() {
271 $this->assertCanNotTalk( NS_MEDIA );
272 $this->assertCanNotTalk( NS_SPECIAL );
273
274 $this->assertCanTalk( NS_MAIN );
275 $this->assertCanTalk( NS_TALK );
276 $this->assertCanTalk( NS_USER );
277 $this->assertCanTalk( NS_USER_TALK );
278
279 // User defined namespaces
280 $this->assertCanTalk( 100 );
281 $this->assertCanTalk( 101 );
282 }
283
284 /**
285 * @covers MWNamespace::isContent
286 */
287 public function testIsContent() {
288 // NS_MAIN is a content namespace per DefaultSettings.php
289 // and per function definition.
290
291 $this->assertIsContent( NS_MAIN );
292
293 // Other namespaces which are not expected to be content
294
295 $this->assertIsNotContent( NS_MEDIA );
296 $this->assertIsNotContent( NS_SPECIAL );
297 $this->assertIsNotContent( NS_TALK );
298 $this->assertIsNotContent( NS_USER );
299 $this->assertIsNotContent( NS_CATEGORY );
300 $this->assertIsNotContent( 100 );
301 }
302
303 /**
304 * Similar to testIsContent() but alters the $wgContentNamespaces
305 * global variable.
306 * @covers MWNamespace::isContent
307 */
308 public function testIsContentAdvanced() {
309 global $wgContentNamespaces;
310
311 // Test that user defined namespace #252 is not content
312 $this->assertIsNotContent( 252 );
313
314 // Bless namespace # 252 as a content namespace
315 $wgContentNamespaces[] = 252;
316
317 $this->assertIsContent( 252 );
318
319 // Makes sure NS_MAIN was not impacted
320 $this->assertIsContent( NS_MAIN );
321 }
322
323 /**
324 * @covers MWNamespace::isWatchable
325 */
326 public function testIsWatchable() {
327 // Specials namespaces are not watchable
328 $this->assertIsNotWatchable( NS_MEDIA );
329 $this->assertIsNotWatchable( NS_SPECIAL );
330
331 // Core defined namespaces are watchables
332 $this->assertIsWatchable( NS_MAIN );
333 $this->assertIsWatchable( NS_TALK );
334
335 // Additional, user defined namespaces are watchables
336 $this->assertIsWatchable( 100 );
337 $this->assertIsWatchable( 101 );
338 }
339
340 /**
341 * @covers MWNamespace::hasSubpages
342 */
343 public function testHasSubpages() {
344 global $wgNamespacesWithSubpages;
345
346 // Special namespaces:
347 $this->assertHasNotSubpages( NS_MEDIA );
348 $this->assertHasNotSubpages( NS_SPECIAL );
349
350 // Namespaces without subpages
351 $this->assertHasNotSubpages( NS_MAIN );
352
353 $wgNamespacesWithSubpages[NS_MAIN] = true;
354 $this->assertHasSubpages( NS_MAIN );
355
356 $wgNamespacesWithSubpages[NS_MAIN] = false;
357 $this->assertHasNotSubpages( NS_MAIN );
358
359 // Some namespaces with subpages
360 $this->assertHasSubpages( NS_TALK );
361 $this->assertHasSubpages( NS_USER );
362 $this->assertHasSubpages( NS_USER_TALK );
363 }
364
365 /**
366 * @covers MWNamespace::getContentNamespaces
367 */
368 public function testGetContentNamespaces() {
369 global $wgContentNamespaces;
370
371 $this->assertEquals(
372 array( NS_MAIN ),
373 MWNamespace::getContentNamespaces(),
374 '$wgContentNamespaces is an array with only NS_MAIN by default'
375 );
376
377 # test !is_array( $wgcontentNamespaces )
378 $wgContentNamespaces = '';
379 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
380
381 $wgContentNamespaces = false;
382 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
383
384 $wgContentNamespaces = null;
385 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
386
387 $wgContentNamespaces = 5;
388 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
389
390 # test $wgContentNamespaces === array()
391 $wgContentNamespaces = array();
392 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
393
394 # test !in_array( NS_MAIN, $wgContentNamespaces )
395 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
396 $this->assertEquals(
397 array( NS_MAIN, NS_USER, NS_CATEGORY ),
398 MWNamespace::getContentNamespaces(),
399 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
400 );
401
402 # test other cases, return $wgcontentNamespaces as is
403 $wgContentNamespaces = array( NS_MAIN );
404 $this->assertEquals(
405 array( NS_MAIN ),
406 MWNamespace::getContentNamespaces()
407 );
408
409 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
410 $this->assertEquals(
411 array( NS_MAIN, NS_USER, NS_CATEGORY ),
412 MWNamespace::getContentNamespaces()
413 );
414 }
415
416 /**
417 * @covers MWNamespace::getSubjectNamespaces
418 */
419 public function testGetSubjectNamespaces() {
420 $subjectsNS = MWNamespace::getSubjectNamespaces();
421 $this->assertContains( NS_MAIN, $subjectsNS,
422 "Talk namespaces should have NS_MAIN" );
423 $this->assertNotContains( NS_TALK, $subjectsNS,
424 "Talk namespaces should have NS_TALK" );
425
426 $this->assertNotContains( NS_MEDIA, $subjectsNS,
427 "Talk namespaces should not have NS_MEDIA" );
428 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
429 "Talk namespaces should not have NS_SPECIAL" );
430 }
431
432 /**
433 * @covers MWNamespace::getTalkNamespaces
434 */
435 public function testGetTalkNamespaces() {
436 $talkNS = MWNamespace::getTalkNamespaces();
437 $this->assertContains( NS_TALK, $talkNS,
438 "Subject namespaces should have NS_TALK" );
439 $this->assertNotContains( NS_MAIN, $talkNS,
440 "Subject namespaces should not have NS_MAIN" );
441
442 $this->assertNotContains( NS_MEDIA, $talkNS,
443 "Subject namespaces should not have NS_MEDIA" );
444 $this->assertNotContains( NS_SPECIAL, $talkNS,
445 "Subject namespaces should not have NS_SPECIAL" );
446 }
447
448 /**
449 * Some namespaces are always capitalized per code definition
450 * in MWNamespace::$alwaysCapitalizedNamespaces
451 * @covers MWNamespace::isCapitalized
452 */
453 public function testIsCapitalizedHardcodedAssertions() {
454 // NS_MEDIA and NS_FILE are treated the same
455 $this->assertEquals(
456 MWNamespace::isCapitalized( NS_MEDIA ),
457 MWNamespace::isCapitalized( NS_FILE ),
458 'NS_MEDIA and NS_FILE have same capitalization rendering'
459 );
460
461 // Boths are capitalized by default
462 $this->assertIsCapitalized( NS_MEDIA );
463 $this->assertIsCapitalized( NS_FILE );
464
465 // Always capitalized namespaces
466 // @see MWNamespace::$alwaysCapitalizedNamespaces
467 $this->assertIsCapitalized( NS_SPECIAL );
468 $this->assertIsCapitalized( NS_USER );
469 $this->assertIsCapitalized( NS_MEDIAWIKI );
470 }
471
472 /**
473 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
474 * global $wgCapitalLink setting to have extended coverage.
475 *
476 * MWNamespace::isCapitalized() rely on two global settings:
477 * $wgCapitalLinkOverrides = array(); by default
478 * $wgCapitalLinks = true; by default
479 * This function test $wgCapitalLinks
480 *
481 * Global setting correctness is tested against the NS_PROJECT and
482 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
483 * @covers MWNamespace::isCapitalized
484 */
485 public function testIsCapitalizedWithWgCapitalLinks() {
486 global $wgCapitalLinks;
487
488 $this->assertIsCapitalized( NS_PROJECT );
489 $this->assertIsCapitalized( NS_PROJECT_TALK );
490
491 $wgCapitalLinks = false;
492
493 // hardcoded namespaces (see above function) are still capitalized:
494 $this->assertIsCapitalized( NS_SPECIAL );
495 $this->assertIsCapitalized( NS_USER );
496 $this->assertIsCapitalized( NS_MEDIAWIKI );
497
498 // setting is correctly applied
499 $this->assertIsNotCapitalized( NS_PROJECT );
500 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
501 }
502
503 /**
504 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
505 * testing the $wgCapitalLinkOverrides global.
506 *
507 * @todo split groups of assertions in autonomous testing functions
508 * @covers MWNamespace::isCapitalized
509 */
510 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
511 global $wgCapitalLinkOverrides;
512
513 // Test default settings
514 $this->assertIsCapitalized( NS_PROJECT );
515 $this->assertIsCapitalized( NS_PROJECT_TALK );
516
517 // hardcoded namespaces (see above function) are capitalized:
518 $this->assertIsCapitalized( NS_SPECIAL );
519 $this->assertIsCapitalized( NS_USER );
520 $this->assertIsCapitalized( NS_MEDIAWIKI );
521
522 // Hardcoded namespaces remains capitalized
523 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
524 $wgCapitalLinkOverrides[NS_USER] = false;
525 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
526
527 $this->assertIsCapitalized( NS_SPECIAL );
528 $this->assertIsCapitalized( NS_USER );
529 $this->assertIsCapitalized( NS_MEDIAWIKI );
530
531 $wgCapitalLinkOverrides[NS_PROJECT] = false;
532 $this->assertIsNotCapitalized( NS_PROJECT );
533
534 $wgCapitalLinkOverrides[NS_PROJECT] = true;
535 $this->assertIsCapitalized( NS_PROJECT );
536
537 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
538 $this->assertIsCapitalized( NS_PROJECT );
539 }
540
541 /**
542 * @covers MWNamespace::hasGenderDistinction
543 */
544 public function testHasGenderDistinction() {
545 // Namespaces with gender distinctions
546 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
547 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
548
549 // Other ones, "genderless"
550 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
551 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
552 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
553 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
554 }
555
556 /**
557 * @covers MWNamespace::isNonincludable
558 */
559 public function testIsNonincludable() {
560 global $wgNonincludableNamespaces;
561
562 $wgNonincludableNamespaces = array( NS_USER );
563
564 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
565 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
566 }
567
568 ####### HELPERS ###########################################################
569 function __call( $method, $args ) {
570 // Call the real method if it exists
571 if ( method_exists( $this, $method ) ) {
572 return $this->$method( $args );
573 }
574
575 if ( preg_match(
576 '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/',
577 $method,
578 $m
579 ) ) {
580 # Interprets arguments:
581 $ns = $args[0];
582 $msg = isset( $args[1] ) ? $args[1] : " dummy message";
583
584 # Forge the namespace constant name:
585 if ( $ns === 0 ) {
586 $ns_name = "NS_MAIN";
587 } else {
588 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
589 }
590 # ... and the MWNamespace method name
591 $nsMethod = strtolower( $m[1] ) . $m[3];
592
593 $expect = ( $m[2] === '' );
594 $expect_name = $expect ? 'TRUE' : 'FALSE';
595
596 return $this->assertEquals( $expect,
597 MWNamespace::$nsMethod( $ns, $msg ),
598 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
599 );
600 }
601
602 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
603 }
604
605 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
606 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
607 }
608
609 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
610 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
611 }
612 }