Merge "Add STRAIGHT_JOIN to ApiQueryLogEvents and LogPager to avoid planner oddness"
[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 private function assertIsContent( $ns ) {
250 $this->assertTrue( $this->obj->isContent( $ns ) );
251 }
252
253 private function assertIsNotContent( $ns ) {
254 $this->assertFalse( $this->obj->isContent( $ns ) );
255 }
256
257 /**
258 * @covers NamespaceInfo::isContent
259 */
260 public function testIsContent() {
261 // NS_MAIN is a content namespace per DefaultSettings.php
262 // and per function definition.
263
264 $this->assertIsContent( NS_MAIN );
265
266 // Other namespaces which are not expected to be content
267
268 $this->assertIsNotContent( NS_MEDIA );
269 $this->assertIsNotContent( NS_SPECIAL );
270 $this->assertIsNotContent( NS_TALK );
271 $this->assertIsNotContent( NS_USER );
272 $this->assertIsNotContent( NS_CATEGORY );
273 $this->assertIsNotContent( 100 );
274 }
275
276 /**
277 * Similar to testIsContent() but alters the $wgContentNamespaces
278 * global variable.
279 * @covers NamespaceInfo::isContent
280 */
281 public function testIsContentAdvanced() {
282 global $wgContentNamespaces;
283
284 // Test that user defined namespace #252 is not content
285 $this->assertIsNotContent( 252 );
286
287 // Bless namespace # 252 as a content namespace
288 $wgContentNamespaces[] = 252;
289
290 $this->assertIsContent( 252 );
291
292 // Makes sure NS_MAIN was not impacted
293 $this->assertIsContent( NS_MAIN );
294 }
295
296 private function assertIsWatchable( $ns ) {
297 $this->assertTrue( $this->obj->isWatchable( $ns ) );
298 }
299
300 private function assertIsNotWatchable( $ns ) {
301 $this->assertFalse( $this->obj->isWatchable( $ns ) );
302 }
303
304 /**
305 * @covers NamespaceInfo::isWatchable
306 */
307 public function testIsWatchable() {
308 // Specials namespaces are not watchable
309 $this->assertIsNotWatchable( NS_MEDIA );
310 $this->assertIsNotWatchable( NS_SPECIAL );
311
312 // Core defined namespaces are watchables
313 $this->assertIsWatchable( NS_MAIN );
314 $this->assertIsWatchable( NS_TALK );
315
316 // Additional, user defined namespaces are watchables
317 $this->assertIsWatchable( 100 );
318 $this->assertIsWatchable( 101 );
319 }
320
321 private function assertHasSubpages( $ns ) {
322 $this->assertTrue( $this->obj->hasSubpages( $ns ) );
323 }
324
325 private function assertHasNotSubpages( $ns ) {
326 $this->assertFalse( $this->obj->hasSubpages( $ns ) );
327 }
328
329 /**
330 * @covers NamespaceInfo::hasSubpages
331 */
332 public function testHasSubpages() {
333 global $wgNamespacesWithSubpages;
334
335 // Special namespaces:
336 $this->assertHasNotSubpages( NS_MEDIA );
337 $this->assertHasNotSubpages( NS_SPECIAL );
338
339 // Namespaces without subpages
340 $this->assertHasNotSubpages( NS_MAIN );
341
342 $wgNamespacesWithSubpages[NS_MAIN] = true;
343 $this->assertHasSubpages( NS_MAIN );
344
345 $wgNamespacesWithSubpages[NS_MAIN] = false;
346 $this->assertHasNotSubpages( NS_MAIN );
347
348 // Some namespaces with subpages
349 $this->assertHasSubpages( NS_TALK );
350 $this->assertHasSubpages( NS_USER );
351 $this->assertHasSubpages( NS_USER_TALK );
352 }
353
354 /**
355 * @covers NamespaceInfo::getContentNamespaces
356 */
357 public function testGetContentNamespaces() {
358 global $wgContentNamespaces;
359
360 $this->assertEquals(
361 [ NS_MAIN ],
362 $this->obj->getContentNamespaces(),
363 '$wgContentNamespaces is an array with only NS_MAIN by default'
364 );
365
366 # test !is_array( $wgcontentNamespaces )
367 $wgContentNamespaces = '';
368 $this->assertEquals( [ NS_MAIN ], $this->obj->getContentNamespaces() );
369
370 $wgContentNamespaces = false;
371 $this->assertEquals( [ NS_MAIN ], $this->obj->getContentNamespaces() );
372
373 $wgContentNamespaces = null;
374 $this->assertEquals( [ NS_MAIN ], $this->obj->getContentNamespaces() );
375
376 $wgContentNamespaces = 5;
377 $this->assertEquals( [ NS_MAIN ], $this->obj->getContentNamespaces() );
378
379 # test $wgContentNamespaces === []
380 $wgContentNamespaces = [];
381 $this->assertEquals( [ NS_MAIN ], $this->obj->getContentNamespaces() );
382
383 # test !in_array( NS_MAIN, $wgContentNamespaces )
384 $wgContentNamespaces = [ NS_USER, NS_CATEGORY ];
385 $this->assertEquals(
386 [ NS_MAIN, NS_USER, NS_CATEGORY ],
387 $this->obj->getContentNamespaces(),
388 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
389 );
390
391 # test other cases, return $wgcontentNamespaces as is
392 $wgContentNamespaces = [ NS_MAIN ];
393 $this->assertEquals(
394 [ NS_MAIN ],
395 $this->obj->getContentNamespaces()
396 );
397
398 $wgContentNamespaces = [ NS_MAIN, NS_USER, NS_CATEGORY ];
399 $this->assertEquals(
400 [ NS_MAIN, NS_USER, NS_CATEGORY ],
401 $this->obj->getContentNamespaces()
402 );
403 }
404
405 /**
406 * @covers NamespaceInfo::getSubjectNamespaces
407 */
408 public function testGetSubjectNamespaces() {
409 $subjectsNS = $this->obj->getSubjectNamespaces();
410 $this->assertContains( NS_MAIN, $subjectsNS,
411 "Talk namespaces should have NS_MAIN" );
412 $this->assertNotContains( NS_TALK, $subjectsNS,
413 "Talk namespaces should have NS_TALK" );
414
415 $this->assertNotContains( NS_MEDIA, $subjectsNS,
416 "Talk namespaces should not have NS_MEDIA" );
417 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
418 "Talk namespaces should not have NS_SPECIAL" );
419 }
420
421 /**
422 * @covers NamespaceInfo::getTalkNamespaces
423 */
424 public function testGetTalkNamespaces() {
425 $talkNS = $this->obj->getTalkNamespaces();
426 $this->assertContains( NS_TALK, $talkNS,
427 "Subject namespaces should have NS_TALK" );
428 $this->assertNotContains( NS_MAIN, $talkNS,
429 "Subject namespaces should not have NS_MAIN" );
430
431 $this->assertNotContains( NS_MEDIA, $talkNS,
432 "Subject namespaces should not have NS_MEDIA" );
433 $this->assertNotContains( NS_SPECIAL, $talkNS,
434 "Subject namespaces should not have NS_SPECIAL" );
435 }
436
437 private function assertIsCapitalized( $ns ) {
438 $this->assertTrue( $this->obj->isCapitalized( $ns ) );
439 }
440
441 private function assertIsNotCapitalized( $ns ) {
442 $this->assertFalse( $this->obj->isCapitalized( $ns ) );
443 }
444
445 /**
446 * Some namespaces are always capitalized per code definition
447 * in NamespaceInfo::$alwaysCapitalizedNamespaces
448 * @covers NamespaceInfo::isCapitalized
449 */
450 public function testIsCapitalizedHardcodedAssertions() {
451 // NS_MEDIA and NS_FILE are treated the same
452 $this->assertEquals(
453 $this->obj->isCapitalized( NS_MEDIA ),
454 $this->obj->isCapitalized( NS_FILE ),
455 'NS_MEDIA and NS_FILE have same capitalization rendering'
456 );
457
458 // Boths are capitalized by default
459 $this->assertIsCapitalized( NS_MEDIA );
460 $this->assertIsCapitalized( NS_FILE );
461
462 // Always capitalized namespaces
463 // @see NamespaceInfo::$alwaysCapitalizedNamespaces
464 $this->assertIsCapitalized( NS_SPECIAL );
465 $this->assertIsCapitalized( NS_USER );
466 $this->assertIsCapitalized( NS_MEDIAWIKI );
467 }
468
469 /**
470 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
471 * global $wgCapitalLink setting to have extended coverage.
472 *
473 * NamespaceInfo::isCapitalized() rely on two global settings:
474 * $wgCapitalLinkOverrides = []; by default
475 * $wgCapitalLinks = true; by default
476 * This function test $wgCapitalLinks
477 *
478 * Global setting correctness is tested against the NS_PROJECT and
479 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
480 * @covers NamespaceInfo::isCapitalized
481 */
482 public function testIsCapitalizedWithWgCapitalLinks() {
483 $this->assertIsCapitalized( NS_PROJECT );
484 $this->assertIsCapitalized( NS_PROJECT_TALK );
485
486 $this->setMwGlobals( 'wgCapitalLinks', false );
487
488 // hardcoded namespaces (see above function) are still capitalized:
489 $this->assertIsCapitalized( NS_SPECIAL );
490 $this->assertIsCapitalized( NS_USER );
491 $this->assertIsCapitalized( NS_MEDIAWIKI );
492
493 // setting is correctly applied
494 $this->assertIsNotCapitalized( NS_PROJECT );
495 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
496 }
497
498 /**
499 * Counter part for NamespaceInfo::testIsCapitalizedWithWgCapitalLinks() now
500 * testing the $wgCapitalLinkOverrides global.
501 *
502 * @todo split groups of assertions in autonomous testing functions
503 * @covers NamespaceInfo::isCapitalized
504 */
505 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
506 global $wgCapitalLinkOverrides;
507
508 // Test default settings
509 $this->assertIsCapitalized( NS_PROJECT );
510 $this->assertIsCapitalized( NS_PROJECT_TALK );
511
512 // hardcoded namespaces (see above function) are capitalized:
513 $this->assertIsCapitalized( NS_SPECIAL );
514 $this->assertIsCapitalized( NS_USER );
515 $this->assertIsCapitalized( NS_MEDIAWIKI );
516
517 // Hardcoded namespaces remains capitalized
518 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
519 $wgCapitalLinkOverrides[NS_USER] = false;
520 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
521
522 $this->assertIsCapitalized( NS_SPECIAL );
523 $this->assertIsCapitalized( NS_USER );
524 $this->assertIsCapitalized( NS_MEDIAWIKI );
525
526 $wgCapitalLinkOverrides[NS_PROJECT] = false;
527 $this->assertIsNotCapitalized( NS_PROJECT );
528
529 $wgCapitalLinkOverrides[NS_PROJECT] = true;
530 $this->assertIsCapitalized( NS_PROJECT );
531
532 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
533 $this->assertIsCapitalized( NS_PROJECT );
534 }
535
536 /**
537 * @covers NamespaceInfo::hasGenderDistinction
538 */
539 public function testHasGenderDistinction() {
540 // Namespaces with gender distinctions
541 $this->assertTrue( $this->obj->hasGenderDistinction( NS_USER ) );
542 $this->assertTrue( $this->obj->hasGenderDistinction( NS_USER_TALK ) );
543
544 // Other ones, "genderless"
545 $this->assertFalse( $this->obj->hasGenderDistinction( NS_MEDIA ) );
546 $this->assertFalse( $this->obj->hasGenderDistinction( NS_SPECIAL ) );
547 $this->assertFalse( $this->obj->hasGenderDistinction( NS_MAIN ) );
548 $this->assertFalse( $this->obj->hasGenderDistinction( NS_TALK ) );
549 }
550
551 /**
552 * @covers NamespaceInfo::isNonincludable
553 */
554 public function testIsNonincludable() {
555 global $wgNonincludableNamespaces;
556
557 $wgNonincludableNamespaces = [ NS_USER ];
558
559 $this->assertTrue( $this->obj->isNonincludable( NS_USER ) );
560 $this->assertFalse( $this->obj->isNonincludable( NS_TEMPLATE ) );
561 }
562
563 private function assertSameSubject( $ns1, $ns2, $msg = '' ) {
564 $this->assertTrue( $this->obj->subjectEquals( $ns1, $ns2 ), $msg );
565 }
566
567 private function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
568 $this->assertFalse( $this->obj->subjectEquals( $ns1, $ns2 ), $msg );
569 }
570
571 public function provideGetCategoryLinkType() {
572 return [
573 [ NS_MAIN, 'page' ],
574 [ NS_TALK, 'page' ],
575 [ NS_USER, 'page' ],
576 [ NS_USER_TALK, 'page' ],
577
578 [ NS_FILE, 'file' ],
579 [ NS_FILE_TALK, 'page' ],
580
581 [ NS_CATEGORY, 'subcat' ],
582 [ NS_CATEGORY_TALK, 'page' ],
583
584 [ 100, 'page' ],
585 [ 101, 'page' ],
586 ];
587 }
588
589 /**
590 * @dataProvider provideGetCategoryLinkType
591 * @covers NamespaceInfo::getCategoryLinkType
592 *
593 * @param int $index
594 * @param string $expected
595 */
596 public function testGetCategoryLinkType( $index, $expected ) {
597 $actual = $this->obj->getCategoryLinkType( $index );
598 $this->assertSame( $expected, $actual, "NS $index" );
599 }
600 }