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