/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->cacheContextsManager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')->disableOriginalConstructor()->getMock(); $this->cacheContextsManager->method('assertValidTokens')->willReturn(TRUE); $container = new ContainerBuilder(); $container->set('cache_contexts_manager', $this->cacheContextsManager); \Drupal::setContainer($container); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->cacheContextsManager = $this->getMockBuilder(CacheContextsManager::class)->disableOriginalConstructor()->getMock(); $this->cacheContextsManager->expects($this->any())->method('assertValidTokens')->willReturn(TRUE); $container = new Container(); $container->set('cache_contexts_manager', $this->cacheContextsManager); \Drupal::setContainer($container); $this->entityType = $this->getMock(EntityTypeInterface::class); $this->moduleHandler = $this->getMock(ModuleHandlerInterface::class); $this->sut = new CurrencyAccessControlHandler($this->entityType, $this->moduleHandler); }
/** * {@inheritdoc} */ public function setUp() { $configuration = []; $plugin_id = $this->randomMachineName(); $this->pluginDefinition = ['cache' => TRUE, 'provider' => $this->randomMachineName()]; $this->cacheContextsManager = $this->getMockBuilder(CacheContextsManager::class)->disableOriginalConstructor()->getMock(); $this->cacheContextsManager->expects($this->any())->method('assertValidTokens')->willReturn(TRUE); $this->currencyStorage = $this->getMock(EntityStorageInterface::class); $this->input = $this->getMock(InputInterface::class); $this->stringTranslation = $this->getStringTranslationStub(); $container = new Container(); $container->set('cache_contexts_manager', $this->cacheContextsManager); \Drupal::setContainer($container); $this->sut = new CurrencyLocalize($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage, $this->input); }
/** * Creates the cache ID for a renderable element. * * Creates the cache ID string based on #cache['keys'] + #cache['contexts']. * * @param array $elements * A renderable array. * * @return string * The cache ID string, or FALSE if the element may not be cached. */ protected function createCacheID(array $elements) { // If the maximum age is zero, then caching is effectively prohibited. if (isset($elements['#cache']['max-age']) && $elements['#cache']['max-age'] === 0) { return FALSE; } if (isset($elements['#cache']['keys'])) { $cid_parts = $elements['#cache']['keys']; if (!empty($elements['#cache']['contexts'])) { $contexts = $this->cacheContextsManager->convertTokensToKeys($elements['#cache']['contexts']); $cid_parts = array_merge($cid_parts, $contexts); } return implode(':', $cid_parts); } return FALSE; }
/** * Creates the cache ID for a renderable element. * * Creates the cache ID string based on #cache['keys'] + #cache['contexts']. * * @param array &$elements * A renderable array. * * @return string * The cache ID string, or FALSE if the element may not be cached. */ protected function createCacheID(array &$elements) { // If the maximum age is zero, then caching is effectively prohibited. if (isset($elements['#cache']['max-age']) && $elements['#cache']['max-age'] === 0) { return FALSE; } if (isset($elements['#cache']['keys'])) { $cid_parts = $elements['#cache']['keys']; if (!empty($elements['#cache']['contexts'])) { $context_cache_keys = $this->cacheContextsManager->convertTokensToKeys($elements['#cache']['contexts']); $cid_parts = array_merge($cid_parts, $context_cache_keys->getKeys()); CacheableMetadata::createFromRenderArray($elements)->merge($context_cache_keys)->applyTo($elements); } return implode(':', $cid_parts); } return FALSE; }
/** * @covers ::checkAccess * * @dataProvider providerTestAccessDelete */ public function testAccessDelete($is_new, $expected) { $this->entityType->getAdminPermission()->willReturn('test permission'); $page = $this->prophesize(PageInterface::class); $page->isNew()->willReturn($is_new); $page->language()->willReturn($this->prophesize(LanguageInterface::class)->reveal()); $page->uuid()->shouldBeCalled(); $page->getEntityTypeId()->shouldBeCalled(); // Ensure that the cache tag is added for the temporary conditions. if ($is_new) { $page->getCacheTags()->willReturn(['page:1']); $page->getCacheContexts()->willReturn([]); $page->getCacheMaxAge()->willReturn(0); } else { $this->cacheContextsManager->assertValidTokens(['user.permissions'])->willReturn(TRUE); } $account = $this->prophesize(AccountInterface::class); $account->hasPermission('test permission')->willReturn(TRUE); $account->id()->shouldBeCalled(); $this->assertSame($expected, $this->pageAccess->access($page->reveal(), 'delete', $account->reveal())); }
/** * @covers ::validateTokens * * @dataProvider validateTokensProvider */ public function testValidateContexts(array $contexts, $expected_exception_message) { $container = new ContainerBuilder(); $cache_contexts_manager = new CacheContextsManager($container, ['foo', 'foo.bar', 'baz']); if ($expected_exception_message !== FALSE) { $this->setExpectedException('LogicException', $expected_exception_message); } // If it doesn't throw an exception, validateTokens() returns NULL. $this->assertNull($cache_contexts_manager->validateTokens($contexts)); }