function ImageFormatterTest::testImageFormatterCacheTags

Same name and namespace in other branches
  1. 9 core/modules/image/tests/src/Kernel/ImageFormatterTest.php \Drupal\Tests\image\Kernel\ImageFormatterTest::testImageFormatterCacheTags()
  2. 10 core/modules/image/tests/src/Kernel/ImageFormatterTest.php \Drupal\Tests\image\Kernel\ImageFormatterTest::testImageFormatterCacheTags()
  3. 11.x 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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.