/** * Clear cache after reindex all * * @param \Magento\Indexer\Model\Processor $subject * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterReindexAllInvalid(\Magento\Indexer\Model\Processor $subject) { $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->context]); if (!empty($this->context->getIdentities())) { $this->getCache()->clean($this->context->getIdentities()); } }
/** * Product variations attributes validation * * @param Product $parentProduct * @param array $products * @param RequestInterface $request * @return array */ protected function _validateProductVariations(Product $parentProduct, array $products, RequestInterface $request) { $this->eventManager->dispatch('catalog_product_validate_variations_before', ['product' => $parentProduct, 'variations' => $products]); $validationResult = []; foreach ($products as $productData) { $product = $this->productFactory->create(); $product->setData('_edit_mode', true); $storeId = $request->getParam('store'); if ($storeId) { $product->setStoreId($storeId); } $product->setAttributeSetId($parentProduct->getAttributeSetId()); $product->addData($this->getRequiredDataFromProduct($parentProduct)); $product->addData($productData); $product->setCollectExceptionMessages(true); $configurableAttribute = []; $encodedData = $productData['configurable_attribute']; if ($encodedData) { $configurableAttribute = $this->jsonHelper->jsonDecode($encodedData); } $configurableAttribute = implode('-', $configurableAttribute); $errorAttributes = $product->validate(); if (is_array($errorAttributes)) { foreach ($errorAttributes as $attributeCode => $result) { if (is_string($result)) { $key = 'variations-matrix-' . $configurableAttribute . '-' . $attributeCode; $validationResult[$key] = $result; } } } } return $validationResult; }
/** * afterUpdateMview with disabled PageCache module */ public function testAfterUpdateMviewPageCacheDisabled() { $this->moduleManager->expects($this->once())->method('isEnabled')->with($this->equalTo('Magento_PageCache'))->will($this->returnValue(false)); $this->eventManagerMock->expects($this->never())->method('dispatch'); $actualResult = $this->plugin->afterUpdateMview($this->subjectMock); $this->assertNull($actualResult); }
/** * Test afterUpdateMview * * @return void */ public function testAfterUpdateMview() { $tags = ['tag_name1', 'tag_name2']; $this->eventManagerMock->expects($this->once())->method('dispatch')->with($this->equalTo('clean_cache_after_reindex'), $this->equalTo(['object' => $this->contextMock])); $this->contextMock->expects($this->atLeastOnce())->method('getIdentities')->willReturn($tags); $this->cacheMock->expects($this->once())->method('clean')->with($tags); $this->plugin->afterUpdateMview($this->subjectMock); }
public function testSave() { $this->item->setData('key', 'value'); $this->eventManager->expects($this->at(0))->method('dispatch')->with('model_save_before', ['object' => $this->item]); $this->eventManager->expects($this->at(1))->method('dispatch')->with('cataloginventory_stock_item_save_before', ['data_object' => $this->item, 'item' => $this->item]); $this->resource->expects($this->once())->method('addCommitCallback')->will($this->returnValue($this->resource)); $this->assertEquals($this->item, $this->item->save()); }
/** * 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 testAfterGenerateXmlNoDepersonalize() { $this->depersonalizeCheckerMock->expects($this->once())->method('checkIfDepersonalize')->willReturn(false); $this->eventManagerMock->expects($this->never())->method('dispatch'); $this->messageSessionMock->expects($this->never())->method('clearStorage'); $expectedResult = $this->getMock('Magento\\Framework\\View\\Layout', [], [], '', false); $actualResult = $this->plugin->afterGenerateXml($this->layoutMock, $expectedResult); $this->assertEquals($expectedResult, $actualResult); }
/** * 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->depersonalizeChecker->checkIfDepersonalize($subject)) { $this->eventManager->dispatch('depersonalize_clear_session'); session_write_close(); $this->messageSession->clearStorage(); } return $result; }
/** * Test for method unassignState */ public function testUnassignStateSuccess() { $params = ['status' => $this->model->getStatus(), 'state' => 'test_state']; $this->resourceMock->expects($this->once())->method('checkIsStateLast')->with($this->equalTo($params['state']))->will($this->returnValue(false)); $this->resourceMock->expects($this->once())->method('checkIsStatusUsed')->with($this->equalTo($params['status']))->will($this->returnValue(false)); $this->eventManagerMock->expects($this->once())->method('dispatch')->with($this->equalTo('sales_order_status_unassign'), $this->equalTo($params)); $this->resourceMock->expects($this->once())->method('unassignState')->with($this->equalTo($params['status']), $this->equalTo($params['state'])); $this->assertEquals($this->model, $this->model->unassignState($params['state'])); }
/** * Test method afterGenerateXml with enabled module PageCache */ public function testAfterGenerateXmlPageCacheEnabled() { $expectedResult = $this->getMock('Magento\\Framework\\View\\Layout', array(), array(), '', false); $this->cacheConfigMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with($this->equalTo('Magento_PageCache'))->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->eventManagerMock->expects($this->once())->method('dispatch')->with($this->equalTo('depersonalize_clear_session')); $this->messageSessionMock->expects($this->once())->method('clearStorage'); $actualResult = $this->plugin->afterGenerateXml($this->layoutMock, $expectedResult); $this->assertEquals($expectedResult, $actualResult); }
/** * @param int $value * @param int $oldValue * @param string $eventName * @dataProvider testAfterSaveDataProvider * @return void */ public function testAfterSave($value, $oldValue, $eventName) { $path = 'dev/grid/async_indexing'; $scope = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT; $this->object->setData(['value' => $value, 'path' => $path, 'scope' => $scope]); $this->config->expects($this->once())->method('getValue')->with($path, $scope)->willReturn($oldValue); if ($value == $oldValue) { $this->eventManager->expects($this->never())->method('dispatch'); } else { $this->eventManager->expects($this->once())->method('dispatch')->with($eventName); } $this->object->afterSave(); }
/** * Test execute add success critical exception * * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecuteAddSuccessCriticalException() { $wishlist = $this->getMock('Magento\\Wishlist\\Model\\Wishlist', [], [], '', false); $product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $item = $this->getMock('Magento\\Wishlist\\Model\\Item', [], [], '', false); $helper = $this->getMock('Magento\\Wishlist\\Helper\\Data', [], [], '', false); $logger = $this->getMock('Magento\\Framework\\Logger\\Monolog', [], [], '', false); $exception = new \Exception(); $logger->expects($this->once())->method('critical')->with($exception)->willReturn(true); $helper->expects($this->exactly(2))->method('calculate')->willReturn(true); $wishlist->expects($this->once())->method('getItem')->with(3)->willReturn($item); $wishlist->expects($this->once())->method('updateItem')->with(3, new \Magento\Framework\DataObject([]))->willReturnSelf(); $wishlist->expects($this->once())->method('save')->willReturn(null); $wishlist->expects($this->once())->method('getId')->willReturn(56); $product->expects($this->once())->method('isVisibleInCatalog')->willReturn(true); $product->expects($this->once())->method('getName')->willReturn('Test name'); $this->request->expects($this->at(0))->method('getParam')->with('product', null)->willReturn(2); $this->request->expects($this->at(1))->method('getParam')->with('id', null)->willReturn(3); $this->productRepository->expects($this->once())->method('getById')->with(2)->willReturn($product); $item->expects($this->once())->method('load')->with(3)->willReturnSelf(); $item->expects($this->once())->method('__call')->with('getWishlistId')->willReturn(12); $this->wishlistProvider->expects($this->once())->method('getWishlist')->with(12)->willReturn($wishlist); $this->om->expects($this->once())->method('create')->with('Magento\\Wishlist\\Model\\Item')->willReturn($item); $this->request->expects($this->once())->method('getParams')->willReturn([]); $this->om->expects($this->at(1))->method('get')->with('Magento\\Wishlist\\Helper\\Data')->willReturn($helper); $this->om->expects($this->at(2))->method('get')->with('Magento\\Wishlist\\Helper\\Data')->willReturn($helper); $this->om->expects($this->at(3))->method('get')->with('Psr\\Log\\LoggerInterface')->willReturn($logger); $this->eventManager->expects($this->once())->method('dispatch')->with('wishlist_update_item', ['wishlist' => $wishlist, 'product' => $product, 'item' => $item])->willReturn(true); $this->messageManager->expects($this->once())->method('addSuccess')->with('Test name has been updated in your Wish List.', null)->willThrowException($exception); $this->messageManager->expects($this->once())->method('addError')->with('We can\'t update your Wish List right now.', null)->willReturn(true); $this->resultRedirectMock->expects($this->once())->method('setPath')->with('*/*', ['wishlist_id' => 56])->willReturnSelf(); $this->assertSame($this->resultRedirectMock, $this->getController()->execute()); }
/** * Run application * * @return ResponseInterface */ public function launch() { $areaCode = $this->_areaList->getCodeByFrontName($this->_request->getFrontName()); $this->_state->setAreaCode($areaCode); $this->_objectManager->configure($this->_configLoader->load($areaCode)); $this->_response = $this->_objectManager->get('Magento\\Framework\\App\\FrontControllerInterface')->dispatch($this->_request); // This event gives possibility to launch something before sending output (allow cookie setting) $eventParams = array('request' => $this->_request, 'response' => $this->_response); $this->_eventManager->dispatch('controller_front_send_response_before', $eventParams); return $this->_response; }
/** * test for method getValue with type Fixed and selectionPriceType not null */ public function testGetValueTypeFixedWithSelectionPriceType() { $this->bundleMock->expects($this->once())->method('getPriceType')->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED)); $this->bundleMock->expects($this->atLeastOnce())->method('getPriceInfo')->will($this->returnValue($this->priceInfoMock)); $this->priceInfoMock->expects($this->once())->method('getPrice')->with($this->equalTo(RegularPrice::PRICE_CODE))->will($this->returnValue($this->regularPriceMock)); $this->regularPriceMock->expects($this->once())->method('getValue')->will($this->returnValue(100)); $this->bundleMock->expects($this->once())->method('setFinalPrice')->will($this->returnSelf()); $this->eventManagerMock->expects($this->once())->method('dispatch'); $this->bundleMock->expects($this->exactly(2))->method('getData')->will($this->returnValueMap([['qty', null, 1], ['final_price', null, 100]])); $this->productMock->expects($this->once())->method('getSelectionPriceType')->will($this->returnValue(true)); $this->productMock->expects($this->any())->method('getSelectionPriceValue')->will($this->returnValue(100)); $this->discountCalculatorMock->expects($this->once())->method('calculateDiscount')->with($this->equalTo($this->bundleMock), $this->equalTo(100))->will($this->returnValue(70)); $this->assertEquals(70, $this->selectionPrice->getValue()); }
/** * Run application * * @return ResponseInterface */ public function launch() { try { $areaCode = $this->_areaList->getCodeByFrontName($this->_request->getFrontName()); $this->_state->setAreaCode($areaCode); $this->_objectManager->configure($this->_configLoader->load($areaCode)); $this->_response = $this->_objectManager->get('Magento\\Framework\\App\\FrontControllerInterface')->dispatch($this->_request); // This event gives possibility to launch something before sending output (allow cookie setting) $eventParams = array('request' => $this->_request, 'response' => $this->_response); $this->_eventManager->dispatch('controller_front_send_response_before', $eventParams); } catch (\Exception $exception) { $message = $exception->getMessage() . "\n"; try { if ($this->_state->getMode() == State::MODE_DEVELOPER) { $message .= '<pre>'; $message .= $exception->getMessage() . "\n\n"; $message .= $exception->getTraceAsString(); $message .= '</pre>'; } else { $reportData = array($exception->getMessage(), $exception->getTraceAsString()); // retrieve server data if (isset($_SERVER)) { if (isset($_SERVER['REQUEST_URI'])) { $reportData['url'] = $_SERVER['REQUEST_URI']; } if (isset($_SERVER['SCRIPT_NAME'])) { $reportData['script_name'] = $_SERVER['SCRIPT_NAME']; } } require_once $this->_filesystem->getPath(Filesystem::PUB_DIR) . '/errors/report.php'; $processor = new \Magento\Framework\Error\Processor($this->_response); $processor->saveReport($reportData); $this->_response = $processor->processReport(); } } catch (\Exception $exception) { $message .= "Unknown error happened."; } $this->_response->setHttpResponseCode(500); $this->_response->setBody($message); } return $this->_response; }
/** * Run application * * @throws \InvalidArgumentException * @return ResponseInterface */ public function launch() { $areaCode = $this->_areaList->getCodeByFrontName($this->_request->getFrontName()); $this->_state->setAreaCode($areaCode); $this->_objectManager->configure($this->_configLoader->load($areaCode)); /** @var \Magento\Framework\App\FrontControllerInterface $frontController */ $frontController = $this->_objectManager->get('Magento\\Framework\\App\\FrontControllerInterface'); $result = $frontController->dispatch($this->_request); // TODO: Temporary solution until all controllers return ResultInterface (MAGETWO-28359) if ($result instanceof ResultInterface) { $this->registry->register('use_page_cache_plugin', true, true); $result->renderResult($this->_response); } elseif ($result instanceof HttpInterface) { $this->_response = $result; } else { throw new \InvalidArgumentException('Invalid return type'); } // This event gives possibility to launch something before sending output (allow cookie setting) $eventParams = ['request' => $this->_request, 'response' => $this->_response]; $this->_eventManager->dispatch('controller_front_send_response_before', $eventParams); return $this->_response; }
/** * test for method getValue with type Fixed and selectionPriceType not null * * @param bool $useRegularPrice * @dataProvider useRegularPriceDataProvider */ public function testGetValueTypeFixedWithSelectionPriceType($useRegularPrice) { $this->setupSelectionPrice($useRegularPrice); $regularPrice = 100.125; $discountedPrice = 70.453; $actualPrice = $useRegularPrice ? $regularPrice : $discountedPrice; $expectedPrice = $useRegularPrice ? round($regularPrice, 2) : round($discountedPrice, 2); $this->bundleMock->expects($this->once())->method('getPriceType')->will($this->returnValue(\Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED)); $this->bundleMock->expects($this->atLeastOnce())->method('getPriceInfo')->will($this->returnValue($this->priceInfoMock)); $this->priceInfoMock->expects($this->once())->method('getPrice')->with($this->equalTo(RegularPrice::PRICE_CODE))->will($this->returnValue($this->regularPriceMock)); $this->regularPriceMock->expects($this->once())->method('getValue')->will($this->returnValue($actualPrice)); $this->bundleMock->expects($this->once())->method('setFinalPrice')->will($this->returnSelf()); $this->eventManagerMock->expects($this->once())->method('dispatch'); $this->bundleMock->expects($this->exactly(2))->method('getData')->will($this->returnValueMap([['qty', null, 1], ['final_price', null, 100]])); $this->productMock->expects($this->once())->method('getSelectionPriceType')->will($this->returnValue(true)); $this->productMock->expects($this->any())->method('getSelectionPriceValue')->will($this->returnValue($actualPrice)); if (!$useRegularPrice) { $this->discountCalculatorMock->expects($this->once())->method('calculateDiscount')->with($this->equalTo($this->bundleMock), $this->equalTo($actualPrice))->will($this->returnValue($discountedPrice)); } $this->priceCurrencyMock->expects($this->once())->method('round')->with($actualPrice)->will($this->returnValue($expectedPrice)); $this->assertEquals($expectedPrice, $this->selectionPrice->getValue()); }
protected function generalGetProductCollection() { $this->eventManager->expects($this->once())->method('dispatch')->will($this->returnValue(true)); $this->scopeConfig->expects($this->once())->method('getValue')->withAnyParameters()->willReturn(false); $this->cacheState->expects($this->atLeastOnce())->method('isEnabled')->withAnyParameters()->willReturn(false); $this->catalogConfig->expects($this->once())->method('getProductAttributes')->willReturn([]); $this->localDate->expects($this->any())->method('date')->willReturn(new \DateTime('now', new \DateTimeZone('UTC'))); $this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager); $this->context->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfig); $this->context->expects($this->once())->method('getCacheState')->willReturn($this->cacheState); $this->context->expects($this->once())->method('getCatalogConfig')->willReturn($this->catalogConfig); $this->context->expects($this->once())->method('getLocaleDate')->willReturn($this->localDate); $this->productCollection = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection')->setMethods(['setVisibility', 'addMinimalPrice', 'addFinalPrice', 'addTaxPercents', 'addAttributeToSelect', 'addUrlRewrite', 'addStoreFilter', 'addAttributeToSort', 'setPageSize', 'setCurPage', 'addAttributeToFilter'])->disableOriginalConstructor()->getMock(); $this->productCollection->expects($this->once())->method('setVisibility')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addMinimalPrice')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addFinalPrice')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addTaxPercents')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addAttributeToSelect')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addUrlRewrite')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addStoreFilter')->willReturnSelf(); $this->productCollection->expects($this->once())->method('addAttributeToSort')->willReturnSelf(); $this->productCollection->expects($this->atLeastOnce())->method('setCurPage')->willReturnSelf(); $this->productCollection->expects($this->any())->method('addAttributeToFilter')->willReturnSelf(); }
/** * Test afterUpdateMview * * @return void */ public function testAfterUpdateMview() { $this->eventManagerMock->expects($this->once())->method('dispatch')->with($this->equalTo('clean_cache_after_reindex'), $this->equalTo(['object' => $this->contextMock])); $this->plugin->afterUpdateMview($this->subjectMock); }
/** * Update indexer views * * @param \Magento\Indexer\Model\Processor $subject * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterUpdateMview(\Magento\Indexer\Model\Processor $subject) { if ($this->moduleManager->isEnabled('Magento_PageCache')) { $this->eventManager->dispatch('clean_cache_after_reindex', ['object' => $this->context]); } }
public function testDispatchWithEmptyEventObservers() { $this->_eventConfigMock->expects($this->once())->method('getObservers')->with('some_event')->will($this->returnValue([])); $this->_invoker->expects($this->never())->method('dispatch'); $this->_eventManager->dispatch('some_event'); }
/** * Clear cache after reindex all * * @param \Magento\Indexer\Model\Processor $subject * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterReindexAllInvalid(\Magento\Indexer\Model\Processor $subject) { $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->context]); }
/** * Run test loadByCode method */ public function testLoadByCode() { $this->eventManager->expects($this->any())->method('dispatch'); $this->resourceMock->expects($this->once())->method('load'); $this->assertEquals($this->couponModel, $this->couponModel->loadByCode('code-value')); }