Skip to content

Commit 12d45e4

Browse files
author
Sergey Shvets
committed
Merge branch 'MAGETWO-59871' of github.com:magento-east/magento2ce into pr-dev
2 parents 6ae3418 + b0ad00b commit 12d45e4

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ public function __construct(
145145
public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null)
146146
{
147147
$prevCustomerData = null;
148+
$prevCustomerDataArr = null;
148149
if ($customer->getId()) {
149150
$prevCustomerData = $this->getById($customer->getId());
151+
$prevCustomerDataArr = $prevCustomerData->__toArray();
150152
}
153+
/** @var $customer \Magento\Customer\Model\Data\Customer */
154+
$customerArr = $customer->__toArray();
151155
$customer = $this->imageProcessor->save(
152156
$customer,
153157
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
@@ -185,6 +189,20 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
185189
$customerModel->setRpToken(null);
186190
$customerModel->setRpTokenCreatedAt(null);
187191
}
192+
if (!array_key_exists('default_billing', $customerArr) &&
193+
null !== $prevCustomerDataArr &&
194+
array_key_exists('default_billing', $prevCustomerDataArr)
195+
) {
196+
$customerModel->setDefaultBilling($prevCustomerDataArr['default_billing']);
197+
}
198+
199+
if (!array_key_exists('default_shipping', $customerArr) &&
200+
null !== $prevCustomerDataArr &&
201+
array_key_exists('default_shipping', $prevCustomerDataArr)
202+
) {
203+
$customerModel->setDefaultShipping($prevCustomerDataArr['default_shipping']);
204+
}
205+
188206
$customerModel->save();
189207
$this->customerRegistry->push($customerModel);
190208
$customerId = $customerModel->getId();

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,22 @@ protected function setUp()
101101
$this->getMock(\Magento\Customer\Model\ResourceModel\Customer::class, [], [], '', false);
102102
$this->customerRegistry = $this->getMock(\Magento\Customer\Model\CustomerRegistry::class, [], [], '', false);
103103
$this->dataObjectHelper = $this->getMock(\Magento\Framework\Api\DataObjectHelper::class, [], [], '', false);
104-
$this->customerFactory = $this->getMock(
105-
\Magento\Customer\Model\CustomerFactory::class,
106-
['create'],
107-
[],
108-
'',
109-
false
110-
);
104+
$this->customerFactory =
105+
$this->getMock(\Magento\Customer\Model\CustomerFactory::class, ['create'], [], '', false);
111106
$this->customerSecureFactory = $this->getMock(
112107
\Magento\Customer\Model\Data\CustomerSecureFactory::class,
113108
['create'],
114109
[],
115110
'',
116111
false
117112
);
118-
119113
$this->addressRepository = $this->getMock(
120114
\Magento\Customer\Model\ResourceModel\AddressRepository::class,
121115
[],
122116
[],
123117
'',
124118
false
125119
);
126-
127120
$this->customerMetadata = $this->getMockForAbstractClass(
128121
\Magento\Customer\Api\CustomerMetadataInterface::class,
129122
[],
@@ -172,11 +165,15 @@ protected function setUp()
172165
\Magento\Customer\Api\Data\CustomerInterface::class,
173166
[],
174167
'',
175-
false
168+
true,
169+
true,
170+
true,
171+
[
172+
'__toArray'
173+
]
176174
);
177175
$this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
178176
->getMock();
179-
180177
$this->model = new \Magento\Customer\Model\ResourceModel\CustomerRepository(
181178
$this->customerFactory,
182179
$this->customerSecureFactory,
@@ -254,6 +251,11 @@ public function testSave()
254251
'',
255252
false
256253
);
254+
255+
$this->customer->expects($this->atLeastOnce())
256+
->method('__toArray')
257+
->willReturn(['default_billing', 'default_shipping']);
258+
257259
$customerAttributesMetaData = $this->getMockForAbstractClass(
258260
\Magento\Framework\Api\CustomAttributesDataInterface::class,
259261
[],
@@ -495,6 +497,11 @@ public function testSaveWithPasswordHash()
495497
'getId'
496498
]
497499
);
500+
501+
$this->customer->expects($this->atLeastOnce())
502+
->method('__toArray')
503+
->willReturn(['default_billing', 'default_shipping']);
504+
498505
$customerModel = $this->getMock(
499506
\Magento\Customer\Model\Customer::class,
500507
[

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)