Debian Codegraph @

tests/phpunit/unit/includes/GlobalFunctions/WfUrlencodeTest.php

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