GlobalFunction additions:
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalTest.php
1 <?php
2
3 class GlobalTest extends MediaWikiTestCase {
4 function setUp() {
5 global $wgReadOnlyFile, $wgContLang, $wgLang;
6 $this->originals['wgReadOnlyFile'] = $wgReadOnlyFile;
7 $wgReadOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" );
8 unlink( $wgReadOnlyFile );
9 $wgContLang = $wgLang = Language::factory( 'en' );
10 }
11
12 function tearDown() {
13 global $wgReadOnlyFile;
14 if ( file_exists( $wgReadOnlyFile ) ) {
15 unlink( $wgReadOnlyFile );
16 }
17 $wgReadOnlyFile = $this->originals['wgReadOnlyFile'];
18 }
19
20 function testRandom() {
21 # This could hypothetically fail, but it shouldn't ;)
22 $this->assertFalse(
23 wfRandom() == wfRandom() );
24 }
25
26 function testUrlencode() {
27 $this->assertEquals(
28 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
29 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
30 }
31
32 function testReadOnlyEmpty() {
33 global $wgReadOnly;
34 $wgReadOnly = null;
35
36 $this->assertFalse( wfReadOnly() );
37 $this->assertFalse( wfReadOnly() );
38 }
39
40 function testReadOnlySet() {
41 global $wgReadOnly, $wgReadOnlyFile;
42
43 $f = fopen( $wgReadOnlyFile, "wt" );
44 fwrite( $f, 'Message' );
45 fclose( $f );
46 $wgReadOnly = null; # Check on $wgReadOnlyFile
47
48 $this->assertTrue( wfReadOnly() );
49 $this->assertTrue( wfReadOnly() ); # Check cached
50
51 unlink( $wgReadOnlyFile );
52 $wgReadOnly = null; # Clean cache
53
54 $this->assertFalse( wfReadOnly() );
55 $this->assertFalse( wfReadOnly() );
56 }
57
58 function testQuotedPrintable() {
59 $this->assertEquals(
60 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
61 UserMailer::quotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
62 }
63
64 function testTime() {
65 $start = wfTime();
66 $this->assertInternalType( 'float', $start );
67 $end = wfTime();
68 $this->assertTrue( $end > $start, "Time is running backwards!" );
69 }
70
71 function testArrayToCGI() {
72 $this->assertEquals(
73 "baz=AT%26T&foo=bar",
74 wfArrayToCGI(
75 array( 'baz' => 'AT&T', 'ignore' => '' ),
76 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
77 }
78
79 function testMimeTypeMatch() {
80 $this->assertEquals(
81 'text/html',
82 mimeTypeMatch( 'text/html',
83 array( 'application/xhtml+xml' => 1.0,
84 'text/html' => 0.7,
85 'text/plain' => 0.3 ) ) );
86 $this->assertEquals(
87 'text/*',
88 mimeTypeMatch( 'text/html',
89 array( 'image/*' => 1.0,
90 'text/*' => 0.5 ) ) );
91 $this->assertEquals(
92 '*/*',
93 mimeTypeMatch( 'text/html',
94 array( '*/*' => 1.0 ) ) );
95 $this->assertNull(
96 mimeTypeMatch( 'text/html',
97 array( 'image/png' => 1.0,
98 'image/svg+xml' => 0.5 ) ) );
99 }
100
101 function testNegotiateType() {
102 $this->assertEquals(
103 'text/html',
104 wfNegotiateType(
105 array( 'application/xhtml+xml' => 1.0,
106 'text/html' => 0.7,
107 'text/plain' => 0.5,
108 'text/*' => 0.2 ),
109 array( 'text/html' => 1.0 ) ) );
110 $this->assertEquals(
111 'application/xhtml+xml',
112 wfNegotiateType(
113 array( 'application/xhtml+xml' => 1.0,
114 'text/html' => 0.7,
115 'text/plain' => 0.5,
116 'text/*' => 0.2 ),
117 array( 'application/xhtml+xml' => 1.0,
118 'text/html' => 0.5 ) ) );
119 $this->assertEquals(
120 'text/html',
121 wfNegotiateType(
122 array( 'text/html' => 1.0,
123 'text/plain' => 0.5,
124 'text/*' => 0.5,
125 'application/xhtml+xml' => 0.2 ),
126 array( 'application/xhtml+xml' => 1.0,
127 'text/html' => 0.5 ) ) );
128 $this->assertEquals(
129 'text/html',
130 wfNegotiateType(
131 array( 'text/*' => 1.0,
132 'image/*' => 0.7,
133 '*/*' => 0.3 ),
134 array( 'application/xhtml+xml' => 1.0,
135 'text/html' => 0.5 ) ) );
136 $this->assertNull(
137 wfNegotiateType(
138 array( 'text/*' => 1.0 ),
139 array( 'application/xhtml+xml' => 1.0 ) ) );
140 }
141
142 function testTimestamp() {
143 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
144 $this->assertEquals(
145 '20010115123456',
146 wfTimestamp( TS_MW, $t ),
147 'TS_UNIX to TS_MW' );
148 $this->assertEquals(
149 '19690115123456',
150 wfTimestamp( TS_MW, -30281104 ),
151 'Negative TS_UNIX to TS_MW' );
152 $this->assertEquals(
153 979562096,
154 wfTimestamp( TS_UNIX, $t ),
155 'TS_UNIX to TS_UNIX' );
156 $this->assertEquals(
157 '2001-01-15 12:34:56',
158 wfTimestamp( TS_DB, $t ),
159 'TS_UNIX to TS_DB' );
160 $this->assertEquals(
161 '20010115T123456Z',
162 wfTimestamp( TS_ISO_8601_BASIC, $t ),
163 'TS_ISO_8601_BASIC to TS_DB' );
164
165 $this->assertEquals(
166 '20010115123456',
167 wfTimestamp( TS_MW, '20010115123456' ),
168 'TS_MW to TS_MW' );
169 $this->assertEquals(
170 979562096,
171 wfTimestamp( TS_UNIX, '20010115123456' ),
172 'TS_MW to TS_UNIX' );
173 $this->assertEquals(
174 '2001-01-15 12:34:56',
175 wfTimestamp( TS_DB, '20010115123456' ),
176 'TS_MW to TS_DB' );
177 $this->assertEquals(
178 '20010115T123456Z',
179 wfTimestamp( TS_ISO_8601_BASIC, '20010115123456' ),
180 'TS_MW to TS_ISO_8601_BASIC' );
181
182 $this->assertEquals(
183 '20010115123456',
184 wfTimestamp( TS_MW, '2001-01-15 12:34:56' ),
185 'TS_DB to TS_MW' );
186 $this->assertEquals(
187 979562096,
188 wfTimestamp( TS_UNIX, '2001-01-15 12:34:56' ),
189 'TS_DB to TS_UNIX' );
190 $this->assertEquals(
191 '2001-01-15 12:34:56',
192 wfTimestamp( TS_DB, '2001-01-15 12:34:56' ),
193 'TS_DB to TS_DB' );
194 $this->assertEquals(
195 '20010115T123456Z',
196 wfTimestamp( TS_ISO_8601_BASIC, '2001-01-15 12:34:56' ),
197 'TS_DB to TS_ISO_8601_BASIC' );
198
199 # rfc2822 section 3.3
200
201 $this->assertEquals(
202 'Mon, 15 Jan 2001 12:34:56 GMT',
203 wfTimestamp( TS_RFC2822, '20010115123456' ),
204 'TS_MW to TS_RFC2822' );
205
206 $this->assertEquals(
207 '20010115123456',
208 wfTimestamp( TS_MW, 'Mon, 15 Jan 2001 12:34:56 GMT' ),
209 'TS_RFC2822 to TS_MW' );
210
211 $this->assertEquals(
212 '20010115123456',
213 wfTimestamp( TS_MW, ' Mon, 15 Jan 2001 12:34:56 GMT' ),
214 'TS_RFC2822 with leading space to TS_MW' );
215
216 $this->assertEquals(
217 '20010115123456',
218 wfTimestamp( TS_MW, '15 Jan 2001 12:34:56 GMT' ),
219 'TS_RFC2822 without optional day-of-week to TS_MW' );
220
221 # FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
222 # obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
223 $this->assertEquals(
224 '20010115123456',
225 wfTimestamp( TS_MW, 'Mon, 15 Jan 2001 12:34:56 GMT' ),
226 'TS_RFC2822 to TS_MW' );
227
228 # WSP = SP / HTAB ; rfc2234
229 $this->assertEquals(
230 '20010115123456',
231 wfTimestamp( TS_MW, "Mon, 15 Jan\x092001 12:34:56 GMT" ),
232 'TS_RFC2822 with HTAB to TS_MW' );
233
234 $this->assertEquals(
235 '20010115123456',
236 wfTimestamp( TS_MW, "Mon, 15 Jan\x09 \x09 2001 12:34:56 GMT" ),
237 'TS_RFC2822 with HTAB and SP to TS_MW' );
238
239 $this->assertEquals(
240 '19941106084937',
241 wfTimestamp( TS_MW, "Sun, 6 Nov 94 08:49:37 GMT" ),
242 'TS_RFC2822 with obsolete year to TS_MW' );
243 }
244
245 /**
246 * This test checks wfTimestamp() with values outside.
247 * It needs PHP 64 bits or PHP > 5.1.
248 * See r74778 and bug 25451
249 */
250 function testOldTimestamps() {
251 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:54 GMT',
252 wfTimestamp( TS_RFC2822, '19011213204554' ),
253 'Earliest time according to php documentation' );
254
255 $this->assertEquals( 'Tue, 19 Jan 2038 03:14:07 GMT',
256 wfTimestamp( TS_RFC2822, '20380119031407' ),
257 'Latest 32 bit time' );
258
259 $this->assertEquals( '-2147483648',
260 wfTimestamp( TS_UNIX, '19011213204552' ),
261 'Earliest 32 bit unix time' );
262
263 $this->assertEquals( '2147483647',
264 wfTimestamp( TS_UNIX, '20380119031407' ),
265 'Latest 32 bit unix time' );
266
267 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:52 GMT',
268 wfTimestamp( TS_RFC2822, '19011213204552' ),
269 'Earliest 32 bit time' );
270
271 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:51 GMT',
272 wfTimestamp( TS_RFC2822, '19011213204551' ),
273 'Earliest 32 bit time - 1' );
274
275 $this->assertEquals( 'Tue, 19 Jan 2038 03:14:08 GMT',
276 wfTimestamp( TS_RFC2822, '20380119031408' ),
277 'Latest 32 bit time + 1' );
278
279 $this->assertEquals( '19011212000000',
280 wfTimestamp(TS_MW, '19011212000000'),
281 'Convert to itself r74778#c10645' );
282
283 $this->assertEquals( '-2147483649',
284 wfTimestamp( TS_UNIX, '19011213204551' ),
285 'Earliest 32 bit unix time - 1' );
286
287 $this->assertEquals( '2147483648',
288 wfTimestamp( TS_UNIX, '20380119031408' ),
289 'Latest 32 bit unix time + 1' );
290
291 $this->assertEquals( '19011213204551',
292 wfTimestamp( TS_MW, '-2147483649' ),
293 '1901 negative unix time to MediaWiki' );
294
295 $this->assertEquals( '18010115123456',
296 wfTimestamp( TS_MW, '-5331871504' ),
297 '1801 negative unix time to MediaWiki' );
298
299 $this->assertEquals( 'Tue, 09 Aug 0117 12:34:56 GMT',
300 wfTimestamp( TS_RFC2822, '0117-08-09 12:34:56'),
301 'Death of Roman Emperor [[Trajan]]');
302
303 /* FIXME: 00 to 101 years are taken as being in [1970-2069] */
304
305 $this->assertEquals( 'Sun, 01 Jan 0101 00:00:00 GMT',
306 wfTimestamp( TS_RFC2822, '-58979923200'),
307 '1/1/101');
308
309 $this->assertEquals( 'Mon, 01 Jan 0001 00:00:00 GMT',
310 wfTimestamp( TS_RFC2822, '-62135596800'),
311 'Year 1');
312
313 /* It is not clear if we should generate a year 0 or not
314 * We are completely off RFC2822 requirement of year being
315 * 1900 or later.
316 */
317 $this->assertEquals( 'Wed, 18 Oct 0000 00:00:00 GMT',
318 wfTimestamp( TS_RFC2822, '-62142076800'),
319 'ISO 8601:2004 [[year 0]], also called [[1 BC]]');
320 }
321
322 function testHttpDate() {
323 # The Resource Loader uses wfTimestamp() to convert timestamps
324 # from If-Modified-Since header.
325 # Thus it must be able to parse all rfc2616 date formats
326 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
327
328 $this->assertEquals(
329 '19941106084937',
330 wfTimestamp( TS_MW, 'Sun, 06 Nov 1994 08:49:37 GMT' ),
331 'RFC 822 date' );
332
333 $this->assertEquals(
334 '19941106084937',
335 wfTimestamp( TS_MW, 'Sunday, 06-Nov-94 08:49:37 GMT' ),
336 'RFC 850 date' );
337
338 $this->assertEquals(
339 '19941106084937',
340 wfTimestamp( TS_MW, 'Sun Nov 6 08:49:37 1994' ),
341 "ANSI C's asctime() format" );
342
343 // See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
344 $this->assertEquals(
345 '20101122141242',
346 wfTimestamp( TS_MW, 'Mon, 22 Nov 2010 14:12:42 GMT; length=52626' ),
347 "Netscape extension to HTTP/1.0" );
348
349 }
350
351 function testTimestampParameter() {
352 // There are a number of assumptions in our codebase where wfTimestamp() should give
353 // the current date but it is not given a 0 there. See r71751 CR
354
355 $now = wfTimestamp( TS_UNIX );
356 // We check that wfTimestamp doesn't return false (error) and use a LessThan assert
357 // for the cases where the test is run in a second boundary.
358
359 $zero = wfTimestamp( TS_UNIX, 0 );
360 $this->assertNotEquals( false, $zero );
361 $this->assertLessThan( 5, $zero - $now );
362
363 $empty = wfTimestamp( TS_UNIX, '' );
364 $this->assertNotEquals( false, $empty );
365 $this->assertLessThan( 5, $empty - $now );
366
367 $null = wfTimestamp( TS_UNIX, null );
368 $this->assertNotEquals( false, $null );
369 $this->assertLessThan( 5, $null - $now );
370 }
371
372 function testBasename() {
373 $sets = array(
374 '' => '',
375 '/' => '',
376 '\\' => '',
377 '//' => '',
378 '\\\\' => '',
379 'a' => 'a',
380 'aaaa' => 'aaaa',
381 '/a' => 'a',
382 '\\a' => 'a',
383 '/aaaa' => 'aaaa',
384 '\\aaaa' => 'aaaa',
385 '/aaaa/' => 'aaaa',
386 '\\aaaa\\' => 'aaaa',
387 '\\aaaa\\' => 'aaaa',
388 '/mnt/upload3/wikipedia/en/thumb/8/8b/Zork_Grand_Inquisitor_box_cover.jpg/93px-Zork_Grand_Inquisitor_box_cover.jpg' => '93px-Zork_Grand_Inquisitor_box_cover.jpg',
389 'C:\\Progra~1\\Wikime~1\\Wikipe~1\\VIEWER.EXE' => 'VIEWER.EXE',
390 'Östergötland_coat_of_arms.png' => 'Östergötland_coat_of_arms.png',
391 );
392 foreach ( $sets as $from => $to ) {
393 $this->assertEquals( $to, wfBaseName( $from ),
394 "wfBaseName('$from') => '$to'" );
395 }
396 }
397
398
399 function testFallbackMbstringFunctions() {
400
401 if( !extension_loaded( 'mbstring' ) ) {
402 $this->markTestSkipped( "The mb_string functions must be installed to test the fallback functions" );
403 }
404
405 $sampleUTF = "Östergötland_coat_of_arms.png";
406
407
408 //mb_substr
409 $substr_params = array(
410 array( 0, 0 ),
411 array( 5, -4 ),
412 array( 33 ),
413 array( 100, -5 ),
414 array( -8, 10 ),
415 array( 1, 1 ),
416 array( 2, -1 )
417 );
418
419 foreach( $substr_params as $param_set ) {
420 $old_param_set = $param_set;
421 array_unshift( $param_set, $sampleUTF );
422
423 $this->assertEquals(
424 call_user_func_array( 'mb_substr', $param_set ),
425 call_user_func_array( 'fallback_mb_substr', $param_set ),
426 'Fallback mb_substr with params ' . implode( ', ', $old_param_set )
427 );
428 }
429
430
431 //mb_strlen
432 $this->assertEquals(
433 mb_strlen( $sampleUTF ),
434 fallback_mb_strlen( $sampleUTF ),
435 'Fallback mb_strlen'
436 );
437
438
439 //mb_str(r?)pos
440 $strpos_params = array(
441 //array( 'ter' ),
442 //array( 'Ö' ),
443 //array( 'Ö', 3 ),
444 //array( 'oat_', 100 ),
445 //array( 'c', -10 ),
446 //Broken for now
447 );
448
449 foreach( $strpos_params as $param_set ) {
450 $old_param_set = $param_set;
451 array_unshift( $param_set, $sampleUTF );
452
453 $this->assertEquals(
454 call_user_func_array( 'mb_strpos', $param_set ),
455 call_user_func_array( 'fallback_mb_strpos', $param_set ),
456 'Fallback mb_strpos with params ' . implode( ', ', $old_param_set )
457 );
458
459 $this->assertEquals(
460 call_user_func_array( 'mb_strrpos', $param_set ),
461 call_user_func_array( 'fallback_mb_strrpos', $param_set ),
462 'Fallback mb_strrpos with params ' . implode( ', ', $old_param_set )
463 );
464 }
465
466 }
467
468
469 function testDebugFunctionTest() {
470
471 global $wgDebugLogFile, $wgOut, $wgShowDebug;
472
473 $old_log_file = $wgDebugLogFile;
474 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
475
476
477
478 wfDebug( "This is a normal string" );
479 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
480 unlink( $wgDebugLogFile );
481
482
483 wfDebug( "This is nöt an ASCII string" );
484 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
485 unlink( $wgDebugLogFile );
486
487
488 wfDebug( "\00305This has böth UTF and control chars\003" );
489 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
490 unlink( $wgDebugLogFile );
491
492
493
494 $old_wgOut = $wgOut;
495 $old_wgShowDebug = $wgShowDebug;
496
497 $wgOut = new StubObject( 'wgOut', 'MockOutputPage' );
498 $wgOut->doNothing(); //just to unstub it
499
500 $wgShowDebug = true;
501
502 $message = "\00305This has böth UTF and control chars\003";
503
504 wfDebug( $message );
505
506 if( $wgOut->message == "JAJA is a stupid error message. Anyway, here's your message: $message" ) {
507 $this->assertTrue( true, 'MockOutputPage called, set the proper message.' );
508 }
509 else {
510 $this->assertTrue( false, 'MockOutputPage was not called.' );
511 }
512
513 $wgOut = $old_wgOut;
514 $wgShowDebug = $old_wgShowDebug;
515 unlink( $wgDebugLogFile );
516
517
518
519 wfDebugMem();
520 $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
521 unlink( $wgDebugLogFile );
522
523 wfDebugMem(true);
524 $this->assertGreaterThan( 5000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
525 unlink( $wgDebugLogFile );
526
527
528
529 $wgDebugLogFile = $old_log_file;
530
531 }
532
533 function testClientAcceptsGzipTest() {
534
535 $settings = array(
536 'gzip' => true,
537 'bzip' => false,
538 '*' => false,
539 'compress, gzip' => true,
540 'gzip;q=1.0' => true,
541 'foozip' => false,
542 'foo*zip' => false,
543 'gzip;q=abcde' => true, //is this REALLY valid?
544 'gzip;q=12345678.9' => true,
545 ' gzip' => true,
546 );
547
548 if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
549
550 foreach ( $settings as $encoding => $expect ) {
551 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
552
553 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
554 "'$encoding' => " . wfBoolToStr( $expect ) );
555 }
556
557 if( isset( $old_server_setting ) ) $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
558
559 }
560
561
562
563 function testSwapVarsTest() {
564
565
566 $var1 = 1;
567 $var2 = 2;
568
569 $this->assertEquals( $var1, 1, 'var1 is set originally' );
570 $this->assertEquals( $var2, 2, 'var1 is set originally' );
571
572 swap( $var1, $var2 );
573
574 $this->assertEquals( $var1, 2, 'var1 is swapped' );
575 $this->assertEquals( $var2, 1, 'var2 is swapped' );
576
577 }
578
579
580 function testWfPercentTest() {
581
582 $pcts = array(
583 array( 6/7, '0.86%', 2, false ),
584 array( 3/3, '1%' ),
585 array( 22/7, '3.14286%', 5 ),
586 array( 3/6, '0.5%' ),
587 array( 1/3, '0%', 0 ),
588 array( 10/3, '0%', -1 ),
589 array( 3/4/5, '0.1%', 1 ),
590 array( 6/7*8, '6.8571428571%', 10 ),
591 );
592
593 foreach( $pcts as $pct ) {
594 if( !isset( $pct[2] ) ) $pct[2] = 2;
595 if( !isset( $pct[3] ) ) $pct[3] = true;
596
597 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
598 }
599
600 }
601
602
603 function testInStringTest() {
604
605 $this->assertTrue( in_string( 'foo', 'foobar' ), 'foo is in foobar' );
606 $this->assertFalse( in_string( 'Bar', 'foobar' ), 'Case-sensitive by default' );
607 $this->assertTrue( in_string( 'Foo', 'foobar', true ), 'Case-insensitive when asked' );
608
609 }
610
611 /* TODO: many more! */
612 }
613
614
615 class MockOutputPage {
616
617 public $message;
618
619 function debug( $message ) {
620 $this->message = "JAJA is a stupid error message. Anyway, here's your message: $message";
621 }
622
623 function doNothing() {}
624 }
625