4 * The function only need a string parameter and might react to IIS7.0
6 * @group GlobalFunctions
7 * @covers ::wfUrlencode
9 class WfUrlencodeTest
extends MediaWikiTestCase
{
10 # ### TESTS ##############################################################
13 * @dataProvider provideURLS
15 public function testEncodingUrlWith( $input, $expected ) {
16 $this->verifyEncodingFor( 'Apache', $input, $expected );
20 * @dataProvider provideURLS
22 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
23 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
26 # ### HELPERS #############################################################
29 * Internal helper that actually run the test.
30 * Called by the public methods testEncodingUrlWith...()
32 private function verifyEncodingFor( $server, $input, $expectations ) {
33 $expected = $this->extractExpect( $server, $expectations );
36 $old = isset( $_SERVER['SERVER_SOFTWARE'] )
37 ?
$_SERVER['SERVER_SOFTWARE']
39 $_SERVER['SERVER_SOFTWARE'] = $server;
42 // do the requested test
45 wfUrlencode( $input ),
46 "Encoding '$input' for server '$server' should be '$expected'"
50 if ( $old === null ) {
51 unset( $_SERVER['SERVER_SOFTWARE'] );
53 $_SERVER['SERVER_SOFTWARE'] = $old;
59 * Interprets the provider array. Return expected value depending
60 * the HTTP server name.
62 private function extractExpect( $server, $expectations ) {
63 if ( is_string( $expectations ) ) {
65 } elseif ( is_array( $expectations ) ) {
66 if ( !array_key_exists( $server, $expectations ) ) {
67 throw new MWException( __METHOD__
. " expectation does not have any "
68 . "value for server name $server. Check the provider array.\n" );
70 return $expectations[$server];
73 throw new MWException( __METHOD__
. " given invalid expectation for "
74 . "'$server'. Should be a string or an array( <http server name> => <string> ).\n" );
78 # ### PROVIDERS ###########################################################
82 * [ 'input', 'expected' ];
85 * [ 'Apache', 'expected' ],
86 * [ 'Microsoft-IIS/7', 'expected' ],
88 * If you want to add other HTTP server name, you will have to add a new
89 * testing method much like the testEncodingUrlWith() method above.
91 public static function provideURLS() {
96 // & and = not safe in queries
102 'Microsoft-IIS/7' => '%3A',
105 // remaining chars do not need encoding
112 // slash remain unchanged. %2F seems to break things
117 // Other 'funnies' chars
121 // Apostrophe is encoded