public function testAfterDelete() { $this->configMock->expects($this->any())->method('getValue')->willReturnMap([[Theme::XML_PATH_INVALID_CACHES, ScopeInterface::SCOPE_STORE, null, ['block_html' => 1, 'layout' => 1, 'translate' => 1]], [null, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, 'old_value']]); $this->cacheTypeListMock->expects($this->exactly(2))->method('invalidate'); $this->model->setValue('some_value'); $this->assertSame($this->model, $this->model->afterDelete()); }
/** * Invalidate full page cache * * @return void */ public function execute() { if ($this->_config->isEnabled()) { $this->_typeList->invalidate('full_page'); } return $this; }
/** * @param int $callNumber * @param string $oldValue * @dataProvider afterSaveDataProvider */ public function testAfterSave($callNumber, $oldValue) { $this->cacheTypeListMock->expects($this->exactly($callNumber))->method('invalidate'); $this->configMock->expects($this->any())->method('getValue')->willReturnMap([[Theme::XML_PATH_INVALID_CACHES, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null, ['block_html' => 1, 'layout' => 1, 'translate' => 1]], [null, \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, $oldValue]]); $this->model->setValue('some_value'); $this->assertInstanceOf(get_class($this->model), $this->model->afterSave()); }
/** * 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); }
/** * Toggle cache * */ public function execute() { $allTypes = array_keys($this->_cacheTypeList->getTypes()); $updatedTypes = 0; $disable = true; $enable = true; foreach ($allTypes as $code) { if ($this->_cacheState->isEnabled($code) && $disable) { $this->_cacheState->setEnabled($code, false); $updatedTypes++; $enable = false; } if (!$this->_cacheState->isEnabled($code) && $enable) { $this->_cacheState->setEnabled($code, true); $updatedTypes++; $disable = false; } if ($disable) { $this->_cacheTypeList->cleanType($code); } } if ($updatedTypes > 0) { $this->_cacheState->persist(); $this->messageManager->addSuccess(__("%1 cache type(s) disabled.", $updatedTypes)); } }
/** * @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; }
/** * Invalidate WebApi cache if needed. * * @param \Magento\Framework\App\Config\Value $subject * @param \Magento\Framework\App\Config\Value $result * @return \Magento\Framework\App\Config\Value * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterAfterSave(\Magento\Framework\App\Config\Value $subject, \Magento\Framework\App\Config\Value $result) { if ($subject->getPath() == \Magento\WebapiSecurity\Model\Plugin\AnonymousResourceSecurity::XML_ALLOW_INSECURE && $subject->isValueChanged()) { $this->cacheTypeList->invalidate(\Magento\Framework\App\Cache\Type\Webapi::TYPE_IDENTIFIER); } return $result; }
/** * Get array of cache types which require data refresh * * @return array */ protected function _getCacheTypesForRefresh() { $output = array(); foreach ($this->_cacheTypeList->getInvalidated() as $type) { $output[] = $type->getCacheType(); } return $output; }
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)); }
/** * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $subject * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $result * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ public function afterSave(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $subject, \Magento\Catalog\Model\ResourceModel\Eav\Attribute $result) { if ($this->swatchHelper->isSwatchAttribute($subject)) { $this->typeList->invalidate(Block::TYPE_IDENTIFIER); $this->typeList->invalidate(Collection::TYPE_IDENTIFIER); } return $result; }
/** * @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); }
/** * Set status 'invalidate' for blocks and other output caches * * @return $this */ public function afterSave() { $types = array_keys($this->_scopeConfig->getValue(self::XML_PATH_INVALID_CACHES, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); if ($this->isValueChanged()) { $this->_cacheTypeList->invalidate($types); } return $this; }
/** * @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); }
/** * Load data * * @param bool $printQuery * @param bool $logQuery * @return $this * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function loadData($printQuery = false, $logQuery = false) { if (!$this->isLoaded()) { foreach ($this->_cacheTypeList->getTypes() as $type) { $this->addItem($type); } $this->_setIsLoaded(true); } return $this; }
/** * Check whether specified cache types exist * * @param array $types * @return void * @throws \Magento\Framework\Exception\LocalizedException */ protected function _validateTypes(array $types) { if (empty($types)) { return; } $allTypes = array_keys($this->_cacheTypeList->getTypes()); $invalidTypes = array_diff($types, $allTypes); if (count($invalidTypes) > 0) { throw new LocalizedException(__('Specified cache type(s) don\'t exist: %1', join(', ', $invalidTypes))); } }
/** * 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'); } } } }
public function testFinish() { $cacheTypeListArray = array('one', 'two'); $this->_cache->expects($this->once())->method('clean'); $this->_config->expects($this->once())->method('reinit'); $this->_cacheState->expects($this->once())->method('persist'); $this->_cacheState->expects($this->exactly(count($cacheTypeListArray)))->method('setEnabled'); $this->_cacheTypeList->expects($this->once())->method('getTypes')->will($this->returnValue($cacheTypeListArray)); $this->_appState->expects($this->once())->method('setInstallDate')->with($this->greaterThanOrEqual(date('r'))); $this->_installerConfig->expects($this->once())->method('replaceTmpInstallDate')->with($this->greaterThanOrEqual(date('r'))); $this->assertSame($this->_model, $this->_model->finish()); }
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); }
/** * Initialize cache management form * * @return $this */ public function initForm() { /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('cache_enable', ['legend' => __('Cache Control')]); $fieldset->addField('all_cache', 'select', ['name' => 'all_cache', 'label' => '<strong>' . __('All Cache') . '</strong>', 'value' => 1, 'options' => ['' => __('No change'), 'refresh' => __('Refresh'), 'disable' => __('Disable'), 'enable' => __('Enable')]]); foreach ($this->cacheTypeList->getTypeLabels() as $type => $label) { $fieldset->addField('enable_' . $type, 'checkbox', ['name' => 'enable[' . $type . ']', 'label' => __($label), 'value' => 1, 'checked' => (int) $this->_cacheState->isEnabled($type)]); } $this->setForm($form); return $this; }
/** * Decorate status column values * * @param string $value * @param \Magento\Framework\Model\AbstractModel $row * @param \Magento\Backend\Block\Widget\Grid\Column $column * @param bool $isExport * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function decorateStatus($value, $row, $column, $isExport) { $invalidedTypes = $this->_cacheTypeList->getInvalidated(); if (isset($invalidedTypes[$row->getId()])) { $cell = '<span class="grid-severity-minor"><span>' . __('Invalidated') . '</span></span>'; } else { if ($row->getStatus()) { $cell = '<span class="grid-severity-notice"><span>' . $value . '</span></span>'; } else { $cell = '<span class="grid-severity-critical"><span>' . $value . '</span></span>'; } } return $cell; }
/** * Set tax ignore notification flag and redirect back * * @return \Magento\Backend\Model\View\Result\Redirect */ public function execute() { try { $path = \ClassyLlama\AvaTax\Helper\Config::XML_PATH_AVATAX_ADMIN_NOTIFICATION_IGNORE_NATIVE_TAX_RULES; $this->_objectManager->get('Magento\\Config\\Model\\ResourceModel\\Config')->saveConfig($path, 1, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0); $this->messageManager->addSuccess('Notification successfully ignored'); } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); } // clear the block html cache $this->cacheTypeList->cleanType('config'); $this->_eventManager->dispatch('adminhtml_cache_refresh_type', ['type' => 'block_html']); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setRefererUrl(); }
/** * Set tax ignore notification flag and redirect back * * @return \Magento\Framework\App\ResponseInterface */ public function execute() { $section = $this->getRequest()->getParam('section'); if ($section) { try { $path = 'tax/notification/ignore_' . $section; $this->_objectManager->get('Magento\\Config\\Model\\Resource\\Config')->saveConfig($path, 1, \Magento\Framework\App\ScopeInterface::SCOPE_DEFAULT, 0); } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); } } // clear the block html cache $this->_cacheTypeList->cleanType('block_html'); $this->_eventManager->dispatch('adminhtml_cache_refresh_type', ['type' => 'block_html']); $this->getResponse()->setRedirect($this->_redirect->getRefererUrl()); }
/** * {@inheritdoc} */ public function install(array $fixtures) { /** @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter */ $adapter = $this->resource->getConnection('core_write'); $regions = $this->loadDirectoryRegions(); foreach ($fixtures as $fileName) { $fileName = $this->fixtureManager->getFixture($fileName); if (!file_exists($fileName)) { continue; } $rows = $this->csvReader->getData($fileName); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } $regionId = $data['region'] != '*' ? $regions[$data['country']][$data['region']] : 0; try { $adapter->insert($this->tablerate->getMainTable(), ['website_id' => $this->storeManager->getWebsite()->getId(), 'dest_country_id' => $data['country'], 'dest_region_id' => $regionId, 'dest_zip' => $data['zip'], 'condition_name' => 'package_value', 'condition_value' => $data['order_subtotal'], 'price' => $data['price'], 'cost' => 0]); } catch (\Zend_Db_Statement_Exception $e) { if ($e->getCode() == self::ERROR_CODE_DUPLICATE_ENTRY) { // In case if Sample data was already installed we just skip duplicated records installation continue; } else { throw $e; } } } } $this->configWriter->save('carriers/tablerate/active', 1); $this->configWriter->save('carriers/tablerate/condition_name', 'package_value'); $this->cacheTypeList->cleanType('config'); }
/** * Invalidate related cache types * * @return $this */ protected function _invalidateCache() { if (count($this->_relatedCacheTypes)) { $this->_cacheTypeList->invalidate($this->_relatedCacheTypes); } return $this; }
/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing Tablerate:'); /** @var \Magento\Framework\DB\Adapter\AdapterInterface $connection */ $connection = $this->resource->getConnection('core'); $fixtureFile = 'OfflineShipping/tablerate.csv'; $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile); $regions = $this->loadDirectoryRegions(); /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */ $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']); foreach ($csvReader as $data) { $regionId = $data['region'] != '*' ? $regions[$data['country']][$data['region']] : 0; try { $connection->insert($this->tablerate->getMainTable(), ['website_id' => $this->storeManager->getWebsiteId(), 'dest_country_id' => $data['country'], 'dest_region_id' => $regionId, 'dest_zip' => $data['zip'], 'condition_name' => 'package_value', 'condition_value' => $data['order_subtotal'], 'price' => $data['price'], 'cost' => 0]); } catch (\Zend_Db_Statement_Exception $e) { if ($e->getCode() == self::ERROR_CODE_DUPLICATE_ENTRY) { // In case if Sample data was already installed we just skip duplicated records installation continue; } else { throw $e; } } $this->logger->logInline('.'); } $this->configWriter->save('carriers/tablerate/active', 1); $this->configWriter->save('carriers/tablerate/condition_name', 'package_value'); $this->cacheTypeList->cleanType('config'); }
/** * {@inheritdoc} * * {@inheritdoc}. In addition, it sets status 'invalidate' for config caches * * @return $this */ public function afterSave() { if ($this->isValueChanged()) { $this->cacheTypeList->invalidate(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); } return parent::afterSave(); }
/** * Parse and save edited translation * * @param array $translateParams * @return array */ public function processAjaxPost(array $translateParams) { if (!$this->_translateInline->isAllowed()) { return ['inline' => 'not allowed']; } $this->_appCache->invalidate(\Magento\Framework\App\Cache\Type\Translate::TYPE_IDENTIFIER); $this->_validateTranslationParams($translateParams); $this->_filterTranslationParams($translateParams, ['custom']); /** @var $validStoreId int */ $validStoreId = $this->_storeManager->getStore()->getId(); /** @var $resource \Magento\Translation\Model\ResourceModel\StringUtils */ $resource = $this->_resourceFactory->create(); foreach ($translateParams as $param) { if ($this->_appState->getAreaCode() == \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) { $storeId = 0; } else { if (empty($param['perstore'])) { $resource->deleteTranslate($param['original'], null, false); $storeId = 0; } else { $storeId = $validStoreId; } } $resource->saveTranslate($param['original'], $param['custom'], null, $storeId); } return $this->getCacheManger()->updateAndGetTranslations(); }
/** * @param int $callNumber * @param string $oldValue * @dataProvider afterSaveDataProvider */ public function testAfterSave($callNumber, $oldValue) { $this->cacheTypeListMock->expects($this->exactly($callNumber))->method('invalidate'); $this->configMock->expects($this->any())->method('getValue')->willReturn($oldValue); $this->model->setValue('some_value'); $this->assertInstanceOf(get_class($this->model), $this->model->afterSave()); }
/** * Clear translate cache * * @return $this */ protected function clearCache() { // clear cache for frontend foreach ($this->_cacheTypes as $cacheType) { $this->_cacheTypeList->invalidate($cacheType); } return $this; }