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