Skip to content

Commit 9ff8ee2

Browse files
ENGCOM-2627: #16273: [Backport] Fix bug in method getUrlInStore() of product model #16310
- Merge Pull Request #16310 from vasilii-b/magento2:#16273-product-geturlinstore-wrong-link_mage21 - Merged commits: 1. 2f8f469 2. ecfbe24 3. 7833f43 4. 7c33686 5. 726b6e9
2 parents 3e387ea + 726b6e9 commit 9ff8ee2

File tree

2 files changed

+91
-69
lines changed

2 files changed

+91
-69
lines changed

app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,36 @@ class RouteParamsResolverTest extends \PHPUnit_Framework_TestCase
2222
*/
2323
protected $queryParamsResolverMock;
2424

25+
/**
26+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
27+
*/
28+
protected $storeMock;
29+
2530
/**
2631
* @var \Magento\Store\Url\Plugin\RouteParamsResolver
2732
*/
2833
protected $model;
2934

35+
/**
36+
* @return void
37+
*/
3038
protected function setUp()
3139
{
32-
$this->scopeConfigMock = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface');
33-
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
34-
$this->queryParamsResolverMock = $this->getMock('Magento\Framework\Url\QueryParamsResolverInterface');
40+
$this->scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
41+
42+
$this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
43+
->setMethods(['getCode'])
44+
->disableOriginalConstructor()
45+
->getMock();
46+
$this->storeMock->expects($this->any())->method('getCode')->willReturn('custom_store');
47+
48+
$this->storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
49+
$this->storeManagerMock
50+
->expects($this->once())
51+
->method('getStore')
52+
->willReturn($this->storeMock);
53+
54+
$this->queryParamsResolverMock = $this->getMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
3555
$this->model = new \Magento\Store\Url\Plugin\RouteParamsResolver(
3656
$this->scopeConfigMock,
3757
$this->storeManagerMock,
@@ -40,11 +60,15 @@ protected function setUp()
4060
}
4161

4262
/**
43-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
63+
* @throws \Magento\Framework\Exception\NoSuchEntityException
64+
*
65+
* @return void
4466
*/
45-
public function testAroundSetRouteParamsScopeInParams()
67+
public function testBeforeSetRouteParamsScopeInParams()
4668
{
4769
$storeCode = 'custom_store';
70+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
71+
4872
$this->scopeConfigMock
4973
->expects($this->once())
5074
->method('getValue')
@@ -55,33 +79,33 @@ public function testAroundSetRouteParamsScopeInParams()
5579
)
5680
->will($this->returnValue(false));
5781
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
58-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
59-
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
60-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
82+
83+
/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
84+
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
6185
->setMethods(['setScope', 'getScope'])
6286
->disableOriginalConstructor()
6387
->getMock();
64-
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
65-
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
88+
$routeResolverMock->expects($this->once())->method('setScope')->with($storeCode);
89+
$routeResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
6690

67-
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
91+
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
6892

69-
$this->model->aroundSetRouteParams(
70-
$routeParamsResolverMock,
71-
function ($data, $unsetOldParams) {
72-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
73-
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
74-
},
93+
$this->model->beforeSetRouteParams(
94+
$routeResolverMock,
7595
$data
7696
);
7797
}
7898

7999
/**
80-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
100+
* @throws \Magento\Framework\Exception\NoSuchEntityException
101+
*
102+
* @return void
81103
*/
82-
public function testAroundSetRouteParamsScopeUseStoreInUrl()
104+
public function testBeforeSetRouteParamsScopeUseStoreInUrl()
83105
{
84106
$storeCode = 'custom_store';
107+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
108+
85109
$this->scopeConfigMock
86110
->expects($this->once())
87111
->method('getValue')
@@ -91,34 +115,35 @@ public function testAroundSetRouteParamsScopeUseStoreInUrl()
91115
$storeCode
92116
)
93117
->will($this->returnValue(true));
118+
94119
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
95-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
96-
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
97-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
120+
121+
/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
122+
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
98123
->setMethods(['setScope', 'getScope'])
99124
->disableOriginalConstructor()
100125
->getMock();
101-
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
102-
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
126+
$routeResolverMock->expects($this->once())->method('setScope')->with($storeCode);
127+
$routeResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
103128

104-
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
129+
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
105130

106-
$this->model->aroundSetRouteParams(
107-
$routeParamsResolverMock,
108-
function ($data, $unsetOldParams) {
109-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
110-
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
111-
},
131+
$this->model->beforeSetRouteParams(
132+
$routeResolverMock,
112133
$data
113134
);
114135
}
115136

116137
/**
117-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
138+
* @throws \Magento\Framework\Exception\NoSuchEntityException
139+
*
140+
* @return void
118141
*/
119-
public function testAroundSetRouteParamsSingleStore()
142+
public function testBeforeSetRouteParamsSingleStore()
120143
{
121144
$storeCode = 'custom_store';
145+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
146+
122147
$this->scopeConfigMock
123148
->expects($this->once())
124149
->method('getValue')
@@ -129,33 +154,33 @@ public function testAroundSetRouteParamsSingleStore()
129154
)
130155
->will($this->returnValue(false));
131156
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(true);
132-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
133-
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
134-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
157+
158+
/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
159+
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
135160
->setMethods(['setScope', 'getScope'])
136161
->disableOriginalConstructor()
137162
->getMock();
138-
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
139-
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
163+
$routeResolverMock->expects($this->once())->method('setScope')->with($storeCode);
164+
$routeResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
140165

141166
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
142167

143-
$this->model->aroundSetRouteParams(
144-
$routeParamsResolverMock,
145-
function ($data, $unsetOldParams) {
146-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
147-
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
148-
},
168+
$this->model->beforeSetRouteParams(
169+
$routeResolverMock,
149170
$data
150171
);
151172
}
152173

153174
/**
154-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
175+
* @throws \Magento\Framework\Exception\NoSuchEntityException
176+
*
177+
* @return void
155178
*/
156-
public function testAroundSetRouteParamsNoScopeInParams()
179+
public function testBeforeSetRouteParamsNoScopeInParams()
157180
{
158181
$storeCode = 'custom_store';
182+
$data = ['_scope_to_url' => true];
183+
159184
$this->scopeConfigMock
160185
->expects($this->once())
161186
->method('getValue')
@@ -164,32 +189,22 @@ public function testAroundSetRouteParamsNoScopeInParams()
164189
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
165190
$storeCode
166191
)
167-
->will($this->returnValue(false));
192+
->will($this->returnValue(true));
193+
168194
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
169-
/** @var \PHPUnit_Framework_MockObject_MockObject| $routeParamsResolverMock */
170-
$storeMock = $this->getMockBuilder('Magento\Store\Model\Store')
171-
->setMethods(['getCode'])
172-
->disableOriginalConstructor()
173-
->getMock();
174-
$storeMock->expects($this->any())->method('getCode')->willReturn($storeCode);
175-
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
176195

177-
$data = ['_scope_to_url' => true];
178-
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
179-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
196+
/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
197+
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
180198
->setMethods(['setScope', 'getScope'])
181199
->disableOriginalConstructor()
182200
->getMock();
183-
$routeParamsResolverMock->expects($this->never())->method('setScope');
184-
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn(false);
201+
$routeResolverMock->expects($this->never())->method('setScope');
202+
$routeResolverMock->expects($this->once())->method('getScope')->willReturn(false);
185203

186204
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
187205

188-
$this->model->aroundSetRouteParams(
189-
$routeParamsResolverMock,
190-
function ($data, $unsetOldParams) {
191-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
192-
},
206+
$this->model->beforeSetRouteParams(
207+
$routeResolverMock,
193208
$data
194209
);
195210
}

app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ public function __construct(
5252
* @param callable $proceed
5353
* @param array $data
5454
* @param bool $unsetOldParams
55+
* @throws \Magento\Framework\Exception\NoSuchEntityException
56+
*
5557
* @return \Magento\Framework\Url\RouteParamsResolver
5658
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5759
*/
58-
public function aroundSetRouteParams(
60+
public function beforeSetRouteParams(
5961
\Magento\Framework\Url\RouteParamsResolver $subject,
60-
\Closure $proceed,
6162
array $data,
6263
$unsetOldParams = true
6364
) {
@@ -66,18 +67,24 @@ public function aroundSetRouteParams(
6667
unset($data['_scope']);
6768
}
6869
if (isset($data['_scope_to_url']) && (bool)$data['_scope_to_url'] === true) {
69-
$storeCode = $subject->getScope() ?: $this->storeManager->getStore()->getCode();
70+
/** @var Store $currentScope */
71+
$currentScope = $subject->getScope();
72+
$storeCode = $currentScope && $currentScope instanceof Store ?
73+
$currentScope->getCode() :
74+
$this->storeManager->getStore()->getCode();
75+
7076
$useStoreInUrl = $this->scopeConfig->getValue(
7177
Store::XML_PATH_STORE_IN_URL,
7278
StoreScopeInterface::SCOPE_STORE,
7379
$storeCode
7480
);
75-
if (!$useStoreInUrl && !$this->storeManager->hasSingleStore()) {
81+
82+
if ($useStoreInUrl && !$this->storeManager->hasSingleStore()) {
7683
$this->queryParamsResolver->setQueryParam('___store', $storeCode);
7784
}
7885
}
7986
unset($data['_scope_to_url']);
8087

81-
return $proceed($data, $unsetOldParams);
88+
return [$data, $unsetOldParams];
8289
}
8390
}

0 commit comments

Comments
 (0)