MediaWiki  REL1_19
wfUrlencodeTest.php
Go to the documentation of this file.
00001 <?php
00008 class wfUrlencodeTest extends MediaWikiTestCase {
00009 
00010         #### TESTS ##############################################################
00011         
00013         public function testEncodingUrlWith( $input, $expected ) {
00014                 $this->verifyEncodingFor( 'Apache', $input, $expected );
00015         }
00016 
00018         public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
00019                 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
00020         }
00021 
00022         #### HELPERS #############################################################
00023         
00029         private function verifyEncodingFor( $server, $input, $expectations ) {
00030                 $expected = $this->extractExpect( $server, $expectations );
00031 
00032                 // save up global
00033                 $old = isset($_SERVER['SERVER_SOFTWARE'])
00034                         ? $_SERVER['SERVER_SOFTWARE']
00035                         : null
00036                 ;
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 
00075         #### PROVIDERS ###########################################################
00076 
00088         public function provideURLS() {
00089                 return array(
00090                 ### RFC 1738 chars      
00091                         // + is not safe
00092                         array( '+', '%2B' ),
00093                         // & and = not safe in queries
00094                         array( '&', '%26' ),
00095                         array( '=', '%3D' ),
00096 
00097                         array( ':', array(
00098                                 'Apache'          => ':',
00099                                 'Microsoft-IIS/7' => '%3A',
00100                         ) ),
00101 
00102                         // remaining chars do not need encoding
00103                         array(
00104                                 ';@$-_.!*',
00105                                 ';@$-_.!*',
00106                         ),
00107 
00108                 ### Other tests
00109                         // slash remain unchanged. %2F seems to break things
00110                         array( '/', '/' ),
00111         
00112                         // Other 'funnies' chars
00113                         array( '[]', '%5B%5D' ),
00114                         array( '<>', '%3C%3E' ),
00115 
00116                         // Apostrophe is encoded
00117                         array( '\'', '%27' ),
00118                 );
00119         }
00120 }