Skip to content

Commit 76588e7

Browse files
author
Anna Bukatar
committed
ACP2E-813: Configurable Stock Not Updated After a Child is Back in Stock
1 parent a945273 commit 76588e7

File tree

2 files changed

+64
-37
lines changed

2 files changed

+64
-37
lines changed

dev/tests/integration/testsuite/Magento/CatalogInventory/Observer/SaveInventoryDataObserverTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Magento\Framework\Exception\InputException;
2020
use Magento\Framework\Exception\StateException;
2121
use Magento\Framework\Exception\CouldNotSaveException;
22+
use Magento\TestFramework\Fixture\DataFixtureStorage;
23+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
2224

2325
/**
2426
* Test for SaveInventoryDataObserver
@@ -35,6 +37,11 @@ class SaveInventoryDataObserverTest extends TestCase
3537
*/
3638
private $stockItemRepository;
3739

40+
/**
41+
* @var DataFixtureStorage
42+
*/
43+
private $fixtures;
44+
3845
/**
3946
* @inheritDoc
4047
*/
@@ -44,6 +51,7 @@ protected function setUp(): void
4451
->get(ProductRepositoryInterface::class);
4552
$this->stockItemRepository = Bootstrap::getObjectManager()
4653
->get(StockItemRepositoryInterface::class);
54+
$this->fixtures = DataFixtureStorageManager::getStorage();
4755
}
4856

4957
/**
@@ -66,7 +74,6 @@ public function testAutoChangingIsInStockForParent()
6674

6775
/** @var ProductExtensionInterface $attributes*/
6876
$attributes = $product->getExtensionAttributes();
69-
7077
/** @var StockItemInterface $stockItem */
7178
$stockItem = $attributes->getStockItem();
7279
$stockItem->setQty(0);
@@ -75,9 +82,29 @@ public function testAutoChangingIsInStockForParent()
7582
$product->setExtensionAttributes($attributes);
7683
$this->productRepository->save($product);
7784

78-
/** @var ProductInterface $product */
79-
$parentProduct = $this->productRepository->get('configurable');
85+
/** @var ProductInterface $product */
86+
$parentProduct = $this->productRepository->get('configurable');
87+
88+
$parentProductStockItem = $this->stockItemRepository->get(
89+
$parentProduct->getExtensionAttributes()->getStockItem()->getItemId()
90+
);
91+
$this->assertFalse($parentProductStockItem->getIsInStock());
92+
}
8093

94+
/**
95+
* Check that a configurable product will be created with out_of_stock status if no children in stock
96+
*
97+
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"stock_item":{"qty": 0}} as:p1
98+
* @magentoDataFixture Magento\ConfigurableProduct\Test\Fixture\Attribute as:attr
99+
* @magentoDataFixture Magento\ConfigurableProduct\Test\Fixture\Product as:conf1
100+
* @magentoDataFixtureDataProvider {"conf1":{"_options":["$attr$"],"_links":["$p1$"]}}
101+
* @return void
102+
*/
103+
public function testAutoChangingIsInStockForNewConfigurable(): void
104+
{
105+
$sku = $this->fixtures->get('conf1')->getSku();
106+
/** @var ProductInterface $parentProduct */
107+
$parentProduct = $this->productRepository->get($sku);
81108
$parentProductStockItem = $this->stockItemRepository->get(
82109
$parentProduct->getExtensionAttributes()->getStockItem()->getItemId()
83110
);

dev/tests/integration/testsuite/Magento/ConfigurableImportExport/Model/Import/Product/Type/ConfigurableTest.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ConfigurableTest extends TestCase
3333
/**
3434
* Configurable product test Type
3535
*/
36-
const TEST_PRODUCT_TYPE = 'configurable';
36+
public const TEST_PRODUCT_TYPE = 'configurable';
3737

3838
/**
3939
* @var \Magento\CatalogImportExport\Model\Import\Product
@@ -59,6 +59,39 @@ protected function setUp(): void
5959
$this->productMetadata = $metadataPool->getMetadata(ProductInterface::class);
6060
}
6161

62+
/**
63+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
64+
*/
65+
public function testShouldUpdateConfigurableStockStatusIfChildProductsStockStatusChanged(): void
66+
{
67+
$sku = 'configurable';
68+
/** @var ProductRepositoryInterface $productRepository */
69+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
70+
/** @var ProductInterface $product */
71+
$product = $productRepository->get($sku, true, null, true);
72+
$stockItem = $this->getStockItem((int) $product->getId());
73+
$this->assertNotNull($stockItem);
74+
$this->assertTrue($stockItem->getIsInStock());
75+
76+
// Set all child product out of stock
77+
$pathToFile = __DIR__ . '/../../_files/import_configurable_child_products_stock_item_status_out_of_stock.csv';
78+
$errors = $this->doImport($pathToFile);
79+
$this->assertEquals(0, $errors->getErrorsCount());
80+
81+
$stockItem = $this->getStockItem((int) $product->getId());
82+
$this->assertNotNull($stockItem);
83+
$this->assertFalse($stockItem->getIsInStock());
84+
85+
// Set some child product in stock
86+
$pathToFile = __DIR__ . '/../../_files/import_configurable_child_products_stock_item_status_in_stock.csv';
87+
$errors = $this->doImport($pathToFile);
88+
$this->assertEquals(0, $errors->getErrorsCount());
89+
90+
$stockItem = $this->getStockItem((int) $product->getId());
91+
$this->assertNotNull($stockItem);
92+
$this->assertTrue($stockItem->getIsInStock());
93+
}
94+
6295
public function configurableImportDataProvider()
6396
{
6497
return [
@@ -202,39 +235,6 @@ public function testConfigurableImportWithStoreSpecifiedMainItem()
202235
}
203236
}
204237

205-
/**
206-
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
207-
*/
208-
public function testShouldUpdateConfigurableStockStatusIfChildProductsStockStatusChanged(): void
209-
{
210-
$sku = 'configurable';
211-
/** @var ProductRepositoryInterface $productRepository */
212-
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
213-
/** @var ProductInterface $product */
214-
$product = $productRepository->get($sku, true, null, true);
215-
$stockItem = $this->getStockItem((int) $product->getId());
216-
$this->assertNotNull($stockItem);
217-
$this->assertTrue($stockItem->getIsInStock());
218-
219-
// Set all child product out of stock
220-
$pathToFile = __DIR__ . '/../../_files/import_configurable_child_products_stock_item_status_out_of_stock.csv';
221-
$errors = $this->doImport($pathToFile);
222-
$this->assertEquals(0, $errors->getErrorsCount());
223-
224-
$stockItem = $this->getStockItem((int) $product->getId());
225-
$this->assertNotNull($stockItem);
226-
$this->assertFalse($stockItem->getIsInStock());
227-
228-
// Set some child product in stock
229-
$pathToFile = __DIR__ . '/../../_files/import_configurable_child_products_stock_item_status_in_stock.csv';
230-
$errors = $this->doImport($pathToFile);
231-
$this->assertEquals(0, $errors->getErrorsCount());
232-
233-
$stockItem = $this->getStockItem((int) $product->getId());
234-
$this->assertNotNull($stockItem);
235-
$this->assertTrue($stockItem->getIsInStock());
236-
}
237-
238238
/**
239239
* @param int $productId
240240
* @return StockItemInterface|null

0 commit comments

Comments
 (0)