Skip to content

Commit 0c9fafb

Browse files
committed
MAGETWO-52117: Customer group is not changed for logged in customer
1 parent 525af29 commit 0c9fafb

File tree

4 files changed

+213
-4
lines changed

4 files changed

+213
-4
lines changed

app/code/Magento/Customer/Controller/Account/UpdateSession.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use Magento\Customer\Controller\AbstractAccount;
99
use Magento\Customer\Model\Customer\NotificationStorage;
10-
use Magento\Customer\Model\ResourceModel\CustomerRepository;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
1111
use Magento\Framework\App\Action\Context;
1212
use Magento\Customer\Model\Session;
1313
use Magento\Framework\Json\Helper\Data;
@@ -20,7 +20,7 @@ class UpdateSession extends AbstractAccount
2020
private $notificationStorage;
2121

2222
/**
23-
* @var CustomerRepository
23+
* @var CustomerRepositoryInterface
2424
*/
2525
private $customerRepository;
2626

@@ -37,14 +37,14 @@ class UpdateSession extends AbstractAccount
3737
/**
3838
* @param Context $context
3939
* @param NotificationStorage $notificationStorage
40-
* @param CustomerRepository $customerRepository
40+
* @param CustomerRepositoryInterface $customerRepository
4141
* @param Session $customerSession
4242
* @param Data $jsonHelper
4343
*/
4444
public function __construct(
4545
Context $context,
4646
NotificationStorage $notificationStorage,
47-
CustomerRepository $customerRepository,
47+
CustomerRepositoryInterface $customerRepository,
4848
Session $customerSession,
4949
Data $jsonHelper
5050
) {

app/code/Magento/Customer/Model/Cache/Type/Notification.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Customer\Model\Cache\Type;
77

8+
/**
9+
* System / Cache Management / Cache type "Customer Notification"
10+
*/
811
class Notification extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
912
{
1013
/**
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Controller\Account;
7+
8+
use Magento\Customer\Controller\Account\UpdateSession;
9+
use Magento\Customer\Model\Customer\NotificationStorage;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\App\Action\Context;
12+
use Magento\Customer\Model\Session;
13+
use Magento\Framework\Json\Helper\Data;
14+
15+
class UpdateSessionTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var UpdateSession
19+
*/
20+
protected $model;
21+
22+
/**
23+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
protected $context;
26+
27+
/**
28+
* @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
protected $notificationStorage;
31+
32+
/**
33+
* @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected $customerRepository;
36+
37+
/**
38+
* @var Session|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
protected $customerSession;
41+
42+
/**
43+
* @var Data|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
protected $jsonHelper;
46+
47+
48+
protected function setUp()
49+
{
50+
$this->context = $this->getMockBuilder('Magento\Framework\App\Action\Context')
51+
->disableOriginalConstructor()
52+
->getMock();
53+
54+
$this->notificationStorage = $this->getMockBuilder('Magento\Customer\Model\Customer\NotificationStorage')
55+
->disableOriginalConstructor()
56+
->getMock();
57+
58+
$this->customerSession = $this->getMockBuilder('Magento\Customer\Model\Session')
59+
->disableOriginalConstructor()
60+
->getMock();
61+
62+
$this->customerRepository = $this->getMockBuilder('Magento\Customer\Api\CustomerRepositoryInterface')
63+
->getMockForAbstractClass();
64+
65+
$this->jsonHelper = $this->getMockBuilder('Magento\Framework\Json\Helper\Data')
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$this->model = new UpdateSession(
70+
$this->context,
71+
$this->notificationStorage,
72+
$this->customerRepository,
73+
$this->customerSession,
74+
$this->jsonHelper
75+
);
76+
}
77+
78+
public function testExecute()
79+
{
80+
$customerData = ['customer_id' => 1];
81+
$customerGroup = 1;
82+
83+
$requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
84+
->setMethods(['getContent'])
85+
->getMockForAbstractClass();
86+
$requestMock->expects($this->any())->method('getContent')->willReturn(json_encode($customerData));
87+
88+
$reflection = new \ReflectionClass(get_class($this->model));
89+
$reflectionProperty = $reflection->getProperty('_request');
90+
$reflectionProperty->setAccessible(true);
91+
$reflectionProperty->setValue($this->model, $requestMock);
92+
93+
$this->jsonHelper->expects($this->any())->method('jsonDecode')->willReturn($customerData);
94+
95+
$customer = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')->getMockForAbstractClass();
96+
$customer->expects($this->once())->method('getId')->willReturn($customerData['customer_id']);
97+
$customer->expects($this->once())->method('getGroupId')->willReturn($customerGroup);
98+
99+
$this->customerRepository->expects($this->once())
100+
->method('getById')
101+
->with($customerData['customer_id'])
102+
->willReturn($customer);
103+
104+
$this->customerSession->expects($this->once())->method('setCustomerData')->with($customer);
105+
$this->customerSession->expects($this->once())->method('setCustomerGroupId')->with($customerGroup);
106+
$this->customerSession->expects($this->once())->method('regenerateId');
107+
108+
$this->notificationStorage->expects($this->once())
109+
->method('isExists')
110+
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerData['customer_id'])
111+
->willReturn(true);
112+
$this->notificationStorage->expects($this->once())
113+
->method('remove')
114+
->with(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customerData['customer_id']);
115+
116+
$this->model->execute();
117+
}
118+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Model\Customer;
7+
8+
use Magento\Customer\Model\Customer\NotificationStorage;
9+
10+
/**
11+
* Class NotificationStorageTest
12+
*
13+
* Test for class \Magento\Customer\Model\Customer\NotificationStorage
14+
*/
15+
class NotificationStorageTest extends \PHPUnit_Framework_TestCase
16+
{
17+
18+
/**
19+
* @var NotificationStorage|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $model;
22+
23+
/**
24+
* @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $cache;
27+
28+
/**
29+
* Set up
30+
*
31+
* @return void
32+
*/
33+
protected function setUp()
34+
{
35+
$this->cache = $this->getMockBuilder('Magento\Framework\Cache\FrontendInterface')->getMockForAbstractClass();
36+
$this->model = new NotificationStorage($this->cache);
37+
}
38+
39+
40+
public function testAdd()
41+
{
42+
$customerId = 1;
43+
$notificationType = 'some_type';
44+
$this->cache->expects($this->once())
45+
->method('save')
46+
->with(
47+
serialize([
48+
'customer_id' => $customerId,
49+
'notification_type' => $notificationType
50+
]),
51+
$this->getCacheKey($notificationType, $customerId)
52+
);
53+
$this->model->add($notificationType, $customerId);
54+
}
55+
56+
public function testIsExists()
57+
{
58+
$customerId = 1;
59+
$notificationType = 'some_type';
60+
$this->cache->expects($this->once())
61+
->method('test')
62+
->with($this->getCacheKey($notificationType, $customerId))
63+
->willReturn(true);
64+
$this->assertTrue($this->model->isExists($notificationType, $customerId));
65+
}
66+
67+
public function testRemove()
68+
{
69+
$customerId = 1;
70+
$notificationType = 'some_type';
71+
$this->cache->expects($this->once())
72+
->method('remove')
73+
->with($this->getCacheKey($notificationType, $customerId));
74+
$this->model->remove($notificationType, $customerId);
75+
}
76+
77+
/**
78+
* Retrieve cache key
79+
*
80+
* @param string $notificationType
81+
* @param string $customerId
82+
* @return string
83+
*/
84+
protected function getCacheKey($notificationType, $customerId)
85+
{
86+
return 'notification_' . $notificationType . '_' . $customerId;
87+
}
88+
}

0 commit comments

Comments
 (0)