4 * The function only need a string parameter and might react to IIS7.0
6 * @group GlobalFunctions
7 * @covers ::wfUrlencode
9 class WfUrlencodeTest
extends \MediaWikiUnitTestCase
{
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 = $_SERVER['SERVER_SOFTWARE'] ??
null;
37 $_SERVER['SERVER_SOFTWARE'] = $server;
40 // do the requested test
43 wfUrlencode( $input ),
44 "Encoding '$input' for server '$server' should be '$expected'"
48 if ( $old === null ) {
49 unset( $_SERVER['SERVER_SOFTWARE'] );
51 $_SERVER['SERVER_SOFTWARE'] = $old;
57 * Interprets the provider array. Return expected value depending
58 * the HTTP server name.
60 private function extractExpect( $server, $expectations ) {
61 if ( is_string( $expectations ) ) {
63 } elseif ( is_array( $expectations ) ) {
64 if ( !array_key_exists( $server, $expectations ) ) {
65 throw new MWException( __METHOD__
. " expectation does not have any "
66 . "value for server name $server. Check the provider array.\n" );
68 return $expectations[$server];
71 throw new MWException( __METHOD__
. " given invalid expectation for "
72 . "'$server'. Should be a string or an array( <http server name> => <string> ).\n" );
76 # ### PROVIDERS ###########################################################
80 * [ 'input', 'expected' ];
83 * [ 'Apache', 'expected' ],
84 * [ 'Microsoft-IIS/7', 'expected' ],
86 * If you want to add other HTTP server name, you will have to add a new
87 * testing method much like the testEncodingUrlWith() method above.
89 public static function provideURLS() {
94 // & and = not safe in queries
100 'Microsoft-IIS/7' => '%3A',
103 // remaining chars do not need encoding
110 // slash remain unchanged. %2F seems to break things
115 // Other 'funnies' chars
119 // Apostrophe is encoded