3 * @author Antoine Musso
4 * @copyright Copyright © 2011, Antoine Musso
8 class MWNamespaceTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgContentNamespaces' => [ NS_MAIN
],
15 'wgNamespacesWithSubpages' => [
20 'wgCapitalLinks' => true,
21 'wgCapitalLinkOverrides' => [],
22 'wgNonincludableNamespaces' => [],
27 * @todo Write more texts, handle $wgAllowImageMoving setting
28 * @covers MWNamespace::isMovable
30 public function testIsMovable() {
31 $this->assertFalse( MWNamespace
::isMovable( NS_SPECIAL
) );
35 * Please make sure to change testIsTalk() if you change the assertions below
36 * @covers MWNamespace::isSubject
38 public function testIsSubject() {
40 $this->assertIsSubject( NS_MEDIA
);
41 $this->assertIsSubject( NS_SPECIAL
);
44 $this->assertIsSubject( NS_MAIN
);
45 $this->assertIsSubject( NS_USER
);
46 $this->assertIsSubject( 100 ); # user defined
49 $this->assertIsNotSubject( NS_TALK
);
50 $this->assertIsNotSubject( NS_USER_TALK
);
51 $this->assertIsNotSubject( 101 ); # user defined
55 * Reverse of testIsSubject().
56 * Please update testIsSubject() if you change assertions below
57 * @covers MWNamespace::isTalk
59 public function testIsTalk() {
61 $this->assertIsNotTalk( NS_MEDIA
);
62 $this->assertIsNotTalk( NS_SPECIAL
);
65 $this->assertIsNotTalk( NS_MAIN
);
66 $this->assertIsNotTalk( NS_USER
);
67 $this->assertIsNotTalk( 100 ); # user defined
70 $this->assertIsTalk( NS_TALK
);
71 $this->assertIsTalk( NS_USER_TALK
);
72 $this->assertIsTalk( 101 ); # user defined
76 * @covers MWNamespace::getSubject
78 public function testGetSubject() {
79 // Special namespaces are their own subjects
80 $this->assertEquals( NS_MEDIA
, MWNamespace
::getSubject( NS_MEDIA
) );
81 $this->assertEquals( NS_SPECIAL
, MWNamespace
::getSubject( NS_SPECIAL
) );
83 $this->assertEquals( NS_MAIN
, MWNamespace
::getSubject( NS_TALK
) );
84 $this->assertEquals( NS_USER
, MWNamespace
::getSubject( NS_USER_TALK
) );
88 * Regular getTalk() calls
89 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
90 * the function testGetTalkExceptions()
91 * @covers MWNamespace::getTalk
93 public function testGetTalk() {
94 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_MAIN
) );
95 $this->assertEquals( NS_TALK
, MWNamespace
::getTalk( NS_TALK
) );
96 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER
) );
97 $this->assertEquals( NS_USER_TALK
, MWNamespace
::getTalk( NS_USER_TALK
) );
101 * Exceptions with getTalk()
102 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
103 * @expectedException MWException
104 * @covers MWNamespace::getTalk
106 public function testGetTalkExceptionsForNsMedia() {
107 $this->assertNull( MWNamespace
::getTalk( NS_MEDIA
) );
111 * Exceptions with getTalk()
112 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
113 * @expectedException MWException
114 * @covers MWNamespace::getTalk
116 public function testGetTalkExceptionsForNsSpecial() {
117 $this->assertNull( MWNamespace
::getTalk( NS_SPECIAL
) );
121 * Regular getAssociated() calls
122 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
123 * the function testGetAssociatedExceptions()
124 * @covers MWNamespace::getAssociated
126 public function testGetAssociated() {
127 $this->assertEquals( NS_TALK
, MWNamespace
::getAssociated( NS_MAIN
) );
128 $this->assertEquals( NS_MAIN
, MWNamespace
::getAssociated( NS_TALK
) );
131 # ## Exceptions with getAssociated()
132 # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
133 # ## an exception for them.
135 * @expectedException MWException
136 * @covers MWNamespace::getAssociated
138 public function testGetAssociatedExceptionsForNsMedia() {
139 $this->assertNull( MWNamespace
::getAssociated( NS_MEDIA
) );
143 * @expectedException MWException
144 * @covers MWNamespace::getAssociated
146 public function testGetAssociatedExceptionsForNsSpecial() {
147 $this->assertNull( MWNamespace
::getAssociated( NS_SPECIAL
) );
151 * Test MWNamespace::equals
152 * Note if we add a namespace registration system with keys like 'MAIN'
153 * we should add tests here for equivilance on things like 'MAIN' == 0
154 * and 'MAIN' == NS_MAIN.
155 * @covers MWNamespace::equals
157 public function testEquals() {
158 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, NS_MAIN
) );
159 $this->assertTrue( MWNamespace
::equals( NS_MAIN
, 0 ) ); // In case we make NS_MAIN 'MAIN'
160 $this->assertTrue( MWNamespace
::equals( NS_USER
, NS_USER
) );
161 $this->assertTrue( MWNamespace
::equals( NS_USER
, 2 ) );
162 $this->assertTrue( MWNamespace
::equals( NS_USER_TALK
, NS_USER_TALK
) );
163 $this->assertTrue( MWNamespace
::equals( NS_SPECIAL
, NS_SPECIAL
) );
164 $this->assertFalse( MWNamespace
::equals( NS_MAIN
, NS_TALK
) );
165 $this->assertFalse( MWNamespace
::equals( NS_USER
, NS_USER_TALK
) );
166 $this->assertFalse( MWNamespace
::equals( NS_PROJECT
, NS_TEMPLATE
) );
170 * @covers MWNamespace::subjectEquals
172 public function testSubjectEquals() {
173 $this->assertSameSubject( NS_MAIN
, NS_MAIN
);
174 $this->assertSameSubject( NS_MAIN
, 0 ); // In case we make NS_MAIN 'MAIN'
175 $this->assertSameSubject( NS_USER
, NS_USER
);
176 $this->assertSameSubject( NS_USER
, 2 );
177 $this->assertSameSubject( NS_USER_TALK
, NS_USER_TALK
);
178 $this->assertSameSubject( NS_SPECIAL
, NS_SPECIAL
);
179 $this->assertSameSubject( NS_MAIN
, NS_TALK
);
180 $this->assertSameSubject( NS_USER
, NS_USER_TALK
);
182 $this->assertDifferentSubject( NS_PROJECT
, NS_TEMPLATE
);
183 $this->assertDifferentSubject( NS_SPECIAL
, NS_MAIN
);
187 * @covers MWNamespace::subjectEquals
189 public function testSpecialAndMediaAreDifferentSubjects() {
190 $this->assertDifferentSubject(
191 NS_MEDIA
, NS_SPECIAL
,
192 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
194 $this->assertDifferentSubject(
195 NS_SPECIAL
, NS_MEDIA
,
196 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
200 public function provideHasTalkNamespace() {
203 [ NS_SPECIAL
, false ],
208 [ NS_USER_TALK
, true ],
216 * @dataProvider provideHasTalkNamespace
217 * @covers MWNamespace::hasTalkNamespace
220 * @param bool $expected
222 public function testHasTalkNamespace( $index, $expected ) {
223 $actual = MWNamespace
::hasTalkNamespace( $index );
224 $this->assertSame( $actual, $expected, "NS $index" );
228 * @dataProvider provideHasTalkNamespace
229 * @covers MWNamespace::canTalk
232 * @param bool $expected
234 public function testCanTalk( $index, $expected ) {
235 $actual = MWNamespace
::canTalk( $index );
236 $this->assertSame( $actual, $expected, "NS $index" );
240 * @covers MWNamespace::isContent
242 public function testIsContent() {
243 // NS_MAIN is a content namespace per DefaultSettings.php
244 // and per function definition.
246 $this->assertIsContent( NS_MAIN
);
248 // Other namespaces which are not expected to be content
250 $this->assertIsNotContent( NS_MEDIA
);
251 $this->assertIsNotContent( NS_SPECIAL
);
252 $this->assertIsNotContent( NS_TALK
);
253 $this->assertIsNotContent( NS_USER
);
254 $this->assertIsNotContent( NS_CATEGORY
);
255 $this->assertIsNotContent( 100 );
259 * Similar to testIsContent() but alters the $wgContentNamespaces
261 * @covers MWNamespace::isContent
263 public function testIsContentAdvanced() {
264 global $wgContentNamespaces;
266 // Test that user defined namespace #252 is not content
267 $this->assertIsNotContent( 252 );
269 // Bless namespace # 252 as a content namespace
270 $wgContentNamespaces[] = 252;
272 $this->assertIsContent( 252 );
274 // Makes sure NS_MAIN was not impacted
275 $this->assertIsContent( NS_MAIN
);
279 * @covers MWNamespace::isWatchable
281 public function testIsWatchable() {
282 // Specials namespaces are not watchable
283 $this->assertIsNotWatchable( NS_MEDIA
);
284 $this->assertIsNotWatchable( NS_SPECIAL
);
286 // Core defined namespaces are watchables
287 $this->assertIsWatchable( NS_MAIN
);
288 $this->assertIsWatchable( NS_TALK
);
290 // Additional, user defined namespaces are watchables
291 $this->assertIsWatchable( 100 );
292 $this->assertIsWatchable( 101 );
296 * @covers MWNamespace::hasSubpages
298 public function testHasSubpages() {
299 global $wgNamespacesWithSubpages;
301 // Special namespaces:
302 $this->assertHasNotSubpages( NS_MEDIA
);
303 $this->assertHasNotSubpages( NS_SPECIAL
);
305 // Namespaces without subpages
306 $this->assertHasNotSubpages( NS_MAIN
);
308 $wgNamespacesWithSubpages[NS_MAIN
] = true;
309 $this->assertHasSubpages( NS_MAIN
);
311 $wgNamespacesWithSubpages[NS_MAIN
] = false;
312 $this->assertHasNotSubpages( NS_MAIN
);
314 // Some namespaces with subpages
315 $this->assertHasSubpages( NS_TALK
);
316 $this->assertHasSubpages( NS_USER
);
317 $this->assertHasSubpages( NS_USER_TALK
);
321 * @covers MWNamespace::getContentNamespaces
323 public function testGetContentNamespaces() {
324 global $wgContentNamespaces;
328 MWNamespace
::getContentNamespaces(),
329 '$wgContentNamespaces is an array with only NS_MAIN by default'
332 # test !is_array( $wgcontentNamespaces )
333 $wgContentNamespaces = '';
334 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
336 $wgContentNamespaces = false;
337 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
339 $wgContentNamespaces = null;
340 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
342 $wgContentNamespaces = 5;
343 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
345 # test $wgContentNamespaces === []
346 $wgContentNamespaces = [];
347 $this->assertEquals( [ NS_MAIN
], MWNamespace
::getContentNamespaces() );
349 # test !in_array( NS_MAIN, $wgContentNamespaces )
350 $wgContentNamespaces = [ NS_USER
, NS_CATEGORY
];
352 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
353 MWNamespace
::getContentNamespaces(),
354 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
357 # test other cases, return $wgcontentNamespaces as is
358 $wgContentNamespaces = [ NS_MAIN
];
361 MWNamespace
::getContentNamespaces()
364 $wgContentNamespaces = [ NS_MAIN
, NS_USER
, NS_CATEGORY
];
366 [ NS_MAIN
, NS_USER
, NS_CATEGORY
],
367 MWNamespace
::getContentNamespaces()
372 * @covers MWNamespace::getSubjectNamespaces
374 public function testGetSubjectNamespaces() {
375 $subjectsNS = MWNamespace
::getSubjectNamespaces();
376 $this->assertContains( NS_MAIN
, $subjectsNS,
377 "Talk namespaces should have NS_MAIN" );
378 $this->assertNotContains( NS_TALK
, $subjectsNS,
379 "Talk namespaces should have NS_TALK" );
381 $this->assertNotContains( NS_MEDIA
, $subjectsNS,
382 "Talk namespaces should not have NS_MEDIA" );
383 $this->assertNotContains( NS_SPECIAL
, $subjectsNS,
384 "Talk namespaces should not have NS_SPECIAL" );
388 * @covers MWNamespace::getTalkNamespaces
390 public function testGetTalkNamespaces() {
391 $talkNS = MWNamespace
::getTalkNamespaces();
392 $this->assertContains( NS_TALK
, $talkNS,
393 "Subject namespaces should have NS_TALK" );
394 $this->assertNotContains( NS_MAIN
, $talkNS,
395 "Subject namespaces should not have NS_MAIN" );
397 $this->assertNotContains( NS_MEDIA
, $talkNS,
398 "Subject namespaces should not have NS_MEDIA" );
399 $this->assertNotContains( NS_SPECIAL
, $talkNS,
400 "Subject namespaces should not have NS_SPECIAL" );
404 * Some namespaces are always capitalized per code definition
405 * in MWNamespace::$alwaysCapitalizedNamespaces
406 * @covers MWNamespace::isCapitalized
408 public function testIsCapitalizedHardcodedAssertions() {
409 // NS_MEDIA and NS_FILE are treated the same
411 MWNamespace
::isCapitalized( NS_MEDIA
),
412 MWNamespace
::isCapitalized( NS_FILE
),
413 'NS_MEDIA and NS_FILE have same capitalization rendering'
416 // Boths are capitalized by default
417 $this->assertIsCapitalized( NS_MEDIA
);
418 $this->assertIsCapitalized( NS_FILE
);
420 // Always capitalized namespaces
421 // @see MWNamespace::$alwaysCapitalizedNamespaces
422 $this->assertIsCapitalized( NS_SPECIAL
);
423 $this->assertIsCapitalized( NS_USER
);
424 $this->assertIsCapitalized( NS_MEDIAWIKI
);
428 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
429 * global $wgCapitalLink setting to have extended coverage.
431 * MWNamespace::isCapitalized() rely on two global settings:
432 * $wgCapitalLinkOverrides = []; by default
433 * $wgCapitalLinks = true; by default
434 * This function test $wgCapitalLinks
436 * Global setting correctness is tested against the NS_PROJECT and
437 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
438 * @covers MWNamespace::isCapitalized
440 public function testIsCapitalizedWithWgCapitalLinks() {
441 global $wgCapitalLinks;
443 $this->assertIsCapitalized( NS_PROJECT
);
444 $this->assertIsCapitalized( NS_PROJECT_TALK
);
446 $wgCapitalLinks = false;
448 // hardcoded namespaces (see above function) are still capitalized:
449 $this->assertIsCapitalized( NS_SPECIAL
);
450 $this->assertIsCapitalized( NS_USER
);
451 $this->assertIsCapitalized( NS_MEDIAWIKI
);
453 // setting is correctly applied
454 $this->assertIsNotCapitalized( NS_PROJECT
);
455 $this->assertIsNotCapitalized( NS_PROJECT_TALK
);
459 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
460 * testing the $wgCapitalLinkOverrides global.
462 * @todo split groups of assertions in autonomous testing functions
463 * @covers MWNamespace::isCapitalized
465 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
466 global $wgCapitalLinkOverrides;
468 // Test default settings
469 $this->assertIsCapitalized( NS_PROJECT
);
470 $this->assertIsCapitalized( NS_PROJECT_TALK
);
472 // hardcoded namespaces (see above function) are capitalized:
473 $this->assertIsCapitalized( NS_SPECIAL
);
474 $this->assertIsCapitalized( NS_USER
);
475 $this->assertIsCapitalized( NS_MEDIAWIKI
);
477 // Hardcoded namespaces remains capitalized
478 $wgCapitalLinkOverrides[NS_SPECIAL
] = false;
479 $wgCapitalLinkOverrides[NS_USER
] = false;
480 $wgCapitalLinkOverrides[NS_MEDIAWIKI
] = false;
482 $this->assertIsCapitalized( NS_SPECIAL
);
483 $this->assertIsCapitalized( NS_USER
);
484 $this->assertIsCapitalized( NS_MEDIAWIKI
);
486 $wgCapitalLinkOverrides[NS_PROJECT
] = false;
487 $this->assertIsNotCapitalized( NS_PROJECT
);
489 $wgCapitalLinkOverrides[NS_PROJECT
] = true;
490 $this->assertIsCapitalized( NS_PROJECT
);
492 unset( $wgCapitalLinkOverrides[NS_PROJECT
] );
493 $this->assertIsCapitalized( NS_PROJECT
);
497 * @covers MWNamespace::hasGenderDistinction
499 public function testHasGenderDistinction() {
500 // Namespaces with gender distinctions
501 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER
) );
502 $this->assertTrue( MWNamespace
::hasGenderDistinction( NS_USER_TALK
) );
504 // Other ones, "genderless"
505 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MEDIA
) );
506 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_SPECIAL
) );
507 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_MAIN
) );
508 $this->assertFalse( MWNamespace
::hasGenderDistinction( NS_TALK
) );
512 * @covers MWNamespace::isNonincludable
514 public function testIsNonincludable() {
515 global $wgNonincludableNamespaces;
517 $wgNonincludableNamespaces = [ NS_USER
];
519 $this->assertTrue( MWNamespace
::isNonincludable( NS_USER
) );
520 $this->assertFalse( MWNamespace
::isNonincludable( NS_TEMPLATE
) );
523 # ###### HELPERS ###########################################################
524 function __call( $method, $args ) {
525 // Call the real method if it exists
526 if ( method_exists( $this, $method ) ) {
527 return $this->$method( $args );
531 '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/',
535 # Interprets arguments:
537 $msg = isset( $args[1] ) ?
$args[1] : " dummy message";
539 # Forge the namespace constant name:
541 $ns_name = "NS_MAIN";
543 $ns_name = "NS_" . strtoupper( MWNamespace
::getCanonicalName( $ns ) );
545 # ... and the MWNamespace method name
546 $nsMethod = strtolower( $m[1] ) . $m[3];
548 $expect = ( $m[2] === '' );
549 $expect_name = $expect ?
'TRUE' : 'FALSE';
551 return $this->assertEquals( $expect,
552 MWNamespace
::$nsMethod( $ns, $msg ),
553 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
557 throw new Exception( __METHOD__
. " could not find a method named $method\n" );
560 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
561 $this->assertTrue( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );
564 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
565 $this->assertFalse( MWNamespace
::subjectEquals( $ns1, $ns2, $msg ) );