/** * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute() { $customerId = 1; $itemId = 5; $itemQty = 123; $productId = 321; $eventObserver = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $event = $this->getMockBuilder('Magento\\Framework\\Event')->setMethods(['getCart', 'getInfo'])->disableOriginalConstructor()->getMock(); $eventObserver->expects($this->exactly(2))->method('getEvent')->willReturn($event); $quoteItem = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item')->setMethods(['getProductId', 'getBuyRequest', '__wakeup'])->disableOriginalConstructor()->getMock(); $buyRequest = $this->getMockBuilder('Magento\\Framework\\DataObject')->setMethods(['setQty'])->disableOriginalConstructor()->getMock(); $infoData = $this->getMockBuilder('Magento\\Framework\\DataObject')->setMethods(['toArray'])->disableOriginalConstructor()->getMock(); $infoData->expects($this->once())->method('toArray')->willReturn([$itemId => ['qty' => $itemQty, 'wishlist' => true]]); $cart = $this->getMockBuilder('Magento\\Checkout\\Model\\Cart')->disableOriginalConstructor()->getMock(); $quote = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->setMethods(['getCustomerId', 'getItemById', 'removeItem', '__wakeup'])->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getCart')->willReturn($cart); $event->expects($this->once())->method('getInfo')->willReturn($infoData); $cart->expects($this->any())->method('getQuote')->willReturn($quote); $quoteItem->expects($this->once())->method('getProductId')->willReturn($productId); $quoteItem->expects($this->once())->method('getBuyRequest')->willReturn($buyRequest); $buyRequest->expects($this->once())->method('setQty')->with($itemQty)->willReturnSelf(); $quote->expects($this->once())->method('getCustomerId')->willReturn($customerId); $quote->expects($this->once())->method('getItemById')->with($itemId)->willReturn($quoteItem); $quote->expects($this->once())->method('removeItem')->with($itemId); $this->wishlist->expects($this->once())->method('loadByCustomerId')->with($this->logicalOr($customerId, true))->willReturnSelf(); $this->wishlist->expects($this->once())->method('addNewItem')->with($this->logicalOr($productId, $buyRequest)); $this->wishlist->expects($this->once())->method('save'); $this->helper->expects($this->once())->method('calculate'); /** @var $eventObserver \Magento\Framework\Event\Observer */ $this->assertSame($this->observer, $this->observer->execute($eventObserver)); }
public function testExecute() { $wishlistId = 1; $customerId = 2; $url = 'http://some.pending/url'; $message = 'some error msg'; $eventObserver = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $event = $this->getMockBuilder('Magento\\Framework\\Event')->setMethods(['getRequest', 'getResponse'])->disableOriginalConstructor()->getMock(); $request = $this->getMockBuilder('Magento\\Framework\\App\\RequestInterface')->getMock(); $response = $this->getMockBuilder('Magento\\Framework\\App\\ResponseInterface')->setMethods(['setRedirect'])->getMockForAbstractClass(); $wishlists = $this->getMockBuilder('Magento\\Wishlist\\Model\\ResourceModel\\Wishlist\\Collection')->disableOriginalConstructor()->getMock(); $loadedWishlist = $this->getMockBuilder('Magento\\Wishlist\\Model\\Wishlist\\Item')->setMethods(['getId', 'delete'])->disableOriginalConstructor()->getMock(); $eventObserver->expects($this->any())->method('getEvent')->willReturn($event); $request->expects($this->any())->method('getParam')->with('wishlist_next')->willReturn(true); $event->expects($this->once())->method('getRequest')->willReturn($request); $this->checkoutSession->expects($this->once())->method('getSharedWishlist'); $this->checkoutSession->expects($this->once())->method('getWishlistPendingMessages')->willReturn([$message]); $this->checkoutSession->expects($this->once())->method('getWishlistPendingUrls')->willReturn([$url]); $this->checkoutSession->expects($this->once())->method('getWishlistIds'); $this->checkoutSession->expects($this->once())->method('getSingleWishlistId')->willReturn($wishlistId); $this->customerSession->expects($this->once())->method('isLoggedIn')->willReturn(true); $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->wishlist->expects($this->once())->method('loadByCustomerId')->with($this->logicalOr($customerId, true))->willReturnSelf(); $this->wishlist->expects($this->once())->method('getItemCollection')->willReturn($wishlists); $loadedWishlist->expects($this->once())->method('getId')->willReturn($wishlistId); $loadedWishlist->expects($this->once())->method('delete'); $wishlists->expects($this->once())->method('load')->willReturn([$loadedWishlist]); $this->checkoutSession->expects($this->once())->method('setWishlistIds')->with([])->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('setSingleWishlistId')->with(null)->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('setWishlistPendingUrls')->with([])->willReturnSelf(); $this->checkoutSession->expects($this->once())->method('setWishlistPendingMessages')->with([])->willReturnSelf(); $this->messageManager->expects($this->once())->method('addError')->with($message)->willReturnSelf(); $event->expects($this->once())->method('getResponse')->willReturn($response); $response->expects($this->once())->method('setRedirect')->with($url); $this->checkoutSession->expects($this->once())->method('setNoCartRedirect')->with(true); /** @var $eventObserver \Magento\Framework\Event\Observer */ $this->observer->execute($eventObserver); }
/** * Retrieve customer wishlist model object * * @param bool $cacheReload pass cached wishlist object and get new one * @return \Magento\Wishlist\Model\Wishlist|false Return false if customer ID is not specified */ public function getCustomerWishlist($cacheReload = false) { if (!is_null($this->_wishlist) && !$cacheReload) { return $this->_wishlist; } $customerId = (int) $this->getSession()->getCustomerId(); if ($customerId) { $this->_wishlist = $this->_objectManager->create('Magento\\Wishlist\\Model\\Wishlist'); $this->_wishlist->loadByCustomerId($customerId, true); $this->_wishlist->setStore($this->getSession()->getStore())->setSharedStoreIds($this->getSession()->getStore()->getWebsite()->getStoreIds()); } else { $this->_wishlist = false; } return $this->_wishlist; }
/** * Retrieve wishlist by logged in customer * * @return \Magento\Wishlist\Model\Wishlist */ public function getWishlist() { if (is_null($this->_wishlist)) { if ($this->_coreRegistry->registry('shared_wishlist')) { $this->_wishlist = $this->_coreRegistry->registry('shared_wishlist'); } else { $this->_wishlist = $this->wishlistProvider->getWishlist(); if (!$this->_wishlist) { $this->_wishlist = $this->_wishlistFactory->create(); if ($this->getCustomer()) { $this->_wishlist->loadByCustomerId($this->getCustomer()->getId()); } } } } return $this->_wishlist; }
/** * @param \Magento\Wishlist\Model\Wishlist $wishlist * @param array $productSkuList * @return void */ public function addProductsToWishlist(\Magento\Wishlist\Model\Wishlist $wishlist, $productSkuList) { $shouldSave = false; foreach ($productSkuList as $productSku) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productFactory->create(); $productId = $product->getIdBySku($productSku); $product->load($productId); if (empty($productId)) { continue; } elseif (!$shouldSave) { $shouldSave = true; } $buyRequest = ['product' => $productId, 'qty' => 1]; if (!$product->isVisibleInSiteVisibility()) { $parentIds = $this->productIndexer->getRelationsByChild($productId); if ($parentIds) { $buyRequest['product'] = $parentIds[0]; /** @var \Magento\Catalog\Model\Product $parentProduct */ $parentProduct = $this->productFactory->create(); $parentProduct->load($buyRequest['product']); $configurableCode = \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE; if ($parentProduct->getTypeId() == $configurableCode) { /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productType */ $productType = $parentProduct->getTypeInstance(); $buyRequest['super_attribute'] = []; foreach ($productType->getConfigurableAttributes($parentProduct) as $attribute) { $attributeCode = $attribute->getProductAttribute()->getAttributeCode(); $buyRequest['super_attribute'][$attribute->getAttributeId()] = $product->getData($attributeCode); } $product = $parentProduct; } else { continue; } } else { continue; } } $wishlist->addNewItem($product, $buyRequest, true); } if ($shouldSave) { $wishlist->save(); } }
/** * @param int|\Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemId * @param \Magento\Framework\Object $buyRequest * @param null|array|\Magento\Framework\Object $param * @throws \Magento\Framework\Exception\LocalizedException * * @dataProvider updateItemDataProvider */ public function testUpdateItem($itemId, $buyRequest, $param) { $storeId = 1; $productId = 1; $stores = [(new \Magento\Framework\Object())->setId($storeId)]; $newItem = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->getMock(); $newItem->expects($this->any())->method('setProductId')->will($this->returnSelf()); $newItem->expects($this->any())->method('setWishlistId')->will($this->returnSelf()); $newItem->expects($this->any())->method('setStoreId')->will($this->returnSelf()); $newItem->expects($this->any())->method('setOptions')->will($this->returnSelf()); $newItem->expects($this->any())->method('setProduct')->will($this->returnSelf()); $newItem->expects($this->any())->method('setQty')->will($this->returnSelf()); $newItem->expects($this->any())->method('getItem')->will($this->returnValue(2)); $this->itemFactory->expects($this->once())->method('create')->will($this->returnValue($newItem)); $this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue($stores)); $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($stores[0])); $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $product->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId)); $instanceType = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type\\AbstractType')->disableOriginalConstructor()->getMock(); $instanceType->expects($this->once())->method('processConfiguration')->will($this->returnValue($this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock())); $newProduct = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $newProduct->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $newProduct->expects($this->once())->method('getTypeInstance')->will($this->returnValue($instanceType)); $item = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->getMock(); $item->expects($this->once())->method('getProduct')->will($this->returnValue($product)); $items = $this->getMockBuilder('Magento\\Wishlist\\Model\\Resource\\Item\\Collection')->disableOriginalConstructor()->getMock(); $items->expects($this->once())->method('addWishlistFilter')->will($this->returnSelf()); $items->expects($this->once())->method('addStoreFilter')->will($this->returnSelf()); $items->expects($this->once())->method('setVisibilityFilter')->will($this->returnSelf()); $items->expects($this->once())->method('getItemById')->will($this->returnValue($item)); $items->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$item]))); $this->itemsFactory->expects($this->any())->method('create')->will($this->returnValue($items)); $this->productRepository->expects($this->once())->method('getById')->with($productId, false, $storeId)->will($this->returnValue($newProduct)); $this->assertInstanceOf('Magento\\Wishlist\\Model\\Wishlist', $this->wishlist->updateItem($itemId, $buyRequest, $param)); }
/** * @param Wishlist $wishlistItem * @return string */ public function getWishlistAddToCartLink($wishlistItem) { return $this->_urlBuilder->getUrl('wishlist/index/cart', ['item' => $wishlistItem->getId()]); }
/** * Add filter by wishlist object * * @param \Magento\Wishlist\Model\Wishlist $wishlist * @return $this */ public function addWishlistFilter(\Magento\Wishlist\Model\Wishlist $wishlist) { $this->addFieldToFilter('wishlist_id', $wishlist->getId()); return $this; }
/** * Additional function to process forming description for wishlist item * * @param \Magento\Wishlist\Model\Wishlist $wishlistModelMock * @param array $staticArgs * @return string */ protected function processWishlistItemDescription($wishlistModelMock, $staticArgs) { $imgThumbSrc = 'http://source-for-thumbnail'; $priceHtmlForTest = '<div class="price">Price is 10 for example</div>'; $productDescription = 'Product description'; $productShortDescription = 'Product short description'; $wishlistItem = $this->getMock('Magento\\Wishlist\\Model\\Item', [], [], '', false); $wishlistItemsCollection = [$wishlistItem]; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getAllowedInRss', 'getAllowedPriceInRss', 'getDescription', 'getShortDescription', 'getName', 'getVisibleInSiteVisibilities', 'getUrlModel', '__wakeup'], [], '', false); $urlModelMock = $this->getMock('Magento\\Catalog\\Model\\Product\\Url', [], [], '', false); $layoutMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\LayoutInterface', [], '', true, true, true, ['getBlock']); $wishlistModelMock->expects($this->once())->method('getItemCollection')->will($this->returnValue($wishlistItemsCollection)); $wishlistItem->expects($this->once())->method('getProduct')->will($this->returnValue($productMock)); $productMock->expects($this->once())->method('getUrlModel')->will($this->returnValue($urlModelMock)); $productMock->expects($this->once())->method('getAllowedPriceInRss')->will($this->returnValue($urlModelMock)); $urlModelMock->expects($this->once())->method('getUrl')->will($this->returnValue($staticArgs['productUrl'])); $productMock->expects($this->once())->method('getName')->will($this->returnValue($staticArgs['productName'])); $productMock->expects($this->once())->method('getAllowedInRss')->will($this->returnValue(true)); $productMock->expects($this->once())->method('getVisibleInSiteVisibilities')->will($this->returnValue(true)); $this->imageHelperMock->expects($this->once())->method('init')->will($this->returnSelf()); $this->imageHelperMock->expects($this->once())->method('resize')->will($this->returnValue($imgThumbSrc)); $priceRendererMock = $this->getMock('Magento\\Framework\\Pricing\\Render', ['render'], [], '', false); $layoutMock->expects($this->once())->method('getBlock')->will($this->returnValue($priceRendererMock)); $priceRendererMock->expects($this->once())->method('render')->will($this->returnValue($priceHtmlForTest)); $productMock->expects($this->any())->method('getDescription')->will($this->returnValue($productDescription)); $productMock->expects($this->any())->method('getShortDescription')->will($this->returnValue($productShortDescription)); $this->catalogOutputMock->expects($this->any())->method('productAttribute')->will($this->returnArgument(1)); $this->block->setLayout($layoutMock); $description = '<table><tr><td><a href="' . $staticArgs['productUrl'] . '"><img src="' . $imgThumbSrc . '" border="0" align="left" height="75" width="75"></a></td><td style="text-decoration:none;">' . $productShortDescription . '<p>' . $priceHtmlForTest . '</p><p>Comment: ' . $productDescription . '<p>' . '</td></tr></table>'; return $description; }
/** * Move all wishlist item to cart * * @param Wishlist $wishlist * @param array $qtys * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function moveAllToCart(Wishlist $wishlist, $qtys) { $isOwner = $wishlist->isOwner($this->customerSession->getCustomerId()); $messages = []; $addedItems = []; $notSalable = []; $hasOptions = []; $cart = $this->cart; $collection = $wishlist->getItemCollection()->setVisibilityFilter(); foreach ($collection as $item) { /** @var \Magento\Wishlist\Model\Item */ try { $disableAddToCart = $item->getProduct()->getDisableAddToCart(); $item->unsProduct(); // Set qty if (isset($qtys[$item->getId()])) { $qty = $this->quantityProcessor->process($qtys[$item->getId()]); if ($qty) { $item->setQty($qty); } } $item->getProduct()->setDisableAddToCart($disableAddToCart); // Add to cart if ($item->addToCart($cart, $isOwner)) { $addedItems[] = $item->getProduct(); } } catch (\Magento\Framework\Exception\LocalizedException $e) { if ($e->getCode() == \Magento\Wishlist\Model\Item::EXCEPTION_CODE_NOT_SALABLE) { $notSalable[] = $item; } elseif ($e->getCode() == \Magento\Wishlist\Model\Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) { $hasOptions[] = $item; } else { $messages[] = __('%1 for "%2".', trim($e->getMessage(), '.'), $item->getProduct()->getName()); } $cartItem = $cart->getQuote()->getItemByProduct($item->getProduct()); if ($cartItem) { $cart->getQuote()->deleteItem($cartItem); } } catch (\Exception $e) { $this->logger->critical($e); $messages[] = __('We cannot add this item to your shopping cart.'); } } if ($isOwner) { $indexUrl = $this->helper->getListUrl($wishlist->getId()); } else { $indexUrl = $this->urlBuilder->getUrl('wishlist/shared', ['code' => $wishlist->getSharingCode()]); } if ($this->cartHelper->getShouldRedirectToCart()) { $redirectUrl = $this->cartHelper->getCartUrl(); } elseif ($this->redirector->getRefererUrl()) { $redirectUrl = $this->redirector->getRefererUrl(); } else { $redirectUrl = $indexUrl; } if ($notSalable) { $products = []; foreach ($notSalable as $item) { $products[] = '"' . $item->getProduct()->getName() . '"'; } $messages[] = __('We couldn\'t add the following product(s) to the shopping cart: %1.', join(', ', $products)); } if ($hasOptions) { $products = []; foreach ($hasOptions as $item) { $products[] = '"' . $item->getProduct()->getName() . '"'; } $messages[] = __('Product(s) %1 have required options. Each product can only be added individually.', join(', ', $products)); } if ($messages) { $isMessageSole = count($messages) == 1; if ($isMessageSole && count($hasOptions) == 1) { $item = $hasOptions[0]; if ($isOwner) { $item->delete(); } $redirectUrl = $item->getProductUrl(); } else { foreach ($messages as $message) { $this->messageManager->addError($message); } $redirectUrl = $indexUrl; } } if ($addedItems) { // save wishlist model for setting date of last update try { $wishlist->save(); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t update wish list.')); $redirectUrl = $indexUrl; } $products = []; foreach ($addedItems as $product) { $products[] = '"' . $product->getName() . '"'; } $this->messageManager->addSuccess(__('%1 product(s) have been added to shopping cart: %2.', count($addedItems), join(', ', $products))); // save cart and collect totals $cart->save()->getQuote()->collectTotals(); } $this->helper->calculate(); return $redirectUrl; }
/** * Move all wishlist item to cart * * @param Wishlist $wishlist * @param array $qtys * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function moveAllToCart(Wishlist $wishlist, $qtys) { $isOwner = $wishlist->isOwner($this->customerSession->getCustomerId()); $messages = []; $addedProducts = []; $notSalable = []; $cart = $this->cart; $collection = $wishlist->getItemCollection()->setVisibilityFilter(); foreach ($collection as $item) { /** @var $item \Magento\Wishlist\Model\Item */ try { $disableAddToCart = $item->getProduct()->getDisableAddToCart(); $item->unsProduct(); // Set qty if (isset($qtys[$item->getId()])) { $qty = $this->quantityProcessor->process($qtys[$item->getId()]); if ($qty) { $item->setQty($qty); } } $item->getProduct()->setDisableAddToCart($disableAddToCart); // Add to cart if ($item->addToCart($cart, $isOwner)) { $addedProducts[] = $item->getProduct(); } } catch (LocalizedException $e) { if ($e instanceof ProductException) { $notSalable[] = $item; } else { $messages[] = __('%1 for "%2".', trim($e->getMessage(), '.'), $item->getProduct()->getName()); } $cartItem = $cart->getQuote()->getItemByProduct($item->getProduct()); if ($cartItem) { $cart->getQuote()->deleteItem($cartItem); } } catch (\Exception $e) { $this->logger->critical($e); $messages[] = __('We can\'t add this item to your shopping cart right now.'); } } if ($isOwner) { $indexUrl = $this->helper->getListUrl($wishlist->getId()); } else { $indexUrl = $this->urlBuilder->getUrl('wishlist/shared', ['code' => $wishlist->getSharingCode()]); } if ($this->cartHelper->getShouldRedirectToCart()) { $redirectUrl = $this->cartHelper->getCartUrl(); } elseif ($this->redirector->getRefererUrl()) { $redirectUrl = $this->redirector->getRefererUrl(); } else { $redirectUrl = $indexUrl; } if ($notSalable) { $products = []; foreach ($notSalable as $item) { $products[] = '"' . $item->getProduct()->getName() . '"'; } $messages[] = __('We couldn\'t add the following product(s) to the shopping cart: %1.', join(', ', $products)); } if ($messages) { foreach ($messages as $message) { $this->messageManager->addError($message); } $redirectUrl = $indexUrl; } if ($addedProducts) { // save wishlist model for setting date of last update try { $wishlist->save(); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t update the Wish List right now.')); $redirectUrl = $indexUrl; } $products = []; foreach ($addedProducts as $product) { /** @var $product \Magento\Catalog\Model\Product */ $products[] = '"' . $product->getName() . '"'; } $this->messageManager->addSuccess(__('%1 product(s) have been added to shopping cart: %2.', count($addedProducts), join(', ', $products))); // save cart and collect totals $cart->save()->getQuote()->collectTotals(); } $this->helper->calculate(); return $redirectUrl; }