public function ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest::testCorrectUserInputMappingOnComplexFields in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest.php \Drupal\FunctionalTests\Entity\ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest::testCorrectUserInputMappingOnComplexFields()
Tests the correct user input mapping on complex fields.
File
- core/
tests/ Drupal/ FunctionalTests/ Entity/ ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest.php, line 79
Class
- ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest
- Tests the correct mapping of user input on the correct field delta elements.
Namespace
Drupal\FunctionalTests\EntityCode
public function testCorrectUserInputMappingOnComplexFields() {
/** @var ContentEntityStorageInterface $storage */
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
/** @var ContentEntityInterface $entity */
$entity = $storage
->create([
$this->fieldName => [
[
'shape' => 'rectangle',
'color' => 'green',
],
[
'shape' => 'circle',
'color' => 'blue',
],
],
]);
$entity
->save();
$this
->drupalGet($this->entityTypeId . '/manage/' . $entity
->id() . '/edit');
// Rearrange the field items.
$edit = [
"{$this->fieldName}[0][_weight]" => 0,
"{$this->fieldName}[1][_weight]" => -1,
];
// Executing an ajax call is important before saving as it will trigger
// form state caching and so if for any reasons the form is rebuilt with
// the entity built based on the user submitted values with already
// reordered field items then the correct mapping will break after the form
// builder maps over the new form the user submitted values based on the
// previous delta ordering.
//
// This is how currently the form building process works and this test
// ensures the correct behavior no matter what changes would be made to the
// form builder or the content entity forms.
$this
->submitForm($edit, 'Add another item');
$this
->submitForm([], 'Save');
// Reload the entity.
$entity = $storage
->load($entity
->id());
// Assert that after rearranging the field items the user input will be
// mapped on the correct delta field items.
$this
->assertEquals([
[
'shape' => 'circle',
'color' => 'blue',
],
[
'shape' => 'rectangle',
'color' => 'green',
],
], $entity
->get($this->fieldName)
->getValue());
}