This commit is contained in:
Daniel Seifert 2023-01-21 13:50:18 +01:00
parent 942a20cdf6
commit 4ab7ad59e8
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
59 changed files with 1344 additions and 1799 deletions

View File

@ -37,7 +37,8 @@
"nyholm/psr7": "^1.5.1", "nyholm/psr7": "^1.5.1",
"nyholm/psr7-server": "^1.0.2", "nyholm/psr7-server": "^1.0.2",
"ext-json": "*", "ext-json": "*",
"d3/testingtools": "^1.1" "d3/testingtools": "^1.1",
"d3/oxid-dic-handler": "^1.0"
}, },
"require-dev": { "require-dev": {
"friendsofphp/php-cs-fixer": "^2.19", "friendsofphp/php-cs-fixer": "^2.19",

View File

@ -33,6 +33,7 @@ use OxidEsales\Eshop\Core\Utils;
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;
use Psr\Log\LoggerInterface;
use Throwable; use Throwable;
class d3user_webauthn extends AdminDetailsController class d3user_webauthn extends AdminDetailsController
@ -48,7 +49,9 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function render(): string public function render(): string
{ {
$this->addTplParam('readonly', !$this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable()); /** @var Webauthn $webauthn */
$webauthn = d3GetOxidDIC()->get(Webauthn::class);
$this->addTplParam('readonly', !$webauthn->isAvailable());
$this->d3CallMockableFunction([AdminDetailsController::class, 'render']); $this->d3CallMockableFunction([AdminDetailsController::class, 'render']);
@ -56,7 +59,7 @@ class d3user_webauthn extends AdminDetailsController
if ($soxId != "-1") { if ($soxId != "-1") {
/** @var d3_User_Webauthn $oUser */ /** @var d3_User_Webauthn $oUser */
$oUser = $this->d3GetMockableOxNewObject(User::class); $oUser = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
if ($oUser->load($soxId)) { if ($oUser->load($soxId)) {
$this->addTplParam("oxid", $oUser->getId()); $this->addTplParam("oxid", $oUser->getId());
} else { } else {
@ -81,10 +84,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->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e->getMessage());
$this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=d3user_webauthn'); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=d3user_webauthn');
} }
} }
@ -105,17 +108,17 @@ 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->d3GetMockableOxNewObject(Webauthn::class); $webauthn = d3GetOxidDIC()->get(Webauthn::class);
$webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname')); $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname'));
} }
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
} catch (Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) { } catch (Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) {
$this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e->getMessage());
} }
} }
@ -134,9 +137,9 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function setAuthnRegister(): void public function setAuthnRegister(): void
{ {
$authn = $this->d3GetMockableOxNewObject(Webauthn::class); $authn = d3GetOxidDIC()->get(Webauthn::class);
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$user->load($this->getEditObjectId()); $user->load($this->getEditObjectId());
$publicKeyCredentialCreationOptions = $authn->getCreationOptions($user); $publicKeyCredentialCreationOptions = $authn->getCreationOptions($user);
@ -160,10 +163,10 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function getCredentialList($userId): array public function getCredentialList($userId): array
{ {
$oUser = $this->d3GetMockableOxNewObject(User::class); $oUser = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$oUser->load($userId); $oUser->load($userId);
$publicKeyCredentials = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); $publicKeyCredentials = d3GetOxidDIC()->get(PublicKeyCredentialList::class);
return $publicKeyCredentials->getAllFromUser($oUser)->getArray(); return $publicKeyCredentials->getAllFromUser($oUser)->getArray();
} }
@ -172,7 +175,7 @@ class d3user_webauthn extends AdminDetailsController
*/ */
public function deleteKey(): void public function deleteKey(): void
{ {
$credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); $credential = d3GetOxidDIC()->get(PublicKeyCredential::class);
$credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid')); $credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid'));
} }
} }

View File

@ -33,6 +33,7 @@ use OxidEsales\Eshop\Core\Utils;
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;
use Psr\Log\LoggerInterface;
class d3webauthnadminlogin extends AdminController class d3webauthnadminlogin extends AdminController
{ {
@ -57,28 +58,28 @@ class d3webauthnadminlogin extends AdminController
*/ */
public function render(): string public function render(): string
{ {
if ($this->d3GetMockableRegistryObject(Session::class) if (d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)
) { ) {
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=admin_start'); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=admin_start');
} elseif (!$this->d3GetMockableRegistryObject(Session::class) } elseif (!d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER)
) { ) {
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login'); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=login');
} }
$this->generateCredentialRequest(); $this->generateCredentialRequest();
$this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class) $this->addTplParam('navFormParams', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); ->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS));
$this->addTplParam('currentProfile', $this->d3GetMockableRegistryObject(Session::class) $this->addTplParam('currentProfile', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE)); ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE));
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); ->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE);
$this->addTplParam('currentChLanguage', $this->d3GetMockableRegistryObject(Session::class) $this->addTplParam('currentChLanguage', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE)); ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE));
$afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class); $afterLogin = d3GetOxidDIC()->get(WebauthnAfterLogin::class);
$afterLogin->changeLanguage(); $afterLogin->changeLanguage();
return $this->d3CallMockableFunction([AdminController::class, 'render']); return $this->d3CallMockableFunction([AdminController::class, 'render']);
@ -93,22 +94,22 @@ class d3webauthnadminlogin extends AdminController
*/ */
public function generateCredentialRequest(): void public function generateCredentialRequest(): void
{ {
$userId = $this->d3GetMockableRegistryObject(Session::class) $userId = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER); ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER);
try { try {
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn = d3GetOxidDIC()->get(Webauthn::class);
$publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId); $publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId);
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->setVariable(WebauthnConf::WEBAUTHN_ADMIN_LOGIN_OBJECT, $publicKeyCredentialRequestOptions); ->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->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->setVariable(WebauthnConf::GLOBAL_SWITCH, true); ->setVariable(WebauthnConf::GLOBAL_SWITCH, true);
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
$this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login'); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=login');
} }
} }
@ -118,16 +119,12 @@ class d3webauthnadminlogin extends AdminController
public function d3AssertAuthn(): ?string public function d3AssertAuthn(): ?string
{ {
try { try {
$login = $this->d3GetMockableOxNewObject( $login = $this->getWebAuthnLogin();
WebauthnLogin::class,
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'),
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error')
);
return $login->adminLogin( return $login->adminLogin(
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('profile') d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('profile')
); );
} catch (WebauthnGetException $e) { } catch (WebauthnGetException $e) {
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
return 'login'; return 'login';
} }
} }
@ -137,7 +134,7 @@ class d3webauthnadminlogin extends AdminController
*/ */
public function d3GetPreviousClass(): ?string public function d3GetPreviousClass(): ?string
{ {
return $this->d3GetMockableRegistryObject(Session::class) return d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS); ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS);
} }
@ -147,7 +144,7 @@ class d3webauthnadminlogin extends AdminController
public function previousClassIsOrderStep(): bool public function previousClassIsOrderStep(): bool
{ {
$sClassKey = $this->d3GetPreviousClass(); $sClassKey = $this->d3GetPreviousClass();
$resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class) $resolvedClass = d3GetOxidDIC()->get('d3ox.webauthn.'.ControllerClassNameResolver::class)
->getClassNameById($sClassKey); ->getClassNameById($sClassKey);
$resolvedClass = $resolvedClass ?: 'start'; $resolvedClass = $resolvedClass ?: 'start';
@ -163,4 +160,18 @@ class d3webauthnadminlogin extends AdminController
{ {
return $this->previousClassIsOrderStep(); return $this->previousClassIsOrderStep();
} }
/**
* @return WebauthnLogin
*/
protected function getWebAuthnLogin(): WebauthnLogin
{
/** @var Request $request */
$request = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class);
return oxNew(WebauthnLogin::class,
$request->getRequestEscapedParameter('credential'),
$request->getRequestEscapedParameter('error')
);
}
} }

View File

@ -43,11 +43,11 @@ trait checkoutGetUserTrait
$user = $this->d3CallMockableFunction([$this->parentClass, 'getUser']); $user = $this->d3CallMockableFunction([$this->parentClass, 'getUser']);
if ($user && $user->isLoaded() && $user->getId()) { if ($user && $user->isLoaded() && $user->getId()) {
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn = d3GetOxidDIC()->get(Webauthn::class);
if ($webauthn->isAvailable() if ($webauthn->isAvailable()
&& $webauthn->isActive($user->getId()) && $webauthn->isActive($user->getId())
&& !$this->d3GetMockableRegistryObject(Session::class) && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH)
) { ) {
return false; return false;

View File

@ -27,10 +27,12 @@ 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\Request;
use OxidEsales\Eshop\Core\SeoEncoder; use OxidEsales\Eshop\Core\SeoEncoder;
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;
use Psr\Log\LoggerInterface;
use Throwable; use Throwable;
class d3_account_webauthn extends AccountController class d3_account_webauthn extends AccountController
@ -48,7 +50,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->d3GetMockableOxNewObject(Webauthn::class)->isAvailable())); $this->addTplParam('readonly', !(d3GetOxidDIC()->get(Webauthn::class)->isAvailable()));
return $sRet; return $sRet;
} }
@ -63,7 +65,7 @@ class d3_account_webauthn extends AccountController
public function getCredentialList(): PublicKeyCredentialList public function getCredentialList(): PublicKeyCredentialList
{ {
$oUser = $this->getUser(); $oUser = $this->getUser();
$credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); $credentialList = d3GetOxidDIC()->get(PublicKeyCredentialList::class);
return $credentialList->getAllFromUser($oUser); return $credentialList->getAllFromUser($oUser);
} }
@ -80,9 +82,9 @@ class d3_account_webauthn extends AccountController
$this->setAuthnRegister(); $this->setAuthnRegister();
$this->setPageType('requestnew'); $this->setPageType('requestnew');
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
} }
} }
@ -104,7 +106,7 @@ class d3_account_webauthn extends AccountController
*/ */
public function setAuthnRegister(): void public function setAuthnRegister(): void
{ {
$publicKeyCredentialCreationOptions = $this->d3GetMockableOxNewObject(Webauthn::class) $publicKeyCredentialCreationOptions = d3GetOxidDIC()->get(Webauthn::class)
->getCreationOptions($this->getUser()); ->getCreationOptions($this->getUser());
$this->addTplParam('webauthn_publickey_create', $publicKeyCredentialCreationOptions); $this->addTplParam('webauthn_publickey_create', $publicKeyCredentialCreationOptions);
@ -122,20 +124,23 @@ class d3_account_webauthn extends AccountController
public function saveAuthn(): void public function saveAuthn(): void
{ {
try { try {
$error = Registry::getRequest()->getRequestEscapedParameter('error'); /** @var Request $request */
$request = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class);
$error = $request->getRequestEscapedParameter('error');
if (strlen((string) $error)) { if (strlen((string) $error)) {
/** @var WebauthnCreateException $e */ /** @var WebauthnCreateException $e */
$e = oxNew(WebauthnCreateException::class, $error); $e = oxNew(WebauthnCreateException::class, $error);
throw $e; throw $e;
} }
$credential = Registry::getRequest()->getRequestEscapedParameter('credential'); $credential = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('credential');
if (strlen((string) $credential)) { if (strlen((string) $credential)) {
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn = d3GetOxidDIC()->get(Webauthn::class);
$webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname')); $webauthn->saveAuthn($credential, d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('keyname'));
} }
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
} }
} }
@ -144,9 +149,9 @@ class d3_account_webauthn extends AccountController
*/ */
public function deleteKey(): void public function deleteKey(): void
{ {
$deleteId = Registry::getRequest()->getRequestEscapedParameter('deleteoxid'); $deleteId = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('deleteoxid');
if ($deleteId) { if ($deleteId) {
$credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); $credential = d3GetOxidDIC()->get(PublicKeyCredential::class);
$credential->delete($deleteId); $credential->delete($deleteId);
} }
} }

View File

@ -28,6 +28,7 @@ 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;
use Psr\Log\LoggerInterface;
class d3webauthnlogin extends FrontendController class d3webauthnlogin extends FrontendController
{ {
@ -40,7 +41,7 @@ class d3webauthnlogin extends FrontendController
*/ */
public function getNavigationParams(): array public function getNavigationParams(): array
{ {
$navparams = $this->d3GetMockableRegistryObject(Session::class)->getVariable( $navparams = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->getVariable(
WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS
); );
@ -60,17 +61,17 @@ class d3webauthnlogin extends FrontendController
*/ */
public function render(): string public function render(): string
{ {
if ($this->d3GetMockableRegistryObject(Session::class) if (d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) || ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) ||
!$this->d3GetMockableRegistryObject(Session::class) !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER) ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER)
) { ) {
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=start'); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=start');
} }
$this->generateCredentialRequest(); $this->generateCredentialRequest();
$this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class) $this->addTplParam('navFormParams', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); ->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS));
return $this->d3CallMockableFunction([FrontendController::class, 'render']); return $this->d3CallMockableFunction([FrontendController::class, 'render']);
@ -85,22 +86,22 @@ class d3webauthnlogin extends FrontendController
*/ */
public function generateCredentialRequest(): void public function generateCredentialRequest(): void
{ {
$userId = $this->d3GetMockableRegistryObject(Session::class) $userId = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
try { try {
/** @var Webauthn $webauthn */ /** @var Webauthn $webauthn */
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn = d3GetOxidDIC()->get(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->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->setVariable(WebauthnConf::GLOBAL_SWITCH, true); ->setVariable(WebauthnConf::GLOBAL_SWITCH, true);
$this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
Registry::getUtilsView()->addErrorToDisplay($e); Registry::getUtilsView()->addErrorToDisplay($e);
$this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=start'); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=start');
} }
} }
@ -109,7 +110,7 @@ class d3webauthnlogin extends FrontendController
*/ */
public function d3GetPreviousClass(): ?string public function d3GetPreviousClass(): ?string
{ {
return $this->d3GetMockableRegistryObject(Session::class) return d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS);
} }
@ -119,7 +120,7 @@ class d3webauthnlogin extends FrontendController
public function previousClassIsOrderStep(): bool public function previousClassIsOrderStep(): bool
{ {
$sClassKey = $this->d3GetPreviousClass(); $sClassKey = $this->d3GetPreviousClass();
$resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class) $resolvedClass = d3GetOxidDIC()->get('d3ox.webauthn.'.ControllerClassNameResolver::class)
->getClassNameById($sClassKey); ->getClassNameById($sClassKey);
$resolvedClass = $resolvedClass ?: 'start'; $resolvedClass = $resolvedClass ?: 'start';

View File

@ -112,8 +112,13 @@ class PublicKeyCredential extends BaseModel
*/ */
public function getCredential(): ?PublicKeyCredentialSource public function getCredential(): ?PublicKeyCredentialSource
{ {
return unserialize(base64_decode($this->__get($this->_getFieldLongName('credential'))->rawValue)) ?: return unserialize(
null; base64_decode(
$this->__get($this->_getFieldLongName('credential'))->rawValue
),
['allowed_classes' => [PublicKeyCredentialSource::class]]
) ?:
null;
} }
/** /**
@ -130,7 +135,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->d3GetMockableOxNewObject(PublicKeyCredentialList::class) if (d3GetOxidDIC()->get(PublicKeyCredentialList::class)
->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId()) ->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId())
) { ) {
return; return;
@ -174,7 +179,7 @@ class PublicKeyCredential extends BaseModel
), ),
$qb->expr()->eq( $qb->expr()->eq(
'oxshopid', 'oxshopid',
$qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
) )
) )
); );

View File

@ -63,12 +63,14 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo
), ),
$qb->expr()->eq( $qb->expr()->eq(
'oxshopid', 'oxshopid',
$qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
) )
) )
); );
$credential = $qb->execute()->fetchOne(); $credential = $qb->execute()->fetchOne();
//dumpvar($qb->getSQL());
//dumpvar($qb->getParameters());
//dumpvar(unserialize(base64_decode($credential)));
if (!strlen((string) $credential)) { if (!strlen((string) $credential)) {
return null; return null;
} }
@ -100,14 +102,17 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo
), ),
$qb->expr()->eq( $qb->expr()->eq(
'oxshopid', 'oxshopid',
$qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
) )
) )
); );
// generate decoded credentials list // generate decoded credentials list
return array_map(function (array $fields) { return array_map(function (array $fields) {
return unserialize(base64_decode($fields['credential'])); /** @var PublicKeyCredential $credential */
$credential = clone $this->getBaseObject();
$credential->assign(['credential' => $fields['credential']]);
return $credential->getCredential();
}, $qb->execute()->fetchAllAssociative()); }, $qb->execute()->fetchAllAssociative());
} }
@ -137,7 +142,7 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo
), ),
$qb->expr()->eq( $qb->expr()->eq(
'oxshopid', 'oxshopid',
$qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
) )
) )
); );

View File

@ -59,6 +59,6 @@ class RelyingPartyEntity extends PublicKeyCredentialRpEntity
*/ */
public function getActiveShop(): Shop public function getActiveShop(): Shop
{ {
return $this->d3GetMockableRegistryObject(Config::class)->getActiveShop(); return d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getActiveShop();
} }
} }

View File

@ -33,6 +33,7 @@ use OxidEsales\Eshop\Core\Session;
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;
use Psr\Log\LoggerInterface;
use Throwable; use Throwable;
use Webauthn\PublicKeyCredentialCreationOptions; use Webauthn\PublicKeyCredentialCreationOptions;
use Webauthn\PublicKeyCredentialDescriptor; use Webauthn\PublicKeyCredentialDescriptor;
@ -62,8 +63,8 @@ class Webauthn
} }
$e = oxNew(WebauthnException::class, 'D3_WEBAUTHN_ERR_UNSECURECONNECTION'); $e = oxNew(WebauthnException::class, 'D3_WEBAUTHN_ERR_UNSECURECONNECTION');
$this->d3GetMockableLogger()->info($e->getDetailedErrorMessage()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->info($e->getDetailedErrorMessage());
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
return false; return false;
} }
@ -78,7 +79,8 @@ class Webauthn
*/ */
public function getCreationOptions(User $user): string public function getCreationOptions(User $user): string
{ {
$userEntity = $this->d3GetMockableOxNewObject(UserEntity::class, $user); d3GetOxidDIC()->set(UserEntity::class.'.args.user', $user);
$userEntity = d3GetOxidDIC()->get(UserEntity::class);
$publicKeyCredentialCreationOptions = $this->getServer()->generatePublicKeyCredentialCreationOptions( $publicKeyCredentialCreationOptions = $this->getServer()->generatePublicKeyCredentialCreationOptions(
$userEntity, $userEntity,
@ -86,7 +88,7 @@ class Webauthn
$this->getExistingCredentials($userEntity) $this->getExistingCredentials($userEntity)
); );
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->setVariable(self::SESSION_CREATIONS_OPTIONS, $publicKeyCredentialCreationOptions); ->setVariable(self::SESSION_CREATIONS_OPTIONS, $publicKeyCredentialCreationOptions);
$json = $this->jsonEncode($publicKeyCredentialCreationOptions); $json = $this->jsonEncode($publicKeyCredentialCreationOptions);
@ -108,7 +110,7 @@ class Webauthn
{ {
// Get the list of authenticators associated to the user // Get the list of authenticators associated to the user
/** @var PublicKeyCredentialList $credentialList */ /** @var PublicKeyCredentialList $credentialList */
$credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); $credentialList = d3GetOxidDIC()->get(PublicKeyCredentialList::class);
$credentialSources = $credentialList->findAllForUserEntity($userEntity); $credentialSources = $credentialList->findAllForUserEntity($userEntity);
// Convert the Credential Sources into Public Key Credential Descriptors // Convert the Credential Sources into Public Key Credential Descriptors
@ -135,9 +137,10 @@ class Webauthn
public function getRequestOptions(string $userId): string public function getRequestOptions(string $userId): string
{ {
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$user->load($userId); $user->load($userId);
$userEntity = $this->d3GetMockableOxNewObject(UserEntity::class, $user); d3GetOxidDIC()->set(UserEntity::class.'.args.user', $user);
$userEntity = d3GetOxidDIC()->get(UserEntity::class);
// We generate the set of options. // We generate the set of options.
$publicKeyCredentialRequestOptions = $this->getServer()->generatePublicKeyCredentialRequestOptions( $publicKeyCredentialRequestOptions = $this->getServer()->generatePublicKeyCredentialRequestOptions(
@ -145,7 +148,7 @@ class Webauthn
$this->getExistingCredentials($userEntity) $this->getExistingCredentials($userEntity)
); );
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->setVariable(self::SESSION_ASSERTION_OPTIONS, $publicKeyCredentialRequestOptions); ->setVariable(self::SESSION_ASSERTION_OPTIONS, $publicKeyCredentialRequestOptions);
$json = $this->jsonEncode($publicKeyCredentialRequestOptions); $json = $this->jsonEncode($publicKeyCredentialRequestOptions);
@ -162,15 +165,8 @@ class Webauthn
*/ */
protected function getServer(): Server protected function getServer(): Server
{ {
/** @var RelyingPartyEntity $rpEntity */ $server = $this->getServerObject();
$rpEntity = $this->d3GetMockableOxNewObject(RelyingPartyEntity::class); $server->setLogger(d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class));
/** @var Server $server */
$server = $this->d3GetMockableOxNewObject(
Server::class,
$rpEntity,
$this->d3GetMockableOxNewObject(PublicKeyCredentialList::class)
);
$server->setLogger($this->d3GetMockableLogger());
return $server; return $server;
} }
@ -196,11 +192,11 @@ class Webauthn
$publicKeyCredentialSource = $this->getServer()->loadAndCheckAttestationResponse( $publicKeyCredentialSource = $this->getServer()->loadAndCheckAttestationResponse(
html_entity_decode($credential), html_entity_decode($credential),
$this->d3GetMockableRegistryObject(Session::class)->getVariable(self::SESSION_CREATIONS_OPTIONS), d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->getVariable(self::SESSION_CREATIONS_OPTIONS),
$serverRequest $serverRequest
); );
$pkCredential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); $pkCredential = d3GetOxidDIC()->get(PublicKeyCredential::class);
$pkCredential->saveCredentialSource($publicKeyCredentialSource, $keyName); $pkCredential->saveCredentialSource($publicKeyCredentialSource, $keyName);
} }
@ -226,7 +222,7 @@ class Webauthn
try { try {
$this->getServer()->loadAndCheckAssertionResponse( $this->getServer()->loadAndCheckAssertionResponse(
html_entity_decode($response), html_entity_decode($response),
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(self::SESSION_ASSERTION_OPTIONS), ->getVariable(self::SESSION_ASSERTION_OPTIONS),
$userEntity, $userEntity,
$serverRequest $serverRequest
@ -247,10 +243,11 @@ class Webauthn
protected function getUserEntityFrom($userId): UserEntity protected function getUserEntityFrom($userId): UserEntity
{ {
/** @var User $user */ /** @var User $user */
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$user->load($userId); $user->load($userId);
return $this->d3GetMockableOxNewObject(UserEntity::class, $user); d3GetOxidDIC()->set(UserEntity::class.'.args.user', $user);
return d3GetOxidDIC()->get(UserEntity::class);
} }
/** /**
@ -258,7 +255,7 @@ class Webauthn
*/ */
protected function getSavedUserIdFromSession(): ?string protected function getSavedUserIdFromSession(): ?string
{ {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
return $this->isAdmin() ? return $this->isAdmin() ?
$session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) : $session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) :
@ -283,9 +280,9 @@ class Webauthn
*/ */
public function isActive($userId): bool public function isActive($userId): bool
{ {
return !$this->d3GetMockableRegistryObject(Config::class) return !d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)
->getConfigParam(WebauthnConf::GLOBAL_SWITCH) ->getConfigParam(WebauthnConf::GLOBAL_SWITCH)
&& !$this->d3GetMockableRegistryObject(Session::class) && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::GLOBAL_SWITCH) ->getVariable(WebauthnConf::GLOBAL_SWITCH)
&& $this->UserUseWebauthn($userId); && $this->UserUseWebauthn($userId);
} }
@ -303,9 +300,24 @@ class Webauthn
$entity = $this->getUserEntityFrom($userId); $entity = $this->getUserEntityFrom($userId);
/** @var PublicKeyCredentialList $credentialList */ /** @var PublicKeyCredentialList $credentialList */
$credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); $credentialList = d3GetOxidDIC()->get(PublicKeyCredentialList::class);
$list = $credentialList->findAllForUserEntity($entity); $list = $credentialList->findAllForUserEntity($entity);
return is_array($list) && count($list); return is_array($list) && count($list);
} }
/**
* @return Server
*/
protected function getServerObject()
{
/** @var RelyingPartyEntity $rpEntity */
$rpEntity = d3GetOxidDIC()->get(RelyingPartyEntity::class);
return oxNew(
Server::class,
$rpEntity,
d3GetOxidDIC()->get(PublicKeyCredentialList::class)
);
}
} }

View File

@ -30,15 +30,15 @@ class WebauthnAfterLogin
*/ */
public function setDisplayProfile(): void public function setDisplayProfile(): void
{ {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
$sProfile = $this->d3GetMockableRegistryObject(Request::class) $sProfile = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)
->getRequestEscapedParameter('profile') ?: ->getRequestEscapedParameter('profile') ?:
$session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); $session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE);
$session->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); $session->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE);
$myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class); $myUtilsServer = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsServer::class);
if (isset($sProfile)) { if (isset($sProfile)) {
$aProfiles = $session->getVariable("aAdminProfiles"); $aProfiles = $session->getVariable("aAdminProfiles");
@ -58,17 +58,17 @@ class WebauthnAfterLogin
*/ */
public function changeLanguage(): void public function changeLanguage(): void
{ {
$myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class); $myUtilsServer = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsServer::class);
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
// languages // languages
$iLang = $this->d3GetMockableRegistryObject(Request::class) $iLang = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)
->getRequestEscapedParameter('chlanguage') ?: ->getRequestEscapedParameter('chlanguage') ?:
$session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE); $session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE);
$session->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE); $session->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE);
$language = $this->d3GetMockableRegistryObject(Language::class); $language = d3GetOxidDIC()->get('d3ox.webauthn.'.Language::class);
$aLanguages = $language->getAdminTplLanguageArray(); $aLanguages = $language->getAdminTplLanguageArray();
if (!isset($aLanguages[$iLang])) { if (!isset($aLanguages[$iLang])) {

View File

@ -38,7 +38,7 @@ class WebauthnErrors
*/ */
public function translateError(string $msg, string $type = null): string public function translateError(string $msg, string $type = null): string
{ {
$lang = $this->d3GetMockableRegistryObject(Language::class); $lang = d3GetOxidDIC()->get('d3ox.webauthn.'.Language::class);
$type = $type ? '_'.$type : null; $type = $type ? '_'.$type : null;
switch ($this->getErrIdFromMessage($msg)) { switch ($this->getErrIdFromMessage($msg)) {

View File

@ -31,6 +31,7 @@ use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsServer; use OxidEsales\Eshop\Core\UtilsServer;
use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\EshopCommunity\Application\Component\UserComponent; use OxidEsales\EshopCommunity\Application\Component\UserComponent;
use Psr\Log\LoggerInterface;
class WebauthnLogin class WebauthnLogin
{ {
@ -104,9 +105,9 @@ class WebauthnLogin
*/ */
public function frontendLogin(UserComponent $usrCmp, bool $setSessionCookie = false) public function frontendLogin(UserComponent $usrCmp, bool $setSessionCookie = false)
{ {
$myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class); $myUtilsView = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class);
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.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->d3GetMockableOxNewObject(User::class)); $usrCmp->setUser(d3GetOxidDIC()->get('d3ox.webauthn.'.User::class));
$this->setFrontendSession($user); $this->setFrontendSession($user);
$usrCmp->setLoginStatus(USER_LOGIN_SUCCESS); $usrCmp->setLoginStatus(USER_LOGIN_SUCCESS);
@ -136,8 +137,8 @@ class WebauthnLogin
$myUtilsView->addErrorToDisplay($oEx); $myUtilsView->addErrorToDisplay($oEx);
} catch (WebauthnException $e) { } catch (WebauthnException $e) {
$myUtilsView->addErrorToDisplay($e); $myUtilsView->addErrorToDisplay($e);
$this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
} }
$user->logout(); $user->logout();
@ -150,9 +151,9 @@ class WebauthnLogin
*/ */
public function adminLogin(string $selectedProfile): string public function adminLogin(string $selectedProfile): string
{ {
$myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class); $myUtilsView = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class);
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$userId = $this->getUserId(); $userId = $this->getUserId();
try { try {
@ -164,10 +165,10 @@ class WebauthnLogin
$this->handleBackendCookie(); $this->handleBackendCookie();
$this->handleBackendSubshopRights($user, $session); $this->handleBackendSubshopRights($user, $session);
$oEventHandler = $this->d3GetMockableOxNewObject(SystemEventHandler::class); $oEventHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.SystemEventHandler::class);
$oEventHandler->onAdminLogin(); $oEventHandler->onAdminLogin();
$afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class); $afterLogin = d3GetOxidDIC()->get(WebauthnAfterLogin::class);
$afterLogin->setDisplayProfile(); $afterLogin->setDisplayProfile();
$afterLogin->changeLanguage(); $afterLogin->changeLanguage();
@ -181,15 +182,15 @@ 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->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
$this->d3GetMockableLogger()->debug($e->getTraceAsString()); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
} }
$user->logout(); $user->logout();
$oStr = Str::getStr(); $oStr = Str::getStr();
$this->d3GetMockableRegistryObject(Config::class)->getActiveView() d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getActiveView()
->addTplParam('user', $oStr->htmlspecialchars($userId)); ->addTplParam('user', $oStr->htmlspecialchars($userId));
$this->d3GetMockableRegistryObject(Config::class)->getActiveView() d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getActiveView()
->addTplParam('profile', $oStr->htmlspecialchars($selectedProfile)); ->addTplParam('profile', $oStr->htmlspecialchars($selectedProfile));
return 'login'; return 'login';
@ -215,7 +216,7 @@ class WebauthnLogin
public function assertAuthn(): void public function assertAuthn(): void
{ {
$credential = $this->getCredential(); $credential = $this->getCredential();
$webAuthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webAuthn = d3GetOxidDIC()->get(Webauthn::class);
$webAuthn->assertAuthn($credential); $webAuthn->assertAuthn($credential);
} }
@ -225,7 +226,7 @@ class WebauthnLogin
*/ */
public function setAdminSession($userId): Session public function setAdminSession($userId): Session
{ {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
$adminProfiles = $session->getVariable("aAdminProfiles"); $adminProfiles = $session->getVariable("aAdminProfiles");
$session->initNewSession(); $session->initNewSession();
$session->setVariable("aAdminProfiles", $adminProfiles); $session->setVariable("aAdminProfiles", $adminProfiles);
@ -239,11 +240,11 @@ class WebauthnLogin
*/ */
public function setSessionCookie(User $user) public function setSessionCookie(User $user)
{ {
if ($this->d3GetMockableRegistryObject(Config::class)->getConfigParam('blShowRememberMe')) { if (d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getConfigParam('blShowRememberMe')) {
$this->d3GetMockableRegistryObject(UtilsServer::class)->setUserCookie( d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsServer::class)->setUserCookie(
$user->getFieldData('oxusername'), $user->getFieldData('oxusername'),
$user->getFieldData('oxpassword'), $user->getFieldData('oxpassword'),
$this->d3GetMockableRegistryObject(Config::class)->getShopId() d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId()
); );
} }
} }
@ -256,7 +257,7 @@ class WebauthnLogin
*/ */
public function assertUser($userId, bool $isBackend = false): User public function assertUser($userId, bool $isBackend = false): User
{ {
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$user->load($userId); $user->load($userId);
if (!$user->isLoaded() || if (!$user->isLoaded() ||
($isBackend && $user->getFieldData('oxrights') === 'user') ($isBackend && $user->getFieldData('oxrights') === 'user')
@ -275,7 +276,7 @@ class WebauthnLogin
*/ */
public function handleBackendCookie(): void public function handleBackendCookie(): void
{ {
$cookie = $this->d3GetMockableRegistryObject(UtilsServer::class)->getOxCookie(); $cookie = d3GetOxidDIC()->get('d3ox.webauthn.'.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');
@ -295,7 +296,7 @@ class WebauthnLogin
if ($iSubshop) { if ($iSubshop) {
$session->setVariable("shp", $iSubshop); $session->setVariable("shp", $iSubshop);
$session->setVariable('currentadminshop', $iSubshop); $session->setVariable('currentadminshop', $iSubshop);
$this->d3GetMockableRegistryObject(Config::class)->setShopId((string) $iSubshop); d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->setShopId((string) $iSubshop);
} }
} }
@ -304,7 +305,7 @@ class WebauthnLogin
*/ */
public function regenerateSessionId(): void public function regenerateSessionId(): void
{ {
$oSession = $this->d3GetMockableRegistryObject(Session::class); $oSession = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
if ($oSession->isSessionStarted()) { if ($oSession->isSessionStarted()) {
$oSession->regenerateSessionId(); $oSession->regenerateSessionId();
} }
@ -314,9 +315,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->d3GetMockableRegistryObject(Config::class)->getShopHomeUrl() . $sUrl = d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopHomeUrl() .
'cl=content&tpl=user_blocked.tpl'; 'cl=content&tpl=user_blocked.tpl';
$this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect($sUrl);
} }
} }
@ -325,7 +326,7 @@ class WebauthnLogin
*/ */
public function updateBasket(): void public function updateBasket(): void
{ {
$oBasket = $this->d3GetMockableRegistryObject(Session::class)->getBasket(); $oBasket = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->getBasket();
$oBasket->onUpdate(); $oBasket->onUpdate();
} }
@ -343,9 +344,9 @@ class WebauthnLogin
public function getUserId(): string public function getUserId(): string
{ {
return $this->isAdmin() ? return $this->isAdmin() ?
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) : ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) :
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
} }
@ -356,7 +357,7 @@ class WebauthnLogin
*/ */
public function setFrontendSession(User $user): void public function setFrontendSession(User $user): void
{ {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
$session->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $this->getCredential()); $session->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $this->getCredential());
$session->setVariable(WebauthnConf::OXID_FRONTEND_AUTH, $user->getId()); $session->setVariable(WebauthnConf::OXID_FRONTEND_AUTH, $user->getId());
} }

138
src/Config/oxid.yaml Normal file
View File

@ -0,0 +1,138 @@
# This Software is the property of Data Development and is protected
# by copyright law - it is NOT Freeware.
#
# Any unauthorized use of this software without a valid license
# is a violation of the license agreement and will be prosecuted by
# civil and criminal law.
#
# https://www.d3data.de
#
# @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
# @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
# @link https://www.oxidmodule.com
services:
_defaults:
autowire: false
public: true
# config
d3ox.webauthn.OxidEsales\Eshop\Core\Config:
class: 'OxidEsales\Eshop\Core\Config'
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getConfig'
shared: true
# request
d3ox.webauthn.OxidEsales\Eshop\Core\Request:
class: 'OxidEsales\Eshop\Core\Request'
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getRequest'
shared: true
# session
d3ox.webauthn.OxidEsales\Eshop\Core\Session:
class: 'OxidEsales\Eshop\Core\Session'
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getSession'
shared: true
# language
d3ox.webauthn.OxidEsales\Eshop\Core\Language:
class: 'OxidEsales\Eshop\Core\Language'
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getLang'
shared: true
# Utils
d3ox.webauthn.OxidEsales\Eshop\Core\Utils:
class: 'OxidEsales\Eshop\Core\Utils'
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getUtils'
shared: true
# UtilsServer
d3ox.webauthn.OxidEsales\Eshop\Core\UtilsServer:
class: 'OxidEsales\Eshop\Core\UtilsServer'
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getUtilsServer'
shared: true
# UtilsView
d3ox.webauthn.OxidEsales\Eshop\Core\UtilsView:
class: 'OxidEsales\Eshop\Core\UtilsView'
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getUtilsView'
shared: true
# DbMetaDataHandler
d3ox.webauthn.OxidEsales\Eshop\Core\DbMetaDataHandler:
class: 'OxidEsales\Eshop\Core\DbMetaDataHandler'
factory: 'oxNew'
arguments:
- 'OxidEsales\Eshop\Core\DbMetaDataHandler'
shared: false
# SeoEncoder
d3ox.webauthn.OxidEsales\Eshop\Core\SeoEncoder:
class: 'OxidEsales\Eshop\Core\SeoEncoder'
factory: 'oxNew'
arguments:
- 'OxidEsales\Eshop\Core\SeoEncoder'
shared: false
# SystemEventHandler
d3ox.webauthn.OxidEsales\Eshop\Core\SystemEventHandler:
class: 'OxidEsales\Eshop\Core\SystemEventHandler'
factory: 'oxNew'
arguments:
- 'OxidEsales\Eshop\Core\SystemEventHandler'
shared: false
# ControllerClassNameResolver
d3ox.webauthn.OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver:
class: 'OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver'
factory: 'oxNew'
arguments:
- 'OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver'
shared: false
# FrontendController
d3ox.webauthn.OxidEsales\Eshop\Application\Controller\FrontendController:
class: 'OxidEsales\Eshop\Application\Controller\FrontendController'
factory: 'oxNew'
arguments:
- 'OxidEsales\Eshop\Application\Controller\FrontendController'
shared: false
# User
d3ox.webauthn.OxidEsales\Eshop\Application\Model\User:
class: 'OxidEsales\Eshop\Application\Model\User'
factory: 'oxNew'
arguments:
- 'OxidEsales\Eshop\Application\Model\User'
shared: false
d3ox.webauthn.Psr\Log\LoggerInterface:
class: Psr\Log\LoggerInterface
factory:
- 'OxidEsales\Eshop\Core\Registry'
- 'getLogger'
shared: true
# DB_assoc
d3ox.webauthn.OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface.assoc:
class: 'OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface'
factory:
- 'OxidEsales\Eshop\Core\DatabaseProvider'
- 'getDb'
arguments:
- 2
shared: true

15
src/Config/services.yaml Normal file
View File

@ -0,0 +1,15 @@
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#
# https://www.d3data.de
#
# @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
# @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
# @link https://www.oxidmodule.com
imports:
- { resource: oxid.yaml }
- { resource: webauthn.yaml }

160
src/Config/webauthn.yaml Normal file
View File

@ -0,0 +1,160 @@
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#
# https://www.d3data.de
#
# @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
# @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
# @link https://www.oxidmodule.com
parameters:
D3\Webauthn\Application\Model\WebauthnLogin.args.credential:
D3\Webauthn\Application\Model\WebauthnLogin.args.error:
# D3\Linkmobility4OXID\Application\Model\RequestFactory.args.message:
# D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms.args.message:
# D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException.args.smscount:
services:
_defaults:
autowire: false
public: true
D3\Webauthn\Application\Model\Webauthn:
class: D3\Webauthn\Application\Model
factory: 'oxNew'
shared: false
arguments:
- D3\Webauthn\Application\Model\Webauthn
D3\Webauthn\Application\Model\RelyingPartyEntity:
class: D3\Webauthn\Application\Model\RelyingPartyEntity
factory: 'oxNew'
shared: false
arguments:
- D3\Webauthn\Application\Model\RelyingPartyEntity
D3\Webauthn\Application\Model\UserEntity:
class: D3\Webauthn\Application\Model\UserEntity
factory: 'oxNew'
shared: false
arguments:
- D3\Webauthn\Application\Model\UserEntity
- '@D3\Webauthn\Application\Model\UserEntity.args.user'
D3\Webauthn\Application\Model\UserEntity.args.user:
synthetic: true
shared: false
D3\Webauthn\Application\Model\WebauthnAfterLogin:
class: D3\Webauthn\Application\Model\WebauthnAfterLogin
factory: 'oxNew'
shared: false
arguments:
- D3\Webauthn\Application\Model\WebauthnAfterLogin
D3\Webauthn\Application\Model\WebauthnLogin:
class: D3\Webauthn\Application\Model\WebauthnLogin
factory: 'oxNew'
shared: false
arguments:
- D3\Webauthn\Application\Model\WebauthnLogin
- '%D3\Webauthn\Application\Model\WebauthnLogin.args.credential%'
- '%D3\Webauthn\Application\Model\WebauthnLogin.args.error%'
D3\Webauthn\Application\Model\Credential\PublicKeyCredential:
class: D3\Webauthn\Application\Model\Credential\PublicKeyCredential
factory: 'oxNew'
shared: false
arguments:
- D3\Webauthn\Application\Model\Credential\PublicKeyCredential
D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList:
class: D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList
factory: 'oxNew'
shared: false
arguments:
- D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList
# D3\Linkmobility4OXID\Application\Model\OrderRecipients:
# class: D3\Linkmobility4OXID\Application\Model\OrderRecipients
# factory: 'oxNew'
# shared: false
# arguments:
# - D3\Linkmobility4OXID\Application\Model\OrderRecipients
# - '@D3\Linkmobility4OXID\Application\Model\OrderRecipients.args.order'
# D3\Linkmobility4OXID\Application\Model\OrderRecipients.args.order:
# synthetic: true
# shared: false
#
# D3\Linkmobility4OXID\Application\Model\UserRecipients:
# class: D3\Linkmobility4OXID\Application\Model\UserRecipients
# factory: 'oxNew'
# shared: false
# arguments:
# - D3\Linkmobility4OXID\Application\Model\UserRecipients
# - '@D3\Linkmobility4OXID\Application\Model\UserRecipients.args.user'
# D3\Linkmobility4OXID\Application\Model\UserRecipients.args.user:
# synthetic: true
# shared: false
#
# D3\Linkmobility4OXID\Application\Model\Configuration:
# class: D3\Linkmobility4OXID\Application\Model\Configuration
# factory: 'oxNew'
# shared: true
# arguments:
# - D3\Linkmobility4OXID\Application\Model\Configuration
#
# D3\Linkmobility4OXID\Application\Model\MessageClient:
# class: D3\Linkmobility4OXID\Application\Model\MessageClient
# factory: 'oxNew'
# shared: true
# arguments:
# - D3\Linkmobility4OXID\Application\Model\MessageClient
#
# D3\Linkmobility4OXID\Application\Model\MessageSender:
# class: D3\Linkmobility4OXID\Application\Model\MessageSender
# factory: 'oxNew'
# shared: true
# arguments:
# - D3\Linkmobility4OXID\Application\Model\MessageSender
#
# D3\Linkmobility4OXID\Application\Model\RequestFactory:
# class: D3\Linkmobility4OXID\Application\Model\RequestFactory
# factory: 'oxNew'
# shared: true
# arguments:
# - D3\Linkmobility4OXID\Application\Model\RequestFactory
# - '%D3\Linkmobility4OXID\Application\Model\RequestFactory.args.message%'
# - '@D3\Linkmobility4OXID\Application\Model\RequestFactory.args.client'
# D3\Linkmobility4OXID\Application\Model\RequestFactory.args.client:
# synthetic: true
# shared: false
#
# D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms:
# class: D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms
# factory: 'oxNew'
# shared: false
# arguments:
# - D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms
# - '%D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms.args.message%'
#
# D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException:
# class: D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException
# factory: 'oxNew'
# shared: false
# arguments:
# - D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException
# - '%D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException.args.smscount%'
#
# D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException:
# class: D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException
# factory: 'oxNew'
# shared: false
# arguments:
# - D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException
#
# D3\Linkmobility4OXID\Setup\Actions:
# class: D3\Linkmobility4OXID\Setup\Actions
# factory: 'oxNew'
# shared: false
# arguments:
# - D3\Linkmobility4OXID\Setup\Actions

View File

@ -11,6 +11,15 @@
* @link https://www.oxidmodule.com * @link https://www.oxidmodule.com
*/ */
namespace D3\Webauthn\Modules
{
use D3\DIContainerHandler\definitionFileContainer;
class WebauthnServices_parent extends definitionFileContainer
{
}
}
namespace D3\Webauthn\Modules\Application\Component namespace D3\Webauthn\Modules\Application\Component
{ {
use OxidEsales\Eshop\Application\Component\UserComponent; use OxidEsales\Eshop\Application\Component\UserComponent;

View File

@ -58,14 +58,14 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
*/ */
public function d3WebauthnLogin(): void public function d3WebauthnLogin(): void
{ {
$lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_usr'); $lgn_user = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('lgn_usr');
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$userId = $user->d3GetLoginUserId($lgn_user); $userId = $user->d3GetLoginUserId($lgn_user);
if ($this->d3CanUseWebauthn($lgn_user, $userId)) { if ($this->d3CanUseWebauthn($lgn_user, $userId)) {
if ($this->d3HasWebauthnButNotLoggedin($userId)) { if ($this->d3HasWebauthnButNotLoggedin($userId)) {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
$session->setVariable( $session->setVariable(
WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS, WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS,
$this->getClassKey() != 'd3webauthnlogin' ? $this->getClassKey() : 'start' $this->getClassKey() != 'd3webauthnlogin' ? $this->getClassKey() : 'start'
@ -83,8 +83,8 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
$this->getParent()->getViewConfig()->getNavFormParams() $this->getParent()->getViewConfig()->getNavFormParams()
); );
$sUrl = $this->d3GetMockableRegistryObject(Config::class)->getShopHomeUrl() . 'cl=d3webauthnlogin'; $sUrl = d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopHomeUrl() . 'cl=d3webauthnlogin';
$this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl); d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect($sUrl);
} }
} }
} }
@ -97,11 +97,11 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
*/ */
protected function d3CanUseWebauthn($lgn_user, ?string $userId): bool protected function d3CanUseWebauthn($lgn_user, ?string $userId): bool
{ {
$password = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_pwd'); $password = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('lgn_pwd');
return $lgn_user && return $lgn_user &&
$userId && $userId &&
false === $this->d3GetMockableRegistryObject(Session::class) false === d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) && ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) &&
(! strlen(trim((string) $password))); (! strlen(trim((string) $password)));
} }
@ -114,10 +114,10 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
*/ */
protected function d3HasWebauthnButNotLoggedin($userId): bool protected function d3HasWebauthnButNotLoggedin($userId): bool
{ {
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn = d3GetOxidDIC()->get(Webauthn::class);
return $webauthn->isActive($userId) return $webauthn->isActive($userId)
&& !$this->d3GetMockableRegistryObject(Session::class) && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH); ->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH);
} }
@ -134,7 +134,7 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
*/ */
public function d3WebauthnClearSessionVariables(): void public function d3WebauthnClearSessionVariables(): void
{ {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
$session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS);
$session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
$session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS);
@ -147,19 +147,29 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
public function d3AssertAuthn(): void public function d3AssertAuthn(): void
{ {
try { try {
$login = $this->d3GetMockableOxNewObject( $login = $this->d3GetWebauthnLogin();
WebauthnLogin::class,
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'),
$this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error')
);
$login->frontendLogin( $login->frontendLogin(
$this, $this,
(bool)$this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_cook') (bool)d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('lgn_cook')
); );
$this->_afterLogin($this->getUser()); $this->_afterLogin($this->getUser());
} catch (WebauthnGetException $e) { } catch (WebauthnGetException $e) {
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
} catch (WebauthnLoginErrorException $e) { } catch (WebauthnLoginErrorException $e) {
} }
} }
/**
* @return WebauthnLogin
*/
protected function d3GetWebauthnLogin(): WebauthnLogin
{
/** @var Request $request */
$request = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class);
return oxNew(WebauthnLogin::class,
$request->getRequestEscapedParameter('credential'),
$request->getRequestEscapedParameter('error')
);
}
} }

View File

@ -40,36 +40,36 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
*/ */
public function checklogin() public function checklogin()
{ {
$lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('user') ?: $lgn_user = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('user') ?:
$this->d3GetMockableRegistryObject(Session::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER); ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER);
/** @var d3_User_Webauthn $user */ /** @var d3_User_Webauthn $user */
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.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->d3GetMockableRegistryObject(Session::class)->setVariable( d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_PROFILE, WebauthnConf::WEBAUTHN_ADMIN_PROFILE,
$this->d3GetMockableRegistryObject(Request::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)
->getRequestEscapedParameter('profile') ->getRequestEscapedParameter('profile')
); );
$this->d3GetMockableRegistryObject(Session::class)->setVariable( d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE, WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE,
$this->d3GetMockableRegistryObject(Request::class) d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)
->getRequestEscapedParameter('chlanguage') ->getRequestEscapedParameter('chlanguage')
); );
if ($this->hasWebauthnButNotLoggedin($userId)) { if ($this->hasWebauthnButNotLoggedin($userId)) {
$this->d3GetMockableRegistryObject(Session::class)->setVariable( d3GetOxidDIC()->get('d3ox.webauthn.'.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->d3GetMockableRegistryObject(Session::class)->setVariable( d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER, WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER,
$userId $userId
); );
$this->d3GetMockableRegistryObject(Session::class)->setVariable( d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable(
WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER, WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER,
$lgn_user $lgn_user
); );
@ -86,7 +86,7 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
*/ */
public function d3WebauthnCancelLogin(): void public function d3WebauthnCancelLogin(): void
{ {
$user = $this->d3GetMockableOxNewObject(User::class); $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class);
$user->logout(); $user->logout();
} }
@ -98,11 +98,11 @@ 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->d3GetMockableRegistryObject(Request::class)->getRequestParameter('pwd'); $password = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('pwd');
return $lgn_user && return $lgn_user &&
$userId && $userId &&
false === $this->d3GetMockableRegistryObject(Session::class) false === d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) && ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) &&
(! strlen(trim((string) $password))); (! strlen(trim((string) $password)));
} }
@ -115,10 +115,10 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
*/ */
protected function hasWebauthnButNotLoggedin($userId): bool protected function hasWebauthnButNotLoggedin($userId): bool
{ {
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn = d3GetOxidDIC()->get(Webauthn::class);
return $webauthn->isActive($userId) return $webauthn->isActive($userId)
&& !$this->d3GetMockableRegistryObject(Session::class) && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH); ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH);
} }
} }

View File

@ -20,6 +20,7 @@ use D3\Webauthn\Application\Model\WebauthnConf;
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Query\QueryBuilder; use Doctrine\DBAL\Query\QueryBuilder;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
@ -46,7 +47,7 @@ class d3_User_Webauthn extends d3_User_Webauthn_parent
*/ */
protected function d3WebauthnLogout(): void protected function d3WebauthnLogout(): void
{ {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
$session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH);
$session->deleteVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT); $session->deleteVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT);
$session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
@ -83,13 +84,13 @@ class d3_User_Webauthn extends d3_User_Webauthn_parent
*/ */
protected function d3WebauthnLogin(string $userName) protected function d3WebauthnLogin(string $userName)
{ {
$session = $this->d3GetMockableRegistryObject(Session::class); $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class);
if ($session->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) && if ($session->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) &&
$userName === $session->getVariable(WebauthnConf::WEBAUTHN_SESSION_LOGINUSER) $userName === $session->getVariable(WebauthnConf::WEBAUTHN_SESSION_LOGINUSER)
) { ) {
$userName = $userName ?: $session->getVariable(WebauthnConf::WEBAUTHN_SESSION_LOGINUSER); $userName = $userName ?: $session->getVariable(WebauthnConf::WEBAUTHN_SESSION_LOGINUSER);
$shopId = $this->d3GetMockableRegistryObject(Config::class)->getShopId(); $shopId = d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId();
/** private method is out of scope */ /** private method is out of scope */
$class = new ReflectionClass($this); $class = new ReflectionClass($this);
@ -133,7 +134,7 @@ class d3_User_Webauthn extends d3_User_Webauthn_parent
), ),
$qb->expr()->eq( $qb->expr()->eq(
'oxshopid', 'oxshopid',
$qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
), ),
$rights ? $rights ?
$qb->expr()->eq( $qb->expr()->eq(

View File

@ -0,0 +1,29 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\Webauthn\Modules;
use D3\TestingTools\Production\IsMockable;
class WebauthnServices extends WebauthnServices_parent
{
use IsMockable;
public function __construct()
{
$this->d3CallMockableFunction([WebauthnServices_parent::class, '__construct']);
$this->addYamlDefinitions('d3/oxwebauthn/Config/services.yaml');
}
}

View File

@ -35,6 +35,7 @@ use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
class Actions class Actions
{ {
@ -85,7 +86,7 @@ class Actions
*/ */
public function tableExists(string $sTableName): bool public function tableExists(string $sTableName): bool
{ {
$oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class); $oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.DbMetaDataHandler::class);
return $oDbMetaDataHandler->tableExists($sTableName); return $oDbMetaDataHandler->tableExists($sTableName);
} }
@ -95,7 +96,9 @@ class Actions
*/ */
protected function d3GetDb(): ?DatabaseInterface protected function d3GetDb(): ?DatabaseInterface
{ {
return DatabaseProvider::getDb(); /** @var DatabaseInterface $db */
$db = d3GetOxidDIC()->get('d3ox.webauthn.'.DatabaseInterface::class.'.assoc');
return $db;
} }
/** /**
@ -120,7 +123,7 @@ class Actions
*/ */
public function fieldExists(string $sFieldName, string $sTableName): bool public function fieldExists(string $sFieldName, string $sTableName): bool
{ {
$oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class); $oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.DbMetaDataHandler::class);
return $oDbMetaDataHandler->fieldExists($sFieldName, $sTableName); return $oDbMetaDataHandler->fieldExists($sFieldName, $sTableName);
} }
@ -129,7 +132,7 @@ class Actions
*/ */
public function regenerateViews() public function regenerateViews()
{ {
$oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class); $oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.DbMetaDataHandler::class);
$oDbMetaDataHandler->updateViews(); $oDbMetaDataHandler->updateViews();
} }
@ -139,12 +142,12 @@ class Actions
public function clearCache() public function clearCache()
{ {
try { try {
$oUtils = $this->d3GetMockableRegistryObject(Utils::class); $oUtils = d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class);
$oUtils->resetTemplateCache($this->getModuleTemplates()); $oUtils->resetTemplateCache($this->getModuleTemplates());
$oUtils->resetLanguageCache(); $oUtils->resetLanguageCache();
} catch (ContainerExceptionInterface|NotFoundExceptionInterface|ModuleConfigurationNotFoundException $e) { } catch (ContainerExceptionInterface|NotFoundExceptionInterface|ModuleConfigurationNotFoundException $e) {
$this->d3GetMockableLogger()->error($e->getMessage(), [$this]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), [$this]);
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e->getMessage());
} }
} }
@ -208,8 +211,8 @@ class Actions
$this->createSeoUrl(); $this->createSeoUrl();
} }
} catch (Exception|NotFoundExceptionInterface|DoctrineDriverException|ContainerExceptionInterface $e) { } catch (Exception|NotFoundExceptionInterface|DoctrineDriverException|ContainerExceptionInterface $e) {
$this->d3GetMockableLogger()->error($e->getMessage(), [$this]); d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), [$this]);
$this->d3GetMockableRegistryObject(UtilsView::class) d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)
->addErrorToDisplay('error wile creating SEO URLs: '.$e->getMessage()); ->addErrorToDisplay('error wile creating SEO URLs: '.$e->getMessage());
} }
} }
@ -219,9 +222,9 @@ class Actions
*/ */
public function hasSeoUrl(): bool public function hasSeoUrl(): bool
{ {
$seoEncoder = $this->d3GetMockableOxNewObject(SeoEncoder::class); $seoEncoder = d3GetOxidDIC()->get('d3ox.webauthn.'.SeoEncoder::class);
$seoUrl = $seoEncoder->getStaticUrl( $seoUrl = $seoEncoder->getStaticUrl(
$this->d3GetMockableOxNewObject(FrontendController::class)->getViewConfig()->getSelfLink() . d3GetOxidDIC()->get('d3ox.webauthn.'.FrontendController::class)->getViewConfig()->getSelfLink() .
"cl=".$this->stdClassName "cl=".$this->stdClassName
); );
@ -233,10 +236,10 @@ class Actions
*/ */
public function createSeoUrl() public function createSeoUrl()
{ {
$seoEncoder = $this->d3GetMockableOxNewObject(SeoEncoder::class); $seoEncoder = d3GetOxidDIC()->get('d3ox.webauthn.'.SeoEncoder::class);
$seoEncoder->addSeoEntry( $seoEncoder->addSeoEntry(
'ff57646b47249ee33c6b672741ac371a', 'ff57646b47249ee33c6b672741ac371a',
$this->d3GetMockableRegistryObject(Config::class)->getShopId(), d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId(),
0, 0,
'index.php?cl='.$this->stdClassName, 'index.php?cl='.$this->stdClassName,
$this->seo_de, $this->seo_de,
@ -245,7 +248,7 @@ class Actions
); );
$seoEncoder->addSeoEntry( $seoEncoder->addSeoEntry(
'ff57646b47249ee33c6b672741ac371a', 'ff57646b47249ee33c6b672741ac371a',
$this->d3GetMockableRegistryObject(Config::class)->getShopId(), d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId(),
1, 1,
'index.php?cl='.$this->stdClassName, 'index.php?cl='.$this->stdClassName,
$this->seo_en, $this->seo_en,

View File

@ -13,6 +13,7 @@
declare(strict_types=1); declare(strict_types=1);
use D3\DIContainerHandler\definitionFileContainer;
use D3\Webauthn\Application\Controller\Admin\d3user_webauthn; use D3\Webauthn\Application\Controller\Admin\d3user_webauthn;
use D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin; use D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin;
use D3\Webauthn\Application\Controller\d3_account_webauthn; use D3\Webauthn\Application\Controller\d3_account_webauthn;
@ -33,6 +34,7 @@ use D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController;
use D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController; use D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController;
use D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController; use D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use D3\Webauthn\Modules\WebauthnServices;
use OxidEsales\Eshop\Application\Component\UserComponent; use OxidEsales\Eshop\Application\Component\UserComponent;
use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Application\Controller\AccountController;
use OxidEsales\Eshop\Application\Controller\AccountDownloadsController; use OxidEsales\Eshop\Application\Controller\AccountDownloadsController;
@ -73,12 +75,13 @@ $aModule = [
'email' => 'support@shopmodule.com', 'email' => 'support@shopmodule.com',
'url' => 'https://www.oxidmodule.com/', 'url' => 'https://www.oxidmodule.com/',
'extend' => [ 'extend' => [
UserController::class => d3_webauthn_UserController::class, UserController::class => d3_webauthn_UserController::class,
PaymentController::class => d3_webauthn_PaymentController::class, PaymentController::class => d3_webauthn_PaymentController::class,
OrderController::class => d3_webauthn_OrderController::class, OrderController::class => d3_webauthn_OrderController::class,
OxidModel\User::class => d3_User_Webauthn::class, OxidModel\User::class => d3_User_Webauthn::class,
LoginController::class => d3_LoginController_Webauthn::class, LoginController::class => d3_LoginController_Webauthn::class,
UserComponent::class => d3_webauthn_UserComponent::class, UserComponent::class => d3_webauthn_UserComponent::class,
definitionFileContainer::class => WebauthnServices::class,
/** workarounds for missing tpl blocks (https://github.com/OXID-eSales/wave-theme/pull/124) */ /** workarounds for missing tpl blocks (https://github.com/OXID-eSales/wave-theme/pull/124) */
AccountController::class => d3_AccountController_Webauthn::class, AccountController::class => d3_AccountController_Webauthn::class,

View File

@ -17,6 +17,7 @@ namespace D3\Webauthn\tests\integration;
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\Core\Database\Adapter\DatabaseInterface;
use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
@ -31,7 +32,9 @@ class passwordAdminAuthTest extends integrationTestCase
public function createTestData() public function createTestData()
{ {
$admin = DatabaseProvider::getDb()->getOne('SELECT oxid FROM oxuser WHERE oxrights = \'malladmin\''); /** @var DatabaseInterface $db */
$db = d3GetOxidDIC()->get('d3ox.webauthn.'.DatabaseInterface::class.'.assoc');
$admin = $db->getOne('SELECT oxid FROM oxuser WHERE oxrights = \'malladmin\'');
Registry::getSession()->setVariable(WebauthnConf::OXID_ADMIN_AUTH, $admin); Registry::getSession()->setVariable(WebauthnConf::OXID_ADMIN_AUTH, $admin);
$this->createUser( $this->createUser(
$this->userList[1], $this->userList[1],

View File

@ -21,9 +21,10 @@ 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 D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use Exception; use Exception;
use Generator;
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;
@ -31,7 +32,7 @@ use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
class d3user_webauthnTest extends TestCase class d3user_webauthnTest extends WAUnitTestCase
{ {
use IsMockable; use IsMockable;
use CanAccessRestricted; use CanAccessRestricted;
@ -58,6 +59,7 @@ class d3user_webauthnTest extends TestCase
->onlyMethods(['isAvailable']) ->onlyMethods(['isAvailable'])
->getMock(); ->getMock();
$webauthnMock->method('isAvailable')->willReturn(false); $webauthnMock->method('isAvailable')->willReturn(false);
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var d3_User_Webauthn|MockObject $userMock */ /** @var d3_User_Webauthn|MockObject $userMock */
$userMock = $this->getMockBuilder(d3_User_Webauthn::class) $userMock = $this->getMockBuilder(d3_User_Webauthn::class)
@ -65,30 +67,17 @@ class d3user_webauthnTest extends TestCase
->getMock(); ->getMock();
$userMock->expects($this->atLeastOnce())->method('load')->with('editObjectId')->willReturn($canLoadUser); $userMock->expects($this->atLeastOnce())->method('load')->with('editObjectId')->willReturn($canLoadUser);
$userMock->method('getId')->willReturn('editObjectId'); $userMock->method('getId')->willReturn('editObjectId');
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var d3user_webauthn|MockObject $sutMock */ /** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class) $sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([ ->onlyMethods([
'd3CallMockableFunction', 'd3CallMockableFunction',
'getEditObjectId', 'getEditObjectId'
'd3GetMockableOxNewObject',
]) ])
->getMock(); ->getMock();
$sutMock->method('d3CallMockableFunction')->willReturn(true); $sutMock->method('d3CallMockableFunction')->willReturn(true);
$sutMock->method('getEditObjectId')->willReturn('editObjectId'); $sutMock->method('getEditObjectId')->willReturn('editObjectId');
$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,
@ -110,14 +99,12 @@ class d3user_webauthnTest extends TestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canRenderDataProvider(): array public function canRenderDataProvider(): Generator
{ {
return [ yield 'can load user' => [true];
'can load user' => [true], yield 'can not load user' => [false];
'can not load user' => [false],
];
} }
/** /**
@ -133,35 +120,23 @@ class d3user_webauthnTest extends TestCase
->onlyMethods(['redirect']) ->onlyMethods(['redirect'])
->getMock(); ->getMock();
$utilsMock->expects($this->never())->method('redirect')->willReturn(true); $utilsMock->expects($this->never())->method('redirect')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('error')->willReturn(true);
$loggerMock->expects($this->never())->method('debug')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var d3user_webauthn|MockObject $sutMock */ /** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class) $sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([ ->onlyMethods([
'setPageType', 'setPageType',
'setAuthnRegister', 'setAuthnRegister'
'd3GetMockableLogger',
'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('d3GetMockableLogger')->willReturn($loggerMock);
$sutMock->expects($this->never())->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sutMock, $sutMock,
@ -182,35 +157,23 @@ class d3user_webauthnTest extends TestCase
->onlyMethods(['redirect']) ->onlyMethods(['redirect'])
->getMock(); ->getMock();
$utilsMock->expects($this->once())->method('redirect')->willReturn(true); $utilsMock->expects($this->once())->method('redirect')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var d3user_webauthn|MockObject $sutMock */ /** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class) $sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([ ->onlyMethods([
'setPageType', 'setPageType',
'setAuthnRegister', 'setAuthnRegister'
'd3GetMockableLogger',
'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('d3GetMockableLogger')->willReturn($loggerMock);
$sutMock->expects($this->atLeastOnce())->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sutMock, $sutMock,
@ -227,34 +190,24 @@ class d3user_webauthnTest extends TestCase
public function canSaveAuthnHasError() public function canSaveAuthnHasError()
{ {
$_POST['error'] = 'msg'; $_POST['error'] = 'msg';
$_GET['error'] = 'msg';
$_REQUEST['error'] = 'msg';
/** @var UtilsView|MockObject $utilsViewMock */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = oxNew(d3user_webauthn::class);
->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger'])
->getMock();
$oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$oControllerMock->expects($this->atLeastOnce())->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->callMethod( $this->callMethod(
$oControllerMock, $oControllerMock,
@ -278,39 +231,17 @@ class d3user_webauthnTest extends TestCase
->onlyMethods(['saveAuthn']) ->onlyMethods(['saveAuthn'])
->getMock(); ->getMock();
$webauthnMock->expects($this->once())->method('saveAuthn'); $webauthnMock->expects($this->once())->method('saveAuthn');
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var UtilsView|MockObject $utilsViewMock */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->never())->method('addErrorToDisplay'); $utilsViewMock->expects($this->never())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = oxNew(d3user_webauthn::class);
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock();
$oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webauthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$oControllerMock, $oControllerMock,
@ -337,45 +268,23 @@ class d3user_webauthnTest extends TestCase
->getMock(); ->getMock();
$webauthnMock->expects($this->once())->method('saveAuthn') $webauthnMock->expects($this->once())->method('saveAuthn')
->willThrowException(oxNew($excClass)); ->willThrowException(oxNew($excClass));
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var UtilsView|MockObject $utilsViewMock */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = oxNew(d3user_webauthn::class);
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3GetMockableLogger'])
->getMock();
$oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webauthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->callMethod( $this->callMethod(
$oControllerMock, $oControllerMock,
@ -433,22 +342,12 @@ class d3user_webauthnTest extends TestCase
} else { } else {
$webAuthnMock->method('getCreationOptions')->willReturn('options'); $webAuthnMock->method('getCreationOptions')->willReturn('options');
} }
d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock);
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser']) ->onlyMethods(['addTplParam', 'getUser'])
->getMock(); ->getMock();
$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));
@ -489,30 +388,19 @@ class d3user_webauthnTest extends TestCase
'oxpassword' => 'foo', 'oxpassword' => 'foo',
] ]
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $oUser);
/** @var PublicKeyCredentialList|MockObject $publicKeyCredentialListMock */ /** @var PublicKeyCredentialList|MockObject $publicKeyCredentialListMock */
$publicKeyCredentialListMock = $this->getMockBuilder(PublicKeyCredentialList::class) $publicKeyCredentialListMock = $this->getMockBuilder(PublicKeyCredentialList::class)
->onlyMethods(['getAllFromUser']) ->onlyMethods(['getAllFromUser'])
->getMock(); ->getMock();
$publicKeyCredentialListMock->method('getAllFromUser')->with($oUser)->willReturnSelf(); $publicKeyCredentialListMock->method('getAllFromUser')->with($oUser)->willReturnSelf();
d3GetOxidDIC()->set(PublicKeyCredentialList::class, $publicKeyCredentialListMock);
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetMockableOxNewObject']) ->onlyMethods([])
->getMock(); ->getMock();
$oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($oUser, $publicKeyCredentialListMock) {
$args = func_get_args();
switch ($args[0]) {
case User::class:
return $oUser;
case PublicKeyCredentialList::class:
return $publicKeyCredentialListMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertIsArray( $this->assertIsArray(
$this->callMethod( $this->callMethod(
@ -540,22 +428,12 @@ class d3user_webauthnTest extends TestCase
->getMock(); ->getMock();
$publicKeyCredentialMock->expects($expected)->method('delete')->with($this->identicalTo($deleteId)) $publicKeyCredentialMock->expects($expected)->method('delete')->with($this->identicalTo($deleteId))
->willReturn(true); ->willReturn(true);
d3GetOxidDIC()->set(PublicKeyCredential::class, $publicKeyCredentialMock);
/** @var d3user_webauthn|MockObject $oControllerMock */ /** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class) $oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['d3GetMockableOxNewObject']) ->onlyMethods([])
->getMock(); ->getMock();
$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');
} }

View File

@ -24,6 +24,7 @@ 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 Generator;
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;
@ -81,6 +82,7 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
[WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH, $auth], [WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH, $auth],
[WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER, $userFromLogin], [WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER, $userFromLogin],
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Utils|MockObject $utilsMock */ /** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class) $utilsMock = $this->getMockBuilder(Utils::class)
@ -88,48 +90,26 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
->getMock(); ->getMock();
$utilsMock->expects($startRedirect ? $this->once() : $this->never()) $utilsMock->expects($startRedirect ? $this->once() : $this->never())
->method('redirect')->with('index.php?cl='.$redirectController)->willReturn(true); ->method('redirect')->with('index.php?cl='.$redirectController)->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var WebauthnAfterLogin|MockObject $afterLoginMock */ /** @var WebauthnAfterLogin|MockObject $afterLoginMock */
$afterLoginMock = $this->getMockBuilder(WebauthnAfterLogin::class) $afterLoginMock = $this->getMockBuilder(WebauthnAfterLogin::class)
->onlyMethods(['changeLanguage']) ->onlyMethods(['changeLanguage'])
->getMock(); ->getMock();
$afterLoginMock->expects($this->once())->method('changeLanguage'); $afterLoginMock->expects($this->once())->method('changeLanguage');
d3GetOxidDIC()->set(WebauthnAfterLogin::class, $afterLoginMock);
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction', 'd3GetMockableOxNewObject', ->onlyMethods(['d3CallMockableFunction',
'generateCredentialRequest', 'addTplParam', ]) 'generateCredentialRequest', 'addTplParam', ])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl'); $sut->method('d3CallMockableFunction')->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('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',
@ -141,15 +121,13 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
} }
/** /**
* @return array * @return Generator
*/ */
public function canRenderDataProvider(): array public function canRenderDataProvider(): Generator
{ {
return [ yield 'has request' => [false, true, false, 'start'];
'has request' => [false, true, false, 'start'], yield 'has auth' => [true, true, true, 'admin_start'];
'has auth' => [true, true, true, 'admin_start'], yield 'missing user' => [false, false, true, 'login'];
'missing user' => [false, false, true, 'login'],
];
} }
/** /**
@ -234,34 +212,14 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
$requestMock = $this->getMockBuilder(Request::class) $requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestEscapedParameter']) ->onlyMethods(['getRequestEscapedParameter'])
->getMock(); ->getMock();
$requestMock->expects($this->exactly(3))->method('getRequestEscapedParameter')->willReturn('abc'); $requestMock->expects($this->exactly(1))->method('getRequestEscapedParameter')->willReturn('abc');
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var d3webauthnadminlogin|MockObject $sut */ /** @var d3webauthnadminlogin|MockObject $sut */
$sut = $this->getMockBuilder(d3webauthnadminlogin::class) $sut = $this->getMockBuilder(d3webauthnadminlogin::class)
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->onlyMethods(['getWebAuthnLogin'])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback( $sut->method('getWebAuthnLogin')->willReturn($loginMock);
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',
@ -285,31 +243,20 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->once())->method('addErrorToDisplay'); $utilsViewMock->expects($this->once())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var Request|MockObject $requestMock */ /** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(Request::class) $requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestEscapedParameter']) ->onlyMethods(['getRequestEscapedParameter'])
->getMock(); ->getMock();
$requestMock->expects($this->atLeast(2))->method('getRequestEscapedParameter')->willReturn('abc'); $requestMock->expects($this->atLeast(0))->method('getRequestEscapedParameter')->willReturn('abc');
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var d3webauthnadminlogin|MockObject $sut */ /** @var d3webauthnadminlogin|MockObject $sut */
$sut = $this->getMockBuilder(d3webauthnadminlogin::class) $sut = $this->getMockBuilder(d3webauthnadminlogin::class)
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->onlyMethods(['getWebAuthnLogin'])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willThrowException(oxNew(WebauthnGetException::class)); $sut->method('getWebAuthnLogin')->willThrowException(oxNew(WebauthnGetException::class));
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock, $requestMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
case Request::class:
return $requestMock;
default:
return Registry::get($args[0]);
}
}
);
$this->assertSame( $this->assertSame(
'login', 'login',
@ -321,15 +268,39 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
} }
/** /**
* @return array * @return Generator
*/ */
public function canAssertAuthnCookieSubshopDataProvider(): array public function canAssertAuthnCookieSubshopDataProvider(): Generator
{ {
return [ yield 'missing cookie' => ['login', true, null, 'user'];
'missing cookie' => ['login', true, null, 'user'], yield 'no admin user' => ['login', true, 'cookie', 'user'];
'no admin user' => ['login', true, 'cookie', 'user'], yield 'assertion succ malladmin' => ['admin_start', false, 'cookie', 'malladmin'];
'assertion succ malladmin' => ['admin_start', false, 'cookie', 'malladmin'], yield 'assertion succ shop1' => ['admin_start', false, 'cookie', 1];
'assertion succ shop1' => ['admin_start', false, 'cookie', 1], }
];
/**
* @test
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::getWebAuthnLogin
*/
public function canGetWebAuthnLogin()
{
/** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestEscapedParameter'])
->getMock();
$requestMock->method('getRequestEscapedParameter')->willReturn('requestReturn');
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var d3webauthnadminlogin|MockObject $sut */
$sut = oxNew(d3webauthnadminlogin::class);
$this->assertInstanceOf(
WebauthnLogin::class,
$this->callMethod(
$sut,
'getWebAuthnLogin'
)
);
} }
} }

View File

@ -21,15 +21,17 @@ 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\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Model\User; 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\UtilsView; use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
class d3_account_webauthnTest extends UnitTestCase class d3_account_webauthnTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -77,23 +79,13 @@ class d3_account_webauthnTest extends UnitTestCase
->onlyMethods(['isAvailable']) ->onlyMethods(['isAvailable'])
->getMock(); ->getMock();
$webAuthnMock->expects($this->atLeastOnce())->method('isAvailable')->willReturn(true); $webAuthnMock->expects($this->atLeastOnce())->method('isAvailable')->willReturn(true);
d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock);
/** @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', 'd3GetMockableOxNewObject']) ->onlyMethods(['getUser'])
->getMock(); ->getMock();
$oControllerMock->method('getUser')->willReturn($oUser); $oControllerMock->method('getUser')->willReturn($oUser);
$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;
@ -119,23 +111,13 @@ class d3_account_webauthnTest extends UnitTestCase
->onlyMethods(['isAvailable']) ->onlyMethods(['isAvailable'])
->getMock(); ->getMock();
$webAuthnMock->expects($this->atLeastOnce())->method('isAvailable')->willReturn(true); $webAuthnMock->expects($this->atLeastOnce())->method('isAvailable')->willReturn(true);
d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock);
/** @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', 'd3GetMockableOxNewObject']) ->onlyMethods(['getUser'])
->getMock(); ->getMock();
$oControllerMock->method('getUser')->willReturn($oUser); $oControllerMock->method('getUser')->willReturn($oUser);
$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;
@ -167,23 +149,13 @@ class d3_account_webauthnTest extends UnitTestCase
->onlyMethods(['getAllFromUser']) ->onlyMethods(['getAllFromUser'])
->getMock(); ->getMock();
$publicKeyCredentialListMock->method('getAllFromUser')->with($oUser)->willReturnSelf(); $publicKeyCredentialListMock->method('getAllFromUser')->with($oUser)->willReturnSelf();
d3GetOxidDIC()->set(PublicKeyCredentialList::class, $publicKeyCredentialListMock);
/** @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', 'd3GetMockableOxNewObject']) ->onlyMethods(['getUser'])
->getMock(); ->getMock();
$oControllerMock->method('getUser')->willReturn($oUser); $oControllerMock->method('getUser')->willReturn($oUser);
$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;
@ -216,15 +188,15 @@ class d3_account_webauthnTest extends UnitTestCase
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('error')->willReturn(true);
$loggerMock->expects($this->never())->method('debug')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @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', 'd3GetMockableLogger']) ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser'])
->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('d3GetMockableLogger')->willReturn($loggerMock);
$this->_oController = $oControllerMock; $this->_oController = $oControllerMock;
@ -254,16 +226,16 @@ class d3_account_webauthnTest extends UnitTestCase
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @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', 'd3GetMockableLogger']) ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser'])
->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('d3GetMockableLogger')->willReturn($loggerMock);
$this->_oController = $oControllerMock; $this->_oController = $oControllerMock;
@ -292,22 +264,12 @@ class d3_account_webauthnTest extends UnitTestCase
} else { } else {
$webAuthnMock->method('getCreationOptions')->willReturn('options'); $webAuthnMock->method('getCreationOptions')->willReturn('options');
} }
d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock);
/** @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(['d3GetMockableOxNewObject', 'addTplParam', 'getUser']) ->onlyMethods(['addTplParam', 'getUser'])
->getMock(); ->getMock();
$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));
@ -376,28 +338,25 @@ class d3_account_webauthnTest extends UnitTestCase
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('error')->willReturn(true);
$loggerMock->expects($this->never())->method('debug')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var Request|MockObject $requestMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger']) ->onlyMethods(['getRequestEscapedParameter'])
->getMock(); ->getMock();
$oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( $requestMock->method('getRequestEscapedParameter')->with(
function () use ($utilsViewMock) { $this->identicalTo('error')
$args = func_get_args(); )->willReturn('errorMsg');
switch ($args[0]) { d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
case UtilsView::class:
return $utilsViewMock; /** @var d3_account_webauthn $oControllerMock */
default: $oControllerMock = oxNew(d3_account_webauthn::class);
return Registry::get($args[0]);
}
}
);
$oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->_oController = $oControllerMock; $this->_oController = $oControllerMock;
@ -423,39 +382,17 @@ class d3_account_webauthnTest extends UnitTestCase
->onlyMethods(['saveAuthn']) ->onlyMethods(['saveAuthn'])
->getMock(); ->getMock();
$webauthnMock->expects($this->once())->method('saveAuthn'); $webauthnMock->expects($this->once())->method('saveAuthn');
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var UtilsView|MockObject $utilsViewMock */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->never())->method('addErrorToDisplay'); $utilsViewMock->expects($this->never())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = oxNew(d3_account_webauthn::class);
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock();
$oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webauthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$this->_oController = $oControllerMock; $this->_oController = $oControllerMock;
@ -482,39 +419,17 @@ class d3_account_webauthnTest extends UnitTestCase
->getMock(); ->getMock();
$webauthnMock->expects($this->once())->method('saveAuthn') $webauthnMock->expects($this->once())->method('saveAuthn')
->willThrowException(oxNew(WebauthnException::class)); ->willThrowException(oxNew(WebauthnException::class));
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var UtilsView|MockObject $utilsViewMock */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = oxNew(d3_account_webauthn::class);
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock();
$oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webauthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$this->_oController = $oControllerMock; $this->_oController = $oControllerMock;
@ -541,22 +456,10 @@ class d3_account_webauthnTest extends UnitTestCase
->getMock(); ->getMock();
$publicKeyCredentialMock->expects($expected)->method('delete')->with($this->identicalTo($deleteId)) $publicKeyCredentialMock->expects($expected)->method('delete')->with($this->identicalTo($deleteId))
->willReturn(true); ->willReturn(true);
d3GetOxidDIC()->set(PublicKeyCredential::class, $publicKeyCredentialMock);
/** @var d3_account_webauthn|MockObject $oControllerMock */ /** @var d3_account_webauthn $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) $oControllerMock = oxNew(d3_account_webauthn::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock();
$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;

View File

@ -20,6 +20,8 @@ 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 D3\Webauthn\tests\unit\WAUnitTestCase;
use Generator;
use OxidEsales\Eshop\Core\Registry; 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;
@ -28,7 +30,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
class d3webauthnloginTest extends UnitTestCase class d3webauthnloginTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -49,22 +51,12 @@ class d3webauthnloginTest extends UnitTestCase
$sessionMock->method('getVariable')->willReturn([ $sessionMock->method('getVariable')->willReturn([
'key1' => 'variable1', 'key1' => 'variable1',
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction']) ->onlyMethods(['d3CallMockableFunction'])
->getMock(); ->getMock();
$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('d3CallMockableFunction')->willReturn(['defKey1' => 'devValues1']); $sut->method('d3CallMockableFunction')->willReturn(['defKey1' => 'devValues1']);
$this->assertSame( $this->assertSame(
@ -101,6 +93,7 @@ class d3webauthnloginTest extends UnitTestCase
[WebauthnConf::WEBAUTHN_SESSION_AUTH, $auth], [WebauthnConf::WEBAUTHN_SESSION_AUTH, $auth],
[WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $userFromLogin], [WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $userFromLogin],
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Utils|MockObject $utilsMock */ /** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class) $utilsMock = $this->getMockBuilder(Utils::class)
@ -108,25 +101,13 @@ class d3webauthnloginTest extends UnitTestCase
->getMock(); ->getMock();
$utilsMock->expects($startRedirect ? $this->once() : $this->never()) $utilsMock->expects($startRedirect ? $this->once() : $this->never())
->method('redirect')->with('index.php?cl='.$redirectController)->willReturn(true); ->method('redirect')->with('index.php?cl='.$redirectController)->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction', ->onlyMethods(['d3CallMockableFunction',
'generateCredentialRequest', 'addTplParam', ]) 'generateCredentialRequest', 'addTplParam', ])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl'); $sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl');
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('generateCredentialRequest'); ->method('generateCredentialRequest');
@ -143,15 +124,13 @@ class d3webauthnloginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canRenderDataProvider(): array public function canRenderDataProvider(): Generator
{ {
return [ yield 'has request' => [false, true, false, 'start'];
'has request' => [false, true, false, 'start'], yield 'has auth' => [true, true, true, 'start'];
'has auth' => [true, true, true, 'start'], yield 'missing user' => [false, false, true, 'start'];
'missing user' => [false, false, true, 'start'],
];
} }
/** /**
@ -168,6 +147,7 @@ class d3webauthnloginTest extends UnitTestCase
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('error')->willReturn(true);
$loggerMock->expects($this->never())->method('debug')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
@ -176,6 +156,7 @@ class d3webauthnloginTest extends UnitTestCase
$sessionMock->method('getVariable')->willReturnMap([ $sessionMock->method('getVariable')->willReturnMap([
[$userSessionVarName, $currUserFixture], [$userSessionVarName, $currUserFixture],
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Webauthn|MockObject $webAuthnMock */ /** @var Webauthn|MockObject $webAuthnMock */
$webAuthnMock = $this->getMockBuilder(Webauthn::class) $webAuthnMock = $this->getMockBuilder(Webauthn::class)
@ -183,36 +164,14 @@ class d3webauthnloginTest extends UnitTestCase
->getMock(); ->getMock();
$webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture) $webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture)
->willReturn('success'); ->willReturn('success');
d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock);
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableOxNewObject', 'addTplParam', 'd3GetMockableLogger']) ->onlyMethods(['addTplParam'])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webAuthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webAuthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->expects($this->atLeast(2)) $sut->expects($this->atLeast(2))
->method('addTplParam')->willReturn(true); ->method('addTplParam')->willReturn(true);
$sut->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -234,6 +193,7 @@ class d3webauthnloginTest extends UnitTestCase
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
@ -244,6 +204,7 @@ class d3webauthnloginTest extends UnitTestCase
]); ]);
$sessionMock->expects($this->once())->method('setVariable')->with(WebauthnConf::GLOBAL_SWITCH) $sessionMock->expects($this->once())->method('setVariable')->with(WebauthnConf::GLOBAL_SWITCH)
->willReturn(true); ->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Webauthn|MockObject $webAuthnMock */ /** @var Webauthn|MockObject $webAuthnMock */
$webAuthnMock = $this->getMockBuilder(Webauthn::class) $webAuthnMock = $this->getMockBuilder(Webauthn::class)
@ -251,6 +212,7 @@ class d3webauthnloginTest extends UnitTestCase
->getMock(); ->getMock();
$webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture) $webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture)
->willThrowException(oxNew(WebauthnException::class, 'foobar0')); ->willThrowException(oxNew(WebauthnException::class, 'foobar0'));
d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock);
/** @var Utils|MockObject $utilsMock */ /** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class) $utilsMock = $this->getMockBuilder(Utils::class)
@ -258,39 +220,14 @@ class d3webauthnloginTest extends UnitTestCase
->getMock(); ->getMock();
$utilsMock->expects($this->once())->method('redirect') $utilsMock->expects($this->once())->method('redirect')
->with('index.php?cl='.$redirectClass)->willReturn(true); ->with('index.php?cl='.$redirectClass)->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', ->onlyMethods(['addTplParam'])
'd3GetMockableLogger', 'd3GetMockableRegistryObject', ])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webAuthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webAuthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->expects($this->never()) $sut->expects($this->never())
->method('addTplParam')->willReturn(true); ->method('addTplParam')->willReturn(true);
$sut->expects($this->atLeast(2))->method('d3GetMockableLogger')->willReturn($loggerMock);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -315,22 +252,10 @@ class d3webauthnloginTest extends UnitTestCase
$sessionMock->method('getVariable')->willReturnMap([ $sessionMock->method('getVariable')->willReturnMap([
[$sessionVarName, $currClassFixture], [$sessionVarName, $currClassFixture],
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var d3webauthnlogin|MockObject $sut */ /** @var d3webauthnlogin $sut */
$sut = $this->getMockBuilder($this->sutClassName) $sut = oxNew($this->sutClassName);
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock();
$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,

View File

@ -19,6 +19,8 @@ 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 D3\Webauthn\Application\Model\UserEntity; use D3\Webauthn\Application\Model\UserEntity;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use Hoa\Iterator\Mock;
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\Eshop\Core\Registry;
@ -27,7 +29,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
use Webauthn\PublicKeyCredentialSource; use Webauthn\PublicKeyCredentialSource;
class PublicKeyCredentialListTest extends UnitTestCase class PublicKeyCredentialListTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -73,27 +75,12 @@ class PublicKeyCredentialListTest extends UnitTestCase
->onlyMethods(['getShopId']) ->onlyMethods(['getShopId'])
->getMock(); ->getMock();
$configMock->method('getShopId')->willReturn(55); $configMock->method('getShopId')->willReturn(55);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var PublicKeyCredentialList|MockObject $sut */ /** @var PublicKeyCredentialList|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredentialList::class) $sut = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetMockableRegistryObject']) ->onlyMethods(['findAllForUserEntity'])
->getMock();
$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 PublicKeyCredentialSource|MockObject $pkcsMock */
$pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class)
->disableOriginalConstructor()
->getMock(); ->getMock();
if ($doCreate) { if ($doCreate) {
@ -106,22 +93,35 @@ class PublicKeyCredentialListTest extends UnitTestCase
$pkc->assign([ $pkc->assign([
'credentialid' => base64_encode('myCredentialId'), 'credentialid' => base64_encode('myCredentialId'),
'oxshopid' => 55, 'oxshopid' => 55,
'name' => __METHOD__,
// can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable
'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9'
]); ]);
$pkc->setCredential($pkcsMock);
$pkc->save(); $pkc->save();
} }
$this->assertEquals( try {
$expected === 'pkcsource' ? $pkcsMock : $expected, $return = $this->callMethod(
$this->callMethod(
$sut, $sut,
'findOneByCredentialId', 'findOneByCredentialId',
['myCredentialId'] ['myCredentialId']
) );
);
if ($doCreate) { if ($expected === 'pkcsource') {
$pkc->delete($oxid); $this->assertInstanceOf(
PublicKeyCredentialSource::class,
$return
);
} else {
$this->assertEquals(
$expected,
$return
);
}
} finally {
if ($doCreate) {
$pkc->delete($oxid);
}
} }
} }
@ -154,26 +154,12 @@ class PublicKeyCredentialListTest extends UnitTestCase
->onlyMethods(['getShopId']) ->onlyMethods(['getShopId'])
->getMock(); ->getMock();
$configMock->method('getShopId')->willReturn(55); $configMock->method('getShopId')->willReturn(55);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var PublicKeyCredentialList|MockObject $sut */ /** @var PublicKeyCredentialList|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredentialList::class) $sut = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetMockableRegistryObject']) ->onlyMethods(['findOneByCredentialId'])
->getMock();
$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]);
}
}
);
$pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class)
->disableOriginalConstructor()
->getMock(); ->getMock();
if ($doCreate) { if ($doCreate) {
@ -186,8 +172,10 @@ class PublicKeyCredentialListTest extends UnitTestCase
$pkc->assign([ $pkc->assign([
'oxuserid' => 'userid', 'oxuserid' => 'userid',
'oxshopid' => 55, 'oxshopid' => 55,
'credentialid' => __METHOD__,
// can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable
'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9'
]); ]);
$pkc->setCredential($pkcsMock);
$pkc->save(); $pkc->save();
} }
} }
@ -199,21 +187,31 @@ class PublicKeyCredentialListTest extends UnitTestCase
->getMock(); ->getMock();
$pkcUserEntity->method('getId')->willReturn('userid'); $pkcUserEntity->method('getId')->willReturn('userid');
$this->assertEquals( try {
$expected === 'pkcsource' ? [$pkcsMock, $pkcsMock] : [], $list = $this->callMethod(
$this->callMethod(
$sut, $sut,
'findAllForUserEntity', 'findAllForUserEntity',
[$pkcUserEntity] [$pkcUserEntity]
) );
);
if ($doCreate) { if ($expected === 'pkcsource') {
foreach ($oxids as $oxid) { $this->assertCount( 2, $list );
$pkc = $this->getMockBuilder(PublicKeyCredential::class) foreach ( $list as $item ) {
->onlyMethods(['allowDerivedDelete']) $this->assertInstanceOf( PublicKeyCredentialSource::class, $item );
->getMock(); }
$pkc->delete($oxid); } else {
$this->assertEmpty($list);
}
} finally {
if ($doCreate) {
foreach ($oxids as $oxid) {
/** @var PublicKeyCredential $pkc */
$pkc = $this->getMockBuilder(PublicKeyCredential::class)
->onlyMethods(['allowDerivedDelete'])
->getMock();
$pkc->method('allowDerivedDelete')->willReturn(true);
$pkc->delete($oxid);
}
} }
} }
} }
@ -237,23 +235,13 @@ class PublicKeyCredentialListTest extends UnitTestCase
->onlyMethods(['getShopId']) ->onlyMethods(['getShopId'])
->getMock(); ->getMock();
$configMock->method('getShopId')->willReturn(55); $configMock->method('getShopId')->willReturn(55);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var PublicKeyCredentialList|MockObject $sut */ /** @var PublicKeyCredentialList|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredentialList::class) $sut = $this->getMockBuilder(PublicKeyCredentialList::class)
->onlyMethods(['saveCredentialSource'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->getMock();
$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)
@ -263,9 +251,6 @@ class PublicKeyCredentialListTest extends UnitTestCase
$userMock->method('getId')->willReturn('userid'); $userMock->method('getId')->willReturn('userid');
if ($doCreate) { if ($doCreate) {
$pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class)
->disableOriginalConstructor()
->getMock();
foreach ($oxids as $oxid) { foreach ($oxids as $oxid) {
$pkc = $this->getMockBuilder(PublicKeyCredential::class) $pkc = $this->getMockBuilder(PublicKeyCredential::class)
->onlyMethods(['allowDerivedDelete']) ->onlyMethods(['allowDerivedDelete'])
@ -275,28 +260,35 @@ class PublicKeyCredentialListTest extends UnitTestCase
$pkc->assign([ $pkc->assign([
'oxuserid' => 'userid', 'oxuserid' => 'userid',
'oxshopid' => 55, 'oxshopid' => 55,
'credentialid' => __METHOD__,
// can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable
'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9'
]); ]);
$pkc->setCredential($pkcsMock);
$pkc->save(); $pkc->save();
} }
} }
$return = $this->callMethod( try {
$sut, $return = $this->callMethod(
'getAllFromUser', $sut,
[$userMock] 'getAllFromUser',
); [$userMock]
);
if ($doCreate) { } finally {
foreach ($oxids as $oxid) { if ($doCreate) {
$pkc = $this->getMockBuilder(PublicKeyCredential::class) foreach ($oxids as $oxid) {
->onlyMethods(['allowDerivedDelete']) /** @var PublicKeyCredential|MockObject $pkc */
->getMock(); $pkc = $this->getMockBuilder(PublicKeyCredential::class)
$pkc->delete($oxid); ->onlyMethods(['allowDerivedDelete'])
->getMock();
$pkc->method('allowDerivedDelete')->willReturn(true);
$pkc->delete($oxid);
}
} }
} }
$this->assertInstanceOf(PublicKeyCredentialList::class, $return); $this->assertInstanceOf(PublicKeyCredentialList::class, $return);
$this->assertCount($expectedCount, $return); $this->assertCount($expectedCount, $return);
} }

View File

@ -18,6 +18,7 @@ namespace D3\Webauthn\tests\unit\Application\Model\Credential;
use D3\TestingTools\Development\CanAccessRestricted; 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 D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
@ -25,7 +26,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
use Webauthn\PublicKeyCredentialSource; use Webauthn\PublicKeyCredentialSource;
class PublicKeyCredentialTest extends UnitTestCase class PublicKeyCredentialTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -193,16 +194,20 @@ class PublicKeyCredentialTest extends UnitTestCase
*/ */
public function canGetCredential() public function canGetCredential()
{ {
/** @var PublicKeyCredentialSource $publicKeyCredentialSourceMock */ // can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable
$publicKeyCredentialSourceMock = $this->getMockBuilder(PublicKeyCredentialSource::class)
->disableOriginalConstructor()
->getMock();
$this->canGetField( /** @var PublicKeyCredential $sut */
'credential', $sut = oxNew(PublicKeyCredential::class);
'getCredential', $sut->assign([
base64_encode(serialize($publicKeyCredentialSourceMock)), 'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9',
$publicKeyCredentialSourceMock ]);
$this->assertInstanceOf(
PublicKeyCredentialSource::class,
$this->callMethod(
$sut,
'getCredential'
)
); );
} }
@ -231,22 +236,12 @@ class PublicKeyCredentialTest extends UnitTestCase
$pkcListObjectMock->method('findOneByCredentialId')->willReturn( $pkcListObjectMock->method('findOneByCredentialId')->willReturn(
$credAlreadyExist ? $publicKeyCredentialSourceMock : null $credAlreadyExist ? $publicKeyCredentialSourceMock : null
); );
d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pkcListObjectMock);
/** @var PublicKeyCredential|MockObject $sut */ /** @var PublicKeyCredential|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredential::class) $sut = $this->getMockBuilder(PublicKeyCredential::class)
->onlyMethods(['d3GetMockableOxNewObject', 'exists', 'getIdByCredentialId', 'load', 'save']) ->onlyMethods(['exists', 'getIdByCredentialId', 'load', 'save'])
->getMock(); ->getMock();
$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');
@ -296,22 +291,12 @@ class PublicKeyCredentialTest extends UnitTestCase
->onlyMethods(['getShopId']) ->onlyMethods(['getShopId'])
->getMock(); ->getMock();
$configMock->method('getShopId')->willReturn($shopId); $configMock->method('getShopId')->willReturn($shopId);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var PublicKeyCredential|MockObject $sut */ /** @var PublicKeyCredential|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredential::class) $sut = $this->getMockBuilder(PublicKeyCredential::class)
->onlyMethods(['d3GetMockableRegistryObject', 'allowDerivedDelete']) ->onlyMethods(['allowDerivedDelete'])
->getMock(); ->getMock();
$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) {
@ -323,17 +308,19 @@ class PublicKeyCredentialTest extends UnitTestCase
$sut->save(); $sut->save();
} }
$this->assertSame( try {
$expected, $this->assertSame(
$this->callMethod( $expected,
$sut, $this->callMethod(
'getIdByCredentialId', $sut,
[$pkcId] 'getIdByCredentialId',
) [$pkcId]
); )
);
if ($doCreate) { } finally {
$sut->delete($oxid); if ($doCreate) {
$sut->delete($oxid);
}
} }
} }

View File

@ -17,11 +17,12 @@ namespace D3\Webauthn\tests\unit\Application\Model\Exceptions;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException; use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class WebauthnCreateExceptionTest extends UnitTestCase class WebauthnCreateExceptionTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;

View File

@ -18,12 +18,13 @@ namespace D3\Webauthn\tests\unit\Application\Model\Exceptions;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\WebauthnErrors; use D3\Webauthn\Application\Model\WebauthnErrors;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use Exception; use Exception;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class WebauthnExceptionTest extends UnitTestCase class WebauthnExceptionTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;

View File

@ -17,11 +17,12 @@ namespace D3\Webauthn\tests\unit\Application\Model\Exceptions;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class WebauthnGetExceptionTest extends UnitTestCase class WebauthnGetExceptionTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;

View File

@ -17,12 +17,13 @@ namespace D3\Webauthn\tests\unit\Application\Model;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\RelyingPartyEntity; use D3\Webauthn\Application\Model\RelyingPartyEntity;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Model\Shop; use OxidEsales\Eshop\Application\Model\Shop;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use ReflectionException; use ReflectionException;
class RelyingPartyEntityTest extends TestCase class RelyingPartyEntityTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;

View File

@ -18,12 +18,13 @@ namespace D3\Webauthn\tests\unit\Application\Model;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\UserEntity; use D3\Webauthn\Application\Model\UserEntity;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\TestingLibrary\UnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class UserEntityTest extends UnitTestCase class UserEntityTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;

View File

@ -18,6 +18,7 @@ namespace D3\Webauthn\tests\unit\Application\Model;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
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\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Core\Language; use OxidEsales\Eshop\Core\Language;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Request;
@ -28,7 +29,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
use stdClass; use stdClass;
class WebauthnAfterLoginTest extends UnitTestCase class WebauthnAfterLoginTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -53,12 +54,14 @@ class WebauthnAfterLoginTest extends UnitTestCase
$this->identicalTo('oxidadminprofile'), $this->identicalTo('oxidadminprofile'),
$this->logicalOr($this->identicalTo('1@prof@No1'), $this->identicalTo('')) $this->logicalOr($this->identicalTo('1@prof@No1'), $this->identicalTo(''))
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock);
/** @var Request|MockObject $requestMock */ /** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(Request::class) $requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestEscapedParameter']) ->onlyMethods(['getRequestEscapedParameter'])
->getMock(); ->getMock();
$requestMock->method('getRequestEscapedParameter')->willReturn($requestProfile); $requestMock->method('getRequestEscapedParameter')->willReturn($requestProfile);
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
@ -70,26 +73,10 @@ class WebauthnAfterLoginTest extends UnitTestCase
]); ]);
$sessionMock->expects($this->once())->method('deleteVariable'); $sessionMock->expects($this->once())->method('deleteVariable');
$sessionMock->expects($this->exactly((int) $setSessionVar))->method('setVariable'); $sessionMock->expects($this->exactly((int) $setSessionVar))->method('setVariable');
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var WebauthnAfterLogin|MockObject $sut */ /** @var WebauthnAfterLogin $sut */
$sut = $this->getMockBuilder(WebauthnAfterLogin::class) $sut = oxNew(WebauthnAfterLogin::class);
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock, $requestMock, $utilsServerMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
case Request::class:
return $requestMock;
case UtilsServer::class:
return $utilsServerMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -133,6 +120,7 @@ class WebauthnAfterLoginTest extends UnitTestCase
); );
$languageMock->expects($this->once())->method('setTplLanguage') $languageMock->expects($this->once())->method('setTplLanguage')
->with($this->identicalTo($expectedLang)); ->with($this->identicalTo($expectedLang));
d3GetOxidDIC()->set('d3ox.webauthn.'.Language::class, $languageMock);
/** @var UtilsServer|MockObject $utilsServerMock */ /** @var UtilsServer|MockObject $utilsServerMock */
$utilsServerMock = $this->getMockBuilder(UtilsServer::class) $utilsServerMock = $this->getMockBuilder(UtilsServer::class)
@ -142,12 +130,14 @@ class WebauthnAfterLoginTest extends UnitTestCase
$this->identicalTo('oxidadminlanguage'), $this->identicalTo('oxidadminlanguage'),
$this->identicalTo($expectedAbbr) $this->identicalTo($expectedAbbr)
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock);
/** @var Request|MockObject $requestMock */ /** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(Request::class) $requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestEscapedParameter']) ->onlyMethods(['getRequestEscapedParameter'])
->getMock(); ->getMock();
$requestMock->method('getRequestEscapedParameter')->willReturn($requestLang); $requestMock->method('getRequestEscapedParameter')->willReturn($requestLang);
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
@ -157,28 +147,10 @@ class WebauthnAfterLoginTest extends UnitTestCase
[WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE, $sessionLang], [WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE, $sessionLang],
]); ]);
$sessionMock->expects($this->once())->method('deleteVariable'); $sessionMock->expects($this->once())->method('deleteVariable');
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var WebauthnAfterLogin|MockObject $sut */ /** @var WebauthnAfterLogin $sut */
$sut = $this->getMockBuilder(WebauthnAfterLogin::class) $sut = oxNew(WebauthnAfterLogin::class);
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock, $requestMock, $utilsServerMock, $languageMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
case Request::class:
return $requestMock;
case UtilsServer::class:
return $utilsServerMock;
case Language::class:
return $languageMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,

View File

@ -17,13 +17,14 @@ namespace D3\Webauthn\tests\unit\Application\Model;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\WebauthnErrors; use D3\Webauthn\Application\Model\WebauthnErrors;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Core\Language; use OxidEsales\Eshop\Core\Language;
use OxidEsales\Eshop\Core\Registry; 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;
class WebauthnErrorsTest extends UnitTestCase class WebauthnErrorsTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -50,22 +51,12 @@ class WebauthnErrorsTest extends UnitTestCase
} }
) )
)->willReturn('translated'); )->willReturn('translated');
d3GetOxidDIC()->set('d3ox.webauthn.'.Language::class, $languageMock);
/** @var WebauthnErrors|MockObject $sut */ /** @var WebauthnErrors|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnErrors::class) $sut = $this->getMockBuilder(WebauthnErrors::class)
->onlyMethods(['d3GetMockableRegistryObject', 'getErrIdFromMessage']) ->onlyMethods(['getErrIdFromMessage'])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($languageMock) {
$args = func_get_args();
switch ($args[0]) {
case Language::class:
return $languageMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('getErrIdFromMessage')->willReturn($errId); $sut->method('getErrIdFromMessage')->willReturn($errId);
$this->assertSame( $this->assertSame(

View File

@ -22,13 +22,14 @@ use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException;
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\WebauthnLogin; use D3\Webauthn\Application\Model\WebauthnLogin;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use Generator;
use OxidEsales\Eshop\Application\Component\UserComponent; use OxidEsales\Eshop\Application\Component\UserComponent;
use OxidEsales\Eshop\Application\Model\Basket; use OxidEsales\Eshop\Application\Model\Basket;
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\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;
@ -40,7 +41,7 @@ use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
use TypeError; use TypeError;
class WebauthnLoginTest extends UnitTestCase class WebauthnLoginTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -157,15 +158,13 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array[] * @return Generator
*/ */
public function credentialErrorDataProvider(): array public function credentialErrorDataProvider(): Generator
{ {
return [ yield 'empty credential' => ['', WebauthnGetException::class, WebauthnGetException::class];
'empty credential' => ['', WebauthnGetException::class, WebauthnGetException::class], yield 'spaced credential' => [' ', WebauthnGetException::class, WebauthnGetException::class];
'spaced credential' => [' ', WebauthnGetException::class, WebauthnGetException::class], yield 'null credential' => [null, TypeError::class, WebauthnGetException::class];
'null credential' => [null, TypeError::class, WebauthnGetException::class],
];
} }
/** /**
@ -216,7 +215,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',
'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId', ]) 'getCredential', 'regenerateSessionId', ])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->expects($this->exactly((int) $setCookie))->method('setSessionCookie'); $sut->expects($this->exactly((int) $setCookie))->method('setSessionCookie');
@ -232,14 +231,12 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function frontendLoginSuccessDataProvider(): array public function frontendLoginSuccessDataProvider(): Generator
{ {
return [ yield 'setCookie' => [true];
'setCookie' => [true], yield 'dontSetCookie' => [false];
'dontSetCookie' => [false],
];
} }
/** /**
@ -259,17 +256,20 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->once())->method('addErrorToDisplay'); $utilsViewMock->expects($this->once())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
->onlyMethods(['logout']) ->onlyMethods(['logout'])
->getMock(); ->getMock();
$userMock->expects($this->once())->method('logout'); $userMock->expects($this->once())->method('logout');
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($writeLog)->method('error')->willReturn(true); $loggerMock->expects($writeLog)->method('error')->willReturn(true);
$loggerMock->expects($writeLog)->method('debug')->willReturn(true); $loggerMock->expects($writeLog)->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var UserComponent|MockObject $userComponentMock */ /** @var UserComponent|MockObject $userComponentMock */
$userComponentMock = $this->getMockBuilder(UserComponent::class) $userComponentMock = $this->getMockBuilder(UserComponent::class)
@ -279,35 +279,12 @@ 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', 'd3GetMockableRegistryObject', ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie', 'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie',
'd3GetMockableLogger', 'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId', ]) 'getCredential', 'regenerateSessionId', ])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass));
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userMock) {
$args = func_get_args();
switch ($args[0]) {
case User::class:
return $userMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->expectException(WebauthnLoginErrorException::class); $this->expectException(WebauthnLoginErrorException::class);
@ -321,16 +298,14 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array[] * @return Generator
*/ */
public function frontendLoginExceptionDataProvider(): array public function frontendLoginExceptionDataProvider(): Generator
{ {
return [ yield 'userException' => [UserException::class, $this->any(), false];
'userException' => [UserException::class, $this->any(), false], yield 'cookieException' => [CookieException::class, $this->any(), true];
'cookieException' => [CookieException::class, $this->any(), true], yield 'webauthnException' => [WebauthnException::class, $this->atLeastOnce(), false];
'webauthnException' => [WebauthnException::class, $this->atLeastOnce(), false], yield 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce(), true];
'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce(), true],
];
} }
/** /**
@ -345,25 +320,14 @@ class WebauthnLoginTest extends UnitTestCase
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @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'])
'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',
@ -389,58 +353,35 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['onAdminLogin']) ->onlyMethods(['onAdminLogin'])
->getMock(); ->getMock();
$systemEventHandlerMock->expects($this->never())->method('onAdminLogin'); $systemEventHandlerMock->expects($this->never())->method('onAdminLogin');
d3GetOxidDIC()->set('d3ox.webauthn.'.SystemEventHandler::class, $systemEventHandlerMock);
/** @var UtilsView|MockObject $utilsViewMock */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->once())->method('addErrorToDisplay'); $utilsViewMock->expects($this->once())->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
->onlyMethods(['logout']) ->onlyMethods(['logout'])
->getMock(); ->getMock();
$userMock->expects($this->once())->method('logout'); $userMock->expects($this->once())->method('logout');
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($writeLog)->method('error')->willReturn(true); $loggerMock->expects($writeLog)->method('error')->willReturn(true);
$loggerMock->expects($writeLog)->method('debug')->willReturn(true); $loggerMock->expects($writeLog)->method('debug')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @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'])
'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject',
'd3GetMockableLogger', ])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass));
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3GetMockableLogger')->willReturn($loggerMock);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userMock, $systemEventHandlerMock) {
$args = func_get_args();
switch ($args[0]) {
case User::class:
return $userMock;
case SystemEventHandler::class:
return $systemEventHandlerMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertSame( $this->assertSame(
'login', 'login',
@ -453,16 +394,14 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function adminLoginExceptionDataProvider(): array public function adminLoginExceptionDataProvider(): Generator
{ {
return [ yield 'userException' => [UserException::class, $this->any()];
'userException' => [UserException::class, $this->any()], yield 'cookieException' => [CookieException::class, $this->any()];
'cookieException' => [CookieException::class, $this->any()], yield 'webauthnException' => [WebauthnException::class, $this->atLeastOnce()];
'webauthnException' => [WebauthnException::class, $this->atLeastOnce()], yield 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce()];
'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce()],
];
} }
/** /**
@ -494,15 +433,13 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canHandleErrorMessageDataProvider(): array public function canHandleErrorMessageDataProvider(): Generator
{ {
return [ yield 'has error message' => ['errorMessage', true];
'has error message' => ['errorMessage', true], yield 'empty error message' => ['', false];
'empty error message' => ['', false], yield 'error message null' => [null, false];
'error message null' => [null, false],
];
} }
/** /**
@ -519,10 +456,11 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['assertAuthn']) ->onlyMethods(['assertAuthn'])
->getMock(); ->getMock();
$webauthnMock->expects($doAssert)->method('assertAuthn'); $webauthnMock->expects($doAssert)->method('assertAuthn');
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getCredential', 'd3GetMockableOxNewObject']) ->onlyMethods(['getCredential'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
if ($throwException) { if ($throwException) {
@ -530,17 +468,6 @@ class WebauthnLoginTest extends UnitTestCase
} else { } else {
$sut->method('getCredential')->willReturn('credential'); $sut->method('getCredential')->willReturn('credential');
} }
$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);
@ -555,14 +482,12 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canAssertAuthDataProvider(): array public function canAssertAuthDataProvider(): Generator
{ {
return [ yield 'has credential' => ['credentialFixture', $this->atLeastOnce(), false];
'has credential' => ['credentialFixture', $this->atLeastOnce(), false], yield 'no credential' => [null, $this->never(), true];
'no credential' => [null, $this->never(), true],
];
} }
/** /**
@ -586,23 +511,13 @@ class WebauthnLoginTest extends UnitTestCase
$this->identicalTo('userId') $this->identicalTo('userId')
) )
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetMockableRegistryObject']) ->onlyMethods(['getCredential'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$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,
@ -634,6 +549,7 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['setUserCookie']) ->onlyMethods(['setUserCookie'])
->getMock(); ->getMock();
$utilsServerMock->expects($this->exactly((int) $setCookie))->method('setUserCookie'); $utilsServerMock->expects($this->exactly((int) $setCookie))->method('setUserCookie');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock);
/** @var Config|MockObject $configMock */ /** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class) $configMock = $this->getMockBuilder(Config::class)
@ -641,25 +557,13 @@ class WebauthnLoginTest extends UnitTestCase
->getMock(); ->getMock();
$configMock->method('getConfigParam')->with('blShowRememberMe')->willReturn($setCookie); $configMock->method('getConfigParam')->with('blShowRememberMe')->willReturn($setCookie);
$configMock->method('getShopId')->willReturn(1); $configMock->method('getShopId')->willReturn(1);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getCredential'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsServerMock, $configMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsServer::class:
return $utilsServerMock;
case Config::class:
return $configMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -669,14 +573,12 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canSetSessionCookieDataProvider(): array public function canSetSessionCookieDataProvider(): Generator
{ {
return [ yield 'set cookie' => [true];
'set cookie' => [true], yield 'dont set cookie' => [false];
'dont set cookie' => [false],
];
} }
/** /**
@ -698,23 +600,13 @@ class WebauthnLoginTest extends UnitTestCase
->getMock(); ->getMock();
$userMock->method('isLoaded')->willReturn($userLoaded); $userMock->method('isLoaded')->willReturn($userLoaded);
$userMock->method('getFieldData')->with('oxrights')->willReturn($rights); $userMock->method('getFieldData')->with('oxrights')->willReturn($rights);
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetMockableOxNewObject']) ->onlyMethods(['getCredential'])
->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);
}
}
);
if ($throwsException) { if ($throwsException) {
$this->expectException(UserException::class); $this->expectException(UserException::class);
@ -731,20 +623,18 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array[] * @return Generator
*/ */
public function canAssertUserDataProvider(): array public function canAssertUserDataProvider(): Generator
{ {
return [ yield 'frontend, user not loaded' => [false, false, 'malladmin', true];
'frontend, user not loaded' => [false, false, 'malladmin', true], yield 'backend, user not loaded' => [true, false, 'malladmin', true];
'backend, user not loaded' => [true, false, 'malladmin', true], yield 'frontend, frontend user loaded' => [false, true, 'user', false];
'frontend, frontend user loaded' => [false, true, 'user', false], yield 'backend, frontend user loaded' => [true, true, 'user', true];
'backend, frontend user loaded' => [true, true, 'user', true], yield 'frontend, backend user loaded' => [false, true, 'malladmin', false];
'frontend, backend user loaded' => [false, true, 'malladmin', false], yield 'backend, backend user loaded' => [true, true, 'malladmin', false];
'backend, backend user loaded' => [true, true, 'malladmin', false], yield 'frontend, backend 2 user loaded' => [false, true, '2', false];
'frontend, backend 2 user loaded' => [false, true, '2', false], yield 'backend, backend 2 user loaded' => [true, true, '2', false];
'backend, backend 2 user loaded' => [true, true, '2', false],
];
} }
/** /**
@ -763,23 +653,13 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['getOxCookie']) ->onlyMethods(['getOxCookie'])
->getMock(); ->getMock();
$utilsServerMock->method('getOxCookie')->willReturn($cookie); $utilsServerMock->method('getOxCookie')->willReturn($cookie);
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetMockableRegistryObject']) ->onlyMethods(['getCredential'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$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);
@ -794,14 +674,12 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array[] * @return Generator
*/ */
public function canHandleBackendCookieDataProvider(): array public function canHandleBackendCookieDataProvider(): Generator
{ {
return [ yield 'has cookie' => ['cookiecontent', false];
'has cookie' => ['cookiecontent', false], yield 'has no cookie' => [null, true];
'has no cookie' => [null, true],
];
} }
/** /**
@ -820,6 +698,7 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['setShopId']) ->onlyMethods(['setShopId'])
->getMock(); ->getMock();
$configMock->expects($setVar)->method('setShopId'); $configMock->expects($setVar)->method('setShopId');
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
@ -835,20 +714,9 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetMockableRegistryObject']) ->onlyMethods(['getCredential'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$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,
@ -858,15 +726,13 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canHandleBackendSubshopRightsDataProvider(): array public function canHandleBackendSubshopRightsDataProvider(): Generator
{ {
return [ yield 'malladmin' => ['malladmin', $this->never()];
'malladmin' => ['malladmin', $this->never()], yield '1' => ['1', $this->atLeastOnce()];
'1' => ['1', $this->atLeastOnce()], yield '2' => ['2', $this->atLeastOnce()];
'2' => ['2', $this->atLeastOnce()],
];
} }
/** /**
@ -885,23 +751,13 @@ class WebauthnLoginTest extends UnitTestCase
->getMock(); ->getMock();
$sessionMock->method('isSessionStarted')->willReturn($sessionStarted); $sessionMock->method('isSessionStarted')->willReturn($sessionStarted);
$sessionMock->expects($this->exactly((int) $sessionStarted))->method('regenerateSessionId'); $sessionMock->expects($this->exactly((int) $sessionStarted))->method('regenerateSessionId');
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['getCredential'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->getMock();
$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,
@ -910,14 +766,12 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canRegenerateSessionIdDataProvider(): array public function canRegenerateSessionIdDataProvider(): Generator
{ {
return [ yield 'session started' => [true];
'session started' => [true], yield 'session not started' => [false];
'session not started' => [false],
];
} }
/** /**
@ -935,12 +789,14 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['redirect']) ->onlyMethods(['redirect'])
->getMock(); ->getMock();
$utilsMock->expects($this->exactly((int) $inBlockedGroup))->method('redirect'); $utilsMock->expects($this->exactly((int) $inBlockedGroup))->method('redirect');
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var Config|MockObject $configMock */ /** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class) $configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getShopHomeUrl']) ->onlyMethods(['getShopHomeUrl'])
->getMock(); ->getMock();
$configMock->method('getShopHomeUrl')->willReturn('homeurl'); $configMock->method('getShopHomeUrl')->willReturn('homeurl');
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
@ -950,22 +806,9 @@ class WebauthnLoginTest extends UnitTestCase
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->onlyMethods(['d3GetMockableRegistryObject']) ->onlyMethods(['getCredential'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $configMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case Config::class:
return $configMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -975,14 +818,12 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canHandleBlockedUserDataProvider(): array public function canHandleBlockedUserDataProvider(): Generator
{ {
return [ yield 'is in blocked group' => [true];
'is in blocked group' => [true], yield 'is not in blocked group' => [false];
'is not in blocked group' => [false],
];
} }
/** /**
@ -1004,23 +845,13 @@ class WebauthnLoginTest extends UnitTestCase
->onlyMethods(['getBasket']) ->onlyMethods(['getBasket'])
->getMock(); ->getMock();
$sessionMock->method('getBasket')->willReturn($basketMock); $sessionMock->method('getBasket')->willReturn($basketMock);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetMockableRegistryObject']) ->onlyMethods(['getCredential'])
->getMock(); ->getMock();
$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,
@ -1073,23 +904,13 @@ class WebauthnLoginTest extends UnitTestCase
return null; return null;
} }
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var WebauthnLogin|MockObject $sut */ /** @var WebauthnLogin|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnLogin::class) $sut = $this->getMockBuilder(WebauthnLogin::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods(['d3GetMockableRegistryObject', 'isAdmin']) ->onlyMethods(['isAdmin'])
->getMock(); ->getMock();
$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(
@ -1102,14 +923,12 @@ class WebauthnLoginTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canGetUserIdDataProvider(): array public function canGetUserIdDataProvider(): Generator
{ {
return [ yield 'admin' => [true, 'adminUser'];
'admin' => [true, 'adminUser'], yield 'frontend' => [false, 'frontendUser'];
'frontend' => [false, 'frontendUser'],
];
} }
/** /**
@ -1134,6 +953,7 @@ class WebauthnLoginTest extends UnitTestCase
$this->identicalTo('idFixture'), $this->identicalTo('idFixture'),
] ]
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
@ -1144,20 +964,9 @@ 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', 'd3GetMockableRegistryObject']) ->onlyMethods(['getCredential'])
->getMock(); ->getMock();
$sut->method('getCredential')->willReturn('credentialFixture'); $sut->method('getCredential')->willReturn('credentialFixture');
$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

@ -24,7 +24,9 @@ use D3\Webauthn\Application\Model\RelyingPartyEntity;
use D3\Webauthn\Application\Model\UserEntity; use D3\Webauthn\Application\Model\UserEntity;
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\tests\unit\WAUnitTestCase;
use Exception; use Exception;
use Generator;
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\Eshop\Core\Registry;
@ -40,7 +42,7 @@ use Webauthn\PublicKeyCredentialRequestOptions;
use Webauthn\PublicKeyCredentialSource; use Webauthn\PublicKeyCredentialSource;
use Webauthn\Server; use Webauthn\Server;
class WebauthnTest extends UnitTestCase class WebauthnTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -68,22 +70,10 @@ class WebauthnTest extends UnitTestCase
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($this->exactly((int) !$expected))->method('addErrorToDisplay'); $utilsViewMock->expects($this->exactly((int) !$expected))->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var Webauthn|MockObject $sut */ /** @var Webauthn $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = oxNew(Webauthn::class);
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,
@ -95,20 +85,18 @@ class WebauthnTest extends UnitTestCase
} }
/** /**
* @return array[] * @return Generator
*/ */
public function canCheckIsAvailableDataProvider(): array public function canCheckIsAvailableDataProvider(): Generator
{ {
return [ yield 'https' => ['on', null, null, null, true];
'https' => ['on', null, null, null, true], yield 'HTTP_X_FORWARDED_PROTO' => [null, 'https', null, null, true];
'HTTP_X_FORWARDED_PROTO' => [null, 'https', null, null, true], yield 'HTTP_X_FORWARDED_SSL' => [null, null, 'on', null, true];
'HTTP_X_FORWARDED_SSL' => [null, null, 'on', null, true], yield 'REMOTE_ADDR v4' => [null, null, null, '127.0.0.1', true];
'REMOTE_ADDR v4' => [null, null, null, '127.0.0.1', true], yield 'REMOTE_ADDR v6' => [null, null, null, '::1', true];
'REMOTE_ADDR v6' => [null, null, null, '::1', true], yield 'REMOTE_ADDR localhost' => [null, null, null, 'some.localhost', true];
'REMOTE_ADDR localhost' => [null, null, null, 'some.localhost', true], yield 'unset' => [null, null, null, null, false];
'unset' => [null, null, null, null, false], yield 'not valid' => ['off', 'http', 'off', '160.158.23.7', false];
'not valid' => ['off', 'http', 'off', '160.158.23.7', false],
];
} }
/** /**
@ -150,11 +138,13 @@ class WebauthnTest extends UnitTestCase
$this->identicalTo(Webauthn::SESSION_CREATIONS_OPTIONS), $this->identicalTo(Webauthn::SESSION_CREATIONS_OPTIONS),
$this->identicalTo($pubKeyCredCreationOptionsMock) $this->identicalTo($pubKeyCredCreationOptionsMock)
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var UserEntity|MockObject $userEntityMock */ /** @var UserEntity|MockObject $userEntityMock */
$userEntityMock = $this->getMockBuilder(UserEntity::class) $userEntityMock = $this->getMockBuilder(UserEntity::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
d3GetOxidDIC()->set(UserEntity::class, $userEntityMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
@ -163,33 +153,9 @@ class WebauthnTest extends UnitTestCase
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['d3GetMockableOxNewObject', 'getServer', 'd3GetMockableRegistryObject', 'jsonEncode', ->onlyMethods(['getServer', 'jsonEncode', 'getExistingCredentials'])
'getExistingCredentials',
])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userEntityMock) {
$args = func_get_args();
switch ($args[0]) {
case UserEntity::class:
return $userEntityMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('getServer')->willReturn($serverMock); $sut->method('getServer')->willReturn($serverMock);
$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->expects($this->once())->method('jsonEncode')->willReturn($jsonReturn); $sut->expects($this->once())->method('jsonEncode')->willReturn($jsonReturn);
$sut->expects($this->once())->method('getExistingCredentials')->willReturn([ $sut->expects($this->once())->method('getExistingCredentials')->willReturn([
$pubKeyCredDescriptorMock, $pubKeyCredDescriptorMock,
@ -214,14 +180,12 @@ class WebauthnTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canGetOptionsDataProvider(): array public function canGetOptionsDataProvider(): Generator
{ {
return [ yield 'json encoded' => ['jsonstring'];
'json encoded' => ['jsonstring'], yield 'json failed' => [false];
'json failed' => [false],
];
} }
/** /**
@ -251,27 +215,15 @@ class WebauthnTest extends UnitTestCase
$pubKeyCredListMock->method('findAllForUserEntity')->willReturn( $pubKeyCredListMock->method('findAllForUserEntity')->willReturn(
[$pubKeyCredSourceMock] [$pubKeyCredSourceMock]
); );
d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pubKeyCredListMock);
/** @var UserEntity|MockObject $userEntityMock */ /** @var UserEntity|MockObject $userEntityMock */
$userEntityMock = $this->getMockBuilder(UserEntity::class) $userEntityMock = $this->getMockBuilder(UserEntity::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
/** @var Webauthn|MockObject $sut */ /** @var Webauthn $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = oxNew(Webauthn::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($pubKeyCredListMock) {
$args = func_get_args();
switch ($args[0]) {
case PublicKeyCredentialList::class:
return $pubKeyCredListMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$return = $this->callMethod( $return = $this->callMethod(
$sut, $sut,
@ -345,6 +297,7 @@ class WebauthnTest extends UnitTestCase
$this->identicalTo(Webauthn::SESSION_ASSERTION_OPTIONS), $this->identicalTo(Webauthn::SESSION_ASSERTION_OPTIONS),
$this->identicalTo($pubKeyCredRequestOptionsMock) $this->identicalTo($pubKeyCredRequestOptionsMock)
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
@ -352,43 +305,21 @@ class WebauthnTest extends UnitTestCase
->onlyMethods(['load']) ->onlyMethods(['load'])
->getMock(); ->getMock();
$userMock->method('load')->willReturn(true); $userMock->method('load')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var UserEntity|MockObject $userEntityMock */ /** @var UserEntity|MockObject $userEntityMock */
$userEntityMock = $this->getMockBuilder(UserEntity::class) $userEntityMock = $this->getMockBuilder(UserEntity::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
d3GetOxidDIC()->set(UserEntity::class, $userEntityMock);
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['d3GetMockableOxNewObject', 'getServer', 'd3GetMockableRegistryObject', 'jsonEncode', ->onlyMethods(['getServer', 'jsonEncode',
'getExistingCredentials', 'getExistingCredentials',
]) ])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userEntityMock, $userMock) {
$args = func_get_args();
switch ($args[0]) {
case UserEntity::class:
return $userEntityMock;
case User::class:
return $userMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('getServer')->willReturn($serverMock); $sut->method('getServer')->willReturn($serverMock);
$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->expects($this->once())->method('jsonEncode')->willReturn($jsonReturn); $sut->expects($this->once())->method('jsonEncode')->willReturn($jsonReturn);
$sut->expects($this->once())->method('getExistingCredentials')->willReturn([ $sut->expects($this->once())->method('getExistingCredentials')->willReturn([
$pubKeyCredDescriptorMock, $pubKeyCredDescriptorMock,
@ -420,11 +351,6 @@ class WebauthnTest extends UnitTestCase
*/ */
public function canGetServer() public function canGetServer()
{ {
/** @var PublicKeyCredentialList|MockObject $pubKeyCredListMock */
$pubKeyCredListMock = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor()
->getMock();
/** @var Server|MockObject $serverMock */ /** @var Server|MockObject $serverMock */
$serverMock = $this->getMockBuilder(Server::class) $serverMock = $this->getMockBuilder(Server::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
@ -432,30 +358,11 @@ class WebauthnTest extends UnitTestCase
->getMock(); ->getMock();
$serverMock->expects($this->atLeastOnce())->method('setLogger'); $serverMock->expects($this->atLeastOnce())->method('setLogger');
/** @var RelyingPartyEntity|MockObject $rpEntityMock */
$rpEntityMock = $this->getMockBuilder(RelyingPartyEntity::class)
->disableOriginalConstructor()
->getMock();
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['d3GetMockableOxNewObject']) ->onlyMethods(['getServerObject'])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback( $sut->method('getServerObject')->willReturn($serverMock);
function () use ($rpEntityMock, $serverMock, $pubKeyCredListMock) {
$args = func_get_args();
switch ($args[0]) {
case RelyingPartyEntity::class:
return $rpEntityMock;
case Server::class:
return $serverMock;
case PublicKeyCredentialList::class:
return $pubKeyCredListMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertSame( $this->assertSame(
$serverMock, $serverMock,
@ -486,12 +393,14 @@ class WebauthnTest extends UnitTestCase
->onlyMethods(['saveCredentialSource']) ->onlyMethods(['saveCredentialSource'])
->getMock(); ->getMock();
$pubKeyCredMock->expects($this->exactly((int) !$throwsException))->method('saveCredentialSource'); $pubKeyCredMock->expects($this->exactly((int) !$throwsException))->method('saveCredentialSource');
d3GetOxidDIC()->set(PublicKeyCredential::class, $pubKeyCredMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['getVariable']) ->onlyMethods(['getVariable'])
->getMock(); ->getMock();
$sessionMock->method('getVariable')->willReturn($pubKeyCredCreationsOptionsMock); $sessionMock->method('getVariable')->willReturn($pubKeyCredCreationsOptionsMock);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Server|MockObject $serverMock */ /** @var Server|MockObject $serverMock */
$serverMock = $this->getMockBuilder(Server::class) $serverMock = $this->getMockBuilder(Server::class)
@ -507,30 +416,8 @@ class WebauthnTest extends UnitTestCase
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableOxNewObject', 'getServer']) ->onlyMethods(['getServer'])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($pubKeyCredMock) {
$args = func_get_args();
switch ($args[0]) {
case PublicKeyCredential::class:
return $pubKeyCredMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('getServer')->willReturn($serverMock); $sut->method('getServer')->willReturn($serverMock);
if ($throwsException) { if ($throwsException) {
@ -545,14 +432,12 @@ class WebauthnTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function loadAndCheckAssertionResponseDataProvider(): array public function loadAndCheckAssertionResponseDataProvider(): Generator
{ {
return [ yield 'check failed' => [true];
'check failed' => [true], yield 'check passed' => [false];
'check passed' => [false],
];
} }
@ -576,6 +461,7 @@ class WebauthnTest extends UnitTestCase
->onlyMethods(['getVariable']) ->onlyMethods(['getVariable'])
->getMock(); ->getMock();
$sessionMock->method('getVariable')->willReturn($pubKeyCredRequestOptionsMock); $sessionMock->method('getVariable')->willReturn($pubKeyCredRequestOptionsMock);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Server|MockObject $serverMock */ /** @var Server|MockObject $serverMock */
$serverMock = $this->getMockBuilder(Server::class) $serverMock = $this->getMockBuilder(Server::class)
@ -596,21 +482,10 @@ class WebauthnTest extends UnitTestCase
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['getUserEntityFrom', 'getServer', 'd3GetMockableRegistryObject', 'getSavedUserIdFromSession']) ->onlyMethods(['getUserEntityFrom', 'getServer', 'getSavedUserIdFromSession'])
->getMock(); ->getMock();
$sut->method('getUserEntityFrom')->willReturn($userEntity); $sut->method('getUserEntityFrom')->willReturn($userEntity);
$sut->method('getServer')->willReturn($serverMock); $sut->method('getServer')->willReturn($serverMock);
$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('getSavedUserIdFromSession')->willReturn('userId'); $sut->method('getSavedUserIdFromSession')->willReturn('userId');
if ($throwsException) { if ($throwsException) {
@ -640,30 +515,18 @@ class WebauthnTest extends UnitTestCase
$userEntityMock = $this->getMockBuilder(UserEntity::class) $userEntityMock = $this->getMockBuilder(UserEntity::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
d3GetOxidDIC()->set(UserEntity::class, $userEntityMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
->onlyMethods(['load']) ->onlyMethods(['load'])
->getMock(); ->getMock();
$userMock->method('load'); $userMock->method('load');
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userMock, $userEntityMock) {
$args = func_get_args();
switch ($args[0]) {
case User::class:
return $userMock;
case UserEntity::class:
return $userEntityMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertSame( $this->assertSame(
$userEntityMock, $userEntityMock,
@ -698,22 +561,12 @@ class WebauthnTest extends UnitTestCase
[WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $frontendUser], [WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $frontendUser],
] ]
); );
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['d3GetMockableRegistryObject', 'isAdmin']) ->onlyMethods(['isAdmin'])
->getMock(); ->getMock();
$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(
@ -726,14 +579,12 @@ class WebauthnTest extends UnitTestCase
} }
/** /**
* @return array[] * @return Generator
*/ */
public function canGetSavedUserIdFromSessionDataProvider(): array public function canGetSavedUserIdFromSessionDataProvider(): Generator
{ {
return [ yield 'admin' => [true, 'admUsr', 'frontendUsr', 'admUsr'];
'admin' => [true, 'admUsr', 'frontendUsr', 'admUsr'], yield 'frontend' => [false, 'admUsr', 'frontendUsr', 'frontendUsr'];
'frontend' => [false, 'admUsr', 'frontendUsr', 'frontendUsr'],
];
} }
/** /**
@ -773,30 +624,19 @@ class WebauthnTest extends UnitTestCase
->onlyMethods(['getVariable']) ->onlyMethods(['getVariable'])
->getMock(); ->getMock();
$sessionMock->method('getVariable')->willReturn($sessionGlobalSwitch); $sessionMock->method('getVariable')->willReturn($sessionGlobalSwitch);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Config|MockObject $configMock */ /** @var Config|MockObject $configMock */
$configMock = $this->getMockBuilder(Config::class) $configMock = $this->getMockBuilder(Config::class)
->onlyMethods(['getConfigParam']) ->onlyMethods(['getConfigParam'])
->getMock(); ->getMock();
$configMock->method('getConfigParam')->willReturn($configGlobalSwitch); $configMock->method('getConfigParam')->willReturn($configGlobalSwitch);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['d3GetMockableRegistryObject', 'UserUseWebauthn']) ->onlyMethods(['UserUseWebauthn'])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($configMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Config::class:
return $configMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('UserUseWebauthn')->willReturn($userUseWebauthn); $sut->method('UserUseWebauthn')->willReturn($userUseWebauthn);
$this->assertSame( $this->assertSame(
@ -810,17 +650,15 @@ class WebauthnTest extends UnitTestCase
} }
/** /**
* @return array * @return Generator
*/ */
public function canCheckIsActiveDataProvider(): array public function canCheckIsActiveDataProvider(): Generator
{ {
return [ yield 'user use webauthn' => [false, false, true, true];
'user use webauthn' => [false, false, true, true], yield 'user use webauthn, config disabled' => [true, false, true, false];
'user use webauthn, config disabled' => [true, false, true, false], yield 'user use webauthn, session disabled' => [false, true, true, false];
'user use webauthn, session disabled' => [false, true, true, false], yield 'user use webauthn, both disabled' => [true, true, true, false];
'user use webauthn, both disabled' => [true, true, true, false], yield 'user dont use ' => [false, false, false, false];
'user dont use ' => [false, false, false, false],
];
} }
/** /**
@ -839,6 +677,7 @@ class WebauthnTest extends UnitTestCase
->addMethods(['findAllForUserEntity']) ->addMethods(['findAllForUserEntity'])
->getMock(); ->getMock();
$pubKeyCredListMock->method('findAllForUserEntity')->willReturn($credList); $pubKeyCredListMock->method('findAllForUserEntity')->willReturn($credList);
d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pubKeyCredListMock);
/** @var UserEntity|MockObject $userEntityMock */ /** @var UserEntity|MockObject $userEntityMock */
$userEntityMock = $this->getMockBuilder(UserEntity::class) $userEntityMock = $this->getMockBuilder(UserEntity::class)
@ -847,20 +686,9 @@ class WebauthnTest extends UnitTestCase
/** @var Webauthn|MockObject $sut */ /** @var Webauthn|MockObject $sut */
$sut = $this->getMockBuilder(Webauthn::class) $sut = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['getUserEntityFrom', 'd3GetMockableOxNewObject']) ->onlyMethods(['getUserEntityFrom'])
->getMock(); ->getMock();
$sut->method('getUserEntityFrom')->willReturn($userEntityMock); $sut->method('getUserEntityFrom')->willReturn($userEntityMock);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($pubKeyCredListMock) {
$args = func_get_args();
switch ($args[0]) {
case PublicKeyCredentialList::class:
return $pubKeyCredListMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,
@ -873,14 +701,43 @@ class WebauthnTest extends UnitTestCase
} }
/** /**
* @return array[] * @return Generator
*/ */
public function canCheckUserUseWebauthnDataProvider(): array public function canCheckUserUseWebauthnDataProvider(): Generator
{ {
return [ yield 'no array' => [null, false];
'no array' => [null, false], yield 'no count' => [[], false];
'no count' => [[], false], yield 'filled array' => [['abc'], true];
'filled array' => [['abc'], true], }
];
/**
* @test
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Model\Webauthn::getServerObject
*/
public function canGetServerObject()
{
/** @var PublicKeyCredentialList|MockObject $pubKeyCredListMock */
$pubKeyCredListMock = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor()
->getMock();
d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pubKeyCredListMock);
/** @var RelyingPartyEntity|MockObject $rpEntityMock */
$rpEntityMock = $this->getMockBuilder(RelyingPartyEntity::class)
->disableOriginalConstructor()
->getMock();
d3GetOxidDIC()->set(RelyingPartyEntity::class, $rpEntityMock);
/** @var Webauthn $sut */
$sut = oxNew(Webauthn::class);
$this->assertInstanceOf(
Server::class,
$this->callMethod(
$sut,
'getServerObject'
)
);
} }
} }

View File

@ -23,6 +23,7 @@ use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\Application\Model\WebauthnLogin;
use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent; use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent;
use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent_parent; use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent_parent;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Component\UserComponent; use OxidEsales\Eshop\Application\Component\UserComponent;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Controller\BaseController; use OxidEsales\Eshop\Core\Controller\BaseController;
@ -35,7 +36,7 @@ use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class UserComponentWebauthnTest extends UnitTestCase class UserComponentWebauthnTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -80,6 +81,7 @@ class UserComponentWebauthnTest extends UnitTestCase
->onlyMethods(['redirect']) ->onlyMethods(['redirect'])
->getMock(); ->getMock();
$utilsMock->expects($this->exactly((int) $doRedirect))->method('redirect'); $utilsMock->expects($this->exactly((int) $doRedirect))->method('redirect');
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var BaseController|MockObject $baseControllerMock */ /** @var BaseController|MockObject $baseControllerMock */
$baseControllerMock = $this->getMockBuilder(BaseController::class) $baseControllerMock = $this->getMockBuilder(BaseController::class)
@ -94,12 +96,14 @@ class UserComponentWebauthnTest extends UnitTestCase
$requestMock->method('getRequestParameter')->willReturnMap([ $requestMock->method('getRequestParameter')->willReturnMap([
['lgn_usr', 'myUserName'], ['lgn_usr', 'myUserName'],
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
->onlyMethods(['d3GetLoginUserId']) ->onlyMethods(['d3GetLoginUserId'])
->getMock(); ->getMock();
$userMock->method('d3GetLoginUserId')->willReturn('myUserId'); $userMock->method('d3GetLoginUserId')->willReturn('myUserId');
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
@ -108,42 +112,15 @@ class UserComponentWebauthnTest extends UnitTestCase
$sessionMock->expects($this->exactly($setVariableCount))->method('setVariable'); $sessionMock->expects($this->exactly($setVariableCount))->method('setVariable');
$sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER) $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER)
->willReturn('myUserName'); ->willReturn('myUserName');
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var d3_webauthn_UserComponent|MockObject $sut */ /** @var d3_webauthn_UserComponent|MockObject $sut */
$sut = $this->getMockBuilder(UserComponent::class) $sut = $this->getMockBuilder(UserComponent::class)
->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'd3HasWebauthnButNotLoggedin', ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'd3HasWebauthnButNotLoggedin', 'getParent'])
'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'getParent',
])
->getMock(); ->getMock();
$sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn); $sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn);
$sut->method('d3CallMockableFunction')->willReturn('parentReturn'); $sut->method('d3CallMockableFunction')->willReturn('parentReturn');
$sut->method('d3HasWebauthnButNotLoggedin')->willReturn($loggedin); $sut->method('d3HasWebauthnButNotLoggedin')->willReturn($loggedin);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userMock) {
$args = func_get_args();
switch ($args[0]) {
case User::class:
return $userMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $requestMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case Request::class:
return $requestMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('getParent')->willReturn($baseControllerMock); $sut->method('getParent')->willReturn($baseControllerMock);
$this->callMethod( $this->callMethod(
@ -184,30 +161,18 @@ class UserComponentWebauthnTest extends UnitTestCase
->getMock(); ->getMock();
$sessionMock->method('hasVariable')->with(WebauthnConf::WEBAUTHN_SESSION_AUTH) $sessionMock->method('hasVariable')->with(WebauthnConf::WEBAUTHN_SESSION_AUTH)
->willReturn($hasWebauthnLogin); ->willReturn($hasWebauthnLogin);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Request|MockObject $requestMock */ /** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(Request::class) $requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestParameter']) ->onlyMethods(['getRequestParameter'])
->getMock(); ->getMock();
$requestMock->method('getRequestParameter')->with('lgn_pwd')->willReturn($usedPassword); $requestMock->method('getRequestParameter')->with('lgn_pwd')->willReturn($usedPassword);
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var d3_webauthn_UserComponent|MockObject $sut */ /** @var d3_webauthn_UserComponent|MockObject $sut */
$sut = $this->getMockBuilder(UserComponent::class) $sut = $this->getMockBuilder(UserComponent::class)
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock(); ->getMock();
$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,
@ -251,39 +216,18 @@ class UserComponentWebauthnTest extends UnitTestCase
->getMock(); ->getMock();
$sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_SESSION_AUTH) $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_SESSION_AUTH)
->willReturn($hasAuth); ->willReturn($hasAuth);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Webauthn|MockObject $webauthnMock */ /** @var Webauthn|MockObject $webauthnMock */
$webauthnMock = $this->getMockBuilder(Webauthn::class) $webauthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['isActive']) ->onlyMethods(['isActive'])
->getMock(); ->getMock();
$webauthnMock->method('isActive')->willReturn($webauthnActive); $webauthnMock->method('isActive')->willReturn($webauthnActive);
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var UserComponent|MockObject $sut */ /** @var UserComponent|MockObject $sut */
$sut = $this->getMockBuilder(UserComponent::class) $sut = $this->getMockBuilder(UserComponent::class)
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webauthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,
@ -340,22 +284,10 @@ class UserComponentWebauthnTest extends UnitTestCase
->onlyMethods(['deleteVariable']) ->onlyMethods(['deleteVariable'])
->getMock(); ->getMock();
$sessionMock->expects($this->atLeast(4))->method('deleteVariable')->willReturn(true); $sessionMock->expects($this->atLeast(4))->method('deleteVariable')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var UserComponent|MockObject $sut */ /** @var UserComponent $sut */
$sut = $this->getMockBuilder(UserComponent::class) $sut = oxNew(UserComponent::class);
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock();
$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,
@ -377,6 +309,7 @@ class UserComponentWebauthnTest extends UnitTestCase
->onlyMethods(['addErrorToDisplay']) ->onlyMethods(['addErrorToDisplay'])
->getMock(); ->getMock();
$utilsViewMock->expects($addErrorInvocationCount)->method('addErrorToDisplay'); $utilsViewMock->expects($addErrorInvocationCount)->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var WebauthnLogin|MockObject $webauthnLoginMock */ /** @var WebauthnLogin|MockObject $webauthnLoginMock */
$webauthnLoginMock = $this->getMockBuilder(WebauthnLogin::class) $webauthnLoginMock = $this->getMockBuilder(WebauthnLogin::class)
@ -393,31 +326,10 @@ class UserComponentWebauthnTest extends UnitTestCase
/** @var UserComponent|MockObject $sut */ /** @var UserComponent|MockObject $sut */
$sut = $this->getMockBuilder(UserComponent::class) $sut = $this->getMockBuilder(UserComponent::class)
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', '_afterLogin']) ->onlyMethods(['_afterLogin', 'd3GetWebauthnLogin'])
->getMock(); ->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnLoginMock) {
$args = func_get_args();
switch ($args[0]) {
case WebauthnLogin::class:
return $webauthnLoginMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->expects($afterLoginInvocationCount)->method('_afterLogin'); $sut->expects($afterLoginInvocationCount)->method('_afterLogin');
$sut->method('d3GetWebauthnLogin')->willReturn($webauthnLoginMock);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -436,4 +348,30 @@ class UserComponentWebauthnTest extends UnitTestCase
'webauthnLoginError' => [WebauthnLoginErrorException::class, $this->never(), $this->never()], 'webauthnLoginError' => [WebauthnLoginErrorException::class, $this->never(), $this->never()],
]; ];
} }
/**
* @test
* @throws ReflectionException
* @covers \D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent::d3GetWebauthnLogin
*/
public function canGetWebAuthnLogin()
{
/** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestEscapedParameter'])
->getMock();
$requestMock->method('getRequestEscapedParameter')->willReturn('requestReturn');
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var d3webauthnadminlogin|MockObject $sut */
$sut = oxNew(UserComponent::class);
$this->assertInstanceOf(
WebauthnLogin::class,
$this->callMethod(
$sut,
'd3GetWebauthnLogin'
)
);
}
} }

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Application\Controller\AccountController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountControllerTest extends UnitTestCase class AccountControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountDownloadsController; use OxidEsales\Eshop\Application\Controller\AccountDownloadsController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountDownloadsControllerTest extends UnitTestCase class AccountDownloadsControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountNewsletterController; use OxidEsales\Eshop\Application\Controller\AccountNewsletterController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountNewsletterControllerTest extends UnitTestCase class AccountNewsletterControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountNoticeListController; use OxidEsales\Eshop\Application\Controller\AccountNoticeListController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountNoticeListControllerTest extends UnitTestCase class AccountNoticeListControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountOrderController; use OxidEsales\Eshop\Application\Controller\AccountOrderController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountOrderControllerTest extends UnitTestCase class AccountOrderControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountPasswordController; use OxidEsales\Eshop\Application\Controller\AccountPasswordController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountPasswordControllerTest extends UnitTestCase class AccountPasswordControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountRecommlistController; use OxidEsales\Eshop\Application\Controller\AccountRecommlistController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountRecommlistControllerTest extends UnitTestCase class AccountRecommlistControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountReviewController; use OxidEsales\Eshop\Application\Controller\AccountReviewController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountReviewControllerTest extends UnitTestCase class AccountReviewControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountUserController; use OxidEsales\Eshop\Application\Controller\AccountUserController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountUserControllerTest extends UnitTestCase class AccountUserControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\AccountWishlistController; use OxidEsales\Eshop\Application\Controller\AccountWishlistController;
use OxidEsales\TestingLibrary\UnitTestCase;
class AccountWishlistControllerTest extends UnitTestCase class AccountWishlistControllerTest extends WAUnitTestCase
{ {
use AccountTestTrait; use AccountTestTrait;

View File

@ -18,6 +18,7 @@ namespace D3\Webauthn\tests\unit\Modules\Application\Controller\Admin;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
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\tests\unit\WAUnitTestCase;
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\Registry;
@ -27,7 +28,7 @@ use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class LoginControllerWebauthnTest extends UnitTestCase class LoginControllerWebauthnTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -43,22 +44,10 @@ class LoginControllerWebauthnTest extends UnitTestCase
->onlyMethods(['logout']) ->onlyMethods(['logout'])
->getMock(); ->getMock();
$userMock->expects($this->atLeastOnce())->method('logout'); $userMock->expects($this->atLeastOnce())->method('logout');
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var LoginController|MockObject $sut */ /** @var LoginController $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = oxNew(LoginController::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->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->callMethod( $this->callMethod(
$sut, $sut,
@ -86,30 +75,17 @@ class LoginControllerWebauthnTest extends UnitTestCase
->getMock(); ->getMock();
$sessionMock->method('hasVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) $sessionMock->method('hasVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)
->willReturn($hasWebauthnLogin); ->willReturn($hasWebauthnLogin);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Request|MockObject $requestMock */ /** @var Request|MockObject $requestMock */
$requestMock = $this->getMockBuilder(Request::class) $requestMock = $this->getMockBuilder(Request::class)
->onlyMethods(['getRequestParameter']) ->onlyMethods(['getRequestParameter'])
->getMock(); ->getMock();
$requestMock->method('getRequestParameter')->with('pwd')->willReturn($usedPassword); $requestMock->method('getRequestParameter')->with('pwd')->willReturn($usedPassword);
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var LoginController|MockObject $sut */ /** @var LoginController $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = oxNew(LoginController::class);
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock();
$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,
@ -153,39 +129,17 @@ class LoginControllerWebauthnTest extends UnitTestCase
->getMock(); ->getMock();
$sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)
->willReturn($hasAuth); ->willReturn($hasAuth);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Webauthn|MockObject $webauthnMock */ /** @var Webauthn|MockObject $webauthnMock */
$webauthnMock = $this->getMockBuilder(Webauthn::class) $webauthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['isActive']) ->onlyMethods(['isActive'])
->getMock(); ->getMock();
$webauthnMock->method('isActive')->willReturn($webauthnActive); $webauthnMock->method('isActive')->willReturn($webauthnActive);
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var LoginController|MockObject $sut */ /** @var LoginController $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = oxNew(LoginController::class);
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webauthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,
@ -230,12 +184,14 @@ class LoginControllerWebauthnTest extends UnitTestCase
['user', 'myUserName'], ['user', 'myUserName'],
['profile', 'myProfile'], ['profile', 'myProfile'],
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock);
/** @var User|MockObject $userMock */ /** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class) $userMock = $this->getMockBuilder(User::class)
->onlyMethods(['d3GetLoginUserId']) ->onlyMethods(['d3GetLoginUserId'])
->getMock(); ->getMock();
$userMock->method('d3GetLoginUserId')->willReturn('myUserId'); $userMock->method('d3GetLoginUserId')->willReturn('myUserId');
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
@ -244,40 +200,15 @@ class LoginControllerWebauthnTest extends UnitTestCase
$sessionMock->expects($this->exactly($setVariableCount))->method('setVariable'); $sessionMock->expects($this->exactly($setVariableCount))->method('setVariable');
$sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER) $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER)
->willReturn('myUserName'); ->willReturn('myUserName');
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var LoginController|MockObject $sut */ /** @var LoginController|MockObject $sut */
$sut = $this->getMockBuilder(LoginController::class) $sut = $this->getMockBuilder(LoginController::class)
->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'hasWebauthnButNotLoggedin', ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'hasWebauthnButNotLoggedin'])
'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject',
])
->getMock(); ->getMock();
$sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn); $sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn);
$sut->method('d3CallMockableFunction')->willReturn('parentReturn'); $sut->method('d3CallMockableFunction')->willReturn('parentReturn');
$sut->method('hasWebauthnButNotLoggedin')->willReturn($loggedin); $sut->method('hasWebauthnButNotLoggedin')->willReturn($loggedin);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userMock) {
$args = func_get_args();
switch ($args[0]) {
case User::class:
return $userMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($requestMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Request::class:
return $requestMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,

View File

@ -38,6 +38,8 @@ trait CheckoutTestTrait
public function setUp(): void public function setUp(): void
{ {
parent::setUp();
$this->userFixture = oxNew(User::class); $this->userFixture = oxNew(User::class);
$this->userFixture->setId($this->userFixtureId); $this->userFixture->setId($this->userFixtureId);
$this->userFixture->assign(['oxlname' => __METHOD__]); $this->userFixture->assign(['oxlname' => __METHOD__]);
@ -47,6 +49,8 @@ trait CheckoutTestTrait
public function tearDown(): void public function tearDown(): void
{ {
parent::tearDown();
$this->userFixture->delete($this->userFixtureId); $this->userFixture->delete($this->userFixtureId);
} }
@ -75,6 +79,7 @@ trait CheckoutTestTrait
->getMock(); ->getMock();
$sessionMock->method('getVariable') $sessionMock->method('getVariable')
->with($this->identicalTo(WebauthnConf::WEBAUTHN_SESSION_AUTH))->willReturn($sessionAuth); ->with($this->identicalTo(WebauthnConf::WEBAUTHN_SESSION_AUTH))->willReturn($sessionAuth);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var Webauthn|MockObject $webauthnMock */ /** @var Webauthn|MockObject $webauthnMock */
$webauthnMock = $this->getMockBuilder(Webauthn::class) $webauthnMock = $this->getMockBuilder(Webauthn::class)
@ -82,33 +87,10 @@ trait CheckoutTestTrait
->getMock(); ->getMock();
$webauthnMock->method('isAvailable')->willReturn($isAvailable); $webauthnMock->method('isAvailable')->willReturn($isAvailable);
$webauthnMock->method('isActive')->willReturn($isActive); $webauthnMock->method('isActive')->willReturn($isActive);
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
/** @var PaymentController|OrderController|UserController|MockObject $sut */ /** @var PaymentController|OrderController|UserController $sut */
$sut = $this->getMockBuilder($this->sutClass) $sut = oxNew($this->sutClass);
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
$args = func_get_args();
switch ($args[0]) {
case Webauthn::class:
return $webauthnMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
if ($hasUser) { if ($hasUser) {
$sut->setUser($this->userFixture); $sut->setUser($this->userFixture);
} }

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\OrderController; use OxidEsales\Eshop\Application\Controller\OrderController;
use OxidEsales\TestingLibrary\UnitTestCase;
class OrderControllerTest extends UnitTestCase class OrderControllerTest extends WAUnitTestCase
{ {
use CheckoutTestTrait; use CheckoutTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\PaymentController; use OxidEsales\Eshop\Application\Controller\PaymentController;
use OxidEsales\TestingLibrary\UnitTestCase;
class PaymentControllerTest extends UnitTestCase class PaymentControllerTest extends WAUnitTestCase
{ {
use CheckoutTestTrait; use CheckoutTestTrait;

View File

@ -15,10 +15,10 @@ declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules\Application\Controller; namespace D3\Webauthn\tests\unit\Modules\Application\Controller;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use OxidEsales\Eshop\Application\Controller\UserController; use OxidEsales\Eshop\Application\Controller\UserController;
use OxidEsales\TestingLibrary\UnitTestCase;
class UserControllerTest extends UnitTestCase class UserControllerTest extends WAUnitTestCase
{ {
use CheckoutTestTrait; use CheckoutTestTrait;

View File

@ -19,6 +19,7 @@ use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn_parent; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn_parent;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use Exception; use Exception;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Config;
@ -29,7 +30,7 @@ use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException; use ReflectionException;
class UserWebauthnTest extends UnitTestCase class UserWebauthnTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
@ -96,22 +97,12 @@ class UserWebauthnTest extends UnitTestCase
->onlyMethods(['deleteVariable']) ->onlyMethods(['deleteVariable'])
->getMock(); ->getMock();
$sessionMock->expects($this->atLeast(11))->method('deleteVariable')->willReturn(true); $sessionMock->expects($this->atLeast(11))->method('deleteVariable')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var User|MockObject $sut */ /** @var User|MockObject $sut */
$sut = $this->getMockBuilder(User::class) $sut = $this->getMockBuilder(User::class)
->onlyMethods(['d3GetMockableRegistryObject']) ->disableOriginalConstructor()
->getMock(); ->getMock();
$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,
@ -157,6 +148,7 @@ class UserWebauthnTest extends UnitTestCase
->onlyMethods(['getShopId']) ->onlyMethods(['getShopId'])
->getMock(); ->getMock();
$configMock->method('getShopId')->willReturn(1); $configMock->method('getShopId')->willReturn(1);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var Session|MockObject $sessionMock */ /** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class) $sessionMock = $this->getMockBuilder(Session::class)
@ -166,24 +158,12 @@ class UserWebauthnTest extends UnitTestCase
[WebauthnConf::WEBAUTHN_SESSION_AUTH, $authInSession], [WebauthnConf::WEBAUTHN_SESSION_AUTH, $authInSession],
[WebauthnConf::WEBAUTHN_SESSION_LOGINUSER, $userNameInSession], [WebauthnConf::WEBAUTHN_SESSION_LOGINUSER, $userNameInSession],
]); ]);
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
/** @var User|MockObject $sut */ /** @var User|MockObject $sut */
$sut = $this->getMockBuilder(User::class) $sut = $this->getMockBuilder(User::class)
->onlyMethods(['d3GetMockableRegistryObject', 'load']) ->onlyMethods(['load'])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock, $configMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
case Config::class:
return $configMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->expects($this->exactly((int) ($canLoad)))->method('load')->will( $sut->expects($this->exactly((int) ($canLoad)))->method('load')->will(
$userIsLoadable ? $userIsLoadable ?
$this->returnValue(true) : $this->returnValue(true) :
@ -235,22 +215,10 @@ class UserWebauthnTest extends UnitTestCase
->onlyMethods(['getShopId']) ->onlyMethods(['getShopId'])
->getMock(); ->getMock();
$configMock->method('getShopId')->willReturn($shopId); $configMock->method('getShopId')->willReturn($shopId);
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
/** @var User|MockObject $sut */ /** @var User $sut */
$sut = $this->getMockBuilder(User::class) $sut = oxNew(User::class);
->onlyMethods(['d3GetMockableRegistryObject'])
->getMock();
$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(
$expected, $expected,

View File

@ -0,0 +1,51 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\Webauthn\tests\unit\Modules;
use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Modules\WebauthnServices;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class WebauthnServicesTest extends WAUnitTestCase
{
use CanAccessRestricted;
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Modules\WebauthnServices::__construct
*/
public function canConstruct()
{
/** @var WebauthnServices|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnServices::class)
->disableOriginalConstructor()
->onlyMethods(['addYamlDefinitions', 'd3CallMockableFunction'])
->getMock();
$sut->expects($this->atLeastOnce())->method('addYamlDefinitions')->with(
$this->identicalTo('d3/oxwebauthn/Config/services.yaml')
);
$sut->method('d3CallMockableFunction')->willReturn(true);
$this->callMethod(
$sut,
'__construct'
);
}
}

View File

@ -17,6 +17,7 @@ namespace D3\Webauthn\tests\unit\Setup;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Setup\Actions; use D3\Webauthn\Setup\Actions;
use D3\Webauthn\tests\unit\WAUnitTestCase;
use Exception; use Exception;
use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface; use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface;
@ -32,25 +33,16 @@ use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\Sho
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ShopConfiguration; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ShopConfiguration;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\ModuleConfigurationNotFoundException; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\ModuleConfigurationNotFoundException;
use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
class ActionsTest extends UnitTestCase class ActionsTest extends WAUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
public function setUp(): void
{
parent::setUp();
//$seoEncoder = oxNew(SeoEncoder::class);
//$seoEncoder->addSeoEntry();
}
/** /**
* @test * @test
* @param $tableExist * @param $tableExist
@ -101,22 +93,10 @@ class ActionsTest extends UnitTestCase
->onlyMethods(['tableExists']) ->onlyMethods(['tableExists'])
->getMock(); ->getMock();
$DbMetaDataMock->expects($this->once())->method('tableExists')->willReturn($expected); $DbMetaDataMock->expects($this->once())->method('tableExists')->willReturn($expected);
d3GetOxidDIC()->set('d3ox.webauthn.'.DbMetaDataHandler::class, $DbMetaDataMock);
/** @var Actions|MockObject $sut */ /** @var Actions $sut */
$sut = $this->getMockBuilder(Actions::class) $sut = oxNew(Actions::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($DbMetaDataMock) {
$args = func_get_args();
switch ($args[0]) {
case DbMetaDataHandler::class:
return $DbMetaDataMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,
@ -188,22 +168,10 @@ class ActionsTest extends UnitTestCase
->onlyMethods(['fieldExists']) ->onlyMethods(['fieldExists'])
->getMock(); ->getMock();
$DbMetaDataMock->expects($this->once())->method('fieldExists')->willReturn($expected); $DbMetaDataMock->expects($this->once())->method('fieldExists')->willReturn($expected);
d3GetOxidDIC()->set('d3ox.webauthn.'.DbMetaDataHandler::class, $DbMetaDataMock);
/** @var Actions|MockObject $sut */ /** @var Actions $sut */
$sut = $this->getMockBuilder(Actions::class) $sut = oxNew(Actions::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($DbMetaDataMock) {
$args = func_get_args();
switch ($args[0]) {
case DbMetaDataHandler::class:
return $DbMetaDataMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,
@ -228,22 +196,10 @@ class ActionsTest extends UnitTestCase
->onlyMethods(['updateViews']) ->onlyMethods(['updateViews'])
->getMock(); ->getMock();
$DbMetaDataMock->expects($this->once())->method('updateViews'); $DbMetaDataMock->expects($this->once())->method('updateViews');
d3GetOxidDIC()->set('d3ox.webauthn.'.DbMetaDataHandler::class, $DbMetaDataMock);
/** @var Actions|MockObject $sut */ /** @var Actions $sut */
$sut = $this->getMockBuilder(Actions::class) $sut = oxNew(Actions::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($DbMetaDataMock) {
$args = func_get_args();
switch ($args[0]) {
case DbMetaDataHandler::class:
return $DbMetaDataMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -263,6 +219,7 @@ class ActionsTest extends UnitTestCase
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($throwException ? $this->atLeastOnce() : $this->never()) $loggerMock->expects($throwException ? $this->atLeastOnce() : $this->never())
->method('error')->willReturn(true); ->method('error')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var UtilsView|MockObject $utilsViewMock */ /** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class) $utilsViewMock = $this->getMockBuilder(UtilsView::class)
@ -270,6 +227,7 @@ class ActionsTest extends UnitTestCase
->getMock(); ->getMock();
$utilsViewMock->expects($throwException ? $this->atLeastOnce() : $this->never()) $utilsViewMock->expects($throwException ? $this->atLeastOnce() : $this->never())
->method('addErrorToDisplay'); ->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var Utils|MockObject $utilsMock */ /** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class) $utilsMock = $this->getMockBuilder(Utils::class)
@ -279,30 +237,17 @@ class ActionsTest extends UnitTestCase
->method('resetTemplateCache'); ->method('resetTemplateCache');
$utilsMock->expects($throwException ? $this->never() : $this->once()) $utilsMock->expects($throwException ? $this->never() : $this->once())
->method('resetLanguageCache'); ->method('resetLanguageCache');
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
/** @var Actions|MockObject $sut */ /** @var Actions|MockObject $sut */
$sut = $this->getMockBuilder(Actions::class) $sut = $this->getMockBuilder(Actions::class)
->onlyMethods(['d3GetMockableRegistryObject', 'getModuleTemplates', 'd3GetMockableLogger']) ->onlyMethods(['getModuleTemplates'])
->getMock(); ->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('getModuleTemplates')->will( $sut->method('getModuleTemplates')->will(
$throwException ? $throwException ?
$this->throwException(oxNew(ModuleConfigurationNotFoundException::class)) : $this->throwException(oxNew(ModuleConfigurationNotFoundException::class)) :
$this->returnValue([]) $this->returnValue([])
); );
$sut->method('d3GetMockableLogger')->willReturn($loggerMock);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -462,15 +407,17 @@ class ActionsTest extends UnitTestCase
->getMock(); ->getMock();
$utilsViewMock->expects($throwException ? $this->atLeastOnce() : $this->never()) $utilsViewMock->expects($throwException ? $this->atLeastOnce() : $this->never())
->method('addErrorToDisplay'); ->method('addErrorToDisplay');
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
/** @var LoggerInterface|MockObject $loggerMock */ /** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($throwException ? $this->atLeastOnce() : $this->never()) $loggerMock->expects($throwException ? $this->atLeastOnce() : $this->never())
->method('error')->willReturn(true); ->method('error')->willReturn(true);
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
/** @var Actions|MockObject $sut */ /** @var Actions|MockObject $sut */
$sut = $this->getMockBuilder(Actions::class) $sut = $this->getMockBuilder(Actions::class)
->onlyMethods(['hasSeoUrl', 'createSeoUrl', 'd3GetMockableLogger', 'd3GetMockableRegistryObject']) ->onlyMethods(['hasSeoUrl', 'createSeoUrl'])
->getMock(); ->getMock();
$sut->method('hasSeoUrl')->willReturn($hasSeoUrl); $sut->method('hasSeoUrl')->willReturn($hasSeoUrl);
$sut->expects($hasSeoUrl ? $this->never() : $this->once())->method('createSeoUrl')->will( $sut->expects($hasSeoUrl ? $this->never() : $this->once())->method('createSeoUrl')->will(
@ -478,18 +425,6 @@ class ActionsTest extends UnitTestCase
$this->throwException(oxNew(Exception::class)) : $this->throwException(oxNew(Exception::class)) :
$this->returnValue(true) $this->returnValue(true)
); );
$sut->method('d3GetMockableLogger')->willReturn($loggerMock);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsViewMock) {
$args = func_get_args();
switch ($args[0]) {
case UtilsView::class:
return $utilsViewMock;
default:
return Registry::get($args[0]);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,
@ -522,6 +457,7 @@ class ActionsTest extends UnitTestCase
->onlyMethods(['getStaticUrl']) ->onlyMethods(['getStaticUrl'])
->getMock(); ->getMock();
$seoEncoderMock->method('getStaticUrl')->willReturn($staticUrl); $seoEncoderMock->method('getStaticUrl')->willReturn($staticUrl);
d3GetOxidDIC()->set('d3ox.webauthn.'.SeoEncoder::class, $seoEncoderMock);
/** @var ViewConfig|MockObject $viewConfigMock */ /** @var ViewConfig|MockObject $viewConfigMock */
$viewConfigMock = $this->getMockBuilder(ViewConfig::class) $viewConfigMock = $this->getMockBuilder(ViewConfig::class)
@ -534,24 +470,10 @@ class ActionsTest extends UnitTestCase
->onlyMethods(['getViewConfig']) ->onlyMethods(['getViewConfig'])
->getMock(); ->getMock();
$controllerMock->method('getViewConfig')->willReturn($viewConfigMock); $controllerMock->method('getViewConfig')->willReturn($viewConfigMock);
d3GetOxidDIC()->set('d3ox.webauthn.'.FrontendController::class, $controllerMock);
/** @var Actions|MockObject $sut */ /** @var Actions $sut */
$sut = $this->getMockBuilder(Actions::class) $sut = oxNew(Actions::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($controllerMock, $seoEncoderMock) {
$args = func_get_args();
switch ($args[0]) {
case FrontendController::class:
return $controllerMock;
case SeoEncoder::class:
return $seoEncoderMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->assertSame( $this->assertSame(
$expected, $expected,
@ -586,22 +508,10 @@ class ActionsTest extends UnitTestCase
->onlyMethods(['addSeoEntry']) ->onlyMethods(['addSeoEntry'])
->getMock(); ->getMock();
$seoEncoderMock->expects($this->exactly(2))->method('addSeoEntry'); $seoEncoderMock->expects($this->exactly(2))->method('addSeoEntry');
d3GetOxidDIC()->set('d3ox.webauthn.'.SeoEncoder::class, $seoEncoderMock);
/** @var Actions|MockObject $sut */ /** @var Actions $sut */
$sut = $this->getMockBuilder(Actions::class) $sut = oxNew(Actions::class);
->onlyMethods(['d3GetMockableOxNewObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($seoEncoderMock) {
$args = func_get_args();
switch ($args[0]) {
case SeoEncoder::class:
return $seoEncoderMock;
default:
return call_user_func_array("oxNew", $args);
}
}
);
$this->callMethod( $this->callMethod(
$sut, $sut,

View File

@ -0,0 +1,60 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\Webauthn\tests\unit;
use D3\DIContainerHandler\d3DicHandler;
use OxidEsales\TestingLibrary\UnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WAUnitTestCase extends UnitTestCase
{
/**
* setup basic requirements
*/
public function setUp(): void
{
parent::setUp();
d3DicHandler::getUncompiledInstance();
}
public function tearDown(): void
{
parent::tearDown();
d3DicHandler::removeInstance();
}
/**
* @param $serviceName
* @param $serviceMock
*
* @return MockObject
*/
protected function getContainerMock($serviceName, $serviceMock): MockObject
{
$container = $this->getMockBuilder(ContainerInterface::class)
->onlyMethods(['get', 'has'])
->getMock();
$container->expects($this->any())
->method('get')
->with($this->equalTo($serviceName))
->will($this->returnValue($serviceMock));
return $container;
}
}