2010-05-04 04:35:22 +00:00
|
|
|
<?php
|
2011-10-10 21:18:48 +00:00
|
|
|
/**
|
|
|
|
* CakeRequest Test case file.
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-10 21:18:48 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-10 21:18:48 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package Cake.Test.Case.Routing.Route
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2010-05-04 04:35:22 +00:00
|
|
|
|
2010-12-22 02:16:12 +00:00
|
|
|
App::uses('CakeRoute', 'Routing/Route');
|
2010-12-10 06:23:27 +00:00
|
|
|
App::uses('Router', 'Routing');
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test case for CakeRoute
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Routing.Route
|
2010-05-04 04:35:22 +00:00
|
|
|
**/
|
2011-09-13 03:00:35 +00:00
|
|
|
class CakeRouteTest extends CakeTestCase {
|
2012-03-13 01:36:27 +00:00
|
|
|
|
2010-05-04 04:35:22 +00:00
|
|
|
/**
|
2010-09-26 01:36:49 +00:00
|
|
|
* setUp method
|
2010-05-04 04:35:22 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::setUp();
|
2010-05-04 04:35:22 +00:00
|
|
|
Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the construction of a CakeRoute
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConstruction() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/:id', array(), array('id' => '[0-9]+'));
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/:controller/:action/:id', $route->template);
|
|
|
|
$this->assertEquals(array(), $route->defaults);
|
|
|
|
$this->assertEquals(array('id' => '[0-9]+'), $route->options);
|
2010-05-04 04:35:22 +00:00
|
|
|
$this->assertFalse($route->compiled());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test Route compiling.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBasicRouteCompiling() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
|
|
|
|
$result = $route->compile();
|
|
|
|
$expected = '#^/*$#';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array(), $route->keys);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/:controller/:action', array('controller' => 'posts'));
|
|
|
|
$result = $route->compile();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/posts/edit');
|
|
|
|
$this->assertRegExp($result, '/posts/super_delete');
|
|
|
|
$this->assertNotRegExp($result, '/posts');
|
|
|
|
$this->assertNotRegExp($result, '/posts/super_delete/1');
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/posts/foo:id', array('controller' => 'posts', 'action' => 'view'));
|
|
|
|
$result = $route->compile();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/posts/foo:1');
|
|
|
|
$this->assertRegExp($result, '/posts/foo:param');
|
|
|
|
$this->assertNotRegExp($result, '/posts');
|
|
|
|
$this->assertNotRegExp($result, '/posts/');
|
2010-05-04 04:35:22 +00:00
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('id'), $route->keys);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/:plugin/:controller/:action/*', array('plugin' => 'test_plugin', 'action' => 'index'));
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/test_plugin/posts/index');
|
|
|
|
$this->assertRegExp($result, '/test_plugin/posts/edit/5');
|
|
|
|
$this->assertRegExp($result, '/test_plugin/posts/edit/5/name:value/nick:name');
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that route parameters that overlap don't cause errors.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRouteParameterOverlap() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute('/invoices/add/:idd/:id', array('controller' => 'invoices', 'action' => 'add'));
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/invoices/add/1/3');
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/invoices/add/:id/:idd', array('controller' => 'invoices', 'action' => 'add'));
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/invoices/add/1/3');
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test compiling routes with keys that have patterns
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRouteCompilingWithParamPatterns() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute(
|
|
|
|
'/:controller/:action/:id',
|
|
|
|
array(),
|
2010-05-05 03:05:45 +00:00
|
|
|
array('id' => Router::ID)
|
2010-05-04 04:35:22 +00:00
|
|
|
);
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/posts/edit/1');
|
|
|
|
$this->assertRegExp($result, '/posts/view/518098');
|
|
|
|
$this->assertNotRegExp($result, '/posts/edit/name-of-post');
|
|
|
|
$this->assertNotRegExp($result, '/posts/edit/4/other:param');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('controller', 'action', 'id'), $route->keys);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute(
|
|
|
|
'/:lang/:controller/:action/:id',
|
|
|
|
array('controller' => 'testing4'),
|
2010-05-05 03:05:45 +00:00
|
|
|
array('id' => Router::ID, 'lang' => '[a-z]{3}')
|
2010-05-04 04:35:22 +00:00
|
|
|
);
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/eng/posts/edit/1');
|
|
|
|
$this->assertRegExp($result, '/cze/articles/view/1');
|
|
|
|
$this->assertNotRegExp($result, '/language/articles/view/2');
|
|
|
|
$this->assertNotRegExp($result, '/eng/articles/view/name-of-article');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('lang', 'controller', 'action', 'id'), $route->keys);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
foreach (array(':', '@', ';', '$', '-') as $delim) {
|
|
|
|
$route = new CakeRoute('/posts/:id' . $delim . ':title');
|
|
|
|
$result = $route->compile();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/posts/1' . $delim . 'name-of-article');
|
|
|
|
$this->assertRegExp($result, '/posts/13244' . $delim . 'name-of_Article[]');
|
|
|
|
$this->assertNotRegExp($result, '/posts/11!nameofarticle');
|
|
|
|
$this->assertNotRegExp($result, '/posts/11');
|
2010-05-04 04:35:22 +00:00
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('id', 'title'), $route->keys);
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$route = new CakeRoute(
|
|
|
|
'/posts/:id::title/:year',
|
|
|
|
array('controller' => 'posts', 'action' => 'view'),
|
2010-05-05 03:05:45 +00:00
|
|
|
array('id' => Router::ID, 'year' => Router::YEAR, 'title' => '[a-z-_]+')
|
2010-05-04 04:35:22 +00:00
|
|
|
);
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/posts/1:name-of-article/2009/');
|
|
|
|
$this->assertRegExp($result, '/posts/13244:name-of-article/1999');
|
|
|
|
$this->assertNotRegExp($result, '/posts/hey_now:nameofarticle');
|
|
|
|
$this->assertNotRegExp($result, '/posts/:nameofarticle/2009');
|
|
|
|
$this->assertNotRegExp($result, '/posts/:nameofarticle/01');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('id', 'title', 'year'), $route->keys);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute(
|
|
|
|
'/posts/:url_title-(uuid::id)',
|
|
|
|
array('controller' => 'posts', 'action' => 'view'),
|
2010-05-05 03:05:45 +00:00
|
|
|
array('pass' => array('id', 'url_title'), 'id' => Router::ID)
|
2010-05-04 04:35:22 +00:00
|
|
|
);
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)/');
|
|
|
|
$this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)');
|
|
|
|
$this->assertNotRegExp($result, '/posts/');
|
|
|
|
$this->assertNotRegExp($result, '/posts/nameofarticle');
|
|
|
|
$this->assertNotRegExp($result, '/posts/nameofarticle-12347');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('url_title', 'id'), $route->keys);
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test more complex route compiling & parsing with mid route greedy stars
|
|
|
|
* and optional routing parameters
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testComplexRouteCompilingAndParsing() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute(
|
|
|
|
'/posts/:month/:day/:year/*',
|
|
|
|
array('controller' => 'posts', 'action' => 'view'),
|
2010-05-05 03:05:45 +00:00
|
|
|
array('year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY)
|
2010-05-04 04:35:22 +00:00
|
|
|
);
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/posts/08/01/2007/title-of-post');
|
2010-05-04 04:35:22 +00:00
|
|
|
$result = $route->parse('/posts/08/01/2007/title-of-post');
|
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(7, count($result));
|
|
|
|
$this->assertEquals('posts', $result['controller']);
|
|
|
|
$this->assertEquals('view', $result['action']);
|
|
|
|
$this->assertEquals('2007', $result['year']);
|
|
|
|
$this->assertEquals('08', $result['month']);
|
|
|
|
$this->assertEquals('01', $result['day']);
|
|
|
|
$this->assertEquals('title-of-post', $result['pass'][0]);
|
2011-05-16 22:49:00 +00:00
|
|
|
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute(
|
|
|
|
"/:extra/page/:slug/*",
|
|
|
|
array('controller' => 'pages', 'action' => 'view', 'extra' => null),
|
|
|
|
array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
|
|
|
|
);
|
|
|
|
$result = $route->compile();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp($result, '/some_extra/page/this_is_the_slug');
|
|
|
|
$this->assertRegExp($result, '/page/this_is_the_slug');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('extra', 'slug'), $route->keys);
|
|
|
|
$this->assertEquals(array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'), $route->options);
|
2010-05-04 04:35:22 +00:00
|
|
|
$expected = array(
|
|
|
|
'controller' => 'pages',
|
2010-12-18 19:16:00 +00:00
|
|
|
'action' => 'view'
|
2010-05-04 04:35:22 +00:00
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $route->defaults);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute(
|
|
|
|
'/:controller/:action/*',
|
|
|
|
array('project' => false),
|
|
|
|
array(
|
|
|
|
'controller' => 'source|wiki|commits|tickets|comments|view',
|
|
|
|
'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertFalse($route->parse('/chaw_test/wiki'));
|
|
|
|
|
|
|
|
$result = $route->compile();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertNotRegExp($result, '/some_project/source');
|
|
|
|
$this->assertRegExp($result, '/source/view');
|
|
|
|
$this->assertRegExp($result, '/source/view/other/params');
|
|
|
|
$this->assertNotRegExp($result, '/chaw_test/wiki');
|
|
|
|
$this->assertNotRegExp($result, '/source/wierd_action');
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that routes match their pattern.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMatchBasic() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/:id', array('plugin' => null));
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 0));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/1', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
|
|
|
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$route = new CakeRoute('/pages/*', array('controller' => 'pages', 'action' => 'display'));
|
|
|
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/pages/home', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/pages/about', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/blog/:action', array('controller' => 'posts'));
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/blog/view', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'nodes', 'action' => 'view'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 1));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'id' => 2));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$route = new CakeRoute('/foo/:controller/:action', array('action' => 'index'));
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/foo/posts/view', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'));
|
|
|
|
$result = $route->match(array('plugin' => 'test', 'controller' => 'posts', 'action' => 'view', 'id' => '1'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/test/1/', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => 'fo', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/fo/1/0', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => 'fo', 'controller' => 'nodes', 'action' => 'view', 'id' => 1));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => 'fo', 'controller' => 'posts', 'action' => 'edit', 'id' => 1));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$route = new CakeRoute('/admin/subscriptions/:action/*', array(
|
|
|
|
'controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'
|
|
|
|
));
|
|
|
|
|
|
|
|
$url = array('controller' => 'subscribe', 'admin' => true, 'action' => 'edit', 1);
|
|
|
|
$result = $route->match($url);
|
|
|
|
$expected = '/admin/subscriptions/edit/1';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-04-25 00:48:23 +00:00
|
|
|
|
|
|
|
$url = array(
|
|
|
|
'controller' => 'subscribe',
|
|
|
|
'admin' => true,
|
|
|
|
'action' => 'edit_admin_e',
|
|
|
|
1
|
|
|
|
);
|
|
|
|
$result = $route->match($url);
|
|
|
|
$expected = '/admin/subscriptions/edit_admin_e/1';
|
|
|
|
$this->assertEquals($expected, $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
|
|
|
|
2010-12-18 19:16:00 +00:00
|
|
|
/**
|
|
|
|
* test that non-greedy routes fail with extra passed args
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGreedyRouteFailurePassedArg() {
|
2010-12-18 19:16:00 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action', array('plugin' => null));
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', '0'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$route = new CakeRoute('/:controller/:action', array('plugin' => null));
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'test'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that non-greedy routes fail with extra passed args
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGreedyRouteFailureNamedParam() {
|
2010-12-18 19:16:00 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action', array('plugin' => null));
|
2010-12-20 03:17:57 +00:00
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'page' => 1));
|
2010-12-18 19:16:00 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2010-12-18 18:32:05 +00:00
|
|
|
/**
|
|
|
|
* test that falsey values do not interrupt a match.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMatchWithFalseyValues() {
|
2010-12-18 18:32:05 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
|
|
|
|
$result = $route->match(array(
|
|
|
|
'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'admin' => false
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/index/', $result);
|
2010-12-18 18:32:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-04 04:35:22 +00:00
|
|
|
/**
|
|
|
|
* test match() with greedy routes, named parameters and passed args.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMatchWithNamedParametersAndPassedArgs() {
|
2010-05-04 04:35:22 +00:00
|
|
|
Router::connectNamed(true);
|
|
|
|
|
|
|
|
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, 'page' => 1));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/index/page:1', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/5', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
2010-12-18 17:34:48 +00:00
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 0));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/0', $result);
|
2010-12-18 17:34:48 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, '0'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/0', $result);
|
2010-12-18 17:34:48 +00:00
|
|
|
|
2010-05-04 04:35:22 +00:00
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, 'page' => 1, 'limit' => 20, 'order' => 'title'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/5/page:1/limit:20/order:title', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
2011-10-30 00:42:07 +00:00
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 'word space', 'order' => 'Θ'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/word%20space/order:%CE%98', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new CakeRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
|
|
|
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 1));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 2, 'something'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/test2/something', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 5, 'something'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2011-10-26 01:08:56 +00:00
|
|
|
/**
|
|
|
|
* Ensure that named parameters are urldecoded
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-25 13:53:31 +00:00
|
|
|
public function testParseNamedParametersUrlDecode() {
|
|
|
|
Router::connectNamed(true);
|
|
|
|
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
|
|
|
|
|
|
|
|
$result = $route->parse('/posts/index/page:%CE%98');
|
|
|
|
$this->assertEquals('Θ', $result['named']['page']);
|
|
|
|
|
|
|
|
$result = $route->parse('/posts/index/page[]:%CE%98');
|
|
|
|
$this->assertEquals('Θ', $result['named']['page'][0]);
|
2011-10-30 00:42:07 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/posts/index/something%20else/page[]:%CE%98');
|
|
|
|
$this->assertEquals('Θ', $result['named']['page'][0]);
|
|
|
|
$this->assertEquals('something else', $result['pass'][0]);
|
2011-10-25 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 22:25:48 +00:00
|
|
|
/**
|
|
|
|
* Ensure that keys at named parameters are urldecoded
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testParseNamedKeyUrlDecode() {
|
|
|
|
Router::connectNamed(true);
|
|
|
|
$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
|
|
|
|
|
|
|
|
// checking /post/index/user[0]:a/user[1]:b
|
|
|
|
$result = $route->parse('/posts/index/user%5B0%5D:a/user%5B1%5D:b');
|
|
|
|
$this->assertArrayHasKey('user', $result['named']);
|
|
|
|
$this->assertEquals(array('a', 'b'), $result['named']['user']);
|
|
|
|
|
|
|
|
// checking /post/index/user[]:a/user[]:b
|
|
|
|
$result = $route->parse('/posts/index/user%5B%5D:a/user%5B%5D:b');
|
|
|
|
$this->assertArrayHasKey('user', $result['named']);
|
|
|
|
$this->assertEquals(array('a', 'b'), $result['named']['user']);
|
|
|
|
}
|
|
|
|
|
2010-12-18 18:40:07 +00:00
|
|
|
/**
|
|
|
|
* test that named params with null/false are excluded
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNamedParamsWithNullFalse() {
|
2010-12-18 19:36:11 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/*');
|
2010-12-20 03:17:57 +00:00
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'page' => null, 'sort' => false));
|
2010-12-18 18:40:07 +00:00
|
|
|
$this->assertEquals('/posts/index/', $result);
|
|
|
|
}
|
|
|
|
|
2010-05-04 04:35:22 +00:00
|
|
|
/**
|
|
|
|
* test that match with patterns works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMatchWithPatterns() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/:id', array('plugin' => null), array('id' => '[0-9]+'));
|
|
|
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'id' => 'foo'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '9'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/9', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '922'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/posts/view/922', $result);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 'a99'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test persistParams ability to persist parameters from $params and remove params.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPersistParams() {
|
2010-05-04 04:35:22 +00:00
|
|
|
$route = new CakeRoute(
|
|
|
|
'/:lang/:color/blog/:action',
|
|
|
|
array('controller' => 'posts'),
|
|
|
|
array('persist' => array('lang', 'color'))
|
|
|
|
);
|
|
|
|
$url = array('controller' => 'posts', 'action' => 'index');
|
|
|
|
$params = array('lang' => 'en', 'color' => 'blue');
|
|
|
|
$result = $route->persistParams($url, $params);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('en', $result['lang']);
|
|
|
|
$this->assertEquals('blue', $result['color']);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$url = array('controller' => 'posts', 'action' => 'index', 'color' => 'red');
|
|
|
|
$params = array('lang' => 'en', 'color' => 'blue');
|
|
|
|
$result = $route->persistParams($url, $params);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('en', $result['lang']);
|
|
|
|
$this->assertEquals('red', $result['color']);
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the parse method of CakeRoute.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParse() {
|
2010-05-05 03:05:45 +00:00
|
|
|
$route = new CakeRoute(
|
|
|
|
'/:controller/:action/:id',
|
|
|
|
array('controller' => 'testing4', 'id' => null),
|
|
|
|
array('id' => Router::ID)
|
|
|
|
);
|
2010-05-04 04:35:22 +00:00
|
|
|
$route->compile();
|
|
|
|
$result = $route->parse('/posts/view/1');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('posts', $result['controller']);
|
|
|
|
$this->assertEquals('view', $result['action']);
|
|
|
|
$this->assertEquals('1', $result['id']);
|
2010-05-04 04:35:22 +00:00
|
|
|
|
|
|
|
$route = new Cakeroute(
|
|
|
|
'/admin/:controller',
|
|
|
|
array('prefix' => 'admin', 'admin' => 1, 'action' => 'index')
|
|
|
|
);
|
|
|
|
$route->compile();
|
|
|
|
$result = $route->parse('/admin/');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->parse('/admin/posts');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('posts', $result['controller']);
|
|
|
|
$this->assertEquals('index', $result['action']);
|
2010-05-04 04:35:22 +00:00
|
|
|
}
|
2010-11-01 01:56:59 +00:00
|
|
|
|
2012-02-18 15:53:47 +00:00
|
|
|
/**
|
|
|
|
* Test that :key elements are urldecoded
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testParseUrlDecodeElements() {
|
|
|
|
$route = new Cakeroute(
|
|
|
|
'/:controller/:slug',
|
|
|
|
array('action' => 'view')
|
|
|
|
);
|
|
|
|
$route->compile();
|
2012-02-18 16:18:03 +00:00
|
|
|
$result = $route->parse('/posts/%E2%88%82%E2%88%82');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('posts', $result['controller']);
|
|
|
|
$this->assertEquals('view', $result['action']);
|
|
|
|
$this->assertEquals('∂∂', $result['slug']);
|
2012-02-18 16:18:03 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/posts/∂∂');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('posts', $result['controller']);
|
|
|
|
$this->assertEquals('view', $result['action']);
|
|
|
|
$this->assertEquals('∂∂', $result['slug']);
|
2012-02-18 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 12:30:22 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test numerically indexed defaults, get appended to pass
|
2011-03-03 12:30:22 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParseWithPassDefaults() {
|
2011-03-03 12:30:22 +00:00
|
|
|
$route = new Cakeroute('/:controller', array('action' => 'display', 'home'));
|
|
|
|
$result = $route->parse('/posts');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'display',
|
|
|
|
'pass' => array('home'),
|
|
|
|
'named' => array()
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that http header conditions can cause route failures.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParseWithHttpHeaderConditions() {
|
2011-03-03 12:30:22 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
$route = new CakeRoute('/sample', array('controller' => 'posts', 'action' => 'index', '[method]' => 'POST'));
|
|
|
|
|
|
|
|
$this->assertFalse($route->parse('/sample'));
|
|
|
|
}
|
|
|
|
|
2010-11-01 01:56:59 +00:00
|
|
|
/**
|
|
|
|
* test that patterns work for :action
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPatternOnAction() {
|
2010-11-13 04:05:44 +00:00
|
|
|
$route = new CakeRoute(
|
2011-05-16 22:49:00 +00:00
|
|
|
'/blog/:action/*',
|
|
|
|
array('controller' => 'blog_posts'),
|
2010-11-01 01:56:59 +00:00
|
|
|
array('action' => 'other|actions')
|
|
|
|
);
|
|
|
|
$result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
|
|
|
|
$result = $route->parse('/blog/other');
|
|
|
|
$expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => array(), 'named' => array());
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-11-01 01:56:59 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/blog/foobar');
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2010-12-18 21:44:21 +00:00
|
|
|
|
2011-03-03 11:11:41 +00:00
|
|
|
/**
|
|
|
|
* test the parseArgs method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParsePassedArgument() {
|
2011-03-03 11:11:41 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/*');
|
|
|
|
$result = $route->parse('/posts/edit/1/2/0');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'edit',
|
|
|
|
'pass' => array('1', '2', '0'),
|
|
|
|
'named' => array()
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
2011-03-03 11:22:59 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/posts/edit/a-string/page:1/sort:value');
|
2011-03-03 11:11:41 +00:00
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'edit',
|
|
|
|
'pass' => array('a-string'),
|
|
|
|
'named' => array(
|
2011-03-03 11:22:59 +00:00
|
|
|
'page' => 1,
|
|
|
|
'sort' => 'value'
|
2011-03-03 11:11:41 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2011-03-03 11:22:59 +00:00
|
|
|
|
2011-03-03 11:36:02 +00:00
|
|
|
/**
|
|
|
|
* test that only named parameter rules are followed.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParseNamedParametersWithRules() {
|
2011-03-03 11:36:02 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/*', array(), array(
|
|
|
|
'named' => array(
|
|
|
|
'wibble',
|
|
|
|
'fish' => array('action' => 'index'),
|
2011-03-03 11:54:46 +00:00
|
|
|
'fizz' => array('controller' => array('comments', 'other')),
|
2011-03-03 11:49:11 +00:00
|
|
|
'pattern' => 'val-[\d]+'
|
2011-03-03 11:36:02 +00:00
|
|
|
)
|
|
|
|
));
|
2011-03-03 11:49:11 +00:00
|
|
|
$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz/unknown:value');
|
2011-03-03 11:36:02 +00:00
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'display',
|
2011-03-03 11:49:11 +00:00
|
|
|
'pass' => array('fish:trout', 'fizz:buzz', 'unknown:value'),
|
2011-03-03 11:36:02 +00:00
|
|
|
'named' => array(
|
|
|
|
'wibble' => 'spin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result, 'Fish should not be parsed, as action != index');
|
|
|
|
|
|
|
|
$result = $route->parse('/posts/index/wibble:spin/fish:trout/fizz:buzz');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'index',
|
|
|
|
'pass' => array('fizz:buzz'),
|
|
|
|
'named' => array(
|
|
|
|
'wibble' => 'spin',
|
|
|
|
'fish' => 'trout'
|
|
|
|
)
|
|
|
|
);
|
2011-11-18 01:30:34 +00:00
|
|
|
$this->assertEquals($expected, $result, 'Fizz should be parsed, as controller == comments|other');
|
2011-03-03 11:36:02 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/comments/index/wibble:spin/fish:trout/fizz:buzz');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'comments',
|
|
|
|
'action' => 'index',
|
|
|
|
'pass' => array(),
|
|
|
|
'named' => array(
|
|
|
|
'wibble' => 'spin',
|
|
|
|
'fish' => 'trout',
|
|
|
|
'fizz' => 'buzz'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
|
2011-03-03 11:49:11 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/comments/index/pattern:val--');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'comments',
|
|
|
|
'action' => 'index',
|
|
|
|
'pass' => array('pattern:val--'),
|
|
|
|
'named' => array()
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result, 'Named parameter pattern unmet.');
|
|
|
|
|
|
|
|
$result = $route->parse('/comments/index/pattern:val-2');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'comments',
|
|
|
|
'action' => 'index',
|
|
|
|
'pass' => array(),
|
|
|
|
'named' => array('pattern' => 'val-2')
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result, 'Named parameter pattern met.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that greedyNamed ignores rules.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParseGreedyNamed() {
|
2011-03-03 11:49:11 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/*', array(), array(
|
|
|
|
'named' => array(
|
|
|
|
'fizz' => array('controller' => 'comments'),
|
|
|
|
'pattern' => 'val-[\d]+',
|
|
|
|
),
|
|
|
|
'greedyNamed' => true
|
|
|
|
));
|
|
|
|
$result = $route->parse('/posts/display/wibble:spin/fizz:buzz/pattern:ignored');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'display',
|
|
|
|
'pass' => array('fizz:buzz', 'pattern:ignored'),
|
|
|
|
'named' => array(
|
|
|
|
'wibble' => 'spin',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result, 'Greedy named grabs everything, rules are followed');
|
2011-03-03 11:36:02 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 01:30:34 +00:00
|
|
|
/**
|
|
|
|
* Having greedNamed enabled should not capture routing.prefixes.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testMatchGreedyNamedExcludesPrefixes() {
|
|
|
|
Configure::write('Routing.prefixes', array('admin'));
|
|
|
|
Router::reload();
|
|
|
|
|
|
|
|
$route = new CakeRoute('/sales/*', array('controller' => 'sales', 'action' => 'index'));
|
|
|
|
$this->assertFalse($route->match(array('controller' => 'sales', 'action' => 'index', 'admin' => 1)), 'Greedy named consume routing prefixes.');
|
|
|
|
}
|
|
|
|
|
2011-03-03 11:22:59 +00:00
|
|
|
/**
|
|
|
|
* test that parsing array format named parameters works
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParseArrayNamedParameters() {
|
2011-03-03 11:22:59 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/*');
|
|
|
|
$result = $route->parse('/tests/action/var[]:val1/var[]:val2');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'tests',
|
|
|
|
'action' => 'action',
|
|
|
|
'named' => array(
|
|
|
|
'var' => array(
|
|
|
|
'val1',
|
|
|
|
'val2'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'pass' => array(),
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-03-03 11:22:59 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/tests/action/theanswer[is]:42/var[]:val2/var[]:val3');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'tests',
|
|
|
|
'action' => 'action',
|
|
|
|
'named' => array(
|
|
|
|
'theanswer' => array(
|
|
|
|
'is' => 42
|
|
|
|
),
|
|
|
|
'var' => array(
|
|
|
|
'val2',
|
|
|
|
'val3'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'pass' => array(),
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-03-03 11:22:59 +00:00
|
|
|
|
|
|
|
$result = $route->parse('/tests/action/theanswer[is][not]:42/theanswer[]:5/theanswer[is]:6');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'tests',
|
|
|
|
'action' => 'action',
|
|
|
|
'named' => array(
|
|
|
|
'theanswer' => array(
|
|
|
|
5,
|
|
|
|
'is' => array(
|
|
|
|
6,
|
|
|
|
'not' => 42
|
|
|
|
)
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'pass' => array(),
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-03-03 11:22:59 +00:00
|
|
|
}
|
2011-03-03 12:30:22 +00:00
|
|
|
|
2011-12-02 01:32:28 +00:00
|
|
|
/**
|
|
|
|
* Test that match can handle array named parameters
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testMatchNamedParametersArray() {
|
|
|
|
$route = new CakeRoute('/:controller/:action/*');
|
|
|
|
|
|
|
|
$url = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'index',
|
|
|
|
'filter' => array(
|
|
|
|
'one',
|
|
|
|
'model' => 'value'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $route->match($url);
|
2012-07-03 03:34:37 +00:00
|
|
|
$expected = '/posts/index/filter%5B0%5D:one/filter%5Bmodel%5D:value';
|
2011-12-02 01:32:28 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$url = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'index',
|
|
|
|
'filter' => array(
|
|
|
|
'one',
|
|
|
|
'model' => array(
|
|
|
|
'two',
|
|
|
|
'order' => 'field'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = $route->match($url);
|
2012-07-03 03:34:37 +00:00
|
|
|
$expected = '/posts/index/filter%5B0%5D:one/filter%5Bmodel%5D%5B0%5D:two/filter%5Bmodel%5D%5Border%5D:field';
|
2011-12-02 01:32:28 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2011-03-03 12:30:22 +00:00
|
|
|
/**
|
|
|
|
* test restructuring args with pass key
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPassArgRestructure() {
|
2011-03-03 12:30:22 +00:00
|
|
|
$route = new CakeRoute('/:controller/:action/:slug', array(), array(
|
|
|
|
'pass' => array('slug')
|
|
|
|
));
|
|
|
|
$result = $route->parse('/posts/view/my-title');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'view',
|
|
|
|
'slug' => 'my-title',
|
|
|
|
'pass' => array('my-title'),
|
|
|
|
'named' => array()
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result, 'Slug should have moved');
|
|
|
|
}
|
2011-10-24 01:23:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the /** special type on parsing.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testParseTrailing() {
|
|
|
|
$route = new CakeRoute('/:controller/:action/**');
|
|
|
|
$result = $route->parse('/posts/index/1/2/3/foo:bar');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'index',
|
|
|
|
'pass' => array('1/2/3/foo:bar'),
|
|
|
|
'named' => array()
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = $route->parse('/posts/index/http://example.com');
|
|
|
|
$expected = array(
|
|
|
|
'controller' => 'posts',
|
|
|
|
'action' => 'index',
|
|
|
|
'pass' => array('http://example.com'),
|
|
|
|
'named' => array()
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2012-03-07 05:20:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the /** special type on parsing - UTF8.
|
|
|
|
*
|
|
|
|
* @return void
|
2012-03-13 01:36:27 +00:00
|
|
|
*/
|
2012-03-07 05:20:11 +00:00
|
|
|
public function testParseTrailingUTF8() {
|
2012-05-19 14:48:15 +00:00
|
|
|
$route = new CakeRoute('/category/**', array('controller' => 'categories','action' => 'index'));
|
2012-03-07 18:21:09 +00:00
|
|
|
$result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84');
|
2012-03-07 05:20:11 +00:00
|
|
|
$expected = array(
|
2012-03-07 18:21:09 +00:00
|
|
|
'controller' => 'categories',
|
2012-03-07 05:20:11 +00:00
|
|
|
'action' => 'index',
|
|
|
|
'pass' => array('موبایل'),
|
|
|
|
'named' => array()
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-03-13 01:36:27 +00:00
|
|
|
}
|
2012-03-07 05:20:11 +00:00
|
|
|
|
2011-05-16 22:49:00 +00:00
|
|
|
}
|