Skip to content

Commit 868b19f

Browse files
committed
MAGETWO-97411: \Magento\Customer\Model\Customer::getDataModel method takes to much time to load with many addresses customer
1 parent f8e5c19 commit 868b19f

File tree

5 files changed

+112
-13
lines changed

5 files changed

+112
-13
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ public function updateData(AddressInterface $address)
172172
public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null)
173173
{
174174
if ($this->getCustomerId() || $this->getParentId()) {
175-
$primaryAddresses = $this->getCustomer()->getPrimaryAddressIds();
176-
$defaultBillingAddressId = $primaryAddresses['billing_address'] ?: $defaultBillingAddressId;
177-
$defaultShippingAddressId = $primaryAddresses['shipping_address'] ?: $defaultShippingAddressId;
175+
$customer = $this->getCustomer();
176+
$defaultBillingAddressId = $customer->getDefaultBilling() ?: $defaultBillingAddressId;
177+
$defaultShippingAddressId = $customer->getDefaultShipping() ?: $defaultShippingAddressId;
178178
}
179179
return parent::getDataModel($defaultBillingAddressId, $defaultShippingAddressId);
180180
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ class Customer extends \Magento\Framework\Model\AbstractModel
220220
private $accountConfirmation;
221221

222222
/**
223+
* Caching property to store customer addresses by the customer ID.
224+
*
223225
* @var array
224226
*/
225227
private $storedAddress;
@@ -684,10 +686,10 @@ public function getPrimaryAddressIds()
684686
{
685687
$ids = [];
686688
if ($this->getDefaultBilling()) {
687-
$ids['billing_address'] = $this->getDefaultBilling();
689+
$ids[] = $this->getDefaultBilling();
688690
}
689691
if ($this->getDefaultShipping()) {
690-
$ids['shipping_address'] = $this->getDefaultShipping();
692+
$ids[] = $this->getDefaultShipping();
691693
}
692694
return $ids;
693695
}

app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Magento\Customer\Model\Customer;
1515
use Magento\Customer\Model\AccountConfirmation;
16+
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory as AddressCollectionFactory;
17+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1618

1719
/**
1820
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -68,6 +70,21 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
6870
*/
6971
private $accountConfirmation;
7072

73+
/**
74+
* @var AddressCollectionFactory|\PHPUnit_Framework_MockObject_MockObject
75+
*/
76+
private $addressesFactory;
77+
78+
/**
79+
* @var CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
80+
*/
81+
private $customerDataFactory;
82+
83+
/**
84+
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
85+
*/
86+
private $dataObjectHelper;
87+
7188
protected function setUp()
7289
{
7390
$this->_website = $this->createMock(\Magento\Store\Model\Website::class);
@@ -100,6 +117,19 @@ protected function setUp()
100117
$this->_encryptor = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
101118
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
102119
$this->accountConfirmation = $this->createMock(AccountConfirmation::class);
120+
$this->addressesFactory = $this->getMockBuilder(AddressCollectionFactory::class)
121+
->disableOriginalConstructor()
122+
->setMethods(['create'])
123+
->getMock();
124+
$this->customerDataFactory = $this->getMockBuilder(CustomerInterfaceFactory::class)
125+
->disableOriginalConstructor()
126+
->setMethods(['create'])
127+
->getMock();
128+
$this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
129+
->disableOriginalConstructor()
130+
->setMethods(['populateWithArray'])
131+
->getMock();
132+
103133
$this->_model = $helper->getObject(
104134
\Magento\Customer\Model\Customer::class,
105135
[
@@ -112,7 +142,10 @@ protected function setUp()
112142
'registry' => $this->registryMock,
113143
'resource' => $this->resourceMock,
114144
'dataObjectProcessor' => $this->dataObjectProcessor,
115-
'accountConfirmation' => $this->accountConfirmation
145+
'accountConfirmation' => $this->accountConfirmation,
146+
'_addressesFactory' => $this->addressesFactory,
147+
'customerDataFactory' => $this->customerDataFactory,
148+
'dataObjectHelper' => $this->dataObjectHelper
116149
]
117150
);
118151
}
@@ -186,13 +219,13 @@ public function testSendNewAccountEmailWithoutStoreId()
186219
->will($this->returnValue($transportMock));
187220

188221
$this->_model->setData([
189-
'website_id' => 1,
190-
'store_id' => 1,
191-
'email' => 'email@example.com',
192-
'firstname' => 'FirstName',
193-
'lastname' => 'LastName',
194-
'middlename' => 'MiddleName',
195-
'prefix' => 'Name Prefix',
222+
'website_id' => 1,
223+
'store_id' => 1,
224+
'email' => 'email@example.com',
225+
'firstname' => 'FirstName',
226+
'lastname' => 'LastName',
227+
'middlename' => 'MiddleName',
228+
'prefix' => 'Name Prefix',
196229
]);
197230
$this->_model->sendNewAccountEmail('registered');
198231
}
@@ -310,4 +343,43 @@ public function testUpdateData()
310343

311344
$this->assertEquals($this->_model->getData(), $expectedResult);
312345
}
346+
347+
/**
348+
* Test for the \Magento\Customer\Model\Customer::getDataModel() method
349+
*/
350+
public function testGetDataModel()
351+
{
352+
$customerId = 1;
353+
$this->_model->setEntityId($customerId);
354+
$this->_model->setId($customerId);
355+
$addressDataModel = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\AddressInterface::class);
356+
$address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
357+
->disableOriginalConstructor()
358+
->setMethods(['setCustomer', 'getDataModel'])
359+
->getMock();
360+
$address->expects($this->atLeastOnce())->method('getDataModel')->willReturn($addressDataModel);
361+
$addresses = new \ArrayIterator([$address, $address]);
362+
$addressCollection = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Address\Collection::class)
363+
->disableOriginalConstructor()
364+
->setMethods(['setCustomerFilter', 'addAttributeToSelect', 'getIterator', 'getItems'])
365+
->getMock();
366+
$addressCollection->expects($this->atLeastOnce())->method('setCustomerFilter')->willReturnSelf();
367+
$addressCollection->expects($this->atLeastOnce())->method('addAttributeToSelect')->willReturnSelf();
368+
$addressCollection->expects($this->atLeastOnce())->method('getIterator')
369+
->willReturn($addresses);
370+
$addressCollection->expects($this->atLeastOnce())->method('getItems')
371+
->willReturn($addresses);
372+
$this->addressesFactory->expects($this->atLeastOnce())->method('create')->willReturn($addressCollection);
373+
$customerDataObject = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
374+
$this->customerDataFactory->expects($this->atLeastOnce())->method('create')->willReturn($customerDataObject);
375+
$this->dataObjectHelper->expects($this->atLeastOnce())->method('populateWithArray')
376+
->with($customerDataObject, $this->_model->getData(), \Magento\Customer\Api\Data\CustomerInterface::class)
377+
->willReturnSelf();
378+
$customerDataObject->expects($this->atLeastOnce())->method('setAddresses')
379+
->with([$addressDataModel, $addressDataModel])
380+
->willReturnSelf();
381+
$customerDataObject->expects($this->atLeastOnce())->method('setId')->with($customerId)->willReturnSelf();
382+
$this->_model->getDataModel();
383+
$this->assertEquals($customerDataObject, $this->_model->getDataModel());
384+
}
313385
}

dev/tests/integration/testsuite/Magento/Customer/Model/AddressTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,28 @@ public function testUpdateDataOverrideExistingData()
6767
$this->assertEquals('CompanyZ', $updatedAddressData->getCompany());
6868
$this->assertEquals('99999', $updatedAddressData->getPostcode());
6969
}
70+
71+
/**
72+
* @magentoDataFixture Magento/Customer/_files/customer_sample.php
73+
*/
74+
public function testUpdateDataForExistingCustomer()
75+
{
76+
/** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
77+
$customerRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(CustomerRegistry::class);
78+
/** @var \Magento\Customer\Model\Data\Address $addressData */
79+
$updatedAddressData = $this->addressFactory->create()
80+
->setId(1)
81+
->setCustomerId($customerRegistry->retrieveByEmail('customer@example.com')->getId())
82+
->setCity('CityZ')
83+
->setCompany('CompanyZ')
84+
->setPostcode('99999');
85+
$updatedAddressData = $this->addressModel->updateData($updatedAddressData)->getDataModel();
86+
87+
$this->assertEquals(1, $updatedAddressData->getId());
88+
$this->assertEquals('CityZ', $updatedAddressData->getCity());
89+
$this->assertEquals('CompanyZ', $updatedAddressData->getCompany());
90+
$this->assertEquals('99999', $updatedAddressData->getPostcode());
91+
$this->assertEquals(true, $updatedAddressData->isDefaultBilling());
92+
$this->assertEquals(true, $updatedAddressData->isDefaultShipping());
93+
}
7094
}

dev/tests/integration/testsuite/Magento/Customer/_files/customer_sample.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'lastname' => 'test lastname',
2020
'email' => 'customer@example.com',
2121
'default_billing' => 1,
22+
'default_shipping' => 1,
2223
'password' => '123123q',
2324
'attribute_set_id' => 1,
2425
];

0 commit comments

Comments
 (0)