Merge "Blacklist Google Glass web browser from JS"
[mediawiki.git] / tests / phpunit / includes / MWNamespaceTest.php
blobb6fa139fb168a3ff12ed105e9297b261c619b77a
1 <?php
2 /**
3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
5 * @file
6 */
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
15 class MWNamespaceTest extends MediaWikiTestCase {
16 protected function setUp() {
17 parent::setUp();
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,
26 'wgCapitalLinks' => true,
27 'wgCapitalLinkOverrides' => array(),
28 'wgNonincludableNamespaces' => array(),
29 ) );
32 #### START OF TESTS #########################################################
34 /**
35 * @todo Write more texts, handle $wgAllowImageMoving setting
36 * @covers MWNamespace::isMovable
38 public function testIsMovable() {
39 $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
40 # @todo FIXME: Write more tests!!
43 /**
44 * Please make sure to change testIsTalk() if you change the assertions below
45 * @covers MWNamespace::isSubject
47 public function testIsSubject() {
48 // Special namespaces
49 $this->assertIsSubject( NS_MEDIA );
50 $this->assertIsSubject( NS_SPECIAL );
52 // Subject pages
53 $this->assertIsSubject( NS_MAIN );
54 $this->assertIsSubject( NS_USER );
55 $this->assertIsSubject( 100 ); # user defined
57 // Talk pages
58 $this->assertIsNotSubject( NS_TALK );
59 $this->assertIsNotSubject( NS_USER_TALK );
60 $this->assertIsNotSubject( 101 ); # user defined
63 /**
64 * Reverse of testIsSubject().
65 * Please update testIsSubject() if you change assertions below
66 * @covers MWNamespace::isTalk
68 public function testIsTalk() {
69 // Special namespaces
70 $this->assertIsNotTalk( NS_MEDIA );
71 $this->assertIsNotTalk( NS_SPECIAL );
73 // Subject pages
74 $this->assertIsNotTalk( NS_MAIN );
75 $this->assertIsNotTalk( NS_USER );
76 $this->assertIsNotTalk( 100 ); # user defined
78 // Talk pages
79 $this->assertIsTalk( NS_TALK );
80 $this->assertIsTalk( NS_USER_TALK );
81 $this->assertIsTalk( 101 ); # user defined
84 /**
85 * @covers MWNamespace::getSubject
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 ) );
92 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
93 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
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
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 ) );
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
115 public function testGetTalkExceptionsForNsMedia() {
116 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
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
125 public function testGetTalkExceptionsForNsSpecial() {
126 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
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
135 public function testGetAssociated() {
136 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
137 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
140 ### Exceptions with getAssociated()
141 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
142 ### an exception for them.
144 * @expectedException MWException
145 * @covers MWNamespace::getAssociated
147 public function testGetAssociatedExceptionsForNsMedia() {
148 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
152 * @expectedException MWException
153 * @covers MWNamespace::getAssociated
155 public function testGetAssociatedExceptionsForNsSpecial() {
156 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
160 * @todo Implement testExists().
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.'
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
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 ) );
191 * @covers MWNamespace::subjectEquals
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 );
203 $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
204 $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN );
208 * @covers MWNamespace::subjectEquals
210 public function testSpecialAndMediaAreDifferentSubjects() {
211 $this->assertDifferentSubject(
212 NS_MEDIA, NS_SPECIAL,
213 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
215 $this->assertDifferentSubject(
216 NS_SPECIAL, NS_MEDIA,
217 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
222 * @todo Implement testGetCanonicalNamespaces().
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.'
233 * @todo Implement testGetCanonicalName().
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.'
244 * @todo Implement testGetCanonicalIndex().
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.'
256 * @todo Implement testGetValidNamespaces().
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.'
268 * @covers MWNamespace::canTalk
270 public function testCanTalk() {
271 $this->assertCanNotTalk( NS_MEDIA );
272 $this->assertCanNotTalk( NS_SPECIAL );
274 $this->assertCanTalk( NS_MAIN );
275 $this->assertCanTalk( NS_TALK );
276 $this->assertCanTalk( NS_USER );
277 $this->assertCanTalk( NS_USER_TALK );
279 // User defined namespaces
280 $this->assertCanTalk( 100 );
281 $this->assertCanTalk( 101 );
285 * @covers MWNamespace::isContent
287 public function testIsContent() {
288 // NS_MAIN is a content namespace per DefaultSettings.php
289 // and per function definition.
291 $this->assertIsContent( NS_MAIN );
293 // Other namespaces which are not expected to be content
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 );
304 * Similar to testIsContent() but alters the $wgContentNamespaces
305 * global variable.
306 * @covers MWNamespace::isContent
308 public function testIsContentAdvanced() {
309 global $wgContentNamespaces;
311 // Test that user defined namespace #252 is not content
312 $this->assertIsNotContent( 252 );
314 // Bless namespace # 252 as a content namespace
315 $wgContentNamespaces[] = 252;
317 $this->assertIsContent( 252 );
319 // Makes sure NS_MAIN was not impacted
320 $this->assertIsContent( NS_MAIN );
324 * @covers MWNamespace::isWatchable
326 public function testIsWatchable() {
327 // Specials namespaces are not watchable
328 $this->assertIsNotWatchable( NS_MEDIA );
329 $this->assertIsNotWatchable( NS_SPECIAL );
331 // Core defined namespaces are watchables
332 $this->assertIsWatchable( NS_MAIN );
333 $this->assertIsWatchable( NS_TALK );
335 // Additional, user defined namespaces are watchables
336 $this->assertIsWatchable( 100 );
337 $this->assertIsWatchable( 101 );
341 * @covers MWNamespace::hasSubpages
343 public function testHasSubpages() {
344 global $wgNamespacesWithSubpages;
346 // Special namespaces:
347 $this->assertHasNotSubpages( NS_MEDIA );
348 $this->assertHasNotSubpages( NS_SPECIAL );
350 // Namespaces without subpages
351 $this->assertHasNotSubpages( NS_MAIN );
353 $wgNamespacesWithSubpages[NS_MAIN] = true;
354 $this->assertHasSubpages( NS_MAIN );
356 $wgNamespacesWithSubpages[NS_MAIN] = false;
357 $this->assertHasNotSubpages( NS_MAIN );
359 // Some namespaces with subpages
360 $this->assertHasSubpages( NS_TALK );
361 $this->assertHasSubpages( NS_USER );
362 $this->assertHasSubpages( NS_USER_TALK );
366 * @covers MWNamespace::getContentNamespaces
368 public function testGetContentNamespaces() {
369 global $wgContentNamespaces;
371 $this->assertEquals(
372 array( NS_MAIN ),
373 MWNamespace::getContentNamespaces(),
374 '$wgContentNamespaces is an array with only NS_MAIN by default'
377 # test !is_array( $wgcontentNamespaces )
378 $wgContentNamespaces = '';
379 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
381 $wgContentNamespaces = false;
382 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
384 $wgContentNamespaces = null;
385 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
387 $wgContentNamespaces = 5;
388 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
390 # test $wgContentNamespaces === array()
391 $wgContentNamespaces = array();
392 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
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'
402 # test other cases, return $wgcontentNamespaces as is
403 $wgContentNamespaces = array( NS_MAIN );
404 $this->assertEquals(
405 array( NS_MAIN ),
406 MWNamespace::getContentNamespaces()
409 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
410 $this->assertEquals(
411 array( NS_MAIN, NS_USER, NS_CATEGORY ),
412 MWNamespace::getContentNamespaces()
417 * @covers MWNamespace::getSubjectNamespaces
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" );
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" );
433 * @covers MWNamespace::getTalkNamespaces
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" );
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" );
449 * Some namespaces are always capitalized per code definition
450 * in MWNamespace::$alwaysCapitalizedNamespaces
451 * @covers MWNamespace::isCapitalized
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'
461 // Boths are capitalized by default
462 $this->assertIsCapitalized( NS_MEDIA );
463 $this->assertIsCapitalized( NS_FILE );
465 // Always capitalized namespaces
466 // @see MWNamespace::$alwaysCapitalizedNamespaces
467 $this->assertIsCapitalized( NS_SPECIAL );
468 $this->assertIsCapitalized( NS_USER );
469 $this->assertIsCapitalized( NS_MEDIAWIKI );
473 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
474 * global $wgCapitalLink setting to have extended coverage.
476 * MWNamespace::isCapitalized() rely on two global settings:
477 * $wgCapitalLinkOverrides = array(); by default
478 * $wgCapitalLinks = true; by default
479 * This function test $wgCapitalLinks
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
485 public function testIsCapitalizedWithWgCapitalLinks() {
486 global $wgCapitalLinks;
488 $this->assertIsCapitalized( NS_PROJECT );
489 $this->assertIsCapitalized( NS_PROJECT_TALK );
491 $wgCapitalLinks = false;
493 // hardcoded namespaces (see above function) are still capitalized:
494 $this->assertIsCapitalized( NS_SPECIAL );
495 $this->assertIsCapitalized( NS_USER );
496 $this->assertIsCapitalized( NS_MEDIAWIKI );
498 // setting is correctly applied
499 $this->assertIsNotCapitalized( NS_PROJECT );
500 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
504 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
505 * testing the $wgCapitalLinkOverrides global.
507 * @todo split groups of assertions in autonomous testing functions
508 * @covers MWNamespace::isCapitalized
510 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
511 global $wgCapitalLinkOverrides;
513 // Test default settings
514 $this->assertIsCapitalized( NS_PROJECT );
515 $this->assertIsCapitalized( NS_PROJECT_TALK );
517 // hardcoded namespaces (see above function) are capitalized:
518 $this->assertIsCapitalized( NS_SPECIAL );
519 $this->assertIsCapitalized( NS_USER );
520 $this->assertIsCapitalized( NS_MEDIAWIKI );
522 // Hardcoded namespaces remains capitalized
523 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
524 $wgCapitalLinkOverrides[NS_USER] = false;
525 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
527 $this->assertIsCapitalized( NS_SPECIAL );
528 $this->assertIsCapitalized( NS_USER );
529 $this->assertIsCapitalized( NS_MEDIAWIKI );
531 $wgCapitalLinkOverrides[NS_PROJECT] = false;
532 $this->assertIsNotCapitalized( NS_PROJECT );
534 $wgCapitalLinkOverrides[NS_PROJECT] = true;
535 $this->assertIsCapitalized( NS_PROJECT );
537 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
538 $this->assertIsCapitalized( NS_PROJECT );
542 * @covers MWNamespace::hasGenderDistinction
544 public function testHasGenderDistinction() {
545 // Namespaces with gender distinctions
546 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
547 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
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 ) );
557 * @covers MWNamespace::isNonincludable
559 public function testIsNonincludable() {
560 global $wgNonincludableNamespaces;
562 $wgNonincludableNamespaces = array( NS_USER );
564 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
565 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
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 );
575 if ( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
576 # Interprets arguments:
577 $ns = $args[0];
578 $msg = isset( $args[1] ) ? $args[1] : " dummy message";
580 # Forge the namespace constant name:
581 if ( $ns === 0 ) {
582 $ns_name = "NS_MAIN";
583 } else {
584 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
586 # ... and the MWNamespace method name
587 $nsMethod = strtolower( $m[1] ) . $m[3];
589 $expect = ( $m[2] === '' );
590 $expect_name = $expect ? 'TRUE' : 'FALSE';
592 return $this->assertEquals( $expect,
593 MWNamespace::$nsMethod( $ns, $msg ),
594 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
598 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
601 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
602 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
605 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
606 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );