b6c668658324a124a7494b7bb9dc13e5bd163ac2
[yaffs-website] / web / core / modules / system / src / Tests / Form / FormStoragePageCacheTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Form;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests form storage from cached pages.
9  *
10  * @group Form
11  */
12 class FormStoragePageCacheTest extends WebTestBase {
13
14   /**
15    * @var array
16    */
17   public static $modules = ['form_test'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     $config = $this->config('system.performance');
26     $config->set('cache.page.max_age', 300);
27     $config->save();
28   }
29
30   /**
31    * Return the build id of the current form.
32    */
33   protected function getFormBuildId() {
34     $build_id_fields = $this->xpath('//input[@name="form_build_id"]');
35     $this->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
36     return (string) $build_id_fields[0]['value'];
37   }
38
39   /**
40    * Build-id is regenerated when validating cached form.
41    */
42   public function testValidateFormStorageOnCachedPage() {
43     $this->drupalGet('form-test/form-storage-page-cache');
44     $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
45     $this->assertText('No old build id', 'No old build id on the page');
46     $build_id_initial = $this->getFormBuildId();
47
48     // Trigger validation error by submitting an empty title.
49     $edit = ['title' => ''];
50     $this->drupalPostForm(NULL, $edit, 'Save');
51     $this->assertText('No old build id', 'No old build id on the page');
52     $build_id_first_validation = $this->getFormBuildId();
53     $this->assertNotEqual($build_id_initial, $build_id_first_validation, 'Build id changes when form validation fails');
54
55     // Trigger validation error by again submitting an empty title.
56     $edit = ['title' => ''];
57     $this->drupalPostForm(NULL, $edit, 'Save');
58     $this->assertText('No old build id', 'No old build id on the page');
59     $build_id_second_validation = $this->getFormBuildId();
60     $this->assertEqual($build_id_first_validation, $build_id_second_validation, 'Build id remains the same when form validation fails subsequently');
61
62     // Repeat the test sequence but this time with a page loaded from the cache.
63     $this->drupalGet('form-test/form-storage-page-cache');
64     $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
65     $this->assertText('No old build id', 'No old build id on the page');
66     $build_id_from_cache_initial = $this->getFormBuildId();
67     $this->assertEqual($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
68
69     // Trigger validation error by submitting an empty title.
70     $edit = ['title' => ''];
71     $this->drupalPostForm(NULL, $edit, 'Save');
72     $this->assertText('No old build id', 'No old build id on the page');
73     $build_id_from_cache_first_validation = $this->getFormBuildId();
74     $this->assertNotEqual($build_id_initial, $build_id_from_cache_first_validation, 'Build id changes when form validation fails');
75     $this->assertNotEqual($build_id_first_validation, $build_id_from_cache_first_validation, 'Build id from first user is not reused');
76
77     // Trigger validation error by again submitting an empty title.
78     $edit = ['title' => ''];
79     $this->drupalPostForm(NULL, $edit, 'Save');
80     $this->assertText('No old build id', 'No old build id on the page');
81     $build_id_from_cache_second_validation = $this->getFormBuildId();
82     $this->assertEqual($build_id_from_cache_first_validation, $build_id_from_cache_second_validation, 'Build id remains the same when form validation fails subsequently');
83   }
84
85   /**
86    * Build-id is regenerated when rebuilding cached form.
87    */
88   public function testRebuildFormStorageOnCachedPage() {
89     $this->drupalGet('form-test/form-storage-page-cache');
90     $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
91     $this->assertText('No old build id', 'No old build id on the page');
92     $build_id_initial = $this->getFormBuildId();
93
94     // Trigger rebuild, should regenerate build id. When a submit handler
95     // triggers a rebuild, the form is built twice in the same POST request,
96     // and during the second build, there is an old build ID, but because the
97     // form is not cached during the initial GET request, it is different from
98     // that initial build ID.
99     $edit = ['title' => 'something'];
100     $this->drupalPostForm(NULL, $edit, 'Rebuild');
101     $this->assertNoText('No old build id', 'There is no old build id on the page.');
102     $this->assertNoText($build_id_initial, 'The old build id is not the initial build id.');
103     $build_id_first_rebuild = $this->getFormBuildId();
104     $this->assertNotEqual($build_id_initial, $build_id_first_rebuild, 'Build id changes on first rebuild.');
105
106     // Trigger subsequent rebuild, should regenerate the build id again.
107     $edit = ['title' => 'something'];
108     $this->drupalPostForm(NULL, $edit, 'Rebuild');
109     $this->assertText($build_id_first_rebuild, 'First build id as old build id on the page');
110     $build_id_second_rebuild = $this->getFormBuildId();
111     $this->assertNotEqual($build_id_first_rebuild, $build_id_second_rebuild, 'Build id changes on second rebuild.');
112   }
113
114 }