PlaceOrderOnNotDefaultStockTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySales\Test\Integration\Order;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Framework\Api\SearchCriteriaBuilder;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Registry;
  12. use Magento\InventoryApi\Api\Data\StockInterface;
  13. use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
  14. use Magento\InventoryConfigurationApi\Api\SaveStockItemConfigurationInterface;
  15. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  16. use Magento\Quote\Api\CartManagementInterface;
  17. use Magento\Quote\Api\CartRepositoryInterface;
  18. use Magento\Quote\Api\Data\CartInterface;
  19. use Magento\Quote\Api\Data\CartItemInterface;
  20. use Magento\Quote\Api\Data\CartItemInterfaceFactory;
  21. use Magento\Sales\Api\OrderManagementInterface;
  22. use Magento\Sales\Api\OrderRepositoryInterface;
  23. use Magento\Store\Api\Data\StoreInterface;
  24. use Magento\Store\Api\StoreRepositoryInterface;
  25. use Magento\Store\Model\StoreManagerInterface;
  26. use PHPUnit\Framework\TestCase;
  27. use Magento\TestFramework\Helper\Bootstrap;
  28. use Magento\InventoryReservationsApi\Model\CleanupReservationsInterface;
  29. use Magento\Catalog\Api\ProductRepositoryInterface;
  30. use Magento\InventoryApi\Api\StockRepositoryInterface;
  31. /**
  32. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  33. */
  34. class PlaceOrderOnNotDefaultStockTest extends TestCase
  35. {
  36. /**
  37. * @var CleanupReservationsInterface
  38. */
  39. private $cleanupReservations;
  40. /**
  41. * @var ProductRepositoryInterface
  42. */
  43. private $productRepository;
  44. /**
  45. * @var CartManagementInterface
  46. */
  47. private $cartManagement;
  48. /**
  49. * @var CartRepositoryInterface
  50. */
  51. private $cartRepository;
  52. /**
  53. * @var CartItemInterfaceFactory
  54. */
  55. private $cartItemFactory;
  56. /**
  57. * @var StockRepositoryInterface
  58. */
  59. private $stockRepository;
  60. /**
  61. * @var StoreRepositoryInterface
  62. */
  63. private $storeRepository;
  64. /**
  65. * @var SearchCriteriaBuilder
  66. */
  67. private $searchCriteriaBuilder;
  68. /**
  69. * @var OrderRepositoryInterface
  70. */
  71. private $orderRepository;
  72. /**
  73. * @var Registry
  74. */
  75. private $registry;
  76. /**
  77. * @var OrderManagementInterface
  78. */
  79. private $orderManagement;
  80. /**
  81. * @var StoreManagerInterface
  82. */
  83. private $storeManager;
  84. /**
  85. * @var GetStockItemConfigurationInterface
  86. */
  87. private $getStockItemConfiguration;
  88. /**
  89. * @var SaveStockItemConfigurationInterface
  90. */
  91. private $saveStockItemConfiguration;
  92. protected function setUp()
  93. {
  94. $this->registry = Bootstrap::getObjectManager()->get(Registry::class);
  95. $this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
  96. $this->storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
  97. $this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
  98. $this->cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
  99. $this->cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
  100. $this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
  101. $this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
  102. $this->cartItemFactory = Bootstrap::getObjectManager()->get(CartItemInterfaceFactory::class);
  103. $this->cleanupReservations = Bootstrap::getObjectManager()->get(CleanupReservationsInterface::class);
  104. $this->orderRepository = Bootstrap::getObjectManager()->get(OrderRepositoryInterface::class);
  105. $this->orderManagement = Bootstrap::getObjectManager()->get(OrderManagementInterface::class);
  106. $this->getStockItemConfiguration =
  107. Bootstrap::getObjectManager()->get(GetStockItemConfigurationInterface::class);
  108. $this->saveStockItemConfiguration =
  109. Bootstrap::getObjectManager()->get(SaveStockItemConfigurationInterface::class);
  110. }
  111. /**
  112. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  113. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  114. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  115. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  116. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  117. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  118. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  119. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
  120. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  121. *
  122. * @magentoDbIsolation disabled
  123. */
  124. public function testPlaceOrderWithInStockProduct()
  125. {
  126. $sku = 'SKU-2';
  127. $stockId = 30;
  128. $quoteItemQty = 2.2;
  129. $this->setStockItemConfigIsDecimal($sku, $stockId);
  130. $cart = $this->getCartByStockId($stockId);
  131. $product = $this->productRepository->get($sku);
  132. $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
  133. $cart->addItem($cartItem);
  134. $this->cartRepository->save($cart);
  135. $orderId = $this->cartManagement->placeOrder($cart->getId());
  136. self::assertNotNull($orderId);
  137. $this->deleteOrderById((int)$orderId);
  138. }
  139. /**
  140. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  141. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  142. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  143. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  144. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  145. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  146. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  147. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
  148. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  149. *
  150. * @magentoDbIsolation disabled
  151. */
  152. public function testPlaceOrderWithOutOffStockProduct()
  153. {
  154. $sku = 'SKU-2';
  155. $stockId = 30;
  156. $quoteItemQty = 6.2;
  157. $this->setStockItemConfigIsDecimal($sku, $stockId);
  158. $cart = $this->getCartByStockId($stockId);
  159. $product = $this->productRepository->get($sku);
  160. $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
  161. $cart->addItem($cartItem);
  162. $this->cartRepository->save($cart);
  163. self::expectException(LocalizedException::class);
  164. $orderId = $this->cartManagement->placeOrder($cart->getId());
  165. self::assertNull($orderId);
  166. }
  167. /**
  168. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  169. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  170. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  171. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  172. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  173. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  174. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  175. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
  176. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  177. * @magentoConfigFixture store_for_global_website_store cataloginventory/item_options/backorders 1
  178. *
  179. * @magentoDbIsolation disabled
  180. */
  181. public function testPlaceOrderWithOutOffStockProductAndBackOrdersTurnedOn()
  182. {
  183. $sku = 'SKU-2';
  184. $stockId = 30;
  185. $quoteItemQty = 6.5;
  186. $this->setStockItemConfigIsDecimal($sku, $stockId);
  187. $cart = $this->getCartByStockId($stockId);
  188. $product = $this->productRepository->get($sku);
  189. $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
  190. $cart->addItem($cartItem);
  191. $this->cartRepository->save($cart);
  192. $orderId = $this->cartManagement->placeOrder($cart->getId());
  193. self::assertNotNull($orderId);
  194. //cleanup
  195. $this->deleteOrderById((int)$orderId);
  196. }
  197. /**
  198. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  199. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  200. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  201. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  202. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  203. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  204. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  205. * @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/quote.php
  206. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  207. * @magentoConfigFixture current_store cataloginventory/item_options/manage_stock 0
  208. *
  209. * @magentoDbIsolation disabled
  210. */
  211. public function testPlaceOrderWithOutOffStockProductAndManageStockTurnedOff()
  212. {
  213. $sku = 'SKU-2';
  214. $stockId = 30;
  215. $quoteItemQty = 6.5;
  216. $this->setStockItemConfigIsDecimal($sku, $stockId);
  217. $cart = $this->getCartByStockId($stockId);
  218. $product = $this->productRepository->get($sku);
  219. $cartItem = $this->getCartItem($product, $quoteItemQty, (int)$cart->getId());
  220. $cart->addItem($cartItem);
  221. $this->cartRepository->save($cart);
  222. $orderId = $this->cartManagement->placeOrder($cart->getId());
  223. self::assertNotNull($orderId);
  224. //cleanup
  225. $this->deleteOrderById((int)$orderId);
  226. }
  227. /**
  228. * @param int $stockId
  229. * @return CartInterface
  230. */
  231. private function getCartByStockId(int $stockId): CartInterface
  232. {
  233. $searchCriteria = $this->searchCriteriaBuilder
  234. ->addFilter('reserved_order_id', 'test_order_1')
  235. ->create();
  236. /** @var CartInterface $cart */
  237. $cart = current($this->cartRepository->getList($searchCriteria)->getItems());
  238. /** @var StockInterface $stock */
  239. $stock = $this->stockRepository->get($stockId);
  240. /** @var SalesChannelInterface[] $salesChannels */
  241. $salesChannels = $stock->getExtensionAttributes()->getSalesChannels();
  242. $storeCode = 'store_for_';
  243. foreach ($salesChannels as $salesChannel) {
  244. if ($salesChannel->getType() == SalesChannelInterface::TYPE_WEBSITE) {
  245. $storeCode .= $salesChannel->getCode();
  246. break;
  247. }
  248. }
  249. /** @var StoreInterface $store */
  250. $store = $this->storeRepository->get($storeCode);
  251. $this->storeManager->setCurrentStore($storeCode);
  252. $cart->setStoreId($store->getId());
  253. return $cart;
  254. }
  255. /**
  256. * @param int $orderId
  257. */
  258. private function deleteOrderById(int $orderId)
  259. {
  260. $this->registry->unregister('isSecureArea');
  261. $this->registry->register('isSecureArea', true);
  262. $this->orderManagement->cancel($orderId);
  263. $this->orderRepository->delete($this->orderRepository->get($orderId));
  264. $this->registry->unregister('isSecureArea');
  265. $this->registry->register('isSecureArea', false);
  266. }
  267. /**
  268. * @param ProductInterface $product
  269. * @param float $quoteItemQty
  270. * @param int $cartId
  271. * @return CartItemInterface
  272. */
  273. private function getCartItem(ProductInterface $product, float $quoteItemQty, int $cartId): CartItemInterface
  274. {
  275. /** @var CartItemInterface $cartItem */
  276. $cartItem =
  277. $this->cartItemFactory->create(
  278. [
  279. 'data' => [
  280. CartItemInterface::KEY_SKU => $product->getSku(),
  281. CartItemInterface::KEY_QTY => $quoteItemQty,
  282. CartItemInterface::KEY_QUOTE_ID => $cartId,
  283. 'product_id' => $product->getId(),
  284. 'product' => $product
  285. ]
  286. ]
  287. );
  288. return $cartItem;
  289. }
  290. /**
  291. * @param $sku
  292. * @param $stockId
  293. */
  294. private function setStockItemConfigIsDecimal(string $sku, int $stockId): void
  295. {
  296. $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
  297. $stockItemConfiguration->setIsQtyDecimal(true);
  298. $this->saveStockItemConfiguration->execute($sku, $stockId, $stockItemConfiguration);
  299. }
  300. protected function tearDown()
  301. {
  302. $this->cleanupReservations->execute();
  303. }
  304. }