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