1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Test\Unit\Observer;
use Magento\Customer\Observer\UpgradeCustomerPasswordObserver;
/**
* Class UpgradeCustomerPasswordObserverTest for testing upgrade password observer
*/
class UpgradeCustomerPasswordObserverTest extends \PHPUnit\Framework\TestCase
{
/**
* @var UpgradeCustomerPasswordObserver
*/
protected $model;
/**
* @var \Magento\Framework\Encryption\Encryptor|\PHPUnit_Framework_MockObject_MockObject
*/
protected $encryptorMock;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerRepository;
/**
* @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerRegistry;
/**
* @inheritdoc
*/
protected function setUp()
{
$this->customerRepository = $this
->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
->getMockForAbstractClass();
$this->customerRegistry = $this->getMockBuilder(\Magento\Customer\Model\CustomerRegistry::class)
->disableOriginalConstructor()
->getMock();
$this->encryptorMock = $this->getMockBuilder(\Magento\Framework\Encryption\Encryptor::class)
->disableOriginalConstructor()
->getMock();
$this->model = new UpgradeCustomerPasswordObserver(
$this->encryptorMock,
$this->customerRegistry,
$this->customerRepository
);
}
/**
* Unit test for verifying customers password upgrade observer
*/
public function testUpgradeCustomerPassword()
{
$customerId = '1';
$password = 'password';
$passwordHash = 'hash:salt:999';
$model = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMock();
$customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
->setMethods(['setData'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
->disableOriginalConstructor()
->setMethods(['getPasswordHash', 'setPasswordHash'])
->getMock();
$model->expects($this->exactly(2))
->method('getId')
->willReturn($customerId);
$this->customerRepository->expects($this->once())
->method('getById')
->with($customerId)
->willReturn($customer);
$this->customerRegistry->expects($this->once())
->method('retrieveSecureData')
->with($customerId)
->willReturn($customerSecure);
$customerSecure->expects($this->once())
->method('getPasswordHash')
->willReturn($passwordHash);
$this->encryptorMock->expects($this->once())
->method('validateHashVersion')
->with($passwordHash)
->willReturn(false);
$this->encryptorMock->expects($this->once())
->method('getHash')
->with($password, true)
->willReturn($passwordHash);
$customerSecure->expects($this->once())
->method('setPasswordHash')
->with($passwordHash);
$this->customerRepository->expects($this->once())
->method('save')
->with($customer);
$event = new \Magento\Framework\DataObject();
$event->setData(['password' => 'password', 'model' => $model]);
$observerMock = new \Magento\Framework\Event\Observer();
$observerMock->setEvent($event);
$this->model->execute($observerMock);
}
}