comparison core/modules/field/src/Tests/NestedFormTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\field\Tests;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10 * Tests field elements in nested forms.
11 *
12 * @group field
13 */
14 class NestedFormTest extends FieldTestBase {
15
16 /**
17 * Modules to enable.
18 *
19 * @var array
20 */
21 public static $modules = ['field_test', 'entity_test'];
22
23 protected function setUp() {
24 parent::setUp();
25
26 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
27 $this->drupalLogin($web_user);
28
29 $this->fieldStorageSingle = [
30 'field_name' => 'field_single',
31 'entity_type' => 'entity_test',
32 'type' => 'test_field',
33 ];
34 $this->fieldStorageUnlimited = [
35 'field_name' => 'field_unlimited',
36 'entity_type' => 'entity_test',
37 'type' => 'test_field',
38 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
39 ];
40
41 $this->field = [
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),
47 'settings' => [
48 'test_field_setting' => $this->randomMachineName(),
49 ],
50 ];
51 }
52
53 /**
54 * Tests Field API form integration within a subform.
55 */
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'])
65 ->save();
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'])
71 ->save();
72
73 // Create two entities.
74 $entity_type = 'entity_test';
75 $storage = $this->container->get('entity_type.manager')
76 ->getStorage($entity_type);
77
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;
82 $entity_1->save();
83
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;
88 $entity_2->save();
89
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.');
96
97 // Submit the form and check that the entities are updated accordingly.
98 $edit = [
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,
105 ];
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]);
113
114 // Submit invalid values and check that errors are reported on the
115 // correct widgets.
116 $edit = [
117 'field_unlimited[1][value]' => -1,
118 ];
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.');
123 $edit = [
124 'entity_2[field_unlimited][1][value]' => -1,
125 ];
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.');
130
131 // Test that reordering works on both entities.
132 $edit = [
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,
137 ];
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]);
141
142 // Test the 'add more' buttons. Only Ajax submission is tested, because
143 // the two 'add more' buttons present in the form have the same #value,
144 // which confuses drupalPostForm().
145 // 'Add more' button in the first entity:
146 $this->drupalGet('test-entity/nested/1/2');
147 $this->drupalPostAjaxForm(NULL, [], 'field_unlimited_add_more');
148 $this->assertFieldByName('field_unlimited[0][value]', 3, 'Entity 1: field_unlimited value 0 appears correctly is the form.');
149 $this->assertFieldByName('field_unlimited[1][value]', 2, 'Entity 1: field_unlimited value 1 appears correctly is the form.');
150 $this->assertFieldByName('field_unlimited[2][value]', '', 'Entity 1: field_unlimited value 2 appears correctly is the form.');
151 $this->assertFieldByName('field_unlimited[3][value]', '', 'Entity 1: an empty widget was added for field_unlimited value 3.');
152 // 'Add more' button in the first entity (changing field values):
153 $edit = [
154 'entity_2[field_unlimited][0][value]' => 13,
155 'entity_2[field_unlimited][1][value]' => 14,
156 'entity_2[field_unlimited][2][value]' => 15,
157 ];
158 $this->drupalPostAjaxForm(NULL, $edit, 'entity_2_field_unlimited_add_more');
159 $this->assertFieldByName('entity_2[field_unlimited][0][value]', 13, 'Entity 2: field_unlimited value 0 appears correctly is the form.');
160 $this->assertFieldByName('entity_2[field_unlimited][1][value]', 14, 'Entity 2: field_unlimited value 1 appears correctly is the form.');
161 $this->assertFieldByName('entity_2[field_unlimited][2][value]', 15, 'Entity 2: field_unlimited value 2 appears correctly is the form.');
162 $this->assertFieldByName('entity_2[field_unlimited][3][value]', '', 'Entity 2: an empty widget was added for field_unlimited value 3.');
163 // Save the form and check values are saved correctly.
164 $this->drupalPostForm(NULL, [], t('Save'));
165 $this->assertFieldValues($entity_1, 'field_unlimited', [3, 2]);
166 $this->assertFieldValues($entity_2, 'field_unlimited', [13, 14, 15]);
167 }
168
169 /**
170 * Tests entity level validation within subforms.
171 */
172 public function testNestedEntityFormEntityLevelValidation() {
173 // Create two entities.
174 $storage = $this->container->get('entity_type.manager')
175 ->getStorage('entity_test_constraints');
176
177 $entity_1 = $storage->create();
178 $entity_1->save();
179
180 $entity_2 = $storage->create();
181 $entity_2->save();
182
183 // Display the 'combined form'.
184 $this->drupalGet("test-entity-constraints/nested/{$entity_1->id()}/{$entity_2->id()}");
185 $this->assertFieldByName('entity_2[changed]', 0, 'Entity 2: changed value appears correctly in the form.');
186
187 // Submit the form and check that the entities are updated accordingly.
188 $edit = ['entity_2[changed]' => REQUEST_TIME - 86400];
189 $this->drupalPostForm(NULL, $edit, t('Save'));
190
191 $elements = $this->cssSelect('.entity-2.error');
192 $this->assertEqual(1, count($elements), 'The whole nested entity form has been correctly flagged with an error class.');
193 }
194
195 }