예제 #1
0
파일: Send.php 프로젝트: tingyeeh/magento2
 /**
  * Show Send to a Friend Form
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     $product = $this->_initProduct();
     if (!$product) {
         /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
         $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
         $resultForward->forward('noroute');
         return $resultForward;
     }
     if ($this->sendFriend->getMaxSendsToFriend() && $this->sendFriend->isExceedLimit()) {
         $this->messageManager->addNotice(__('You can\'t send messages more than %1 times an hour.', $this->sendFriend->getMaxSendsToFriend()));
     }
     /** @var \Magento\Framework\View\Result\Page $resultPage */
     $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
     $this->_eventManager->dispatch('sendfriend_product', ['product' => $product]);
     $data = $this->catalogSession->getSendfriendFormData();
     if ($data) {
         $this->catalogSession->setSendfriendFormData(true);
         $block = $resultPage->getLayout()->getBlock('sendfriend.send');
         if ($block) {
             /** @var \Magento\SendFriend\Block\Send $block */
             $block->setFormData($data);
         }
     }
     return $resultPage;
 }
 /**
  * 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->catalogSession->clearStorage();
     }
     return $result;
 }
 public function testAfterGenerateXmlNoDepersonalize()
 {
     $this->catalogSessionMock->expects($this->never())->method('clearStorage');
     $this->depersonalizeCheckerMock->expects($this->once())->method('checkIfDepersonalize')->willReturn(false);
     $actualResult = $this->plugin->afterGenerateXml($this->layoutMock, $this->resultLayout);
     $this->assertEquals($this->resultLayout, $actualResult);
 }
예제 #4
0
 public function testExecuteWithItems()
 {
     $this->request->expects($this->any())->method('getParam')->willReturnMap([['items', null, '1,2,3'], ['uenc', null, null]]);
     $this->decoderMock->expects($this->never())->method('decode');
     $this->catalogSession->expects($this->never())->method('setBeforeCompareUrl');
     $this->listCompareMock->expects($this->once())->method('addProducts')->with([1, 2, 3]);
     $redirect = $this->getMock('Magento\\Framework\\Controller\\Result\\Redirect', ['setPath'], [], '', false);
     $redirect->expects($this->once())->method('setPath')->with('*/*/*');
     $this->redirectFactoryMock->expects($this->once())->method('create')->willReturn($redirect);
     $this->index->execute();
 }
예제 #5
0
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testExecuteWithException()
 {
     $productId = 11;
     $categoryId = 5;
     $sender = 'sender';
     $recipients = 'recipients';
     $formData = ['sender' => $sender, 'recipients' => $recipients];
     $redirectUrl = 'redirect_url';
     /** @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
     $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock();
     $this->resultFactoryMock->expects($this->once())->method('create')->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT, [])->willReturn($redirectMock);
     $this->validatorMock->expects($this->once())->method('validate')->with($this->requestMock)->willReturn(true);
     $this->requestMock->expects($this->exactly(2))->method('getParam')->willReturnMap([['id', null, $productId], ['cat_id', null, $categoryId]]);
     /** @var \Magento\Catalog\Api\Data\ProductInterface|\PHPUnit_Framework_MockObject_MockObject $productMock */
     $productMock = $this->getMockBuilder('Magento\\Catalog\\Api\\Data\\ProductInterface')->setMethods(['isVisibleInCatalog', 'setCategory', 'getProductUrl'])->getMockForAbstractClass();
     $this->productRepositoryMock->expects($this->once())->method('getById')->with($productId, false, null, false)->willReturn($productMock);
     $productMock->expects($this->once())->method('isVisibleInCatalog')->willReturn(true);
     $this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId, null)->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('No Category Exception.')));
     $productMock->expects($this->never())->method('setCategory');
     $this->registryMock->expects($this->once())->method('register')->willReturnMap([['product', $productMock, false, null]]);
     $this->requestMock->expects($this->once())->method('getPostValue')->willReturn($formData);
     $this->requestMock->expects($this->exactly(2))->method('getPost')->willReturnMap([['sender', $sender], ['recipients', $recipients]]);
     $this->sendFriendMock->expects($this->once())->method('setSender')->with($sender)->willReturnSelf();
     $this->sendFriendMock->expects($this->once())->method('setRecipients')->with($recipients)->willReturnSelf();
     $this->sendFriendMock->expects($this->once())->method('setProduct')->with($productMock)->willReturnSelf();
     $exception = new \Exception(__('Exception.'));
     $this->sendFriendMock->expects($this->once())->method('validate')->willThrowException($exception);
     $this->sendFriendMock->expects($this->never())->method('send');
     $this->messageManagerMock->expects($this->once())->method('addException')->with($exception, __('Some emails were not sent.'))->willReturnSelf();
     $this->catalogSessionMock->expects($this->once())->method('setSendfriendFormData')->with($formData);
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('sendfriend/product/send', ['_current' => true])->willReturn($redirectUrl);
     $this->redirectMock->expects($this->once())->method('error')->with($redirectUrl)->willReturnArgument(0);
     $redirectMock->expects($this->once())->method('setUrl')->with($redirectUrl)->willReturnSelf();
     $this->assertEquals($redirectMock, $this->model->execute());
 }
예제 #6
0
 /**
  * Memorize parameter value for session.
  *
  * @param string $param Parameter name.
  * @param string $value Parameter value.
  *
  * @return $this
  */
 protected function memorizeParam($param, $value)
 {
     if ($this->paramsMemorizeAllowed && !$this->session->getParamsMemorizeDisabled()) {
         $this->session->setData($param, $value);
     }
     return $this;
 }
예제 #7
0
 /**
  * Retrieve Visitor/Customer Last Viewed URL
  *
  * @return string
  */
 public function getLastViewedUrl()
 {
     $productId = $this->_catalogSession->getLastViewedProductId();
     if ($productId) {
         try {
             $product = $this->productRepository->getById($productId);
         } catch (NoSuchEntityException $e) {
             return '';
         }
         /* @var $product \Magento\Catalog\Model\Product */
         if ($this->_catalogProduct->canShow($product, 'catalog')) {
             return $product->getProductUrl();
         }
     }
     $categoryId = $this->_catalogSession->getLastViewedCategoryId();
     if ($categoryId) {
         try {
             $category = $this->categoryRepository->get($categoryId);
         } catch (NoSuchEntityException $e) {
             return '';
         }
         /* @var $category \Magento\Catalog\Model\Category */
         if (!$this->_catalogCategory->canShow($category)) {
             return '';
         }
         return $category->getCategoryUrl();
     }
     return '';
 }
예제 #8
0
파일: View.php 프로젝트: aiesh/magento2
 /**
  * Prepares product view page - inits layout and all needed stuff
  *
  * $params can have all values as $params in \Magento\Catalog\Helper\Product - initProduct().
  * Plus following keys:
  *   - 'buy_request' - \Magento\Framework\Object holding buyRequest to configure product
  *   - 'specify_options' - boolean, whether to show 'Specify options' message
  *   - 'configure_mode' - boolean, whether we're in Configure-mode to edit product configuration
  *
  * @param int $productId
  * @param \Magento\Framework\App\Action\Action $controller
  * @param null|\Magento\Framework\Object $params
  *
  * @return \Magento\Catalog\Helper\Product\View
  * @throws \Magento\Framework\Model\Exception
  */
 public function prepareAndRender($productId, $controller, $params = null)
 {
     // Prepare data
     $productHelper = $this->_catalogProduct;
     if (!$params) {
         $params = new \Magento\Framework\Object();
     }
     // Standard algorithm to prepare and render product view page
     $product = $productHelper->initProduct($productId, $controller, $params);
     if (!$product) {
         throw new \Magento\Framework\Model\Exception(__('Product is not loaded'), $this->ERR_NO_PRODUCT_LOADED);
     }
     $buyRequest = $params->getBuyRequest();
     if ($buyRequest) {
         $productHelper->prepareProductOptions($product, $buyRequest);
     }
     if ($params->hasConfigureMode()) {
         $product->setConfigureMode($params->getConfigureMode());
     }
     $this->_eventManager->dispatch('catalog_controller_product_view', array('product' => $product));
     $this->_catalogSession->setLastViewedProductId($product->getId());
     $this->initProductLayout($product, $controller, $params);
     if ($controller instanceof \Magento\Catalog\Controller\Product\View\ViewInterface) {
         $this->_view->getLayout()->initMessages($this->messageGroups);
     } else {
         throw new \Magento\Framework\Model\Exception(__('Bad controller interface for showing product'), $this->ERR_BAD_CONTROLLER_INTERFACE);
     }
     $this->_view->renderLayout();
     return $this;
 }
예제 #9
0
 /**
  * Prepares product view page - inits layout and all needed stuff
  *
  * $params can have all values as $params in \Magento\Catalog\Helper\Product - initProduct().
  * Plus following keys:
  *   - 'buy_request' - \Magento\Framework\Object holding buyRequest to configure product
  *   - 'specify_options' - boolean, whether to show 'Specify options' message
  *   - 'configure_mode' - boolean, whether we're in Configure-mode to edit product configuration
  *
  * @param \Magento\Framework\View\Result\Page $resultPage
  * @param int $productId
  * @param \Magento\Framework\App\Action\Action $controller
  * @param null|\Magento\Framework\Object $params
  * @throws \Magento\Framework\Exception\LocalizedException
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  * @return \Magento\Catalog\Helper\Product\View
  */
 public function prepareAndRender(ResultPage $resultPage, $productId, $controller, $params = null)
 {
     // Prepare data
     $productHelper = $this->_catalogProduct;
     if (!$params) {
         $params = new \Magento\Framework\Object();
     }
     // Standard algorithm to prepare and render product view page
     $product = $productHelper->initProduct($productId, $controller, $params);
     if (!$product) {
         throw new \Magento\Framework\Exception\NoSuchEntityException(__('Product is not loaded'));
     }
     $buyRequest = $params->getBuyRequest();
     if ($buyRequest) {
         $productHelper->prepareProductOptions($product, $buyRequest);
     }
     if ($params->hasConfigureMode()) {
         $product->setConfigureMode($params->getConfigureMode());
     }
     $this->_eventManager->dispatch('catalog_controller_product_view', ['product' => $product]);
     $this->_catalogSession->setLastViewedProductId($product->getId());
     $this->initProductLayout($resultPage, $product, $params);
     if ($controller instanceof \Magento\Catalog\Controller\Product\View\ViewInterface) {
         $resultPage->getLayout()->initMessages($this->messageGroups);
     } else {
         throw new \Magento\Framework\Exception\LocalizedException(__('Bad controller interface for showing product'));
     }
     return $this;
 }
예제 #10
0
파일: Compare.php 프로젝트: aiesh/magento2
 /**
  * Retrieve count of items in compare list
  *
  * @return int
  */
 public function getItemCount()
 {
     if (!$this->_catalogSession->hasCatalogCompareItemsCount()) {
         $this->calculate();
     }
     return $this->_catalogSession->getCatalogCompareItemsCount();
 }
예제 #11
0
 /**
  * Send Email Post Action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute()
 {
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     if (!$this->_formKeyValidator->validate($this->getRequest())) {
         $resultRedirect->setPath('sendfriend/product/send', ['_current' => true]);
         return $resultRedirect;
     }
     $product = $this->_initProduct();
     $data = $this->getRequest()->getPostValue();
     if (!$product || !$data) {
         /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
         $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
         $resultForward->forward('noroute');
         return $resultForward;
     }
     $categoryId = $this->getRequest()->getParam('cat_id', null);
     if ($categoryId) {
         try {
             $category = $this->categoryRepository->get($categoryId);
         } catch (NoSuchEntityException $noEntityException) {
             $category = null;
         }
         if ($category) {
             $product->setCategory($category);
             $this->_coreRegistry->register('current_category', $category);
         }
     }
     $this->sendFriend->setSender($this->getRequest()->getPost('sender'));
     $this->sendFriend->setRecipients($this->getRequest()->getPost('recipients'));
     $this->sendFriend->setProduct($product);
     try {
         $validate = $this->sendFriend->validate();
         if ($validate === true) {
             $this->sendFriend->send();
             $this->messageManager->addSuccess(__('The link to a friend was sent.'));
             $url = $product->getProductUrl();
             $resultRedirect->setUrl($this->_redirect->success($url));
             return $resultRedirect;
         } else {
             if (is_array($validate)) {
                 foreach ($validate as $errorMessage) {
                     $this->messageManager->addError($errorMessage);
                 }
             } else {
                 $this->messageManager->addError(__('We found some problems with the data.'));
             }
         }
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addError($e->getMessage());
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('Some emails were not sent.'));
     }
     // save form data
     $this->catalogSession->setSendfriendFormData($data);
     $url = $this->_url->getUrl('sendfriend/product/send', ['_current' => true]);
     $resultRedirect->setUrl($this->_redirect->error($url));
     return $resultRedirect;
 }
예제 #12
0
 public function testLayoutIsNotCacheable()
 {
     $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with($this->equalTo('Magento_PageCache'))->willReturn(true);
     $this->cacheConfigMock->expects($this->once())->method('isEnabled')->willReturn(true);
     $this->requestMock->expects($this->once($this->once()))->method('isAjax')->willReturn(false);
     $this->layoutMock->expects($this->once())->method('isCacheable')->willReturn(false);
     $this->catalogSessionMock->expects($this->never())->method('clearStorage');
     $actualResult = $this->plugin->afterGenerateXml($this->layoutMock, $this->resultLayout);
     $this->assertEquals($this->resultLayout, $actualResult);
 }
예제 #13
0
 /**
  * Inits product to be used for product controller actions and layouts
  * $params can have following data:
  *   'category_id' - id of category to check and append to product as current.
  *     If empty (except FALSE) - will be guessed (e.g. from last visited) to load as current.
  *
  * @param int $productId
  * @param \Magento\Framework\App\Action\Action $controller
  * @param \Magento\Framework\DataObject $params
  *
  * @return false|ModelProduct
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function initProduct($productId, $controller, $params = null)
 {
     // Prepare data for routine
     if (!$params) {
         $params = new \Magento\Framework\DataObject();
     }
     // Init and load product
     $this->_eventManager->dispatch('catalog_controller_product_init_before', ['controller_action' => $controller, 'params' => $params]);
     if (!$productId) {
         return false;
     }
     try {
         $product = $this->productRepository->getById($productId, false, $this->_storeManager->getStore()->getId());
     } catch (NoSuchEntityException $e) {
         return false;
     }
     if (!$this->canShow($product)) {
         return false;
     }
     if (!in_array($this->_storeManager->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
         return false;
     }
     // Load product current category
     $categoryId = $params->getCategoryId();
     if (!$categoryId && $categoryId !== false) {
         $lastId = $this->_catalogSession->getLastVisitedCategoryId();
         if ($product->canBeShowInCategory($lastId)) {
             $categoryId = $lastId;
         }
     } elseif (!$product->canBeShowInCategory($categoryId)) {
         $categoryId = null;
     }
     if ($categoryId) {
         try {
             $category = $this->categoryRepository->get($categoryId);
         } catch (NoSuchEntityException $e) {
             $category = null;
         }
         if ($category) {
             $product->setCategory($category);
             $this->_coreRegistry->register('current_category', $category);
         }
     }
     // Register current data and dispatch final events
     $this->_coreRegistry->register('current_product', $product);
     $this->_coreRegistry->register('product', $product);
     try {
         $this->_eventManager->dispatch('catalog_controller_product_init_after', ['product' => $product, 'controller_action' => $controller]);
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->_logger->critical($e);
         return false;
     }
     return $product;
 }
예제 #14
0
 /**
  * Category view action
  *
  * @return void
  */
 public function execute()
 {
     if ($this->_request->getParam(\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED)) {
         $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
         return;
     }
     $category = $this->_initCategory();
     if ($category) {
         $settings = $this->_catalogDesign->getDesignSettings($category);
         // apply custom design
         if ($settings->getCustomDesign()) {
             $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
         }
         $this->_catalogSession->setLastViewedCategoryId($category->getId());
         $update = $this->_view->getLayout()->getUpdate();
         $update->addHandle('default');
         if ($category->getIsAnchor()) {
             $type = $category->hasChildren() ? 'layered' : 'layered_without_children';
         } else {
             $type = $category->hasChildren() ? 'default' : 'default_without_children';
         }
         if (!$category->hasChildren()) {
             // Two levels removed from parent.  Need to add default page type.
             $parentType = strtok($type, '_');
             $this->_view->addPageLayoutHandles(array('type' => $parentType));
         }
         $this->_view->addPageLayoutHandles(array('type' => $type, 'id' => $category->getId()));
         // apply custom layout (page) template once the blocks are generated
         if ($settings->getPageLayout()) {
             $this->_objectManager->get('Magento\\Theme\\Helper\\Layout')->applyHandle($settings->getPageLayout());
         }
         $this->_view->loadLayoutUpdates();
         // apply custom layout update once layout is loaded
         $layoutUpdates = $settings->getLayoutUpdates();
         if ($layoutUpdates && is_array($layoutUpdates)) {
             foreach ($layoutUpdates as $layoutUpdate) {
                 $update->addUpdate($layoutUpdate);
             }
         }
         $this->_view->generateLayoutXml();
         $this->_view->generateLayoutBlocks();
         $root = $this->_view->getLayout()->getBlock('root');
         if ($root) {
             $root->addBodyClass('categorypath-' . $category->getUrlPath())->addBodyClass('category-' . $category->getUrlKey());
         }
         $this->_view->getLayout()->initMessages();
         $this->_view->renderLayout();
     } elseif (!$this->getResponse()->isRedirect()) {
         $this->_forward('noroute');
     }
 }
예제 #15
0
 /**
  * Category view action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     if ($this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)) {
         return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
     }
     $category = $this->_initCategory();
     if ($category) {
         $this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
         $settings = $this->_catalogDesign->getDesignSettings($category);
         // apply custom design
         if ($settings->getCustomDesign()) {
             $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
         }
         $this->_catalogSession->setLastViewedCategoryId($category->getId());
         $page = $this->resultPageFactory->create();
         // apply custom layout (page) template once the blocks are generated
         if ($settings->getPageLayout()) {
             $page->getConfig()->setPageLayout($settings->getPageLayout());
         }
         $hasChildren = $category->hasChildren();
         if ($category->getIsAnchor()) {
             $type = $hasChildren ? 'layered' : 'layered_without_children';
         } else {
             $type = $hasChildren ? 'default' : 'default_without_children';
         }
         if (!$hasChildren) {
             // Two levels removed from parent.  Need to add default page type.
             $parentType = strtok($type, '_');
             $page->addPageLayoutHandles(['type' => $parentType]);
         }
         $page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
         // apply custom layout update once layout is loaded
         $layoutUpdates = $settings->getLayoutUpdates();
         if ($layoutUpdates && is_array($layoutUpdates)) {
             foreach ($layoutUpdates as $layoutUpdate) {
                 $page->addUpdate($layoutUpdate);
                 $page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
             }
         }
         $page->getConfig()->addBodyClass('page-products')->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))->addBodyClass('category-' . $category->getUrlKey());
         return $page;
     } elseif (!$this->getResponse()->isRedirect()) {
         return $this->resultForwardFactory->create()->forward('noroute');
     }
 }
예제 #16
0
 /**
  * Prepares product view page - inits layout and all needed stuff
  *
  * $params can have all values as $params in \Magento\Catalog\Helper\Product - initProduct().
  * Plus following keys:
  *   - 'buy_request' - \Magento\Framework\DataObject holding buyRequest to configure product
  *   - 'specify_options' - boolean, whether to show 'Specify options' message
  *   - 'configure_mode' - boolean, whether we're in Configure-mode to edit product configuration
  *
  * @param \Magento\Framework\View\Result\Page $resultPage
  * @param int $productId
  * @param \Magento\Framework\App\Action\Action $controller
  * @param null|\Magento\Framework\DataObject $params
  * @throws \Magento\Framework\Exception\LocalizedException
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  * @return \Magento\Catalog\Helper\Product\View
  */
 public function prepareAndRender(ResultPage $resultPage, $productId, $controller, $params = null)
 {
     /**
      * Remove default action handle from layout update to avoid its usage during processing of another action,
      * It is possible that forwarding to another action occurs, e.g. to 'noroute'.
      * Default action handle is restored just before the end of current method.
      */
     $defaultActionHandle = $resultPage->getDefaultLayoutHandle();
     $handles = $resultPage->getLayout()->getUpdate()->getHandles();
     if (in_array($defaultActionHandle, $handles)) {
         $resultPage->getLayout()->getUpdate()->removeHandle($resultPage->getDefaultLayoutHandle());
     }
     if (!$controller instanceof \Magento\Catalog\Controller\Product\View\ViewInterface) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Bad controller interface for showing product'));
     }
     // Prepare data
     $productHelper = $this->_catalogProduct;
     if (!$params) {
         $params = new \Magento\Framework\DataObject();
     }
     // Standard algorithm to prepare and render product view page
     $product = $productHelper->initProduct($productId, $controller, $params);
     if (!$product) {
         throw new \Magento\Framework\Exception\NoSuchEntityException(__('Product is not loaded'));
     }
     $buyRequest = $params->getBuyRequest();
     if ($buyRequest) {
         $productHelper->prepareProductOptions($product, $buyRequest);
     }
     if ($params->hasConfigureMode()) {
         $product->setConfigureMode($params->getConfigureMode());
     }
     $this->_eventManager->dispatch('catalog_controller_product_view', ['product' => $product]);
     $this->_catalogSession->setLastViewedProductId($product->getId());
     if (in_array($defaultActionHandle, $handles)) {
         $resultPage->addDefaultHandle();
     }
     $this->initProductLayout($resultPage, $product, $params);
     return $this;
 }
예제 #17
0
 public function testExecuteWithNoticeAndNoData()
 {
     $productId = 11;
     $formData = null;
     $this->requestMock->expects($this->once())->method('getParam')->with('id', null)->willReturn($productId);
     /** @var \Magento\Catalog\Api\Data\ProductInterface|\PHPUnit_Framework_MockObject_MockObject $productMock */
     $productMock = $this->getMockBuilder('Magento\\Catalog\\Api\\Data\\ProductInterface')->setMethods(['isVisibleInCatalog'])->getMockForAbstractClass();
     $this->productRepositoryMock->expects($this->once())->method('getById')->with($productId, false, null, false)->willReturn($productMock);
     $productMock->expects($this->once())->method('isVisibleInCatalog')->willReturn(true);
     $this->registryMock->expects($this->once())->method('register')->with('product', $productMock, false);
     $this->sendFriendMock->expects($this->exactly(2))->method('getMaxSendsToFriend')->willReturn(11);
     $this->sendFriendMock->expects($this->once())->method('isExceedLimit')->willReturn(true);
     $this->messageManagerMock->expects($this->once())->method('addNotice')->with(__('You can\'t send messages more than %1 times an hour.', 11))->willReturnSelf();
     /** @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject $pageMock */
     $pageMock = $this->getMockBuilder('Magento\\Framework\\View\\Result\\Page')->disableOriginalConstructor()->getMock();
     $this->resultFactoryMock->expects($this->once())->method('create')->with(\Magento\Framework\Controller\ResultFactory::TYPE_PAGE, [])->willReturn($pageMock);
     $this->eventManagerMock->expects($this->once())->method('dispatch')->with('sendfriend_product', ['product' => $productMock]);
     $this->catalogSessionMock->expects($this->once())->method('getSendfriendFormData')->willReturn($formData);
     $this->catalogSessionMock->expects($this->never())->method('setSendfriendFormData');
     $pageMock->expects($this->never())->method('getLayout');
     $this->assertEquals($pageMock, $this->model->execute());
 }
예제 #18
0
 /**
  * Retrieve Visitor/Customer Last Viewed URL
  *
  * @return string
  */
 public function getLastViewedUrl()
 {
     $productId = $this->_catalogSession->getLastViewedProductId();
     if ($productId) {
         $product = $this->_productFactory->create()->load($productId);
         /* @var $product \Magento\Catalog\Model\Product */
         if ($this->_catalogProduct->canShow($product, 'catalog')) {
             return $product->getProductUrl();
         }
     }
     $categoryId = $this->_catalogSession->getLastViewedCategoryId();
     if ($categoryId) {
         $category = $this->_categoryFactory->create()->load($categoryId);
         /* @var $category \Magento\Catalog\Model\Category */
         if (!$this->_catalogCategory->canShow($category)) {
             return '';
         }
         return $category->getCategoryUrl();
     }
     return '';
 }