pictcode / lib / Cake / Test / Case / Routing / RouterTest.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (99.206 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* RouterTest file
|
4 |
*
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
7 |
*
|
8 |
* Licensed under The MIT License
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
10 |
* Redistributions of files must retain the above copyright notice.
|
11 |
*
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
14 |
* @package Cake.Test.Case.Routing
|
15 |
* @since CakePHP(tm) v 1.2.0.4206
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
17 |
*/
|
18 |
|
19 |
App::uses('Router', 'Routing'); |
20 |
App::uses('CakeResponse', 'Network'); |
21 |
|
22 |
if (!defined('FULL_BASE_URL')) { |
23 |
define('FULL_BASE_URL', 'http://cakephp.org'); |
24 |
} |
25 |
|
26 |
/**
|
27 |
* RouterTest class
|
28 |
*
|
29 |
* @package Cake.Test.Case.Routing
|
30 |
*/
|
31 |
class RouterTest extends CakeTestCase { |
32 |
|
33 |
/**
|
34 |
* setUp method
|
35 |
*
|
36 |
* @return void
|
37 |
*/
|
38 |
public function setUp() { |
39 |
parent::setUp();
|
40 |
Configure::write('Routing', array('admin' => null, 'prefixes' => array())); |
41 |
} |
42 |
|
43 |
/**
|
44 |
* tearDown method
|
45 |
*
|
46 |
* @return void
|
47 |
*/
|
48 |
public function tearDown() { |
49 |
parent::tearDown();
|
50 |
CakePlugin::unload();
|
51 |
Router::fullBaseUrl(''); |
52 |
Configure::write('App.fullBaseUrl', 'http://localhost'); |
53 |
} |
54 |
|
55 |
/**
|
56 |
* testFullBaseUrl method
|
57 |
*
|
58 |
* @return void
|
59 |
*/
|
60 |
public function testFullBaseUrl() { |
61 |
$this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true)); |
62 |
$this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true)); |
63 |
$this->assertRegExp('/^http(s)?:\/\//', Router::url(array('full_base' => true))); |
64 |
$this->assertSame(FULL_BASE_URL . '/', Router::url(array('full_base' => true))); |
65 |
} |
66 |
|
67 |
/**
|
68 |
* Tests that the base URL can be changed at runtime.
|
69 |
*
|
70 |
* @return void
|
71 |
*/
|
72 |
public function testBaseUrl() { |
73 |
$this->assertEquals(FULL_BASE_URL, Router::fullBaseUrl()); |
74 |
Router::fullBaseUrl('http://example.com'); |
75 |
$this->assertEquals('http://example.com/', Router::url('/', true)); |
76 |
$this->assertEquals('http://example.com', Configure::read('App.fullBaseUrl')); |
77 |
Router::fullBaseUrl('https://example.com'); |
78 |
$this->assertEquals('https://example.com/', Router::url('/', true)); |
79 |
$this->assertEquals('https://example.com', Configure::read('App.fullBaseUrl')); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Test that Router uses App.base to build URL's when there are no stored
|
84 |
* request objects.
|
85 |
*
|
86 |
* @return void
|
87 |
*/
|
88 |
public function testBaseUrlWithBasePath() { |
89 |
Configure::write('App.base', '/cakephp'); |
90 |
Router::fullBaseUrl('http://example.com'); |
91 |
$this->assertEquals('http://example.com/cakephp/tasks', Router::url('/tasks', true)); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* testRouteDefaultParams method
|
96 |
*
|
97 |
* @return void
|
98 |
*/
|
99 |
public function testRouteDefaultParams() { |
100 |
Router::connect('/:controller', array('controller' => 'posts')); |
101 |
$this->assertEquals(Router::url(array('action' => 'index')), '/'); |
102 |
} |
103 |
|
104 |
/**
|
105 |
* testMapResources method
|
106 |
*
|
107 |
* @return void
|
108 |
*/
|
109 |
public function testMapResources() { |
110 |
$resources = Router::mapResources('Posts'); |
111 |
|
112 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
113 |
$result = Router::parse('/posts'); |
114 |
$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => 'GET'), $result); |
115 |
$this->assertEquals(array('posts'), $resources); |
116 |
|
117 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
118 |
$result = Router::parse('/posts/13'); |
119 |
$this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => '13', '[method]' => 'GET'), $result); |
120 |
|
121 |
$_SERVER['REQUEST_METHOD'] = 'POST'; |
122 |
$result = Router::parse('/posts'); |
123 |
$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST'), $result); |
124 |
|
125 |
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
126 |
$result = Router::parse('/posts/13'); |
127 |
$this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT'), $result); |
128 |
|
129 |
$result = Router::parse('/posts/475acc39-a328-44d3-95fb-015000000000'); |
130 |
$this->assertEquals(array('pass' => array('475acc39-a328-44d3-95fb-015000000000'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '475acc39-a328-44d3-95fb-015000000000', '[method]' => 'PUT'), $result); |
131 |
|
132 |
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
133 |
$result = Router::parse('/posts/13'); |
134 |
$this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE'), $result); |
135 |
|
136 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
137 |
$result = Router::parse('/posts/add'); |
138 |
$this->assertSame(array(), $result); |
139 |
|
140 |
Router::reload();
|
141 |
$resources = Router::mapResources('Posts', array('id' => '[a-z0-9_]+')); |
142 |
$this->assertEquals(array('posts'), $resources); |
143 |
|
144 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
145 |
$result = Router::parse('/posts/add'); |
146 |
$this->assertEquals(array('pass' => array('add'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => 'add', '[method]' => 'GET'), $result); |
147 |
|
148 |
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
149 |
$result = Router::parse('/posts/name'); |
150 |
$this->assertEquals(array('pass' => array('name'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => 'name', '[method]' => 'PUT'), $result); |
151 |
} |
152 |
|
153 |
/**
|
154 |
* testMapResources with plugin controllers.
|
155 |
*
|
156 |
* @return void
|
157 |
*/
|
158 |
public function testPluginMapResources() { |
159 |
App::build(array( |
160 |
'Plugin' => array( |
161 |
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS |
162 |
) |
163 |
)); |
164 |
$resources = Router::mapResources('TestPlugin.TestPlugin'); |
165 |
|
166 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
167 |
$result = Router::parse('/test_plugin/test_plugin'); |
168 |
$expected = array( |
169 |
'pass' => array(), |
170 |
'named' => array(), |
171 |
'plugin' => 'test_plugin', |
172 |
'controller' => 'test_plugin', |
173 |
'action' => 'index', |
174 |
'[method]' => 'GET' |
175 |
); |
176 |
$this->assertEquals($expected, $result); |
177 |
$this->assertEquals(array('test_plugin'), $resources); |
178 |
|
179 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
180 |
$result = Router::parse('/test_plugin/test_plugin/13'); |
181 |
$expected = array( |
182 |
'pass' => array('13'), |
183 |
'named' => array(), |
184 |
'plugin' => 'test_plugin', |
185 |
'controller' => 'test_plugin', |
186 |
'action' => 'view', |
187 |
'id' => '13', |
188 |
'[method]' => 'GET' |
189 |
); |
190 |
$this->assertEquals($expected, $result); |
191 |
} |
192 |
|
193 |
/**
|
194 |
* testMapResources with custom connectOptions
|
195 |
*/
|
196 |
public function testMapResourcesConnectOptions() { |
197 |
App::build(array( |
198 |
'Plugin' => array( |
199 |
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS |
200 |
) |
201 |
)); |
202 |
CakePlugin::load('TestPlugin'); |
203 |
App::uses('TestRoute', 'TestPlugin.Routing/Route'); |
204 |
Router::mapResources('Posts', array( |
205 |
'connectOptions' => array( |
206 |
'routeClass' => 'TestPlugin.TestRoute', |
207 |
'foo' => '^(bar)$', |
208 |
), |
209 |
)); |
210 |
$route = end(Router::$routes); |
211 |
$this->assertInstanceOf('TestRoute', $route); |
212 |
$this->assertEquals('^(bar)$', $route->options['foo']); |
213 |
} |
214 |
|
215 |
/**
|
216 |
* Test mapResources with a plugin and prefix.
|
217 |
*
|
218 |
* @return void
|
219 |
*/
|
220 |
public function testPluginMapResourcesWithPrefix() { |
221 |
App::build(array( |
222 |
'Plugin' => array( |
223 |
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS |
224 |
) |
225 |
)); |
226 |
$resources = Router::mapResources('TestPlugin.TestPlugin', array('prefix' => '/api/')); |
227 |
|
228 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
229 |
$result = Router::parse('/api/test_plugin'); |
230 |
$expected = array( |
231 |
'pass' => array(), |
232 |
'named' => array(), |
233 |
'plugin' => 'test_plugin', |
234 |
'controller' => 'test_plugin', |
235 |
'action' => 'index', |
236 |
'[method]' => 'GET' |
237 |
); |
238 |
$this->assertEquals($expected, $result); |
239 |
$this->assertEquals(array('test_plugin'), $resources); |
240 |
|
241 |
Router::mapResources('Posts', array('prefix' => 'api')); |
242 |
|
243 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
244 |
$result = Router::parse('/api/posts'); |
245 |
$expected = array( |
246 |
'pass' => array(), |
247 |
'named' => array(), |
248 |
'plugin' => null, |
249 |
'controller' => 'posts', |
250 |
'action' => 'index', |
251 |
'[method]' => 'GET' |
252 |
); |
253 |
$this->assertEquals($expected, $result); |
254 |
} |
255 |
|
256 |
/**
|
257 |
* testMultipleResourceRoute method
|
258 |
*
|
259 |
* @return void
|
260 |
*/
|
261 |
public function testMultipleResourceRoute() { |
262 |
Router::connect('/:controller', array('action' => 'index', '[method]' => array('GET', 'POST'))); |
263 |
|
264 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
265 |
$result = Router::parse('/posts'); |
266 |
$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => array('GET', 'POST')), $result); |
267 |
|
268 |
$_SERVER['REQUEST_METHOD'] = 'POST'; |
269 |
$result = Router::parse('/posts'); |
270 |
$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => array('GET', 'POST')), $result); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* testGenerateUrlResourceRoute method
|
275 |
*
|
276 |
* @return void
|
277 |
*/
|
278 |
public function testGenerateUrlResourceRoute() { |
279 |
Router::mapResources('Posts'); |
280 |
|
281 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '[method]' => 'GET')); |
282 |
$expected = '/posts'; |
283 |
$this->assertEquals($expected, $result); |
284 |
|
285 |
$result = Router::url(array('controller' => 'posts', 'action' => 'view', '[method]' => 'GET', 'id' => 10)); |
286 |
$expected = '/posts/10'; |
287 |
$this->assertEquals($expected, $result); |
288 |
|
289 |
$result = Router::url(array('controller' => 'posts', 'action' => 'add', '[method]' => 'POST')); |
290 |
$expected = '/posts'; |
291 |
$this->assertEquals($expected, $result); |
292 |
|
293 |
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'id' => 10)); |
294 |
$expected = '/posts/10'; |
295 |
$this->assertEquals($expected, $result); |
296 |
|
297 |
$result = Router::url(array('controller' => 'posts', 'action' => 'delete', '[method]' => 'DELETE', 'id' => 10)); |
298 |
$expected = '/posts/10'; |
299 |
$this->assertEquals($expected, $result); |
300 |
|
301 |
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'POST', 'id' => 10)); |
302 |
$expected = '/posts/10'; |
303 |
$this->assertEquals($expected, $result); |
304 |
} |
305 |
|
306 |
/**
|
307 |
* testUrlNormalization method
|
308 |
*
|
309 |
* @return void
|
310 |
*/
|
311 |
public function testUrlNormalization() { |
312 |
$expected = '/users/logout'; |
313 |
|
314 |
$result = Router::normalize('/users/logout/'); |
315 |
$this->assertEquals($expected, $result); |
316 |
|
317 |
$result = Router::normalize('//users//logout//'); |
318 |
$this->assertEquals($expected, $result); |
319 |
|
320 |
$result = Router::normalize('users/logout'); |
321 |
$this->assertEquals($expected, $result); |
322 |
|
323 |
$result = Router::normalize(array('controller' => 'users', 'action' => 'logout')); |
324 |
$this->assertEquals($expected, $result); |
325 |
|
326 |
$result = Router::normalize('/'); |
327 |
$this->assertEquals('/', $result); |
328 |
|
329 |
$result = Router::normalize('http://google.com/'); |
330 |
$this->assertEquals('http://google.com/', $result); |
331 |
|
332 |
$result = Router::normalize('http://google.com//'); |
333 |
$this->assertEquals('http://google.com//', $result); |
334 |
|
335 |
$result = Router::normalize('/users/login/scope://foo'); |
336 |
$this->assertEquals('/users/login/scope:/foo', $result); |
337 |
|
338 |
$result = Router::normalize('/recipe/recipes/add'); |
339 |
$this->assertEquals('/recipe/recipes/add', $result); |
340 |
|
341 |
$request = new CakeRequest(); |
342 |
$request->base = '/us'; |
343 |
Router::setRequestInfo($request); |
344 |
$result = Router::normalize('/us/users/logout/'); |
345 |
$this->assertEquals('/users/logout', $result); |
346 |
|
347 |
Router::reload();
|
348 |
|
349 |
$request = new CakeRequest(); |
350 |
$request->base = '/cake_12'; |
351 |
Router::setRequestInfo($request); |
352 |
$result = Router::normalize('/cake_12/users/logout/'); |
353 |
$this->assertEquals('/users/logout', $result); |
354 |
|
355 |
Router::reload();
|
356 |
$_back = Configure::read('App.baseUrl'); |
357 |
Configure::write('App.baseUrl', '/'); |
358 |
|
359 |
$request = new CakeRequest(); |
360 |
$request->base = '/'; |
361 |
Router::setRequestInfo($request); |
362 |
$result = Router::normalize('users/login'); |
363 |
$this->assertEquals('/users/login', $result); |
364 |
Configure::write('App.baseUrl', $_back); |
365 |
|
366 |
Router::reload();
|
367 |
$request = new CakeRequest(); |
368 |
$request->base = 'beer'; |
369 |
Router::setRequestInfo($request); |
370 |
$result = Router::normalize('beer/admin/beers_tags/add'); |
371 |
$this->assertEquals('/admin/beers_tags/add', $result); |
372 |
|
373 |
$result = Router::normalize('/admin/beers_tags/add'); |
374 |
$this->assertEquals('/admin/beers_tags/add', $result); |
375 |
} |
376 |
|
377 |
/**
|
378 |
* test generation of basic URLs.
|
379 |
*
|
380 |
* @return void
|
381 |
*/
|
382 |
public function testUrlGenerationBasic() { |
383 |
extract(Router::getNamedExpressions()); |
384 |
|
385 |
$request = new CakeRequest(); |
386 |
$request->addParams(array( |
387 |
'action' => 'index', 'plugin' => null, 'controller' => 'subscribe', 'admin' => true |
388 |
)); |
389 |
$request->base = '/magazine'; |
390 |
$request->here = '/magazine'; |
391 |
$request->webroot = '/magazine/'; |
392 |
Router::setRequestInfo($request); |
393 |
|
394 |
$result = Router::url(); |
395 |
$this->assertEquals('/magazine', $result); |
396 |
|
397 |
Router::reload();
|
398 |
|
399 |
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); |
400 |
$out = Router::url(array('controller' => 'pages', 'action' => 'display', 'home')); |
401 |
$this->assertEquals('/', $out); |
402 |
|
403 |
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); |
404 |
$result = Router::url(array('controller' => 'pages', 'action' => 'display', 'about')); |
405 |
$expected = '/pages/about'; |
406 |
$this->assertEquals($expected, $result); |
407 |
|
408 |
Router::reload();
|
409 |
Router::connect('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'), array('id' => $ID)); |
410 |
Router::parse('/'); |
411 |
|
412 |
$result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1')); |
413 |
$expected = '/cake_plugin/1'; |
414 |
$this->assertEquals($expected, $result); |
415 |
|
416 |
$result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0')); |
417 |
$expected = '/cake_plugin/1/0'; |
418 |
$this->assertEquals($expected, $result); |
419 |
|
420 |
Router::reload();
|
421 |
Router::connect('/:controller/:action/:id', array(), array('id' => $ID)); |
422 |
Router::parse('/'); |
423 |
|
424 |
$result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1')); |
425 |
$expected = '/posts/view/1'; |
426 |
$this->assertEquals($expected, $result); |
427 |
|
428 |
Router::reload();
|
429 |
Router::connect('/:controller/:id', array('action' => 'view')); |
430 |
Router::parse('/'); |
431 |
|
432 |
$result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1')); |
433 |
$expected = '/posts/1'; |
434 |
$this->assertEquals($expected, $result); |
435 |
|
436 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0')); |
437 |
$expected = '/posts/index/0'; |
438 |
$this->assertEquals($expected, $result); |
439 |
|
440 |
Router::connect('/view/*', array('controller' => 'posts', 'action' => 'view')); |
441 |
Router::promote();
|
442 |
$result = Router::url(array('controller' => 'posts', 'action' => 'view', '1')); |
443 |
$expected = '/view/1'; |
444 |
$this->assertEquals($expected, $result); |
445 |
|
446 |
Router::reload();
|
447 |
$request = new CakeRequest(); |
448 |
$request->addParams(array( |
449 |
'action' => 'index', 'plugin' => null, 'controller' => 'real_controller_name' |
450 |
)); |
451 |
$request->base = '/'; |
452 |
$request->here = '/'; |
453 |
$request->webroot = '/'; |
454 |
Router::setRequestInfo($request); |
455 |
|
456 |
Router::connect('short_controller_name/:action/*', array('controller' => 'real_controller_name')); |
457 |
Router::parse('/'); |
458 |
|
459 |
$result = Router::url(array('controller' => 'real_controller_name', 'page' => '1')); |
460 |
$expected = '/short_controller_name/index/page:1'; |
461 |
$this->assertEquals($expected, $result); |
462 |
|
463 |
$result = Router::url(array('action' => 'add')); |
464 |
$expected = '/short_controller_name/add'; |
465 |
$this->assertEquals($expected, $result); |
466 |
|
467 |
Router::reload();
|
468 |
Router::parse('/'); |
469 |
$request = new CakeRequest(); |
470 |
$request->addParams(array( |
471 |
'action' => 'index', 'plugin' => null, 'controller' => 'users', 'url' => array('url' => 'users') |
472 |
)); |
473 |
$request->base = '/'; |
474 |
$request->here = '/'; |
475 |
$request->webroot = '/'; |
476 |
Router::setRequestInfo($request); |
477 |
|
478 |
$result = Router::url(array('action' => 'login')); |
479 |
$expected = '/users/login'; |
480 |
$this->assertEquals($expected, $result); |
481 |
|
482 |
Router::reload();
|
483 |
Router::connect('/page/*', array('plugin' => null, 'controller' => 'pages', 'action' => 'view')); |
484 |
Router::parse('/'); |
485 |
|
486 |
$result = Router::url(array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'view', 'my-page')); |
487 |
$expected = '/my_plugin/pages/view/my-page'; |
488 |
$this->assertEquals($expected, $result); |
489 |
|
490 |
Router::reload();
|
491 |
Router::connect('/contact/:action', array('plugin' => 'contact', 'controller' => 'contact')); |
492 |
Router::parse('/'); |
493 |
|
494 |
$result = Router::url(array('plugin' => 'contact', 'controller' => 'contact', 'action' => 'me')); |
495 |
|
496 |
$expected = '/contact/me'; |
497 |
$this->assertEquals($expected, $result); |
498 |
|
499 |
Router::reload();
|
500 |
$request = new CakeRequest(); |
501 |
$request->addParams(array( |
502 |
'action' => 'index', 'plugin' => 'myplugin', 'controller' => 'mycontroller', 'admin' => false |
503 |
)); |
504 |
$request->base = '/'; |
505 |
$request->here = '/'; |
506 |
$request->webroot = '/'; |
507 |
Router::setRequestInfo($request); |
508 |
|
509 |
$result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller')); |
510 |
$expected = '/myothercontroller'; |
511 |
$this->assertEquals($expected, $result); |
512 |
} |
513 |
|
514 |
/**
|
515 |
* Test that catch all routes work with a variety of falsey inputs.
|
516 |
*
|
517 |
* @return void
|
518 |
*/
|
519 |
public function testUrlCatchAllRoute() { |
520 |
Router::connect('/*', array('controller' => 'categories', 'action' => 'index')); |
521 |
$result = Router::url(array('controller' => 'categories', 'action' => 'index', '0')); |
522 |
$this->assertEquals('/0', $result); |
523 |
|
524 |
$expected = array( |
525 |
'plugin' => null, |
526 |
'controller' => 'categories', |
527 |
'action' => 'index', |
528 |
'pass' => array('0'), |
529 |
'named' => array() |
530 |
); |
531 |
$result = Router::parse('/0'); |
532 |
$this->assertEquals($expected, $result); |
533 |
|
534 |
$result = Router::parse('0'); |
535 |
$this->assertEquals($expected, $result); |
536 |
} |
537 |
|
538 |
/**
|
539 |
* Tests using arrays in named parameters
|
540 |
*
|
541 |
* @return void
|
542 |
*/
|
543 |
public function testArrayNamedParameters() { |
544 |
$result = Router::url(array('controller' => 'tests', 'pages' => array( |
545 |
1, 2, 3 |
546 |
))); |
547 |
$expected = '/tests/index/pages%5B0%5D:1/pages%5B1%5D:2/pages%5B2%5D:3'; |
548 |
$this->assertEquals($expected, $result); |
549 |
|
550 |
$result = Router::url(array('controller' => 'tests', |
551 |
'pages' => array( |
552 |
'param1' => array( |
553 |
'one',
|
554 |
'two'
|
555 |
), |
556 |
'three'
|
557 |
) |
558 |
)); |
559 |
$expected = '/tests/index/pages%5Bparam1%5D%5B0%5D:one/pages%5Bparam1%5D%5B1%5D:two/pages%5B0%5D:three'; |
560 |
$this->assertEquals($expected, $result); |
561 |
|
562 |
$result = Router::url(array('controller' => 'tests', |
563 |
'pages' => array( |
564 |
'param1' => array( |
565 |
'one' => 1, |
566 |
'two' => 2 |
567 |
), |
568 |
'three'
|
569 |
) |
570 |
)); |
571 |
$expected = '/tests/index/pages%5Bparam1%5D%5Bone%5D:1/pages%5Bparam1%5D%5Btwo%5D:2/pages%5B0%5D:three'; |
572 |
$this->assertEquals($expected, $result); |
573 |
|
574 |
$result = Router::url(array('controller' => 'tests', |
575 |
'super' => array( |
576 |
'nested' => array( |
577 |
'array' => 'awesome', |
578 |
'something' => 'else' |
579 |
), |
580 |
'cool'
|
581 |
) |
582 |
)); |
583 |
$expected = '/tests/index/super%5Bnested%5D%5Barray%5D:awesome/super%5Bnested%5D%5Bsomething%5D:else/super%5B0%5D:cool'; |
584 |
$this->assertEquals($expected, $result); |
585 |
|
586 |
$result = Router::url(array('controller' => 'tests', 'namedParam' => array( |
587 |
'keyed' => 'is an array', |
588 |
'test'
|
589 |
))); |
590 |
$expected = '/tests/index/namedParam%5Bkeyed%5D:is%20an%20array/namedParam%5B0%5D:test'; |
591 |
$this->assertEquals($expected, $result); |
592 |
} |
593 |
|
594 |
/**
|
595 |
* Test generation of routes with query string parameters.
|
596 |
*
|
597 |
* @return void
|
598 |
*/
|
599 |
public function testUrlGenerationWithQueryStrings() { |
600 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2')); |
601 |
$expected = '/posts/index/0?var=test&var2=test2'; |
602 |
$this->assertEquals($expected, $result); |
603 |
|
604 |
$result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2')); |
605 |
$this->assertEquals($expected, $result); |
606 |
|
607 |
$result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => 'test', 'var2' => 'test2'))); |
608 |
$this->assertEquals($expected, $result); |
609 |
|
610 |
$result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => null))); |
611 |
$this->assertEquals('/posts/index/0', $result); |
612 |
|
613 |
$result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2', '#' => 'unencoded string %')); |
614 |
$expected = '/posts/index/0?var=test&var2=test2#unencoded string %'; |
615 |
$this->assertEquals($expected, $result); |
616 |
} |
617 |
|
618 |
/**
|
619 |
* test that regex validation of keyed route params is working.
|
620 |
*
|
621 |
* @return void
|
622 |
*/
|
623 |
public function testUrlGenerationWithRegexQualifiedParams() { |
624 |
Router::connect(
|
625 |
':language/galleries',
|
626 |
array('controller' => 'galleries', 'action' => 'index'), |
627 |
array('language' => '[a-z]{3}') |
628 |
); |
629 |
|
630 |
Router::connect(
|
631 |
'/:language/:admin/:controller/:action/*',
|
632 |
array('admin' => 'admin'), |
633 |
array('language' => '[a-z]{3}', 'admin' => 'admin') |
634 |
); |
635 |
|
636 |
Router::connect('/:language/:controller/:action/*', |
637 |
array(),
|
638 |
array('language' => '[a-z]{3}') |
639 |
); |
640 |
|
641 |
$result = Router::url(array('admin' => false, 'language' => 'dan', 'action' => 'index', 'controller' => 'galleries')); |
642 |
$expected = '/dan/galleries'; |
643 |
$this->assertEquals($expected, $result); |
644 |
|
645 |
$result = Router::url(array('admin' => false, 'language' => 'eng', 'action' => 'index', 'controller' => 'galleries')); |
646 |
$expected = '/eng/galleries'; |
647 |
$this->assertEquals($expected, $result); |
648 |
|
649 |
Router::reload();
|
650 |
Router::connect('/:language/pages', |
651 |
array('controller' => 'pages', 'action' => 'index'), |
652 |
array('language' => '[a-z]{3}') |
653 |
); |
654 |
Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}')); |
655 |
|
656 |
$result = Router::url(array('language' => 'eng', 'action' => 'index', 'controller' => 'pages')); |
657 |
$expected = '/eng/pages'; |
658 |
$this->assertEquals($expected, $result); |
659 |
|
660 |
$result = Router::url(array('language' => 'eng', 'controller' => 'pages')); |
661 |
$this->assertEquals($expected, $result); |
662 |
|
663 |
$result = Router::url(array('language' => 'eng', 'controller' => 'pages', 'action' => 'add')); |
664 |
$expected = '/eng/pages/add'; |
665 |
$this->assertEquals($expected, $result); |
666 |
|
667 |
Router::reload();
|
668 |
Router::connect('/forestillinger/:month/:year/*', |
669 |
array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'), |
670 |
array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}') |
671 |
); |
672 |
Router::parse('/'); |
673 |
|
674 |
$result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'month' => 10, 'year' => 2007, 'min-forestilling')); |
675 |
$expected = '/forestillinger/10/2007/min-forestilling'; |
676 |
$this->assertEquals($expected, $result); |
677 |
|
678 |
Router::reload();
|
679 |
Router::connect('/kalender/:month/:year/*', |
680 |
array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'), |
681 |
array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}') |
682 |
); |
683 |
Router::connect('/kalender/*', array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar')); |
684 |
Router::parse('/'); |
685 |
|
686 |
$result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'min-forestilling')); |
687 |
$expected = '/kalender/min-forestilling'; |
688 |
$this->assertEquals($expected, $result); |
689 |
|
690 |
$result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'min-forestilling')); |
691 |
$expected = '/kalender/10/2007/min-forestilling'; |
692 |
$this->assertEquals($expected, $result); |
693 |
|
694 |
Router::reload();
|
695 |
Router::connect('/:controller/:action/*', array(), array( |
696 |
'controller' => 'source|wiki|commits|tickets|comments|view', |
697 |
'action' => 'branches|history|branch|logs|view|start|add|edit|modify' |
698 |
)); |
699 |
} |
700 |
|
701 |
/**
|
702 |
* Test URL generation with an admin prefix
|
703 |
*
|
704 |
* @return void
|
705 |
*/
|
706 |
public function testUrlGenerationWithAdminPrefix() { |
707 |
Configure::write('Routing.prefixes', array('admin')); |
708 |
Router::reload();
|
709 |
|
710 |
Router::connectNamed(array('event', 'lang')); |
711 |
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); |
712 |
Router::connect('/pages/contact_us', array('controller' => 'pages', 'action' => 'contact_us')); |
713 |
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); |
714 |
Router::connect('/reset/*', array('admin' => true, 'controller' => 'users', 'action' => 'reset')); |
715 |
Router::connect('/tests', array('controller' => 'tests', 'action' => 'index')); |
716 |
Router::parseExtensions('rss'); |
717 |
|
718 |
$request = new CakeRequest(); |
719 |
$request->addParams(array( |
720 |
'controller' => 'registrations', 'action' => 'admin_index', |
721 |
'plugin' => null, 'prefix' => 'admin', 'admin' => true, |
722 |
'ext' => 'html' |
723 |
)); |
724 |
$request->base = ''; |
725 |
$request->here = '/admin/registrations/index'; |
726 |
$request->webroot = '/'; |
727 |
Router::setRequestInfo($request); |
728 |
|
729 |
$result = Router::url(array('page' => 2)); |
730 |
$expected = '/admin/registrations/index/page:2'; |
731 |
$this->assertEquals($expected, $result); |
732 |
|
733 |
Router::reload();
|
734 |
$request = new CakeRequest(); |
735 |
$request->addParams(array( |
736 |
'controller' => 'subscriptions', 'action' => 'admin_index', |
737 |
'plugin' => null, 'admin' => true, |
738 |
'url' => array('url' => 'admin/subscriptions/index/page:2') |
739 |
)); |
740 |
$request->base = '/magazine'; |
741 |
$request->here = '/magazine/admin/subscriptions/index/page:2'; |
742 |
$request->webroot = '/magazine/'; |
743 |
Router::setRequestInfo($request); |
744 |
|
745 |
Router::parse('/'); |
746 |
|
747 |
$result = Router::url(array('page' => 3)); |
748 |
$expected = '/magazine/admin/subscriptions/index/page:3'; |
749 |
$this->assertEquals($expected, $result); |
750 |
|
751 |
Router::reload();
|
752 |
Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin')); |
753 |
Router::parse('/'); |
754 |
|
755 |
$request = new CakeRequest(); |
756 |
$request->addParams(array( |
757 |
'action' => 'admin_index', 'plugin' => null, 'controller' => 'subscribe', |
758 |
'admin' => true, 'url' => array('url' => 'admin/subscriptions/edit/1') |
759 |
)); |
760 |
$request->base = '/magazine'; |
761 |
$request->here = '/magazine/admin/subscriptions/edit/1'; |
762 |
$request->webroot = '/magazine/'; |
763 |
Router::setRequestInfo($request); |
764 |
|
765 |
$result = Router::url(array('action' => 'edit', 1)); |
766 |
$expected = '/magazine/admin/subscriptions/edit/1'; |
767 |
$this->assertEquals($expected, $result); |
768 |
|
769 |
$result = Router::url(array('admin' => true, 'controller' => 'users', 'action' => 'login')); |
770 |
$expected = '/magazine/admin/users/login'; |
771 |
$this->assertEquals($expected, $result); |
772 |
|
773 |
Router::reload();
|
774 |
$request = new CakeRequest(); |
775 |
$request->addParams(array( |
776 |
'admin' => true, 'action' => 'index', 'plugin' => null, 'controller' => 'users', |
777 |
'url' => array('url' => 'users') |
778 |
)); |
779 |
$request->base = '/'; |
780 |
$request->here = '/'; |
781 |
$request->webroot = '/'; |
782 |
Router::setRequestInfo($request); |
783 |
|
784 |
Router::connect('/page/*', array('controller' => 'pages', 'action' => 'view', 'admin' => true, 'prefix' => 'admin')); |
785 |
Router::parse('/'); |
786 |
|
787 |
$result = Router::url(array('admin' => true, 'controller' => 'pages', 'action' => 'view', 'my-page')); |
788 |
$expected = '/page/my-page'; |
789 |
$this->assertEquals($expected, $result); |
790 |
|
791 |
Router::reload();
|
792 |
|
793 |
$request = new CakeRequest(); |
794 |
$request->addParams(array( |
795 |
'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin', 'admin' => true, |
796 |
'url' => array('url' => 'admin/pages/add') |
797 |
)); |
798 |
$request->base = ''; |
799 |
$request->here = '/admin/pages/add'; |
800 |
$request->webroot = '/'; |
801 |
Router::setRequestInfo($request); |
802 |
Router::parse('/'); |
803 |
|
804 |
$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false)); |
805 |
$expected = '/admin/pages/add'; |
806 |
$this->assertEquals($expected, $result); |
807 |
|
808 |
Router::reload();
|
809 |
Router::parse('/'); |
810 |
$request = new CakeRequest(); |
811 |
$request->addParams(array( |
812 |
'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin', 'admin' => true, |
813 |
'url' => array('url' => 'admin/pages/add') |
814 |
)); |
815 |
$request->base = ''; |
816 |
$request->here = '/admin/pages/add'; |
817 |
$request->webroot = '/'; |
818 |
Router::setRequestInfo($request); |
819 |
|
820 |
$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false)); |
821 |
$expected = '/admin/pages/add'; |
822 |
$this->assertEquals($expected, $result); |
823 |
|
824 |
Router::reload();
|
825 |
Router::connect('/admin/:controller/:action/:id', array('admin' => true), array('id' => '[0-9]+')); |
826 |
Router::parse('/'); |
827 |
$request = new CakeRequest(); |
828 |
Router::setRequestInfo(
|
829 |
$request->addParams(array( |
830 |
'plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'pass' => array('284'), |
831 |
'prefix' => 'admin', 'admin' => true, |
832 |
'url' => array('url' => 'admin/pages/edit/284') |
833 |
))->addPaths(array(
|
834 |
'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/' |
835 |
)) |
836 |
); |
837 |
|
838 |
$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'id' => '284')); |
839 |
$expected = '/admin/pages/edit/284'; |
840 |
$this->assertEquals($expected, $result); |
841 |
|
842 |
Router::reload();
|
843 |
Router::parse('/'); |
844 |
|
845 |
$request = new CakeRequest(); |
846 |
Router::setRequestInfo(
|
847 |
$request->addParams(array( |
848 |
'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin', |
849 |
'admin' => true, 'url' => array('url' => 'admin/pages/add') |
850 |
))->addPaths(array(
|
851 |
'base' => '', 'here' => '/admin/pages/add', 'webroot' => '/' |
852 |
)) |
853 |
); |
854 |
|
855 |
$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false)); |
856 |
$expected = '/admin/pages/add'; |
857 |
$this->assertEquals($expected, $result); |
858 |
|
859 |
Router::reload();
|
860 |
Router::parse('/'); |
861 |
|
862 |
$request = new CakeRequest(); |
863 |
Router::setRequestInfo(
|
864 |
$request->addParams(array( |
865 |
'plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'prefix' => 'admin', |
866 |
'admin' => true, 'pass' => array('284'), 'url' => array('url' => 'admin/pages/edit/284') |
867 |
))->addPaths(array(
|
868 |
'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/' |
869 |
)) |
870 |
); |
871 |
|
872 |
$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 284)); |
873 |
$expected = '/admin/pages/edit/284'; |
874 |
$this->assertEquals($expected, $result); |
875 |
|
876 |
Router::reload();
|
877 |
Router::connect('/admin/posts/*', array('controller' => 'posts', 'action' => 'index', 'admin' => true)); |
878 |
Router::parse('/'); |
879 |
Router::setRequestInfo(
|
880 |
$request->addParams(array( |
881 |
'plugin' => null, 'controller' => 'posts', 'action' => 'admin_index', 'prefix' => 'admin', |
882 |
'admin' => true, 'pass' => array('284'), 'url' => array('url' => 'admin/posts') |
883 |
))->addPaths(array(
|
884 |
'base' => '', 'here' => '/admin/posts', 'webroot' => '/' |
885 |
)) |
886 |
); |
887 |
|
888 |
$result = Router::url(array('all')); |
889 |
$expected = '/admin/posts/all'; |
890 |
$this->assertEquals($expected, $result); |
891 |
} |
892 |
|
893 |
/**
|
894 |
* testUrlGenerationWithExtensions method
|
895 |
*
|
896 |
* @return void
|
897 |
*/
|
898 |
public function testUrlGenerationWithExtensions() { |
899 |
Router::parse('/'); |
900 |
$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'id' => null, 'ext' => 'json')); |
901 |
$expected = '/articles/add.json'; |
902 |
$this->assertEquals($expected, $result); |
903 |
|
904 |
$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'ext' => 'json')); |
905 |
$expected = '/articles/add.json'; |
906 |
$this->assertEquals($expected, $result); |
907 |
|
908 |
$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'id' => null, 'ext' => 'json')); |
909 |
$expected = '/articles.json'; |
910 |
$this->assertEquals($expected, $result); |
911 |
|
912 |
$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'ext' => 'json')); |
913 |
$expected = '/articles.json'; |
914 |
$this->assertEquals($expected, $result); |
915 |
} |
916 |
|
917 |
/**
|
918 |
* testPluginUrlGeneration method
|
919 |
*
|
920 |
* @return void
|
921 |
*/
|
922 |
public function testUrlGenerationPlugins() { |
923 |
$request = new CakeRequest(); |
924 |
Router::setRequestInfo(
|
925 |
$request->addParams(array( |
926 |
'plugin' => 'test', 'controller' => 'controller', 'action' => 'index' |
927 |
))->addPaths(array(
|
928 |
'base' => '/base', 'here' => '/clients/sage/portal/donations', 'webroot' => '/base/' |
929 |
)) |
930 |
); |
931 |
|
932 |
$this->assertEquals(Router::url('read/1'), '/base/test/controller/read/1'); |
933 |
|
934 |
Router::reload();
|
935 |
Router::connect('/:lang/:plugin/:controller/*', array('action' => 'index')); |
936 |
|
937 |
$request = new CakeRequest(); |
938 |
Router::setRequestInfo(
|
939 |
$request->addParams(array( |
940 |
'lang' => 'en', |
941 |
'plugin' => 'shows', 'controller' => 'shows', 'action' => 'index', |
942 |
'url' => array('url' => 'en/shows/'), |
943 |
))->addPaths(array(
|
944 |
'base' => '', 'here' => '/en/shows', 'webroot' => '/' |
945 |
)) |
946 |
); |
947 |
|
948 |
Router::parse('/en/shows/'); |
949 |
|
950 |
$result = Router::url(array( |
951 |
'lang' => 'en', |
952 |
'controller' => 'shows', 'action' => 'index', 'page' => '1', |
953 |
)); |
954 |
$expected = '/en/shows/shows/page:1'; |
955 |
$this->assertEquals($expected, $result); |
956 |
} |
957 |
|
958 |
/**
|
959 |
* test that you can leave active plugin routes with plugin = null
|
960 |
*
|
961 |
* @return void
|
962 |
*/
|
963 |
public function testCanLeavePlugin() { |
964 |
Router::reload();
|
965 |
Router::connect(
|
966 |
'/admin/other/:controller/:action/*',
|
967 |
array(
|
968 |
'admin' => 1, |
969 |
'plugin' => 'aliased', |
970 |
'prefix' => 'admin' |
971 |
) |
972 |
); |
973 |
$request = new CakeRequest(); |
974 |
Router::setRequestInfo(
|
975 |
$request->addParams(array( |
976 |
'pass' => array(), |
977 |
'admin' => true, |
978 |
'prefix' => 'admin', |
979 |
'plugin' => 'this', |
980 |
'action' => 'admin_index', |
981 |
'controller' => 'interesting', |
982 |
'url' => array('url' => 'admin/this/interesting/index'), |
983 |
))->addPaths(array(
|
984 |
'base' => '', |
985 |
'here' => '/admin/this/interesting/index', |
986 |
'webroot' => '/', |
987 |
)) |
988 |
); |
989 |
$result = Router::url(array('plugin' => null, 'controller' => 'posts', 'action' => 'index')); |
990 |
$this->assertEquals('/admin/posts', $result); |
991 |
|
992 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index')); |
993 |
$this->assertEquals('/admin/this/posts', $result); |
994 |
|
995 |
$result = Router::url(array('plugin' => 'aliased', 'controller' => 'posts', 'action' => 'index')); |
996 |
$this->assertEquals('/admin/other/posts/index', $result); |
997 |
} |
998 |
|
999 |
/**
|
1000 |
* Test that URL's fail to parse when they are prefixed with //
|
1001 |
*
|
1002 |
* @return void
|
1003 |
*/
|
1004 |
public function testUrlParseFailureDoubleSlash() { |
1005 |
Router::connect('/posts', array('controller' => 'posts', 'action' => 'index')); |
1006 |
$result = Router::parse('/posts'); |
1007 |
$this->assertEquals(
|
1008 |
array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'index'), |
1009 |
$result
|
1010 |
); |
1011 |
|
1012 |
$result = Router::parse('//posts'); |
1013 |
$this->assertEquals(array(), $result); |
1014 |
} |
1015 |
|
1016 |
/**
|
1017 |
* testUrlParsing method
|
1018 |
*
|
1019 |
* @return void
|
1020 |
*/
|
1021 |
public function testUrlParsing() { |
1022 |
extract(Router::getNamedExpressions()); |
1023 |
|
1024 |
Router::connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value', 'somevalue', 'othervalue')); |
1025 |
$result = Router::parse('/posts/2007/08/01/title-of-post-here'); |
1026 |
$expected = array('value' => '2007', 'somevalue' => '08', 'othervalue' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); |
1027 |
$this->assertEquals($expected, $result); |
1028 |
|
1029 |
Router::reload();
|
1030 |
Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)); |
1031 |
$result = Router::parse('/posts/2007/08/01/title-of-post-here'); |
1032 |
$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); |
1033 |
$this->assertEquals($expected, $result); |
1034 |
|
1035 |
Router::reload();
|
1036 |
Router::connect('/posts/:day/:year/:month/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)); |
1037 |
$result = Router::parse('/posts/01/2007/08/title-of-post-here'); |
1038 |
$expected = array('day' => '01', 'year' => '2007', 'month' => '08', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); |
1039 |
$this->assertEquals($expected, $result); |
1040 |
|
1041 |
Router::reload();
|
1042 |
Router::connect('/posts/:month/:day/:year/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)); |
1043 |
$result = Router::parse('/posts/08/01/2007/title-of-post-here'); |
1044 |
$expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); |
1045 |
$this->assertEquals($expected, $result); |
1046 |
|
1047 |
Router::reload();
|
1048 |
Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view')); |
1049 |
$result = Router::parse('/posts/2007/08/01/title-of-post-here'); |
1050 |
$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array()); |
1051 |
$this->assertEquals($expected, $result); |
1052 |
|
1053 |
Router::reload();
|
1054 |
require CAKE . 'Config' . DS . 'routes.php'; |
1055 |
$result = Router::parse('/pages/display/home'); |
1056 |
$expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display', 'named' => array()); |
1057 |
$this->assertEquals($expected, $result); |
1058 |
|
1059 |
$result = Router::parse('pages/display/home/'); |
1060 |
$this->assertEquals($expected, $result); |
1061 |
|
1062 |
$result = Router::parse('pages/display/home'); |
1063 |
$this->assertEquals($expected, $result); |
1064 |
|
1065 |
Router::reload();
|
1066 |
Router::connect('/page/*', array('controller' => 'test')); |
1067 |
$result = Router::parse('/page/my-page'); |
1068 |
$expected = array('pass' => array('my-page'), 'plugin' => null, 'controller' => 'test', 'action' => 'index', 'named' => array()); |
1069 |
$this->assertEquals($expected, $result); |
1070 |
|
1071 |
Router::reload();
|
1072 |
Router::connect('/:language/contact', array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'), array('language' => '[a-z]{3}')); |
1073 |
$result = Router::parse('/eng/contact'); |
1074 |
$expected = array('pass' => array(), 'named' => array(), 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'); |
1075 |
$this->assertEquals($expected, $result); |
1076 |
|
1077 |
Router::reload();
|
1078 |
Router::connect('/forestillinger/:month/:year/*', |
1079 |
array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'), |
1080 |
array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}') |
1081 |
); |
1082 |
|
1083 |
$result = Router::parse('/forestillinger/10/2007/min-forestilling'); |
1084 |
$expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'named' => array()); |
1085 |
$this->assertEquals($expected, $result); |
1086 |
|
1087 |
Router::reload();
|
1088 |
Router::connect('/:controller/:action/*'); |
1089 |
Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display')); |
1090 |
$result = Router::parse('/'); |
1091 |
$expected = array('pass' => array(), 'named' => array(), 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages'); |
1092 |
$this->assertEquals($expected, $result); |
1093 |
|
1094 |
$result = Router::parse('/posts/edit/0'); |
1095 |
$expected = array('pass' => array(0), 'named' => array(), 'controller' => 'posts', 'action' => 'edit', 'plugin' => null); |
1096 |
$this->assertEquals($expected, $result); |
1097 |
|
1098 |
Router::reload();
|
1099 |
Router::connect('/posts/:id::url_title', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+')); |
1100 |
$result = Router::parse('/posts/5:sample-post-title'); |
1101 |
$expected = array('pass' => array('5', 'sample-post-title'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); |
1102 |
$this->assertEquals($expected, $result); |
1103 |
|
1104 |
Router::reload();
|
1105 |
Router::connect('/posts/:id::url_title/*', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+')); |
1106 |
$result = Router::parse('/posts/5:sample-post-title/other/params/4'); |
1107 |
$expected = array('pass' => array('5', 'sample-post-title', 'other', 'params', '4'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); |
1108 |
$this->assertEquals($expected, $result); |
1109 |
|
1110 |
Router::reload();
|
1111 |
Router::connect('/posts/:url_title-(uuid::id)', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => $UUID)); |
1112 |
$result = Router::parse('/posts/sample-post-title-(uuid:47fc97a9-019c-41d1-a058-1fa3cbdd56cb)'); |
1113 |
$expected = array('pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'), 'named' => array(), 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); |
1114 |
$this->assertEquals($expected, $result); |
1115 |
|
1116 |
Router::reload();
|
1117 |
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => false)); |
1118 |
$result = Router::parse('/posts/view/foo:bar/routing:fun'); |
1119 |
$expected = array('pass' => array('foo:bar', 'routing:fun'), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); |
1120 |
$this->assertEquals($expected, $result); |
1121 |
|
1122 |
Router::reload();
|
1123 |
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'))); |
1124 |
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42'); |
1125 |
$expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); |
1126 |
$this->assertEquals($expected, $result); |
1127 |
|
1128 |
Router::reload();
|
1129 |
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true)); |
1130 |
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42'); |
1131 |
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view'); |
1132 |
$this->assertEquals($expected, $result); |
1133 |
|
1134 |
Router::reload();
|
1135 |
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true)); |
1136 |
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42?id=123&tab=abc'); |
1137 |
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '?' => array('id' => '123', 'tab' => 'abc')); |
1138 |
$this->assertEquals($expected, $result); |
1139 |
} |
1140 |
|
1141 |
/**
|
1142 |
* test that the persist key works.
|
1143 |
*
|
1144 |
* @return void
|
1145 |
*/
|
1146 |
public function testPersistentParameters() { |
1147 |
Router::reload();
|
1148 |
Router::connect(
|
1149 |
'/:lang/:color/posts/view/*',
|
1150 |
array('controller' => 'posts', 'action' => 'view'), |
1151 |
array('persist' => array('lang', 'color')) |
1152 |
); |
1153 |
Router::connect(
|
1154 |
'/:lang/:color/posts/index',
|
1155 |
array('controller' => 'posts', 'action' => 'index'), |
1156 |
array('persist' => array('lang')) |
1157 |
); |
1158 |
Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'edit')); |
1159 |
Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about')); |
1160 |
Router::parse('/en/red/posts/view/5'); |
1161 |
|
1162 |
$request = new CakeRequest(); |
1163 |
Router::setRequestInfo(
|
1164 |
$request->addParams(array( |
1165 |
'lang' => 'en', |
1166 |
'color' => 'red', |
1167 |
'prefix' => 'admin', |
1168 |
'plugin' => null, |
1169 |
'action' => 'view', |
1170 |
'controller' => 'posts', |
1171 |
))->addPaths(array(
|
1172 |
'base' => '/', |
1173 |
'here' => '/en/red/posts/view/5', |
1174 |
'webroot' => '/', |
1175 |
)) |
1176 |
); |
1177 |
$expected = '/en/red/posts/view/6'; |
1178 |
$result = Router::url(array('controller' => 'posts', 'action' => 'view', 6)); |
1179 |
$this->assertEquals($expected, $result); |
1180 |
|
1181 |
$expected = '/en/blue/posts/index'; |
1182 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue')); |
1183 |
$this->assertEquals($expected, $result); |
1184 |
|
1185 |
$expected = '/posts/edit/6'; |
1186 |
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 6, 'color' => null, 'lang' => null)); |
1187 |
$this->assertEquals($expected, $result); |
1188 |
|
1189 |
$expected = '/posts'; |
1190 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index')); |
1191 |
$this->assertEquals($expected, $result); |
1192 |
|
1193 |
$expected = '/posts/edit/7'; |
1194 |
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 7)); |
1195 |
$this->assertEquals($expected, $result); |
1196 |
|
1197 |
$expected = '/about'; |
1198 |
$result = Router::url(array('controller' => 'pages', 'action' => 'view', 'about')); |
1199 |
$this->assertEquals($expected, $result); |
1200 |
} |
1201 |
|
1202 |
/**
|
1203 |
* testUuidRoutes method
|
1204 |
*
|
1205 |
* @return void
|
1206 |
*/
|
1207 |
public function testUuidRoutes() { |
1208 |
Router::connect(
|
1209 |
'/subjects/add/:category_id',
|
1210 |
array('controller' => 'subjects', 'action' => 'add'), |
1211 |
array('category_id' => '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}') |
1212 |
); |
1213 |
$result = Router::parse('/subjects/add/4795d601-19c8-49a6-930e-06a8b01d17b7'); |
1214 |
$expected = array('pass' => array(), 'named' => array(), 'category_id' => '4795d601-19c8-49a6-930e-06a8b01d17b7', 'plugin' => null, 'controller' => 'subjects', 'action' => 'add'); |
1215 |
$this->assertEquals($expected, $result); |
1216 |
} |
1217 |
|
1218 |
/**
|
1219 |
* testRouteSymmetry method
|
1220 |
*
|
1221 |
* @return void
|
1222 |
*/
|
1223 |
public function testRouteSymmetry() { |
1224 |
Router::connect(
|
1225 |
"/:extra/page/:slug/*",
|
1226 |
array('controller' => 'pages', 'action' => 'view', 'extra' => null), |
1227 |
array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view') |
1228 |
); |
1229 |
|
1230 |
$result = Router::parse('/some_extra/page/this_is_the_slug'); |
1231 |
$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra'); |
1232 |
$this->assertEquals($expected, $result); |
1233 |
|
1234 |
$result = Router::parse('/page/this_is_the_slug'); |
1235 |
$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null); |
1236 |
$this->assertEquals($expected, $result); |
1237 |
|
1238 |
Router::reload();
|
1239 |
Router::connect(
|
1240 |
"/:extra/page/:slug/*",
|
1241 |
array('controller' => 'pages', 'action' => 'view', 'extra' => null), |
1242 |
array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+') |
1243 |
); |
1244 |
Router::parse('/'); |
1245 |
|
1246 |
$result = Router::url(array('admin' => null, 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null)); |
1247 |
$expected = '/page/this_is_the_slug'; |
1248 |
$this->assertEquals($expected, $result); |
1249 |
|
1250 |
$result = Router::url(array('admin' => null, 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra')); |
1251 |
$expected = '/some_extra/page/this_is_the_slug'; |
1252 |
$this->assertEquals($expected, $result); |
1253 |
} |
1254 |
|
1255 |
/**
|
1256 |
* Test parse and reverse symmetry
|
1257 |
*
|
1258 |
* @return void
|
1259 |
* @dataProvider parseReverseSymmetryData
|
1260 |
*/
|
1261 |
public function testParseReverseSymmetry($url) { |
1262 |
$this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => array()))); |
1263 |
} |
1264 |
|
1265 |
/**
|
1266 |
* Data for parse and reverse test
|
1267 |
*
|
1268 |
* @return array
|
1269 |
*/
|
1270 |
public function parseReverseSymmetryData() { |
1271 |
return array( |
1272 |
array('/'), |
1273 |
array('/controller/action'), |
1274 |
array('/controller/action/param'), |
1275 |
array('/controller/action?param1=value1¶m2=value2'), |
1276 |
array('/controller/action/param?param1=value1'), |
1277 |
array('/controller/action/named1:nv1'), |
1278 |
array('/controller/action/named1:nv1?param1=value1') |
1279 |
); |
1280 |
} |
1281 |
|
1282 |
/**
|
1283 |
* Test that Routing.prefixes are used when a Router instance is created
|
1284 |
* or reset
|
1285 |
*
|
1286 |
* @return void
|
1287 |
*/
|
1288 |
public function testRoutingPrefixesSetting() { |
1289 |
$restore = Configure::read('Routing'); |
1290 |
|
1291 |
Configure::write('Routing.prefixes', array('admin', 'member', 'super_user')); |
1292 |
Router::reload();
|
1293 |
$result = Router::prefixes(); |
1294 |
$expected = array('admin', 'member', 'super_user'); |
1295 |
$this->assertEquals($expected, $result); |
1296 |
|
1297 |
Configure::write('Routing.prefixes', array('admin', 'member')); |
1298 |
Router::reload();
|
1299 |
$result = Router::prefixes(); |
1300 |
$expected = array('admin', 'member'); |
1301 |
$this->assertEquals($expected, $result); |
1302 |
|
1303 |
Configure::write('Routing', $restore); |
1304 |
} |
1305 |
|
1306 |
/**
|
1307 |
* Test prefix routing and plugin combinations
|
1308 |
*
|
1309 |
* @return void
|
1310 |
*/
|
1311 |
public function testPrefixRoutingAndPlugins() { |
1312 |
Configure::write('Routing.prefixes', array('admin')); |
1313 |
$paths = App::path('plugins'); |
1314 |
App::build(array( |
1315 |
'plugins' => array( |
1316 |
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS |
1317 |
) |
1318 |
), App::RESET); |
1319 |
CakePlugin::load(array('TestPlugin')); |
1320 |
|
1321 |
Router::reload();
|
1322 |
require CAKE . 'Config' . DS . 'routes.php'; |
1323 |
$request = new CakeRequest(); |
1324 |
Router::setRequestInfo(
|
1325 |
$request->addParams(array( |
1326 |
'admin' => true, 'controller' => 'controller', 'action' => 'action', |
1327 |
'plugin' => null, 'prefix' => 'admin' |
1328 |
))->addPaths(array(
|
1329 |
'base' => '/', |
1330 |
'here' => '/', |
1331 |
'webroot' => '/base/', |
1332 |
)) |
1333 |
); |
1334 |
Router::parse('/'); |
1335 |
|
1336 |
$result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index')); |
1337 |
$expected = '/admin/test_plugin'; |
1338 |
$this->assertEquals($expected, $result); |
1339 |
|
1340 |
Router::reload();
|
1341 |
require CAKE . 'Config' . DS . 'routes.php'; |
1342 |
$request = new CakeRequest(); |
1343 |
Router::setRequestInfo(
|
1344 |
$request->addParams(array( |
1345 |
'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'admin_edit', |
1346 |
'pass' => array('6'), 'prefix' => 'admin', 'admin' => true, 'form' => array(), |
1347 |
'url' => array('url' => 'admin/shows/show_tickets/edit/6') |
1348 |
))->addPaths(array(
|
1349 |
'base' => '/', |
1350 |
'here' => '/admin/shows/show_tickets/edit/6', |
1351 |
'webroot' => '/', |
1352 |
)) |
1353 |
); |
1354 |
|
1355 |
$result = Router::url(array( |
1356 |
'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'edit', 6, |
1357 |
'admin' => true, 'prefix' => 'admin' |
1358 |
)); |
1359 |
$expected = '/admin/test_plugin/show_tickets/edit/6'; |
1360 |
$this->assertEquals($expected, $result); |
1361 |
|
1362 |
$result = Router::url(array( |
1363 |
'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'index', 'admin' => true |
1364 |
)); |
1365 |
$expected = '/admin/test_plugin/show_tickets'; |
1366 |
$this->assertEquals($expected, $result); |
1367 |
|
1368 |
App::build(array('plugins' => $paths)); |
1369 |
} |
1370 |
|
1371 |
/**
|
1372 |
* testParseExtensions method
|
1373 |
*
|
1374 |
* @return void
|
1375 |
*/
|
1376 |
public function testParseExtensions() { |
1377 |
$this->assertEquals(array(), Router::extensions()); |
1378 |
|
1379 |
Router::parseExtensions('rss'); |
1380 |
$this->assertEquals(array('rss'), Router::extensions()); |
1381 |
} |
1382 |
|
1383 |
/**
|
1384 |
* testSetExtensions method
|
1385 |
*
|
1386 |
* @return void
|
1387 |
*/
|
1388 |
public function testSetExtensions() { |
1389 |
Router::setExtensions(array('rss')); |
1390 |
$this->assertEquals(array('rss'), Router::extensions()); |
1391 |
|
1392 |
require CAKE . 'Config' . DS . 'routes.php'; |
1393 |
$result = Router::parse('/posts.rss'); |
1394 |
$this->assertFalse(isset($result['ext'])); |
1395 |
|
1396 |
Router::parseExtensions();
|
1397 |
$result = Router::parse('/posts.rss'); |
1398 |
$this->assertEquals('rss', $result['ext']); |
1399 |
|
1400 |
$result = Router::parse('/posts.xml'); |
1401 |
$this->assertFalse(isset($result['ext'])); |
1402 |
|
1403 |
Router::setExtensions(array('xml')); |
1404 |
$result = Router::extensions(); |
1405 |
$this->assertEquals(array('rss', 'xml'), $result); |
1406 |
|
1407 |
$result = Router::parse('/posts.xml'); |
1408 |
$this->assertEquals('xml', $result['ext']); |
1409 |
|
1410 |
$result = Router::setExtensions(array('pdf'), false); |
1411 |
$this->assertEquals(array('pdf'), $result); |
1412 |
} |
1413 |
|
1414 |
/**
|
1415 |
* testExtensionParsing method
|
1416 |
*
|
1417 |
* @return void
|
1418 |
*/
|
1419 |
public function testExtensionParsing() { |
1420 |
Router::parseExtensions();
|
1421 |
require CAKE . 'Config' . DS . 'routes.php'; |
1422 |
|
1423 |
$result = Router::parse('/posts.rss'); |
1424 |
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'rss', 'pass' => array(), 'named' => array()); |
1425 |
$this->assertEquals($expected, $result); |
1426 |
|
1427 |
$result = Router::parse('/posts/view/1.rss'); |
1428 |
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'ext' => 'rss'); |
1429 |
$this->assertEquals($expected, $result); |
1430 |
|
1431 |
$result = Router::parse('/posts/view/1.rss?query=test'); |
1432 |
$expected['?'] = array('query' => 'test'); |
1433 |
$this->assertEquals($expected, $result); |
1434 |
|
1435 |
$result = Router::parse('/posts/view/1.atom'); |
1436 |
unset($expected['?']); |
1437 |
$expected['ext'] = 'atom'; |
1438 |
$this->assertEquals($expected, $result); |
1439 |
|
1440 |
Router::reload();
|
1441 |
require CAKE . 'Config' . DS . 'routes.php'; |
1442 |
|
1443 |
Router::parseExtensions('rss', 'xml'); |
1444 |
|
1445 |
$result = Router::parse('/posts.xml'); |
1446 |
$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'xml', 'pass' => array(), 'named' => array()); |
1447 |
$this->assertEquals($expected, $result); |
1448 |
|
1449 |
$result = Router::parse('/posts.atom?hello=goodbye'); |
1450 |
$expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array(), '?' => array('hello' => 'goodbye')); |
1451 |
$this->assertEquals($expected, $result); |
1452 |
|
1453 |
Router::reload();
|
1454 |
Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss')); |
1455 |
$result = Router::parse('/controller/action'); |
1456 |
$expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array()); |
1457 |
$this->assertEquals($expected, $result); |
1458 |
|
1459 |
Router::reload();
|
1460 |
Router::parseExtensions('rss'); |
1461 |
Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss')); |
1462 |
$result = Router::parse('/controller/action'); |
1463 |
$expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array()); |
1464 |
$this->assertEquals($expected, $result); |
1465 |
} |
1466 |
|
1467 |
/**
|
1468 |
* testQuerystringGeneration method
|
1469 |
*
|
1470 |
* @return void
|
1471 |
*/
|
1472 |
public function testQuerystringGeneration() { |
1473 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2')); |
1474 |
$expected = '/posts/index/0?var=test&var2=test2'; |
1475 |
$this->assertEquals($expected, $result); |
1476 |
|
1477 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2'))); |
1478 |
$this->assertEquals($expected, $result); |
1479 |
|
1480 |
$expected .= '&more=test+data'; |
1481 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data'))); |
1482 |
$this->assertEquals($expected, $result); |
1483 |
|
1484 |
// Test bug #4614
|
1485 |
$restore = ini_get('arg_separator.output'); |
1486 |
ini_set('arg_separator.output', '&'); |
1487 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data'))); |
1488 |
$this->assertEquals($expected, $result); |
1489 |
ini_set('arg_separator.output', $restore); |
1490 |
|
1491 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')), array('escape' => true)); |
1492 |
$expected = '/posts/index/0?var=test&var2=test2'; |
1493 |
$this->assertEquals($expected, $result); |
1494 |
} |
1495 |
|
1496 |
/**
|
1497 |
* testConnectNamed method
|
1498 |
*
|
1499 |
* @return void
|
1500 |
*/
|
1501 |
public function testConnectNamed() { |
1502 |
$named = Router::connectNamed(false, array('default' => true)); |
1503 |
$this->assertFalse($named['greedyNamed']); |
1504 |
$this->assertEquals(array_keys($named['rules']), $named['default']); |
1505 |
|
1506 |
Router::reload();
|
1507 |
Router::connect('/foo/*', array('controller' => 'bar', 'action' => 'fubar')); |
1508 |
Router::connectNamed(array(), array('separator' => '=')); |
1509 |
$result = Router::parse('/foo/param1=value1/param2=value2'); |
1510 |
$expected = array('pass' => array(), 'named' => array('param1' => 'value1', 'param2' => 'value2'), 'controller' => 'bar', 'action' => 'fubar', 'plugin' => null); |
1511 |
$this->assertEquals($expected, $result); |
1512 |
|
1513 |
Router::reload();
|
1514 |
Router::connect('/controller/action/*', array('controller' => 'controller', 'action' => 'action'), array('named' => array('param1' => 'value[\d]'))); |
1515 |
Router::connectNamed(array(), array('greedy' => false, 'separator' => '=')); |
1516 |
$result = Router::parse('/controller/action/param1=value1/param2=value2'); |
1517 |
$expected = array('pass' => array('param2=value2'), 'named' => array('param1' => 'value1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); |
1518 |
$this->assertEquals($expected, $result); |
1519 |
|
1520 |
Router::reload();
|
1521 |
Router::connect('/:controller/:action/*'); |
1522 |
Router::connectNamed(array('page'), array('default' => false, 'greedy' => false)); |
1523 |
$result = Router::parse('/categories/index/limit=5'); |
1524 |
$this->assertTrue(empty($result['named'])); |
1525 |
} |
1526 |
|
1527 |
/**
|
1528 |
* testNamedArgsUrlGeneration method
|
1529 |
*
|
1530 |
* @return void
|
1531 |
*/
|
1532 |
public function testNamedArgsUrlGeneration() { |
1533 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 1, 'deleted' => 1)); |
1534 |
$expected = '/posts/index/published:1/deleted:1'; |
1535 |
$this->assertEquals($expected, $result); |
1536 |
|
1537 |
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 0, 'deleted' => 0)); |
1538 |
$expected = '/posts/index/published:0/deleted:0'; |
1539 |
$this->assertEquals($expected, $result); |
1540 |
|
1541 |
Router::reload();
|
1542 |
extract(Router::getNamedExpressions()); |
1543 |
Router::connectNamed(array('file' => '[\w\.\-]+\.(html|png)')); |
1544 |
Router::connect('/', array('controller' => 'graphs', 'action' => 'index')); |
1545 |
Router::connect('/:id/*', array('controller' => 'graphs', 'action' => 'view'), array('id' => $ID)); |
1546 |
|
1547 |
$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 'id' => 12, 'file' => 'asdf.png')); |
1548 |
$expected = '/12/file:asdf.png'; |
1549 |
$this->assertEquals($expected, $result); |
1550 |
|
1551 |
$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 12, 'file' => 'asdf.foo')); |
1552 |
$expected = '/graphs/view/12/file:asdf.foo'; |
1553 |
$this->assertEquals($expected, $result); |
1554 |
|
1555 |
Configure::write('Routing.prefixes', array('admin')); |
1556 |
|
1557 |
Router::reload();
|
1558 |
$request = new CakeRequest(); |
1559 |
Router::setRequestInfo(
|
1560 |
$request->addParams(array( |
1561 |
'admin' => true, 'controller' => 'controller', 'action' => 'index', 'plugin' => null |
1562 |
))->addPaths(array(
|
1563 |
'base' => '/', |
1564 |
'here' => '/', |
1565 |
'webroot' => '/base/', |
1566 |
)) |
1567 |
); |
1568 |
Router::parse('/'); |
1569 |
|
1570 |
$result = Router::url(array('page' => 1, 0 => null, 'sort' => 'controller', 'direction' => 'asc', 'order' => null)); |
1571 |
$expected = "/admin/controller/index/page:1/sort:controller/direction:asc"; |
1572 |
$this->assertEquals($expected, $result); |
1573 |
|
1574 |
Router::reload();
|
1575 |
$request = new CakeRequest('admin/controller/index'); |
1576 |
$request->addParams(array( |
1577 |
'admin' => true, 'controller' => 'controller', 'action' => 'index', 'plugin' => null |
1578 |
)); |
1579 |
$request->base = '/'; |
1580 |
Router::setRequestInfo($request); |
1581 |
|
1582 |
Router::parse('/admin/controller/index/type:whatever'); |
1583 |
$result = Router::url(array('type' => 'new')); |
1584 |
$expected = "/admin/controller/index/type:new"; |
1585 |
$this->assertEquals($expected, $result); |
1586 |
} |
1587 |
|
1588 |
/**
|
1589 |
* testNamedArgsUrlParsing method
|
1590 |
*
|
1591 |
* @return void
|
1592 |
*/
|
1593 |
public function testNamedArgsUrlParsing() { |
1594 |
Router::reload();
|
1595 |
require CAKE . 'Config' . DS . 'routes.php'; |
1596 |
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value'); |
1597 |
$expected = array('pass' => array(), 'named' => array('param1' => 'value1:1', 'param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); |
1598 |
$this->assertEquals($expected, $result); |
1599 |
|
1600 |
Router::reload();
|
1601 |
require CAKE . 'Config' . DS . 'routes.php'; |
1602 |
$result = Router::connectNamed(false); |
1603 |
$this->assertEquals(array(), array_keys($result['rules'])); |
1604 |
$this->assertFalse($result['greedyNamed']); |
1605 |
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value'); |
1606 |
$expected = array('pass' => array('param1:value1:1', 'param2:value2:3', 'param:value'), 'named' => array(), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); |
1607 |
$this->assertEquals($expected, $result); |
1608 |
|
1609 |
Router::reload();
|
1610 |
require CAKE . 'Config' . DS . 'routes.php'; |
1611 |
$result = Router::connectNamed(true); |
1612 |
$named = Router::namedConfig(); |
1613 |
$this->assertEquals($named['default'], array_keys($result['rules'])); |
1614 |
$this->assertTrue($result['greedyNamed']); |
1615 |
|
1616 |
Router::reload();
|
1617 |
require CAKE . 'Config' . DS . 'routes.php'; |
1618 |
Router::connectNamed(array('param1' => 'not-matching')); |
1619 |
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value'); |
1620 |
$expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); |
1621 |
$this->assertEquals($expected, $result); |
1622 |
|
1623 |
$result = Router::parse('/foo/view/param1:value1:1/param2:value2:3/param:value'); |
1624 |
$expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'foo', 'action' => 'view', 'plugin' => null); |
1625 |
$this->assertEquals($expected, $result); |
1626 |
|
1627 |
Router::reload();
|
1628 |
require CAKE . 'Config' . DS . 'routes.php'; |
1629 |
Router::connectNamed(array('param1' => '[\d]', 'param2' => '[a-z]', 'param3' => '[\d]')); |
1630 |
$result = Router::parse('/controller/action/param1:1/param2:2/param3:3'); |
1631 |
$expected = array('pass' => array('param2:2'), 'named' => array('param1' => '1', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); |
1632 |
$this->assertEquals($expected, $result); |
1633 |
|
1634 |
Router::reload();
|
1635 |
require CAKE . 'Config' . DS . 'routes.php'; |
1636 |
Router::connectNamed(array('param1' => '[\d]', 'param2' => true, 'param3' => '[\d]')); |
1637 |
$result = Router::parse('/controller/action/param1:1/param2:2/param3:3'); |
1638 |
$expected = array('pass' => array(), 'named' => array('param1' => '1', 'param2' => '2', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); |
1639 |
$this->assertEquals($expected, $result); |
1640 |
|
1641 |
Router::reload();
|
1642 |
require CAKE . 'Config' . DS . 'routes.php'; |
1643 |
Router::connectNamed(array('param1' => 'value[\d]+:[\d]+'), array('greedy' => false)); |
1644 |
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param3:value'); |
1645 |
$expected = array('pass' => array('param2:value2:3', 'param3:value'), 'named' => array('param1' => 'value1:1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); |
1646 |
$this->assertEquals($expected, $result); |
1647 |
} |
1648 |
|
1649 |
/**
|
1650 |
* Test URL generation with legacy (1.2) style prefix routes.
|
1651 |
*
|
1652 |
* @return void
|
1653 |
* @see testUrlGenerationWithAutoPrefixes
|
1654 |
*/
|
1655 |
public function testUrlGenerationWithLegacyPrefixes() { |
1656 |
Router::reload();
|
1657 |
Router::connect('/protected/:controller/:action/*', array( |
1658 |
'prefix' => 'protected', |
1659 |
'protected' => true |
1660 |
)); |
1661 |
Router::parse('/'); |
1662 |
|
1663 |
$request = new CakeRequest(); |
1664 |
Router::setRequestInfo(
|
1665 |
$request->addParams(array( |
1666 |
'plugin' => null, 'controller' => 'images', 'action' => 'index', |
1667 |
'prefix' => null, 'admin' => false, 'url' => array('url' => 'images/index') |
1668 |
))->addPaths(array(
|
1669 |
'base' => '', |
1670 |
'here' => '/images/index', |
1671 |
'webroot' => '/', |
1672 |
)) |
1673 |
); |
1674 |
|
1675 |
$result = Router::url(array('protected' => true)); |
1676 |
$expected = '/protected/images/index'; |
1677 |
$this->assertEquals($expected, $result); |
1678 |
|
1679 |
$result = Router::url(array('controller' => 'images', 'action' => 'add')); |
1680 |
$expected = '/images/add'; |
1681 |
$this->assertEquals($expected, $result); |
1682 |
|
1683 |
$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true)); |
1684 |
$expected = '/protected/images/add'; |
1685 |
$this->assertEquals($expected, $result); |
1686 |
|
1687 |
$result = Router::url(array('action' => 'edit', 1)); |
1688 |
$expected = '/images/edit/1'; |
1689 |
$this->assertEquals($expected, $result); |
1690 |
|
1691 |
$result = Router::url(array('action' => 'edit', 1, 'protected' => true)); |
1692 |
$expected = '/protected/images/edit/1'; |
1693 |
$this->assertEquals($expected, $result); |
1694 |
|
1695 |
$result = Router::url(array('action' => 'protected_edit', 1, 'protected' => true)); |
1696 |
$expected = '/protected/images/edit/1'; |
1697 |
$this->assertEquals($expected, $result); |
1698 |
|
1699 |
$result = Router::url(array('action' => 'edit', 1, 'protected' => true)); |
1700 |
$expected = '/protected/images/edit/1'; |
1701 |
$this->assertEquals($expected, $result); |
1702 |
|
1703 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1)); |
1704 |
$expected = '/others/edit/1'; |
1705 |
$this->assertEquals($expected, $result); |
1706 |
|
1707 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true)); |
1708 |
$expected = '/protected/others/edit/1'; |
1709 |
$this->assertEquals($expected, $result); |
1710 |
|
1711 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1)); |
1712 |
$expected = '/protected/others/edit/1/page:1'; |
1713 |
$this->assertEquals($expected, $result); |
1714 |
|
1715 |
Router::connectNamed(array('random')); |
1716 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value')); |
1717 |
$expected = '/protected/others/edit/1/random:my-value'; |
1718 |
$this->assertEquals($expected, $result); |
1719 |
} |
1720 |
|
1721 |
/**
|
1722 |
* test newer style automatically generated prefix routes.
|
1723 |
*
|
1724 |
* @return void
|
1725 |
*/
|
1726 |
public function testUrlGenerationWithAutoPrefixes() { |
1727 |
Configure::write('Routing.prefixes', array('protected')); |
1728 |
Router::reload();
|
1729 |
Router::parse('/'); |
1730 |
|
1731 |
$request = new CakeRequest(); |
1732 |
Router::setRequestInfo(
|
1733 |
$request->addParams(array( |
1734 |
'plugin' => null, 'controller' => 'images', 'action' => 'index', |
1735 |
'prefix' => null, 'protected' => false, 'url' => array('url' => 'images/index') |
1736 |
))->addPaths(array(
|
1737 |
'base' => '', |
1738 |
'here' => '/images/index', |
1739 |
'webroot' => '/', |
1740 |
)) |
1741 |
); |
1742 |
|
1743 |
$result = Router::url(array('controller' => 'images', 'action' => 'add')); |
1744 |
$expected = '/images/add'; |
1745 |
$this->assertEquals($expected, $result); |
1746 |
|
1747 |
$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true)); |
1748 |
$expected = '/protected/images/add'; |
1749 |
$this->assertEquals($expected, $result); |
1750 |
|
1751 |
$result = Router::url(array('controller' => 'images', 'action' => 'add_protected_test', 'protected' => true)); |
1752 |
$expected = '/protected/images/add_protected_test'; |
1753 |
$this->assertEquals($expected, $result); |
1754 |
|
1755 |
$result = Router::url(array('action' => 'edit', 1)); |
1756 |
$expected = '/images/edit/1'; |
1757 |
$this->assertEquals($expected, $result); |
1758 |
|
1759 |
$result = Router::url(array('action' => 'edit', 1, 'protected' => true)); |
1760 |
$expected = '/protected/images/edit/1'; |
1761 |
$this->assertEquals($expected, $result); |
1762 |
|
1763 |
$result = Router::url(array('action' => 'protected_edit', 1, 'protected' => true)); |
1764 |
$expected = '/protected/images/edit/1'; |
1765 |
$this->assertEquals($expected, $result); |
1766 |
|
1767 |
$result = Router::url(array('action' => 'protectededit', 1, 'protected' => true)); |
1768 |
$expected = '/protected/images/protectededit/1'; |
1769 |
$this->assertEquals($expected, $result); |
1770 |
|
1771 |
$result = Router::url(array('action' => 'edit', 1, 'protected' => true)); |
1772 |
$expected = '/protected/images/edit/1'; |
1773 |
$this->assertEquals($expected, $result); |
1774 |
|
1775 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1)); |
1776 |
$expected = '/others/edit/1'; |
1777 |
$this->assertEquals($expected, $result); |
1778 |
|
1779 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true)); |
1780 |
$expected = '/protected/others/edit/1'; |
1781 |
$this->assertEquals($expected, $result); |
1782 |
|
1783 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1)); |
1784 |
$expected = '/protected/others/edit/1/page:1'; |
1785 |
$this->assertEquals($expected, $result); |
1786 |
|
1787 |
Router::connectNamed(array('random')); |
1788 |
$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value')); |
1789 |
$expected = '/protected/others/edit/1/random:my-value'; |
1790 |
$this->assertEquals($expected, $result); |
1791 |
} |
1792 |
|
1793 |
/**
|
1794 |
* test that auto-generated prefix routes persist
|
1795 |
*
|
1796 |
* @return void
|
1797 |
*/
|
1798 |
public function testAutoPrefixRoutePersistence() { |
1799 |
Configure::write('Routing.prefixes', array('protected')); |
1800 |
Router::reload();
|
1801 |
Router::parse('/'); |
1802 |
|
1803 |
$request = new CakeRequest(); |
1804 |
Router::setRequestInfo(
|
1805 |
$request->addParams(array( |
1806 |
'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected', |
1807 |
'protected' => true, 'url' => array('url' => 'protected/images/index') |
1808 |
))->addPaths(array(
|
1809 |
'base' => '', |
1810 |
'here' => '/protected/images/index', |
1811 |
'webroot' => '/', |
1812 |
)) |
1813 |
); |
1814 |
|
1815 |
$result = Router::url(array('controller' => 'images', 'action' => 'add')); |
1816 |
$expected = '/protected/images/add'; |
1817 |
$this->assertEquals($expected, $result); |
1818 |
|
1819 |
$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => false)); |
1820 |
$expected = '/images/add'; |
1821 |
$this->assertEquals($expected, $result); |
1822 |
} |
1823 |
|
1824 |
/**
|
1825 |
* test that setting a prefix override the current one
|
1826 |
*
|
1827 |
* @return void
|
1828 |
*/
|
1829 |
public function testPrefixOverride() { |
1830 |
Configure::write('Routing.prefixes', array('protected', 'admin')); |
1831 |
Router::reload();
|
1832 |
Router::parse('/'); |
1833 |
|
1834 |
$request = new CakeRequest(); |
1835 |
Router::setRequestInfo(
|
1836 |
$request->addParams(array( |
1837 |
'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected', |
1838 |
'protected' => true, 'url' => array('url' => 'protected/images/index') |
1839 |
))->addPaths(array(
|
1840 |
'base' => '', |
1841 |
'here' => '/protected/images/index', |
1842 |
'webroot' => '/', |
1843 |
)) |
1844 |
); |
1845 |
|
1846 |
$result = Router::url(array('controller' => 'images', 'action' => 'add', 'admin' => true)); |
1847 |
$expected = '/admin/images/add'; |
1848 |
$this->assertEquals($expected, $result); |
1849 |
|
1850 |
$request = new CakeRequest(); |
1851 |
Router::setRequestInfo(
|
1852 |
$request->addParams(array( |
1853 |
'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'admin', |
1854 |
'admin' => true, 'url' => array('url' => 'admin/images/index') |
1855 |
))->addPaths(array(
|
1856 |
'base' => '', |
1857 |
'here' => '/admin/images/index', |
1858 |
'webroot' => '/', |
1859 |
)) |
1860 |
); |
1861 |
$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true)); |
1862 |
$expected = '/protected/images/add'; |
1863 |
$this->assertEquals($expected, $result); |
1864 |
} |
1865 |
|
1866 |
/**
|
1867 |
* Test that setting a prefix to false is ignored, as its generally user error.
|
1868 |
*
|
1869 |
* @return void
|
1870 |
*/
|
1871 |
public function testPrefixFalseIgnored() { |
1872 |
Configure::write('Routing.prefixes', array('admin')); |
1873 |
Router::reload();
|
1874 |
|
1875 |
Router::connect('/cache_css/*', array('admin' => false, 'controller' => 'asset_compress', 'action' => 'get')); |
1876 |
|
1877 |
$url = Router::url(array('controller' => 'asset_compress', 'action' => 'get', 'test')); |
1878 |
$expected = '/cache_css/test'; |
1879 |
$this->assertEquals($expected, $url); |
1880 |
|
1881 |
$url = Router::url(array('admin' => false, 'controller' => 'asset_compress', 'action' => 'get', 'test')); |
1882 |
$expected = '/cache_css/test'; |
1883 |
$this->assertEquals($expected, $url); |
1884 |
|
1885 |
$url = Router::url(array('admin' => true, 'controller' => 'asset_compress', 'action' => 'get', 'test')); |
1886 |
$this->assertEquals('/admin/asset_compress/get/test', $url); |
1887 |
} |
1888 |
|
1889 |
/**
|
1890 |
* testRemoveBase method
|
1891 |
*
|
1892 |
* @return void
|
1893 |
*/
|
1894 |
public function testRemoveBase() { |
1895 |
$request = new CakeRequest(); |
1896 |
Router::setRequestInfo(
|
1897 |
$request->addParams(array( |
1898 |
'plugin' => null, 'controller' => 'controller', 'action' => 'index', |
1899 |
'bare' => 0, 'url' => array('url' => 'protected/images/index') |
1900 |
))->addPaths(array(
|
1901 |
'base' => '/base', |
1902 |
'here' => '/', |
1903 |
'webroot' => '/base/', |
1904 |
)) |
1905 |
); |
1906 |
|
1907 |
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action')); |
1908 |
$expected = '/base/my_controller/my_action'; |
1909 |
$this->assertEquals($expected, $result); |
1910 |
|
1911 |
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => false)); |
1912 |
$expected = '/my_controller/my_action'; |
1913 |
$this->assertEquals($expected, $result); |
1914 |
|
1915 |
$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => true)); |
1916 |
$expected = '/base/my_controller/my_action/base:1'; |
1917 |
$this->assertEquals($expected, $result); |
1918 |
} |
1919 |
|
1920 |
/**
|
1921 |
* testPagesUrlParsing method
|
1922 |
*
|
1923 |
* @return void
|
1924 |
*/
|
1925 |
public function testPagesUrlParsing() { |
1926 |
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); |
1927 |
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); |
1928 |
|
1929 |
$result = Router::parse('/'); |
1930 |
$expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); |
1931 |
$this->assertEquals($expected, $result); |
1932 |
|
1933 |
$result = Router::parse('/pages/home/'); |
1934 |
$expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); |
1935 |
$this->assertEquals($expected, $result); |
1936 |
|
1937 |
Router::reload();
|
1938 |
require CAKE . 'Config' . DS . 'routes.php'; |
1939 |
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); |
1940 |
|
1941 |
$result = Router::parse('/'); |
1942 |
$expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); |
1943 |
$this->assertEquals($expected, $result); |
1944 |
|
1945 |
$result = Router::parse('/pages/display/home/event:value'); |
1946 |
$expected = array('pass' => array('home'), 'named' => array('event' => 'value'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); |
1947 |
$this->assertEquals($expected, $result); |
1948 |
|
1949 |
$result = Router::parse('/pages/display/home/event:Val_u2'); |
1950 |
$expected = array('pass' => array('home'), 'named' => array('event' => 'Val_u2'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); |
1951 |
$this->assertEquals($expected, $result); |
1952 |
|
1953 |
$result = Router::parse('/pages/display/home/event:val-ue'); |
1954 |
$expected = array('pass' => array('home'), 'named' => array('event' => 'val-ue'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); |
1955 |
$this->assertEquals($expected, $result); |
1956 |
|
1957 |
Router::reload();
|
1958 |
Router::connect('/', array('controller' => 'posts', 'action' => 'index')); |
1959 |
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); |
1960 |
$result = Router::parse('/pages/contact/'); |
1961 |
|
1962 |
$expected = array('pass' => array('contact'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); |
1963 |
$this->assertEquals($expected, $result); |
1964 |
} |
1965 |
|
1966 |
/**
|
1967 |
* test that requests with a trailing dot don't loose the do.
|
1968 |
*
|
1969 |
* @return void
|
1970 |
*/
|
1971 |
public function testParsingWithTrailingPeriod() { |
1972 |
Router::reload();
|
1973 |
Router::connect('/:controller/:action/*'); |
1974 |
$result = Router::parse('/posts/view/something.'); |
1975 |
$this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s'); |
1976 |
|
1977 |
$result = Router::parse('/posts/view/something. . .'); |
1978 |
$this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s'); |
1979 |
} |
1980 |
|
1981 |
/**
|
1982 |
* test that requests with a trailing dot don't loose the do.
|
1983 |
*
|
1984 |
* @return void
|
1985 |
*/
|
1986 |
public function testParsingWithTrailingPeriodAndParseExtensions() { |
1987 |
Router::reload();
|
1988 |
Router::connect('/:controller/:action/*'); |
1989 |
Router::parseExtensions('json'); |
1990 |
|
1991 |
$result = Router::parse('/posts/view/something.'); |
1992 |
$this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s'); |
1993 |
|
1994 |
$result = Router::parse('/posts/view/something. . .'); |
1995 |
$this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s'); |
1996 |
} |
1997 |
|
1998 |
/**
|
1999 |
* test that patterns work for :action
|
2000 |
*
|
2001 |
* @return void
|
2002 |
*/
|
2003 |
public function testParsingWithPatternOnAction() { |
2004 |
Router::reload();
|
2005 |
Router::connect(
|
2006 |
'/blog/:action/*',
|
2007 |
array('controller' => 'blog_posts'), |
2008 |
array('action' => 'other|actions') |
2009 |
); |
2010 |
$result = Router::parse('/blog/other'); |
2011 |
$expected = array( |
2012 |
'plugin' => null, |
2013 |
'controller' => 'blog_posts', |
2014 |
'action' => 'other', |
2015 |
'pass' => array(), |
2016 |
'named' => array() |
2017 |
); |
2018 |
$this->assertEquals($expected, $result); |
2019 |
|
2020 |
$result = Router::parse('/blog/foobar'); |
2021 |
$this->assertSame(array(), $result); |
2022 |
|
2023 |
$result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo')); |
2024 |
$this->assertEquals('/blog_posts/foo', $result); |
2025 |
|
2026 |
$result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions')); |
2027 |
$this->assertEquals('/blog/actions', $result); |
2028 |
} |
2029 |
|
2030 |
/**
|
2031 |
* testParsingWithPrefixes method
|
2032 |
*
|
2033 |
* @return void
|
2034 |
*/
|
2035 |
public function testParsingWithPrefixes() { |
2036 |
$adminParams = array('prefix' => 'admin', 'admin' => true); |
2037 |
Router::connect('/admin/:controller', $adminParams); |
2038 |
Router::connect('/admin/:controller/:action', $adminParams); |
2039 |
Router::connect('/admin/:controller/:action/*', $adminParams); |
2040 |
|
2041 |
$request = new CakeRequest(); |
2042 |
Router::setRequestInfo(
|
2043 |
$request->addParams(array( |
2044 |
'plugin' => null, 'controller' => 'controller', 'action' => 'index' |
2045 |
))->addPaths(array(
|
2046 |
'base' => '/base', |
2047 |
'here' => '/', |
2048 |
'webroot' => '/base/', |
2049 |
)) |
2050 |
); |
2051 |
|
2052 |
$result = Router::parse('/admin/posts/'); |
2053 |
$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'admin_index', 'admin' => true); |
2054 |
$this->assertEquals($expected, $result); |
2055 |
|
2056 |
$result = Router::parse('/admin/posts'); |
2057 |
$this->assertEquals($expected, $result); |
2058 |
|
2059 |
$result = Router::url(array('admin' => true, 'controller' => 'posts')); |
2060 |
$expected = '/base/admin/posts'; |
2061 |
$this->assertEquals($expected, $result); |
2062 |
|
2063 |
$result = Router::prefixes(); |
2064 |
$expected = array('admin'); |
2065 |
$this->assertEquals($expected, $result); |
2066 |
|
2067 |
Router::reload();
|
2068 |
|
2069 |
$prefixParams = array('prefix' => 'members', 'members' => true); |
2070 |
Router::connect('/members/:controller', $prefixParams); |
2071 |
Router::connect('/members/:controller/:action', $prefixParams); |
2072 |
Router::connect('/members/:controller/:action/*', $prefixParams); |
2073 |
|
2074 |
$request = new CakeRequest(); |
2075 |
Router::setRequestInfo(
|
2076 |
$request->addParams(array( |
2077 |
'plugin' => null, 'controller' => 'controller', 'action' => 'index', |
2078 |
'bare' => 0 |
2079 |
))->addPaths(array(
|
2080 |
'base' => '/base', |
2081 |
'here' => '/', |
2082 |
'webroot' => '/', |
2083 |
)) |
2084 |
); |
2085 |
|
2086 |
$result = Router::parse('/members/posts/index'); |
2087 |
$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'members_index', 'members' => true); |
2088 |
$this->assertEquals($expected, $result); |
2089 |
|
2090 |
$result = Router::url(array('members' => true, 'controller' => 'posts', 'action' => 'index', 'page' => 2)); |
2091 |
$expected = '/base/members/posts/index/page:2'; |
2092 |
$this->assertEquals($expected, $result); |
2093 |
|
2094 |
$result = Router::url(array('members' => true, 'controller' => 'users', 'action' => 'add')); |
2095 |
$expected = '/base/members/users/add'; |
2096 |
$this->assertEquals($expected, $result); |
2097 |
} |
2098 |
|
2099 |
/**
|
2100 |
* Tests URL generation with flags and prefixes in and out of context
|
2101 |
*
|
2102 |
* @return void
|
2103 |
*/
|
2104 |
public function testUrlWritingWithPrefixes() { |
2105 |
Router::connect('/company/:controller/:action/*', array('prefix' => 'company', 'company' => true)); |
2106 |
Router::connect('/login', array('controller' => 'users', 'action' => 'login')); |
2107 |
|
2108 |
$result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => true)); |
2109 |
$expected = '/company/users/login'; |
2110 |
$this->assertEquals($expected, $result); |
2111 |
|
2112 |
$result = Router::url(array('controller' => 'users', 'action' => 'company_login', 'company' => true)); |
2113 |
$expected = '/company/users/login'; |
2114 |
$this->assertEquals($expected, $result); |
2115 |
|
2116 |
$request = new CakeRequest(); |
2117 |
Router::setRequestInfo(
|
2118 |
$request->addParams(array( |
2119 |
'plugin' => null, 'controller' => 'users', 'action' => 'login', |
2120 |
'company' => true |
2121 |
))->addPaths(array(
|
2122 |
'base' => '/', |
2123 |
'here' => '/', |
2124 |
'webroot' => '/base/', |
2125 |
)) |
2126 |
); |
2127 |
|
2128 |
$result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => false)); |
2129 |
$expected = '/login'; |
2130 |
$this->assertEquals($expected, $result); |
2131 |
} |
2132 |
|
2133 |
/**
|
2134 |
* test url generation with prefixes and custom routes
|
2135 |
*
|
2136 |
* @return void
|
2137 |
*/
|
2138 |
public function testUrlWritingWithPrefixesAndCustomRoutes() { |
2139 |
Router::connect(
|
2140 |
'/admin/login',
|
2141 |
array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin', 'admin' => true) |
2142 |
); |
2143 |
$request = new CakeRequest(); |
2144 |
Router::setRequestInfo(
|
2145 |
$request->addParams(array( |
2146 |
'plugin' => null, 'controller' => 'posts', 'action' => 'index', |
2147 |
'admin' => true, 'prefix' => 'admin' |
2148 |
))->addPaths(array(
|
2149 |
'base' => '/', |
2150 |
'here' => '/', |
2151 |
'webroot' => '/', |
2152 |
)) |
2153 |
); |
2154 |
$result = Router::url(array('controller' => 'users', 'action' => 'login', 'admin' => true)); |
2155 |
$this->assertEquals('/admin/login', $result); |
2156 |
|
2157 |
$result = Router::url(array('controller' => 'users', 'action' => 'login')); |
2158 |
$this->assertEquals('/admin/login', $result); |
2159 |
|
2160 |
$result = Router::url(array('controller' => 'users', 'action' => 'admin_login')); |
2161 |
$this->assertEquals('/admin/login', $result); |
2162 |
} |
2163 |
|
2164 |
/**
|
2165 |
* testPassedArgsOrder method
|
2166 |
*
|
2167 |
* @return void
|
2168 |
*/
|
2169 |
public function testPassedArgsOrder() { |
2170 |
Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home')); |
2171 |
Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2)); |
2172 |
Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1)); |
2173 |
Router::parse('/'); |
2174 |
|
2175 |
$result = Router::url(array('controller' => 'pages', 'action' => 'display', 1, 'whatever')); |
2176 |
$expected = '/test/whatever'; |
2177 |
$this->assertEquals($expected, $result); |
2178 |
|
2179 |
$result = Router::url(array('controller' => 'pages', 'action' => 'display', 2, 'whatever')); |
2180 |
$expected = '/test2/whatever'; |
2181 |
$this->assertEquals($expected, $result); |
2182 |
|
2183 |
$result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever')); |
2184 |
$expected = '/test-passed/whatever'; |
2185 |
$this->assertEquals($expected, $result); |
2186 |
|
2187 |
Configure::write('Routing.prefixes', array('admin')); |
2188 |
Router::reload();
|
2189 |
|
2190 |
$request = new CakeRequest(); |
2191 |
Router::setRequestInfo(
|
2192 |
$request->addParams(array( |
2193 |
'plugin' => null, 'controller' => 'images', 'action' => 'index', |
2194 |
'url' => array('url' => 'protected/images/index') |
2195 |
))->addPaths(array(
|
2196 |
'base' => '', |
2197 |
'here' => '/protected/images/index', |
2198 |
'webroot' => '/', |
2199 |
)) |
2200 |
); |
2201 |
|
2202 |
Router::connect('/protected/:controller/:action/*', array( |
2203 |
'controller' => 'users', |
2204 |
'action' => 'index', |
2205 |
'prefix' => 'protected' |
2206 |
)); |
2207 |
|
2208 |
Router::parse('/'); |
2209 |
$result = Router::url(array('controller' => 'images', 'action' => 'add')); |
2210 |
$expected = '/protected/images/add'; |
2211 |
$this->assertEquals($expected, $result); |
2212 |
|
2213 |
$result = Router::prefixes(); |
2214 |
$expected = array('admin', 'protected'); |
2215 |
$this->assertEquals($expected, $result); |
2216 |
} |
2217 |
|
2218 |
/**
|
2219 |
* testRegexRouteMatching method
|
2220 |
*
|
2221 |
* @return void
|
2222 |
*/
|
2223 |
public function testRegexRouteMatching() { |
2224 |
Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng')); |
2225 |
|
2226 |
$result = Router::parse('/eng/test/test_action'); |
2227 |
$expected = array('pass' => array(), 'named' => array(), 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null); |
2228 |
$this->assertEquals($expected, $result); |
2229 |
|
2230 |
$result = Router::parse('/badness/test/test_action'); |
2231 |
$this->assertSame(array(), $result); |
2232 |
|
2233 |
Router::reload();
|
2234 |
Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng')); |
2235 |
|
2236 |
$request = new CakeRequest(); |
2237 |
Router::setRequestInfo(
|
2238 |
$request->addParams(array( |
2239 |
'plugin' => null, 'controller' => 'test', 'action' => 'index', |
2240 |
'url' => array('url' => 'test/test_action') |
2241 |
))->addPaths(array(
|
2242 |
'base' => '', |
2243 |
'here' => '/test/test_action', |
2244 |
'webroot' => '/', |
2245 |
)) |
2246 |
); |
2247 |
|
2248 |
$result = Router::url(array('action' => 'test_another_action')); |
2249 |
$expected = '/test/test_another_action'; |
2250 |
$this->assertEquals($expected, $result); |
2251 |
|
2252 |
$result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng')); |
2253 |
$expected = '/eng/test/test_another_action'; |
2254 |
$this->assertEquals($expected, $result); |
2255 |
|
2256 |
$result = Router::url(array('action' => 'test_another_action', 'locale' => 'badness')); |
2257 |
$expected = '/test/test_another_action/locale:badness'; |
2258 |
$this->assertEquals($expected, $result); |
2259 |
} |
2260 |
|
2261 |
/**
|
2262 |
* testStripPlugin
|
2263 |
*
|
2264 |
* @return void
|
2265 |
*/
|
2266 |
public function testStripPlugin() { |
2267 |
$pluginName = 'forums'; |
2268 |
$url = 'example.com/' . $pluginName . '/'; |
2269 |
$expected = 'example.com'; |
2270 |
|
2271 |
$this->assertEquals($expected, Router::stripPlugin($url, $pluginName)); |
2272 |
$this->assertEquals(Router::stripPlugin($url), $url); |
2273 |
$this->assertEquals(Router::stripPlugin($url, null), $url); |
2274 |
} |
2275 |
|
2276 |
/**
|
2277 |
* testCurrentRouteWhenNonExistentRoute
|
2278 |
*
|
2279 |
* @return void
|
2280 |
*/
|
2281 |
public function testCurrentRouteWhenNonExistentRoute() { |
2282 |
$route = Router::currentRoute(); |
2283 |
$this->assertFalse($route); |
2284 |
} |
2285 |
|
2286 |
/**
|
2287 |
* testCurrentRoute
|
2288 |
*
|
2289 |
* This test needs some improvement and actual requestAction() usage
|
2290 |
*
|
2291 |
* @return void
|
2292 |
*/
|
2293 |
public function testCurrentRoute() { |
2294 |
$url = array('controller' => 'pages', 'action' => 'display', 'government'); |
2295 |
Router::connect('/government', $url); |
2296 |
Router::parse('/government'); |
2297 |
$route = Router::currentRoute(); |
2298 |
$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults); |
2299 |
} |
2300 |
|
2301 |
/**
|
2302 |
* testRequestRoute
|
2303 |
*
|
2304 |
* @return void
|
2305 |
*/
|
2306 |
public function testRequestRoute() { |
2307 |
$url = array('controller' => 'products', 'action' => 'display', 5); |
2308 |
Router::connect('/government', $url); |
2309 |
Router::parse('/government'); |
2310 |
$route = Router::requestRoute(); |
2311 |
$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults); |
2312 |
|
2313 |
// test that the first route is matched
|
2314 |
Router::connect('/government', $url); |
2315 |
Router::parse('/government'); |
2316 |
$route = Router::requestRoute(); |
2317 |
$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults); |
2318 |
|
2319 |
// test that an unmatched route does not change the current route
|
2320 |
Router::connect('/actor', $url); |
2321 |
Router::parse('/government'); |
2322 |
$route = Router::requestRoute(); |
2323 |
$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults); |
2324 |
} |
2325 |
|
2326 |
/**
|
2327 |
* testGetParams
|
2328 |
*
|
2329 |
* @return void
|
2330 |
*/
|
2331 |
public function testGetParams() { |
2332 |
$paths = array('base' => '/', 'here' => '/products/display/5', 'webroot' => '/webroot'); |
2333 |
$params = array('param1' => '1', 'param2' => '2'); |
2334 |
Router::setRequestInfo(array($params, $paths)); |
2335 |
|
2336 |
$expected = array( |
2337 |
'plugin' => null, 'controller' => false, 'action' => false, |
2338 |
'named' => array(), 'pass' => array(), |
2339 |
'param1' => '1', 'param2' => '2', |
2340 |
); |
2341 |
$this->assertEquals($expected, Router::getParams()); |
2342 |
$this->assertEquals(false, Router::getParam('controller')); |
2343 |
$this->assertEquals('1', Router::getParam('param1')); |
2344 |
$this->assertEquals('2', Router::getParam('param2')); |
2345 |
|
2346 |
Router::reload();
|
2347 |
|
2348 |
$params = array('controller' => 'pages', 'action' => 'display'); |
2349 |
Router::setRequestInfo(array($params, $paths)); |
2350 |
$expected = array( |
2351 |
'plugin' => null, 'controller' => 'pages', 'action' => 'display', |
2352 |
'named' => array(), 'pass' => array(), |
2353 |
); |
2354 |
$this->assertEquals($expected, Router::getParams()); |
2355 |
$this->assertEquals($expected, Router::getParams(true)); |
2356 |
} |
2357 |
|
2358 |
/**
|
2359 |
* test that connectDefaults() can disable default route connection
|
2360 |
*
|
2361 |
* @return void
|
2362 |
*/
|
2363 |
public function testDefaultsMethod() { |
2364 |
Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 2)); |
2365 |
$result = Router::parse('/posts/edit/5'); |
2366 |
$this->assertFalse(isset($result['controller'])); |
2367 |
$this->assertFalse(isset($result['action'])); |
2368 |
} |
2369 |
|
2370 |
/**
|
2371 |
* test that the required default routes are connected.
|
2372 |
*
|
2373 |
* @return void
|
2374 |
*/
|
2375 |
public function testConnectDefaultRoutes() { |
2376 |
App::build(array( |
2377 |
'plugins' => array( |
2378 |
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS |
2379 |
) |
2380 |
), App::RESET); |
2381 |
CakePlugin::load(array('TestPlugin', 'PluginJs')); |
2382 |
Router::reload();
|
2383 |
require CAKE . 'Config' . DS . 'routes.php'; |
2384 |
|
2385 |
$result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index')); |
2386 |
$this->assertEquals('/plugin_js/js_file', $result); |
2387 |
|
2388 |
$result = Router::parse('/plugin_js/js_file'); |
2389 |
$expected = array( |
2390 |
'plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index', |
2391 |
'named' => array(), 'pass' => array() |
2392 |
); |
2393 |
$this->assertEquals($expected, $result); |
2394 |
|
2395 |
$result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index')); |
2396 |
$this->assertEquals('/test_plugin', $result); |
2397 |
|
2398 |
$result = Router::parse('/test_plugin'); |
2399 |
$expected = array( |
2400 |
'plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index', |
2401 |
'named' => array(), 'pass' => array() |
2402 |
); |
2403 |
|
2404 |
$this->assertEquals($expected, $result, 'Plugin shortcut route broken. %s'); |
2405 |
} |
2406 |
|
2407 |
/**
|
2408 |
* test using a custom route class for route connection
|
2409 |
*
|
2410 |
* @return void
|
2411 |
*/
|
2412 |
public function testUsingCustomRouteClass() { |
2413 |
$this->getMock('CakeRoute', array(), array(), 'MockConnectedRoute', false); |
2414 |
$routes = Router::connect( |
2415 |
'/:slug',
|
2416 |
array('controller' => 'posts', 'action' => 'view'), |
2417 |
array('routeClass' => 'MockConnectedRoute', 'slug' => '[a-z_-]+') |
2418 |
); |
2419 |
$this->assertInstanceOf('MockConnectedRoute', $routes[0], 'Incorrect class used. %s'); |
2420 |
$expected = array('controller' => 'posts', 'action' => 'view', 'slug' => 'test'); |
2421 |
$routes[0]->expects($this->any()) |
2422 |
->method('parse')
|
2423 |
->will($this->returnValue($expected)); |
2424 |
$result = Router::parse('/test'); |
2425 |
$this->assertEquals($expected, $result); |
2426 |
} |
2427 |
|
2428 |
/**
|
2429 |
* test using custom route class in PluginDot notation
|
2430 |
*
|
2431 |
* @return void
|
2432 |
*/
|
2433 |
public function testUsingCustomRouteClassPluginDotSyntax() { |
2434 |
App::build(array( |
2435 |
'Plugin' => array( |
2436 |
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS |
2437 |
) |
2438 |
)); |
2439 |
CakePlugin::load('TestPlugin'); |
2440 |
App::uses('TestRoute', 'TestPlugin.Routing/Route'); |
2441 |
$routes = Router::connect( |
2442 |
'/:slug',
|
2443 |
array('controller' => 'posts', 'action' => 'view'), |
2444 |
array('routeClass' => 'TestPlugin.TestRoute', 'slug' => '[a-z_-]+') |
2445 |
); |
2446 |
$this->assertInstanceOf('TestRoute', $routes[0]); |
2447 |
CakePlugin::unload('TestPlugin'); |
2448 |
} |
2449 |
|
2450 |
/**
|
2451 |
* test that route classes must extend CakeRoute
|
2452 |
*
|
2453 |
* @expectedException RouterException
|
2454 |
* @return void
|
2455 |
*/
|
2456 |
public function testCustomRouteException() { |
2457 |
Router::connect('/:controller', array(), array('routeClass' => 'Object')); |
2458 |
} |
2459 |
|
2460 |
/**
|
2461 |
* test reversing parameter arrays back into strings.
|
2462 |
*
|
2463 |
* Mark the router as initialized so it doesn't auto-load routes
|
2464 |
*
|
2465 |
* @return void
|
2466 |
*/
|
2467 |
public function testRouterReverse() { |
2468 |
Router::$initialized = true; |
2469 |
|
2470 |
$params = array( |
2471 |
'controller' => 'posts', |
2472 |
'action' => 'view', |
2473 |
'pass' => array(1), |
2474 |
'named' => array(), |
2475 |
'url' => array(), |
2476 |
'autoRender' => 1, |
2477 |
'bare' => 1, |
2478 |
'return' => 1, |
2479 |
'requested' => 1, |
2480 |
'_Token' => array('key' => 'sekret') |
2481 |
); |
2482 |
$result = Router::reverse($params); |
2483 |
$this->assertEquals('/posts/view/1', $result); |
2484 |
|
2485 |
$params = array( |
2486 |
'controller' => 'posts', |
2487 |
'action' => 'index', |
2488 |
'pass' => array(1), |
2489 |
'named' => array('page' => 1, 'sort' => 'Article.title', 'direction' => 'desc'), |
2490 |
'url' => array() |
2491 |
); |
2492 |
$result = Router::reverse($params); |
2493 |
$this->assertEquals('/posts/index/1/page:1/sort:Article.title/direction:desc', $result); |
2494 |
|
2495 |
Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}')); |
2496 |
$params = array( |
2497 |
'lang' => 'eng', |
2498 |
'controller' => 'posts', |
2499 |
'action' => 'view', |
2500 |
'pass' => array(1), |
2501 |
'named' => array(), |
2502 |
'url' => array('url' => 'eng/posts/view/1') |
2503 |
); |
2504 |
$result = Router::reverse($params); |
2505 |
$this->assertEquals('/eng/posts/view/1', $result); |
2506 |
|
2507 |
$params = array( |
2508 |
'lang' => 'eng', |
2509 |
'controller' => 'posts', |
2510 |
'action' => 'view', |
2511 |
'pass' => array(1), |
2512 |
'named' => array(), |
2513 |
'url' => array('url' => 'eng/posts/view/1', 'foo' => 'bar', 'baz' => 'quu'), |
2514 |
'paging' => array(), |
2515 |
'models' => array() |
2516 |
); |
2517 |
$result = Router::reverse($params); |
2518 |
$this->assertEquals('/eng/posts/view/1?foo=bar&baz=quu', $result); |
2519 |
|
2520 |
$request = new CakeRequest('/eng/posts/view/1'); |
2521 |
$request->addParams(array( |
2522 |
'lang' => 'eng', |
2523 |
'controller' => 'posts', |
2524 |
'action' => 'view', |
2525 |
'pass' => array(1), |
2526 |
'named' => array(), |
2527 |
)); |
2528 |
$request->query = array('url' => 'eng/posts/view/1', 'test' => 'value'); |
2529 |
$result = Router::reverse($request); |
2530 |
$expected = '/eng/posts/view/1?test=value'; |
2531 |
$this->assertEquals($expected, $result); |
2532 |
|
2533 |
$params = array( |
2534 |
'lang' => 'eng', |
2535 |
'controller' => 'posts', |
2536 |
'action' => 'view', |
2537 |
'pass' => array(1), |
2538 |
'named' => array(), |
2539 |
'url' => array('url' => 'eng/posts/view/1') |
2540 |
); |
2541 |
$result = Router::reverse($params, true); |
2542 |
$this->assertRegExp('/^http(s)?:\/\//', $result); |
2543 |
} |
2544 |
|
2545 |
/**
|
2546 |
* Test that extensions work with Router::reverse()
|
2547 |
*
|
2548 |
* @return void
|
2549 |
*/
|
2550 |
public function testReverseWithExtension() { |
2551 |
Router::parseExtensions('json'); |
2552 |
|
2553 |
$request = new CakeRequest('/posts/view/1.json'); |
2554 |
$request->addParams(array( |
2555 |
'controller' => 'posts', |
2556 |
'action' => 'view', |
2557 |
'pass' => array(1), |
2558 |
'named' => array(), |
2559 |
'ext' => 'json', |
2560 |
)); |
2561 |
$request->query = array(); |
2562 |
$result = Router::reverse($request); |
2563 |
$expected = '/posts/view/1.json'; |
2564 |
$this->assertEquals($expected, $result); |
2565 |
} |
2566 |
|
2567 |
/**
|
2568 |
* test that setRequestInfo can accept arrays and turn that into a CakeRequest object.
|
2569 |
*
|
2570 |
* @return void
|
2571 |
*/
|
2572 |
public function testSetRequestInfoLegacy() { |
2573 |
Router::setRequestInfo(array( |
2574 |
array(
|
2575 |
'plugin' => null, 'controller' => 'images', 'action' => 'index', |
2576 |
'url' => array('url' => 'protected/images/index') |
2577 |
), |
2578 |
array(
|
2579 |
'base' => '', |
2580 |
'here' => '/protected/images/index', |
2581 |
'webroot' => '/', |
2582 |
) |
2583 |
)); |
2584 |
$result = Router::getRequest(); |
2585 |
$this->assertEquals('images', $result->controller); |
2586 |
$this->assertEquals('index', $result->action); |
2587 |
$this->assertEquals('', $result->base); |
2588 |
$this->assertEquals('/protected/images/index', $result->here); |
2589 |
$this->assertEquals('/', $result->webroot); |
2590 |
} |
2591 |
|
2592 |
/**
|
2593 |
* Test that Router::url() uses the first request
|
2594 |
*
|
2595 |
* @return void
|
2596 |
*/
|
2597 |
public function testUrlWithRequestAction() { |
2598 |
$firstRequest = new CakeRequest('/posts/index'); |
2599 |
$firstRequest->addParams(array( |
2600 |
'plugin' => null, |
2601 |
'controller' => 'posts', |
2602 |
'action' => 'index' |
2603 |
))->addPaths(array('base' => '')); |
2604 |
|
2605 |
$secondRequest = new CakeRequest('/posts/index'); |
2606 |
$secondRequest->addParams(array( |
2607 |
'requested' => 1, |
2608 |
'plugin' => null, |
2609 |
'controller' => 'comments', |
2610 |
'action' => 'listing' |
2611 |
))->addPaths(array('base' => '')); |
2612 |
|
2613 |
Router::setRequestInfo($firstRequest); |
2614 |
Router::setRequestInfo($secondRequest); |
2615 |
|
2616 |
$result = Router::url(array('base' => false)); |
2617 |
$this->assertEquals('/comments/listing', $result, 'with second requests, the last should win.'); |
2618 |
|
2619 |
Router::popRequest();
|
2620 |
$result = Router::url(array('base' => false)); |
2621 |
$this->assertEquals('/posts', $result, 'with second requests, the last should win.'); |
2622 |
} |
2623 |
|
2624 |
/**
|
2625 |
* test that a route object returning a full URL is not modified.
|
2626 |
*
|
2627 |
* @return void
|
2628 |
*/
|
2629 |
public function testUrlFullUrlReturnFromRoute() { |
2630 |
$url = 'http://example.com/posts/view/1'; |
2631 |
|
2632 |
$this->getMock('CakeRoute', array(), array('/'), 'MockReturnRoute'); |
2633 |
$routes = Router::connect('/:controller/:action', array(), array('routeClass' => 'MockReturnRoute')); |
2634 |
$routes[0]->expects($this->any())->method('match') |
2635 |
->will($this->returnValue($url)); |
2636 |
|
2637 |
$result = Router::url(array('controller' => 'posts', 'action' => 'view', 1)); |
2638 |
$this->assertEquals($url, $result); |
2639 |
} |
2640 |
|
2641 |
/**
|
2642 |
* test protocol in url
|
2643 |
*
|
2644 |
* @return void
|
2645 |
*/
|
2646 |
public function testUrlProtocol() { |
2647 |
$url = 'http://example.com'; |
2648 |
$this->assertEquals($url, Router::url($url)); |
2649 |
|
2650 |
$url = 'ed2k://example.com'; |
2651 |
$this->assertEquals($url, Router::url($url)); |
2652 |
|
2653 |
$url = 'svn+ssh://example.com'; |
2654 |
$this->assertEquals($url, Router::url($url)); |
2655 |
|
2656 |
$url = '://example.com'; |
2657 |
$this->assertEquals($url, Router::url($url)); |
2658 |
|
2659 |
$url = '//example.com'; |
2660 |
$this->assertEquals($url, Router::url($url)); |
2661 |
|
2662 |
$url = 'javascript:void(0)'; |
2663 |
$this->assertEquals($url, Router::url($url)); |
2664 |
|
2665 |
$url = 'tel:012345-678'; |
2666 |
$this->assertEquals($url, Router::url($url)); |
2667 |
|
2668 |
$url = 'sms:012345-678'; |
2669 |
$this->assertEquals($url, Router::url($url)); |
2670 |
|
2671 |
$url = '#here'; |
2672 |
$this->assertEquals($url, Router::url($url)); |
2673 |
|
2674 |
$url = '?param=0'; |
2675 |
$this->assertEquals($url, Router::url($url)); |
2676 |
|
2677 |
$url = 'posts/index#here'; |
2678 |
$expected = FULL_BASE_URL . '/posts/index#here'; |
2679 |
$this->assertEquals($expected, Router::url($url, true)); |
2680 |
} |
2681 |
|
2682 |
/**
|
2683 |
* Testing that patterns on the :action param work properly.
|
2684 |
*
|
2685 |
* @return void
|
2686 |
*/
|
2687 |
public function testPatternOnAction() { |
2688 |
$route = new CakeRoute( |
2689 |
'/blog/:action/*',
|
2690 |
array('controller' => 'blog_posts'), |
2691 |
array('action' => 'other|actions') |
2692 |
); |
2693 |
$result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo')); |
2694 |
$this->assertFalse($result); |
2695 |
|
2696 |
$result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions')); |
2697 |
$this->assertEquals('/blog/actions/', $result); |
2698 |
|
2699 |
$result = $route->parse('/blog/other'); |
2700 |
$expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => array(), 'named' => array()); |
2701 |
$this->assertEquals($expected, $result); |
2702 |
|
2703 |
$result = $route->parse('/blog/foobar'); |
2704 |
$this->assertFalse($result); |
2705 |
} |
2706 |
|
2707 |
/**
|
2708 |
* Tests resourceMap as getter and setter.
|
2709 |
*
|
2710 |
* @return void
|
2711 |
*/
|
2712 |
public function testResourceMap() { |
2713 |
$default = Router::resourceMap(); |
2714 |
$expected = array( |
2715 |
array('action' => 'index', 'method' => 'GET', 'id' => false), |
2716 |
array('action' => 'view', 'method' => 'GET', 'id' => true), |
2717 |
array('action' => 'add', 'method' => 'POST', 'id' => false), |
2718 |
array('action' => 'edit', 'method' => 'PUT', 'id' => true), |
2719 |
array('action' => 'delete', 'method' => 'DELETE', 'id' => true), |
2720 |
array('action' => 'edit', 'method' => 'POST', 'id' => true) |
2721 |
); |
2722 |
$this->assertEquals($expected, $default); |
2723 |
|
2724 |
$custom = array( |
2725 |
array('action' => 'index', 'method' => 'GET', 'id' => false), |
2726 |
array('action' => 'view', 'method' => 'GET', 'id' => true), |
2727 |
array('action' => 'add', 'method' => 'POST', 'id' => false), |
2728 |
array('action' => 'edit', 'method' => 'PUT', 'id' => true), |
2729 |
array('action' => 'delete', 'method' => 'DELETE', 'id' => true), |
2730 |
array('action' => 'update', 'method' => 'POST', 'id' => true) |
2731 |
); |
2732 |
Router::resourceMap($custom); |
2733 |
$this->assertEquals(Router::resourceMap(), $custom); |
2734 |
|
2735 |
Router::resourceMap($default); |
2736 |
} |
2737 |
|
2738 |
/**
|
2739 |
* test setting redirect routes
|
2740 |
*
|
2741 |
* @return void
|
2742 |
*/
|
2743 |
public function testRouteRedirection() { |
2744 |
Router::redirect('/blog', array('controller' => 'posts'), array('status' => 302)); |
2745 |
$this->assertEquals(1, count(Router::$routes)); |
2746 |
Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader')); |
2747 |
Router::$routes[0]->stop = false; |
2748 |
$this->assertEquals(302, Router::$routes[0]->options['status']); |
2749 |
|
2750 |
Router::parse('/blog'); |
2751 |
$header = Router::$routes[0]->response->header(); |
2752 |
$this->assertEquals(Router::url('/posts', true), $header['Location']); |
2753 |
$this->assertEquals(302, Router::$routes[0]->response->statusCode()); |
2754 |
|
2755 |
Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader')); |
2756 |
Router::parse('/not-a-match'); |
2757 |
$this->assertEquals(array(), Router::$routes[0]->response->header()); |
2758 |
} |
2759 |
|
2760 |
/**
|
2761 |
* Test setting the default route class
|
2762 |
*
|
2763 |
* @return void
|
2764 |
*/
|
2765 |
public function testDefaultRouteClass() { |
2766 |
$this->getMock('CakeRoute', array(), array('/test'), 'TestDefaultRouteClass'); |
2767 |
Router::defaultRouteClass('TestDefaultRouteClass'); |
2768 |
|
2769 |
$result = Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); |
2770 |
$this->assertInstanceOf('TestDefaultRouteClass', $result[0]); |
2771 |
} |
2772 |
|
2773 |
/**
|
2774 |
* Test getting the default route class
|
2775 |
*
|
2776 |
* @return void
|
2777 |
*/
|
2778 |
public function testDefaultRouteClassGetter() { |
2779 |
$routeClass = 'TestDefaultRouteClass'; |
2780 |
Router::defaultRouteClass($routeClass); |
2781 |
|
2782 |
$this->assertEquals($routeClass, Router::defaultRouteClass()); |
2783 |
$this->assertEquals($routeClass, Router::defaultRouteClass(null)); |
2784 |
} |
2785 |
|
2786 |
/**
|
2787 |
* Test that route classes must extend CakeRoute
|
2788 |
*
|
2789 |
* @expectedException RouterException
|
2790 |
* @return void
|
2791 |
*/
|
2792 |
public function testDefaultRouteException() { |
2793 |
Router::defaultRouteClass(''); |
2794 |
Router::connect('/:controller', array()); |
2795 |
} |
2796 |
|
2797 |
/**
|
2798 |
* Test that route classes must extend CakeRoute
|
2799 |
*
|
2800 |
* @expectedException RouterException
|
2801 |
* @return void
|
2802 |
*/
|
2803 |
public function testSettingInvalidDefaultRouteException() { |
2804 |
Router::defaultRouteClass('Object'); |
2805 |
} |
2806 |
|
2807 |
/**
|
2808 |
* Test that class must exist
|
2809 |
*
|
2810 |
* @expectedException RouterException
|
2811 |
* @return void
|
2812 |
*/
|
2813 |
public function testSettingNonExistentDefaultRouteException() { |
2814 |
Router::defaultRouteClass('NonExistentClass'); |
2815 |
} |
2816 |
|
2817 |
/**
|
2818 |
* Tests generating well-formed querystrings
|
2819 |
*
|
2820 |
* @return void
|
2821 |
*/
|
2822 |
public function testQueryString() { |
2823 |
$result = Router::queryString(array('var' => 'foo bar')); |
2824 |
$expected = '?var=foo+bar'; |
2825 |
$this->assertEquals($expected, $result); |
2826 |
|
2827 |
$result = Router::queryString(false, array('some' => 'param', 'foo' => 'bar')); |
2828 |
$expected = '?some=param&foo=bar'; |
2829 |
$this->assertEquals($expected, $result); |
2830 |
|
2831 |
$existing = array('apple' => 'red', 'pear' => 'green'); |
2832 |
$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); |
2833 |
$expected = '?apple=red&pear=green&some=param&foo=bar'; |
2834 |
$this->assertEquals($expected, $result); |
2835 |
|
2836 |
$existing = 'apple=red&pear=green'; |
2837 |
$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); |
2838 |
$expected = '?apple=red&pear=green&some=param&foo=bar'; |
2839 |
$this->assertEquals($expected, $result); |
2840 |
|
2841 |
$existing = '?apple=red&pear=green'; |
2842 |
$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar')); |
2843 |
$expected = '?apple=red&pear=green&some=param&foo=bar'; |
2844 |
$this->assertEquals($expected, $result); |
2845 |
|
2846 |
$result = Router::queryString('apple=red&pear=green'); |
2847 |
$expected = '?apple=red&pear=green'; |
2848 |
$this->assertEquals($expected, $result); |
2849 |
|
2850 |
$result = Router::queryString('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true); |
2851 |
$expected = '?foo=bar&php=nut&jose=zap'; |
2852 |
$this->assertEquals($expected, $result); |
2853 |
|
2854 |
$result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap'), true); |
2855 |
$expected = '?foo=bar&php=nut&jose=zap'; |
2856 |
$this->assertEquals($expected, $result); |
2857 |
|
2858 |
$result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap')); |
2859 |
$expected = '?foo=bar&php=nut&jose=zap'; |
2860 |
$this->assertEquals($expected, $result); |
2861 |
} |
2862 |
} |