Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/2.4-develop' into PR_20210609
Browse files Browse the repository at this point in the history
  • Loading branch information
o-dubovyk committed Jun 30, 2021
2 parents 30b8eaa + 912857f commit 3ae8fe1
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
*/
namespace Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Indexer;

use Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\ConfigurableProductsProvider;

/**
* Class ReindexProduct. Add configurable sub-products to reindex
* Add configurable sub-products to reindex
*/
class ProductRuleReindex
{
/**
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
* @var Configurable
*/
private $configurable;

/**
* @var \Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\ConfigurableProductsProvider
* @var ConfigurableProductsProvider
*/
private $configurableProductsProvider;

Expand All @@ -37,61 +38,47 @@ public function __construct(
}

/**
* @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
* Reindex configurable product with sub-products
*
* @param ProductRuleIndexer $subject
* @param \Closure $proceed
* @param int $id
*
* @return void
*/
public function aroundExecuteRow(
\Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject,
\Closure $proceed,
$id
) {
public function aroundExecuteRow(ProductRuleIndexer $subject, \Closure $proceed, $id)
{
$isReindexed = false;

$configurableProductIds = $this->configurableProductsProvider->getIds([$id]);
$this->reindexSubProducts($configurableProductIds, $subject);
if (!$configurableProductIds) {
$proceed($id);
if ($configurableProductIds) {
$subProducts = array_values($this->configurable->getChildrenIds($id)[0]);
if ($subProducts) {
$subject->executeList(array_merge([$id], $subProducts));
$isReindexed = true;
}
}
}

/**
* @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
* @param \Closure $proceed
* @param array $ids
*
* @return void
*/
public function aroundExecuteList(
\Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject,
\Closure $proceed,
array $ids
) {
$configurableProductIds = $this->configurableProductsProvider->getIds($ids);
$subProducts = $this->reindexSubProducts($configurableProductIds, $subject);
$ids = array_diff($ids, $configurableProductIds, $subProducts);
if ($ids) {
$proceed($ids);
if (!$isReindexed) {
$proceed($id);
}
}

/**
* @param array $configurableIds
* @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
* Add sub-products to reindex
*
* @param ProductRuleIndexer $subject
* @param array $ids
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
private function reindexSubProducts(
array $configurableIds,
\Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject
) {
$subProducts = [];
if ($configurableIds) {
$subProducts = array_values($this->configurable->getChildrenIds($configurableIds)[0]);
if ($subProducts) {
$subject->executeList($subProducts);
}
public function beforeExecuteList(ProductRuleIndexer $subject, array $ids): array
{
$configurableProductIds = $this->configurableProductsProvider->getIds($ids);
if ($configurableProductIds) {
$subProducts = array_values($this->configurable->getChildrenIds($configurableProductIds)[0]);
$ids = array_unique(array_merge($ids, $subProducts));
}
return $subProducts;

return [$ids];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogRuleConfigurable\Model\Indexer\Product;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer;
use Magento\CatalogRule\Pricing\Price\CatalogRulePrice;
use Magento\Framework\Pricing\Price\Factory as PriceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* @magentoDbIsolation disabled
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/CatalogRuleConfigurable/_files/configurable_product_with_percent_rule.php
*/
class ProductRuleIndexerTest extends TestCase
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var PriceFactory
*/
private $priceFactory;

/**
* @var ProductRuleIndexer
*/
private $productRuleIndexer;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
$this->priceFactory = $objectManager->get(PriceFactory::class);
$this->productRuleIndexer = $objectManager->create(ProductRuleIndexer::class);
}

/**
* @return void
*/
public function testExecute(): void
{
$product = $this->productRepository->get('configurable');
$this->productRuleIndexer->execute([$product->getId()]);

$product = $this->productRepository->get('simple_10');
$price = $this->getCatalogRulePrice($product);
$this->assertEquals(5, $price);
}

/**
* @return void
*/
public function testExecuteRow(): void
{
$product = $this->productRepository->get('configurable');
$this->productRuleIndexer->executeRow($product->getId());

$product = $this->productRepository->get('simple_10');
$price = $this->getCatalogRulePrice($product);
$this->assertEquals(5, $price);
}

/**
* @return void
*/
public function testExecuteList(): void
{
$product = $this->productRepository->get('configurable');
$this->productRuleIndexer->executeList([$product->getId()]);

$product = $this->productRepository->get('simple_10');
$price = $this->getCatalogRulePrice($product);
$this->assertEquals(5, $price);
}

public function testExecuteFull(): void
{
$this->productRuleIndexer->executeFull();

$product = $this->productRepository->get('simple_10');
$price = $this->getCatalogRulePrice($product);
$this->assertEquals(5, $price);
}

/**
* @param Product $product
* @return float|bool
*/
private function getCatalogRulePrice(Product $product)
{
$catalogRulePrice = $this->priceFactory->create($product, CatalogRulePrice::class, 1);
$price = $catalogRulePrice->getValue();

return $price;
}
}

0 comments on commit 3ae8fe1

Please sign in to comment.