4 * Tests for the PathRouter parsing.
8 class PathRouterTest
extends MediaWikiTestCase
{
13 protected $basicRouter;
15 protected function setUp() {
17 $router = new PathRouter
;
18 $router->add( "/wiki/$1" );
19 $this->basicRouter
= $router;
22 public static function provideParse() {
25 'Basic path parsing' => [
31 'Loose path auto-$1: /$1' => [
36 'Loose path auto-$1: /wiki' => [
41 'Loose path auto-$1: /wiki/' => [
46 // Ensure that path is based on specificity, not order
47 'Order, /$1 added first' => [
48 [ "/$1", "/a/$1", "/b/$1" ],
52 'Order, /$1 added last' => [
53 [ "/b/$1", "/a/$1", "/$1" ],
57 // Handling of key based arrays with a url parameter
58 'Key based array' => [
60 'path' => [ 'edit' => "/edit/$1" ],
61 'params' => [ 'action' => '$key' ],
64 [ 'title' => "Foo", 'action' => 'edit' ]
66 // Additional parameter
70 'params' => [ 'test' => '$2' ]
73 [ 'title' => "Foo", 'test' => 'asdf' ]
76 // Shared patterns for restricted value parameter tests
77 $restrictedPatterns = [
80 'params' => [ 'test' => '$2' ],
81 'options' => [ '$2' => [ 'a', 'b' ] ]
85 'params' => [ 'test2' => '$2' ],
86 'options' => [ '$2' => 'c' ]
91 // Restricted value parameter tests
95 [ 'title' => "asdf/Foo" ]
100 [ 'title' => "Foo", 'test' => 'a' ]
105 [ 'title' => "Foo", 'test2' => 'c' ]
112 'params' => [ 'a' => 'b', 'data:foo' => 'bar' ],
113 'options' => [ 'callback' => [ __CLASS__
, 'callbackForTest' ] ]
124 // Test to ensure that matches are not made if a parameter expects nonexistent input
127 'path' => "/wiki/$1",
128 'params' => [ 'title' => "$1$2" ],
134 // Make sure the router handles titles like Special:Recentchanges correctly
137 "/wiki/Special:Recentchanges",
138 [ 'title' => "Special:Recentchanges" ]
141 // Make sure the router decodes urlencoding properly
144 "/wiki/Title_With%20Space",
145 [ 'title' => "Title_With Space" ]
148 // Double slash and dot expansion
149 'Double slash in prefix' => [
154 'Double slash at start of $1' => [
157 [ 'title' => '/Foo' ]
159 'Double slash in middle of $1' => [
162 [ 'title' => '.hack//SIGN' ]
164 'Dots removed 1' => [
169 'Dots removed 2' => [
174 'Dots retained 1' => [
177 [ 'title' => '../wiki/Foo' ]
179 'Dots retained 2' => [
182 [ 'title' => './Foo' ]
189 // '..' only traverses one slash, see e.g. RFC 3986
190 'Dots traversing double slash 1' => [
192 '/a//b/../../wiki/Foo',
195 'Dots traversing double slash 2' => [
197 '/a//b/../../../wiki/Foo',
202 // Make sure the router doesn't break on special characters like $ used in regexp replacements
203 foreach ( [ "$", "$1", "\\", "\\$1" ] as $char ) {
204 $tests["Regexp character $char"] = [
207 [ 'title' => "$char" ]
212 // Make sure the router handles characters like +&() properly
213 "Special characters" => [
215 "/wiki/Plus+And&Dollar\\Stuff();[]{}*",
216 [ 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ],
219 // Make sure the router handles unicode characters correctly
222 "/wiki/Spécial:Modifications_récentes" ,
223 [ 'title' => "Spécial:Modifications_récentes" ],
228 "/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes",
229 [ 'title' => "Spécial:Modifications_récentes" ],
233 // Ensure the router doesn't choke on long paths.
234 $lorem = "Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit,_sed_do_eiusmod_" .
235 "tempor_incididunt_ut_labore_et_dolore_magna_aliqua._Ut_enim_ad_minim_veniam,_quis_" .
236 "nostrud_exercitation_ullamco_laboris_nisi_ut_aliquip_ex_ea_commodo_consequat._" .
237 "Duis_aute_irure_dolor_in_reprehenderit_in_voluptate_velit_esse_cillum_dolore_" .
238 "eu_fugiat_nulla_pariatur._Excepteur_sint_occaecat_cupidatat_non_proident,_sunt_" .
239 "in_culpa_qui_officia_deserunt_mollit_anim_id_est_laborum.";
245 [ 'title' => $lorem ]
248 // Ensure that the php passed site of parameter values are not urldecoded
249 "Pattern urlencoding" => [
250 [ [ 'path' => "/wiki/$1", 'params' => [ 'title' => '%20:$1' ] ] ],
252 [ 'title' => '%20:Foo' ]
255 // Ensure that raw parameter values do not have any variable replacements or urldecoding
256 "Raw param value" => [
257 [ [ 'path' => "/wiki/$1", 'params' => [ 'title' => [ 'value' => 'bar%20$1' ] ] ] ],
259 [ 'title' => 'bar%20$1' ]
268 * @dataProvider provideParse
270 public function testParse( $patterns, $path, $expected ) {
271 $patterns = (array)$patterns;
273 $router = new PathRouter
;
274 foreach ( $patterns as $pattern ) {
275 if ( is_array( $pattern ) ) {
276 $router->add( $pattern['path'], $pattern['params'] ??
[],
277 $pattern['options'] ??
[] );
279 $router->add( $pattern );
282 $matches = $router->parse( $path );
283 $this->assertEquals( $matches, $expected );
286 public static function callbackForTest( &$matches, $data ) {
287 $matches['x'] = $data['$1'];
288 $matches['foo'] = $data['foo'];
291 public static function provideWeight() {
293 [ '/Foo', [ 'title' => 'Foo' ] ],
294 [ '/Bar', [ 'ping' => 'pong' ] ],
295 [ '/Baz', [ 'marco' => 'polo' ] ],
296 [ '/asdf-foo', [ 'title' => 'qwerty-foo' ] ],
297 [ '/qwerty-bar', [ 'title' => 'asdf-bar' ] ],
298 [ '/a/Foo', [ 'title' => 'Foo' ] ],
299 [ '/asdf/Foo', [ 'title' => 'Foo' ] ],
300 [ '/qwerty/Foo', [ 'title' => 'Foo', 'qwerty' => 'qwerty' ] ],
301 [ '/baz/Foo', [ 'title' => 'Foo', 'unrestricted' => 'baz' ] ],
302 [ '/y/Foo', [ 'title' => 'Foo', 'restricted-to-y' => 'y' ] ],
307 * Test to ensure weight of paths is handled correctly
308 * @dataProvider provideWeight
310 public function testWeight( $path, $expected ) {
311 $router = new PathRouter
;
312 $router->addStrict( "/Bar", [ 'ping' => 'pong' ] );
313 $router->add( "/asdf-$1", [ 'title' => 'qwerty-$1' ] );
314 $router->add( "/$1" );
315 $router->add( "/qwerty-$1", [ 'title' => 'asdf-$1' ] );
316 $router->addStrict( "/Baz", [ 'marco' => 'polo' ] );
317 $router->add( "/a/$1" );
318 $router->add( "/asdf/$1" );
319 $router->add( "/$2/$1", [ 'unrestricted' => '$2' ] );
320 $router->add( [ 'qwerty' => "/qwerty/$1" ], [ 'qwerty' => '$key' ] );
321 $router->add( "/$2/$1", [ 'restricted-to-y' => '$2' ], [ '$2' => 'y' ] );
323 $this->assertEquals( $router->parse( $path ), $expected );