33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace 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 ;
818use Magento \Framework \App \Filesystem \DirectoryList ;
19+ use Magento \Framework \Exception \LocalizedException ;
20+ use Magento \Framework \Filesystem ;
21+ use Magento \Framework \ObjectManagerInterface ;
922use 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}
0 commit comments