3 namespace Drupal\Tests\field\Functional;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Tests field elements in nested forms.
14 class NestedFormTest extends FieldTestBase {
21 public static $modules = ['field_test', 'entity_test'];
23 protected function setUp() {
26 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
27 $this->drupalLogin($web_user);
29 $this->fieldStorageSingle = [
30 'field_name' => 'field_single',
31 'entity_type' => 'entity_test',
32 'type' => 'test_field',
34 $this->fieldStorageUnlimited = [
35 'field_name' => 'field_unlimited',
36 'entity_type' => 'entity_test',
37 'type' => 'test_field',
38 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
42 'entity_type' => 'entity_test',
43 'bundle' => 'entity_test',
44 'label' => $this->randomMachineName() . '_label',
45 'description' => '[site:name]_description',
46 'weight' => mt_rand(0, 127),
48 'test_field_setting' => $this->randomMachineName(),
54 * Tests Field API form integration within a subform.
56 public function testNestedFieldForm() {
57 // Add two fields on the 'entity_test'
58 FieldStorageConfig::create($this->fieldStorageSingle)->save();
59 FieldStorageConfig::create($this->fieldStorageUnlimited)->save();
60 $this->field['field_name'] = 'field_single';
61 $this->field['label'] = 'Single field';
62 FieldConfig::create($this->field)->save();
63 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
64 ->setComponent($this->field['field_name'])
66 $this->field['field_name'] = 'field_unlimited';
67 $this->field['label'] = 'Unlimited field';
68 FieldConfig::create($this->field)->save();
69 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
70 ->setComponent($this->field['field_name'])
73 // Create two entities.
74 $entity_type = 'entity_test';
75 $storage = $this->container->get('entity_type.manager')
76 ->getStorage($entity_type);
78 $entity_1 = $storage->create(['id' => 1]);
79 $entity_1->enforceIsNew();
80 $entity_1->field_single->value = 0;
81 $entity_1->field_unlimited->value = 1;
84 $entity_2 = $storage->create(['id' => 2]);
85 $entity_2->enforceIsNew();
86 $entity_2->field_single->value = 10;
87 $entity_2->field_unlimited->value = 11;
90 // Display the 'combined form'.
91 $this->drupalGet('test-entity/nested/1/2');
92 $this->assertFieldByName('field_single[0][value]', 0, 'Entity 1: field_single value appears correctly is the form.');
93 $this->assertFieldByName('field_unlimited[0][value]', 1, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
94 $this->assertFieldByName('entity_2[field_single][0][value]', 10, 'Entity 2: field_single value appears correctly is the form.');
95 $this->assertFieldByName('entity_2[field_unlimited][0][value]', 11, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
97 // Submit the form and check that the entities are updated accordingly.
99 'field_single[0][value]' => 1,
100 'field_unlimited[0][value]' => 2,
101 'field_unlimited[1][value]' => 3,
102 'entity_2[field_single][0][value]' => 11,
103 'entity_2[field_unlimited][0][value]' => 12,
104 'entity_2[field_unlimited][1][value]' => 13,
106 $this->drupalPostForm(NULL, $edit, t('Save'));
107 $entity_1 = $storage->load(1);
108 $entity_2 = $storage->load(2);
109 $this->assertFieldValues($entity_1, 'field_single', [1]);
110 $this->assertFieldValues($entity_1, 'field_unlimited', [2, 3]);
111 $this->assertFieldValues($entity_2, 'field_single', [11]);
112 $this->assertFieldValues($entity_2, 'field_unlimited', [12, 13]);
114 // Submit invalid values and check that errors are reported on the
117 'field_unlimited[1][value]' => -1,
119 $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
120 $this->assertRaw(t('%label does not accept the value -1', ['%label' => 'Unlimited field']), 'Entity 1: the field validation error was reported.');
121 $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', [':id' => 'edit-field-unlimited-1-value']);
122 $this->assertTrue($error_field, 'Entity 1: the error was flagged on the correct element.');
124 'entity_2[field_unlimited][1][value]' => -1,
126 $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
127 $this->assertRaw(t('%label does not accept the value -1', ['%label' => 'Unlimited field']), 'Entity 2: the field validation error was reported.');
128 $error_field = $this->xpath('//input[@id=:id and contains(@class, "error")]', [':id' => 'edit-entity-2-field-unlimited-1-value']);
129 $this->assertTrue($error_field, 'Entity 2: the error was flagged on the correct element.');
131 // Test that reordering works on both entities.
133 'field_unlimited[0][_weight]' => 0,
134 'field_unlimited[1][_weight]' => -1,
135 'entity_2[field_unlimited][0][_weight]' => 0,
136 'entity_2[field_unlimited][1][_weight]' => -1,
138 $this->drupalPostForm('test-entity/nested/1/2', $edit, t('Save'));
139 $this->assertFieldValues($entity_1, 'field_unlimited', [3, 2]);
140 $this->assertFieldValues($entity_2, 'field_unlimited', [13, 12]);
142 // Test the 'add more' buttons.
143 // 'Add more' button in the first entity:
144 $this->drupalGet('test-entity/nested/1/2');
145 $this->drupalPostForm(NULL, [], 'field_unlimited_add_more');
146 $this->assertFieldByName('field_unlimited[0][value]', 3, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
147 $this->assertFieldByName('field_unlimited[1][value]', 2, 'Entity 1: field_unlimited value 1 appears correctly is the form.');
148 $this->assertFieldByName('field_unlimited[2][value]', '', 'Entity 1: field_unlimited value 2 appears correctly is the form.');
149 $this->assertFieldByName('field_unlimited[3][value]', '', 'Entity 1: an empty widget was added for field_unlimited value 3.');
150 // 'Add more' button in the first entity (changing field values):
152 'entity_2[field_unlimited][0][value]' => 13,
153 'entity_2[field_unlimited][1][value]' => 14,
154 'entity_2[field_unlimited][2][value]' => 15,
156 $this->drupalPostForm(NULL, $edit, 'entity_2_field_unlimited_add_more');
157 $this->assertFieldByName('entity_2[field_unlimited][0][value]', 13, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
158 $this->assertFieldByName('entity_2[field_unlimited][1][value]', 14, 'Entity 2: field_unlimited value 1 appears correctly is the form.');
159 $this->assertFieldByName('entity_2[field_unlimited][2][value]', 15, 'Entity 2: field_unlimited value 2 appears correctly is the form.');
160 $this->assertFieldByName('entity_2[field_unlimited][3][value]', '', 'Entity 2: an empty widget was added for field_unlimited value 3.');
161 // Save the form and check values are saved correctly.
162 $this->drupalPostForm(NULL, [], t('Save'));
163 $this->assertFieldValues($entity_1, 'field_unlimited', [3, 2]);
164 $this->assertFieldValues($entity_2, 'field_unlimited', [13, 14, 15]);
168 * Tests entity level validation within subforms.
170 public function testNestedEntityFormEntityLevelValidation() {
171 // Create two entities.
172 $storage = $this->container->get('entity_type.manager')
173 ->getStorage('entity_test_constraints');
175 $entity_1 = $storage->create();
178 $entity_2 = $storage->create();
181 $page = $this->getSession()->getPage();
182 $assert_session = $this->assertSession();
184 // Display the 'combined form'.
185 $this->drupalGet("test-entity-constraints/nested/{$entity_1->id()}/{$entity_2->id()}");
186 $assert_session->hiddenFieldValueEquals('entity_2[changed]', REQUEST_TIME);
188 // Submit the form and check that the entities are updated accordingly.
189 $assert_session->hiddenFieldExists('entity_2[changed]')
190 ->setValue(REQUEST_TIME - 86400);
191 $page->pressButton(t('Save'));
193 $elements = $this->cssSelect('.entity-2.error');
194 $this->assertEqual(1, count($elements), 'The whole nested entity form has been correctly flagged with an error class.');