ltsecurityinc安防产品在线商城代码
This commit is contained in:
+77
@@ -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],
|
||||
];
|
||||
}
|
||||
}
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
<?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\Helper\Bootstrap;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @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()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @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()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @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.
|
||||
*
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
<?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\ProductRepository;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\Collection;
|
||||
use Magento\CatalogRule\Model\Indexer\IndexBuilder;
|
||||
use Magento\CatalogRule\Model\ResourceModel\Rule;
|
||||
use Magento\Framework\Api\SearchCriteriaInterface;
|
||||
use Magento\Framework\Api\SortOrder;
|
||||
use Magento\Store\Api\WebsiteRepositoryInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class PriceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var Rule
|
||||
*/
|
||||
private $resourceRule;
|
||||
|
||||
/**
|
||||
* @var WebsiteRepositoryInterface
|
||||
*/
|
||||
private $websiteRepository;
|
||||
|
||||
/**
|
||||
* @var ProductRepository
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var IndexBuilder
|
||||
*/
|
||||
private $indexerBuilder;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->resourceRule = $this->objectManager->get(Rule::class);
|
||||
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
|
||||
$this->productRepository = $this->objectManager->create(ProductRepository::class);
|
||||
$this->indexerBuilder = $this->objectManager->get(IndexBuilder::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_product_with_catalog_rule_50_percent_off.php
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @return void
|
||||
*/
|
||||
public function testPriceForSecondStore():void
|
||||
{
|
||||
$websiteId = $this->websiteRepository->get('test')->getId();
|
||||
$simpleProduct = $this->productRepository->get('simple');
|
||||
$simpleProduct->setPriceCalculation(true);
|
||||
$this->assertEquals('simple', $simpleProduct->getSku());
|
||||
$this->assertFalse(
|
||||
$this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId())
|
||||
);
|
||||
$this->indexerBuilder->reindexById($simpleProduct->getId());
|
||||
$this->assertEquals(
|
||||
25,
|
||||
$this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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()));
|
||||
}
|
||||
}
|
||||
+1104
File diff suppressed because it is too large
Load Diff
+82
@@ -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'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user