Skip to content

Commit 12ebc0e

Browse files
authored
Merge pull request #6725 from magento-tsg/2.4-develop-pr136
[Arrows] Fixes for 2.4 (pr136) (2.4-develop)
2 parents aaeadff + a5c60c5 commit 12ebc0e

File tree

5 files changed

+155
-32
lines changed

5 files changed

+155
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GroupedImportExport\Plugin\CatalogImportExport\Model\StockItemImporter;
9+
10+
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
11+
use Magento\GroupedProduct\Model\Inventory\ChangeParentStockStatus;
12+
13+
/**
14+
* Update grouped product stock status during import plugin.
15+
*/
16+
class UpdateGroupedProductStockStatusPlugin
17+
{
18+
/**
19+
* @var ChangeParentStockStatus
20+
*/
21+
private ChangeParentStockStatus $changeParentStockStatus;
22+
23+
/**
24+
* @param ChangeParentStockStatus $changeParentStockStatus
25+
*/
26+
public function __construct(ChangeParentStockStatus $changeParentStockStatus)
27+
{
28+
$this->changeParentStockStatus = $changeParentStockStatus;
29+
}
30+
31+
/**
32+
* Update grouped product stock status during import.
33+
*
34+
* @param StockItemImporterInterface $subject
35+
* @param null $result
36+
* @param array $stockData
37+
* @return void
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
39+
*/
40+
public function afterImport(
41+
StockItemImporterInterface $subject,
42+
$result,
43+
array $stockData
44+
) {
45+
$productIds = array_column($stockData, 'product_id');
46+
foreach ($productIds as $productId) {
47+
$this->changeParentStockStatus->execute((int)$productId);
48+
}
49+
}
50+
}

app/code/Magento/GroupedImportExport/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="Magento\CatalogImportExport\Model\StockItemImporterInterface">
17+
<plugin name="update_grouped_product_stock_status_plugin" type="Magento\GroupedImportExport\Plugin\CatalogImportExport\Model\StockItemImporter\UpdateGroupedProductStockStatusPlugin" />
18+
</type>
1619
</config>

dev/tests/integration/testsuite/Magento/GroupedImportExport/Model/Import/Product/Type/GroupedTest.php

Lines changed: 97 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\GroupedImportExport\Model\Import\Product\Type;
79

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
13+
use Magento\CatalogImportExport\Model\Import\Product as ProductImport;
14+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
15+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
16+
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
17+
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
818
use Magento\Framework\App\Filesystem\DirectoryList;
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Magento\Framework\Filesystem;
21+
use Magento\Framework\ObjectManagerInterface;
922
use Magento\ImportExport\Model\Import;
23+
use Magento\ImportExport\Model\Import\Source\Csv;
24+
use Magento\TestFramework\Helper\Bootstrap;
25+
use PHPUnit\Framework\TestCase;
1026

11-
class GroupedTest extends \PHPUnit\Framework\TestCase
27+
class GroupedTest extends TestCase
1228
{
1329
/**
1430
* Configurable product test Name
@@ -21,26 +37,29 @@ class GroupedTest extends \PHPUnit\Framework\TestCase
2137
const TEST_PRODUCT_TYPE = 'grouped';
2238

2339
/**
24-
* @var \Magento\CatalogImportExport\Model\Import\Product
40+
* @var ProductImport
2541
*/
26-
protected $model;
42+
private $model;
2743

2844
/**
29-
* @var \Magento\Framework\ObjectManagerInterface
45+
* @var ObjectManagerInterface
3046
*/
31-
protected $objectManager;
47+
private $objectManager;
3248

3349
/**
3450
* Grouped product options SKU list
3551
*
3652
* @var array
3753
*/
38-
protected $optionSkuList = ['Simple for Grouped 1', 'Simple for Grouped 2'];
54+
private $optionSkuList = ['Simple for Grouped 1', 'Simple for Grouped 2'];
3955

56+
/**
57+
* @ingeritdoc
58+
*/
4059
protected function setUp(): void
4160
{
42-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
43-
$this->model = $this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class);
61+
$this->objectManager = Bootstrap::getObjectManager();
62+
$this->model = $this->objectManager->create(ProductImport::class);
4463
}
4564

4665
/**
@@ -52,33 +71,12 @@ public function testImport()
5271
{
5372
// Import data from CSV file
5473
$pathToFile = __DIR__ . '/../../_files/grouped_product.csv';
55-
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
74+
$this->import($pathToFile);
5675

57-
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
58-
$source = $this->objectManager->create(
59-
\Magento\ImportExport\Model\Import\Source\Csv::class,
60-
[
61-
'file' => $pathToFile,
62-
'directory' => $directory
63-
]
64-
);
65-
$errors = $this->model->setSource(
66-
$source
67-
)->setParameters(
68-
[
69-
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
70-
'entity' => 'catalog_product'
71-
]
72-
)->validateData();
73-
74-
$this->assertTrue($errors->getErrorsCount() == 0);
75-
$this->model->importData();
76-
77-
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
76+
$resource = $this->objectManager->get(ProductResource::class);
7877
$productId = $resource->getIdBySku('Test Grouped');
7978
$this->assertIsNumeric($productId);
80-
/** @var \Magento\Catalog\Model\Product $product */
81-
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
79+
$product = $this->objectManager->create(Product::class);
8280
$product->load($productId);
8381

8482
$this->assertFalse($product->isObjectNew());
@@ -91,4 +89,71 @@ public function testImport()
9189
$this->assertContains($childProduct->getSku(), $this->optionSkuList);
9290
}
9391
}
92+
93+
/**
94+
* Verify grouped product stock status updated during import.
95+
*
96+
* @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
97+
* @return void
98+
*/
99+
public function testImportUpdateStockStatus(): void
100+
{
101+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
102+
//Verify grouped product is out of stock after import.
103+
$pathToOutOfStockFile = __DIR__ . '/../../_files/grouped_product_children_out_of_stock.csv';
104+
$this->import($pathToOutOfStockFile);
105+
$groupedProduct = $productRepository->get('grouped-product', false, null, true);
106+
$stockItem = $this->getStockItem((int)$groupedProduct->getId());
107+
self::assertFalse($stockItem->getIsInStock());
108+
//Verify grouped product is in stock after import.
109+
$pathToOutOfStockFile = __DIR__ . '/../../_files/grouped_product_children_in_stock.csv';
110+
$this->import($pathToOutOfStockFile);
111+
$groupedProduct = $productRepository->get('grouped-product', false, null, true);
112+
$stockItem = $this->getStockItem((int)$groupedProduct->getId());
113+
self::assertTrue($stockItem->getIsInStock());
114+
}
115+
116+
/**
117+
* Retrieve product stock status.
118+
*
119+
* @param int $productId
120+
* @return StockItemInterface|null
121+
*/
122+
private function getStockItem(int $productId): ?StockItemInterface
123+
{
124+
$criteriaFactory = $this->objectManager->create(StockItemCriteriaInterfaceFactory::class);
125+
$stockItemRepository = $this->objectManager->create(StockItemRepositoryInterface::class);
126+
$stockConfiguration = $this->objectManager->create(StockConfigurationInterface::class);
127+
$criteria = $criteriaFactory->create();
128+
$criteria->setScopeFilter($stockConfiguration->getDefaultScopeId());
129+
$criteria->setProductsFilter($productId);
130+
$items = $stockItemRepository->getList($criteria)->getItems();
131+
132+
return reset($items);
133+
}
134+
135+
136+
/**
137+
* Perform products import.
138+
*
139+
* @param string $pathToFile
140+
* @throws LocalizedException
141+
*/
142+
private function import(string $pathToFile): void
143+
{
144+
$filesystem = $this->objectManager->create(Filesystem::class);
145+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
146+
$source = $this->objectManager->create(Csv::class, ['file' => $pathToFile, 'directory' => $directory]);
147+
$errors = $this->model->setSource(
148+
$source
149+
)->setParameters(
150+
[
151+
'behavior' => Import::BEHAVIOR_APPEND,
152+
'entity' => 'catalog_product',
153+
]
154+
)->validateData();
155+
156+
$this->assertTrue($errors->getErrorsCount() == 0);
157+
$this->model->importData();
158+
}
94159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sku,qty,is_in_stock
2+
simple,10,1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sku,qty,is_in_stock
2+
simple,0,0
3+
virtual-product,0,0

0 commit comments

Comments
 (0)