From 32f8752fc608e5a027eba00c1a5f0e7a7c4b18e1 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 25 Oct 2014 21:55:11 -0400 Subject: [PATCH] Fix / being handled incorrect by referer() When the referer is `/` Controller::referer() should not be appending the base path as it results in the base path being added twice causing incorrect results in AuthComponent. Refs #4812 --- src/Controller/Controller.php | 2 +- tests/TestCase/Controller/ControllerTest.php | 24 +++++++++++++++++++ .../TestApp/Controller/PagesController.php | 7 ------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index 8c504a9b941..2f48bbcad4d 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -604,7 +604,7 @@ public function referer($default = null, $local = false) { } $referer = $this->request->referer($local); - if ($referer === '/' && $default) { + if ($referer === '/' && $default && $default !== $referer) { return Router::url($default, !$local); } return $referer; diff --git a/tests/TestCase/Controller/ControllerTest.php b/tests/TestCase/Controller/ControllerTest.php index a1c12903860..ef4ec95fb7a 100644 --- a/tests/TestCase/Controller/ControllerTest.php +++ b/tests/TestCase/Controller/ControllerTest.php @@ -574,6 +574,30 @@ public function testReferer() { $this->assertEquals('/', $result); } +/** + * Test that the referer is not absolute if it is '/'. + * + * This avoids the base path being applied twice on string urls. + * + * @return void + */ + public function testRefererSlash() { + $request = $this->getMock('Cake\Network\Request', ['referer']); + $request->base = '/base'; + Router::pushRequest($request); + + $request->expects($this->any())->method('referer') + ->will($this->returnValue('/')); + + $controller = new Controller($request); + $result = $controller->referer('/', true); + $this->assertEquals('/', $result); + + $controller = new Controller($request); + $result = $controller->referer('/some/path', true); + $this->assertEquals('/base/some/path', $result); + } + /** * testSetAction method * diff --git a/tests/test_app/TestApp/Controller/PagesController.php b/tests/test_app/TestApp/Controller/PagesController.php index be8371eb269..a514f214619 100644 --- a/tests/test_app/TestApp/Controller/PagesController.php +++ b/tests/test_app/TestApp/Controller/PagesController.php @@ -39,13 +39,6 @@ class PagesController extends AppController { */ public $helpers = array('Html', 'Session'); -/** - * This controller does not use a model - * - * @var array - */ - public $uses = array(); - /** * Displays a view *