From 36a71582fc6128892be7ad8f917e5f30f01d931c Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 15 May 2010 00:53:15 -0400 Subject: [PATCH] Fixing issues with Controller::referer() and CakeRequest::referer(). Tests updated. --- cake/libs/cake_request.php | 8 +++++--- cake/libs/controller/controller.php | 6 +++++- cake/tests/cases/libs/cake_request.test.php | 13 +++++++------ .../tests/cases/libs/controller/controller.test.php | 9 +++++++-- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index 438c4af4e..5c3315db1 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -387,16 +387,18 @@ class CakeRequest implements ArrayAccess { $base = ''; if (defined('FULL_BASE_URL')) { - $base = FULL_BASE_URL; + $base = FULL_BASE_URL . $this->webroot; } - if (!empty($ref)) { + if (!empty($ref) && !empty($base)) { if ($local && strpos($ref, $base) === 0) { $ref = substr($ref, strlen($base)); if ($ref[0] != '/') { $ref = '/' . $ref; } + return $ref; + } elseif (!$local) { + return $ref; } - return $ref; } return '/'; } diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 2a755e677..3831bb3a3 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -920,7 +920,11 @@ class Controller extends Object { */ public function referer($default = null, $local = false) { if ($this->request) { - return $this->request->referer($default, $local); + $referer = $this->request->referer($local); + if ($referer == '/' && $default != null) { + return Router::url($default, true); + } + return $referer; } return '/'; } diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index 8b6531dc4..47eae44a6 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -442,6 +442,7 @@ class CakeRequestTestCase extends CakeTestCase { */ function testReferer() { $request = new CakeRequest('some/path'); + $request->webroot = '/'; $_SERVER['HTTP_REFERER'] = 'http://cakephp.org'; $result = $request->referer(); @@ -451,19 +452,19 @@ class CakeRequestTestCase extends CakeTestCase { $result = $request->referer(); $this->assertIdentical($result, '/'); - $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'some/path'; + $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path'; $result = $request->referer(true); $this->assertIdentical($result, '/some/path'); - $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'some/path'; - $result = $request->referer(); - $this->assertIdentical($result, FULL_BASE_URL . 'some/path'); + $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path'; + $result = $request->referer(false); + $this->assertIdentical($result, FULL_BASE_URL . '/some/path'); - $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'some/path'; + $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path'; $result = $request->referer(true); $this->assertIdentical($result, '/some/path'); - $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'recipes/add'; + $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/recipes/add'; $result = $request->referer(true); $this->assertIdentical($result, '/recipes/add'); diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index 3690970d1..8270bd07f 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -1175,8 +1175,8 @@ class ControllerTest extends CakeTestCase { */ function testReferer() { $request = new ControllerMockCakeRequest(); - $request->setReturnValue('referer', 'http://localhost/posts/index', array(null, false)); - $request->setReturnValue('referer', '/posts/index', array(null, true)); + $request->setReturnValue('referer', 'http://localhost/posts/index', array(false)); + $request->setReturnValue('referer', '/posts/index', array(true)); $Controller = new Controller($request); $result = $Controller->referer(null, true); @@ -1185,6 +1185,11 @@ class ControllerTest extends CakeTestCase { $result = $Controller->referer(); $this->assertEqual($result, 'http://localhost/posts/index'); + $Controller = new Controller($request); + $request->setReturnValue('referer', '/', array(true)); + $result = $Controller->referer(array('controller' => 'posts', 'action' => 'index'), true); + $this->assertEqual($result, '/posts/index'); + $Controller = new Controller(null); $result = $Controller->referer(); $this->assertEqual($result, '/');