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\KernelTests\KernelTestBase;
11 * Tests \Drupal::formBuilder()->setCache() and
12 * \Drupal::formBuilder()->getCache().
16 class FormCacheTest extends KernelTestBase {
23 public static $modules = ['system', 'user'];
28 protected $formBuildId;
40 protected function setUp() {
42 $this->installSchema('system', ['key_value_expire']);
44 $this->formBuildId = $this->randomMachineName();
46 '#property' => $this->randomMachineName(),
48 $this->formState = new FormState();
49 $this->formState->set('example', $this->randomMachineName());
53 * Tests the form cache with a logged-in user.
55 public function testCacheToken() {
56 \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
57 \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
59 $cached_form_state = new FormState();
60 $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
61 $this->assertEqual($this->form['#property'], $cached_form['#property']);
62 $this->assertTrue(!empty($cached_form['#cache_token']), 'Form has a cache token');
63 $this->assertEqual($this->formState->get('example'), $cached_form_state->get('example'));
65 // Test that the form cache isn't loaded when the session/token has changed.
66 // Change the private key. (We cannot change the session ID because this
67 // will break the parent site test runner batch.)
68 \Drupal::state()->set('system.private_key', 'invalid');
69 $cached_form_state = new FormState();
70 $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
71 $this->assertFalse($cached_form, 'No form returned from cache');
72 $cached_form_state_example = $cached_form_state->get('example');
73 $this->assertTrue(empty($cached_form_state_example));
75 // Test that loading the cache with a different form_id fails.
76 $wrong_form_build_id = $this->randomMachineName(9);
77 $cached_form_state = new FormState();
78 $this->assertFalse(\Drupal::formBuilder()->getCache($wrong_form_build_id, $cached_form_state), 'No form returned from cache');
79 $cached_form_state_example = $cached_form_state->get('example');
80 $this->assertTrue(empty($cached_form_state_example), 'Cached form state was not loaded');
84 * Tests the form cache without a logged-in user.
86 public function testNoCacheToken() {
87 // Switch to a anonymous user account.
88 $account_switcher = \Drupal::service('account_switcher');
89 $account_switcher->switchTo(new AnonymousUserSession());
91 $this->formState->set('example', $this->randomMachineName());
92 \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
94 $cached_form_state = new FormState();
95 $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
96 $this->assertEqual($this->form['#property'], $cached_form['#property']);
97 $this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token');
98 $this->assertEqual($this->formState->get('example'), $cached_form_state->get('example'));
100 // Restore user account.
101 $account_switcher->switchBack();