Use consistent notation for "@todo FIXME". Should update http://svn.wikimedia.org...
[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 PHPUnit_Framework_TestCase {
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 $this->assertFalse( MWNamespace::hasSubpages( NS_MAIN ) );
275
276 // Some namespaces with subpages
277 $this->assertTrue( MWNamespace::hasSubpages( NS_TALK ) );
278 $this->assertTrue( MWNamespace::hasSubpages( NS_USER ) );
279 $this->assertTrue( MWNamespace::hasSubpages( NS_USER_TALK ) );
280 }
281
282 /**
283 */
284 public function testGetContentNamespaces() {
285 $this->assertEquals(
286 MWNamespace::getcontentNamespaces(),
287 array( NS_MAIN),
288 '$wgContentNamespaces is an array with only NS_MAIN by default'
289 );
290
291 global $wgContentNamespaces;
292
293 # test !is_array( $wgcontentNamespaces )
294 $wgContentNamespaces = '';
295 $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN );
296 $wgContentNamespaces = false;
297 $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN );
298 $wgContentNamespaces = null;
299 $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN );
300 $wgContentNamespaces = 5;
301 $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN );
302
303 # test $wgContentNamespaces === array()
304 $wgContentNamespaces = array();
305 $this->assertEquals( MWNamespace::getcontentNamespaces(), NS_MAIN );
306
307 # test !in_array( NS_MAIN, $wgContentNamespaces )
308 $wgContentNamespaces = array( NS_USER, NS_CATEGORY );
309 $this->assertEquals(
310 MWNamespace::getcontentNamespaces(),
311 array( NS_MAIN, NS_USER, NS_CATEGORY ),
312 'NS_MAIN is forced in wgContentNamespaces even if unwanted'
313 );
314
315 # test other cases, return $wgcontentNamespaces as is
316 $wgContentNamespaces = array( NS_MAIN );
317 $this->assertEquals(
318 MWNamespace::getcontentNamespaces(),
319 array( NS_MAIN )
320 );
321
322 $wgContentNamespaces = array( NS_MAIN, NS_USER, NS_CATEGORY );
323 $this->assertEquals(
324 MWNamespace::getcontentNamespaces(),
325 array( NS_MAIN, NS_USER, NS_CATEGORY )
326 );
327
328 }
329
330 /**
331 * Some namespaces are always capitalized per code definition
332 * in MWNamespace::$alwaysCapitalizedNamespaces
333 */
334 public function testIsCapitalizedHardcodedAssertions() {
335 // NS_MEDIA and NS_FILE are treated the same
336 $this->assertEquals(
337 MWNamespace::isCapitalized( NS_MEDIA ),
338 MWNamespace::isCapitalized( NS_FILE ),
339 'NS_MEDIA and NS_FILE have same capitalization rendering'
340 );
341
342 // Boths are capitalized by default
343 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIA ) );
344 $this->assertTrue( MWNamespace::isCapitalized( NS_FILE ) );
345
346 // Always capitalized namespaces
347 // @see MWNamespace::$alwaysCapitalizedNamespaces
348 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
349 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
350 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
351 }
352
353 /**
354 * Follows up for testIsCapitalizedHardcodedAssertions() but alter the
355 * global $wgCapitalLink setting to have extended coverage.
356 *
357 * MWNamespace::isCapitalized() rely on two global settings:
358 * $wgCapitalLinkOverrides = array(); by default
359 * $wgCapitalLinks = true; by default
360 * This function test $wgCapitalLinks
361 *
362 * Global setting correctness is tested against the NS_PROJECT and
363 * NS_PROJECT_TALK namespaces since they are not hardcoded nor specials
364 */
365 public function testIsCapitalizedWithWgCapitalLinks() {
366 global $wgCapitalLinks;
367 // Save the global to easily reset to MediaWiki default settings
368 $savedGlobal = $wgCapitalLinks;
369
370 $wgCapitalLinks = true;
371 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
372 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
373
374 $wgCapitalLinks = false;
375 // hardcoded namespaces (see above function) are still capitalized:
376 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
377 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
378 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
379 // setting is correctly applied
380 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
381 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
382
383 // reset global state:
384 $wgCapitalLinks = $savedGlobal;
385 }
386
387 /**
388 * Counter part for MWNamespace::testIsCapitalizedWithWgCapitalLinks() now
389 * testing the $wgCapitalLinkOverrides global.
390 *
391 * @todo split groups of assertions in autonomous testing functions
392 */
393 public function testIsCapitalizedWithWgCapitalLinkOverrides() {
394 global $wgCapitalLinkOverrides;
395 // Save the global to easily reset to MediaWiki default settings
396 $savedGlobal = $wgCapitalLinkOverrides;
397
398 // Test default settings
399 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
400 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT_TALK ) );
401 // hardcoded namespaces (see above function) are capitalized:
402 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
403 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
404 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
405
406 // Hardcoded namespaces remains capitalized
407 $wgCapitalLinkOverrides[NS_SPECIAL] = false;
408 $wgCapitalLinkOverrides[NS_USER] = false;
409 $wgCapitalLinkOverrides[NS_MEDIAWIKI] = false;
410 $this->assertTrue( MWNamespace::isCapitalized( NS_SPECIAL ) );
411 $this->assertTrue( MWNamespace::isCapitalized( NS_USER ) );
412 $this->assertTrue( MWNamespace::isCapitalized( NS_MEDIAWIKI ) );
413
414 $wgCapitalLinkOverrides = $savedGlobal;
415 $wgCapitalLinkOverrides[NS_PROJECT] = false;
416 $this->assertFalse( MWNamespace::isCapitalized( NS_PROJECT ) );
417 $wgCapitalLinkOverrides[NS_PROJECT] = true ;
418 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
419 unset( $wgCapitalLinkOverrides[NS_PROJECT] );
420 $this->assertTrue( MWNamespace::isCapitalized( NS_PROJECT ) );
421
422 // reset global state:
423 $wgCapitalLinkOverrides = $savedGlobal;
424 }
425
426 public function testHasGenderDistinction() {
427 // Namespaces with gender distinctions
428 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER ) );
429 $this->assertTrue( MWNamespace::hasGenderDistinction( NS_USER_TALK ) );
430
431 // Other ones, "genderless"
432 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MEDIA ) );
433 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_SPECIAL ) );
434 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_MAIN ) );
435 $this->assertFalse( MWNamespace::hasGenderDistinction( NS_TALK ) );
436
437 }
438 }
439