Skip to content

Commit baeb177

Browse files
committed
Removed deprecated methods, get data using MSI and unit test added
1 parent cc2409a commit baeb177

File tree

8 files changed

+164
-130
lines changed

8 files changed

+164
-130
lines changed

app/code/Magento/CatalogInventory/Block/Plugin/ProductView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function afterGetQuantityValidators(
4040
return array_merge(
4141
$validators,
4242
$this->productQuantityValidator->getData(
43-
$block->getProduct()->getId(),
43+
$block->getProduct()->getSku(),
4444
$block->getProduct()->getStore()->getWebsiteId()
4545
)
4646
);

app/code/Magento/CatalogInventory/Model/Product/QuantityValidator.php

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,79 @@
77

88
namespace Magento\CatalogInventory\Model\Product;
99

10-
use Magento\CatalogInventory\Api\StockRegistryInterface;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
12+
use Magento\InventorySalesApi\Api\StockResolverInterface;
13+
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
1115

1216
class QuantityValidator
1317
{
1418
/**
15-
* @param StockRegistryInterface $stockRegistry
19+
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
20+
* @param StoreManagerInterface $storeManager
21+
* @param StockResolverInterface $stockResolver
1622
*/
1723
public function __construct(
18-
private readonly StockRegistryInterface $stockRegistry
24+
private readonly GetStockItemConfigurationInterface $getStockItemConfiguration,
25+
private readonly StoreManagerInterface $storeManager,
26+
private readonly StockResolverInterface $stockResolver
1927
) {
2028
}
2129

2230
/**
2331
* To get quantity validators
2432
*
25-
* @param int $productId
33+
* @param string $sku
2634
* @param int|null $websiteId
2735
*
2836
* @return array
2937
*/
30-
public function getData(int $productId, int|null $websiteId): array
38+
public function getData(string $sku, int|null $websiteId): array
3139
{
32-
$stockItem = $this->stockRegistry->getStockItem($productId, $websiteId);
40+
try {
41+
$stockItemConfig = $this->getStockItemConfiguration->execute(
42+
$sku,
43+
$this->getStockId($websiteId)
44+
);
3345

34-
$params = [];
35-
$validators = [];
36-
$params['minAllowed'] = $stockItem->getMinSaleQty();
37-
if ($stockItem->getMaxSaleQty()) {
38-
$params['maxAllowed'] = $stockItem->getMaxSaleQty();
46+
$params = [];
47+
$validators = [];
48+
$params['minAllowed'] = $stockItemConfig->getMinSaleQty();
49+
50+
if ($stockItemConfig->getMaxSaleQty()) {
51+
$params['maxAllowed'] = $stockItemConfig->getMaxSaleQty();
52+
}
53+
if ($stockItemConfig->getQtyIncrements() > 0) {
54+
$params['qtyIncrements'] = $stockItemConfig->getQtyIncrements();
55+
}
56+
$validators['validate-item-quantity'] = $params;
57+
58+
return $validators;
59+
} catch (\Exception $e) {
60+
return [];
3961
}
40-
if ($stockItem->getQtyIncrements() > 0) {
41-
$params['qtyIncrements'] = (float) $stockItem->getQtyIncrements();
62+
}
63+
64+
/**
65+
* Get Stock ID by Website ID
66+
*
67+
* @param int|null $websiteId
68+
* @return int
69+
* @throws LocalizedException
70+
*/
71+
private function getStockId(?int $websiteId): int
72+
{
73+
if ($websiteId === null) {
74+
$websiteId = $this->storeManager->getWebsite()->getId();
4275
}
43-
$validators['validate-item-quantity'] = $params;
4476

45-
return $validators;
77+
$websiteCode = $this->storeManager->getWebsite($websiteId)->getCode();
78+
$stock = $this->stockResolver->execute(
79+
SalesChannelInterface::TYPE_WEBSITE,
80+
$websiteCode
81+
);
82+
83+
return (int) $stock->getStockId();
4684
}
4785
}

app/code/Magento/CatalogInventory/Test/Unit/Block/Plugin/ProductViewTest.php

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Magento\CatalogInventory\Test\Unit\Model\Product;
5+
6+
use Magento\CatalogInventory\Model\Product\QuantityValidator;
7+
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
8+
use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
9+
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
10+
use Magento\InventoryApi\Api\Data\StockInterface;
11+
use Magento\InventorySalesApi\Api\StockResolverInterface;
12+
use Magento\Store\Api\Data\WebsiteInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* @covers \Magento\CatalogInventory\Model\Product\QuantityValidator
19+
*/
20+
class QuantityValidatorTest extends TestCase
21+
{
22+
private const WEBSITE_ID = 1;
23+
private const WEBSITE_CODE = 'base';
24+
private const STOCK_ID = 1;
25+
private const TEST_SKU = 'TEST-SKU';
26+
27+
/**
28+
* @var QuantityValidator
29+
*/
30+
private $quantityValidator;
31+
32+
/**
33+
* @var GetStockItemConfigurationInterface|MockObject
34+
*/
35+
private $getStockItemConfiguration;
36+
37+
/**
38+
* @var StoreManagerInterface|MockObject
39+
*/
40+
private $storeManager;
41+
42+
/**
43+
* @var StockResolverInterface|MockObject
44+
*/
45+
private $stockResolver;
46+
47+
protected function setUp(): void
48+
{
49+
$this->getStockItemConfiguration = $this->createMock(GetStockItemConfigurationInterface::class);
50+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
51+
$this->stockResolver = $this->createMock(StockResolverInterface::class);
52+
53+
$this->quantityValidator = new QuantityValidator(
54+
$this->getStockItemConfiguration,
55+
$this->storeManager,
56+
$this->stockResolver
57+
);
58+
}
59+
60+
public function testGetDataWithValidators(): void
61+
{
62+
$website = $this->createMock(WebsiteInterface::class);
63+
$website->method('getCode')->willReturn(self::WEBSITE_CODE);
64+
65+
$stock = $this->createMock(StockInterface::class);
66+
$stock->method('getStockId')->willReturn(self::STOCK_ID);
67+
68+
$stockItemConfiguration = $this->createMock(StockItemConfigurationInterface::class);
69+
$stockItemConfiguration->method('getMinSaleQty')->willReturn(2.0);
70+
$stockItemConfiguration->method('getMaxSaleQty')->willReturn(10.0);
71+
$stockItemConfiguration->method('getQtyIncrements')->willReturn(2.0);
72+
73+
// Set expectations
74+
$this->storeManager->expects($this->once())
75+
->method('getWebsite')
76+
->with(self::WEBSITE_ID)
77+
->willReturn($website);
78+
79+
$this->stockResolver->expects($this->once())
80+
->method('execute')
81+
->with(SalesChannelInterface::TYPE_WEBSITE, self::WEBSITE_CODE)
82+
->willReturn($stock);
83+
84+
$this->getStockItemConfiguration->expects($this->once())
85+
->method('execute')
86+
->with(self::TEST_SKU, self::STOCK_ID)
87+
->willReturn($stockItemConfiguration);
88+
89+
$expected = [
90+
'validate-item-quantity' => [
91+
'minAllowed' => 2,
92+
'maxAllowed' => 10,
93+
'qtyIncrements' => 2.0
94+
]
95+
];
96+
97+
$result = $this->quantityValidator->getData(self::TEST_SKU, self::WEBSITE_ID);
98+
$this->assertEquals($expected, $result);
99+
}
100+
}

app/code/Magento/CatalogInventory/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"php": "~8.2.0||~8.3.0||~8.4.0",
99
"magento/framework": "*",
1010
"magento/module-catalog": "*",
11+
"magento/module-inventory-configuration-api": "*",
12+
"magento/module-inventory-sales-api": "*",
1113
"magento/module-config": "*",
1214
"magento/module-customer": "*",
1315
"magento/module-eav": "*",
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2011 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
99
<module name="Magento_CatalogInventory" >
1010
<sequence>
1111
<module name="Magento_Catalog"/>
12+
<module name="Magento_InventoryConfigurationApi"/>
13+
<module name="Magento_InventorySalesApi"/>
1214
</sequence>
1315
</module>
1416
</config>

app/code/Magento/GroupedProduct/ViewModel/ValidateQuantity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ public function __construct(
2929
/**
3030
* To get the quantity validators
3131
*
32-
* @param int $productId
32+
* @param string $sku
3333
* @param int|null $websiteId
3434
*
3535
* @return string
3636
*/
37-
public function getQuantityValidators(int $productId, int|null $websiteId): string
37+
public function getQuantityValidators(string $sku, int|null $websiteId): string
3838
{
3939
return $this->serializer->serialize(
4040
array_merge(
4141
['validate-grouped-qty' => '#super-product-table'],
42-
$this->productQuantityValidator->getData($productId, $websiteId)
42+
$this->productQuantityValidator->getData($sku, $websiteId)
4343
)
4444
);
4545
}

app/code/Magento/GroupedProduct/view/frontend/templates/product/view/type/grouped.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
title="<?= $escaper->escapeHtmlAttr(__('Qty')) ?>"
5757
class="input-text qty"
5858
data-validate="<?= $escaper->escapeHtmlAttr($viewModel->getQuantityValidators(
59-
$_item->getId(),
59+
$_item->getSku(),
6060
$_item->getWebsiteId()
6161
)) ?>"
6262
data-no-validation-for-zero-qty="true"

0 commit comments

Comments
 (0)