Navigation Menu

Skip to content

Commit

Permalink
Fixed SessionHelper not handling stacked messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Aug 22, 2017
1 parent cdf00a9 commit 2e75f12
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
34 changes: 21 additions & 13 deletions lib/Cake/Test/Case/View/Helper/SessionHelperTest.php
Expand Up @@ -47,24 +47,32 @@ public function setUp() {
'test' => 'info',
'Message' => array(
'flash' => array(
'element' => 'default',
'params' => array(),
'message' => 'This is a calling'
array(
'element' => 'default',
'params' => array(),
'message' => 'This is a calling'
),
),
'notification' => array(
'element' => 'session_helper',
'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
'message' => 'This is a test of the emergency broadcasting system',
array(
'element' => 'session_helper',
'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
'message' => 'This is a test of the emergency broadcasting system',
),
),
'classy' => array(
'element' => 'default',
'params' => array('class' => 'positive'),
'message' => 'Recorded'
array(
'element' => 'default',
'params' => array('class' => 'positive'),
'message' => 'Recorded'
),
),
'bare' => array(
'element' => null,
'message' => 'Bare message',
'params' => array(),
array(
'element' => null,
'message' => 'Bare message',
'params' => array(),
),
),
),
'Deeply' => array('nested' => array('key' => 'value')),
Expand Down Expand Up @@ -104,7 +112,7 @@ public function testRead() {
public function testCheck() {
$this->assertTrue($this->Session->check('test'));

$this->assertTrue($this->Session->check('Message.flash.element'));
$this->assertTrue($this->Session->check('Message.flash.0.element'));

$this->assertFalse($this->Session->check('Does.not.exist'));

Expand Down
58 changes: 36 additions & 22 deletions lib/Cake/View/Helper/SessionHelper.php
Expand Up @@ -134,30 +134,14 @@ public function flash($key = 'flash', $attrs = array()) {
if (CakeSession::check('Message.' . $key)) {
$flash = CakeSession::read('Message.' . $key);
CakeSession::delete('Message.' . $key);
$message = $flash['message'];
unset($flash['message']);

if (!empty($attrs)) {
$flash = array_merge($flash, $attrs);
}

if ($flash['element'] === 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
$out = '';
foreach ($flash as $flashArray) {
if (!empty($attrs)) {
$flashArray = array_merge($flashArray, $attrs);
}
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
} elseif (!$flash['element']) {
$out = $message;
} else {
$options = array();
if (isset($flash['params']['plugin'])) {
$options['plugin'] = $flash['params']['plugin'];
}
$tmpVars = $flash['params'];
$tmpVars['message'] = $message;
$tmpVars['key'] = $key;
$out = $this->_View->element($flash['element'], $tmpVars, $options);
$flashArray['key'] = $key;
$out .= $this->_render($flashArray);
}
}
return $out;
Expand All @@ -173,4 +157,34 @@ public function valid() {
return CakeSession::valid();
}

/**
* Renders a flash message
*
* @param array $flash Flash message array
* @return string
*/
protected function _render($flash) {
$message = $flash['message'];
unset($flash['message']);

This comment has been minimized.

Copy link
@davidyell

davidyell Jan 23, 2018

Contributor

If $flash is a string, this will cause a fatal.

This comment has been minimized.

Copy link
@jeremyharris

jeremyharris Jan 23, 2018

Author Member

Can you open up a new issue with a way to reproduce? I'm not sure how it could be a string at this point unless you're overriding it. I will take a look at a fix.

This comment has been minimized.

Copy link
@davidyell

davidyell Jan 24, 2018

Contributor

if ($flash['element'] === 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
}
$out = '<div id="' . $flash['key'] . 'Message" class="' . $class . '">' . $message . '</div>';
} elseif (!$flash['element']) {
$out = $message;
} else {
$options = array();
if (isset($flash['params']['plugin'])) {
$options['plugin'] = $flash['params']['plugin'];
}
$tmpVars = $flash['params'];
$tmpVars['message'] = $message;
$tmpVars['key'] = $flash['key'];
$out = $this->_View->element($flash['element'], $tmpVars, $options);
}
return $out;
}
}

0 comments on commit 2e75f12

Please sign in to comment.