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