View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Tests\BrowserTestBase;
class FormStoragePageCacheTest extends BrowserTestBase {
protected static $modules = [
'form_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 300);
$config
->save();
}
protected function getFormBuildId() {
$this
->assertSession()
->elementsCount('xpath', '//input[@name="form_build_id"]', 1);
return (string) $this
->assertSession()
->hiddenFieldExists('form_build_id')
->getAttribute('value');
}
public function testValidateFormStorageOnCachedPage() {
$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();
$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');
$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');
$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');
$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');
$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');
}
public function testRebuildFormStorageOnCachedPage() {
$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();
$edit = [
'title' => 'something',
];
$this
->submitForm($edit, 'Rebuild');
$this
->assertSession()
->pageTextNotContains('No old build id');
$this
->assertSession()
->pageTextNotContains($build_id_initial);
$build_id_first_rebuild = $this
->getFormBuildId();
$this
->assertNotEquals($build_id_initial, $build_id_first_rebuild, 'Build id changes on first rebuild.');
$edit = [
'title' => 'something',
];
$this
->submitForm($edit, 'Rebuild');
$this
->assertSession()
->pageTextContains($build_id_first_rebuild);
$build_id_second_rebuild = $this
->getFormBuildId();
$this
->assertNotEquals($build_id_first_rebuild, $build_id_second_rebuild, 'Build id changes on second rebuild.');
}
}