function StorageTest::testCachedFormStorageValidation

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/StorageTest.php \Drupal\Tests\system\Functional\Form\StorageTest::testCachedFormStorageValidation()
  2. 8.9.x core/modules/system/tests/src/Functional/Form/StorageTest.php \Drupal\Tests\system\Functional\Form\StorageTest::testCachedFormStorageValidation()
  3. 11.x core/modules/system/tests/src/Functional/Form/StorageTest.php \Drupal\Tests\system\Functional\Form\StorageTest::testCachedFormStorageValidation()

Tests updating cached form storage during form validation.

If form caching is enabled and a form stores data in the form storage, then the form storage also has to be updated in case of a validation error in the form. This test re-uses the existing form for multi-step tests, but triggers a special #element_validate handler to update the form storage during form validation, while another, required element in the form triggers a form validation error.

File

core/modules/system/tests/src/Functional/Form/StorageTest.php, line 124

Class

StorageTest
Tests a multistep form using form storage.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testCachedFormStorageValidation() : void {
    // Request the form with 'cache' query parameter to enable form caching.
    $this->drupalGet('form_test/form-storage', [
        'query' => [
            'cache' => 1,
        ],
    ]);
    // Skip step 1 of the multi-step form, since the first step copies over
    // 'title' into form storage, but we want to verify that changes in the form
    // storage are updated in the cache during form validation.
    $edit = [
        'title' => 'foo',
    ];
    $this->submitForm($edit, 'Continue submit');
    // In step 2, trigger a validation error for the required 'title' field, and
    // post the special 'change_title' value for the 'value' field, which
    // conditionally invokes the #element_validate handler to update the form
    // storage.
    $edit = [
        'title' => '',
        'value' => 'change_title',
    ];
    $this->submitForm($edit, 'Save');
    // At this point, the form storage should contain updated values, but we do
    // not see them, because the form has not been rebuilt yet due to the
    // validation error. Post again and verify that the rebuilt form contains
    // the values of the updated form storage.
    $this->submitForm([
        'title' => 'foo',
        'value' => 'bar',
    ], 'Save');
    $this->assertSession()
        ->pageTextContains("The thing has been changed.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.