Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3609 from magento-panda/MAGETWO-97411
Browse files Browse the repository at this point in the history
Fixed issue:
  - MAGETWO-97411: \Magento\Customer\Model\Customer::getDataModel method takes to much time to load with many addresses customer
  • Loading branch information
igrybkov authored Jan 21, 2019
2 parents 2c31192 + fcbb2b9 commit d1ebc17
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 15 deletions.
9 changes: 3 additions & 6 deletions app/code/Magento/Customer/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,9 @@ public function updateData(AddressInterface $address)
public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null)
{
if ($this->getCustomerId() || $this->getParentId()) {
if ($this->getCustomer()->getDefaultBillingAddress()) {
$defaultBillingAddressId = $this->getCustomer()->getDefaultBillingAddress()->getId();
}
if ($this->getCustomer()->getDefaultShippingAddress()) {
$defaultShippingAddressId = $this->getCustomer()->getDefaultShippingAddress()->getId();
}
$customer = $this->getCustomer();
$defaultBillingAddressId = $customer->getDefaultBilling() ?: $defaultBillingAddressId;
$defaultShippingAddressId = $customer->getDefaultShipping() ?: $defaultShippingAddressId;
}
return parent::getDataModel($defaultBillingAddressId, $defaultShippingAddressId);
}
Expand Down
12 changes: 11 additions & 1 deletion app/code/Magento/Customer/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ class Customer extends \Magento\Framework\Model\AbstractModel
*/
private $accountConfirmation;

/**
* Caching property to store customer address data models by the address ID.
*
* @var array
*/
private $storedAddress;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -314,7 +321,10 @@ public function getDataModel()
$addressesData = [];
/** @var \Magento\Customer\Model\Address $address */
foreach ($this->getAddresses() as $address) {
$addressesData[] = $address->getDataModel();
if (!isset($this->storedAddress[$address->getId()])) {
$this->storedAddress[$address->getId()] = $address->getDataModel();
}
$addressesData[] = $this->storedAddress[$address->getId()];
}
$customerDataObject = $this->customerDataFactory->create();
$this->dataObjectHelper->populateWithArray(
Expand Down
89 changes: 81 additions & 8 deletions app/code/Magento/Customer/Test/Unit/Model/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

use Magento\Customer\Model\Customer;
use Magento\Customer\Model\AccountConfirmation;
use Magento\Customer\Model\ResourceModel\Address\CollectionFactory as AddressCollectionFactory;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
class CustomerTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -68,6 +71,21 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
*/
private $accountConfirmation;

/**
* @var AddressCollectionFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $addressesFactory;

/**
* @var CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $customerDataFactory;

/**
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
*/
private $dataObjectHelper;

protected function setUp()
{
$this->_website = $this->createMock(\Magento\Store\Model\Website::class);
Expand Down Expand Up @@ -100,6 +118,19 @@ protected function setUp()
$this->_encryptor = $this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class);
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->accountConfirmation = $this->createMock(AccountConfirmation::class);
$this->addressesFactory = $this->getMockBuilder(AddressCollectionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->customerDataFactory = $this->getMockBuilder(CustomerInterfaceFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
->disableOriginalConstructor()
->setMethods(['populateWithArray'])
->getMock();

$this->_model = $helper->getObject(
\Magento\Customer\Model\Customer::class,
[
Expand All @@ -112,7 +143,10 @@ protected function setUp()
'registry' => $this->registryMock,
'resource' => $this->resourceMock,
'dataObjectProcessor' => $this->dataObjectProcessor,
'accountConfirmation' => $this->accountConfirmation
'accountConfirmation' => $this->accountConfirmation,
'_addressesFactory' => $this->addressesFactory,
'customerDataFactory' => $this->customerDataFactory,
'dataObjectHelper' => $this->dataObjectHelper
]
);
}
Expand Down Expand Up @@ -186,13 +220,13 @@ public function testSendNewAccountEmailWithoutStoreId()
->will($this->returnValue($transportMock));

$this->_model->setData([
'website_id' => 1,
'store_id' => 1,
'email' => 'email@example.com',
'firstname' => 'FirstName',
'lastname' => 'LastName',
'middlename' => 'MiddleName',
'prefix' => 'Name Prefix',
'website_id' => 1,
'store_id' => 1,
'email' => 'email@example.com',
'firstname' => 'FirstName',
'lastname' => 'LastName',
'middlename' => 'MiddleName',
'prefix' => 'Name Prefix',
]);
$this->_model->sendNewAccountEmail('registered');
}
Expand Down Expand Up @@ -310,4 +344,43 @@ public function testUpdateData()
$this->assertEquals($this->_model->getData(), $expectedResult);
}
/**
* Test for the \Magento\Customer\Model\Customer::getDataModel() method
*/
public function testGetDataModel()
{
$customerId = 1;
$this->_model->setEntityId($customerId);
$this->_model->setId($customerId);
$addressDataModel = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\AddressInterface::class);
$address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
->disableOriginalConstructor()
->setMethods(['setCustomer', 'getDataModel'])
->getMock();
$address->expects($this->atLeastOnce())->method('getDataModel')->willReturn($addressDataModel);
$addresses = new \ArrayIterator([$address, $address]);
$addressCollection = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Address\Collection::class)
->disableOriginalConstructor()
->setMethods(['setCustomerFilter', 'addAttributeToSelect', 'getIterator', 'getItems'])
->getMock();
$addressCollection->expects($this->atLeastOnce())->method('setCustomerFilter')->willReturnSelf();
$addressCollection->expects($this->atLeastOnce())->method('addAttributeToSelect')->willReturnSelf();
$addressCollection->expects($this->atLeastOnce())->method('getIterator')
->willReturn($addresses);
$addressCollection->expects($this->atLeastOnce())->method('getItems')
->willReturn($addresses);
$this->addressesFactory->expects($this->atLeastOnce())->method('create')->willReturn($addressCollection);
$customerDataObject = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
$this->customerDataFactory->expects($this->atLeastOnce())->method('create')->willReturn($customerDataObject);
$this->dataObjectHelper->expects($this->atLeastOnce())->method('populateWithArray')
->with($customerDataObject, $this->_model->getData(), \Magento\Customer\Api\Data\CustomerInterface::class)
->willReturnSelf();
$customerDataObject->expects($this->atLeastOnce())->method('setAddresses')
->with([$addressDataModel, $addressDataModel])
->willReturnSelf();
$customerDataObject->expects($this->atLeastOnce())->method('setId')->with($customerId)->willReturnSelf();
$this->_model->getDataModel();
$this->assertEquals($customerDataObject, $this->_model->getDataModel());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,28 @@ public function testUpdateDataOverrideExistingData()
$this->assertEquals('CompanyZ', $updatedAddressData->getCompany());
$this->assertEquals('99999', $updatedAddressData->getPostcode());
}

/**
* @magentoDataFixture Magento/Customer/_files/customer_sample.php
*/
public function testUpdateDataForExistingCustomer()
{
/** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
$customerRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(CustomerRegistry::class);
/** @var \Magento\Customer\Model\Data\Address $addressData */
$updatedAddressData = $this->addressFactory->create()
->setId(1)
->setCustomerId($customerRegistry->retrieveByEmail('customer@example.com')->getId())
->setCity('CityZ')
->setCompany('CompanyZ')
->setPostcode('99999');
$updatedAddressData = $this->addressModel->updateData($updatedAddressData)->getDataModel();

$this->assertEquals(1, $updatedAddressData->getId());
$this->assertEquals('CityZ', $updatedAddressData->getCity());
$this->assertEquals('CompanyZ', $updatedAddressData->getCompany());
$this->assertEquals('99999', $updatedAddressData->getPostcode());
$this->assertEquals(true, $updatedAddressData->isDefaultBilling());
$this->assertEquals(true, $updatedAddressData->isDefaultShipping());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'lastname' => 'test lastname',
'email' => 'customer@example.com',
'default_billing' => 1,
'default_shipping' => 1,
'password' => '123123q',
'attribute_set_id' => 1,
];
Expand Down

0 comments on commit d1ebc17

Please sign in to comment.