You are here

public function FormStoragePageCacheTest::testValidateFormStorageOnCachedPage in Drupal 8

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()

Build-id is regenerated when validating cached form.

File

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

Class

FormStoragePageCacheTest
Tests form storage from cached pages.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testValidateFormStorageOnCachedPage() {
  $this
    ->drupalGet('form-test/form-storage-page-cache');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  $this
    ->assertText('No old build id', 'No old build id on the page');
  $build_id_initial = $this
    ->getFormBuildId();

  // Trigger validation error by submitting an empty title.
  $edit = [
    'title' => '',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertText('No old build id', 'No old build id on the page');
  $build_id_first_validation = $this
    ->getFormBuildId();
  $this
    ->assertNotEqual($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
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertText('No old build id', 'No old build id on the page');
  $build_id_second_validation = $this
    ->getFormBuildId();
  $this
    ->assertEqual($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
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->assertText('No old build id', 'No old build id on the page');
  $build_id_from_cache_initial = $this
    ->getFormBuildId();
  $this
    ->assertEqual($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
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertText('No old build id', 'No old build id on the page');
  $build_id_from_cache_first_validation = $this
    ->getFormBuildId();
  $this
    ->assertNotEqual($build_id_initial, $build_id_from_cache_first_validation, 'Build id changes when form validation fails');
  $this
    ->assertNotEqual($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
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertText('No old build id', 'No old build id on the page');
  $build_id_from_cache_second_validation = $this
    ->getFormBuildId();
  $this
    ->assertEqual($build_id_from_cache_first_validation, $build_id_from_cache_second_validation, 'Build id remains the same when form validation fails subsequently');
}