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