|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Checkout\Block\Cart; |
| 8 | + |
| 9 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 10 | +use Magento\CatalogInventory\Api\StockItemRepositoryInterface; |
| 11 | +use Magento\CatalogInventory\Model\StockRegistryProvider; |
| 12 | +use Magento\Checkout\Model\Session; |
| 13 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 14 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | + |
| 17 | +class AbstractCartTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Tests that product in shopping cart remains visible even after it becomes out of stock. |
| 21 | + * |
| 22 | + * @magentoDataFixture Magento/Sales/_files/quote.php |
| 23 | + */ |
| 24 | + public function testProductsOutOfStockIsVisibleInShoppingCart() |
| 25 | + { |
| 26 | + $productSku = 'simple'; |
| 27 | + $reservedOrderId = 'test01'; |
| 28 | + $objectManager = Bootstrap::getObjectManager(); |
| 29 | + |
| 30 | + /** @var ProductRepositoryInterface $productRepository */ |
| 31 | + $productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 32 | + $product = $productRepository->get($productSku); |
| 33 | + |
| 34 | + $this->setProductOutOfStock($product->getId()); |
| 35 | + |
| 36 | + $quote = $this->getStoredQuote($reservedOrderId); |
| 37 | + |
| 38 | + $checkoutSession = $this->getMockBuilder(Session::class) |
| 39 | + ->disableOriginalConstructor() |
| 40 | + ->getMock(); |
| 41 | + $checkoutSession->method('getQuote')->willReturn($quote); |
| 42 | + |
| 43 | + $abstractCart = $objectManager->create( |
| 44 | + AbstractCart::class, |
| 45 | + ['checkoutSession' => $checkoutSession] |
| 46 | + ); |
| 47 | + |
| 48 | + $items = array_filter( |
| 49 | + $abstractCart->getItems(), |
| 50 | + function (\Magento\Quote\Model\Quote\Item $item) use ($productSku) { |
| 51 | + return $item->getSku() === $productSku; |
| 52 | + } |
| 53 | + ); |
| 54 | + |
| 55 | + $this->assertNotEmpty( |
| 56 | + $items, |
| 57 | + 'Product disappeared from shopping cart after it had become out of stock.' |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param int $productId |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + private function setProductOutOfStock($productId) |
| 66 | + { |
| 67 | + /** @var $stockRegistryProvider StockRegistryProvider */ |
| 68 | + $stockRegistryProvider = Bootstrap::getObjectManager()->get(StockRegistryProvider::class); |
| 69 | + $stockItem = $stockRegistryProvider->getStockItem($productId, 0); |
| 70 | + $stockItem->setIsInStock(false); |
| 71 | + |
| 72 | + /** @var $stockItemRepository StockItemRepositoryInterface */ |
| 73 | + $stockItemRepository = Bootstrap::getObjectManager()->get(StockItemRepositoryInterface::class); |
| 74 | + $stockItemRepository->save($stockItem); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param string $reservedOrderId |
| 79 | + * @return \Magento\Quote\Api\Data\CartInterface |
| 80 | + */ |
| 81 | + private function getStoredQuote($reservedOrderId) |
| 82 | + { |
| 83 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 84 | + $searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class); |
| 85 | + $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId); |
| 86 | + |
| 87 | + /** @var CartRepositoryInterface $cartRepository */ |
| 88 | + $cartRepository = Bootstrap::getObjectManager()->create(CartRepositoryInterface::class); |
| 89 | + $quoteItems = $cartRepository->getList($searchCriteriaBuilder->create())->getItems(); |
| 90 | + |
| 91 | + return current($quoteItems); |
| 92 | + } |
| 93 | +} |
0 commit comments