Merge "Add version number to deprecated setting"
[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
10 #### TESTS ##############################################################
11
12 /** @dataProvider provideURLS */
13 public function testEncodingUrlWith( $input, $expected ) {
14 $this->verifyEncodingFor( 'Apache', $input, $expected );
15 }
16
17 /** @dataProvider provideURLS */
18 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
19 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
20 }
21
22 #### HELPERS #############################################################
23
24 /**
25 * Internal helper that actually run the test.
26 * Called by the public methods testEncodingUrlWith...()
27 *
28 */
29 private function verifyEncodingFor( $server, $input, $expectations ) {
30 $expected = $this->extractExpect( $server, $expectations );
31
32 // save up global
33 $old = isset($_SERVER['SERVER_SOFTWARE'])
34 ? $_SERVER['SERVER_SOFTWARE']
35 : null
36 ;
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
75 #### PROVIDERS ###########################################################
76
77 /**
78 * Format is either:
79 * array( 'input', 'expected' );
80 * Or:
81 * array( 'input',
82 * array( 'Apache', 'expected' ),
83 * array( 'Microsoft-IIS/7', 'expected' ),
84 * ),
85 * If you want to add other HTTP server name, you will have to add a new
86 * testing method much like the testEncodingUrlWith() method above.
87 */
88 public static function provideURLS() {
89 return array(
90 ### RFC 1738 chars
91 // + is not safe
92 array( '+', '%2B' ),
93 // & and = not safe in queries
94 array( '&', '%26' ),
95 array( '=', '%3D' ),
96
97 array( ':', array(
98 'Apache' => ':',
99 'Microsoft-IIS/7' => '%3A',
100 ) ),
101
102 // remaining chars do not need encoding
103 array(
104 ';@$-_.!*',
105 ';@$-_.!*',
106 ),
107
108 ### Other tests
109 // slash remain unchanged. %2F seems to break things
110 array( '/', '/' ),
111
112 // Other 'funnies' chars
113 array( '[]', '%5B%5D' ),
114 array( '<>', '%3C%3E' ),
115
116 // Apostrophe is encoded
117 array( '\'', '%27' ),
118 );
119 }
120 }