Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpgradeCustomerPasswordObserverTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class UpgradeCustomerPasswordObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
20  protected $encryptorMock;
21 
26 
30  protected $customerRegistry;
31 
32  protected function setUp()
33  {
34  $this->customerRepository = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
35  ->getMockForAbstractClass();
36  $this->customerRegistry = $this->getMockBuilder(\Magento\Customer\Model\CustomerRegistry::class)
37  ->disableOriginalConstructor()
38  ->getMock();
39  $this->encryptorMock = $this->getMockBuilder(\Magento\Framework\Encryption\Encryptor::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42 
43  $this->model = new UpgradeCustomerPasswordObserver(
44  $this->encryptorMock,
45  $this->customerRegistry,
46  $this->customerRepository
47  );
48  }
49 
50  public function testUpgradeCustomerPassword()
51  {
52  $customerId = '1';
53  $password = 'password';
54  $passwordHash = 'hash:salt:999';
55  $model = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
56  ->disableOriginalConstructor()
57  ->setMethods(['getId'])
58  ->getMock();
59  $customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
60  ->getMockForAbstractClass();
61  $customerSecure = $this->getMockBuilder(\Magento\Customer\Model\Data\CustomerSecure::class)
62  ->disableOriginalConstructor()
63  ->setMethods(['getPasswordHash', 'setPasswordHash'])
64  ->getMock();
65  $model->expects($this->exactly(2))
66  ->method('getId')
67  ->willReturn($customerId);
68  $this->customerRepository->expects($this->once())
69  ->method('getById')
70  ->with($customerId)
71  ->willReturn($customer);
72  $this->customerRegistry->expects($this->once())
73  ->method('retrieveSecureData')
74  ->with($customerId)
75  ->willReturn($customerSecure);
76  $customerSecure->expects($this->once())
77  ->method('getPasswordHash')
78  ->willReturn($passwordHash);
79  $this->encryptorMock->expects($this->once())
80  ->method('validateHashVersion')
81  ->with($passwordHash)
82  ->willReturn(false);
83  $this->encryptorMock->expects($this->once())
84  ->method('getHash')
85  ->with($password, true)
86  ->willReturn($passwordHash);
87  $customerSecure->expects($this->once())
88  ->method('setPasswordHash')
89  ->with($passwordHash);
90  $this->customerRepository->expects($this->once())
91  ->method('save')
92  ->with($customer);
93  $event = new \Magento\Framework\DataObject();
94  $event->setData(['password' => 'password', 'model' => $model]);
95  $observerMock = new \Magento\Framework\Event\Observer();
96  $observerMock->setEvent($event);
97  $this->model->execute($observerMock);
98  }
99 }
$customer
Definition: customers.php:11