Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 3e28326d9c2715eeff8e2f318421230cbbc89a0c 1 parent 35e0984
@markstory markstory authored
View
4 lib/Cake/Controller/Controller.php
@@ -797,7 +797,7 @@ public function redirect($url, $status = null, $exit = true) {
* @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 @@ protected function _parseBeforeRedirect($response, $url, $status, $exit) {
$url = $resp;
}
}
+ } elseif (is_array($response)) {
+ extract($response, EXTR_OVERWRITE);
}
return compact('url', 'status', 'exit');
}
View
27 lib/Cake/Test/Case/Controller/ControllerTest.php
@@ -875,6 +875,33 @@ public function testRedirectBeforeRedirectInController() {
}
/**
+ * 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
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.