3 * Tests for wfUrlencode()
5 * The function only need a string parameter and might react to IIS7.0
7 class WfUrlencodeTest
extends MediaWikiTestCase
{
8 #### TESTS ##############################################################
10 /** @dataProvider provideURLS */
11 public function testEncodingUrlWith( $input, $expected ) {
12 $this->verifyEncodingFor( 'Apache', $input, $expected );
15 /** @dataProvider provideURLS */
16 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
17 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
20 #### HELPERS #############################################################
23 * Internal helper that actually run the test.
24 * Called by the public methods testEncodingUrlWith...()
27 private function verifyEncodingFor( $server, $input, $expectations ) {
28 $expected = $this->extractExpect( $server, $expectations );
31 $old = isset( $_SERVER['SERVER_SOFTWARE'] )
32 ?
$_SERVER['SERVER_SOFTWARE']
34 $_SERVER['SERVER_SOFTWARE'] = $server;
37 // do the requested test
40 wfUrlencode( $input ),
41 "Encoding '$input' for server '$server' should be '$expected'"
45 if ( $old === null ) {
46 unset( $_SERVER['SERVER_SOFTWARE'] );
48 $_SERVER['SERVER_SOFTWARE'] = $old;
54 * Interprets the provider array. Return expected value depending
55 * the HTTP server name.
57 private function extractExpect( $server, $expectations ) {
58 if ( is_string( $expectations ) ) {
60 } elseif ( is_array( $expectations ) ) {
61 if ( !array_key_exists( $server, $expectations ) ) {
62 throw new MWException( __METHOD__
. " expectation does not have any value for server name $server. Check the provider array.\n" );
64 return $expectations[$server];
67 throw new MWException( __METHOD__
. " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
71 #### PROVIDERS ###########################################################
75 * array( 'input', 'expected' );
78 * array( 'Apache', 'expected' ),
79 * array( 'Microsoft-IIS/7', 'expected' ),
81 * If you want to add other HTTP server name, you will have to add a new
82 * testing method much like the testEncodingUrlWith() method above.
84 public static function provideURLS() {
89 // & and = not safe in queries
95 'Microsoft-IIS/7' => '%3A',
98 // remaining chars do not need encoding
105 // slash remain unchanged. %2F seems to break things
108 // Other 'funnies' chars
109 array( '[]', '%5B%5D' ),
110 array( '<>', '%3C%3E' ),
112 // Apostrophe is encoded
113 array( '\'', '%27' ),