PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php

https://gitlab.com/reasonat/test8
PHP | 434 lines | 266 code | 66 blank | 102 comment | 0 complexity | a93888660c4d18bff68fc30dbb3493e1 MD5 | raw file
  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->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
  85. $this->formCacheStore = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
  86. $this->formStateCacheStore = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
  87. $this->keyValueExpirableFactory = $this->getMock('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->getMock('Drupal\Core\Session\AccountInterface');
  98. $this->logger = $this->getMock('Psr\Log\LoggerInterface');
  99. $this->requestStack = $this->getMock('\Symfony\Component\HttpFoundation\RequestStack');
  100. $this->requestPolicy = $this->getMock('\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 = ['build_info' => ['files' => [
  257. [
  258. 'module' => 'a_module',
  259. 'type' => 'the_type',
  260. 'name' => 'some_name',
  261. ],
  262. [
  263. 'module' => 'another_module',
  264. ],
  265. ]]];
  266. $this->moduleHandler->expects($this->at(0))
  267. ->method('loadInclude')
  268. ->with('a_module', 'the_type', 'some_name');
  269. $this->moduleHandler->expects($this->at(1))
  270. ->method('loadInclude')
  271. ->with('another_module', 'inc', 'another_module');
  272. $this->formStateCacheStore->expects($this->once())
  273. ->method('get')
  274. ->with($form_build_id)
  275. ->willReturn($cached_form_state);
  276. $this->formCache->getCache($form_build_id, $form_state);
  277. }
  278. /**
  279. * @covers ::setCache
  280. */
  281. public function testSetCacheWithForm() {
  282. $form_build_id = 'the_form_build_id';
  283. $form = [
  284. '#form_id' => 'the_form_id'
  285. ];
  286. $form_state = new FormState();
  287. $this->formCacheStore->expects($this->once())
  288. ->method('setWithExpire')
  289. ->with($form_build_id, $form, $this->isType('int'));
  290. $form_state_data = $form_state->getCacheableArray();
  291. $this->formStateCacheStore->expects($this->once())
  292. ->method('setWithExpire')
  293. ->with($form_build_id, $form_state_data, $this->isType('int'));
  294. $this->formCache->setCache($form_build_id, $form, $form_state);
  295. }
  296. /**
  297. * @covers ::setCache
  298. */
  299. public function testSetCacheWithoutForm() {
  300. $form_build_id = 'the_form_build_id';
  301. $form = NULL;
  302. $form_state = new FormState();
  303. $this->formCacheStore->expects($this->never())
  304. ->method('setWithExpire');
  305. $form_state_data = $form_state->getCacheableArray();
  306. $this->formStateCacheStore->expects($this->once())
  307. ->method('setWithExpire')
  308. ->with($form_build_id, $form_state_data, $this->isType('int'));
  309. $this->formCache->setCache($form_build_id, $form, $form_state);
  310. }
  311. /**
  312. * @covers ::setCache
  313. */
  314. public function testSetCacheAuthUser() {
  315. $form_build_id = 'the_form_build_id';
  316. $form = [];
  317. $form_state = new FormState();
  318. $cache_token = 'the_cache_token';
  319. $form_data = $form;
  320. $form_data['#cache_token'] = $cache_token;
  321. $this->formCacheStore->expects($this->once())
  322. ->method('setWithExpire')
  323. ->with($form_build_id, $form_data, $this->isType('int'));
  324. $form_state_data = $form_state->getCacheableArray();
  325. $this->formStateCacheStore->expects($this->once())
  326. ->method('setWithExpire')
  327. ->with($form_build_id, $form_state_data, $this->isType('int'));
  328. $this->csrfToken->expects($this->once())
  329. ->method('get')
  330. ->willReturn($cache_token);
  331. $this->account->expects($this->once())
  332. ->method('isAuthenticated')
  333. ->willReturn(TRUE);
  334. $this->formCache->setCache($form_build_id, $form, $form_state);
  335. }
  336. /**
  337. * @covers ::setCache
  338. */
  339. public function testSetCacheBuildIdMismatch() {
  340. $form_build_id = 'the_form_build_id';
  341. $form = [
  342. '#form_id' => 'the_form_id',
  343. '#build_id' => 'stale_form_build_id',
  344. ];
  345. $form_state = new FormState();
  346. $this->formCacheStore->expects($this->never())
  347. ->method('setWithExpire');
  348. $this->formStateCacheStore->expects($this->never())
  349. ->method('setWithExpire');
  350. $this->logger->expects($this->once())
  351. ->method('error')
  352. ->with('Form build-id mismatch detected while attempting to store a form in the cache.');
  353. $this->formCache->setCache($form_build_id, $form, $form_state);
  354. }
  355. /**
  356. * @covers ::deleteCache
  357. */
  358. public function testDeleteCache() {
  359. $form_build_id = 'the_form_build_id';
  360. $this->formCacheStore->expects($this->once())
  361. ->method('delete')
  362. ->with($form_build_id);
  363. $this->formStateCacheStore->expects($this->once())
  364. ->method('delete')
  365. ->with($form_build_id);
  366. $this->formCache->deleteCache($form_build_id);
  367. }
  368. }