Fix testGetTalkExceptions from r82577
[lhc/web/wiklou.git] / tests / phpunit / includes / MWNamespaceTest.php
1 <?php
2 /**
3 * @author Ashar Voultoiz
4 * @copyright Copyright © 2011, Ashar Voultoiz
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 /**
15 * Sets up the fixture, for example, opens a network connection.
16 * This method is called before a test is executed.
17 */
18 protected function setUp() {
19 }
20
21 /**
22 * Tears down the fixture, for example, closes a network connection.
23 * This method is called after a test is executed.
24 */
25 protected function tearDown() {
26 }
27
28
29 #### START OF TESTS #########################################################
30
31 /**
32 * @todo Write more texts, handle $wgAllowImageMoving setting
33 */
34 public function testIsMovable() {
35 $this->assertFalse( MWNamespace::isMovable( NS_CATEGORY ) );
36 # @todo FIXME: Write more tests!!
37 }
38
39 /**
40 * Please make sure to change testIsTalk() if you change the assertions below
41 */
42 public function testIsMain() {
43 // Special namespaces
44 $this->assertTrue( MWNamespace::isMain( NS_MEDIA ) );
45 $this->assertTrue( MWNamespace::isMain( NS_SPECIAL ) );
46
47 // Subject pages
48 $this->assertTrue( MWNamespace::isMain( NS_MAIN ) );
49 $this->assertTrue( MWNamespace::isMain( NS_USER ) );
50 $this->assertTrue( MWNamespace::isMain( 100 ) ); # user defined
51
52 // Talk pages
53 $this->assertFalse( MWNamespace::isMain( NS_TALK ) );
54 $this->assertFalse( MWNamespace::isMain( NS_USER_TALK ) );
55 $this->assertFalse( MWNamespace::isMain( 101 ) ); # user defined
56 }
57
58 /**
59 * Reverse of testIsMain().
60 * Please update testIsMain() if you change assertions below
61 */
62 public function testIsTalk() {
63 // Special namespaces
64 $this->assertFalse( MWNamespace::isTalk( NS_MEDIA ) );
65 $this->assertFalse( MWNamespace::isTalk( NS_SPECIAL ) );
66
67 // Subject pages
68 $this->assertFalse( MWNamespace::isTalk( NS_MAIN ) );
69 $this->assertFalse( MWNamespace::isTalk( NS_USER ) );
70 $this->assertFalse( MWNamespace::isTalk( 100 ) ); # user defined
71
72 // Talk pages
73 $this->assertTrue( MWNamespace::isTalk( NS_TALK ) );
74 $this->assertTrue( MWNamespace::isTalk( NS_USER_TALK ) );
75 $this->assertTrue( MWNamespace::isTalk( 101 ) ); # user defined
76 }
77
78 /**
79 * Regular getTalk() calls
80 * Namespaces without a talk page (NS_MEDIA, NS_SPECIAL) are tested in
81 * the function testGetTalkExceptions()
82 */
83 public function testGetTalk() {
84 $this->assertEquals( NS_TALK, MWNamespace::getTalk( NS_MAIN ) );
85 }
86
87 /**
88 * Exceptions with getTalk()
89 * NS_MEDIA does not have talk pages. MediaWiki raise an exception for them.
90 * @expectedException MWException
91 */
92 public function testGetTalkExceptionsForNsMedia() {
93 $this->assertNull( MWNamespace::getTalk( NS_MEDIA ) );
94 }
95
96 /**
97 * Exceptions with getTalk()
98 * NS_SPECIAL does not have talk pages. MediaWiki raise an exception for them.
99 * @expectedException MWException
100 */
101 public function testGetTalkExceptionsForNsSpecial() {
102 $this->assertNull( MWNamespace::getTalk( NS_SPECIAL ) );
103 }
104
105 /**
106 * Regular getAssociated() calls
107 * Namespaces without an associated page (NS_MEDIA, NS_SPECIAL) are tested in
108 * the function testGetAssociatedExceptions()
109 */
110 public function testGetAssociated() {
111 $this->assertEquals( NS_TALK, MWNamespace::getAssociated( NS_MAIN ) );
112 $this->assertEquals( NS_MAIN, MWNamespace::getAssociated( NS_TALK ) );
113
114 }
115
116 ### Exceptions with getAssociated()
117 ### NS_MEDIA and NS_SPECIAL do not have talk pages. MediaWiki raises
118 ### an exception for them.
119 /**
120 * @expectedException MWException
121 */
122 public function testGetAssociatedExceptionsForNsMedia() {
123 $this->assertNull( MWNamespace::getAssociated( NS_MEDIA ) );
124 }
125
126 /**
127 * @expectedException MWException
128 */
129 public function testGetAssociatedExceptionsForNsSpecial() {
130 $this->assertNull( MWNamespace::getAssociated( NS_SPECIAL ) );
131 }
132
133 /**
134 */
135 public function testGetSubject() {
136 // Special namespaces are their own subjects
137 $this->assertEquals( NS_MEDIA, MWNamespace::getSubject( NS_MEDIA ) );
138 $this->assertEquals( NS_SPECIAL, MWNamespace::getSubject( NS_SPECIAL ) );
139
140 $this->assertEquals( NS_MAIN, MWNamespace::getSubject( NS_TALK ) );
141 $this->assertEquals( NS_USER, MWNamespace::getSubject( NS_USER_TALK ) );
142 }
143
144 /**
145 * @todo Implement testExists().
146 */
147 /*
148 public function testExists() {
149 // Remove the following lines when you implement this test.
150 $this->markTestIncomplete(
151 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
152 );
153 }
154 */
155 /**
156 * @todo Implement testGetCanonicalNamespaces().
157 */
158 /*
159 public function testGetCanonicalNamespaces() {
160 // Remove the following lines when you implement this test.
161 $this->markTestIncomplete(
162 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
163 );
164 }
165 */
166 /**
167 * @todo Implement testGetCanonicalName().
168 */
169 /*
170 public function testGetCanonicalName() {
171 // Remove the following lines when you implement this test.
172 $this->markTestIncomplete(
173 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
174 );
175 }
176 */
177 /**
178 * @todo Implement testGetCanonicalIndex().
179 */
180 /*
181 public function testGetCanonicalIndex() {
182 // Remove the following lines when you implement this test.
183 $this->markTestIncomplete(
184 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
185 );
186 }
187 */
188 /**
189 * @todo Implement testGetValidNamespaces().
190 */
191 /*
192 public function testGetValidNamespaces() {
193 // Remove the following lines when you implement this test.
194 $this->markTestIncomplete(
195 'This test has not been implemented yet. Rely on $wgCanonicalNamespaces.'
196 );
197 }
198 */
199 /**
200 */
201 public function testCanTalk() {
202 $this->assertFalse( MWNamespace::canTalk( NS_MEDIA ) );
203 $this->assertFalse( MWNamespace::canTalk( NS_SPECIAL ) );
204
205 $this->assertTrue( MWNamespace::canTalk( NS_MAIN ) );
206 $this->assertTrue( MWNamespace::canTalk( NS_TALK ) );
207 $this->assertTrue( MWNamespace::canTalk( NS_USER ) );
208 $this->assertTrue( MWNamespace::canTalk( NS_USER_TALK ) );
209
210 // User defined namespaces
211 $this->assertTrue( MWNamespace::canTalk( 100 ) );
212 $this->assertTrue( MWNamespace::canTalk( 101 ) );
213 }
214
215 /**
216 */
217 public function testIsContent() {
218 // NS_MAIN is a content namespace per DefaultSettings.php
219 // and per function definition.
220 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
221
222 // Other namespaces which are not expected to be content
223 $this->assertFalse( MWNamespace::isContent( NS_MEDIA ) );
224 $this->assertFalse( MWNamespace::isContent( NS_SPECIAL ) );
225 $this->assertFalse( MWNamespace::isContent( NS_TALK ) );
226 $this->assertFalse( MWNamespace::isContent( NS_USER ) );
227 $this->assertFalse( MWNamespace::isContent( NS_CATEGORY ) );
228 // User defined namespace:
229 $this->assertFalse( MWNamespace::isContent( 100 ) );
230 }
231
232 /**
233 * Similar to testIsContent() but alters the $wgContentNamespaces
234 * global variable.
235 */
236 public function testIsContentWithAdditionsInWgContentNamespaces() {
237 // NS_MAIN is a content namespace per DefaultSettings.php
238 // and per function definition.
239 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
240
241 // Tests that user defined namespace #252 is not content:
242 $this->assertFalse( MWNamespace::isContent( 252 ) );
243
244 # @todo FIXME: Is global saving really required for PHPUnit?
245 // Bless namespace # 252 as a content namespace
246 global $wgContentNamespaces;
247 $savedGlobal = $wgContentNamespaces;
248 $wgContentNamespaces[] = 252;
249 $this->assertTrue( MWNamespace::isContent( 252 ) );
250
251 // Makes sure NS_MAIN was not impacted
252 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
253
254 // Restore global
255 $wgContentNamespaces = $savedGlobal;
256
257 // Verify namespaces after global restauration
258 $this->assertTrue( MWNamespace::isContent( NS_MAIN ) );
259 $this->assertFalse( MWNamespace::isContent( 252 ) );
260 }
261
262 public function testIsWatchable() {
263 // Specials namespaces are not watchable
264 $this->assertFalse( MWNamespace::isWatchable( NS_MEDIA ) );
265 $this->assertFalse( MWNamespace::isWatchable( NS_SPECIAL ) );
266
267 // Core defined namespaces are watchables
268 $this->assertTrue( MWNamespace::isWatchable( NS_MAIN ) );
269 $this->assertTrue( MWNamespace::isWatchable( NS_TALK ) );
270
271 // Additional, user defined namespaces are watchables
272 $this->assertTrue( MWNamespace::isWatchable( 100 ) );
273 $this->assertTrue( MWNamespace::isWatchable( 101 ) );
274 }
275
276 public function testHasSubpages() {
277 // Special namespaces:
278 $this->assertFalse( MWNamespace::hasSubpages( NS_MEDIA ) );
279 $this->assertFalse( MWNamespace::hasSubpages( NS_SPECIAL ) );
280
281 // namespaces without subpages
282 # save up global
283 global $wgNamespacesWithSubpages;
284 $saved = null;
285 if( array_key_exists( NS_MAIN, $wgNamespacesWithSubpages ) ) {
286 $saved = $wgNamespacesWithSubpages[NS_MAIN];
287 unset( $wgNamespacesWithSubpages[NS_MAIN] );
288 }
289
290 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
291
292 $wgNamespacesWithSubpages[NS_MAIN] = true;
293 $this->assertTrue( MWNamespace::hasSubpages( NS_MAIN ) );
294 $wgNamespacesWithSubpages[NS_MAIN] = false;
295 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
296
297 # restore global
298 if( $saved !== null ) {
299 $wgNamespacesWithSubpages[NS_MAIN] = $saved;
300 }
301
302 // Some namespaces with subpages
303 $this->assertTrue( MWNamespace::hasSubpages( NS_TALK ) );
304 $this->assertTrue( MWNamespace::hasSubpages( NS_USER ) );
305 $this->assertTrue( MWNamespace::hasSubpages( NS_USER_TALK ) );
306 }
307
308 /**
309 */
310 public function testGetContentNamespaces() {
311 $this->assertEquals(
312 array( NS_MAIN ),
313 MWNamespace::getcontentNamespaces(),
314 '$wgContentNamespaces is an array with only NS_MAIN by default'
315 );
316
317 global $wgContentNamespaces;
318
319 # test !is_array( $wgcontentNamespaces )
320 $wgContentNamespaces = '';
321 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
322 $wgContentNamespaces = false;
323 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
324 $wgContentNamespaces = null;
325 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
326 $wgContentNamespaces = 5;
327 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
328
329 # test $wgContentNamespaces === array()
330 $wgContentNamespaces = array();
331 $this->assertEquals( NS_MAIN, MWNamespace::getcontentNamespaces() );
332
333 # test !in_array( NS_MAIN, $wgContentNamespaces )
334 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
335 $this->assertEquals(
336 array( NS_MAIN, NS_USER, NS_CATEGORY ),
337 MWNamespace::getcontentNamespaces(),
338 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
339 );
340
341 # test other cases, return $wgcontentNamespaces as is
342 $wgContentNamespaces = array( NS_MAIN );
343 $this->assertEquals(
344 array( NS_MAIN ),
345 MWNamespace::getcontentNamespaces()
346 );
347
348 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
349 $this->assertEquals(
350 array( NS_MAIN, NS_USER, NS_CATEGORY ),
351 MWNamespace::getcontentNamespaces()
352 );
353
354 }
355
356 /**
357 * Some namespaces are always capitalized per code definition
358 * in MWNamespace::$alwaysCapitalizedNamespaces
359 */
360 public function testIsCapitalizedHardcodedAssertions() {
361 // NS_MEDIA and NS_FILE are treated the same
362 $this->assertEquals(
363 MWNamespace::isCapitalized( NS_MEDIA ),
364 MWNamespace::isCapitalized( NS_FILE ),
365 'NS_MEDIA and NS_FILE have same capitalization rendering'
366 );
367
368 // Boths are capitalized by default
369 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIA ) );
370 $this->assertTrue( MWNamespace::isCapitalized( NS_FILE ) );
371
372 // Always capitalized namespaces
373 // @see MWNamespace::$alwaysCapitalizedNamespaces
374 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
375 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
376 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
377 }
378
379 /**
380 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
381 * global $wgCapitalLink setting to have extended coverage.
382 *
383 * MWNamespace::isCapitalized() rely on two global settings:
384 * $wgCapitalLinkOverrides = array(); by default
385 * $wgCapitalLinks = true; by default
386 * This function test $wgCapitalLinks
387 *
388 * Global setting correctness is tested against the NS_PROJECT and
389 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
390 */
391 public function testIsCapitalizedWithWgCapitalLinks() {
392 global $wgCapitalLinks;
393 // Save the global to easily reset to MediaWiki default settings
394 $savedGlobal = $wgCapitalLinks;
395
396 $wgCapitalLinks = true;
397 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
398 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
399
400 $wgCapitalLinks = false;
401 // hardcoded namespaces (see above function) are still capitalized:
402 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
403 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
404 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
405 // setting is correctly applied
406 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
407 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
408
409 // reset global state:
410 $wgCapitalLinks = $savedGlobal;
411 }
412
413 /**
414 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
415 * testing the $wgCapitalLinkOverrides global.
416 *
417 * @todo split groups of assertions in autonomous testing functions
418 */
419 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
420 global $wgCapitalLinkOverrides;
421 // Save the global to easily reset to MediaWiki default settings
422 $savedGlobal = $wgCapitalLinkOverrides;
423
424 // Test default settings
425 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
426 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
427 // hardcoded namespaces (see above function) are capitalized:
428 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
429 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
430 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
431
432 // Hardcoded namespaces remains capitalized
433 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
434 $wgCapitalLinkOverrides[NS_USER] = false;
435 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
436 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
437 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
438 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
439
440 $wgCapitalLinkOverrides = $savedGlobal;
441 $wgCapitalLinkOverrides[NS_PROJECT] = false;
442 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
443 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
444 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
445 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
446 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
447
448 // reset global state:
449 $wgCapitalLinkOverrides = $savedGlobal;
450 }
451
452 public function testHasGenderDistinction() {
453 // Namespaces with gender distinctions
454 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
455 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
456
457 // Other ones, "genderless"
458 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
459 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
460 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
461 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
462
463 }
464 }
465