GlobalFunction additions:
authorX! <soxred93@users.mediawiki.org>
Sun, 2 Jan 2011 04:38:04 +0000 (04:38 +0000)
committerX! <soxred93@users.mediawiki.org>
Sun, 2 Jan 2011 04:38:04 +0000 (04:38 +0000)
-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

includes/GlobalFunctions.php
tests/phpunit/includes/GlobalTest.php

index f5cb93f..d9f5585 100644 (file)
@@ -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 ) {
index 1cc3b31..cdea1fe 100644 (file)
@@ -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! */
 }