Add DROP INDEX support to DatabaseSqlite::replaceVars method
[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 protected function setUp() {
15 parent::setUp();
16
17 $this->setMwGlobals( array(
18 'wgContentNamespaces' => array( NS_MAIN ),
19 'wgNamespacesWithSubpages' => array(
20 NS_TALK => true,
21 NS_USER => true,
22 NS_USER_TALK => true,
23 ),
24 'wgCapitalLinks' => true,
25 'wgCapitalLinkOverrides' => array(),
26 'wgNonincludableNamespaces' => array(),
27 ) );
28 }
29
30 #### START OF TESTS #########################################################
31
32 /**
33 * @todo Write more texts, handle $wgAllowImageMoving setting
34 */
35 public function testIsMovable() {
36 $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
37 # @todo FIXME: Write more tests!!
38 }
39
40 /**
41 * Please make sure to change testIsTalk() if you change the assertions below
42 */
43 public function testIsSubject() {
44 // Special namespaces
45 $this->assertIsSubject( NS_MEDIA );
46 $this->assertIsSubject( NS_SPECIAL );
47
48 // Subject pages
49 $this->assertIsSubject( NS_MAIN );
50 $this->assertIsSubject( NS_USER );
51 $this->assertIsSubject( 100 ); # user defined
52
53 // Talk pages
54 $this->assertIsNotSubject( NS_TALK );
55 $this->assertIsNotSubject( NS_USER_TALK );
56 $this->assertIsNotSubject( 101 ); # user defined
57 }
58
59 /**
60 * Reverse of testIsSubject().
61 * Please update testIsSubject() if you change assertions below
62 */
63 public function testIsTalk() {
64 // Special namespaces
65 $this->assertIsNotTalk( NS_MEDIA );
66 $this->assertIsNotTalk( NS_SPECIAL );
67
68 // Subject pages
69 $this->assertIsNotTalk( NS_MAIN );
70 $this->assertIsNotTalk( NS_USER );
71 $this->assertIsNotTalk( 100 ); # user defined
72
73 // Talk pages
74 $this->assertIsTalk( NS_TALK );
75 $this->assertIsTalk( NS_USER_TALK );
76 $this->assertIsTalk( 101 ); # user defined
77 }
78
79 /**
80 */
81 public function testGetSubject() {
82 // Special namespaces are their own subjects
83 $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
84 $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
85
86 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
87 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
88 }
89
90 /**
91 * Regular getTalk() calls
92 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
93 * the function testGetTalkExceptions()
94 */
95 public function testGetTalk() {
96 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
97 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_TALK ) );
98 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER ) );
99 $this->assertEquals( NS_USER_TALK, MWNamespace::getTalk( NS_USER_TALK ) );
100 }
101
102 /**
103 * Exceptions with getTalk()
104 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
105 * @expectedException MWException
106 */
107 public function testGetTalkExceptionsForNsMedia() {
108 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
109 }
110
111 /**
112 * Exceptions with getTalk()
113 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
114 * @expectedException MWException
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 */
125 public function testGetAssociated() {
126 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
127 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
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 * @todo Implement testGetCanonicalNamespaces().
207 */
208 /*
209 public function testGetCanonicalNamespaces() {
210 // Remove the following lines when you implement this test.
211 $this->markTestIncomplete(
212 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
213 );
214 }
215 */
216 /**
217 * @todo Implement testGetCanonicalName().
218 */
219 /*
220 public function testGetCanonicalName() {
221 // Remove the following lines when you implement this test.
222 $this->markTestIncomplete(
223 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
224 );
225 }
226 */
227 /**
228 * @todo Implement testGetCanonicalIndex().
229 */
230 /*
231 public function testGetCanonicalIndex() {
232 // Remove the following lines when you implement this test.
233 $this->markTestIncomplete(
234 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
235 );
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 */
253 public function testCanTalk() {
254 $this->assertCanNotTalk( NS_MEDIA );
255 $this->assertCanNotTalk( NS_SPECIAL );
256
257 $this->assertCanTalk( NS_MAIN );
258 $this->assertCanTalk( NS_TALK );
259 $this->assertCanTalk( NS_USER );
260 $this->assertCanTalk( NS_USER_TALK );
261
262 // User defined namespaces
263 $this->assertCanTalk( 100 );
264 $this->assertCanTalk( 101 );
265 }
266
267 /**
268 */
269 public function testIsContent() {
270 // NS_MAIN is a content namespace per DefaultSettings.php
271 // and per function definition.
272
273 $this->assertIsContent( NS_MAIN );
274
275 // Other namespaces which are not expected to be content
276
277 $this->assertIsNotContent( NS_MEDIA );
278 $this->assertIsNotContent( NS_SPECIAL );
279 $this->assertIsNotContent( NS_TALK );
280 $this->assertIsNotContent( NS_USER );
281 $this->assertIsNotContent( NS_CATEGORY );
282 $this->assertIsNotContent( 100 );
283 }
284
285 /**
286 * Similar to testIsContent() but alters the $wgContentNamespaces
287 * global variable.
288 */
289 public function testIsContentAdvanced() {
290 global $wgContentNamespaces;
291
292 // Test that user defined namespace #252 is not content
293 $this->assertIsNotContent( 252 );
294
295 // Bless namespace # 252 as a content namespace
296 $wgContentNamespaces[] = 252;
297
298 $this->assertIsContent( 252 );
299
300 // Makes sure NS_MAIN was not impacted
301 $this->assertIsContent( NS_MAIN );
302 }
303
304 public function testIsWatchable() {
305 // Specials namespaces are not watchable
306 $this->assertIsNotWatchable( NS_MEDIA );
307 $this->assertIsNotWatchable( NS_SPECIAL );
308
309 // Core defined namespaces are watchables
310 $this->assertIsWatchable( NS_MAIN );
311 $this->assertIsWatchable( NS_TALK );
312
313 // Additional, user defined namespaces are watchables
314 $this->assertIsWatchable( 100 );
315 $this->assertIsWatchable( 101 );
316 }
317
318 public function testHasSubpages() {
319 global $wgNamespacesWithSubpages;
320
321 // Special namespaces:
322 $this->assertHasNotSubpages( NS_MEDIA );
323 $this->assertHasNotSubpages( NS_SPECIAL );
324
325 // Namespaces without subpages
326 $this->assertHasNotSubpages( NS_MAIN );
327
328 $wgNamespacesWithSubpages[NS_MAIN] = true;
329 $this->assertHasSubpages( NS_MAIN );
330
331 $wgNamespacesWithSubpages[NS_MAIN] = false;
332 $this->assertHasNotSubpages( NS_MAIN );
333
334 // Some namespaces with subpages
335 $this->assertHasSubpages( NS_TALK );
336 $this->assertHasSubpages( NS_USER );
337 $this->assertHasSubpages( NS_USER_TALK );
338 }
339
340 /**
341 */
342 public function testGetContentNamespaces() {
343 global $wgContentNamespaces;
344
345 $this->assertEquals(
346 array( NS_MAIN ),
347 MWNamespace::getContentNamespaces(),
348 '$wgContentNamespaces is an array with only NS_MAIN by default'
349 );
350
351 # test !is_array( $wgcontentNamespaces )
352 $wgContentNamespaces = '';
353 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
354
355 $wgContentNamespaces = false;
356 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
357
358 $wgContentNamespaces = null;
359 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
360
361 $wgContentNamespaces = 5;
362 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
363
364 # test $wgContentNamespaces === array()
365 $wgContentNamespaces = array();
366 $this->assertEquals( array( NS_MAIN ), MWNamespace::getContentNamespaces() );
367
368 # test !in_array( NS_MAIN, $wgContentNamespaces )
369 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
370 $this->assertEquals(
371 array( NS_MAIN, NS_USER, NS_CATEGORY ),
372 MWNamespace::getContentNamespaces(),
373 'NS_MAIN is forced in $wgContentNamespaces even if unwanted'
374 );
375
376 # test other cases, return $wgcontentNamespaces as is
377 $wgContentNamespaces = array( NS_MAIN );
378 $this->assertEquals(
379 array( NS_MAIN ),
380 MWNamespace::getContentNamespaces()
381 );
382
383 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
384 $this->assertEquals(
385 array( NS_MAIN, NS_USER, NS_CATEGORY ),
386 MWNamespace::getContentNamespaces()
387 );
388 }
389
390 /**
391 */
392 public function testGetSubjectNamespaces() {
393 $subjectsNS = MWNamespace::getSubjectNamespaces();
394 $this->assertContains( NS_MAIN, $subjectsNS,
395 "Talk namespaces should have NS_MAIN" );
396 $this->assertNotContains( NS_TALK, $subjectsNS,
397 "Talk namespaces should have NS_TALK" );
398
399 $this->assertNotContains( NS_MEDIA, $subjectsNS,
400 "Talk namespaces should not have NS_MEDIA" );
401 $this->assertNotContains( NS_SPECIAL, $subjectsNS,
402 "Talk namespaces should not have NS_SPECIAL" );
403 }
404
405 /**
406 */
407 public function testGetTalkNamespaces() {
408 $talkNS = MWNamespace::getTalkNamespaces();
409 $this->assertContains( NS_TALK, $talkNS,
410 "Subject namespaces should have NS_TALK" );
411 $this->assertNotContains( NS_MAIN, $talkNS,
412 "Subject namespaces should not have NS_MAIN" );
413
414 $this->assertNotContains( NS_MEDIA, $talkNS,
415 "Subject namespaces should not have NS_MEDIA" );
416 $this->assertNotContains( NS_SPECIAL, $talkNS,
417 "Subject namespaces should not have NS_SPECIAL" );
418 }
419
420 /**
421 * Some namespaces are always capitalized per code definition
422 * in MWNamespace::$alwaysCapitalizedNamespaces
423 */
424 public function testIsCapitalizedHardcodedAssertions() {
425 // NS_MEDIA and NS_FILE are treated the same
426 $this->assertEquals(
427 MWNamespace::isCapitalized( NS_MEDIA ),
428 MWNamespace::isCapitalized( NS_FILE ),
429 'NS_MEDIA and NS_FILE have same capitalization rendering'
430 );
431
432 // Boths are capitalized by default
433 $this->assertIsCapitalized( NS_MEDIA );
434 $this->assertIsCapitalized( NS_FILE );
435
436 // Always capitalized namespaces
437 // @see MWNamespace::$alwaysCapitalizedNamespaces
438 $this->assertIsCapitalized( NS_SPECIAL );
439 $this->assertIsCapitalized( NS_USER );
440 $this->assertIsCapitalized( NS_MEDIAWIKI );
441 }
442
443 /**
444 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
445 * global $wgCapitalLink setting to have extended coverage.
446 *
447 * MWNamespace::isCapitalized() rely on two global settings:
448 * $wgCapitalLinkOverrides = array(); by default
449 * $wgCapitalLinks = true; by default
450 * This function test $wgCapitalLinks
451 *
452 * Global setting correctness is tested against the NS_PROJECT and
453 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
454 */
455 public function testIsCapitalizedWithWgCapitalLinks() {
456 global $wgCapitalLinks;
457
458 $this->assertIsCapitalized( NS_PROJECT );
459 $this->assertIsCapitalized( NS_PROJECT_TALK );
460
461 $wgCapitalLinks = false;
462
463 // hardcoded namespaces (see above function) are still capitalized:
464 $this->assertIsCapitalized( NS_SPECIAL );
465 $this->assertIsCapitalized( NS_USER );
466 $this->assertIsCapitalized( NS_MEDIAWIKI );
467
468 // setting is correctly applied
469 $this->assertIsNotCapitalized( NS_PROJECT );
470 $this->assertIsNotCapitalized( NS_PROJECT_TALK );
471 }
472
473 /**
474 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
475 * testing the $wgCapitalLinkOverrides global.
476 *
477 * @todo split groups of assertions in autonomous testing functions
478 */
479 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
480 global $wgCapitalLinkOverrides;
481
482 // Test default settings
483 $this->assertIsCapitalized( NS_PROJECT );
484 $this->assertIsCapitalized( NS_PROJECT_TALK );
485
486 // hardcoded namespaces (see above function) are capitalized:
487 $this->assertIsCapitalized( NS_SPECIAL );
488 $this->assertIsCapitalized( NS_USER );
489 $this->assertIsCapitalized( NS_MEDIAWIKI );
490
491 // Hardcoded namespaces remains capitalized
492 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
493 $wgCapitalLinkOverrides[NS_USER] = false;
494 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
495
496 $this->assertIsCapitalized( NS_SPECIAL );
497 $this->assertIsCapitalized( NS_USER );
498 $this->assertIsCapitalized( NS_MEDIAWIKI );
499
500 $wgCapitalLinkOverrides[NS_PROJECT] = false;
501 $this->assertIsNotCapitalized( NS_PROJECT );
502
503 $wgCapitalLinkOverrides[NS_PROJECT] = true;
504 $this->assertIsCapitalized( NS_PROJECT );
505
506 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
507 $this->assertIsCapitalized( NS_PROJECT );
508 }
509
510 public function testHasGenderDistinction() {
511 // Namespaces with gender distinctions
512 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
513 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
514
515 // Other ones, "genderless"
516 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
517 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
518 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
519 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
520 }
521
522 public function testIsNonincludable() {
523 global $wgNonincludableNamespaces;
524
525 $wgNonincludableNamespaces = array( NS_USER );
526
527 $this->assertTrue( MWNamespace::isNonincludable( NS_USER ) );
528 $this->assertFalse( MWNamespace::isNonincludable( NS_TEMPLATE ) );
529 }
530
531 ####### HELPERS ###########################################################
532 function __call( $method, $args ) {
533 // Call the real method if it exists
534 if ( method_exists( $this, $method ) ) {
535 return $this->$method( $args );
536 }
537
538 if ( preg_match( '/^assert(Has|Is|Can)(Not|)(Subject|Talk|Watchable|Content|Subpages|Capitalized)$/', $method, $m ) ) {
539 # Interprets arguments:
540 $ns = $args[0];
541 $msg = isset( $args[1] ) ? $args[1] : " dummy message";
542
543 # Forge the namespace constant name:
544 if ( $ns === 0 ) {
545 $ns_name = "NS_MAIN";
546 } else {
547 $ns_name = "NS_" . strtoupper( MWNamespace::getCanonicalName( $ns ) );
548 }
549 # ... and the MWNamespace method name
550 $nsMethod = strtolower( $m[1] ) . $m[3];
551
552 $expect = ( $m[2] === '' );
553 $expect_name = $expect ? 'TRUE' : 'FALSE';
554
555 return $this->assertEquals( $expect,
556 MWNamespace::$nsMethod( $ns, $msg ),
557 "MWNamespace::$nsMethod( $ns_name ) should returns $expect_name"
558 );
559 }
560
561 throw new Exception( __METHOD__ . " could not find a method named $method\n" );
562 }
563
564 function assertSameSubject( $ns1, $ns2, $msg = '' ) {
565 $this->assertTrue( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
566 }
567
568 function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
569 $this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2, $msg ) );
570 }
571 }