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