Skip to content

Commit ee69692

Browse files
committed
Merge branch 'MAGETWO-99585' of https://github.com/magento-chaika/magento2ce into pr_2019_06_11_ce
2 parents 920296a + fa856cd commit ee69692

File tree

2 files changed

+67
-14
lines changed
  • app/code/Magento/CatalogInventory

2 files changed

+67
-14
lines changed

app/code/Magento/CatalogInventory/Model/Quote/Item/QuantityValidator/Initializer/StockItem.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77

88
use Magento\Catalog\Model\ProductTypes\ConfigInterface;
99
use Magento\CatalogInventory\Api\StockStateInterface;
10+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1011
use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\QuoteItemQtyList;
12+
use Magento\CatalogInventory\Model\Spi\StockStateProviderInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Quote\Model\Quote\Item;
1115

16+
/**
17+
* Class StockItem initializes stock item and populates it with data
18+
*/
1219
class StockItem
1320
{
1421
/**
@@ -26,26 +33,35 @@ class StockItem
2633
*/
2734
protected $stockState;
2835

36+
/**
37+
* @var StockStateProviderInterface
38+
*/
39+
private $stockStateProvider;
40+
2941
/**
3042
* @param ConfigInterface $typeConfig
3143
* @param QuoteItemQtyList $quoteItemQtyList
3244
* @param StockStateInterface $stockState
45+
* @param StockStateProviderInterface|null $stockStateProvider
3346
*/
3447
public function __construct(
3548
ConfigInterface $typeConfig,
3649
QuoteItemQtyList $quoteItemQtyList,
37-
StockStateInterface $stockState
50+
StockStateInterface $stockState,
51+
StockStateProviderInterface $stockStateProvider = null
3852
) {
3953
$this->quoteItemQtyList = $quoteItemQtyList;
4054
$this->typeConfig = $typeConfig;
4155
$this->stockState = $stockState;
56+
$this->stockStateProvider = $stockStateProvider ?: ObjectManager::getInstance()
57+
->get(StockStateProviderInterface::class);
4258
}
4359

4460
/**
4561
* Initialize stock item
4662
*
47-
* @param \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem
48-
* @param \Magento\Quote\Model\Quote\Item $quoteItem
63+
* @param StockItemInterface $stockItem
64+
* @param Item $quoteItem
4965
* @param int $qty
5066
*
5167
* @return \Magento\Framework\DataObject
@@ -54,11 +70,14 @@ public function __construct(
5470
* @SuppressWarnings(PHPMD.NPathComplexity)
5571
*/
5672
public function initialize(
57-
\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem,
58-
\Magento\Quote\Model\Quote\Item $quoteItem,
73+
StockItemInterface $stockItem,
74+
Item $quoteItem,
5975
$qty
6076
) {
6177
$product = $quoteItem->getProduct();
78+
$quoteItemId = $quoteItem->getId();
79+
$quoteId = $quoteItem->getQuoteId();
80+
$productId = $product->getId();
6281
/**
6382
* When we work with subitem
6483
*/
@@ -68,14 +87,14 @@ public function initialize(
6887
* we are using 0 because original qty was processed
6988
*/
7089
$qtyForCheck = $this->quoteItemQtyList
71-
->getQty($product->getId(), $quoteItem->getId(), $quoteItem->getQuoteId(), 0);
90+
->getQty($productId, $quoteItemId, $quoteId, 0);
7291
} else {
7392
$increaseQty = $quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty;
7493
$rowQty = $qty;
7594
$qtyForCheck = $this->quoteItemQtyList->getQty(
76-
$product->getId(),
77-
$quoteItem->getId(),
78-
$quoteItem->getQuoteId(),
95+
$productId,
96+
$quoteItemId,
97+
$quoteId,
7998
$increaseQty
8099
);
81100
}
@@ -90,14 +109,20 @@ public function initialize(
90109

91110
$stockItem->setProductName($product->getName());
92111

112+
/** @var \Magento\Framework\DataObject $result */
93113
$result = $this->stockState->checkQuoteItemQty(
94-
$product->getId(),
114+
$productId,
95115
$rowQty,
96116
$qtyForCheck,
97117
$qty,
98118
$product->getStore()->getWebsiteId()
99119
);
100120

121+
/* We need to ensure that any possible plugin will not erase the data */
122+
$backOrdersQty = $this->stockStateProvider->checkQuoteItemQty($stockItem, $rowQty, $qtyForCheck, $qty)
123+
->getItemBackorders();
124+
$result->setItemBackorders($backOrdersQty);
125+
101126
if ($stockItem->hasIsChildItem()) {
102127
$stockItem->unsIsChildItem();
103128
}

app/code/Magento/CatalogInventory/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/StockItemTest.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\QuoteItemQtyList;
99

1010
/**
11+
* Class StockItemTest
1112
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1213
*/
1314
class StockItemTest extends \PHPUnit\Framework\TestCase
@@ -28,10 +29,18 @@ class StockItemTest extends \PHPUnit\Framework\TestCase
2829
protected $typeConfig;
2930

3031
/**
31-
* @var \PHPUnit_Framework_MockObject_MockObject
32+
* @var \Magento\CatalogInventory\Api\StockStateInterface\PHPUnit_Framework_MockObject_MockObject
3233
*/
3334
protected $stockStateMock;
3435

36+
/**
37+
* @var \Magento\CatalogInventory\Model\StockStateProviderInterface| \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $stockStateProviderMock;
40+
41+
/**
42+
* @inheritdoc
43+
*/
3544
protected function setUp()
3645
{
3746
$this->quoteItemQtyList = $this
@@ -48,17 +57,25 @@ protected function setUp()
4857
$this->stockStateMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\StockStateInterface::class)
4958
->disableOriginalConstructor()
5059
->getMock();
60+
61+
$this->stockStateProviderMock = $this
62+
->getMockBuilder(\Magento\CatalogInventory\Model\StockStateProvider::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
5166
$this->model = $objectManagerHelper->getObject(
5267
\Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\StockItem::class,
5368
[
5469
'quoteItemQtyList' => $this->quoteItemQtyList,
5570
'typeConfig' => $this->typeConfig,
56-
'stockState' => $this->stockStateMock
71+
'stockState' => $this->stockStateMock,
72+
'stockStateProvider' => $this->stockStateProviderMock
5773
]
5874
);
5975
}
6076

6177
/**
78+
* Test initialize with Subitem
6279
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
6380
*/
6481
public function testInitializeWithSubitem()
@@ -141,6 +158,10 @@ public function testInitializeWithSubitem()
141158
->method('checkQuoteItemQty')
142159
->withAnyParameters()
143160
->will($this->returnValue($result));
161+
$this->stockStateProviderMock->expects($this->once())
162+
->method('checkQuoteItemQty')
163+
->withAnyParameters()
164+
->will($this->returnValue($result));
144165
$product->expects($this->once())
145166
->method('getCustomOption')
146167
->with('product_type')
@@ -177,13 +198,16 @@ public function testInitializeWithSubitem()
177198
$quoteItem->expects($this->once())->method('setUseOldQty')->with('item')->will($this->returnSelf());
178199
$result->expects($this->exactly(2))->method('getMessage')->will($this->returnValue('message'));
179200
$quoteItem->expects($this->once())->method('setMessage')->with('message')->will($this->returnSelf());
180-
$result->expects($this->exactly(2))->method('getItemBackorders')->will($this->returnValue('backorders'));
201+
$result->expects($this->exactly(3))->method('getItemBackorders')->will($this->returnValue('backorders'));
181202
$quoteItem->expects($this->once())->method('setBackorders')->with('backorders')->will($this->returnSelf());
182203
$quoteItem->expects($this->once())->method('setStockStateResult')->with($result)->will($this->returnSelf());
183204

184205
$this->model->initialize($stockItem, $quoteItem, $qty);
185206
}
186207

208+
/**
209+
* Test initialize without Subitem
210+
*/
187211
public function testInitializeWithoutSubitem()
188212
{
189213
$qty = 3;
@@ -234,6 +258,10 @@ public function testInitializeWithoutSubitem()
234258
->with($productId, 'quote_item_id', 'quote_id', $qty)
235259
->will($this->returnValue('summary_qty'));
236260
$this->stockStateMock->expects($this->once())
261+
->method('checkQuoteItemQty')
262+
->withAnyParameters()
263+
->will($this->returnValue($result));
264+
$this->stockStateProviderMock->expects($this->once())
237265
->method('checkQuoteItemQty')
238266
->withAnyParameters()
239267
->will($this->returnValue($result));
@@ -256,7 +284,7 @@ public function testInitializeWithoutSubitem()
256284
$result->expects($this->once())->method('getHasQtyOptionUpdate')->will($this->returnValue(false));
257285
$result->expects($this->once())->method('getItemUseOldQty')->will($this->returnValue(null));
258286
$result->expects($this->once())->method('getMessage')->will($this->returnValue(null));
259-
$result->expects($this->once())->method('getItemBackorders')->will($this->returnValue(null));
287+
$result->expects($this->exactly(2))->method('getItemBackorders')->will($this->returnValue(null));
260288

261289
$this->model->initialize($stockItem, $quoteItem, $qty);
262290
}

0 commit comments

Comments
 (0)