Skip to content

Commit 977753b

Browse files
authored
Merge pull request #4345 from magento-mpi/pr_2019_06_11_ce
[mpi] bugfixes 2.3
2 parents 8e16abc + 2dce436 commit 977753b

File tree

5 files changed

+101
-24
lines changed

5 files changed

+101
-24
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
}

app/code/Magento/CustomerImportExport/Model/Import/CustomerComposite.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ protected function _initAddressAttributes()
270270
protected function _importData()
271271
{
272272
$result = $this->_customerEntity->importData();
273+
$this->countItemsCreated += $this->_customerEntity->getCreatedItemsCount();
274+
$this->countItemsUpdated += $this->_customerEntity->getUpdatedItemsCount();
275+
$this->countItemsDeleted += $this->_customerEntity->getDeletedItemsCount();
276+
273277
if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) {
274278
return $result && $this->_addressEntity->setCustomerAttributes($this->_customerAttributes)->importData();
275279
}

app/code/Magento/Ui/view/base/web/js/form/element/date.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,13 @@ define([
125125
shiftedValue = moment(value, dateFormat);
126126
}
127127

128-
if (!shiftedValue.isValid()) {
129-
shiftedValue = moment(value, this.inputDateFormat);
130-
}
131128
shiftedValue = shiftedValue.format(this.pickerDateTimeFormat);
132-
} else {
133-
shiftedValue = '';
134-
}
135129

136-
if (shiftedValue !== this.shiftedValue()) {
137-
this.shiftedValue(shiftedValue);
130+
if (shiftedValue !== this.shiftedValue()) {
131+
this.shiftedValue(shiftedValue);
132+
}
138133
}
134+
139135
},
140136

141137
/**

dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Import/CustomerCompositeTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1010

11+
/**
12+
* Test for CustomerComposite import class
13+
*/
1114
class CustomerCompositeTest extends \PHPUnit\Framework\TestCase
1215
{
1316
/**#@+
@@ -129,15 +132,26 @@ protected function _assertCustomerData(array $expectedData)
129132
* @param string $sourceFile
130133
* @param array $dataBefore
131134
* @param array $dataAfter
135+
* @param int $updatedItemsCount
136+
* @param int $createdItemsCount
137+
* @param int $deletedItemsCount
132138
* @param array $errors
133139
*
134140
* @magentoDataFixture Magento/Customer/_files/import_export/customers_for_address_import.php
135141
* @magentoAppIsolation enabled
136142
*
137143
* @dataProvider importDataDataProvider
138144
*/
139-
public function testImportData($behavior, $sourceFile, array $dataBefore, array $dataAfter, array $errors = [])
140-
{
145+
public function testImportData(
146+
$behavior,
147+
$sourceFile,
148+
array $dataBefore,
149+
array $dataAfter,
150+
$updatedItemsCount,
151+
$createdItemsCount,
152+
$deletedItemsCount,
153+
array $errors = []
154+
) {
141155
\Magento\TestFramework\Helper\Bootstrap::getInstance()
142156
->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
143157
// set entity adapter parameters
@@ -173,6 +187,9 @@ public function testImportData($behavior, $sourceFile, array $dataBefore, array
173187

174188
// import data
175189
$this->_entityAdapter->importData();
190+
$this->assertSame($updatedItemsCount, $this->_entityAdapter->getUpdatedItemsCount());
191+
$this->assertSame($createdItemsCount, $this->_entityAdapter->getCreatedItemsCount());
192+
$this->assertSame($deletedItemsCount, $this->_entityAdapter->getDeletedItemsCount());
176193

177194
// assert data after import
178195
$this->_assertCustomerData($dataAfter);
@@ -192,6 +209,10 @@ public function importDataDataProvider()
192209
'$sourceFile' => $filesDirectory . self::DELETE_FILE_NAME,
193210
'$dataBefore' => $this->_beforeImport,
194211
'$dataAfter' => [],
212+
'$updatedItemsCount' => 0,
213+
'$createdItemsCount' => 0,
214+
'$deletedItemsCount' => 1,
215+
'$errors' => [],
195216
],
196217
];
197218

@@ -200,6 +221,9 @@ public function importDataDataProvider()
200221
'$sourceFile' => $filesDirectory . self::UPDATE_FILE_NAME,
201222
'$dataBefore' => $this->_beforeImport,
202223
'$dataAfter' => $this->_afterImport,
224+
'$updatedItemsCount' => 1,
225+
'$createdItemsCount' => 3,
226+
'$deletedItemsCount' => 0,
203227
'$errors' => [],
204228
];
205229

0 commit comments

Comments
 (0)