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