basic tests for wfUrlencode
authorAntoine Musso <hashar@users.mediawiki.org>
Wed, 29 Jun 2011 20:28:43 +0000 (20:28 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Wed, 29 Jun 2011 20:28:43 +0000 (20:28 +0000)
Please note this function use a static variable based on the value
of $GLOBALS[’SERVER_SOFTWARE'].  With PHP 5.2 and PHPUnit 3.5.x, there
is currently now way to reset the static variable :-/   Tests relying on
a change of SERVER_SOFTWARE are thus skipped, still the framework is there
for later use.

tests/phpunit/includes/GlobalFunctions.php/wfUrlencodeTest.php [new file with mode: 0644]

diff --git a/tests/phpunit/includes/GlobalFunctions.php/wfUrlencodeTest.php b/tests/phpunit/includes/GlobalFunctions.php/wfUrlencodeTest.php
new file mode 100644 (file)
index 0000000..582fd35
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+/**
+ * Tests for includes/GlobalFunctions.php -> wfUrlencode()
+ *
+ * The function only need a string parameter and might react to IIS7.0
+ */
+
+class wfUrlencodeTest extends MediaWikiTestCase {
+
+       #### TESTS ##############################################################
+       
+       /** @dataProvider provideURLS */
+       public function testEncodingUrlWith( $input, $expected ) {
+               $this->verifyEncodingFor( 'Apache', $input, $expected );
+       }
+
+       /** @dataProvider provideURLS */
+       public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
+               $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
+       }
+
+       #### HELPERS #############################################################
+       
+       /**
+        * Internal helper that actually run the test.
+        * Called by the public methods testEncodingUrlWith...()
+        *
+        */
+       private function verifyEncodingFor( $server, $input, $expectations ) {
+               $expected = $this->extractExpect( $server, $expectations );
+
+               // save up global
+               $old = isset($_SERVER['SERVER_SOFTWARE'])
+                       ? $_SERVER['SERVER_SOFTWARE']
+                       : null
+               ;
+               $_SERVER['SERVER_SOFTWARE'] = $server;
+
+               // do the requested test
+               $this->assertEquals(
+                       $expected,
+                       wfUrlencode( $input ),
+                       "Encoding '$input' for server '$server' should be '$expected'"
+               );
+
+               // restore global
+               if( $old === null ) {
+                       unset( $_SERVER['SERVER_SOFTWARE'] );
+               } else {
+                       $_SERVER['SERVER_SOFTWARE'] = $old;
+               }
+       }
+
+       /**
+        * Interprets the provider array. Return expected value depending
+        * the HTTP server name.
+        */
+       private function extractExpect( $server, $expectations ) {
+               if( is_string( $expectations ) ) {
+                       return $expectations;
+               } elseif( is_array( $expectations ) ) {
+
+                       /**
+                        * FIXME FIXME FIXME FIXME
+                        * wfUrlencode use a static variable so we can not just
+                        * change the $GLOBALS server name :(
+                        */
+                       $this->markTestSkipped( 'FIXME: wfUrlencode() use a static, thus changing $GLOBALS[SERVER_SOFTWARE] is useless' );
+
+                       if( !array_key_exists( $server, $expectations ) ) {
+                               throw new MWException( __METHOD__ . " expectation does not have any value for server name $server. Check the provider array.\n" );
+                       } else {
+                               return $expectations[$server];
+                       }
+               } else {
+                       throw new MWException( __METHOD__ . " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
+               }
+       }  
+
+
+       #### PROVIDERS ###########################################################
+
+       /**
+        * Format is either:
+        *   array( 'input', 'expected' );
+        * Or:
+        *   array( 'input',
+        *       array( 'Apache', 'expected' ),
+        *       array( 'Microsoft-IIS/7', 'expected' ),
+        *    ),
+        * If you want to add other HTTP server name, you will have to add a new
+        * testing method much like the testEncodingUrlWith() method above. 
+        */
+       public function provideURLS() {
+               return array(
+               ### RFC 1738 chars      
+                       // + is not safe
+                       array( '+', '%2B' ),
+                       // & and = not safe in queries
+                       array( '&', '%26' ),
+                       array( '=', '%3D' ),
+
+                       array( ':', array(
+                               'Apache'          => ':',
+                               'Microsoft-IIS/7' => '%3A',
+                       ) ),
+
+                       // remaining chars do not need encoding
+                       array(
+                               ';@$-_.!*',
+                               ';@$-_.!*',
+                       ),
+
+               ### Other tests
+                       // slash remain unchanged. %2F seems to break things
+                       array( '/', '/' ),
+       
+                       // Other 'funnies' chars
+                       array( '[]', '%5B%5D' ),
+                       array( '<>', '%3C%3E' ),
+               );
+       }
+}