f6fb4f8713a42a26d00c7ef522a36f11378ed6ae
[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\Core\Site\Settings;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Tests \Drupal::formBuilder()->setCache() and
13  * \Drupal::formBuilder()->getCache().
14  *
15  * @group Form
16  */
17 class FormCacheTest extends KernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['system', 'user'];
25
26   /**
27    * @var string
28    */
29   protected $formBuildId;
30
31   /**
32    * @var array
33    */
34   protected $form;
35
36   /**
37    * @var array
38    */
39   protected $formState;
40
41   protected function setUp() {
42     parent::setUp();
43     $this->installSchema('system', ['key_value_expire']);
44
45     $this->formBuildId = $this->randomMachineName();
46     $this->form = [
47       '#property' => $this->randomMachineName(),
48     ];
49     $this->formState = new FormState();
50     $this->formState->set('example', $this->randomMachineName());
51   }
52
53   /**
54    * Tests the form cache with a logged-in user.
55    */
56   public function testCacheToken() {
57     \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
58     \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
59
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'));
65
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));
75
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');
82   }
83
84   /**
85    * Tests the form cache without a logged-in user.
86    */
87   public function testNoCacheToken() {
88     // Switch to a anonymous user account.
89     $account_switcher = \Drupal::service('account_switcher');
90     $account_switcher->switchTo(new AnonymousUserSession());
91
92     $this->formState->set('example', $this->randomMachineName());
93     \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
94
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'));
100
101     // Restore user account.
102     $account_switcher->switchBack();
103   }
104
105   /**
106    * Tests the form cache with an overridden cache expiration.
107    */
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);
112
113     $cached_form_state = new FormState();
114     $this->assertFalse(\Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state), 'Expired form not returned from cache');
115   }
116
117 }