2323 */
2424class 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}
0 commit comments