function FormStoragePageCacheTest::testValidateFormStorageOnCachedPage

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

Build-id is regenerated when validating cached form.

File

core/modules/system/tests/src/Functional/Form/FormStoragePageCacheTest.php, line 49

Class

FormStoragePageCacheTest
Tests form storage from cached pages.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testValidateFormStorageOnCachedPage() : void {
    $this->drupalGet('form-test/form-storage-page-cache');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()
        ->pageTextContains('No old build id');
    $build_id_initial = $this->getFormBuildId();
    // Trigger validation error by submitting an empty title.
    $edit = [
        'title' => '',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('No old build id');
    $build_id_first_validation = $this->getFormBuildId();
    $this->assertNotEquals($build_id_initial, $build_id_first_validation, 'Build id changes when form validation fails');
    // Trigger validation error by again submitting an empty title.
    $edit = [
        'title' => '',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('No old build id');
    $build_id_second_validation = $this->getFormBuildId();
    $this->assertEquals($build_id_first_validation, $build_id_second_validation, 'Build id remains the same when form validation fails subsequently');
    // Repeat the test sequence but this time with a page loaded from the cache.
    $this->drupalGet('form-test/form-storage-page-cache');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertSession()
        ->pageTextContains('No old build id');
    $build_id_from_cache_initial = $this->getFormBuildId();
    $this->assertEquals($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
    // Trigger validation error by submitting an empty title.
    $edit = [
        'title' => '',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('No old build id');
    $build_id_from_cache_first_validation = $this->getFormBuildId();
    $this->assertNotEquals($build_id_initial, $build_id_from_cache_first_validation, 'Build id changes when form validation fails');
    $this->assertNotEquals($build_id_first_validation, $build_id_from_cache_first_validation, 'Build id from first user is not reused');
    // Trigger validation error by again submitting an empty title.
    $edit = [
        'title' => '',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('No old build id');
    $build_id_from_cache_second_validation = $this->getFormBuildId();
    $this->assertEquals($build_id_from_cache_first_validation, $build_id_from_cache_second_validation, 'Build id remains the same when form validation fails subsequently');
}

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