Skip to content

Commit b99d64d

Browse files
author
Vladyslav Shcherbyna
committed
MAGETWO-31896: Changes are not saved when default billing/shipping address is unchecked in customer addresses
1 parent 4393143 commit b99d64d

File tree

2 files changed

+110
-54
lines changed

2 files changed

+110
-54
lines changed

app/code/Magento/Customer/Model/Resource/Customer.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,8 @@ protected function _saveAddresses(\Magento\Customer\Model\Customer $customer)
221221
}
222222
}
223223
}
224-
if ($customer->dataHasChangedFor('default_billing')) {
225-
$this->saveAttribute($customer, 'default_billing');
226-
}
227-
if ($customer->dataHasChangedFor('default_shipping')) {
228-
$this->saveAttribute($customer, 'default_shipping');
229-
}
224+
$this->saveAttribute($customer, 'default_billing');
225+
$this->saveAttribute($customer, 'default_shipping');
230226

231227
return $this;
232228
}

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

Lines changed: 108 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\Framework\Api\SearchCriteriaInterface;
1111
use Magento\TestFramework\Helper\Bootstrap;
1212

13+
/**
14+
* Checks Customer insert, update, search with repository
15+
*/
1316
class CustomerRepositoryTest extends \PHPUnit_Framework_TestCase
1417
{
1518
/** @var AccountManagementInterface */
@@ -112,31 +115,39 @@ public function testCreateNewCustomer()
112115
}
113116

114117
/**
118+
* @dataProvider updateCustomerDataProvider
115119
* @magentoAppArea frontend
116120
* @magentoDataFixture Magento/Customer/_files/customer.php
121+
* @param int|null $defaultBilling
122+
* @param int|null $defaultShipping
117123
*/
118-
public function testUpdateCustomer()
124+
public function testUpdateCustomer($defaultBilling, $defaultShipping)
119125
{
120-
$existingCustId = 1;
126+
$existingCustomerId = 1;
121127
$email = 'savecustomer@example.com';
122128
$firstName = 'Firstsave';
123-
$lastname = 'Lastsave';
124-
$customerBefore = $this->customerRepository->getById($existingCustId);
129+
$lastName = 'Lastsave';
130+
$customerBefore = $this->customerRepository->getById($existingCustomerId);
125131
$customerData = array_merge($customerBefore->__toArray(), [
126132
'id' => 1,
127133
'email' => $email,
128134
'firstname' => $firstName,
129-
'lastname' => $lastname,
135+
'lastname' => $lastName,
130136
'created_in' => 'Admin',
131-
'password' => 'notsaved'
137+
'password' => 'notsaved',
138+
'default_billing' => $defaultBilling,
139+
'default_shipping' => $defaultShipping
132140
]);
133141
$this->customerBuilder->populateWithArray($customerData);
134-
$customerDetails = $this->customerBuilder->setId($existingCustId)->create();
142+
$customerDetails = $this->customerBuilder->create();
135143
$this->customerRepository->save($customerDetails);
136-
$customerAfter = $this->customerRepository->getById($existingCustId);
144+
$customerAfter = $this->customerRepository->getById($existingCustomerId);
137145
$this->assertEquals($email, $customerAfter->getEmail());
138146
$this->assertEquals($firstName, $customerAfter->getFirstname());
139-
$this->assertEquals($lastname, $customerAfter->getLastname());
147+
$this->assertEquals($lastName, $customerAfter->getLastname());
148+
$this->assertEquals($defaultBilling, $customerAfter->getDefaultBilling());
149+
$this->assertEquals($defaultShipping, $customerAfter->getDefaultShipping());
150+
$this->expectedDefaultShippingsInCustomerModelAttributes($existingCustomerId, $defaultBilling, $defaultShipping);
140151
$this->assertEquals('Admin', $customerAfter->getCreatedIn());
141152
$passwordFromFixture = 'password';
142153
$this->accountManagement->authenticate($customerAfter->getEmail(), $passwordFromFixture);
@@ -269,45 +280,6 @@ public function testSearchCustomers($filters, $filterGroup, $expectedResult)
269280
}
270281
}
271282

272-
public function searchCustomersDataProvider()
273-
{
274-
$builder = Bootstrap::getObjectManager()->create('\Magento\Framework\Api\FilterBuilder');
275-
return [
276-
'Customer with specific email' => [
277-
[$builder->setField('email')->setValue('customer@search.example.com')->create()],
278-
null,
279-
[1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname']],
280-
],
281-
'Customer with specific first name' => [
282-
[$builder->setField('firstname')->setValue('Firstname2')->create()],
283-
null,
284-
[2 => ['email' => 'customer2@search.example.com', 'firstname' => 'Firstname2']],
285-
],
286-
'Customers with either email' => [
287-
[],
288-
[
289-
$builder->setField('firstname')->setValue('Firstname')->create(),
290-
$builder->setField('firstname')->setValue('Firstname2')->create()
291-
],
292-
[
293-
1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname'],
294-
2 => ['email' => 'customer2@search.example.com', 'firstname' => 'Firstname2']
295-
],
296-
],
297-
'Customers created since' => [
298-
[
299-
$builder->setField('created_at')->setValue('2011-02-28 15:52:26')
300-
->setConditionType('gt')->create(),
301-
],
302-
[],
303-
[
304-
1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname'],
305-
3 => ['email' => 'customer3@search.example.com', 'firstname' => 'Firstname3']
306-
],
307-
]
308-
];
309-
}
310-
311283
/**
312284
* Test ordering
313285
*
@@ -387,4 +359,92 @@ public function testDeleteById()
387359
);
388360
$this->customerRepository->get($fixtureCustomerEmail);
389361
}
362+
363+
/**
364+
* DataProvider update customer
365+
*
366+
* @return array
367+
*/
368+
public function updateCustomerDataProvider()
369+
{
370+
return [
371+
'Customer remove default shipping and billing' => [
372+
null,
373+
null
374+
],
375+
'Customer update default shipping and billing' => [
376+
1,
377+
1
378+
],
379+
];
380+
}
381+
382+
public function searchCustomersDataProvider()
383+
{
384+
$builder = Bootstrap::getObjectManager()->create('\Magento\Framework\Api\FilterBuilder');
385+
return [
386+
'Customer with specific email' => [
387+
[$builder->setField('email')->setValue('customer@search.example.com')->create()],
388+
null,
389+
[1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname']],
390+
],
391+
'Customer with specific first name' => [
392+
[$builder->setField('firstname')->setValue('Firstname2')->create()],
393+
null,
394+
[2 => ['email' => 'customer2@search.example.com', 'firstname' => 'Firstname2']],
395+
],
396+
'Customers with either email' => [
397+
[],
398+
[
399+
$builder->setField('firstname')->setValue('Firstname')->create(),
400+
$builder->setField('firstname')->setValue('Firstname2')->create()
401+
],
402+
[
403+
1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname'],
404+
2 => ['email' => 'customer2@search.example.com', 'firstname' => 'Firstname2']
405+
],
406+
],
407+
'Customers created since' => [
408+
[
409+
$builder->setField('created_at')->setValue('2011-02-28 15:52:26')
410+
->setConditionType('gt')->create(),
411+
],
412+
[],
413+
[
414+
1 => ['email' => 'customer@search.example.com', 'firstname' => 'Firstname'],
415+
3 => ['email' => 'customer3@search.example.com', 'firstname' => 'Firstname3']
416+
],
417+
]
418+
];
419+
}
420+
421+
/**
422+
* Check defaults billing and shipping in customer model
423+
*
424+
* @param $customerId
425+
* @param $defaultBilling
426+
* @param $defaultShipping
427+
*/
428+
protected function expectedDefaultShippingsInCustomerModelAttributes(
429+
$customerId,
430+
$defaultBilling,
431+
$defaultShipping
432+
) {
433+
/**
434+
* @var \Magento\Customer\Model\Customer $customer
435+
*/
436+
$customer = $this->objectManager->create('Magento\Customer\Model\Customer');
437+
/** @var \Magento\Customer\Model\Customer $customer */
438+
$customer->load($customerId);
439+
$this->assertEquals(
440+
$defaultBilling,
441+
$customer->getDefaultBilling(),
442+
'default_billing customer attribute did not updated'
443+
);
444+
$this->assertEquals(
445+
$defaultShipping,
446+
$customer->getDefaultShipping(),
447+
'default_shipping customer attribute did not updated'
448+
);
449+
}
390450
}

0 commit comments

Comments
 (0)