Fixing issues with Controller::referer() and CakeRequest::referer(). Tests updated.

This commit is contained in:
mark_story 2010-05-15 00:53:15 -04:00
parent 4520ff7c82
commit 36a71582fc
4 changed files with 24 additions and 12 deletions

View file

@ -387,16 +387,18 @@ class CakeRequest implements ArrayAccess {
$base = ''; $base = '';
if (defined('FULL_BASE_URL')) { 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) { if ($local && strpos($ref, $base) === 0) {
$ref = substr($ref, strlen($base)); $ref = substr($ref, strlen($base));
if ($ref[0] != '/') { if ($ref[0] != '/') {
$ref = '/' . $ref; $ref = '/' . $ref;
} }
return $ref;
} elseif (!$local) {
return $ref;
} }
return $ref;
} }
return '/'; return '/';
} }

View file

@ -920,7 +920,11 @@ class Controller extends Object {
*/ */
public function referer($default = null, $local = false) { public function referer($default = null, $local = false) {
if ($this->request) { 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 '/'; return '/';
} }

View file

@ -442,6 +442,7 @@ class CakeRequestTestCase extends CakeTestCase {
*/ */
function testReferer() { function testReferer() {
$request = new CakeRequest('some/path'); $request = new CakeRequest('some/path');
$request->webroot = '/';
$_SERVER['HTTP_REFERER'] = 'http://cakephp.org'; $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
$result = $request->referer(); $result = $request->referer();
@ -451,19 +452,19 @@ class CakeRequestTestCase extends CakeTestCase {
$result = $request->referer(); $result = $request->referer();
$this->assertIdentical($result, '/'); $this->assertIdentical($result, '/');
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'some/path'; $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
$result = $request->referer(true); $result = $request->referer(true);
$this->assertIdentical($result, '/some/path'); $this->assertIdentical($result, '/some/path');
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'some/path'; $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
$result = $request->referer(); $result = $request->referer(false);
$this->assertIdentical($result, FULL_BASE_URL . 'some/path'); $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); $result = $request->referer(true);
$this->assertIdentical($result, '/some/path'); $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); $result = $request->referer(true);
$this->assertIdentical($result, '/recipes/add'); $this->assertIdentical($result, '/recipes/add');

View file

@ -1175,8 +1175,8 @@ class ControllerTest extends CakeTestCase {
*/ */
function testReferer() { function testReferer() {
$request = new ControllerMockCakeRequest(); $request = new ControllerMockCakeRequest();
$request->setReturnValue('referer', 'http://localhost/posts/index', array(null, false)); $request->setReturnValue('referer', 'http://localhost/posts/index', array(false));
$request->setReturnValue('referer', '/posts/index', array(null, true)); $request->setReturnValue('referer', '/posts/index', array(true));
$Controller = new Controller($request); $Controller = new Controller($request);
$result = $Controller->referer(null, true); $result = $Controller->referer(null, true);
@ -1185,6 +1185,11 @@ class ControllerTest extends CakeTestCase {
$result = $Controller->referer(); $result = $Controller->referer();
$this->assertEqual($result, 'http://localhost/posts/index'); $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); $Controller = new Controller(null);
$result = $Controller->referer(); $result = $Controller->referer();
$this->assertEqual($result, '/'); $this->assertEqual($result, '/');