1cbb5dd896c2f59066965fb9ee4235f0bbd06aed
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' );
636 * test @see wfShorthandToInteger()
637 * @dataProvider provideShorthand
639 public function testWfShorthandToInteger( $shorthand, $expected ) {
640 $this->assertEquals( $expected,
641 wfShorthandToInteger( $shorthand )
645 /** array( shorthand, expected integer ) */
646 public function provideShorthand() {
653 # Failures returns 0 :(
654 array( 'ABCDEFG', 0 ),
657 # Int, strings with spaces
661 array( ' 1023 ', 1023 ),
666 array( '1m', 1024 * 1024 ),
667 array( '1M', 1024 * 1024 ),
668 array( '1g', 1024 * 1024 * 1024 ),
669 array( '1G', 1024 * 1024 * 1024 ),
674 array( '-500', -500 ),
675 array( '-1k', -1024 ),
691 * test @see wfBCP47().
692 * Please note the BCP explicitly state that language codes are case
693 * insensitive, there are some exceptions to the rule :)
694 * This test is used to verify our formatting against all lower and
695 * all upper cases language code.
697 * @see http://tools.ietf.org/html/bcp47
698 * @dataProvider provideLanguageCodes()
700 function testBCP47( $code, $expected ) {
701 $code = strtolower( $code );
702 $this->assertEquals( $expected, wfBCP47($code),
703 "Applying BCP47 standard to lower case '$code'"
706 $code = strtoupper( $code );
707 $this->assertEquals( $expected, wfBCP47($code),
708 "Applying BCP47 standard to upper case '$code'"
713 * Array format is ($code, $expected)
715 function provideLanguageCodes() {
717 // Extracted from BCP47 (list not exhaustive)
719 array( 'en-ca-x-ca' , 'en-CA-x-ca' ),
720 array( 'sgn-be-fr' , 'sgn-BE-FR' ),
721 array( 'az-latn-x-latn', 'az-Latn-x-latn' ),
723 array( 'sr-Latn-RS', 'sr-Latn-RS' ),
724 array( 'az-arab-ir', 'az-Arab-IR' ),
727 array( 'sl-nedis' , 'sl-nedis' ),
728 array( 'de-ch-1996', 'de-CH-1996' ),
732 'en-latn-gb-boont-r-extended-sequence-x-private',
733 'en-Latn-GB-boont-r-extended-sequence-x-private'
736 // Examples from BCP47 Appendix A
737 # Simple language subtag:
742 # Language subtag plus script subtag:
743 array( 'zh-hans', 'zh-Hans'),
744 array( 'sr-cyrl', 'sr-Cyrl'),
745 array( 'sr-latn', 'sr-Latn'),
747 # Extended language subtags and their primary language subtag
749 array( 'zh-cmn-hans-cn', 'zh-cmn-Hans-CN' ),
750 array( 'cmn-hans-cn' , 'cmn-Hans-CN' ),
751 array( 'zh-yue-hk' , 'zh-yue-HK' ),
752 array( 'yue-hk' , 'yue-HK' ),
754 # Language-Script-Region:
755 array( 'zh-hans-cn', 'zh-Hans-CN' ),
756 array( 'sr-latn-RS', 'sr-Latn-RS' ),
759 array( 'sl-rozaj' , 'sl-rozaj' ),
760 array( 'sl-rozaj-biske', 'sl-rozaj-biske' ),
761 array( 'sl-nedis' , 'sl-nedis' ),
763 # Language-Region-Variant:
764 array( 'de-ch-1901' , 'de-CH-1901' ),
765 array( 'sl-it-nedis' , 'sl-IT-nedis' ),
767 # Language-Script-Region-Variant:
768 array( 'hy-latn-it-arevela', 'hy-Latn-IT-arevela' ),
771 array( 'de-de' , 'de-DE' ),
772 array( 'en-us' , 'en-US' ),
773 array( 'es-419', 'es-419'),
775 # Private use subtags:
776 array( 'de-ch-x-phonebk' , 'de-CH-x-phonebk' ),
777 array( 'az-arab-x-aze-derbend', 'az-Arab-x-aze-derbend' ),
779 * Previous test does not reflect the BCP which states:
780 * az-Arab-x-AZE-derbend
781 * AZE being private, it should be lower case, hence the test above
782 * should probably be:
783 #array( 'az-arab-x-aze-derbend', 'az-Arab-x-AZE-derbend' ),
786 # Private use registry values:
787 array( 'x-whatever', 'x-whatever' ),
788 array( 'qaa-qaaa-qm-x-southern', 'qaa-Qaaa-QM-x-southern' ),
789 array( 'de-qaaa' , 'de-Qaaa' ),
790 array( 'sr-latn-qm', 'sr-Latn-QM' ),
791 array( 'sr-qaaa-rs', 'sr-Qaaa-RS' ),
793 # Tags that use extensions
794 array( 'en-us-u-islamcal', 'en-US-u-islamcal' ),
795 array( 'zh-cn-a-myext-x-private', 'zh-CN-a-myext-x-private' ),
796 array( 'en-a-myext-b-another', 'en-a-myext-b-another' ),
801 // ar-a-aaa-b-bbb-a-ccc
805 array( 'sr-Cyrl', 'sr-Cyrl' ),
806 array( 'SR-lATN', 'sr-Latn' ), # FIXME fix our function?
807 array( 'fr-latn', 'fr-Latn' ),
808 // Use lowercase for single segment
809 // ISO 3166-1-alpha-2 code
810 array( 'US', 'us' ), # USA
811 array( 'uS', 'us' ), # USA
812 array( 'Fr', 'fr' ), # France
813 array( 'va', 'va' ), # Holy See (Vatican City State)
817 /* TODO: many more! */
821 class MockOutputPage
{
825 function debug( $message ) {
826 $this->message
= "JAJA is a stupid error message. Anyway, here's your message: $message";
829 function doNothing() {}