FormCacheTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace Drupal\Tests\Core\Form;
  3. use Drupal\Core\Form\FormCache;
  4. use Drupal\Core\Form\FormState;
  5. use Drupal\Tests\UnitTestCase;
  6. /**
  7. * @coversDefaultClass \Drupal\Core\Form\FormCache
  8. * @group Form
  9. */
  10. class FormCacheTest extends UnitTestCase {
  11. /**
  12. * The form cache object under test.
  13. *
  14. * @var \Drupal\Core\Form\FormCache
  15. */
  16. protected $formCache;
  17. /**
  18. * The expirable key value factory.
  19. *
  20. * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
  21. */
  22. protected $keyValueExpirableFactory;
  23. /**
  24. * The current user.
  25. *
  26. * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
  27. */
  28. protected $account;
  29. /**
  30. * The CSRF token generator.
  31. *
  32. * @var \Drupal\Core\Access\CsrfTokenGenerator|\PHPUnit\Framework\MockObject\MockObject
  33. */
  34. protected $csrfToken;
  35. /**
  36. * The mocked module handler.
  37. *
  38. * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
  39. */
  40. protected $moduleHandler;
  41. /**
  42. * The expirable key value store used by form cache.
  43. *
  44. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit\Framework\MockObject\MockObject
  45. */
  46. protected $formCacheStore;
  47. /**
  48. * The expirable key value store used by form state cache.
  49. *
  50. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit\Framework\MockObject\MockObject
  51. */
  52. protected $formStateCacheStore;
  53. /**
  54. * The logger channel.
  55. *
  56. * @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
  57. */
  58. protected $logger;
  59. /**
  60. * The request stack.
  61. *
  62. * @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit\Framework\MockObject\MockObject
  63. */
  64. protected $requestStack;
  65. /**
  66. * A policy rule determining the cacheability of a request.
  67. *
  68. * @var \Drupal\Core\PageCache\RequestPolicyInterface|\PHPUnit\Framework\MockObject\MockObject
  69. */
  70. protected $requestPolicy;
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected $runTestInSeparateProcess = TRUE;
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected $preserveGlobalState = FALSE;
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function setUp() {
  83. parent::setUp();
  84. $this->moduleHandler = $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface');
  85. $this->formCacheStore = $this->createMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
  86. $this->formStateCacheStore = $this->createMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
  87. $this->keyValueExpirableFactory = $this->createMock('Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface');
  88. $this->keyValueExpirableFactory->expects($this->any())
  89. ->method('get')
  90. ->will($this->returnValueMap([
  91. ['form', $this->formCacheStore],
  92. ['form_state', $this->formStateCacheStore],
  93. ]));
  94. $this->csrfToken = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator')
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->account = $this->createMock('Drupal\Core\Session\AccountInterface');
  98. $this->logger = $this->createMock('Psr\Log\LoggerInterface');
  99. $this->requestStack = $this->createMock('\Symfony\Component\HttpFoundation\RequestStack');
  100. $this->requestPolicy = $this->createMock('\Drupal\Core\PageCache\RequestPolicyInterface');
  101. $this->formCache = new FormCache($this->root, $this->keyValueExpirableFactory, $this->moduleHandler, $this->account, $this->csrfToken, $this->logger, $this->requestStack, $this->requestPolicy);
  102. }
  103. /**
  104. * @covers ::getCache
  105. */
  106. public function testGetCacheValidToken() {
  107. $form_build_id = 'the_form_build_id';
  108. $form_state = new FormState();
  109. $cache_token = 'the_cache_token';
  110. $cached_form = ['#cache_token' => $cache_token];
  111. $this->formCacheStore->expects($this->once())
  112. ->method('get')
  113. ->with($form_build_id)
  114. ->willReturn($cached_form);
  115. $this->csrfToken->expects($this->once())
  116. ->method('validate')
  117. ->with($cache_token)
  118. ->willReturn(TRUE);
  119. $this->account->expects($this->never())
  120. ->method('isAnonymous');
  121. $form = $this->formCache->getCache($form_build_id, $form_state);
  122. $this->assertSame($cached_form, $form);
  123. }
  124. /**
  125. * @covers ::getCache
  126. */
  127. public function testGetCacheInvalidToken() {
  128. $form_build_id = 'the_form_build_id';
  129. $form_state = new FormState();
  130. $cache_token = 'the_cache_token';
  131. $cached_form = ['#cache_token' => $cache_token];
  132. $this->formCacheStore->expects($this->once())
  133. ->method('get')
  134. ->with($form_build_id)
  135. ->willReturn($cached_form);
  136. $this->csrfToken->expects($this->once())
  137. ->method('validate')
  138. ->with($cache_token)
  139. ->willReturn(FALSE);
  140. $this->account->expects($this->never())
  141. ->method('isAnonymous');
  142. $form = $this->formCache->getCache($form_build_id, $form_state);
  143. $this->assertNull($form);
  144. }
  145. /**
  146. * @covers ::getCache
  147. */
  148. public function testGetCacheAnonUser() {
  149. $form_build_id = 'the_form_build_id';
  150. $form_state = new FormState();
  151. $cached_form = ['#cache_token' => NULL];
  152. $this->formCacheStore->expects($this->once())
  153. ->method('get')
  154. ->with($form_build_id)
  155. ->willReturn($cached_form);
  156. $this->account->expects($this->once())
  157. ->method('isAnonymous')
  158. ->willReturn(TRUE);
  159. $this->csrfToken->expects($this->never())
  160. ->method('validate');
  161. $form = $this->formCache->getCache($form_build_id, $form_state);
  162. $this->assertSame($cached_form, $form);
  163. }
  164. /**
  165. * @covers ::getCache
  166. */
  167. public function testGetCacheAuthUser() {
  168. $form_build_id = 'the_form_build_id';
  169. $form_state = new FormState();
  170. $cached_form = ['#cache_token' => NULL];
  171. $this->formCacheStore->expects($this->once())
  172. ->method('get')
  173. ->with($form_build_id)
  174. ->willReturn($cached_form);
  175. $this->account->expects($this->once())
  176. ->method('isAnonymous')
  177. ->willReturn(FALSE);
  178. $form = $this->formCache->getCache($form_build_id, $form_state);
  179. $this->assertNull($form);
  180. }
  181. /**
  182. * @covers ::getCache
  183. */
  184. public function testGetCacheNoForm() {
  185. $form_build_id = 'the_form_build_id';
  186. $form_state = new FormState();
  187. $cached_form = NULL;
  188. $this->formCacheStore->expects($this->once())
  189. ->method('get')
  190. ->with($form_build_id)
  191. ->willReturn($cached_form);
  192. $this->account->expects($this->never())
  193. ->method('isAnonymous');
  194. $form = $this->formCache->getCache($form_build_id, $form_state);
  195. $this->assertNull($form);
  196. }
  197. /**
  198. * @covers ::getCache
  199. */
  200. public function testGetCacheImmutableForm() {
  201. $form_build_id = 'the_form_build_id';
  202. $form_state = (new FormState())
  203. ->addBuildInfo('immutable', TRUE);
  204. $cached_form = [
  205. '#build_id' => 'the_old_build_form_id',
  206. ];
  207. $this->account->expects($this->once())
  208. ->method('isAnonymous')
  209. ->willReturn(TRUE);
  210. $this->formCacheStore->expects($this->once())
  211. ->method('get')
  212. ->with($form_build_id)
  213. ->willReturn($cached_form);
  214. $form = $this->formCache->getCache($form_build_id, $form_state);
  215. $this->assertSame($cached_form['#build_id'], $form['#build_id_old']);
  216. $this->assertNotSame($cached_form['#build_id'], $form['#build_id']);
  217. $this->assertSame($form['#build_id'], $form['form_build_id']['#value']);
  218. $this->assertSame($form['#build_id'], $form['form_build_id']['#id']);
  219. }
  220. /**
  221. * @covers ::loadCachedFormState
  222. */
  223. public function testLoadCachedFormState() {
  224. $form_build_id = 'the_form_build_id';
  225. $form_state = new FormState();
  226. $cached_form = ['#cache_token' => NULL];
  227. $this->formCacheStore->expects($this->once())
  228. ->method('get')
  229. ->with($form_build_id)
  230. ->willReturn($cached_form);
  231. $this->account->expects($this->once())
  232. ->method('isAnonymous')
  233. ->willReturn(TRUE);
  234. $cached_form_state = ['storage' => ['foo' => 'bar']];
  235. $this->formStateCacheStore->expects($this->once())
  236. ->method('get')
  237. ->with($form_build_id)
  238. ->willReturn($cached_form_state);
  239. $this->formCache->getCache($form_build_id, $form_state);
  240. $this->assertSame($cached_form_state['storage'], $form_state->getStorage());
  241. }
  242. /**
  243. * @covers ::loadCachedFormState
  244. */
  245. public function testLoadCachedFormStateWithFiles() {
  246. $form_build_id = 'the_form_build_id';
  247. $form_state = new FormState();
  248. $cached_form = ['#cache_token' => NULL];
  249. $this->formCacheStore->expects($this->once())
  250. ->method('get')
  251. ->with($form_build_id)
  252. ->willReturn($cached_form);
  253. $this->account->expects($this->once())
  254. ->method('isAnonymous')
  255. ->willReturn(TRUE);
  256. $cached_form_state = [
  257. 'build_info' => [
  258. 'files' => [
  259. [
  260. 'module' => 'a_module',
  261. 'type' => 'the_type',
  262. 'name' => 'some_name',
  263. ],
  264. ['module' => 'another_module'],
  265. ],
  266. ],
  267. ];
  268. $this->moduleHandler->expects($this->at(0))
  269. ->method('loadInclude')
  270. ->with('a_module', 'the_type', 'some_name');
  271. $this->moduleHandler->expects($this->at(1))
  272. ->method('loadInclude')
  273. ->with('another_module', 'inc', 'another_module');
  274. $this->formStateCacheStore->expects($this->once())
  275. ->method('get')
  276. ->with($form_build_id)
  277. ->willReturn($cached_form_state);
  278. $this->formCache->getCache($form_build_id, $form_state);
  279. }
  280. /**
  281. * @covers ::setCache
  282. */
  283. public function testSetCacheWithForm() {
  284. $form_build_id = 'the_form_build_id';
  285. $form = [
  286. '#form_id' => 'the_form_id',
  287. ];
  288. $form_state = new FormState();
  289. $this->formCacheStore->expects($this->once())
  290. ->method('setWithExpire')
  291. ->with($form_build_id, $form, $this->isType('int'));
  292. $form_state_data = $form_state->getCacheableArray();
  293. $this->formStateCacheStore->expects($this->once())
  294. ->method('setWithExpire')
  295. ->with($form_build_id, $form_state_data, $this->isType('int'));
  296. $this->formCache->setCache($form_build_id, $form, $form_state);
  297. }
  298. /**
  299. * @covers ::setCache
  300. */
  301. public function testSetCacheWithoutForm() {
  302. $form_build_id = 'the_form_build_id';
  303. $form = NULL;
  304. $form_state = new FormState();
  305. $this->formCacheStore->expects($this->never())
  306. ->method('setWithExpire');
  307. $form_state_data = $form_state->getCacheableArray();
  308. $this->formStateCacheStore->expects($this->once())
  309. ->method('setWithExpire')
  310. ->with($form_build_id, $form_state_data, $this->isType('int'));
  311. $this->formCache->setCache($form_build_id, $form, $form_state);
  312. }
  313. /**
  314. * @covers ::setCache
  315. */
  316. public function testSetCacheAuthUser() {
  317. $form_build_id = 'the_form_build_id';
  318. $form = [];
  319. $form_state = new FormState();
  320. $cache_token = 'the_cache_token';
  321. $form_data = $form;
  322. $form_data['#cache_token'] = $cache_token;
  323. $this->formCacheStore->expects($this->once())
  324. ->method('setWithExpire')
  325. ->with($form_build_id, $form_data, $this->isType('int'));
  326. $form_state_data = $form_state->getCacheableArray();
  327. $this->formStateCacheStore->expects($this->once())
  328. ->method('setWithExpire')
  329. ->with($form_build_id, $form_state_data, $this->isType('int'));
  330. $this->csrfToken->expects($this->once())
  331. ->method('get')
  332. ->willReturn($cache_token);
  333. $this->account->expects($this->once())
  334. ->method('isAuthenticated')
  335. ->willReturn(TRUE);
  336. $this->formCache->setCache($form_build_id, $form, $form_state);
  337. }
  338. /**
  339. * @covers ::setCache
  340. */
  341. public function testSetCacheBuildIdMismatch() {
  342. $form_build_id = 'the_form_build_id';
  343. $form = [
  344. '#form_id' => 'the_form_id',
  345. '#build_id' => 'stale_form_build_id',
  346. ];
  347. $form_state = new FormState();
  348. $this->formCacheStore->expects($this->never())
  349. ->method('setWithExpire');
  350. $this->formStateCacheStore->expects($this->never())
  351. ->method('setWithExpire');
  352. $this->logger->expects($this->once())
  353. ->method('error')
  354. ->with('Form build-id mismatch detected while attempting to store a form in the cache.');
  355. $this->formCache->setCache($form_build_id, $form, $form_state);
  356. }
  357. /**
  358. * @covers ::deleteCache
  359. */
  360. public function testDeleteCache() {
  361. $form_build_id = 'the_form_build_id';
  362. $this->formCacheStore->expects($this->once())
  363. ->method('delete')
  364. ->with($form_build_id);
  365. $this->formStateCacheStore->expects($this->once())
  366. ->method('delete')
  367. ->with($form_build_id);
  368. $this->formCache->deleteCache($form_build_id);
  369. }
  370. }