/** * Is varnish cache engine enabled * * @return bool */ protected function isVarnishEnabled() { if ($this->isVarnishEnabled === null) { $this->isVarnishEnabled = $this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH; } return $this->isVarnishEnabled; }
/** * @param bool $cacheState * @param bool $varnishIsEnabled * @param bool $scopeIsPrivate * @param int|null $blockTtl * @param string $expectedOutput * @dataProvider processLayoutRenderDataProvider */ public function testExecute($cacheState, $varnishIsEnabled, $scopeIsPrivate, $blockTtl, $expectedOutput) { $eventMock = $this->getMock('Magento\\Framework\\Event', ['getLayout', 'getElementName', 'getTransport'], [], '', false); $this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock)); $eventMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->_layoutMock)); $this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState)); if ($cacheState) { $eventMock->expects($this->once())->method('getElementName')->will($this->returnValue('blockName')); $eventMock->expects($this->once())->method('getTransport')->will($this->returnValue($this->_transport)); $this->_layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue(true)); $this->_layoutMock->expects($this->any())->method('getUpdate')->will($this->returnSelf()); $this->_layoutMock->expects($this->any())->method('getHandles')->will($this->returnValue([])); $this->_layoutMock->expects($this->once())->method('getBlock')->will($this->returnValue($this->_blockMock)); if ($varnishIsEnabled) { $this->_blockMock->expects($this->once())->method('getData')->with('ttl')->will($this->returnValue($blockTtl)); $this->_blockMock->expects($this->any())->method('getUrl')->will($this->returnValue('page_cache/block/wrapesi/with/handles/and/other/stuff')); } if ($scopeIsPrivate) { $this->_blockMock->expects($this->once())->method('getNameInLayout')->will($this->returnValue('testBlockName')); $this->_blockMock->expects($this->once())->method('isScopePrivate')->will($this->returnValue($scopeIsPrivate)); } $this->_configMock->expects($this->any())->method('getType')->will($this->returnValue($varnishIsEnabled)); } $this->_model->execute($this->_observerMock); $this->assertEquals($expectedOutput, $this->_transport['output']); }
/** * Whether a cache type is enabled */ public function testIsEnabled() { $this->_cacheState->expects($this->at(0))->method('isEnabled')->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)->will($this->returnValue(true)); $this->_cacheState->expects($this->at(1))->method('isEnabled')->with(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)->will($this->returnValue(false)); $this->assertTrue($this->_model->isEnabled()); $this->assertFalse($this->_model->isEnabled()); }
/** * Invalidate full page and block cache * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_config->isEnabled()) { $this->_typeList->invalidate(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER); } $this->_typeList->invalidate(\Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER); }
/** * @param AbstractAction $subject * @param AbstractAction $result * @return AbstractAction * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterExecute(AbstractAction $subject, AbstractAction $result) { if ($this->config->isEnabled()) { $this->typeList->invalidate('full_page'); } return $result; }
/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { /** @var \Magento\Customer\Model\Data\Customer $customer */ $customer = $observer->getData('customer'); $customerGroupId = $customer->getGroupId(); $customerGroup = $this->groupRepository->getById($customerGroupId); $customerTaxClassId = $customerGroup->getTaxClassId(); $this->customerSession->setCustomerTaxClassId($customerTaxClassId); /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */ $addresses = $customer->getAddresses(); if (isset($addresses)) { $defaultShippingFound = false; $defaultBillingFound = false; foreach ($addresses as $address) { if ($address->isDefaultBilling()) { $defaultBillingFound = true; $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($address->isDefaultShipping()) { $defaultShippingFound = true; $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($defaultShippingFound && $defaultBillingFound) { break; } } } } }
/** * After generate Xml * * @param \Magento\Framework\View\LayoutInterface $subject * @param \Magento\Framework\View\LayoutInterface $result * @return \Magento\Framework\View\LayoutInterface */ public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && !$this->request->isAjax() && $subject->isCacheable()) { $this->checkoutSession->clearStorage(); } return $result; }
/** * Invalidate full page cache * * @return void */ public function execute() { if ($this->_config->isEnabled()) { $this->_typeList->invalidate('full_page'); } return $this; }
public function testAfterExecuteInvalidate() { $subject = $this->getMockBuilder('Magento\\Catalog\\Model\\Indexer\\Category\\Product\\AbstractAction')->disableOriginalConstructor()->getMockForAbstractClass(); $result = $this->getMockBuilder('Magento\\Catalog\\Model\\Indexer\\Category\\Product\\AbstractAction')->disableOriginalConstructor()->getMockForAbstractClass(); $this->config->expects($this->once())->method('isEnabled')->willReturn(true); $this->typeList->expects($this->once())->method('invalidate')->with('full_page'); $this->assertEquals($result, $this->execute->afterExecute($subject, $result)); }
/** * @dataProvider dataProvider */ public function testAroundDispatchDisabled($state) { $this->configMock->expects($this->any())->method('getType')->will($this->returnValue(null)); $this->versionMock->expects($this->never())->method('process'); $this->stateMock->expects($this->any())->method('getMode')->will($this->returnValue($state)); $this->responseMock->expects($this->never())->method('setHeader'); $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock); }
/** * @param \Magento\Catalog\Model\Resource\Attribute $subject * @param callable $proceed * @param \Magento\Framework\Model\AbstractModel $attribute * @return mixed * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Catalog\Model\Resource\Attribute $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $attribute) { $result = $proceed($attribute); if ($this->config->isEnabled()) { $this->typeList->invalidate('full_page'); } return $result; }
/** * @dataProvider invalidateCacheDataProvider * @param bool $cacheState */ public function testExecute($cacheState) { $this->_configMock->expects($this->once())->method('isEnabled')->will($this->returnValue($cacheState)); if ($cacheState) { $this->_typeListMock->expects($this->once())->method('invalidate')->with($this->equalTo('full_page')); } $this->_model->execute($this->observerMock); }
/** * Test of adding design exceptions to the kay of cache hash * * @param string $cacheType * @param bool $isPageCacheEnabled * @param string|false $result * @param string $uaException * @param string $expected * @dataProvider testAfterGetValueDataProvider */ public function testAfterGetValue($cacheType, $isPageCacheEnabled, $result, $uaException, $expected) { $identifierMock = $this->getMock('Magento\\Framework\\App\\PageCache\\Identifier', [], [], '', false); $this->pageCacheConfigMock->expects($this->once())->method('getType')->will($this->returnValue($cacheType)); $this->pageCacheConfigMock->expects($this->any())->method('isEnabled')->will($this->returnValue($isPageCacheEnabled)); $this->designExceptionsMock->expects($this->any())->method('getThemeByRequest')->will($this->returnValue($uaException)); $this->assertEquals($expected, $this->plugin->afterGetValue($identifierMock, $result)); }
/** * @dataProvider invalidateCacheDataProvider * @param bool $cacheState */ public function testExecuteNoChanged($cacheState) { $this->configMock->expects($this->once())->method('isEnabled')->will($this->returnValue($cacheState)); $this->typeListMock->expects($this->never())->method('invalidate'); if ($cacheState) { $this->objectMock->expects($this->once())->method('getIdentities')->will($this->returnValue([])); } $this->model->execute($this->observerMock); }
/** * After generate Xml * * @param \Magento\Framework\View\LayoutInterface $subject * @param \Magento\Framework\View\LayoutInterface $result * @return \Magento\Framework\View\LayoutInterface */ public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && !$this->request->isAjax() && $subject->isCacheable()) { $this->eventManager->dispatch('depersonalize_clear_session'); session_write_close(); $this->messageSession->clearStorage(); } return $result; }
public function testExportVarnishConfigAction() { $fileContent = 'some conetnt'; $filename = 'varnish.vcl'; $responseMock = $this->getMockBuilder('Magento\\Framework\\App\\ResponseInterface')->disableOriginalConstructor()->getMock(); $this->configMock->expects($this->once())->method('getVclFile')->will($this->returnValue($fileContent)); $this->fileFactoryMock->expects($this->once())->method('create')->with($this->equalTo($filename), $this->equalTo($fileContent), $this->equalTo(DirectoryList::VAR_DIR))->will($this->returnValue($responseMock)); $result = $this->action->executeInternal(); $this->assertInstanceOf('Magento\\Framework\\App\\ResponseInterface', $result); }
/** * Invalidate full page cache if content is changed * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->config->isEnabled()) { $object = $observer->getEvent()->getObject(); if ($object instanceof \Magento\Framework\Object\IdentityInterface) { if ($object->getIdentities()) { $this->typeList->invalidate('full_page'); } } } }
/** * Test method afterGenerateXml */ public function testAfterGenerateXml() { $expectedResult = $this->getMock('Magento\\Framework\\View\\Layout', array(), array(), '', false); $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with($this->equalTo('Magento_PageCache'))->will($this->returnValue(true)); $this->cacheConfigMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->requestMock->expects($this->once($this->once()))->method('isAjax')->will($this->returnValue(false)); $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue(true)); $this->checkoutSessionMock->expects($this->once())->method('clearStorage')->will($this->returnValue($expectedResult)); $actualResult = $this->plugin->afterGenerateXml($this->layoutMock, $expectedResult); $this->assertEquals($expectedResult, $actualResult); }
/** * If Built-In caching is enabled it collects array of tags * of incoming object and asks to clean cache. * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) { $object = $observer->getEvent()->getObject(); if ($object instanceof \Magento\Framework\DataObject\IdentityInterface) { $tags = $object->getIdentities(); if (!empty($tags)) { $this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags)); } } } }
public function testAroundSave() { $subject = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Attribute')->disableOriginalConstructor()->getMock(); $attribute = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute')->disableOriginalConstructor()->getMock(); $self = $this; $proceed = function ($object) use($self, $attribute) { $self->assertEquals($object, $attribute); }; $this->config->expects($this->once())->method('isEnabled')->willReturn(true); $this->typeList->expects($this->once())->method('invalidate')->with('full_page'); $this->save->aroundSave($subject, $proceed, $attribute); }
/** * @param array $requestResult * @param bool $moduleManagerResult * @param bool $cacheConfigResult * @param bool $layoutResult * @param bool $can Depersonalize * @dataProvider checkIfDepersonalizeDataProvider */ public function testCheckIfDepersonalize(array $requestResult, $moduleManagerResult, $cacheConfigResult, $layoutResult, $canDepersonalize) { $this->requestMock->expects($this->any())->method('isAjax')->willReturn($requestResult['ajax']); $this->requestMock->expects($this->any())->method('isGet')->willReturn($requestResult['get']); $this->requestMock->expects($this->any())->method('isHead')->willReturn($requestResult['head']); $this->moduleManagerMock->expects($this->any())->method('isEnabled')->with('Magento_PageCache')->willReturn($moduleManagerResult); $this->cacheConfigMock->expects($this->any())->method('isEnabled')->willReturn($cacheConfigResult); $layoutMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\LayoutInterface', [], '', false); $layoutMock->expects($this->any())->method('isCacheable')->willReturn($layoutResult); $object = new DepersonalizeChecker($this->requestMock, $this->moduleManagerMock, $this->cacheConfigMock); $this->assertEquals($canDepersonalize, $object->checkIfDepersonalize($layoutMock)); }
/** * If Built-In caching is enabled it collects array of tags * of incoming object and asks to clean cache. * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) { $object = $observer->getEvent()->getObject(); if ($object instanceof \Magento\Framework\Object\IdentityInterface) { $tags = $object->getIdentities(); foreach ($tags as $tag) { $tags[] = preg_replace("~_\\d+\$~", '', $tag); } $this->_cache->clean(array_unique($tags)); } } }
/** * @param \Magento\Framework\App\ActionInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request) { if (!$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled() || !$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { return $proceed($request); } $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress(); $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress(); $customerTaxClassId = $this->customerSession->getCustomerTaxClassId(); if (!empty($defaultBillingAddress) || !empty($defaultShippingAddress)) { $taxRates = $this->taxCalculation->getTaxRates($defaultBillingAddress, $defaultShippingAddress, $customerTaxClassId); $this->httpContext->setValue('tax_rates', $taxRates, 0); } return $proceed($request); }
public function testExecuteWithEmptyTags() { $this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue(true)); $observerObject = $this->getMock('Magento\\Framework\\Event\\Observer'); $observedObject = $this->getMock('Magento\\Store\\Model\\Store', [], [], '', false); $tags = []; $eventMock = $this->getMock('Magento\\Framework\\Event', ['getObject'], [], '', false); $eventMock->expects($this->once())->method('getObject')->will($this->returnValue($observedObject)); $observerObject->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock)); $this->_configMock->expects($this->once())->method('getType')->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)); $observedObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags)); $this->_cacheMock->expects($this->never())->method('clean'); $this->_model->execute($observerObject); }
/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { /** @var $customerAddress Address */ $address = $observer->getCustomerAddress(); // Check if the address is either the default billing, shipping, or both if ($this->isDefaultBilling($address)) { $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } if ($this->isDefaultShipping($address)) { $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]); } } }
/** * Export Varnish Configuration as .vcl * * @return \Magento\Framework\App\ResponseInterface */ public function executeInternal() { $fileName = 'varnish.vcl'; $varnishVersion = $this->getRequest()->getParam('varnish'); switch ($varnishVersion) { case 3: $content = $this->config->getVclFile(\Magento\PageCache\Model\Config::VARNISH_3_CONFIGURATION_PATH); break; default: $content = $this->config->getVclFile(\Magento\PageCache\Model\Config::VARNISH_4_CONFIGURATION_PATH); break; } return $this->fileFactory->create($fileName, $content, DirectoryList::VAR_DIR); }
/** * Set X-Cache-Tags header with all the Magento Cache Tags so * they can be purged by the CloudFlare API * * @param \Magento\Framework\View\Layout $subject * @param $result * @return mixed */ public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result) { if (!$subject->isCacheable() || !$this->config->isEnabled()) { return $result; } $tags = []; foreach ($subject->getAllBlocks() as $block) { if ($block->getIdentities() !== null) { $tags = array_merge($tags, $block->getIdentities()); } } $tags = array_unique($tags); $this->cacheTagsUtil->setCloudFlareCacheTagsResponseHeader($this->response, $tags); return $result; }
/** * @param \Magento\Framework\App\ActionInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function aroundExecute( \Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request ) { if (!$this->weeeHelper->isEnabled() || !$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled()) { return $proceed($request); } $basedOn = $this->taxHelper->getTaxBasedOn(); if ($basedOn != 'shipping' && $basedOn != 'billing') { return $proceed($request); } $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $countryId = $weeeTaxRegion['countryId']; $regionId = $weeeTaxRegion['regionId']; if (!$countryId && !$regionId) { // country and region does not exist return $proceed($request); } else if ($countryId && !$regionId) { // country exist and region does not exist $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } else { // country and region exist $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); if (!$exist) { // just check the country for weee $regionId = 0; $exist = $this->weeeTax->isWeeeInLocation( $countryId, $regionId, $websiteId ); } } if ($exist) { $this->httpContext->setValue( 'weee_tax_region', ['countryId' => $countryId, 'regionId' => $regionId], 0 ); } return $proceed($request); }
/** * Test case for cache invalidation * * @dataProvider flushCacheByTagsDataProvider * @param $cacheState */ public function testExecute($cacheState) { $this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState)); $observerObject = $this->getMock('Magento\\Framework\\Event\\Observer'); $observedObject = $this->getMock('Magento\\Store\\Model\\Store', [], [], '', false); if ($cacheState) { $tags = ['cache_1', 'cache_group']; $expectedTags = ['cache_1', 'cache_group', 'cache']; $eventMock = $this->getMock('Magento\\Framework\\Event', ['getObject'], [], '', false); $eventMock->expects($this->once())->method('getObject')->will($this->returnValue($observedObject)); $observerObject->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock)); $this->_configMock->expects($this->once())->method('getType')->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)); $observedObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags)); $this->_cacheMock->expects($this->once())->method('clean')->with($this->equalTo($expectedTags)); } $this->_model->execute($observerObject); }
public function testAfterAddressSave() { $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with('Magento_PageCache')->willReturn(true); $this->cacheConfigMock->expects($this->once())->method('isEnabled')->willReturn(true); $this->taxHelperMock->expects($this->any())->method('isCatalogPriceDisplayAffectedByTax')->willReturn(true); $address = $this->objectManager->getObject('Magento\\Customer\\Model\\Address'); $address->setIsDefaultShipping(true); $address->setIsDefaultBilling(true); $address->setIsPrimaryBilling(true); $address->setIsPrimaryShipping(true); $address->setCountryId(1); $address->setData('postcode', 11111); $this->customerSessionMock->expects($this->once())->method('setDefaultTaxBillingAddress')->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); $this->customerSessionMock->expects($this->once())->method('setDefaultTaxShippingAddress')->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); $this->observerMock->expects($this->once())->method('getCustomerAddress')->willReturn($address); $this->session->afterAddressSave($this->observerMock); }