f001743509a567f60cff26a0d102041f948c9588
[yaffs-website] / web / core / modules / image / tests / src / Kernel / ImageFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\image\Kernel;
4
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;
11
12 /**
13  * Tests the image field rendering using entity fields of the image field type.
14  *
15  * @group image
16  */
17 class ImageFormatterTest extends FieldKernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['file', 'image'];
25
26   /**
27    * @var string
28    */
29   protected $entityType;
30
31   /**
32    * @var string
33    */
34   protected $bundle;
35
36   /**
37    * @var string
38    */
39   protected $fieldName;
40
41   /**
42    * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
43    */
44   protected $display;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     $this->installConfig(['field']);
53     $this->installEntitySchema('entity_test');
54     $this->installEntitySchema('file');
55     $this->installSchema('file', ['file_usage']);
56
57     $this->entityType = 'entity_test';
58     $this->bundle = $this->entityType;
59     $this->fieldName = Unicode::strtolower($this->randomMachineName());
60
61     FieldStorageConfig::create([
62       'entity_type' => $this->entityType,
63       'field_name' => $this->fieldName,
64       'type' => 'image',
65       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
66     ])->save();
67     FieldConfig::create([
68       'entity_type' => $this->entityType,
69       'field_name' => $this->fieldName,
70       'bundle' => $this->bundle,
71       'settings' => [
72         'file_extensions' => 'jpg',
73       ],
74     ])->save();
75
76     $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
77       ->setComponent($this->fieldName, [
78         'type' => 'image',
79         'label' => 'hidden',
80       ]);
81     $this->display->save();
82   }
83
84   /**
85    * Tests the cache tags from image formatters.
86    */
87   public function testImageFormatterCacheTags() {
88     // Create a test entity with the image field set.
89     $entity = EntityTest::create([
90       'name' => $this->randomMachineName(),
91     ]);
92     $entity->{$this->fieldName}->generateSampleItems(2);
93     $entity->save();
94
95     // Generate the render array to verify if the cache tags are as expected.
96     $build = $this->display->build($entity);
97
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');
100   }
101
102 }