3 namespace Drupal\field\Tests;
4 use Drupal\Core\Field\FieldStorageDefinitionInterface;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
9 * Tests field elements in nested forms.
13 class NestedFormTest extends FieldTestBase {
20 public static $modules = ['field_test', 'entity_test'];
22 protected function setUp() {
25 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
26 $this->drupalLogin($web_user);
28 $this->fieldStorageSingle = [
29 'field_name' => 'field_single',
30 'entity_type' => 'entity_test',
31 'type' => 'test_field',
33 $this->fieldStorageUnlimited = [
34 'field_name' => 'field_unlimited',
35 'entity_type' => 'entity_test',
36 'type' => 'test_field',
37 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
41 'entity_type' => 'entity_test',
42 'bundle' => 'entity_test',
43 'label' => $this->randomMachineName() . '_label',
44 'description' => '[site:name]_description',
45 'weight' => mt_rand(0, 127),
47 'test_field_setting' => $this->randomMachineName(),
53 * Tests Field API form integration within a subform.
55 public function testNestedFieldForm() {
56 // Add two fields on the 'entity_test'
57 FieldStorageConfig::create($this->fieldStorageSingle)->save();
58 FieldStorageConfig::create($this->fieldStorageUnlimited)->save();
59 $this->field['field_name'] = 'field_single';
60 $this->field['label'] = 'Single field';
61 FieldConfig::create($this->field)->save();
62 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
63 ->setComponent($this->field['field_name'])
65 $this->field['field_name'] = 'field_unlimited';
66 $this->field['label'] = 'Unlimited field';
67 FieldConfig::create($this->field)->save();
68 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
69 ->setComponent($this->field['field_name'])
72 // Create two entities.
73 $entity_type = 'entity_test';
74 $storage = $this->container->get('entity_type.manager')
75 ->getStorage($entity_type);
77 $entity_1 = $storage->create(['id' => 1]);
78 $entity_1->enforceIsNew();
79 $entity_1->field_single->value = 0;
80 $entity_1->field_unlimited->value = 1;
83 $entity_2 = $storage->create(['id' => 2]);
84 $entity_2->enforceIsNew();
85 $entity_2->field_single->value = 10;
86 $entity_2->field_unlimited->value = 11;
89 // Display the 'combined form'.
90 $this->drupalGet('test-entity/nested/1/2');
91 $this->assertFieldByName('field_single[0][value]', 0, 'Entity 1: field_single value appears correctly is the form.');
92 $this->assertFieldByName('field_unlimited[0][value]', 1, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
93 $this->assertFieldByName('entity_2[field_single][0][value]', 10, 'Entity 2: field_single value appears correctly is the form.');
94 $this->assertFieldByName('entity_2[field_unlimited][0][value]', 11, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
96 // Submit the form and check that the entities are updated accordingly.
98 'field_single[0][value]' => 1,
99 'field_unlimited[0][value]' => 2,
100 'field_unlimited[1][value]' => 3,
101 'entity_2[field_single][0][value]' => 11,
102 'entity_2[field_unlimited][0][value]' => 12,
103 'entity_2[field_unlimited][1][value]' => 13,
105 $this->drupalPostForm(NULL, $edit, t('Save'));
106 $entity_1 = $storage->load(1);
107 $entity_2 = $storage->load(2);
108 $this->assertFieldValues($entity_1, 'field_single', [1]);
109 $this->assertFieldValues($entity_1, 'field_unlimited', [2, 3]);
110 $this->assertFieldValues($entity_2, 'field_single', [11]);
111 $this->assertFieldValues($entity_2, 'field_unlimited', [12, 13]);
113 // Submit invalid values and check that errors are reported on the
116 'field_unlimited[1][value]' => -1,
118 $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
119 $this->assertRaw(t('%label does not accept the value -1', ['%label' => 'Unlimited field']), 'Entity 1: the field validation error was reported.');
120 $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', [':id' => 'edit-field-unlimited-1-value']);
121 $this->assertTrue($error_field, 'Entity 1: the error was flagged on the correct element.');
123 'entity_2[field_unlimited][1][value]' => -1,
125 $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
126 $this->assertRaw(t('%label does not accept the value -1', ['%label' => 'Unlimited field']), 'Entity 2: the field validation error was reported.');
127 $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', [':id' => 'edit-entity-2-field-unlimited-1-value']);
128 $this->assertTrue($error_field, 'Entity 2: the error was flagged on the correct element.');
130 // Test that reordering works on both entities.
132 'field_unlimited[0][_weight]' => 0,
133 'field_unlimited[1][_weight]' => -1,
134 'entity_2[field_unlimited][0][_weight]' => 0,
135 'entity_2[field_unlimited][1][_weight]' => -1,
137 $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
138 $this->assertFieldValues($entity_1, 'field_unlimited', [3, 2]);
139 $this->assertFieldValues($entity_2, 'field_unlimited', [13, 12]);
141 // Test the 'add more' buttons. Only Ajax submission is tested, because
142 // the two 'add more' buttons present in the form have the same #value,
143 // which confuses drupalPostForm().
144 // 'Add more' button in the first entity:
145 $this->drupalGet('test-entity/nested/1/2');
146 $this->drupalPostAjaxForm(NULL, [], 'field_unlimited_add_more');
147 $this->assertFieldByName('field_unlimited[0][value]', 3, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
148 $this->assertFieldByName('field_unlimited[1][value]', 2, 'Entity 1: field_unlimited value 1 appears correctly is the form.');
149 $this->assertFieldByName('field_unlimited[2][value]', '', 'Entity 1: field_unlimited value 2 appears correctly is the form.');
150 $this->assertFieldByName('field_unlimited[3][value]', '', 'Entity 1: an empty widget was added for field_unlimited value 3.');
151 // 'Add more' button in the first entity (changing field values):
153 'entity_2[field_unlimited][0][value]' => 13,
154 'entity_2[field_unlimited][1][value]' => 14,
155 'entity_2[field_unlimited][2][value]' => 15,
157 $this->drupalPostAjaxForm(NULL, $edit, 'entity_2_field_unlimited_add_more');
158 $this->assertFieldByName('entity_2[field_unlimited][0][value]', 13, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
159 $this->assertFieldByName('entity_2[field_unlimited][1][value]', 14, 'Entity 2: field_unlimited value 1 appears correctly is the form.');
160 $this->assertFieldByName('entity_2[field_unlimited][2][value]', 15, 'Entity 2: field_unlimited value 2 appears correctly is the form.');
161 $this->assertFieldByName('entity_2[field_unlimited][3][value]', '', 'Entity 2: an empty widget was added for field_unlimited value 3.');
162 // Save the form and check values are saved correctly.
163 $this->drupalPostForm(NULL, [], t('Save'));
164 $this->assertFieldValues($entity_1, 'field_unlimited', [3, 2]);
165 $this->assertFieldValues($entity_2, 'field_unlimited', [13, 14, 15]);
169 * Tests entity level validation within subforms.
171 public function testNestedEntityFormEntityLevelValidation() {
172 // Create two entities.
173 $storage = $this->container->get('entity_type.manager')
174 ->getStorage('entity_test_constraints');
176 $entity_1 = $storage->create();
179 $entity_2 = $storage->create();
182 // Display the 'combined form'.
183 $this->drupalGet("test-entity-constraints/nested/{$entity_1->id()}/{$entity_2->id()}");
184 $this->assertFieldByName('entity_2[changed]', 0, 'Entity 2: changed value appears correctly in the form.');
186 // Submit the form and check that the entities are updated accordingly.
187 $edit = ['entity_2[changed]' => REQUEST_TIME - 86400];
188 $this->drupalPostForm(NULL, $edit, t('Save'));
190 $elements = $this->cssSelect('.entity-2.error');
191 $this->assertEqual(1, count($elements), 'The whole nested entity form has been correctly flagged with an error class.');