123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- namespace Drupal\Tests\Core\Form;
- use Drupal\Core\Form\FormCache;
- use Drupal\Core\Form\FormState;
- use Drupal\Tests\UnitTestCase;
- /**
- * @coversDefaultClass \Drupal\Core\Form\FormCache
- * @group Form
- */
- class FormCacheTest extends UnitTestCase {
- /**
- * The form cache object under test.
- *
- * @var \Drupal\Core\Form\FormCache
- */
- protected $formCache;
- /**
- * The expirable key value factory.
- *
- * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $keyValueExpirableFactory;
- /**
- * The current user.
- *
- * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $account;
- /**
- * The CSRF token generator.
- *
- * @var \Drupal\Core\Access\CsrfTokenGenerator|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $csrfToken;
- /**
- * The mocked module handler.
- *
- * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $moduleHandler;
- /**
- * The expirable key value store used by form cache.
- *
- * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $formCacheStore;
- /**
- * The expirable key value store used by form state cache.
- *
- * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $formStateCacheStore;
- /**
- * The logger channel.
- *
- * @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $logger;
- /**
- * The request stack.
- *
- * @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $requestStack;
- /**
- * A policy rule determining the cacheability of a request.
- *
- * @var \Drupal\Core\PageCache\RequestPolicyInterface|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $requestPolicy;
- /**
- * {@inheritdoc}
- */
- protected $runTestInSeparateProcess = TRUE;
- /**
- * {@inheritdoc}
- */
- protected $preserveGlobalState = FALSE;
- /**
- * {@inheritdoc}
- */
- protected function setUp() {
- parent::setUp();
- $this->moduleHandler = $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface');
- $this->formCacheStore = $this->createMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
- $this->formStateCacheStore = $this->createMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
- $this->keyValueExpirableFactory = $this->createMock('Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface');
- $this->keyValueExpirableFactory->expects($this->any())
- ->method('get')
- ->will($this->returnValueMap([
- ['form', $this->formCacheStore],
- ['form_state', $this->formStateCacheStore],
- ]));
- $this->csrfToken = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator')
- ->disableOriginalConstructor()
- ->getMock();
- $this->account = $this->createMock('Drupal\Core\Session\AccountInterface');
- $this->logger = $this->createMock('Psr\Log\LoggerInterface');
- $this->requestStack = $this->createMock('\Symfony\Component\HttpFoundation\RequestStack');
- $this->requestPolicy = $this->createMock('\Drupal\Core\PageCache\RequestPolicyInterface');
- $this->formCache = new FormCache($this->root, $this->keyValueExpirableFactory, $this->moduleHandler, $this->account, $this->csrfToken, $this->logger, $this->requestStack, $this->requestPolicy);
- }
- /**
- * @covers ::getCache
- */
- public function testGetCacheValidToken() {
- $form_build_id = 'the_form_build_id';
- $form_state = new FormState();
- $cache_token = 'the_cache_token';
- $cached_form = ['#cache_token' => $cache_token];
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $this->csrfToken->expects($this->once())
- ->method('validate')
- ->with($cache_token)
- ->willReturn(TRUE);
- $this->account->expects($this->never())
- ->method('isAnonymous');
- $form = $this->formCache->getCache($form_build_id, $form_state);
- $this->assertSame($cached_form, $form);
- }
- /**
- * @covers ::getCache
- */
- public function testGetCacheInvalidToken() {
- $form_build_id = 'the_form_build_id';
- $form_state = new FormState();
- $cache_token = 'the_cache_token';
- $cached_form = ['#cache_token' => $cache_token];
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $this->csrfToken->expects($this->once())
- ->method('validate')
- ->with($cache_token)
- ->willReturn(FALSE);
- $this->account->expects($this->never())
- ->method('isAnonymous');
- $form = $this->formCache->getCache($form_build_id, $form_state);
- $this->assertNull($form);
- }
- /**
- * @covers ::getCache
- */
- public function testGetCacheAnonUser() {
- $form_build_id = 'the_form_build_id';
- $form_state = new FormState();
- $cached_form = ['#cache_token' => NULL];
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $this->account->expects($this->once())
- ->method('isAnonymous')
- ->willReturn(TRUE);
- $this->csrfToken->expects($this->never())
- ->method('validate');
- $form = $this->formCache->getCache($form_build_id, $form_state);
- $this->assertSame($cached_form, $form);
- }
- /**
- * @covers ::getCache
- */
- public function testGetCacheAuthUser() {
- $form_build_id = 'the_form_build_id';
- $form_state = new FormState();
- $cached_form = ['#cache_token' => NULL];
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $this->account->expects($this->once())
- ->method('isAnonymous')
- ->willReturn(FALSE);
- $form = $this->formCache->getCache($form_build_id, $form_state);
- $this->assertNull($form);
- }
- /**
- * @covers ::getCache
- */
- public function testGetCacheNoForm() {
- $form_build_id = 'the_form_build_id';
- $form_state = new FormState();
- $cached_form = NULL;
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $this->account->expects($this->never())
- ->method('isAnonymous');
- $form = $this->formCache->getCache($form_build_id, $form_state);
- $this->assertNull($form);
- }
- /**
- * @covers ::getCache
- */
- public function testGetCacheImmutableForm() {
- $form_build_id = 'the_form_build_id';
- $form_state = (new FormState())
- ->addBuildInfo('immutable', TRUE);
- $cached_form = [
- '#build_id' => 'the_old_build_form_id',
- ];
- $this->account->expects($this->once())
- ->method('isAnonymous')
- ->willReturn(TRUE);
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $form = $this->formCache->getCache($form_build_id, $form_state);
- $this->assertSame($cached_form['#build_id'], $form['#build_id_old']);
- $this->assertNotSame($cached_form['#build_id'], $form['#build_id']);
- $this->assertSame($form['#build_id'], $form['form_build_id']['#value']);
- $this->assertSame($form['#build_id'], $form['form_build_id']['#id']);
- }
- /**
- * @covers ::loadCachedFormState
- */
- public function testLoadCachedFormState() {
- $form_build_id = 'the_form_build_id';
- $form_state = new FormState();
- $cached_form = ['#cache_token' => NULL];
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $this->account->expects($this->once())
- ->method('isAnonymous')
- ->willReturn(TRUE);
- $cached_form_state = ['storage' => ['foo' => 'bar']];
- $this->formStateCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form_state);
- $this->formCache->getCache($form_build_id, $form_state);
- $this->assertSame($cached_form_state['storage'], $form_state->getStorage());
- }
- /**
- * @covers ::loadCachedFormState
- */
- public function testLoadCachedFormStateWithFiles() {
- $form_build_id = 'the_form_build_id';
- $form_state = new FormState();
- $cached_form = ['#cache_token' => NULL];
- $this->formCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form);
- $this->account->expects($this->once())
- ->method('isAnonymous')
- ->willReturn(TRUE);
- $cached_form_state = [
- 'build_info' => [
- 'files' => [
- [
- 'module' => 'a_module',
- 'type' => 'the_type',
- 'name' => 'some_name',
- ],
- ['module' => 'another_module'],
- ],
- ],
- ];
- $this->moduleHandler->expects($this->at(0))
- ->method('loadInclude')
- ->with('a_module', 'the_type', 'some_name');
- $this->moduleHandler->expects($this->at(1))
- ->method('loadInclude')
- ->with('another_module', 'inc', 'another_module');
- $this->formStateCacheStore->expects($this->once())
- ->method('get')
- ->with($form_build_id)
- ->willReturn($cached_form_state);
- $this->formCache->getCache($form_build_id, $form_state);
- }
- /**
- * @covers ::setCache
- */
- public function testSetCacheWithForm() {
- $form_build_id = 'the_form_build_id';
- $form = [
- '#form_id' => 'the_form_id',
- ];
- $form_state = new FormState();
- $this->formCacheStore->expects($this->once())
- ->method('setWithExpire')
- ->with($form_build_id, $form, $this->isType('int'));
- $form_state_data = $form_state->getCacheableArray();
- $this->formStateCacheStore->expects($this->once())
- ->method('setWithExpire')
- ->with($form_build_id, $form_state_data, $this->isType('int'));
- $this->formCache->setCache($form_build_id, $form, $form_state);
- }
- /**
- * @covers ::setCache
- */
- public function testSetCacheWithoutForm() {
- $form_build_id = 'the_form_build_id';
- $form = NULL;
- $form_state = new FormState();
- $this->formCacheStore->expects($this->never())
- ->method('setWithExpire');
- $form_state_data = $form_state->getCacheableArray();
- $this->formStateCacheStore->expects($this->once())
- ->method('setWithExpire')
- ->with($form_build_id, $form_state_data, $this->isType('int'));
- $this->formCache->setCache($form_build_id, $form, $form_state);
- }
- /**
- * @covers ::setCache
- */
- public function testSetCacheAuthUser() {
- $form_build_id = 'the_form_build_id';
- $form = [];
- $form_state = new FormState();
- $cache_token = 'the_cache_token';
- $form_data = $form;
- $form_data['#cache_token'] = $cache_token;
- $this->formCacheStore->expects($this->once())
- ->method('setWithExpire')
- ->with($form_build_id, $form_data, $this->isType('int'));
- $form_state_data = $form_state->getCacheableArray();
- $this->formStateCacheStore->expects($this->once())
- ->method('setWithExpire')
- ->with($form_build_id, $form_state_data, $this->isType('int'));
- $this->csrfToken->expects($this->once())
- ->method('get')
- ->willReturn($cache_token);
- $this->account->expects($this->once())
- ->method('isAuthenticated')
- ->willReturn(TRUE);
- $this->formCache->setCache($form_build_id, $form, $form_state);
- }
- /**
- * @covers ::setCache
- */
- public function testSetCacheBuildIdMismatch() {
- $form_build_id = 'the_form_build_id';
- $form = [
- '#form_id' => 'the_form_id',
- '#build_id' => 'stale_form_build_id',
- ];
- $form_state = new FormState();
- $this->formCacheStore->expects($this->never())
- ->method('setWithExpire');
- $this->formStateCacheStore->expects($this->never())
- ->method('setWithExpire');
- $this->logger->expects($this->once())
- ->method('error')
- ->with('Form build-id mismatch detected while attempting to store a form in the cache.');
- $this->formCache->setCache($form_build_id, $form, $form_state);
- }
- /**
- * @covers ::deleteCache
- */
- public function testDeleteCache() {
- $form_build_id = 'the_form_build_id';
- $this->formCacheStore->expects($this->once())
- ->method('delete')
- ->with($form_build_id);
- $this->formStateCacheStore->expects($this->once())
- ->method('delete')
- ->with($form_build_id);
- $this->formCache->deleteCache($form_build_id);
- }
- }
|