Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityDisplayBaseTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayBase
13  *
14  * @group Entity
15  */
16 class EntityDisplayBaseTest extends KernelTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['entity_test', 'entity_test_third_party', 'field', 'system', 'comment'];
22
23   /**
24    * @covers ::preSave
25    */
26   public function testPreSave() {
27     $entity_display = EntityViewDisplay::create([
28       'targetEntityType' => 'entity_test',
29       'bundle' => 'entity_test',
30       'mode' => 'default',
31       'status' => TRUE,
32       'content' => [
33         'foo' => ['type' => 'visible'],
34         'bar' => ['type' => 'hidden'],
35         'name' => ['type' => 'hidden', 'region' => 'content'],
36       ],
37     ]);
38
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'));
42
43     // Ensure that a region is set on the component after saving.
44     $entity_display->save();
45
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']);
50
51     // The component with a hidden type has been removed.
52     $this->assertNull($entity_display->getComponent('bar'));
53
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']);
58   }
59
60   /**
61    * @covers ::onDependencyRemoval
62    */
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',
68       'description' => '',
69       'target_entity_type_id' => 'entity_test',
70     ]);
71     $comment_bundle->save();
72     $comment_display = EntityViewDisplay::create([
73       'targetEntityType' => 'comment',
74       'bundle' => 'entity_test',
75       'mode' => 'default',
76       'status' => TRUE,
77       'third_party_settings' => [
78         'entity_test_third_party' => [
79           'key' => 'value',
80         ],
81       ],
82     ]);
83     $comment_display->save();
84     $field_storage = FieldStorageConfig::create([
85       'entity_type' => 'entity_test',
86       'field_name' => 'test_field',
87       'type' => 'comment',
88       'settings' => [
89         'comment_type' => 'entity_test',
90       ],
91     ]);
92     $field_storage->save();
93     $field = FieldConfig::create([
94       'field_storage' => $field_storage,
95       'label' => $this->randomMachineName(),
96       'bundle' => 'entity_test',
97     ]);
98     $field->save();
99
100     // Create an entity view display for entity_test.
101     $entity_display = EntityViewDisplay::create([
102       'targetEntityType' => 'entity_test',
103       'bundle' => 'entity_test',
104       'mode' => 'default',
105       'status' => TRUE,
106       'content' => [
107         'test_field' => ['type' => 'comment_default', 'region' => 'content', 'settings' => ['view_mode' => 'default'], 'label' => 'hidden', 'third_party_settings' => []],
108       ],
109       'third_party_settings' => [
110         'entity_test_third_party' => [
111           'key' => 'value',
112         ],
113       ],
114     ]);
115     $entity_display->save();
116
117     $expected_component = [
118       'type' => 'comment_default',
119       'region' => 'content',
120       'settings' => ['view_mode' => 'default'],
121       'label' => 'hidden',
122       'third_party_settings' => [],
123     ];
124     $entity_display->getComponent('test_field');
125     $this->assertEquals($expected_component, $entity_display->getComponent('test_field'));
126     $expected_dependencies = [
127       'config' => [
128         'core.entity_view_display.comment.entity_test.default',
129         'field.field.entity_test.entity_test.test_field',
130       ],
131       'module' => [
132         'comment',
133         'entity_test',
134         'entity_test_third_party',
135       ],
136     ];
137     $this->assertSame($expected_dependencies, $entity_display->getDependencies());
138
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');
142
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'] = [
147       'comment',
148       'entity_test',
149     ];
150     $this->assertSame($expected_dependencies, $entity_display->getDependencies());
151   }
152
153 }