/web/core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayFormBaseTest.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 147 lines · 112 code · 14 blank · 21 comment · 0 complexity · fbcc8d815573eb008d2a7ce5d090b7be MD5 · raw file

  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. protected 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(
  112. $this->container->get('plugin.manager.field.field_type'),
  113. $this->container->get('plugin.manager.field.formatter'),
  114. $this->container->get('entity_display.repository'),
  115. $this->container->get('entity_field.manager')
  116. );
  117. $form_object->setEntity($entity->reveal());
  118. $form = [
  119. '#fields' => array_keys($field_values),
  120. '#extra' => [],
  121. ];
  122. $form_state = new FormState();
  123. $form_state->setValues(['fields' => $field_values]);
  124. $form_state->setProcessInput();
  125. $form_object->buildEntity($form, $form_state);
  126. $form_state->setSubmitted();
  127. // Flag one field for updating plugin settings.
  128. $form_state->set('plugin_settings_update', 'field_plugin_settings_update');
  129. // During form submission, buildEntity() will be called twice. Simulate that
  130. // here to prove copyFormValuesToEntity() is idempotent.
  131. $form_object->buildEntity($form, $form_state);
  132. }
  133. }