Make wfUrlEncode(null) reset the static. Two skipped tests work now.
[mediawiki.git] / tests / phpunit / includes / GlobalFunctions / wfUrlencodeTest.php
blobcd1a8dbd24f6218aa205bebcedb81ee136375bb0
1 <?php
2 /**
3 * Tests for includes/GlobalFunctions.php -> wfUrlencode()
5 * The function only need a string parameter and might react to IIS7.0
6 */
8 class wfUrlencodeTest extends MediaWikiTestCase {
10 #### TESTS ##############################################################
12 /** @dataProvider provideURLS */
13 public function testEncodingUrlWith( $input, $expected ) {
14 $this->verifyEncodingFor( 'Apache', $input, $expected );
17 /** @dataProvider provideURLS */
18 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
19 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
22 #### HELPERS #############################################################
24 /**
25 * Internal helper that actually run the test.
26 * Called by the public methods testEncodingUrlWith...()
29 private function verifyEncodingFor( $server, $input, $expectations ) {
30 $expected = $this->extractExpect( $server, $expectations );
32 // save up global
33 $old = isset($_SERVER['SERVER_SOFTWARE'])
34 ? $_SERVER['SERVER_SOFTWARE']
35 : null
37 $_SERVER['SERVER_SOFTWARE'] = $server;
38 wfUrlencode( null );
40 // do the requested test
41 $this->assertEquals(
42 $expected,
43 wfUrlencode( $input ),
44 "Encoding '$input' for server '$server' should be '$expected'"
47 // restore global
48 if( $old === null ) {
49 unset( $_SERVER['SERVER_SOFTWARE'] );
50 } else {
51 $_SERVER['SERVER_SOFTWARE'] = $old;
53 wfUrlencode( null );
56 /**
57 * Interprets the provider array. Return expected value depending
58 * the HTTP server name.
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];
69 } else {
70 throw new MWException( __METHOD__ . " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
75 #### PROVIDERS ###########################################################
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.
88 public 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' ),
97 array( ':', array(
98 'Apache' => ':',
99 'Microsoft-IIS/7' => '%3A',
100 ) ),
102 // remaining chars do not need encoding
103 array(
104 ';@$-_.!*',
105 ';@$-_.!*',
108 ### Other tests
109 // slash remain unchanged. %2F seems to break things
110 array( '/', '/' ),
112 // Other 'funnies' chars
113 array( '[]', '%5B%5D' ),
114 array( '<>', '%3C%3E' ),
116 // Apostrophe is encoded
117 array( '\'', '%27' ),