diff --git a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemHalNormalizerTest.php b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemHalNormalizerTest.php new file mode 100644 index 0000000..01fe6d1 --- /dev/null +++ b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemHalNormalizerTest.php @@ -0,0 +1,36 @@ +setCacheContexts(['url.site']); + } + +} diff --git a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTest.php b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTest.php index 06f32f0..278a2cb 100644 --- a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTest.php +++ b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTest.php @@ -2,139 +2,22 @@ namespace Drupal\Tests\image\Kernel\Normalizer; -use Drupal\Core\Cache\Cache; -use Drupal\Core\Field\FieldStorageDefinitionInterface; -use Drupal\Core\Render\BubbleableMetadata; -use Drupal\Core\Render\RenderContext; -use Drupal\entity_test\Entity\EntityTest; -use Drupal\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; -use Drupal\file\Entity\File; -use Drupal\image\Entity\ImageStyle; -use Drupal\KernelTests\KernelTestBase; - /** * @coversDefaultClass \Drupal\image\Normalizer\ImageItemNormalizer * @group image */ -class ImageItemNormalizerTest extends KernelTestBase { +class ImageItemNormalizerTest extends ItemItemNormalizerBase { /** * {@inheritdoc} */ - public static $modules = ['system', 'entity_test', 'serialization', 'image', 'field', 'user', 'file']; - - /** - * The serializer. - * - * @var \Symfony\Component\Serializer\SerializerInterface - */ - protected $serializer; - - /** - * An image for testing. - * - * @var \Drupal\file\FileInterface - */ - protected $image; + protected $format = 'json'; /** * {@inheritdoc} */ - protected function setUp() { - parent::setUp(); - - $this->serializer = \Drupal::service('serializer'); - - $this->installEntitySchema('entity_test'); - $this->installEntitySchema('user'); - $this->installEntitySchema('file'); - $this->installConfig('system'); - $this->installConfig('image'); - $this->installSchema('file', ['file_usage']); - - FieldStorageConfig::create(array( - 'entity_type' => 'entity_test', - 'field_name' => 'image_test', - 'type' => 'image', - 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - ))->save(); - FieldConfig::create([ - 'entity_type' => 'entity_test', - 'field_name' => 'image_test', - 'bundle' => 'entity_test', - 'settings' => [ - 'file_extensions' => 'jpg', - ], - ])->save(); - - // Change all images style scale effect to upscale. - /** @var \Drupal\image\Entity\ImageStyle $image_style */ - foreach (ImageStyle::loadMultiple() as $image_style) { - foreach ($image_style->getEffects() as $effect) { - $config = $effect->getConfiguration(); - $config['data']['upscale'] = TRUE; - $effect->setConfiguration($config); - } - $image_style->save(); - } - - file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg'); - $this->image = File::create([ - 'uri' => 'public://example.jpg', - ]); - $this->image->save(); - } - - /** - * @covers ::normalize - */ - public function testNormalize() { - // 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->alt = $alt = $this->randomMachineName(); - $original_entity->image_test->title = $title = $this->randomMachineName(); - $original_entity->name->value = $this->randomMachineName(); - $original_entity->save(); - - $entity = clone $original_entity; - $context = new RenderContext(); - $data = $this->container->get('renderer') - ->executeInRenderContext($context, function () use ($entity) { - return $this->serializer->normalize($entity); - }); - $cacheability = new BubbleableMetadata(); - $cache_tags = []; - /** @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); - - $image_style_ids = array_keys($data['image_test'][0]['image_styles']); - $expected_image_style_keys = ['large', 'medium', 'thumbnail']; - $this->assertEquals($cacheability, $context->pop()); - $this->assertEquals($expected_image_style_keys, $image_style_ids); - $expect_dimensions = [ - 'large' => [ - 'width' => '422', - 'height' => '480', - ], - 'medium' => [ - 'width' => '194', - 'height' => '220', - ], - 'thumbnail' => [ - 'width' => '88', - 'height' => '100', - ], - ]; - foreach ($data['image_test'][0]['image_styles'] as $id => $img_info) { - $this->assertNotEmpty(strstr($img_info['url'], "files/styles/$id/public/example.jpg")); - $this->assertEquals($expect_dimensions[$id]['height'], $img_info['height'], "Style $id matches height."); - $this->assertEquals($expect_dimensions[$id]['width'], $img_info['width'], "Style $id matches width."); - } + protected function getNormalizedImageStyles(array $data) { + return $data['image_test'][0]['image_styles']; } } diff --git a/core/modules/image/tests/src/Kernel/Normalizer/ItemItemNormalizerBase.php b/core/modules/image/tests/src/Kernel/Normalizer/ItemItemNormalizerBase.php new file mode 100644 index 0000000..ece4a19 --- /dev/null +++ b/core/modules/image/tests/src/Kernel/Normalizer/ItemItemNormalizerBase.php @@ -0,0 +1,168 @@ +serializer = \Drupal::service('serializer'); + + $this->installEntitySchema('entity_test'); + $this->installEntitySchema('user'); + $this->installEntitySchema('file'); + $this->installConfig('system'); + $this->installConfig('image'); + $this->installSchema('file', ['file_usage']); + + FieldStorageConfig::create(array( + 'entity_type' => 'entity_test', + 'field_name' => 'image_test', + 'type' => 'image', + 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, + ))->save(); + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'image_test', + 'bundle' => 'entity_test', + 'settings' => [ + 'file_extensions' => 'jpg', + ], + ])->save(); + + // Change all images style scale effect to upscale. + /** @var \Drupal\image\Entity\ImageStyle $image_style */ + foreach (ImageStyle::loadMultiple() as $image_style) { + foreach ($image_style->getEffects() as $effect) { + $config = $effect->getConfiguration(); + $config['data']['upscale'] = TRUE; + $effect->setConfiguration($config); + } + $image_style->save(); + } + + file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg'); + $this->image = File::create([ + 'uri' => 'public://example.jpg', + ]); + $this->image->save(); + } + + /** + * @covers ::normalize + */ + public function testNormalize() { + // 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->alt = $alt = $this->randomMachineName(); + $original_entity->image_test->title = $title = $this->randomMachineName(); + $original_entity->name->value = $this->randomMachineName(); + $original_entity->save(); + + $entity = clone $original_entity; + $context = new RenderContext(); + $data = $this->container->get('renderer') + ->executeInRenderContext($context, function () use ($entity) { + return $this->serializer->normalize($entity, $this->format); + }); + + $normalized_image_styles = $this->getNormalizedImageStyles($data); + $this->assertEquals($this->getExpectedCacheability(), $context->pop()); + $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 $id => $img_info) { + $this->assertNotEmpty(strstr($img_info['url'], "files/styles/$id/public/example.jpg")); + $this->assertEquals($expect_dimensions[$id]['height'], $img_info['height'], "Style $id matches height."); + $this->assertEquals($expect_dimensions[$id]['width'], $img_info['width'], "Style $id matches width."); + } + } + + /** + * Gets normalized image styles from normalized entity. + * + * @param array $data + * The normalized entity. + * + * @return array + * The normalized image styles. + */ + abstract protected function getNormalizedImageStyles(array $data); + + /** + * Gets the expected cacheability metadata. + * + * @return \Drupal\Core\Render\BubbleableMetadata + * The cacheability metadata. + */ + protected function getExpectedCacheability() { + $cacheability = new BubbleableMetadata(); + $cache_tags = []; + /** @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; + } + +}