Skip to content

Commit

Permalink
Merge pull request #3552 from magento-panda/MAGETWO-94394-93769
Browse files Browse the repository at this point in the history
[PANDA] [B2B] Unable to add large catalog to shared catalog & Cover new customer addresses grid by MFTF tests
  • Loading branch information
okolesnyk authored Dec 20, 2018
2 parents 4ee3bb3 + 7362c89 commit c307d25
Show file tree
Hide file tree
Showing 26 changed files with 702 additions and 26 deletions.
29 changes: 15 additions & 14 deletions app/code/Magento/Catalog/Model/Product/Price/TierPriceStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function get(array $skus)
{
Expand All @@ -107,7 +107,7 @@ public function get(array $skus)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function update(array $prices)
{
Expand All @@ -128,7 +128,7 @@ public function update(array $prices)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function replace(array $prices)
{
Expand All @@ -144,7 +144,7 @@ public function replace(array $prices)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function delete(array $prices)
{
Expand All @@ -171,16 +171,17 @@ private function getExistingPrices(array $skus, $groupBySku = false)
$ids = $this->retrieveAffectedIds($skus);
$rawPrices = $this->tierPricePersistence->get($ids);
$prices = [];

$linkField = $this->tierPricePersistence->getEntityLinkField();
$skuByIdLookup = $this->buildSkuByIdLookup($skus);
foreach ($rawPrices as $rawPrice) {
$sku = $skuByIdLookup[$rawPrice[$linkField]];
$price = $this->tierPriceFactory->create($rawPrice, $sku);
if ($groupBySku) {
$prices[$sku][] = $price;
} else {
$prices[] = $price;
if ($rawPrices) {
$linkField = $this->tierPricePersistence->getEntityLinkField();
$skuByIdLookup = $this->buildSkuByIdLookup($skus);
foreach ($rawPrices as $rawPrice) {
$sku = $skuByIdLookup[$rawPrice[$linkField]];
$price = $this->tierPriceFactory->create($rawPrice, $sku);
if ($groupBySku) {
$prices[$sku][] = $price;
} else {
$prices[] = $price;
}
}
}

Expand Down
38 changes: 33 additions & 5 deletions app/code/Magento/Catalog/Model/ProductIdLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,43 @@ class ProductIdLocator implements \Magento\Catalog\Model\ProductIdLocatorInterfa
*/
private $idsBySku = [];

/**
* Batch size to iterate collection
*
* @var int
*/
private $batchSize;

/**
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
* @param string $limitIdsBySkuValues
* @param string $idsLimit
* @param int $batchSize defines how many items can be processed by one iteration
*/
public function __construct(
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
$idsLimit
$idsLimit,
int $batchSize = 5000
) {
$this->metadataPool = $metadataPool;
$this->collectionFactory = $collectionFactory;
$this->idsLimit = (int)$idsLimit;
$this->batchSize = $batchSize;
}

/**
* {@inheritdoc}
* @inheritdoc
*
* Load product items by provided products SKUs.
* Products collection will be iterated by pages with the $this->batchSize as a page size (for a cases when to many
* products SKUs were provided in parameters.
* Loaded products will be chached in the $this->idsBySku variable, but in the end of the method these storage will
* be truncated to $idsLimit quantity.
* As a result array with the products data will be returned with the following scheme:
* $data['product_sku']['link_field_value' => 'product_type']
*
* @throws \Exception
*/
public function retrieveProductIdsBySkus(array $skus)
{
Expand All @@ -72,8 +92,16 @@ public function retrieveProductIdsBySkus(array $skus)
$linkField = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
->getLinkField();

foreach ($collection as $item) {
$this->idsBySku[strtolower(trim($item->getSku()))][$item->getData($linkField)] = $item->getTypeId();
$collection->setPageSize($this->batchSize);
$pages = $collection->getLastPageNumber();
for ($currentPage = 1; $currentPage <= $pages; $currentPage++) {
$collection->setCurPage($currentPage);
foreach ($collection->getItems() as $item) {
$sku = strtolower(trim($item->getSku()));
$itemIdentifier = $item->getData($linkField);
$this->idsBySku[$sku][$itemIdentifier] = $item->getTypeId();
}
$collection->clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
*/
private $dimensionFactory;

/**
* @var \Magento\Framework\DataObject
*/
private $emptyItem;

/**
* Collection constructor
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
Expand Down Expand Up @@ -550,7 +555,10 @@ protected function _prepareStaticFields()
*/
public function getNewEmptyItem()
{
$object = parent::getNewEmptyItem();
if (null === $this->emptyItem) {
$this->emptyItem = parent::getNewEmptyItem();
}
$object = clone $this->emptyItem;
if ($this->isEnabledFlat()) {
$object->setIdFieldName($this->getEntity()->getIdFieldName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,30 @@ public function testGet()
$this->assertEquals(2, count($prices));
}

/**
* Test get method without tierprices.
*
* @return void
*/
public function testGetWithoutTierPrices()
{
$skus = ['simple', 'virtual'];
$this->tierPriceValidator
->expects($this->once())
->method('validateSkus')
->with($skus)
->willReturn($skus);
$this->productIdLocator->expects($this->atLeastOnce())
->method('retrieveProductIdsBySkus')
->with(['simple', 'virtual'])
->willReturn(['simple' => ['2' => 'simple'], 'virtual' => ['3' => 'virtual']]);
$this->tierPricePersistence->expects($this->once())->method('get')->willReturn([]);
$this->tierPricePersistence->expects($this->never())->method('getEntityLinkField');
$this->tierPriceFactory->expects($this->never())->method('create');
$prices = $this->tierPriceStorage->get($skus);
$this->assertEmpty($prices);
}

/**
* Test update method.
*
Expand Down
17 changes: 15 additions & 2 deletions app/code/Magento/Catalog/Test/Unit/Model/ProductIdLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ public function testRetrieveProductIdsBySkus()
{
$skus = ['sku_1', 'sku_2'];
$collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
->setMethods(['getIterator', 'addFieldToFilter'])
->setMethods(
[
'getItems',
'addFieldToFilter',
'setPageSize',
'getLastPageNumber',
'setCurPage',
'clear'
]
)
->disableOriginalConstructor()->getMock();
$product = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
->setMethods(['getSku', 'getData', 'getTypeId'])
Expand All @@ -69,7 +78,11 @@ public function testRetrieveProductIdsBySkus()
$this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
$collection->expects($this->once())->method('addFieldToFilter')
->with(\Magento\Catalog\Api\Data\ProductInterface::SKU, ['in' => $skus])->willReturnSelf();
$collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$product]));
$collection->expects($this->atLeastOnce())->method('getItems')->willReturn([$product]);
$collection->expects($this->atLeastOnce())->method('setPageSize')->willReturnSelf();
$collection->expects($this->atLeastOnce())->method('getLastPageNumber')->willReturn(1);
$collection->expects($this->atLeastOnce())->method('setCurPage')->with(1)->willReturnSelf();
$collection->expects($this->atLeastOnce())->method('clear')->willReturnSelf();
$this->metadataPool
->expects($this->once())
->method('getMetadata')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
*/
private $storeManager;

/**
* @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $entityFactory;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$entityFactory = $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class);
$this->entityFactory = $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class);
$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
Expand Down Expand Up @@ -168,7 +173,7 @@ protected function setUp()
$this->collection = $this->objectManager->getObject(
\Magento\Catalog\Model\ResourceModel\Product\Collection::class,
[
'entityFactory' => $entityFactory,
'entityFactory' => $this->entityFactory,
'logger' => $logger,
'fetchStrategy' => $fetchStrategy,
'eventManager' => $eventManager,
Expand Down Expand Up @@ -379,4 +384,20 @@ public function testAddTierPriceData()

$this->assertSame($this->collection, $this->collection->addTierPriceData());
}

/**
* Test for getNewEmptyItem() method
*
* @return void
*/
public function testGetNewEmptyItem()
{
$item = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
$this->entityFactory->expects($this->once())->method('create')->willReturn($item);
$firstItem = $this->collection->getNewEmptyItem();
$secondItem = $this->collection->getNewEmptyItem();
$this->assertEquals($firstItem, $secondItem);
}
}
16 changes: 16 additions & 0 deletions app/code/Magento/Customer/Test/Mftf/Data/AddressData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@
<requiredEntity type="region">RegionNY</requiredEntity>
<data key="country">United States</data>
</entity>
<entity name="US_Address_NY_Not_Default_Address" type="address">
<data key="firstname">John</data>
<data key="lastname">Doe</data>
<data key="company">368</data>
<array key="street">
<item>368 Broadway St.</item>
<item>Apt. 113</item>
</array>
<data key="city">New York</data>
<data key="state">New York</data>
<data key="country_id">US</data>
<data key="postcode">10001</data>
<data key="telephone">512-345-6789</data>
<requiredEntity type="region">RegionNY</requiredEntity>
<data key="country">United States</data>
</entity>
<entity name="US_Address_CA" type="address">
<data key="firstname">John</data>
<data key="lastname">Doe</data>
Expand Down
14 changes: 14 additions & 0 deletions app/code/Magento/Customer/Test/Mftf/Data/CustomerData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@
<requiredEntity type="address">US_Address_NY</requiredEntity>
<requiredEntity type="address">UK_Not_Default_Address</requiredEntity>
</entity>
<entity name="Simple_US_Customer_Multiple_Addresses_No_Default_Address" type="customer">
<data key="group_id">0</data>
<data key="default_billing">true</data>
<data key="default_shipping">true</data>
<data key="email" unique="prefix">John.Doe@example.com</data>
<data key="firstname">John</data>
<data key="lastname">Doe</data>
<data key="fullname">John Doe</data>
<data key="password">pwdTest123!</data>
<data key="store_id">0</data>
<data key="website_id">0</data>
<requiredEntity type="address">US_Address_NY_Not_Default_Address</requiredEntity>
<requiredEntity type="address">UK_Not_Default_Address</requiredEntity>
</entity>
<entity name="Simple_US_Customer_NY" type="customer">
<data key="group_id">0</data>
<data key="default_billing">true</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/PageObject.xsd">
<page name="AdminEditCustomerPage" url="/customer/index/edit/id/{{var1}}" area="admin" module="Magento_Customer" parameterized="true">
<section name="AdminCustomerAccountInformationSection"/>
<section name="AdminCustomerAddressesGridSection"/>
<section name="AdminCustomerAddressesGridActionsSection"/>
<section name="AdminCustomerAddressesSection"/>
<section name="AdminCustomerMainActionsSection"/>
</page>
</pages>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<element name="countryInput" type="input" selector="select[name=country_id]"/>
<element name="telephoneInput" type="input" selector="input[name=telephone]"/>
<element name="applyFilter" type="button" selector="button[data-action=grid-filter-apply]" timeout="30"/>
<element name="clearAll" type="button" selector=".admin__data-grid-header .action-tertiary.action-clear"/>
<element name="clearAll" type="button" selector=".admin__data-grid-header .action-tertiary.action-clear" timeout="30"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="AdminCustomerAddressesDefaultBillingSection">
<element name="addressDetails" type="text" selector="//div[@class='customer-default-billing-address-content']//div[@class='address_details']"/>
<element name="address" type="text" selector="//div[@class='customer-default-billing-address-content']//address//span"/>
<element name="editButton" type="text" selector="//button[@data-index='edit_billing_address']"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="AdminCustomerAddressesDefaultShippingSection">
<element name="addressDetails" type="text" selector="//div[@class='customer-default-shipping-address-content']//div[@class='address_details']"/>
<element name="address" type="text" selector="//div[@class='customer-default-shipping-address-content']//address//span"/>
<element name="editButton" type="text" selector="//button[@data-index='edit_shipping_address']"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="AdminCustomerAddressesGridActionsSection">
<element name="spinner" type="button" selector=".spinner"/>
<element name="gridLoadingMask" type="button" selector=".admin__data-grid-loading-mask"/>
<element name="search" type="input" selector="#fulltext"/>
<element name="delete" type="button" selector="//*[contains(@class, 'admin__data-grid-header')]//span[contains(@class,'action-menu-item') and text()='Delete']"/>
<element name="actions" type="text" selector="//div[@class='admin__data-grid-header']//button[@class='action-select']"/>
<element name="filters" type="button" selector="button[data-action='grid-filter-expand']" timeout="30"/>
<element name="ok" type="button" selector="//button[@data-role='action']//span[text()='OK']"/>
</section>
</sections>
Loading

0 comments on commit c307d25

Please sign in to comment.