move OXID object getters to testing library

This commit is contained in:
Daniel Seifert 2022-12-04 00:24:28 +01:00 committed by Daniel Seifert
parent b8865241c5
commit ed9a06ee2d
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
26 changed files with 1020 additions and 1122 deletions

View File

@ -16,22 +16,26 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Controller\Admin; namespace D3\Webauthn\Application\Controller\Admin;
use D3\TestingTools\Production\IsMockable; 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\WebauthnCreateException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Exception as DoctrineException;
use Exception; use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsView;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
class d3user_webauthn extends AdminDetailsController class d3user_webauthn extends AdminDetailsController
{ {
use IsMockable; use IsMockable;
use helpersTrait;
protected $_sSaveError = null; protected $_sSaveError = null;
@ -42,7 +46,7 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function render(): string public function render(): string
{ {
$this->addTplParam('readonly', !$this->d3GetWebauthnObject()->isAvailable()); $this->addTplParam('readonly', !$this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable());
$this->d3CallMockableParent('render'); $this->d3CallMockableParent('render');
@ -50,7 +54,7 @@ class d3user_webauthn extends AdminDetailsController
if (isset($soxId) && $soxId != "-1") { if (isset($soxId) && $soxId != "-1") {
/** @var d3_User_Webauthn $oUser */ /** @var d3_User_Webauthn $oUser */
$oUser = $this->d3GetUserObject(); $oUser = $this->d3GetMockableOxNewObject(User::class);
if ($oUser->load($soxId)) { if ($oUser->load($soxId)) {
$this->addTplParam("oxid", $oUser->getId()); $this->addTplParam("oxid", $oUser->getId());
} else { } else {
@ -75,10 +79,10 @@ class d3user_webauthn extends AdminDetailsController
$this->setPageType( 'requestnew' ); $this->setPageType( 'requestnew' );
$this->setAuthnRegister(); $this->setAuthnRegister();
} catch (Exception|ContainerExceptionInterface|NotFoundExceptionInterface|DoctrineDriverException $e) { } catch (Exception|ContainerExceptionInterface|NotFoundExceptionInterface|DoctrineDriverException $e) {
$this->d3GetUtilsViewObject()->addErrorToDisplay($e); $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e);
$this->d3GetLoggerObject()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); $this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]);
$this->d3GetLoggerObject()->debug($e->getTraceAsString()); $this->d3GetMockableLogger()->debug($e->getTraceAsString());
$this->d3GetUtilsObject()->redirect('index.php?cl=d3user_webauthn'); $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=d3user_webauthn');
} }
} }
@ -97,13 +101,13 @@ class d3user_webauthn extends AdminDetailsController
$credential = Registry::getRequest()->getRequestEscapedParameter('credential'); $credential = Registry::getRequest()->getRequestEscapedParameter('credential');
if ( strlen((string) $credential) ) { if ( strlen((string) $credential) ) {
$webauthn = $this->d3GetWebauthnObject(); $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
$webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter( 'keyname' ) ); $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter( 'keyname' ) );
} }
} catch (WebauthnException|Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) { } catch (WebauthnException|Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) {
$this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]); $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]);
$this->d3GetLoggerObject()->debug($e->getTraceAsString()); $this->d3GetMockableLogger()->debug($e->getTraceAsString());
$this->d3GetUtilsViewObject()->addErrorToDisplay($e); $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e);
} }
} }
@ -122,9 +126,9 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function setAuthnRegister(): void public function setAuthnRegister(): void
{ {
$authn = $this->d3GetWebauthnObject(); $authn = $this->d3GetMockableOxNewObject(Webauthn::class);
$user = $this->d3GetUserObject(); $user = $this->d3GetMockableOxNewObject(User::class);
$user->load($this->getEditObjectId()); $user->load($this->getEditObjectId());
$publicKeyCredentialCreationOptions = $authn->getCreationOptions($user); $publicKeyCredentialCreationOptions = $authn->getCreationOptions($user);
@ -148,10 +152,10 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function getCredentialList($userId): array public function getCredentialList($userId): array
{ {
$oUser = $this->d3GetUserObject(); $oUser = $this->d3GetMockableOxNewObject(User::class);
$oUser->load($userId); $oUser->load($userId);
$publicKeyCredentials = $this->d3GetPublicKeyCredentialListObject(); $publicKeyCredentials = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class);
return $publicKeyCredentials->getAllFromUser($oUser)->getArray(); return $publicKeyCredentials->getAllFromUser($oUser)->getArray();
} }
@ -160,7 +164,7 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function deleteKey(): void public function deleteKey(): void
{ {
$credential = $this->d3GetPublicKeyCredentialObject(); $credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class);
$credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid')); $credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid'));
} }
} }

View File

@ -16,8 +16,8 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Controller\Admin; namespace D3\Webauthn\Application\Controller\Admin;
use D3\TestingTools\Production\IsMockable; use D3\TestingTools\Production\IsMockable;
use D3\Webauthn\Application\Controller\Traits\helpersTrait;
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnAfterLogin; use D3\Webauthn\Application\Model\WebauthnAfterLogin;
use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; 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 Doctrine\DBAL\Exception as DoctrineException;
use OxidEsales\Eshop\Application\Controller\Admin\AdminController; use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; 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\Utils;
use OxidEsales\Eshop\Core\UtilsServer;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
class d3webauthnadminlogin extends AdminController class d3webauthnadminlogin extends AdminController
{ {
use helpersTrait;
use IsMockable; use IsMockable;
protected $_sThisTemplate = 'd3webauthnadminlogin.tpl'; protected $_sThisTemplate = 'd3webauthnadminlogin.tpl';
@ -59,20 +57,28 @@ class d3webauthnadminlogin extends AdminController
*/ */
public function render(): string public function render(): string
{ {
if ($this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)) { if ($this->d3GetMockableRegistryObject(Session::class)
$this->getUtils()->redirect('index.php?cl=admin_start'); ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)
} elseif (!$this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER)) { ) {
$this->getUtils()->redirect('index.php?cl=login'); $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->generateCredentialRequest();
$this->addTplParam('navFormParams', $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); $this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class)
$this->addTplParam('currentProfile', $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE)); ->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS));
$this->d3GetSession()->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); $this->addTplParam('currentProfile', $this->d3GetMockableRegistryObject(Session::class)
$this->addTplParam('currentChLanguage', $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE)); ->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(); $afterLogin->changeLanguage();
return $this->d3CallMockableParent('render'); return $this->d3CallMockableParent('render');
@ -87,66 +93,51 @@ class d3webauthnadminlogin extends AdminController
*/ */
public function generateCredentialRequest(): void 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 { try {
$webauthn = $this->d3GetWebauthnObject(); $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
$publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId); $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('webauthn_publickey_login', $publicKeyCredentialRequestOptions);
$this->addTplParam('isAdmin', isAdmin()); $this->addTplParam('isAdmin', isAdmin());
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$this->d3GetSession()->setVariable(WebauthnConf::GLOBAL_SWITCH, true); $this->d3GetMockableRegistryObject(Session::class)
$this->d3GetUtilsViewObject()->addErrorToDisplay($e); ->setVariable(WebauthnConf::GLOBAL_SWITCH, true);
$this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e);
$this->d3GetLoggerObject()->debug($e->getTraceAsString()); $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->getUtils()->redirect('index.php?cl=login'); $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 * @return string|null
*/ */
public function d3AssertAuthn(): ?string public function d3AssertAuthn(): ?string
{ {
try { try {
$login = $this->getWebauthnLoginObject( $login = $this->d3GetMockableOxNewObject(WebauthnLogin::class,
$this->d3WebAuthnGetRequest()->getRequestEscapedParameter('credential'), $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'),
$this->d3WebAuthnGetRequest()->getRequestEscapedParameter('error') $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error')
); );
return $login->adminLogin( return $login->adminLogin(
$this->d3WebAuthnGetRequest()->getRequestEscapedParameter('profile') $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('profile')
); );
} catch (WebauthnGetException $e) { } catch (WebauthnGetException $e) {
$this->d3GetUtilsViewObject()->addErrorToDisplay($e); $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e);
return 'login'; return 'login';
} }
} }
/**
* @return Utils
*/
public function getUtils(): Utils
{
return Registry::getUtils();
}
/** /**
* @return string|null * @return string|null
*/ */
public function d3GetPreviousClass(): ?string 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 public function previousClassIsOrderStep(): bool
{ {
$sClassKey = $this->d3GetPreviousClass(); $sClassKey = $this->d3GetPreviousClass();
$resolvedClass = $this->d3GetControllerClassNameResolver()->getClassNameById($sClassKey); $resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class)
->getClassNameById($sClassKey);
$resolvedClass = $resolvedClass ?: 'start'; $resolvedClass = $resolvedClass ?: 'start';
/** @var FrontendController $oController */ /** @var FrontendController $oController */
@ -170,36 +162,4 @@ class d3webauthnadminlogin extends AdminController
{ {
return $this->previousClassIsOrderStep(); 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();
}
} }

View File

@ -21,7 +21,6 @@ use D3\Webauthn\Application\Model\WebauthnConf;
use Doctrine\DBAL\Driver\Exception; use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Exception as DoctrineException;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@ -42,10 +41,11 @@ trait checkoutGetUserTrait
$user = $this->d3CallMockableParent('getUser'); $user = $this->d3CallMockableParent('getUser');
if ($user && $user->getId()) { if ($user && $user->getId()) {
$webauthn = $this->d3GetWebauthnObject(); $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
if ($webauthn->isActive($user->getId()) if ($webauthn->isActive($user->getId())
&& !$this->d3WebauthnGetSessionObject()->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) && !$this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH)
) { ) {
return false; return false;
} }
@ -53,20 +53,4 @@ trait checkoutGetUserTrait
return $user; return $user;
} }
/**
* @return Webauthn
*/
public function d3GetWebauthnObject(): Webauthn
{
return oxNew(Webauthn::class);
}
/**
* @return Session
*/
public function d3WebauthnGetSessionObject(): Session
{
return Registry::getSession();
}
} }

View File

@ -15,23 +15,26 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Controller; namespace D3\Webauthn\Application\Controller;
use D3\TestingTools\Production\IsMockable;
use D3\Webauthn\Application\Controller\Traits\accountTrait; 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\Credential\PublicKeyCredentialList;
use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException; use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn;
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Exception as DoctrineException;
use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Application\Controller\AccountController;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\SeoEncoder; use OxidEsales\Eshop\Core\SeoEncoder;
use OxidEsales\Eshop\Core\UtilsView;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
class d3_account_webauthn extends AccountController class d3_account_webauthn extends AccountController
{ {
use accountTrait; use accountTrait;
use helpersTrait; use IsMockable;
protected $_sThisTemplate = 'd3_account_webauthn.tpl'; protected $_sThisTemplate = 'd3_account_webauthn.tpl';
@ -43,7 +46,7 @@ class d3_account_webauthn extends AccountController
$sRet = parent::render(); $sRet = parent::render();
$this->addTplParam('user', $this->getUser()); $this->addTplParam('user', $this->getUser());
$this->addTplParam('readonly', !($this->d3GetWebauthnObject()->isAvailable())); $this->addTplParam('readonly', !($this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable()));
return $sRet; return $sRet;
} }
@ -58,7 +61,7 @@ class d3_account_webauthn extends AccountController
public function getCredentialList(): PublicKeyCredentialList public function getCredentialList(): PublicKeyCredentialList
{ {
$oUser = $this->getUser(); $oUser = $this->getUser();
$credentialList = $this->d3GetPublicKeyCredentialListObject(); $credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class);
return $credentialList->getAllFromUser($oUser); return $credentialList->getAllFromUser($oUser);
} }
@ -75,9 +78,9 @@ class d3_account_webauthn extends AccountController
$this->setAuthnRegister(); $this->setAuthnRegister();
$this->setPageType('requestnew'); $this->setPageType('requestnew');
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]); $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]);
$this->d3GetLoggerObject()->debug($e->getTraceAsString()); $this->d3GetMockableLogger()->debug($e->getTraceAsString());
$this->d3GetUtilsViewObject()->addErrorToDisplay($e); $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e);
} }
} }
@ -100,7 +103,8 @@ class d3_account_webauthn extends AccountController
*/ */
public function setAuthnRegister(): void 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('webauthn_publickey_create', $publicKeyCredentialCreationOptions);
$this->addTplParam('isAdmin', isAdmin()); $this->addTplParam('isAdmin', isAdmin());
@ -126,11 +130,11 @@ class d3_account_webauthn extends AccountController
$credential = Registry::getRequest()->getRequestEscapedParameter('credential'); $credential = Registry::getRequest()->getRequestEscapedParameter('credential');
if (strlen((string) $credential)) { if (strlen((string) $credential)) {
$webauthn = $this->d3GetWebauthnObject(); $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
$webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname')); $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname'));
} }
} catch (WebauthnException $e) { } 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'); $deleteId = Registry::getRequest()->getRequestEscapedParameter('deleteoxid');
if ($deleteId) { if ($deleteId) {
$credential = $this->d3GetPublicKeyCredentialObject(); $credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class);
$credential->delete($deleteId); $credential->delete($deleteId);
} }
} }

View File

@ -16,7 +16,6 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Controller; namespace D3\Webauthn\Application\Controller;
use D3\TestingTools\Production\IsMockable; use D3\TestingTools\Production\IsMockable;
use D3\Webauthn\Application\Controller\Traits\helpersTrait;
use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; 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 Doctrine\DBAL\Exception as DoctrineException;
use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver;
use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\Utils;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@ -31,7 +32,6 @@ use Psr\Container\NotFoundExceptionInterface;
class d3webauthnlogin extends FrontendController class d3webauthnlogin extends FrontendController
{ {
use IsMockable; use IsMockable;
use helpersTrait;
protected $_sThisTemplate = 'd3webauthnlogin.tpl'; protected $_sThisTemplate = 'd3webauthnlogin.tpl';
@ -39,8 +39,8 @@ class d3webauthnlogin extends FrontendController
* @return array * @return array
*/ */
public function getNavigationParams(): array public function getNavigationParams(): array
{; {
$navparams = $this->d3GetSession()->getVariable( $navparams = $this->d3GetMockableRegistryObject(Session::class)->getVariable(
WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS
); );
@ -60,15 +60,18 @@ class d3webauthnlogin extends FrontendController
*/ */
public function render(): string public function render(): string
{ {
if ($this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) || if ($this->d3GetMockableRegistryObject(Session::class)
!$this->d3GetSession()->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER) ->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->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'); return $this->d3CallMockableParent('render');
} }
@ -82,37 +85,32 @@ class d3webauthnlogin extends FrontendController
*/ */
public function generateCredentialRequest(): void public function generateCredentialRequest(): void
{ {
$userId = $this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); $userId = $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
try { try {
/** @var Webauthn $webauthn */ /** @var Webauthn $webauthn */
$webauthn = $this->d3GetWebauthnObject(); $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
$publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId); $publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId);
$this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions); $this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions);
$this->addTplParam('isAdmin', isAdmin()); $this->addTplParam('isAdmin', isAdmin());
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$this->d3GetSession()->setVariable(WebauthnConf::GLOBAL_SWITCH, true); $this->d3GetMockableRegistryObject(Session::class)
$this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); ->setVariable(WebauthnConf::GLOBAL_SWITCH, true);
$this->d3GetLoggerObject()->debug($e->getTraceAsString()); $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString());
Registry::getUtilsView()->addErrorToDisplay($e); 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 * @return string|null
*/ */
public function d3GetPreviousClass(): ?string 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 public function previousClassIsOrderStep(): bool
{ {
$sClassKey = $this->d3GetPreviousClass(); $sClassKey = $this->d3GetPreviousClass();
$resolvedClass = $this->d3GetControllerClassNameResolver()->getClassNameById($sClassKey); $resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class)
->getClassNameById($sClassKey);
$resolvedClass = $resolvedClass ?: 'start'; $resolvedClass = $resolvedClass ?: 'start';
/** @var FrontendController $oController */ /** @var FrontendController $oController */

View File

@ -15,6 +15,7 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Model\Credential; namespace D3\Webauthn\Application\Model\Credential;
use D3\TestingTools\Production\IsMockable;
use DateTime; use DateTime;
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Exception as DoctrineException;
@ -31,6 +32,8 @@ use Webauthn\PublicKeyCredentialSource;
class PublicKeyCredential extends BaseModel class PublicKeyCredential extends BaseModel
{ {
use IsMockable;
protected $_sCoreTable = 'd3wa_usercredentials'; protected $_sCoreTable = 'd3wa_usercredentials';
public function __construct() public function __construct()
@ -126,7 +129,7 @@ class PublicKeyCredential extends BaseModel
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $keyName = null): void public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $keyName = null): void
{ {
// item exist already // item exist already
if ($this->getPublicKeyCredentialListObject() if ($this->d3GetMockableOxNewObject(PublicKeyCredentialList::class)
->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId()) ->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId())
) { ) {
return; return;
@ -147,14 +150,6 @@ class PublicKeyCredential extends BaseModel
$this->save(); $this->save();
} }
/**
* @return PublicKeyCredentialList
*/
protected function getPublicKeyCredentialListObject(): PublicKeyCredentialList
{
return oxNew(PublicKeyCredentialList::class);
}
/** /**
* @param string $publicKeyCredentialId * @param string $publicKeyCredentialId
* *
@ -178,7 +173,7 @@ class PublicKeyCredential extends BaseModel
), ),
$qb->expr()->eq( $qb->expr()->eq(
'oxshopid', '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 strlen((string) $oxid) ? $oxid : null;
} }
/**
* @return Config
*/
public function d3GetConfig(): Config
{
return Registry::getConfig();
}
} }

View File

@ -20,9 +20,8 @@ use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Exception as DoctrineException;
use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Query\QueryBuilder;
use OxidEsales\Eshop\Application\Model\User; 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\Model\ListModel;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface; use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
@ -64,7 +63,7 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo
), ),
$qb->expr()->eq( $qb->expr()->eq(
'oxshopid', '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( $qb->expr()->eq(
'oxshopid', '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( $qb->expr()->eq(
'oxshopid', '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); $this->getBaseObject()->saveCredentialSource($publicKeyCredentialSource);
} }
/**
* @return Config
*/
public function getConfigObject(): Config
{
return Registry::getConfig();
}
} }

View File

@ -18,7 +18,6 @@ namespace D3\Webauthn\Application\Model;
use D3\TestingTools\Production\IsMockable; use D3\TestingTools\Production\IsMockable;
use OxidEsales\Eshop\Application\Model\Shop; use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use Webauthn\PublicKeyCredentialRpEntity; use Webauthn\PublicKeyCredentialRpEntity;
class RelyingPartyEntity extends PublicKeyCredentialRpEntity class RelyingPartyEntity extends PublicKeyCredentialRpEntity
@ -49,7 +48,7 @@ class RelyingPartyEntity extends PublicKeyCredentialRpEntity
*/ */
public function getConfiguredShopUrl() 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(); $this->getShopUrlByHost();
} }
/**
* @return Config
*/
public function getConfig(): Config
{
return Registry::getConfig();
}
/** /**
* @return Shop * @return Shop
*/ */
public function getActiveShop(): Shop public function getActiveShop(): Shop
{ {
return Registry::getConfig()->getActiveShop(); return $this->d3GetMockableRegistryObject(Config::class)->getActiveShop();
} }
} }

View File

@ -99,11 +99,10 @@ class Webauthn
} }
/** /**
* @param string $userId
* @return string * @return string
* @throws DoctrineDriverException * @throws DoctrineDriverException
* @throws DoctrineException * @throws DoctrineException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public function getRequestOptions(string $userId): string public function getRequestOptions(string $userId): string
{ {

View File

@ -15,10 +15,14 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Model; namespace D3\Webauthn\Application\Model;
use D3\TestingTools\Production\IsMockable;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\UtilsServer;
class WebauthnAfterLogin class WebauthnAfterLogin
{ {
use IsMockable;
public function setDisplayProfile() public function setDisplayProfile()
{ {
$sProfile = Registry::getRequest()->getRequestEscapedParameter('profile') ?: $sProfile = Registry::getRequest()->getRequestEscapedParameter('profile') ?:
@ -26,7 +30,7 @@ class WebauthnAfterLogin
Registry::getSession()->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); Registry::getSession()->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE);
$myUtilsServer = Registry::getUtilsServer(); $myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class);
if (isset($sProfile)) { if (isset($sProfile)) {
$aProfiles = Registry::getSession()->getVariable("aAdminProfiles"); $aProfiles = Registry::getSession()->getVariable("aAdminProfiles");
@ -46,7 +50,7 @@ class WebauthnAfterLogin
*/ */
public function changeLanguage() public function changeLanguage()
{ {
$myUtilsServer = Registry::getUtilsServer(); $myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class);
// languages // languages
$iLang = Registry::getRequest()->getRequestEscapedParameter('chlanguage') ?: $iLang = Registry::getRequest()->getRequestEscapedParameter('chlanguage') ?:
Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE); Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE);

View File

@ -55,7 +55,7 @@ class WebauthnErrors
return $lang->translateString('D3_WEBAUTHN_ERR_NOPUBKEYSUPPORT', null, true); return $lang->translateString('D3_WEBAUTHN_ERR_NOPUBKEYSUPPORT', null, true);
} }
switch ($msg) { switch (strtoupper($msg)) {
case self::UNSECURECONNECTION: case self::UNSECURECONNECTION:
return $lang->translateString($msg); return $lang->translateString($msg);
} }

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Model; 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\WebauthnException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException; 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\Config;
use OxidEsales\Eshop\Core\Exception\CookieException; use OxidEsales\Eshop\Core\Exception\CookieException;
use OxidEsales\Eshop\Core\Exception\UserException; use OxidEsales\Eshop\Core\Exception\UserException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\Str; use OxidEsales\Eshop\Core\Str;
use OxidEsales\Eshop\Core\SystemEventHandler; use OxidEsales\Eshop\Core\SystemEventHandler;
use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsServer; use OxidEsales\Eshop\Core\UtilsServer;
use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\EshopCommunity\Application\Component\UserComponent; use OxidEsales\EshopCommunity\Application\Component\UserComponent;
class WebauthnLogin class WebauthnLogin
{ {
use helpersTrait; use IsMockable;
public $credential; public $credential;
@ -104,9 +105,9 @@ class WebauthnLogin
*/ */
public function frontendLogin(UserComponent $usrCmp, bool $setSessionCookie = false) public function frontendLogin(UserComponent $usrCmp, bool $setSessionCookie = false)
{ {
$myUtilsView = $this->d3GetUtilsViewObject(); $myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class);
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3GetUserObject(); $user = $this->d3GetMockableOxNewObject(User::class);
$userId = $this->getUserId(); $userId = $this->getUserId();
try { try {
@ -116,7 +117,7 @@ class WebauthnLogin
$this->assertAuthn(); $this->assertAuthn();
// relogin, don't extract from this try block // relogin, don't extract from this try block
$usrCmp->setUser($this->d3GetUserObject()); $usrCmp->setUser($this->d3GetMockableOxNewObject(User::class));
$this->setFrontendSession($user); $this->setFrontendSession($user);
$usrCmp->setLoginStatus(USER_LOGIN_SUCCESS); $usrCmp->setLoginStatus(USER_LOGIN_SUCCESS);
@ -134,14 +135,14 @@ class WebauthnLogin
$myUtilsView->addErrorToDisplay($oEx, false, true, '', false); $myUtilsView->addErrorToDisplay($oEx, false, true, '', false);
//return 'user'; //return 'user';
} catch (\OxidEsales\Eshop\Core\Exception\CookieException $oEx) { } catch (CookieException $oEx) {
$myUtilsView->addErrorToDisplay($oEx); $myUtilsView->addErrorToDisplay($oEx);
//return 'user'; //return 'user';
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$myUtilsView->addErrorToDisplay($e); $myUtilsView->addErrorToDisplay($e);
$this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetLoggerObject()->debug($e->getTraceAsString()); $this->d3GetMockableLogger()->debug($e->getTraceAsString());
} }
$user->logout(); $user->logout();
@ -155,9 +156,9 @@ class WebauthnLogin
*/ */
public function adminLogin(string $selectedProfile): string public function adminLogin(string $selectedProfile): string
{ {
$myUtilsView = $this->d3GetUtilsViewObject(); $myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class);
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3GetUserObject(); $user = $this->d3GetMockableOxNewObject(User::class);
$userId = $this->getUserId(); $userId = $this->getUserId();
try { try {
@ -169,10 +170,10 @@ class WebauthnLogin
$this->handleBackendCookie(); $this->handleBackendCookie();
$this->handleBackendSubshopRights($user, $session); $this->handleBackendSubshopRights($user, $session);
$oEvenHandler = $this->d3WebauthnGetEventHandler(); $oEventHandler = $this->d3GetMockableOxNewObject(SystemEventHandler::class);
$oEvenHandler->onAdminLogin(); $oEventHandler->onAdminLogin();
$afterLogin = $this->getAfterLogin(); $afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class);
$afterLogin->setDisplayProfile(); $afterLogin->setDisplayProfile();
$afterLogin->changeLanguage(); $afterLogin->changeLanguage();
@ -186,14 +187,16 @@ class WebauthnLogin
$myUtilsView->addErrorToDisplay('LOGIN_NO_COOKIE_SUPPORT'); $myUtilsView->addErrorToDisplay('LOGIN_NO_COOKIE_SUPPORT');
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$myUtilsView->addErrorToDisplay($e); $myUtilsView->addErrorToDisplay($e);
$this->d3GetLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetLoggerObject()->debug($e->getTraceAsString()); $this->d3GetMockableLogger()->debug($e->getTraceAsString());
} }
$user->logout(); $user->logout();
$oStr = Str::getStr(); $oStr = Str::getStr();
$this->d3GetConfig()->getActiveView()->addTplParam('user', $oStr->htmlspecialchars($userId)); $this->d3GetMockableRegistryObject(Config::class)->getActiveView()
$this->d3GetConfig()->getActiveView()->addTplParam('profile', $oStr->htmlspecialchars($selectedProfile)); ->addTplParam('user', $oStr->htmlspecialchars($userId));
$this->d3GetMockableRegistryObject(Config::class)->getActiveView()
->addTplParam('profile', $oStr->htmlspecialchars($selectedProfile));
return 'login'; return 'login';
} }
@ -218,7 +221,7 @@ class WebauthnLogin
public function assertAuthn(): void public function assertAuthn(): void
{ {
$credential = $this->getCredential(); $credential = $this->getCredential();
$webAuthn = $this->d3GetWebauthnObject(); $webAuthn = $this->d3GetMockableOxNewObject(Webauthn::class);
$webAuthn->assertAuthn($credential); $webAuthn->assertAuthn($credential);
} }
@ -228,7 +231,7 @@ class WebauthnLogin
*/ */
public function setAdminSession($userId): Session public function setAdminSession($userId): Session
{ {
$session = $this->d3GetSession(); $session = $this->d3GetMockableRegistryObject(Session::class);
$adminProfiles = $session->getVariable("aAdminProfiles"); $adminProfiles = $session->getVariable("aAdminProfiles");
$session->initNewSession(); $session->initNewSession();
$session->setVariable("aAdminProfiles", $adminProfiles); $session->setVariable("aAdminProfiles", $adminProfiles);
@ -242,11 +245,11 @@ class WebauthnLogin
*/ */
public function setSessionCookie(User $user) public function setSessionCookie(User $user)
{ {
if ($this->d3GetConfig()->getConfigParam('blShowRememberMe')) { if ($this->d3GetMockableRegistryObject(Config::class)->getConfigParam('blShowRememberMe')) {
$this->getUtilsServer()->setUserCookie( $this->d3GetMockableRegistryObject(UtilsServer::class)->setUserCookie(
$user->getFieldData('oxusername'), $user->getFieldData('oxusername'),
$user->getFieldData('oxpassword'), $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 public function assertUser($userId, bool $isBackend = false): User
{ {
$user = $this->d3GetUserObject(); $user = $this->d3GetMockableOxNewObject(User::class);
$user->load($userId); $user->load($userId);
if (!$user->isLoaded() || if (!$user->isLoaded() ||
($isBackend && $user->getFieldData('oxrights') === 'user') ($isBackend && $user->getFieldData('oxrights') === 'user')
@ -278,7 +281,7 @@ class WebauthnLogin
*/ */
public function handleBackendCookie(): void public function handleBackendCookie(): void
{ {
$cookie = $this->getUtilsServer()->getOxCookie(); $cookie = $this->d3GetMockableRegistryObject(UtilsServer::class)->getOxCookie();
if ($cookie === null) { if ($cookie === null) {
/** @var CookieException $exc */ /** @var CookieException $exc */
$exc = oxNew(CookieException::class, 'ERROR_MESSAGE_COOKIE_NOCOOKIE'); $exc = oxNew(CookieException::class, 'ERROR_MESSAGE_COOKIE_NOCOOKIE');
@ -298,7 +301,7 @@ class WebauthnLogin
if ($iSubshop) { if ($iSubshop) {
$session->setVariable("shp", $iSubshop); $session->setVariable("shp", $iSubshop);
$session->setVariable('currentadminshop', $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 public function regenerateSessionId(): void
{ {
$oSession = $this->d3GetSession(); $oSession = $this->d3GetMockableRegistryObject(Session::class);
if ($oSession->isSessionStarted()) { if ($oSession->isSessionStarted()) {
$oSession->regenerateSessionId(); $oSession->regenerateSessionId();
} }
@ -317,8 +320,9 @@ class WebauthnLogin
{ {
// this user is blocked, deny him // this user is blocked, deny him
if ($user->inGroup('oxidblocked')) { if ($user->inGroup('oxidblocked')) {
$sUrl = $this->d3GetConfig()->getShopHomeUrl() . 'cl=content&tpl=user_blocked.tpl'; $sUrl = $this->d3GetMockableRegistryObject(Config::class)->getShopHomeUrl() .
$this->d3GetUtilsObject()->redirect($sUrl, true, 302); 'cl=content&tpl=user_blocked.tpl';
$this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl);
} }
} }
@ -327,27 +331,11 @@ class WebauthnLogin
*/ */
public function updateBasket(): void public function updateBasket(): void
{ {
if ($oBasket = $this->d3GetSession()->getBasket()) { if ($oBasket = $this->d3GetMockableRegistryObject(Session::class)->getBasket()) {
$oBasket->onUpdate(); $oBasket->onUpdate();
} }
} }
/**
* @return SystemEventHandler
*/
public function d3WebauthnGetEventHandler(): SystemEventHandler
{
return oxNew(SystemEventHandler::class);
}
/**
* @return WebauthnAfterLogin
*/
public function getAfterLogin(): WebauthnAfterLogin
{
return oxNew(WebauthnAfterLogin::class);
}
/** /**
* @return bool * @return bool
*/ */
@ -362,24 +350,10 @@ class WebauthnLogin
public function getUserId(): string public function getUserId(): string
{ {
return $this->isAdmin() ? return $this->isAdmin() ?
$this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) : $this->d3GetMockableRegistryObject(Session::class)
$this->d3GetSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) :
} $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
/**
* @return Config
*/
public function d3GetConfig(): Config
{
return Registry::getConfig();
}
/**
* @return UtilsServer
*/
public function getUtilsServer(): UtilsServer
{
return Registry::getUtilsServer();
} }
/** /**
@ -389,7 +363,8 @@ class WebauthnLogin
*/ */
public function setFrontendSession(User $user): void public function setFrontendSession(User $user): void
{ {
$this->d3GetSession()->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $this->getCredential()); $session = $this->d3GetMockableRegistryObject(Session::class);
$this->d3GetSession()->setVariable(WebauthnConf::OXID_FRONTEND_AUTH, $user->getId()); $session->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $this->getCredential());
$session->setVariable(WebauthnConf::OXID_FRONTEND_AUTH, $user->getId());
} }
} }

View File

@ -15,11 +15,11 @@ declare(strict_types=1);
namespace D3\Webauthn\Modules\Application\Component; 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\WebauthnGetException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException; use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException;
use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\Application\Model\WebauthnLogin;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; 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\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\Utils;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
{ {
use IsMockable;
/** /**
* @return string * @return string
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
@ -44,14 +45,14 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
*/ */
public function login() public function login()
{ {
$lgn_user = $this->d3WebAuthnGetRequest()->getRequestParameter('lgn_usr'); $lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_usr');
$password = $this->d3WebAuthnGetRequest()->getRequestParameter('lgn_pwd'); $password = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_pwd');
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = oxNew(User::class); $user = oxNew(User::class);
$userId = $user->d3GetLoginUserId($lgn_user); $userId = $user->d3GetLoginUserId($lgn_user);
if ($lgn_user && $userId && !strlen(trim((string) $password))) { if ($lgn_user && $userId && !strlen(trim((string) $password))) {
$webauthn = $this->d3GetWebauthnObject(); $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
if ($webauthn->isActive($userId) if ($webauthn->isActive($userId)
&& !Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) && !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'; $sUrl = Registry::getConfig()->getShopHomeUrl() . 'cl=d3webauthnlogin';
Registry::getUtils()->redirect($sUrl, true, 302); $this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl);
} }
} }
return parent::login(); return parent::login();
} }
/**
* @return Webauthn
*/
public function d3GetWebauthnObject(): Webauthn
{
return oxNew(Webauthn::class);
}
public function d3CancelWebauthnLogin(): void public function d3CancelWebauthnLogin(): void
{ {
$this->d3WebauthnClearSessionVariables(); $this->d3WebauthnClearSessionVariables();
@ -99,10 +92,11 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
*/ */
public function d3WebauthnClearSessionVariables(): void public function d3WebauthnClearSessionVariables(): void
{ {
$this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); $session = $this->d3GetMockableRegistryObject(Session::class);
$this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS);
$this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
$this->d3WebauthnGetSession()->deleteVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT); $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 public function d3AssertAuthn(): void
{ {
try { try {
$login = $this->getWebauthnLoginObject( $login = $this->d3GetMockableOxNewObject(WebauthnLogin::class,
$this->d3WebAuthnGetRequest()->getRequestEscapedParameter('credential'), $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'),
$this->d3WebAuthnGetRequest()->getRequestEscapedParameter('error') $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()); $this->_afterLogin($this->getUser());
} catch (WebauthnGetException $e) { } catch (WebauthnGetException $e) {
Registry::getUtilsView()->addErrorToDisplay($e); Registry::getUtilsView()->addErrorToDisplay($e);
} catch (WebauthnLoginErrorException $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();
}
} }

View File

@ -22,7 +22,6 @@ use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use Doctrine\DBAL\Driver\Exception as DoctrineException; use Doctrine\DBAL\Driver\Exception as DoctrineException;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
@ -41,33 +40,36 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
*/ */
public function checklogin() public function checklogin()
{ {
$lgn_user = $this->d3WebauthnGetRequestObject()->getRequestParameter( 'user') ?: $lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter( 'user') ?:
$this->d3WebauthnGetSessionObject()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER); $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER);
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3WebauthnGetUserObject(); $user = $this->d3GetMockableOxNewObject(User::class);
$userId = $user->d3GetLoginUserId($lgn_user, 'malladmin'); $userId = $user->d3GetLoginUserId($lgn_user, 'malladmin');
if ( $this->d3CanUseWebauthn( $lgn_user, $userId)) { if ( $this->d3CanUseWebauthn( $lgn_user, $userId)) {
$this->d3WebauthnGetSessionObject()->setVariable( $this->d3GetMockableRegistryObject(Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_PROFILE, 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, WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE,
$this->d3WebauthnGetRequestObject()->getRequestEscapedParameter( 'chlanguage') $this->d3GetMockableRegistryObject(Request::class)
->getRequestEscapedParameter( 'chlanguage')
); );
if ($this->hasWebauthnButNotLoggedin($userId)) { if ($this->hasWebauthnButNotLoggedin($userId)) {
$this->d3WebauthnGetSessionObject()->setVariable( $this->d3GetMockableRegistryObject(Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS, WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS,
$this->getClassKey() != 'd3webauthnadminlogin' ? $this->getClassKey() : 'admin_start' $this->getClassKey() != 'd3webauthnadminlogin' ? $this->getClassKey() : 'admin_start'
); );
$this->d3WebauthnGetSessionObject()->setVariable( $this->d3GetMockableRegistryObject(Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER, WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER,
$userId $userId
); );
$this->d3WebauthnGetSessionObject()->setVariable( $this->d3GetMockableRegistryObject(Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER, WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER,
$lgn_user $lgn_user
); );
@ -79,45 +81,13 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
return $this->d3CallMockableParent('checklogin'); return $this->d3CallMockableParent('checklogin');
} }
/**
* @return Webauthn
*/
public function d3GetWebauthnObject(): Webauthn
{
return oxNew(Webauthn::class);
}
/** /**
* @return void * @return void
*/ */
public function d3WebauthnCancelLogin(): void public function d3WebauthnCancelLogin(): void
{ {
$oUser = $this->d3WebauthnGetUserObject(); $user = $this->d3GetMockableOxNewObject(User::class);
$oUser->logout(); $user->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();
} }
/** /**
@ -128,11 +98,12 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
*/ */
protected function d3CanUseWebauthn( $lgn_user, ?string $userId): bool protected function d3CanUseWebauthn( $lgn_user, ?string $userId): bool
{ {
$password = $this->d3WebauthnGetRequestObject()->getRequestParameter( 'pwd'); $password = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter( 'pwd');
return $lgn_user && return $lgn_user &&
$userId && $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 ) ) ); ( ! strlen( trim( (string) $password ) ) );
} }
@ -144,9 +115,10 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
*/ */
protected function hasWebauthnButNotLoggedin($userId): bool protected function hasWebauthnButNotLoggedin($userId): bool
{ {
$webauthn = $this->d3GetWebauthnObject(); $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
return $webauthn->isActive($userId) return $webauthn->isActive($userId)
&& !$this->d3WebauthnGetSessionObject()->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH); && !$this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH);
} }
} }

View File

@ -58,6 +58,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase
* @param $sClass * @param $sClass
* @param $sId * @param $sId
* @param array $aFields * @param array $aFields
* @param bool $blAdmin
* @throws Exception * @throws Exception
*/ */
public function createObject($sClass, $sId, $aFields = [], $blAdmin = false) public function createObject($sClass, $sId, $aFields = [], $blAdmin = false)
@ -112,6 +113,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase
/** /**
* @param $sId * @param $sId
* @param array $aFields * @param array $aFields
* @param bool $blAdmin
* @throws Exception * @throws Exception
*/ */
public function createUser($sId, $aFields = [], $blAdmin = false) public function createUser($sId, $aFields = [], $blAdmin = false)

View File

@ -14,6 +14,7 @@
namespace D3\Webauthn\tests\unit\Application\Controller\Admin; namespace D3\Webauthn\tests\unit\Application\Controller\Admin;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\TestingTools\Production\IsMockable;
use D3\Webauthn\Application\Controller\Admin\d3user_webauthn; use D3\Webauthn\Application\Controller\Admin\d3user_webauthn;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; 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\Application\Model\Webauthn;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
@ -30,6 +32,7 @@ use ReflectionException;
class d3user_webauthnTest extends TestCase class d3user_webauthnTest extends TestCase
{ {
use IsMockable;
use CanAccessRestricted; use CanAccessRestricted;
public function setUp(): void public function setUp(): void
@ -65,16 +68,26 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $sutMock */ /** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class) $sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([ ->onlyMethods([
'd3GetWebauthnObject',
'd3CallMockableParent', 'd3CallMockableParent',
'getEditObjectId', 'getEditObjectId',
'd3GetUserObject' 'd3GetMockableOxNewObject'
]) ])
->getMock(); ->getMock();
$sutMock->method('d3GetWebauthnObject')->willReturn($webauthnMock);
$sutMock->method('d3CallMockableParent')->willReturn(true); $sutMock->method('d3CallMockableParent')->willReturn(true);
$sutMock->method('getEditObjectId')->willReturn('editObjectId'); $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( $this->setValue(
$sutMock, $sutMock,
@ -130,14 +143,24 @@ class d3user_webauthnTest extends TestCase
->onlyMethods([ ->onlyMethods([
'setPageType', 'setPageType',
'setAuthnRegister', 'setAuthnRegister',
'd3GetLoggerObject', 'd3GetMockableLogger',
'd3GetUtilsObject' 'd3GetMockableRegistryObject'
]) ])
->getMock(); ->getMock();
$sutMock->expects($this->atLeastOnce())->method('setPageType'); $sutMock->expects($this->atLeastOnce())->method('setPageType');
$sutMock->expects($this->atLeastOnce())->method('setAuthnRegister'); $sutMock->expects($this->atLeastOnce())->method('setAuthnRegister');
$sutMock->expects($this->never())->method('d3GetLoggerObject')->willReturn($loggerMock); $sutMock->expects($this->never())->method('d3GetMockableLogger')->willReturn($loggerMock);
$sutMock->expects($this->never())->method('d3GetUtilsObject')->willReturn($utilsMock); $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( $this->callMethod(
$sutMock, $sutMock,
@ -169,14 +192,24 @@ class d3user_webauthnTest extends TestCase
->onlyMethods([ ->onlyMethods([
'setPageType', 'setPageType',
'setAuthnRegister', 'setAuthnRegister',
'd3GetLoggerObject', 'd3GetMockableLogger',
'd3GetUtilsObject' 'd3GetMockableRegistryObject'
]) ])
->getMock(); ->getMock();
$sutMock->expects($this->atLeastOnce())->method('setPageType'); $sutMock->expects($this->atLeastOnce())->method('setPageType');
$sutMock->expects($this->atLeastOnce())->method('setAuthnRegister')->willThrowException(oxNew(WebauthnException::class)); $sutMock->expects($this->atLeastOnce())->method('setAuthnRegister')->willThrowException(oxNew(WebauthnException::class));
$sutMock->expects($this->atLeastOnce())->method('d3GetLoggerObject')->willReturn($loggerMock); $sutMock->expects($this->atLeastOnce())->method('d3GetMockableLogger')->willReturn($loggerMock);
$sutMock->expects($this->atLeastOnce())->method('d3GetUtilsObject')->willReturn($utilsMock); $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( $this->callMethod(
$sutMock, $sutMock,
@ -207,10 +240,20 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetUtilsViewObject', 'd3GetLoggerObject']) ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback(
$oControllerMock->expects($this->atLeastOnce())->method('d3GetLoggerObject')->willReturn($loggerMock); 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( $this->callMethod(
$oControllerMock, $oControllerMock,
@ -243,10 +286,30 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
$oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); 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( $this->callMethod(
$oControllerMock, $oControllerMock,
@ -285,11 +348,31 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject', 'd3GetLoggerObject']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3GetMockableLogger'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
$oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); function () use ($webauthnMock) {
$oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); $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( $this->callMethod(
$oControllerMock, $oControllerMock,
@ -339,9 +422,19 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetWebauthnObject', 'addTplParam', 'getUser']) ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser'])
->getMock(); ->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)) $oControllerMock->expects($throwExc ? $this->never() : $this->atLeast(3))
->method('addTplParam'); ->method('addTplParam');
$oControllerMock->method('getUser')->willReturn(oxNew(User::class)); $oControllerMock->method('getUser')->willReturn(oxNew(User::class));
@ -391,10 +484,21 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetUserObject', 'd3GetPublicKeyCredentialListObject']) ->onlyMethods(['d3GetMockableOxNewObject'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetUserObject')->willReturn($oUser); $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
$oControllerMock->method('d3GetPublicKeyCredentialListObject')->willReturn($publicKeyCredentialListMock); 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->assertIsArray(
$this->callMethod( $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 * @test
* @throws ReflectionException * @throws ReflectionException
@ -445,9 +529,19 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetPublicKeyCredentialObject']) ->onlyMethods(['d3GetMockableOxNewObject'])
->getMock(); ->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'); $this->callMethod($oControllerMock, 'deleteKey');
} }
@ -461,23 +555,4 @@ class d3user_webauthnTest extends TestCase
'has delete id' => ['deleteId', $this->once()] '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'
)
);
}
} }

View File

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Application\Controller\Admin; namespace D3\Webauthn\tests\unit\Application\Controller\Admin;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\TestingTools\Production\IsMockable;
use D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin; use D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin;
use D3\Webauthn\Application\Controller\d3webauthnlogin; use D3\Webauthn\Application\Controller\d3webauthnlogin;
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; 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\WebauthnConf;
use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\Application\Model\WebauthnLogin;
use D3\Webauthn\tests\unit\Application\Controller\d3webauthnloginTest; use D3\Webauthn\tests\unit\Application\Controller\d3webauthnloginTest;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\SystemEventHandler;
use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsServer;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class d3webauthnadminloginTest extends d3webauthnloginTest class d3webauthnadminloginTest extends d3webauthnloginTest
{ {
use IsMockable;
use CanAccessRestricted; use CanAccessRestricted;
protected $sutClassName = d3webauthnadminlogin::class; protected $sutClassName = d3webauthnadminlogin::class;
@ -95,18 +96,39 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetSession', 'getUtils', 'd3CallMockableParent', 'd3WebauthnGetAfterLogin', ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent', 'd3GetMockableOxNewObject',
'generateCredentialRequest', 'addTplParam']) 'generateCredentialRequest', 'addTplParam'])
->getMock(); ->getMock();
$sut->method('d3GetSession')->willReturn($sessionMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('getUtils')->willReturn($utilsMock); 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->method('d3CallMockableParent')->willReturn('myTemplate.tpl');
// "any" because redirect doesn't stop execution // "any" because redirect doesn't stop execution
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('generateCredentialRequest'); ->method('generateCredentialRequest');
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('addTplParam')->willReturn(true); ->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( $this->assertSame(
'myTemplate.tpl', 'myTemplate.tpl',
@ -151,17 +173,6 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
parent::generateCredentialRequestFailed($redirectClass, $userVarName); parent::generateCredentialRequestFailed($redirectClass, $userVarName);
} }
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::getUtils
*/
public function getUtilsReturnsRightInstance()
{
parent::getUtilsReturnsRightInstance();
}
/** /**
* @test * @test
* @return void * @return void
@ -226,10 +237,30 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
/** @var d3webauthnadminlogin|MockObject $sut */ /** @var d3webauthnadminlogin|MockObject $sut */
$sut = $this->getMockBuilder(d3webauthnadminlogin::class) $sut = $this->getMockBuilder(d3webauthnadminlogin::class)
->onlyMethods(['getWebauthnLoginObject', 'd3WebAuthnGetRequest']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('getWebauthnLoginObject')->willReturn($loginMock); $sut->method('d3GetMockableOxNewObject')->willReturnCallback(
$sut->method('d3WebAuthnGetRequest')->willReturn($requestMock); 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( $this->assertSame(
'expected', 'expected',
@ -262,11 +293,22 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
/** @var d3webauthnadminlogin|MockObject $sut */ /** @var d3webauthnadminlogin|MockObject $sut */
$sut = $this->getMockBuilder(d3webauthnadminlogin::class) $sut = $this->getMockBuilder(d3webauthnadminlogin::class)
->onlyMethods(['getWebauthnLoginObject', 'd3WebAuthnGetRequest', 'd3GetUtilsViewObject']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('getWebauthnLoginObject')->willThrowException(oxNew(WebauthnGetException::class)); $sut->method('d3GetMockableOxNewObject')->willThrowException(oxNew(WebauthnGetException::class));
$sut->method('d3WebAuthnGetRequest')->willReturn($requestMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); 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( $this->assertSame(
'login', 'login',
@ -289,100 +331,4 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
'assertion succ shop1' => ['admin_start', false, 'cookie', 1], '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'
)
);
}
} }

View File

@ -22,6 +22,7 @@ use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\Webauthn;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
@ -79,10 +80,20 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['getUser', 'd3GetWebauthnObject']) ->onlyMethods(['getUser', 'd3GetMockableOxNewObject'])
->getMock(); ->getMock();
$oControllerMock->method('getUser')->willReturn($oUser); $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; $this->_oController = $oControllerMock;
@ -111,10 +122,20 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['getUser', 'd3GetWebauthnObject']) ->onlyMethods(['getUser', 'd3GetMockableOxNewObject'])
->getMock(); ->getMock();
$oControllerMock->method('getUser')->willReturn($oUser); $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; $this->_oController = $oControllerMock;
@ -149,10 +170,20 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['getUser', 'd3GetPublicKeyCredentialListObject']) ->onlyMethods(['getUser', 'd3GetMockableOxNewObject'])
->getMock(); ->getMock();
$oControllerMock->method('getUser')->willReturn($oUser); $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; $this->_oController = $oControllerMock;
@ -188,12 +219,12 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetLoggerObject']) ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetMockableLogger'])
->getMock(); ->getMock();
$oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister'); $oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister');
$oControllerMock->expects($this->atLeastOnce())->method('setPageType'); $oControllerMock->expects($this->atLeastOnce())->method('setPageType');
$oControllerMock->method('getUser')->willReturn($oUser); $oControllerMock->method('getUser')->willReturn($oUser);
$oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->_oController = $oControllerMock; $this->_oController = $oControllerMock;
@ -226,13 +257,13 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetLoggerObject']) ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetMockableLogger'])
->getMock(); ->getMock();
$oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister') $oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister')
->willThrowException(oxNew(WebauthnException::class)); ->willThrowException(oxNew(WebauthnException::class));
$oControllerMock->expects($this->never())->method('setPageType'); $oControllerMock->expects($this->never())->method('setPageType');
$oControllerMock->method('getUser')->willReturn($oUser); $oControllerMock->method('getUser')->willReturn($oUser);
$oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->_oController = $oControllerMock; $this->_oController = $oControllerMock;
@ -264,9 +295,19 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['d3GetWebauthnObject', 'addTplParam', 'getUser']) ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser'])
->getMock(); ->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)) $oControllerMock->expects($throwExc ? $this->never() : $this->atLeast(3))
->method('addTplParam'); ->method('addTplParam');
$oControllerMock->method('getUser')->willReturn(oxNew(User::class)); $oControllerMock->method('getUser')->willReturn(oxNew(User::class));
@ -343,10 +384,20 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['d3GetUtilsViewObject', 'd3GetLoggerObject']) ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback(
$oControllerMock->method('d3GetLoggerObject')->willReturn($loggerMock); 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; $this->_oController = $oControllerMock;
@ -381,10 +432,30 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
$oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); 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; $this->_oController = $oControllerMock;
@ -420,10 +491,30 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['d3GetWebauthnObject', 'd3GetUtilsViewObject']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetWebauthnObject')->willReturn($webauthnMock); $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
$oControllerMock->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); 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; $this->_oController = $oControllerMock;
@ -453,9 +544,19 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['d3GetPublicKeyCredentialObject']) ->onlyMethods(['d3GetMockableOxNewObject'])
->getMock(); ->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; $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'
)
);
}
} }

View File

@ -20,12 +20,11 @@ use D3\Webauthn\Application\Controller\d3webauthnlogin;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf; 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\Session;
use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\Utils;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
@ -53,9 +52,19 @@ class d3webauthnloginTest extends UnitTestCase
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetSession', 'd3CallMockableParent']) ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent'])
->getMock(); ->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']); $sut->method('d3CallMockableParent')->willReturn(['defKey1' => 'devValues1']);
$this->assertSame( $this->assertSame(
@ -76,9 +85,10 @@ class d3webauthnloginTest extends UnitTestCase
* @param $auth * @param $auth
* @param $userFromLogin * @param $userFromLogin
* @param $startRedirect * @param $startRedirect
* @param $redirectController
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::render * @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::render
* @dataProvider canRenderDataProvider * @dataProvider canRenderDataProvider
*/ */
public function canRender($auth, $userFromLogin, $startRedirect, $redirectController) public function canRender($auth, $userFromLogin, $startRedirect, $redirectController)
@ -101,11 +111,22 @@ class d3webauthnloginTest extends UnitTestCase
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetSession', 'getUtils', 'd3CallMockableParent', ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent',
'generateCredentialRequest', 'addTplParam']) 'generateCredentialRequest', 'addTplParam'])
->getMock(); ->getMock();
$sut->method('d3GetSession')->willReturn($sessionMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('getUtils')->willReturn($utilsMock); 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->method('d3CallMockableParent')->willReturn('myTemplate.tpl');
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('generateCredentialRequest'); ->method('generateCredentialRequest');
@ -165,13 +186,33 @@ class d3webauthnloginTest extends UnitTestCase
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetSession', 'd3GetWebauthnObject', 'addTplParam', 'd3GetLoggerObject']) ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableOxNewObject', 'addTplParam', 'd3GetMockableLogger'])
->getMock(); ->getMock();
$sut->method('d3GetSession')->willReturn($sessionMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('d3GetWebauthnObject')->willReturn($webAuthnMock); 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)) $sut->expects($this->atLeast(2))
->method('addTplParam')->willReturn(true); ->method('addTplParam')->willReturn(true);
$sut->method('d3GetLoggerObject')->willReturn($loggerMock); $sut->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -220,15 +261,36 @@ class d3webauthnloginTest extends UnitTestCase
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetSession', 'd3GetWebauthnObject', 'addTplParam', ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam',
'd3GetLoggerObject', 'getUtils']) 'd3GetMockableLogger', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('d3GetSession')->willReturn($sessionMock); $sut->method('d3GetMockableOxNewObject')->willReturnCallback(
$sut->method('d3GetWebauthnObject')->willReturn($webAuthnMock); 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()) $sut->expects($this->never())
->method('addTplParam')->willReturn(true); ->method('addTplParam')->willReturn(true);
$sut->expects($this->atLeast(2))->method('d3GetLoggerObject')->willReturn($loggerMock); $sut->expects($this->atLeast(2))->method('d3GetMockableLogger')->willReturn($loggerMock);
$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]);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $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 * @test
* @return void * @return void
@ -275,9 +318,19 @@ class d3webauthnloginTest extends UnitTestCase
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetSession']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->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( $this->assertSame(
$currClassFixture, $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'
)
);
}
} }

View File

@ -21,11 +21,11 @@ use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
use D3\Webauthn\Application\Model\UserEntity; use D3\Webauthn\Application\Model\UserEntity;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
use Webauthn\PublicKeyCredentialSource; use Webauthn\PublicKeyCredentialSource;
use Webauthn\PublicKeyCredentialUserEntity;
class PublicKeyCredentialListTest extends UnitTestCase class PublicKeyCredentialListTest extends UnitTestCase
{ {
@ -77,9 +77,19 @@ class PublicKeyCredentialListTest extends UnitTestCase
/** @var PublicKeyCredentialList|MockObject $sut */ /** @var PublicKeyCredentialList|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredentialList::class) $sut = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['getConfigObject']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->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) { if ($doCreate) {
$pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class)
@ -145,9 +155,19 @@ class PublicKeyCredentialListTest extends UnitTestCase
/** @var PublicKeyCredentialList|MockObject $sut */ /** @var PublicKeyCredentialList|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredentialList::class) $sut = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['getConfigObject']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->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) { if ($doCreate) {
$pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class)
@ -214,9 +234,19 @@ class PublicKeyCredentialListTest extends UnitTestCase
/** @var PublicKeyCredentialList|MockObject $sut */ /** @var PublicKeyCredentialList|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredentialList::class) $sut = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['getConfigObject']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->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 */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
@ -304,28 +334,4 @@ class PublicKeyCredentialListTest extends UnitTestCase
[$pkcsMock] [$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'
)
);
}
} }

View File

@ -19,6 +19,7 @@ use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
@ -75,7 +76,8 @@ class PublicKeyCredentialTest extends UnitTestCase
/** /**
* @param $fieldName * @param $fieldName
* @param $sutMethod * @param $sutMethod
* @param $value * @param $setValue
* @param $getValue
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
*/ */
@ -232,9 +234,19 @@ class PublicKeyCredentialTest extends UnitTestCase
/** @var PublicKeyCredential|MockObject $sut */ /** @var PublicKeyCredential|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredential::class) $sut = $this->getMockBuilder(PublicKeyCredential::class)
->onlyMethods(['getPublicKeyCredentialListObject', 'exists', 'getIdByCredentialId', 'load', 'save']) ->onlyMethods(['d3GetMockableOxNewObject', 'exists', 'getIdByCredentialId', 'load', 'save'])
->getMock(); ->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->method('exists')->willReturn($credIdExist);
$sut->expects($this->exactly((int) $doSave))->method('getIdByCredentialId'); $sut->expects($this->exactly((int) $doSave))->method('getIdByCredentialId');
$sut->expects($this->exactly((int) ($doSave && $credIdExist)))->method('load'); $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 * @test
* @return void * @return void
@ -309,9 +299,19 @@ class PublicKeyCredentialTest extends UnitTestCase
/** @var PublicKeyCredential|MockObject $sut */ /** @var PublicKeyCredential|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredential::class) $sut = $this->getMockBuilder(PublicKeyCredential::class)
->onlyMethods(['d3GetConfig', 'allowDerivedDelete']) ->onlyMethods(['d3GetMockableRegistryObject', 'allowDerivedDelete'])
->getMock(); ->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); $sut->method('allowDerivedDelete')->willReturn(true);
if ($doCreate) { if ($doCreate) {
@ -347,24 +347,4 @@ class PublicKeyCredentialTest extends UnitTestCase
'item not exists' => [false, null] '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'
)
);
}
} }

View File

@ -19,7 +19,7 @@ use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\RelyingPartyEntity; use D3\Webauthn\Application\Model\RelyingPartyEntity;
use OxidEsales\Eshop\Application\Model\Shop; use OxidEsales\Eshop\Application\Model\Shop;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\Eshop\Core\Registry;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use ReflectionException; use ReflectionException;
@ -120,9 +120,19 @@ class RelyingPartyEntityTest extends TestCase
/** @var RelyingPartyEntity|MockObject $sut */ /** @var RelyingPartyEntity|MockObject $sut */
$sut = $this->getMockBuilder(RelyingPartyEntity::class) $sut = $this->getMockBuilder(RelyingPartyEntity::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['getConfig']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->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( $this->assertSame(
$fixture, $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 * @test
* @return void * @return void

View File

@ -65,7 +65,7 @@ class UserEntityTest extends UnitTestCase
if (!$runParent) { if (!$runParent) {
$this->expectException(WebauthnException::class); $this->expectException(WebauthnException::class);
}; }
$this->callMethod( $this->callMethod(
$sut, $sut,

View File

@ -20,7 +20,6 @@ use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException; use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException;
use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnAfterLogin;
use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\Application\Model\WebauthnLogin;
use OxidEsales\Eshop\Application\Component\UserComponent; 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\Config;
use OxidEsales\Eshop\Core\Exception\CookieException; use OxidEsales\Eshop\Core\Exception\CookieException;
use OxidEsales\Eshop\Core\Exception\UserException; use OxidEsales\Eshop\Core\Exception\UserException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\SystemEventHandler; use OxidEsales\Eshop\Core\SystemEventHandler;
use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\Utils;
@ -92,7 +92,7 @@ class WebauthnLoginTest extends UnitTestCase
$this->assertSame( $this->assertSame(
$credFixture, $credFixture,
$cred = $this->callMethod( $this->callMethod(
$sut, $sut,
'getCredential' 'getCredential'
) )
@ -216,7 +216,7 @@ class WebauthnLoginTest extends UnitTestCase
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie', 'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie',
'd3WebauthnGetEventHandler', 'getAfterLogin', 'getCredential', 'regenerateSessionId']) 'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->expects($this->exactly((int) $setCookie))->method('setSessionCookie'); $sut->expects($this->exactly((int) $setCookie))->method('setSessionCookie');
@ -279,15 +279,35 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'd3GetUtilsViewObject', ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'd3GetMockableRegistryObject',
'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie', 'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie',
'd3GetLoggerObject', 'd3GetUserObject', 'getCredential', 'regenerateSessionId']) 'd3GetMockableLogger', 'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass));
$sut->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('d3GetUserObject')->willReturn($userMock); function () use ($utilsViewMock) {
$sut->method('d3GetLoggerObject')->willReturn($loggerMock); $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); $this->expectException(WebauthnLoginErrorException::class);
@ -321,13 +341,29 @@ class WebauthnLoginTest extends UnitTestCase
*/ */
public function adminLoginSuccess() public function adminLoginSuccess()
{ {
/** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights',
'd3WebauthnGetEventHandler', 'getAfterLogin']) 'd3GetMockableOxNewObject'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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( $this->assertSame(
'admin_start', 'admin_start',
@ -348,6 +384,12 @@ class WebauthnLoginTest extends UnitTestCase
*/ */
public function adminLoginException($exceptionClass, $writeLog) 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 */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
@ -369,14 +411,36 @@ class WebauthnLoginTest extends UnitTestCase
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights',
'd3WebauthnGetEventHandler', 'getAfterLogin', 'd3GetUtilsViewObject', 'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject',
'd3GetUserObject', 'd3GetLoggerObject']) 'd3GetMockableLogger'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass));
$sut->method('d3GetUtilsViewObject')->willReturn($utilsViewMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('d3GetUserObject')->willReturn($userMock); function () use ($utilsViewMock) {
$sut->method('d3GetLoggerObject')->willReturn($loggerMock); $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( $this->assertSame(
'login', 'login',
@ -458,7 +522,7 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getCredential', 'd3GetWebauthnObject']) ->onlyMethods(['getCredential', 'd3GetMockableOxNewObject'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
if ($throwException) { if ($throwException) {
@ -466,7 +530,17 @@ class WebauthnLoginTest extends UnitTestCase
} else { } else {
$sut->method('getCredential')->willReturn('credential'); $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) { if ($throwException) {
$this->expectException(WebauthnGetException::class); $this->expectException(WebauthnGetException::class);
@ -515,10 +589,20 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetSession']) ->onlyMethods(['d3GetMockableRegistryObject'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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( $this->assertSame(
$sessionMock, $sessionMock,
@ -561,10 +645,21 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetConfig', 'getUtilsServer']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('d3GetConfig')->willReturn($configMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('getUtilsServer')->willReturn($utilsServerMock); 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( $this->callMethod(
$sut, $sut,
@ -606,10 +701,20 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetUserObject']) ->onlyMethods(['d3GetMockableOxNewObject'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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) { if ($throwsException) {
$this->expectException(UserException::class); $this->expectException(UserException::class);
@ -661,10 +766,20 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getUtilsServer']) ->onlyMethods(['d3GetMockableRegistryObject'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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) { if ($throwException) {
$this->expectException(CookieException::class); $this->expectException(CookieException::class);
@ -720,10 +835,20 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetConfig']) ->onlyMethods(['d3GetMockableRegistryObject'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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( $this->callMethod(
$sut, $sut,
@ -764,9 +889,19 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetSession']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->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( $this->callMethod(
$sut, $sut,
@ -815,11 +950,22 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetConfig', 'd3GetUtilsObject']) ->onlyMethods(['d3GetMockableRegistryObject'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->method('d3GetConfig')->willReturn($configMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('d3GetUtilsObject')->willReturn($utilsMock); 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( $this->callMethod(
$sut, $sut,
@ -864,9 +1010,19 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetSession']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->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( $this->callMethod(
$sut, $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 * @test
* @return void * @return void
@ -973,9 +1089,19 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetSession', 'isAdmin']) ->onlyMethods(['d3GetMockableRegistryObject', 'isAdmin'])
->getMock(); ->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); $sut->method('isAdmin')->willReturn($isAdmin);
$this->assertSame( $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 * @test
* @return void * @return void
@ -1070,10 +1156,20 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['getCredential', 'd3GetSession']) ->onlyMethods(['getCredential', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('getCredential')->willReturn('credentialFixture'); $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( $this->callMethod(
$sut, $sut,

View File

@ -20,6 +20,7 @@ use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnConf;
use OxidEsales\Eshop\Application\Controller\Admin\LoginController; use OxidEsales\Eshop\Application\Controller\Admin\LoginController;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
@ -30,24 +31,6 @@ class LoginControllerWebauthnTest extends UnitTestCase
{ {
use CanAccessRestricted; 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 * @test
* @throws ReflectionException * @throws ReflectionException
@ -63,9 +46,19 @@ class LoginControllerWebauthnTest extends UnitTestCase
/** @var LoginController|MockObject $sut */ /** @var LoginController|MockObject $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = $this->getMockBuilder(LoginController::class)
->onlyMethods(['d3WebauthnGetUserObject']) ->onlyMethods(['d3GetMockableOxNewObject'])
->getMock(); ->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( $this->callMethod(
$sut, $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 * @test
* @param $username * @param $username
@ -159,10 +95,21 @@ class LoginControllerWebauthnTest extends UnitTestCase
/** @var LoginController|MockObject $sut */ /** @var LoginController|MockObject $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = $this->getMockBuilder(LoginController::class)
->onlyMethods(['d3WebauthnGetRequestObject', 'd3WebauthnGetSessionObject']) ->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('d3WebauthnGetRequestObject')->willReturn($requestMock); $sut->method('d3GetMockableRegistryObject')->willReturnCallback(
$sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); 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( $this->assertSame(
$expected, $expected,
@ -215,10 +162,30 @@ class LoginControllerWebauthnTest extends UnitTestCase
/** @var LoginController|MockObject $sut */ /** @var LoginController|MockObject $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = $this->getMockBuilder(LoginController::class)
->onlyMethods(['d3GetWebauthnObject', 'd3WebauthnGetSessionObject']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('d3GetWebauthnObject')->willReturn($webauthnMock); $sut->method('d3GetMockableOxNewObject')->willReturnCallback(
$sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); 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( $this->assertSame(
$expected, $expected,
@ -281,15 +248,36 @@ class LoginControllerWebauthnTest extends UnitTestCase
/** @var LoginController|MockObject $sut */ /** @var LoginController|MockObject $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = $this->getMockBuilder(LoginController::class)
->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableParent', 'hasWebauthnButNotLoggedin', ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableParent', 'hasWebauthnButNotLoggedin',
'd3WebauthnGetSessionObject', 'd3WebauthnGetUserObject', 'd3WebauthnGetRequestObject' 'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject'
]) ])
->getMock(); ->getMock();
$sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn); $sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn);
$sut->method('d3CallMockableParent')->willReturn('parentReturn'); $sut->method('d3CallMockableParent')->willReturn('parentReturn');
$sut->method('hasWebauthnButNotLoggedin')->willReturn($loggedin); $sut->method('hasWebauthnButNotLoggedin')->willReturn($loggedin);
$sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); $sut->method('d3GetMockableOxNewObject')->willReturnCallback(
$sut->method('d3WebauthnGetUserObject')->willReturn($userMock); function () use ($userMock) {
$sut->method('d3WebauthnGetRequestObject')->willReturn($requestMock); $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( $this->assertSame(
$expected, $expected,

View File

@ -22,6 +22,7 @@ use OxidEsales\Eshop\Application\Controller\OrderController;
use OxidEsales\Eshop\Application\Controller\PaymentController; use OxidEsales\Eshop\Application\Controller\PaymentController;
use OxidEsales\Eshop\Application\Controller\UserController; use OxidEsales\Eshop\Application\Controller\UserController;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
@ -72,10 +73,30 @@ trait CheckoutTestTrait
/** @var PaymentController|OrderController|UserController|MockObject $sut */ /** @var PaymentController|OrderController|UserController|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClass) $sut = $this->getMockBuilder($this->sutClass)
->onlyMethods(['d3GetWebauthnObject', 'd3WebauthnGetSessionObject', 'd3CallMockableParent']) ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3CallMockableParent'])
->getMock(); ->getMock();
$sut->method('d3GetWebauthnObject')->willReturn($webauthnMock); $sut->method('d3GetMockableOxNewObject')->willReturnCallback(
$sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); 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); $sut->method('d3CallMockableParent')->willReturn($userMock);
$return = $this->callMethod( $return = $this->callMethod(
@ -103,50 +124,4 @@ trait CheckoutTestTrait
'no webauthn auth' => [true, 'userIdFixture', true, null, false], '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'
)
);
}
} }