From 0c1fb8265425bfa303b607710a82519b5129374f Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Fri, 14 Mar 2025 18:35:27 +0000
Subject: [PATCH] Issue #3496529 by spokje, acbramley: [random test failure]
 DownloadTest::doPrivateFileTransferTest

---
 .../file/tests/file_test/src/FileTestHelper.php | 17 +++++++++--------
 .../tests/file_test/src/Hook/FileTestHooks.php  |  2 +-
 .../file/tests/src/Functional/DownloadTest.php  |  6 +++---
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/core/modules/file/tests/file_test/src/FileTestHelper.php b/core/modules/file/tests/file_test/src/FileTestHelper.php
index 9a5bc42dfbd4..9a3641104156 100644
--- a/core/modules/file/tests/file_test/src/FileTestHelper.php
+++ b/core/modules/file/tests/file_test/src/FileTestHelper.php
@@ -30,14 +30,14 @@ public static function reset(): void {
       'move' => [],
       'delete' => [],
     ];
-    \Drupal::state()->set('file_test.results', $results);
+    \Drupal::keyValue('file_test')->set('results', $results);
 
     // These hooks will return these values, see FileTestHelper::setReturn().
     $return = [
       'validate' => [],
       'download' => NULL,
     ];
-    \Drupal::state()->set('file_test.return', $return);
+    \Drupal::keyValue('file_test')->set('return', $return);
   }
 
   /**
@@ -57,7 +57,7 @@ public static function reset(): void {
    * @see Drupal\file_test\FileTestHelper::reset()
    */
   public static function getCalls($op): array {
-    $results = \Drupal::state()->get('file_test.results', []);
+    $results = \Drupal::keyValue('file_test')->get('results', []);
     return $results[$op];
   }
 
@@ -70,7 +70,7 @@ public static function getCalls($op): array {
    *   parameters passed to each call.
    */
   public static function getAllCalls(): array {
-    return \Drupal::state()->get('file_test.results', []);
+    return \Drupal::keyValue('file_test')->get('results', []);
   }
 
   /**
@@ -87,9 +87,9 @@ public static function getAllCalls(): array {
    */
   public static function logCall($op, $args): void {
     if (\Drupal::state()->get('file_test.count_hook_invocations', TRUE)) {
-      $results = \Drupal::state()->get('file_test.results', []);
+      $results = \Drupal::keyValue('file_test')->get('results', []);
       $results[$op][] = $args;
-      \Drupal::state()->set('file_test.results', $results);
+      \Drupal::keyValue('file_test')->set('results', $results);
     }
   }
 
@@ -105,9 +105,10 @@ public static function logCall($op, $args): void {
    * @see Drupal\file_test\FileTestHelper::reset()
    */
   public static function setReturn($op, $value): void {
-    $return = \Drupal::state()->get('file_test.return', []);
+    $return = \Drupal::keyValue('file_test')->get('return', []);
+
     $return[$op] = $value;
-    \Drupal::state()->set('file_test.return', $return);
+    \Drupal::keyValue('file_test')->set('return', $return);
   }
 
   /**
diff --git a/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php b/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
index 267e2ada834c..c58c2eba4fd0 100644
--- a/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
+++ b/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
@@ -212,7 +212,7 @@ public function entityTypeAlter(&$entity_types) : void {
    * @see Drupal\file_test\FileTestHelper::reset()
    */
   public function getReturn($op): array|int|null {
-    $return = \Drupal::state()->get('file_test.return', [$op => NULL]);
+    $return = \Drupal::keyValue('file_test')->get('return', [$op => NULL]);
     return $return[$op];
   }
 
diff --git a/core/modules/file/tests/src/Functional/DownloadTest.php b/core/modules/file/tests/src/Functional/DownloadTest.php
index d90ec492487c..ddbb1868744c 100644
--- a/core/modules/file/tests/src/Functional/DownloadTest.php
+++ b/core/modules/file/tests/src/Functional/DownloadTest.php
@@ -105,7 +105,7 @@ protected function doPrivateFileTransferTest(): void {
     $this->assertSession()->responseHeaderDoesNotExist('x-drupal-cache');
     $this->assertSession()->statusCodeEquals(200);
     // Ensure hook_file_download is fired correctly.
-    $this->assertEquals($file->getFileUri(), \Drupal::state()->get('file_test.results')['download'][0][0]);
+    $this->assertEquals($file->getFileUri(), FileTestHelper::getCalls('download')[0][0]);
 
     // Test that the file transferred correctly.
     $this->assertSame($contents, $this->getSession()->getPage()->getContent(), 'Contents of the file are correct.');
@@ -117,7 +117,7 @@ protected function doPrivateFileTransferTest(): void {
     $response = $http_client->head($not_found_url, ['http_errors' => FALSE]);
     $this->assertSame(404, $response->getStatusCode(), 'Correctly returned 404 response for a non-existent file.');
     // Assert that hook_file_download is not called.
-    $this->assertEquals([], \Drupal::state()->get('file_test.results')['download']);
+    $this->assertEquals([], FileTestHelper::getCalls('download'));
 
     // Having tried a non-existent file, try the original file again to ensure
     // it's returned instead of a 404 response.
@@ -143,7 +143,7 @@ protected function doPrivateFileTransferTest(): void {
     $this->drupalGet('/system/files');
     $this->assertSession()->statusCodeEquals(404);
     // Assert that hook_file_download is not called.
-    $this->assertEquals([], \Drupal::state()->get('file_test.results')['download']);
+    $this->assertEquals([], FileTestHelper::getCalls('download'));
   }
 
   /**
-- 
GitLab