Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalFunctions / wfUrlencodeTest.php
1 <?php
2
3 /**
4 * The function only need a string parameter and might react to IIS7.0
5 *
6 * @group GlobalFunctions
7 * @covers ::wfUrlencode
8 */
9 class WfUrlencodeTest extends MediaWikiTestCase {
10 # ### TESTS ##############################################################
11
12 /**
13 * @dataProvider provideURLS
14 */
15 public function testEncodingUrlWith( $input, $expected ) {
16 $this->verifyEncodingFor( 'Apache', $input, $expected );
17 }
18
19 /**
20 * @dataProvider provideURLS
21 */
22 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
23 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
24 }
25
26 # ### HELPERS #############################################################
27
28 /**
29 * Internal helper that actually run the test.
30 * Called by the public methods testEncodingUrlWith...()
31 *
32 */
33 private function verifyEncodingFor( $server, $input, $expectations ) {
34 $expected = $this->extractExpect( $server, $expectations );
35
36 // save up global
37 $old = isset( $_SERVER['SERVER_SOFTWARE'] )
38 ? $_SERVER['SERVER_SOFTWARE']
39 : null;
40 $_SERVER['SERVER_SOFTWARE'] = $server;
41 wfUrlencode( null );
42
43 // do the requested test
44 $this->assertEquals(
45 $expected,
46 wfUrlencode( $input ),
47 "Encoding '$input' for server '$server' should be '$expected'"
48 );
49
50 // restore global
51 if ( $old === null ) {
52 unset( $_SERVER['SERVER_SOFTWARE'] );
53 } else {
54 $_SERVER['SERVER_SOFTWARE'] = $old;
55 }
56 wfUrlencode( null );
57 }
58
59 /**
60 * Interprets the provider array. Return expected value depending
61 * the HTTP server name.
62 */
63 private function extractExpect( $server, $expectations ) {
64 if ( is_string( $expectations ) ) {
65 return $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" );
70 } else {
71 return $expectations[$server];
72 }
73 } else {
74 throw new MWException( __METHOD__ . " given invalid expectation for "
75 . "'$server'. Should be a string or an array( <http server name> => <string> ).\n" );
76 }
77 }
78
79 # ### PROVIDERS ###########################################################
80
81 /**
82 * Format is either:
83 * [ 'input', 'expected' ];
84 * Or:
85 * [ 'input',
86 * [ 'Apache', 'expected' ],
87 * [ 'Microsoft-IIS/7', 'expected' ],
88 * ],
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.
91 */
92 public static function provideURLS() {
93 return [
94 # ## RFC 1738 chars
95 // + is not safe
96 [ '+', '%2B' ],
97 // & and = not safe in queries
98 [ '&', '%26' ],
99 [ '=', '%3D' ],
100
101 [ ':', [
102 'Apache' => ':',
103 'Microsoft-IIS/7' => '%3A',
104 ] ],
105
106 // remaining chars do not need encoding
107 [
108 ';@$-_.!*',
109 ';@$-_.!*',
110 ],
111
112 # ## Other tests
113 // slash remain unchanged. %2F seems to break things
114 [ '/', '/' ],
115 // T105265
116 [ '~', '~' ],
117
118 // Other 'funnies' chars
119 [ '[]', '%5B%5D' ],
120 [ '<>', '%3C%3E' ],
121
122 // Apostrophe is encoded
123 [ '\'', '%27' ],
124 ];
125 }
126 }