MediaWiki  REL1_24
wfUrlencodeTest.php
Go to the documentation of this file.
00001 <?php
00002 
00009 class WfUrlencodeTest extends MediaWikiTestCase {
00010     #### TESTS ##############################################################
00011 
00015     public function testEncodingUrlWith( $input, $expected ) {
00016         $this->verifyEncodingFor( 'Apache', $input, $expected );
00017     }
00018 
00022     public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
00023         $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
00024     }
00025 
00026     #### HELPERS #############################################################
00027 
00033     private function verifyEncodingFor( $server, $input, $expectations ) {
00034         $expected = $this->extractExpect( $server, $expectations );
00035 
00036         // save up global
00037         $old = isset( $_SERVER['SERVER_SOFTWARE'] )
00038             ? $_SERVER['SERVER_SOFTWARE']
00039             : null;
00040         $_SERVER['SERVER_SOFTWARE'] = $server;
00041         wfUrlencode( null );
00042 
00043         // do the requested test
00044         $this->assertEquals(
00045             $expected,
00046             wfUrlencode( $input ),
00047             "Encoding '$input' for server '$server' should be '$expected'"
00048         );
00049 
00050         // restore global
00051         if ( $old === null ) {
00052             unset( $_SERVER['SERVER_SOFTWARE'] );
00053         } else {
00054             $_SERVER['SERVER_SOFTWARE'] = $old;
00055         }
00056         wfUrlencode( null );
00057     }
00058 
00063     private function extractExpect( $server, $expectations ) {
00064         if ( is_string( $expectations ) ) {
00065             return $expectations;
00066         } elseif ( is_array( $expectations ) ) {
00067             if ( !array_key_exists( $server, $expectations ) ) {
00068                 throw new MWException( __METHOD__ . " expectation does not have any "
00069                     . "value for server name $server. Check the provider array.\n" );
00070             } else {
00071                 return $expectations[$server];
00072             }
00073         } else {
00074             throw new MWException( __METHOD__ . " given invalid expectation for "
00075                 . "'$server'. Should be a string or an array( <http server name> => <string> ).\n" );
00076         }
00077     }
00078 
00079     #### PROVIDERS ###########################################################
00080 
00092     public static function provideURLS() {
00093         return array(
00094             ### RFC 1738 chars
00095             // + is not safe
00096             array( '+', '%2B' ),
00097             // & and = not safe in queries
00098             array( '&', '%26' ),
00099             array( '=', '%3D' ),
00100 
00101             array( ':', array(
00102                 'Apache' => ':',
00103                 'Microsoft-IIS/7' => '%3A',
00104             ) ),
00105 
00106             // remaining chars do not need encoding
00107             array(
00108                 ';@$-_.!*',
00109                 ';@$-_.!*',
00110             ),
00111 
00112             ### Other tests
00113             // slash remain unchanged. %2F seems to break things
00114             array( '/', '/' ),
00115 
00116             // Other 'funnies' chars
00117             array( '[]', '%5B%5D' ),
00118             array( '<>', '%3C%3E' ),
00119 
00120             // Apostrophe is encoded
00121             array( '\'', '%27' ),
00122         );
00123     }
00124 }