Skip to content

Commit 96aaf95

Browse files
author
Oleksandr Dubovyk
committed
MAGETWO-99585: When backorders are allowed on an item, the qty_backordered value in sales_order_item is 0
- fixed - modified test
1 parent 1656409 commit 96aaf95

File tree

2 files changed

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

2 files changed

+65
-14
lines changed

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
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\Quote\Model\Quote\Item;
1114

15+
/**
16+
* Class StockItem initializes stock item and populates it with data
17+
*/
1218
class StockItem
1319
{
1420
/**
@@ -26,26 +32,34 @@ class StockItem
2632
*/
2733
protected $stockState;
2834

35+
/**
36+
* @var StockStateProviderInterface
37+
*/
38+
private $stockStateProvider;
39+
2940
/**
3041
* @param ConfigInterface $typeConfig
3142
* @param QuoteItemQtyList $quoteItemQtyList
3243
* @param StockStateInterface $stockState
44+
* @param StockStateProviderInterface $stockStateProvider
3345
*/
3446
public function __construct(
3547
ConfigInterface $typeConfig,
3648
QuoteItemQtyList $quoteItemQtyList,
37-
StockStateInterface $stockState
49+
StockStateInterface $stockState,
50+
StockStateProviderInterface $stockStateProvider
3851
) {
3952
$this->quoteItemQtyList = $quoteItemQtyList;
4053
$this->typeConfig = $typeConfig;
4154
$this->stockState = $stockState;
55+
$this->stockStateProvider = $stockStateProvider;
4256
}
4357

4458
/**
4559
* Initialize stock item
4660
*
47-
* @param \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem
48-
* @param \Magento\Quote\Model\Quote\Item $quoteItem
61+
* @param StockItemInterface $stockItem
62+
* @param Item $quoteItem
4963
* @param int $qty
5064
*
5165
* @return \Magento\Framework\DataObject
@@ -54,11 +68,14 @@ public function __construct(
5468
* @SuppressWarnings(PHPMD.NPathComplexity)
5569
*/
5670
public function initialize(
57-
\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem,
58-
\Magento\Quote\Model\Quote\Item $quoteItem,
71+
StockItemInterface $stockItem,
72+
Item $quoteItem,
5973
$qty
6074
) {
6175
$product = $quoteItem->getProduct();
76+
$quoteItemId = $quoteItem->getId();
77+
$quoteId = $quoteItem->getQuoteId();
78+
$productId = $product->getId();
6279
/**
6380
* When we work with subitem
6481
*/
@@ -68,14 +85,14 @@ public function initialize(
6885
* we are using 0 because original qty was processed
6986
*/
7087
$qtyForCheck = $this->quoteItemQtyList
71-
->getQty($product->getId(), $quoteItem->getId(), $quoteItem->getQuoteId(), 0);
88+
->getQty($productId, $quoteItemId, $quoteId, 0);
7289
} else {
7390
$increaseQty = $quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty;
7491
$rowQty = $qty;
7592
$qtyForCheck = $this->quoteItemQtyList->getQty(
76-
$product->getId(),
77-
$quoteItem->getId(),
78-
$quoteItem->getQuoteId(),
93+
$productId,
94+
$quoteItemId,
95+
$quoteId,
7996
$increaseQty
8097
);
8198
}
@@ -90,14 +107,20 @@ public function initialize(
90107

91108
$stockItem->setProductName($product->getName());
92109

110+
/** @var \Magento\Framework\DataObject $result */
93111
$result = $this->stockState->checkQuoteItemQty(
94-
$product->getId(),
112+
$productId,
95113
$rowQty,
96114
$qtyForCheck,
97115
$qty,
98116
$product->getStore()->getWebsiteId()
99117
);
100118

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

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)