Skip to content

Commit 40b9d02

Browse files
committed
MAGETWO-59871: [GITHUB] Updating customer via REST API without address unsets default billing and default shipping address #7380
1 parent f3fb417 commit 40b9d02

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,15 @@ public function __construct(
145145
public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null)
146146
{
147147
$prevCustomerData = null;
148+
$prevCustomerDataArr = null;
149+
148150
if ($customer->getId()) {
149151
$prevCustomerData = $this->getById($customer->getId());
152+
$prevCustomerDataArr = $prevCustomerData->__toArray();
150153
}
154+
155+
/** @var $customer \Magento\Customer\Model\Data\Customer */
156+
$customerArr = $customer->__toArray();
151157
$customer = $this->imageProcessor->save(
152158
$customer,
153159
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
@@ -185,6 +191,21 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
185191
$customerModel->setRpToken(null);
186192
$customerModel->setRpTokenCreatedAt(null);
187193
}
194+
195+
if (!array_key_exists('default_billing', $customerArr) &&
196+
null !== $prevCustomerDataArr &&
197+
array_key_exists('default_billing', $prevCustomerDataArr)
198+
) {
199+
$customerModel->setDefaultBilling($prevCustomerDataArr['default_billing']);
200+
}
201+
202+
if (!array_key_exists('default_shipping', $customerArr) &&
203+
null !== $prevCustomerDataArr &&
204+
array_key_exists('default_shipping', $prevCustomerDataArr)
205+
) {
206+
$customerModel->setDefaultShipping($prevCustomerDataArr['default_shipping']);
207+
}
208+
188209
$customerModel->save();
189210
$this->customerRegistry->push($customerModel);
190211
$customerId = $customerModel->getId();

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,14 @@ protected function setUp()
172172
\Magento\Customer\Api\Data\CustomerInterface::class,
173173
[],
174174
'',
175-
false
175+
true,
176+
true,
177+
true,
178+
[
179+
'__toArray'
180+
]
176181
);
182+
177183
$this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
178184
->getMock();
179185

@@ -254,6 +260,11 @@ public function testSave()
254260
'',
255261
false
256262
);
263+
264+
$this->customer->expects($this->atLeastOnce())
265+
->method('__toArray')
266+
->willReturn(['default_billing', 'default_shipping']);
267+
257268
$customerAttributesMetaData = $this->getMockForAbstractClass(
258269
\Magento\Framework\Api\CustomAttributesDataInterface::class,
259270
[],
@@ -495,6 +506,11 @@ public function testSaveWithPasswordHash()
495506
'getId'
496507
]
497508
);
509+
510+
$this->customer->expects($this->atLeastOnce())
511+
->method('__toArray')
512+
->willReturn(['default_billing', 'default_shipping']);
513+
498514
$customerModel = $this->getMock(
499515
\Magento\Customer\Model\Customer::class,
500516
[

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,42 @@ protected function expectedDefaultShippingsInCustomerModelAttributes(
505505
'default_shipping customer attribute did not updated'
506506
);
507507
}
508+
509+
/**
510+
* @magentoDataFixture Magento/Customer/_files/customer.php
511+
* @magentoDbIsolation enabled
512+
*/
513+
public function testUpdateDefaultShippingAndDefaultBillingTest()
514+
{
515+
$customerId = 1;
516+
$customerData = [
517+
"id" => 1,
518+
"website_id" => 1,
519+
"email" => "[email protected]",
520+
"firstname" => "1111",
521+
"lastname" => "Boss",
522+
"middlename" => null,
523+
"gender" => 0
524+
];
525+
526+
$customerEntity = $this->customerFactory->create(['data' => $customerData]);
527+
528+
$customer = $this->customerRepository->getById($customerId);
529+
$oldDefaultBilling = $customer->getDefaultBilling();
530+
$oldDefaultShipping = $customer->getDefaultShipping();
531+
532+
$savedCustomer = $this->customerRepository->save($customerEntity);
533+
534+
$this->assertEquals(
535+
$savedCustomer->getDefaultBilling(),
536+
$oldDefaultBilling,
537+
'Default billing shoud not be overridden'
538+
);
539+
540+
$this->assertEquals(
541+
$savedCustomer->getDefaultShipping(),
542+
$oldDefaultShipping,
543+
'Default shipping shoud not be overridden'
544+
);
545+
}
508546
}

0 commit comments

Comments
 (0)