diff --git a/src/Application/Controller/Admin/d3user_webauthn.php b/src/Application/Controller/Admin/d3user_webauthn.php index b2daf20..65a0e50 100755 --- a/src/Application/Controller/Admin/d3user_webauthn.php +++ b/src/Application/Controller/Admin/d3user_webauthn.php @@ -16,22 +16,26 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Controller\Admin; use D3\TestingTools\Production\IsMockable; -use D3\Webauthn\Application\Controller\Traits\helpersTrait; +use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; +use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; +use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception as DoctrineException; use Exception; use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; +use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Utils; +use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; class d3user_webauthn extends AdminDetailsController { use IsMockable; - use helpersTrait; protected $_sSaveError = null; @@ -42,7 +46,7 @@ class d3user_webauthn extends AdminDetailsController */ public function render(): string { - $this->addTplParam('readonly', !$this->d3GetWebauthnObject()->isAvailable()); + $this->addTplParam('readonly', !$this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable()); $this->d3CallMockableParent('render'); @@ -50,7 +54,7 @@ class d3user_webauthn extends AdminDetailsController if (isset($soxId) && $soxId != "-1") { /** @var d3_User_Webauthn $oUser */ - $oUser = $this->d3GetUserObject(); + $oUser = $this->d3GetMockableOxNewObject(User::class); if ($oUser->load($soxId)) { $this->addTplParam("oxid", $oUser->getId()); } else { @@ -75,10 +79,10 @@ class d3user_webauthn extends AdminDetailsController $this->setPageType( 'requestnew' ); $this->setAuthnRegister(); } catch (Exception|ContainerExceptionInterface|NotFoundExceptionInterface|DoctrineDriverException $e) { - $this->d3GetUtilsViewObject()->addErrorToDisplay($e); - $this->d3GetLoggerObject()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); - $this->d3GetLoggerObject()->debug($e->getTraceAsString()); - $this->d3GetUtilsObject()->redirect('index.php?cl=d3user_webauthn'); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + $this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=d3user_webauthn'); } } @@ -97,13 +101,13 @@ class d3user_webauthn extends AdminDetailsController $credential = Registry::getRequest()->getRequestEscapedParameter('credential'); if ( strlen((string) $credential) ) { - $webauthn = $this->d3GetWebauthnObject(); + $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter( 'keyname' ) ); } } catch (WebauthnException|Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) { - $this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]); - $this->d3GetLoggerObject()->debug($e->getTraceAsString()); - $this->d3GetUtilsViewObject()->addErrorToDisplay($e); + $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); } } @@ -122,9 +126,9 @@ class d3user_webauthn extends AdminDetailsController */ public function setAuthnRegister(): void { - $authn = $this->d3GetWebauthnObject(); + $authn = $this->d3GetMockableOxNewObject(Webauthn::class); - $user = $this->d3GetUserObject(); + $user = $this->d3GetMockableOxNewObject(User::class); $user->load($this->getEditObjectId()); $publicKeyCredentialCreationOptions = $authn->getCreationOptions($user); @@ -148,10 +152,10 @@ class d3user_webauthn extends AdminDetailsController */ public function getCredentialList($userId): array { - $oUser = $this->d3GetUserObject(); + $oUser = $this->d3GetMockableOxNewObject(User::class); $oUser->load($userId); - $publicKeyCredentials = $this->d3GetPublicKeyCredentialListObject(); + $publicKeyCredentials = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); return $publicKeyCredentials->getAllFromUser($oUser)->getArray(); } @@ -160,7 +164,7 @@ class d3user_webauthn extends AdminDetailsController */ public function deleteKey(): void { - $credential = $this->d3GetPublicKeyCredentialObject(); + $credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); $credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid')); } } \ No newline at end of file diff --git a/src/Application/Controller/Admin/d3webauthnadminlogin.php b/src/Application/Controller/Admin/d3webauthnadminlogin.php index 105d7e4..54eebac 100755 --- a/src/Application/Controller/Admin/d3webauthnadminlogin.php +++ b/src/Application/Controller/Admin/d3webauthnadminlogin.php @@ -16,8 +16,8 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Controller\Admin; use D3\TestingTools\Production\IsMockable; -use D3\Webauthn\Application\Controller\Traits\helpersTrait; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; +use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnAfterLogin; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; @@ -26,18 +26,16 @@ use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception as DoctrineException; use OxidEsales\Eshop\Application\Controller\Admin\AdminController; use OxidEsales\Eshop\Application\Controller\FrontendController; -use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Request; -use OxidEsales\Eshop\Core\SystemEventHandler; +use OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver; +use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Utils; -use OxidEsales\Eshop\Core\UtilsServer; use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; class d3webauthnadminlogin extends AdminController { - use helpersTrait; use IsMockable; protected $_sThisTemplate = 'd3webauthnadminlogin.tpl'; @@ -59,20 +57,28 @@ class d3webauthnadminlogin extends AdminController */ public function render(): string { - if ($this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)) { - $this->getUtils()->redirect('index.php?cl=admin_start'); - } elseif (!$this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER)) { - $this->getUtils()->redirect('index.php?cl=login'); + if ($this->d3GetMockableRegistryObject(Session::class) + ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) + ) { + $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=admin_start'); + } elseif (!$this->d3GetMockableRegistryObject(Session::class) + ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) + ) { + $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login'); } $this->generateCredentialRequest(); - $this->addTplParam('navFormParams', $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); - $this->addTplParam('currentProfile', $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE)); - $this->d3GetSession()->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); - $this->addTplParam('currentChLanguage', $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE)); + $this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); + $this->addTplParam('currentProfile', $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE)); + $this->d3GetMockableRegistryObject(Session::class) + ->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); + $this->addTplParam('currentChLanguage', $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE)); - $afterLogin = $this->d3WebauthnGetAfterLogin(); + $afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class); $afterLogin->changeLanguage(); return $this->d3CallMockableParent('render'); @@ -87,66 +93,51 @@ class d3webauthnadminlogin extends AdminController */ public function generateCredentialRequest(): void { - $userId = $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER); + $userId = $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER); try { - $webauthn = $this->d3GetWebauthnObject(); + $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId); - $this->d3GetSession()->setVariable(WebauthnConf::WEBAUTHN_ADMIN_LOGIN_OBJECT, $publicKeyCredentialRequestOptions); + $this->d3GetMockableRegistryObject(Session::class) + ->setVariable(WebauthnConf::WEBAUTHN_ADMIN_LOGIN_OBJECT, $publicKeyCredentialRequestOptions); $this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions); $this->addTplParam('isAdmin', isAdmin()); } catch (WebauthnException $e) { - $this->d3GetSession()->setVariable(WebauthnConf::GLOBAL_SWITCH, true); - $this->d3GetUtilsViewObject()->addErrorToDisplay($e); - $this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetLoggerObject()->debug($e->getTraceAsString()); - $this->getUtils()->redirect('index.php?cl=login'); + $this->d3GetMockableRegistryObject(Session::class) + ->setVariable(WebauthnConf::GLOBAL_SWITCH, true); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login'); } } - /** - * @param string $credential - * @param string|null $error - * @throws WebauthnGetException - * @return WebauthnLogin - */ - public function getWebauthnLoginObject(string $credential, ?string $error): WebauthnLogin - { - return oxNew(WebauthnLogin::class, $credential, $error); - } - /** * @return string|null */ public function d3AssertAuthn(): ?string { try { - $login = $this->getWebauthnLoginObject( - $this->d3WebAuthnGetRequest()->getRequestEscapedParameter('credential'), - $this->d3WebAuthnGetRequest()->getRequestEscapedParameter('error') + $login = $this->d3GetMockableOxNewObject(WebauthnLogin::class, + $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'), + $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error') ); return $login->adminLogin( - $this->d3WebAuthnGetRequest()->getRequestEscapedParameter('profile') + $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('profile') ); } catch (WebauthnGetException $e) { - $this->d3GetUtilsViewObject()->addErrorToDisplay($e); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); return 'login'; } } - /** - * @return Utils - */ - public function getUtils(): Utils - { - return Registry::getUtils(); - } - /** * @return string|null */ public function d3GetPreviousClass(): ?string { - return $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS); + return $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS); } /** @@ -155,7 +146,8 @@ class d3webauthnadminlogin extends AdminController public function previousClassIsOrderStep(): bool { $sClassKey = $this->d3GetPreviousClass(); - $resolvedClass = $this->d3GetControllerClassNameResolver()->getClassNameById($sClassKey); + $resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class) + ->getClassNameById($sClassKey); $resolvedClass = $resolvedClass ?: 'start'; /** @var FrontendController $oController */ @@ -170,36 +162,4 @@ class d3webauthnadminlogin extends AdminController { return $this->previousClassIsOrderStep(); } - - /** - * @return WebauthnAfterLogin - */ - public function d3WebauthnGetAfterLogin(): WebauthnAfterLogin - { - return oxNew(WebauthnAfterLogin::class); - } - - /** - * @return SystemEventHandler - */ - public function d3WebauthnGetEventHandler(): SystemEventHandler - { - return oxNew(SystemEventHandler::class); - } - - /** - * @return Request - */ - public function d3WebAuthnGetRequest(): Request - { - return Registry::getRequest(); - } - - /** - * @return UtilsServer - */ - public function d3WebauthnGetUtilsServer(): UtilsServer - { - return Registry::getUtilsServer(); - } } \ No newline at end of file diff --git a/src/Application/Controller/Traits/checkoutGetUserTrait.php b/src/Application/Controller/Traits/checkoutGetUserTrait.php index 9ca55f4..bb6aa52 100755 --- a/src/Application/Controller/Traits/checkoutGetUserTrait.php +++ b/src/Application/Controller/Traits/checkoutGetUserTrait.php @@ -21,7 +21,6 @@ use D3\Webauthn\Application\Model\WebauthnConf; use Doctrine\DBAL\Driver\Exception; use Doctrine\DBAL\Exception as DoctrineException; use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -42,10 +41,11 @@ trait checkoutGetUserTrait $user = $this->d3CallMockableParent('getUser'); if ($user && $user->getId()) { - $webauthn = $this->d3GetWebauthnObject(); + $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); if ($webauthn->isActive($user->getId()) - && !$this->d3WebauthnGetSessionObject()->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) + && !$this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) ) { return false; } @@ -53,20 +53,4 @@ trait checkoutGetUserTrait return $user; } - - /** - * @return Webauthn - */ - public function d3GetWebauthnObject(): Webauthn - { - return oxNew(Webauthn::class); - } - - /** - * @return Session - */ - public function d3WebauthnGetSessionObject(): Session - { - return Registry::getSession(); - } } \ No newline at end of file diff --git a/src/Application/Controller/d3_account_webauthn.php b/src/Application/Controller/d3_account_webauthn.php index 6be844d..aa2cd9b 100755 --- a/src/Application/Controller/d3_account_webauthn.php +++ b/src/Application/Controller/d3_account_webauthn.php @@ -15,23 +15,26 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Controller; +use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Controller\Traits\accountTrait; -use D3\Webauthn\Application\Controller\Traits\helpersTrait; +use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; +use D3\Webauthn\Application\Model\Webauthn; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception as DoctrineException; use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\SeoEncoder; +use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; class d3_account_webauthn extends AccountController { use accountTrait; - use helpersTrait; + use IsMockable; protected $_sThisTemplate = 'd3_account_webauthn.tpl'; @@ -43,7 +46,7 @@ class d3_account_webauthn extends AccountController $sRet = parent::render(); $this->addTplParam('user', $this->getUser()); - $this->addTplParam('readonly', !($this->d3GetWebauthnObject()->isAvailable())); + $this->addTplParam('readonly', !($this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable())); return $sRet; } @@ -58,7 +61,7 @@ class d3_account_webauthn extends AccountController public function getCredentialList(): PublicKeyCredentialList { $oUser = $this->getUser(); - $credentialList = $this->d3GetPublicKeyCredentialListObject(); + $credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); return $credentialList->getAllFromUser($oUser); } @@ -75,9 +78,9 @@ class d3_account_webauthn extends AccountController $this->setAuthnRegister(); $this->setPageType('requestnew'); } catch (WebauthnException $e) { - $this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]); - $this->d3GetLoggerObject()->debug($e->getTraceAsString()); - $this->d3GetUtilsViewObject()->addErrorToDisplay($e); + $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); } } @@ -100,7 +103,8 @@ class d3_account_webauthn extends AccountController */ public function setAuthnRegister(): void { - $publicKeyCredentialCreationOptions = $this->d3GetWebauthnObject()->getCreationOptions($this->getUser()); + $publicKeyCredentialCreationOptions = $this->d3GetMockableOxNewObject(Webauthn::class) + ->getCreationOptions($this->getUser()); $this->addTplParam('webauthn_publickey_create', $publicKeyCredentialCreationOptions); $this->addTplParam('isAdmin', isAdmin()); @@ -126,11 +130,11 @@ class d3_account_webauthn extends AccountController $credential = Registry::getRequest()->getRequestEscapedParameter('credential'); if (strlen((string) $credential)) { - $webauthn = $this->d3GetWebauthnObject(); + $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname')); } } catch (WebauthnException $e) { - $this->d3GetUtilsViewObject()->addErrorToDisplay( $e ); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay( $e ); } } @@ -141,7 +145,7 @@ class d3_account_webauthn extends AccountController { $deleteId = Registry::getRequest()->getRequestEscapedParameter('deleteoxid'); if ($deleteId) { - $credential = $this->d3GetPublicKeyCredentialObject(); + $credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); $credential->delete($deleteId); } } diff --git a/src/Application/Controller/d3webauthnlogin.php b/src/Application/Controller/d3webauthnlogin.php index de0bb27..71a9248 100755 --- a/src/Application/Controller/d3webauthnlogin.php +++ b/src/Application/Controller/d3webauthnlogin.php @@ -16,7 +16,6 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Controller; use D3\TestingTools\Production\IsMockable; -use D3\Webauthn\Application\Controller\Traits\helpersTrait; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; @@ -24,6 +23,8 @@ use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception as DoctrineException; use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver; +use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Utils; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -31,7 +32,6 @@ use Psr\Container\NotFoundExceptionInterface; class d3webauthnlogin extends FrontendController { use IsMockable; - use helpersTrait; protected $_sThisTemplate = 'd3webauthnlogin.tpl'; @@ -39,8 +39,8 @@ class d3webauthnlogin extends FrontendController * @return array */ public function getNavigationParams(): array - {; - $navparams = $this->d3GetSession()->getVariable( + { + $navparams = $this->d3GetMockableRegistryObject(Session::class)->getVariable( WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS ); @@ -60,15 +60,18 @@ class d3webauthnlogin extends FrontendController */ public function render(): string { - if ($this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) || - !$this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER) + if ($this->d3GetMockableRegistryObject(Session::class) + ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) || + !$this->d3GetMockableRegistryObject(Session::class) + ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER) ) { - $this->getUtils()->redirect('index.php?cl=start'); + $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=start'); } $this->generateCredentialRequest(); - $this->addTplParam('navFormParams', $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); + $this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); return $this->d3CallMockableParent('render'); } @@ -82,37 +85,32 @@ class d3webauthnlogin extends FrontendController */ public function generateCredentialRequest(): void { - $userId = $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); + $userId = $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); try { /** @var Webauthn $webauthn */ - $webauthn = $this->d3GetWebauthnObject(); + $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId); $this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions); $this->addTplParam('isAdmin', isAdmin()); } catch (WebauthnException $e) { - $this->d3GetSession()->setVariable(WebauthnConf::GLOBAL_SWITCH, true); - $this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetLoggerObject()->debug($e->getTraceAsString()); + $this->d3GetMockableRegistryObject(Session::class) + ->setVariable(WebauthnConf::GLOBAL_SWITCH, true); + $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); Registry::getUtilsView()->addErrorToDisplay($e); - $this->getUtils()->redirect('index.php?cl=start'); + $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=start'); } } - /** - * @return Utils - */ - public function getUtils(): Utils - { - return Registry::getUtils(); - } - /** * @return string|null */ public function d3GetPreviousClass(): ?string { - return $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); + return $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); } /** @@ -121,7 +119,8 @@ class d3webauthnlogin extends FrontendController public function previousClassIsOrderStep(): bool { $sClassKey = $this->d3GetPreviousClass(); - $resolvedClass = $this->d3GetControllerClassNameResolver()->getClassNameById($sClassKey); + $resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class) + ->getClassNameById($sClassKey); $resolvedClass = $resolvedClass ?: 'start'; /** @var FrontendController $oController */ diff --git a/src/Application/Model/Credential/PublicKeyCredential.php b/src/Application/Model/Credential/PublicKeyCredential.php index b3bca3c..fe69504 100755 --- a/src/Application/Model/Credential/PublicKeyCredential.php +++ b/src/Application/Model/Credential/PublicKeyCredential.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Model\Credential; +use D3\TestingTools\Production\IsMockable; use DateTime; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception as DoctrineException; @@ -31,6 +32,8 @@ use Webauthn\PublicKeyCredentialSource; class PublicKeyCredential extends BaseModel { + use IsMockable; + protected $_sCoreTable = 'd3wa_usercredentials'; public function __construct() @@ -126,7 +129,7 @@ class PublicKeyCredential extends BaseModel public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $keyName = null): void { // item exist already - if ($this->getPublicKeyCredentialListObject() + if ($this->d3GetMockableOxNewObject(PublicKeyCredentialList::class) ->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId()) ) { return; @@ -147,14 +150,6 @@ class PublicKeyCredential extends BaseModel $this->save(); } - /** - * @return PublicKeyCredentialList - */ - protected function getPublicKeyCredentialListObject(): PublicKeyCredentialList - { - return oxNew(PublicKeyCredentialList::class); - } - /** * @param string $publicKeyCredentialId * @@ -178,7 +173,7 @@ class PublicKeyCredential extends BaseModel ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->d3GetConfig()->getShopId()) + $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) ) ) ); @@ -186,12 +181,4 @@ class PublicKeyCredential extends BaseModel return strlen((string) $oxid) ? $oxid : null; } - - /** - * @return Config - */ - public function d3GetConfig(): Config - { - return Registry::getConfig(); - } } \ No newline at end of file diff --git a/src/Application/Model/Credential/PublicKeyCredentialList.php b/src/Application/Model/Credential/PublicKeyCredentialList.php index 60e7929..e400983 100755 --- a/src/Application/Model/Credential/PublicKeyCredentialList.php +++ b/src/Application/Model/Credential/PublicKeyCredentialList.php @@ -20,9 +20,8 @@ use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Query\QueryBuilder; use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Config as Config; +use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Model\ListModel; -use OxidEsales\Eshop\Core\Registry; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface; use Psr\Container\ContainerExceptionInterface; @@ -64,7 +63,7 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->getConfigObject()->getShopId()) + $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) ) ) ); @@ -101,7 +100,7 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->getConfigObject()->getShopId()) + $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) ) ) ); @@ -138,7 +137,7 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->getConfigObject()->getShopId()) + $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) ) ) ); @@ -161,12 +160,4 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo { $this->getBaseObject()->saveCredentialSource($publicKeyCredentialSource); } - - /** - * @return Config - */ - public function getConfigObject(): Config - { - return Registry::getConfig(); - } } \ No newline at end of file diff --git a/src/Application/Model/RelyingPartyEntity.php b/src/Application/Model/RelyingPartyEntity.php index 20dd0cf..763711c 100755 --- a/src/Application/Model/RelyingPartyEntity.php +++ b/src/Application/Model/RelyingPartyEntity.php @@ -18,7 +18,6 @@ namespace D3\Webauthn\Application\Model; use D3\TestingTools\Production\IsMockable; use OxidEsales\Eshop\Application\Model\Shop; use OxidEsales\Eshop\Core\Config; -use OxidEsales\Eshop\Core\Registry; use Webauthn\PublicKeyCredentialRpEntity; class RelyingPartyEntity extends PublicKeyCredentialRpEntity @@ -49,7 +48,7 @@ class RelyingPartyEntity extends PublicKeyCredentialRpEntity */ public function getConfiguredShopUrl() { - return $this->getConfig()->getConfigParam('d3webauthn_diffshopurl'); + return $this->d3GetMockableRegistryObject(Config::class)->getConfigParam('d3webauthn_diffshopurl'); } /** @@ -70,19 +69,11 @@ class RelyingPartyEntity extends PublicKeyCredentialRpEntity $this->getShopUrlByHost(); } - /** - * @return Config - */ - public function getConfig(): Config - { - return Registry::getConfig(); - } - /** * @return Shop */ public function getActiveShop(): Shop { - return Registry::getConfig()->getActiveShop(); + return $this->d3GetMockableRegistryObject(Config::class)->getActiveShop(); } } \ No newline at end of file diff --git a/src/Application/Model/Webauthn.php b/src/Application/Model/Webauthn.php index 9fc477b..28c2f97 100644 --- a/src/Application/Model/Webauthn.php +++ b/src/Application/Model/Webauthn.php @@ -99,11 +99,10 @@ class Webauthn } /** + * @param string $userId * @return string * @throws DoctrineDriverException * @throws DoctrineException - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface */ public function getRequestOptions(string $userId): string { diff --git a/src/Application/Model/WebauthnAfterLogin.php b/src/Application/Model/WebauthnAfterLogin.php index a346833..c26d8fe 100644 --- a/src/Application/Model/WebauthnAfterLogin.php +++ b/src/Application/Model/WebauthnAfterLogin.php @@ -15,10 +15,14 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Model; +use D3\TestingTools\Production\IsMockable; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\UtilsServer; class WebauthnAfterLogin { + use IsMockable; + public function setDisplayProfile() { $sProfile = Registry::getRequest()->getRequestEscapedParameter('profile') ?: @@ -26,7 +30,7 @@ class WebauthnAfterLogin Registry::getSession()->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); - $myUtilsServer = Registry::getUtilsServer(); + $myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class); if (isset($sProfile)) { $aProfiles = Registry::getSession()->getVariable("aAdminProfiles"); @@ -46,7 +50,7 @@ class WebauthnAfterLogin */ public function changeLanguage() { - $myUtilsServer = Registry::getUtilsServer(); + $myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class); // languages $iLang = Registry::getRequest()->getRequestEscapedParameter('chlanguage') ?: Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE); diff --git a/src/Application/Model/WebauthnErrors.php b/src/Application/Model/WebauthnErrors.php index 93612a8..2a7cfcb 100644 --- a/src/Application/Model/WebauthnErrors.php +++ b/src/Application/Model/WebauthnErrors.php @@ -55,7 +55,7 @@ class WebauthnErrors return $lang->translateString('D3_WEBAUTHN_ERR_NOPUBKEYSUPPORT', null, true); } - switch ($msg) { + switch (strtoupper($msg)) { case self::UNSECURECONNECTION: return $lang->translateString($msg); } diff --git a/src/Application/Model/WebauthnLogin.php b/src/Application/Model/WebauthnLogin.php index 43cc5ae..ec0d954 100644 --- a/src/Application/Model/WebauthnLogin.php +++ b/src/Application/Model/WebauthnLogin.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Model; -use D3\Webauthn\Application\Controller\Traits\helpersTrait; +use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException; @@ -24,16 +24,17 @@ use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Exception\CookieException; use OxidEsales\Eshop\Core\Exception\UserException; -use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Str; use OxidEsales\Eshop\Core\SystemEventHandler; +use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsServer; +use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\EshopCommunity\Application\Component\UserComponent; class WebauthnLogin { - use helpersTrait; + use IsMockable; public $credential; @@ -104,9 +105,9 @@ class WebauthnLogin */ public function frontendLogin(UserComponent $usrCmp, bool $setSessionCookie = false) { - $myUtilsView = $this->d3GetUtilsViewObject(); + $myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class); /** @var d3_User_Webauthn $user */ - $user = $this->d3GetUserObject(); + $user = $this->d3GetMockableOxNewObject(User::class); $userId = $this->getUserId(); try { @@ -116,7 +117,7 @@ class WebauthnLogin $this->assertAuthn(); // relogin, don't extract from this try block - $usrCmp->setUser($this->d3GetUserObject()); + $usrCmp->setUser($this->d3GetMockableOxNewObject(User::class)); $this->setFrontendSession($user); $usrCmp->setLoginStatus(USER_LOGIN_SUCCESS); @@ -134,14 +135,14 @@ class WebauthnLogin $myUtilsView->addErrorToDisplay($oEx, false, true, '', false); //return 'user'; - } catch (\OxidEsales\Eshop\Core\Exception\CookieException $oEx) { + } catch (CookieException $oEx) { $myUtilsView->addErrorToDisplay($oEx); //return 'user'; } catch (WebauthnException $e) { $myUtilsView->addErrorToDisplay($e); - $this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetLoggerObject()->debug($e->getTraceAsString()); + $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); } $user->logout(); @@ -155,9 +156,9 @@ class WebauthnLogin */ public function adminLogin(string $selectedProfile): string { - $myUtilsView = $this->d3GetUtilsViewObject(); + $myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class); /** @var d3_User_Webauthn $user */ - $user = $this->d3GetUserObject(); + $user = $this->d3GetMockableOxNewObject(User::class); $userId = $this->getUserId(); try { @@ -169,10 +170,10 @@ class WebauthnLogin $this->handleBackendCookie(); $this->handleBackendSubshopRights($user, $session); - $oEvenHandler = $this->d3WebauthnGetEventHandler(); - $oEvenHandler->onAdminLogin(); + $oEventHandler = $this->d3GetMockableOxNewObject(SystemEventHandler::class); + $oEventHandler->onAdminLogin(); - $afterLogin = $this->getAfterLogin(); + $afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class); $afterLogin->setDisplayProfile(); $afterLogin->changeLanguage(); @@ -186,14 +187,16 @@ class WebauthnLogin $myUtilsView->addErrorToDisplay('LOGIN_NO_COOKIE_SUPPORT'); } catch (WebauthnException $e) { $myUtilsView->addErrorToDisplay($e); - $this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetLoggerObject()->debug($e->getTraceAsString()); + $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); } $user->logout(); $oStr = Str::getStr(); - $this->d3GetConfig()->getActiveView()->addTplParam('user', $oStr->htmlspecialchars($userId)); - $this->d3GetConfig()->getActiveView()->addTplParam('profile', $oStr->htmlspecialchars($selectedProfile)); + $this->d3GetMockableRegistryObject(Config::class)->getActiveView() + ->addTplParam('user', $oStr->htmlspecialchars($userId)); + $this->d3GetMockableRegistryObject(Config::class)->getActiveView() + ->addTplParam('profile', $oStr->htmlspecialchars($selectedProfile)); return 'login'; } @@ -218,7 +221,7 @@ class WebauthnLogin public function assertAuthn(): void { $credential = $this->getCredential(); - $webAuthn = $this->d3GetWebauthnObject(); + $webAuthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webAuthn->assertAuthn($credential); } @@ -228,7 +231,7 @@ class WebauthnLogin */ public function setAdminSession($userId): Session { - $session = $this->d3GetSession(); + $session = $this->d3GetMockableRegistryObject(Session::class); $adminProfiles = $session->getVariable("aAdminProfiles"); $session->initNewSession(); $session->setVariable("aAdminProfiles", $adminProfiles); @@ -242,11 +245,11 @@ class WebauthnLogin */ public function setSessionCookie(User $user) { - if ($this->d3GetConfig()->getConfigParam('blShowRememberMe')) { - $this->getUtilsServer()->setUserCookie( + if ($this->d3GetMockableRegistryObject(Config::class)->getConfigParam('blShowRememberMe')) { + $this->d3GetMockableRegistryObject(UtilsServer::class)->setUserCookie( $user->getFieldData('oxusername'), $user->getFieldData('oxpassword'), - $this->d3GetConfig()->getShopId() + $this->d3GetMockableRegistryObject(Config::class)->getShopId() ); } } @@ -259,7 +262,7 @@ class WebauthnLogin */ public function assertUser($userId, bool $isBackend = false): User { - $user = $this->d3GetUserObject(); + $user = $this->d3GetMockableOxNewObject(User::class); $user->load($userId); if (!$user->isLoaded() || ($isBackend && $user->getFieldData('oxrights') === 'user') @@ -278,7 +281,7 @@ class WebauthnLogin */ public function handleBackendCookie(): void { - $cookie = $this->getUtilsServer()->getOxCookie(); + $cookie = $this->d3GetMockableRegistryObject(UtilsServer::class)->getOxCookie(); if ($cookie === null) { /** @var CookieException $exc */ $exc = oxNew(CookieException::class, 'ERROR_MESSAGE_COOKIE_NOCOOKIE'); @@ -298,7 +301,7 @@ class WebauthnLogin if ($iSubshop) { $session->setVariable("shp", $iSubshop); $session->setVariable('currentadminshop', $iSubshop); - $this->d3GetConfig()->setShopId($iSubshop); + $this->d3GetMockableRegistryObject(Config::class)->setShopId($iSubshop); } } @@ -307,7 +310,7 @@ class WebauthnLogin */ public function regenerateSessionId(): void { - $oSession = $this->d3GetSession(); + $oSession = $this->d3GetMockableRegistryObject(Session::class); if ($oSession->isSessionStarted()) { $oSession->regenerateSessionId(); } @@ -317,8 +320,9 @@ class WebauthnLogin { // this user is blocked, deny him if ($user->inGroup('oxidblocked')) { - $sUrl = $this->d3GetConfig()->getShopHomeUrl() . 'cl=content&tpl=user_blocked.tpl'; - $this->d3GetUtilsObject()->redirect($sUrl, true, 302); + $sUrl = $this->d3GetMockableRegistryObject(Config::class)->getShopHomeUrl() . + 'cl=content&tpl=user_blocked.tpl'; + $this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl); } } @@ -327,27 +331,11 @@ class WebauthnLogin */ public function updateBasket(): void { - if ($oBasket = $this->d3GetSession()->getBasket()) { + if ($oBasket = $this->d3GetMockableRegistryObject(Session::class)->getBasket()) { $oBasket->onUpdate(); } } - /** - * @return SystemEventHandler - */ - public function d3WebauthnGetEventHandler(): SystemEventHandler - { - return oxNew(SystemEventHandler::class); - } - - /** - * @return WebauthnAfterLogin - */ - public function getAfterLogin(): WebauthnAfterLogin - { - return oxNew(WebauthnAfterLogin::class); - } - /** * @return bool */ @@ -362,24 +350,10 @@ class WebauthnLogin public function getUserId(): string { return $this->isAdmin() ? - $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) : - $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); - } - - /** - * @return Config - */ - public function d3GetConfig(): Config - { - return Registry::getConfig(); - } - - /** - * @return UtilsServer - */ - public function getUtilsServer(): UtilsServer - { - return Registry::getUtilsServer(); + $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) : + $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); } /** @@ -389,7 +363,8 @@ class WebauthnLogin */ public function setFrontendSession(User $user): void { - $this->d3GetSession()->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $this->getCredential()); - $this->d3GetSession()->setVariable(WebauthnConf::OXID_FRONTEND_AUTH, $user->getId()); + $session = $this->d3GetMockableRegistryObject(Session::class); + $session->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $this->getCredential()); + $session->setVariable(WebauthnConf::OXID_FRONTEND_AUTH, $user->getId()); } } \ No newline at end of file diff --git a/src/Modules/Application/Component/d3_webauthn_UserComponent.php b/src/Modules/Application/Component/d3_webauthn_UserComponent.php index 025e483..caa1c9a 100755 --- a/src/Modules/Application/Component/d3_webauthn_UserComponent.php +++ b/src/Modules/Application/Component/d3_webauthn_UserComponent.php @@ -15,11 +15,11 @@ declare(strict_types=1); namespace D3\Webauthn\Modules\Application\Component; -use D3\Webauthn\Application\Model\Exceptions\WebauthnException; +use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException; -use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\Webauthn; +use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; @@ -28,13 +28,14 @@ use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Session; -use OxidEsales\Eshop\Core\UtilsView; +use OxidEsales\Eshop\Core\Utils; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; -use Psr\Log\LoggerInterface; class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent { + use IsMockable; + /** * @return string * @throws ContainerExceptionInterface @@ -44,14 +45,14 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent */ public function login() { - $lgn_user = $this->d3WebAuthnGetRequest()->getRequestParameter('lgn_usr'); - $password = $this->d3WebAuthnGetRequest()->getRequestParameter('lgn_pwd'); + $lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_usr'); + $password = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_pwd'); /** @var d3_User_Webauthn $user */ $user = oxNew(User::class); $userId = $user->d3GetLoginUserId($lgn_user); if ($lgn_user && $userId && !strlen(trim((string) $password))) { - $webauthn = $this->d3GetWebauthnObject(); + $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); if ($webauthn->isActive($userId) && !Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) @@ -74,21 +75,13 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent ); $sUrl = Registry::getConfig()->getShopHomeUrl() . 'cl=d3webauthnlogin'; - Registry::getUtils()->redirect($sUrl, true, 302); + $this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl); } } return parent::login(); } - /** - * @return Webauthn - */ - public function d3GetWebauthnObject(): Webauthn - { - return oxNew(Webauthn::class); - } - public function d3CancelWebauthnLogin(): void { $this->d3WebauthnClearSessionVariables(); @@ -99,10 +92,11 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent */ public function d3WebauthnClearSessionVariables(): void { - $this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); - $this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); - $this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS); - $this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT); + $session = $this->d3GetMockableRegistryObject(Session::class); + $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); + $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); + $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS); + $session->deleteVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT); } /** @@ -111,65 +105,15 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent public function d3AssertAuthn(): void { try { - $login = $this->getWebauthnLoginObject( - $this->d3WebAuthnGetRequest()->getRequestEscapedParameter('credential'), - $this->d3WebAuthnGetRequest()->getRequestEscapedParameter('error') + $login = $this->d3GetMockableOxNewObject(WebauthnLogin::class, + $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'), + $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error') ); - $login->frontendLogin($this, (bool)$this->d3WebAuthnGetRequest()->getRequestParameter('lgn_cook')); + $login->frontendLogin($this, (bool)$this->d3GetMockableRegistryObject(Request::class) + ->getRequestParameter('lgn_cook')); $this->_afterLogin($this->getUser()); } catch (WebauthnGetException $e) { Registry::getUtilsView()->addErrorToDisplay($e); } catch (WebauthnLoginErrorException $e) {} } - - /** - * @param string $credential - * @param string|null $error - * @throws WebauthnGetException - * @return WebauthnLogin - */ - public function getWebauthnLoginObject(string $credential, ?string $error): WebauthnLogin - { - return oxNew(WebauthnLogin::class, $credential, $error); - } - - /** - * @return Session - */ - public function d3WebauthnGetSession(): Session - { - return Registry::getSession(); - } - - /** - * @return User - */ - public function d3WebauthnGetUserObject(): User - { - return oxNew(User::class); - } - - /** - * @return UtilsView - */ - public function d3GetUtilsViewObject(): UtilsView - { - return Registry::getUtilsView(); - } - - /** - * @return LoggerInterface - */ - public function d3GetLoggerObject(): LoggerInterface - { - return Registry::getLogger(); - } - - /** - * @return Request - */ - public function d3WebAuthnGetRequest(): Request - { - return Registry::getRequest(); - } } \ No newline at end of file diff --git a/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php b/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php index cb65fa5..2d9191e 100755 --- a/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php +++ b/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php @@ -22,7 +22,6 @@ use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use Doctrine\DBAL\Driver\Exception as DoctrineException; use Doctrine\DBAL\Exception; use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Session; use Psr\Container\ContainerExceptionInterface; @@ -41,33 +40,36 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent */ public function checklogin() { - $lgn_user = $this->d3WebauthnGetRequestObject()->getRequestParameter( 'user') ?: - $this->d3WebauthnGetSessionObject()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER); + $lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter( 'user') ?: + $this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER); /** @var d3_User_Webauthn $user */ - $user = $this->d3WebauthnGetUserObject(); + $user = $this->d3GetMockableOxNewObject(User::class); $userId = $user->d3GetLoginUserId($lgn_user, 'malladmin'); if ( $this->d3CanUseWebauthn( $lgn_user, $userId)) { - $this->d3WebauthnGetSessionObject()->setVariable( + $this->d3GetMockableRegistryObject(Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_PROFILE, - $this->d3WebauthnGetRequestObject()->getRequestEscapedParameter( 'profile') + $this->d3GetMockableRegistryObject(Request::class) + ->getRequestEscapedParameter( 'profile') ); - $this->d3WebauthnGetSessionObject()->setVariable( + $this->d3GetMockableRegistryObject(Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE, - $this->d3WebauthnGetRequestObject()->getRequestEscapedParameter( 'chlanguage') + $this->d3GetMockableRegistryObject(Request::class) + ->getRequestEscapedParameter( 'chlanguage') ); if ($this->hasWebauthnButNotLoggedin($userId)) { - $this->d3WebauthnGetSessionObject()->setVariable( + $this->d3GetMockableRegistryObject(Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS, $this->getClassKey() != 'd3webauthnadminlogin' ? $this->getClassKey() : 'admin_start' ); - $this->d3WebauthnGetSessionObject()->setVariable( + $this->d3GetMockableRegistryObject(Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER, $userId ); - $this->d3WebauthnGetSessionObject()->setVariable( + $this->d3GetMockableRegistryObject(Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER, $lgn_user ); @@ -79,45 +81,13 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent return $this->d3CallMockableParent('checklogin'); } - /** - * @return Webauthn - */ - public function d3GetWebauthnObject(): Webauthn - { - return oxNew(Webauthn::class); - } - /** * @return void */ public function d3WebauthnCancelLogin(): void { - $oUser = $this->d3WebauthnGetUserObject(); - $oUser->logout(); - } - - /** - * @return User - */ - public function d3WebauthnGetUserObject(): User - { - return oxNew(User::class); - } - - /** - * @return Request - */ - public function d3WebauthnGetRequestObject(): Request - { - return Registry::getRequest(); - } - - /** - * @return Session - */ - public function d3WebauthnGetSessionObject(): Session - { - return Registry::getSession(); + $user = $this->d3GetMockableOxNewObject(User::class); + $user->logout(); } /** @@ -128,11 +98,12 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent */ protected function d3CanUseWebauthn( $lgn_user, ?string $userId): bool { - $password = $this->d3WebauthnGetRequestObject()->getRequestParameter( 'pwd'); + $password = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter( 'pwd'); return $lgn_user && $userId && - false === $this->d3WebauthnGetSessionObject()->hasVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH ) && + false === $this->d3GetMockableRegistryObject(Session::class) + ->hasVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH ) && ( ! strlen( trim( (string) $password ) ) ); } @@ -144,9 +115,10 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent */ protected function hasWebauthnButNotLoggedin($userId): bool { - $webauthn = $this->d3GetWebauthnObject(); + $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); return $webauthn->isActive($userId) - && !$this->d3WebauthnGetSessionObject()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH); + && !$this->d3GetMockableRegistryObject(Session::class) + ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH); } } \ No newline at end of file diff --git a/src/tests/integration/integrationTestCase.php b/src/tests/integration/integrationTestCase.php index a8ea38e..c6343ca 100644 --- a/src/tests/integration/integrationTestCase.php +++ b/src/tests/integration/integrationTestCase.php @@ -58,6 +58,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase * @param $sClass * @param $sId * @param array $aFields + * @param bool $blAdmin * @throws Exception */ public function createObject($sClass, $sId, $aFields = [], $blAdmin = false) @@ -112,6 +113,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase /** * @param $sId * @param array $aFields + * @param bool $blAdmin * @throws Exception */ public function createUser($sId, $aFields = [], $blAdmin = false) diff --git a/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php b/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php index bc5eb36..5da6a8b 100644 --- a/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php +++ b/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php @@ -14,6 +14,7 @@ namespace D3\Webauthn\tests\unit\Application\Controller\Admin; use D3\TestingTools\Development\CanAccessRestricted; +use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Controller\Admin\d3user_webauthn; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; @@ -21,6 +22,7 @@ use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsView; use PHPUnit\Framework\MockObject\MockObject; @@ -30,6 +32,7 @@ use ReflectionException; class d3user_webauthnTest extends TestCase { + use IsMockable; use CanAccessRestricted; public function setUp(): void @@ -65,16 +68,26 @@ class d3user_webauthnTest extends TestCase /** @var d3user_webauthn|MockObject $sutMock */ $sutMock = $this->getMockBuilder(d3user_webauthn::class) ->onlyMethods([ - 'd3GetWebauthnObject', 'd3CallMockableParent', 'getEditObjectId', - 'd3GetUserObject' + 'd3GetMockableOxNewObject' ]) ->getMock(); - $sutMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); $sutMock->method('d3CallMockableParent')->willReturn(true); $sutMock->method('getEditObjectId')->willReturn('editObjectId'); - $sutMock->method('d3GetUserObject')->willReturn($userMock); + $sutMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($userMock, $webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $userMock; + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->setValue( $sutMock, @@ -130,14 +143,24 @@ class d3user_webauthnTest extends TestCase ->onlyMethods([ 'setPageType', 'setAuthnRegister', - 'd3GetLoggerObject', - 'd3GetUtilsObject' + 'd3GetMockableLogger', + 'd3GetMockableRegistryObject' ]) ->getMock(); $sutMock->expects($this->atLeastOnce())->method('setPageType'); $sutMock->expects($this->atLeastOnce())->method('setAuthnRegister'); - $sutMock->expects($this->never())->method('d3GetLoggerObject')->willReturn($loggerMock); - $sutMock->expects($this->never())->method('d3GetUtilsObject')->willReturn($utilsMock); + $sutMock->expects($this->never())->method('d3GetMockableLogger')->willReturn($loggerMock); + $sutMock->expects($this->never())->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsMock) { + $args = func_get_args(); + switch ($args[0]) { + case Utils::class: + return $utilsMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sutMock, @@ -169,14 +192,24 @@ class d3user_webauthnTest extends TestCase ->onlyMethods([ 'setPageType', 'setAuthnRegister', - 'd3GetLoggerObject', - 'd3GetUtilsObject' + 'd3GetMockableLogger', + 'd3GetMockableRegistryObject' ]) ->getMock(); $sutMock->expects($this->atLeastOnce())->method('setPageType'); $sutMock->expects($this->atLeastOnce())->method('setAuthnRegister')->willThrowException(oxNew(WebauthnException::class)); - $sutMock->expects($this->atLeastOnce())->method('d3GetLoggerObject')->willReturn($loggerMock); - $sutMock->expects($this->atLeastOnce())->method('d3GetUtilsObject')->willReturn($utilsMock); + $sutMock->expects($this->atLeastOnce())->method('d3GetMockableLogger')->willReturn($loggerMock); + $sutMock->expects($this->atLeastOnce())->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsMock) { + $args = func_get_args(); + switch ($args[0]) { + case Utils::class: + return $utilsMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sutMock, @@ -207,10 +240,20 @@ class d3user_webauthnTest extends TestCase /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetUtilsViewObject', 'd3GetLoggerObject']) + ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger']) ->getMock(); - $oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); - $oControllerMock->expects($this->atLeastOnce())->method('d3GetLoggerObject')->willReturn($loggerMock); + $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); + $oControllerMock->expects($this->atLeastOnce())->method('d3GetMockableLogger')->willReturn($loggerMock); $this->callMethod( $oControllerMock, @@ -243,10 +286,30 @@ class d3user_webauthnTest extends TestCase /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->getMock(); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); - $oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $oControllerMock, @@ -285,11 +348,31 @@ class d3user_webauthnTest extends TestCase /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject', 'd3GetLoggerObject']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3GetMockableLogger']) ->getMock(); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); - $oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); - $oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); + $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); $this->callMethod( $oControllerMock, @@ -339,9 +422,19 @@ class d3user_webauthnTest extends TestCase /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetWebauthnObject', 'addTplParam', 'getUser']) + ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser']) ->getMock(); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webAuthnMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webAuthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webAuthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $oControllerMock->expects($throwExc ? $this->never() : $this->atLeast(3)) ->method('addTplParam'); $oControllerMock->method('getUser')->willReturn(oxNew(User::class)); @@ -391,10 +484,21 @@ class d3user_webauthnTest extends TestCase /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetUserObject', 'd3GetPublicKeyCredentialListObject']) + ->onlyMethods(['d3GetMockableOxNewObject']) ->getMock(); - $oControllerMock->method('d3GetUserObject')->willReturn($oUser); - $oControllerMock->method('d3GetPublicKeyCredentialListObject')->willReturn($publicKeyCredentialListMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($oUser, $publicKeyCredentialListMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $oUser; + case PublicKeyCredentialList::class: + return $publicKeyCredentialListMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->assertIsArray( $this->callMethod( @@ -405,26 +509,6 @@ class d3user_webauthnTest extends TestCase ); } - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::d3GetUserObject - */ - public function d3GetUserObjectReturnsRightInstance() - { - /** @var d3user_webauthn $sut */ - $sut = oxNew(d3user_webauthn::class); - - $this->assertInstanceOf( - User::class, - $this->callMethod( - $sut, - 'd3GetUserObject' - ) - ); - } - /** * @test * @throws ReflectionException @@ -445,9 +529,19 @@ class d3user_webauthnTest extends TestCase /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetPublicKeyCredentialObject']) + ->onlyMethods(['d3GetMockableOxNewObject']) ->getMock(); - $oControllerMock->method('d3GetPublicKeyCredentialObject')->willReturn($publicKeyCredentialMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($publicKeyCredentialMock) { + $args = func_get_args(); + switch ($args[0]) { + case PublicKeyCredential::class: + return $publicKeyCredentialMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->callMethod($oControllerMock, 'deleteKey'); } @@ -461,23 +555,4 @@ class d3user_webauthnTest extends TestCase 'has delete id' => ['deleteId', $this->once()] ]; } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::d3GetUtilsObject - */ - public function getUtilsObjectReturnsRightInstance() - { - /** @var d3user_webauthn $sut */ - $sut = oxNew(d3user_webauthn::class); - $this->assertInstanceOf( - Utils::class, - $this->callMethod( - $sut, - 'd3GetUtilsObject' - ) - ); - } } \ No newline at end of file diff --git a/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php b/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php index 1c742c4..aae9249 100644 --- a/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php +++ b/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php @@ -16,6 +16,7 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Application\Controller\Admin; use D3\TestingTools\Development\CanAccessRestricted; +use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin; use D3\Webauthn\Application\Controller\d3webauthnlogin; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; @@ -23,17 +24,17 @@ use D3\Webauthn\Application\Model\WebauthnAfterLogin; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\tests\unit\Application\Controller\d3webauthnloginTest; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Session; -use OxidEsales\Eshop\Core\SystemEventHandler; use OxidEsales\Eshop\Core\Utils; -use OxidEsales\Eshop\Core\UtilsServer; use OxidEsales\Eshop\Core\UtilsView; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; class d3webauthnadminloginTest extends d3webauthnloginTest { + use IsMockable; use CanAccessRestricted; protected $sutClassName = d3webauthnadminlogin::class; @@ -95,18 +96,39 @@ class d3webauthnadminloginTest extends d3webauthnloginTest /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetSession', 'getUtils', 'd3CallMockableParent', 'd3WebauthnGetAfterLogin', + ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent', 'd3GetMockableOxNewObject', 'generateCredentialRequest', 'addTplParam']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); - $sut->method('getUtils')->willReturn($utilsMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsMock, $sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Utils::class: + return $utilsMock; + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $sut->method('d3CallMockableParent')->willReturn('myTemplate.tpl'); // "any" because redirect doesn't stop execution $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) ->method('generateCredentialRequest'); $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) ->method('addTplParam')->willReturn(true); - $sut->method('d3WebauthnGetAfterLogin')->willReturn($afterLoginMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($afterLoginMock) { + $args = func_get_args(); + switch ($args[0]) { + case WebauthnAfterLogin::class: + return $afterLoginMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->assertSame( 'myTemplate.tpl', @@ -151,17 +173,6 @@ class d3webauthnadminloginTest extends d3webauthnloginTest parent::generateCredentialRequestFailed($redirectClass, $userVarName); } - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::getUtils - */ - public function getUtilsReturnsRightInstance() - { - parent::getUtilsReturnsRightInstance(); - } - /** * @test * @return void @@ -226,10 +237,30 @@ class d3webauthnadminloginTest extends d3webauthnloginTest /** @var d3webauthnadminlogin|MockObject $sut */ $sut = $this->getMockBuilder(d3webauthnadminlogin::class) - ->onlyMethods(['getWebauthnLoginObject', 'd3WebAuthnGetRequest']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->getMock(); - $sut->method('getWebauthnLoginObject')->willReturn($loginMock); - $sut->method('d3WebAuthnGetRequest')->willReturn($requestMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($loginMock) { + $args = func_get_args(); + switch ($args[0]) { + case WebauthnLogin::class: + return $loginMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($requestMock) { + $args = func_get_args(); + switch ($args[0]) { + case Request::class: + return $requestMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( 'expected', @@ -262,11 +293,22 @@ class d3webauthnadminloginTest extends d3webauthnloginTest /** @var d3webauthnadminlogin|MockObject $sut */ $sut = $this->getMockBuilder(d3webauthnadminlogin::class) - ->onlyMethods(['getWebauthnLoginObject', 'd3WebAuthnGetRequest', 'd3GetUtilsViewObject']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->getMock(); - $sut->method('getWebauthnLoginObject')->willThrowException(oxNew(WebauthnGetException::class)); - $sut->method('d3WebAuthnGetRequest')->willReturn($requestMock); - $sut->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); + $sut->method('d3GetMockableOxNewObject')->willThrowException(oxNew(WebauthnGetException::class)); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock, $requestMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + case Request::class: + return $requestMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( 'login', @@ -289,100 +331,4 @@ class d3webauthnadminloginTest extends d3webauthnloginTest 'assertion succ shop1' => ['admin_start', false, 'cookie', 1], ]; } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::d3WebauthnGetEventHandler - */ - public function canGetSystemEventHandler() - { - $sut = oxNew(d3webauthnadminlogin::class); - - $this->assertInstanceOf( - SystemEventHandler::class, - $this->callMethod( - $sut, - 'd3WebauthnGetEventHandler' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::d3WebAuthnGetRequest - */ - public function canGetRequest() - { - $sut = oxNew(d3webauthnadminlogin::class); - - $this->assertInstanceOf( - Request::class, - $this->callMethod( - $sut, - 'd3WebAuthnGetRequest' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::d3WebauthnGetUtilsServer - */ - public function canGetUtilsServer() - { - $sut = oxNew(d3webauthnadminlogin::class); - - $this->assertInstanceOf( - UtilsServer::class, - $this->callMethod( - $sut, - 'd3WebauthnGetUtilsServer' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::getWebauthnLoginObject - */ - public function canGetWebauthnLoginObject() - { - $sut = oxNew(d3webauthnadminlogin::class); - - $this->assertInstanceOf( - WebauthnLogin::class, - $this->callMethod( - $sut, - 'getWebauthnLoginObject', - ['credential', 'error'] - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::d3WebauthnGetAfterLogin - */ - public function canGetWebauthnAfterLoginObject() - { - $sut = oxNew(d3webauthnadminlogin::class); - - $this->assertInstanceOf( - WebauthnAfterLogin::class, - $this->callMethod( - $sut, - 'd3WebauthnGetAfterLogin' - ) - ); - } } \ No newline at end of file diff --git a/src/tests/unit/Application/Controller/d3_account_webauthnTest.php b/src/tests/unit/Application/Controller/d3_account_webauthnTest.php index ebecb3f..e79c084 100644 --- a/src/tests/unit/Application/Controller/d3_account_webauthnTest.php +++ b/src/tests/unit/Application/Controller/d3_account_webauthnTest.php @@ -22,6 +22,7 @@ use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Webauthn; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; @@ -79,10 +80,20 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['getUser', 'd3GetWebauthnObject']) + ->onlyMethods(['getUser', 'd3GetMockableOxNewObject']) ->getMock(); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webAuthnMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webAuthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webAuthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->_oController = $oControllerMock; @@ -111,10 +122,20 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['getUser', 'd3GetWebauthnObject']) + ->onlyMethods(['getUser', 'd3GetMockableOxNewObject']) ->getMock(); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webAuthnMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webAuthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webAuthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->_oController = $oControllerMock; @@ -149,10 +170,20 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['getUser', 'd3GetPublicKeyCredentialListObject']) + ->onlyMethods(['getUser', 'd3GetMockableOxNewObject']) ->getMock(); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetPublicKeyCredentialListObject')->willReturn($publicKeyCredentialListMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($publicKeyCredentialListMock) { + $args = func_get_args(); + switch ($args[0]) { + case PublicKeyCredentialList::class: + return $publicKeyCredentialListMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->_oController = $oControllerMock; @@ -188,12 +219,12 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetLoggerObject']) + ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetMockableLogger']) ->getMock(); $oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister'); $oControllerMock->expects($this->atLeastOnce())->method('setPageType'); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); + $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); $this->_oController = $oControllerMock; @@ -226,13 +257,13 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetLoggerObject']) + ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetMockableLogger']) ->getMock(); $oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister') ->willThrowException(oxNew(WebauthnException::class)); $oControllerMock->expects($this->never())->method('setPageType'); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); + $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); $this->_oController = $oControllerMock; @@ -264,9 +295,19 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetWebauthnObject', 'addTplParam', 'getUser']) + ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser']) ->getMock(); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webAuthnMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webAuthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webAuthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $oControllerMock->expects($throwExc ? $this->never() : $this->atLeast(3)) ->method('addTplParam'); $oControllerMock->method('getUser')->willReturn(oxNew(User::class)); @@ -343,10 +384,20 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetUtilsViewObject', 'd3GetLoggerObject']) + ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger']) ->getMock(); - $oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); - $oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); + $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); + $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); $this->_oController = $oControllerMock; @@ -381,10 +432,30 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->getMock(); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); - $oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); $this->_oController = $oControllerMock; @@ -420,10 +491,30 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->getMock(); - $oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); - $oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); $this->_oController = $oControllerMock; @@ -453,9 +544,19 @@ class d3_account_webauthnTest extends UnitTestCase /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetPublicKeyCredentialObject']) + ->onlyMethods(['d3GetMockableOxNewObject']) ->getMock(); - $oControllerMock->method('d3GetPublicKeyCredentialObject')->willReturn($publicKeyCredentialMock); + $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($publicKeyCredentialMock) { + $args = func_get_args(); + switch ($args[0]) { + case PublicKeyCredential::class: + return $publicKeyCredentialMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->_oController = $oControllerMock; @@ -488,52 +589,4 @@ class d3_account_webauthnTest extends UnitTestCase ) ); } - - /** - * @test - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3_account_webauthn::d3GetPublicKeyCredentialObject - */ - public function getPublicKeyCredentialObjectReturnsRightObject() - { - $this->assertInstanceOf( - PublicKeyCredential::class, - $this->callMethod( - $this->_oController, - 'd3GetPublicKeyCredentialObject' - ) - ); - } - - /** - * @test - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3_account_webauthn::d3GetPublicKeyCredentialListObject - */ - public function getPublicKeyCredentialListObjectReturnsRightObject() - { - $this->assertInstanceOf( - PublicKeyCredentialList::class, - $this->callMethod( - $this->_oController, - 'd3GetPublicKeyCredentialListObject' - ) - ); - } - - /** - * @test - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3_account_webauthn::d3GetUtilsViewObject - */ - public function getUtilsViewObjectReturnsRightObject() - { - $this->assertInstanceOf( - UtilsView::class, - $this->callMethod( - $this->_oController, - 'd3GetUtilsViewObject' - ) - ); - } } diff --git a/src/tests/unit/Application/Controller/d3webauthnloginTest.php b/src/tests/unit/Application/Controller/d3webauthnloginTest.php index d06343e..5d85b00 100644 --- a/src/tests/unit/Application/Controller/d3webauthnloginTest.php +++ b/src/tests/unit/Application/Controller/d3webauthnloginTest.php @@ -20,12 +20,11 @@ use D3\Webauthn\Application\Controller\d3webauthnlogin; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; -use OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Utils; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use ReflectionException; @@ -53,9 +52,19 @@ class d3webauthnloginTest extends UnitTestCase /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetSession', 'd3CallMockableParent']) + ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $sut->method('d3CallMockableParent')->willReturn(['defKey1' => 'devValues1']); $this->assertSame( @@ -76,9 +85,10 @@ class d3webauthnloginTest extends UnitTestCase * @param $auth * @param $userFromLogin * @param $startRedirect + * @param $redirectController * @return void * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::render + * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::render * @dataProvider canRenderDataProvider */ public function canRender($auth, $userFromLogin, $startRedirect, $redirectController) @@ -101,11 +111,22 @@ class d3webauthnloginTest extends UnitTestCase /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetSession', 'getUtils', 'd3CallMockableParent', + ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent', 'generateCredentialRequest', 'addTplParam']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); - $sut->method('getUtils')->willReturn($utilsMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsMock, $sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Utils::class: + return $utilsMock; + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $sut->method('d3CallMockableParent')->willReturn('myTemplate.tpl'); $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) ->method('generateCredentialRequest'); @@ -165,13 +186,33 @@ class d3webauthnloginTest extends UnitTestCase /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetSession', 'd3GetWebauthnObject', 'addTplParam', 'd3GetLoggerObject']) + ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableOxNewObject', 'addTplParam', 'd3GetMockableLogger']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); - $sut->method('d3GetWebauthnObject')->willReturn($webAuthnMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webAuthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webAuthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $sut->expects($this->atLeast(2)) ->method('addTplParam')->willReturn(true); - $sut->method('d3GetLoggerObject')->willReturn($loggerMock); + $sut->method('d3GetMockableLogger')->willReturn($loggerMock); $this->callMethod( $sut, @@ -220,15 +261,36 @@ class d3webauthnloginTest extends UnitTestCase /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetSession', 'd3GetWebauthnObject', 'addTplParam', - 'd3GetLoggerObject', 'getUtils']) + ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', + 'd3GetMockableLogger', 'd3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); - $sut->method('d3GetWebauthnObject')->willReturn($webAuthnMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webAuthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webAuthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $sut->expects($this->never()) ->method('addTplParam')->willReturn(true); - $sut->expects($this->atLeast(2))->method('d3GetLoggerObject')->willReturn($loggerMock); - $sut->method('getUtils')->willReturn($utilsMock); + $sut->expects($this->atLeast(2))->method('d3GetMockableLogger')->willReturn($loggerMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsMock, $sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Utils::class: + return $utilsMock; + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sut, @@ -236,25 +298,6 @@ class d3webauthnloginTest extends UnitTestCase ); } - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::getUtils - */ - public function getUtilsReturnsRightInstance() - { - $sut = oxNew($this->sutClassName); - - $this->assertInstanceOf( - Utils::class, - $this->callMethod( - $sut, - 'getUtils' - ) - ); - } - /** * @test * @return void @@ -275,9 +318,19 @@ class d3webauthnloginTest extends UnitTestCase /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetSession']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( $currClassFixture, @@ -378,80 +431,4 @@ class d3webauthnloginTest extends UnitTestCase ) ); } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::d3GetSession - */ - public function canGetSession() - { - $sut = oxNew($this->sutClassName); - - $this->assertInstanceOf( - Session::class, - $this->callMethod( - $sut, - 'd3GetSession' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::d3GetWebauthnObject - */ - public function canGetWebauthnObject() - { - $sut = oxNew($this->sutClassName); - - $this->assertInstanceOf( - Webauthn::class, - $this->callMethod( - $sut, - 'd3GetWebauthnObject' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::d3GetLoggerObject - */ - public function canGetLogger() - { - $sut = oxNew($this->sutClassName); - - $this->assertInstanceOf( - LoggerInterface::class, - $this->callMethod( - $sut, - 'd3GetLoggerObject' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::d3GetControllerClassNameResolver - */ - public function canGetClassNameResolver() - { - $sut = oxNew($this->sutClassName); - - $this->assertInstanceOf( - ControllerClassNameResolver::class, - $this->callMethod( - $sut, - 'd3GetControllerClassNameResolver' - ) - ); - } } \ No newline at end of file diff --git a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php index 598068d..fe7244c 100644 --- a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php +++ b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php @@ -21,11 +21,11 @@ use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\UserEntity; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; use Webauthn\PublicKeyCredentialSource; -use Webauthn\PublicKeyCredentialUserEntity; class PublicKeyCredentialListTest extends UnitTestCase { @@ -77,9 +77,19 @@ class PublicKeyCredentialListTest extends UnitTestCase /** @var PublicKeyCredentialList|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredentialList::class) ->disableOriginalConstructor() - ->onlyMethods(['getConfigObject']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('getConfigObject')->willReturn($configMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($configMock) { + $args = func_get_args(); + switch ($args[0]) { + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); if ($doCreate) { $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) @@ -145,9 +155,19 @@ class PublicKeyCredentialListTest extends UnitTestCase /** @var PublicKeyCredentialList|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredentialList::class) ->disableOriginalConstructor() - ->onlyMethods(['getConfigObject']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('getConfigObject')->willReturn($configMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($configMock) { + $args = func_get_args(); + switch ($args[0]) { + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); if ($doCreate) { $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) @@ -214,9 +234,19 @@ class PublicKeyCredentialListTest extends UnitTestCase /** @var PublicKeyCredentialList|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredentialList::class) ->disableOriginalConstructor() - ->onlyMethods(['getConfigObject']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('getConfigObject')->willReturn($configMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($configMock) { + $args = func_get_args(); + switch ($args[0]) { + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -304,28 +334,4 @@ class PublicKeyCredentialListTest extends UnitTestCase [$pkcsMock] ); } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList::getConfigObject() - */ - public function canGetConfigObject() - { - /** @var PublicKeyCredentialList|MockObject $sut */ - $sut = $this->getMockBuilder(PublicKeyCredentialList::class) - ->disableOriginalConstructor() - ->onlyMethods(['saveCredentialSource']) // required for code coverage - ->getMock(); - $sut->method('saveCredentialSource'); - - $this->assertInstanceOf( - Config::class, - $this->callMethod( - $sut, - 'getConfigObject' - ) - ); - } } \ No newline at end of file diff --git a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php index b6e3499..5679541 100644 --- a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php +++ b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php @@ -19,6 +19,7 @@ use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use OxidEsales\Eshop\Core\Config; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; @@ -75,7 +76,8 @@ class PublicKeyCredentialTest extends UnitTestCase /** * @param $fieldName * @param $sutMethod - * @param $value + * @param $setValue + * @param $getValue * @return void * @throws ReflectionException */ @@ -232,9 +234,19 @@ class PublicKeyCredentialTest extends UnitTestCase /** @var PublicKeyCredential|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredential::class) - ->onlyMethods(['getPublicKeyCredentialListObject', 'exists', 'getIdByCredentialId', 'load', 'save']) + ->onlyMethods(['d3GetMockableOxNewObject', 'exists', 'getIdByCredentialId', 'load', 'save']) ->getMock(); - $sut->method('getPublicKeyCredentialListObject')->willReturn($pkcListObjectMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($pkcListObjectMock) { + $args = func_get_args(); + switch ($args[0]) { + case PublicKeyCredentialList::class: + return $pkcListObjectMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $sut->method('exists')->willReturn($credIdExist); $sut->expects($this->exactly((int) $doSave))->method('getIdByCredentialId'); $sut->expects($this->exactly((int) ($doSave && $credIdExist)))->method('load'); @@ -266,28 +278,6 @@ class PublicKeyCredentialTest extends UnitTestCase ]; } - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\Credential\PublicKeyCredential::getPublicKeyCredentialListObject - */ - public function canGetPublicKeyCredentialListObject() - { - /** @var PublicKeyCredential|MockObject $sut */ - $sut = $this->getMockBuilder(PublicKeyCredential::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->assertInstanceOf( - PublicKeyCredentialList::class, - $this->callMethod( - $sut, - 'getPublicKeyCredentialListObject' - ) - ); - } - /** * @test * @return void @@ -309,9 +299,19 @@ class PublicKeyCredentialTest extends UnitTestCase /** @var PublicKeyCredential|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredential::class) - ->onlyMethods(['d3GetConfig', 'allowDerivedDelete']) + ->onlyMethods(['d3GetMockableRegistryObject', 'allowDerivedDelete']) ->getMock(); - $sut->method('d3GetConfig')->willReturn($configMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($configMock) { + $args = func_get_args(); + switch ($args[0]) { + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); $sut->method('allowDerivedDelete')->willReturn(true); if ($doCreate) { @@ -347,24 +347,4 @@ class PublicKeyCredentialTest extends UnitTestCase 'item not exists' => [false, null] ]; } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\Credential\PublicKeyCredential::d3GetConfig - */ - public function canGetConfig() - { - /** @var PublicKeyCredential $sut */ - $sut = oxNew(PublicKeyCredential::class); - - $this->assertInstanceOf( - Config::class, - $this->callMethod( - $sut, - 'd3GetConfig' - ) - ); - } } \ No newline at end of file diff --git a/src/tests/unit/Application/Model/RelyingPartyEntityTest.php b/src/tests/unit/Application/Model/RelyingPartyEntityTest.php index 77f0377..29e768d 100644 --- a/src/tests/unit/Application/Model/RelyingPartyEntityTest.php +++ b/src/tests/unit/Application/Model/RelyingPartyEntityTest.php @@ -19,7 +19,7 @@ use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\RelyingPartyEntity; use OxidEsales\Eshop\Application\Model\Shop; use OxidEsales\Eshop\Core\Config; -use OxidEsales\TestingLibrary\UnitTestCase; +use OxidEsales\Eshop\Core\Registry; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use ReflectionException; @@ -120,9 +120,19 @@ class RelyingPartyEntityTest extends TestCase /** @var RelyingPartyEntity|MockObject $sut */ $sut = $this->getMockBuilder(RelyingPartyEntity::class) ->disableOriginalConstructor() - ->onlyMethods(['getConfig']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('getConfig')->willReturn($configMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($configMock) { + $args = func_get_args(); + switch ($args[0]) { + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( $fixture, @@ -216,30 +226,6 @@ class RelyingPartyEntityTest extends TestCase ]; } - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::getConfig - */ - public function canGetConfig() - { - /** @var RelyingPartyEntity|MockObject $sut */ - $sut = $this->getMockBuilder(RelyingPartyEntity::class) - ->disableOriginalConstructor() - ->onlyMethods(['hasConfiguredShopUrl']) // required for code coverage - ->getMock(); - $sut->method('hasConfiguredShopUrl')->willReturn(true); - - $this->assertInstanceOf( - Config::class, - $this->callMethod( - $sut, - 'getConfig' - ) - ); - } - /** * @test * @return void diff --git a/src/tests/unit/Application/Model/UserEntityTest.php b/src/tests/unit/Application/Model/UserEntityTest.php index 803b802..af3cf1e 100644 --- a/src/tests/unit/Application/Model/UserEntityTest.php +++ b/src/tests/unit/Application/Model/UserEntityTest.php @@ -65,7 +65,7 @@ class UserEntityTest extends UnitTestCase if (!$runParent) { $this->expectException(WebauthnException::class); - }; + } $this->callMethod( $sut, diff --git a/src/tests/unit/Application/Model/WebauthnLoginTest.php b/src/tests/unit/Application/Model/WebauthnLoginTest.php index 92d3f4b..4b55d1e 100644 --- a/src/tests/unit/Application/Model/WebauthnLoginTest.php +++ b/src/tests/unit/Application/Model/WebauthnLoginTest.php @@ -20,7 +20,6 @@ use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException; use D3\Webauthn\Application\Model\Webauthn; -use D3\Webauthn\Application\Model\WebauthnAfterLogin; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnLogin; use OxidEsales\Eshop\Application\Component\UserComponent; @@ -29,6 +28,7 @@ use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Exception\CookieException; use OxidEsales\Eshop\Core\Exception\UserException; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\SystemEventHandler; use OxidEsales\Eshop\Core\Utils; @@ -92,7 +92,7 @@ class WebauthnLoginTest extends UnitTestCase $this->assertSame( $credFixture, - $cred = $this->callMethod( + $this->callMethod( $sut, 'getCredential' ) @@ -216,7 +216,7 @@ class WebauthnLoginTest extends UnitTestCase $sut = $this->getMockBuilder(WebauthnLogin::class) ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie', - 'd3WebauthnGetEventHandler', 'getAfterLogin', 'getCredential', 'regenerateSessionId']) + 'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId']) ->disableOriginalConstructor() ->getMock(); $sut->expects($this->exactly((int) $setCookie))->method('setSessionCookie'); @@ -279,15 +279,35 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'd3GetUtilsViewObject', + ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'd3GetMockableRegistryObject', 'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie', - 'd3GetLoggerObject', 'd3GetUserObject', 'getCredential', 'regenerateSessionId']) + 'd3GetMockableLogger', 'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId']) ->disableOriginalConstructor() ->getMock(); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); - $sut->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); - $sut->method('d3GetUserObject')->willReturn($userMock); - $sut->method('d3GetLoggerObject')->willReturn($loggerMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($userMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $userMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $sut->method('d3GetMockableLogger')->willReturn($loggerMock); $this->expectException(WebauthnLoginErrorException::class); @@ -321,13 +341,29 @@ class WebauthnLoginTest extends UnitTestCase */ public function adminLoginSuccess() { + /** @var User|MockObject $userMock */ + $userMock = $this->getMockBuilder(User::class) + ->disableOriginalConstructor() + ->getMock(); + /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights', - 'd3WebauthnGetEventHandler', 'getAfterLogin']) + 'd3GetMockableOxNewObject']) ->disableOriginalConstructor() ->getMock(); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($userMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $userMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->assertSame( 'admin_start', @@ -348,6 +384,12 @@ class WebauthnLoginTest extends UnitTestCase */ public function adminLoginException($exceptionClass, $writeLog) { + /** @var SystemEventHandler|MockObject $systemEventHandlerMock */ + $systemEventHandlerMock = $this->getMockBuilder(SystemEventHandler::class) + ->onlyMethods(['onAdminLogin']) + ->getMock(); + $systemEventHandlerMock->expects($this->never())->method('onAdminLogin'); + /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) ->onlyMethods(['addErrorToDisplay']) @@ -369,14 +411,36 @@ class WebauthnLoginTest extends UnitTestCase $sut = $this->getMockBuilder(WebauthnLogin::class) ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights', - 'd3WebauthnGetEventHandler', 'getAfterLogin', 'd3GetUtilsViewObject', - 'd3GetUserObject', 'd3GetLoggerObject']) + 'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject', + 'd3GetMockableLogger']) ->disableOriginalConstructor() ->getMock(); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); - $sut->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); - $sut->method('d3GetUserObject')->willReturn($userMock); - $sut->method('d3GetLoggerObject')->willReturn($loggerMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); + $sut->method('d3GetMockableLogger')->willReturn($loggerMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($userMock, $systemEventHandlerMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $userMock; + case SystemEventHandler::class: + return $systemEventHandlerMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->assertSame( 'login', @@ -458,7 +522,7 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['getCredential', 'd3GetWebauthnObject']) + ->onlyMethods(['getCredential', 'd3GetMockableOxNewObject']) ->disableOriginalConstructor() ->getMock(); if ($throwException) { @@ -466,7 +530,17 @@ class WebauthnLoginTest extends UnitTestCase } else { $sut->method('getCredential')->willReturn('credential'); } - $sut->method('d3GetWebauthnObject')->willReturn($webauthnMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); if ($throwException) { $this->expectException(WebauthnGetException::class); @@ -515,10 +589,20 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetSession']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( $sessionMock, @@ -561,10 +645,21 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetConfig', 'getUtilsServer']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetConfig')->willReturn($configMock); - $sut->method('getUtilsServer')->willReturn($utilsServerMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsServerMock, $configMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsServer::class: + return $utilsServerMock; + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sut, @@ -606,10 +701,20 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetUserObject']) + ->onlyMethods(['d3GetMockableOxNewObject']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetUserObject')->willReturn($userMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($userMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $userMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); if ($throwsException) { $this->expectException(UserException::class); @@ -661,10 +766,20 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['getUtilsServer']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->disableOriginalConstructor() ->getMock(); - $sut->method('getUtilsServer')->willReturn($utilsServerMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsServerMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsServer::class: + return $utilsServerMock; + default: + return Registry::get($args[0]); + } + } + ); if ($throwException) { $this->expectException(CookieException::class); @@ -720,10 +835,20 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetConfig']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetConfig')->willReturn($configMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($configMock) { + $args = func_get_args(); + switch ($args[0]) { + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sut, @@ -764,9 +889,19 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetSession']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sut, @@ -815,11 +950,22 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetConfig', 'd3GetUtilsObject']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetConfig')->willReturn($configMock); - $sut->method('d3GetUtilsObject')->willReturn($utilsMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsMock, $configMock) { + $args = func_get_args(); + switch ($args[0]) { + case Utils::class: + return $utilsMock; + case Config::class: + return $configMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sut, @@ -864,9 +1010,19 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetSession']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sut, @@ -885,46 +1041,6 @@ class WebauthnLoginTest extends UnitTestCase ]; } - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\WebauthnLogin::d3WebauthnGetEventHandler - */ - public function canGetEventHandler() - { - /** @var WebauthnLogin $sut */ - $sut = oxNew(WebauthnLogin::class, 'cred'); - - $this->assertInstanceOf( - SystemEventHandler::class, - $this->callMethod( - $sut, - 'd3WebauthnGetEventHandler' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\WebauthnLogin::getAfterLogin - */ - public function canGetAfterLogin() - { - /** @var WebauthnLogin $sut */ - $sut = oxNew(WebauthnLogin::class, 'cred'); - - $this->assertInstanceOf( - WebauthnAfterLogin::class, - $this->callMethod( - $sut, - 'getAfterLogin' - ) - ); - } - /** * @test * @return void @@ -973,9 +1089,19 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetSession', 'isAdmin']) + ->onlyMethods(['d3GetMockableRegistryObject', 'isAdmin']) ->getMock(); - $sut->method('d3GetSession')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $sut->method('isAdmin')->willReturn($isAdmin); $this->assertSame( @@ -998,46 +1124,6 @@ class WebauthnLoginTest extends UnitTestCase ]; } - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\WebauthnLogin::d3GetConfig - */ - public function canGetConfig() - { - /** @var WebauthnLogin $sut */ - $sut = oxNew(WebauthnLogin::class, 'cred'); - - $this->assertInstanceOf( - Config::class, - $this->callMethod( - $sut, - 'd3GetConfig' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Model\WebauthnLogin::getUtilsServer - */ - public function canGetUtilsServer() - { - /** @var WebauthnLogin $sut */ - $sut = oxNew(WebauthnLogin::class, 'cred'); - - $this->assertInstanceOf( - UtilsServer::class, - $this->callMethod( - $sut, - 'getUtilsServer' - ) - ); - } - /** * @test * @return void @@ -1070,10 +1156,20 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['getCredential', 'd3GetSession']) + ->onlyMethods(['getCredential', 'd3GetMockableRegistryObject']) ->getMock(); $sut->method('getCredential')->willReturn('credentialFixture'); - $sut->method('d3GetSession')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->callMethod( $sut, diff --git a/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php b/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php index 6be4f38..7ab0f81 100644 --- a/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php +++ b/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php @@ -20,6 +20,7 @@ use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; use OxidEsales\Eshop\Application\Controller\Admin\LoginController; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Session; use OxidEsales\TestingLibrary\UnitTestCase; @@ -30,24 +31,6 @@ class LoginControllerWebauthnTest extends UnitTestCase { use CanAccessRestricted; - /** - * @test - * @covers \D3\Webauthn\Modules\Application\Controller\Admin\d3_LoginController_Webauthn::d3GetWebauthnObject - * @throws ReflectionException - */ - public function canGetWebauthnObject() - { - $sut = oxNew(LoginController::class); - - $this->assertInstanceOf( - Webauthn::class, - $this->callMethod( - $sut, - 'd3GetWebauthnObject' - ) - ); - } - /** * @test * @throws ReflectionException @@ -63,9 +46,19 @@ class LoginControllerWebauthnTest extends UnitTestCase /** @var LoginController|MockObject $sut */ $sut = $this->getMockBuilder(LoginController::class) - ->onlyMethods(['d3WebauthnGetUserObject']) + ->onlyMethods(['d3GetMockableOxNewObject']) ->getMock(); - $sut->method('d3WebauthnGetUserObject')->willReturn($userMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($userMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $userMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); $this->callMethod( $sut, @@ -73,63 +66,6 @@ class LoginControllerWebauthnTest extends UnitTestCase ); } - /** - * @test - * @throws ReflectionException - * @covers \D3\Webauthn\Modules\Application\Controller\Admin\d3_LoginController_Webauthn::d3WebauthnGetUserObject - */ - public function canGetUserObject() - { - /** @var LoginController $sut */ - $sut = oxNew(LoginController::class); - - $this->assertInstanceOf( - User::class, - $this->callMethod( - $sut, - 'd3WebauthnGetUserObject' - ) - ); - } - - /** - * @test - * @throws ReflectionException - * @covers \D3\Webauthn\Modules\Application\Controller\Admin\d3_LoginController_Webauthn::d3WebauthnGetRequestObject - */ - public function canGetRequestObject() - { - /** @var LoginController $sut */ - $sut = oxNew(LoginController::class); - - $this->assertInstanceOf( - Request::class, - $this->callMethod( - $sut, - 'd3WebauthnGetRequestObject' - ) - ); - } - - /** - * @test - * @throws ReflectionException - * @covers \D3\Webauthn\Modules\Application\Controller\Admin\d3_LoginController_Webauthn::d3WebauthnGetSessionObject - */ - public function canGetSessionObject() - { - /** @var LoginController $sut */ - $sut = oxNew(LoginController::class); - - $this->assertInstanceOf( - Session::class, - $this->callMethod( - $sut, - 'd3WebauthnGetSessionObject' - ) - ); - } - /** * @test * @param $username @@ -159,10 +95,21 @@ class LoginControllerWebauthnTest extends UnitTestCase /** @var LoginController|MockObject $sut */ $sut = $this->getMockBuilder(LoginController::class) - ->onlyMethods(['d3WebauthnGetRequestObject', 'd3WebauthnGetSessionObject']) + ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3WebauthnGetRequestObject')->willReturn($requestMock); - $sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($requestMock, $sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Request::class: + return $requestMock; + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( $expected, @@ -215,10 +162,30 @@ class LoginControllerWebauthnTest extends UnitTestCase /** @var LoginController|MockObject $sut */ $sut = $this->getMockBuilder(LoginController::class) - ->onlyMethods(['d3GetWebauthnObject', 'd3WebauthnGetSessionObject']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetWebauthnObject')->willReturn($webauthnMock); - $sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( $expected, @@ -281,15 +248,36 @@ class LoginControllerWebauthnTest extends UnitTestCase /** @var LoginController|MockObject $sut */ $sut = $this->getMockBuilder(LoginController::class) ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableParent', 'hasWebauthnButNotLoggedin', - 'd3WebauthnGetSessionObject', 'd3WebauthnGetUserObject', 'd3WebauthnGetRequestObject' + 'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject' ]) ->getMock(); $sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn); $sut->method('d3CallMockableParent')->willReturn('parentReturn'); $sut->method('hasWebauthnButNotLoggedin')->willReturn($loggedin); - $sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); - $sut->method('d3WebauthnGetUserObject')->willReturn($userMock); - $sut->method('d3WebauthnGetRequestObject')->willReturn($requestMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($userMock) { + $args = func_get_args(); + switch ($args[0]) { + case User::class: + return $userMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($requestMock, $sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Request::class: + return $requestMock; + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $this->assertSame( $expected, diff --git a/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php index b7531a2..1b3ad04 100644 --- a/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php +++ b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php @@ -22,6 +22,7 @@ use OxidEsales\Eshop\Application\Controller\OrderController; use OxidEsales\Eshop\Application\Controller\PaymentController; use OxidEsales\Eshop\Application\Controller\UserController; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; @@ -72,10 +73,30 @@ trait CheckoutTestTrait /** @var PaymentController|OrderController|UserController|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClass) - ->onlyMethods(['d3GetWebauthnObject', 'd3WebauthnGetSessionObject', 'd3CallMockableParent']) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3CallMockableParent']) ->getMock(); - $sut->method('d3GetWebauthnObject')->willReturn($webauthnMock); - $sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($webauthnMock) { + $args = func_get_args(); + switch ($args[0]) { + case Webauthn::class: + return $webauthnMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($sessionMock) { + $args = func_get_args(); + switch ($args[0]) { + case Session::class: + return $sessionMock; + default: + return Registry::get($args[0]); + } + } + ); $sut->method('d3CallMockableParent')->willReturn($userMock); $return = $this->callMethod( @@ -103,50 +124,4 @@ trait CheckoutTestTrait 'no webauthn auth' => [true, 'userIdFixture', true, null, false], ]; } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait::d3GetWebauthnObject - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController::d3GetWebauthnObject - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::d3GetWebauthnObject - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::d3GetWebauthnObject - */ - public function canGetWebauthnObject() - { - /** @var PaymentController|OrderController|UserController $sut */ - $sut = oxNew($this->sutClass); - - $this->assertInstanceOf( - Webauthn::class, - $this->callMethod( - $sut, - 'd3GetWebauthnObject' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait::d3WebauthnGetSessionObject - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController::d3WebauthnGetSessionObject - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::d3WebauthnGetSessionObject - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::d3WebauthnGetSessionObject - */ - public function canGetSessionObject() - { - /** @var PaymentController|OrderController|UserController $sut */ - $sut = oxNew($this->sutClass); - - $this->assertInstanceOf( - Session::class, - $this->callMethod( - $sut, - 'd3WebauthnGetSessionObject' - ) - ); - } } \ No newline at end of file