CustomerRepositoryTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\ResourceModel;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  10. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  11. use Magento\Customer\Api\Data\RegionInterfaceFactory;
  12. use Magento\Framework\Api\ExtensibleDataObjectConverter;
  13. use Magento\Framework\Api\DataObjectHelper;
  14. use Magento\Framework\Encryption\EncryptorInterface;
  15. use Magento\Customer\Api\Data\CustomerInterface;
  16. use Magento\Customer\Model\CustomerRegistry;
  17. use Magento\Framework\Api\SortOrder;
  18. use Magento\Framework\Config\CacheInterface;
  19. use Magento\Framework\ObjectManagerInterface;
  20. use Magento\TestFramework\Helper\Bootstrap;
  21. use Magento\Customer\Api\Data\AddressInterface;
  22. use Magento\Framework\Api\SearchCriteriaBuilder;
  23. use Magento\Framework\Api\FilterBuilder;
  24. use Magento\Framework\Api\SortOrderBuilder;
  25. use Magento\Framework\Exception\NoSuchEntityException;
  26. use Magento\Customer\Model\Customer;
  27. /**
  28. * Checks Customer insert, update, search with repository
  29. *
  30. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  31. */
  32. class CustomerRepositoryTest extends \PHPUnit\Framework\TestCase
  33. {
  34. /** @var AccountManagementInterface */
  35. private $accountManagement;
  36. /** @var CustomerRepositoryInterface */
  37. private $customerRepository;
  38. /** @var ObjectManagerInterface */
  39. private $objectManager;
  40. /** @var CustomerInterfaceFactory */
  41. private $customerFactory;
  42. /** @var AddressInterfaceFactory */
  43. private $addressFactory;
  44. /** @var RegionInterfaceFactory */
  45. private $regionFactory;
  46. /** @var ExtensibleDataObjectConverter */
  47. private $converter;
  48. /** @var DataObjectHelper */
  49. protected $dataObjectHelper;
  50. /** @var EncryptorInterface */
  51. protected $encryptor;
  52. /** @var CustomerRegistry */
  53. protected $customerRegistry;
  54. /**
  55. * @inheritdoc
  56. */
  57. protected function setUp()
  58. {
  59. $this->objectManager = Bootstrap::getObjectManager();
  60. $this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
  61. $this->customerFactory = $this->objectManager->create(CustomerInterfaceFactory::class);
  62. $this->addressFactory = $this->objectManager->create(AddressInterfaceFactory::class);
  63. $this->regionFactory = $this->objectManager->create(RegionInterfaceFactory::class);
  64. $this->accountManagement = $this->objectManager->create(AccountManagementInterface::class);
  65. $this->converter = $this->objectManager->create(ExtensibleDataObjectConverter::class);
  66. $this->dataObjectHelper = $this->objectManager->create(DataObjectHelper::class);
  67. $this->encryptor = $this->objectManager->create(EncryptorInterface::class);
  68. $this->customerRegistry = $this->objectManager->create(CustomerRegistry::class);
  69. /** @var CacheInterface $cache */
  70. $cache = $this->objectManager->create(CacheInterface::class);
  71. $cache->remove('extension_attributes_config');
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. protected function tearDown()
  77. {
  78. $objectManager = Bootstrap::getObjectManager();
  79. /** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
  80. $customerRegistry = $objectManager->get(CustomerRegistry::class);
  81. $customerRegistry->remove(1);
  82. }
  83. /**
  84. * Check if first name update was successful
  85. *
  86. * @magentoDbIsolation enabled
  87. */
  88. public function testCreateCustomerNewThenUpdateFirstName()
  89. {
  90. /** Create a new customer */
  91. $email = 'first_last@example.com';
  92. $storeId = 1;
  93. $firstname = 'Tester';
  94. $lastname = 'McTest';
  95. $groupId = 1;
  96. $newCustomerEntity = $this->customerFactory->create()
  97. ->setStoreId($storeId)
  98. ->setEmail($email)
  99. ->setFirstname($firstname)
  100. ->setLastname($lastname)
  101. ->setGroupId($groupId);
  102. $customer = $this->customerRepository->save($newCustomerEntity);
  103. /** Update customer */
  104. $newCustomerFirstname = 'New First Name';
  105. $updatedCustomer = $this->customerFactory->create();
  106. $this->dataObjectHelper->mergeDataObjects(
  107. CustomerInterface::class,
  108. $updatedCustomer,
  109. $customer
  110. );
  111. $updatedCustomer->setFirstname($newCustomerFirstname);
  112. $this->customerRepository->save($updatedCustomer);
  113. /** Check if update was successful */
  114. $customer = $this->customerRepository->get($customer->getEmail());
  115. $this->assertEquals($newCustomerFirstname, $customer->getFirstname());
  116. $this->assertEquals($lastname, $customer->getLastname());
  117. }
  118. /**
  119. * Test create new customer
  120. *
  121. * @magentoDbIsolation enabled
  122. */
  123. public function testCreateNewCustomer()
  124. {
  125. $email = 'email@example.com';
  126. $storeId = 1;
  127. $firstname = 'Tester';
  128. $lastname = 'McTest';
  129. $groupId = 1;
  130. $newCustomerEntity = $this->customerFactory->create()
  131. ->setStoreId($storeId)
  132. ->setEmail($email)
  133. ->setFirstname($firstname)
  134. ->setLastname($lastname)
  135. ->setGroupId($groupId);
  136. $savedCustomer = $this->customerRepository->save($newCustomerEntity);
  137. $this->assertNotNull($savedCustomer->getId());
  138. $this->assertEquals($email, $savedCustomer->getEmail());
  139. $this->assertEquals($storeId, $savedCustomer->getStoreId());
  140. $this->assertEquals($firstname, $savedCustomer->getFirstname());
  141. $this->assertEquals($lastname, $savedCustomer->getLastname());
  142. $this->assertEquals($groupId, $savedCustomer->getGroupId());
  143. $this->assertTrue(!$savedCustomer->getSuffix());
  144. }
  145. /**
  146. * Test update customer
  147. *
  148. * @dataProvider updateCustomerDataProvider
  149. * @magentoAppArea frontend
  150. * @magentoDataFixture Magento/Customer/_files/customer.php
  151. * @param int|null $defaultBilling
  152. * @param int|null $defaultShipping
  153. */
  154. public function testUpdateCustomer($defaultBilling, $defaultShipping)
  155. {
  156. $existingCustomerId = 1;
  157. $email = 'savecustomer@example.com';
  158. $firstName = 'Firstsave';
  159. $lastName = 'Lastsave';
  160. $newPassword = 'newPassword123';
  161. $newPasswordHash = $this->encryptor->getHash($newPassword, true);
  162. $customerBefore = $this->customerRepository->getById($existingCustomerId);
  163. $customerData = array_merge($customerBefore->__toArray(), [
  164. 'id' => 1,
  165. 'email' => $email,
  166. 'firstname' => $firstName,
  167. 'lastname' => $lastName,
  168. 'created_in' => 'Admin',
  169. 'password' => 'notsaved',
  170. 'default_billing' => $defaultBilling,
  171. 'default_shipping' => $defaultShipping
  172. ]);
  173. $customerDetails = $this->customerFactory->create();
  174. $this->dataObjectHelper->populateWithArray(
  175. $customerDetails,
  176. $customerData,
  177. CustomerInterface::class
  178. );
  179. $this->customerRepository->save($customerDetails, $newPasswordHash);
  180. $customerAfter = $this->customerRepository->getById($existingCustomerId);
  181. $this->assertEquals($email, $customerAfter->getEmail());
  182. $this->assertEquals($firstName, $customerAfter->getFirstname());
  183. $this->assertEquals($lastName, $customerAfter->getLastname());
  184. $this->assertEquals($defaultBilling, $customerAfter->getDefaultBilling());
  185. $this->assertEquals($defaultShipping, $customerAfter->getDefaultShipping());
  186. $this->expectedDefaultShippingsInCustomerModelAttributes(
  187. $existingCustomerId,
  188. $defaultBilling,
  189. $defaultShipping
  190. );
  191. $this->assertEquals('Admin', $customerAfter->getCreatedIn());
  192. $this->accountManagement->authenticate($customerAfter->getEmail(), $newPassword);
  193. $attributesBefore = $this->converter->toFlatArray(
  194. $customerBefore,
  195. [],
  196. CustomerInterface::class
  197. );
  198. $attributesAfter = $this->converter->toFlatArray(
  199. $customerAfter,
  200. [],
  201. CustomerInterface::class
  202. );
  203. // ignore 'updated_at'
  204. unset($attributesBefore['updated_at']);
  205. unset($attributesAfter['updated_at']);
  206. $inBeforeOnly = array_diff_assoc($attributesBefore, $attributesAfter);
  207. $inAfterOnly = array_diff_assoc($attributesAfter, $attributesBefore);
  208. $expectedInBefore = [
  209. 'firstname',
  210. 'lastname',
  211. 'email',
  212. ];
  213. foreach ($expectedInBefore as $key) {
  214. $this->assertContains($key, array_keys($inBeforeOnly));
  215. }
  216. $this->assertContains('created_in', array_keys($inAfterOnly));
  217. $this->assertContains('firstname', array_keys($inAfterOnly));
  218. $this->assertContains('lastname', array_keys($inAfterOnly));
  219. $this->assertContains('email', array_keys($inAfterOnly));
  220. $this->assertNotContains('password_hash', array_keys($inAfterOnly));
  221. }
  222. /**
  223. * Test update customer address
  224. *
  225. * @magentoAppArea frontend
  226. * @magentoDataFixture Magento/Customer/_files/customer.php
  227. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  228. */
  229. public function testUpdateCustomerAddress()
  230. {
  231. $customerId = 1;
  232. $city = 'San Jose';
  233. $email = 'customer@example.com';
  234. $customer = $this->customerRepository->getById($customerId);
  235. $customerDetails = $customer->__toArray();
  236. $addresses = $customer->getAddresses();
  237. $addressId = $addresses[0]->getId();
  238. $newAddress = array_merge($addresses[0]->__toArray(), ['city' => $city]);
  239. $newAddressDataObject = $this->addressFactory->create();
  240. $this->dataObjectHelper->populateWithArray(
  241. $newAddressDataObject,
  242. $newAddress,
  243. AddressInterface::class
  244. );
  245. $newAddressDataObject->setRegion($addresses[0]->getRegion());
  246. $newCustomerEntity = $this->customerFactory->create();
  247. $this->dataObjectHelper->populateWithArray(
  248. $newCustomerEntity,
  249. $customerDetails,
  250. CustomerInterface::class
  251. );
  252. $newCustomerEntity->setId($customerId)
  253. ->setAddresses([$newAddressDataObject, $addresses[1]]);
  254. $this->customerRepository->save($newCustomerEntity);
  255. $newCustomer = $this->customerRepository->get($email);
  256. $this->assertEquals(2, count($newCustomer->getAddresses()));
  257. foreach ($newCustomer->getAddresses() as $newAddress) {
  258. if ($newAddress->getId() == $addressId) {
  259. $this->assertEquals($city, $newAddress->getCity());
  260. }
  261. }
  262. }
  263. /**
  264. * Test preserve all addresses after customer update
  265. *
  266. * @magentoAppArea frontend
  267. * @magentoDataFixture Magento/Customer/_files/customer.php
  268. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  269. */
  270. public function testUpdateCustomerPreserveAllAddresses()
  271. {
  272. $customerId = 1;
  273. $customer = $this->customerRepository->getById($customerId);
  274. $customerDetails = $customer->__toArray();
  275. $newCustomerEntity = $this->customerFactory->create();
  276. $this->dataObjectHelper->populateWithArray(
  277. $newCustomerEntity,
  278. $customerDetails,
  279. CustomerInterface::class
  280. );
  281. $newCustomerEntity->setId($customer->getId())
  282. ->setAddresses(null);
  283. $this->customerRepository->save($newCustomerEntity);
  284. $newCustomerDetails = $this->customerRepository->getById($customerId);
  285. //Verify that old addresses are still present
  286. $this->assertEquals(2, count($newCustomerDetails->getAddresses()));
  287. }
  288. /**
  289. * Test update delete all addresses with empty arrays
  290. *
  291. * @magentoAppArea frontend
  292. * @magentoDataFixture Magento/Customer/_files/customer.php
  293. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  294. */
  295. public function testUpdateCustomerDeleteAllAddressesWithEmptyArray()
  296. {
  297. $customerId = 1;
  298. $customer = $this->customerRepository->getById($customerId);
  299. $customerDetails = $customer->__toArray();
  300. $newCustomerEntity = $this->customerFactory->create();
  301. $this->dataObjectHelper->populateWithArray(
  302. $newCustomerEntity,
  303. $customerDetails,
  304. CustomerInterface::class
  305. );
  306. $newCustomerEntity->setId($customer->getId())
  307. ->setAddresses([]);
  308. $this->customerRepository->save($newCustomerEntity);
  309. $newCustomerDetails = $this->customerRepository->getById($customerId);
  310. //Verify that old addresses are removed
  311. $this->assertEquals(0, count($newCustomerDetails->getAddresses()));
  312. }
  313. /**
  314. * Test customer update with new address
  315. *
  316. * @magentoAppArea frontend
  317. * @magentoDataFixture Magento/Customer/_files/customer.php
  318. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  319. */
  320. public function testUpdateCustomerWithNewAddress()
  321. {
  322. $customerId = 1;
  323. $customer = $this->customerRepository->getById($customerId);
  324. $customerDetails = $customer->__toArray();
  325. unset($customerDetails['default_billing']);
  326. unset($customerDetails['default_shipping']);
  327. $beforeSaveCustomer = $this->customerFactory->create();
  328. $this->dataObjectHelper->populateWithArray(
  329. $beforeSaveCustomer,
  330. $customerDetails,
  331. CustomerInterface::class
  332. );
  333. $addresses = $customer->getAddresses();
  334. $beforeSaveAddress = $addresses[0]->__toArray();
  335. unset($beforeSaveAddress['id']);
  336. $newAddressDataObject = $this->addressFactory->create();
  337. $this->dataObjectHelper->populateWithArray(
  338. $newAddressDataObject,
  339. $beforeSaveAddress,
  340. AddressInterface::class
  341. );
  342. $beforeSaveCustomer->setAddresses([$newAddressDataObject]);
  343. $this->customerRepository->save($beforeSaveCustomer);
  344. $newCustomer = $this->customerRepository->getById($customerId);
  345. $newCustomerAddresses = $newCustomer->getAddresses();
  346. $addressId = $newCustomerAddresses[0]->getId();
  347. $this->assertEquals($newCustomer->getDefaultBilling(), $addressId, "Default billing invalid value");
  348. $this->assertEquals($newCustomer->getDefaultShipping(), $addressId, "Default shipping invalid value");
  349. }
  350. /**
  351. * Test search customers
  352. *
  353. * @param \Magento\Framework\Api\Filter[] $filters
  354. * @param \Magento\Framework\Api\Filter[] $filterGroup
  355. * @param array $expectedResult array of expected results indexed by ID
  356. *
  357. * @dataProvider searchCustomersDataProvider
  358. *
  359. * @magentoDataFixture Magento/Customer/_files/three_customers.php
  360. * @magentoDbIsolation enabled
  361. */
  362. public function testSearchCustomers($filters, $filterGroup, $expectedResult)
  363. {
  364. /** @var SearchCriteriaBuilder $searchBuilder */
  365. $searchBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
  366. foreach ($filters as $filter) {
  367. $searchBuilder->addFilters([$filter]);
  368. }
  369. if ($filterGroup !== null) {
  370. $searchBuilder->addFilters($filterGroup);
  371. }
  372. $searchResults = $this->customerRepository->getList($searchBuilder->create());
  373. $this->assertEquals(count($expectedResult), $searchResults->getTotalCount());
  374. foreach ($searchResults->getItems() as $item) {
  375. $this->assertEquals($expectedResult[$item->getId()]['email'], $item->getEmail());
  376. $this->assertEquals($expectedResult[$item->getId()]['firstname'], $item->getFirstname());
  377. unset($expectedResult[$item->getId()]);
  378. }
  379. }
  380. /**
  381. * Test ordering
  382. *
  383. * @magentoDataFixture Magento/Customer/_files/three_customers.php
  384. * @magentoDbIsolation enabled
  385. */
  386. public function testSearchCustomersOrder()
  387. {
  388. /** @var SearchCriteriaBuilder $searchBuilder */
  389. $objectManager = Bootstrap::getObjectManager();
  390. $searchBuilder = $objectManager->create(SearchCriteriaBuilder::class);
  391. // Filter for 'firstname' like 'First'
  392. $filterBuilder = $objectManager->create(FilterBuilder::class);
  393. $firstnameFilter = $filterBuilder->setField('firstname')
  394. ->setConditionType('like')
  395. ->setValue('First%')
  396. ->create();
  397. $searchBuilder->addFilters([$firstnameFilter]);
  398. // Search ascending order
  399. $sortOrderBuilder = $objectManager->create(SortOrderBuilder::class);
  400. $sortOrder = $sortOrderBuilder
  401. ->setField('lastname')
  402. ->setDirection(SortOrder::SORT_ASC)
  403. ->create();
  404. $searchBuilder->addSortOrder($sortOrder);
  405. $searchResults = $this->customerRepository->getList($searchBuilder->create());
  406. $this->assertEquals(3, $searchResults->getTotalCount());
  407. $this->assertEquals('Lastname', $searchResults->getItems()[0]->getLastname());
  408. $this->assertEquals('Lastname2', $searchResults->getItems()[1]->getLastname());
  409. $this->assertEquals('Lastname3', $searchResults->getItems()[2]->getLastname());
  410. // Search descending order
  411. $sortOrder = $sortOrderBuilder
  412. ->setField('lastname')
  413. ->setDirection(SortOrder::SORT_DESC)
  414. ->create();
  415. $searchBuilder->addSortOrder($sortOrder);
  416. $searchResults = $this->customerRepository->getList($searchBuilder->create());
  417. $this->assertEquals('Lastname3', $searchResults->getItems()[0]->getLastname());
  418. $this->assertEquals('Lastname2', $searchResults->getItems()[1]->getLastname());
  419. $this->assertEquals('Lastname', $searchResults->getItems()[2]->getLastname());
  420. }
  421. /**
  422. * Test delete
  423. *
  424. * @magentoAppArea adminhtml
  425. * @magentoDataFixture Magento/Customer/_files/customer.php
  426. * @magentoAppIsolation enabled
  427. */
  428. public function testDelete()
  429. {
  430. $fixtureCustomerEmail = 'customer@example.com';
  431. $customer = $this->customerRepository->get($fixtureCustomerEmail);
  432. $this->customerRepository->delete($customer);
  433. /** Ensure that customer was deleted */
  434. $this->expectException(NoSuchEntityException::class);
  435. $this->expectExceptionMessage('No such entity with email = customer@example.com, websiteId = 1');
  436. $this->customerRepository->get($fixtureCustomerEmail);
  437. }
  438. /**
  439. * Test delete by id
  440. *
  441. * @magentoAppArea adminhtml
  442. * @magentoDataFixture Magento/Customer/_files/customer.php
  443. * @magentoAppIsolation enabled
  444. */
  445. public function testDeleteById()
  446. {
  447. $fixtureCustomerEmail = 'customer@example.com';
  448. $fixtureCustomerId = 1;
  449. $this->customerRepository->deleteById($fixtureCustomerId);
  450. /** Ensure that customer was deleted */
  451. $this->expectException(NoSuchEntityException::class);
  452. $this->expectExceptionMessage('No such entity with email = customer@example.com, websiteId = 1');
  453. $this->customerRepository->get($fixtureCustomerEmail);
  454. }
  455. /**
  456. * DataProvider update customer
  457. *
  458. * @return array
  459. */
  460. public function updateCustomerDataProvider()
  461. {
  462. return [
  463. 'Customer remove default shipping and billing' => [
  464. null,
  465. null
  466. ],
  467. 'Customer update default shipping and billing' => [
  468. 1,
  469. 1
  470. ],
  471. ];
  472. }
  473. /**
  474. * Search customer data provider
  475. *
  476. * @return array
  477. */
  478. public function searchCustomersDataProvider()
  479. {
  480. $builder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
  481. return [
  482. 'Customer with specific email' => [
  483. [$builder->setField('email')->setValue('customer@search.example.com')->create()],
  484. null,
  485. [1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname']],
  486. ],
  487. 'Customer with specific first name' => [
  488. [$builder->setField('firstname')->setValue('Firstname2')->create()],
  489. null,
  490. [2 => ['email' => 'customer2@search.example.com', 'firstname' => 'Firstname2']],
  491. ],
  492. 'Customers with either email' => [
  493. [],
  494. [
  495. $builder->setField('firstname')->setValue('Firstname')->create(),
  496. $builder->setField('firstname')->setValue('Firstname2')->create()
  497. ],
  498. [
  499. 1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname'],
  500. 2 => ['email' => 'customer2@search.example.com', 'firstname' => 'Firstname2']
  501. ],
  502. ],
  503. 'Customers created since' => [
  504. [
  505. $builder->setField('created_at')->setValue('2011-02-28 15:52:26')
  506. ->setConditionType('gt')->create(),
  507. ],
  508. [],
  509. [
  510. 1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname'],
  511. 3 => ['email' => 'customer3@search.example.com', 'firstname' => 'Firstname3']
  512. ],
  513. ]
  514. ];
  515. }
  516. /**
  517. * Check defaults billing and shipping in customer model
  518. *
  519. * @param $customerId
  520. * @param $defaultBilling
  521. * @param $defaultShipping
  522. */
  523. protected function expectedDefaultShippingsInCustomerModelAttributes(
  524. $customerId,
  525. $defaultBilling,
  526. $defaultShipping
  527. ) {
  528. /**
  529. * @var Customer $customer
  530. */
  531. $customer = $this->objectManager->create(Customer::class);
  532. /** @var \Magento\Customer\Model\Customer $customer */
  533. $customer->load($customerId);
  534. $this->assertEquals(
  535. $defaultBilling,
  536. $customer->getDefaultBilling(),
  537. 'default_billing customer attribute did not updated'
  538. );
  539. $this->assertEquals(
  540. $defaultShipping,
  541. $customer->getDefaultShipping(),
  542. 'default_shipping customer attribute did not updated'
  543. );
  544. }
  545. /**
  546. * Test update default shipping and default billing address
  547. *
  548. * @magentoDataFixture Magento/Customer/_files/customer.php
  549. * @magentoDbIsolation enabled
  550. */
  551. public function testUpdateDefaultShippingAndDefaultBillingTest()
  552. {
  553. $customerId = 1;
  554. $customerData = [
  555. "id" => 1,
  556. "website_id" => 1,
  557. "email" => "roni_cost@example.com",
  558. "firstname" => "1111",
  559. "lastname" => "Boss",
  560. "middlename" => null,
  561. "gender" => 0
  562. ];
  563. $customerEntity = $this->customerFactory->create(['data' => $customerData]);
  564. $customer = $this->customerRepository->getById($customerId);
  565. $oldDefaultBilling = $customer->getDefaultBilling();
  566. $oldDefaultShipping = $customer->getDefaultShipping();
  567. $savedCustomer = $this->customerRepository->save($customerEntity);
  568. $this->assertEquals(
  569. $savedCustomer->getDefaultBilling(),
  570. $oldDefaultBilling,
  571. 'Default billing should not be overridden'
  572. );
  573. $this->assertEquals(
  574. $savedCustomer->getDefaultShipping(),
  575. $oldDefaultShipping,
  576. 'Default shipping should not be overridden'
  577. );
  578. }
  579. }