| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class WfUrlencodeTest extends MediaWikiUnitTestCase { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public function testEncodingUrlWith( $input, $expected ) { |
| 16 |
$this->verifyEncodingFor( 'Apache', $input, $expected ); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) { |
| 23 |
$this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected ); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private function verifyEncodingFor( $server, $input, $expectations ) { |
| 36 |
$expected = $this->extractExpect( $server, $expectations ); |
| 37 |
|
| 38 |
|
| 39 |
$old = $_SERVER['SERVER_SOFTWARE'] ?? null; |
| 40 |
$_SERVER['SERVER_SOFTWARE'] = $server; |
| 41 |
wfUrlencode( null ); |
| 42 |
|
| 43 |
|
| 44 |
$this->assertEquals( |
| 45 |
$expected, |
| 46 |
wfUrlencode( $input ), |
| 47 |
"Encoding '$input' for server '$server' should be '$expected'" |
| 48 |
); |
| 49 |
|
| 50 |
|
| 51 |
if ( $old === null ) { |
| 52 |
unset( $_SERVER['SERVER_SOFTWARE'] ); |
| 53 |
} else { |
| 54 |
$_SERVER['SERVER_SOFTWARE'] = $old; |
| 55 |
} |
| 56 |
wfUrlencode( null ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private function extractExpect( $server, $expectations ) { |
| 67 |
if ( is_string( $expectations ) ) { |
| 68 |
return $expectations; |
| 69 |
} elseif ( is_array( $expectations ) ) { |
| 70 |
if ( !array_key_exists( $server, $expectations ) ) { |
| 71 |
throw new InvalidArgumentException( __METHOD__ . " expectation does not have any " |
| 72 |
. "value for server name $server. Check the provider array.\n" ); |
| 73 |
} else { |
| 74 |
return $expectations[$server]; |
| 75 |
} |
| 76 |
} else { |
| 77 |
throw new InvalidArgumentException( __METHOD__ . " given invalid expectation for " |
| 78 |
. "'$server'. Should be a string or an array [ <http server name> => <string> ].\n" ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static function provideURLS() { |
| 96 |
|
| 97 |
return [ |
| 98 |
|
| 99 |
[ '+', '%2B' ], |
| 100 |
|
| 101 |
[ '&', '%26' ], |
| 102 |
[ '=', '%3D' ], |
| 103 |
[ ':', [ |
| 104 |
'Apache' => ':', |
| 105 |
'Microsoft-IIS/7' => '%3A', |
| 106 |
] ], |
| 107 |
|
| 108 |
[ '/', '/' ], |
| 109 |
|
| 110 |
[ '~', '~' ], |
| 111 |
|
| 112 |
[ '\'', '%27' ], |
| 113 |
[ '[]', '%5B%5D' ], |
| 114 |
[ '<>', '%3C%3E' ], |
| 115 |
|
| 116 |
[ |
| 117 |
';@$-_.!*()', |
| 118 |
';@$-_.!*()', |
| 119 |
], |
| 120 |
]; |
| 121 |
} |
| 122 |
} |