MediaWiki REL1_28
MWNamespaceTest.php
Go to the documentation of this file.
1<?php
16 protected function setUp() {
17 parent::setUp();
18
19 $this->setMwGlobals( [
20 'wgContentNamespaces' => [ NS_MAIN ],
21 'wgNamespacesWithSubpages' => [
22 NS_TALK => true,
23 NS_USER => true,
24 NS_USER_TALK => true,
25 ],
26 'wgCapitalLinks' => true,
27 'wgCapitalLinkOverrides' => [],
28 'wgNonincludableNamespaces' => [],
29 ] );
30 }
31
32# ### START OF TESTS #########################################################
33
38 public function testIsMovable() {
39 $this->assertFalse( MWNamespace::isMovable( NS_SPECIAL ) );
40 # @todo FIXME: Write more tests!!
41 }
42
47 public function testIsSubject() {
48 // Special namespaces
49 $this->assertIsSubject( NS_MEDIA );
50 $this->assertIsSubject( NS_SPECIAL );
51
52 // Subject pages
53 $this->assertIsSubject( NS_MAIN );
54 $this->assertIsSubject( NS_USER );
55 $this->assertIsSubject( 100 ); # user defined
56
57 // Talk pages
58 $this->assertIsNotSubject( NS_TALK );
59 $this->assertIsNotSubject( NS_USER_TALK );
60 $this->assertIsNotSubject( 101 ); # user defined
61 }
62
68 public function testIsTalk() {
69 // Special namespaces
70 $this->assertIsNotTalk( NS_MEDIA );
71 $this->assertIsNotTalk( NS_SPECIAL );
72
73 // Subject pages
74 $this->assertIsNotTalk( NS_MAIN );
75 $this->assertIsNotTalk( NS_USER );
76 $this->assertIsNotTalk( 100 ); # user defined
77
78 // Talk pages
79 $this->assertIsTalk( NS_TALK );
80 $this->assertIsTalk( NS_USER_TALK );
81 $this->assertIsTalk( 101 ); # user defined
82 }
83
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 ) );
91
92 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
93 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
94 }
95
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 ) );
107 }
108
116 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
117 }
118
126 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
127 }
128
135 public function testGetAssociated() {
136 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
137 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
138 }
139
140 # ## Exceptions with getAssociated()
141 # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
142 # ## an exception for them.
148 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
149 }
150
156 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
157 }
158
162 /*
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.'
167 );
168 }
169 */
170
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 ) );
188 }
189
193 public function testSubjectEquals() {
195 $this->assertSameSubject( NS_MAIN, 0 ); // In case we make NS_MAIN 'MAIN'
197 $this->assertSameSubject( NS_USER, 2 );
202
205 }
206
213 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
214 );
217 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
218 );
219 }
220
224 /*
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.'
229 );
230 }
231 */
235 /*
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.'
240 );
241 }
242 */
246 /*
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.'
251 );
252 }
253 */
254
258 /*
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.'
263 );
264 }
265 */
266
270 public function testCanTalk() {
271 $this->assertCanNotTalk( NS_MEDIA );
272 $this->assertCanNotTalk( NS_SPECIAL );
273
274 $this->assertCanTalk( NS_MAIN );
275 $this->assertCanTalk( NS_TALK );
276 $this->assertCanTalk( NS_USER );
277 $this->assertCanTalk( NS_USER_TALK );
278
279 // User defined namespaces
280 $this->assertCanTalk( 100 );
281 $this->assertCanTalk( 101 );
282 }
283
287 public function testIsContent() {
288 // NS_MAIN is a content namespace per DefaultSettings.php
289 // and per function definition.
290
291 $this->assertIsContent( NS_MAIN );
292
293 // Other namespaces which are not expected to be content
294
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 );
301 }
302
308 public function testIsContentAdvanced() {
310
311 // Test that user defined namespace #252 is not content
312 $this->assertIsNotContent( 252 );
313
314 // Bless namespace # 252 as a content namespace
315 $wgContentNamespaces[] = 252;
316
317 $this->assertIsContent( 252 );
318
319 // Makes sure NS_MAIN was not impacted
320 $this->assertIsContent( NS_MAIN );
321 }
322
326 public function testIsWatchable() {
327 // Specials namespaces are not watchable
328 $this->assertIsNotWatchable( NS_MEDIA );
329 $this->assertIsNotWatchable( NS_SPECIAL );
330
331 // Core defined namespaces are watchables
332 $this->assertIsWatchable( NS_MAIN );
333 $this->assertIsWatchable( NS_TALK );
334
335 // Additional, user defined namespaces are watchables
336 $this->assertIsWatchable( 100 );
337 $this->assertIsWatchable( 101 );
338 }
339
343 public function testHasSubpages() {
345
346 // Special namespaces:
347 $this->assertHasNotSubpages( NS_MEDIA );
348 $this->assertHasNotSubpages( NS_SPECIAL );
349
350 // Namespaces without subpages
351 $this->assertHasNotSubpages( NS_MAIN );
352
354 $this->assertHasSubpages( NS_MAIN );
355
357 $this->assertHasNotSubpages( NS_MAIN );
358
359 // Some namespaces with subpages
360 $this->assertHasSubpages( NS_TALK );
361 $this->assertHasSubpages( NS_USER );
362 $this->assertHasSubpages( NS_USER_TALK );
363 }
364
368 public function testGetContentNamespaces() {
370
371 $this->assertEquals(
372 [ NS_MAIN ],
373 MWNamespace::getContentNamespaces(),
374 '$wgContentNamespaces is an array with only NS_MAIN by default'
375 );
376
377 # test !is_array( $wgcontentNamespaces )
379 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
380
381 $wgContentNamespaces = false;
382 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
383
385 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
386
388 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
389
390 # test $wgContentNamespaces === []
392 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
393
394 # test !in_array( NS_MAIN, $wgContentNamespaces )
396 $this->assertEquals(
398 MWNamespace::getContentNamespaces(),
399 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
400 );
401
402 # test other cases, return $wgcontentNamespaces as is
404 $this->assertEquals(
405 [ NS_MAIN ],
406 MWNamespace::getContentNamespaces()
407 );
408
410 $this->assertEquals(
412 MWNamespace::getContentNamespaces()
413 );
414 }
415
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" );
425
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" );
430 }
431
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" );
441
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" );
446 }
447
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'
459 );
460
461 // Boths are capitalized by default
462 $this->assertIsCapitalized( NS_MEDIA );
463 $this->assertIsCapitalized( NS_FILE );
464
465 // Always capitalized namespaces
466 // @see MWNamespace::$alwaysCapitalizedNamespaces
467 $this->assertIsCapitalized( NS_SPECIAL );
468 $this->assertIsCapitalized( NS_USER );
469 $this->assertIsCapitalized( NS_MEDIAWIKI );
470 }
471
487
488 $this->assertIsCapitalized( NS_PROJECT );
489 $this->assertIsCapitalized( NS_PROJECT_TALK );
490
491 $wgCapitalLinks = false;
492
493 // hardcoded namespaces (see above function) are still capitalized:
494 $this->assertIsCapitalized( NS_SPECIAL );
495 $this->assertIsCapitalized( NS_USER );
496 $this->assertIsCapitalized( NS_MEDIAWIKI );
497
498 // setting is correctly applied
499 $this->assertIsNotCapitalized( NS_PROJECT );
500 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
501 }
502
512
513 // Test default settings
514 $this->assertIsCapitalized( NS_PROJECT );
515 $this->assertIsCapitalized( NS_PROJECT_TALK );
516
517 // hardcoded namespaces (see above function) are capitalized:
518 $this->assertIsCapitalized( NS_SPECIAL );
519 $this->assertIsCapitalized( NS_USER );
520 $this->assertIsCapitalized( NS_MEDIAWIKI );
521
522 // Hardcoded namespaces remains capitalized
526
527 $this->assertIsCapitalized( NS_SPECIAL );
528 $this->assertIsCapitalized( NS_USER );
529 $this->assertIsCapitalized( NS_MEDIAWIKI );
530
532 $this->assertIsNotCapitalized( NS_PROJECT );
533
535 $this->assertIsCapitalized( NS_PROJECT );
536
538 $this->assertIsCapitalized( NS_PROJECT );
539 }
540
544 public function testHasGenderDistinction() {
545 // Namespaces with gender distinctions
546 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
547 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
548
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 ) );
554 }
555
559 public function testIsNonincludable() {
561
563
564 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
565 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
566 }
567
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 );
573 }
574
575 if ( preg_match(
576 '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/',
577 $method,
578 $m
579 ) ) {
580 # Interprets arguments:
581 $ns = $args[0];
582 $msg = isset( $args[1] ) ? $args[1] : " dummy message";
583
584 # Forge the namespace constant name:
585 if ( $ns === 0 ) {
586 $ns_name = "NS_MAIN";
587 } else {
588 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
589 }
590 # ... and the MWNamespace method name
591 $nsMethod = strtolower( $m[1] ) . $m[3];
592
593 $expect = ( $m[2] === '' );
594 $expect_name = $expect ? 'TRUE' : 'FALSE';
595
596 return $this->assertEquals( $expect,
597 MWNamespace::$nsMethod( $ns, $msg ),
598 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
599 );
600 }
601
602 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
603 }
604
605 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
606 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
607 }
608
609 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
610 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
611 }
612}
$wgNamespacesWithSubpages
Which namespaces should support subpages? See Language.php for a list of namespaces.
$wgCapitalLinks
Set this to false to avoid forcing the first letter of links to capitals.
$wgNonincludableNamespaces
Pages in namespaces in this array can not be used as templates.
$wgContentNamespaces
Array of namespaces which can be deemed to contain valid "content", as far as the site statistics are...
$wgCapitalLinkOverrides
if( $line===false) $args
Definition cdb.php:64
Test class for MWNamespace.
testHasGenderDistinction()
MWNamespace::hasGenderDistinction.
testGetTalkNamespaces()
MWNamespace::getTalkNamespaces.
assertDifferentSubject( $ns1, $ns2, $msg='')
testGetAssociatedExceptionsForNsSpecial()
MWException MWNamespace::getAssociated.
testSubjectEquals()
MWNamespace::subjectEquals.
testGetTalk()
Regular getTalk() calls Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in the funct...
testHasSubpages()
MWNamespace::hasSubpages.
testIsWatchable()
MWNamespace::isWatchable.
testIsContentAdvanced()
Similar to testIsContent() but alters the $wgContentNamespaces global variable.
testGetContentNamespaces()
MWNamespace::getContentNamespaces.
testGetAssociated()
Regular getAssociated() calls Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested...
testIsCapitalizedWithWgCapitalLinks()
Follows up for testIsCapitalizedHardcodedAssertions() but alter the global $wgCapitalLink setting to ...
testGetTalkExceptionsForNsSpecial()
Exceptions with getTalk() NS_SPECIAL does not have talk pages.
testGetSubject()
MWNamespace::getSubject.
testIsCapitalizedHardcodedAssertions()
Some namespaces are always capitalized per code definition in MWNamespace::$alwaysCapitalizedNamespac...
__call( $method, $args)
testGetAssociatedExceptionsForNsMedia()
MWException MWNamespace::getAssociated.
testEquals()
Test MWNamespace::equals Note if we add a namespace registration system with keys like 'MAIN' we shou...
testCanTalk()
MWNamespace::canTalk.
testIsNonincludable()
MWNamespace::isNonincludable.
testGetSubjectNamespaces()
MWNamespace::getSubjectNamespaces.
testIsCapitalizedWithWgCapitalLinkOverrides()
Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now testing the $wgCapitalLinkOve...
assertSameSubject( $ns1, $ns2, $msg='')
testIsTalk()
Reverse of testIsSubject().
testSpecialAndMediaAreDifferentSubjects()
MWNamespace::subjectEquals.
testGetTalkExceptionsForNsMedia()
Exceptions with getTalk() NS_MEDIA does not have talk pages.
testIsContent()
MWNamespace::isContent.
testIsSubject()
Please make sure to change testIsTalk() if you change the assertions below MWNamespace::isSubject.
setMwGlobals( $pairs, $value=null)
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
const NS_USER
Definition Defines.php:58
const NS_FILE
Definition Defines.php:62
const NS_MAIN
Definition Defines.php:56
const NS_PROJECT_TALK
Definition Defines.php:61
const NS_MEDIAWIKI
Definition Defines.php:64
const NS_TEMPLATE
Definition Defines.php:66
const NS_SPECIAL
Definition Defines.php:45
const NS_MEDIA
Definition Defines.php:44
const NS_TALK
Definition Defines.php:57
const NS_USER_TALK
Definition Defines.php:59
const NS_PROJECT
Definition Defines.php:60
const NS_CATEGORY
Definition Defines.php:70
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37