From d40914e45d14ca50fdea9f4440cb58ceb7e1ac6f Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 12 Nov 2024 08:53:42 +0530 Subject: [PATCH 01/10] ACP2E-3355: Cron reindex fails to update product availability on the frontend --- .../StockItemRepositoryPlugin.php | 74 +++++++++++++++++++ InventoryCatalog/etc/webapi_rest/di.xml | 3 + 2 files changed, 77 insertions(+) create mode 100644 InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php diff --git a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php new file mode 100644 index 000000000000..d2d32efc5d4c --- /dev/null +++ b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php @@ -0,0 +1,74 @@ +fullProductIndexer = $fullProductIndexer; + $this->inventoryIndexer = $inventoryIndexer; + $this->productRepository = $productRepository; + $this->getSourceItemsBySku = $getSourceItemsBySku; + } + + /** + * Complex reindex after product stock item has been saved. + * + * @param StockItemRepository $subject + * @param StockItemInterface $stockItem + * @return StockItemInterface + * @throws NoSuchEntityException + */ + public function afterSave(StockItemRepository $subject, StockItemInterface $stockItem): StockItemInterface + { + $product = $this->productRepository->getById($stockItem->getProductId()); + $this->fullProductIndexer->executeRow($product->getId()); + $sourceItems = $this->getSourceItemsBySku->execute($product->getSku()); + $sourceItemIds = []; + + foreach ($sourceItems as $sourceItem) { + $sourceItemIds[] = $sourceItem->getId(); + } + $this->inventoryIndexer->executeList($sourceItemIds); + return $stockItem; + } +} diff --git a/InventoryCatalog/etc/webapi_rest/di.xml b/InventoryCatalog/etc/webapi_rest/di.xml index 223b6edb2d02..5c114681cb0d 100644 --- a/InventoryCatalog/etc/webapi_rest/di.xml +++ b/InventoryCatalog/etc/webapi_rest/di.xml @@ -9,4 +9,7 @@ + + + From a7abea0585b2a9b85b014618dbe1c3b964139287 Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 12 Nov 2024 10:05:07 +0530 Subject: [PATCH 02/10] ACP2E-3355: Cron reindex fails to update product availability on the frontend --- InventoryCatalog/etc/webapi_rest/di.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InventoryCatalog/etc/webapi_rest/di.xml b/InventoryCatalog/etc/webapi_rest/di.xml index 5c114681cb0d..4197b26b524d 100644 --- a/InventoryCatalog/etc/webapi_rest/di.xml +++ b/InventoryCatalog/etc/webapi_rest/di.xml @@ -10,6 +10,6 @@ - + From f26a7a62d22c29ee55ebee2456956caa2a2a247d Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 12 Nov 2024 12:09:49 +0530 Subject: [PATCH 03/10] ACP2E-3355: Cron reindex fails to update product availability on the frontend --- .../Stock/StockItemRepository/StockItemRepositoryPlugin.php | 5 +++++ InventoryCatalog/etc/webapi_rest/di.xml | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php index d2d32efc5d4c..1842f0e23ddf 100644 --- a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php +++ b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php @@ -1,4 +1,9 @@ From 45b8375be8f37c419af786c1615e327cc5225f05 Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 12 Nov 2024 17:58:52 +0530 Subject: [PATCH 04/10] ACP2E-3355: Cron reindex fails to update product availability on the frontend --- .../StockItemRepositoryPlugin.php | 34 +++---------------- InventoryCatalog/etc/webapi_rest/di.xml | 2 +- InventoryCatalog/etc/webapi_soap/di.xml | 7 ++-- 3 files changed, 11 insertions(+), 32 deletions(-) diff --git a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php index 1842f0e23ddf..730d08b97d24 100644 --- a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php +++ b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php @@ -17,25 +17,6 @@ class StockItemRepositoryPlugin { - /** - * @var FullProductIndexer - */ - private $fullProductIndexer; - - /** - * @var ProductRepositoryInterface - */ - private $productRepository; - - /** - * @var ProductRepositoryInterface - */ - private $inventoryIndexer; - - /** - * @var GetSourceItemsBySku - */ - private $getSourceItemsBySku; /** * @param FullProductIndexer $fullProductIndexer @@ -44,16 +25,11 @@ class StockItemRepositoryPlugin * @param GetSourceItemsBySku $getSourceItemsBySku */ public function __construct( - FullProductIndexer $fullProductIndexer, - InventoryIndexer $inventoryIndexer, - ProductRepositoryInterface $productRepository, - getSourceItemsBySku $getSourceItemsBySku - ) { - $this->fullProductIndexer = $fullProductIndexer; - $this->inventoryIndexer = $inventoryIndexer; - $this->productRepository = $productRepository; - $this->getSourceItemsBySku = $getSourceItemsBySku; - } + private FullProductIndexer $fullProductIndexer, + private InventoryIndexer $inventoryIndexer, + private ProductRepositoryInterface $productRepository, + private getSourceItemsBySku $getSourceItemsBySku + ) {} /** * Complex reindex after product stock item has been saved. diff --git a/InventoryCatalog/etc/webapi_rest/di.xml b/InventoryCatalog/etc/webapi_rest/di.xml index d5c459185674..4118e19caf38 100644 --- a/InventoryCatalog/etc/webapi_rest/di.xml +++ b/InventoryCatalog/etc/webapi_rest/di.xml @@ -1,7 +1,7 @@ diff --git a/InventoryCatalog/etc/webapi_soap/di.xml b/InventoryCatalog/etc/webapi_soap/di.xml index 223b6edb2d02..4118e19caf38 100644 --- a/InventoryCatalog/etc/webapi_soap/di.xml +++ b/InventoryCatalog/etc/webapi_soap/di.xml @@ -1,12 +1,15 @@ + + + From 9caa0f60fa8ac22c5e544f15e921343b8138cca0 Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 12 Nov 2024 18:01:12 +0530 Subject: [PATCH 05/10] ACP2E-3355: Cron reindex fails to update product availability on the frontend --- .../Stock/StockItemRepository/StockItemRepositoryPlugin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php index 730d08b97d24..b3261b885928 100644 --- a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php +++ b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php @@ -29,7 +29,8 @@ public function __construct( private InventoryIndexer $inventoryIndexer, private ProductRepositoryInterface $productRepository, private getSourceItemsBySku $getSourceItemsBySku - ) {} + ) { + } /** * Complex reindex after product stock item has been saved. From 29d7e3a3019e7a147c75b427ebe1922419b96c30 Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 12 Nov 2024 18:18:20 +0530 Subject: [PATCH 06/10] ACP2E-3355: Cron reindex fails to update product availability on the frontend --- .../StockItemRepositoryPlugin.php | 13 ++++++++++++- InventoryCatalog/etc/webapi_rest/di.xml | 13 ++++++++++++- InventoryCatalog/etc/webapi_soap/di.xml | 13 ++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php index b3261b885928..e6775e7d74d2 100644 --- a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php +++ b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php @@ -1,7 +1,18 @@ diff --git a/InventoryCatalog/etc/webapi_soap/di.xml b/InventoryCatalog/etc/webapi_soap/di.xml index 4118e19caf38..b5837fe856a5 100644 --- a/InventoryCatalog/etc/webapi_soap/di.xml +++ b/InventoryCatalog/etc/webapi_soap/di.xml @@ -1,8 +1,19 @@ From e104902bc2ac71d8910964eae755e4de82176228 Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 12 Nov 2024 19:10:51 +0530 Subject: [PATCH 07/10] ACP2E-3355: Cron reindex fails to update product availability on the frontend --- .../StockItemRepositoryPlugin.php | 13 +------------ InventoryCatalog/etc/webapi_rest/di.xml | 13 +------------ InventoryCatalog/etc/webapi_soap/di.xml | 13 +------------ 3 files changed, 3 insertions(+), 36 deletions(-) diff --git a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php index e6775e7d74d2..b3261b885928 100644 --- a/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php +++ b/InventoryCatalog/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPlugin.php @@ -1,18 +1,7 @@ diff --git a/InventoryCatalog/etc/webapi_soap/di.xml b/InventoryCatalog/etc/webapi_soap/di.xml index b5837fe856a5..4118e19caf38 100644 --- a/InventoryCatalog/etc/webapi_soap/di.xml +++ b/InventoryCatalog/etc/webapi_soap/di.xml @@ -1,19 +1,8 @@ From a18e33de53dac0e420571a8fb57f3793f3699f6e Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 26 Nov 2024 15:03:32 +0530 Subject: [PATCH 08/10] ACP2E-3355: Test Coverage added --- .../Api/StockItemRepositoryTest.php | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php diff --git a/dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php new file mode 100644 index 000000000000..d52aa264ef10 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php @@ -0,0 +1,136 @@ +objectManager = Bootstrap::getObjectManager(); + $this->fixtures = DataFixtureStorageManager::getStorage(); + $this->command = $this->objectManager->get(CronCommand::class); + $this->isProductSalable = $this->objectManager->get(IsProductSalableInterface::class); + $this->inputMock = $this->getMockBuilder(InputInterface::class)->getMockForAbstractClass(); + $this->outputMock = $this->getMockBuilder(OutputInterface::class)->getMockForAbstractClass(); + } + + + /** + * Test stock status for product after stock item update + */ + #[ + Config('cataloginventory/options/show_out_of_stock', '1'), + DataFixture(SourceFixture::class, as: 'src1'), + DataFixture(StockFixture::class, as: 'stk2'), + DataFixture( + StockSourceLinksFixture::class, + [ + ['stock_id' => '$stk2.stock_id$', 'source_code' => '$src1.source_code$'], + ] + ), + DataFixture( + StockSalesChannelsFixture::class, + ['stock_id' => '$stk2.stock_id$', 'sales_channels' => ['base']] + ), + DataFixture(ProductFixture::class, as: 'p1'), + DataFixture( + SourceItemsFixture::class, + [ + ['sku' => '$p1.sku$', 'source_code' => 'default', 'quantity' => 0], + ['sku' => '$p1.sku$', 'source_code' => '$src1.source_code$', 'quantity' => 0], + ] + ), + ] + public function testProductIsInStockAfterUpdateStockItem(): void + { + $product = $this->fixtures->get('p1'); + $stockId = $this->fixtures->get('stk2')->getStockId(); + $productSku = $product->getSku(); + $isSalable = $this->isProductSalable->execute($productSku, $stockId); + $this->assertFalse($isSalable); + $productParams = [ + "sku"=> $productSku, + "extension_attributes"=> [ + "stock_item"=> [ + "stock_id"=> $stockId, + "is_in_stock"=> 1, + "use_config_backorders"=> false, + "backorders"=> 1, + ] + ], + ]; + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH, + 'httpMethod' => Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'Save', + ], + ]; + $this->_webApiCall($serviceInfo, ['product' => $productParams]); + $isSalableAfterUpdate = $this->isProductSalable->execute($productSku, $stockId); + $this->assertTrue($isSalableAfterUpdate); + } +} From a32440204916b38bca8743c3fc6ff0aba96fb924 Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 3 Dec 2024 12:24:28 +0530 Subject: [PATCH 09/10] ACP2E-3355: Test Coverage removed --- .../Api/StockItemRepositoryTest.php | 136 ------------------ 1 file changed, 136 deletions(-) delete mode 100644 dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php diff --git a/dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php deleted file mode 100644 index d52aa264ef10..000000000000 --- a/dev/tests/api-functional/testsuite/Magento/InventoryCatalog/Api/StockItemRepositoryTest.php +++ /dev/null @@ -1,136 +0,0 @@ -objectManager = Bootstrap::getObjectManager(); - $this->fixtures = DataFixtureStorageManager::getStorage(); - $this->command = $this->objectManager->get(CronCommand::class); - $this->isProductSalable = $this->objectManager->get(IsProductSalableInterface::class); - $this->inputMock = $this->getMockBuilder(InputInterface::class)->getMockForAbstractClass(); - $this->outputMock = $this->getMockBuilder(OutputInterface::class)->getMockForAbstractClass(); - } - - - /** - * Test stock status for product after stock item update - */ - #[ - Config('cataloginventory/options/show_out_of_stock', '1'), - DataFixture(SourceFixture::class, as: 'src1'), - DataFixture(StockFixture::class, as: 'stk2'), - DataFixture( - StockSourceLinksFixture::class, - [ - ['stock_id' => '$stk2.stock_id$', 'source_code' => '$src1.source_code$'], - ] - ), - DataFixture( - StockSalesChannelsFixture::class, - ['stock_id' => '$stk2.stock_id$', 'sales_channels' => ['base']] - ), - DataFixture(ProductFixture::class, as: 'p1'), - DataFixture( - SourceItemsFixture::class, - [ - ['sku' => '$p1.sku$', 'source_code' => 'default', 'quantity' => 0], - ['sku' => '$p1.sku$', 'source_code' => '$src1.source_code$', 'quantity' => 0], - ] - ), - ] - public function testProductIsInStockAfterUpdateStockItem(): void - { - $product = $this->fixtures->get('p1'); - $stockId = $this->fixtures->get('stk2')->getStockId(); - $productSku = $product->getSku(); - $isSalable = $this->isProductSalable->execute($productSku, $stockId); - $this->assertFalse($isSalable); - $productParams = [ - "sku"=> $productSku, - "extension_attributes"=> [ - "stock_item"=> [ - "stock_id"=> $stockId, - "is_in_stock"=> 1, - "use_config_backorders"=> false, - "backorders"=> 1, - ] - ], - ]; - $serviceInfo = [ - 'rest' => [ - 'resourcePath' => self::RESOURCE_PATH, - 'httpMethod' => Request::HTTP_METHOD_POST, - ], - 'soap' => [ - 'service' => self::SERVICE_NAME, - 'serviceVersion' => self::SERVICE_VERSION, - 'operation' => self::SERVICE_NAME . 'Save', - ], - ]; - $this->_webApiCall($serviceInfo, ['product' => $productParams]); - $isSalableAfterUpdate = $this->isProductSalable->execute($productSku, $stockId); - $this->assertTrue($isSalableAfterUpdate); - } -} From 25e8915833dc3a42da65bc708ea54f2372da3c89 Mon Sep 17 00:00:00 2001 From: lakshmana49 Date: Tue, 3 Dec 2024 13:10:08 +0530 Subject: [PATCH 10/10] ACP2E-3355: Test Coverage Added --- .../StockItemRepositoryPluginTest.php | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 InventoryCatalog/Test/Unit/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php diff --git a/InventoryCatalog/Test/Unit/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php b/InventoryCatalog/Test/Unit/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php new file mode 100644 index 000000000000..03b7f06b977d --- /dev/null +++ b/InventoryCatalog/Test/Unit/Plugin/CatalogInventory/Model/Stock/StockItemRepository/StockItemRepositoryPluginTest.php @@ -0,0 +1,81 @@ +fullProductIndexer = $this->createMock(FullProductIndexer::class); + $this->inventoryIndexer = $this->createMock(InventoryIndexer::class); + $this->productRepository = $this->createMock(ProductRepositoryInterface::class); + $this->getSourceItemsBySku = $this->createMock(GetSourceItemsBySku::class); + + $this->plugin = new StockItemRepositoryPlugin( + $this->fullProductIndexer, + $this->inventoryIndexer, + $this->productRepository, + $this->getSourceItemsBySku + ); + } + + public function testAfterSave(): void + { + $productId = 123; + $sku = 'test-sku'; + $sourceItemId = 456; + + $stockItem = $this->createMock(StockItemInterface::class); + $stockItem->method('getProductId')->willReturn($productId); + + $product = $this->createMock(\Magento\Catalog\Api\Data\ProductInterface::class); + $product->method('getId')->willReturn($productId); + $product->method('getSku')->willReturn($sku); + $this->productRepository->method('getById')->with($productId)->willReturn($product); + + $sourceItem = $this->createMock(SourceItem::class); + $sourceItem->method('getId')->willReturn($sourceItemId); + $this->getSourceItemsBySku->method('execute')->with($sku)->willReturn([$sourceItem]); + + $this->fullProductIndexer->expects($this->once())->method('executeRow')->with($productId); + $this->inventoryIndexer->expects($this->once())->method('executeList')->with([$sourceItemId]); + + $result = $this->plugin->afterSave( + $this->createMock(StockItemRepository::class), + $stockItem + ); + + $this->assertSame($stockItem, $result); + } +}