From d70a4752348a9a8809fe4957c9a893aac13bc518 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 4 Jan 2022 23:06:03 +0000
Subject: [PATCH] Issue #3031130 by tim.plunkett:
 \Drupal\Core\KeyValueStore\MemoryStorage::rename() erases data if keys match

---
 .../lib/Drupal/Core/KeyValueStore/MemoryStorage.php |  6 ++++--
 .../Core/KeyValueStore/StorageTestBase.php          | 13 +++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php
index d6a6ca91d409..38c62685df70 100644
--- a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php
+++ b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php
@@ -71,8 +71,10 @@ public function setMultiple(array $data) {
    * {@inheritdoc}
    */
   public function rename($key, $new_key) {
-    $this->data[$new_key] = $this->data[$key];
-    unset($this->data[$key]);
+    if ($key !== $new_key) {
+      $this->data[$new_key] = $this->data[$key];
+      unset($this->data[$key]);
+    }
   }
 
   /**
diff --git a/core/tests/Drupal/KernelTests/Core/KeyValueStore/StorageTestBase.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/StorageTestBase.php
index a79e0232b1f7..23059b8372be 100644
--- a/core/tests/Drupal/KernelTests/Core/KeyValueStore/StorageTestBase.php
+++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/StorageTestBase.php
@@ -189,6 +189,19 @@ public function testRename() {
     $this->assertNull($store->get('old'));
   }
 
+  /**
+   * Tests the rename operation.
+   */
+  public function testRenameNoChange() {
+    $stores = $this->createStorage();
+    $store = $stores[0];
+
+    $store->set('old', 'thing');
+    $this->assertSame($store->get('old'), 'thing');
+    $store->rename('old', 'old');
+    $this->assertSame($store->get('old'), 'thing');
+  }
+
   /**
    * Creates storage objects for each collection defined for this class.
    *
-- 
GitLab