Skip to content

Commit 25f3732

Browse files
committed
Merge branch 'MAGETWO-72587' into MPI-PR-2.2.1
2 parents 707f673 + 59cd452 commit 25f3732

File tree

4 files changed

+114
-63
lines changed

4 files changed

+114
-63
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Framework\App\Area;
1313
use Magento\Framework\App\RequestInterface;
1414
use Magento\Framework\App\State;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Psr\Log\LoggerInterface;
1517

1618
class CustomerNotification
1719
{
@@ -35,24 +37,32 @@ class CustomerNotification
3537
*/
3638
private $state;
3739

40+
/**
41+
* @var LoggerInterface
42+
*/
43+
private $logger;
44+
3845
/**
3946
* Initialize dependencies.
4047
*
4148
* @param Session $session
4249
* @param NotificationStorage $notificationStorage
4350
* @param State $state
4451
* @param CustomerRepositoryInterface $customerRepository
52+
* @param LoggerInterface $logger
4553
*/
4654
public function __construct(
4755
Session $session,
4856
NotificationStorage $notificationStorage,
4957
State $state,
50-
CustomerRepositoryInterface $customerRepository
58+
CustomerRepositoryInterface $customerRepository,
59+
LoggerInterface $logger
5160
) {
5261
$this->session = $session;
5362
$this->notificationStorage = $notificationStorage;
5463
$this->state = $state;
5564
$this->customerRepository = $customerRepository;
65+
$this->logger = $logger;
5666
}
5767

5868
/**
@@ -63,17 +73,23 @@ public function __construct(
6373
*/
6474
public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
6575
{
76+
$customerId = $this->session->getCustomerId();
77+
6678
if ($this->state->getAreaCode() == Area::AREA_FRONTEND && $request->isPost()
6779
&& $this->notificationStorage->isExists(
6880
NotificationStorage::UPDATE_CUSTOMER_SESSION,
69-
$this->session->getCustomerId()
81+
$customerId
7082
)
7183
) {
72-
$customer = $this->customerRepository->getById($this->session->getCustomerId());
73-
$this->session->setCustomerData($customer);
74-
$this->session->setCustomerGroupId($customer->getGroupId());
75-
$this->session->regenerateId();
76-
$this->notificationStorage->remove(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customer->getId());
84+
try {
85+
$customer = $this->customerRepository->getById($customerId);
86+
$this->session->setCustomerData($customer);
87+
$this->session->setCustomerGroupId($customer->getGroupId());
88+
$this->session->regenerateId();
89+
$this->notificationStorage->remove(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId);
90+
} catch (NoSuchEntityException $e) {
91+
$this->logger->error($e);
92+
}
7793
}
7894
}
7995
}

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Customer\Model\ResourceModel;
88

99
use Magento\Customer\Api\CustomerMetadataInterface;
10+
use Magento\Customer\Model\Customer\NotificationStorage;
1011
use Magento\Framework\Api\DataObjectHelper;
1112
use Magento\Framework\Api\ImageProcessorInterface;
1213
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
@@ -88,6 +89,11 @@ class CustomerRepository implements \Magento\Customer\Api\CustomerRepositoryInte
8889
*/
8990
private $collectionProcessor;
9091

92+
/**
93+
* @var NotificationStorage
94+
*/
95+
private $notificationStorage;
96+
9197
/**
9298
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
9399
* @param \Magento\Customer\Model\Data\CustomerSecureFactory $customerSecureFactory
@@ -103,6 +109,7 @@ class CustomerRepository implements \Magento\Customer\Api\CustomerRepositoryInte
103109
* @param ImageProcessorInterface $imageProcessor
104110
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
105111
* @param CollectionProcessorInterface $collectionProcessor
112+
* @param NotificationStorage $notificationStorage
106113
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
107114
*/
108115
public function __construct(
@@ -119,7 +126,8 @@ public function __construct(
119126
DataObjectHelper $dataObjectHelper,
120127
ImageProcessorInterface $imageProcessor,
121128
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
122-
CollectionProcessorInterface $collectionProcessor = null
129+
CollectionProcessorInterface $collectionProcessor,
130+
NotificationStorage $notificationStorage
123131
) {
124132
$this->customerFactory = $customerFactory;
125133
$this->customerSecureFactory = $customerSecureFactory;
@@ -134,7 +142,8 @@ public function __construct(
134142
$this->dataObjectHelper = $dataObjectHelper;
135143
$this->imageProcessor = $imageProcessor;
136144
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
137-
$this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
145+
$this->collectionProcessor = $collectionProcessor;
146+
$this->notificationStorage = $notificationStorage;
138147
}
139148

140149
/**
@@ -345,6 +354,8 @@ public function deleteById($customerId)
345354
$customerModel = $this->customerRegistry->retrieve($customerId);
346355
$customerModel->delete();
347356
$this->customerRegistry->remove($customerId);
357+
$this->notificationStorage->remove(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId);
358+
348359
return true;
349360
}
350361

@@ -370,20 +381,4 @@ protected function addFilterGroupToCollection(
370381
$collection->addFieldToFilter($fields);
371382
}
372383
}
373-
374-
/**
375-
* Retrieve collection processor
376-
*
377-
* @deprecated 100.2.0
378-
* @return CollectionProcessorInterface
379-
*/
380-
private function getCollectionProcessor()
381-
{
382-
if (!$this->collectionProcessor) {
383-
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
384-
'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor'
385-
);
386-
}
387-
return $this->collectionProcessor;
388-
}
389384
}

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

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

8+
use Magento\Backend\App\AbstractAction;
9+
use Magento\Customer\Api\CustomerRepositoryInterface;
10+
use Magento\Customer\Api\Data\CustomerInterface;
811
use Magento\Customer\Model\Customer\NotificationStorage;
912
use Magento\Customer\Model\Plugin\CustomerNotification;
13+
use Magento\Customer\Model\Session;
14+
use Magento\Framework\App\Area;
15+
use Magento\Framework\App\RequestInterface;
16+
use Magento\Framework\App\State;
17+
use Magento\Framework\Exception\NoSuchEntityException;
18+
use Psr\Log\LoggerInterface;
1019

1120
class CustomerNotificationTest extends \PHPUnit\Framework\TestCase
1221
{
13-
/** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
14-
protected $session;
22+
/** @var Session|\PHPUnit_Framework_MockObject_MockObject */
23+
private $session;
1524

1625
/** @var \Magento\Customer\Model\Customer\NotificationStorage|\PHPUnit_Framework_MockObject_MockObject */
17-
protected $notificationStorage;
26+
private $notificationStorage;
1827

19-
/** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
20-
protected $customerRepository;
28+
/** @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
29+
private $customerRepository;
2130

22-
/** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
23-
protected $appState;
31+
/** @var State|\PHPUnit_Framework_MockObject_MockObject */
32+
private $appState;
2433

25-
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
26-
protected $request;
34+
/** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
35+
private $request;
2736

28-
/** @var \Magento\Backend\App\AbstractAction|\PHPUnit_Framework_MockObject_MockObject */
29-
protected $abstractAction;
37+
/** @var AbstractAction|\PHPUnit_Framework_MockObject_MockObject */
38+
private $abstractAction;
3039

3140
/** @var CustomerNotification */
32-
protected $plugin;
41+
private $plugin;
42+
43+
/** @var int */
44+
private static $customerId = 1;
3345

3446
protected function setUp()
3547
{
36-
$this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
48+
$this->session = $this->getMockBuilder(Session::class)
3749
->disableOriginalConstructor()
3850
->getMock();
39-
$this->notificationStorage = $this->getMockBuilder(\Magento\Customer\Model\Customer\NotificationStorage::class)
51+
$this->notificationStorage = $this->getMockBuilder(NotificationStorage::class)
4052
->disableOriginalConstructor()
4153
->getMock();
42-
$this->customerRepository = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
43-
->getMockForAbstractClass();
44-
$this->abstractAction = $this->getMockBuilder(\Magento\Backend\App\AbstractAction::class)
54+
$this->customerRepository = $this->getMockForAbstractClass(CustomerRepositoryInterface::class);
55+
$this->abstractAction = $this->getMockBuilder(AbstractAction::class)
4556
->disableOriginalConstructor()
4657
->getMockForAbstractClass();
47-
$this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
58+
$this->request = $this->getMockBuilder(RequestInterface::class)
4859
->setMethods(['isPost'])
4960
->getMockForAbstractClass();
50-
$this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
61+
$this->appState = $this->getMockBuilder(State::class)
5162
->disableOriginalConstructor()->getMock();
63+
$this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
64+
65+
$this->appState->method('getAreaCode')->willReturn(Area::AREA_FRONTEND);
66+
$this->request->method('isPost')->willReturn(true);
67+
$this->session->method('getCustomerId')->willReturn(self::$customerId);
68+
$this->notificationStorage->expects($this->any())
69+
->method('isExists')
70+
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId)
71+
->willReturn(true);
72+
5273
$this->plugin = new CustomerNotification(
5374
$this->session,
5475
$this->notificationStorage,
5576
$this->appState,
56-
$this->customerRepository
77+
$this->customerRepository,
78+
$this->logger
5779
);
5880
}
5981

6082
public function testBeforeDispatch()
6183
{
62-
$customerId = 1;
6384
$customerGroupId =1;
64-
$this->appState->expects($this->any())
65-
->method('getAreaCode')
66-
->willReturn(\Magento\Framework\App\Area::AREA_FRONTEND);
67-
$this->request->expects($this->any())->method('isPost')->willReturn(true);
68-
$customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
69-
->getMockForAbstractClass();
70-
$customerMock->expects($this->any())->method('getGroupId')->willReturn($customerGroupId);
71-
$this->customerRepository->expects($this->any())
85+
86+
$customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
87+
$customerMock->method('getGroupId')->willReturn($customerGroupId);
88+
$this->customerRepository->expects($this->once())
7289
->method('getById')
73-
->with($customerId)
90+
->with(self::$customerId)
7491
->willReturn($customerMock);
75-
$this->session->expects($this->any())->method('getCustomerId')->willReturn($customerId);
76-
$this->session->expects($this->any())->method('setCustomerData')->with($customerMock);
77-
$this->session->expects($this->any())->method('setCustomerGroupId')->with($customerGroupId);
92+
$this->session->expects($this->once())->method('setCustomerData')->with($customerMock);
93+
$this->session->expects($this->once())->method('setCustomerGroupId')->with($customerGroupId);
7894
$this->session->expects($this->once())->method('regenerateId');
79-
$this->notificationStorage->expects($this->any())
80-
->method('isExists')
81-
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId)
82-
->willReturn(true);
95+
$this->notificationStorage->expects($this->once())
96+
->method('remove')
97+
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, self::$customerId);
98+
99+
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
100+
}
101+
102+
public function testBeforeDispatchWithNoCustomerFound()
103+
{
104+
$this->customerRepository->method('getById')
105+
->with(self::$customerId)
106+
->willThrowException(new NoSuchEntityException());
107+
$this->logger->expects($this->once())
108+
->method('error');
83109

84110
$this->plugin->beforeDispatch($this->abstractAction, $this->request);
85111
}

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/CustomerRepositoryTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Customer\Test\Unit\Model\ResourceModel;
88

99
use Magento\Customer\Api\CustomerMetadataInterface;
10+
use Magento\Customer\Model\Customer\NotificationStorage;
1011
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
1112

1213
/**
@@ -95,6 +96,11 @@ class CustomerRepositoryTest extends \PHPUnit\Framework\TestCase
9596
*/
9697
protected $model;
9798

99+
/**
100+
* @var NotificationStorage
101+
*/
102+
private $notificationStorage;
103+
98104
protected function setUp()
99105
{
100106
$this->customerResourceModel =
@@ -158,6 +164,10 @@ protected function setUp()
158164
);
159165
$this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
160166
->getMock();
167+
$this->notificationStorage = $this->getMockBuilder(NotificationStorage::class)
168+
->disableOriginalConstructor()
169+
->getMock();
170+
161171
$this->model = new \Magento\Customer\Model\ResourceModel\CustomerRepository(
162172
$this->customerFactory,
163173
$this->customerSecureFactory,
@@ -172,7 +182,8 @@ protected function setUp()
172182
$this->dataObjectHelper,
173183
$this->imageProcessor,
174184
$this->extensionAttributesJoinProcessor,
175-
$this->collectionProcessorMock
185+
$this->collectionProcessorMock,
186+
$this->notificationStorage
176187
);
177188
}
178189

@@ -789,6 +800,9 @@ public function testDelete()
789800
$this->customerRegistry->expects($this->atLeastOnce())
790801
->method('remove')
791802
->with($customerId);
803+
$this->notificationStorage->expects($this->atLeastOnce())
804+
->method('remove')
805+
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerId);
792806

793807
$this->assertTrue($this->model->delete($this->customer));
794808
}

0 commit comments

Comments
 (0)