From 351c253564e43e3d9573bd202d4f212f3a8bcc16 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 31 Dec 2016 00:32:30 -0500 Subject: [PATCH] Fix controller->redirect response handling Store references to the updated response. Previously controller methods that forgot to `return $this->redirect()` would break in 3.4 as `$this->response` was not correct. --- src/Controller/Controller.php | 6 +++--- tests/TestCase/Controller/ControllerTest.php | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index 0a55c9a20c5..03667aa03fb 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -545,17 +545,17 @@ public function redirect($url, $status = 302) $event = $this->dispatchEvent('Controller.beforeRedirect', [$url, $response]); if ($event->result() instanceof Response) { - return $event->result(); + return $this->response = $event->result(); } if ($event->isStopped()) { return null; } if (!$response->location()) { - $response->location(Router::url($url, true)); + $response = $response->withLocation(Router::url($url, true)); } - return $response; + return $this->response = $response; } /** diff --git a/tests/TestCase/Controller/ControllerTest.php b/tests/TestCase/Controller/ControllerTest.php index 52000c01359..1efb889f0ea 100644 --- a/tests/TestCase/Controller/ControllerTest.php +++ b/tests/TestCase/Controller/ControllerTest.php @@ -475,6 +475,7 @@ public function testRedirectByCode($code, $msg) $Controller = new Controller(null, new Response()); $response = $Controller->redirect('http://cakephp.org', (int)$code); + $this->assertSame($response, $Controller->response); $this->assertEquals($code, $response->statusCode()); $this->assertEquals('http://cakephp.org', $response->header()['Location']); $this->assertFalse($Controller->autoRender); @@ -534,6 +535,7 @@ public function testRedirectBeforeRedirectListenerReturnResponse() $result = $Controller->redirect('http://cakephp.org'); $this->assertSame($newResponse, $result); + $this->assertSame($newResponse, $Controller->response); } /**