Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / MWNamespaceTest.php
blob092d57b3aae837fa148e3332294ae37710ca0d40
1 <?php
2 /**
3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
5 * @file
6 */
8 /**
9 * Test class for MWNamespace.
10 * @todo covers tags
11 * @todo FIXME: this test file is a mess
13 class MWNamespaceTest extends MediaWikiTestCase {
14 protected function setUp() {
15 parent::setUp();
17 $this->setMwGlobals( [
18 'wgContentNamespaces' => [ NS_MAIN ],
19 'wgNamespacesWithSubpages' => [
20 NS_TALK => true,
21 NS_USER => true,
22 NS_USER_TALK => true,
24 'wgCapitalLinks' => true,
25 'wgCapitalLinkOverrides' => [],
26 'wgNonincludableNamespaces' => [],
27 ] );
30 # ### START OF TESTS #########################################################
32 /**
33 * @todo Write more texts, handle $wgAllowImageMoving setting
34 * @covers MWNamespace::isMovable
36 public function testIsMovable() {
37 $this->assertFalse( MWNamespace::isMovable( NS_SPECIAL ) );
38 # @todo FIXME: Write more tests!!
41 /**
42 * Please make sure to change testIsTalk() if you change the assertions below
43 * @covers MWNamespace::isSubject
45 public function testIsSubject() {
46 // Special namespaces
47 $this->assertIsSubject( NS_MEDIA );
48 $this->assertIsSubject( NS_SPECIAL );
50 // Subject pages
51 $this->assertIsSubject( NS_MAIN );
52 $this->assertIsSubject( NS_USER );
53 $this->assertIsSubject( 100 ); # user defined
55 // Talk pages
56 $this->assertIsNotSubject( NS_TALK );
57 $this->assertIsNotSubject( NS_USER_TALK );
58 $this->assertIsNotSubject( 101 ); # user defined
61 /**
62 * Reverse of testIsSubject().
63 * Please update testIsSubject() if you change assertions below
64 * @covers MWNamespace::isTalk
66 public function testIsTalk() {
67 // Special namespaces
68 $this->assertIsNotTalk( NS_MEDIA );
69 $this->assertIsNotTalk( NS_SPECIAL );
71 // Subject pages
72 $this->assertIsNotTalk( NS_MAIN );
73 $this->assertIsNotTalk( NS_USER );
74 $this->assertIsNotTalk( 100 ); # user defined
76 // Talk pages
77 $this->assertIsTalk( NS_TALK );
78 $this->assertIsTalk( NS_USER_TALK );
79 $this->assertIsTalk( 101 ); # user defined
82 /**
83 * @covers MWNamespace::getSubject
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 ) );
90 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
91 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
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
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 ) );
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
113 public function testGetTalkExceptionsForNsMedia() {
114 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
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
123 public function testGetTalkExceptionsForNsSpecial() {
124 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
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
133 public function testGetAssociated() {
134 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
135 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
138 # ## Exceptions with getAssociated()
139 # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
140 # ## an exception for them.
142 * @expectedException MWException
143 * @covers MWNamespace::getAssociated
145 public function testGetAssociatedExceptionsForNsMedia() {
146 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
150 * @expectedException MWException
151 * @covers MWNamespace::getAssociated
153 public function testGetAssociatedExceptionsForNsSpecial() {
154 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
158 * @todo Implement testExists().
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.'
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
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 ) );
189 * @covers MWNamespace::subjectEquals
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 );
201 $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
202 $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN );
206 * @covers MWNamespace::subjectEquals
208 public function testSpecialAndMediaAreDifferentSubjects() {
209 $this->assertDifferentSubject(
210 NS_MEDIA, NS_SPECIAL,
211 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
213 $this->assertDifferentSubject(
214 NS_SPECIAL, NS_MEDIA,
215 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
220 * @todo Implement testGetCanonicalNamespaces().
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.'
231 * @todo Implement testGetCanonicalName().
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.'
242 * @todo Implement testGetCanonicalIndex().
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.'
254 * @todo Implement testGetValidNamespaces().
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.'
266 * @covers MWNamespace::canTalk
268 public function testCanTalk() {
269 $this->assertCanNotTalk( NS_MEDIA );
270 $this->assertCanNotTalk( NS_SPECIAL );
272 $this->assertCanTalk( NS_MAIN );
273 $this->assertCanTalk( NS_TALK );
274 $this->assertCanTalk( NS_USER );
275 $this->assertCanTalk( NS_USER_TALK );
277 // User defined namespaces
278 $this->assertCanTalk( 100 );
279 $this->assertCanTalk( 101 );
283 * @covers MWNamespace::isContent
285 public function testIsContent() {
286 // NS_MAIN is a content namespace per DefaultSettings.php
287 // and per function definition.
289 $this->assertIsContent( NS_MAIN );
291 // Other namespaces which are not expected to be content
293 $this->assertIsNotContent( NS_MEDIA );
294 $this->assertIsNotContent( NS_SPECIAL );
295 $this->assertIsNotContent( NS_TALK );
296 $this->assertIsNotContent( NS_USER );
297 $this->assertIsNotContent( NS_CATEGORY );
298 $this->assertIsNotContent( 100 );
302 * Similar to testIsContent() but alters the $wgContentNamespaces
303 * global variable.
304 * @covers MWNamespace::isContent
306 public function testIsContentAdvanced() {
307 global $wgContentNamespaces;
309 // Test that user defined namespace #252 is not content
310 $this->assertIsNotContent( 252 );
312 // Bless namespace # 252 as a content namespace
313 $wgContentNamespaces[] = 252;
315 $this->assertIsContent( 252 );
317 // Makes sure NS_MAIN was not impacted
318 $this->assertIsContent( NS_MAIN );
322 * @covers MWNamespace::isWatchable
324 public function testIsWatchable() {
325 // Specials namespaces are not watchable
326 $this->assertIsNotWatchable( NS_MEDIA );
327 $this->assertIsNotWatchable( NS_SPECIAL );
329 // Core defined namespaces are watchables
330 $this->assertIsWatchable( NS_MAIN );
331 $this->assertIsWatchable( NS_TALK );
333 // Additional, user defined namespaces are watchables
334 $this->assertIsWatchable( 100 );
335 $this->assertIsWatchable( 101 );
339 * @covers MWNamespace::hasSubpages
341 public function testHasSubpages() {
342 global $wgNamespacesWithSubpages;
344 // Special namespaces:
345 $this->assertHasNotSubpages( NS_MEDIA );
346 $this->assertHasNotSubpages( NS_SPECIAL );
348 // Namespaces without subpages
349 $this->assertHasNotSubpages( NS_MAIN );
351 $wgNamespacesWithSubpages[NS_MAIN] = true;
352 $this->assertHasSubpages( NS_MAIN );
354 $wgNamespacesWithSubpages[NS_MAIN] = false;
355 $this->assertHasNotSubpages( NS_MAIN );
357 // Some namespaces with subpages
358 $this->assertHasSubpages( NS_TALK );
359 $this->assertHasSubpages( NS_USER );
360 $this->assertHasSubpages( NS_USER_TALK );
364 * @covers MWNamespace::getContentNamespaces
366 public function testGetContentNamespaces() {
367 global $wgContentNamespaces;
369 $this->assertEquals(
370 [ NS_MAIN ],
371 MWNamespace::getContentNamespaces(),
372 '$wgContentNamespaces is an array with only NS_MAIN by default'
375 # test !is_array( $wgcontentNamespaces )
376 $wgContentNamespaces = '';
377 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
379 $wgContentNamespaces = false;
380 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
382 $wgContentNamespaces = null;
383 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
385 $wgContentNamespaces = 5;
386 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
388 # test $wgContentNamespaces === []
389 $wgContentNamespaces = [];
390 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
392 # test !in_array( NS_MAIN, $wgContentNamespaces )
393 $wgContentNamespaces = [ NS_USER, NS_CATEGORY ];
394 $this->assertEquals(
395 [ NS_MAIN, NS_USER, NS_CATEGORY ],
396 MWNamespace::getContentNamespaces(),
397 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
400 # test other cases, return $wgcontentNamespaces as is
401 $wgContentNamespaces = [ NS_MAIN ];
402 $this->assertEquals(
403 [ NS_MAIN ],
404 MWNamespace::getContentNamespaces()
407 $wgContentNamespaces = [ NS_MAIN, NS_USER, NS_CATEGORY ];
408 $this->assertEquals(
409 [ NS_MAIN, NS_USER, NS_CATEGORY ],
410 MWNamespace::getContentNamespaces()
415 * @covers MWNamespace::getSubjectNamespaces
417 public function testGetSubjectNamespaces() {
418 $subjectsNS = MWNamespace::getSubjectNamespaces();
419 $this->assertContains( NS_MAIN, $subjectsNS,
420 "Talk namespaces should have NS_MAIN" );
421 $this->assertNotContains( NS_TALK, $subjectsNS,
422 "Talk namespaces should have NS_TALK" );
424 $this->assertNotContains( NS_MEDIA, $subjectsNS,
425 "Talk namespaces should not have NS_MEDIA" );
426 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
427 "Talk namespaces should not have NS_SPECIAL" );
431 * @covers MWNamespace::getTalkNamespaces
433 public function testGetTalkNamespaces() {
434 $talkNS = MWNamespace::getTalkNamespaces();
435 $this->assertContains( NS_TALK, $talkNS,
436 "Subject namespaces should have NS_TALK" );
437 $this->assertNotContains( NS_MAIN, $talkNS,
438 "Subject namespaces should not have NS_MAIN" );
440 $this->assertNotContains( NS_MEDIA, $talkNS,
441 "Subject namespaces should not have NS_MEDIA" );
442 $this->assertNotContains( NS_SPECIAL, $talkNS,
443 "Subject namespaces should not have NS_SPECIAL" );
447 * Some namespaces are always capitalized per code definition
448 * in MWNamespace::$alwaysCapitalizedNamespaces
449 * @covers MWNamespace::isCapitalized
451 public function testIsCapitalizedHardcodedAssertions() {
452 // NS_MEDIA and NS_FILE are treated the same
453 $this->assertEquals(
454 MWNamespace::isCapitalized( NS_MEDIA ),
455 MWNamespace::isCapitalized( NS_FILE ),
456 'NS_MEDIA and NS_FILE have same capitalization rendering'
459 // Boths are capitalized by default
460 $this->assertIsCapitalized( NS_MEDIA );
461 $this->assertIsCapitalized( NS_FILE );
463 // Always capitalized namespaces
464 // @see MWNamespace::$alwaysCapitalizedNamespaces
465 $this->assertIsCapitalized( NS_SPECIAL );
466 $this->assertIsCapitalized( NS_USER );
467 $this->assertIsCapitalized( NS_MEDIAWIKI );
471 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
472 * global $wgCapitalLink setting to have extended coverage.
474 * MWNamespace::isCapitalized() rely on two global settings:
475 * $wgCapitalLinkOverrides = []; by default
476 * $wgCapitalLinks = true; by default
477 * This function test $wgCapitalLinks
479 * Global setting correctness is tested against the NS_PROJECT and
480 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
481 * @covers MWNamespace::isCapitalized
483 public function testIsCapitalizedWithWgCapitalLinks() {
484 global $wgCapitalLinks;
486 $this->assertIsCapitalized( NS_PROJECT );
487 $this->assertIsCapitalized( NS_PROJECT_TALK );
489 $wgCapitalLinks = false;
491 // hardcoded namespaces (see above function) are still capitalized:
492 $this->assertIsCapitalized( NS_SPECIAL );
493 $this->assertIsCapitalized( NS_USER );
494 $this->assertIsCapitalized( NS_MEDIAWIKI );
496 // setting is correctly applied
497 $this->assertIsNotCapitalized( NS_PROJECT );
498 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
502 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
503 * testing the $wgCapitalLinkOverrides global.
505 * @todo split groups of assertions in autonomous testing functions
506 * @covers MWNamespace::isCapitalized
508 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
509 global $wgCapitalLinkOverrides;
511 // Test default settings
512 $this->assertIsCapitalized( NS_PROJECT );
513 $this->assertIsCapitalized( NS_PROJECT_TALK );
515 // hardcoded namespaces (see above function) are capitalized:
516 $this->assertIsCapitalized( NS_SPECIAL );
517 $this->assertIsCapitalized( NS_USER );
518 $this->assertIsCapitalized( NS_MEDIAWIKI );
520 // Hardcoded namespaces remains capitalized
521 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
522 $wgCapitalLinkOverrides[NS_USER] = false;
523 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
525 $this->assertIsCapitalized( NS_SPECIAL );
526 $this->assertIsCapitalized( NS_USER );
527 $this->assertIsCapitalized( NS_MEDIAWIKI );
529 $wgCapitalLinkOverrides[NS_PROJECT] = false;
530 $this->assertIsNotCapitalized( NS_PROJECT );
532 $wgCapitalLinkOverrides[NS_PROJECT] = true;
533 $this->assertIsCapitalized( NS_PROJECT );
535 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
536 $this->assertIsCapitalized( NS_PROJECT );
540 * @covers MWNamespace::hasGenderDistinction
542 public function testHasGenderDistinction() {
543 // Namespaces with gender distinctions
544 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
545 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
547 // Other ones, "genderless"
548 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
549 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
550 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
551 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
555 * @covers MWNamespace::isNonincludable
557 public function testIsNonincludable() {
558 global $wgNonincludableNamespaces;
560 $wgNonincludableNamespaces = [ NS_USER ];
562 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
563 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
566 # ###### HELPERS ###########################################################
567 function __call( $method, $args ) {
568 // Call the real method if it exists
569 if ( method_exists( $this, $method ) ) {
570 return $this->$method( $args );
573 if ( preg_match(
574 '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/',
575 $method,
577 ) ) {
578 # Interprets arguments:
579 $ns = $args[0];
580 $msg = isset( $args[1] ) ? $args[1] : " dummy message";
582 # Forge the namespace constant name:
583 if ( $ns === 0 ) {
584 $ns_name = "NS_MAIN";
585 } else {
586 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
588 # ... and the MWNamespace method name
589 $nsMethod = strtolower( $m[1] ) . $m[3];
591 $expect = ( $m[2] === '' );
592 $expect_name = $expect ? 'TRUE' : 'FALSE';
594 return $this->assertEquals( $expect,
595 MWNamespace::$nsMethod( $ns, $msg ),
596 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
600 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
603 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
604 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
607 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
608 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );