/** * Product view action * * @return \Magento\Framework\Controller\Result\Forward|\Magento\Framework\Controller\Result\Redirect */ public function execute() { // Get initial data from request $categoryId = (int) $this->getRequest()->getParam('category', false); $productId = (int) $this->getRequest()->getParam('id'); $specifyOptions = $this->getRequest()->getParam('options'); if ($this->getRequest()->isPost() && $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) { $product = $this->_initProduct(); if (!$product) { return $this->noProductRedirect(); } if ($specifyOptions) { $notice = $product->getTypeInstance()->getSpecifyOptionMessage(); $this->messageManager->addNotice($notice); } if ($this->getRequest()->isAjax()) { $this->getResponse()->representJson($this->_objectManager->get('Magento\\Framework\\Json\\Helper\\Data')->jsonEncode(['backUrl' => $this->_redirect->getRedirectUrl()])); return; } $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setRefererOrBaseUrl(); return $resultRedirect; } // Prepare helper and params $params = new \Magento\Framework\DataObject(); $params->setCategoryId($categoryId); $params->setSpecifyOptions($specifyOptions); // Render page try { $page = $this->resultPageFactory->create(false, ['isIsolated' => true]); $this->viewHelper->prepareAndRender($page, $productId, $this, $params); return $page; } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { return $this->noProductRedirect(); } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('noroute'); return $resultForward; } }
/** * @magentoAppIsolation enabled * @expectedException \Magento\Framework\Model\Exception */ public function testPrepareAndRenderWrongProduct() { $this->_helper->prepareAndRender(999, $this->_controller); }