diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 75d50c5..64e01e6 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -189,7 +189,7 @@ public function buildForm($form_id, FormStateInterface &$form_state) { // Inform $form_state about the request method that's building it, so that // it can prevent persisting state changes during HTTP methods for which - // that is disallowed by HTTP. + // that is disallowed by HTTP: GET and HEAD. $form_state->setRequestMethod($request->getMethod()); // Initialize the form's user input. The user input should include only the diff --git a/core/lib/Drupal/Core/Form/FormStateInterface.php b/core/lib/Drupal/Core/Form/FormStateInterface.php index 8164227..fd46f70 100644 --- a/core/lib/Drupal/Core/Form/FormStateInterface.php +++ b/core/lib/Drupal/Core/Form/FormStateInterface.php @@ -747,7 +747,7 @@ public function getLimitValidationErrors(); * methods. * * @see \Drupal\Core\Form\FormState::$method - * @see self::setRequestMethod(). + * @see \Drupal\Core\Form\FormStateInterface::setRequestMethod() * * @return $this */ @@ -760,6 +760,8 @@ public function setMethod($method); * Can be any valid HTTP method, such as GET, POST, HEAD, etc. * * @return $this + * + * @see \Drupal\Core\Form\FormStateInterface::setMethod() */ public function setRequestMethod($method); diff --git a/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php b/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php index 136ec69..4113263 100644 --- a/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php +++ b/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php @@ -35,7 +35,7 @@ protected function getFormBuildId() { } /** - * Create a simple form, then POST via AJAX to change to it. + * Create a simple form, then submit the form via AJAX to change to it. */ public function testSimpleAJAXFormValue() { $this->drupalGet('ajax_forms_test_get_form'); diff --git a/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php b/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php index e0be445..5cad67f 100644 --- a/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php +++ b/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php @@ -103,7 +103,7 @@ public function testRebuildFormStorageOnCachedPage() { // that initial build ID. $edit = ['title' => 'something']; $this->drupalPostForm(NULL, $edit, 'Rebuild'); - $this->assertNoText('No old build id', 'There is an old build id on the page.'); + $this->assertNoText('No old build id', 'There is no old build id on the page.'); $this->assertNoText($build_id_initial, 'The old build id is not the initial build id.'); $build_id_first_rebuild = $this->getFormBuildId(); $this->assertNotEqual($build_id_initial, $build_id_first_rebuild, 'Build id changes on first rebuild.'); diff --git a/core/modules/system/src/Tests/Form/StorageTest.php b/core/modules/system/src/Tests/Form/StorageTest.php index 8f3873a..831ba25 100644 --- a/core/modules/system/src/Tests/Form/StorageTest.php +++ b/core/modules/system/src/Tests/Form/StorageTest.php @@ -73,16 +73,19 @@ function testFormCached() { // Use form rebuilding triggered by a submit button. $this->drupalPostForm(NULL, $edit, 'Continue submit'); + // The first one is for the building of the form. $this->assertText('Form constructions: 2'); + // The second one is for the rebuilding of the form. + $this->assertText('Form constructions: 3'); // Reset the form to the values of the storage, using a form rebuild // triggered by button of type button. $this->drupalPostForm(NULL, array('title' => 'changed'), 'Reset'); $this->assertFieldByName('title', 'new', 'Values have been reset.'); - $this->assertText('Form constructions: 3'); + $this->assertText('Form constructions: 4'); $this->drupalPostForm(NULL, $edit, 'Save'); - $this->assertText('Form constructions: 3'); + $this->assertText('Form constructions: 4'); $this->assertText('Title: new', 'The form storage has stored the values.'); } @@ -130,55 +133,6 @@ function testCachedFormStorageValidation() { } /** - * Tests a form using form state without using 'storage' to pass data from the - * constructor to a submit handler. The data has to persist even when caching - * gets activated, what may happen when a modules alter the form and adds - * #ajax properties. - */ - function testFormStatePersist() { - // Test the form one time with caching activated and one time without. - $run_options = array( - array(), - array('query' => array('cache' => 1)), - ); - foreach ($run_options as $options) { - $this->drupalPostForm('form-test/state-persist', array(), t('Submit'), $options); - // The submit handler outputs the value in $form_state, assert it's there. - $this->assertText('State persisted.'); - - // Test it again, but first trigger a validation error, then test. - $this->drupalPostForm('form-test/state-persist', array('title' => ''), t('Submit'), $options); - $this->assertText(t('!name field is required.', array('!name' => 'title'))); - // Submit the form again triggering no validation error. - $this->drupalPostForm(NULL, array('title' => 'foo'), t('Submit'), $options); - $this->assertText('State persisted.'); - - // Now post to the rebuilt form and verify it's still there afterwards. - $this->drupalPostForm(NULL, array('title' => 'bar'), t('Submit'), $options); - $this->assertText('State persisted.'); - } - } - - /** - * Verify that the form build-id remains the same when validation errors - * occur on a mutable form. - */ - public function testMutableForm() { - // Request the form with 'cache' query parameter to enable form caching. - $this->drupalGet('form_test/form-storage', ['query' => ['cache' => 1]]); - $buildIdFields = $this->xpath('//input[@name="form_build_id"]'); - $this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page'); - $buildId = (string) $buildIdFields[0]['value']; - - // Trigger validation error by submitting an empty title. - $edit = ['title' => '']; - $this->drupalPostForm(NULL, $edit, 'Continue submit'); - - // Verify that the build-id did not change. - $this->assertFieldByName('form_build_id', $buildId, 'Build id remains the same when form validation fails'); - } - - /** * Verifies that form build-id is regenerated when loading an immutable form * from the cache. */ diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index d297945..2a970a2 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -76,19 +76,6 @@ function _form_test_tableselect_get_data() { } /** - * Implements hook_form_FORM_ID_alter(). - * - * @see form_test_state_persist() - */ -function form_test_form_form_test_state_persist_alter(&$form, FormStateInterface $form_state) { - // Simulate a form alter implementation inserting form elements that enable - // caching of the form, e.g. elements having #ajax. - if (\Drupal::request()->get('cache')) { - $form_state->setCached(); - } -} - -/** * Implements hook_form_FORM_ID_alter() for the registration form. */ function form_test_form_user_register_form_alter(&$form, FormStateInterface $form_state) { diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php index 9510ef4..ac65ee8 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php @@ -82,12 +82,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#value' => 'Save', ); - if (\Drupal::request()->get('cache')) { - // Manually activate caching, so we can test that the storage keeps working - // when it's enabled. - $form_state->setCached(); - } - if ($this->getRequest()->get('immutable')) { $form_state->addBuildInfo('immutable', TRUE); } @@ -96,6 +90,17 @@ public function buildForm(array $form, FormStateInterface $form_state) { } /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + if (\Drupal::request()->get('cache')) { + // Manually activate caching, so we can test that the storage keeps working + // when it's enabled. + $form_state->setCached(); + } + } + + /** * Form element validation handler for 'value' element. * * Tests updating of cached form storage during validation.