Skip to content

Commit dde4262

Browse files
authored
ENGCOM-8238: Fix issue #29932 "only_x_left_in_stock returns null if inventory is 0" #30000
2 parents 5d2c62a + 63caeac commit dde4262

File tree

3 files changed

+282
-7
lines changed

3 files changed

+282
-7
lines changed

app/code/Magento/CatalogInventoryGraphQl/Model/Resolver/OnlyXLeftInStockResolver.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,8 @@ private function getOnlyXLeftQty(ProductInterface $product): ?float
8686

8787
$stockLeft = $stockCurrentQty - $stockItem->getMinQty();
8888

89-
$thresholdQty = (float)$this->scopeConfig->getValue(
90-
Configuration::XML_PATH_STOCK_THRESHOLD_QTY,
91-
ScopeInterface::SCOPE_STORE
92-
);
93-
94-
if ($stockCurrentQty > 0 && $stockLeft <= $thresholdQty) {
95-
return (float)$stockLeft;
89+
if ($stockCurrentQty >= 0 && $stockLeft <= $thresholdQty) {
90+
return (float)$stockCurrentQty;
9691
}
9792

9893
return null;
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogInventoryGraphQl\Test\Unit\Model\Resolver;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use Magento\CatalogInventoryGraphQl\Model\Resolver\OnlyXLeftInStockResolver;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\GraphQl\Model\Query\ContextInterface;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\CatalogInventory\Api\StockRegistryInterface;
18+
use Magento\Catalog\Model\Product;
19+
use Magento\Store\Api\Data\StoreInterface;
20+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
21+
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
22+
23+
/**
24+
* Test class for \Magento\CatalogInventoryGraphQl\Model\Resolver\OnlyXLeftInStockResolver
25+
*/
26+
class OnlyXLeftInStockResolverTest extends TestCase
27+
{
28+
/**
29+
* Object Manager Instance
30+
*
31+
* @var ObjectManager
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* Testable Object
37+
*
38+
* @var RevokeCustomerToken
39+
*/
40+
private $resolver;
41+
42+
/**
43+
* @var ContextInterface|MockObject
44+
*/
45+
private $contextMock;
46+
47+
/**
48+
* @var Field|MockObject
49+
*/
50+
private $fieldMock;
51+
52+
/**
53+
* @var ResolveInfo|MockObject
54+
*/
55+
private $resolveInfoMock;
56+
57+
/**
58+
* @var ScopeConfigInterface|MockObject
59+
*/
60+
private $scopeConfigMock;
61+
62+
/**
63+
* @var StockRegistryInterface|MockObject
64+
*/
65+
private $stockRegistryMock;
66+
67+
/**
68+
* @var Product|MockObject
69+
*/
70+
private $productModelMock;
71+
72+
/**
73+
* @var StoreInterface|MockObject
74+
*/
75+
private $storeMock;
76+
77+
/**
78+
* @var StockItemInterface|MockObject
79+
*/
80+
private $stockItemMock;
81+
82+
/**
83+
* @var StockStatusInterface|MockObject
84+
*/
85+
private $stockStatusMock;
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
91+
protected function setUp(): void
92+
{
93+
$this->objectManager = new ObjectManager($this);
94+
95+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
96+
->disableOriginalConstructor()
97+
->getMockForAbstractClass();
98+
99+
$this->fieldMock = $this->getMockBuilder(Field::class)
100+
->disableOriginalConstructor()
101+
->getMock();
102+
103+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
104+
->disableOriginalConstructor()
105+
->getMock();
106+
107+
$this->productModelMock = $this->getMockBuilder(Product::class)
108+
->disableOriginalConstructor()
109+
->getMock();
110+
111+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)->getMock();
112+
$this->stockRegistryMock = $this->getMockBuilder(StockRegistryInterface::class)->getMock();
113+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)->getMock();
114+
$this->stockItemMock = $this->getMockBuilder(StockItemInterface::class)->getMock();
115+
$this->stockStatusMock = $this->getMockBuilder(StockStatusInterface::class)->getMock();
116+
$this->productModelMock->expects($this->any())->method('getId')
117+
->willReturn(1);
118+
$this->productModelMock->expects($this->once())->method('getStore')
119+
->willReturn($this->storeMock);
120+
$this->stockRegistryMock->expects($this->once())->method('getStockStatus')
121+
->willReturn($this->stockStatusMock);
122+
$this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1);
123+
124+
$this->resolver = $this->objectManager->getObject(
125+
OnlyXLeftInStockResolver::class,
126+
[
127+
'scopeConfig' => $this->scopeConfigMock,
128+
'stockRegistry' => $this->stockRegistryMock
129+
]
130+
);
131+
}
132+
133+
public function testResolve()
134+
{
135+
$stockCurrentQty = 3;
136+
$minQty = 2;
137+
$thresholdQty = 1;
138+
139+
$this->stockItemMock->expects($this->once())->method('getMinQty')
140+
->willReturn($minQty);
141+
$this->stockStatusMock->expects($this->once())->method('getQty')
142+
->willReturn($stockCurrentQty);
143+
$this->stockRegistryMock->expects($this->once())->method('getStockItem')
144+
->willReturn($this->stockItemMock);
145+
$this->scopeConfigMock->method('getValue')->willReturn($thresholdQty);
146+
147+
$this->assertEquals(
148+
$stockCurrentQty,
149+
$this->resolver->resolve(
150+
$this->fieldMock,
151+
$this->contextMock,
152+
$this->resolveInfoMock,
153+
['model' => $this->productModelMock]
154+
)
155+
);
156+
}
157+
158+
public function testResolveOutStock()
159+
{
160+
$stockCurrentQty = 0;
161+
$minQty = 2;
162+
$thresholdQty = 1;
163+
$this->stockItemMock->expects($this->once())->method('getMinQty')
164+
->willReturn($minQty);
165+
$this->stockStatusMock->expects($this->once())->method('getQty')
166+
->willReturn($stockCurrentQty);
167+
$this->stockRegistryMock->expects($this->once())->method('getStockItem')
168+
->willReturn($this->stockItemMock);
169+
$this->scopeConfigMock->method('getValue')->willReturn($thresholdQty);
170+
171+
$this->assertEquals(
172+
0,
173+
$this->resolver->resolve(
174+
$this->fieldMock,
175+
$this->contextMock,
176+
$this->resolveInfoMock,
177+
['model' => $this->productModelMock]
178+
)
179+
);
180+
}
181+
182+
public function testResolveNoThresholdQty()
183+
{
184+
$stockCurrentQty = 3;
185+
$minQty = 2;
186+
$thresholdQty = null;
187+
$this->stockItemMock->expects($this->once())->method('getMinQty')
188+
->willReturn($minQty);
189+
$this->stockStatusMock->expects($this->once())->method('getQty')
190+
->willReturn($stockCurrentQty);
191+
$this->stockRegistryMock->expects($this->once())->method('getStockItem')
192+
->willReturn($this->stockItemMock);
193+
$this->scopeConfigMock->method('getValue')->willReturn($thresholdQty);
194+
195+
$this->assertEquals(
196+
null,
197+
$this->resolver->resolve(
198+
$this->fieldMock,
199+
$this->contextMock,
200+
$this->resolveInfoMock,
201+
['model' => $this->productModelMock]
202+
)
203+
);
204+
}
205+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/ProductOnlyXLeftInStockTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,52 @@
77

88
namespace Magento\GraphQl\CatalogInventory;
99

10+
use Magento\Config\Model\ResourceModel\Config;
11+
use Magento\Framework\App\Config\ReinitableConfigInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\TestFramework\ObjectManager;
1015
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
use Magento\CatalogInventory\Model\Configuration;
1117

1218
/**
1319
* Test for the product only x left in stock
1420
*/
1521
class ProductOnlyXLeftInStockTest extends GraphQlAbstract
1622
{
23+
/**
24+
* @var ProductRepositoryInterface
25+
*/
26+
private $productRepository;
27+
/**
28+
* @var Config $config
29+
*/
30+
private $resourceConfig;
31+
32+
/**
33+
* @var ScopeConfigInterface
34+
*/
35+
private $scopeConfig;
36+
37+
/**
38+
* @var ReinitableConfigInterface
39+
*/
40+
private $reinitConfig;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
49+
$objectManager = ObjectManager::getInstance();
50+
$this->productRepository = $objectManager->create(ProductRepositoryInterface::class);
51+
$this->resourceConfig = $objectManager->get(Config::class);
52+
$this->scopeConfig = $objectManager->get(ScopeConfigInterface::class);
53+
$this->reinitConfig = $objectManager->get(ReinitableConfigInterface::class);
54+
}
55+
1756
/**
1857
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_all_fields.php
1958
*/
@@ -63,4 +102,40 @@ public function testQueryProductOnlyXLeftInStockEnabled()
63102
$this->assertArrayHasKey('only_x_left_in_stock', $response['products']['items'][0]);
64103
$this->assertEquals(100, $response['products']['items'][0]['only_x_left_in_stock']);
65104
}
105+
106+
/**
107+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_out_of_stock_without_categories.php
108+
* @magentoConfigFixture default_store cataloginventory/options/stock_threshold_qty 120
109+
*/
110+
public function testQueryProductOnlyXLeftInStockOutstock()
111+
{
112+
$productSku = 'simple';
113+
$showOutOfStock = $this->scopeConfig->getValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK);
114+
115+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, 1);
116+
$this->reinitConfig->reinit();
117+
118+
// need to resave product to reindex it with new configuration.
119+
$product = $this->productRepository->get($productSku);
120+
$this->productRepository->save($product);
121+
122+
$query = <<<QUERY
123+
{
124+
products(filter: {sku: {eq: "{$productSku}"}})
125+
{
126+
items {
127+
only_x_left_in_stock
128+
}
129+
}
130+
}
131+
QUERY;
132+
$response = $this->graphQlQuery($query);
133+
134+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, $showOutOfStock);
135+
$this->reinitConfig->reinit();
136+
137+
$this->assertArrayHasKey(0, $response['products']['items']);
138+
$this->assertArrayHasKey('only_x_left_in_stock', $response['products']['items'][0]);
139+
$this->assertEquals(0, $response['products']['items'][0]['only_x_left_in_stock']);
140+
}
66141
}

0 commit comments

Comments
 (0)