comparison cake/tests/cases/libs/router.test.php @ 0:261e66bd5a0c

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