Skip to content

Commit

Permalink
OXDEV-8489 Added module status check to block activation
Browse files Browse the repository at this point in the history
- Added logic to check if a module is in the blocklist before activation.
- Throws ModuleActivationBlockedException if the module is blocked.
- Mentioned this change in readme and chanelog files.
  • Loading branch information
RahatHameed committed Aug 15, 2024
1 parent 26a8ab4 commit 1056fcb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Theme list and filtering option on basis of theme name and status
- Module list and filtering option on basis of module name and status
- Activation of given theme by themeId
- Mutations to activate and deactive a module.
- Prevention of deactivation of certain modules mentioned in modules_blocklist.yaml.
- Mutations to de/activate a module.
- Prevention of de/activation of certain modules mentioned in modules_blocklist.yaml.

## [1.1.0] - 2024-07-05
This is stable release for v1.1.0. No changes have been made since v1.1.0-rc.1.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ $ vendor/bin/oe-console oe:module:activate oe_graphql_configuration_access

A good starting point is to check the [How to use section in the GraphQL Base Module](https://github.com/OXID-eSales/graphql-base-module/#how-to-use)

## Blocking modules from deactivation via GraphQL
## Blocking modules from de/activation via GraphQL

The file module_blockilst.yaml contains a list of modules which are necessary to handle configurations or de/activate
modules via GraphQL or should be blocked for deactivation via GraphQL in general. Modules like ``oe_graphql_base`` and
modules via GraphQL or should be blocked for de/activation via GraphQL in general. Modules like ``oe_graphql_base`` and
``oe_graphql_configuration_access`` are listed there.

## Testing
Expand Down
22 changes: 22 additions & 0 deletions src/Module/Exception/ModuleActivationBlockedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\ConfigurationAccess\Module\Exception;

use OxidEsales\GraphQL\Base\Exception\NotFound;

final class ModuleActivationBlockedException extends NotFound
{
private const EXCEPTION_MESSAGE = 'Module "%s" is in the blocklist and cannot be activated.';

public function __construct(string $moduleId)
{
parent::__construct(sprintf(self::EXCEPTION_MESSAGE, $moduleId));
}
}
5 changes: 5 additions & 0 deletions src/Module/Service/ModuleActivationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Bridge\ModuleActivationBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationBlockedException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationBlockedException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationException;

Expand All @@ -29,6 +30,10 @@ public function __construct(
*/
public function activateModule(string $moduleId): bool
{
if ($this->moduleBlocklistService->isModuleBlocked($moduleId)) {
throw new ModuleActivationBlockedException($moduleId);
}

$shopId = $this->context->getCurrentShopId();

try {
Expand Down
33 changes: 27 additions & 6 deletions tests/Unit/Module/Service/ModuleActivationsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Setup\Bridge\ModuleActivationBridgeInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleActivationBlockedException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationBlockedException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Exception\ModuleDeactivationException;
use OxidEsales\GraphQL\ConfigurationAccess\Module\Service\ModuleActivationService;
Expand All @@ -26,7 +27,7 @@ class ModuleActivationsServiceTest extends UnitTestCase
/**
* @dataProvider activationDataProvider
*/
public function testModuleActivationAndDeactivation(
public function testModuleActivationAndDeactivationSuccess(
string $method,
): void {
$shopId = 1;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function testModuleActivationAndDeactivation(
/**
* @dataProvider exceptionDataProvider
*/
public function testModuleActivationAndDeactivationExceptions(
public function testModuleActivationAndDeactivationThrowsExceptions(
string $method,
mixed $exceptionClass
): void {
Expand All @@ -76,8 +77,13 @@ public function testModuleActivationAndDeactivationExceptions(
($method === 'activate') ? $sut->activateModule($moduleId) : $sut->deactivateModule($moduleId);
}

public function testModuleDeactivationBlockedException()
{
/**
* @dataProvider moduleBlockedExceptionDataProvider
*/
public function testModuleActivationAndDeactivationBlockedException(
string $method,
mixed $exceptionClass
) {
$moduleId = uniqid();
$moduleBlockListServiceMock = $this->createMock(ModuleBlocklistServiceInterface::class);
$moduleBlockListServiceMock
Expand All @@ -89,8 +95,23 @@ public function testModuleDeactivationBlockedException()
moduleBlocklistService: $moduleBlockListServiceMock
);

$this->expectException(ModuleDeactivationBlockedException::class);
$sut->deactivateModule($moduleId);
$this->expectException($exceptionClass);
($method === 'activate') ? $sut->activateModule($moduleId) : $sut->deactivateModule($moduleId);
}

public static function moduleBlockedExceptionDataProvider(): \Generator
{
yield 'test activate module blocked exception' => [
'method' => 'activate',
'exceptionClass' => ModuleActivationBlockedException::class

];

yield 'test deactivate module blocked exception' => [
'method' => 'deactivate',
'exceptionClass' => ModuleDeactivationBlockedException::class

];
}

public static function activationDataProvider(): \Generator
Expand Down

0 comments on commit 1056fcb

Please sign in to comment.