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...()
33 private function verifyEncodingFor( $server, $input, $expectations ) {
34 $expected = $this->extractExpect( $server, $expectations );
37 $old = isset( $_SERVER['SERVER_SOFTWARE'] )
38 ?
$_SERVER['SERVER_SOFTWARE']
40 $_SERVER['SERVER_SOFTWARE'] = $server;
43 // do the requested test
46 wfUrlencode( $input ),
47 "Encoding '$input' for server '$server' should be '$expected'"
51 if ( $old === null ) {
52 unset( $_SERVER['SERVER_SOFTWARE'] );
54 $_SERVER['SERVER_SOFTWARE'] = $old;
60 * Interprets the provider array. Return expected value depending
61 * the HTTP server name.
63 private function extractExpect( $server, $expectations ) {
64 if ( is_string( $expectations ) ) {
66 } elseif ( is_array( $expectations ) ) {
67 if ( !array_key_exists( $server, $expectations ) ) {
68 throw new MWException( __METHOD__
. " expectation does not have any "
69 . "value for server name $server. Check the provider array.\n" );
71 return $expectations[$server];
74 throw new MWException( __METHOD__
. " given invalid expectation for "
75 . "'$server'. Should be a string or an array( <http server name> => <string> ).\n" );
79 # ### PROVIDERS ###########################################################
83 * [ 'input', 'expected' ];
86 * [ 'Apache', 'expected' ],
87 * [ 'Microsoft-IIS/7', 'expected' ],
89 * If you want to add other HTTP server name, you will have to add a new
90 * testing method much like the testEncodingUrlWith() method above.
92 public static function provideURLS() {
97 // & and = not safe in queries
103 'Microsoft-IIS/7' => '%3A',
106 // remaining chars do not need encoding
113 // slash remain unchanged. %2F seems to break things
118 // Other 'funnies' chars
122 // Apostrophe is encoded