diff --git a/src/Controller/Component/FlashComponent.php b/src/Controller/Component/FlashComponent.php index 859c68864a6..1b74a8171b9 100644 --- a/src/Controller/Component/FlashComponent.php +++ b/src/Controller/Component/FlashComponent.php @@ -40,7 +40,7 @@ class FlashComponent extends Component { */ protected $_defaultConfig = [ 'key' => 'flash', - 'element' => null, + 'element' => 'default', 'params' => [] ]; @@ -63,7 +63,7 @@ public function __construct(ComponentRegistry $collection, array $config = []) { * ### Options: * * - `key` The key to set under the session's Flash key - * - `element` The element used to render the flash message + * - `element` The element used to render the flash message. Default to 'default'. * - `params` An array of variables to make available when using an element * * @param string|\Exception $message Message to be flashed. If an instance @@ -80,14 +80,12 @@ public function set($message, array $options = []) { $message = $message->getMessage(); } - if ($opts['element'] !== null) { - list($plugin, $element) = pluginSplit($opts['element']); + list($plugin, $element) = pluginSplit($opts['element']); - if ($plugin) { - $opts['element'] = $plugin . '.Flash/' . $element; - } else { - $opts['element'] = 'Flash/' . $element; - } + if ($plugin) { + $opts['element'] = $plugin . '.Flash/' . $element; + } else { + $opts['element'] = 'Flash/' . $element; } $this->_session->write('Flash.' . $opts['key'], [ diff --git a/src/View/Helper/FlashHelper.php b/src/View/Helper/FlashHelper.php index 407fc8e5bbb..14bfa5872d2 100644 --- a/src/View/Helper/FlashHelper.php +++ b/src/View/Helper/FlashHelper.php @@ -62,18 +62,12 @@ class FlashHelper extends Helper { * @return string */ public function render($key = 'flash', array $options = []) { - $flash = $this->request->session()->read("Flash.$key"); - $this->request->session()->delete("Flash.$key"); - - if (!$flash) { + if (!$this->request->session()->check("Flash.$key")) { return ''; } - $flash = $options + $flash; - - if ($flash['element'] === null) { - return $flash['message']; - } + $flash = $options + $this->request->session()->read("Flash.$key"); + $this->request->session()->delete("Flash.$key"); return $this->_View->element($flash['element'], $flash); } diff --git a/tests/TestCase/Controller/Component/FlashComponentTest.php b/tests/TestCase/Controller/Component/FlashComponentTest.php index 7a13c4ebf9b..c98041b1c35 100644 --- a/tests/TestCase/Controller/Component/FlashComponentTest.php +++ b/tests/TestCase/Controller/Component/FlashComponentTest.php @@ -65,7 +65,7 @@ public function testSet() { $expected = [ 'message' => 'This is a test message', 'key' => 'flash', - 'element' => null, + 'element' => 'Flash/default', 'params' => [] ]; $result = $this->Session->read('Flash.flash'); @@ -95,7 +95,7 @@ public function testSet() { $expected = [ 'message' => 'This is a test message', 'key' => 'foobar', - 'element' => null, + 'element' => 'Flash/default', 'params' => [] ]; $result = $this->Session->read('Flash.foobar'); @@ -115,7 +115,7 @@ public function testSetWithException() { $expected = [ 'message' => 'This is a test message', 'key' => 'flash', - 'element' => null, + 'element' => 'Flash/default', 'params' => ['code' => 404] ]; $result = $this->Session->read('Flash.flash'); diff --git a/tests/TestCase/View/Helper/FlashHelperTest.php b/tests/TestCase/View/Helper/FlashHelperTest.php index 4edd071c499..381488f6230 100644 --- a/tests/TestCase/View/Helper/FlashHelperTest.php +++ b/tests/TestCase/View/Helper/FlashHelperTest.php @@ -46,7 +46,7 @@ public function setUp() { 'flash' => array( 'key' => 'flash', 'message' => 'This is a calling', - 'element' => null, + 'element' => 'Flash/default', 'params' => array() ), 'notification' => array( @@ -75,7 +75,7 @@ public function setUp() { */ public function tearDown() { parent::tearDown(); - unset($this->View, $this->Session); + unset($this->View, $this->Flash); } /** @@ -85,8 +85,8 @@ public function tearDown() { */ public function testFlash() { $result = $this->Flash->render(); - $expected = 'This is a calling'; - $this->assertEquals($expected, $result); + $expected = '
This is a calling
'; + $this->assertContains($expected, $result); $expected = '
Recorded
'; $result = $this->Flash->render('classy'); diff --git a/tests/test_app/TestApp/Template/Element/Flash/default.ctp b/tests/test_app/TestApp/Template/Element/Flash/default.ctp new file mode 100644 index 00000000000..ea70000206a --- /dev/null +++ b/tests/test_app/TestApp/Template/Element/Flash/default.ctp @@ -0,0 +1 @@ +