diff --git a/src/TestSuite/IntegrationTestCase.php b/src/TestSuite/IntegrationTestCase.php index 610e1de6656..f231b0a93a2 100644 --- a/src/TestSuite/IntegrationTestCase.php +++ b/src/TestSuite/IntegrationTestCase.php @@ -452,7 +452,7 @@ protected function _sendRequest($url, $method, $data = []) $request = $this->_buildRequest($url, $method, $data); $response = $dispatcher->execute($request); $this->_requestSession = $request['session']; - if ($this->_retainFlashMessages) { + if ($this->_retainFlashMessages && $this->_flashMessages) { $this->_requestSession->write('Flash', $this->_flashMessages); } $this->_response = $response; diff --git a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php index 40ab4dac615..5e5e51eb56e 100644 --- a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php +++ b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php @@ -419,6 +419,21 @@ public function testFlashAssertionsAfterRender() $this->assertSession('An error message', 'Flash.flash.0.message'); } + /** + * Test flash assertions stored with enableRememberFlashMessages() even if + * no view is rendered + * + * @return void + */ + public function testFlashAssertionsWithNoRender() + { + $this->enableRetainFlashMessages(); + $this->get('/posts/flashNoRender'); + $this->assertRedirect(); + + $this->assertSession('An error message', 'Flash.flash.0.message'); + } + /** * Tests the failure message for assertCookieNotSet * diff --git a/tests/test_app/TestApp/Controller/PostsController.php b/tests/test_app/TestApp/Controller/PostsController.php index cb90edbf2fb..bff8c71c6e0 100644 --- a/tests/test_app/TestApp/Controller/PostsController.php +++ b/tests/test_app/TestApp/Controller/PostsController.php @@ -62,6 +62,17 @@ public function index($layout = 'default') $this->viewBuilder()->setLayout($layout); } + /** + * Sets a flash message and redirects (no rendering) + * + * @return \Cake\Network\Response + */ + public function flashNoRender() + { + $this->Flash->error('An error message'); + return $this->redirect(['action' => 'index']); + } + /** * Stub get method *