Fix Controller::beforeRedirect() and array return.

Array return values from Controller::beforeRedirect() would be
incorrectly handled causing wrong URL's to be generated.

Fixes #2973
This commit is contained in:
mark_story 2012-06-24 11:59:18 -04:00
parent 35e0984bec
commit 3e28326d9c
2 changed files with 30 additions and 1 deletions

View file

@ -797,7 +797,7 @@ class Controller extends Object implements CakeEventListener {
* @return array Array with keys url, status and exit
*/
protected function _parseBeforeRedirect($response, $url, $status, $exit) {
if (is_array($response)) {
if (is_array($response) && isset($response[0])) {
foreach ($response as $resp) {
if (is_array($resp) && isset($resp['url'])) {
extract($resp, EXTR_OVERWRITE);
@ -805,6 +805,8 @@ class Controller extends Object implements CakeEventListener {
$url = $resp;
}
}
} elseif (is_array($response)) {
extract($response, EXTR_OVERWRITE);
}
return compact('url', 'status', 'exit');
}

View file

@ -874,6 +874,33 @@ class ControllerTest extends CakeTestCase {
$Controller->redirect('http://cakephp.org');
}
/**
* Test that beforeRedirect works with returning an array from the controller method.
*
* @return void
*/
public function testRedirectBeforeRedirectInControllerWithArray() {
$Controller = $this->getMock('Controller', array('_stop', 'beforeRedirect'));
$Controller->response = $this->getMock('CakeResponse', array('header'));
$Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
$Controller->expects($this->once())
->method('beforeRedirect')
->with('http://cakephp.org', null, true)
->will($this->returnValue(array(
'url' => 'http://example.org',
'status' => 302,
'exit' => true
)));
$Controller->response->expects($this->at(0))
->method('header')
->with('Location', 'http://example.org');
$Controller->expects($this->once())->method('_stop');
$Controller->redirect('http://cakephp.org');
}
/**
* testMergeVars method
*