Browse Source

Override "params" instead of merging in FlashMessage::set().

This makes the behavior inline with how FlashComponent::set() works.
ADmad 5 years ago
parent
commit
2e155979fd
2 changed files with 22 additions and 2 deletions
  1. 1 2
      src/Http/FlashMessage.php
  2. 21 0
      tests/TestCase/Http/FlashMessageTest.php

+ 1 - 2
src/Http/FlashMessage.php

@@ -17,7 +17,6 @@ declare(strict_types=1);
 namespace Cake\Http;
 
 use Cake\Core\InstanceConfigTrait;
-use Cake\Utility\Hash;
 use Throwable;
 
 /**
@@ -82,7 +81,7 @@ class FlashMessage
      */
     public function set($message, array $options = []): void
     {
-        $options = Hash::merge((array)$this->getConfig(), $options);
+        $options += (array)$this->getConfig();
 
         if (isset($options['escape']) && !isset($options['params']['escape'])) {
             $options['params']['escape'] = $options['escape'];

+ 21 - 0
tests/TestCase/Http/FlashMessageTest.php

@@ -104,6 +104,27 @@ class FlashMessageTest extends TestCase
         $this->assertEquals($expected, $result);
     }
 
+    public function testDefaultParamsOverriding()
+    {
+        $this->Flash = new FlashMessage(
+            $this->Session,
+            ['params' => ['foo' => 'bar']]
+        );
+
+        $this->Flash->set(
+            'This is a test message',
+            ['params' => ['username' => 'ADmad']]
+        );
+        $expected[] = [
+            'message' => 'This is a test message',
+            'key' => 'flash',
+            'element' => 'flash/default',
+            'params' => ['username' => 'ADmad'],
+        ];
+        $result = $this->Session->read('Flash.flash');
+        $this->assertEquals($expected, $result);
+    }
+
     public function testDuplicateIgnored(): void
     {
         $this->assertNull($this->Session->read('Flash.flash'));