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