Skip to content

Commit 8de80d7

Browse files
committed
MAGETWO-83169: Loaded quote items collection contains removed product items
1 parent 2275b89 commit 8de80d7

File tree

3 files changed

+81
-31
lines changed

3 files changed

+81
-31
lines changed

app/code/Magento/Quote/Model/ResourceModel/Quote/Item/Collection.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,6 @@ public function resetJoinQuotes($quotesTableName, $productId = null)
147147
return $this;
148148
}
149149

150-
/**
151-
* Join product entities to select existing products items only
152-
*
153-
* @return void
154-
*/
155-
protected function _beforeLoad()
156-
{
157-
parent::_beforeLoad();
158-
$this->join(
159-
['cpe' => $this->getResource()->getTable('catalog_product_entity')],
160-
"cpe.entity_id = main_table.product_id",
161-
[]
162-
);
163-
}
164-
165150
/**
166151
* After load processing
167152
*
@@ -171,16 +156,29 @@ protected function _afterLoad()
171156
{
172157
parent::_afterLoad();
173158

174-
/**
175-
* Assign parent items
176-
*/
159+
$productIds = [];
177160
foreach ($this as $item) {
161+
// Assign parent items
178162
if ($item->getParentItemId()) {
179163
$item->setParentItem($this->getItemById($item->getParentItemId()));
180164
}
181165
if ($this->_quote) {
182166
$item->setQuote($this->_quote);
183167
}
168+
// Collect quote products ids
169+
$productIds[] = (int)$item->getProductId();
170+
}
171+
$this->_productIds = array_merge($this->_productIds, $productIds);
172+
$productCollection = $this->_productCollectionFactory->create()->addIdFilter($this->_productIds);
173+
$existingProductsIds = $productCollection->getAllIds();
174+
$absentProductsIds = array_diff($this->_productIds, $existingProductsIds);
175+
// Remove not existing products from items collection
176+
if (!empty($absentProductsIds)) {
177+
foreach ($absentProductsIds as $productIdToExclude) {
178+
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
179+
$quoteItem = $this->getItemByColumnValue('product_id', $productIdToExclude);
180+
$this->removeItemByKey($quoteItem->getId());
181+
}
184182
}
185183

186184
/**

dev/tests/integration/testsuite/Magento/Quote/Model/QuoteManagementTest.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
use Magento\Catalog\Model\Product\Type;
99
use Magento\TestFramework\Helper\Bootstrap;
1010

11+
/**
12+
* Class for testing QuoteManagement model
13+
*/
1114
class QuoteManagementTest extends \PHPUnit\Framework\TestCase
1215
{
1316
/**
1417
* Create order with product that has child items
1518
*
19+
* @magentoAppIsolation enabled
1620
* @magentoDataFixture Magento/Sales/_files/quote_with_bundle.php
1721
*/
1822
public function testSubmit()
1923
{
2024
/**
2125
* Preconditions:
22-
* Load quote with Bundle product that has at least to child products
26+
* Load quote with Bundle product that has at least two child products
2327
*/
2428
$objectManager = Bootstrap::getObjectManager();
2529
/** @var \Magento\Quote\Model\Quote $quote */
@@ -28,8 +32,11 @@ public function testSubmit()
2832

2933
/** Execute SUT */
3034
/** @var \Magento\Quote\Api\CartManagementInterface $model */
31-
$model = $objectManager->create(\Magento\Quote\Api\CartManagementInterface::class);
32-
$order = $model->submit($quote);
35+
$cartManagement = $objectManager->create(\Magento\Quote\Api\CartManagementInterface::class);
36+
/** @var \Magento\Sales\Api\OrderRepositoryInterface $orderRepository */
37+
$orderRepository = $objectManager->create(\Magento\Sales\Api\OrderRepositoryInterface::class);
38+
$orderId = $cartManagement->placeOrder($quote->getId());
39+
$order = $orderRepository->get($orderId);
3340

3441
/** Check if SUT caused expected effects */
3542
$orderItems = $order->getItems();
@@ -41,4 +48,45 @@ public function testSubmit()
4148
}
4249
}
4350
}
51+
52+
/**
53+
* Create order with product that has child items and one of them was deleted
54+
*
55+
* @magentoAppArea adminhtml
56+
* @magentoAppIsolation enabled
57+
* @magentoDataFixture Magento/Sales/_files/quote_with_bundle.php
58+
*/
59+
public function testSubmitWithDeletedItem()
60+
{
61+
/**
62+
* Preconditions:
63+
* Load quote with Bundle product that have at least to child products
64+
*/
65+
$objectManager = Bootstrap::getObjectManager();
66+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
67+
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
68+
$product = $productRepository->get('simple-2');
69+
$productRepository->delete($product);
70+
/** @var \Magento\Quote\Model\Quote $quote */
71+
$quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
72+
$quote->load('test01', 'reserved_order_id');
73+
74+
/** Execute SUT */
75+
/** @var \Magento\Quote\Api\CartManagementInterface $model */
76+
$cartManagement = $objectManager->create(\Magento\Quote\Api\CartManagementInterface::class);
77+
/** @var \Magento\Sales\Api\OrderRepositoryInterface $orderRepository */
78+
$orderRepository = $objectManager->create(\Magento\Sales\Api\OrderRepositoryInterface::class);
79+
$orderId = $cartManagement->placeOrder($quote->getId());
80+
$order = $orderRepository->get($orderId);
81+
82+
/** Check if SUT caused expected effects */
83+
$orderItems = $order->getItems();
84+
$this->assertCount(2, $orderItems);
85+
foreach ($orderItems as $orderItem) {
86+
if ($orderItem->getProductType() == Type::TYPE_SIMPLE) {
87+
$this->assertNotEmpty($orderItem->getParentItem(), 'Parent is not set for child product');
88+
$this->assertNotEmpty($orderItem->getParentItemId(), 'Parent is not set for child product');
89+
}
90+
}
91+
}
4492
}

dev/tests/integration/testsuite/Magento/Sales/_files/quote_with_bundle_rollback.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
78
/** @var \Magento\Framework\Registry $registry */
8-
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
9+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
910
$registry->unregister('isSecureArea');
1011
$registry->register('isSecureArea', true);
1112

13+
// Delete quote
1214
/** @var $quote \Magento\Quote\Model\Quote */
13-
$quote = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Quote\Model\Quote::class);
15+
$quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
1416
$quote->load('test01', 'reserved_order_id');
1517
if ($quote->getId()) {
1618
$quote->delete();
1719
}
18-
19-
/** @var $product \Magento\Catalog\Model\Product */
20-
$productIds = [1, 2, 3];
21-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
22-
foreach ($productIds as $productId) {
23-
$product->load($productId);
24-
if ($product->getId()) {
25-
$product->delete();
20+
// Delete products
21+
$productSkus = ['simple-1', 'simple-2', 'bundle-product'];
22+
/** @var Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
23+
$productRepository = $objectManager->get(Magento\Catalog\Api\ProductRepositoryInterface::class);
24+
foreach ($productSkus as $sku) {
25+
try {
26+
$product = $productRepository->get($sku, false, null, true);
27+
$productRepository->delete($product);
28+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
29+
//Product already removed
2630
}
2731
}
2832

0 commit comments

Comments
 (0)