Skip to content

Commit 16731a0

Browse files
MAGETWO-96007: Customer address issue when creating or updating via API
1 parent 2f88c6b commit 16731a0

File tree

1 file changed

+114
-47
lines changed

1 file changed

+114
-47
lines changed

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

Lines changed: 114 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,24 @@
88

99
use Magento\Customer\Api\AccountManagementInterface;
1010
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
12+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
13+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
14+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
15+
use Magento\Framework\Api\DataObjectHelper;
16+
use Magento\Framework\Encryption\EncryptorInterface;
17+
use Magento\Customer\Api\Data\CustomerInterface;
18+
use Magento\Customer\Model\CustomerRegistry;
1119
use Magento\Framework\Api\SortOrder;
20+
use Magento\Framework\Config\CacheInterface;
21+
use Magento\Framework\ObjectManagerInterface;
1222
use Magento\TestFramework\Helper\Bootstrap;
23+
use Magento\Customer\Api\Data\AddressInterface;
24+
use Magento\Framework\Api\SearchCriteriaBuilder;
25+
use Magento\Framework\Api\FilterBuilder;
26+
use Magento\Framework\Api\SortOrderBuilder;
27+
use Magento\Framework\Exception\NoSuchEntityException;
28+
use Magento\Customer\Model\Customer;
1329

1430
/**
1531
* Checks Customer insert, update, search with repository
@@ -24,60 +40,65 @@ class CustomerRepositoryTest extends \PHPUnit\Framework\TestCase
2440
/** @var CustomerRepositoryInterface */
2541
private $customerRepository;
2642

27-
/** @var \Magento\Framework\ObjectManagerInterface */
43+
/** @var ObjectManagerInterface */
2844
private $objectManager;
2945

30-
/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory */
46+
/** @var CustomerInterfaceFactory */
3147
private $customerFactory;
3248

33-
/** @var \Magento\Customer\Api\Data\AddressInterfaceFactory */
49+
/** @var AddressInterfaceFactory */
3450
private $addressFactory;
3551

36-
/** @var \Magento\Customer\Api\Data\RegionInterfaceFactory */
52+
/** @var RegionInterfaceFactory */
3753
private $regionFactory;
3854

39-
/** @var \Magento\Framework\Api\ExtensibleDataObjectConverter */
55+
/** @var ExtensibleDataObjectConverter */
4056
private $converter;
4157

42-
/** @var \Magento\Framework\Api\DataObjectHelper */
58+
/** @var DataObjectHelper */
4359
protected $dataObjectHelper;
4460

45-
/** @var \Magento\Framework\Encryption\EncryptorInterface */
61+
/** @var EncryptorInterface */
4662
protected $encryptor;
4763

48-
/** @var \Magento\Customer\Model\CustomerRegistry */
64+
/** @var CustomerRegistry */
4965
protected $customerRegistry;
5066

67+
/**
68+
* @inheritdoc
69+
*/
5170
protected function setUp()
5271
{
5372
$this->objectManager = Bootstrap::getObjectManager();
54-
$this->customerRepository =
55-
$this->objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
56-
$this->customerFactory =
57-
$this->objectManager->create(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class);
58-
$this->addressFactory = $this->objectManager->create(\Magento\Customer\Api\Data\AddressInterfaceFactory::class);
59-
$this->regionFactory = $this->objectManager->create(\Magento\Customer\Api\Data\RegionInterfaceFactory::class);
60-
$this->accountManagement =
61-
$this->objectManager->create(\Magento\Customer\Api\AccountManagementInterface::class);
62-
$this->converter = $this->objectManager->create(\Magento\Framework\Api\ExtensibleDataObjectConverter::class);
63-
$this->dataObjectHelper = $this->objectManager->create(\Magento\Framework\Api\DataObjectHelper::class);
64-
$this->encryptor = $this->objectManager->create(\Magento\Framework\Encryption\EncryptorInterface::class);
65-
$this->customerRegistry = $this->objectManager->create(\Magento\Customer\Model\CustomerRegistry::class);
66-
67-
/** @var \Magento\Framework\Config\CacheInterface $cache */
68-
$cache = $this->objectManager->create(\Magento\Framework\Config\CacheInterface::class);
73+
$this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
74+
$this->customerFactory = $this->objectManager->create(CustomerInterfaceFactory::class);
75+
$this->addressFactory = $this->objectManager->create(AddressInterfaceFactory::class);
76+
$this->regionFactory = $this->objectManager->create(RegionInterfaceFactory::class);
77+
$this->accountManagement = $this->objectManager->create(AccountManagementInterface::class);
78+
$this->converter = $this->objectManager->create(ExtensibleDataObjectConverter::class);
79+
$this->dataObjectHelper = $this->objectManager->create(DataObjectHelper::class);
80+
$this->encryptor = $this->objectManager->create(EncryptorInterface::class);
81+
$this->customerRegistry = $this->objectManager->create(CustomerRegistry::class);
82+
83+
/** @var CacheInterface $cache */
84+
$cache = $this->objectManager->create(CacheInterface::class);
6985
$cache->remove('extension_attributes_config');
7086
}
7187

88+
/**
89+
* @inheritdoc
90+
*/
7291
protected function tearDown()
7392
{
74-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
93+
$objectManager = Bootstrap::getObjectManager();
7594
/** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
76-
$customerRegistry = $objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
95+
$customerRegistry = $objectManager->get(CustomerRegistry::class);
7796
$customerRegistry->remove(1);
7897
}
7998

8099
/**
100+
* Check if first name update was successful
101+
*
81102
* @magentoDbIsolation enabled
82103
*/
83104
public function testCreateCustomerNewThenUpdateFirstName()
@@ -99,7 +120,7 @@ public function testCreateCustomerNewThenUpdateFirstName()
99120
$newCustomerFirstname = 'New First Name';
100121
$updatedCustomer = $this->customerFactory->create();
101122
$this->dataObjectHelper->mergeDataObjects(
102-
\Magento\Customer\Api\Data\CustomerInterface::class,
123+
CustomerInterface::class,
103124
$updatedCustomer,
104125
$customer
105126
);
@@ -112,6 +133,8 @@ public function testCreateCustomerNewThenUpdateFirstName()
112133
}
113134

114135
/**
136+
* Test create new customer
137+
*
115138
* @magentoDbIsolation enabled
116139
*/
117140
public function testCreateNewCustomer()
@@ -139,6 +162,8 @@ public function testCreateNewCustomer()
139162
}
140163

141164
/**
165+
* Test update customer
166+
*
142167
* @dataProvider updateCustomerDataProvider
143168
* @magentoAppArea frontend
144169
* @magentoDataFixture Magento/Customer/_files/customer.php
@@ -168,7 +193,7 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
168193
$this->dataObjectHelper->populateWithArray(
169194
$customerDetails,
170195
$customerData,
171-
\Magento\Customer\Api\Data\CustomerInterface::class
196+
CustomerInterface::class
172197
);
173198
$this->customerRepository->save($customerDetails, $newPasswordHash);
174199
$customerAfter = $this->customerRepository->getById($existingCustomerId);
@@ -187,12 +212,12 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
187212
$attributesBefore = $this->converter->toFlatArray(
188213
$customerBefore,
189214
[],
190-
\Magento\Customer\Api\Data\CustomerInterface::class
215+
CustomerInterface::class
191216
);
192217
$attributesAfter = $this->converter->toFlatArray(
193218
$customerAfter,
194219
[],
195-
\Magento\Customer\Api\Data\CustomerInterface::class
220+
CustomerInterface::class
196221
);
197222
// ignore 'updated_at'
198223
unset($attributesBefore['updated_at']);
@@ -215,6 +240,8 @@ public function testUpdateCustomer($defaultBilling, $defaultShipping)
215240
}
216241

217242
/**
243+
* Test update customer address
244+
*
218245
* @magentoAppArea frontend
219246
* @magentoDataFixture Magento/Customer/_files/customer.php
220247
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -233,14 +260,14 @@ public function testUpdateCustomerAddress()
233260
$this->dataObjectHelper->populateWithArray(
234261
$newAddressDataObject,
235262
$newAddress,
236-
\Magento\Customer\Api\Data\AddressInterface::class
263+
AddressInterface::class
237264
);
238265
$newAddressDataObject->setRegion($addresses[0]->getRegion());
239266
$newCustomerEntity = $this->customerFactory->create();
240267
$this->dataObjectHelper->populateWithArray(
241268
$newCustomerEntity,
242269
$customerDetails,
243-
\Magento\Customer\Api\Data\CustomerInterface::class
270+
CustomerInterface::class
244271
);
245272
$newCustomerEntity->setId($customerId)
246273
->setAddresses([$newAddressDataObject, $addresses[1]]);
@@ -256,6 +283,8 @@ public function testUpdateCustomerAddress()
256283
}
257284

258285
/**
286+
* Test preserve all addresses after customer update
287+
*
259288
* @magentoAppArea frontend
260289
* @magentoDataFixture Magento/Customer/_files/customer.php
261290
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -265,22 +294,37 @@ public function testUpdateCustomerPreserveAllAddresses()
265294
$customerId = 1;
266295
$customer = $this->customerRepository->getById($customerId);
267296
$customerDetails = $customer->__toArray();
297+
$defaultBilling = $customerDetails['default_billing'];
298+
$defaultShipping = $customerDetails['default_shipping'];
268299
$newCustomerEntity = $this->customerFactory->create();
269300
$this->dataObjectHelper->populateWithArray(
270301
$newCustomerEntity,
271302
$customerDetails,
272-
\Magento\Customer\Api\Data\CustomerInterface::class
303+
CustomerInterface::class
273304
);
274305
$newCustomerEntity->setId($customer->getId())
275306
->setAddresses(null);
276307
$this->customerRepository->save($newCustomerEntity);
277308

278309
$newCustomerDetails = $this->customerRepository->getById($customerId);
310+
$newCustomerArray = $newCustomerDetails->__toArray();
279311
//Verify that old addresses are still present
280312
$this->assertEquals(2, count($newCustomerDetails->getAddresses()));
313+
$this->assertEquals(
314+
$defaultBilling,
315+
$newCustomerArray['default_billing'],
316+
"Default billing invalid value"
317+
);
318+
$this->assertEquals(
319+
$defaultShipping,
320+
$newCustomerArray['default_shipping'],
321+
"Default shipping invalid value"
322+
);
281323
}
282324

283325
/**
326+
* Test update delete all addresses with empty arrays
327+
*
284328
* @magentoAppArea frontend
285329
* @magentoDataFixture Magento/Customer/_files/customer.php
286330
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
@@ -294,18 +338,31 @@ public function testUpdateCustomerDeleteAllAddressesWithEmptyArray()
294338
$this->dataObjectHelper->populateWithArray(
295339
$newCustomerEntity,
296340
$customerDetails,
297-
\Magento\Customer\Api\Data\CustomerInterface::class
341+
CustomerInterface::class
298342
);
299343
$newCustomerEntity->setId($customer->getId())
300344
->setAddresses([]);
301345
$this->customerRepository->save($newCustomerEntity);
302346

303347
$newCustomerDetails = $this->customerRepository->getById($customerId);
348+
$newCustomerArray = $newCustomerDetails->__toArray();
304349
//Verify that old addresses are removed
305350
$this->assertEquals(0, count($newCustomerDetails->getAddresses()));
351+
$this->assertEquals(
352+
$newCustomerArray['default_billing'],
353+
null,
354+
"Default billing invalid value"
355+
);
356+
$this->assertEquals(
357+
$newCustomerArray['default_shipping'],
358+
null,
359+
"Default shipping invalid value"
360+
);
306361
}
307362

308363
/**
364+
* Test search customers
365+
*
309366
* @param \Magento\Framework\Api\Filter[] $filters
310367
* @param \Magento\Framework\Api\Filter[] $filterGroup
311368
* @param array $expectedResult array of expected results indexed by ID
@@ -317,9 +374,8 @@ public function testUpdateCustomerDeleteAllAddressesWithEmptyArray()
317374
*/
318375
public function testSearchCustomers($filters, $filterGroup, $expectedResult)
319376
{
320-
/** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
321-
$searchBuilder = Bootstrap::getObjectManager()
322-
->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
377+
/** @var SearchCriteriaBuilder $searchBuilder */
378+
$searchBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
323379
foreach ($filters as $filter) {
324380
$searchBuilder->addFilters([$filter]);
325381
}
@@ -346,19 +402,19 @@ public function testSearchCustomers($filters, $filterGroup, $expectedResult)
346402
*/
347403
public function testSearchCustomersOrder()
348404
{
349-
/** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
405+
/** @var SearchCriteriaBuilder $searchBuilder */
350406
$objectManager = Bootstrap::getObjectManager();
351-
$searchBuilder = $objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
407+
$searchBuilder = $objectManager->create(SearchCriteriaBuilder::class);
352408

353409
// Filter for 'firstname' like 'First'
354-
$filterBuilder = $objectManager->create(\Magento\Framework\Api\FilterBuilder::class);
410+
$filterBuilder = $objectManager->create(FilterBuilder::class);
355411
$firstnameFilter = $filterBuilder->setField('firstname')
356412
->setConditionType('like')
357413
->setValue('First%')
358414
->create();
359415
$searchBuilder->addFilters([$firstnameFilter]);
360416
// Search ascending order
361-
$sortOrderBuilder = $objectManager->create(\Magento\Framework\Api\SortOrderBuilder::class);
417+
$sortOrderBuilder = $objectManager->create(SortOrderBuilder::class);
362418
$sortOrder = $sortOrderBuilder
363419
->setField('lastname')
364420
->setDirection(SortOrder::SORT_ASC)
@@ -383,6 +439,8 @@ public function testSearchCustomersOrder()
383439
}
384440

385441
/**
442+
* Test delete
443+
*
386444
* @magentoAppArea adminhtml
387445
* @magentoDataFixture Magento/Customer/_files/customer.php
388446
* @magentoAppIsolation enabled
@@ -393,12 +451,14 @@ public function testDelete()
393451
$customer = $this->customerRepository->get($fixtureCustomerEmail);
394452
$this->customerRepository->delete($customer);
395453
/** Ensure that customer was deleted */
396-
$this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
454+
$this->expectException(NoSuchEntityException::class);
397455
$this->expectExceptionMessage('No such entity with email = customer@example.com, websiteId = 1');
398456
$this->customerRepository->get($fixtureCustomerEmail);
399457
}
400458

401459
/**
460+
* Test delete by id
461+
*
402462
* @magentoAppArea adminhtml
403463
* @magentoDataFixture Magento/Customer/_files/customer.php
404464
* @magentoAppIsolation enabled
@@ -409,7 +469,7 @@ public function testDeleteById()
409469
$fixtureCustomerId = 1;
410470
$this->customerRepository->deleteById($fixtureCustomerId);
411471
/** Ensure that customer was deleted */
412-
$this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
472+
$this->expectException(NoSuchEntityException::class);
413473
$this->expectExceptionMessage('No such entity with email = customer@example.com, websiteId = 1');
414474
$this->customerRepository->get($fixtureCustomerEmail);
415475
}
@@ -433,9 +493,14 @@ public function updateCustomerDataProvider()
433493
];
434494
}
435495

496+
/**
497+
* Search customer data provider
498+
*
499+
* @return array
500+
*/
436501
public function searchCustomersDataProvider()
437502
{
438-
$builder = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\FilterBuilder::class);
503+
$builder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
439504
return [
440505
'Customer with specific email' => [
441506
[$builder->setField('email')->setValue('customer@search.example.com')->create()],
@@ -485,9 +550,9 @@ protected function expectedDefaultShippingsInCustomerModelAttributes(
485550
$defaultShipping
486551
) {
487552
/**
488-
* @var \Magento\Customer\Model\Customer $customer
553+
* @var Customer $customer
489554
*/
490-
$customer = $this->objectManager->create(\Magento\Customer\Model\Customer::class);
555+
$customer = $this->objectManager->create(Customer::class);
491556
/** @var \Magento\Customer\Model\Customer $customer */
492557
$customer->load($customerId);
493558
$this->assertEquals(
@@ -503,6 +568,8 @@ protected function expectedDefaultShippingsInCustomerModelAttributes(
503568
}
504569

505570
/**
571+
* Test update default shipping and default billing address
572+
*
506573
* @magentoDataFixture Magento/Customer/_files/customer.php
507574
* @magentoDbIsolation enabled
508575
*/
@@ -530,13 +597,13 @@ public function testUpdateDefaultShippingAndDefaultBillingTest()
530597
$this->assertEquals(
531598
$savedCustomer->getDefaultBilling(),
532599
$oldDefaultBilling,
533-
'Default billing shoud not be overridden'
600+
'Default billing should not be overridden'
534601
);
535602

536603
$this->assertEquals(
537604
$savedCustomer->getDefaultShipping(),
538605
$oldDefaultShipping,
539-
'Default shipping shoud not be overridden'
606+
'Default shipping should not be overridden'
540607
);
541608
}
542609
}

0 commit comments

Comments
 (0)