test that wfUrlencode() encodes apostrophe
[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
39 // do the requested test
40 $this->assertEquals(
41 $expected,
42 wfUrlencode( $input ),
43 "Encoding '$input' for server '$server' should be '$expected'"
44 );
45
46 // restore global
47 if( $old === null ) {
48 unset( $_SERVER['SERVER_SOFTWARE'] );
49 } else {
50 $_SERVER['SERVER_SOFTWARE'] = $old;
51 }
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
63 /**
64 * FIXME FIXME FIXME FIXME
65 * wfUrlencode use a static variable so we can not just
66 * change the $GLOBALS server name :(
67 */
68 $this->markTestSkipped( 'FIXME: wfUrlencode() use a static, thus changing $GLOBALS[SERVER_SOFTWARE] is useless' );
69
70 if( !array_key_exists( $server, $expectations ) ) {
71 throw new MWException( __METHOD__ . " expectation does not have any value for server name $server. Check the provider array.\n" );
72 } else {
73 return $expectations[$server];
74 }
75 } else {
76 throw new MWException( __METHOD__ . " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
77 }
78 }
79
80
81 #### PROVIDERS ###########################################################
82
83 /**
84 * Format is either:
85 * array( 'input', 'expected' );
86 * Or:
87 * array( 'input',
88 * array( 'Apache', 'expected' ),
89 * array( 'Microsoft-IIS/7', 'expected' ),
90 * ),
91 * If you want to add other HTTP server name, you will have to add a new
92 * testing method much like the testEncodingUrlWith() method above.
93 */
94 public function provideURLS() {
95 return array(
96 ### RFC 1738 chars
97 // + is not safe
98 array( '+', '%2B' ),
99 // & and = not safe in queries
100 array( '&', '%26' ),
101 array( '=', '%3D' ),
102
103 array( ':', array(
104 'Apache' => ':',
105 'Microsoft-IIS/7' => '%3A',
106 ) ),
107
108 // remaining chars do not need encoding
109 array(
110 ';@$-_.!*',
111 ';@$-_.!*',
112 ),
113
114 ### Other tests
115 // slash remain unchanged. %2F seems to break things
116 array( '/', '/' ),
117
118 // Other 'funnies' chars
119 array( '[]', '%5B%5D' ),
120 array( '<>', '%3C%3E' ),
121
122 // Apostrophe is encoded
123 array( '\'', '%27' ),
124 );
125 }
126 }