3 namespace Drupal\Tests\image\Kernel;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
13 * Tests the image field rendering using entity fields of the image field type.
17 class ImageFormatterTest extends FieldKernelTestBase {
24 public static $modules = ['file', 'image'];
29 protected $entityType;
42 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
49 protected function setUp() {
52 $this->installConfig(['field']);
53 $this->installEntitySchema('entity_test');
54 $this->installEntitySchema('file');
55 $this->installSchema('file', ['file_usage']);
57 $this->entityType = 'entity_test';
58 $this->bundle = $this->entityType;
59 $this->fieldName = Unicode::strtolower($this->randomMachineName());
61 FieldStorageConfig::create([
62 'entity_type' => $this->entityType,
63 'field_name' => $this->fieldName,
65 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
68 'entity_type' => $this->entityType,
69 'field_name' => $this->fieldName,
70 'bundle' => $this->bundle,
72 'file_extensions' => 'jpg',
76 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
77 ->setComponent($this->fieldName, [
81 $this->display->save();
85 * Tests the cache tags from image formatters.
87 public function testImageFormatterCacheTags() {
88 // Create a test entity with the image field set.
89 $entity = EntityTest::create([
90 'name' => $this->randomMachineName(),
92 $entity->{$this->fieldName}->generateSampleItems(2);
95 // Generate the render array to verify if the cache tags are as expected.
96 $build = $this->display->build($entity);
98 $this->assertEquals($entity->{$this->fieldName}[0]->entity->getCacheTags(), $build[$this->fieldName][0]['#cache']['tags'], 'First image cache tags is as expected');
99 $this->assertEquals($entity->{$this->fieldName}[1]->entity->getCacheTags(), $build[$this->fieldName][1]['#cache']['tags'], 'Second image cache tags is as expected');