aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/PathRouterTest.php
blob: d8916751c0815462b3274aaf8312bcda90f93d98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php

/**
 * Tests for the PathRouter parsing.
 *
 * @covers PathRouter
 */
class PathRouterTest extends MediaWikiTestCase {

	/**
	 * @var PathRouter
	 */
	protected $basicRouter;

	protected function setUp() {
		parent::setUp();
		$router = new PathRouter;
		$router->add( "/wiki/$1" );
		$this->basicRouter = $router;
	}

	public static function provideParse() {
		$tests = [
			// Basic path parsing
			'Basic path parsing' => [
				"/wiki/$1",
				"/wiki/Foo",
				[ 'title' => "Foo" ]
			],
			//
			'Loose path auto-$1: /$1' => [
				"/",
				"/Foo",
				[ 'title' => "Foo" ]
			],
			'Loose path auto-$1: /wiki' => [
				"/wiki",
				"/wiki/Foo",
				[ 'title' => "Foo" ]
			],
			'Loose path auto-$1: /wiki/' => [
				"/wiki/",
				"/wiki/Foo",
				[ 'title' => "Foo" ]
			],
			// Ensure that path is based on specificity, not order
			'Order, /$1 added first' => [
				[ "/$1", "/a/$1", "/b/$1" ],
				"/a/Foo",
				[ 'title' => "Foo" ]
			],
			'Order, /$1 added last' => [
				[ "/b/$1", "/a/$1", "/$1" ],
				"/a/Foo",
				[ 'title' => "Foo" ]
			],
			// Handling of key based arrays with a url parameter
			'Key based array' => [
				[ [
					'path' => [ 'edit' => "/edit/$1" ],
					'params' => [ 'action' => '$key' ],
				] ],
				"/edit/Foo",
				[ 'title' => "Foo", 'action' => 'edit' ]
			],
			// Additional parameter
			'Basic $2' => [
				[ [
					'path' => '/$2/$1',
					'params' => [ 'test' => '$2' ]
				] ],
				"/asdf/Foo",
				[ 'title' => "Foo", 'test' => 'asdf' ]
			],
		];
		// Shared patterns for restricted value parameter tests
		$restrictedPatterns = [
			[
				'path' => '/$2/$1',
				'params' => [ 'test' => '$2' ],
				'options' => [ '$2' => [ 'a', 'b' ] ]
			],
			[
				'path' => '/$2/$1',
				'params' => [ 'test2' => '$2' ],
				'options' => [ '$2' => 'c' ]
			],
			'/$1'
		];
		$tests += [
			// Restricted value parameter tests
			'Restricted 1' => [
				$restrictedPatterns,
				"/asdf/Foo",
				[ 'title' => "asdf/Foo" ]
			],
			'Restricted 2' => [
				$restrictedPatterns,
				"/a/Foo",
				[ 'title' => "Foo", 'test' => 'a' ]
			],
			'Restricted 3' => [
				$restrictedPatterns,
				"/c/Foo",
				[ 'title' => "Foo", 'test2' => 'c' ]
			],

			// Callback test
			'Callback' => [
				[ [
					'path' => "/$1",
					'params' => [ 'a' => 'b', 'data:foo' => 'bar' ],
					'options' => [ 'callback' => [ __CLASS__, 'callbackForTest' ] ]
				] ],
				'/Foo',
				[
					'title' => "Foo",
					'x' => 'Foo',
					'a' => 'b',
					'foo' => 'bar'
				]
			],

			// Test to ensure that matches are not made if a parameter expects nonexistent input
			'Fail' => [
				[ [
					'path' => "/wiki/$1",
					'params' => [ 'title' => "$1$2" ],
				] ],
				"/wiki/A",
				[]
			],

			// Make sure the router handles titles like Special:Recentchanges correctly
			'Special title' => [
				"/wiki/$1",
				"/wiki/Special:Recentchanges",
				[ 'title' => "Special:Recentchanges" ]
			],

			// Make sure the router decodes urlencoding properly
			'URL encoding' => [
				"/wiki/$1",
				"/wiki/Title_With%20Space",
				[ 'title' => "Title_With Space" ]
			],

			// Double slash and dot expansion
			'Double slash in prefix' => [
				'/wiki/$1',
				'//wiki/Foo',
				[ 'title' => 'Foo' ]
			],
			'Double slash at start of $1' => [
				'/wiki/$1',
				'/wiki//Foo',
				[ 'title' => '/Foo' ]
			],
			'Double slash in middle of $1' => [
				'/wiki/$1',
				'/wiki/.hack//SIGN',
				[ 'title' => '.hack//SIGN' ]
			],
			'Dots removed 1' => [
				'/wiki/$1',
				'/x/../wiki/Foo',
				[ 'title' => 'Foo' ]
			],
			'Dots removed 2' => [
				'/wiki/$1',
				'/./wiki/Foo',
				[ 'title' => 'Foo' ]
			],
			'Dots retained 1' => [
				'/wiki/$1',
				'/wiki/../wiki/Foo',
				[ 'title' => '../wiki/Foo' ]
			],
			'Dots retained 2' => [
				'/wiki/$1',
				'/wiki/./Foo',
				[ 'title' => './Foo' ]
			],
			'Triple slash' => [
				'/wiki/$1',
				'///wiki/Foo',
				[ 'title' => 'Foo' ]
			],
			// '..' only traverses one slash, see e.g. RFC 3986
			'Dots traversing double slash 1' => [
				'/wiki/$1',
				'/a//b/../../wiki/Foo',
				[]
			],
			'Dots traversing double slash 2' => [
				'/wiki/$1',
				'/a//b/../../../wiki/Foo',
				[ 'title' => 'Foo' ]
			],
		];

		// Make sure the router doesn't break on special characters like $ used in regexp replacements
		foreach ( [ "$", "$1", "\\", "\\$1" ] as $char ) {
			$tests["Regexp character $char"] = [
				"/wiki/$1",
				"/wiki/$char",
				[ 'title' => "$char" ]
			];
		}

		$tests += [
			// Make sure the router handles characters like +&() properly
			"Special characters" => [
				"/wiki/$1",
				"/wiki/Plus+And&Dollar\\Stuff();[]{}*",
				[ 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ],
			],

			// Make sure the router handles unicode characters correctly
			"Unicode 1" => [
				"/wiki/$1",
				"/wiki/Spécial:Modifications_récentes" ,
				[ 'title' => "Spécial:Modifications_récentes" ],
			],

			"Unicode 2" => [
				"/wiki/$1",
				"/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes",
				[ 'title' => "Spécial:Modifications_récentes" ],
			]
		];

		// Ensure the router doesn't choke on long paths.
		$lorem = "Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit,_sed_do_eiusmod_" .
			"tempor_incididunt_ut_labore_et_dolore_magna_aliqua._Ut_enim_ad_minim_veniam,_quis_" .
			 "nostrud_exercitation_ullamco_laboris_nisi_ut_aliquip_ex_ea_commodo_consequat._" .
			 "Duis_aute_irure_dolor_in_reprehenderit_in_voluptate_velit_esse_cillum_dolore_" .
			 "eu_fugiat_nulla_pariatur._Excepteur_sint_occaecat_cupidatat_non_proident,_sunt_" .
			 "in_culpa_qui_officia_deserunt_mollit_anim_id_est_laborum.";

		$tests += [
			"Long path" => [
				"/wiki/$1",
				"/wiki/$lorem",
				[ 'title' => $lorem ]
			],

			// Ensure that the php passed site of parameter values are not urldecoded
			"Pattern urlencoding" => [
				[ [ 'path' => "/wiki/$1", 'params' => [ 'title' => '%20:$1' ] ] ],
				"/wiki/Foo",
				[ 'title' => '%20:Foo' ]
			],

			// Ensure that raw parameter values do not have any variable replacements or urldecoding
			"Raw param value" => [
				[ [ 'path' => "/wiki/$1", 'params' => [ 'title' => [ 'value' => 'bar%20$1' ] ] ] ],
				"/wiki/Foo",
				[ 'title' => 'bar%20$1' ]
			]
		];

		return $tests;
	}

	/**
	 * Test path parsing
	 * @dataProvider provideParse
	 */
	public function testParse( $patterns, $path, $expected ) {
		$patterns = (array)$patterns;

		$router = new PathRouter;
		foreach ( $patterns as $pattern ) {
			if ( is_array( $pattern ) ) {
				$router->add( $pattern['path'], $pattern['params'] ?? [],
					$pattern['options'] ?? [] );
			} else {
				$router->add( $pattern );
			}
		}
		$matches = $router->parse( $path );
		$this->assertEquals( $matches, $expected );
	}

	public static function callbackForTest( &$matches, $data ) {
		$matches['x'] = $data['$1'];
		$matches['foo'] = $data['foo'];
	}

	public static function provideWeight() {
		return [
			[ '/Foo', [ 'title' => 'Foo' ] ],
			[ '/Bar', [ 'ping' => 'pong' ] ],
			[ '/Baz', [ 'marco' => 'polo' ] ],
			[ '/asdf-foo', [ 'title' => 'qwerty-foo' ] ],
			[ '/qwerty-bar', [ 'title' => 'asdf-bar' ] ],
			[ '/a/Foo', [ 'title' => 'Foo' ] ],
			[ '/asdf/Foo', [ 'title' => 'Foo' ] ],
			[ '/qwerty/Foo', [ 'title' => 'Foo', 'qwerty' => 'qwerty' ] ],
			[ '/baz/Foo', [ 'title' => 'Foo', 'unrestricted' => 'baz' ] ],
			[ '/y/Foo', [ 'title' => 'Foo', 'restricted-to-y' => 'y' ] ],
		];
	}

	/**
	 * Test to ensure weight of paths is handled correctly
	 * @dataProvider provideWeight
	 */
	public function testWeight( $path, $expected ) {
		$router = new PathRouter;
		$router->addStrict( "/Bar", [ 'ping' => 'pong' ] );
		$router->add( "/asdf-$1", [ 'title' => 'qwerty-$1' ] );
		$router->add( "/$1" );
		$router->add( "/qwerty-$1", [ 'title' => 'asdf-$1' ] );
		$router->addStrict( "/Baz", [ 'marco' => 'polo' ] );
		$router->add( "/a/$1" );
		$router->add( "/asdf/$1" );
		$router->add( "/$2/$1", [ 'unrestricted' => '$2' ] );
		$router->add( [ 'qwerty' => "/qwerty/$1" ], [ 'qwerty' => '$key' ] );
		$router->add( "/$2/$1", [ 'restricted-to-y' => '$2' ], [ '$2' => 'y' ] );

		$this->assertEquals( $router->parse( $path ), $expected );
	}
}