EntityDisplayFormBaseTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\Core\Entity\Display\EntityDisplayInterface;
  4. use Drupal\Core\Form\FormState;
  5. use Drupal\field_ui\Form\EntityViewDisplayEditForm;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * @coversDefaultClass \Drupal\field_ui\Form\EntityDisplayFormBase
  9. *
  10. * @group Entity
  11. */
  12. class EntityDisplayFormBaseTest extends KernelTestBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public static $modules = ['entity_test'];
  17. /**
  18. * @covers ::copyFormValuesToEntity
  19. */
  20. public function testCopyFormValuesToEntity() {
  21. $field_values = [];
  22. $entity = $this->prophesize(EntityDisplayInterface::class);
  23. $entity->getPluginCollections()->willReturn([]);
  24. $entity->getTargetEntityTypeId()->willReturn('entity_test_with_bundle');
  25. $entity->getTargetBundle()->willReturn('target_bundle');
  26. // An initially hidden field, with a submitted region change.
  27. $entity->getComponent('new_field_mismatch_type_visible')->willReturn([]);
  28. $field_values['new_field_mismatch_type_visible'] = [
  29. 'weight' => 0,
  30. 'type' => 'textfield',
  31. 'region' => 'hidden',
  32. ];
  33. $entity->removeComponent('new_field_mismatch_type_visible')
  34. ->will(function ($args) {
  35. // On subsequent calls, getComponent() will return an empty array.
  36. $this->getComponent($args[0])->willReturn([]);
  37. })
  38. ->shouldBeCalled();
  39. // An initially visible field, with identical submitted values.
  40. $entity->getComponent('field_visible_no_changes')
  41. ->willReturn([
  42. 'weight' => 0,
  43. 'type' => 'textfield',
  44. 'region' => 'content',
  45. ]);
  46. $field_values['field_visible_no_changes'] = [
  47. 'weight' => 0,
  48. 'type' => 'textfield',
  49. 'region' => 'content',
  50. ];
  51. $entity
  52. ->setComponent('field_visible_no_changes', [
  53. 'weight' => 0,
  54. 'type' => 'textfield',
  55. 'region' => 'content',
  56. ])
  57. ->shouldBeCalled();
  58. // An initially visible field, with a submitted region change.
  59. $entity->getComponent('field_start_visible_change_region')
  60. ->willReturn([
  61. 'weight' => 0,
  62. 'type' => 'textfield',
  63. 'region' => 'content',
  64. ]);
  65. $field_values['field_start_visible_change_region'] = [
  66. 'weight' => 0,
  67. 'type' => 'textfield',
  68. 'region' => 'hidden',
  69. ];
  70. $entity->removeComponent('field_start_visible_change_region')
  71. ->will(function ($args) {
  72. // On subsequent calls, getComponent() will return an empty array.
  73. $this->getComponent($args[0])->willReturn([]);
  74. })
  75. ->shouldBeCalled();
  76. // A field that is flagged for plugin settings update on the second build.
  77. $entity->getComponent('field_plugin_settings_update')
  78. ->willReturn([
  79. 'weight' => 0,
  80. 'type' => 'textfield',
  81. 'region' => 'content',
  82. ]);
  83. $field_values['field_plugin_settings_update'] = [
  84. 'weight' => 0,
  85. 'type' => 'textfield',
  86. 'region' => 'content',
  87. 'settings_edit_form' => [
  88. 'third_party_settings' => [
  89. 'foo' => 'bar',
  90. ],
  91. ],
  92. ];
  93. $entity
  94. ->setComponent('field_plugin_settings_update', [
  95. 'weight' => 0,
  96. 'type' => 'textfield',
  97. 'region' => 'content',
  98. ])
  99. ->will(function ($args) {
  100. // On subsequent calls, getComponent() will return the newly set values.
  101. $this->getComponent($args[0])->willReturn($args[1]);
  102. $args[1] += [
  103. 'settings' => [],
  104. 'third_party_settings' => [
  105. 'foo' => 'bar',
  106. ],
  107. ];
  108. $this->setComponent($args[0], $args[1])->shouldBeCalled();
  109. })
  110. ->shouldBeCalled();
  111. $form_object = new EntityViewDisplayEditForm($this->container->get('plugin.manager.field.field_type'), $this->container->get('plugin.manager.field.formatter'));
  112. $form_object->setEntityManager($this->container->get('entity.manager'));
  113. $form_object->setEntity($entity->reveal());
  114. $form = [
  115. '#fields' => array_keys($field_values),
  116. '#extra' => [],
  117. ];
  118. $form_state = new FormState();
  119. $form_state->setValues(['fields' => $field_values]);
  120. $form_state->setProcessInput();
  121. $form_object->buildEntity($form, $form_state);
  122. $form_state->setSubmitted();
  123. // Flag one field for updating plugin settings.
  124. $form_state->set('plugin_settings_update', 'field_plugin_settings_update');
  125. // During form submission, buildEntity() will be called twice. Simulate that
  126. // here to prove copyFormValuesToEntity() is idempotent.
  127. $form_object->buildEntity($form, $form_state);
  128. }
  129. }