You are here

public function ImageFormatterTest::testImageFormatterCacheTags in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/image/tests/src/Kernel/ImageFormatterTest.php \Drupal\Tests\image\Kernel\ImageFormatterTest::testImageFormatterCacheTags()

Tests the cache tags from image formatters.

File

core/modules/image/tests/src/Kernel/ImageFormatterTest.php, line 90

Class

ImageFormatterTest
Tests the image field rendering using entity fields of the image field type.

Namespace

Drupal\Tests\image\Kernel

Code

public function testImageFormatterCacheTags() {

  // Create a test entity with the image field set.
  $entity = EntityTest::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $entity->{$this->fieldName}
    ->generateSampleItems(2);
  $entity
    ->save();

  // Generate the render array to verify if the cache tags are as expected.
  $build = $this->display
    ->build($entity);
  $this
    ->assertEquals($entity->{$this->fieldName}[0]->entity
    ->getCacheTags(), $build[$this->fieldName][0]['#cache']['tags'], 'First image cache tags is as expected');
  $this
    ->assertEquals($entity->{$this->fieldName}[1]->entity
    ->getCacheTags(), $build[$this->fieldName][1]['#cache']['tags'], 'Second image cache tags is as expected');
}