function StorageTest::testImmutableForm

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

Tests form build ID regeneration when loading a cached immutable form.

File

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

Class

StorageTest
Tests a multistep form using form storage.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testImmutableForm() : void {
    // Request the form with 'cache' query parameter to enable form caching.
    $this->drupalGet('form_test/form-storage', [
        'query' => [
            'cache' => 1,
            'immutable' => 1,
        ],
    ]);
    // Ensure the hidden 'form_build_id' field is unique.
    $this->assertSession()
        ->elementsCount('xpath', '//input[@name="form_build_id"]', 1);
    $buildId = $this->assertSession()
        ->hiddenFieldExists('form_build_id')
        ->getValue();
    // Trigger validation error by submitting an empty title.
    $edit = [
        'title' => '',
    ];
    $this->submitForm($edit, 'Continue submit');
    // Verify that the build-id did change.
    $this->assertSession()
        ->hiddenFieldValueNotEquals('form_build_id', $buildId);
    // Ensure the hidden 'form_build_id' field is unique.
    $this->assertSession()
        ->elementsCount('xpath', '//input[@name="form_build_id"]', 1);
    // Retrieve the new build-id.
    $buildId = (string) $this->assertSession()
        ->hiddenFieldExists('form_build_id')
        ->getValue();
    // Trigger validation error by again submitting an empty title.
    $edit = [
        'title' => '',
    ];
    $this->submitForm($edit, 'Continue submit');
    // Verify that the build-id does not change the second time.
    $this->assertSession()
        ->hiddenFieldValueEquals('form_build_id', $buildId);
}

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