From 6d6aa3cb7592330fcdb3beb50ba2f34b95108acf Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 22 Oct 2011 22:56:50 -0400 Subject: [PATCH] Add _stop() to RedirectRoute. Moved from CakeResponse to RedirectRoute, as RedirectRoute is the only place its currently needed. Refs #2143 --- lib/Cake/Routing/Route/RedirectRoute.php | 23 ++++++++++++++++++- .../Case/Routing/Route/RedirectRouteTest.php | 8 +++++++ lib/Cake/Test/Case/Routing/RouterTest.php | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Routing/Route/RedirectRoute.php b/lib/Cake/Routing/Route/RedirectRoute.php index 82e1697bcea..aa4ea602089 100644 --- a/lib/Cake/Routing/Route/RedirectRoute.php +++ b/lib/Cake/Routing/Route/RedirectRoute.php @@ -37,6 +37,13 @@ class RedirectRoute extends CakeRoute { */ public $redirect; +/** + * Flag for disabling exit() when this route parses a url. + * + * @var boolean + */ + public $stop = true; + /** * Constructor * @@ -79,6 +86,7 @@ public function parse($url) { $this->response->header(array('Location' => Router::url($redirect, true))); $this->response->statusCode($status); $this->response->send(); + $this->_stop(); } /** @@ -90,4 +98,17 @@ public function parse($url) { public function match($url) { return false; } -} \ No newline at end of file + +/** + * Stop execution of the current script. Wraps exit() making + * testing easier. + * + * @param integer|string $status see http://php.net/exit for values + * @return void + */ + protected function _stop($code = 0) { + if ($this->stop) { + exit($code); + } + } +} diff --git a/lib/Cake/Test/Case/Routing/Route/RedirectRouteTest.php b/lib/Cake/Test/Case/Routing/Route/RedirectRouteTest.php index 94efe04e12f..80d66c6ecf2 100644 --- a/lib/Cake/Test/Case/Routing/Route/RedirectRouteTest.php +++ b/lib/Cake/Test/Case/Routing/Route/RedirectRouteTest.php @@ -45,43 +45,51 @@ public function setUp() { */ public function testParsing() { $route = new RedirectRoute('/home', array('controller' => 'posts')); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/home'); $this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true))); $route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index')); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/home'); $this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true))); $this->assertEqual($route->response->statusCode(), 301); $route = new RedirectRoute('/google', 'http://google.com'); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/google'); $this->assertEqual($route->response->header(), array('Location' => 'http://google.com')); $route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302)); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/posts/2'); $this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view', true))); $this->assertEqual($route->response->statusCode(), 302); $route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true)); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/posts/2'); $this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view/2', true))); $route = new RedirectRoute('/posts/*', '/test', array('persist' => true)); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/posts/2'); $this->assertEqual($route->response->header(), array('Location' => Router::url('/test', true))); $route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true)); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/my_controllers/do_something/passme/named:param'); $this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add/passme/named:param', true))); $route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add')); + $route->stop = false; $route->response = $this->getMock('CakeResponse', array('_sendHeader')); $result = $route->parse('/my_controllers/do_something/passme/named:param'); $this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add', true))); diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index 6a0cbc291ca..ceca433d32b 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -2459,6 +2459,7 @@ public function testRouteRedirection() { Router::redirect('/blog', array('controller' => 'posts'), array('status' => 302)); $this->assertEqual(count(Router::$routes), 1); Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader')); + Router::$routes[0]->stop = false; $this->assertEqual(Router::$routes[0]->options['status'], 302); Router::parse('/blog');