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