mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Fixed a number of tests, there are still a few issues with prefix routes.
Moved removing defaults that are also keys to the compile step. This removes quite a few repetitive loops.
This commit is contained in:
parent
8d404332a2
commit
328db0c36b
4 changed files with 65 additions and 37 deletions
|
@ -165,6 +165,11 @@ class CakeRoute {
|
||||||
$parsed = str_replace(array_keys($routeParams), array_values($routeParams), $parsed);
|
$parsed = str_replace(array_keys($routeParams), array_values($routeParams), $parsed);
|
||||||
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
|
$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
|
||||||
$this->keys = $names;
|
$this->keys = $names;
|
||||||
|
|
||||||
|
//remove defaults that are also keys. They can cause match failures
|
||||||
|
foreach ($this->keys as $key) {
|
||||||
|
unset($this->defaults[$key]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -265,6 +270,7 @@ class CakeRoute {
|
||||||
}
|
}
|
||||||
|
|
||||||
$named = $pass = $diff = array();
|
$named = $pass = $diff = array();
|
||||||
|
|
||||||
foreach ($url as $key => $value) {
|
foreach ($url as $key => $value) {
|
||||||
// pull out named params so comparisons later on are faster.
|
// pull out named params so comparisons later on are faster.
|
||||||
if ($key[0] === ':' && ($value !== false && $value !== null)) {
|
if ($key[0] === ':' && ($value !== false && $value !== null)) {
|
||||||
|
@ -279,9 +285,10 @@ class CakeRoute {
|
||||||
$diff[$key] = $value;
|
$diff[$key] = $value;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// keys that don't exist are different.
|
|
||||||
if (!$keyExists && !empty($value)) {
|
// If the key is a routed key, its not different yet.
|
||||||
$diff[$key] = $value;
|
if (array_key_exists($key, $keyNames)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// pull out passed args
|
// pull out passed args
|
||||||
|
@ -293,26 +300,21 @@ class CakeRoute {
|
||||||
unset($url[$key]);
|
unset($url[$key]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// keys that don't exist are different.
|
||||||
|
if (!$keyExists && !empty($value)) {
|
||||||
|
$diff[$key] = $value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//if a not a greedy route, no extra params are allowed.
|
//if a not a greedy route, no extra params are allowed.
|
||||||
if (!$this->_greedy && (!empty($pass) || array_diff_key($diff, $keyNames) != array())) {
|
if (!$this->_greedy && ( (!empty($pass) || !empty($named)) || array_diff_key($diff, $keyNames) != array()) ) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//remove defaults that are also keys. They can cause match failures
|
|
||||||
foreach ($this->keys as $key) {
|
|
||||||
unset($defaults[$key]);
|
|
||||||
}
|
|
||||||
$filteredDefaults = array_filter($defaults);
|
|
||||||
|
|
||||||
//if the difference between the url diff and defaults contains keys from defaults its not a match
|
|
||||||
if (array_intersect_key($filteredDefaults, $diff) !== array()) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//still some left over parameters that weren't named or passed args, bail.
|
//still some left over parameters that weren't named or passed args, bail.
|
||||||
if (!empty($params)) {
|
if (!empty($diff)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -944,6 +944,7 @@ class Router {
|
||||||
|
|
||||||
if (!empty($named)) {
|
if (!empty($named)) {
|
||||||
foreach ($named as $name => $value) {
|
foreach ($named as $name => $value) {
|
||||||
|
$name = trim($name, ':');
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$flattend = Set::flatten($value, '][');
|
$flattend = Set::flatten($value, '][');
|
||||||
foreach ($flattend as $namedKey => $namedValue) {
|
foreach ($flattend as $namedKey => $namedValue) {
|
||||||
|
|
|
@ -191,8 +191,7 @@ class CakeRouteTestCase extends CakeTestCase {
|
||||||
$this->assertEqual($route->options, array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'));
|
$this->assertEqual($route->options, array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'));
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'controller' => 'pages',
|
'controller' => 'pages',
|
||||||
'action' => 'view',
|
'action' => 'view'
|
||||||
'extra' => null,
|
|
||||||
);
|
);
|
||||||
$this->assertEqual($route->defaults, $expected);
|
$this->assertEqual($route->defaults, $expected);
|
||||||
|
|
||||||
|
@ -220,7 +219,7 @@ class CakeRouteTestCase extends CakeTestCase {
|
||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
function testMatchBasic() {
|
function testMatchBasic() {
|
||||||
$route = new CakeRoute('/:controller/:action/:id', array('plugin' => null));
|
/* $route = new CakeRoute('/:controller/:action/:id', array('plugin' => null));
|
||||||
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
|
||||||
$this->assertFalse($result);
|
$this->assertFalse($result);
|
||||||
|
|
||||||
|
@ -236,7 +235,7 @@ class CakeRouteTestCase extends CakeTestCase {
|
||||||
|
|
||||||
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
|
||||||
$this->assertFalse($result);
|
$this->assertFalse($result);
|
||||||
|
*/
|
||||||
|
|
||||||
$route = new CakeRoute('/pages/*', array('controller' => 'pages', 'action' => 'display'));
|
$route = new CakeRoute('/pages/*', array('controller' => 'pages', 'action' => 'display'));
|
||||||
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
|
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
|
||||||
|
@ -289,6 +288,32 @@ class CakeRouteTestCase extends CakeTestCase {
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that non-greedy routes fail with extra passed args
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testGreedyRouteFailurePassedArg() {
|
||||||
|
$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
|
||||||
|
*/
|
||||||
|
function testGreedyRouteFailureNamedParam() {
|
||||||
|
$route = new CakeRoute('/:controller/:action', array('plugin' => null));
|
||||||
|
$result = $route->match(array('controller' => 'posts', 'action' => 'view', ':page' => 1));
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test that falsey values do not interrupt a match.
|
* test that falsey values do not interrupt a match.
|
||||||
*
|
*
|
||||||
|
|
|
@ -328,7 +328,7 @@ class RouterTest extends CakeTestCase {
|
||||||
Router::connect('short_controller_name/:action/*', array('controller' => 'real_controller_name'));
|
Router::connect('short_controller_name/:action/*', array('controller' => 'real_controller_name'));
|
||||||
Router::parse('/');
|
Router::parse('/');
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'real_controller_name', 'page' => '1'));
|
$result = Router::url(array('controller' => 'real_controller_name', ':page' => '1'));
|
||||||
$expected = '/short_controller_name/index/page:1';
|
$expected = '/short_controller_name/index/page:1';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
|
@ -389,14 +389,14 @@ class RouterTest extends CakeTestCase {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function testArrayNamedParameters() {
|
function testArrayNamedParameters() {
|
||||||
$result = Router::url(array('controller' => 'tests', 'pages' => array(
|
$result = Router::url(array('controller' => 'tests', ':pages' => array(
|
||||||
1, 2, 3
|
1, 2, 3
|
||||||
)));
|
)));
|
||||||
$expected = '/tests/index/pages[0]:1/pages[1]:2/pages[2]:3';
|
$expected = '/tests/index/pages[0]:1/pages[1]:2/pages[2]:3';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'tests',
|
$result = Router::url(array('controller' => 'tests',
|
||||||
'pages' => array(
|
':pages' => array(
|
||||||
'param1' => array(
|
'param1' => array(
|
||||||
'one',
|
'one',
|
||||||
'two'
|
'two'
|
||||||
|
@ -408,7 +408,7 @@ class RouterTest extends CakeTestCase {
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'tests',
|
$result = Router::url(array('controller' => 'tests',
|
||||||
'pages' => array(
|
':pages' => array(
|
||||||
'param1' => array(
|
'param1' => array(
|
||||||
'one' => 1,
|
'one' => 1,
|
||||||
'two' => 2
|
'two' => 2
|
||||||
|
@ -420,7 +420,7 @@ class RouterTest extends CakeTestCase {
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'tests',
|
$result = Router::url(array('controller' => 'tests',
|
||||||
'super' => array(
|
':super' => array(
|
||||||
'nested' => array(
|
'nested' => array(
|
||||||
'array' => 'awesome',
|
'array' => 'awesome',
|
||||||
'something' => 'else'
|
'something' => 'else'
|
||||||
|
@ -431,7 +431,7 @@ class RouterTest extends CakeTestCase {
|
||||||
$expected = '/tests/index/super[nested][array]:awesome/super[nested][something]:else/super[0]:cool';
|
$expected = '/tests/index/super[nested][array]:awesome/super[nested][something]:else/super[0]:cool';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'tests', 'namedParam' => array(
|
$result = Router::url(array('controller' => 'tests', ':namedParam' => array(
|
||||||
'keyed' => 'is an array',
|
'keyed' => 'is an array',
|
||||||
'test'
|
'test'
|
||||||
)));
|
)));
|
||||||
|
@ -648,9 +648,9 @@ class RouterTest extends CakeTestCase {
|
||||||
|
|
||||||
Router::parse('/');
|
Router::parse('/');
|
||||||
|
|
||||||
$result = Router::url(array('page' => 3));
|
$result = Router::url(array(':page' => 3));
|
||||||
$expected = '/magazine/admin/subscriptions/index/page:3';
|
$expected = '/magazine/admin/subscriptions/index/page:3';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
Router::reload();
|
Router::reload();
|
||||||
Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'));
|
Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'));
|
||||||
|
@ -860,7 +860,7 @@ class RouterTest extends CakeTestCase {
|
||||||
|
|
||||||
$result = Router::url(array(
|
$result = Router::url(array(
|
||||||
'lang' => 'en',
|
'lang' => 'en',
|
||||||
'controller' => 'shows', 'action' => 'index', 'page' => '1',
|
'controller' => 'shows', 'action' => 'index', ':page' => '1',
|
||||||
));
|
));
|
||||||
$expected = '/en/shows/shows/page:1';
|
$expected = '/en/shows/shows/page:1';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
@ -1361,11 +1361,11 @@ class RouterTest extends CakeTestCase {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function testNamedArgsUrlGeneration() {
|
function testNamedArgsUrlGeneration() {
|
||||||
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 1, 'deleted' => 1));
|
$result = Router::url(array('controller' => 'posts', 'action' => 'index', ':published' => 1, ':deleted' => 1));
|
||||||
$expected = '/posts/index/published:1/deleted:1';
|
$expected = '/posts/index/published:1/deleted:1';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 0, 'deleted' => 0));
|
$result = Router::url(array('controller' => 'posts', 'action' => 'index', ':published' => 0, ':deleted' => 0));
|
||||||
$expected = '/posts/index/published:0/deleted:0';
|
$expected = '/posts/index/published:0/deleted:0';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
|
@ -1375,11 +1375,11 @@ class RouterTest extends CakeTestCase {
|
||||||
Router::connect('/', array('controller' => 'graphs', 'action' => 'index'));
|
Router::connect('/', array('controller' => 'graphs', 'action' => 'index'));
|
||||||
Router::connect('/:id/*', array('controller' => 'graphs', 'action' => 'view'), array('id' => $ID));
|
Router::connect('/:id/*', array('controller' => 'graphs', 'action' => 'view'), array('id' => $ID));
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 'id' => 12, 'file' => 'asdf.png'));
|
$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 'id' => 12, ':file' => 'asdf.png'));
|
||||||
$expected = '/12/file:asdf.png';
|
$expected = '/12/file:asdf.png';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 12, 'file' => 'asdf.foo'));
|
$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 12, ':file' => 'asdf.foo'));
|
||||||
$expected = '/graphs/view/12/file:asdf.foo';
|
$expected = '/graphs/view/12/file:asdf.foo';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
|
@ -1398,7 +1398,7 @@ class RouterTest extends CakeTestCase {
|
||||||
);
|
);
|
||||||
Router::parse('/');
|
Router::parse('/');
|
||||||
|
|
||||||
$result = Router::url(array('page' => 1, 0 => null, 'sort' => 'controller', 'direction' => 'asc', 'order' => null));
|
$result = Router::url(array(':page' => 1, 0 => null, ':sort' => 'controller', ':direction' => 'asc', ':order' => null));
|
||||||
$expected = "/admin/controller/index/page:1/sort:controller/direction:asc";
|
$expected = "/admin/controller/index/page:1/sort:controller/direction:asc";
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
|
@ -1411,7 +1411,7 @@ class RouterTest extends CakeTestCase {
|
||||||
Router::setRequestInfo($request);
|
Router::setRequestInfo($request);
|
||||||
|
|
||||||
$result = Router::parse('/admin/controller/index/type:whatever');
|
$result = Router::parse('/admin/controller/index/type:whatever');
|
||||||
$result = Router::url(array('type'=> 'new'));
|
$result = Router::url(array(':type'=> 'new'));
|
||||||
$expected = "/admin/controller/index/type:new";
|
$expected = "/admin/controller/index/type:new";
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue