ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 $defaultTheme = 'stark';
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function setUp() {
  37. parent::setUp();
  38. $web_user = $this->drupalCreateUser(['administer entity_test content']);
  39. $this->drupalLogin($web_user);
  40. // Create a field of field type "shape" with unlimited cardinality on the
  41. // entity type "entity_test".
  42. $this->entityTypeId = 'entity_test';
  43. $this->fieldName = 'shape';
  44. FieldStorageConfig::create([
  45. 'field_name' => $this->fieldName,
  46. 'entity_type' => $this->entityTypeId,
  47. 'type' => 'shape',
  48. 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  49. ])
  50. ->save();
  51. FieldConfig::create([
  52. 'entity_type' => $this->entityTypeId,
  53. 'field_name' => $this->fieldName,
  54. 'bundle' => $this->entityTypeId,
  55. 'label' => 'Shape',
  56. 'translatable' => FALSE,
  57. ])
  58. ->save();
  59. \Drupal::service('entity_display.repository')
  60. ->getFormDisplay($this->entityTypeId, $this->entityTypeId)
  61. ->setComponent($this->fieldName, ['type' => 'shape_only_color_editable_widget'])
  62. ->save();
  63. }
  64. /**
  65. * Tests the correct user input mapping on complex fields.
  66. */
  67. public function testCorrectUserInputMappingOnComplexFields() {
  68. /** @var ContentEntityStorageInterface $storage */
  69. $storage = $this->container->get('entity_type.manager')->getStorage($this->entityTypeId);
  70. /** @var ContentEntityInterface $entity */
  71. $entity = $storage->create([
  72. $this->fieldName => [
  73. ['shape' => 'rectangle', 'color' => 'green'],
  74. ['shape' => 'circle', 'color' => 'blue'],
  75. ],
  76. ]);
  77. $entity->save();
  78. $this->drupalGet($this->entityTypeId . '/manage/' . $entity->id() . '/edit');
  79. // Rearrange the field items.
  80. $edit = [
  81. "$this->fieldName[0][_weight]" => 0,
  82. "$this->fieldName[1][_weight]" => -1,
  83. ];
  84. // Executing an ajax call is important before saving as it will trigger
  85. // form state caching and so if for any reasons the form is rebuilt with
  86. // the entity built based on the user submitted values with already
  87. // reordered field items then the correct mapping will break after the form
  88. // builder maps over the new form the user submitted values based on the
  89. // previous delta ordering.
  90. //
  91. // This is how currently the form building process works and this test
  92. // ensures the correct behavior no matter what changes would be made to the
  93. // form builder or the content entity forms.
  94. $this->drupalPostForm(NULL, $edit, t('Add another item'));
  95. $this->drupalPostForm(NULL, [], t('Save'));
  96. // Reload the entity.
  97. $entity = $storage->load($entity->id());
  98. // Assert that after rearranging the field items the user input will be
  99. // mapped on the correct delta field items.
  100. $this->assertEquals($entity->get($this->fieldName)->getValue(), [
  101. ['shape' => 'circle', 'color' => 'blue'],
  102. ['shape' => 'rectangle', 'color' => 'green'],
  103. ]);
  104. }
  105. }