Skip to content

Commit c6dd83e

Browse files
author
vpaladiychuk
committed
Merge branch 'MAGETWO-26762' into MAGETWO-34995
2 parents b697ecd + dfc676a commit c6dd83e

File tree

9 files changed

+166
-30
lines changed

9 files changed

+166
-30
lines changed

app/code/Magento/Catalog/Controller/Index/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Catalog\Controller\Index;
87

98
use Magento\Framework\App\Action\Context;
9+
1010
class Index extends \Magento\Framework\App\Action\Action
1111
{
1212
/**

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Action/Attribute/SaveTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ protected function prepareContext()
216216
$this->context->expects($this->any())->method('getFormKeyValidator')->willReturn($this->formKeyValidator);
217217
$this->context->expects($this->any())->method('getTitle')->willReturn($this->title);
218218
$this->context->expects($this->any())->method('getLocaleResolver')->willReturn($this->localeResolver);
219-
$this->context->expects($this->any())->method('getResultRedirectFactory')->willReturn($this->resultRedirectFactory);
219+
$this->context->expects($this->any())
220+
->method('getResultRedirectFactory')
221+
->willReturn($this->resultRedirectFactory);
220222

221223
$this->product = $this->getMock(
222224
'Magento\Catalog\Model\Product',

app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/MassDeleteTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ protected function setUp()
7373
->method('create')
7474
->will($this->returnValue($this->redirect));
7575
$this->context = $this->getMockBuilder('Magento\Backend\App\Action\Context')
76-
->setMethods(['getRequest', 'getResponse', 'getObjectManager', 'getMessageManager', 'getResultRedirectFactory'])
76+
->setMethods(
77+
['getRequest', 'getResponse', 'getObjectManager', 'getMessageManager', 'getResultRedirectFactory']
78+
)
7779
->disableOriginalConstructor()
7880
->getMock();
7981
$this->context->expects($this->atLeastOnce())

app/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,12 @@
10741074
</argument>
10751075
</arguments>
10761076
</type>
1077+
<type name="Magento\Framework\App\FrontController">
1078+
<arguments>
1079+
<argument name="appState" xsi:type="object">Magento\Framework\App\State\Proxy</argument>
1080+
<argument name="messageManager" xsi:type="object">Magento\Framework\Message\ManagerInterface\Proxy</argument>
1081+
</arguments>
1082+
</type>
10771083
<type name="Magento\Framework\Webapi\Rest\Request\DeserializerFactory">
10781084
<arguments>
10791085
<argument name="deserializers" xsi:type="array">

lib/internal/Magento/Framework/App/Action/AbstractAction.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@ abstract class AbstractAction implements \Magento\Framework\App\ActionInterface
2525
protected $resultRedirectFactory;
2626

2727
/**
28-
* @param \Magento\Framework\App\RequestInterface $request
29-
* @param \Magento\Framework\App\ResponseInterface $response
3028
* @param \Magento\Framework\App\Action\Context $context
3129
*/
3230
public function __construct(
33-
\Magento\Framework\App\RequestInterface $request,
34-
\Magento\Framework\App\ResponseInterface $response,
3531
\Magento\Framework\App\Action\Context $context
3632
) {
37-
$this->_request = $request;
38-
$this->_response = $response;
33+
$this->_request = $context->getRequest();
34+
$this->_response = $context->getResponse();
3935
$this->resultRedirectFactory = $context->getResultRedirectFactory();
4036
}
4137

lib/internal/Magento/Framework/App/Action/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Action extends AbstractAction
6565
*/
6666
public function __construct(Context $context)
6767
{
68-
parent::__construct($context->getRequest(), $context->getResponse(), $context);
68+
parent::__construct($context);
6969
$this->_objectManager = $context->getObjectManager();
7070
$this->_eventManager = $context->getEventManager();
7171
$this->_url = $context->getUrl();

lib/internal/Magento/Framework/App/FrontController.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function dispatch(RequestInterface $request)
6767
$routingCycleCounter = 0;
6868
$result = null;
6969
while (!$request->isDispatched() && $routingCycleCounter++ < 100) {
70-
$result = $this->matchAction($request);
70+
$result = $this->processRequest($request);
7171
}
7272
\Magento\Framework\Profiler::stop('routers_match');
7373
if ($routingCycleCounter > 100) {
@@ -81,23 +81,27 @@ public function dispatch(RequestInterface $request)
8181
*
8282
* @param \Exception $e
8383
* @param \Magento\Framework\App\ActionInterface $actionInstance
84-
* @param string $message
8584
* @return \Magento\Framework\Controller\Result\Redirect
8685
*/
87-
protected function handleException($e, $actionInstance, $message)
86+
protected function handleException($e, $actionInstance)
8887
{
89-
$this->messageManager->addError($message);
88+
$needToMaskDisplayMessage = !($e instanceof \Magento\Framework\Exception\LocalizedException)
89+
&& ($this->appState->getMode() != State::MODE_DEVELOPER);
90+
$displayMessage = $needToMaskDisplayMessage
91+
? (string)new \Magento\Framework\Phrase('An error occurred while processing your request')
92+
: $e->getMessage();
93+
$this->messageManager->addError($displayMessage);
9094
$this->logger->critical($e->getMessage());
9195
return $actionInstance->getDefaultRedirect();
9296
}
9397

9498
/**
95-
* Match action, dispatch
99+
* Route request and dispatch it
96100
*
97101
* @param RequestInterface $request
98-
* @return \Magento\Framework\Controller\Result\Redirect
102+
* @return ResponseInterface|\Magento\Framework\Controller\ResultInterface|null
99103
*/
100-
protected function matchAction(RequestInterface $request)
104+
protected function processRequest(RequestInterface $request)
101105
{
102106
$result = null;
103107
/** @var \Magento\Framework\App\RouterInterface $router */
@@ -107,24 +111,20 @@ protected function matchAction(RequestInterface $request)
107111
if ($actionInstance) {
108112
$request->setDispatched(true);
109113
$actionInstance->getResponse()->setNoCacheHeaders();
110-
$result = $actionInstance->dispatch($request);
114+
try {
115+
$result = $actionInstance->dispatch($request);
116+
} catch (Action\NotFoundException $e) {
117+
throw $e;
118+
} catch (\Exception $e) {
119+
$result = $this->handleException($e, $actionInstance);
120+
}
111121
break;
112122
}
113123
} catch (Action\NotFoundException $e) {
114124
$request->initForward();
115125
$request->setActionName('noroute');
116126
$request->setDispatched(false);
117127
break;
118-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
119-
$result = $this->handleException($e, $actionInstance, $e->getMessage());
120-
break;
121-
} catch (\Exception $e) {
122-
// @todo Message should be clarified
123-
$message = $this->appState->getMode() == State::MODE_DEVELOPER
124-
? $e->getMessage()
125-
: (string)new \Magento\Framework\Phrase('An error occurred while processing your request');
126-
$result = $this->handleException($e, $actionInstance, $message);
127-
break;
128128
}
129129
}
130130
return $result;

lib/internal/Magento/Framework/App/Test/Unit/FrontControllerTest.php

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\App\Test\Unit;
77

88
use Magento\Framework\App\Action\NotFoundException;
9+
use \Magento\Framework\App\State;
910

1011
class FrontControllerTest extends \PHPUnit_Framework_TestCase
1112
{
@@ -29,6 +30,26 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
2930
*/
3031
protected $router;
3132

33+
/**
34+
* @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $resultRedirect;
37+
38+
/**
39+
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $messageManager;
42+
43+
/**
44+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
protected $logger;
47+
48+
/**
49+
* @var State|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
protected $appState;
52+
3253
protected function setUp()
3354
{
3455
$this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
@@ -38,8 +59,19 @@ protected function setUp()
3859

3960
$this->router = $this->getMock('Magento\Framework\App\RouterInterface');
4061
$this->routerList = $this->getMock('Magento\Framework\App\RouterList', [], [], '', false);
62+
$this->messageManager = $this->getMock('Magento\Framework\Message\ManagerInterface', [], [], '', false);
63+
$this->logger = $this->getMock('Psr\Log\LoggerInterface', [], [], '', false);
64+
$this->appState = $this->getMock('Magento\Framework\App\State', [], [], '', false);
4165
$this->model = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
42-
->getObject('Magento\Framework\App\FrontController', ['routerList' => $this->routerList]);
66+
->getObject(
67+
'Magento\Framework\App\FrontController',
68+
[
69+
'routerList' => $this->routerList,
70+
'messageManager' => $this->messageManager,
71+
'logger' => $this->logger,
72+
'appState' => $this->appState
73+
]
74+
);
4375
}
4476

4577
/**
@@ -141,4 +173,103 @@ public function testDispatchedNotFoundException()
141173

142174
$this->assertEquals($response, $this->model->dispatch($this->request));
143175
}
176+
177+
public function testDispatchedLocalizedException()
178+
{
179+
$message = 'Test';
180+
$this->routerList->expects($this->any())
181+
->method('valid')
182+
->willReturn(true);
183+
184+
$this->resultRedirect = $this->getMock('Magento\Framework\Controller\Result\Redirect', [], [], '', false);
185+
186+
$response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false);
187+
$controllerInstance = $this->getMock('Magento\Framework\App\ActionInterface');
188+
$controllerInstance->expects($this->any())
189+
->method('getResponse')
190+
->willReturn($response);
191+
$controllerInstance->expects($this->any())
192+
->method('dispatch')
193+
->with($this->request)
194+
->willThrowException(
195+
new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase($message))
196+
);
197+
$controllerInstance->expects($this->once())->method('getDefaultRedirect')->willReturn($this->resultRedirect);
198+
199+
$this->router->expects($this->once())
200+
->method('match')
201+
->with($this->request)
202+
->willReturn($controllerInstance);
203+
204+
$this->routerList->expects($this->any())
205+
->method('current')
206+
->willReturn($this->router);
207+
208+
$this->request->expects($this->at(0))->method('isDispatched')->willReturn(false);
209+
$this->request->expects($this->once())->method('setDispatched')->with(true);
210+
$this->request->expects($this->at(2))->method('isDispatched')->willReturn(true);
211+
212+
$this->messageManager->expects($this->once())->method('addError')->with($message);
213+
$this->logger->expects($this->once())->method('critical')->with($message);
214+
215+
$this->assertEquals($this->resultRedirect, $this->model->dispatch($this->request));
216+
}
217+
218+
/**
219+
* @param string $mode
220+
* @param string $exceptionMessage
221+
* @param string $sessionMessage
222+
* @dataProvider dispatchedWithPhpExceptionDataProvider
223+
*/
224+
public function testDispatchedPhpException($mode, $exceptionMessage, $sessionMessage)
225+
{
226+
$this->routerList->expects($this->any())
227+
->method('valid')
228+
->willReturn(true);
229+
230+
$this->resultRedirect = $this->getMock('Magento\Framework\Controller\Result\Redirect', [], [], '', false);
231+
232+
$response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false);
233+
$controllerInstance = $this->getMock('Magento\Framework\App\ActionInterface');
234+
$controllerInstance->expects($this->any())
235+
->method('getResponse')
236+
->willReturn($response);
237+
$controllerInstance->expects($this->any())
238+
->method('dispatch')
239+
->with($this->request)
240+
->willThrowException(new \Exception(new \Magento\Framework\Phrase($exceptionMessage)));
241+
$controllerInstance->expects($this->once())->method('getDefaultRedirect')->willReturn($this->resultRedirect);
242+
243+
$this->router->expects($this->once())
244+
->method('match')
245+
->with($this->request)
246+
->willReturn($controllerInstance);
247+
248+
$this->routerList->expects($this->any())
249+
->method('current')
250+
->willReturn($this->router);
251+
252+
$this->request->expects($this->at(0))->method('isDispatched')->willReturn(false);
253+
$this->request->expects($this->once())->method('setDispatched')->with(true);
254+
$this->request->expects($this->at(2))->method('isDispatched')->willReturn(true);
255+
256+
$this->appState->expects($this->once())->method('getMode')->willReturn($mode);
257+
258+
$this->messageManager->expects($this->once())->method('addError')->with($sessionMessage);
259+
$this->logger->expects($this->once())->method('critical')->with($exceptionMessage);
260+
261+
$this->assertEquals($this->resultRedirect, $this->model->dispatch($this->request));
262+
}
263+
264+
/**
265+
* @return array
266+
*/
267+
public function dispatchedWithPhpExceptionDataProvider()
268+
{
269+
return [
270+
[State::MODE_DEVELOPER, 'Test', 'Test'],
271+
[State::MODE_DEFAULT, 'Test', 'An error occurred while processing your request'],
272+
[State::MODE_PRODUCTION, 'Test', 'An error occurred while processing your request'],
273+
];
274+
}
144275
}

lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use \Magento\Framework\App\View\Deployment\Version;
1010

11-
1211
class VersionTest extends \PHPUnit_Framework_TestCase
1312
{
1413
/**

0 commit comments

Comments
 (0)