Browse Source

Simplify escape for flash messages.

Mark Scherer 9 years ago
parent
commit
676ff2f938

+ 5 - 0
src/Controller/Component/FlashComponent.php

@@ -75,6 +75,7 @@ class FlashComponent extends Component
      * - `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
@@ -91,6 +92,10 @@ class FlashComponent extends Component
             $message = $message->getMessage();
         }
 
+        if (isset($options['escape'])) {
+            $options['params'] += ['escape' => $options['escape']];
+        }
+
         list($plugin, $element) = pluginSplit($options['element']);
 
         if ($plugin) {

+ 33 - 1
tests/TestCase/Controller/Component/FlashComponentTest.php

@@ -111,6 +111,38 @@ class FlashComponentTest extends TestCase
     }
 
     /**
+     * @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
      *
      * @return void
@@ -223,7 +255,7 @@ class FlashComponentTest extends TestCase
         ];
         $result = $this->Session->read('Flash.flash');
         $this->assertEquals($expected, $result, 'Element is ignored in magic call.');
-        
+
         $this->Flash->success('It worked', ['plugin' => 'MyPlugin']);
 
         $expected[] = [