Merge "Revert "Log the reason why revision->getContent() returns null""
[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 class MWNamespaceTest extends MediaWikiTestCase {
9
10 protected function setUp() {
11 parent::setUp();
12
13 $this->setMwGlobals( [
14 'wgContentNamespaces' => [ NS_MAIN ],
15 'wgNamespacesWithSubpages' => [
16 NS_TALK => true,
17 NS_USER => true,
18 NS_USER_TALK => true,
19 ],
20 'wgCapitalLinks' => true,
21 'wgCapitalLinkOverrides' => [],
22 'wgNonincludableNamespaces' => [],
23 ] );
24 }
25
26 /**
27 * @todo Write more texts, handle $wgAllowImageMoving setting
28 * @covers MWNamespace::isMovable
29 */
30 public function testIsMovable() {
31 $this->assertFalse( MWNamespace::isMovable( NS_SPECIAL ) );
32 }
33
34 /**
35 * Please make sure to change testIsTalk() if you change the assertions below
36 * @covers MWNamespace::isSubject
37 */
38 public function testIsSubject() {
39 // Special namespaces
40 $this->assertIsSubject( NS_MEDIA );
41 $this->assertIsSubject( NS_SPECIAL );
42
43 // Subject pages
44 $this->assertIsSubject( NS_MAIN );
45 $this->assertIsSubject( NS_USER );
46 $this->assertIsSubject( 100 ); # user defined
47
48 // Talk pages
49 $this->assertIsNotSubject( NS_TALK );
50 $this->assertIsNotSubject( NS_USER_TALK );
51 $this->assertIsNotSubject( 101 ); # user defined
52 }
53
54 /**
55 * Reverse of testIsSubject().
56 * Please update testIsSubject() if you change assertions below
57 * @covers MWNamespace::isTalk
58 */
59 public function testIsTalk() {
60 // Special namespaces
61 $this->assertIsNotTalk( NS_MEDIA );
62 $this->assertIsNotTalk( NS_SPECIAL );
63
64 // Subject pages
65 $this->assertIsNotTalk( NS_MAIN );
66 $this->assertIsNotTalk( NS_USER );
67 $this->assertIsNotTalk( 100 ); # user defined
68
69 // Talk pages
70 $this->assertIsTalk( NS_TALK );
71 $this->assertIsTalk( NS_USER_TALK );
72 $this->assertIsTalk( 101 ); # user defined
73 }
74
75 /**
76 * @covers MWNamespace::getSubject
77 */
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 ) );
82
83 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
84 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
85 }
86
87 /**
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
92 */
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 ) );
98 }
99
100 /**
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
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 * @covers MWNamespace::getTalk
115 */
116 public function testGetTalkExceptionsForNsSpecial() {
117 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
118 }
119
120 /**
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
125 */
126 public function testGetAssociated() {
127 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
128 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
129 }
130
131 # ## Exceptions with getAssociated()
132 # ## NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
133 # ## an exception for them.
134 /**
135 * @expectedException MWException
136 * @covers MWNamespace::getAssociated
137 */
138 public function testGetAssociatedExceptionsForNsMedia() {
139 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
140 }
141
142 /**
143 * @expectedException MWException
144 * @covers MWNamespace::getAssociated
145 */
146 public function testGetAssociatedExceptionsForNsSpecial() {
147 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
148 }
149
150 /**
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
156 */
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 ) );
167 }
168
169 /**
170 * @covers MWNamespace::subjectEquals
171 */
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 );
181
182 $this->assertDifferentSubject( NS_PROJECT, NS_TEMPLATE );
183 $this->assertDifferentSubject( NS_SPECIAL, NS_MAIN );
184 }
185
186 /**
187 * @covers MWNamespace::subjectEquals
188 */
189 public function testSpecialAndMediaAreDifferentSubjects() {
190 $this->assertDifferentSubject(
191 NS_MEDIA, NS_SPECIAL,
192 "NS_MEDIA and NS_SPECIAL are different subject namespaces"
193 );
194 $this->assertDifferentSubject(
195 NS_SPECIAL, NS_MEDIA,
196 "NS_SPECIAL and NS_MEDIA are different subject namespaces"
197 );
198 }
199
200 public function provideHasTalkNamespace() {
201 return [
202 [ NS_MEDIA, false ],
203 [ NS_SPECIAL, false ],
204
205 [ NS_MAIN, true ],
206 [ NS_TALK, true ],
207 [ NS_USER, true ],
208 [ NS_USER_TALK, true ],
209
210 [ 100, true ],
211 [ 101, true ],
212 ];
213 }
214
215 /**
216 * @dataProvider provideHasTalkNamespace
217 * @covers MWNamespace::hasTalkNamespace
218 *
219 * @param int $index
220 * @param bool $expected
221 */
222 public function testHasTalkNamespace( $index, $expected ) {
223 $actual = MWNamespace::hasTalkNamespace( $index );
224 $this->assertSame( $actual, $expected, "NS $index" );
225 }
226
227 /**
228 * @dataProvider provideHasTalkNamespace
229 * @covers MWNamespace::canTalk
230 *
231 * @param int $index
232 * @param bool $expected
233 */
234 public function testCanTalk( $index, $expected ) {
235 $actual = MWNamespace::canTalk( $index );
236 $this->assertSame( $actual, $expected, "NS $index" );
237 }
238
239 /**
240 * @covers MWNamespace::isContent
241 */
242 public function testIsContent() {
243 // NS_MAIN is a content namespace per DefaultSettings.php
244 // and per function definition.
245
246 $this->assertIsContent( NS_MAIN );
247
248 // Other namespaces which are not expected to be content
249
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 );
256 }
257
258 /**
259 * Similar to testIsContent() but alters the $wgContentNamespaces
260 * global variable.
261 * @covers MWNamespace::isContent
262 */
263 public function testIsContentAdvanced() {
264 global $wgContentNamespaces;
265
266 // Test that user defined namespace #252 is not content
267 $this->assertIsNotContent( 252 );
268
269 // Bless namespace # 252 as a content namespace
270 $wgContentNamespaces[] = 252;
271
272 $this->assertIsContent( 252 );
273
274 // Makes sure NS_MAIN was not impacted
275 $this->assertIsContent( NS_MAIN );
276 }
277
278 /**
279 * @covers MWNamespace::isWatchable
280 */
281 public function testIsWatchable() {
282 // Specials namespaces are not watchable
283 $this->assertIsNotWatchable( NS_MEDIA );
284 $this->assertIsNotWatchable( NS_SPECIAL );
285
286 // Core defined namespaces are watchables
287 $this->assertIsWatchable( NS_MAIN );
288 $this->assertIsWatchable( NS_TALK );
289
290 // Additional, user defined namespaces are watchables
291 $this->assertIsWatchable( 100 );
292 $this->assertIsWatchable( 101 );
293 }
294
295 /**
296 * @covers MWNamespace::hasSubpages
297 */
298 public function testHasSubpages() {
299 global $wgNamespacesWithSubpages;
300
301 // Special namespaces:
302 $this->assertHasNotSubpages( NS_MEDIA );
303 $this->assertHasNotSubpages( NS_SPECIAL );
304
305 // Namespaces without subpages
306 $this->assertHasNotSubpages( NS_MAIN );
307
308 $wgNamespacesWithSubpages[NS_MAIN] = true;
309 $this->assertHasSubpages( NS_MAIN );
310
311 $wgNamespacesWithSubpages[NS_MAIN] = false;
312 $this->assertHasNotSubpages( NS_MAIN );
313
314 // Some namespaces with subpages
315 $this->assertHasSubpages( NS_TALK );
316 $this->assertHasSubpages( NS_USER );
317 $this->assertHasSubpages( NS_USER_TALK );
318 }
319
320 /**
321 * @covers MWNamespace::getContentNamespaces
322 */
323 public function testGetContentNamespaces() {
324 global $wgContentNamespaces;
325
326 $this->assertEquals(
327 [ NS_MAIN ],
328 MWNamespace::getContentNamespaces(),
329 '$wgContentNamespaces is an array with only NS_MAIN by default'
330 );
331
332 # test !is_array( $wgcontentNamespaces )
333 $wgContentNamespaces = '';
334 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
335
336 $wgContentNamespaces = false;
337 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
338
339 $wgContentNamespaces = null;
340 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
341
342 $wgContentNamespaces = 5;
343 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
344
345 # test $wgContentNamespaces === []
346 $wgContentNamespaces = [];
347 $this->assertEquals( [ NS_MAIN ], MWNamespace::getContentNamespaces() );
348
349 # test !in_array( NS_MAIN, $wgContentNamespaces )
350 $wgContentNamespaces = [ NS_USER, NS_CATEGORY ];
351 $this->assertEquals(
352 [ NS_MAIN, NS_USER, NS_CATEGORY ],
353 MWNamespace::getContentNamespaces(),
354 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
355 );
356
357 # test other cases, return $wgcontentNamespaces as is
358 $wgContentNamespaces = [ NS_MAIN ];
359 $this->assertEquals(
360 [ NS_MAIN ],
361 MWNamespace::getContentNamespaces()
362 );
363
364 $wgContentNamespaces = [ NS_MAIN, NS_USER, NS_CATEGORY ];
365 $this->assertEquals(
366 [ NS_MAIN, NS_USER, NS_CATEGORY ],
367 MWNamespace::getContentNamespaces()
368 );
369 }
370
371 /**
372 * @covers MWNamespace::getSubjectNamespaces
373 */
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" );
380
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" );
385 }
386
387 /**
388 * @covers MWNamespace::getTalkNamespaces
389 */
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" );
396
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" );
401 }
402
403 /**
404 * Some namespaces are always capitalized per code definition
405 * in MWNamespace::$alwaysCapitalizedNamespaces
406 * @covers MWNamespace::isCapitalized
407 */
408 public function testIsCapitalizedHardcodedAssertions() {
409 // NS_MEDIA and NS_FILE are treated the same
410 $this->assertEquals(
411 MWNamespace::isCapitalized( NS_MEDIA ),
412 MWNamespace::isCapitalized( NS_FILE ),
413 'NS_MEDIA and NS_FILE have same capitalization rendering'
414 );
415
416 // Boths are capitalized by default
417 $this->assertIsCapitalized( NS_MEDIA );
418 $this->assertIsCapitalized( NS_FILE );
419
420 // Always capitalized namespaces
421 // @see MWNamespace::$alwaysCapitalizedNamespaces
422 $this->assertIsCapitalized( NS_SPECIAL );
423 $this->assertIsCapitalized( NS_USER );
424 $this->assertIsCapitalized( NS_MEDIAWIKI );
425 }
426
427 /**
428 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
429 * global $wgCapitalLink setting to have extended coverage.
430 *
431 * MWNamespace::isCapitalized() rely on two global settings:
432 * $wgCapitalLinkOverrides = []; by default
433 * $wgCapitalLinks = true; by default
434 * This function test $wgCapitalLinks
435 *
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
439 */
440 public function testIsCapitalizedWithWgCapitalLinks() {
441 global $wgCapitalLinks;
442
443 $this->assertIsCapitalized( NS_PROJECT );
444 $this->assertIsCapitalized( NS_PROJECT_TALK );
445
446 $wgCapitalLinks = false;
447
448 // hardcoded namespaces (see above function) are still capitalized:
449 $this->assertIsCapitalized( NS_SPECIAL );
450 $this->assertIsCapitalized( NS_USER );
451 $this->assertIsCapitalized( NS_MEDIAWIKI );
452
453 // setting is correctly applied
454 $this->assertIsNotCapitalized( NS_PROJECT );
455 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
456 }
457
458 /**
459 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
460 * testing the $wgCapitalLinkOverrides global.
461 *
462 * @todo split groups of assertions in autonomous testing functions
463 * @covers MWNamespace::isCapitalized
464 */
465 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
466 global $wgCapitalLinkOverrides;
467
468 // Test default settings
469 $this->assertIsCapitalized( NS_PROJECT );
470 $this->assertIsCapitalized( NS_PROJECT_TALK );
471
472 // hardcoded namespaces (see above function) are capitalized:
473 $this->assertIsCapitalized( NS_SPECIAL );
474 $this->assertIsCapitalized( NS_USER );
475 $this->assertIsCapitalized( NS_MEDIAWIKI );
476
477 // Hardcoded namespaces remains capitalized
478 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
479 $wgCapitalLinkOverrides[NS_USER] = false;
480 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
481
482 $this->assertIsCapitalized( NS_SPECIAL );
483 $this->assertIsCapitalized( NS_USER );
484 $this->assertIsCapitalized( NS_MEDIAWIKI );
485
486 $wgCapitalLinkOverrides[NS_PROJECT] = false;
487 $this->assertIsNotCapitalized( NS_PROJECT );
488
489 $wgCapitalLinkOverrides[NS_PROJECT] = true;
490 $this->assertIsCapitalized( NS_PROJECT );
491
492 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
493 $this->assertIsCapitalized( NS_PROJECT );
494 }
495
496 /**
497 * @covers MWNamespace::hasGenderDistinction
498 */
499 public function testHasGenderDistinction() {
500 // Namespaces with gender distinctions
501 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
502 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
503
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 ) );
509 }
510
511 /**
512 * @covers MWNamespace::isNonincludable
513 */
514 public function testIsNonincludable() {
515 global $wgNonincludableNamespaces;
516
517 $wgNonincludableNamespaces = [ NS_USER ];
518
519 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
520 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
521 }
522
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 );
528 }
529
530 if ( preg_match(
531 '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/',
532 $method,
533 $m
534 ) ) {
535 # Interprets arguments:
536 $ns = $args[0];
537 $msg = isset( $args[1] ) ? $args[1] : " dummy message";
538
539 # Forge the namespace constant name:
540 if ( $ns === 0 ) {
541 $ns_name = "NS_MAIN";
542 } else {
543 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
544 }
545 # ... and the MWNamespace method name
546 $nsMethod = strtolower( $m[1] ) . $m[3];
547
548 $expect = ( $m[2] === '' );
549 $expect_name = $expect ? 'TRUE' : 'FALSE';
550
551 return $this->assertEquals( $expect,
552 MWNamespace::$nsMethod( $ns, $msg ),
553 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
554 );
555 }
556
557 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
558 }
559
560 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
561 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
562 }
563
564 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
565 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
566 }
567 }