From 6acf024a2b7582ce025494b17c0915e6c4eb00f6 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 22 Aug 2011 21:19:35 -0400 Subject: [PATCH] Fixing incorrect keying for ext routing parameter. It was nested under params[url][ext]. This makes it unlike all other routing parameters. Having the nested value also makes reversing requests harder, and generating urls more difficult. Adding a test for Router::reverse() and extensions. Fixes #1923, fixes #1928 --- .../Component/RequestHandlerComponent.php | 4 +- lib/Cake/Routing/Router.php | 4 +- lib/Cake/Test/Case/Routing/RouterTest.php | 39 ++++++++++++++----- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Controller/Component/RequestHandlerComponent.php b/lib/Cake/Controller/Component/RequestHandlerComponent.php index 866e4c539..1796088bc 100644 --- a/lib/Cake/Controller/Component/RequestHandlerComponent.php +++ b/lib/Cake/Controller/Component/RequestHandlerComponent.php @@ -109,8 +109,8 @@ class RequestHandlerComponent extends Component { public function initialize($controller, $settings = array()) { $this->request = $controller->request; $this->response = $controller->response; - if (isset($this->request->params['url']['ext'])) { - $this->ext = $this->request->params['url']['ext']; + if (isset($this->request->params['ext'])) { + $this->ext = $this->request->params['ext']; } if (empty($this->ext) || $this->ext == 'html') { $accepts = $this->request->accepts(); diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index c7f4f0abe..d43f58a4f 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -476,8 +476,8 @@ class Router { $out['action'] = $out['prefix'] . '_' . $out['action']; } - if (!empty($ext) && !isset($out['url']['ext'])) { - $out['url']['ext'] = $ext; + if (!empty($ext) && !isset($out['ext'])) { + $out['ext'] = $ext; } return $out; } diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index f1088093a..987067688 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -577,7 +577,7 @@ class RouterTest extends CakeTestCase { $request->addParams(array( 'controller' => 'registrations', 'action' => 'admin_index', 'plugin' => null, 'prefix' => 'admin', 'admin' => true, - 'url' => array('ext' => 'html', 'url' => 'admin/registrations/index') + 'ext' => 'html' )); $request->base = ''; $request->here = '/admin/registrations/index'; @@ -1210,18 +1210,18 @@ class RouterTest extends CakeTestCase { require CAKE . 'Config' . DS . 'routes.php'; $result = Router::parse('/posts.rss'); - $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'url' => array('ext' => 'rss'), 'pass'=> array(), 'named' => array()); + $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'rss', 'pass'=> array(), 'named' => array()); $this->assertEqual($expected, $result); $result = Router::parse('/posts/view/1.rss'); - $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'url' => array('ext' => 'rss'), 'named' => array()); + $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'ext' => 'rss', 'named' => array()); $this->assertEqual($expected, $result); $result = Router::parse('/posts/view/1.rss?query=test'); $this->assertEqual($expected, $result); $result = Router::parse('/posts/view/1.atom'); - $expected['url'] = array('ext' => 'atom'); + $expected['ext'] = 'atom'; $this->assertEqual($expected, $result); Router::reload(); @@ -1230,7 +1230,7 @@ class RouterTest extends CakeTestCase { Router::parseExtensions('rss', 'xml'); $result = Router::parse('/posts.xml'); - $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'url' => array('ext' => 'xml'), 'pass'=> array(), 'named' => array()); + $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'xml', 'pass'=> array(), 'named' => array()); $this->assertEqual($expected, $result); $result = Router::parse('/posts.atom?hello=goodbye'); @@ -1238,16 +1238,16 @@ class RouterTest extends CakeTestCase { $this->assertEqual($expected, $result); Router::reload(); - Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'url' => array('ext' => 'rss'))); + Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss')); $result = Router::parse('/controller/action'); - $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'url' => array('ext' => 'rss'), 'named' => array(), 'pass' => array()); + $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array()); $this->assertEqual($expected, $result); Router::reload(); Router::parseExtensions('rss'); - Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'url' => array('ext' => 'rss'))); + Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss')); $result = Router::parse('/controller/action'); - $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'url' => array('ext' => 'rss'), 'named' => array(), 'pass' => array()); + $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array()); $this->assertEqual($expected, $result); } @@ -2273,6 +2273,27 @@ class RouterTest extends CakeTestCase { $this->assertPattern('/^http(s)?:\/\//', $result); } +/** + * Test that extensions work with Router::reverse() + * + * @return void + */ + public function testReverseWithExtension() { + Router::parseExtensions('json'); + + $request = new CakeRequest('/posts/view/1.json'); + $request->addParams(array( + 'controller' => 'posts', + 'action' => 'view', + 'pass' => array(1), + 'named' => array(), + 'ext' => 'json', + )); + $result = Router::reverse($request); + $expected = '/posts/view/1.json'; + $this->assertEquals($expected, $result); + } + /** * test that setRequestInfo can accept arrays and turn that into a CakeRequest object. *