View source
<?php
namespace Drupal\Tests\image\Kernel;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
class ImageStyleIntegrationTest extends KernelTestBase {
protected static $modules = [
'image',
'file',
'field',
'system',
'user',
'node',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('node');
}
public function testEntityDisplayDependency() {
$style = ImageStyle::create([
'name' => 'main_style',
]);
$style
->save();
$replacement = ImageStyle::create([
'name' => 'replacement_style',
]);
$replacement
->save();
$node_type = NodeType::create([
'type' => 'note',
]);
$node_type
->save();
FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'sticker',
'type' => 'image',
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'field_name' => 'sticker',
'bundle' => 'note',
])
->save();
$view_display = EntityViewDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'note',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('sticker', [
'settings' => [
'image_style' => 'main_style',
],
]);
$view_display
->save();
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'note',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('sticker', [
'settings' => [
'preview_image_style' => 'main_style',
],
]);
$form_display
->save();
$this
->assertNotNull(EntityViewDisplay::load($view_display
->id()));
$this
->assertNotNull(EntityFormDisplay::load($form_display
->id()));
$storage = $this->container
->get('entity_type.manager')
->getStorage($style
->getEntityTypeId());
$storage
->setReplacementId('main_style', 'replacement_style');
$style
->delete();
$this
->assertNotNull($view_display = EntityViewDisplay::load($view_display
->id()));
$this
->assertNotNull($form_display = EntityFormDisplay::load($form_display
->id()));
$this
->assertNotNull($formatter = $view_display
->getComponent('sticker'));
$this
->assertNotNull($widget = $form_display
->getComponent('sticker'));
$this
->assertSame('replacement_style', $formatter['settings']['image_style']);
$this
->assertSame('replacement_style', $widget['settings']['preview_image_style']);
$replacement
->delete();
$this
->assertNotNull($view_display = EntityViewDisplay::load($view_display
->id()));
$this
->assertNotNull($form_display = EntityFormDisplay::load($form_display
->id()));
$this
->assertNull($view_display
->getComponent('sticker'));
$this
->assertTrue($view_display
->get('hidden')['sticker']);
$this
->assertNotNull($widget = $form_display
->getComponent('sticker'));
$this
->assertSame('', $widget['settings']['preview_image_style']);
}
public function testEntityDisplayDependencyRename() {
$style = ImageStyle::create([
'name' => 'main_style',
]);
$style
->save();
$node_type = NodeType::create([
'type' => 'note',
]);
$node_type
->save();
FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'sticker',
'type' => 'image',
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'field_name' => 'sticker',
'bundle' => 'note',
])
->save();
$view_display = EntityViewDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'note',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('sticker', [
'settings' => [
'image_style' => 'main_style',
],
]);
$view_display
->save();
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'note',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('sticker', [
'settings' => [
'preview_image_style' => 'main_style',
],
]);
$form_display
->save();
$this
->assertNotNull(EntityViewDisplay::load($view_display
->id()));
$this
->assertNotNull(EntityFormDisplay::load($form_display
->id()));
$style
->setName('main_style_renamed');
$style
->save();
$this
->assertNotNull($view_display = EntityViewDisplay::load($view_display
->id()));
$this
->assertNotNull($form_display = EntityFormDisplay::load($form_display
->id()));
$this
->assertNotNull($formatter = $view_display
->getComponent('sticker'));
$this
->assertNotNull($widget = $form_display
->getComponent('sticker'));
$this
->assertSame('main_style_renamed', $formatter['settings']['image_style']);
$this
->assertSame('main_style_renamed', $widget['settings']['preview_image_style']);
}
}