Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@

namespace Magento\SalesRule\Model\Plugin;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Quote\Model\Quote\Config;
use Magento\SalesRule\Model\ResourceModel\Rule as RuleResource;

class QuoteConfigProductAttributes
{
/**
* Cache key for active salesrule attributes
*/
private const CACHE_KEY = 'salesrule_active_product_attributes';

/**
* Cache tag for salesrule attributes
*/
private const CACHE_TAG = 'salesrule';

/**
* @var RuleResource
*/
Expand All @@ -21,17 +33,36 @@ class QuoteConfigProductAttributes
*/
private $activeAttributeCodes;

/**
* @var CacheInterface
*/
private $cache;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @param RuleResource $ruleResource
* @param CacheInterface $cache
* @param SerializerInterface $serializer
*/
public function __construct(RuleResource $ruleResource)
{
public function __construct(
RuleResource $ruleResource,
CacheInterface $cache,
SerializerInterface $serializer
) {
$this->ruleResource = $ruleResource;
$this->cache = $cache;
$this->serializer = $serializer;
}

/**
* Append sales rule product attribute keys to select by quote item collection
*
* Uses cache to avoid database queries on every request.
*
* @param Config $subject
* @param array $attributeKeys
*
Expand All @@ -41,7 +72,21 @@ public function __construct(RuleResource $ruleResource)
public function afterGetProductAttributes(Config $subject, array $attributeKeys): array
{
if ($this->activeAttributeCodes === null) {
$this->activeAttributeCodes = array_column($this->ruleResource->getActiveAttributes(), 'attribute_code');
$cachedData = $this->cache->load(self::CACHE_KEY);

if ($cachedData !== false) {
$this->activeAttributeCodes = $this->serializer->unserialize($cachedData);
} else {
$this->activeAttributeCodes = array_column(
$this->ruleResource->getActiveAttributes(),
'attribute_code'
);
$this->cache->save(
$this->serializer->serialize($this->activeAttributeCodes),
self::CACHE_KEY,
[self::CACHE_TAG]
);
}
}

return array_merge($attributeKeys, $this->activeAttributeCodes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*/

namespace Magento\SalesRule\Observer;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

/**
* Clear cached salesrule product attributes when rules are saved
*/
class ClearProductAttributesCacheObserver implements ObserverInterface
{
/**
* Cache key for active salesrule attributes
*/
private const CACHE_KEY = 'salesrule_active_product_attributes';

/**
* @var CacheInterface
*/
private $cache;

/**
* @param CacheInterface $cache
*/
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}

/**
* Clear salesrule product attributes cache
*
* @param Observer $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(Observer $observer): void
{
$this->cache->remove(self::CACHE_KEY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\SalesRule\Test\Unit\Model\Plugin;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Quote\Model\Quote\Config;
use Magento\SalesRule\Model\Plugin\QuoteConfigProductAttributes;
Expand All @@ -17,7 +19,7 @@
class QuoteConfigProductAttributesTest extends TestCase
{
/**
* @var QuoteConfigProductAttributes|MockObject
* @var QuoteConfigProductAttributes
*/
protected $plugin;

Expand All @@ -26,24 +28,67 @@ class QuoteConfigProductAttributesTest extends TestCase
*/
protected $ruleResource;

/**
* @var CacheInterface|MockObject
*/
protected $cache;

/**
* @var SerializerInterface|MockObject
*/
protected $serializer;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->ruleResource = $this->createMock(Rule::class);
$this->cache = $this->createMock(CacheInterface::class);
$this->serializer = $this->createMock(SerializerInterface::class);

$this->plugin = $objectManager->getObject(
QuoteConfigProductAttributes::class,
[
'ruleResource' => $this->ruleResource
'ruleResource' => $this->ruleResource,
'cache' => $this->cache,
'serializer' => $this->serializer
]
);
}

public function testAfterGetProductAttributes()
public function testAfterGetProductAttributesWithCache()
{
$subject = $this->createMock(Config::class);
$attributeCode = 'code of the attribute';
$expected = [0 => $attributeCode];
$serializedData = '["' . $attributeCode . '"]';

$this->cache->expects($this->once())
->method('load')
->with('salesrule_active_product_attributes')
->willReturn($serializedData);

$this->serializer->expects($this->once())
->method('unserialize')
->with($serializedData)
->willReturn([$attributeCode]);

$this->ruleResource->expects($this->never())
->method('getActiveAttributes');

$this->assertEquals($expected, $this->plugin->afterGetProductAttributes($subject, []));
}

public function testAfterGetProductAttributesWithoutCache()
{
$subject = $this->createMock(Config::class);
$attributeCode = 'code of the attribute';
$expected = [0 => $attributeCode];
$serializedData = '["' . $attributeCode . '"]';

$this->cache->expects($this->once())
->method('load')
->with('salesrule_active_product_attributes')
->willReturn(false);

$this->ruleResource->expects($this->once())
->method('getActiveAttributes')
Expand All @@ -53,6 +98,15 @@ public function testAfterGetProductAttributes()
]
);

$this->serializer->expects($this->once())
->method('serialize')
->with([$attributeCode])
->willReturn($serializedData);

$this->cache->expects($this->once())
->method('save')
->with($serializedData, 'salesrule_active_product_attributes', ['salesrule']);

$this->assertEquals($expected, $this->plugin->afterGetProductAttributes($subject, []));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\SalesRule\Test\Unit\Observer;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\Event\Observer;
use Magento\SalesRule\Observer\ClearProductAttributesCacheObserver;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ClearProductAttributesCacheObserverTest extends TestCase
{
/**
* @var ClearProductAttributesCacheObserver
*/
private $observer;

/**
* @var CacheInterface|MockObject
*/
private $cache;

protected function setUp(): void
{
$this->cache = $this->createMock(CacheInterface::class);
$this->observer = new ClearProductAttributesCacheObserver($this->cache);
}

public function testExecuteClearsCache()
{
$event = $this->createMock(Observer::class);

$this->cache->expects($this->once())
->method('remove')
->with('salesrule_active_product_attributes');

$this->observer->execute($event);
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/SalesRule/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
</event>
<event name="salesrule_rule_save_commit_after">
<observer name="salesrule_quote_recollect_totals_on_disabled" instance="\Magento\SalesRule\Observer\RuleQuoteRecollectTotalsObserver" />
<observer name="clear_product_attributes_cache" instance="Magento\SalesRule\Observer\ClearProductAttributesCacheObserver" />
</event>
<event name="salesrule_rule_delete_commit_after">
<observer name="salesrule_quote_recollect_totals_on_delete" instance="\Magento\SalesRule\Observer\RuleQuoteRecollectTotalsObserver" />
<observer name="clear_product_attributes_cache_on_delete" instance="Magento\SalesRule\Observer\ClearProductAttributesCacheObserver" />
</event>
<event name="sales_quote_collect_totals_before">
<observer name="salesrule_sales_quote_collect_totals_before" instance="\Magento\SalesRule\Observer\QuoteResetAppliedRulesObserver" />
Expand Down