Skip to content

Commit

Permalink
ENGCOM-6500: #14663 Updating Customer through rest/all/V1/customers/:…
Browse files Browse the repository at this point in the history
…id resets group_id if group_id not passed in payload #25958
  • Loading branch information
VladimirZaets authored Jan 2, 2020
2 parents 4e6f316 + e5c4bac commit b63a4cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Customer\Model\Data\CustomerSecureFactory;
use Magento\Customer\Model\Delegation\Data\NewOperation;
use Magento\Customer\Model\Delegation\Storage as DelegatedStorage;
use Magento\Customer\Model\ResourceModel\Customer\Collection;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
Expand All @@ -31,6 +32,8 @@
/**
* Customer repository.
*
* CRUD operations for customer entity
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
Expand Down Expand Up @@ -187,8 +190,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
{
/** @var NewOperation|null $delegatedNewOperation */
$delegatedNewOperation = !$customer->getId() ? $this->delegatedStorage->consumeNewOperation() : null;
$prevCustomerData = null;
$prevCustomerDataArr = null;
$prevCustomerData = $prevCustomerDataArr = null;
if ($customer->getId()) {
$prevCustomerData = $this->getById($customer->getId());
$prevCustomerDataArr = $prevCustomerData->__toArray();
Expand All @@ -214,6 +216,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
$prevCustomerData ? $prevCustomerData->getStoreId() : $this->storeManager->getStore()->getId()
);
}
$this->setCustomerGroupId($customerModel, $customerArr, $prevCustomerDataArr);
// Need to use attribute set or future updates can cause data loss
if (!$customerModel->getAttributeSetId()) {
$customerModel->setAttributeSetId(CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
Expand Down Expand Up @@ -452,4 +455,18 @@ private function setValidationFlag($customerArray, $customerModel)
$customerModel->setData('ignore_validation_flag', true);
}
}

/**
* Set customer group id
*
* @param Customer $customerModel
* @param array $customerArr
* @param array $prevCustomerDataArr
*/
private function setCustomerGroupId($customerModel, $customerArr, $prevCustomerDataArr)
{
if (!isset($customerArr['group_id']) && $prevCustomerDataArr && isset($prevCustomerDataArr['group_id'])) {
$customerModel->setGroupId($prevCustomerDataArr['group_id']);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public function testSave()
'setFirstFailure',
'setLockExpires',
'save',
'setGroupId'
]
);

Expand Down Expand Up @@ -245,9 +246,15 @@ public function testSave()
$this->customer->expects($this->atLeastOnce())
->method('getId')
->willReturn($customerId);
$this->customer->expects($this->atLeastOnce())
$this->customer->expects($this->at(4))
->method('__toArray')
->willReturn([]);
$this->customer->expects($this->at(3))
->method('__toArray')
->willReturn(['group_id' => 1]);
$customerModel->expects($this->once())
->method('setGroupId')
->with(1);
$this->customerRegistry->expects($this->atLeastOnce())
->method('retrieve')
->with($customerId)
Expand Down

0 comments on commit b63a4cd

Please sign in to comment.