Skip to content

Commit

Permalink
Merge branch '2.4-develop' into 2.4-bugfix/date_login_customer_issue3…
Browse files Browse the repository at this point in the history
…0328
  • Loading branch information
engcom-Hotel committed Nov 4, 2020
2 parents a16616b + 632a7c6 commit 8ddd329
Show file tree
Hide file tree
Showing 89 changed files with 2,328 additions and 726 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,82 @@
*/
namespace Magento\Catalog\Model\Indexer\Product\Price\Action;

use Magento\Directory\Model\CurrencyFactory;
use Magento\Catalog\Model\Indexer\Product\Price\DimensionCollectionFactory;
use Magento\Catalog\Model\Indexer\Product\Price\TableMaintainer;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Factory;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\TierPrice;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class Rows reindex action for mass actions
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) to preserve compatibility with parent class
*/
class Rows extends \Magento\Catalog\Model\Indexer\Product\Price\AbstractAction
{
/**
* Default batch size
*/
private const BATCH_SIZE = 100;

/**
* @var int
*/
private $batchSize;

/**
* @param ScopeConfigInterface $config
* @param StoreManagerInterface $storeManager
* @param CurrencyFactory $currencyFactory
* @param TimezoneInterface $localeDate
* @param DateTime $dateTime
* @param Type $catalogProductType
* @param Factory $indexerPriceFactory
* @param DefaultPrice $defaultIndexerResource
* @param TierPrice|null $tierPriceIndexResource
* @param DimensionCollectionFactory|null $dimensionCollectionFactory
* @param TableMaintainer|null $tableMaintainer
* @param int|null $batchSize
* @SuppressWarnings(PHPMD.NPathComplexity) Added to backward compatibility with abstract class
* @SuppressWarnings(PHPMD.CyclomaticComplexity) Added to backward compatibility with abstract class
* @SuppressWarnings(PHPMD.ExcessiveParameterList) Added to backward compatibility with abstract class
*/
public function __construct(
ScopeConfigInterface $config,
StoreManagerInterface $storeManager,
CurrencyFactory $currencyFactory,
TimezoneInterface $localeDate,
DateTime $dateTime,
Type $catalogProductType,
Factory $indexerPriceFactory,
DefaultPrice $defaultIndexerResource,
TierPrice $tierPriceIndexResource = null,
DimensionCollectionFactory $dimensionCollectionFactory = null,
TableMaintainer $tableMaintainer = null,
?int $batchSize = null
) {
parent::__construct(
$config,
$storeManager,
$currencyFactory,
$localeDate,
$dateTime,
$catalogProductType,
$indexerPriceFactory,
$defaultIndexerResource,
$tierPriceIndexResource,
$dimensionCollectionFactory,
$tableMaintainer
);
$this->batchSize = $batchSize ?? self::BATCH_SIZE;
}

/**
* Execute Rows reindex
*
Expand All @@ -24,10 +94,28 @@ public function execute($ids)
if (empty($ids)) {
throw new \Magento\Framework\Exception\InputException(__('Bad value was supplied.'));
}
try {
$this->_reindexRows($ids);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()), $e);
$currentBatch = [];
$i = 0;

foreach ($ids as $id) {
$currentBatch[] = $id;
if (++$i === $this->batchSize) {
try {
$this->_reindexRows($currentBatch);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()), $e);
}
$i = 0;
$currentBatch = [];
}
}

if (!empty($currentBatch)) {
try {
$this->_reindexRows($currentBatch);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()), $e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,38 @@
*/
namespace Magento\Catalog\Model\ResourceModel\Product;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Catalog\Api\Data\ProductInterface;

/**
* Catalog Product Relations Resource model
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Relation extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
class Relation extends AbstractDb
{
/**
* @var MetadataPool
*/
private $metadataPool;

/**
* @param Context $context
* @param string $connectionName
* @param MetadataPool $metadataPool
*/
public function __construct(
Context $context,
$connectionName = null,
MetadataPool $metadataPool = null
) {
parent::__construct($context, $connectionName);
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
}

/**
* Initialize resource model and define main table
*
Expand Down Expand Up @@ -109,4 +134,27 @@ public function removeRelations($parentId, $childIds)
}
return $this;
}

/**
* Finds parent relations by given children ids.
*
* @param array $childrenIds Child products entity ids.
* @return array Parent products entity ids.
*/
public function getRelationsByChildren(array $childrenIds): array
{
$connection = $this->getConnection();
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)
->getLinkField();
$select = $connection->select()
->from(
['cpe' => $this->getTable('catalog_product_entity')],
'entity_id'
)->join(
['relation' => $this->getTable('catalog_product_relation')],
'relation.parent_id = cpe.' . $linkField
)->where('relation.child_id IN(?)', $childrenIds);

return $connection->fetchCol($select);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,189 @@

namespace Magento\Catalog\Test\Unit\Model\Indexer\Product\Price\Action;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Directory\Model\CurrencyFactory;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Factory;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\DefaultPrice;
use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\TierPrice;
use Magento\Catalog\Model\Indexer\Product\Price\DimensionCollectionFactory;
use Magento\Catalog\Model\Indexer\Product\Price\TableMaintainer;
use Magento\Catalog\Model\Indexer\Product\Price\Action\Rows;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\Indexer\MultiDimensionProvider;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Test coverage for the rows action
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) to preserve compatibility with parent class
*/
class RowsTest extends TestCase
{
/**
* @var Rows
*/
protected $_model;
private $actionRows;

/**
* @var ScopeConfigInterface|MockObject
*/
private $config;

/**
* @var StoreManagerInterface|MockObject
*/
private $storeManager;

/**
* @var CurrencyFactory|MockObject
*/
private $currencyFactory;

/**
* @var TimezoneInterface|MockObject
*/
private $localeDate;

/**
* @var DateTime|MockObject
*/
private $dateTime;

/**
* @var Type|MockObject
*/
private $catalogProductType;

/**
* @var Factory|MockObject
*/
private $indexerPriceFactory;

/**
* @var DefaultPrice|MockObject
*/
private $defaultIndexerResource;

/**
* @var TierPrice|MockObject
*/
private $tierPriceIndexResource;

/**
* @var DimensionCollectionFactory|MockObject
*/
private $dimensionCollectionFactory;

/**
* @var TableMaintainer|MockObject
*/
private $tableMaintainer;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->_model = $objectManager->getObject(Rows::class);
$this->config = $this->getMockBuilder(ScopeConfigInterface::class)
->getMockForAbstractClass();
$this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
$this->currencyFactory = $this->getMockBuilder(CurrencyFactory::class)
->disableOriginalConstructor()
->getMock();
$this->localeDate = $this->getMockBuilder(TimezoneInterface::class)
->getMockForAbstractClass();
$this->dateTime = $this->getMockBuilder(DateTime::class)
->disableOriginalConstructor()
->getMock();
$this->catalogProductType = $this->getMockBuilder(Type::class)
->disableOriginalConstructor()
->getMock();
$this->indexerPriceFactory = $this->getMockBuilder(Factory::class)
->disableOriginalConstructor()
->getMock();
$this->defaultIndexerResource = $this->getMockBuilder(DefaultPrice::class)
->disableOriginalConstructor()
->getMock();
$this->tierPriceIndexResource = $this->getMockBuilder(TierPrice::class)
->disableOriginalConstructor()
->getMock();
$this->dimensionCollectionFactory = $this->getMockBuilder(DimensionCollectionFactory::class)
->disableOriginalConstructor()
->getMock();
$this->tableMaintainer = $this->getMockBuilder(TableMaintainer::class)
->disableOriginalConstructor()
->getMock();
$batchSize = 2;

$this->actionRows = new Rows(
$this->config,
$this->storeManager,
$this->currencyFactory,
$this->localeDate,
$this->dateTime,
$this->catalogProductType,
$this->indexerPriceFactory,
$this->defaultIndexerResource,
$this->tierPriceIndexResource,
$this->dimensionCollectionFactory,
$this->tableMaintainer,
$batchSize
);
}

public function testEmptyIds()
{
$this->expectException('Magento\Framework\Exception\InputException');
$this->expectExceptionMessage('Bad value was supplied.');
$this->_model->execute(null);
$this->actionRows->execute(null);
}

public function testBatchProcessing()
{
$ids = [1, 2, 3, 4];
$select = $this->getMockBuilder(Select::class)
->disableOriginalConstructor()
->getMock();
$select->expects($this->any())->method('from')->willReturnSelf();
$select->expects($this->any())->method('where')->willReturnSelf();
$select->expects($this->any())->method('join')->willReturnSelf();
$adapter = $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass();
$adapter->expects($this->any())->method('select')->willReturn($select);
$this->defaultIndexerResource->expects($this->any())
->method('getConnection')
->willReturn($adapter);
$adapter->expects($this->any())
->method('fetchAll')
->with($select)
->willReturn([]);
$adapter->expects($this->any())
->method('fetchPairs')
->with($select)
->willReturn([]);
$multiDimensionProvider = $this->getMockBuilder(MultiDimensionProvider::class)
->disableOriginalConstructor()
->getMock();
$this->dimensionCollectionFactory->expects($this->exactly(2))
->method('create')
->willReturn($multiDimensionProvider);
$iterator = new \ArrayIterator([]);
$multiDimensionProvider->expects($this->exactly(2))
->method('getIterator')
->willReturn($iterator);
$this->catalogProductType->expects($this->any())
->method('getTypesByPriority')
->willReturn([]);
$adapter->expects($this->exactly(2))
->method('getIndexList')
->willReturn(['entity_id'=>['COLUMNS_LIST'=>['test']]]);
$adapter->expects($this->exactly(2))
->method('getPrimaryKeyName')
->willReturn('entity_id');
$this->actionRows->execute($ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function build(AggregationInterface $aggregation, ?int $storeId): array
);
}

return [$result];
return [self::PRICE_BUCKET => $result];
}

/**
Expand Down
Loading

0 comments on commit 8ddd329

Please sign in to comment.