Skip to content

Commit a998b2f

Browse files
authored
ENGCOM-6869: issue/26384 Fix store switcher when using different base url on stores #26548
2 parents 5eb2a6a + 0213f14 commit a998b2f

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

app/code/Magento/Store/Controller/Store/Redirect.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
use Magento\Framework\App\Action\Context;
1212
use Magento\Framework\App\Action\HttpGetActionInterface;
1313
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Framework\App\ActionInterface;
15+
use Magento\Framework\App\ObjectManager;
1416
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\Session\Generic;
18+
use Magento\Framework\Session\SidResolverInterface;
1519
use Magento\Store\Api\StoreRepositoryInterface;
1620
use Magento\Store\Api\StoreResolverInterface;
1721
use Magento\Store\Model\Store;
22+
use Magento\Store\Model\StoreManagerInterface;
1823
use Magento\Store\Model\StoreResolver;
1924
use Magento\Store\Model\StoreSwitcher\HashGenerator;
2025

@@ -38,27 +43,35 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI
3843
*/
3944
private $hashGenerator;
4045

46+
/**
47+
* @var StoreManagerInterface
48+
*/
49+
private $storeManager;
50+
4151
/**
4252
* @param Context $context
4353
* @param StoreRepositoryInterface $storeRepository
4454
* @param StoreResolverInterface $storeResolver
45-
* @param \Magento\Framework\Session\Generic $session
46-
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
55+
* @param Generic $session
56+
* @param SidResolverInterface $sidResolver
4757
* @param HashGenerator $hashGenerator
58+
* @param StoreManagerInterface $storeManager
4859
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4960
*/
5061
public function __construct(
5162
Context $context,
5263
StoreRepositoryInterface $storeRepository,
5364
StoreResolverInterface $storeResolver,
54-
\Magento\Framework\Session\Generic $session,
55-
\Magento\Framework\Session\SidResolverInterface $sidResolver,
56-
HashGenerator $hashGenerator
65+
Generic $session,
66+
SidResolverInterface $sidResolver,
67+
HashGenerator $hashGenerator,
68+
StoreManagerInterface $storeManager = null
5769
) {
5870
parent::__construct($context);
5971
$this->storeRepository = $storeRepository;
6072
$this->storeResolver = $storeResolver;
6173
$this->hashGenerator = $hashGenerator;
74+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
6275
}
6376

6477
/**
@@ -81,6 +94,9 @@ public function execute()
8194
try {
8295
/** @var Store $fromStore */
8396
$fromStore = $this->storeRepository->get($fromStoreCode);
97+
/** @var Store $targetStore */
98+
$targetStore = $this->storeRepository->get($targetStoreCode);
99+
$this->storeManager->setCurrentStore($targetStore);
84100
} catch (NoSuchEntityException $e) {
85101
$error = __("Requested store is not found ({$fromStoreCode})");
86102
}
@@ -89,12 +105,11 @@ public function execute()
89105
$this->messageManager->addErrorMessage($error);
90106
$this->_redirect->redirect($this->_response, $currentStore->getBaseUrl());
91107
} else {
92-
$encodedUrl = $this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED);
93-
108+
$encodedUrl = $this->_request->getParam(ActionInterface::PARAM_NAME_URL_ENCODED);
94109
$query = [
95110
'___from_store' => $fromStore->getCode(),
96111
StoreResolverInterface::PARAM_NAME => $targetStoreCode,
97-
\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED => $encodedUrl,
112+
ActionInterface::PARAM_NAME_URL_ENCODED => $encodedUrl,
98113
];
99114

100115
$customerHash = $this->hashGenerator->generateHash($fromStore);
@@ -104,6 +119,7 @@ public function execute()
104119
'_nosid' => true,
105120
'_query' => $query
106121
];
122+
107123
$this->_redirect->redirect($this->_response, 'stores/store/switch', $arguments);
108124
}
109125

app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Store\Api\StoreResolverInterface;
2121
use Magento\Store\Controller\Store\Redirect;
2222
use Magento\Store\Model\Store;
23+
use Magento\Store\Model\StoreManagerInterface;
2324
use Magento\Store\Model\StoreResolver;
2425
use Magento\Store\Model\StoreSwitcher\HashGenerator;
2526
use PHPUnit\Framework\MockObject\MockObject;
@@ -31,8 +32,20 @@
3132
*/
3233
class RedirectTest extends TestCase
3334
{
34-
private const DEFAULT_STORE_VIEW_CODE = 'default';
35-
private const STORE_CODE = 'sv1';
35+
/**
36+
* Stub for default store view code
37+
*/
38+
private const STUB_DEFAULT_STORE_VIEW_CODE = 'default';
39+
40+
/**
41+
* Stub for default store code
42+
*/
43+
private const STUB_STORE_CODE = 'sv1';
44+
45+
/**
46+
* @var StoreManagerInterface|MockObject
47+
*/
48+
private $storeManagerMock;
3649

3750
/**
3851
* @var StoreRepositoryInterface|MockObject
@@ -67,7 +80,12 @@ class RedirectTest extends TestCase
6780
/**
6881
* @var Store|MockObject
6982
*/
70-
private $formStoreMock;
83+
private $fromStoreMock;
84+
85+
/**
86+
* @var Store|MockObject
87+
*/
88+
private $targetStoreMock;
7189

7290
/**
7391
* @var Store|MockObject
@@ -87,13 +105,14 @@ class RedirectTest extends TestCase
87105
/**
88106
* @var Redirect
89107
*/
90-
private $redirectController;
108+
private $model;
91109

92110
/**
93111
* @inheritDoc
94112
*/
95113
protected function setUp()
96114
{
115+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
97116
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
98117
->disableOriginalConstructor()
99118
->setMethods(['getParam'])
@@ -117,7 +136,11 @@ protected function setUp()
117136
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
118137
->disableOriginalConstructor()
119138
->getMockForAbstractClass();
120-
$this->formStoreMock = $this->getMockBuilder(Store::class)
139+
$this->fromStoreMock = $this->getMockBuilder(Store::class)
140+
->disableOriginalConstructor()
141+
->setMethods(['getCode'])
142+
->getMockForAbstractClass();
143+
$this->targetStoreMock = $this->getMockBuilder(Store::class)
121144
->disableOriginalConstructor()
122145
->setMethods(['getCode'])
123146
->getMockForAbstractClass();
@@ -150,9 +173,10 @@ protected function setUp()
150173
'messageManager' => $this->messageManagerMock,
151174
]
152175
);
153-
$this->redirectController = $objectManager->getObject(
176+
$this->model = $objectManager->getObject(
154177
Redirect::class,
155178
[
179+
'storeManager' => $this->storeManagerMock,
156180
'storeRepository' => $this->storeRepositoryMock,
157181
'storeResolver' => $this->storeResolverMock,
158182
'sidResolver' => $this->sidResolverMock,
@@ -186,19 +210,25 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
186210
$defaultStoreViewCode
187211
);
188212
$this->storeRepositoryMock
189-
->expects($this->once())
213+
->expects($this->exactly(2))
190214
->method('get')
191-
->with($defaultStoreViewCode)
192-
->willReturn($this->formStoreMock);
193-
$this->formStoreMock
215+
->willReturnMap([
216+
[$defaultStoreViewCode, $this->fromStoreMock],
217+
[$storeCode, $this->targetStoreMock],
218+
]);
219+
$this->fromStoreMock
194220
->expects($this->once())
195221
->method('getCode')
196222
->willReturn($defaultStoreViewCode);
197223
$this->hashGeneratorMock
198224
->expects($this->once())
199225
->method('generateHash')
200-
->with($this->formStoreMock)
226+
->with($this->fromStoreMock)
201227
->willReturn([]);
228+
$this->storeManagerMock
229+
->expects($this->once())
230+
->method('setCurrentStore')
231+
->with($this->targetStoreMock);
202232
$this->redirectMock
203233
->expects($this->once())
204234
->method('redirect')
@@ -214,7 +244,7 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
214244
]
215245
);
216246

217-
$this->assertEquals(null, $this->redirectController->execute());
247+
$this->assertEquals(null, $this->model->execute());
218248
}
219249

220250
/**
@@ -257,7 +287,7 @@ public function testRedirectWithThrowsException(string $defaultStoreViewCode, st
257287
->with($this->responseMock, $this->currentStoreMock)
258288
->willReturnSelf();
259289

260-
$this->assertEquals(null, $this->redirectController->execute());
290+
$this->assertEquals(null, $this->model->execute());
261291
}
262292

263293
/**
@@ -281,7 +311,7 @@ public function testRedirectTargetIsNull(): void
281311
->expects($this->never())
282312
->method('get');
283313

284-
$this->assertEquals($this->responseMock, $this->redirectController->execute());
314+
$this->assertEquals($this->responseMock, $this->model->execute());
285315
}
286316

287317
/**
@@ -292,7 +322,7 @@ public function testRedirectTargetIsNull(): void
292322
public function getConfigDataProvider(): array
293323
{
294324
return [
295-
[self::DEFAULT_STORE_VIEW_CODE, self::STORE_CODE]
325+
[self::STUB_DEFAULT_STORE_VIEW_CODE, self::STUB_STORE_CODE]
296326
];
297327
}
298328
}

0 commit comments

Comments
 (0)