Skip to content

Commit bd65f3a

Browse files
committed
ACP2E-3413: All products are indexed with [is_out_of_stock] = 1 when the SKU is not set as a searchable attribute
1 parent dce0b52 commit bd65f3a

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

InventoryElasticsearch/Plugin/Model/Adapter/BatchDataMapper/ProductDataMapperPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function afterMap(
5050
$store = $this->storeRepository->getById($storeId);
5151
$stock = $this->stockByWebsiteIdResolver->execute((int)$store->getWebsiteId());
5252
$skus = $this->getSkusByProductIds->execute(array_keys($documents));
53-
$stockItems = $this->getStockItemsData->execute($skus, $stock->getStockId());
53+
$stockItems = $this->getStockItemsData->execute(array_values($skus), $stock->getStockId());
5454
foreach ($documents as $productId => $document) {
5555
$sku = $skus[$productId];
5656
$document['is_out_of_stock'] = (int)!($stockItems[$sku][GetStockItemsDataInterface::IS_SALABLE] ?? 0);

InventoryElasticsearch/Test/Unit/Plugin/Model/Adapter/BatchDataMapper/ProductDataMapperPluginTest.php

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2022 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -10,11 +10,12 @@
1010
use Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\Exception\NoSuchEntityException;
13-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1413
use Magento\InventoryApi\Api\Data\StockInterface;
14+
use Magento\InventoryCatalog\Model\GetSkusByProductIds;
1515
use Magento\InventoryElasticsearch\Plugin\Model\Adapter\BatchDataMapper\ProductDataMapperPlugin;
16-
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
16+
use Magento\InventorySalesApi\Model\GetStockItemsDataInterface;
1717
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
18+
use Magento\Store\Api\Data\StoreInterface;
1819
use Magento\Store\Api\StoreRepositoryInterface;
1920
use PHPUnit\Framework\MockObject\MockObject;
2021
use PHPUnit\Framework\TestCase;
@@ -40,35 +41,35 @@ class ProductDataMapperPluginTest extends TestCase
4041
private $storeRepositoryMock;
4142

4243
/**
43-
* @var GetStockItemDataInterface|MockObject
44+
* @var GetStockItemsDataInterface|MockObject
4445
*/
45-
private $getStockItemDataMock;
46+
private $getStockItemsDataMock;
4647

4748
/**
4849
* @var ProductDataMapper|MockObject
4950
*/
5051
private $productDataMapperMock;
5152

53+
/**
54+
* @var GetSkusByProductIds|MockObject
55+
*/
56+
private $getSkusByProductIdsMock;
57+
5258
/**
5359
* @inheirtDoc
5460
*/
5561
protected function setUp(): void
5662
{
57-
$this->stockByWebsiteIdResolverMock = $this->getMockForAbstractClass(StockByWebsiteIdResolverInterface::class);
58-
$this->storeRepositoryMock = $this->getMockBuilder(StoreRepositoryInterface::class)
59-
->disableOriginalConstructor()
60-
->onlyMethods(['getById'])
61-
->addMethods(['getWebsiteId'])
62-
->getMockForAbstractClass();
63-
$this->getStockItemDataMock = $this->createMock(GetStockItemDataInterface::class);
63+
$this->stockByWebsiteIdResolverMock = $this->createMock(StockByWebsiteIdResolverInterface::class);
64+
$this->storeRepositoryMock = $this->createMock(StoreRepositoryInterface::class);
65+
$this->getStockItemsDataMock = $this->createMock(GetStockItemsDataInterface::class);
6466
$this->productDataMapperMock = $this->createMock(ProductDataMapper::class);
65-
$this->plugin = (new ObjectManager($this))->getObject(
66-
ProductDataMapperPlugin::class,
67-
[
68-
'stockByWebsiteIdResolver' => $this->stockByWebsiteIdResolverMock,
69-
'storeRepository' => $this->storeRepositoryMock,
70-
'getStockItemData' => $this->getStockItemDataMock
71-
]
67+
$this->getSkusByProductIdsMock = $this->createMock(GetSkusByProductIds::class);
68+
$this->plugin = new ProductDataMapperPlugin(
69+
$this->stockByWebsiteIdResolverMock,
70+
$this->storeRepositoryMock,
71+
$this->getStockItemsDataMock,
72+
$this->getSkusByProductIdsMock
7273
);
7374
}
7475

@@ -85,43 +86,42 @@ protected function setUp(): void
8586
*/
8687
public function testAfterMap(int $storeId, int $websiteId, int $stockId, int $salability): void
8788
{
89+
$productId = 123;
8890
$sku = '24-MB01';
89-
$attribute = ['is_out_of_stock' => $salability];
91+
$attribute = ['is_out_of_stock' => (int)!$salability];
9092
$documents = [
91-
1 => [
93+
$productId => [
9294
'store_id' => $storeId,
93-
'sku' => $sku,
94-
'status' => $salability
95+
'status' => 1,
9596
],
9697
];
97-
$expectedResult[1] = array_merge($documents[1], $attribute);
98+
$expectedResult = [$productId => array_merge($documents[$productId], $attribute)];
9899

99-
$this->storeRepositoryMock
100-
->expects($this->once())
100+
$storeMock = $this->createMock(StoreInterface::class);
101+
$this->storeRepositoryMock->expects(self::once())
101102
->method('getById')
102103
->with($storeId)
103-
->willReturnSelf();
104-
$this->storeRepositoryMock
105-
->expects($this->once())
104+
->willReturn($storeMock);
105+
$storeMock->expects(self::atLeastOnce())
106106
->method('getWebsiteId')
107107
->willReturn($websiteId);
108108

109-
$stock = $this->getMockForAbstractClass(StockInterface::class);
110-
$stock->method('getStockId')
109+
$stock = $this->createMock(StockInterface::class);
110+
$stock->expects(self::atLeastOnce())
111+
->method('getStockId')
111112
->willReturn($stockId);
112-
$this->stockByWebsiteIdResolverMock
113+
$this->stockByWebsiteIdResolverMock->expects(self::once())
113114
->method('execute')
115+
->with($websiteId)
114116
->willReturn($stock);
115-
116-
$this->getStockItemDataMock->expects($this->atLeastOnce())
117+
$this->getSkusByProductIdsMock->expects(self::once())
118+
->method('execute')
119+
->with([$productId])
120+
->willReturn([$productId => $sku]);
121+
$this->getStockItemsDataMock->expects(self::once())
117122
->method('execute')
118-
->willReturnCallback(
119-
function ($sku) use ($salability) {
120-
return isset($sku)
121-
? ['is_salable' => $salability]
122-
: null;
123-
}
124-
);
123+
->with([$sku], $stockId)
124+
->willReturn([$sku => ['is_salable' => $salability]]);
125125

126126
$this->assertSame(
127127
$expectedResult,

0 commit comments

Comments
 (0)