3 namespace Drupal\system\Tests\Form;
5 use Drupal\simpletest\WebTestBase;
8 * Tests a multistep form using form storage and makes sure validation and
11 * The tested form puts data into the storage during the initial form
12 * construction. These tests verify that there are no duplicate form
13 * constructions, with and without manual form caching activated. Furthermore
14 * when a validation error occurs, it makes sure that changed form element
15 * values are not lost due to a wrong form rebuild.
19 class StorageTest extends WebTestBase {
26 public static $modules = ['form_test', 'dblog'];
28 protected function setUp() {
31 $this->drupalLogin($this->drupalCreateUser());
35 * Tests using the form in a usual way.
37 public function testForm() {
38 $this->drupalGet('form_test/form-storage');
39 $this->assertText('Form constructions: 1');
41 $edit = ['title' => 'new', 'value' => 'value_is_set'];
43 // Use form rebuilding triggered by a submit button.
44 $this->drupalPostForm(NULL, $edit, 'Continue submit');
45 $this->assertText('Form constructions: 2');
46 $this->assertText('Form constructions: 3');
48 // Reset the form to the values of the storage, using a form rebuild
49 // triggered by button of type button.
50 $this->drupalPostForm(NULL, ['title' => 'changed'], 'Reset');
51 $this->assertFieldByName('title', 'new', 'Values have been reset.');
52 // After rebuilding, the form has been cached.
53 $this->assertText('Form constructions: 4');
55 $this->drupalPostForm(NULL, $edit, 'Save');
56 $this->assertText('Form constructions: 4');
57 $this->assertText('Title: new', 'The form storage has stored the values.');
61 * Tests using the form after calling $form_state->setCached().
63 public function testFormCached() {
64 $this->drupalGet('form_test/form-storage', ['query' => ['cache' => 1]]);
65 $this->assertText('Form constructions: 1');
67 $edit = ['title' => 'new', 'value' => 'value_is_set'];
69 // Use form rebuilding triggered by a submit button.
70 $this->drupalPostForm(NULL, $edit, 'Continue submit');
71 // The first one is for the building of the form.
72 $this->assertText('Form constructions: 2');
73 // The second one is for the rebuilding of the form.
74 $this->assertText('Form constructions: 3');
76 // Reset the form to the values of the storage, using a form rebuild
77 // triggered by button of type button.
78 $this->drupalPostForm(NULL, ['title' => 'changed'], 'Reset');
79 $this->assertFieldByName('title', 'new', 'Values have been reset.');
80 $this->assertText('Form constructions: 4');
82 $this->drupalPostForm(NULL, $edit, 'Save');
83 $this->assertText('Form constructions: 4');
84 $this->assertText('Title: new', 'The form storage has stored the values.');
88 * Tests validation when form storage is used.
90 public function testValidation() {
91 $this->drupalPostForm('form_test/form-storage', ['title' => '', 'value' => 'value_is_set'], 'Continue submit');
92 $this->assertPattern('/value_is_set/', 'The input values have been kept.');
96 * Tests updating cached form storage during form validation.
98 * If form caching is enabled and a form stores data in the form storage, then
99 * the form storage also has to be updated in case of a validation error in
100 * the form. This test re-uses the existing form for multi-step tests, but
101 * triggers a special #element_validate handler to update the form storage
102 * during form validation, while another, required element in the form
103 * triggers a form validation error.
105 public function testCachedFormStorageValidation() {
106 // Request the form with 'cache' query parameter to enable form caching.
107 $this->drupalGet('form_test/form-storage', ['query' => ['cache' => 1]]);
109 // Skip step 1 of the multi-step form, since the first step copies over
110 // 'title' into form storage, but we want to verify that changes in the form
111 // storage are updated in the cache during form validation.
112 $edit = ['title' => 'foo'];
113 $this->drupalPostForm(NULL, $edit, 'Continue submit');
115 // In step 2, trigger a validation error for the required 'title' field, and
116 // post the special 'change_title' value for the 'value' field, which
117 // conditionally invokes the #element_validate handler to update the form
119 $edit = ['title' => '', 'value' => 'change_title'];
120 $this->drupalPostForm(NULL, $edit, 'Save');
122 // At this point, the form storage should contain updated values, but we do
123 // not see them, because the form has not been rebuilt yet due to the
124 // validation error. Post again and verify that the rebuilt form contains
125 // the values of the updated form storage.
126 $this->drupalPostForm(NULL, ['title' => 'foo', 'value' => 'bar'], 'Save');
127 $this->assertText("The thing has been changed.", 'The altered form storage value was updated in cache and taken over.');
131 * Verifies that form build-id is regenerated when loading an immutable form
134 public function testImmutableForm() {
135 // Request the form with 'cache' query parameter to enable form caching.
136 $this->drupalGet('form_test/form-storage', ['query' => ['cache' => 1, 'immutable' => 1]]);
137 $buildIdFields = $this->xpath('//input[@name="form_build_id"]');
138 $this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
139 $buildId = (string) $buildIdFields[0]['value'];
141 // Trigger validation error by submitting an empty title.
142 $edit = ['title' => ''];
143 $this->drupalPostForm(NULL, $edit, 'Continue submit');
145 // Verify that the build-id did change.
146 $this->assertNoFieldByName('form_build_id', $buildId, 'Build id changes when form validation fails');
148 // Retrieve the new build-id.
149 $buildIdFields = $this->xpath('//input[@name="form_build_id"]');
150 $this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
151 $buildId = (string) $buildIdFields[0]['value'];
153 // Trigger validation error by again submitting an empty title.
154 $edit = ['title' => ''];
155 $this->drupalPostForm(NULL, $edit, 'Continue submit');
157 // Verify that the build-id does not change the second time.
158 $this->assertFieldByName('form_build_id', $buildId, 'Build id remains the same when form validation fails subsequently');
162 * Verify that existing contrib code cannot overwrite immutable form state.
164 public function testImmutableFormLegacyProtection() {
165 $this->drupalGet('form_test/form-storage', ['query' => ['cache' => 1, 'immutable' => 1]]);
166 $build_id_fields = $this->xpath('//input[@name="form_build_id"]');
167 $this->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
168 $build_id = (string) $build_id_fields[0]['value'];
170 // Try to poison the form cache.
171 $original = $this->drupalGetAjax('form-test/form-storage-legacy/' . $build_id);
172 $this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
173 $this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
175 // Assert that a watchdog message was logged by
176 // \Drupal::formBuilder()->setCache().
177 $status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, [':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.']);
178 $this->assert($status, 'A watchdog message was logged by \Drupal::formBuilder()->setCache');
180 // Ensure that the form state was not poisoned by the preceding call.
181 $original = $this->drupalGetAjax('form-test/form-storage-legacy/' . $build_id);
182 $this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
183 $this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
184 $this->assert(empty($original['form']['#poisoned']), 'Original form structure was preserved');
185 $this->assert(empty($original['form_state']['poisoned']), 'Original form state was preserved');