From 97175aac901d11beb8460b6eb32f1f7c302d1557 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 3 Mar 2011 06:49:11 -0500 Subject: [PATCH] Making greedy -> greedyNamed its clearer and doesn't consume a possibly easy to use named parameter key. Expanding tests for named parameter rules/conditions. --- cake/libs/route/cake_route.php | 4 +- cake/libs/router.php | 2 +- .../cases/libs/route/cake_route.test.php | 50 +++++++++++++++++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/cake/libs/route/cake_route.php b/cake/libs/route/cake_route.php index 81ccaf425..369c127dd 100644 --- a/cake/libs/route/cake_route.php +++ b/cake/libs/route/cake_route.php @@ -252,10 +252,10 @@ class CakeRoute { $context = compact('controller', 'action'); $namedConfig = Router::namedConfig(); - $greedy = isset($this->options['greedy']) ? $this->options['greedy'] : $namedConfig['greedy']; + $greedy = isset($this->options['greedyNamed']) ? $this->options['greedyNamed'] : $namedConfig['greedy']; $rules = $namedConfig['rules']; if (isset($this->options['named'])) { - $greedy = isset($this->options['greedy']) && $this->options['greedy'] === true; + $greedy = isset($this->options['greedyNamed']) && $this->options['greedyNamed'] === true; foreach ((array)$this->options['named'] as $key => $val) { if (is_numeric($key)) { $rules[$val] = true; diff --git a/cake/libs/router.php b/cake/libs/router.php index 7ed994ce0..0ac9338c2 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -395,7 +395,7 @@ class Router { self::$_namedConfig['rules'][$key] = $val; } } - self::$_namedConfig['greedy'] = $options['greedy']; + self::$_namedConfig['greedyNamed'] = $options['greedy']; return self::$_namedConfig; } diff --git a/cake/tests/cases/libs/route/cake_route.test.php b/cake/tests/cases/libs/route/cake_route.test.php index a29ebe665..4a337f02f 100644 --- a/cake/tests/cases/libs/route/cake_route.test.php +++ b/cake/tests/cases/libs/route/cake_route.test.php @@ -514,14 +514,15 @@ class CakeRouteTestCase extends CakeTestCase { 'named' => array( 'wibble', 'fish' => array('action' => 'index'), - 'fizz' => array('controller' => 'comments') + 'fizz' => array('controller' => 'comments'), + 'pattern' => 'val-[\d]+' ) )); - $result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz'); + $result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz/unknown:value'); $expected = array( 'controller' => 'posts', 'action' => 'display', - 'pass' => array('fish:trout', 'fizz:buzz'), + 'pass' => array('fish:trout', 'fizz:buzz', 'unknown:value'), 'named' => array( 'wibble' => 'spin' ) @@ -552,6 +553,49 @@ class CakeRouteTestCase extends CakeTestCase { ) ); $this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.'); + + $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 + */ + function testParseGreedyNamed() { + $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'); } /**