Skip to content

Commit a9710ae

Browse files
author
Anna Bukatar
committed
Merge branch 'ACP2E-443' of https://github.com/magento-l3/magento2ce into PR-2022-02-22
2 parents 15cf009 + 70ff9fe commit a9710ae

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\ResourceModel;
8+
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Framework\App\ResourceConnection;
11+
12+
/**
13+
* Get product type ID by product ID.
14+
*
15+
*/
16+
class GetProductTypeById
17+
{
18+
/**
19+
* @var ResourceConnection
20+
*/
21+
private $resource;
22+
23+
/**
24+
* @param ResourceConnection $resource
25+
*/
26+
public function __construct(
27+
ResourceConnection $resource
28+
) {
29+
$this->resource = $resource;
30+
}
31+
32+
/**
33+
* Retrieve product type by its product ID
34+
*
35+
* @param int $productId
36+
* @return string
37+
*/
38+
public function execute(int $productId)
39+
{
40+
$connection = $this->resource->getConnection();
41+
$productTable = $this->resource->getTableName('catalog_product_entity');
42+
43+
$select = $connection->select()
44+
->from(
45+
$productTable,
46+
ProductInterface::TYPE_ID
47+
)->where('entity_id = ?', $productId);
48+
49+
$result = $connection->fetchOne($select);
50+
return $result ?: '';
51+
}
52+
}

app/code/Magento/ConfigurableProduct/Model/Inventory/ChangeParentStockStatus.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ private function processStockForParent(int $productId): void
106106
if ($this->isNeedToUpdateParent($parentStockItem, $childrenIsInStock)) {
107107
$parentStockItem->setIsInStock($childrenIsInStock);
108108
$parentStockItem->setStockStatusChangedAuto(1);
109+
$parentStockItem->setStockStatusChangedAutomaticallyFlag(true);
109110
$this->stockItemRepository->save($parentStockItem);
110111
}
111112
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Model\Plugin;
8+
9+
use Magento\Catalog\Model\ResourceModel\GetProductTypeById;
10+
use Magento\CatalogInventory\Model\ResourceModel\Stock\Item as ItemResourceModel;
11+
use Magento\Framework\Model\AbstractModel as StockItem;
12+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
13+
14+
class UpdateStockChangedAuto
15+
{
16+
/**
17+
* @var GetProductTypeById
18+
*/
19+
private $getProductTypeById;
20+
21+
/**
22+
* @param GetProductTypeById $getProductTypeById
23+
*/
24+
public function __construct(GetProductTypeById $getProductTypeById)
25+
{
26+
$this->getProductTypeById = $getProductTypeById;
27+
}
28+
29+
/**
30+
* Updates stock_status_changed_auto for configurable product
31+
*
32+
* @param ItemResourceModel $subject
33+
* @param StockItem $stockItem
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
35+
*/
36+
public function beforeSave(ItemResourceModel $subject, StockItem $stockItem): void
37+
{
38+
if (!$stockItem->getIsInStock() &&
39+
!$stockItem->hasStockStatusChangedAutomaticallyFlag() &&
40+
$this->getProductTypeById->execute($stockItem->getProductId()) == Configurable::TYPE_CODE
41+
) {
42+
$stockItem->setStockStatusChangedAuto(0);
43+
}
44+
}
45+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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\ConfigurableProduct\Test\Unit\Model\Plugin;
9+
10+
use Magento\CatalogInventory\Model\Stock;
11+
use Magento\ConfigurableProduct\Model\Plugin\UpdateStockChangedAuto;
12+
use Magento\Catalog\Model\ResourceModel\GetProductTypeById;
13+
use Magento\CatalogInventory\Model\ResourceModel\Stock\Item as ItemResourceModel;
14+
use Magento\Framework\Model\AbstractModel as StockItem;
15+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Unit test for Magento\ConfigurableProduct\Model\Plugin\UpdateStockChangedAuto class.
21+
*
22+
* @SuppressWarnings(PHPMD.LongVariable)
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24+
*/
25+
class UpdateStockChangedAutoTest extends TestCase
26+
{
27+
/**
28+
* @var MockObject
29+
*/
30+
private $getProductTypeByIdMock;
31+
32+
/**
33+
* @var UpdateStockChangedAuto
34+
*/
35+
private $plugin;
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
protected function setUp(): void
41+
{
42+
$this->getProductTypeByIdMock = $this->getMockBuilder(GetProductTypeById::class)
43+
->disableOriginalConstructor()
44+
->getMock();
45+
$this->plugin = new UpdateStockChangedAuto($this->getProductTypeByIdMock);
46+
}
47+
48+
/**
49+
* Verify before Stock Item save. Negative scenario
50+
*
51+
* @return void
52+
*/
53+
public function testBeforeSaveForInStock()
54+
{
55+
$itemResourceModel = $this->getMockBuilder(ItemResourceModel::class)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$stockItem = $this->getMockBuilder(StockItem::class)
59+
->disableOriginalConstructor()
60+
->setMethods(['getIsInStock', 'setStockStatusChangedAuto'])
61+
->getMock();
62+
$stockItem->expects(self::once())
63+
->method('getIsInStock')
64+
->willReturn(Stock::STOCK_IN_STOCK);
65+
$stockItem->expects(self::never())->method('setStockStatusChangedAuto');
66+
$this->plugin->beforeSave($itemResourceModel, $stockItem);
67+
}
68+
69+
/**
70+
* Verify before Stock Item save
71+
*
72+
* @return void
73+
*/
74+
public function testBeforeSaveForConfigurableInStock()
75+
{
76+
$productType = Configurable::TYPE_CODE;
77+
$productId = 1;
78+
$itemResourceModel = $this->getMockBuilder(ItemResourceModel::class)
79+
->disableOriginalConstructor()
80+
->getMock();
81+
$stockItem = $this->getMockBuilder(StockItem::class)
82+
->disableOriginalConstructor()
83+
->setMethods([
84+
'getIsInStock',
85+
'getProductId',
86+
'hasStockStatusChangedAutomaticallyFlag',
87+
'setStockStatusChangedAuto'
88+
])
89+
->getMock();
90+
$stockItem->expects(self::once())
91+
->method('getIsInStock')
92+
->willReturn(Stock::STOCK_OUT_OF_STOCK);
93+
$stockItem->expects(self::once())
94+
->method('hasStockStatusChangedAutomaticallyFlag')
95+
->willReturn(false);
96+
$stockItem->expects(self::once())
97+
->method('getProductId')
98+
->willReturn($productId);
99+
$this->getProductTypeByIdMock->expects(self::once())
100+
->method('execute')
101+
->with($productId)
102+
->willReturn($productType);
103+
$stockItem->expects(self::once())->method('setStockStatusChangedAuto')->with(0);
104+
105+
$this->plugin->beforeSave($itemResourceModel, $stockItem);
106+
}
107+
}

app/code/Magento/ConfigurableProduct/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,7 @@
280280
<argument name="serializer" xsi:type="object">Magento\Framework\Serialize\Serializer\Json</argument>
281281
</arguments>
282282
</type>
283+
<type name="Magento\CatalogInventory\Model\ResourceModel\Stock\Item">
284+
<plugin name="updateStockChangedAuto" type="Magento\ConfigurableProduct\Model\Plugin\UpdateStockChangedAuto" />
285+
</type>
283286
</config>

0 commit comments

Comments
 (0)