Skip to content

Commit f70273b

Browse files
committed
ACP2E-2843: Products on the frontend use store specific data when Single-Store Mode is enabled
- solution with test coverage
1 parent e0e5d89 commit f70273b

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

app/code/Magento/Catalog/Model/ResourceModel/CatalogCategoryAndProductResolverOnSingleStoreMode.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
namespace Magento\Catalog\Model\ResourceModel;
2020

2121
use Exception;
22+
use Magento\Catalog\Api\Data\ProductInterface;
23+
use Magento\Eav\Api\Data\AttributeInterface;
2224
use Magento\Framework\App\ResourceConnection;
25+
use Magento\Framework\EntityManager\MetadataPool;
2326
use Magento\Framework\Exception\CouldNotSaveException;
2427
use Magento\Framework\Exception\LocalizedException;
2528
use Magento\Store\Model\Store;
@@ -31,9 +34,11 @@ class CatalogCategoryAndProductResolverOnSingleStoreMode
3134
{
3235
/**
3336
* @param ResourceConnection $resourceConnection
37+
* @param MetadataPool $metadataPool
3438
*/
3539
public function __construct(
36-
private readonly ResourceConnection $resourceConnection
40+
private readonly ResourceConnection $resourceConnection,
41+
private readonly MetadataPool $metadataPool
3742
) {
3843
}
3944

@@ -47,6 +52,8 @@ public function __construct(
4752
*/
4853
private function process(int $storeId, string $table): void
4954
{
55+
$productMetadata = $this->metadataPool->getMetadata(ProductInterface::class);
56+
$productLinkField = $productMetadata->getLinkField();
5057
$catalogProductTable = $this->resourceConnection->getTableName($table);
5158

5259
$catalogProducts = $this->getCatalogProducts($table, $storeId);
@@ -56,8 +63,8 @@ private function process(int $storeId, string $table): void
5663
try {
5764
if ($catalogProducts) {
5865
foreach ($catalogProducts as $catalogProduct) {
59-
$rowIds[] = $catalogProduct['row_id'];
60-
$attributeIds[] = $catalogProduct['attribute_id'];
66+
$rowIds[] = $catalogProduct[$productLinkField];
67+
$attributeIds[] = $catalogProduct[AttributeInterface::ATTRIBUTE_ID];
6168
$valueIds[] = $catalogProduct['value_id'];
6269
}
6370
$this->massDelete($catalogProductTable, $attributeIds, $rowIds);
@@ -107,12 +114,12 @@ public function migrateCatalogCategoryAndProductTables(int $storeId): void
107114
/**
108115
* Delete default store related products
109116
*
110-
* @param $catalogProductTable
117+
* @param string $catalogProductTable
111118
* @param array $attributeIds
112119
* @param array $rowIds
113120
* @return void
114121
*/
115-
private function massDelete($catalogProductTable, array $attributeIds, array $rowIds): void
122+
private function massDelete(string $catalogProductTable, array $attributeIds, array $rowIds): void
116123
{
117124
$connection = $this->resourceConnection->getConnection();
118125

@@ -129,11 +136,11 @@ private function massDelete($catalogProductTable, array $attributeIds, array $ro
129136
/**
130137
* Update default store related products
131138
*
132-
* @param $catalogProductTable
139+
* @param string $catalogProductTable
133140
* @param array $valueIds
134141
* @return void
135142
*/
136-
private function massUpdate($catalogProductTable, array $valueIds): void
143+
private function massUpdate(string $catalogProductTable, array $valueIds): void
137144
{
138145
$connection = $this->resourceConnection->getConnection();
139146

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CatalogCategoryAndProductResolverOnSingleStoreModeTest.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
use Magento\Framework\App\ResourceConnection;
2424
use Magento\Framework\DB\Adapter\AdapterInterface;
2525
use Magento\Framework\DB\Select;
26-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
26+
use Magento\Framework\EntityManager\EntityMetadataInterface;
27+
use Magento\Framework\EntityManager\MetadataPool;
2728
use PHPUnit\Framework\MockObject\MockObject;
2829
use PHPUnit\Framework\TestCase;
2930

@@ -39,17 +40,21 @@ class CatalogCategoryAndProductResolverOnSingleStoreModeTest extends TestCase
3940
*/
4041
private $resourceConnectionMock;
4142

43+
/**
44+
* @var MetadataPool|MockObject
45+
*/
46+
private $metadataPoolMock;
47+
4248
protected function setUp(): void
4349
{
44-
$objectManager = new ObjectManager($this);
4550
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
46-
47-
$this->model = $objectManager->getObject(
48-
Resolver::class,
49-
[
50-
'resourceConnection' => $this->resourceConnectionMock
51-
]
52-
);
51+
$this->metadataPoolMock = $this->getMockBuilder(MetadataPool::class)
52+
->disableOriginalConstructor()
53+
->getMock();
54+
$this->model = new Resolver(
55+
$this->resourceConnectionMock,
56+
$this->metadataPoolMock
57+
);
5358
}
5459

5560
/**
@@ -78,7 +83,7 @@ public function testCheckMigrateCatalogCategoryAndProductTablesWithoutException(
7883
]
7984
];
8085
$connection = $this->getConnection();
81-
$connection->expects($this->any())->method('fetchCol')->willReturn($catalogProducts);
86+
$connection->expects($this->any())->method('fetchAll')->willReturn($catalogProducts);
8287
$connection->expects($this->any())->method('delete')->willReturnSelf();
8388
$connection->expects($this->any())->method('update')->willReturnSelf();
8489
$connection->expects($this->any())->method('commit')->willReturnSelf();
@@ -94,7 +99,7 @@ public function testCheckMigrateCatalogCategoryAndProductTablesWithException():
9499
$exceptionMessage = 'Exception message';
95100
$connection = $this->getConnection();
96101
$connection->expects($this->any())
97-
->method('fetchCol')
102+
->method('fetchAll')
98103
->willThrowException(new Exception($exceptionMessage));
99104
$connection->expects($this->any())->method('rollBack')->willReturnSelf();
100105
$this->model->migrateCatalogCategoryAndProductTables(1);
@@ -108,6 +113,14 @@ public function testCheckMigrateCatalogCategoryAndProductTablesWithException():
108113
private function getConnection(): MockObject
109114
{
110115
$connection = $this->getMockForAbstractClass(AdapterInterface::class);
116+
$metadata = $this->getMockForAbstractClass(EntityMetadataInterface::class);
117+
$this->metadataPoolMock->expects($this->any())
118+
->method('getMetadata')
119+
->willReturn($metadata);
120+
$metadata
121+
->expects($this->any())
122+
->method('getLinkField')
123+
->willReturn('row_id');
111124
$this->resourceConnectionMock
112125
->expects($this->any())
113126
->method('getConnection')

0 commit comments

Comments
 (0)