Skip to content

Commit e0d9056

Browse files
committed
Change the customer notification plugin from AbstractAction:dispatch to ActionInterface::execute
1 parent 64dcbea commit e0d9056

File tree

3 files changed

+89
-64
lines changed

3 files changed

+89
-64
lines changed

app/code/Magento/Customer/Model/Plugin/CustomerNotification.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Customer\Model\Plugin;
78

89
use Magento\Customer\Api\CustomerRepositoryInterface;
910
use Magento\Customer\Model\Customer\NotificationStorage;
1011
use Magento\Customer\Model\Session;
11-
use Magento\Framework\App\Action\AbstractAction;
12+
use Magento\Framework\App\ActionInterface;
1213
use Magento\Framework\App\Area;
14+
use Magento\Framework\App\ObjectManager;
1315
use Magento\Framework\App\RequestInterface;
1416
use Magento\Framework\App\State;
1517
use Magento\Framework\Exception\NoSuchEntityException;
@@ -42,6 +44,11 @@ class CustomerNotification
4244
*/
4345
private $logger;
4446

47+
/**
48+
* @var RequestInterface|\Magento\Framework\App\Request\Http
49+
*/
50+
private $request;
51+
4552
/**
4653
* Initialize dependencies.
4754
*
@@ -50,37 +57,38 @@ class CustomerNotification
5057
* @param State $state
5158
* @param CustomerRepositoryInterface $customerRepository
5259
* @param LoggerInterface $logger
60+
* @param RequestInterface|null $request
5361
*/
5462
public function __construct(
5563
Session $session,
5664
NotificationStorage $notificationStorage,
5765
State $state,
5866
CustomerRepositoryInterface $customerRepository,
59-
LoggerInterface $logger
67+
LoggerInterface $logger,
68+
RequestInterface $request = null
6069
) {
6170
$this->session = $session;
6271
$this->notificationStorage = $notificationStorage;
6372
$this->state = $state;
6473
$this->customerRepository = $customerRepository;
6574
$this->logger = $logger;
75+
$this->request = $request ?? ObjectManager::getInstance()->get(RequestInterface::class);
6676
}
6777

6878
/**
69-
* @param AbstractAction $subject
70-
* @param RequestInterface $request
79+
* Refresh the customer session on frontend post requests if an update session notification is registered.
80+
*
81+
* @param ActionInterface $subject
7182
* @return void
83+
* @throws \Magento\Framework\Exception\LocalizedException
7284
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7385
*/
74-
public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
86+
public function beforeExecute(ActionInterface $subject)
7587
{
7688
$customerId = $this->session->getCustomerId();
7789

78-
if ($this->state->getAreaCode() == Area::AREA_FRONTEND && $request->isPost()
79-
&& $this->notificationStorage->isExists(
80-
NotificationStorage::UPDATE_CUSTOMER_SESSION,
81-
$customerId
82-
)
83-
) {
90+
if ($this->isFrontendRequest() && $this->isPostRequest() && $this->isSessionUpdateRegisteredFor($customerId))
91+
{
8492
try {
8593
$customer = $this->customerRepository->getById($customerId);
8694
$this->session->setCustomerData($customer);
@@ -92,4 +100,36 @@ public function beforeDispatch(AbstractAction $subject, RequestInterface $reques
92100
}
93101
}
94102
}
103+
104+
/**
105+
* Because RequestInterface has no isPost method the check is requied before calling it.
106+
*
107+
* @return bool
108+
*/
109+
private function isPostRequest(): bool
110+
{
111+
return method_exists($this->request, 'isPost') && $this->request->isPost();
112+
}
113+
114+
/**
115+
* Check if the current application area is frontend.
116+
*
117+
* @return bool
118+
* @throws \Magento\Framework\Exception\LocalizedException
119+
*/
120+
private function isFrontendRequest(): bool
121+
{
122+
return $this->state->getAreaCode() == Area::AREA_FRONTEND;
123+
}
124+
125+
/**
126+
* True if the session for the given customer ID needs to be refreshed.
127+
*
128+
* @param int $customerId
129+
* @return bool
130+
*/
131+
private function isSessionUpdateRegisteredFor($customerId): bool
132+
{
133+
return $this->notificationStorage->isExists(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId);
134+
}
95135
}

app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerNotificationTest.php

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66
namespace Magento\Customer\Test\Unit\Model\Plugin;
77

8-
use Magento\Backend\App\AbstractAction;
98
use Magento\Customer\Api\CustomerRepositoryInterface;
109
use Magento\Customer\Api\Data\CustomerInterface;
1110
use Magento\Customer\Model\Customer\NotificationStorage;
1211
use Magento\Customer\Model\Plugin\CustomerNotification;
1312
use Magento\Customer\Model\Session;
13+
use Magento\Framework\App\ActionInterface;
1414
use Magento\Framework\App\Area;
1515
use Magento\Framework\App\RequestInterface;
1616
use Magento\Framework\App\State;
@@ -20,25 +20,25 @@
2020
class CustomerNotificationTest extends \PHPUnit\Framework\TestCase
2121
{
2222
/** @var Session|\PHPUnit_Framework_MockObject_MockObject */
23-
private $sessionMock;
23+
private $session;
2424

2525
/** @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject */
26-
private $notificationStorageMock;
26+
private $notificationStorage;
2727

2828
/** @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
29-
private $customerRepositoryMock;
29+
private $customerRepository;
3030

3131
/** @var State|\PHPUnit_Framework_MockObject_MockObject */
32-
private $appStateMock;
32+
private $appState;
3333

3434
/** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
35-
private $requestMock;
35+
private $request;
3636

37-
/** @var AbstractAction|\PHPUnit_Framework_MockObject_MockObject */
38-
private $abstractActionMock;
39-
40-
/** @var LoggerInterface */
41-
private $loggerMock;
37+
/** @var ActionInterface|\PHPUnit_Framework_MockObject_MockObject */
38+
private $actionInterfaceMock;
39+
40+
/** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
41+
private $logger;
4242

4343
/** @var CustomerNotification */
4444
private $plugin;
@@ -48,76 +48,61 @@ class CustomerNotificationTest extends \PHPUnit\Framework\TestCase
4848

4949
protected function setUp()
5050
{
51-
$this->sessionMock = $this->getMockBuilder(Session::class)
52-
->disableOriginalConstructor()
53-
->setMethods(['getCustomerId', 'setCustomerData', 'setCustomerGroupId', 'regenerateId'])
54-
->getMock();
55-
$this->notificationStorageMock = $this->getMockBuilder(NotificationStorage::class)
56-
->disableOriginalConstructor()
57-
->setMethods(['isExists', 'remove'])
58-
->getMock();
59-
$this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
60-
->getMockForAbstractClass();
61-
$this->abstractActionMock = $this->getMockBuilder(AbstractAction::class)
62-
->disableOriginalConstructor()
63-
->getMockForAbstractClass();
64-
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
51+
$this->session = $this->createMock(Session::class);
52+
$this->notificationStorage = $this->createMock(NotificationStorage::class);
53+
$this->customerRepository = $this->createMock(CustomerRepositoryInterface::class);
54+
$this->actionInterfaceMock = $this->createMock(ActionInterface::class);
55+
$this->request = $this->getMockBuilder(RequestInterface::class)
6556
->setMethods(['isPost'])
6657
->getMockForAbstractClass();
67-
$this->appStateMock = $this->getMockBuilder(State::class)
68-
->disableOriginalConstructor()
69-
->setMethods(['getAreaCode'])
70-
->getMock();
71-
72-
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
73-
$this->appStateMock->method('getAreaCode')->willReturn(Area::AREA_FRONTEND);
74-
$this->requestMock->method('isPost')->willReturn(true);
75-
$this->sessionMock->method('getCustomerId')->willReturn(self::$customerId);
76-
$this->notificationStorageMock->expects($this->any())
58+
$this->appState = $this->createMock(State::class);
59+
$this->logger = $this->createMock(LoggerInterface::class);
60+
61+
$this->appState->method('getAreaCode')->willReturn(Area::AREA_FRONTEND);
62+
$this->request->method('isPost')->willReturn(true);
63+
$this->session->method('getCustomerId')->willReturn(self::$customerId);
64+
$this->notificationStorage->expects($this->any())
7765
->method('isExists')
7866
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId)
7967
->willReturn(true);
8068

8169
$this->plugin = new CustomerNotification(
82-
$this->sessionMock,
83-
$this->notificationStorageMock,
84-
$this->appStateMock,
85-
$this->customerRepositoryMock,
86-
$this->loggerMock
70+
$this->session,
71+
$this->notificationStorage,
72+
$this->appState,
73+
$this->customerRepository,
74+
$this->logger,
75+
$this->request
8776
);
8877
}
8978

90-
public function testBeforeDispatch()
79+
public function testBeforeExecute()
9180
{
9281
$customerGroupId =1;
9382

94-
$customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
83+
$customerMock = $this->createMock(CustomerInterface::class);
9584
$customerMock->method('getGroupId')->willReturn($customerGroupId);
9685
$customerMock->method('getId')->willReturn(self::$customerId);
9786

98-
$this->customerRepositoryMock->expects($this->once())
87+
$this->customerRepository->expects($this->once())
9988
->method('getById')
10089
->with(self::$customerId)
10190
->willReturn($customerMock);
102-
$this->notificationStorageMock->expects($this->once())
91+
$this->notificationStorage->expects($this->once())
10392
->method('remove')
10493
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId);
10594

106-
$this->sessionMock->expects($this->once())->method('setCustomerData')->with($customerMock);
107-
$this->sessionMock->expects($this->once())->method('setCustomerGroupId')->with($customerGroupId);
108-
$this->sessionMock->expects($this->once())->method('regenerateId');
109-
110-
$this->plugin->beforeDispatch($this->abstractActionMock, $this->requestMock);
95+
$this->plugin->beforeExecute($this->actionInterfaceMock);
11196
}
11297

11398
public function testBeforeDispatchWithNoCustomerFound()
11499
{
115-
$this->customerRepositoryMock->method('getById')
100+
$this->customerRepository->method('getById')
116101
->with(self::$customerId)
117102
->willThrowException(new NoSuchEntityException());
118-
$this->loggerMock->expects($this->once())
103+
$this->logger->expects($this->once())
119104
->method('error');
120105

121-
$this->plugin->beforeDispatch($this->abstractActionMock, $this->requestMock);
106+
$this->plugin->beforeExecute($this->actionInterfaceMock);
122107
}
123108
}

app/code/Magento/Customer/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
<type name="Magento\Directory\Model\AllowedCountries">
334334
<plugin name="customerAllowedCountries" type="Magento\Customer\Model\Plugin\AllowedCountries"/>
335335
</type>
336-
<type name="Magento\Framework\App\Action\AbstractAction">
336+
<type name="Magento\Framework\App\ActionInterface">
337337
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
338338
</type>
339339
<type name="Magento\PageCache\Observer\FlushFormKey">

0 commit comments

Comments
 (0)