Skip to content

Commit adb7c0b

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 e9bd3a2 commit adb7c0b

File tree

3 files changed

+133
-9
lines changed

3 files changed

+133
-9
lines changed

app/code/Magento/Catalog/Model/Product/CatalogCategoryAndProductResolverOnSingleStoreMode.php renamed to app/code/Magento/Catalog/Model/ResourceModel/CatalogCategoryAndProductResolverOnSingleStoreMode.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?php
22
/************************************************************************
3-
*
4-
* ADOBE CONFIDENTIAL
5-
* ___________________
63
*
74
* Copyright 2024 Adobe
85
* All Rights Reserved.
@@ -19,11 +16,10 @@
1916
*/
2017
declare(strict_types=1);
2118

22-
namespace Magento\Catalog\Model\Product;
19+
namespace Magento\Catalog\Model\ResourceModel;
2320

2421
use Exception;
2522
use Magento\Framework\App\ResourceConnection;
26-
use Magento\Framework\EntityManager\MetadataPool;
2723
use Magento\Framework\Exception\CouldNotSaveException;
2824
use Magento\Framework\Exception\LocalizedException;
2925
use Magento\Store\Model\Store;
@@ -34,11 +30,9 @@
3430
class CatalogCategoryAndProductResolverOnSingleStoreMode
3531
{
3632
/**
37-
* @param MetadataPool $metadataPool
3833
* @param ResourceConnection $resourceConnection
3934
*/
4035
public function __construct(
41-
private readonly MetadataPool $metadataPool,
4236
private readonly ResourceConnection $resourceConnection
4337
) {
4438
}

app/code/Magento/Catalog/Observer/MoveStoreLevelCatalogDataToWebsiteScopeOnSingleStoreMode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
use Magento\Catalog\Model\Indexer\Category\Product;
2222
use Magento\Catalog\Model\Indexer\Product\Category as ProductCategoryIndexer;
23-
use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
2423
use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceIndexProcessor;
25-
use Magento\Catalog\Model\Product\CatalogCategoryAndProductResolverOnSingleStoreMode as Resolver;
24+
use Magento\Catalog\Model\ResourceModel\CatalogCategoryAndProductResolverOnSingleStoreMode as Resolver;
25+
use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
2626
use Magento\Framework\App\Config\ScopeConfigInterface;
2727
use Magento\Framework\Event\Observer;
2828
use Magento\Framework\Event\ObserverInterface;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Catalog\Test\Unit\Model\ResourceModel;
20+
21+
use Exception;
22+
use Magento\Catalog\Model\ResourceModel\CatalogCategoryAndProductResolverOnSingleStoreMode as Resolver;
23+
use Magento\Framework\App\ResourceConnection;
24+
use Magento\Framework\DB\Adapter\AdapterInterface;
25+
use Magento\Framework\DB\Select;
26+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
27+
use PHPUnit\Framework\MockObject\MockObject;
28+
use PHPUnit\Framework\TestCase;
29+
30+
class CatalogCategoryAndProductResolverOnSingleStoreModeTest extends TestCase
31+
{
32+
/**
33+
* @var Resolver
34+
*/
35+
private $model;
36+
37+
/**
38+
* @var ResourceConnection|MockObject
39+
*/
40+
private $resourceConnectionMock;
41+
42+
protected function setUp(): void
43+
{
44+
$objectManager = new ObjectManager($this);
45+
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
46+
47+
$this->model = $objectManager->getObject(
48+
Resolver::class,
49+
[
50+
'resourceConnection' => $this->resourceConnectionMock
51+
]
52+
);
53+
}
54+
55+
/**
56+
* Test Migrate catalog category and product tables without exception
57+
*/
58+
public function testCheckMigrateCatalogCategoryAndProductTablesWithoutException(): void
59+
{
60+
$catalogProducts = [
61+
[
62+
'id' => 1,
63+
'name' => 'simple1',
64+
'category_id' => '1',
65+
'website_id' => '2'
66+
],
67+
[
68+
'id' => 2,
69+
'name' => 'simple2',
70+
'category_id' => '1',
71+
'website_id' => '2'
72+
],
73+
[
74+
'id' => 3,
75+
'name' => 'bundle1',
76+
'category_id' => '1',
77+
'website_id' => '2'
78+
]
79+
];
80+
$connection = $this->getConnection();
81+
$connection->expects($this->any())->method('fetchCol')->willReturn($catalogProducts);
82+
$connection->expects($this->any())->method('delete')->willReturnSelf();
83+
$connection->expects($this->any())->method('update')->willReturnSelf();
84+
$connection->expects($this->any())->method('commit')->willReturnSelf();
85+
86+
$this->model->migrateCatalogCategoryAndProductTables(1);
87+
}
88+
89+
/**
90+
* Test Migrate catalog category and product tables with exception
91+
*/
92+
public function testCheckMigrateCatalogCategoryAndProductTablesWithException(): void
93+
{
94+
$exceptionMessage = 'Exception message';
95+
$connection = $this->getConnection();
96+
$connection->expects($this->any())
97+
->method('fetchCol')
98+
->willThrowException(new Exception($exceptionMessage));
99+
$connection->expects($this->any())->method('rollBack')->willReturnSelf();
100+
$this->model->migrateCatalogCategoryAndProductTables(1);
101+
}
102+
103+
/**
104+
* Get connection
105+
*
106+
* @return MockObject
107+
*/
108+
private function getConnection(): MockObject
109+
{
110+
$connection = $this->getMockForAbstractClass(AdapterInterface::class);
111+
$this->resourceConnectionMock
112+
->expects($this->any())
113+
->method('getConnection')
114+
->willReturn($connection);
115+
$connection->expects($this->once())
116+
->method('beginTransaction')
117+
->willReturnSelf();
118+
$this->resourceConnectionMock
119+
->expects($this->any())
120+
->method('getTableName')
121+
->willReturnArgument(0);
122+
123+
$select = $this->createMock(Select::class);
124+
$select->expects($this->any())->method('from')->willReturnSelf();
125+
$select->expects($this->any())->method('where')->willReturnSelf();
126+
127+
$connection->expects($this->any())->method('select')->willReturn($select);
128+
return $connection;
129+
}
130+
}

0 commit comments

Comments
 (0)