Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module enabled check to plugin enabled #228

Merged
merged 5 commits into from
Oct 16, 2023
Merged
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
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
Loading