3 class GlobalTest
extends MediaWikiTestCase
{
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' );
13 global $wgReadOnlyFile;
14 if ( file_exists( $wgReadOnlyFile ) ) {
15 unlink( $wgReadOnlyFile );
17 $wgReadOnlyFile = $this->originals
['wgReadOnlyFile'];
20 /** @dataProvider provideForWfArrayDiff2 */
21 public function testWfArrayDiff2( $a, $b, $expected ) {
23 wfArrayDiff2( $a, $b), $expected
27 // @todo Provide more tests
28 public function provideForWfArrayDiff2() {
37 array( array( 'a'), array( 'a', 'b', 'c' )),
38 array( array( 'a'), array( 'a', 'b' )),
39 array( 1 => array( 'a', 'b', 'c' ) ),
44 function testRandom() {
45 # This could hypothetically fail, but it shouldn't ;)
47 wfRandom() == wfRandom() );
50 function testUrlencode() {
52 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
53 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
56 function testReadOnlyEmpty() {
60 $this->assertFalse( wfReadOnly() );
61 $this->assertFalse( wfReadOnly() );
64 function testReadOnlySet() {
65 global $wgReadOnly, $wgReadOnlyFile;
67 $f = fopen( $wgReadOnlyFile, "wt" );
68 fwrite( $f, 'Message' );
70 $wgReadOnly = null; # Check on $wgReadOnlyFile
72 $this->assertTrue( wfReadOnly() );
73 $this->assertTrue( wfReadOnly() ); # Check cached
75 unlink( $wgReadOnlyFile );
76 $wgReadOnly = null; # Clean cache
78 $this->assertFalse( wfReadOnly() );
79 $this->assertFalse( wfReadOnly() );
82 function testQuotedPrintable() {
84 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
85 UserMailer
::quotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
90 $this->assertInternalType( 'float', $start );
92 $this->assertTrue( $end > $start, "Time is running backwards!" );
95 function testArrayToCGI() {
99 array( 'baz' => 'AT&T', 'ignore' => '' ),
100 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
103 function testMimeTypeMatch() {
106 mimeTypeMatch( 'text/html',
107 array( 'application/xhtml+xml' => 1.0,
109 'text/plain' => 0.3 ) ) );
112 mimeTypeMatch( 'text/html',
113 array( 'image/*' => 1.0,
114 'text/*' => 0.5 ) ) );
117 mimeTypeMatch( 'text/html',
118 array( '*/*' => 1.0 ) ) );
120 mimeTypeMatch( 'text/html',
121 array( 'image/png' => 1.0,
122 'image/svg+xml' => 0.5 ) ) );
125 function testNegotiateType() {
129 array( 'application/xhtml+xml' => 1.0,
133 array( 'text/html' => 1.0 ) ) );
135 'application/xhtml+xml',
137 array( 'application/xhtml+xml' => 1.0,
141 array( 'application/xhtml+xml' => 1.0,
142 'text/html' => 0.5 ) ) );
146 array( 'text/html' => 1.0,
149 'application/xhtml+xml' => 0.2 ),
150 array( 'application/xhtml+xml' => 1.0,
151 'text/html' => 0.5 ) ) );
155 array( 'text/*' => 1.0,
158 array( 'application/xhtml+xml' => 1.0,
159 'text/html' => 0.5 ) ) );
162 array( 'text/*' => 1.0 ),
163 array( 'application/xhtml+xml' => 1.0 ) ) );
166 function testTimestamp() {
167 $t = gmmktime( 12, 34, 56, 1, 15, 2001 );
170 wfTimestamp( TS_MW
, $t ),
171 'TS_UNIX to TS_MW' );
174 wfTimestamp( TS_MW
, -30281104 ),
175 'Negative TS_UNIX to TS_MW' );
178 wfTimestamp( TS_UNIX
, $t ),
179 'TS_UNIX to TS_UNIX' );
181 '2001-01-15 12:34:56',
182 wfTimestamp( TS_DB
, $t ),
183 'TS_UNIX to TS_DB' );
186 wfTimestamp( TS_ISO_8601_BASIC
, $t ),
187 'TS_ISO_8601_BASIC to TS_DB' );
191 wfTimestamp( TS_MW
, '20010115123456' ),
195 wfTimestamp( TS_UNIX
, '20010115123456' ),
196 'TS_MW to TS_UNIX' );
198 '2001-01-15 12:34:56',
199 wfTimestamp( TS_DB
, '20010115123456' ),
203 wfTimestamp( TS_ISO_8601_BASIC
, '20010115123456' ),
204 'TS_MW to TS_ISO_8601_BASIC' );
208 wfTimestamp( TS_MW
, '2001-01-15 12:34:56' ),
212 wfTimestamp( TS_UNIX
, '2001-01-15 12:34:56' ),
213 'TS_DB to TS_UNIX' );
215 '2001-01-15 12:34:56',
216 wfTimestamp( TS_DB
, '2001-01-15 12:34:56' ),
220 wfTimestamp( TS_ISO_8601_BASIC
, '2001-01-15 12:34:56' ),
221 'TS_DB to TS_ISO_8601_BASIC' );
223 # rfc2822 section 3.3
226 'Mon, 15 Jan 2001 12:34:56 GMT',
227 wfTimestamp( TS_RFC2822
, '20010115123456' ),
228 'TS_MW to TS_RFC2822' );
232 wfTimestamp( TS_MW
, 'Mon, 15 Jan 2001 12:34:56 GMT' ),
233 'TS_RFC2822 to TS_MW' );
237 wfTimestamp( TS_MW
, ' Mon, 15 Jan 2001 12:34:56 GMT' ),
238 'TS_RFC2822 with leading space to TS_MW' );
242 wfTimestamp( TS_MW
, '15 Jan 2001 12:34:56 GMT' ),
243 'TS_RFC2822 without optional day-of-week to TS_MW' );
245 # FWS = ([*WSP CRLF] 1*WSP) / obs-FWS ; Folding white space
246 # obs-FWS = 1*WSP *(CRLF 1*WSP) ; Section 4.2
249 wfTimestamp( TS_MW
, 'Mon, 15 Jan 2001 12:34:56 GMT' ),
250 'TS_RFC2822 to TS_MW' );
252 # WSP = SP / HTAB ; rfc2234
255 wfTimestamp( TS_MW
, "Mon, 15 Jan\x092001 12:34:56 GMT" ),
256 'TS_RFC2822 with HTAB to TS_MW' );
260 wfTimestamp( TS_MW
, "Mon, 15 Jan\x09 \x09 2001 12:34:56 GMT" ),
261 'TS_RFC2822 with HTAB and SP to TS_MW' );
265 wfTimestamp( TS_MW
, "Sun, 6 Nov 94 08:49:37 GMT" ),
266 'TS_RFC2822 with obsolete year to TS_MW' );
270 * This test checks wfTimestamp() with values outside.
271 * It needs PHP 64 bits or PHP > 5.1.
272 * See r74778 and bug 25451
274 function testOldTimestamps() {
275 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:54 GMT',
276 wfTimestamp( TS_RFC2822
, '19011213204554' ),
277 'Earliest time according to php documentation' );
279 $this->assertEquals( 'Tue, 19 Jan 2038 03:14:07 GMT',
280 wfTimestamp( TS_RFC2822
, '20380119031407' ),
281 'Latest 32 bit time' );
283 $this->assertEquals( '-2147483648',
284 wfTimestamp( TS_UNIX
, '19011213204552' ),
285 'Earliest 32 bit unix time' );
287 $this->assertEquals( '2147483647',
288 wfTimestamp( TS_UNIX
, '20380119031407' ),
289 'Latest 32 bit unix time' );
291 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:52 GMT',
292 wfTimestamp( TS_RFC2822
, '19011213204552' ),
293 'Earliest 32 bit time' );
295 $this->assertEquals( 'Fri, 13 Dec 1901 20:45:51 GMT',
296 wfTimestamp( TS_RFC2822
, '19011213204551' ),
297 'Earliest 32 bit time - 1' );
299 $this->assertEquals( 'Tue, 19 Jan 2038 03:14:08 GMT',
300 wfTimestamp( TS_RFC2822
, '20380119031408' ),
301 'Latest 32 bit time + 1' );
303 $this->assertEquals( '19011212000000',
304 wfTimestamp(TS_MW
, '19011212000000'),
305 'Convert to itself r74778#c10645' );
307 $this->assertEquals( '-2147483649',
308 wfTimestamp( TS_UNIX
, '19011213204551' ),
309 'Earliest 32 bit unix time - 1' );
311 $this->assertEquals( '2147483648',
312 wfTimestamp( TS_UNIX
, '20380119031408' ),
313 'Latest 32 bit unix time + 1' );
315 $this->assertEquals( '19011213204551',
316 wfTimestamp( TS_MW
, '-2147483649' ),
317 '1901 negative unix time to MediaWiki' );
319 $this->assertEquals( '18010115123456',
320 wfTimestamp( TS_MW
, '-5331871504' ),
321 '1801 negative unix time to MediaWiki' );
323 $this->assertEquals( 'Tue, 09 Aug 0117 12:34:56 GMT',
324 wfTimestamp( TS_RFC2822
, '0117-08-09 12:34:56'),
325 'Death of Roman Emperor [[Trajan]]');
327 /* FIXME: 00 to 101 years are taken as being in [1970-2069] */
329 $this->assertEquals( 'Sun, 01 Jan 0101 00:00:00 GMT',
330 wfTimestamp( TS_RFC2822
, '-58979923200'),
333 $this->assertEquals( 'Mon, 01 Jan 0001 00:00:00 GMT',
334 wfTimestamp( TS_RFC2822
, '-62135596800'),
337 /* It is not clear if we should generate a year 0 or not
338 * We are completely off RFC2822 requirement of year being
341 $this->assertEquals( 'Wed, 18 Oct 0000 00:00:00 GMT',
342 wfTimestamp( TS_RFC2822
, '-62142076800'),
343 'ISO 8601:2004 [[year 0]], also called [[1 BC]]');
346 function testHttpDate() {
347 # The Resource Loader uses wfTimestamp() to convert timestamps
348 # from If-Modified-Since header.
349 # Thus it must be able to parse all rfc2616 date formats
350 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
354 wfTimestamp( TS_MW
, 'Sun, 06 Nov 1994 08:49:37 GMT' ),
359 wfTimestamp( TS_MW
, 'Sunday, 06-Nov-94 08:49:37 GMT' ),
364 wfTimestamp( TS_MW
, 'Sun Nov 6 08:49:37 1994' ),
365 "ANSI C's asctime() format" );
367 // See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html and r77171
370 wfTimestamp( TS_MW
, 'Mon, 22 Nov 2010 14:12:42 GMT; length=52626' ),
371 "Netscape extension to HTTP/1.0" );
375 function testTimestampParameter() {
376 // There are a number of assumptions in our codebase where wfTimestamp() should give
377 // the current date but it is not given a 0 there. See r71751 CR
379 $now = wfTimestamp( TS_UNIX
);
380 // We check that wfTimestamp doesn't return false (error) and use a LessThan assert
381 // for the cases where the test is run in a second boundary.
383 $zero = wfTimestamp( TS_UNIX
, 0 );
384 $this->assertNotEquals( false, $zero );
385 $this->assertLessThan( 5, $zero - $now );
387 $empty = wfTimestamp( TS_UNIX
, '' );
388 $this->assertNotEquals( false, $empty );
389 $this->assertLessThan( 5, $empty - $now );
391 $null = wfTimestamp( TS_UNIX
, null );
392 $this->assertNotEquals( false, $null );
393 $this->assertLessThan( 5, $null - $now );
396 function testBasename() {
410 '\\aaaa\\' => 'aaaa',
411 '\\aaaa\\' => 'aaaa',
412 '/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',
413 'C:\\Progra~1\\Wikime~1\\Wikipe~1\\VIEWER.EXE' => 'VIEWER.EXE',
414 'Östergötland_coat_of_arms.png' => 'Östergötland_coat_of_arms.png',
416 foreach ( $sets as $from => $to ) {
417 $this->assertEquals( $to, wfBaseName( $from ),
418 "wfBaseName('$from') => '$to'" );
423 function testFallbackMbstringFunctions() {
425 if( !extension_loaded( 'mbstring' ) ) {
426 $this->markTestSkipped( "The mb_string functions must be installed to test the fallback functions" );
429 $sampleUTF = "Östergötland_coat_of_arms.png";
433 $substr_params = array(
443 foreach( $substr_params as $param_set ) {
444 $old_param_set = $param_set;
445 array_unshift( $param_set, $sampleUTF );
448 MWFunction
::callArray( 'mb_substr', $param_set ),
449 MWFunction
::callArray( 'Fallback::mb_substr', $param_set ),
450 'Fallback mb_substr with params ' . implode( ', ', $old_param_set )
457 mb_strlen( $sampleUTF ),
458 Fallback
::mb_strlen( $sampleUTF ),
464 $strpos_params = array(
468 //array( 'oat_', 100 ),
473 foreach( $strpos_params as $param_set ) {
474 $old_param_set = $param_set;
475 array_unshift( $param_set, $sampleUTF );
478 MWFunction
::callArray( 'mb_strpos', $param_set ),
479 MWFunction
::callArray( 'Fallback::mb_strpos', $param_set ),
480 'Fallback mb_strpos with params ' . implode( ', ', $old_param_set )
484 MWFunction
::callArray( 'mb_strrpos', $param_set ),
485 MWFunction
::callArray( 'Fallback::mb_strrpos', $param_set ),
486 'Fallback mb_strrpos with params ' . implode( ', ', $old_param_set )
493 function testDebugFunctionTest() {
495 global $wgDebugLogFile, $wgOut, $wgShowDebug;
497 $old_log_file = $wgDebugLogFile;
498 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
502 wfDebug( "This is a normal string" );
503 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
504 unlink( $wgDebugLogFile );
507 wfDebug( "This is nöt an ASCII string" );
508 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
509 unlink( $wgDebugLogFile );
512 wfDebug( "\00305This has böth UTF and control chars\003" );
513 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
514 unlink( $wgDebugLogFile );
519 $old_wgShowDebug = $wgShowDebug;
521 $wgOut = new StubObject( 'wgOut', 'MockOutputPage' );
522 $wgOut->doNothing(); //just to unstub it
526 $message = "\00305This has böth UTF and control chars\003";
530 if( $wgOut->message
== "JAJA is a stupid error message. Anyway, here's your message: $message" ) {
531 $this->assertTrue( true, 'MockOutputPage called, set the proper message.' );
534 $this->assertTrue( false, 'MockOutputPage was not called.' );
538 $wgShowDebug = $old_wgShowDebug;
539 unlink( $wgDebugLogFile );
544 $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
545 unlink( $wgDebugLogFile );
548 $this->assertGreaterThan( 5000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
549 unlink( $wgDebugLogFile );
553 $wgDebugLogFile = $old_log_file;
557 function testClientAcceptsGzipTest() {
563 'compress, gzip' => true,
564 'gzip;q=1.0' => true,
567 'gzip;q=abcde' => true, //is this REALLY valid?
568 'gzip;q=12345678.9' => true,
572 if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
574 foreach ( $settings as $encoding => $expect ) {
575 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
577 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
578 "'$encoding' => " . wfBoolToStr( $expect ) );
581 if( isset( $old_server_setting ) ) $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
587 function testSwapVarsTest() {
593 $this->assertEquals( $var1, 1, 'var1 is set originally' );
594 $this->assertEquals( $var2, 2, 'var1 is set originally' );
596 swap( $var1, $var2 );
598 $this->assertEquals( $var1, 2, 'var1 is swapped' );
599 $this->assertEquals( $var2, 1, 'var2 is swapped' );
604 function testWfPercentTest() {
607 array( 6/7, '0.86%', 2, false ),
609 array( 22/7, '3.14286%', 5 ),
610 array( 3/6, '0.5%' ),
611 array( 1/3, '0%', 0 ),
612 array( 10/3, '0%', -1 ),
613 array( 3/4/5, '0.1%', 1 ),
614 array( 6/7*8, '6.8571428571%', 10 ),
617 foreach( $pcts as $pct ) {
618 if( !isset( $pct[2] ) ) $pct[2] = 2;
619 if( !isset( $pct[3] ) ) $pct[3] = true;
621 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
627 function testInStringTest() {
629 $this->assertTrue( in_string( 'foo', 'foobar' ), 'foo is in foobar' );
630 $this->assertFalse( in_string( 'Bar', 'foobar' ), 'Case-sensitive by default' );
631 $this->assertTrue( in_string( 'Foo', 'foobar', true ), 'Case-insensitive when asked' );
635 /* TODO: many more! */
639 class MockOutputPage
{
643 function debug( $message ) {
644 $this->message
= "JAJA is a stupid error message. Anyway, here's your message: $message";
647 function doNothing() {}