View source
<?php
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
class FormStoragePageCacheTest extends WebTestBase {
public static $modules = array(
'form_test',
);
protected function setUp() {
parent::setUp();
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 300);
$config
->save();
}
protected function getFormBuildId() {
$build_id_fields = $this
->xpath('//input[@name="form_build_id"]');
$this
->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
return (string) $build_id_fields[0]['value'];
}
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();
$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');
$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');
$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');
$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');
$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');
}
public function testRebuildFormStorageOnCachedPage() {
$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();
$edit = [
'title' => 'something',
];
$this
->drupalPostForm(NULL, $edit, 'Rebuild');
$this
->assertNoText('No old build id', 'There is no old build id on the page.');
$this
->assertNoText($build_id_initial, 'The old build id is not the initial build id.');
$build_id_first_rebuild = $this
->getFormBuildId();
$this
->assertNotEqual($build_id_initial, $build_id_first_rebuild, 'Build id changes on first rebuild.');
$edit = [
'title' => 'something',
];
$this
->drupalPostForm(NULL, $edit, 'Rebuild');
$this
->assertText($build_id_first_rebuild, 'First build id as old build id on the page');
$build_id_second_rebuild = $this
->getFormBuildId();
$this
->assertNotEqual($build_id_first_rebuild, $build_id_second_rebuild, 'Build id changes on second rebuild.');
}
}