Skip to content

Commit 2c1a29e

Browse files
author
Stanislav Idolov
authored
ENGCOM-1101: #10057 #14327
2 parents 24d091d + 49fe1f6 commit 2c1a29e

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ public function validate(Observer $observer)
103103
$quoteItem = $observer->getEvent()->getItem();
104104
if (!$quoteItem ||
105105
!$quoteItem->getProductId() ||
106-
!$quoteItem->getQuote() ||
107-
$quoteItem->getQuote()->getIsSuperMode()
106+
!$quoteItem->getQuote()
108107
) {
109108
return;
110109
}
@@ -117,6 +116,18 @@ public function validate(Observer $observer)
117116
throw new LocalizedException(__('The stock item for Product is not valid.'));
118117
}
119118

119+
if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
120+
foreach ($options as $option) {
121+
$this->optionInitializer->initialize($option, $quoteItem, $qty);
122+
}
123+
} else {
124+
$this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
125+
}
126+
127+
if ($quoteItem->getQuote()->getIsSuperMode()) {
128+
return;
129+
}
130+
120131
/* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $stockStatus */
121132
$stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId());
122133

@@ -159,7 +170,7 @@ public function validate(Observer $observer)
159170
/**
160171
* Check item for options
161172
*/
162-
if (($options = $quoteItem->getQtyOptions()) && $qty > 0) {
173+
if ($options) {
163174
$qty = $product->getTypeInstance()->prepareQuoteItemQty($qty, $product);
164175
$quoteItem->setData('qty', $qty);
165176
if ($stockStatus) {
@@ -193,7 +204,7 @@ public function validate(Observer $observer)
193204
$removeError = true;
194205

195206
foreach ($options as $option) {
196-
$result = $this->optionInitializer->initialize($option, $quoteItem, $qty);
207+
$result = $option->getStockStateResult();
197208
if ($result->getHasError()) {
198209
$option->setHasError(true);
199210
//Setting this to false, so no error statuses are cleared
@@ -206,7 +217,7 @@ public function validate(Observer $observer)
206217
}
207218
} else {
208219
if ($quoteItem->getParentItem() === null) {
209-
$result = $this->stockItemInitializer->initialize($stockItem, $quoteItem, $qty);
220+
$result = $quoteItem->getStockStateResult();
210221
if ($result->getHasError()) {
211222
$this->addErrorInfoToQuote($result, $quoteItem);
212223
} else {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public function initialize(
133133

134134
$stockItem->unsIsChildItem();
135135

136+
$option->setStockStateResult($result);
137+
136138
return $result;
137139
}
138140
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public function initialize(
135135
$quoteItem->setBackorders($result->getItemBackorders());
136136
}
137137

138+
$quoteItem->setStockStateResult($result);
139+
138140
return $result;
139141
}
140142
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ public function testValidateWithOptions()
278278
{
279279
$optionMock = $this->getMockBuilder(OptionItem::class)
280280
->disableOriginalConstructor()
281-
->setMethods(['setHasError'])
281+
->setMethods(['setHasError', 'getStockStateResult'])
282282
->getMock();
283+
$optionMock->expects($this->once())
284+
->method('getStockStateResult')
285+
->willReturn($this->resultMock);
283286
$this->stockRegistryMock->expects($this->at(0))
284287
->method('getStockItem')
285288
->willReturn($this->stockItemMock);
@@ -316,14 +319,17 @@ public function testValidateWithOptionsAndError()
316319
{
317320
$optionMock = $this->getMockBuilder(OptionItem::class)
318321
->disableOriginalConstructor()
319-
->setMethods(['setHasError'])
322+
->setMethods(['setHasError', 'getStockStateResult'])
320323
->getMock();
321324
$this->stockRegistryMock->expects($this->at(0))
322325
->method('getStockItem')
323326
->willReturn($this->stockItemMock);
324327
$this->stockRegistryMock->expects($this->at(1))
325328
->method('getStockStatus')
326329
->willReturn($this->stockStatusMock);
330+
$optionMock->expects($this->once())
331+
->method('getStockStateResult')
332+
->willReturn($this->resultMock);
327333
$options = [$optionMock];
328334
$this->createInitialStub(1);
329335
$this->setUpStubForQuantity(1, true);
@@ -354,12 +360,15 @@ public function testValidateAndRemoveErrorsFromQuote()
354360
{
355361
$optionMock = $this->getMockBuilder(OptionItem::class)
356362
->disableOriginalConstructor()
357-
->setMethods(['setHasError'])
363+
->setMethods(['setHasError', 'getStockStateResult'])
358364
->getMock();
359365
$quoteItem = $this->getMockBuilder(Item::class)
360366
->disableOriginalConstructor()
361367
->setMethods(['getItemId', 'getErrorInfos'])
362368
->getMock();
369+
$optionMock->expects($this->once())
370+
->method('getStockStateResult')
371+
->willReturn($this->resultMock);
363372
$this->stockRegistryMock->expects($this->at(0))
364373
->method('getStockItem')
365374
->willReturn($this->stockItemMock);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function testInitializeWithSubitem()
8484
'setMessage',
8585
'setBackorders',
8686
'__wakeup',
87+
'setStockStateResult'
8788
]
8889
)
8990
->disableOriginalConstructor()
@@ -178,6 +179,7 @@ public function testInitializeWithSubitem()
178179
$quoteItem->expects($this->once())->method('setMessage')->with('message')->will($this->returnSelf());
179180
$result->expects($this->exactly(2))->method('getItemBackorders')->will($this->returnValue('backorders'));
180181
$quoteItem->expects($this->once())->method('setBackorders')->with('backorders')->will($this->returnSelf());
182+
$quoteItem->expects($this->once())->method('setStockStateResult')->with($result)->will($this->returnSelf());
181183

182184
$this->model->initialize($stockItem, $quoteItem, $qty);
183185
}

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/Quote/Item/QuantityValidatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,31 @@ public function testQuoteWithOptionsWithErrors()
130130
$this->stockState->expects($this->any())->method('checkQtyIncrements')->willReturn($resultMock);
131131
$this->optionInitializer->expects($this->any())->method('initialize')->willReturn($resultMock);
132132
$resultMock->expects($this->any())->method('getHasError')->willReturn(true);
133+
$this->setMockStockStateResultToQuoteItemOptions($quoteItem, $resultMock);
133134
$this->observer->execute($this->observerMock);
134135
$this->assertCount(2, $quoteItem->getErrorInfos(), 'Expected 2 errors in QuoteItem');
135136
}
136137

138+
/**
139+
* Set mock of Stock State Result to Quote Item Options.
140+
*
141+
*
142+
* @param \Magento\Quote\Model\Quote\Item $quoteItem
143+
* @param \PHPUnit_Framework_MockObject_MockObject $resultMock
144+
*/
145+
private function setMockStockStateResultToQuoteItemOptions($quoteItem, $resultMock)
146+
{
147+
if ($options = $quoteItem->getQtyOptions()) {
148+
foreach ($options as $option) {
149+
$option->setStockStateResult($resultMock);
150+
}
151+
152+
return;
153+
}
154+
155+
$this->fail('Test failed since Quote Item does not have Qty options.');
156+
}
157+
137158
/**
138159
* Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id
139160
*

0 commit comments

Comments
 (0)