Skip to content

Commit 80f3467

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-54615' into BUGS
2 parents bdd977d + 491a101 commit 80f3467

File tree

3 files changed

+97
-21
lines changed

3 files changed

+97
-21
lines changed

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

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
168168
\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER
169169
);
170170
}
171-
// Populate model with secure data
172-
if ($customer->getId()) {
173-
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
174-
$customerModel->setRpToken($customerSecure->getRpToken());
175-
$customerModel->setRpTokenCreatedAt($customerSecure->getRpTokenCreatedAt());
176-
$customerModel->setPasswordHash($customerSecure->getPasswordHash());
177-
$customerModel->setFailuresNum($customerSecure->getFailuresNum());
178-
$customerModel->setFirstFailure($customerSecure->getFirstFailure());
179-
$customerModel->setLockExpires($customerSecure->getLockExpires());
180-
} else {
181-
if ($passwordHash) {
182-
$customerModel->setPasswordHash($passwordHash);
183-
}
184-
}
171+
$this->populateCustomerWithSecureData($customerModel, $passwordHash);
185172

186173
// If customer email was changed, reset RpToken info
187174
if ($prevCustomerData
@@ -229,6 +216,34 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
229216
return $savedCustomer;
230217
}
231218

219+
/**
220+
* Set secure data to customer model
221+
*
222+
* @param \Magento\Customer\Model\Customer $customerModel
223+
* @param string|null $passwordHash
224+
* @return void
225+
*/
226+
private function populateCustomerWithSecureData($customerModel, $passwordHash = null)
227+
{
228+
if ($customerModel->getId()) {
229+
$customerSecure = $this->customerRegistry->retrieveSecureData($customerModel->getId());
230+
231+
$customerModel->setRpToken($passwordHash ? null : $customerSecure->getRpToken());
232+
$customerModel->setRpTokenCreatedAt($passwordHash ? null : $customerSecure->getRpTokenCreatedAt());
233+
$customerModel->setPasswordHash($passwordHash ?: $customerSecure->getPasswordHash());
234+
235+
$customerModel->setFailuresNum($customerSecure->getFailuresNum());
236+
$customerModel->setFirstFailure($customerSecure->getFirstFailure());
237+
$customerModel->setLockExpires($customerSecure->getLockExpires());
238+
} elseif ($passwordHash) {
239+
$customerModel->setPasswordHash($passwordHash);
240+
}
241+
242+
if ($passwordHash && $customerModel->getId()) {
243+
$this->customerRegistry->remove($customerModel->getId());
244+
}
245+
}
246+
232247
/**
233248
* {@inheritdoc}
234249
*/

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,20 @@ public function testSaveWithPasswordHash()
428428
$storeId = 2;
429429
$passwordHash = 'ukfa4sdfa56s5df02asdf4rt';
430430

431+
$customerSecureData = $this->getMock(
432+
'Magento\Customer\Model\Data\CustomerSecure',
433+
[
434+
'getRpToken',
435+
'getRpTokenCreatedAt',
436+
'getPasswordHash',
437+
'getFailuresNum',
438+
'getFirstFailure',
439+
'getLockExpires',
440+
],
441+
[],
442+
'',
443+
false
444+
);
431445
$region = $this->getMockForAbstractClass('Magento\Customer\Api\Data\RegionInterface', [], '', false);
432446
$address = $this->getMockForAbstractClass(
433447
'Magento\Customer\Api\Data\AddressInterface',
@@ -491,6 +505,42 @@ public function testSaveWithPasswordHash()
491505
'setAddresses'
492506
]
493507
);
508+
$customerModel->expects($this->atLeastOnce())
509+
->method('setRpToken')
510+
->with(null);
511+
$customerModel->expects($this->atLeastOnce())
512+
->method('setRpTokenCreatedAt')
513+
->with(null);
514+
$customerModel->expects($this->atLeastOnce())
515+
->method('setPasswordHash')
516+
->with($passwordHash);
517+
$this->customerRegistry->expects($this->once())
518+
->method('remove')
519+
->with($customerId);
520+
521+
$this->customerRegistry->expects($this->once())
522+
->method('retrieveSecureData')
523+
->with($customerId)
524+
->willReturn($customerSecureData);
525+
$customerSecureData->expects($this->never())
526+
->method('getRpToken')
527+
->willReturn('rpToken');
528+
$customerSecureData->expects($this->never())
529+
->method('getRpTokenCreatedAt')
530+
->willReturn('rpTokenCreatedAt');
531+
$customerSecureData->expects($this->never())
532+
->method('getPasswordHash')
533+
->willReturn('passwordHash');
534+
$customerSecureData->expects($this->once())
535+
->method('getFailuresNum')
536+
->willReturn('failuresNum');
537+
$customerSecureData->expects($this->once())
538+
->method('getFirstFailure')
539+
->willReturn('firstFailure');
540+
$customerSecureData->expects($this->once())
541+
->method('getLockExpires')
542+
->willReturn('lockExpires');
543+
494544
$this->customer->expects($this->atLeastOnce())
495545
->method('getId')
496546
->willReturn($customerId);
@@ -527,6 +577,10 @@ public function testSaveWithPasswordHash()
527577
$customerAttributesMetaData->expects($this->at(2))
528578
->method('setAddresses')
529579
->with([$address]);
580+
$customerAttributesMetaData
581+
->expects($this->atLeastOnce())
582+
->method('getId')
583+
->willReturn($customerId);
530584
$this->extensibleDataObjectConverter->expects($this->once())
531585
->method('toNestedArray')
532586
->with($customerAttributesMetaData, [], '\Magento\Customer\Api\Data\CustomerInterface')
@@ -551,16 +605,13 @@ public function testSaveWithPasswordHash()
551605
->with($storeId);
552606
$customerModel->expects($this->once())
553607
->method('setId')
554-
->with(null);
608+
->with($customerId);
555609
$customerModel->expects($this->once())
556610
->method('getAttributeSetId')
557611
->willReturn(null);
558612
$customerModel->expects($this->once())
559613
->method('setAttributeSetId')
560614
->with(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
561-
$customerModel->expects($this->once())
562-
->method('setPasswordHash')
563-
->with($passwordHash);
564615
$customerModel->expects($this->atLeastOnce())
565616
->method('getId')
566617
->willReturn($customerId);

dev/tests/integration/testsuite/Magento/Customer/Model/ResourceModel/CustomerRepositoryTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class CustomerRepositoryTest extends \PHPUnit_Framework_TestCase
4040
/** @var \Magento\Framework\Api\DataObjectHelper */
4141
protected $dataObjectHelper;
4242

43+
/** @var \Magento\Framework\Encryption\EncryptorInterface */
44+
protected $encryptor;
45+
46+
/** @var \Magento\Customer\Model\CustomerRegistry */
47+
protected $customerRegistry;
48+
4349
protected function setUp()
4450
{
4551
$this->objectManager = Bootstrap::getObjectManager();
@@ -50,6 +56,9 @@ protected function setUp()
5056
$this->accountManagement = $this->objectManager->create('Magento\Customer\Api\AccountManagementInterface');
5157
$this->converter = $this->objectManager->create('Magento\Framework\Api\ExtensibleDataObjectConverter');
5258
$this->dataObjectHelper = $this->objectManager->create('Magento\Framework\Api\DataObjectHelper');
59+
$this->encryptor = $this->objectManager->create(\Magento\Framework\Encryption\EncryptorInterface::class);
60+
$this->customerRegistry = $this->objectManager->create(\Magento\Customer\Model\CustomerRegistry::class);
61+
5362
/** @var \Magento\Framework\Config\CacheInterface $cache */
5463
$cache = $this->objectManager->create('Magento\Framework\Config\CacheInterface');
5564
$cache->remove('extension_attributes_config');
@@ -137,6 +146,8 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
137146
$email = 'savecustomer@example.com';
138147
$firstName = 'Firstsave';
139148
$lastName = 'Lastsave';
149+
$newPassword = 'newPassword123';
150+
$newPasswordHash = $this->encryptor->getHash($newPassword, true);
140151
$customerBefore = $this->customerRepository->getById($existingCustomerId);
141152
$customerData = array_merge($customerBefore->__toArray(), [
142153
'id' => 1,
@@ -154,7 +165,7 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
154165
$customerData,
155166
'\Magento\Customer\Api\Data\CustomerInterface'
156167
);
157-
$this->customerRepository->save($customerDetails);
168+
$this->customerRepository->save($customerDetails, $newPasswordHash);
158169
$customerAfter = $this->customerRepository->getById($existingCustomerId);
159170
$this->assertEquals($email, $customerAfter->getEmail());
160171
$this->assertEquals($firstName, $customerAfter->getFirstname());
@@ -167,8 +178,7 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
167178
$defaultShipping
168179
);
169180
$this->assertEquals('Admin', $customerAfter->getCreatedIn());
170-
$passwordFromFixture = 'password';
171-
$this->accountManagement->authenticate($customerAfter->getEmail(), $passwordFromFixture);
181+
$this->accountManagement->authenticate($customerAfter->getEmail(), $newPassword);
172182
$attributesBefore = $this->converter->toFlatArray(
173183
$customerBefore,
174184
[],

0 commit comments

Comments
 (0)