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