Stacking messages in SessionComponent::setFlash

This commit is contained in:
Jeremy Harris 2017-08-30 10:06:56 -05:00
parent 81d824077a
commit f9f06e68b1
No known key found for this signature in database
GPG key ID: C90F85CC81D949C8
2 changed files with 10 additions and 7 deletions

View file

@ -136,7 +136,13 @@ class SessionComponent extends Component {
* @deprecated 3.0.0 Since 2.7, use the FlashComponent instead. * @deprecated 3.0.0 Since 2.7, use the FlashComponent instead.
*/ */
public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') { public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
CakeSession::write('Message.' . $key, compact('message', 'element', 'params')); $messages = (array)CakeSession::read('Message.' . $key);
$messages[] = [
'message' => $message,
'element' => $element,
'params' => $params,
];
CakeSession::write('Message.' . $key, $messages);
} }
/** /**

View file

@ -247,16 +247,13 @@ class SessionComponentTest extends CakeTestCase {
$this->assertNull($Session->read('Message.flash')); $this->assertNull($Session->read('Message.flash'));
$Session->setFlash('This is a test message'); $Session->setFlash('This is a test message');
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash')); $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash.0'));
$Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss')); $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash')); $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash.1'));
$Session->setFlash('This is a test message', 'default', array(), 'myFlash'); $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash')); $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash.0'));
$Session->setFlash('This is a test message', 'non_existing_layout');
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
$Session->delete('Message'); $Session->delete('Message');
} }