Skip to content

Commit 12e9244

Browse files
authored
ENGCOM-6653: Extend exception messages #25994
2 parents 7862cbc + 35d873f commit 12e9244

File tree

4 files changed

+446
-2
lines changed

4 files changed

+446
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function execute()
8282
/** @var Store $fromStore */
8383
$fromStore = $this->storeRepository->get($fromStoreCode);
8484
} catch (NoSuchEntityException $e) {
85-
$error = __('Requested store is not found');
85+
$error = __("Requested store is not found ({$fromStoreCode})");
8686
}
8787

8888
if ($error !== null) {
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Store\Test\Unit\Controller\Store;
9+
10+
use Magento\Framework\App\Action\Context;
11+
use Magento\Framework\App\ActionInterface;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\App\Response\RedirectInterface;
14+
use Magento\Framework\App\ResponseInterface;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\Framework\Message\ManagerInterface;
17+
use Magento\Framework\Session\SidResolverInterface;
18+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
19+
use Magento\Store\Api\StoreRepositoryInterface;
20+
use Magento\Store\Api\StoreResolverInterface;
21+
use Magento\Store\Controller\Store\Redirect;
22+
use Magento\Store\Model\Store;
23+
use Magento\Store\Model\StoreResolver;
24+
use Magento\Store\Model\StoreSwitcher\HashGenerator;
25+
use PHPUnit\Framework\MockObject\MockObject;
26+
use PHPUnit\Framework\TestCase;
27+
28+
/**
29+
* Test class for redirect controller
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
32+
class RedirectTest extends TestCase
33+
{
34+
private const DEFAULT_STORE_VIEW_CODE = 'default';
35+
private const STORE_CODE = 'sv1';
36+
37+
/**
38+
* @var StoreRepositoryInterface|MockObject
39+
*/
40+
private $storeRepositoryMock;
41+
42+
/**
43+
* @var RequestInterface|MockObject
44+
*/
45+
private $requestMock;
46+
47+
/**
48+
* @var StoreResolverInterface|MockObject
49+
*/
50+
private $storeResolverMock;
51+
52+
/**
53+
* @var RedirectInterface|MockObject
54+
*/
55+
private $redirectMock;
56+
57+
/**
58+
* @var ResponseInterface|MockObject
59+
*/
60+
private $responseMock;
61+
62+
/**
63+
* @var ManagerInterface|MockObject
64+
*/
65+
private $messageManagerMock;
66+
67+
/**
68+
* @var Store|MockObject
69+
*/
70+
private $formStoreMock;
71+
72+
/**
73+
* @var Store|MockObject
74+
*/
75+
private $currentStoreMock;
76+
77+
/**
78+
* @var SidResolverInterface|MockObject
79+
*/
80+
private $sidResolverMock;
81+
82+
/**
83+
* @var HashGenerator|MockObject
84+
*/
85+
private $hashGeneratorMock;
86+
87+
/**
88+
* @var Redirect
89+
*/
90+
private $redirectController;
91+
92+
/**
93+
* @inheritDoc
94+
*/
95+
protected function setUp()
96+
{
97+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
98+
->disableOriginalConstructor()
99+
->setMethods(['getParam'])
100+
->getMockForAbstractClass();
101+
$this->redirectMock = $this->getMockBuilder(RedirectInterface::class)
102+
->disableOriginalConstructor()
103+
->setMethods(['redirect'])
104+
->getMockForAbstractClass();
105+
$this->storeResolverMock = $this->getMockBuilder(StoreResolverInterface::class)
106+
->disableOriginalConstructor()
107+
->setMethods(['getCurrentStoreId'])
108+
->getMockForAbstractClass();
109+
$this->storeRepositoryMock = $this->getMockBuilder(StoreRepositoryInterface::class)
110+
->disableOriginalConstructor()
111+
->setMethods(['getById', 'get'])
112+
->getMockForAbstractClass();
113+
$this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class)
114+
->disableOriginalConstructor()
115+
->setMethods(['addErrorMessage'])
116+
->getMockForAbstractClass();
117+
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
118+
->disableOriginalConstructor()
119+
->getMockForAbstractClass();
120+
$this->formStoreMock = $this->getMockBuilder(Store::class)
121+
->disableOriginalConstructor()
122+
->setMethods(['getCode'])
123+
->getMockForAbstractClass();
124+
$this->sidResolverMock = $this->getMockBuilder(SidResolverInterface::class)
125+
->disableOriginalConstructor()
126+
->setMethods(['getUseSessionInUrl'])
127+
->getMockForAbstractClass();
128+
$this->hashGeneratorMock = $this->createMock(HashGenerator::class);
129+
130+
$this->currentStoreMock = $this->getMockBuilder(Store::class)
131+
->disableOriginalConstructor()
132+
->setMethods(['getBaseUrl'])
133+
->getMock();
134+
$this->storeRepositoryMock
135+
->expects($this->once())
136+
->method('getById')
137+
->willReturn($this->currentStoreMock);
138+
$this->storeResolverMock
139+
->expects($this->once())
140+
->method('getCurrentStoreId')
141+
->willReturnSelf();
142+
143+
$objectManager = new ObjectManagerHelper($this);
144+
$context = $objectManager->getObject(
145+
Context::class,
146+
[
147+
'_request' => $this->requestMock,
148+
'_redirect' => $this->redirectMock,
149+
'_response' => $this->responseMock,
150+
'messageManager' => $this->messageManagerMock,
151+
]
152+
);
153+
$this->redirectController = $objectManager->getObject(
154+
Redirect::class,
155+
[
156+
'storeRepository' => $this->storeRepositoryMock,
157+
'storeResolver' => $this->storeResolverMock,
158+
'sidResolver' => $this->sidResolverMock,
159+
'hashGenerator' => $this->hashGeneratorMock,
160+
'context' => $context,
161+
]
162+
);
163+
}
164+
165+
/**
166+
* Verify redirect controller
167+
*
168+
* @param string $defaultStoreViewCode
169+
* @param string $storeCode
170+
*
171+
* @dataProvider getConfigDataProvider
172+
* @return void
173+
*/
174+
public function testRedirect(string $defaultStoreViewCode, string $storeCode): void
175+
{
176+
$this->requestMock
177+
->expects($this->exactly(3))
178+
->method('getParam')
179+
->withConsecutive(
180+
[StoreResolver::PARAM_NAME],
181+
['___from_store'],
182+
[ActionInterface::PARAM_NAME_URL_ENCODED]
183+
)->willReturnOnConsecutiveCalls(
184+
$storeCode,
185+
$defaultStoreViewCode,
186+
$defaultStoreViewCode
187+
);
188+
$this->storeRepositoryMock
189+
->expects($this->once())
190+
->method('get')
191+
->with($defaultStoreViewCode)
192+
->willReturn($this->formStoreMock);
193+
$this->formStoreMock
194+
->expects($this->once())
195+
->method('getCode')
196+
->willReturn($defaultStoreViewCode);
197+
$this->hashGeneratorMock
198+
->expects($this->once())
199+
->method('generateHash')
200+
->with($this->formStoreMock)
201+
->willReturn([]);
202+
$this->redirectMock
203+
->expects($this->once())
204+
->method('redirect')
205+
->with(
206+
$this->responseMock,
207+
'stores/store/switch',
208+
['_nosid' => true,
209+
'_query' => [
210+
'uenc' => $defaultStoreViewCode,
211+
'___from_store' => $defaultStoreViewCode,
212+
'___store' => $storeCode
213+
]
214+
]
215+
);
216+
217+
$this->assertEquals(null, $this->redirectController->execute());
218+
}
219+
220+
/**
221+
* Verify execute with exception
222+
*
223+
* @param string $defaultStoreViewCode
224+
* @param string $storeCode
225+
* @return void
226+
* @dataProvider getConfigDataProvider
227+
*/
228+
public function testRedirectWithThrowsException(string $defaultStoreViewCode, string $storeCode): void
229+
{
230+
$this->requestMock
231+
->expects($this->exactly(2))
232+
->method('getParam')
233+
->withConsecutive(
234+
[StoreResolver::PARAM_NAME],
235+
['___from_store']
236+
)->willReturnOnConsecutiveCalls(
237+
$storeCode,
238+
$defaultStoreViewCode
239+
);
240+
$this->storeRepositoryMock
241+
->expects($this->once())
242+
->method('get')
243+
->with($defaultStoreViewCode)
244+
->willThrowException(new NoSuchEntityException());
245+
$this->messageManagerMock
246+
->expects($this->once())
247+
->method('addErrorMessage')
248+
->with("Requested store is not found ({$defaultStoreViewCode})")
249+
->willReturnSelf();
250+
$this->currentStoreMock
251+
->expects($this->once())
252+
->method('getBaseUrl')
253+
->willReturnSelf();
254+
$this->redirectMock
255+
->expects($this->once())
256+
->method('redirect')
257+
->with($this->responseMock, $this->currentStoreMock)
258+
->willReturnSelf();
259+
260+
$this->assertEquals(null, $this->redirectController->execute());
261+
}
262+
263+
/**
264+
* Verify redirect target is null
265+
*
266+
* @return void
267+
*/
268+
public function testRedirectTargetIsNull(): void
269+
{
270+
$this->requestMock
271+
->expects($this->exactly(2))
272+
->method('getParam')
273+
->withConsecutive(
274+
[StoreResolver::PARAM_NAME],
275+
['___from_store']
276+
)->willReturnOnConsecutiveCalls(
277+
null,
278+
null
279+
);
280+
$this->storeRepositoryMock
281+
->expects($this->never())
282+
->method('get');
283+
284+
$this->assertEquals($this->responseMock, $this->redirectController->execute());
285+
}
286+
287+
/**
288+
* Data provider
289+
*
290+
* @return array
291+
*/
292+
public function getConfigDataProvider(): array
293+
{
294+
return [
295+
[self::DEFAULT_STORE_VIEW_CODE, self::STORE_CODE]
296+
];
297+
}
298+
}

app/code/Magento/StoreGraphQl/Controller/HttpRequestValidator/StoreValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function validate(HttpRequestInterface $request): void
4545
$storeCode = trim($headerValue);
4646
if (!$this->isStoreActive($storeCode)) {
4747
$this->storeManager->setCurrentStore(null);
48-
throw new GraphQlInputException(__('Requested store is not found'));
48+
throw new GraphQlInputException(__('Requested store is not found ({$storeCode})'));
4949
}
5050
}
5151
}

0 commit comments

Comments
 (0)