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