Skip to content

Commit

Permalink
Simplify escape for flash messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Aug 27, 2016
1 parent bfd8117 commit 676ff2f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Controller/Component/FlashComponent.php
Expand Up @@ -75,6 +75,7 @@ public function __construct(ComponentRegistry $registry, array $config = [])
* - `element` The element used to render the flash message. Default to 'default'.
* - `params` An array of variables to make available when using an element
* - `clear` A bool stating if the current stack should be cleared to start a new one
* - `escape` Set to false to allow templates to print out HTML content
*
* @param string|\Exception $message Message to be flashed. If an instance
* of \Exception the exception message will be used and code will be set
Expand All @@ -91,6 +92,10 @@ public function set($message, array $options = [])
$message = $message->getMessage();
}

if (isset($options['escape'])) {
$options['params'] += ['escape' => $options['escape']];
}

list($plugin, $element) = pluginSplit($options['element']);

if ($plugin) {
Expand Down
34 changes: 33 additions & 1 deletion tests/TestCase/Controller/Component/FlashComponentTest.php
Expand Up @@ -110,6 +110,38 @@ public function testSet()
$this->assertEquals($expected, $result);
}

/**
* @return void
*/
public function testSetEscape()
{
$this->assertNull($this->Session->read('Flash.flash'));

$this->Flash->set('This is a test message', ['escape' => false, 'params' => ['foo' => 'bar']]);
$expected = [
[
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => ['foo' => 'bar', 'escape' => false]
]
];
$result = $this->Session->read('Flash.flash');
$this->assertEquals($expected, $result);

$this->Flash->set('This is a test message', ['key' => 'escaped', 'escape' => false, 'params' => ['foo' => 'bar', 'escape' => true]]);
$expected = [
[
'message' => 'This is a test message',
'key' => 'escaped',
'element' => 'Flash/default',
'params' => ['foo' => 'bar', 'escape' => true]
]
];
$result = $this->Session->read('Flash.escaped');
$this->assertEquals($expected, $result);
}

/**
* test setting messages with using the clear option
*
Expand Down Expand Up @@ -223,7 +255,7 @@ public function testCall()
];
$result = $this->Session->read('Flash.flash');
$this->assertEquals($expected, $result, 'Element is ignored in magic call.');

$this->Flash->success('It worked', ['plugin' => 'MyPlugin']);

$expected[] = [
Expand Down

0 comments on commit 676ff2f

Please sign in to comment.