3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\KernelTests\KernelTestBase;
12 * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayBase
16 class EntityDisplayBaseTest extends KernelTestBase {
21 public static $modules = ['entity_test', 'entity_test_third_party', 'field', 'system', 'comment'];
26 public function testPreSave() {
27 $entity_display = EntityViewDisplay::create([
28 'targetEntityType' => 'entity_test',
29 'bundle' => 'entity_test',
33 'foo' => ['type' => 'visible'],
34 'bar' => ['type' => 'hidden'],
35 'name' => ['type' => 'hidden', 'region' => 'content'],
39 // Ensure that no region is set on the component.
40 $this->assertArrayNotHasKey('region', $entity_display->getComponent('foo'));
41 $this->assertArrayNotHasKey('region', $entity_display->getComponent('bar'));
43 // Ensure that a region is set on the component after saving.
44 $entity_display->save();
46 // The component with a visible type has been assigned a region.
47 $component = $entity_display->getComponent('foo');
48 $this->assertArrayHasKey('region', $component);
49 $this->assertSame('content', $component['region']);
51 // The component with a hidden type has been removed.
52 $this->assertNull($entity_display->getComponent('bar'));
54 // The component with a valid region and hidden type is unchanged.
55 $component = $entity_display->getComponent('name');
56 $this->assertArrayHasKey('region', $component);
57 $this->assertSame('content', $component['region']);
61 * @covers ::onDependencyRemoval
63 public function testOnDependencyRemoval() {
64 // Create a comment field for entity_test.
65 $comment_bundle = CommentType::create([
66 'id' => 'entity_test',
67 'label' => 'entity_test',
69 'target_entity_type_id' => 'entity_test',
71 $comment_bundle->save();
72 $comment_display = EntityViewDisplay::create([
73 'targetEntityType' => 'comment',
74 'bundle' => 'entity_test',
77 'third_party_settings' => [
78 'entity_test_third_party' => [
83 $comment_display->save();
84 $field_storage = FieldStorageConfig::create([
85 'entity_type' => 'entity_test',
86 'field_name' => 'test_field',
89 'comment_type' => 'entity_test',
92 $field_storage->save();
93 $field = FieldConfig::create([
94 'field_storage' => $field_storage,
95 'label' => $this->randomMachineName(),
96 'bundle' => 'entity_test',
100 // Create an entity view display for entity_test.
101 $entity_display = EntityViewDisplay::create([
102 'targetEntityType' => 'entity_test',
103 'bundle' => 'entity_test',
107 'test_field' => ['type' => 'comment_default', 'region' => 'content', 'settings' => ['view_mode' => 'default'], 'label' => 'hidden', 'third_party_settings' => []],
109 'third_party_settings' => [
110 'entity_test_third_party' => [
115 $entity_display->save();
117 $expected_component = [
118 'type' => 'comment_default',
119 'region' => 'content',
120 'settings' => ['view_mode' => 'default'],
122 'third_party_settings' => [],
124 $entity_display->getComponent('test_field');
125 $this->assertEquals($expected_component, $entity_display->getComponent('test_field'));
126 $expected_dependencies = [
128 'core.entity_view_display.comment.entity_test.default',
129 'field.field.entity_test.entity_test.test_field',
134 'entity_test_third_party',
137 $this->assertSame($expected_dependencies, $entity_display->getDependencies());
139 // Uninstall the third-party settings provider and reload the display.
140 $this->container->get('module_installer')->uninstall(['entity_test_third_party']);
141 $entity_display = EntityViewDisplay::load('entity_test.entity_test.default');
143 // The component should remain unchanged.
144 $this->assertEquals($expected_component, $entity_display->getComponent('test_field'));
145 // The dependencies should no longer contain 'entity_test_third_party'.
146 $expected_dependencies['module'] = [
150 $this->assertSame($expected_dependencies, $entity_display->getDependencies());