Merge "registration: Expose config array to ExtensionRegistry"
[lhc/web/wiklou.git] / tests / phpunit / includes / PathRouterTest.php
1 <?php
2
3 /**
4 * Tests for the PathRouter parsing.
5 *
6 * @covers PathRouter
7 */
8 class PathRouterTest extends MediaWikiTestCase {
9
10 /**
11 * @var PathRouter
12 */
13 protected $basicRouter;
14
15 protected function setUp() {
16 parent::setUp();
17 $router = new PathRouter;
18 $router->add( "/wiki/$1" );
19 $this->basicRouter = $router;
20 }
21
22 public static function provideParse() {
23 $tests = [
24 // Basic path parsing
25 'Basic path parsing' => [
26 "/wiki/$1",
27 "/wiki/Foo",
28 [ 'title' => "Foo" ]
29 ],
30 //
31 'Loose path auto-$1: /$1' => [
32 "/",
33 "/Foo",
34 [ 'title' => "Foo" ]
35 ],
36 'Loose path auto-$1: /wiki' => [
37 "/wiki",
38 "/wiki/Foo",
39 [ 'title' => "Foo" ]
40 ],
41 'Loose path auto-$1: /wiki/' => [
42 "/wiki/",
43 "/wiki/Foo",
44 [ 'title' => "Foo" ]
45 ],
46 // Ensure that path is based on specificity, not order
47 'Order, /$1 added first' => [
48 [ "/$1", "/a/$1", "/b/$1" ],
49 "/a/Foo",
50 [ 'title' => "Foo" ]
51 ],
52 'Order, /$1 added last' => [
53 [ "/b/$1", "/a/$1", "/$1" ],
54 "/a/Foo",
55 [ 'title' => "Foo" ]
56 ],
57 // Handling of key based arrays with a url parameter
58 'Key based array' => [
59 [ [
60 'path' => [ 'edit' => "/edit/$1" ],
61 'params' => [ 'action' => '$key' ],
62 ] ],
63 "/edit/Foo",
64 [ 'title' => "Foo", 'action' => 'edit' ]
65 ],
66 // Additional parameter
67 'Basic $2' => [
68 [ [
69 'path' => '/$2/$1',
70 'params' => [ 'test' => '$2' ]
71 ] ],
72 "/asdf/Foo",
73 [ 'title' => "Foo", 'test' => 'asdf' ]
74 ],
75 ];
76 // Shared patterns for restricted value parameter tests
77 $restrictedPatterns = [
78 [
79 'path' => '/$2/$1',
80 'params' => [ 'test' => '$2' ],
81 'options' => [ '$2' => [ 'a', 'b' ] ]
82 ],
83 [
84 'path' => '/$2/$1',
85 'params' => [ 'test2' => '$2' ],
86 'options' => [ '$2' => 'c' ]
87 ],
88 '/$1'
89 ];
90 $tests += [
91 // Restricted value parameter tests
92 'Restricted 1' => [
93 $restrictedPatterns,
94 "/asdf/Foo",
95 [ 'title' => "asdf/Foo" ]
96 ],
97 'Restricted 2' => [
98 $restrictedPatterns,
99 "/a/Foo",
100 [ 'title' => "Foo", 'test' => 'a' ]
101 ],
102 'Restricted 3' => [
103 $restrictedPatterns,
104 "/c/Foo",
105 [ 'title' => "Foo", 'test2' => 'c' ]
106 ],
107
108 // Callback test
109 'Callback' => [
110 [ [
111 'path' => "/$1",
112 'params' => [ 'a' => 'b', 'data:foo' => 'bar' ],
113 'options' => [ 'callback' => [ __CLASS__, 'callbackForTest' ] ]
114 ] ],
115 '/Foo',
116 [
117 'title' => "Foo",
118 'x' => 'Foo',
119 'a' => 'b',
120 'foo' => 'bar'
121 ]
122 ],
123
124 // Test to ensure that matches are not made if a parameter expects nonexistent input
125 'Fail' => [
126 [ [
127 'path' => "/wiki/$1",
128 'params' => [ 'title' => "$1$2" ],
129 ] ],
130 "/wiki/A",
131 []
132 ],
133
134 // Make sure the router handles titles like Special:Recentchanges correctly
135 'Special title' => [
136 "/wiki/$1",
137 "/wiki/Special:Recentchanges",
138 [ 'title' => "Special:Recentchanges" ]
139 ],
140
141 // Make sure the router decodes urlencoding properly
142 'URL encoding' => [
143 "/wiki/$1",
144 "/wiki/Title_With%20Space",
145 [ 'title' => "Title_With Space" ]
146 ],
147
148 ];
149
150 // Make sure the router doesn't break on special characters like $ used in regexp replacements
151 foreach ( [ "$", "$1", "\\", "\\$1" ] as $char ) {
152 $tests["Regexp character $char"] = [
153 "/wiki/$1",
154 "/wiki/$char",
155 [ 'title' => "$char" ]
156 ];
157 }
158
159 $tests += [
160 // Make sure the router handles characters like +&() properly
161 "Special characters" => [
162 "/wiki/$1",
163 "/wiki/Plus+And&Dollar\\Stuff();[]{}*",
164 [ 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ],
165 ],
166
167 // Make sure the router handles unicode characters correctly
168 "Unicode 1" => [
169 "/wiki/$1",
170 "/wiki/Spécial:Modifications_récentes" ,
171 [ 'title' => "Spécial:Modifications_récentes" ],
172 ],
173
174 "Unicode 2" => [
175 "/wiki/$1",
176 "/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes",
177 [ 'title' => "Spécial:Modifications_récentes" ],
178 ]
179 ];
180
181 // Ensure the router doesn't choke on long paths.
182 $lorem = "Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit,_sed_do_eiusmod_" .
183 "tempor_incididunt_ut_labore_et_dolore_magna_aliqua._Ut_enim_ad_minim_veniam,_quis_" .
184 "nostrud_exercitation_ullamco_laboris_nisi_ut_aliquip_ex_ea_commodo_consequat._" .
185 "Duis_aute_irure_dolor_in_reprehenderit_in_voluptate_velit_esse_cillum_dolore_" .
186 "eu_fugiat_nulla_pariatur._Excepteur_sint_occaecat_cupidatat_non_proident,_sunt_" .
187 "in_culpa_qui_officia_deserunt_mollit_anim_id_est_laborum.";
188
189 $tests += [
190 "Long path" => [
191 "/wiki/$1",
192 "/wiki/$lorem",
193 [ 'title' => $lorem ]
194 ],
195
196 // Ensure that the php passed site of parameter values are not urldecoded
197 "Pattern urlencoding" => [
198 [ [ 'path' => "/wiki/$1", 'params' => [ 'title' => '%20:$1' ] ] ],
199 "/wiki/Foo",
200 [ 'title' => '%20:Foo' ]
201 ],
202
203 // Ensure that raw parameter values do not have any variable replacements or urldecoding
204 "Raw param value" => [
205 [ [ 'path' => "/wiki/$1", 'params' => [ 'title' => [ 'value' => 'bar%20$1' ] ] ] ],
206 "/wiki/Foo",
207 [ 'title' => 'bar%20$1' ]
208 ]
209 ];
210
211 return $tests;
212 }
213
214 /**
215 * Test path parsing
216 * @dataProvider provideParse
217 */
218 public function testParse( $patterns, $path, $expected ) {
219 $patterns = (array)$patterns;
220
221 $router = new PathRouter;
222 foreach ( $patterns as $pattern ) {
223 if ( is_array( $pattern ) ) {
224 $router->add( $pattern['path'], $pattern['params'] ?? [],
225 $pattern['options'] ?? [] );
226 } else {
227 $router->add( $pattern );
228 }
229 }
230 $matches = $router->parse( $path );
231 $this->assertEquals( $matches, $expected );
232 }
233
234 public static function callbackForTest( &$matches, $data ) {
235 $matches['x'] = $data['$1'];
236 $matches['foo'] = $data['foo'];
237 }
238
239 public static function provideWeight() {
240 return [
241 [ '/Foo', [ 'title' => 'Foo' ] ],
242 [ '/Bar', [ 'ping' => 'pong' ] ],
243 [ '/Baz', [ 'marco' => 'polo' ] ],
244 [ '/asdf-foo', [ 'title' => 'qwerty-foo' ] ],
245 [ '/qwerty-bar', [ 'title' => 'asdf-bar' ] ],
246 [ '/a/Foo', [ 'title' => 'Foo' ] ],
247 [ '/asdf/Foo', [ 'title' => 'Foo' ] ],
248 [ '/qwerty/Foo', [ 'title' => 'Foo', 'qwerty' => 'qwerty' ] ],
249 [ '/baz/Foo', [ 'title' => 'Foo', 'unrestricted' => 'baz' ] ],
250 [ '/y/Foo', [ 'title' => 'Foo', 'restricted-to-y' => 'y' ] ],
251 ];
252 }
253
254 /**
255 * Test to ensure weight of paths is handled correctly
256 * @dataProvider provideWeight
257 */
258 public function testWeight( $path, $expected ) {
259 $router = new PathRouter;
260 $router->addStrict( "/Bar", [ 'ping' => 'pong' ] );
261 $router->add( "/asdf-$1", [ 'title' => 'qwerty-$1' ] );
262 $router->add( "/$1" );
263 $router->add( "/qwerty-$1", [ 'title' => 'asdf-$1' ] );
264 $router->addStrict( "/Baz", [ 'marco' => 'polo' ] );
265 $router->add( "/a/$1" );
266 $router->add( "/asdf/$1" );
267 $router->add( "/$2/$1", [ 'unrestricted' => '$2' ] );
268 $router->add( [ 'qwerty' => "/qwerty/$1" ], [ 'qwerty' => '$key' ] );
269 $router->add( "/$2/$1", [ 'restricted-to-y' => '$2' ], [ '$2' => 'y' ] );
270
271 $this->assertEquals( $router->parse( $path ), $expected );
272 }
273 }