From 217938a97084655540ea869cbcb4da7c92aedbb9 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 3 Mar 2011 07:30:22 -0500 Subject: [PATCH] Moving pass key restructuring into CakeRoute. Adding tests for that and http header matching. --- cake/libs/route/cake_route.php | 10 ++++ .../cases/libs/route/cake_route.test.php | 49 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/cake/libs/route/cake_route.php b/cake/libs/route/cake_route.php index 015abb42b..da29b3e26 100644 --- a/cake/libs/route/cake_route.php +++ b/cake/libs/route/cake_route.php @@ -233,6 +233,16 @@ class CakeRoute { $route['named'] = $named; unset($route['_args_']); } + + // restructure 'pass' key route params + if (isset($this->options['pass'])) { + $j = count($this->options['pass']); + while($j--) { + if (isset($route[$this->options['pass'][$j]])) { + array_unshift($route['pass'], $route[$this->options['pass'][$j]]); + } + } + } return $route; } diff --git a/cake/tests/cases/libs/route/cake_route.test.php b/cake/tests/cases/libs/route/cake_route.test.php index b68e05338..d70eed4e6 100644 --- a/cake/tests/cases/libs/route/cake_route.test.php +++ b/cake/tests/cases/libs/route/cake_route.test.php @@ -450,6 +450,35 @@ class CakeRouteTestCase extends CakeTestCase { $this->assertEqual($result['action'], 'index'); } +/** + * test numerically indexed defaults, get appeneded to pass + * + * @return void + */ + function testParseWithPassDefaults() { + $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 + */ + function testParseWithHttpHeaderConditions() { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $route = new CakeRoute('/sample', array('controller' => 'posts', 'action' => 'index', '[method]' => 'POST')); + + $this->assertFalse($route->parse('/sample')); + } + /** * test that patterns work for :action * @@ -653,4 +682,24 @@ class CakeRouteTestCase extends CakeTestCase { ); $this->assertEqual($result, $expected); } + +/** + * test restructuring args with pass key + * + * @return void + */ + function testPassArgRestructure() { + $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'); + } } \ No newline at end of file