From 590371caddeeaa671f787e8552e404a52c438f94 Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Fri, 10 Jan 2025 12:26:34 +0000
Subject: [PATCH] Issue #3496319 by spokje: [random test failure]
CommentPreviewTest::testCommentPreview
(cherry picked from commit 2ba2f9f3fedc93a580e4657a3c7406bf5751ef2a)
---
.../comment/tests/src/Functional/CommentPreviewTest.php | 4 ++--
.../modules/user_hooks_test/src/Hook/UserHooksTest.php | 8 ++------
core/modules/user/tests/src/Functional/UserEditTest.php | 2 +-
.../user/tests/src/Functional/UserTokenReplaceTest.php | 7 +++----
.../modules/user/tests/src/Kernel/UserEntityLabelTest.php | 2 +-
5 files changed, 9 insertions(+), 14 deletions(-)
diff --git a/core/modules/comment/tests/src/Functional/CommentPreviewTest.php b/core/modules/comment/tests/src/Functional/CommentPreviewTest.php
index a3d3c16f636..1f7b5540ecc 100644
--- a/core/modules/comment/tests/src/Functional/CommentPreviewTest.php
+++ b/core/modules/comment/tests/src/Functional/CommentPreviewTest.php
@@ -45,7 +45,7 @@ class CommentPreviewTest extends CommentTestBase {
// 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 @@ class CommentPreviewTest extends CommentTestBase {
$this->submitForm($edit, 'Preview');
$this->assertSession()->assertEscaped('' . $this->webUser->id() . '');
- \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 282791a0b13..ed10dcf6037 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
@@ -7,16 +7,12 @@ namespace Drupal\user_hooks_test\Hook;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Session\AccountInterface;
-use Drupal\Core\State\StateInterface;
/**
* Contains hook implementations.
*/
class UserHooksTest {
- public function __construct(protected StateInterface $state) {
- }
-
/**
* Alters the username.
*
@@ -29,8 +25,8 @@ class UserHooksTest {
*/
#[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 (\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('@uid', ['@uid' => $account->id()]);
}
else {
diff --git a/core/modules/user/tests/src/Functional/UserEditTest.php b/core/modules/user/tests/src/Functional/UserEditTest.php
index 295bc01f26e..e8596786373 100644
--- a/core/modules/user/tests/src/Functional/UserEditTest.php
+++ b/core/modules/user/tests/src/Functional/UserEditTest.php
@@ -36,7 +36,7 @@ class UserEditTest extends BrowserTestBase {
// 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 f398d55d261..f41476bbf37 100644
--- a/core/modules/user/tests/src/Functional/UserTokenReplaceTest.php
+++ b/core/modules/user/tests/src/Functional/UserTokenReplaceTest.php
@@ -51,8 +51,8 @@ class UserTokenReplaceTest extends BrowserTestBase {
'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 @@ class UserTokenReplaceTest extends BrowserTestBase {
}
// 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 = "{$user1->id()} {$user2->id()}";
$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 dbf08670782..46ade1ac470 100644
--- a/core/modules/user/tests/src/Kernel/UserEntityLabelTest.php
+++ b/core/modules/user/tests/src/Kernel/UserEntityLabelTest.php
@@ -46,7 +46,7 @@ class UserEntityLabelTest extends KernelTestBase {
$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('' . $account->id() . '', $account->getDisplayName());