This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CatalogRule\Model;
use Magento\CatalogRule\Api\CatalogRuleRepositoryInterface;
use Magento\CatalogRule\Api\Data\RuleInterface;
use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
use Magento\CatalogRule\Model\ResourceModel\RuleFactory as ResourceRuleFactory;
use Magento\Framework\Indexer\StateInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Covers with test catalog rule repository functionality.
*/
class CatalogRuleRepositoryTest extends TestCase
{
/**
* @var CatalogRuleRepositoryInterface
*/
private $catalogRuleRepository;
/**
* @inheridoc
*/
protected function setUp(): void
{
$this->catalogRuleRepository = Bootstrap::getObjectManager()->get(CatalogRuleRepositoryInterface::class);
}
/**
* Verify index become invalid in case rule become inactive and stays active in case inactive rule has been saved.
*
* @magentoDataFixture Magento/CatalogRule/_files/catalog_rule_25_customer_group_all.php
* @magentoDbIsolation disabled
*
* @return void
*/
public function testIndexInvalidationAfterInactiveRuleSave(): void
{
$ruleProductProcessor = Bootstrap::getObjectManager()->get(RuleProductProcessor::class);
$state = $ruleProductProcessor->getIndexer()->getState();
$state->setStatus(StateInterface::STATUS_VALID);
$ruleProductProcessor->getIndexer()->setState($state);
$rule = $this->getRuleByName('Test Catalog Rule With 25 Percent Off');
$rule->setIsActive(0);
$this->catalogRuleRepository->save($rule);
self::assertEquals(StateInterface::STATUS_INVALID, $ruleProductProcessor->getIndexer()->getStatus());
$state = $ruleProductProcessor->getIndexer()->getState();
$state->setStatus(StateInterface::STATUS_VALID);
$ruleProductProcessor->getIndexer()->setState($state);
$this->catalogRuleRepository->save($rule);
self::assertEquals(StateInterface::STATUS_VALID, $ruleProductProcessor->getIndexer()->getStatus());
}
/**
* Retrieve catalog rule by name from db.
*
* @param string $name
* @return RuleInterface
*/
private function getRuleByName(string $name): RuleInterface
{
$catalogRuleResource = Bootstrap::getObjectManager()->get(ResourceRuleFactory::class)->create();
$select = $catalogRuleResource->getConnection()->select();
$select->from($catalogRuleResource->getMainTable(), RuleInterface::RULE_ID);
$select->where(RuleInterface::NAME . ' = ?', $name);
$ruleId = $catalogRuleResource->getConnection()->fetchOne($select);
return $this->catalogRuleRepository->get((int)$ruleId);
}
}
@@ -0,0 +1,140 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\Indexer;
use Magento\TestFramework\Helper\Bootstrap;
/**
* @magentoAppIsolation enabled
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/CatalogRule/_files/two_rules.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
class BatchIndexTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
protected $productRepository;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;
/**
* @var \Magento\CatalogRule\Model\ResourceModel\Rule
*/
protected $resourceRule;
protected function setUp(): void
{
$this->resourceRule = Bootstrap::getObjectManager()->get(\Magento\CatalogRule\Model\ResourceModel\Rule::class);
$this->product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Product::class);
$this->productRepository = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\ProductRepository::class);
}
protected function tearDown(): void
{
/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Framework\Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = Bootstrap::getObjectManager()->get(
\Magento\Catalog\Model\ResourceModel\Product\Collection::class
);
$productCollection->delete();
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
parent::tearDown();
}
/**
* @magentoDbIsolation disabled
* @dataProvider dataProvider
* @magentoAppIsolation enabled
* @magentoAppArea adminhtml
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/two_rules.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testPriceForSmallBatch($batchCount, $price, $expectedPrice)
{
$productIds = $this->prepareProducts($price);
/**
* @var IndexBuilder $indexerBuilder
*/
$indexerBuilder = Bootstrap::getObjectManager()->create(
\Magento\CatalogRule\Model\Indexer\IndexBuilder::class,
['batchCount' => $batchCount]
);
$indexerBuilder->reindexFull();
foreach ([0, 1] as $customerGroupId) {
foreach ($productIds as $productId) {
$this->assertEquals(
$expectedPrice,
$this->resourceRule->getRulePrice(new \DateTime(), 1, $customerGroupId, $productId)
);
}
}
}
/**
* @return array
*/
protected function prepareProducts($price)
{
$this->product = $this->productRepository->get('simple');
$productSecond = clone $this->product;
$productSecond->setId(null)
->setUrlKey(null)
->setSku(uniqid($this->product->getSku() . '-'))
->setName(uniqid($this->product->getName() . '-'))
->setWebsiteIds([1])
->save();
$productSecond->setPrice($price);
$this->productRepository->save($productSecond);
$productThird = clone $this->product;
$productThird->setId(null)
->setUrlKey(null)
->setSku(uniqid($this->product->getSku() . '--'))
->setName(uniqid($this->product->getName() . '--'))
->setWebsiteIds([1])
->save();
$productThird->setPrice($price);
$this->productRepository->save($productThird);
return [
$productSecond->getEntityId(),
$productThird->getEntityId(),
];
}
/**
* @return array
*/
public function dataProvider()
{
return [
[1, 20, 17],
[3, 40, 36],
[3, 60, 55],
[5, 100, 93],
[8, 200, 188],
[10, 500, 473],
[11, 760, 720],
];
}
}
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CatalogRule\Model\Indexer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
use Magento\Framework\DataObject;
use Magento\TestFramework\Fixture\AppIsolation;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DbIsolation;
use Magento\TestFramework\Helper\Bootstrap;
class IndexerBuilderInScheduledModeTest extends \PHPUnit\Framework\TestCase
{
/**
* @var RuleProductProcessor
*/
private $ruleProductProcessor;
/**
* @var CollectionFactory
*/
private $productCollectionFactory;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
protected function setUp(): void
{
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
$this->ruleProductProcessor = Bootstrap::getObjectManager()->get(RuleProductProcessor::class);
$this->productCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
}
#[
DbIsolation(false),
AppIsolation(true),
DataFixture('Magento/Catalog/_files/product_with_options.php'),
DataFixture('Magento/CatalogRule/_files/catalog_rule_10_off_not_logged.php'),
]
public function testReindexOfDependentIndexer(): void
{
$indexer = $this->ruleProductProcessor->getIndexer();
$indexer->reindexAll();
$indexer->setScheduled(true);
$product = $this->productRepository->get('simple');
$productId = (int)$product->getId();
$product = $this->getProductFromCollection($productId);
$this->assertEquals(9, $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue());
$product->setPrice(100);
$this->productRepository->save($product);
$product = $this->getProductFromCollection($productId);
$this->assertEquals(9, $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue());
$indexer->reindexList([$productId]);
$product = $this->getProductFromCollection($productId);
$this->assertEquals(90, $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue());
$indexer->setScheduled(false);
}
/**
* Get the product from the product collection
*
* @param int $productId
* @return DataObject
*/
private function getProductFromCollection(int $productId) : DataObject
{
$productCollection = $this->productCollectionFactory->create();
$productCollection->addIdFilter($productId);
$productCollection->addPriceData();
$productCollection->load();
return $productCollection->getFirstItem();
}
}
@@ -0,0 +1,274 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\Indexer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Fixture\AppIsolation;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DbIsolation;
use Magento\TestFramework\Helper\Bootstrap;
#[
DbIsolation(false),
AppIsolation(true),
]
class IndexerBuilderTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\CatalogRule\Model\Indexer\IndexBuilder
*/
protected $indexerBuilder;
/**
* @var \Magento\CatalogRule\Model\ResourceModel\Rule
*/
protected $resourceRule;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $productSecond;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $productThird;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var ResourceConnection
*/
private $connection;
/**
* @var Processor
*/
private $indexProductProcessor;
protected function setUp(): void
{
$this->indexerBuilder = Bootstrap::getObjectManager()->get(
\Magento\CatalogRule\Model\Indexer\IndexBuilder::class
);
$this->resourceRule = Bootstrap::getObjectManager()->get(\Magento\CatalogRule\Model\ResourceModel\Rule::class);
$this->product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Product::class);
$this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
$this->connection = Bootstrap::getObjectManager()->get(ResourceConnection::class);
$this->indexProductProcessor = Bootstrap::getObjectManager()->get(Processor::class);
}
protected function tearDown(): void
{
/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Framework\Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = Bootstrap::getObjectManager()->get(
\Magento\Catalog\Model\ResourceModel\Product\Collection::class
);
$productCollection->delete();
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/CatalogRule/_files/attribute.php
* @magentoDataFixture Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testReindexById()
{
$product = $this->product->loadByAttribute('sku', 'simple');
$product->load($product->getId());
$product->setData('test_attribute', 'test_attribute_value')->save();
$this->indexerBuilder->reindexById($product->getId());
$this->assertEquals(9.8, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $product->getId()));
}
/**
* @magentoDataFixture Magento/CatalogRule/_files/simple_product_with_catalog_rule_50_percent_off_tomorrow.php
* @magentoConfigFixture base_website general/locale/timezone Europe/Amsterdam
* @magentoConfigFixture general/locale/timezone America/Chicago
*/
public function testReindexByIdDifferentTimezones()
{
$productId = $this->productRepository->get('simple')->getId();
$this->indexerBuilder->reindexById($productId);
$mainWebsiteId = $this->storeManager->getWebsite('base')->getId();
$secondWebsiteId = $this->storeManager->getWebsite('test')->getId();
$rawTimestamp = (new \DateTime('+1 day'))->getTimestamp();
$timestamp = $rawTimestamp - ($rawTimestamp % (60 * 60 * 24));
$mainWebsiteActiveRules =
$this->resourceRule->getRulesFromProduct($timestamp, $mainWebsiteId, 1, $productId);
$secondWebsiteActiveRules =
$this->resourceRule->getRulesFromProduct($timestamp, $secondWebsiteId, 1, $productId);
$this->assertCount(1, $mainWebsiteActiveRules);
// Avoid failure when staging is enabled as it removes catalog rule timestamp.
if ((int)$mainWebsiteActiveRules[0]['from_time'] !== 0) {
$this->assertCount(0, $secondWebsiteActiveRules);
}
}
/**
* @magentoDataFixture Magento/CatalogRule/_files/attribute.php
* @magentoDataFixture Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testReindexByIds()
{
$this->prepareProducts();
$this->indexerBuilder->reindexByIds(
[
$this->product->getId(),
$this->productSecond->getId(),
$this->productThird->getId(),
]
);
$this->assertEquals(9.8, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->product->getId()));
$this->assertEquals(
9.8,
$this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productSecond->getId())
);
$this->assertFalse($this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productThird->getId()));
}
/**
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/attribute.php
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testReindexFull()
{
$this->prepareProducts();
$this->indexerBuilder->reindexFull();
$rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->product->getId());
$this->assertEquals(9.8, $rulePrice);
$rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productSecond->getId());
$this->assertEquals(9.8, $rulePrice);
$this->assertFalse($this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productThird->getId()));
}
/**
* Tests restoring triggers on `catalogrule_product_price` table after full reindexing in 'Update by schedule' mode.
*/
public function testRestoringTriggersAfterFullReindex()
{
$tableName = $this->connection->getTableName('catalogrule_product_price');
$this->indexProductProcessor->getIndexer()->setScheduled(false);
$this->assertEquals(0, $this->getTriggersCount($tableName));
$this->indexProductProcessor->getIndexer()->setScheduled(true);
$this->assertGreaterThan(0, $this->getTriggersCount($tableName));
$this->indexerBuilder->reindexFull();
$this->assertGreaterThan(0, $this->getTriggersCount($tableName));
$this->indexProductProcessor->getIndexer()->setScheduled(false);
$this->assertEquals(0, $this->getTriggersCount($tableName));
}
#[
DataFixture('Magento/CatalogRule/_files/simple_product_with_catalog_rule_50_percent_off.php'),
]
public function testReindexByIdForSecondStore(): void
{
$websiteId = $this->storeManager->getWebsite('test')->getId();
$simpleProduct = $this->productRepository->get('simple');
$this->indexerBuilder->reindexById($simpleProduct->getId());
$rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId());
$this->assertEquals(25, $rulePrice);
}
#[
DataFixture('Magento/CatalogRule/_files/simple_product_with_catalog_rule_50_percent_off.php'),
]
public function testReindexByIdsForSecondStore(): void
{
$websiteId = $this->storeManager->getWebsite('test')->getId();
$simpleProduct = $this->productRepository->get('simple');
$this->indexerBuilder->reindexByIds([$simpleProduct->getId()]);
$rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId());
$this->assertEquals(25, $rulePrice);
}
#[
DataFixture('Magento/CatalogRule/_files/simple_product_with_catalog_rule_50_percent_off.php'),
]
public function testReindexFullForSecondStore(): void
{
$websiteId = $this->storeManager->getWebsite('test')->getId();
$simpleProduct = $this->productRepository->get('simple');
$this->indexerBuilder->reindexFull();
$rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId());
$this->assertEquals(25, $rulePrice);
}
/**
* Returns triggers count.
*
* @param string $tableName
* @return int
* @throws \Zend_Db_Statement_Exception
*/
private function getTriggersCount(string $tableName): int
{
return count(
$this->connection->getConnection()
->query('SHOW TRIGGERS LIKE \''. $tableName . '\'')
->fetchAll()
);
}
protected function prepareProducts()
{
$product = $this->product->loadByAttribute('sku', 'simple');
$product->load($product->getId());
$this->product = $product;
$this->product->setStoreId(0)->setData('test_attribute', 'test_attribute_value')->save();
$this->productSecond = clone $this->product;
$this->productSecond->setId(null)->setUrlKey('product-second')->save();
$this->productThird = clone $this->product;
$this->productThird->setId(null)
->setUrlKey('product-third')
->setData('test_attribute', 'NO_test_attribute_value')
->save();
}
}
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\Indexer\Product;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogRule\Model\ResourceModel\Rule;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
use Magento\TestFramework\Helper\Bootstrap;
class PriceTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
/**
* @var Rule
*/
private $resourceRule;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->resourceRule = $this->objectManager->get(Rule::class);
}
/**
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/configurable_product.php
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testPriceApplying()
{
$customerGroupId = 1;
$websiteId = 1;
$simpleProductId = 1;
$collection = Bootstrap::getObjectManager()->create(Collection::class);
$collection->addIdFilter($simpleProductId);
$collection->addPriceData($customerGroupId, $websiteId);
$collection->load();
/** @var \Magento\Catalog\Model\Product $simpleProduct */
$simpleProduct = $collection->getFirstItem();
$simpleProduct->setPriceCalculation(false);
$rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), $websiteId, $customerGroupId, $simpleProductId);
$this->assertEquals($rulePrice, $simpleProduct->getFinalPrice());
$confProductId = 666;
$collection = Bootstrap::getObjectManager()->create(Collection::class);
$collection->addIdFilter($confProductId);
$collection->addPriceData($customerGroupId, $websiteId);
$collection->load();
/** @var \Magento\Catalog\Model\Product $confProduct */
$confProduct = $collection->getFirstItem();
$this->assertEquals($simpleProduct->getFinalPrice(), $confProduct->getMinimalPrice());
}
/**
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/simple_products.php
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
* @magentoAppArea frontend
*/
public function testSortByPrice()
{
$searchCriteria = Bootstrap::getObjectManager()->create(SearchCriteriaInterface::class);
$sortOrder = Bootstrap::getObjectManager()->create(SortOrder::class);
$sortOrder->setField('price')->setDirection(SortOrder::SORT_ASC);
$searchCriteria->setSortOrders([$sortOrder]);
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
$searchResults = $productRepository->getList($searchCriteria);
/** @var \Magento\Catalog\Model\Product[] $products */
$products = array_values($searchResults->getItems());
$product1 = $products[0];
$product1->setPriceCalculation(false);
$this->assertEquals('simple1', $product1->getSku());
$rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $product1->getId());
$this->assertEquals($rulePrice, $product1->getFinalPrice());
}
}
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\Indexer;
use Magento\Catalog\Model\ProductRepository;
use Magento\TestFramework\Helper\Bootstrap;
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
class ProductRuleTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\CatalogRule\Model\ResourceModel\Rule
*/
protected $resourceRule;
protected function setUp(): void
{
$this->resourceRule = Bootstrap::getObjectManager()->get(\Magento\CatalogRule\Model\ResourceModel\Rule::class);
Bootstrap::getObjectManager()->get(\Magento\CatalogRule\Model\Indexer\Product\ProductRuleProcessor::class)
->getIndexer()->isScheduled(false);
}
/**
* @magentoDataFixture Magento/CatalogRule/_files/attribute.php
* @magentoDataFixture Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoAppArea adminhtml
*/
public function testReindexAfterSuitableProductSaving()
{
/** @var ProductRepository $productRepository */
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
ProductRepository::class
);
$product = $productRepository->get('simple');
$product->setData('test_attribute', 'test_attribute_value')->save();
$this->assertEquals(9.8, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $product->getId()));
}
/**
* Checks whether category price rule applies to product with visibility value "Not Visibility Individually".
*
* @magentoDataFixture Magento/CatalogRule/_files/rule_by_category_ids.php
* @magentoDataFixture Magento/Catalog/_files/categories.php
*/
public function testReindexWithProductNotVisibleIndividually()
{
/** @var ProductRepository $productRepository */
$productRepository = Bootstrap::getObjectManager()->create(
ProductRepository::class
);
$product = $productRepository->get('simple-3');
$indexBuilder = Bootstrap::getObjectManager()->get(
IndexBuilder::class
);
$indexBuilder->reindexById($product->getId());
$this->assertEquals(
7.5,
$this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $product->getId()),
"Catalog price rule doesn't apply to product with visibility value \"Not Visibility Individually\""
);
}
}
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\Indexer;
use Magento\TestFramework\Helper\Bootstrap;
/**
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
class RuleProductTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\CatalogRule\Model\Indexer\IndexBuilder
*/
protected $indexBuilder;
/**
* @var \Magento\CatalogRule\Model\ResourceModel\Rule
*/
protected $resourceRule;
protected function setUp(): void
{
$this->indexBuilder = Bootstrap::getObjectManager()->get(
\Magento\CatalogRule\Model\Indexer\IndexBuilder::class
);
$this->resourceRule = Bootstrap::getObjectManager()->get(\Magento\CatalogRule\Model\ResourceModel\Rule::class);
}
/**
* @magentoDbIsolation disabled
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/attribute.php
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testReindexAfterRuleCreation()
{
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Catalog\Model\ProductRepository::class
);
$product = $productRepository->get('simple');
$product->setData('test_attribute', 'test_attribute_value')->save();
$this->assertFalse($this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $product->getId()));
// apply all rules
$this->indexBuilder->reindexFull();
$this->assertEquals(9.8, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $product->getId()));
}
}
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\ResourceModel\Rule;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\CatalogRule\Model\Indexer\IndexBuilder;
use Magento\CatalogRule\Model\ResourceModel\Rule;
use Magento\CatalogRule\Model\ResourceModel\Rule\Collection as RuleCollection;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Catalog\Setup\CategorySetup;
/**
* @magentoAppIsolation enabled
*/
class CollectionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
/**
* @var IndexBuilder
*/
protected $indexBuilder;
/**
* @var Rule
*/
protected $resourceRule;
/**
* @var Collection
*/
protected $resourceRuleCollection;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->indexBuilder = $this->objectManager->get(IndexBuilder::class);
$this->resourceRule = $this->objectManager->get(Rule::class);
$this->resourceRuleCollection = $this->objectManager->get(Collection::class);
}
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
* @magentoDataFixture Magento/CatalogRule/_files/attribute.php
* @magentoDataFixture Magento/CatalogRule/_files/rule_by_attribute.php
* @magentoDataFixture Magento/CatalogRule/_files/two_rules.php
*/
public function testReindexAfterRuleCreation()
{
$this->indexBuilder->reindexFull();
$installer = $this->objectManager->create(CategorySetup::class);
$resourceRuleCollection = $this->objectManager->create(RuleCollection::class);
$resourceRuleCollection->addFilter('is_active', 1);
$this->assertEquals(3, $resourceRuleCollection->count());
$resourceRuleCollection = $this->objectManager->create(RuleCollection::class);
$resourceRuleCollection->addFilter('is_active', 1);
$resourceRuleCollection->addFilter('name', 'test_rule');
$this->assertEquals(1, $resourceRuleCollection->count());
$model = $this->objectManager->create(Attribute::class);
$model->loadByCode($installer->getEntityTypeId('catalog_product'), 'test_attribute');
$model->delete();
$resourceRuleCollection = $this->objectManager->create(RuleCollection::class);
$resourceRuleCollection->addFilter('is_active', 1);
$this->assertEquals(2, $resourceRuleCollection->count());
$resourceRuleCollection = $this->objectManager->create(RuleCollection::class);
$resourceRuleCollection->addFilter('is_active', 1);
$resourceRuleCollection->addFilter('name', 'test_rule');
$this->assertEquals(0, $resourceRuleCollection->count());
}
}
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model;
class RuleTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\CatalogRule\Model\Rule
*/
protected $_object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
$resourceMock = $this->createPartialMock(
\Magento\CatalogRule\Model\ResourceModel\Rule::class,
['getIdFieldName', 'getRulesFromProduct']
);
$resourceMock->expects($this->any())->method('getIdFieldName')->willReturn('id');
$resourceMock->expects(
$this->any()
)->method(
'getRulesFromProduct'
)->willReturn(
$this->_getCatalogRulesFixtures()
);
$this->_object = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\CatalogRule\Model\Rule::class,
['ruleResourceModel' => $resourceMock]
);
}
/**
* @magentoAppIsolation enabled
* @covers \Magento\CatalogRule\Model\Rule::calcProductPriceRule
*/
public function testCalcProductPriceRule()
{
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\Product::class
);
$this->assertEquals($this->_object->calcProductPriceRule($product, 100), 45);
$product->setParentId(true);
$this->assertEquals($this->_object->calcProductPriceRule($product, 50), 50);
}
/**
* Get array with catalog rule data
*
* @return array
*/
protected function _getCatalogRulesFixtures()
{
return [
[
'action_operator' => 'by_percent',
'action_amount' => '50.0000',
'action_stop' => '0'
],
[
'action_operator' => 'by_percent',
'action_amount' => '10.0000',
'action_stop' => '0'
]
];
}
}