View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\comment\Entity\CommentType;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
class EntityDisplayBaseTest extends KernelTestBase {
protected static $modules = [
'entity_test',
'entity_test_third_party',
'field',
'system',
'comment',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('comment');
$this
->installEntitySchema('entity_test');
$this
->installSchema('user', [
'users_data',
]);
}
public function testPreSave() {
$entity_display = EntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
'content' => [
'foo' => [
'type' => 'visible',
],
'bar' => [
'region' => 'hidden',
],
'name' => [
'type' => 'hidden',
'region' => 'content',
],
],
]);
$this
->assertArrayNotHasKey('region', $entity_display
->getComponent('foo'));
$entity_display
->save();
$component = $entity_display
->getComponent('foo');
$this
->assertArrayHasKey('region', $component);
$this
->assertSame('content', $component['region']);
$component = $entity_display
->getComponent('bar');
$this
->assertArrayHasKey('region', $component);
$this
->assertSame('hidden', $component['region']);
$component = $entity_display
->getComponent('name');
$this
->assertArrayHasKey('region', $component);
$this
->assertSame('content', $component['region']);
}
public function testOnDependencyRemoval() {
$comment_bundle = CommentType::create([
'id' => 'entity_test',
'label' => 'entity_test',
'description' => '',
'target_entity_type_id' => 'entity_test',
]);
$comment_bundle
->save();
$comment_display = EntityViewDisplay::create([
'targetEntityType' => 'comment',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
'third_party_settings' => [
'entity_test_third_party' => [
'key' => 'value',
],
],
]);
$comment_display
->save();
$field_storage = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'test_field',
'type' => 'comment',
'settings' => [
'comment_type' => 'entity_test',
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'label' => $this
->randomMachineName(),
'bundle' => 'entity_test',
]);
$field
->save();
$entity_display = EntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
'content' => [
'test_field' => [
'type' => 'comment_default',
'region' => 'content',
'settings' => [
'view_mode' => 'default',
],
'label' => 'hidden',
'third_party_settings' => [],
],
],
'third_party_settings' => [
'entity_test_third_party' => [
'key' => 'value',
],
],
]);
$entity_display
->save();
$expected_component = [
'type' => 'comment_default',
'region' => 'content',
'settings' => [
'view_mode' => 'default',
],
'label' => 'hidden',
'third_party_settings' => [],
];
$entity_display
->getComponent('test_field');
$this
->assertEquals($expected_component, $entity_display
->getComponent('test_field'));
$expected_dependencies = [
'config' => [
'core.entity_view_display.comment.entity_test.default',
'field.field.entity_test.entity_test.test_field',
],
'module' => [
'comment',
'entity_test',
'entity_test_third_party',
],
];
$this
->assertSame($expected_dependencies, $entity_display
->getDependencies());
$this->container
->get('module_installer')
->uninstall([
'entity_test_third_party',
]);
$entity_display = EntityViewDisplay::load('entity_test.entity_test.default');
$this
->assertEquals($expected_component, $entity_display
->getComponent('test_field'));
$expected_dependencies['module'] = [
'comment',
'entity_test',
];
$this
->assertSame($expected_dependencies, $entity_display
->getDependencies());
}
}