3 namespace Drupal\KernelTests\Core\Form;
5 use Drupal\Core\Form\FormState;
6 use Drupal\Core\Session\AnonymousUserSession;
7 use Drupal\Core\Session\UserSession;
8 use Drupal\Core\Site\Settings;
9 use Drupal\KernelTests\KernelTestBase;
12 * Tests \Drupal::formBuilder()->setCache() and
13 * \Drupal::formBuilder()->getCache().
17 class FormCacheTest extends KernelTestBase {
24 public static $modules = ['system', 'user'];
29 protected $formBuildId;
41 protected function setUp() {
43 $this->installSchema('system', ['key_value_expire']);
45 $this->formBuildId = $this->randomMachineName();
47 '#property' => $this->randomMachineName(),
49 $this->formState = new FormState();
50 $this->formState->set('example', $this->randomMachineName());
54 * Tests the form cache with a logged-in user.
56 public function testCacheToken() {
57 \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
58 \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
60 $cached_form_state = new FormState();
61 $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
62 $this->assertEqual($this->form['#property'], $cached_form['#property']);
63 $this->assertTrue(!empty($cached_form['#cache_token']), 'Form has a cache token');
64 $this->assertEqual($this->formState->get('example'), $cached_form_state->get('example'));
66 // Test that the form cache isn't loaded when the session/token has changed.
67 // Change the private key. (We cannot change the session ID because this
68 // will break the parent site test runner batch.)
69 \Drupal::state()->set('system.private_key', 'invalid');
70 $cached_form_state = new FormState();
71 $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
72 $this->assertFalse($cached_form, 'No form returned from cache');
73 $cached_form_state_example = $cached_form_state->get('example');
74 $this->assertTrue(empty($cached_form_state_example));
76 // Test that loading the cache with a different form_id fails.
77 $wrong_form_build_id = $this->randomMachineName(9);
78 $cached_form_state = new FormState();
79 $this->assertFalse(\Drupal::formBuilder()->getCache($wrong_form_build_id, $cached_form_state), 'No form returned from cache');
80 $cached_form_state_example = $cached_form_state->get('example');
81 $this->assertTrue(empty($cached_form_state_example), 'Cached form state was not loaded');
85 * Tests the form cache without a logged-in user.
87 public function testNoCacheToken() {
88 // Switch to a anonymous user account.
89 $account_switcher = \Drupal::service('account_switcher');
90 $account_switcher->switchTo(new AnonymousUserSession());
92 $this->formState->set('example', $this->randomMachineName());
93 \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
95 $cached_form_state = new FormState();
96 $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
97 $this->assertEqual($this->form['#property'], $cached_form['#property']);
98 $this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token');
99 $this->assertEqual($this->formState->get('example'), $cached_form_state->get('example'));
101 // Restore user account.
102 $account_switcher->switchBack();
106 * Tests the form cache with an overridden cache expiration.
108 public function testCacheCustomExpiration() {
109 // Override form cache expiration so that the cached form expired yesterday.
110 new Settings(['form_cache_expiration' => -1 * (24 * 60 * 60), 'hash_salt' => $this->randomMachineName()]);
111 \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
113 $cached_form_state = new FormState();
114 $this->assertFalse(\Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state), 'Expired form not returned from cache');