Skip to content

Commit 730d61d

Browse files
authored
Merge pull request #6477 from magento-tsg/MC-39985
[Arrows] MC-39985: Request error when displaying content on CMS page of second website
2 parents a826722 + de173a8 commit 730d61d

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

app/code/Magento/Store/Model/StoreSwitcher/HashProcessor.php

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,39 +96,41 @@ public function __construct(
9696
*/
9797
public function switch(StoreInterface $fromStore, StoreInterface $targetStore, string $redirectUrl): string
9898
{
99-
$timestamp = (int) $this->request->getParam('time_stamp');
100-
$signature = (string) $this->request->getParam('signature');
101-
$data = (string) $this->request->getParam('data');
102-
$context = $this->contextFactory->create(
103-
[
104-
'fromStore' => $fromStore,
105-
'targetStore' => $targetStore,
106-
'redirectUrl' => $redirectUrl
107-
]
108-
);
109-
$redirectDataObject = $this->dataFactory->create(
110-
[
111-
'signature' => $signature,
112-
'timestamp' => $timestamp,
113-
'data' => $data
114-
]
115-
);
99+
if ($this->request->getParam('data') !== null) {
100+
$timestamp = (int) $this->request->getParam('time_stamp');
101+
$signature = (string) $this->request->getParam('signature');
102+
$data = (string) $this->request->getParam('data');
103+
$context = $this->contextFactory->create(
104+
[
105+
'fromStore' => $fromStore,
106+
'targetStore' => $targetStore,
107+
'redirectUrl' => $redirectUrl
108+
]
109+
);
110+
$redirectDataObject = $this->dataFactory->create(
111+
[
112+
'signature' => $signature,
113+
'timestamp' => $timestamp,
114+
'data' => $data
115+
]
116+
);
116117

117-
try {
118-
if ($redirectUrl && $this->dataValidator->validate($context, $redirectDataObject)) {
119-
$this->postprocessor->process($context, $this->dataSerializer->unserialize($data));
120-
} else {
121-
throw new LocalizedException(
122-
__('The requested store cannot be found. Please check the request and try again.')
118+
try {
119+
if ($redirectUrl && $this->dataValidator->validate($context, $redirectDataObject)) {
120+
$this->postprocessor->process($context, $this->dataSerializer->unserialize($data));
121+
} else {
122+
throw new LocalizedException(
123+
__('The requested store cannot be found. Please check the request and try again.')
124+
);
125+
}
126+
} catch (LocalizedException $exception) {
127+
$this->messageManager->addErrorMessage($exception->getMessage());
128+
} catch (\Throwable $exception) {
129+
$this->logger->error($exception);
130+
$this->messageManager->addErrorMessage(
131+
__('Something went wrong.')
123132
);
124133
}
125-
} catch (LocalizedException $exception) {
126-
$this->messageManager->addErrorMessage($exception->getMessage());
127-
} catch (\Throwable $exception) {
128-
$this->logger->error($exception);
129-
$this->messageManager->addErrorMessage(
130-
__('Something went wrong.')
131-
);
132134
}
133135

134136
return $redirectUrl;

app/code/Magento/Store/Test/Unit/Model/StoreSwitcher/HashProcessorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,22 @@ function ($arg) {
128128
$this->postprocessor->expects($this->once())
129129
->method('process')
130130
->with($this->isInstanceOf(ContextInterface::class), ['customer_id' => 1]);
131+
$this->messageManager->expects($this->never())
132+
->method('addErrorMessage');
131133
$this->assertEquals($redirectUrl, $this->model->switch($this->store1, $this->store2, $redirectUrl));
132134
}
133135

134136
public function testShouldNotProcessIfDataValidationFailed(): void
135137
{
136138
$redirectUrl = '/category-1/category-1.1.html';
139+
$this->request->method('getParam')
140+
->willReturnMap(
141+
[
142+
['time_stamp', null, time() - 1],
143+
['data', null, '{"customer_id":1}'],
144+
['signature', null, 'randomstring'],
145+
]
146+
);
137147
$this->dataValidator->method('validate')
138148
->willReturn(false);
139149
$this->postprocessor->expects($this->never())
@@ -148,6 +158,14 @@ public function testShouldNotProcessIfDataValidationFailed(): void
148158
public function testShouldNotProcessIfDataUnserializationFailed(): void
149159
{
150160
$redirectUrl = '/category-1/category-1.1.html';
161+
$this->request->method('getParam')
162+
->willReturnMap(
163+
[
164+
['time_stamp', null, time() - 1],
165+
['data', null, '{"customer_id":1}'],
166+
['signature', null, 'randomstring'],
167+
]
168+
);
151169
$this->dataValidator->method('validate')
152170
->willReturn(true);
153171
$this->dataSerializer->method('unserialize')
@@ -160,4 +178,16 @@ public function testShouldNotProcessIfDataUnserializationFailed(): void
160178

161179
$this->assertEquals($redirectUrl, $this->model->switch($this->store1, $this->store2, $redirectUrl));
162180
}
181+
182+
public function testShouldNotProcessIfDataIsNotPresentInTheRequest(): void
183+
{
184+
$redirectUrl = '/category-1/category-1.1.html';
185+
$this->dataValidator->expects($this->never())
186+
->method('validate');
187+
$this->postprocessor->expects($this->never())
188+
->method('process');
189+
$this->messageManager->expects($this->never())
190+
->method('addErrorMessage');
191+
$this->assertEquals($redirectUrl, $this->model->switch($this->store1, $this->store2, $redirectUrl));
192+
}
163193
}

0 commit comments

Comments
 (0)