From: X! Date: Sun, 2 Jan 2011 04:38:04 +0000 (+0000) Subject: GlobalFunction additions: X-Git-Tag: 1.31.0-rc.0~32893 X-Git-Url: http://git.cyclocoop.org/%22.%20generer_url_ecrire%28%22sites_tous%22%2C%22%22%29.%20%22?a=commitdiff_plain;h=7d686e016418dd31c0e6e0e5e2bc9c9f81d070bd;p=lhc%2Fweb%2Fwiklou.git GlobalFunction additions: -in_string has a case-insensitive option -wfClientAcceptsGzip has a force option to force resetting the static value, useful for unit tests Unit tests for more global functions added --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index f5cb93f5fa..d9f5585ea2 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1273,9 +1273,9 @@ function wfNumLink( $offset, $limit, $title, $query = '' ) { * * @return bool Whereas client accept gzip compression */ -function wfClientAcceptsGzip() { +function wfClientAcceptsGzip( $force = false ) { static $result = null; - if ( $result === null ) { + if ( $result === null || $force ) { $result = false; if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) { # FIXME: we may want to blacklist some broken browsers @@ -2426,10 +2426,14 @@ function wfEmptyMsg( $key ) { * * @param $needle String * @param $str String + * @param $insensitive Boolean * @return Boolean */ -function in_string( $needle, $str ) { - return strpos( $str, $needle ) !== false; +function in_string( $needle, $str, $insensitive = false ) { + $func = 'strpos'; + if( $insensitive ) $func = 'stripos'; + + return $func( $str, $needle ) !== false; } function wfSpecialList( $page, $details ) { diff --git a/tests/phpunit/includes/GlobalTest.php b/tests/phpunit/includes/GlobalTest.php index 1cc3b3136e..cdea1fe071 100644 --- a/tests/phpunit/includes/GlobalTest.php +++ b/tests/phpunit/includes/GlobalTest.php @@ -529,6 +529,84 @@ class GlobalTest extends MediaWikiTestCase { $wgDebugLogFile = $old_log_file; } + + function testClientAcceptsGzipTest() { + + $settings = array( + 'gzip' => true, + 'bzip' => false, + '*' => false, + 'compress, gzip' => true, + 'gzip;q=1.0' => true, + 'foozip' => false, + 'foo*zip' => false, + 'gzip;q=abcde' => true, //is this REALLY valid? + 'gzip;q=12345678.9' => true, + ' gzip' => true, + ); + + if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING']; + + foreach ( $settings as $encoding => $expect ) { + $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding; + + $this->assertEquals( $expect, wfClientAcceptsGzip( true ), + "'$encoding' => " . wfBoolToStr( $expect ) ); + } + + if( isset( $old_server_setting ) ) $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting; + + } + + + + function testSwapVarsTest() { + + + $var1 = 1; + $var2 = 2; + + $this->assertEquals( $var1, 1, 'var1 is set originally' ); + $this->assertEquals( $var2, 2, 'var1 is set originally' ); + + swap( $var1, $var2 ); + + $this->assertEquals( $var1, 2, 'var1 is swapped' ); + $this->assertEquals( $var2, 1, 'var2 is swapped' ); + + } + + + function testWfPercentTest() { + + $pcts = array( + array( 6/7, '0.86%', 2, false ), + array( 3/3, '1%' ), + array( 22/7, '3.14286%', 5 ), + array( 3/6, '0.5%' ), + array( 1/3, '0%', 0 ), + array( 10/3, '0%', -1 ), + array( 3/4/5, '0.1%', 1 ), + array( 6/7*8, '6.8571428571%', 10 ), + ); + + foreach( $pcts as $pct ) { + if( !isset( $pct[2] ) ) $pct[2] = 2; + if( !isset( $pct[3] ) ) $pct[3] = true; + + $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] ); + } + + } + + + function testInStringTest() { + + $this->assertTrue( in_string( 'foo', 'foobar' ), 'foo is in foobar' ); + $this->assertFalse( in_string( 'Bar', 'foobar' ), 'Case-sensitive by default' ); + $this->assertTrue( in_string( 'Foo', 'foobar', true ), 'Case-insensitive when asked' ); + + } /* TODO: many more! */ }