Skip to content

Commit f78a040

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-83631' into PANDA-FIXES-2.1
2 parents 308dd99 + 8cc2d14 commit f78a040

File tree

4 files changed

+110
-36
lines changed

4 files changed

+110
-36
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,24 @@ public function submit(QuoteEntity $quote, $orderData = [])
397397
*/
398398
protected function resolveItems(QuoteEntity $quote)
399399
{
400-
$quoteItems = [];
401-
foreach ($quote->getAllItems() as $quoteItem) {
402-
/** @var \Magento\Quote\Model\ResourceModel\Quote\Item $quoteItem */
403-
$quoteItems[$quoteItem->getId()] = $quoteItem;
404-
}
405400
$orderItems = [];
406-
foreach ($quoteItems as $quoteItem) {
407-
$parentItem = (isset($orderItems[$quoteItem->getParentItemId()])) ?
408-
$orderItems[$quoteItem->getParentItemId()] : null;
409-
$orderItems[$quoteItem->getId()] =
410-
$this->quoteItemToOrderItem->convert($quoteItem, ['parent_item' => $parentItem]);
401+
foreach ($quote->getAllItems() as $quoteItem) {
402+
$itemId = $quoteItem->getId();
403+
404+
if (!empty($orderItems[$itemId])) {
405+
continue;
406+
}
407+
408+
$parentItemId = $quoteItem->getParentItemId();
409+
/** @var \Magento\Quote\Model\ResourceModel\Quote\Item $parentItem */
410+
if ($parentItemId && !isset($orderItems[$parentItemId])) {
411+
$orderItems[$parentItemId] = $this->quoteItemToOrderItem->convert(
412+
$quoteItem->getParentItem(),
413+
['parent_item' => null]
414+
);
415+
}
416+
$parentItem = isset($orderItems[$parentItemId]) ? $orderItems[$parentItemId] : null;
417+
$orderItems[$itemId] = $this->quoteItemToOrderItem->convert($quoteItem, ['parent_item' => $parentItem]);
411418
}
412419
return array_values($orderItems);
413420
}

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,20 @@ protected function _afterLoad()
150150
{
151151
parent::_afterLoad();
152152

153-
/**
154-
* Assign parent items
155-
*/
153+
$productIds = [];
156154
foreach ($this as $item) {
155+
// Assign parent items
157156
if ($item->getParentItemId()) {
158157
$item->setParentItem($this->getItemById($item->getParentItemId()));
159158
}
160159
if ($this->_quote) {
161160
$item->setQuote($this->_quote);
162161
}
162+
// Collect quote products ids
163+
$productIds[] = (int)$item->getProductId();
163164
}
164-
165+
$this->_productIds = array_merge($this->_productIds, $productIds);
166+
$this->removeItemsWithAbsentProducts();
165167
/**
166168
* Assign options and products
167169
*/
@@ -199,12 +201,6 @@ protected function _assignOptions()
199201
protected function _assignProducts()
200202
{
201203
\Magento\Framework\Profiler::start('QUOTE:' . __METHOD__, ['group' => 'QUOTE', 'method' => __METHOD__]);
202-
$productIds = [];
203-
foreach ($this as $item) {
204-
$productIds[] = (int)$item->getProductId();
205-
}
206-
$this->_productIds = array_merge($this->_productIds, $productIds);
207-
208204
$productCollection = $this->_productCollectionFactory->create()->setStoreId(
209205
$this->getStoreId()
210206
)->addIdFilter(
@@ -268,4 +264,24 @@ protected function _assignProducts()
268264

269265
return $this;
270266
}
267+
268+
/**
269+
* Find and remove quote items with non existing products
270+
*
271+
* @return void
272+
*/
273+
private function removeItemsWithAbsentProducts()
274+
{
275+
$productCollection = $this->_productCollectionFactory->create()->addIdFilter($this->_productIds);
276+
$existingProductsIds = $productCollection->getAllIds();
277+
$absentProductsIds = array_diff($this->_productIds, $existingProductsIds);
278+
// Remove not existing products from items collection
279+
if (!empty($absentProductsIds)) {
280+
foreach ($absentProductsIds as $productIdToExclude) {
281+
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
282+
$quoteItem = $this->getItemByColumnValue('product_id', $productIdToExclude);
283+
$this->removeItemByKey($quoteItem->getId());
284+
}
285+
}
286+
}
271287
}

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,35 @@
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
{
20-
$this->markTestSkipped('MAGETWO-50989');
2124
/**
2225
* Preconditions:
23-
* 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
2427
*/
2528
$objectManager = Bootstrap::getObjectManager();
2629
/** @var \Magento\Quote\Model\Quote $quote */
27-
$quote = $objectManager->create('\Magento\Quote\Model\Quote');
30+
$quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
2831
$quote->load('test01', 'reserved_order_id');
2932

3033
/** Execute SUT */
3134
/** @var \Magento\Quote\Api\CartManagementInterface $model */
32-
$model = $objectManager->create('\Magento\Quote\Api\CartManagementInterface');
33-
$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);
3440

3541
/** Check if SUT caused expected effects */
3642
$orderItems = $order->getItems();
@@ -42,4 +48,45 @@ public function testSubmit()
4248
}
4349
}
4450
}
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+
}
4592
}

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

Lines changed: 15 additions & 11 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');
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');
14-
$quote->load('test01', 'reserved_order_id');
15+
$quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
16+
$quote->load('test_order_item_with_bundle_items', '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');
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)