Skip to content

Commit 6654d85

Browse files
committed
MAGETWO-61908: [Backport] - Updating customer via REST API without address unsets default billing and default shipping address - for 2.0
1 parent 54d4e70 commit 6654d85

File tree

3 files changed

+141
-68
lines changed

3 files changed

+141
-68
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,13 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
142142
$this->validate($customer);
143143

144144
$prevCustomerData = null;
145+
$prevCustomerDataArr = null;
145146
if ($customer->getId()) {
146147
$prevCustomerData = $this->getById($customer->getId());
148+
$prevCustomerDataArr = $prevCustomerData->__toArray();
147149
}
150+
/** @var $customer \Magento\Customer\Model\Data\Customer */
151+
$customerArr = $customer->__toArray();
148152
$customer = $this->imageProcessor->save(
149153
$customer,
150154
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
@@ -156,7 +160,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
156160
$customerData = $this->extensibleDataObjectConverter->toNestedArray(
157161
$customer,
158162
[],
159-
'\Magento\Customer\Api\Data\CustomerInterface'
163+
\Magento\Customer\Api\Data\CustomerInterface::class
160164
);
161165

162166
$customer->setAddresses($origAddresses);
@@ -192,6 +196,21 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
192196
$customerModel->setRpToken(null);
193197
$customerModel->setRpTokenCreatedAt(null);
194198
}
199+
200+
if (!array_key_exists('default_billing', $customerArr) &&
201+
null !== $prevCustomerDataArr &&
202+
array_key_exists('default_billing', $prevCustomerDataArr)
203+
) {
204+
$customerModel->setDefaultBilling($prevCustomerDataArr['default_billing']);
205+
}
206+
207+
if (!array_key_exists('default_shipping', $customerArr) &&
208+
null !== $prevCustomerDataArr &&
209+
array_key_exists('default_shipping', $prevCustomerDataArr)
210+
) {
211+
$customerModel->setDefaultShipping($prevCustomerDataArr['default_shipping']);
212+
}
213+
195214
$customerModel->save();
196215
$this->customerRegistry->push($customerModel);
197216
$customerId = $customerModel->getId();
@@ -258,7 +277,7 @@ public function getList(SearchCriteriaInterface $searchCriteria)
258277
$searchResults->setSearchCriteria($searchCriteria);
259278
/** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */
260279
$collection = $this->customerFactory->create()->getCollection();
261-
$this->extensionAttributesJoinProcessor->process($collection, 'Magento\Customer\Api\Data\CustomerInterface');
280+
$this->extensionAttributesJoinProcessor->process($collection, \Magento\Customer\Api\Data\CustomerInterface::class);
262281
// This is needed to make sure all the attributes are properly loaded
263282
foreach ($this->customerMetadata->getAllAttributesMetadata() as $metadata) {
264283
$collection->addAttributeToSelect($metadata->getAttributeCode());

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

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -91,75 +91,81 @@ class CustomerRepositoryTest extends \PHPUnit_Framework_TestCase
9191
public function setUp()
9292
{
9393
$this->customerResourceModel =
94-
$this->getMock('Magento\Customer\Model\ResourceModel\Customer', [], [], '', false);
95-
$this->customerRegistry = $this->getMock('Magento\Customer\Model\CustomerRegistry', [], [], '', false);
96-
$this->dataObjectHelper = $this->getMock('Magento\Framework\Api\DataObjectHelper', [], [], '', false);
97-
$this->customerFactory = $this->getMock('Magento\Customer\Model\CustomerFactory', ['create'], [], '', false);
94+
$this->getMock(\Magento\Customer\Model\ResourceModel\Customer::class, [], [], '', false);
95+
$this->customerRegistry = $this->getMock(\Magento\Customer\Model\CustomerRegistry::class, [], [], '', false);
96+
$this->dataObjectHelper = $this->getMock(\Magento\Framework\Api\DataObjectHelper::class, [], [], '', false);
97+
$this->customerFactory =
98+
$this->getMock(\Magento\Customer\Model\CustomerFactory::class, ['create'], [], '', false);
9899
$this->customerSecureFactory = $this->getMock(
99-
'Magento\Customer\Model\Data\CustomerSecureFactory',
100+
\Magento\Customer\Model\Data\CustomerSecureFactory::class,
100101
['create'],
101102
[],
102103
'',
103104
false
104105
);
105106

106107
$this->addressRepository = $this->getMock(
107-
'Magento\Customer\Model\ResourceModel\AddressRepository',
108+
\Magento\Customer\Model\ResourceModel\AddressRepository::class,
108109
[],
109110
[],
110111
'',
111112
false
112113
);
113114

114115
$this->customerMetadata = $this->getMockForAbstractClass(
115-
'Magento\Customer\Api\CustomerMetadataInterface',
116+
\Magento\Customer\Api\CustomerMetadataInterface::class,
116117
[],
117118
'',
118119
false
119120
);
120121
$this->searchResultsFactory = $this->getMock(
121-
'Magento\Customer\Api\Data\CustomerSearchResultsInterfaceFactory',
122+
\Magento\Customer\Api\Data\CustomerSearchResultsInterfaceFactory::class,
122123
['create'],
123124
[],
124125
'',
125126
false
126127
);
127128
$this->eventManager = $this->getMockForAbstractClass(
128-
'Magento\Framework\Event\ManagerInterface',
129+
\Magento\Framework\Event\ManagerInterface::class,
129130
[],
130131
'',
131132
false
132133
);
133134
$this->storeManager = $this->getMockForAbstractClass(
134-
'Magento\Store\Model\StoreManagerInterface',
135+
\Magento\Store\Model\StoreManagerInterface::class,
135136
[],
136137
'',
137138
false
138139
);
139140
$this->extensibleDataObjectConverter = $this->getMock(
140-
'Magento\Framework\Api\ExtensibleDataObjectConverter',
141+
\Magento\Framework\Api\ExtensibleDataObjectConverter::class,
141142
[],
142143
[],
143144
'',
144145
false
145146
);
146147
$this->imageProcessor = $this->getMockForAbstractClass(
147-
'Magento\Framework\Api\ImageProcessorInterface',
148+
\Magento\Framework\Api\ImageProcessorInterface::class,
148149
[],
149150
'',
150151
false
151152
);
152153
$this->extensionAttributesJoinProcessor = $this->getMockForAbstractClass(
153-
'Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface',
154+
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class,
154155
[],
155156
'',
156157
false
157158
);
158159
$this->customer = $this->getMockForAbstractClass(
159-
'Magento\Customer\Api\Data\CustomerInterface',
160+
\Magento\Customer\Api\Data\CustomerInterface::class,
160161
[],
161162
'',
162-
false
163+
true,
164+
true,
165+
true,
166+
[
167+
'__toArray'
168+
]
163169
);
164170

165171
$this->model = new \Magento\Customer\Model\ResourceModel\CustomerRepository(
@@ -185,7 +191,7 @@ public function setUp()
185191
protected function prepareMocksForValidation($isValid = false)
186192
{
187193
$attributeMetaData = $this->getMockForAbstractClass(
188-
'Magento\Customer\Api\Data\AttributeMetadataInterface',
194+
\Magento\Customer\Api\Data\AttributeMetadataInterface::class,
189195
[],
190196
'',
191197
false
@@ -226,9 +232,9 @@ public function testSave()
226232
$storeId = 2;
227233
$this->prepareMocksForValidation(true);
228234

229-
$region = $this->getMockForAbstractClass('Magento\Customer\Api\Data\RegionInterface', [], '', false);
235+
$region = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\RegionInterface::class, [], '', false);
230236
$address = $this->getMockForAbstractClass(
231-
'Magento\Customer\Api\Data\AddressInterface',
237+
\Magento\Customer\Api\Data\AddressInterface::class,
232238
[],
233239
'',
234240
false,
@@ -242,7 +248,7 @@ public function testSave()
242248
]
243249
);
244250
$address2 = $this->getMockForAbstractClass(
245-
'Magento\Customer\Api\Data\AddressInterface',
251+
\Magento\Customer\Api\Data\AddressInterface::class,
246252
[],
247253
'',
248254
false,
@@ -256,7 +262,7 @@ public function testSave()
256262
]
257263
);
258264
$customerModel = $this->getMock(
259-
'Magento\Customer\Model\Customer',
265+
\Magento\Customer\Model\Customer::class,
260266
[
261267
'getId',
262268
'setId',
@@ -274,8 +280,13 @@ public function testSave()
274280
'',
275281
false
276282
);
283+
284+
$this->customer->expects($this->atLeastOnce())
285+
->method('__toArray')
286+
->willReturn(['default_billing', 'default_shipping']);
287+
277288
$customerAttributesMetaData = $this->getMockForAbstractClass(
278-
'Magento\Framework\Api\CustomAttributesDataInterface',
289+
\Magento\Framework\Api\CustomAttributesDataInterface::class,
279290
[],
280291
'',
281292
false,
@@ -290,7 +301,7 @@ public function testSave()
290301
]
291302
);
292303
$customerSecureData = $this->getMock(
293-
'Magento\Customer\Model\Data\CustomerSecure',
304+
\Magento\Customer\Model\Data\CustomerSecure::class,
294305
[
295306
'getRpToken',
296307
'getRpTokenCreatedAt',
@@ -338,7 +349,7 @@ public function testSave()
338349
->with([$address]);
339350
$this->extensibleDataObjectConverter->expects($this->once())
340351
->method('toNestedArray')
341-
->with($customerAttributesMetaData, [], '\Magento\Customer\Api\Data\CustomerInterface')
352+
->with($customerAttributesMetaData, [], \Magento\Customer\Api\Data\CustomerInterface::class)
342353
->willReturn(['customerData']);
343354
$this->customerFactory->expects($this->once())
344355
->method('create')
@@ -347,7 +358,7 @@ public function testSave()
347358
$customerModel->expects($this->once())
348359
->method('getStoreId')
349360
->willReturn(null);
350-
$store = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
361+
$store = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
351362
$store->expects($this->once())
352363
->method('getId')
353364
->willReturn($storeId);
@@ -444,9 +455,9 @@ public function testSaveWithPasswordHash()
444455
$passwordHash = 'ukfa4sdfa56s5df02asdf4rt';
445456
$this->prepareMocksForValidation(true);
446457

447-
$region = $this->getMockForAbstractClass('Magento\Customer\Api\Data\RegionInterface', [], '', false);
458+
$region = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\RegionInterface::class, [], '', false);
448459
$address = $this->getMockForAbstractClass(
449-
'Magento\Customer\Api\Data\AddressInterface',
460+
\Magento\Customer\Api\Data\AddressInterface::class,
450461
[],
451462
'',
452463
false,
@@ -460,7 +471,7 @@ public function testSaveWithPasswordHash()
460471
]
461472
);
462473
$address2 = $this->getMockForAbstractClass(
463-
'Magento\Customer\Api\Data\AddressInterface',
474+
\Magento\Customer\Api\Data\AddressInterface::class,
464475
[],
465476
'',
466477
false,
@@ -473,8 +484,13 @@ public function testSaveWithPasswordHash()
473484
'getId'
474485
]
475486
);
487+
488+
$this->customer->expects($this->atLeastOnce())
489+
->method('__toArray')
490+
->willReturn(['default_billing', 'default_shipping']);
491+
476492
$customerModel = $this->getMock(
477-
'Magento\Customer\Model\Customer',
493+
\Magento\Customer\Model\Customer::class,
478494
[
479495
'getId',
480496
'setId',
@@ -493,7 +509,7 @@ public function testSaveWithPasswordHash()
493509
false
494510
);
495511
$customerAttributesMetaData = $this->getMockForAbstractClass(
496-
'Magento\Framework\Api\CustomAttributesDataInterface',
512+
\Magento\Framework\Api\CustomAttributesDataInterface::class,
497513
[],
498514
'',
499515
false,
@@ -545,7 +561,7 @@ public function testSaveWithPasswordHash()
545561
->with([$address]);
546562
$this->extensibleDataObjectConverter->expects($this->once())
547563
->method('toNestedArray')
548-
->with($customerAttributesMetaData, [], '\Magento\Customer\Api\Data\CustomerInterface')
564+
->with($customerAttributesMetaData, [], \Magento\Customer\Api\Data\CustomerInterface::class)
549565
->willReturn(['customerData']);
550566
$this->customerFactory->expects($this->once())
551567
->method('create')
@@ -554,7 +570,7 @@ public function testSaveWithPasswordHash()
554570
$customerModel->expects($this->once())
555571
->method('getStoreId')
556572
->willReturn(null);
557-
$store = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
573+
$store = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
558574
$store->expects($this->once())
559575
->method('getId')
560576
->willReturn($storeId);
@@ -625,24 +641,24 @@ public function testSaveWithException()
625641
*/
626642
public function testGetList()
627643
{
628-
$sortOrder = $this->getMock('Magento\Framework\Api\SortOrder', [], [], '', false);
629-
$filterGroup = $this->getMock('Magento\Framework\Api\Search\FilterGroup', [], [], '', false);
630-
$filter = $this->getMock('Magento\Framework\Api\Filter', [], [], '', false);
631-
$collection = $this->getMock('Magento\Customer\Model\ResourceModel\Customer\Collection', [], [], '', false);
644+
$sortOrder = $this->getMock(\Magento\Framework\Api\SortOrder::class, [], [], '', false);
645+
$filterGroup = $this->getMock(\Magento\Framework\Api\Search\FilterGroup::class, [], [], '', false);
646+
$filter = $this->getMock(\Magento\Framework\Api\Filter::class, [], [], '', false);
647+
$collection = $this->getMock(\Magento\Customer\Model\ResourceModel\Customer\Collection::class, [], [], '', false);
632648
$searchResults = $this->getMockForAbstractClass(
633-
'Magento\Customer\Api\Data\AddressSearchResultsInterface',
649+
\Magento\Customer\Api\Data\AddressSearchResultsInterface::class,
634650
[],
635651
'',
636652
false
637653
);
638654
$searchCriteria = $this->getMockForAbstractClass(
639-
'Magento\Framework\Api\SearchCriteriaInterface',
655+
\Magento\Framework\Api\SearchCriteriaInterface::class,
640656
[],
641657
'',
642658
false
643659
);
644660
$customerModel = $this->getMock(
645-
'Magento\Customer\Model\Customer',
661+
\Magento\Customer\Model\Customer::class,
646662
[
647663
'getId',
648664
'setId',
@@ -661,7 +677,7 @@ public function testGetList()
661677
false
662678
);
663679
$metadata = $this->getMockForAbstractClass(
664-
'Magento\Customer\Api\Data\AttributeMetadataInterface',
680+
\Magento\Customer\Api\Data\AttributeMetadataInterface::class,
665681
[],
666682
'',
667683
false
@@ -681,7 +697,7 @@ public function testGetList()
681697
->willReturn($collection);
682698
$this->extensionAttributesJoinProcessor->expects($this->once())
683699
->method('process')
684-
->with($collection, 'Magento\Customer\Api\Data\CustomerInterface');
700+
->with($collection, \Magento\Customer\Api\Data\CustomerInterface::class);
685701
$this->customerMetadata->expects($this->once())
686702
->method('getAllAttributesMetadata')
687703
->willReturn([$metadata]);
@@ -782,7 +798,7 @@ public function testDeleteById()
782798
{
783799
$customerId = 14;
784800
$customerModel = $this->getMock(
785-
'Magento\Customer\Model\Customer',
801+
\Magento\Customer\Model\Customer::class,
786802
['delete'],
787803
[],
788804
'',
@@ -807,7 +823,7 @@ public function testDelete()
807823
{
808824
$customerId = 14;
809825
$customerModel = $this->getMock(
810-
'Magento\Customer\Model\Customer',
826+
\Magento\Customer\Model\Customer::class,
811827
['delete'],
812828
[],
813829
'',

0 commit comments

Comments
 (0)