diff --git a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php index 6084afc..95e51d5 100644 --- a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php +++ b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php @@ -69,7 +69,7 @@ protected function setUp() { 'field_name' => 'image_test', 'bundle' => 'entity_test', 'settings' => [ - 'file_extensions' => 'jpg', + 'file_extensions' => 'jpg,svg', ], ])->save(); @@ -84,11 +84,13 @@ protected function setUp() { $image_style->save(); } + // Create the images needed for tests. + // @see ::imagesProvider() file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg'); - $this->image = File::create([ - 'uri' => 'public://example.jpg', - ]); - $this->image->save(); + + // We don't have to put any real SVG data in here, because the GD toolkit + // won't be able to load it anyway. + touch('public://example.svg'); } /** @@ -97,11 +99,18 @@ protected function setUp() { * @see \Drupal\image\Normalizer\ImageItemNormalizerTrait::decorateWithImageStyles() * * @covers ::normalize + * + * @dataProvider imagesProvider */ - public function testNormalize() { + public function testNormalize($uri, $styles_included) { + + $image = File::create([ + 'uri' => $uri, + ]); + $image->save(); // Create a test entity with the image field set. $original_entity = EntityTest::create(); - $original_entity->image_test->target_id = $this->image->id(); + $original_entity->image_test->target_id = $image->id(); $original_entity->image_test->alt = $alt = $this->randomMachineName(); $original_entity->image_test->title = $title = $this->randomMachineName(); $original_entity->name->value = $this->randomMachineName(); @@ -112,28 +121,34 @@ public function testNormalize() { $normalization = $this->serializer->normalize($entity, $this->format, ['cacheability' => $cacheable_metadata]); $normalized_image_styles = $this->getNormalizedImageStyles($normalization); - $this->assertEquals($this->getExpectedCacheability(), $cacheable_metadata); - $expect_dimensions = [ - 'large' => [ - 'width' => '422', - 'height' => '480', - ], - 'medium' => [ - 'width' => '194', - 'height' => '220', - ], - 'thumbnail' => [ - 'width' => '88', - 'height' => '100', - ], - ]; - $this->assertEquals(array_keys($expect_dimensions), array_keys($normalized_image_styles)); - - foreach ($normalized_image_styles as $image_style_id => $image_style_dimensions) { - $this->assertContains("files/styles/$image_style_id/public/example.jpg", $image_style_dimensions['url']); - $this->assertEquals($expect_dimensions[$image_style_id]['height'], $image_style_dimensions['height'], "Style $image_style_id matches height."); - $this->assertEquals($expect_dimensions[$image_style_id]['width'], $image_style_dimensions['width'], "Style $image_style_id matches width."); + if ($styles_included) { + $this->assertEquals($this->getExpectedCacheability($styles_included), $cacheable_metadata); + $expect_dimensions = [ + 'large' => [ + 'width' => '422', + 'height' => '480', + ], + 'medium' => [ + 'width' => '194', + 'height' => '220', + ], + 'thumbnail' => [ + 'width' => '88', + 'height' => '100', + ], + ]; + $this->assertEquals(array_keys($expect_dimensions), array_keys($normalized_image_styles)); + + foreach ($normalized_image_styles as $image_style_id => $image_style_dimensions) { + $this->assertContains("files/styles/$image_style_id/public/example.jpg", $image_style_dimensions['url']); + $this->assertEquals($expect_dimensions[$image_style_id]['height'], $image_style_dimensions['height'], "Style $image_style_id matches height."); + $this->assertEquals($expect_dimensions[$image_style_id]['width'], $image_style_dimensions['width'], "Style $image_style_id matches width."); + } + } + else { + $this->assertEquals([], $normalized_image_styles); } + } /** @@ -150,18 +165,41 @@ public function testNormalize() { /** * Gets the expected bubbled cacheability metadata. * + * @param bool $styles_included + * Whether styles will be included in normalization. + * * @return \Drupal\Core\Cache\CacheableMetadata * The expected cacheability metadata. */ - protected function getExpectedCacheability() { + protected function getExpectedCacheability($styles_included) { $cacheability = new CacheableMetadata(); $cache_tags = []; - /** @var \Drupal\image\ImageStyleInterface $image_style */ - foreach (ImageStyle::loadMultiple() as $image_style) { - $cache_tags = Cache::mergeTags($cache_tags, $image_style->getCacheTags()); + if ($styles_included) { + /** @var \Drupal\image\ImageStyleInterface $image_style */ + foreach (ImageStyle::loadMultiple() as $image_style) { + $cache_tags = Cache::mergeTags($cache_tags, $image_style->getCacheTags()); + } } $cacheability->setCacheTags($cache_tags); return $cacheability; } + /** + * Datatprovider for testNormalize(). + */ + public function imagesProvider() { + return [ + 'jpg' => [ + 'uri' => 'public://example.jpg', + 'styles_included' => TRUE, + ], + // Add a test case for svg file because images styles will not be + // included. + 'svg' => [ + 'uri' => 'public://example.svg', + 'styles_included' => FALSE, + ], + ]; + } + }