From eccee6e1e4644483f568a48f59178f6109caae1a Mon Sep 17 00:00:00 2001
From: Spokje <39382-Spokje@users.noreply.drupalcode.org>
Date: Sat, 28 Dec 2024 11:29:17 +0100
Subject: [PATCH 1/2] \Drupal::state()-get()/set() to
 Drupal::keyValue()->get()/set()

---
 .../tests/src/Functional/CommentPreviewTest.php        |  4 ++--
 .../modules/user_hooks_test/src/Hook/UserHooksTest.php |  8 ++++----
 .../user_hooks_test/user_hooks_test.services.yml       | 10 ++++++++++
 .../modules/user/tests/src/Functional/UserEditTest.php |  2 +-
 .../user/tests/src/Functional/UserTokenReplaceTest.php |  7 +++----
 .../user/tests/src/Kernel/UserEntityLabelTest.php      |  2 +-
 6 files changed, 21 insertions(+), 12 deletions(-)
 create mode 100644 core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml

diff --git a/core/modules/comment/tests/src/Functional/CommentPreviewTest.php b/core/modules/comment/tests/src/Functional/CommentPreviewTest.php
index a3d3c16f6367..1f7b5540ecc3 100644
--- a/core/modules/comment/tests/src/Functional/CommentPreviewTest.php
+++ b/core/modules/comment/tests/src/Functional/CommentPreviewTest.php
@@ -45,7 +45,7 @@ public function testCommentPreview(): void {
 
     // Test escaping of the username on the preview form.
     \Drupal::service('module_installer')->install(['user_hooks_test']);
-    \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
+    \Drupal::keyValue('user_hooks_test')->set('user_format_name_alter', TRUE);
     $edit = [];
     $edit['subject[0][value]'] = $this->randomMachineName(8);
     $edit['comment_body[0][value]'] = $this->randomMachineName(16);
@@ -53,7 +53,7 @@ public function testCommentPreview(): void {
     $this->submitForm($edit, 'Preview');
     $this->assertSession()->assertEscaped('<em>' . $this->webUser->id() . '</em>');
 
-    \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
+    \Drupal::keyValue('user_hooks_test')->set('user_format_name_alter_safe', TRUE);
     $this->drupalGet('node/' . $this->node->id());
     $this->submitForm($edit, 'Preview');
     $this->assertInstanceOf(MarkupInterface::class, $this->webUser->getDisplayName());
diff --git a/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php b/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php
index 282791a0b130..b6d15ea3e957 100644
--- a/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php
+++ b/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php
@@ -6,15 +6,15 @@
 
 use Drupal\Component\Render\FormattableMarkup;
 use Drupal\Core\Hook\Attribute\Hook;
+use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
 use Drupal\Core\Session\AccountInterface;
-use Drupal\Core\State\StateInterface;
 
 /**
  * Contains hook implementations.
  */
 class UserHooksTest {
 
-  public function __construct(protected StateInterface $state) {
+  public function __construct(protected KeyValueStoreInterface $keyValue) {
   }
 
   /**
@@ -29,8 +29,8 @@ public function __construct(protected StateInterface $state) {
    */
   #[Hook('user_format_name_alter')]
   public function userFormatNameAlter(&$name, AccountInterface $account): void {
-    if ($this->state->get('user_hooks_test_user_format_name_alter', FALSE)) {
-      if ($this->state->get('user_hooks_test_user_format_name_alter_safe', FALSE)) {
+    if ($this->keyValue->get('user_format_name_alter', FALSE)) {
+      if ($this->keyValue->get('user_format_name_alter_safe', FALSE)) {
         $name = new FormattableMarkup('<em>@uid</em>', ['@uid' => $account->id()]);
       }
       else {
diff --git a/core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml b/core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml
new file mode 100644
index 000000000000..6f341503fa98
--- /dev/null
+++ b/core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml
@@ -0,0 +1,10 @@
+services:
+  user_hooks_test.user_hooks_test:
+    class: \Drupal\user_hooks_test\Hook\UserHooksTest
+    arguments: ['@user_hooks_test.key_value.user_hooks_test']
+  user_hooks_test.key_value.user_hooks_test:
+    class: Drupal\Core\KeyValueStore\KeyValueStoreInterface
+    factory: ['@keyvalue', 'get']
+    arguments: ['user_hooks_test']
+    public: true
+  Drupal\Core\KeyValueStore\KeyValueStoreInterface: '@user_hooks_test.key_value.user_hooks_test'
diff --git a/core/modules/user/tests/src/Functional/UserEditTest.php b/core/modules/user/tests/src/Functional/UserEditTest.php
index 295bc01f26e9..e8596786373a 100644
--- a/core/modules/user/tests/src/Functional/UserEditTest.php
+++ b/core/modules/user/tests/src/Functional/UserEditTest.php
@@ -36,7 +36,7 @@ public function testUserEdit(): void {
 
     // Check that the default value in user name field
     // is the raw value and not a formatted one.
-    \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
+    \Drupal::keyValue('user_hooks_test')->set('user_format_name_alter', TRUE);
     \Drupal::service('module_installer')->install(['user_hooks_test']);
     Cache::invalidateTags(['rendered']);
     $this->drupalGet('user/' . $user1->id() . '/edit');
diff --git a/core/modules/user/tests/src/Functional/UserTokenReplaceTest.php b/core/modules/user/tests/src/Functional/UserTokenReplaceTest.php
index f398d55d2614..f41476bbf372 100644
--- a/core/modules/user/tests/src/Functional/UserTokenReplaceTest.php
+++ b/core/modules/user/tests/src/Functional/UserTokenReplaceTest.php
@@ -51,8 +51,8 @@ public function testUserTokenReplacement(): void {
       'language' => $language_interface,
     ];
 
-    \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
-    \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
+    \Drupal::keyValue('user_hooks_test')->set('user_format_name_alter', TRUE);
+    \Drupal::keyValue('user_hooks_test')->set('user_format_name_alter_safe', TRUE);
 
     // Create two users and log them in one after another.
     $user1 = $this->drupalCreateUser([]);
@@ -173,8 +173,7 @@ public function testUserTokenReplacement(): void {
     }
 
     // Generate user display name tokens when safe markup is returned.
-    // @see user_hooks_test_user_format_name_alter()
-    \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
+    \Drupal::keyValue('user_hooks_test')->set('user_format_name_alter_safe', TRUE);
     $input = '[user:display-name] [current-user:display-name]';
     $expected = "<em>{$user1->id()}</em> <em>{$user2->id()}</em>";
     $output = $token_service->replace($input, ['user' => $user1]);
diff --git a/core/modules/user/tests/src/Kernel/UserEntityLabelTest.php b/core/modules/user/tests/src/Kernel/UserEntityLabelTest.php
index dbf086707829..46ade1ac470b 100644
--- a/core/modules/user/tests/src/Kernel/UserEntityLabelTest.php
+++ b/core/modules/user/tests/src/Kernel/UserEntityLabelTest.php
@@ -46,7 +46,7 @@ public function testLabelCallback(): void {
     $this->assertEmpty($anonymous->getAccountName());
 
     // Set to test the altered username.
-    \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
+    \Drupal::keyValue('user_hooks_test')->set('user_format_name_alter', TRUE);
 
     // The user display name should be altered.
     $this->assertEquals('<em>' . $account->id() . '</em>', $account->getDisplayName());
-- 
GitLab


From 535703a88d0202fbe333313d80daa9f4090d3c98 Mon Sep 17 00:00:00 2001
From: Spokje <39382-Spokje@users.noreply.drupalcode.org>
Date: Sat, 4 Jan 2025 11:38:07 +0100
Subject: [PATCH 2/2] F.U.D.I.

---
 .../modules/user_hooks_test/src/Hook/UserHooksTest.php |  8 ++------
 .../user_hooks_test/user_hooks_test.services.yml       | 10 ----------
 2 files changed, 2 insertions(+), 16 deletions(-)
 delete mode 100644 core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml

diff --git a/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php b/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php
index b6d15ea3e957..ed10dcf60370 100644
--- a/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php
+++ b/core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php
@@ -6,7 +6,6 @@
 
 use Drupal\Component\Render\FormattableMarkup;
 use Drupal\Core\Hook\Attribute\Hook;
-use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
 use Drupal\Core\Session\AccountInterface;
 
 /**
@@ -14,9 +13,6 @@
  */
 class UserHooksTest {
 
-  public function __construct(protected KeyValueStoreInterface $keyValue) {
-  }
-
   /**
    * Alters the username.
    *
@@ -29,8 +25,8 @@ public function __construct(protected KeyValueStoreInterface $keyValue) {
    */
   #[Hook('user_format_name_alter')]
   public function userFormatNameAlter(&$name, AccountInterface $account): void {
-    if ($this->keyValue->get('user_format_name_alter', FALSE)) {
-      if ($this->keyValue->get('user_format_name_alter_safe', FALSE)) {
+    if (\Drupal::keyValue('user_hooks_test')->get('user_format_name_alter', FALSE)) {
+      if (\Drupal::keyValue('user_hooks_test')->get('user_format_name_alter_safe', FALSE)) {
         $name = new FormattableMarkup('<em>@uid</em>', ['@uid' => $account->id()]);
       }
       else {
diff --git a/core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml b/core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml
deleted file mode 100644
index 6f341503fa98..000000000000
--- a/core/modules/user/tests/modules/user_hooks_test/user_hooks_test.services.yml
+++ /dev/null
@@ -1,10 +0,0 @@
-services:
-  user_hooks_test.user_hooks_test:
-    class: \Drupal\user_hooks_test\Hook\UserHooksTest
-    arguments: ['@user_hooks_test.key_value.user_hooks_test']
-  user_hooks_test.key_value.user_hooks_test:
-    class: Drupal\Core\KeyValueStore\KeyValueStoreInterface
-    factory: ['@keyvalue', 'get']
-    arguments: ['user_hooks_test']
-    public: true
-  Drupal\Core\KeyValueStore\KeyValueStoreInterface: '@user_hooks_test.key_value.user_hooks_test'
-- 
GitLab