test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / GlobalFunctions / wfUrlencodeTest.php
blob77685d5034e07c655a2d107a246639fe0a8203b7
1 <?php
2 /**
3 * Tests for wfUrlencode()
5 * The function only need a string parameter and might react to IIS7.0
6 */
7 class WfUrlencodeTest extends MediaWikiTestCase {
8 #### TESTS ##############################################################
10 /** @dataProvider provideURLS */
11 public function testEncodingUrlWith( $input, $expected ) {
12 $this->verifyEncodingFor( 'Apache', $input, $expected );
15 /** @dataProvider provideURLS */
16 public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
17 $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
20 #### HELPERS #############################################################
22 /**
23 * Internal helper that actually run the test.
24 * Called by the public methods testEncodingUrlWith...()
27 private function verifyEncodingFor( $server, $input, $expectations ) {
28 $expected = $this->extractExpect( $server, $expectations );
30 // save up global
31 $old = isset( $_SERVER['SERVER_SOFTWARE'] )
32 ? $_SERVER['SERVER_SOFTWARE']
33 : null;
34 $_SERVER['SERVER_SOFTWARE'] = $server;
35 wfUrlencode( null );
37 // do the requested test
38 $this->assertEquals(
39 $expected,
40 wfUrlencode( $input ),
41 "Encoding '$input' for server '$server' should be '$expected'"
44 // restore global
45 if ( $old === null ) {
46 unset( $_SERVER['SERVER_SOFTWARE'] );
47 } else {
48 $_SERVER['SERVER_SOFTWARE'] = $old;
50 wfUrlencode( null );
53 /**
54 * Interprets the provider array. Return expected value depending
55 * the HTTP server name.
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];
66 } else {
67 throw new MWException( __METHOD__ . " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
71 #### PROVIDERS ###########################################################
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.
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' ),
93 array( ':', array(
94 'Apache' => ':',
95 'Microsoft-IIS/7' => '%3A',
96 ) ),
98 // remaining chars do not need encoding
99 array(
100 ';@$-_.!*',
101 ';@$-_.!*',
104 ### Other tests
105 // slash remain unchanged. %2F seems to break things
106 array( '/', '/' ),
108 // Other 'funnies' chars
109 array( '[]', '%5B%5D' ),
110 array( '<>', '%3C%3E' ),
112 // Apostrophe is encoded
113 array( '\'', '%27' ),