6425a3099d5c58db401320b595d094ca58020b30
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Form / FormCacheTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Form;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\Core\Session\AnonymousUserSession;
7 use Drupal\Core\Session\UserSession;
8 use Drupal\KernelTests\KernelTestBase;
9
10 /**
11  * Tests \Drupal::formBuilder()->setCache() and
12  * \Drupal::formBuilder()->getCache().
13  *
14  * @group Form
15  */
16 class FormCacheTest extends KernelTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['system', 'user'];
24
25   /**
26    * @var string
27    */
28   protected $formBuildId;
29
30   /**
31    * @var array
32    */
33   protected $form;
34
35   /**
36    * @var array
37    */
38   protected $formState;
39
40   protected function setUp() {
41     parent::setUp();
42     $this->installSchema('system', ['key_value_expire']);
43
44     $this->formBuildId = $this->randomMachineName();
45     $this->form = [
46       '#property' => $this->randomMachineName(),
47     ];
48     $this->formState = new FormState();
49     $this->formState->set('example', $this->randomMachineName());
50   }
51
52   /**
53    * Tests the form cache with a logged-in user.
54    */
55   public function testCacheToken() {
56     \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
57     \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
58
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'));
64
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));
74
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');
81   }
82
83   /**
84    * Tests the form cache without a logged-in user.
85    */
86   public function testNoCacheToken() {
87     // Switch to a anonymous user account.
88     $account_switcher = \Drupal::service('account_switcher');
89     $account_switcher->switchTo(new AnonymousUserSession());
90
91     $this->formState->set('example', $this->randomMachineName());
92     \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
93
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'));
99
100     // Restore user account.
101     $account_switcher->switchBack();
102   }
103
104 }