Skip to content

Commit

Permalink
feat: [MINT-2591] enable/disable SP at the store view level (#141)
Browse files Browse the repository at this point in the history
* feat: [MINT-2591] enable/disable SP at the store view level

* remove logger annotation

* auto-changelog

* update aftergetfilesplugin comment

* correct logic in comment

* auto-changelog

---------

Co-authored-by: magento-bot <[email protected]>
  • Loading branch information
milesmcleod and magento-bot authored Sep 5, 2024
1 parent 7f4e09d commit ffb9266
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Plugin/Checkout/LayoutProcessorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function afterProcess(
LayoutProcessorInterface $subject,
array $jsLayout
) {
if ($this->extendService->isEnabled()) {
if ($this->extendService->isShippingProtectionEnabled()) {
// Add the SP offer to the shipping address layout
$this->addShippingProtectionOfferToShippingAddressLayout($jsLayout);
// Add the SP summary line to the sidebar layout
Expand Down
10 changes: 8 additions & 2 deletions Plugin/RequireJs/AfterGetFilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ public function afterGetFiles(
Aggregated $subject,
$result
) {
// if the default Extend isEnabled boolean config value is false, remove the Extend files from the requirejs config
if (!$this->extendService->isEnabled()) {
// if the default Extend isEnabled boolean config value is false, or if both PP and SP
// are disabled for the given store context, remove the Extend files from the requirejs config.
if (!$this->extendService->isEnabled() ||
(
!$this->extendService->isProductProtectionEnabled() &&
!$this->extendService->isShippingProtectionEnabled()
)
) {
foreach ($result as $key => &$file) {
if ($file->getModule() == "Extend_Integration") {
unset($result[$key]);
Expand Down
43 changes: 41 additions & 2 deletions Service/Extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace Extend\Integration\Service;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class Extend
{
Expand Down Expand Up @@ -69,15 +71,21 @@ class Extend
/** @var ScopeConfigInterface */
private $scopeConfig;

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

/**
* Extend constructor
*
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
) {
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -92,12 +100,43 @@ public static function isProductionProtectionSku(string $sku): bool
}

/**
* Check if Extend module is enabled
* Check if Extend module is enabled. No need to use any scope for this value, as it is only settable at the global
* level.
*
* @return boolean
*/
public function isEnabled(): bool
{
return (bool)$this->scopeConfig->getValue(self::ENABLE_EXTEND);
}

/**
* Check if product protection is enabled. We need to retrieve this value using the store scope
* because this config value can be set at the store view level.
*
* @return boolean
*/
public function isProductProtectionEnabled(): bool
{
return $this->isEnabled() && (bool)$this->scopeConfig->getValue(
self::ENABLE_PRODUCT_PROTECTION,
ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()->getCode()
);
}

/**
* Check if product protection is enabled. We need to retrieve this value using the store scope
* because this config value can be set at the store view level.
*
* @return boolean
*/
public function isShippingProtectionEnabled(): bool
{
return $this->isEnabled() && (bool)$this->scopeConfig->getValue(
self::ENABLE_SHIPPING_PROTECTION,
ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()->getCode()
);
}
}
8 changes: 4 additions & 4 deletions Test/Unit/Plugin/Checkout/LayoutProcessorPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ protected function setUp(): void
$this->sidebarTotalsLayout = &$this->jsLayoutMock['components']['checkout']['children']['sidebar']['children']['summary']['children']['totals']['children'];
}

public function testAddsShippingProtectionLayoutsWhenExtendIsEnabled()
public function testAddsShippingProtectionLayoutsWhenExtendShippingProtectionIsEnabled()
{
$this->extendServiceMock->method('isEnabled')->willReturn(true);
$this->extendServiceMock->method('isShippingProtectionEnabled')->willReturn(true);
$this->testSubject->afterProcess($this->layoutProcessorSubjectMock, $this->jsLayoutMock);
$this->expectShippingProtectionComponentsToBeAddedToJSLayout();
}

public function testDoesNothingWhenExtendIsDisabled()
public function testDoesNothingWhenExtendShippingProtectionIsDisabled()
{
$this->extendServiceMock->method('isEnabled')->willReturn(false);
$this->extendServiceMock->method('isShippingProtectionEnabled')->willReturn(false);
$this->testSubject->afterProcess($this->layoutProcessorSubjectMock, $this->jsLayoutMock);
$this->expectJsLayoutNotToBeChanged();
}
Expand Down
33 changes: 31 additions & 2 deletions Test/Unit/Plugin/RequireJs/AfterGetFilesPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,44 @@ protected function setUp(): void
/* =================================================================================================== */


public function testDoesNotFilterOutExtendFilesWhenExtendIsEnabled()
public function testDoesNotFilterOutExtendFilesWhenOnlyPpIsEnabled()
{
$this->extendServiceMock->method('isEnabled')->willReturn(true);
$this->extendServiceMock->method('isProductProtectionEnabled')->willReturn(false);
$this->extendServiceMock->method('isShippingProtectionEnabled')->willReturn(true);
$this->expectUnfilteredFiles();
}

public function testFiltersOutExtendFilesWhenExtendIsDisabled()
public function testDoesNotFilterOutExtendFilesWhenOnlySpIsEnabled()
{
$this->extendServiceMock->method('isEnabled')->willReturn(true);
$this->extendServiceMock->method('isProductProtectionEnabled')->willReturn(true);
$this->extendServiceMock->method('isShippingProtectionEnabled')->willReturn(false);
$this->expectUnfilteredFiles();
}

public function testDoesNotFilterOutExtendFilesWhenOnlyBothSpAndPpAreEnabled()
{
$this->extendServiceMock->method('isEnabled')->willReturn(true);
$this->extendServiceMock->method('isProductProtectionEnabled')->willReturn(true);
$this->extendServiceMock->method('isShippingProtectionEnabled')->willReturn(true);
$this->expectUnfilteredFiles();
}

public function testFiltersOutExtendFilesWhenSpAndPpAreDisabled()
{
$this->extendServiceMock->method('isEnabled')->willReturn(true);
$this->extendServiceMock->method('isProductProtectionEnabled')->willReturn(false);
$this->extendServiceMock->method('isShippingProtectionEnabled')->willReturn(false);
// expect the extend module file to be removed
$this->expectFilteredFiles();
}

public function testFiltersOutExtendFilesWhenExtendIsGloballyDisabledRegardlessOfOtherConfigValues()
{
$this->extendServiceMock->method('isEnabled')->willReturn(false);
$this->extendServiceMock->method('isProductProtectionEnabled')->willReturn(true);
$this->extendServiceMock->method('isShippingProtectionEnabled')->willReturn(true);
// expect the extend module file to be removed
$this->expectFilteredFiles();
}
Expand Down
97 changes: 93 additions & 4 deletions Test/Unit/Service/ExtendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,117 @@

namespace Extend\Integration\Test\Unit\Service;

use Extend\Integration\Service\Extend;
use Extend\Integration\Service\Extend as ExtendService;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Api\Data\StoreInterface as Store;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class ExtendTest extends TestCase
{

/**
* @var ExtendService | MockObject
*/
private $testSubject;

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

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

/**
* @var Store | MockObject
*/
private $store;

private $storeCode = 'default';

protected function setUp(): void
{
$this->store = $this->createConfiguredMock(Store::class, [
'getCode' => $this->storeCode,
]);
$this->storeManagerMock = $this->createStub(StoreManagerInterface::class);
$this->storeManagerMock->method('getStore')->willReturn($this->store);
$this->scopeConfigMock = $this->createStub(ScopeConfigInterface::class);
$this->testSubject = new ExtendService(
$this->scopeConfigMock,
$this->storeManagerMock
);
}

public function testIsProductProtectionSkuFalseWithArbitrarySku()
{
$this->assertFalse(Extend::isProductionProtectionSku('ABC123'));
$this->assertFalse($this->testSubject->isProductionProtectionSku('ABC123'));
}

public function testIsProductProtectionSkuTrueWithLegacyExtendProductProtectionSku()
{
$this->assertTrue(Extend::isProductionProtectionSku(Extend::WARRANTY_PRODUCT_LEGACY_SKU));
$this->assertTrue($this->testSubject->isProductionProtectionSku(ExtendService::WARRANTY_PRODUCT_LEGACY_SKU));
}

public function testIsProductProtectionSkuTrueWithExtendProductProtectionSku()
{
$this->assertTrue(Extend::isProductionProtectionSku(Extend::WARRANTY_PRODUCT_SKU));
$this->assertTrue($this->testSubject->isProductionProtectionSku(ExtendService::WARRANTY_PRODUCT_SKU));
}

public function testIsEnabledReturnsTrueIfExtendIsEnabled()
{
$this->scopeConfigMock->method('getValue')->willReturnMap([
[ExtendService::ENABLE_EXTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, '1'],
]);
$this->assertTrue($this->testSubject->isEnabled());
}

public function testIsEnabledReturnsFalseIfExtendIsDisabled()
{
$this->scopeConfigMock->method('getValue')->willReturnMap([
[ExtendService::ENABLE_EXTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, '0'],
]);
$this->assertFalse($this->testSubject->isEnabled());
}

public function testIsProductProtectionEnabledReturnsTrueIfExtendIsEnabledAndProductProtectionIsEnabled()
{
$this->scopeConfigMock->method('getValue')->willReturnMap([
[ExtendService::ENABLE_EXTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, '1'],
[ExtendService::ENABLE_PRODUCT_PROTECTION, ScopeInterface::SCOPE_STORE, $this->storeCode, '1'],
]);
$this->assertTrue($this->testSubject->isProductProtectionEnabled());
}

public function testIsProductProtectionEnabledReturnsFalseIfExtendIsDisabled()
{
$this->scopeConfigMock->method('getValue')->willReturnMap([
[ExtendService::ENABLE_EXTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, '0'],
[ExtendService::ENABLE_PRODUCT_PROTECTION, ScopeInterface::SCOPE_STORE, $this->storeCode, '1'],
]);
$this->assertFalse($this->testSubject->isProductProtectionEnabled());
}

public function testIsProductProtectionEnabledReturnsFalseIfExtendIsEnabledButProductProtectionIsDisabled()
{
$this->scopeConfigMock->method('getValue')->willReturnMap([
[ExtendService::ENABLE_EXTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, '1'],
[ExtendService::ENABLE_PRODUCT_PROTECTION, ScopeInterface::SCOPE_STORE, $this->storeCode, '0'],
]);
$this->assertFalse($this->testSubject->isProductProtectionEnabled());
}

public function testIsProductProtectionEnabledReturnsFalseIfExtendAndProductProtectionAreExplicitlyDisabled()
{
$this->scopeConfigMock->method('getValue')->willReturnMap([
[ExtendService::ENABLE_EXTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null, '0'],
[ExtendService::ENABLE_PRODUCT_PROTECTION, ScopeInterface::SCOPE_STORE, $this->storeCode, '0'],
]);
$this->assertFalse($this->testSubject->isProductProtectionEnabled());
}
}
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<resource>Magento_Catalog::config_catalog</resource>
<group id="shipping_protection" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Shipping Protection</label>
<field id="enable" translate="label comment" type="select" sortOrder="10" showInDefault="1" canRestore="1">
<field id="enable" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable Shipping Protection</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<frontend_model>Extend\Integration\Model\Config\Frontend\EnableShippingProtection</frontend_model>
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/layout/checkout_cart_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="checkout.cart.totals.container">
<block name="extend.checkout.cart.totals.shipping.protection" template="Extend_Integration::checkout/cart/totals/shipping-protection.phtml" ifconfig="extend/integration/enable">
<block name="extend.checkout.cart.totals.shipping.protection" template="Extend_Integration::checkout/cart/totals/shipping-protection.phtml" ifconfig="extend_plans/shipping_protection/enable">
<arguments>
<argument name="viewModel" xsi:type="object">Extend\Integration\ViewModel\EnvironmentAndExtendStoreUuid</argument>
</arguments>
Expand Down
2 changes: 1 addition & 1 deletion view/frontend/layout/checkout_index_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block name="extend-environment-store-uuid" template="Extend_Integration::checkout/sp-quote-config.phtml" ifconfig="extend/integration/enable">
<block name="extend-environment-store-uuid" template="Extend_Integration::checkout/sp-quote-config.phtml" ifconfig="extend_plans/shipping_protection/enable">
<arguments>
<argument name="viewModel" xsi:type="object">Extend\Integration\ViewModel\EnvironmentAndExtendStoreUuid</argument>
</arguments>
Expand Down

0 comments on commit ffb9266

Please sign in to comment.