From 7b24d54842e3580de9268f63d183c01dd0880092 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 24 Apr 2024 01:36:43 +0100
Subject: [PATCH] Issue #3439981 by fago, ericgsmith, petar_basic, acbramley:
 Uploading a file to media library flushes theme registry

---
 core/modules/image/src/Entity/ImageStyle.php  | 10 ++--
 .../image/tests/src/Unit/ImageStyleTest.php   | 57 +++++++++++++++++++
 2 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php
index 873568d1e13b..abb6160dffd7 100644
--- a/core/modules/image/src/Entity/ImageStyle.php
+++ b/core/modules/image/src/Entity/ImageStyle.php
@@ -298,10 +298,12 @@ public function flush($path = NULL) {
     $module_handler = \Drupal::moduleHandler();
     $module_handler->invokeAll('image_style_flush', [$this, $path]);
 
-    // Clear caches so that formatters may be added for this style.
-    \Drupal::service('theme.registry')->reset();
-
-    Cache::invalidateTags($this->getCacheTagsToInvalidate());
+    // Clear caches when the complete image style is flushed,
+    // so that field formatters may be added for this style.
+    if (!isset($path)) {
+      \Drupal::service('theme.registry')->reset();
+      Cache::invalidateTags($this->getCacheTagsToInvalidate());
+    }
 
     return $this;
   }
diff --git a/core/modules/image/tests/src/Unit/ImageStyleTest.php b/core/modules/image/tests/src/Unit/ImageStyleTest.php
index 9b923062f143..c0a80032b27e 100644
--- a/core/modules/image/tests/src/Unit/ImageStyleTest.php
+++ b/core/modules/image/tests/src/Unit/ImageStyleTest.php
@@ -5,6 +5,7 @@
 namespace Drupal\Tests\image\Unit;
 
 use Drupal\Component\Utility\Crypt;
+use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Tests\UnitTestCase;
 
 /**
@@ -198,6 +199,62 @@ public function testGetPathToken() {
     $this->assertEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg'));
   }
 
+  /**
+   * @covers ::flush
+   */
+  public function testFlush() {
+    $cache_tag_invalidator = $this->createMock('\Drupal\Core\Cache\CacheTagsInvalidator');
+    $file_system = $this->createMock('\Drupal\Core\File\FileSystemInterface');
+    $module_handler = $this->createMock('\Drupal\Core\Extension\ModuleHandlerInterface');
+    $stream_wrapper_manager = $this->createMock('\Drupal\Core\StreamWrapper\StreamWrapperManagerInterface');
+    $stream_wrapper_manager->expects($this->any())
+      ->method('getWrappers')
+      ->will($this->returnValue([]));
+    $theme_registry = $this->createMock('\Drupal\Core\Theme\Registry');
+
+    $container = new ContainerBuilder();
+    $container->set('cache_tags.invalidator', $cache_tag_invalidator);
+    $container->set('file_system', $file_system);
+    $container->set('module_handler', $module_handler);
+    $container->set('stream_wrapper_manager', $stream_wrapper_manager);
+    $container->set('theme.registry', $theme_registry);
+    \Drupal::setContainer($container);
+
+    $image_effect_id = $this->randomMachineName();
+    $image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase');
+
+    $image_style = $this->getImageStyleMock($image_effect_id, $image_effect, ['buildUri', 'getCacheTagsToInvalidate']);
+    $image_style->expects($this->any())
+      ->method('buildUri')
+      ->willReturn('test.jpg');
+    $image_style->expects($this->any())
+      ->method('getCacheTagsToInvalidate')
+      ->willReturn([]);
+
+    // Assert the theme registry is reset.
+    $theme_registry
+      ->expects($this->once())
+      ->method('reset');
+    // Assert the cache tags are invalidated.
+    $cache_tag_invalidator
+      ->expects($this->once())
+      ->method('invalidateTags');
+
+    $image_style->flush();
+
+    // Assert the theme registry is not reset a path is flushed.
+    $theme_registry
+      ->expects($this->never())
+      ->method('reset');
+    // Assert the cache tags are not reset a path is flushed.
+    $cache_tag_invalidator
+      ->expects($this->never())
+      ->method('invalidateTags');
+
+    $image_style->flush('test.jpg');
+
+  }
+
   /**
    * Mock function for ImageStyle::fileDefaultScheme().
    */
-- 
GitLab