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