Skip to content

Commit

Permalink
Merge pull request #228 from rikwillems/pr/issue-182
Browse files Browse the repository at this point in the history
Add module enabled check to plugin enabled
  • Loading branch information
shochdoerfer committed Oct 16, 2023
2 parents 4e2d8e9 + 143eaac commit e06f7a9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
19 changes: 15 additions & 4 deletions Plugin/AfterLoginPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace BitExpert\ForceCustomerLogin\Plugin;

use BitExpert\ForceCustomerLogin\Controller\ModuleCheck;
use BitExpert\ForceCustomerLogin\Model\Session;
use Magento\Customer\Controller\Account\LoginPost;
use Magento\Framework\App\Config\ScopeConfigInterface;
Expand All @@ -37,14 +38,18 @@ class AfterLoginPlugin
* @var Session
*/
private $session;
/**
* @var string
*/
private $defaultTargetUrl;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var ModuleCheck
*/
private $moduleCheck;
/**
* @var string
*/
private $defaultTargetUrl;

/**
* AfterLoginPlugin constructor.
Expand All @@ -56,10 +61,12 @@ class AfterLoginPlugin
public function __construct(
Session $session,
ScopeConfigInterface $scopeConfig,
ModuleCheck $moduleCheck,
$defaultTargetUrl
) {
$this->session = $session;
$this->scopeConfig = $scopeConfig;
$this->moduleCheck = $moduleCheck;
$this->defaultTargetUrl = $defaultTargetUrl;
}

Expand All @@ -72,6 +79,10 @@ public function __construct(
*/
public function afterExecute(LoginPost $customerAccountLoginController, ResultInterface $resultRedirect)
{
if ($this->moduleCheck->isModuleEnabled() === false) {
return $resultRedirect;
}

if (self::REDIRECT_DASHBOARD_ENABLED ===
$this->scopeConfig->getValue(self::REDIRECT_DASHBOARD_CONFIG)) {
return $resultRedirect;
Expand Down
62 changes: 62 additions & 0 deletions Test/Unit/Plugin/AfterLoginPluginUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace BitExpert\ForceCustomerLogin\Test\Unit\Plugin;

use BitExpert\ForceCustomerLogin\Controller\ModuleCheck;
use BitExpert\ForceCustomerLogin\Model\Session;
use BitExpert\ForceCustomerLogin\Plugin\AfterLoginPlugin;
use Magento\Customer\Controller\Account\LoginPost;
Expand Down Expand Up @@ -41,9 +42,15 @@ public function runAfterExecuteWithRedirectDashboardOptionEnabled()
->with(AfterLoginPlugin::REDIRECT_DASHBOARD_CONFIG)
->willReturn(AfterLoginPlugin::REDIRECT_DASHBOARD_ENABLED);

$moduleCheck = $this->getModuleCheck();
$moduleCheck->expects($this->once())
->method('isModuleEnabled')
->willReturn(true);

$plugin = new AfterLoginPlugin(
$session,
$scopeConfig,
$moduleCheck,
'default-target-url'
);

Expand All @@ -70,9 +77,15 @@ public function runAfterExecuteWithSpecificTargetUrl()
->with(AfterLoginPlugin::REDIRECT_DASHBOARD_CONFIG)
->willReturn(AfterLoginPlugin::REDIRECT_DASHBOARD_DISABLED);

$moduleCheck = $this->getModuleCheck();
$moduleCheck->expects($this->once())
->method('isModuleEnabled')
->willReturn(true);

$plugin = new AfterLoginPlugin(
$session,
$scopeConfig,
$moduleCheck,
'default-target-url'
);

Expand Down Expand Up @@ -101,9 +114,15 @@ public function runAfterExecuteWithDefaultTargetUrl()
->with(AfterLoginPlugin::REDIRECT_DASHBOARD_CONFIG)
->willReturn(AfterLoginPlugin::REDIRECT_DASHBOARD_DISABLED);

$moduleCheck = $this->getModuleCheck();
$moduleCheck->expects($this->once())
->method('isModuleEnabled')
->willReturn(true);

$plugin = new AfterLoginPlugin(
$session,
$scopeConfig,
$moduleCheck,
'default-target-url'
);

Expand All @@ -116,6 +135,39 @@ public function runAfterExecuteWithDefaultTargetUrl()
$this->assertEquals($redirect, $plugin->afterExecute($loginPost, $redirect));
}

/**
* @test
*/
public function runAfterExecuteWithModuleDisabled()
{
$session = $this->getSession();
$session->expects($this->never())
->method('getAfterLoginReferer');

$scopeConfig = $this->getScopeConfig();
$scopeConfig->expects($this->never())
->method('getValue');

$moduleCheck = $this->getModuleCheck();
$moduleCheck->expects($this->once())
->method('isModuleEnabled')
->willReturn(false);

$plugin = new AfterLoginPlugin(
$session,
$scopeConfig,
$moduleCheck,
'default-target-url'
);

$loginPost = $this->getLoginPost();
$redirect = $this->getRedirect();
$redirect->expects($this->never())
->method('setUrl');

$this->assertEquals($redirect, $plugin->afterExecute($loginPost, $redirect));
}

/**
* @return MockObject|Session
*/
Expand All @@ -139,6 +191,16 @@ private function getScopeConfig()
->getMock();
}

/**
* @return MockObject|ModuleCheck
*/
private function getModuleCheck()
{
return $this->getMockBuilder(ModuleCheck::class)
->disableOriginalConstructor()
->getMock();
}

/**
* @return MockObject|LoginPost
*/
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ parameters:
- Validator
- view
magento:
checkServiceContracts: false
checkServiceContracts: false
ignoreErrors:
-
message: '~Parameter #1 \$modelId of method Magento\\Framework\\Model\\AbstractModel::load\(\) expects int, string given~'
Expand Down

0 comments on commit e06f7a9

Please sign in to comment.