Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / system / src / Tests / Form / StorageTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Form;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests a multistep form using form storage and makes sure validation and
9  * caching works right.
10  *
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.
16  *
17  * @group Form
18  */
19 class StorageTest extends WebTestBase {
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['form_test', 'dblog'];
27
28   protected function setUp() {
29     parent::setUp();
30
31     $this->drupalLogin($this->drupalCreateUser());
32   }
33
34   /**
35    * Tests using the form in a usual way.
36    */
37   public function testForm() {
38     $this->drupalGet('form_test/form-storage');
39     $this->assertText('Form constructions: 1');
40
41     $edit = ['title' => 'new', 'value' => 'value_is_set'];
42
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');
47
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');
54
55     $this->drupalPostForm(NULL, $edit, 'Save');
56     $this->assertText('Form constructions: 4');
57     $this->assertText('Title: new', 'The form storage has stored the values.');
58   }
59
60   /**
61    * Tests using the form after calling $form_state->setCached().
62    */
63   public function testFormCached() {
64     $this->drupalGet('form_test/form-storage', ['query' => ['cache' => 1]]);
65     $this->assertText('Form constructions: 1');
66
67     $edit = ['title' => 'new', 'value' => 'value_is_set'];
68
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');
75
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');
81
82     $this->drupalPostForm(NULL, $edit, 'Save');
83     $this->assertText('Form constructions: 4');
84     $this->assertText('Title: new', 'The form storage has stored the values.');
85   }
86
87   /**
88    * Tests validation when form storage is used.
89    */
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.');
93   }
94
95   /**
96    * Tests updating cached form storage during form validation.
97    *
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.
104    */
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]]);
108
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');
114
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
118     // storage.
119     $edit = ['title' => '', 'value' => 'change_title'];
120     $this->drupalPostForm(NULL, $edit, 'Save');
121
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.');
128   }
129
130   /**
131    * Verifies that form build-id is regenerated when loading an immutable form
132    * from the cache.
133    */
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'];
140
141     // Trigger validation error by submitting an empty title.
142     $edit = ['title' => ''];
143     $this->drupalPostForm(NULL, $edit, 'Continue submit');
144
145     // Verify that the build-id did change.
146     $this->assertNoFieldByName('form_build_id', $buildId, 'Build id changes when form validation fails');
147
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'];
152
153     // Trigger validation error by again submitting an empty title.
154     $edit = ['title' => ''];
155     $this->drupalPostForm(NULL, $edit, 'Continue submit');
156
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');
159   }
160
161   /**
162    * Verify that existing contrib code cannot overwrite immutable form state.
163    */
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'];
169
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');
174
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');
179
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');
186   }
187
188 }