UpgradeCustomerPasswordObserverTest.php 3.9 KB

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