ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Drupal\FunctionalTests\Entity;
  3. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  4. use Drupal\field\Entity\FieldConfig;
  5. use Drupal\field\Entity\FieldStorageConfig;
  6. use Drupal\Tests\BrowserTestBase;
  7. /**
  8. * Tests the correct mapping of user input on the correct field delta elements.
  9. *
  10. * @group Entity
  11. */
  12. class ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest extends BrowserTestBase {
  13. /**
  14. * The ID of the type of the entity under test.
  15. *
  16. * @var string
  17. */
  18. protected $entityTypeId;
  19. /**
  20. * The field name with multiple properties being test with the entity type.
  21. *
  22. * @var string
  23. */
  24. protected $fieldName;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static $modules = ['entity_test'];
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function setUp() {
  33. parent::setUp();
  34. $web_user = $this->drupalCreateUser(['administer entity_test content']);
  35. $this->drupalLogin($web_user);
  36. // Create a field of field type "shape" with unlimited cardinality on the
  37. // entity type "entity_test".
  38. $this->entityTypeId = 'entity_test';
  39. $this->fieldName = 'shape';
  40. FieldStorageConfig::create([
  41. 'field_name' => $this->fieldName,
  42. 'entity_type' => $this->entityTypeId,
  43. 'type' => 'shape',
  44. 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  45. ])
  46. ->save();
  47. FieldConfig::create([
  48. 'entity_type' => $this->entityTypeId,
  49. 'field_name' => $this->fieldName,
  50. 'bundle' => $this->entityTypeId,
  51. 'label' => 'Shape',
  52. 'translatable' => FALSE,
  53. ])
  54. ->save();
  55. entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
  56. ->setComponent($this->fieldName, ['type' => 'shape_only_color_editable_widget'])
  57. ->save();
  58. }
  59. /**
  60. * Tests the correct user input mapping on complex fields.
  61. */
  62. public function testCorrectUserInputMappingOnComplexFields() {
  63. /** @var ContentEntityStorageInterface $storage */
  64. $storage = $this->container->get('entity_type.manager')->getStorage($this->entityTypeId);
  65. /** @var ContentEntityInterface $entity */
  66. $entity = $storage->create([
  67. $this->fieldName => [
  68. ['shape' => 'rectangle', 'color' => 'green'],
  69. ['shape' => 'circle', 'color' => 'blue'],
  70. ],
  71. ]);
  72. $entity->save();
  73. $this->drupalGet($this->entityTypeId . '/manage/' . $entity->id() . '/edit');
  74. // Rearrange the field items.
  75. $edit = [
  76. "$this->fieldName[0][_weight]" => 0,
  77. "$this->fieldName[1][_weight]" => -1,
  78. ];
  79. // Executing an ajax call is important before saving as it will trigger
  80. // form state caching and so if for any reasons the form is rebuilt with
  81. // the entity built based on the user submitted values with already
  82. // reordered field items then the correct mapping will break after the form
  83. // builder maps over the new form the user submitted values based on the
  84. // previous delta ordering.
  85. //
  86. // This is how currently the form building process works and this test
  87. // ensures the correct behavior no matter what changes would be made to the
  88. // form builder or the content entity forms.
  89. $this->drupalPostForm(NULL, $edit, t('Add another item'));
  90. $this->drupalPostForm(NULL, [], t('Save'));
  91. // Reload the entity.
  92. $entity = $storage->load($entity->id());
  93. // Assert that after rearranging the field items the user input will be
  94. // mapped on the correct delta field items.
  95. $this->assertEquals($entity->get($this->fieldName)->getValue(), [
  96. ['shape' => 'circle', 'color' => 'blue'],
  97. ['shape' => 'rectangle', 'color' => 'green'],
  98. ]);
  99. }
  100. }