View source
<?php
namespace Drupal\Tests\image\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Url;
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\Tests\field\Kernel\FieldKernelTestBase;
class ImageFormatterTest extends FieldKernelTestBase {
public static $modules = [
'file',
'image',
];
protected $entityType;
protected $bundle;
protected $fieldName;
protected $display;
protected function setUp() {
parent::setUp();
$this
->installConfig([
'field',
]);
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('file');
$this
->installSchema('file', [
'file_usage',
]);
$this->entityType = 'entity_test';
$this->bundle = $this->entityType;
$this->fieldName = mb_strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'entity_type' => $this->entityType,
'field_name' => $this->fieldName,
'type' => 'image',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'entity_type' => $this->entityType,
'field_name' => $this->fieldName,
'bundle' => $this->bundle,
'settings' => [
'file_extensions' => 'jpg',
],
])
->save();
$this->display = \Drupal::service('entity_display.repository')
->getViewDisplay($this->entityType, $this->bundle)
->setComponent($this->fieldName, [
'type' => 'image',
'label' => 'hidden',
]);
$this->display
->save();
}
public function testImageFormatterCacheTags() {
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity->{$this->fieldName}
->generateSampleItems(2);
$entity
->save();
$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');
}
public function testImageFormatterSvg() {
$this
->installConfig([
'image',
]);
$renderer = $this->container
->get('renderer');
$png = File::create([
'uri' => 'public://test-image.png',
]);
$png
->save();
$png_resource = imagecreate(300, 300);
imagefill($png_resource, 0, 0, imagecolorallocate($png_resource, 0, 0, 0));
imagepng($png_resource, $png
->getFileUri());
$svg = File::create([
'uri' => 'public://test-image.svg',
]);
$svg
->save();
touch($svg
->getFileUri());
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
$this->fieldName => [
$png,
$svg,
],
]);
$entity
->save();
$component = $this->display
->getComponent($this->fieldName);
$component['settings']['image_style'] = 'medium';
$this->display
->setComponent($this->fieldName, $component)
->save();
$build = $this->display
->build($entity);
$this
->assertCacheTags($build[$this->fieldName][0], ImageStyle::load('medium')
->getCacheTags());
$renderer
->renderRoot($build[$this->fieldName][0]);
$this
->assertEquals('medium', $build[$this->fieldName][0]['#image_style']);
$this
->assertStringContainsString('styles/medium/public/test-image.png', $build[$this->fieldName][0]['#markup']);
$this
->assertStringContainsString('width="220"', $build[$this->fieldName][0]['#markup']);
$this
->assertStringContainsString('height="220"', $build[$this->fieldName][0]['#markup']);
$this
->assertCacheTags($build[$this->fieldName][1], ImageStyle::load('medium')
->getCacheTags());
$renderer
->renderRoot($build[$this->fieldName][1]);
$this
->assertEquals('medium', $build[$this->fieldName][1]['#image_style']);
$this
->assertStringNotContainsString('styles/medium/public/test-image.svg', $build[$this->fieldName][1]['#markup']);
$this
->assertStringNotContainsString('width', $build[$this->fieldName][1]['#markup']);
$this
->assertStringNotContainsString('height', $build[$this->fieldName][1]['#markup']);
}
public function testImageFormatterUrlOptions() {
$this->display
->setComponent($this->fieldName, [
'settings' => [
'image_link' => 'content',
],
]);
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity->{$this->fieldName}
->generateSampleItems(2);
$entity
->save();
$build = $this->display
->build($entity);
$this
->assertInstanceOf(Url::class, $build[$this->fieldName][0]['#url']);
$build[$this->fieldName][0]['#url']
->setOption('attributes', [
'data-attributes-test' => 'test123',
]);
$renderer = $this->container
->get('renderer');
$output = $renderer
->renderRoot($build[$this->fieldName][0]);
$this
->assertStringContainsString('<a href="' . $entity
->toUrl()
->toString() . '" data-attributes-test="test123"', (string) $output);
}
protected function assertCacheTags(array $renderable, array $cache_tags) {
$diff = array_diff($cache_tags, $renderable['#cache']['tags']);
$this
->assertEmpty($diff);
}
}