3 * Tests for includes/GlobalFunctions.php -> wfUrlencode()
5 * The function only need a string parameter and might react to IIS7.0
8 class wfUrlencodeTest
extends MediaWikiTestCase
{
9 #### TESTS ##############################################################
11 /** @dataProvider provideURLS */
12 public function testEncodingUrlWith( $input, $expected ) {
13 $this->verifyEncodingFor( 'Apache', $input, $expected );
16 /** @dataProvider provideURLS */
17 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
18 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
21 #### HELPERS #############################################################
24 * Internal helper that actually run the test.
25 * Called by the public methods testEncodingUrlWith...()
28 private function verifyEncodingFor( $server, $input, $expectations ) {
29 $expected = $this->extractExpect( $server, $expectations );
32 $old = isset( $_SERVER['SERVER_SOFTWARE'] )
33 ?
$_SERVER['SERVER_SOFTWARE']
35 $_SERVER['SERVER_SOFTWARE'] = $server;
38 // do the requested test
41 wfUrlencode( $input ),
42 "Encoding '$input' for server '$server' should be '$expected'"
46 if ( $old === null ) {
47 unset( $_SERVER['SERVER_SOFTWARE'] );
49 $_SERVER['SERVER_SOFTWARE'] = $old;
55 * Interprets the provider array. Return expected value depending
56 * the HTTP server name.
58 private function extractExpect( $server, $expectations ) {
59 if ( is_string( $expectations ) ) {
61 } elseif ( is_array( $expectations ) ) {
62 if ( !array_key_exists( $server, $expectations ) ) {
63 throw new MWException( __METHOD__
. " expectation does not have any value for server name $server. Check the provider array.\n" );
65 return $expectations[$server];
68 throw new MWException( __METHOD__
. " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
72 #### PROVIDERS ###########################################################
76 * array( 'input', 'expected' );
79 * array( 'Apache', 'expected' ),
80 * array( 'Microsoft-IIS/7', 'expected' ),
82 * If you want to add other HTTP server name, you will have to add a new
83 * testing method much like the testEncodingUrlWith() method above.
85 public static function provideURLS() {
90 // & and = not safe in queries
96 'Microsoft-IIS/7' => '%3A',
99 // remaining chars do not need encoding
106 // slash remain unchanged. %2F seems to break things
109 // Other 'funnies' chars
110 array( '[]', '%5B%5D' ),
111 array( '<>', '%3C%3E' ),
113 // Apostrophe is encoded
114 array( '\'', '%27' ),