Skip to content

Commit fe706c8

Browse files
author
Stanislav Idolov
authored
ENGCOM-2606: [Forwardport] #16273: Fix bug in method getUrlInStore() of product model #17261
2 parents 51f40fb + 1bffc91 commit fe706c8

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ 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
*/
@@ -30,7 +35,19 @@ class RouteParamsResolverTest extends \PHPUnit\Framework\TestCase
3035
protected function setUp()
3136
{
3237
$this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
38+
39+
$this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
40+
->setMethods(['getCode'])
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$this->storeMock->expects($this->any())->method('getCode')->willReturn('custom_store');
44+
3345
$this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
46+
$this->storeManagerMock
47+
->expects($this->once())
48+
->method('getStore')
49+
->willReturn($this->storeMock);
50+
3451
$this->queryParamsResolverMock = $this->createMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
3552
$this->model = new \Magento\Store\Url\Plugin\RouteParamsResolver(
3653
$this->scopeConfigMock,
@@ -42,6 +59,8 @@ protected function setUp()
4259
public function testBeforeSetRouteParamsScopeInParams()
4360
{
4461
$storeCode = 'custom_store';
62+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
63+
4564
$this->scopeConfigMock
4665
->expects($this->once())
4766
->method('getValue')
@@ -52,7 +71,7 @@ public function testBeforeSetRouteParamsScopeInParams()
5271
)
5372
->will($this->returnValue(false));
5473
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
55-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
74+
5675
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
5776
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
5877
->setMethods(['setScope', 'getScope'])
@@ -61,7 +80,7 @@ public function testBeforeSetRouteParamsScopeInParams()
6180
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
6281
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
6382

64-
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
83+
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
6584

6685
$this->model->beforeSetRouteParams(
6786
$routeParamsResolverMock,
@@ -72,6 +91,8 @@ public function testBeforeSetRouteParamsScopeInParams()
7291
public function testBeforeSetRouteParamsScopeUseStoreInUrl()
7392
{
7493
$storeCode = 'custom_store';
94+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
95+
7596
$this->scopeConfigMock
7697
->expects($this->once())
7798
->method('getValue')
@@ -81,8 +102,9 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl()
81102
$storeCode
82103
)
83104
->will($this->returnValue(true));
105+
84106
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
85-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
107+
86108
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
87109
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
88110
->setMethods(['setScope', 'getScope'])
@@ -91,7 +113,7 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl()
91113
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
92114
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
93115

94-
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
116+
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
95117

96118
$this->model->beforeSetRouteParams(
97119
$routeParamsResolverMock,
@@ -102,6 +124,8 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl()
102124
public function testBeforeSetRouteParamsSingleStore()
103125
{
104126
$storeCode = 'custom_store';
127+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
128+
105129
$this->scopeConfigMock
106130
->expects($this->once())
107131
->method('getValue')
@@ -112,7 +136,7 @@ public function testBeforeSetRouteParamsSingleStore()
112136
)
113137
->will($this->returnValue(false));
114138
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(true);
115-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
139+
116140
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
117141
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
118142
->setMethods(['setScope', 'getScope'])
@@ -132,6 +156,8 @@ public function testBeforeSetRouteParamsSingleStore()
132156
public function testBeforeSetRouteParamsNoScopeInParams()
133157
{
134158
$storeCode = 'custom_store';
159+
$data = ['_scope_to_url' => true];
160+
135161
$this->scopeConfigMock
136162
->expects($this->once())
137163
->method('getValue')
@@ -140,17 +166,10 @@ public function testBeforeSetRouteParamsNoScopeInParams()
140166
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
141167
$storeCode
142168
)
143-
->will($this->returnValue(false));
169+
->will($this->returnValue(true));
170+
144171
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
145-
/** @var \PHPUnit_Framework_MockObject_MockObject| $routeParamsResolverMock */
146-
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
147-
->setMethods(['getCode'])
148-
->disableOriginalConstructor()
149-
->getMock();
150-
$storeMock->expects($this->any())->method('getCode')->willReturn($storeCode);
151-
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
152172

153-
$data = ['_scope_to_url' => true];
154173
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
155174
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
156175
->setMethods(['setScope', 'getScope'])

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Store\Url\Plugin;
77

88
use \Magento\Store\Model\Store;
9+
use \Magento\Store\Api\Data\StoreInterface;
910
use \Magento\Store\Model\ScopeInterface as StoreScopeInterface;
1011

1112
/**
@@ -51,6 +52,8 @@ public function __construct(
5152
* @param \Magento\Framework\Url\RouteParamsResolver $subject
5253
* @param array $data
5354
* @param bool $unsetOldParams
55+
* @throws \Magento\Framework\Exception\NoSuchEntityException
56+
*
5457
* @return array
5558
*/
5659
public function beforeSetRouteParams(
@@ -63,13 +66,19 @@ public function beforeSetRouteParams(
6366
unset($data['_scope']);
6467
}
6568
if (isset($data['_scope_to_url']) && (bool)$data['_scope_to_url'] === true) {
66-
$storeCode = $subject->getScope() ?: $this->storeManager->getStore()->getCode();
69+
/** @var StoreInterface $currentScope */
70+
$currentScope = $subject->getScope();
71+
$storeCode = $currentScope && $currentScope instanceof StoreInterface ?
72+
$currentScope->getCode() :
73+
$this->storeManager->getStore()->getCode();
74+
6775
$useStoreInUrl = $this->scopeConfig->getValue(
6876
Store::XML_PATH_STORE_IN_URL,
6977
StoreScopeInterface::SCOPE_STORE,
7078
$storeCode
7179
);
72-
if (!$useStoreInUrl && !$this->storeManager->hasSingleStore()) {
80+
81+
if ($useStoreInUrl && !$this->storeManager->hasSingleStore()) {
7382
$this->queryParamsResolver->setQueryParam('___store', $storeCode);
7483
}
7584
}

0 commit comments

Comments
 (0)