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
{
10 #### TESTS ##############################################################
12 /** @dataProvider provideURLS */
13 public function testEncodingUrlWith( $input, $expected ) {
14 $this->verifyEncodingFor( 'Apache', $input, $expected );
17 /** @dataProvider provideURLS */
18 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
19 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
22 #### HELPERS #############################################################
25 * Internal helper that actually run the test.
26 * Called by the public methods testEncodingUrlWith...()
29 private function verifyEncodingFor( $server, $input, $expectations ) {
30 $expected = $this->extractExpect( $server, $expectations );
33 $old = isset($_SERVER['SERVER_SOFTWARE'])
34 ?
$_SERVER['SERVER_SOFTWARE']
37 $_SERVER['SERVER_SOFTWARE'] = $server;
39 // do the requested test
42 wfUrlencode( $input ),
43 "Encoding '$input' for server '$server' should be '$expected'"
48 unset( $_SERVER['SERVER_SOFTWARE'] );
50 $_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 ) ) {
64 * FIXME FIXME FIXME FIXME
65 * wfUrlencode use a static variable so we can not just
66 * change the $GLOBALS server name :(
68 $this->markTestSkipped( 'FIXME: wfUrlencode() use a static, thus changing $GLOBALS[SERVER_SOFTWARE] is useless' );
70 if( !array_key_exists( $server, $expectations ) ) {
71 throw new MWException( __METHOD__
. " expectation does not have any value for server name $server. Check the provider array.\n" );
73 return $expectations[$server];
76 throw new MWException( __METHOD__
. " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
81 #### PROVIDERS ###########################################################
85 * array( 'input', 'expected' );
88 * array( 'Apache', 'expected' ),
89 * array( 'Microsoft-IIS/7', 'expected' ),
91 * If you want to add other HTTP server name, you will have to add a new
92 * testing method much like the testEncodingUrlWith() method above.
94 public function provideURLS() {
99 // & and = not safe in queries
105 'Microsoft-IIS/7' => '%3A',
108 // remaining chars do not need encoding
115 // slash remain unchanged. %2F seems to break things
118 // Other 'funnies' chars
119 array( '[]', '%5B%5D' ),
120 array( '<>', '%3C%3E' ),
122 // Apostrophe is encoded
123 array( '\'', '%27' ),