diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index 873568d1e13b3b18151dfd88f3222bfc7e940c9b..abb6160dffd71612230183c602d8968ca9ebdea7 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 9b923062f143bc0c4940f4e88045bb0bed1dd6bb..c0a80032b27e5fc23705fb7e2ed1f6e1e7521bee 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(). */