Debian Codegraph @

tests/phpunit/unit/includes/Request/PathRouterTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Request\PathRouter;
4
5 /**
6 * Tests for the MediaWiki\Request\PathRouter parsing.
7 *
8 * @covers \MediaWiki\Request\PathRouter
9 */
10 class PathRouterTest extends MediaWikiUnitTestCase {
11
12 public static function provideParse() {
13 $tests = [
14 // Basic path parsing
15 'Basic path parsing' => [
16 "/wiki/$1",
17 "/wiki/Foo",
18 [ 'title' => "Foo" ]
19 ],
20 //
21 'Loose path auto-$1: /$1' => [
22 "/",
23 "/Foo",
24 [ 'title' => "Foo" ]
25 ],
26 'Loose path auto-$1: /wiki' => [
27 "/wiki",
28 "/wiki/Foo",
29 [ 'title' => "Foo" ]
30 ],
31 'Loose path auto-$1: /wiki/' => [
32 "/wiki/",
33 "/wiki/Foo",
34 [ 'title' => "Foo" ]
35 ],
36 // Ensure that path is based on specificity, not order
37 'Order, /$1 added first' => [
38 [ "/$1", "/a/$1", "/b/$1" ],
39 "/a/Foo",
40 [ 'title' => "Foo" ]
41 ],
42 'Order, /$1 added last' => [
43 [ "/b/$1", "/a/$1", "/$1" ],
44 "/a/Foo",
45 [ 'title' => "Foo" ]
46 ],
47 // Handling of key based arrays with a url parameter
48 'Key based array' => [
49 [ [
50 'path' => [ 'edit' => "/edit/$1" ],
51 'params' => [ 'action' => '$key' ],
52 ] ],
53 "/edit/Foo",
54 [ 'title' => "Foo", 'action' => 'edit' ]
55 ],
56 // Additional parameter
57 'Basic $2' => [
58 [ [
59 'path' => '/$2/$1',
60 'params' => [ 'test' => '$2' ]
61 ] ],
62 "/asdf/Foo",
63 [ 'title' => "Foo", 'test' => 'asdf' ]
64 ],
65 ];
66 // Shared patterns for restricted value parameter tests
67 $restrictedPatterns = [
68 [
69 'path' => '/$2/$1',
70 'params' => [ 'test' => '$2' ],
71 'options' => [ '$2' => [ 'a', 'b' ] ]
72 ],
73 [
74 'path' => '/$2/$1',
75 'params' => [ 'test2' => '$2' ],
76 'options' => [ '$2' => 'c' ]
77 ],
78 '/$1'
79 ];
80 $tests += [
81 // Restricted value parameter tests
82 'Restricted 1' => [
83 $restrictedPatterns,
84 "/asdf/Foo",
85 [ 'title' => "asdf/Foo" ]
86 ],
87 'Restricted 2' => [
88 $restrictedPatterns,
89 "/a/Foo",
90 [ 'title' => "Foo", 'test' => 'a' ]
91 ],
92 'Restricted 3' => [
93 $restrictedPatterns,
94 "/c/Foo",
95 [ 'title' => "Foo", 'test2' => 'c' ]
96 ],
97
98 // Callback test
99 'Callback' => [
100 [ [
101 'path' => "/$1",
102 'params' => [ 'a' => 'b', 'data:foo' => 'bar' ],
103 'options' => [ 'callback' => [ __CLASS__, 'callbackForTest' ] ]
104 ] ],
105 '/Foo',
106 [
107 'title' => "Foo",
108 'x' => 'Foo',
109 'a' => 'b',
110 'foo' => 'bar'
111 ]
112 ],
113
114 // Test to ensure that matches are not made if a parameter expects nonexistent input
115 'Fail' => [
116 [ [
117 'path' => "/wiki/$1",
118 'params' => [ 'title' => "$1$2" ],
119 ] ],
120 "/wiki/A",
121 []
122 ],
123
124 // Make sure the router handles titles like Special:Recentchanges correctly
125 'Special title' => [
126 "/wiki/$1",
127 "/wiki/Special:Recentchanges",
128 [ 'title' => "Special:Recentchanges" ]
129 ],
130
131 // Make sure the router decodes urlencoding properly
132 'URL encoding' => [
133 "/wiki/$1",
134 "/wiki/Title_With%20Space",
135 [ 'title' => "Title_With Space" ]
136 ],
137
138 // Double slash and dot expansion
139 'Double slash in prefix' => [
140 '/wiki/$1',
141 '//wiki/Foo',
142 [ 'title' => 'Foo' ]
143 ],
144 'Double slash at start of $1' => [
145 '/wiki/$1',
146 '/wiki//Foo',
147 [ 'title' => '/Foo' ]
148 ],
149 'Double slash in middle of $1' => [
150 '/wiki/$1',
151 '/wiki/.hack//SIGN',
152 [ 'title' => '.hack//SIGN' ]
153 ],
154 'Dots removed 1' => [
155 '/wiki/$1',
156 '/x/../wiki/Foo',
157 [ 'title' => 'Foo' ]
158 ],
159 'Dots removed 2' => [
160 '/wiki/$1',
161 '/./wiki/Foo',
162 [ 'title' => 'Foo' ]
163 ],
164 'Dots retained 1' => [
165 '/wiki/$1',
166 '/wiki/../wiki/Foo',
167 [ 'title' => '../wiki/Foo' ]
168 ],
169 'Dots retained 2' => [
170 '/wiki/$1',
171 '/wiki/./Foo',
172 [ 'title' => './Foo' ]
173 ],
174 'Triple slash' => [
175 '/wiki/$1',
176 '///wiki/Foo',
177 [ 'title' => 'Foo' ]
178 ],
179 // '..' only traverses one slash, see e.g. RFC 3986
180 'Dots traversing double slash 1' => [
181 '/wiki/$1',
182 '/a//b/../../wiki/Foo',
183 []
184 ],
185 'Dots traversing double slash 2' => [
186 '/wiki/$1',
187 '/a//b/../../../wiki/Foo',
188 [ 'title' => 'Foo' ]
189 ],
190 ];
191
192 // Make sure the router doesn't break on special characters like $ used in regexp replacements
193 foreach ( [ "$", "$1", "\\", "\\$1" ] as $char ) {
194 $tests["Regexp character $char"] = [
195 "/wiki/$1",
196 "/wiki/$char",
197 [ 'title' => "$char" ]
198 ];
199 }
200
201 $tests += [
202 // Make sure the router handles characters like +&() properly
203 "Special characters" => [
204 "/wiki/$1",
205 "/wiki/Plus+And&Dollar\\Stuff();[]{}*",
206 [ 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ],
207 ],
208
209 // Make sure the router handles unicode characters correctly
210 "Unicode 1" => [
211 "/wiki/$1",
212 "/wiki/Spécial:Modifications_récentes",
213 [ 'title' => "Spécial:Modifications_récentes" ],
214 ],
215
216 "Unicode 2" => [
217 "/wiki/$1",
218 "/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes",
219 [ 'title' => "Spécial:Modifications_récentes" ],
220 ]
221 ];
222
223 // Ensure the router doesn't choke on long paths.
224 $lorem = "Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit,_sed_do_eiusmod_" .
225 "tempor_incididunt_ut_labore_et_dolore_magna_aliqua._Ut_enim_ad_minim_veniam,_quis_" .
226 "nostrud_exercitation_ullamco_laboris_nisi_ut_aliquip_ex_ea_commodo_consequat._" .
227 "Duis_aute_irure_dolor_in_reprehenderit_in_voluptate_velit_esse_cillum_dolore_" .
228 "eu_fugiat_nulla_pariatur._Excepteur_sint_occaecat_cupidatat_non_proident,_sunt_" .
229 "in_culpa_qui_officia_deserunt_mollit_anim_id_est_laborum.";
230
231 $tests += [
232 "Long path" => [
233 "/wiki/$1",
234 "/wiki/$lorem",
235 [ 'title' => $lorem ]
236 ],
237
238 // Ensure that the php passed site of parameter values are not urldecoded
239 "Pattern urlencoding" => [
240 [ [ 'path' => "/wiki/$1", 'params' => [ 'title' => '%20:$1' ] ] ],
241 "/wiki/Foo",
242 [ 'title' => '%20:Foo' ]
243 ],
244
245 // Ensure that raw parameter values do not have any variable replacements or urldecoding
246 "Raw param value" => [
247 [ [ 'path' => "/wiki/$1", 'params' => [ 'title' => [ 'value' => 'bar%20$1' ] ] ] ],
248 "/wiki/Foo",
249 [ 'title' => 'bar%20$1' ]
250 ]
251 ];
252
253 return $tests;
254 }
255
256 /**
257 * Test path parsing
258 * @dataProvider provideParse
259 */
260 public function testParse( $patterns, $path, $expected ) {
261 $patterns = (array)$patterns;
262
263 $router = new PathRouter;
264 foreach ( $patterns as $pattern ) {
265 if ( is_array( $pattern ) ) {
266 $router->add( $pattern['path'], $pattern['params'] ?? [],
267 $pattern['options'] ?? [] );
268 } else {
269 $router->add( $pattern );
270 }
271 }
272 $matches = $router->parse( $path );
273 $this->assertEquals( $expected, $matches );
274 }
275
276 public static function callbackForTest( &$matches, $data ) {
277 $matches['x'] = $data['$1'];
278 $matches['foo'] = $data['foo'];
279 }
280
281 public static function provideWeight() {
282 return [
283 [ '/Foo', [ 'title' => 'Foo' ] ],
284 [ '/Bar', [ 'ping' => 'pong' ] ],
285 [ '/Baz', [ 'marco' => 'polo' ] ],
286 [ '/asdf-foo', [ 'title' => 'qwerty-foo' ] ],
287 [ '/qwerty-bar', [ 'title' => 'asdf-bar' ] ],
288 [ '/a/Foo', [ 'title' => 'Foo' ] ],
289 [ '/asdf/Foo', [ 'title' => 'Foo' ] ],
290 [ '/qwerty/Foo', [ 'title' => 'Foo', 'qwerty' => 'qwerty' ] ],
291 [ '/baz/Foo', [ 'title' => 'Foo', 'unrestricted' => 'baz' ] ],
292 [ '/y/Foo', [ 'title' => 'Foo', 'restricted-to-y' => 'y' ] ],
293 ];
294 }
295
296 /**
297 * Test to ensure weight of paths is handled correctly
298 * @dataProvider provideWeight
299 */
300 public function testWeight( $path, $expected ) {
301 $router = new PathRouter;
302 $router->addStrict( "/Bar", [ 'ping' => 'pong' ] );
303 $router->add( "/asdf-$1", [ 'title' => 'qwerty-$1' ] );
304 $router->add( "/$1" );
305 $router->add( "/qwerty-$1", [ 'title' => 'asdf-$1' ] );
306 $router->addStrict( "/Baz", [ 'marco' => 'polo' ] );
307 $router->add( "/a/$1" );
308 $router->add( "/asdf/$1" );
309 $router->add( "/$2/$1", [ 'unrestricted' => '$2' ] );
310 $router->add( [ 'qwerty' => "/qwerty/$1" ], [ 'qwerty' => '$key' ] );
311 $router->add( "/$2/$1", [ 'restricted-to-y' => '$2' ], [ '$2' => 'y' ] );
312
313 $this->assertEquals( $expected, $router->parse( $path ) );
314 }
315
316 public static function provideValidateRoute() {
317 yield [ 'https://test/wiki/$1' ];
318 yield [ 'http://test/wiki/$1' ];
319 yield [ '//test/wiki/$1' ];
320 yield [ '/w/index.php?title=$1' ];
321 yield [ '/w/index.php/$1' ];
322 yield [ '/wiki/$1' ];
323
324 // T48998
325 yield [ 'wiki/$1', false ];
326 }
327
328 /**
329 * @dataProvider provideValidateRoute
330 */
331 public function testValidateRoute( $path, $valid = true ) {
332 $router = new PathRouter;
333 if ( !$valid ) {
334 $this->expectException( FatalError::class );
335 }
336 $this->assertNull( $router->validateRoute( $path, 'wgExample' ) );
337 }
338 }