Skip to content

Commit e786294

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-14' into L3_PR_21-10-10
2 parents 9ff22de + 91730cd commit e786294

File tree

4 files changed

+119
-137
lines changed

4 files changed

+119
-137
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ class Product extends AbstractEntity
171171
const URL_KEY = 'url_key';
172172

173173
/**
174-
* Attribute cache
175-
*
176174
* @var array
177175
*/
178176
protected $_attributeCache = [];
@@ -238,8 +236,6 @@ class Product extends AbstractEntity
238236
];
239237

240238
/**
241-
* Attributes codes which shows as date
242-
*
243239
* @var array
244240
* @since 100.1.2
245241
*/
@@ -253,8 +249,6 @@ class Product extends AbstractEntity
253249
];
254250

255251
/**
256-
* Need to log in import history
257-
*
258252
* @var bool
259253
*/
260254
protected $logInHistory = true;
@@ -267,8 +261,6 @@ class Product extends AbstractEntity
267261
protected $_mediaGalleryAttributeId = null;
268262

269263
/**
270-
* Validation failure message template definitions
271-
*
272264
* @var array
273265
* @codingStandardsIgnoreStart
274266
*/
@@ -650,7 +642,7 @@ class Product extends AbstractEntity
650642
private $_logger;
651643

652644
/**
653-
* {@inheritdoc}
645+
* @var string
654646
*/
655647
protected $masterAttributeCode = 'sku';
656648

@@ -691,15 +683,11 @@ class Product extends AbstractEntity
691683
protected $rowNumbers = [];
692684

693685
/**
694-
* Product entity link field
695-
*
696686
* @var string
697687
*/
698688
private $productEntityLinkField;
699689

700690
/**
701-
* Product entity identifier field
702-
*
703691
* @var string
704692
*/
705693
private $productEntityIdentifierField;
@@ -726,8 +714,6 @@ class Product extends AbstractEntity
726714
private $catalogConfig;
727715

728716
/**
729-
* Stock Item Importer
730-
*
731717
* @var StockItemImporterInterface
732718
*/
733719
private $stockItemImporter;
@@ -3161,10 +3147,6 @@ private function formatStockDataForRow(array $rowData): array
31613147
$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
31623148
$existStockData = $stockItemDo->getData();
31633149

3164-
if (isset($rowData['qty']) && $rowData['qty'] == 0 && !isset($rowData['is_in_stock'])) {
3165-
$rowData['is_in_stock'] = 0;
3166-
}
3167-
31683150
$row = array_merge(
31693151
$this->defaultStockData,
31703152
array_intersect_key($existStockData, $this->defaultStockData),
@@ -3173,11 +3155,10 @@ private function formatStockDataForRow(array $rowData): array
31733155
);
31743156

31753157
if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($sku)['type_id'])) {
3176-
if (isset($rowData['qty']) && $rowData['qty'] == 0) {
3177-
$row['is_in_stock'] = 0;
3178-
}
31793158
$stockItemDo->setData($row);
3180-
$row['is_in_stock'] = $row['is_in_stock'] ?? $this->stockStateProvider->verifyStock($stockItemDo);
3159+
$row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo)
3160+
? (int) $row['is_in_stock']
3161+
: 0;
31813162
if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
31823163
$date = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
31833164
$row['low_stock_date'] = $date->format(DateTime::DATETIME_PHP_FORMAT);

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest/ProductStockTest.php

Lines changed: 107 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -23,67 +23,54 @@
2323
*/
2424
class ProductStockTest extends ProductTestBase
2525
{
26+
/**
27+
* @var StockRegistryStorage
28+
*/
29+
private $stockRegistryStorage;
30+
31+
/**
32+
* @var StockRegistry
33+
*/
34+
private $stockRegistry;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp(): void
40+
{
41+
parent::setUp();
42+
$this->stockRegistryStorage = $this->objectManager->get(StockRegistryStorage::class);
43+
$this->stockRegistry = $this->objectManager->get(StockRegistry::class);
44+
}
45+
2646
/**
2747
* Test if stock item quantity properly saved after import
2848
*
2949
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
3050
*/
3151
public function testSaveStockItemQty()
3252
{
33-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
34-
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
35-
\Magento\Catalog\Api\ProductRepositoryInterface::class
36-
);
37-
$id1 = $productRepository->get('simple1')->getId();
38-
$id2 = $productRepository->get('simple2')->getId();
39-
$id3 = $productRepository->get('simple3')->getId();
40-
$existingProductIds = [$id1, $id2, $id3];
41-
$stockItems = [];
42-
foreach ($existingProductIds as $productId) {
43-
/** @var $stockRegistry StockRegistry */
44-
$stockRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
45-
StockRegistry::class
46-
);
47-
48-
$stockItem = $stockRegistry->getStockItem($productId, 1);
49-
$stockItems[$productId] = $stockItem;
50-
}
51-
52-
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
53-
->create(\Magento\Framework\Filesystem::class);
54-
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
55-
$source = $this->objectManager->create(
56-
\Magento\ImportExport\Model\Import\Source\Csv::class,
57-
[
58-
'file' => __DIR__ . '/../_files/products_to_import.csv',
59-
'directory' => $directory
60-
]
61-
);
62-
$errors = $this->_model->setParameters(
63-
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
64-
)->setSource(
65-
$source
66-
)->validateData();
67-
68-
$this->assertTrue($errors->getErrorsCount() == 0);
69-
70-
$this->_model->importData();
71-
72-
/** @var $stockItmBeforeImport \Magento\CatalogInventory\Model\Stock\Item */
73-
foreach ($stockItems as $productId => $stockItmBeforeImport) {
74-
/** @var $stockRegistry StockRegistry */
75-
$stockRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
76-
StockRegistry::class
77-
);
78-
79-
$stockItemAfterImport = $stockRegistry->getStockItem($productId, 1);
80-
81-
$this->assertEquals($stockItmBeforeImport->getQty(), $stockItemAfterImport->getQty());
82-
$this->assertEquals(1, $stockItemAfterImport->getIsInStock());
83-
unset($stockItemAfterImport);
84-
}
85-
86-
unset($stockItems, $stockItem);
53+
$id1 = $this->getProductBySku('simple1')->getId();
54+
$id2 = $this->getProductBySku('simple2')->getId();
55+
$id3 = $this->getProductBySku('simple3')->getId();
56+
$stockItem = $this->stockRegistry->getStockItem($id1, 1);
57+
$id1Qty = $stockItem->getQty();
58+
$stockItem = $this->stockRegistry->getStockItem($id2, 1);
59+
$id2Qty = $stockItem->getQty();
60+
$stockItem = $this->stockRegistry->getStockItem($id3, 1);
61+
$id3Qty = $stockItem->getQty();
62+
63+
$this->importFile('products_to_import.csv');
64+
65+
$stockItem = $this->stockRegistry->getStockItem($id1, 1);
66+
$this->assertEquals(1, $stockItem->getIsInStock());
67+
$this->assertEquals($id1Qty, $stockItem->getQty());
68+
$stockItem = $this->stockRegistry->getStockItem($id2, 1);
69+
$this->assertEquals(1, $stockItem->getIsInStock());
70+
$this->assertEquals($id2Qty, $stockItem->getQty());
71+
$stockItem = $this->stockRegistry->getStockItem($id3, 1);
72+
$this->assertEquals(1, $stockItem->getIsInStock());
73+
$this->assertEquals($id3Qty, $stockItem->getQty());
8774
}
8875

8976
/**
@@ -95,47 +82,16 @@ public function testSaveStockItemQty()
9582
*/
9683
public function testSaveIsInStockByZeroQty(): void
9784
{
98-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
99-
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
100-
\Magento\Catalog\Api\ProductRepositoryInterface::class
101-
);
102-
$id1 = $productRepository->get('simple1')->getId();
103-
$id2 = $productRepository->get('simple2')->getId();
104-
$id3 = $productRepository->get('simple3')->getId();
105-
$existingProductIds = [$id1, $id2, $id3];
106-
107-
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
108-
->create(\Magento\Framework\Filesystem::class);
109-
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
110-
$source = $this->objectManager->create(
111-
\Magento\ImportExport\Model\Import\Source\Csv::class,
112-
[
113-
'file' => __DIR__ . '/../_files/products_to_import_zero_qty.csv',
114-
'directory' => $directory
115-
]
116-
);
117-
$errors = $this->_model->setParameters(
118-
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
119-
)->setSource(
120-
$source
121-
)->validateData();
122-
123-
$this->assertTrue($errors->getErrorsCount() == 0);
124-
125-
$this->_model->importData();
126-
127-
/** @var $stockItmBeforeImport \Magento\CatalogInventory\Model\Stock\Item */
128-
foreach ($existingProductIds as $productId) {
129-
/** @var $stockRegistry StockRegistry */
130-
$stockRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
131-
StockRegistry::class
132-
);
133-
134-
$stockItemAfterImport = $stockRegistry->getStockItem($productId, 1);
135-
136-
$this->assertEquals(0, $stockItemAfterImport->getIsInStock());
137-
unset($stockItemAfterImport);
138-
}
85+
$this->importFile('products_to_import_zero_qty.csv');
86+
$product = $this->getProductBySku('simple1');
87+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
88+
$this->assertEquals(0, $stockItem->getIsInStock());
89+
$product = $this->getProductBySku('simple2');
90+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
91+
$this->assertEquals(0, $stockItem->getIsInStock());
92+
$product = $this->getProductBySku('simple3');
93+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
94+
$this->assertEquals(0, $stockItem->getIsInStock());
13995
}
14096

14197
/**
@@ -201,19 +157,13 @@ public function testImportWithBackordersDisabled(): void
201157
*/
202158
public function testProductStockStatusShouldBeUpdated()
203159
{
204-
/** @var $stockRegistry StockRegistry */
205-
$stockRegistry = $this->objectManager->create(StockRegistry::class);
206-
/** @var StockRegistryStorage $stockRegistryStorage */
207-
$stockRegistryStorage = $this->objectManager->get(StockRegistryStorage::class);
208-
$status = $stockRegistry->getStockStatusBySku('simple');
160+
$status = $this->stockRegistry->getStockStatusBySku('simple');
209161
$this->assertEquals(Stock::STOCK_IN_STOCK, $status->getStockStatus());
210-
$this->importDataForMediaTest('disable_product.csv');
211-
$stockRegistryStorage->clean();
212-
$status = $stockRegistry->getStockStatusBySku('simple');
162+
$this->importFile('disable_product.csv');
163+
$status = $this->stockRegistry->getStockStatusBySku('simple');
213164
$this->assertEquals(Stock::STOCK_OUT_OF_STOCK, $status->getStockStatus());
214165
$this->importDataForMediaTest('enable_product.csv');
215-
$stockRegistryStorage->clean();
216-
$status = $stockRegistry->getStockStatusBySku('simple');
166+
$status = $this->stockRegistry->getStockStatusBySku('simple');
217167
$this->assertEquals(Stock::STOCK_IN_STOCK, $status->getStockStatus());
218168
}
219169

@@ -229,21 +179,64 @@ public function testProductStockStatusShouldBeUpdatedOnSchedule()
229179
{
230180
/** * @var $indexProcessor \Magento\Indexer\Model\Processor */
231181
$indexProcessor = $this->objectManager->create(\Magento\Indexer\Model\Processor::class);
232-
/** @var $stockRegistry StockRegistry */
233-
$stockRegistry = $this->objectManager->create(StockRegistry::class);
234-
/** @var StockRegistryStorage $stockRegistryStorage */
235-
$stockRegistryStorage = $this->objectManager->get(StockRegistryStorage::class);
236-
$status = $stockRegistry->getStockStatusBySku('simple');
182+
$status = $this->stockRegistry->getStockStatusBySku('simple');
237183
$this->assertEquals(Stock::STOCK_IN_STOCK, $status->getStockStatus());
238184
$this->importDataForMediaTest('disable_product.csv');
239185
$indexProcessor->updateMview();
240-
$stockRegistryStorage->clean();
241-
$status = $stockRegistry->getStockStatusBySku('simple');
186+
$status = $this->stockRegistry->getStockStatusBySku('simple');
242187
$this->assertEquals(Stock::STOCK_OUT_OF_STOCK, $status->getStockStatus());
243188
$this->importDataForMediaTest('enable_product.csv');
244189
$indexProcessor->updateMview();
245-
$stockRegistryStorage->clean();
246-
$status = $stockRegistry->getStockStatusBySku('simple');
190+
$status = $this->stockRegistry->getStockStatusBySku('simple');
247191
$this->assertEquals(Stock::STOCK_IN_STOCK, $status->getStockStatus());
248192
}
193+
194+
/**
195+
* Test that product stock status should be 'out of stock' if quantity is 0 regardless of 'is_in_stock' value
196+
*
197+
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
198+
*/
199+
public function testImportWithQtyZeroAndWithoutStockStatus(): void
200+
{
201+
$this->importFile('products_to_import_with_qty_zero_only.csv');
202+
$product = $this->getProductBySku('simple1');
203+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
204+
$this->assertEquals(0, $stockItem->getIsInStock());
205+
$product = $this->getProductBySku('simple2');
206+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
207+
$this->assertEquals(0, $stockItem->getIsInStock());
208+
$product = $this->getProductBySku('simple3');
209+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
210+
$this->assertEquals(0, $stockItem->getIsInStock());
211+
}
212+
213+
/**
214+
* Test that product stock status should be 'in stock' if quantity is 0 and backorders is enabled
215+
*
216+
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
217+
*/
218+
public function testImportWithQtyZeroAndWithBackOrdersEnabled(): void
219+
{
220+
$this->importFile('products_to_import_with_qty_zero_backorders_enabled.csv');
221+
$product = $this->getProductBySku('simple1');
222+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
223+
$this->assertEquals(1, $stockItem->getIsInStock());
224+
$product = $this->getProductBySku('simple2');
225+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
226+
$this->assertEquals(1, $stockItem->getIsInStock());
227+
$product = $this->getProductBySku('simple3');
228+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), 1);
229+
$this->assertEquals(1, $stockItem->getIsInStock());
230+
}
231+
232+
/**
233+
* @inheritdoc
234+
*/
235+
protected function importFile(string $fileName, int $bunchSize = 100): bool
236+
{
237+
$this->stockRegistryStorage->clean();
238+
$result = parent::importFile($fileName, $bunchSize);
239+
$this->stockRegistryStorage->clean();
240+
return $result;
241+
}
249242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sku,qty,allow_backorders,use_config_backorders
2+
simple1,0,1,0
3+
simple2,0,1,0
4+
simple3,0,1,0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sku,qty
2+
simple1,0
3+
simple2,0
4+
simple3,0

0 commit comments

Comments
 (0)