EntityDisplayBaseTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\comment\Entity\CommentType;
  4. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. use Drupal\KernelTests\KernelTestBase;
  8. /**
  9. * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayBase
  10. *
  11. * @group Entity
  12. */
  13. class EntityDisplayBaseTest extends KernelTestBase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static $modules = [
  18. 'entity_test',
  19. 'entity_test_third_party',
  20. 'field',
  21. 'system',
  22. 'comment',
  23. 'user',
  24. ];
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function setUp() {
  29. parent::setUp();
  30. $this->installEntitySchema('comment');
  31. $this->installEntitySchema('entity_test');
  32. $this->installSchema('user', ['users_data']);
  33. }
  34. /**
  35. * @covers ::preSave
  36. */
  37. public function testPreSave() {
  38. $entity_display = EntityViewDisplay::create([
  39. 'targetEntityType' => 'entity_test',
  40. 'bundle' => 'entity_test',
  41. 'mode' => 'default',
  42. 'status' => TRUE,
  43. 'content' => [
  44. 'foo' => ['type' => 'visible'],
  45. 'bar' => ['region' => 'hidden'],
  46. 'name' => ['type' => 'hidden', 'region' => 'content'],
  47. ],
  48. ]);
  49. // Ensure that no region is set on the component.
  50. $this->assertArrayNotHasKey('region', $entity_display->getComponent('foo'));
  51. // Ensure that a region is set on the component after saving.
  52. $entity_display->save();
  53. // The component with a visible type has been assigned a region.
  54. $component = $entity_display->getComponent('foo');
  55. $this->assertArrayHasKey('region', $component);
  56. $this->assertSame('content', $component['region']);
  57. $component = $entity_display->getComponent('bar');
  58. $this->assertArrayHasKey('region', $component);
  59. $this->assertSame('hidden', $component['region']);
  60. // The component with a valid region and hidden type is unchanged.
  61. $component = $entity_display->getComponent('name');
  62. $this->assertArrayHasKey('region', $component);
  63. $this->assertSame('content', $component['region']);
  64. }
  65. /**
  66. * @covers ::onDependencyRemoval
  67. */
  68. public function testOnDependencyRemoval() {
  69. // Create a comment field for entity_test.
  70. $comment_bundle = CommentType::create([
  71. 'id' => 'entity_test',
  72. 'label' => 'entity_test',
  73. 'description' => '',
  74. 'target_entity_type_id' => 'entity_test',
  75. ]);
  76. $comment_bundle->save();
  77. $comment_display = EntityViewDisplay::create([
  78. 'targetEntityType' => 'comment',
  79. 'bundle' => 'entity_test',
  80. 'mode' => 'default',
  81. 'status' => TRUE,
  82. 'third_party_settings' => [
  83. 'entity_test_third_party' => [
  84. 'key' => 'value',
  85. ],
  86. ],
  87. ]);
  88. $comment_display->save();
  89. $field_storage = FieldStorageConfig::create([
  90. 'entity_type' => 'entity_test',
  91. 'field_name' => 'test_field',
  92. 'type' => 'comment',
  93. 'settings' => [
  94. 'comment_type' => 'entity_test',
  95. ],
  96. ]);
  97. $field_storage->save();
  98. $field = FieldConfig::create([
  99. 'field_storage' => $field_storage,
  100. 'label' => $this->randomMachineName(),
  101. 'bundle' => 'entity_test',
  102. ]);
  103. $field->save();
  104. // Create an entity view display for entity_test.
  105. $entity_display = EntityViewDisplay::create([
  106. 'targetEntityType' => 'entity_test',
  107. 'bundle' => 'entity_test',
  108. 'mode' => 'default',
  109. 'status' => TRUE,
  110. 'content' => [
  111. 'test_field' => ['type' => 'comment_default', 'region' => 'content', 'settings' => ['view_mode' => 'default'], 'label' => 'hidden', 'third_party_settings' => []],
  112. ],
  113. 'third_party_settings' => [
  114. 'entity_test_third_party' => [
  115. 'key' => 'value',
  116. ],
  117. ],
  118. ]);
  119. $entity_display->save();
  120. $expected_component = [
  121. 'type' => 'comment_default',
  122. 'region' => 'content',
  123. 'settings' => ['view_mode' => 'default'],
  124. 'label' => 'hidden',
  125. 'third_party_settings' => [],
  126. ];
  127. $entity_display->getComponent('test_field');
  128. $this->assertEquals($expected_component, $entity_display->getComponent('test_field'));
  129. $expected_dependencies = [
  130. 'config' => [
  131. 'core.entity_view_display.comment.entity_test.default',
  132. 'field.field.entity_test.entity_test.test_field',
  133. ],
  134. 'module' => [
  135. 'comment',
  136. 'entity_test',
  137. 'entity_test_third_party',
  138. ],
  139. ];
  140. $this->assertSame($expected_dependencies, $entity_display->getDependencies());
  141. // Uninstall the third-party settings provider and reload the display.
  142. $this->container->get('module_installer')->uninstall(['entity_test_third_party']);
  143. $entity_display = EntityViewDisplay::load('entity_test.entity_test.default');
  144. // The component should remain unchanged.
  145. $this->assertEquals($expected_component, $entity_display->getComponent('test_field'));
  146. // The dependencies should no longer contain 'entity_test_third_party'.
  147. $expected_dependencies['module'] = [
  148. 'comment',
  149. 'entity_test',
  150. ];
  151. $this->assertSame($expected_dependencies, $entity_display->getDependencies());
  152. }
  153. }