FormCacheTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Drupal\KernelTests\Core\Form;
  3. use Drupal\Core\Form\FormState;
  4. use Drupal\Core\Session\AnonymousUserSession;
  5. use Drupal\Core\Session\UserSession;
  6. use Drupal\Core\Site\Settings;
  7. use Drupal\KernelTests\KernelTestBase;
  8. /**
  9. * Tests \Drupal::formBuilder()->setCache() and
  10. * \Drupal::formBuilder()->getCache().
  11. *
  12. * @group Form
  13. */
  14. class FormCacheTest extends KernelTestBase {
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['system', 'user'];
  21. /**
  22. * @var string
  23. */
  24. protected $formBuildId;
  25. /**
  26. * @var array
  27. */
  28. protected $form;
  29. /**
  30. * @var array
  31. */
  32. protected $formState;
  33. protected function setUp() {
  34. parent::setUp();
  35. $this->installSchema('system', ['key_value_expire']);
  36. $this->formBuildId = $this->randomMachineName();
  37. $this->form = [
  38. '#property' => $this->randomMachineName(),
  39. ];
  40. $this->formState = new FormState();
  41. $this->formState->set('example', $this->randomMachineName());
  42. }
  43. /**
  44. * Tests the form cache with a logged-in user.
  45. */
  46. public function testCacheToken() {
  47. \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
  48. \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
  49. $cached_form_state = new FormState();
  50. $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
  51. $this->assertEqual($this->form['#property'], $cached_form['#property']);
  52. $this->assertTrue(!empty($cached_form['#cache_token']), 'Form has a cache token');
  53. $this->assertEqual($this->formState->get('example'), $cached_form_state->get('example'));
  54. // Test that the form cache isn't loaded when the session/token has changed.
  55. // Change the private key. (We cannot change the session ID because this
  56. // will break the parent site test runner batch.)
  57. \Drupal::state()->set('system.private_key', 'invalid');
  58. $cached_form_state = new FormState();
  59. $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
  60. $this->assertFalse($cached_form, 'No form returned from cache');
  61. $cached_form_state_example = $cached_form_state->get('example');
  62. $this->assertTrue(empty($cached_form_state_example));
  63. // Test that loading the cache with a different form_id fails.
  64. $wrong_form_build_id = $this->randomMachineName(9);
  65. $cached_form_state = new FormState();
  66. $this->assertFalse(\Drupal::formBuilder()->getCache($wrong_form_build_id, $cached_form_state), 'No form returned from cache');
  67. $cached_form_state_example = $cached_form_state->get('example');
  68. $this->assertTrue(empty($cached_form_state_example), 'Cached form state was not loaded');
  69. }
  70. /**
  71. * Tests the form cache without a logged-in user.
  72. */
  73. public function testNoCacheToken() {
  74. // Switch to a anonymous user account.
  75. $account_switcher = \Drupal::service('account_switcher');
  76. $account_switcher->switchTo(new AnonymousUserSession());
  77. $this->formState->set('example', $this->randomMachineName());
  78. \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
  79. $cached_form_state = new FormState();
  80. $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
  81. $this->assertEqual($this->form['#property'], $cached_form['#property']);
  82. $this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token');
  83. $this->assertEqual($this->formState->get('example'), $cached_form_state->get('example'));
  84. // Restore user account.
  85. $account_switcher->switchBack();
  86. }
  87. /**
  88. * Tests the form cache with an overridden cache expiration.
  89. */
  90. public function testCacheCustomExpiration() {
  91. // Override form cache expiration so that the cached form expired yesterday.
  92. new Settings(['form_cache_expiration' => -1 * (24 * 60 * 60), 'hash_salt' => $this->randomMachineName()]);
  93. \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
  94. $cached_form_state = new FormState();
  95. $this->assertFalse(\Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state), 'Expired form not returned from cache');
  96. }
  97. }