3 namespace Drupal\FunctionalTests\Entity;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the correct mapping of user input on the correct field delta elements.
15 class ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest extends BrowserTestBase {
18 * The ID of the type of the entity under test.
22 protected $entityTypeId;
25 * The field name with multiple properties being test with the entity type.
34 public static $modules = ['entity_test'];
39 protected function setUp() {
41 $web_user = $this->drupalCreateUser(['administer entity_test content']);
42 $this->drupalLogin($web_user);
44 // Create a field of field type "shape" with unlimited cardinality on the
45 // entity type "entity_test".
46 $this->entityTypeId = 'entity_test';
47 $this->fieldName = 'shape';
49 FieldStorageConfig::create([
50 'field_name' => $this->fieldName,
51 'entity_type' => $this->entityTypeId,
53 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
57 'entity_type' => $this->entityTypeId,
58 'field_name' => $this->fieldName,
59 'bundle' => $this->entityTypeId,
61 'translatable' => FALSE,
65 entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
66 ->setComponent($this->fieldName, ['type' => 'shape_only_color_editable_widget'])
71 * Tests the correct user input mapping on complex fields.
73 public function testCorrectUserInputMappingOnComplexFields() {
74 /** @var ContentEntityStorageInterface $storage */
75 $storage = $this->container->get('entity_type.manager')->getStorage($this->entityTypeId);
77 /** @var ContentEntityInterface $entity */
78 $entity = $storage->create([
80 ['shape' => 'rectangle', 'color' => 'green'],
81 ['shape' => 'circle', 'color' => 'blue'],
86 $this->drupalGet($this->entityTypeId . '/manage/' . $entity->id() . '/edit');
88 // Rearrange the field items.
90 "$this->fieldName[0][_weight]" => 0,
91 "$this->fieldName[1][_weight]" => -1,
93 // Executing an ajax call is important before saving as it will trigger
94 // form state caching and so if for any reasons the form is rebuilt with
95 // the entity built based on the user submitted values with already
96 // reordered field items then the correct mapping will break after the form
97 // builder maps over the new form the user submitted values based on the
98 // previous delta ordering.
100 // This is how currently the form building process works and this test
101 // ensures the correct behavior no matter what changes would be made to the
102 // form builder or the content entity forms.
103 $this->drupalPostForm(NULL, $edit, t('Add another item'));
104 $this->drupalPostForm(NULL, [], t('Save'));
106 // Reload the entity.
107 $entity = $storage->load($entity->id());
109 // Assert that after rearranging the field items the user input will be
110 // mapped on the correct delta field items.
111 $this->assertEquals($entity->get($this->fieldName)->getValue(), [
112 ['shape' => 'circle', 'color' => 'blue'],
113 ['shape' => 'rectangle', 'color' => 'green'],