diff --git a/src/Application/Controller/Admin/d3totpadminlogin.php b/src/Application/Controller/Admin/d3totpadminlogin.php index 0135ca7..8e17543 100644 --- a/src/Application/Controller/Admin/d3totpadminlogin.php +++ b/src/Application/Controller/Admin/d3totpadminlogin.php @@ -91,7 +91,7 @@ class d3totpadminlogin extends AdminController $this->addTplParam('selectedChLanguage', Registry::getRequest()->getRequestEscapedParameter('chlanguage')); /** @var d3_totp_LoginController $loginController */ - $loginController = oxNew(LoginController::class); + $loginController = $this->d3GetLoginController(); $loginController->d3totpAfterLoginSetLanguage(); return parent::render(); @@ -174,7 +174,7 @@ class d3totpadminlogin extends AdminController $session->deleteVariable(d3totp_conf::SESSION_ADMIN_CURRENTUSER); /** @var d3_totp_LoginController $loginController */ - $loginController = oxNew(LoginController::class); + $loginController = $this->d3GetLoginController(); $loginController->d3totpAfterLogin(); return "admin_start"; @@ -221,4 +221,12 @@ class d3totpadminlogin extends AdminController { return Registry::getLogger(); } + + /** + * @return d3_totp_LoginController + */ + public function d3GetLoginController(): LoginController + { + return oxNew(LoginController::class); + } } \ No newline at end of file diff --git a/src/IntelliSenseHelper.php b/src/IntelliSenseHelper.php index 12ffcc2..86aa605 100644 --- a/src/IntelliSenseHelper.php +++ b/src/IntelliSenseHelper.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\Modules\Application\Component { diff --git a/src/Modules/Application/Controller/Admin/d3_totp_LoginController.php b/src/Modules/Application/Controller/Admin/d3_totp_LoginController.php index 9fedd96..5b4bab7 100644 --- a/src/Modules/Application/Controller/Admin/d3_totp_LoginController.php +++ b/src/Modules/Application/Controller/Admin/d3_totp_LoginController.php @@ -50,11 +50,11 @@ class d3_totp_LoginController extends d3_totp_LoginController_parent */ public function checklogin() { - Registry::getSession()->setVariable( + $this->d3TotpGetSession()->setVariable( d3totp_conf::SESSION_ADMIN_PROFILE, Registry::getRequest()->getRequestEscapedParameter('profile') ); - Registry::getSession()->setVariable( + $this->d3TotpGetSession()->setVariable( d3totp_conf::SESSION_ADMIN_CHLANGUAGE, Registry::getRequest()->getRequestEscapedParameter('chlanguage') ); @@ -65,17 +65,17 @@ class d3_totp_LoginController extends d3_totp_LoginController_parent public function d3totpAfterLogin() { - $myUtilsServer = Registry::getUtilsServer(); - $sProfile = Registry::getSession()->getVariable(d3totp_conf::SESSION_ADMIN_PROFILE); + $myUtilsServer = $this->d3TotpGetUtilsServer(); + $sProfile = $this->d3TotpGetSession()->getVariable(d3totp_conf::SESSION_ADMIN_PROFILE); // #533 if (isset($sProfile)) { - $aProfiles = Registry::getSession()->getVariable("aAdminProfiles"); + $aProfiles = $this->d3TotpGetSession()->getVariable("aAdminProfiles"); if ($aProfiles && isset($aProfiles[$sProfile])) { // setting cookie to store last locally used profile $myUtilsServer->setOxCookie("oxidadminprofile", $sProfile . "@" . implode("@", $aProfiles[$sProfile]), time() + 31536000, "/"); - Registry::getSession()->setVariable("profile", $aProfiles[$sProfile]); - Registry::getSession()->deleteVariable(d3totp_conf::SESSION_ADMIN_PROFILE); + $this->d3TotpGetSession()->setVariable("profile", $aProfiles[$sProfile]); + $this->d3TotpGetSession()->deleteVariable(d3totp_conf::SESSION_ADMIN_PROFILE); } } else { //deleting cookie info, as setting profile to default @@ -83,21 +83,21 @@ class d3_totp_LoginController extends d3_totp_LoginController_parent } $this->d3totpAfterLoginSetLanguage(); - Registry::getSession()->deleteVariable(d3totp_conf::SESSION_ADMIN_CHLANGUAGE); + $this->d3TotpGetSession()->deleteVariable(d3totp_conf::SESSION_ADMIN_CHLANGUAGE); } public function d3totpAfterLoginSetLanguage() { - $myUtilsServer = Registry::getUtilsServer(); - $iLang = Registry::getSession()->getVariable(d3totp_conf::SESSION_ADMIN_CHLANGUAGE); + $myUtilsServer = $this->d3TotpGetUtilsServer(); + $iLang = $this->d3TotpGetSession()->getVariable(d3totp_conf::SESSION_ADMIN_CHLANGUAGE); - $aLanguages = Registry::getLang()->getAdminTplLanguageArray(); + $aLanguages = $this->d3TotpGetLangObject()->getAdminTplLanguageArray(); if (!isset($aLanguages[$iLang])) { $iLang = key($aLanguages); } $myUtilsServer->setOxCookie("oxidadminlanguage", $aLanguages[$iLang]->abbr, time() + 31536000, "/"); - Registry::getLang()->setTplLanguage($iLang); + $this->d3TotpGetLangObject()->setTplLanguage( $iLang); } /** @@ -117,4 +117,20 @@ class d3_totp_LoginController extends d3_totp_LoginController_parent { return oxNew( User::class ); } + + /** + * @return object|\OxidEsales\Eshop\Core\UtilsServer + */ + protected function d3TotpGetUtilsServer() + { + return Registry::getUtilsServer(); + } + + /** + * @return object|\OxidEsales\Eshop\Core\Language + */ + protected function d3TotpGetLangObject() + { + return Registry::getLang(); + } } diff --git a/src/Modules/Core/totpSystemEventHandler.php b/src/Modules/Core/totpSystemEventHandler.php index f2582e3..bcf1aca 100644 --- a/src/Modules/Core/totpSystemEventHandler.php +++ b/src/Modules/Core/totpSystemEventHandler.php @@ -15,20 +15,24 @@ declare(strict_types=1); namespace D3\Totp\Modules\Core; +use D3\TestingTools\Production\IsMockable; use D3\Totp\Application\Model\d3totp; use D3\Totp\Application\Model\d3totp_conf; use D3\Totp\Modules\Application\Model\d3_totp_user; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; +use OxidEsales\Eshop\Core\Utils; class totpSystemEventHandler extends totpSystemEventHandler_parent { + use IsMockable; + public function onAdminLogin() { $this->d3RequestTotp(); - parent::onAdminLogin(); + $this->d3CallMockableParent('onAdminLogin'); } protected function d3requestTotp() @@ -44,7 +48,7 @@ class totpSystemEventHandler extends totpSystemEventHandler_parent $this->d3TotpGetSession()->setVariable(d3totp_conf::SESSION_ADMIN_CURRENTUSER, $userId); - Registry::getUtils()->redirect( + $this->getUtilsObject()->redirect( 'index.php?cl=d3totpadminlogin&'. 'profile='.$this->d3TotpGetSession()->getVariable(d3totp_conf::SESSION_ADMIN_PROFILE).'&'. 'chlanguage='.$this->d3TotpGetSession()->getVariable(d3totp_conf::SESSION_ADMIN_CHLANGUAGE) @@ -60,6 +64,14 @@ class totpSystemEventHandler extends totpSystemEventHandler_parent return oxNew(d3totp::class); } + /** + * @return Utils + */ + public function getUtilsObject(): Utils + { + return Registry::getUtils(); + } + /** * @return Session */ diff --git a/src/tests/unit/Application/Controller/Admin/d3totpadminloginTest.php b/src/tests/unit/Application/Controller/Admin/d3totpadminloginTest.php index 1eb7e7e..34c5137 100644 --- a/src/tests/unit/Application/Controller/Admin/d3totpadminloginTest.php +++ b/src/tests/unit/Application/Controller/Admin/d3totpadminloginTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Controller\Admin; use D3\TestingTools\Development\CanAccessRestricted; @@ -19,8 +21,10 @@ use D3\Totp\Application\Model\d3backupcodelist; use D3\Totp\Application\Model\d3totp; use D3\Totp\Application\Model\d3totp_conf; use D3\Totp\Application\Model\Exceptions\d3totp_wrongOtpException; +use D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController; use D3\Totp\Modules\Application\Model\d3_totp_user; use D3\Totp\tests\unit\d3TotpUnitTestCase; +use OxidEsales\Eshop\Application\Controller\Admin\LoginController; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; @@ -227,17 +231,26 @@ class d3totpadminloginTest extends d3TotpUnitTestCase ->with($this->identicalTo('index.php?cl='.$redirect)) ->willReturn(true); + /** @var d3_totp_LoginController|MockObject $loginControllerMock */ + $loginControllerMock = $this->getMockBuilder(LoginController::class) + ->onlyMethods(['d3totpAfterLoginSetLanguage']) + ->getMock(); + $loginControllerMock->expects($this->once())->method('d3totpAfterLoginSetLanguage') + ->willReturn(true); + /** @var d3totpadminlogin|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3totpadminlogin::class) ->onlyMethods([ 'isTotpIsNotRequired', 'isTotpLoginNotPossible', - 'd3TotpGetUtils' + 'd3TotpGetUtils', + 'd3GetLoginController' ]) ->getMock(); $oControllerMock->method('isTotpIsNotRequired')->willReturn($totpRequired); $oControllerMock->method('isTotpLoginNotPossible')->willReturn($totpNotPossible); $oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock); + $oControllerMock->method('d3GetLoginController')->willReturn($loginControllerMock); $this->_oController = $oControllerMock; @@ -406,18 +419,26 @@ class d3totpadminloginTest extends d3TotpUnitTestCase $oSessionMock->expects($this->never())->method('setVariable')->willReturn(false); $oSessionMock->expects($this->never())->method('deleteVariable')->willReturn(false); + /** @var d3_totp_LoginController|MockObject $loginControllerMock */ + $loginControllerMock = $this->getMockBuilder(LoginController::class) + ->onlyMethods(['d3totpAfterLogin']) + ->getMock(); + $loginControllerMock->expects($this->never())->method('d3totpAfterLogin')->willReturn(true); + /** @var d3totpadminlogin|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3totpadminlogin::class) ->onlyMethods([ 'getLogger', 'd3TotpHasValidTotp', - 'd3TotpGetSession' + 'd3TotpGetSession', + 'd3GetLoginController' ]) ->getMock(); $oControllerMock->method('d3TotpHasValidTotp') ->willThrowException(oxNew(d3totp_wrongOtpException::class)); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('getLogger')->willReturn($loggerMock); + $oControllerMock->method('d3GetLoginController')->willReturn($loginControllerMock); $this->_oController = $oControllerMock; @@ -461,17 +482,25 @@ class d3totpadminloginTest extends d3TotpUnitTestCase $oSessionMock->expects($this->atLeastOnce())->method('setVariable')->willReturn(false); $oSessionMock->expects($this->atLeastOnce())->method('deleteVariable')->willReturn(false); + /** @var d3_totp_LoginController|MockObject $loginControllerMock */ + $loginControllerMock = $this->getMockBuilder(LoginController::class) + ->onlyMethods(['d3totpAfterLogin']) + ->getMock(); + $loginControllerMock->expects($this->once())->method('d3totpAfterLogin')->willReturn(true); + /** @var d3totpadminlogin|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3totpadminlogin::class) ->onlyMethods([ 'getLogger', 'd3TotpHasValidTotp', - 'd3TotpGetSession' + 'd3TotpGetSession', + 'd3GetLoginController' ]) ->getMock(); $oControllerMock->method('d3TotpHasValidTotp')->willReturn(true); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('getLogger')->willReturn($loggerMock); + $oControllerMock->method('d3GetLoginController')->willReturn($loginControllerMock); $this->_oController = $oControllerMock; @@ -612,4 +641,20 @@ class d3totpadminloginTest extends d3TotpUnitTestCase ) ); } + + /** + * @test + * @throws ReflectionException + * @covers \D3\Totp\Application\Controller\Admin\d3totpadminlogin::d3GetLoginControllert + */ + public function d3GetLoginControllerReturnsRightObject() + { + $this->assertInstanceOf( + LoginController::class, + $this->callMethod( + $this->_oController, + 'd3GetLoginController' + ) + ); + } } diff --git a/src/tests/unit/Application/Controller/Admin/d3user_totpTest.php b/src/tests/unit/Application/Controller/Admin/d3user_totpTest.php index 1e58388..f958ce6 100644 --- a/src/tests/unit/Application/Controller/Admin/d3user_totpTest.php +++ b/src/tests/unit/Application/Controller/Admin/d3user_totpTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Controller\Admin; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Application/Controller/d3_account_totpTest.php b/src/tests/unit/Application/Controller/d3_account_totpTest.php index f91ec22..66489d8 100644 --- a/src/tests/unit/Application/Controller/d3_account_totpTest.php +++ b/src/tests/unit/Application/Controller/d3_account_totpTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Controller; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Application/Controller/d3totploginTest.php b/src/tests/unit/Application/Controller/d3totploginTest.php index 02cd912..bc7a9b2 100644 --- a/src/tests/unit/Application/Controller/d3totploginTest.php +++ b/src/tests/unit/Application/Controller/d3totploginTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Controller; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/Exceptions/d3totp_wrongOtpExceptionTest.php b/src/tests/unit/Application/Model/Exceptions/d3totp_wrongOtpExceptionTest.php index c5af93b..e6e483d 100644 --- a/src/tests/unit/Application/Model/Exceptions/d3totp_wrongOtpExceptionTest.php +++ b/src/tests/unit/Application/Model/Exceptions/d3totp_wrongOtpExceptionTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Model\Exceptions; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/d3RandomGeneratorTest.php b/src/tests/unit/Application/Model/d3RandomGeneratorTest.php index b3819f8..44c5ec8 100644 --- a/src/tests/unit/Application/Model/d3RandomGeneratorTest.php +++ b/src/tests/unit/Application/Model/d3RandomGeneratorTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/d3backupcodeTest.php b/src/tests/unit/Application/Model/d3backupcodeTest.php index 1ecdede..ad5d57d 100644 --- a/src/tests/unit/Application/Model/d3backupcodeTest.php +++ b/src/tests/unit/Application/Model/d3backupcodeTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/d3backupcodelistTest.php b/src/tests/unit/Application/Model/d3backupcodelistTest.php index 9049abf..388033c 100644 --- a/src/tests/unit/Application/Model/d3backupcodelistTest.php +++ b/src/tests/unit/Application/Model/d3backupcodelistTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/d3totpTest.php b/src/tests/unit/Application/Model/d3totpTest.php index c0385c3..cffbbee 100644 --- a/src/tests/unit/Application/Model/d3totpTest.php +++ b/src/tests/unit/Application/Model/d3totpTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Application\Model; use BaconQrCode\Writer; @@ -865,7 +867,7 @@ class d3totpTest extends d3TotpUnitTestCase ->onlyMethods(['d3Base64_decode']) ->getMock(); $oModelMock->method('d3Base64_decode')->willReturn( - str_pad('foobar', 50, 0, STR_PAD_LEFT) + str_pad('foobar', 50, '0', STR_PAD_LEFT) ); $this->_oModel = $oModelMock; diff --git a/src/tests/unit/Modules/Application/Component/d3_totp_UserComponentTest.php b/src/tests/unit/Modules/Application/Component/d3_totp_UserComponentTest.php index 24141cd..37ee7ad 100644 --- a/src/tests/unit/Modules/Application/Component/d3_totp_UserComponentTest.php +++ b/src/tests/unit/Modules/Application/Component/d3_totp_UserComponentTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Application\Component; use D3\TestingTools\Development\CanAccessRestricted; @@ -92,6 +94,7 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase 'd3TotpGetUtils', ]) ->getMock(); + $oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock); diff --git a/src/tests/unit/Modules/Application/Controller/Admin/d3_totp_LoginControllerTest.php b/src/tests/unit/Modules/Application/Controller/Admin/d3_totp_LoginControllerTest.php index 8438f4c..94f8309 100644 --- a/src/tests/unit/Modules/Application/Controller/Admin/d3_totp_LoginControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/Admin/d3_totp_LoginControllerTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Application\Controller\Admin; use D3\TestingTools\Development\CanAccessRestricted; @@ -19,8 +21,11 @@ use D3\Totp\Application\Model\d3totp_conf; use D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController; use D3\Totp\Modules\Application\Model\d3_totp_user; use D3\Totp\tests\unit\d3TotpUnitTestCase; +use OxidEsales\Eshop\Application\Controller\Admin\LoginController; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Language; use OxidEsales\Eshop\Core\Session; +use OxidEsales\Eshop\Core\UtilsServer; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; @@ -79,84 +84,14 @@ class d3_totp_LoginControllerTest extends d3TotpUnitTestCase * @throws ReflectionException * @covers \D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController::checklogin */ - public function checkloginMissingTotp() + public function canChecklogin() { $fixture = 'returnString'; - /** @var d3totp|MockObject $oTotpMock */ - $oTotpMock = $this->getMockBuilder(d3totp::class) - ->disableOriginalConstructor() - ->onlyMethods(['loadByUserId']) - ->getMock(); - $oTotpMock->method('loadByUserId')->willReturn(true); - - /** @var d3_totp_user|MockObject $userMock */ - $userMock = $this->getMockBuilder(User::class) - ->onlyMethods(['logout']) - ->getMock(); - $userMock->expects($this->once())->method('logout')->willReturn(true); - /** @var d3_totp_LoginController|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_totp_LoginController::class) - ->onlyMethods([ - 'd3GetTotpObject', - 'd3TotpGetUserObject', - 'd3TotpLoginMissing', - 'd3CallMockableParent' - ]) + ->onlyMethods(['d3CallMockableParent']) ->getMock(); - $oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock); - $oControllerMock->method('d3TotpGetUserObject')->willReturn($userMock); - $oControllerMock->method('d3TotpLoginMissing')->with($this->identicalTo($oTotpMock)) - ->willReturn(true); - $oControllerMock->method('d3CallMockableParent')->willReturn($fixture); - - $this->_oController = $oControllerMock; - - $this->assertSame( - 'd3totpadminlogin', - $this->callMethod( - $this->_oController, - 'checklogin' - ) - ); - } - - /** - * @test - * @throws ReflectionException - * @covers \D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController::checklogin - */ - public function checkloginNotMissingTotp() - { - $fixture = 'returnString'; - - /** @var d3totp|MockObject $oTotpMock */ - $oTotpMock = $this->getMockBuilder(d3totp::class) - ->disableOriginalConstructor() - ->onlyMethods(['loadByUserId']) - ->getMock(); - $oTotpMock->method('loadByUserId')->willReturn(true); - - /** @var d3_totp_user|MockObject $userMock */ - $userMock = $this->getMockBuilder(User::class) - ->onlyMethods(['logout']) - ->getMock(); - $userMock->expects($this->never())->method('logout')->willReturn(true); - - /** @var d3_totp_LoginController|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3_totp_LoginController::class) - ->onlyMethods([ - 'd3GetTotpObject', - 'd3TotpGetUserObject', - 'd3TotpLoginMissing', - 'd3CallMockableParent' - ]) - ->getMock(); - $oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock); - $oControllerMock->method('d3TotpGetUserObject')->willReturn($userMock); - $oControllerMock->method('d3TotpLoginMissing')->with($this->identicalTo($oTotpMock)) - ->willReturn(false); $oControllerMock->method('d3CallMockableParent')->willReturn($fixture); $this->_oController = $oControllerMock; @@ -170,6 +105,109 @@ class d3_totp_LoginControllerTest extends d3TotpUnitTestCase ); } + /** + * @test + * + * @param $selectedProfile + * @param $setCookie + * @param $expectedCookie + * @param $setSession + * + * @throws ReflectionException + * @dataProvider canRunTotpAfterLoginDataProvider + * @covers \D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController::d3totpAfterLogin + */ + public function canRunTotpAfterLogin($selectedProfile, $setCookie, $expectedCookie, $setSession) + { + /** @var Session|MockObject $sessionMock */ + $sessionMock = $this->getMockBuilder(Session::class) + ->onlyMethods(['getVariable', 'setVariable']) + ->getMock(); + $variableMap = [ + [d3totp_conf::SESSION_ADMIN_PROFILE, $selectedProfile], + ['aAdminProfiles', [ + 0 => ['abc', 0], + 1 => ['def', 1], + 2 => ['geh', 2], + ]], + ]; + $sessionMock->method('getVariable')->willReturnMap($variableMap); + $sessionMock->expects($setSession)->method('setVariable')->willReturnMap($variableMap); + + /** @var UtilsServer|MockObject $utilsServerMock */ + $utilsServerMock = $this->getMockBuilder(UtilsServer::class) + ->onlyMethods(['setOxCookie']) + ->getMock(); + $utilsServerMock->expects($setCookie)->method('setOxCookie')->with( + $this->anything(), + $expectedCookie + ); + + /** @var d3_totp_LoginController|MockObject $sut */ + $sut = $this->getMockBuilder(LoginController::class) + ->onlyMethods(['d3TotpGetUtilsServer', 'd3TotpGetSession', 'd3totpAfterLoginSetLanguage']) + ->getMock(); + $sut->method('d3TotpGetUtilsServer')->willReturn($utilsServerMock); + $sut->method('d3TotpGetSession')->willReturn($sessionMock); + $sut->expects($this->once())->method('d3totpAfterLoginSetLanguage')->willReturn($sessionMock); + + $this->callMethod( + $sut, + 'd3totpAfterLogin' + ); + } + + /** + * @return array + */ + public function canRunTotpAfterLoginDataProvider(): array + { + return [ + 'no profile selected' => [null, $this->once(), '', $this->never()], + 'valid profile selected' => [2, $this->once(), '2@geh@2', $this->once()], + 'invalid profile selected' => [5, $this->never(), false, $this->never()] + ]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController::d3totpAfterLoginSetLanguage + */ + public function canRunTotpAfterLoginSetLanguage() + { + /** @var Session|MockObject $sessionMock */ + $sessionMock = $this->getMockBuilder(Session::class) + ->onlyMethods(['getVariable']) + ->getMock(); + $sessionMock->method('getVariable')->willReturn(0); + + /** @var UtilsServer|MockObject $utilsServerMock */ + $utilsServerMock = $this->getMockBuilder(UtilsServer::class) + ->onlyMethods(['setOxCookie']) + ->getMock(); + $utilsServerMock->expects($this->once())->method('setOxCookie'); + + /** @var Language|MockObject $langMock */ + $langMock = $this->getMockBuilder(Language::class) + ->onlyMethods(['setTplLanguage']) + ->getMock(); + $langMock->expects($this->once())->method('setTplLanguage'); + + /** @var d3_totp_LoginController|MockObject $sut */ + $sut = $this->getMockBuilder(LoginController::class) + ->onlyMethods(['d3TotpGetUtilsServer', 'd3TotpGetSession', 'd3TotpGetLangObject']) + ->getMock(); + $sut->method('d3TotpGetUtilsServer')->willReturn($utilsServerMock); + $sut->method('d3TotpGetSession')->willReturn($sessionMock); + $sut->method('d3TotpGetLangObject')->willReturn($langMock); + + $this->callMethod( + $sut, + 'd3totpAfterLoginSetlanguage' + ); + } + /** * @test * @param $totpActive @@ -240,4 +278,30 @@ class d3_totp_LoginControllerTest extends d3TotpUnitTestCase $this->callMethod($this->_oController, 'd3TotpGetUserObject') ); } + + /** + * @test + * @throws ReflectionException + * @covers \D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController::d3TotpGetUtilsServer + */ + public function d3GetUtilsServerObjectReturnsRightObject() + { + $this->assertInstanceOf( + UtilsServer::class, + $this->callMethod($this->_oController, 'd3TotpGetUtilsServer') + ); + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\Totp\Modules\Application\Controller\Admin\d3_totp_LoginController::d3TotpGetLangObject + */ + public function d3GetLangObjectReturnsRightObject() + { + $this->assertInstanceOf( + Language::class, + $this->callMethod($this->_oController, 'd3TotpGetLangObject') + ); + } } diff --git a/src/tests/unit/Modules/Application/Controller/d3_totp_OrderControllerTest.php b/src/tests/unit/Modules/Application/Controller/d3_totp_OrderControllerTest.php index 3c7656d..fcc688b 100644 --- a/src/tests/unit/Modules/Application/Controller/d3_totp_OrderControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/d3_totp_OrderControllerTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Application\Controller; use D3\Totp\Modules\Application\Controller\d3_totp_OrderController; diff --git a/src/tests/unit/Modules/Application/Controller/d3_totp_PaymentControllerTest.php b/src/tests/unit/Modules/Application/Controller/d3_totp_PaymentControllerTest.php index 9fd2c55..d85cd32 100644 --- a/src/tests/unit/Modules/Application/Controller/d3_totp_PaymentControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/d3_totp_PaymentControllerTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Application\Controller; use D3\Totp\Modules\Application\Controller\d3_totp_PaymentController; diff --git a/src/tests/unit/Modules/Application/Controller/d3_totp_UserControllerTest.php b/src/tests/unit/Modules/Application/Controller/d3_totp_UserControllerTest.php index 56cdc3f..d3f2860 100644 --- a/src/tests/unit/Modules/Application/Controller/d3_totp_UserControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/d3_totp_UserControllerTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Application\Controller; use D3\Totp\Modules\Application\Controller\d3_totp_UserController; diff --git a/src/tests/unit/Modules/Application/Controller/d3_totp_getUserTestTrait.php b/src/tests/unit/Modules/Application/Controller/d3_totp_getUserTestTrait.php index c9e8603..9936f8a 100644 --- a/src/tests/unit/Modules/Application/Controller/d3_totp_getUserTestTrait.php +++ b/src/tests/unit/Modules/Application/Controller/d3_totp_getUserTestTrait.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Application\Controller; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Modules/Application/Model/d3_totp_userTest.php b/src/tests/unit/Modules/Application/Model/d3_totp_userTest.php index c683af5..dbb5399 100644 --- a/src/tests/unit/Modules/Application/Model/d3_totp_userTest.php +++ b/src/tests/unit/Modules/Application/Model/d3_totp_userTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Modules/Core/d3_totp_utilsTest.php b/src/tests/unit/Modules/Core/d3_totp_utilsTest.php index 1fd7106..911577c 100644 --- a/src/tests/unit/Modules/Core/d3_totp_utilsTest.php +++ b/src/tests/unit/Modules/Core/d3_totp_utilsTest.php @@ -11,6 +11,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit\Modules\Core; use D3\TestingTools\Development\CanAccessRestricted; diff --git a/src/tests/unit/Modules/Core/totpSystemEventHandlerTest.php b/src/tests/unit/Modules/Core/totpSystemEventHandlerTest.php new file mode 100644 index 0000000..df0678d --- /dev/null +++ b/src/tests/unit/Modules/Core/totpSystemEventHandlerTest.php @@ -0,0 +1,253 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace unit\Modules\Core; + +use D3\TestingTools\Development\CanAccessRestricted; +use D3\Totp\Application\Model\d3totp; +use D3\Totp\Modules\Application\Model\d3_totp_user; +use D3\Totp\Modules\Core\totpSystemEventHandler; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Session; +use OxidEsales\Eshop\Core\SystemEventHandler; +use OxidEsales\Eshop\Core\Utils; +use OxidEsales\TestingLibrary\UnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class totpSystemEventHandlerTest extends UnitTestCase +{ + use CanAccessRestricted; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Totp\Modules\Core\totpSystemEventHandler::onAdminLogin + */ + public function runOnAdminLogin() + { + /** @var totpSystemEventHandler|MockObject $sut */ + $sut = $this->getMockBuilder(SystemEventHandler::class) + ->onlyMethods(['d3CallMockableParent', 'd3requestTotp']) + ->getMock(); + + $sut->method('d3CallMockableParent')->willReturn(true); + $sut->expects($this->once())->method('d3requestTotp')->willReturn(true); + + $this->callMethod( + $sut, + 'onAdminLogin' + ); + } + + /** + * @test + * + * @param $totpMissing + * @param $doLogout + * @param $doRedirect + * + * @return void + * @throws ReflectionException + * @dataProvider canRequestTotpDataProvider + * @covers \D3\Totp\Modules\Core\totpSystemEventHandler::d3requestTotp + */ + public function canRequestTotp($totpMissing, $doLogout, $doRedirect) + { + /** @var Session|MockObject $sessionMock */ + $sessionMock = $this->getMockBuilder(Session::class) + ->onlyMethods(['getVariable']) + ->getMock(); + $sessionMock->method('getVariable')->willReturn('myUserId'); + + /** @var d3_totp_user|MockObject $userMock */ + $userMock = $this->getMockBuilder(User::class) + ->onlyMethods(['logout']) + ->getMock(); + $userMock->expects($doLogout)->method('logout')->willReturn(true); + + /** @var Utils|MockObject $utilsMock */ + $utilsMock = $this->getMockBuilder(Utils::class) + ->onlyMethods(['redirect']) + ->getMock(); + $utilsMock->expects($doRedirect)->method('redirect')->willReturn(true); + + /** @var d3totp|MockObject $totpMock */ + $totpMock = $this->getMockBuilder(d3totp::class) + ->onlyMethods(['loadByUserId']) + ->getMock(); + $totpMock->expects($this->atLeastOnce())->method('loadByUserId')->with('myUserId')->willReturn(1); + + /** @var totpSystemEventHandler|MockObject $sut */ + $sut = $this->getMockBuilder(SystemEventHandler::class) + ->onlyMethods(['d3GetTotpObject', 'd3TotpGetSession', 'd3TotpLoginMissing', + 'd3TotpGetUserObject', 'getUtilsObject']) + ->getMock(); + $sut->method('d3GetTotpObject')->willReturn($totpMock); + $sut->method('d3TotpGetSession')->willReturn($sessionMock); + $sut->method('d3TotpLoginMissing')->with($totpMock)->willReturn($totpMissing); + $sut->method('d3TotpGetUserObject')->willReturn($userMock); + $sut->method('getUtilsObject')->willReturn($utilsMock); + + $this->callMethod( + $sut, + 'd3requestTotp' + ); + } + + /** + * @return array + */ + public function canRequestTotpDataProvider(): array + { + return [ + 'no totp missing' => [false, $this->never(), $this->never()], + 'totp missing' => [true, $this->once(), $this->once()] + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Totp\Modules\Core\totpSystemEventHandler::d3GetTotpObject + */ + public function canGetTotpObject() + { + /** @var totpSystemEventHandler $sut */ + $sut = oxNew(SystemEventHandler::class); + + $this->assertInstanceOf( + d3totp::class, + $this->callMethod( + $sut, + 'd3GetTotpObject' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Totp\Modules\Core\totpSystemEventHandler::getUtilsObject + */ + public function canGetUtilsObject() + { + /** @var totpSystemEventHandler $sut */ + $sut = oxNew(SystemEventHandler::class); + + $this->assertInstanceOf( + Utils::class, + $this->callMethod( + $sut, + 'getUtilsObject' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Totp\Modules\Core\totpSystemEventHandler::d3TotpGetSession + */ + public function canGetSessionObject() + { + /** @var totpSystemEventHandler $sut */ + $sut = oxNew(SystemEventHandler::class); + + $this->assertInstanceOf( + Session::class, + $this->callMethod( + $sut, + 'd3TotpGetSession' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Totp\Modules\Core\totpSystemEventHandler::d3TotpGetUserObject + */ + public function canGetUserObject() + { + /** @var totpSystemEventHandler $sut */ + $sut = oxNew(SystemEventHandler::class); + + $this->assertInstanceOf( + User::class, + $this->callMethod( + $sut, + 'd3TotpGetUserObject' + ) + ); + } + + /** + * @test + * @param $isActive + * @param $hasTotpAuth + * @param $expected + * @return void + * @throws ReflectionException + * @dataProvider checkTotpLoginMissingDataProvider + * @covers \D3\Totp\Modules\Core\totpSystemEventHandler::d3TotpLoginMissing + */ + public function checkTotpLoginMissing($isActive, $hasTotpAuth, $expected) + { + /** @var Session|MockObject $sessionMock */ + $sessionMock = $this->getMockBuilder(Session::class) + ->onlyMethods(['getVariable']) + ->getMock(); + $sessionMock->method('getVariable')->willReturn($hasTotpAuth); + + /** @var d3totp|MockObject $totpMock */ + $totpMock = $this->getMockBuilder(d3totp::class) + ->onlyMethods(['isActive']) + ->getMock(); + $totpMock->method('isActive')->willReturn($isActive); + + /** @var totpSystemEventHandler|MockObject $sut */ + $sut = $this->getMockBuilder(SystemEventHandler::class) + ->onlyMethods(['d3TotpGetSession']) + ->getMock(); + $sut->method('d3TotpGetSession')->willReturn($sessionMock); + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'd3TotpLoginMissing', + [$totpMock] + ) + ); + } + + /** + * @return array + */ + public function checkTotpLoginMissingDataProvider(): array + { + return [ + 'totp not active' => [false, false, false], + 'missing totp' => [true, false, true], + 'totp exists' => [true, true, false], + ]; + } +} \ No newline at end of file diff --git a/src/tests/unit/d3TotpUnitTestCase.php b/src/tests/unit/d3TotpUnitTestCase.php index 531b60c..1b634db 100644 --- a/src/tests/unit/d3TotpUnitTestCase.php +++ b/src/tests/unit/d3TotpUnitTestCase.php @@ -10,6 +10,8 @@ * @link https://www.oxidmodule.com */ +declare(strict_types=1); + namespace D3\Totp\tests\unit; use OxidEsales\TestingLibrary\UnitTestCase;