Skip to content

Commit 6547fab

Browse files
committed
MAGETWO-61907: [Backport] - Updating customer via REST API without address unsets default billing and default shipping address - for 2.1
1 parent 0cee64b commit 6547fab

File tree

2 files changed

+69
-34
lines changed

2 files changed

+69
-34
lines changed

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

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
/**
1616
* Customer repository.
1717
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1918
*/
2019
class CustomerRepository implements \Magento\Customer\Api\CustomerRepositoryInterface
2120
{
@@ -132,8 +131,6 @@ public function __construct(
132131

133132
/**
134133
* {@inheritdoc}
135-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
136-
* @SuppressWarnings(PHPMD.NPathComplexity)
137134
*/
138135
public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null)
139136
{
@@ -218,32 +215,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
218215
$this->customerRegistry->push($customerModel);
219216
$customerId = $customerModel->getId();
220217

221-
if ($customer->getAddresses() !== null) {
222-
if ($customer->getId()) {
223-
$existingAddresses = $this->getById($customer->getId())->getAddresses();
224-
$getIdFunc = function ($address) {
225-
return $address->getId();
226-
};
227-
$existingAddressIds = array_map($getIdFunc, $existingAddresses);
228-
} else {
229-
$existingAddressIds = [];
230-
}
231-
232-
$savedAddressIds = [];
233-
foreach ($customer->getAddresses() as $address) {
234-
$address->setCustomerId($customerId)
235-
->setRegion($address->getRegion());
236-
$this->addressRepository->save($address);
237-
if ($address->getId()) {
238-
$savedAddressIds[] = $address->getId();
239-
}
240-
}
241-
242-
$addressIdsToDelete = array_diff($existingAddressIds, $savedAddressIds);
243-
foreach ($addressIdsToDelete as $addressId) {
244-
$this->addressRepository->deleteById($addressId);
245-
}
246-
}
218+
$this->updateAddresses($customer, $customerId);
247219

248220
$savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId());
249221
$this->eventManager->dispatch(
@@ -363,4 +335,42 @@ protected function addFilterGroupToCollection(
363335
$collection->addFieldToFilter($fields);
364336
}
365337
}
338+
339+
/**
340+
* Update customer addresses.
341+
*
342+
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
343+
* @param $customerId
344+
* @return void
345+
* @throws \Magento\Framework\Exception\InputException
346+
*/
347+
private function updateAddresses(\Magento\Customer\Api\Data\CustomerInterface $customer, $customerId)
348+
{
349+
if ($customer->getAddresses() !== null) {
350+
if ($customer->getId()) {
351+
$existingAddresses = $this->getById($customer->getId())->getAddresses();
352+
$getIdFunc = function ($address) {
353+
return $address->getId();
354+
};
355+
$existingAddressIds = array_map($getIdFunc, $existingAddresses);
356+
} else {
357+
$existingAddressIds = [];
358+
}
359+
360+
$savedAddressIds = [];
361+
foreach ($customer->getAddresses() as $address) {
362+
$address->setCustomerId($customerId)
363+
->setRegion($address->getRegion());
364+
$this->addressRepository->save($address);
365+
if ($address->getId()) {
366+
$savedAddressIds[] = $address->getId();
367+
}
368+
}
369+
370+
$addressIdsToDelete = array_diff($existingAddressIds, $savedAddressIds);
371+
foreach ($addressIdsToDelete as $addressId) {
372+
$this->addressRepository->deleteById($addressId);
373+
}
374+
}
375+
}
366376
}

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ protected function tearDown()
7676
}
7777

7878
/**
79+
* Test create new customer new then update first name.
80+
*
7981
* @magentoDbIsolation enabled
8082
*/
8183
public function testCreateCustomerNewThenUpdateFirstName()
@@ -110,6 +112,8 @@ public function testCreateCustomerNewThenUpdateFirstName()
110112
}
111113

112114
/**
115+
* Test create new customer.
116+
*
113117
* @magentoDbIsolation enabled
114118
*/
115119
public function testCreateNewCustomer()
@@ -137,6 +141,8 @@ public function testCreateNewCustomer()
137141
}
138142

139143
/**
144+
* Test update customer.
145+
*
140146
* @dataProvider updateCustomerDataProvider
141147
* @magentoAppArea frontend
142148
* @magentoDataFixture Magento/Customer/_files/customer.php
@@ -212,6 +218,8 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
212218
}
213219

214220
/**
221+
* Test update customer address.
222+
*
215223
* @magentoAppArea frontend
216224
* @magentoDataFixture Magento/Customer/_files/customer.php
217225
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -253,6 +261,8 @@ public function testUpdateCustomerAddress()
253261
}
254262

255263
/**
264+
* Test update customer preserve all addresses.
265+
*
256266
* @magentoAppArea frontend
257267
* @magentoDataFixture Magento/Customer/_files/customer.php
258268
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -278,6 +288,8 @@ public function testUpdateCustomerPreserveAllAddresses()
278288
}
279289

280290
/**
291+
* Test update customer delete all addresses with empty array.
292+
*
281293
* @magentoAppArea frontend
282294
* @magentoDataFixture Magento/Customer/_files/customer.php
283295
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -303,6 +315,8 @@ public function testUpdateCustomerDeleteAllAddressesWithEmptyArray()
303315
}
304316

305317
/**
318+
* Test search customers.
319+
*
306320
* @param \Magento\Framework\Api\Filter[] $filters
307321
* @param \Magento\Framework\Api\Filter[] $filterGroup
308322
* @param array $expectedResult array of expected results indexed by ID
@@ -336,7 +350,7 @@ public function testSearchCustomers($filters, $filterGroup, $expectedResult)
336350
}
337351

338352
/**
339-
* Test ordering
353+
* Test search customers order.
340354
*
341355
* @magentoDataFixture Magento/Customer/_files/three_customers.php
342356
* @magentoDbIsolation enabled
@@ -380,6 +394,8 @@ public function testSearchCustomersOrder()
380394
}
381395

382396
/**
397+
* Test delete.
398+
*
383399
* @magentoAppArea adminhtml
384400
* @magentoDataFixture Magento/Customer/_files/customer.php
385401
* @magentoAppIsolation enabled
@@ -398,6 +414,8 @@ public function testDelete()
398414
}
399415

400416
/**
417+
* Test delete by id.
418+
*
401419
* @magentoAppArea adminhtml
402420
* @magentoDataFixture Magento/Customer/_files/customer.php
403421
* @magentoAppIsolation enabled
@@ -416,7 +434,7 @@ public function testDeleteById()
416434
}
417435

418436
/**
419-
* DataProvider update customer
437+
* DataProvider update customer.
420438
*
421439
* @return array
422440
*/
@@ -434,6 +452,11 @@ public function updateCustomerDataProvider()
434452
];
435453
}
436454

455+
/**
456+
* DataProvider search customers.
457+
*
458+
* @return array
459+
*/
437460
public function searchCustomersDataProvider()
438461
{
439462
$builder = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\FilterBuilder::class);
@@ -474,7 +497,7 @@ public function searchCustomersDataProvider()
474497
}
475498

476499
/**
477-
* Check defaults billing and shipping in customer model
500+
* Check default shippings in customer model.
478501
*
479502
* @param $customerId
480503
* @param $defaultBilling
@@ -504,6 +527,8 @@ protected function expectedDefaultShippingsInCustomerModelAttributes(
504527
}
505528

506529
/**
530+
* Test update default shipping and default billing address.
531+
*
507532
* @magentoDataFixture Magento/Customer/_files/customer.php
508533
* @magentoDbIsolation enabled
509534
*/
@@ -531,13 +556,13 @@ public function testUpdateDefaultShippingAndDefaultBillingTest()
531556
$this->assertEquals(
532557
$savedCustomer->getDefaultBilling(),
533558
$oldDefaultBilling,
534-
'Default billing shoud not be overridden'
559+
'Default billing should not be overridden'
535560
);
536561

537562
$this->assertEquals(
538563
$savedCustomer->getDefaultShipping(),
539564
$oldDefaultShipping,
540-
'Default shipping shoud not be overridden'
565+
'Default shipping should not be overridden'
541566
);
542567
}
543568
}

0 commit comments

Comments
 (0)