Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / FunctionalTests / Entity / ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest.php
1 <?php
2
3 namespace Drupal\FunctionalTests\Entity;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests the correct mapping of user input on the correct field delta elements.
12  *
13  * @group Entity
14  */
15 class ContentEntityFormCorrectUserInputMappingOnFieldDeltaElementsTest extends BrowserTestBase {
16
17   /**
18    * The ID of the type of the entity under test.
19    *
20    * @var string
21    */
22   protected $entityTypeId;
23
24   /**
25    * The field name with multiple properties being test with the entity type.
26    *
27    * @var string
28    */
29   protected $fieldName;
30
31   /**
32    * {@inheritdoc}
33    */
34   public static $modules = ['entity_test'];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41     $web_user = $this->drupalCreateUser(['administer entity_test content']);
42     $this->drupalLogin($web_user);
43
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';
48
49     FieldStorageConfig::create([
50       'field_name' => $this->fieldName,
51       'entity_type' => $this->entityTypeId,
52       'type' => 'shape',
53       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
54     ])
55       ->save();
56     FieldConfig::create([
57       'entity_type' => $this->entityTypeId,
58       'field_name' => $this->fieldName,
59       'bundle' => $this->entityTypeId,
60       'label' => 'Shape',
61       'translatable' => FALSE,
62     ])
63       ->save();
64
65     entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
66       ->setComponent($this->fieldName, ['type' => 'shape_only_color_editable_widget'])
67       ->save();
68   }
69
70   /**
71    * Tests the correct user input mapping on complex fields.
72    */
73   public function testCorrectUserInputMappingOnComplexFields() {
74     /** @var ContentEntityStorageInterface $storage */
75     $storage = $this->container->get('entity_type.manager')->getStorage($this->entityTypeId);
76
77     /** @var ContentEntityInterface $entity */
78     $entity = $storage->create([
79       $this->fieldName => [
80         ['shape' => 'rectangle', 'color' => 'green'],
81         ['shape' => 'circle', 'color' => 'blue'],
82       ],
83     ]);
84     $entity->save();
85
86     $this->drupalGet($this->entityTypeId . '/manage/' . $entity->id() . '/edit');
87
88     // Rearrange the field items.
89     $edit = [
90       "$this->fieldName[0][_weight]" => 0,
91       "$this->fieldName[1][_weight]" => -1,
92     ];
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.
99     //
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'));
105
106     // Reload the entity.
107     $entity = $storage->load($entity->id());
108
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'],
114     ]);
115   }
116
117 }