MediaWiki  REL1_21
wfUrlencodeTest.php
Go to the documentation of this file.
00001 <?php
00007 class WfUrlencodeTest extends MediaWikiTestCase {
00008         #### TESTS ##############################################################
00009 
00011         public function testEncodingUrlWith( $input, $expected ) {
00012                 $this->verifyEncodingFor( 'Apache', $input, $expected );
00013         }
00014 
00016         public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
00017                 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
00018         }
00019 
00020         #### HELPERS #############################################################
00021 
00027         private function verifyEncodingFor( $server, $input, $expectations ) {
00028                 $expected = $this->extractExpect( $server, $expectations );
00029 
00030                 // save up global
00031                 $old = isset( $_SERVER['SERVER_SOFTWARE'] )
00032                         ? $_SERVER['SERVER_SOFTWARE']
00033                         : null;
00034                 $_SERVER['SERVER_SOFTWARE'] = $server;
00035                 wfUrlencode( null );
00036 
00037                 // do the requested test
00038                 $this->assertEquals(
00039                         $expected,
00040                         wfUrlencode( $input ),
00041                         "Encoding '$input' for server '$server' should be '$expected'"
00042                 );
00043 
00044                 // restore global
00045                 if ( $old === null ) {
00046                         unset( $_SERVER['SERVER_SOFTWARE'] );
00047                 } else {
00048                         $_SERVER['SERVER_SOFTWARE'] = $old;
00049                 }
00050                 wfUrlencode( null );
00051         }
00052 
00057         private function extractExpect( $server, $expectations ) {
00058                 if ( is_string( $expectations ) ) {
00059                         return $expectations;
00060                 } elseif ( is_array( $expectations ) ) {
00061                         if ( !array_key_exists( $server, $expectations ) ) {
00062                                 throw new MWException( __METHOD__ . " expectation does not have any value for server name $server. Check the provider array.\n" );
00063                         } else {
00064                                 return $expectations[$server];
00065                         }
00066                 } else {
00067                         throw new MWException( __METHOD__ . " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
00068                 }
00069         }
00070 
00071         #### PROVIDERS ###########################################################
00072 
00084         public static function provideURLS() {
00085                 return array(
00086                         ### RFC 1738 chars
00087                         // + is not safe
00088                         array( '+', '%2B' ),
00089                         // & and = not safe in queries
00090                         array( '&', '%26' ),
00091                         array( '=', '%3D' ),
00092 
00093                         array( ':', array(
00094                                 'Apache' => ':',
00095                                 'Microsoft-IIS/7' => '%3A',
00096                         ) ),
00097 
00098                         // remaining chars do not need encoding
00099                         array(
00100                                 ';@$-_.!*',
00101                                 ';@$-_.!*',
00102                         ),
00103 
00104                         ### Other tests
00105                         // slash remain unchanged. %2F seems to break things
00106                         array( '/', '/' ),
00107 
00108                         // Other 'funnies' chars
00109                         array( '[]', '%5B%5D' ),
00110                         array( '<>', '%3C%3E' ),
00111 
00112                         // Apostrophe is encoded
00113                         array( '\'', '%27' ),
00114                 );
00115         }
00116 }