diff --git a/composer.json b/composer.json index 0b06d70..9f63a0d 100644 --- a/composer.json +++ b/composer.json @@ -40,6 +40,9 @@ "ext-soap": "*", "ext-PDO": "*" }, + "require-dev": { + "phpstan/phpstan": "^1.8" + }, "autoload": { "psr-4": { "D3\\Webauthn\\": "../../../source/modules/d3/oxwebauthn" diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..431a7e9 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,13 @@ +parameters: + scanFiles: + - src/IntelliSenseHelper.php + - ../../oxid-esales/oxideshop-ce/source/oxfunctions.php + paths: + - src + level: 5 + phpVersion: 70400 + ignoreErrors: + - '#Call to an undefined method OxidEsales\\Eshop\\Core\\Controller\\BaseController::getNavigationParams\(\)\.#' + - '#Call to an undefined method PHPUnit\\Framework\\MockObject\\MockObject::.*#' + - '#Return type \(array\) of method D3\\Webauthn\\Application\\Controller\\d3webauthnlogin::getBreadCrumb\(\) should be compatible with return type \(null\) of method OxidEsales\\EshopCommunity\\Application\\Controller\\FrontendController::getBreadCrumb\(\)#' + - '#Method D3\\Webauthn\\Application\\Model\\RelyingPartyEntity::getActiveShop\(\) should return OxidEsales\\Eshop\\Application\\Model\\Shop but returns oxShop.#' \ No newline at end of file diff --git a/src/Application/Controller/Admin/d3user_webauthn.php b/src/Application/Controller/Admin/d3user_webauthn.php index df35cd0..5172ea2 100755 --- a/src/Application/Controller/Admin/d3user_webauthn.php +++ b/src/Application/Controller/Admin/d3user_webauthn.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Controller\Admin; +use Assert\AssertionFailedException; use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; @@ -32,6 +33,7 @@ use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Throwable; class d3user_webauthn extends AdminDetailsController { @@ -52,7 +54,7 @@ class d3user_webauthn extends AdminDetailsController $soxId = $this->getEditObjectId(); - if (isset($soxId) && $soxId != "-1") { + if ($soxId != "-1") { /** @var d3_User_Webauthn $oUser */ $oUser = $this->d3GetMockableOxNewObject(User::class); if ($oUser->load($soxId)) { @@ -79,7 +81,7 @@ class d3user_webauthn extends AdminDetailsController $this->setPageType( 'requestnew' ); $this->setAuthnRegister(); } catch (Exception|ContainerExceptionInterface|NotFoundExceptionInterface|DoctrineDriverException $e) { - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); $this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); $this->d3GetMockableLogger()->debug($e->getTraceAsString()); $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=d3user_webauthn'); @@ -88,6 +90,8 @@ class d3user_webauthn extends AdminDetailsController /** * @return void + * @throws AssertionFailedException + * @throws Throwable */ public function saveAuthn(): void { @@ -104,10 +108,14 @@ class d3user_webauthn extends AdminDetailsController $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter( 'keyname' ) ); } - } catch (WebauthnException|Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) { + } catch (WebauthnException $e) { $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]); $this->d3GetMockableLogger()->debug($e->getTraceAsString()); $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + } catch (Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) { + $this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); + $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); } } diff --git a/src/Application/Controller/d3_account_webauthn.php b/src/Application/Controller/d3_account_webauthn.php index aa2cd9b..619c992 100755 --- a/src/Application/Controller/d3_account_webauthn.php +++ b/src/Application/Controller/d3_account_webauthn.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Controller; +use Assert\AssertionFailedException; use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Controller\Traits\accountTrait; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; @@ -30,6 +31,7 @@ use OxidEsales\Eshop\Core\SeoEncoder; use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Throwable; class d3_account_webauthn extends AccountController { @@ -94,7 +96,6 @@ class d3_account_webauthn extends AccountController } /** - * @throws WebauthnException * @throws DoctrineDriverException * @throws DoctrineException * @throws ContainerExceptionInterface @@ -113,10 +114,10 @@ class d3_account_webauthn extends AccountController /** * @return void - * @throws ContainerExceptionInterface * @throws DoctrineDriverException * @throws DoctrineException - * @throws NotFoundExceptionInterface + * @throws AssertionFailedException + * @throws Throwable */ public function saveAuthn(): void { @@ -161,11 +162,19 @@ class d3_account_webauthn extends AccountController $iBaseLanguage = Registry::getLang()->getBaseLanguage(); /** @var SeoEncoder $oSeoEncoder */ $oSeoEncoder = Registry::getSeoEncoder(); - $aPath['title'] = Registry::getLang()->translateString('MY_ACCOUNT', $iBaseLanguage, false); + $aPath['title'] = Registry::getLang()->translateString( + 'MY_ACCOUNT', + (int) $iBaseLanguage, + false + ); $aPath['link'] = $oSeoEncoder->getStaticUrl($this->getViewConfig()->getSelfLink() . "cl=account"); $aPaths[] = $aPath; - $aPath['title'] = Registry::getLang()->translateString('D3_WEBAUTHN_ACCOUNT', $iBaseLanguage, false); + $aPath['title'] = Registry::getLang()->translateString( + 'D3_WEBAUTHN_ACCOUNT', + (int) $iBaseLanguage, + false + ); $aPath['link'] = $this->getLink(); $aPaths[] = $aPath; diff --git a/src/Application/Controller/d3webauthnlogin.php b/src/Application/Controller/d3webauthnlogin.php index 23f7d2c..527f205 100755 --- a/src/Application/Controller/d3webauthnlogin.php +++ b/src/Application/Controller/d3webauthnlogin.php @@ -145,7 +145,11 @@ class d3webauthnlogin extends FrontendController $aPaths = []; $aPath = []; $iBaseLanguage = Registry::getLang()->getBaseLanguage(); - $aPath['title'] = Registry::getLang()->translateString('D3_WEBAUTHN_BREADCRUMB', $iBaseLanguage, false); + $aPath['title'] = Registry::getLang()->translateString( + 'D3_WEBAUTHN_BREADCRUMB', + (int) $iBaseLanguage, + false + ); $aPath['link'] = $this->getLink(); $aPaths[] = $aPath; diff --git a/src/Application/Model/Credential/PublicKeyCredential.php b/src/Application/Model/Credential/PublicKeyCredential.php index fe69504..e103aec 100755 --- a/src/Application/Model/Credential/PublicKeyCredential.php +++ b/src/Application/Model/Credential/PublicKeyCredential.php @@ -72,11 +72,11 @@ class PublicKeyCredential extends BaseModel } /** - * @return false|string + * @return null|string */ public function getCredentialId(): ?string { - return base64_decode($this->__get($this->_getFieldLongName('credentialid'))->rawValue); + return base64_decode($this->__get($this->_getFieldLongName('credentialid'))->rawValue) ?: null; } /** @@ -108,11 +108,12 @@ class PublicKeyCredential extends BaseModel } /** - * @return false|PublicKeyCredentialSource + * @return null|PublicKeyCredentialSource */ public function getCredential(): ?PublicKeyCredentialSource { - return unserialize(base64_decode($this->__get($this->_getFieldLongName('credential'))->rawValue)); + return unserialize(base64_decode($this->__get($this->_getFieldLongName('credential'))->rawValue)) ?: + null; } /** diff --git a/src/Application/Model/Credential/PublicKeyCredentialList.php b/src/Application/Model/Credential/PublicKeyCredentialList.php index 5b92b5e..66b0109 100755 --- a/src/Application/Model/Credential/PublicKeyCredentialList.php +++ b/src/Application/Model/Credential/PublicKeyCredentialList.php @@ -158,6 +158,8 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo */ public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void { - $this->getBaseObject()->saveCredentialSource($publicKeyCredentialSource); + /** @var PublicKeyCredential $base */ + $base = $this->getBaseObject(); + $base->saveCredentialSource($publicKeyCredentialSource); } } \ No newline at end of file diff --git a/src/Application/Model/Webauthn.php b/src/Application/Model/Webauthn.php index c7e88f3..7251dbc 100644 --- a/src/Application/Model/Webauthn.php +++ b/src/Application/Model/Webauthn.php @@ -107,7 +107,7 @@ class Webauthn public function getExistingCredentials(UserEntity $userEntity): array { // Get the list of authenticators associated to the user - /** @var PublicKeyCredentialList $credentialSourceRepository */ + /** @var PublicKeyCredentialList $credentialList */ $credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); $credentialSources = $credentialList->findAllForUserEntity($userEntity); @@ -249,7 +249,7 @@ class Webauthn /** @var User $user */ $user = $this->d3GetMockableOxNewObject(User::class); $user->load($userId); - /** @var UserEntity $userEntity */ + return $this->d3GetMockableOxNewObject(UserEntity::class, $user); } diff --git a/src/Application/Model/WebauthnErrors.php b/src/Application/Model/WebauthnErrors.php index aa133a8..862ba63 100644 --- a/src/Application/Model/WebauthnErrors.php +++ b/src/Application/Model/WebauthnErrors.php @@ -33,10 +33,10 @@ class WebauthnErrors /** * @param $msg - * @param null $type + * @param null|string $type * @return string */ - public function translateError($msg, $type = null): string + public function translateError(string $msg, string $type = null): string { $lang = $this->d3GetMockableRegistryObject(Language::class); $type = $type ? '_'.$type : null; @@ -58,8 +58,7 @@ class WebauthnErrors return $lang->translateString('D3_WEBAUTHN_ERR_NOPUBKEYSUPPORT', null, true); } - switch (strtoupper($msg)) { - case self::UNSECURECONNECTION: + if (strtoupper($msg) === self::UNSECURECONNECTION) { return $lang->translateString($msg); } diff --git a/src/Application/Model/WebauthnLogin.php b/src/Application/Model/WebauthnLogin.php index ec0d954..19d11fc 100644 --- a/src/Application/Model/WebauthnLogin.php +++ b/src/Application/Model/WebauthnLogin.php @@ -101,7 +101,6 @@ class WebauthnLogin * @param UserComponent $usrCmp * @param bool $setSessionCookie * @return void - * @throws WebauthnLoginErrorException */ public function frontendLogin(UserComponent $usrCmp, bool $setSessionCookie = false) { @@ -132,13 +131,9 @@ class WebauthnLogin return; } catch (UserException $oEx) { // for login component send exception text to a custom component (if defined) - $myUtilsView->addErrorToDisplay($oEx, false, true, '', false); - - //return 'user'; + $myUtilsView->addErrorToDisplay($oEx, false, true); } catch (CookieException $oEx) { $myUtilsView->addErrorToDisplay($oEx); - - //return 'user'; } catch (WebauthnException $e) { $myUtilsView->addErrorToDisplay($e); $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); @@ -146,8 +141,7 @@ class WebauthnLogin } $user->logout(); - $exc = oxNew(WebauthnLoginErrorException::class); - throw $exc; + throw oxNew(WebauthnLoginErrorException::class); } /** @@ -301,7 +295,7 @@ class WebauthnLogin if ($iSubshop) { $session->setVariable("shp", $iSubshop); $session->setVariable('currentadminshop', $iSubshop); - $this->d3GetMockableRegistryObject(Config::class)->setShopId($iSubshop); + $this->d3GetMockableRegistryObject(Config::class)->setShopId((string) $iSubshop); } } @@ -331,9 +325,8 @@ class WebauthnLogin */ public function updateBasket(): void { - if ($oBasket = $this->d3GetMockableRegistryObject(Session::class)->getBasket()) { - $oBasket->onUpdate(); - } + $oBasket = $this->d3GetMockableRegistryObject(Session::class)->getBasket(); + $oBasket->onUpdate(); } /** diff --git a/src/Application/views/admin/tpl/d3webauthnlogin.tpl b/src/Application/views/admin/tpl/d3webauthnlogin.tpl index 8778948..20f25b3 100644 --- a/src/Application/views/admin/tpl/d3webauthnlogin.tpl +++ b/src/Application/views/admin/tpl/d3webauthnlogin.tpl @@ -1,5 +1,5 @@ - + [{oxmultilang ident="LOGIN_TITLE"}] diff --git a/src/Modules/Application/Component/d3_webauthn_UserComponent.php b/src/Modules/Application/Component/d3_webauthn_UserComponent.php index 94efde6..912e039 100755 --- a/src/Modules/Application/Component/d3_webauthn_UserComponent.php +++ b/src/Modules/Application/Component/d3_webauthn_UserComponent.php @@ -151,8 +151,10 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'), $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error') ); - $login->frontendLogin($this, (bool)$this->d3GetMockableRegistryObject(Request::class) - ->getRequestParameter('lgn_cook')); + $login->frontendLogin( + $this, + (bool)$this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_cook') + ); $this->_afterLogin($this->getUser()); } catch (WebauthnGetException $e) { $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); diff --git a/src/Setup/Actions.php b/src/Setup/Actions.php index 76b64cb..8192dd9 100644 --- a/src/Setup/Actions.php +++ b/src/Setup/Actions.php @@ -47,9 +47,9 @@ class Actions /** * SQL statement, that will be executed only at the first time of module installation. * - * @var array + * @var string */ - protected $createCredentialSql = + protected $createCredentialSql = "CREATE TABLE `d3wa_usercredentials` ( `OXID` char(32) NOT NULL, `OXUSERID` char(32) NOT NULL, @@ -144,7 +144,7 @@ class Actions $oUtils->resetLanguageCache(); } catch (ContainerExceptionInterface|NotFoundExceptionInterface|ModuleConfigurationNotFoundException $e) { $this->d3GetMockableLogger()->error($e->getMessage(), [$this]); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); } } @@ -230,8 +230,6 @@ class Actions /** * @return void - * @throws DatabaseConnectionException - * @throws DatabaseErrorException */ public function createSeoUrl() { @@ -243,7 +241,7 @@ class Actions 'index.php?cl='.$this->stdClassName, $this->seo_de, 'static', - 0 + false ); $seoEncoder->addSeoEntry( 'ff57646b47249ee33c6b672741ac371a', @@ -252,7 +250,7 @@ class Actions 'index.php?cl='.$this->stdClassName, $this->seo_en, 'static', - 0 + false ); } diff --git a/src/tests/additional.inc.php b/src/tests/additional.inc.php index 9b9823d..b51d7b3 100644 --- a/src/tests/additional.inc.php +++ b/src/tests/additional.inc.php @@ -16,20 +16,8 @@ namespace D3\Webauthn\tests; use D3\ModCfg\Tests\additional_abstract; use OxidEsales\Eshop\Core\Exception\StandardException; -include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'd3webauthn_config.php'); - class additional extends additional_abstract { - /** - * additional constructor. - * @throws StandardException - */ - public function __construct() - { - if (D3WEBAUTHN_REQUIRE_MODCFG) { - $this->reactivateModCfg(); - } - } } oxNew(additional::class); diff --git a/src/tests/integration/integrationTestCase.php b/src/tests/integration/integrationTestCase.php index c6343ca..f8901ca 100644 --- a/src/tests/integration/integrationTestCase.php +++ b/src/tests/integration/integrationTestCase.php @@ -21,6 +21,7 @@ use D3\ModCfg\Application\Model\DependencyInjectionContainer\d3DicHandler; use D3\ModCfg\Tests\unit\d3ModCfgUnitTestCase; use Exception; use OxidEsales\Eshop\Application\Model\Article; +use OxidEsales\Eshop\Application\Model\Rights; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Model\BaseModel; @@ -61,7 +62,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase * @param bool $blAdmin * @throws Exception */ - public function createObject($sClass, $sId, $aFields = [], $blAdmin = false) + public function createObject($sClass, $sId, array $aFields = [], bool $blAdmin = false) { /** @var BaseModel $oObject */ $oObject = oxNew($sClass); @@ -83,7 +84,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase * @param array $aFields * @throws Exception */ - public function createBaseModelObject($sTableName, $sId, $aFields = []) + public function createBaseModelObject($sTableName, $sId, array $aFields = []) { /** @var BaseModel $oObject */ $oObject = oxNew(BaseModel::class); @@ -98,7 +99,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase * @param array $aFields * @throws Exception */ - public function createArticle($sId, $aFields = []) + public function createArticle($sId, array $aFields = []) { $this->createObject( Article::class, @@ -116,7 +117,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase * @param bool $blAdmin * @throws Exception */ - public function createUser($sId, $aFields = [], $blAdmin = false) + public function createUser($sId, array $aFields = [], bool $blAdmin = false) { $this->createObject( User::class, @@ -136,7 +137,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase /** @var BaseModel $oObject */ $oObject = oxNew($sClass); if (method_exists($oObject, 'setRights')) { - $oObject->setRights(null); + $oObject->setRights(oxNew(Rights::class)); } if ($oObject->exists($sId)) { $oObject->delete($sId); @@ -156,7 +157,7 @@ abstract class integrationTestCase extends d3ModCfgUnitTestCase $oObject = oxNew(BaseModel::class); $oObject->init($sTableName); if (method_exists($oObject, 'setRights')) { - $oObject->setRights(null); + $oObject->setRights(oxNew(Rights::class)); } if ($oObject->exists($sId)) { $oObject->delete($sId); diff --git a/src/tests/integration/passwordFrontendAuthTest.php b/src/tests/integration/passwordFrontendAuthTest.php index 6aafe1e..a043fb5 100644 --- a/src/tests/integration/passwordFrontendAuthTest.php +++ b/src/tests/integration/passwordFrontendAuthTest.php @@ -46,7 +46,7 @@ class passwordFrontendAuthTest extends integrationTestCase 'oxactive' => 1, 'oxrights' => 'user', 'oxshopid' => 2, - 'oxusername' => 'wrongshop@user.localhost', + 'oxusername' => 'wrongshop_fe@user.localhost', 'oxpassword' => '$2y$10$b3O5amXZVMGGZbL4X10TIOHiOwEkq3C0ofObuTgHAS4Io0uMLauUS', // 123456 'oxstreet' => __CLASS__ ] @@ -81,7 +81,7 @@ class passwordFrontendAuthTest extends integrationTestCase $_POST['lgn_usr'] = $username; $_POST['lgn_pwd'] = $password; - /** @var AccountController $login */ + /** @var AccountController $controller */ $controller = oxNew(AccountController::class); $controller->init(); /** @var d3_webauthn_UserComponent $component */ @@ -104,7 +104,7 @@ class passwordFrontendAuthTest extends integrationTestCase 'not existing account' => ['unknown@user.localhost', '123456', 'user'], 'missing password' => ['noadmin@user.localhost', null, 'user'], 'inactive account' => ['inactive@user.localhost', '123456', 'user'], - 'wrong shop account' => ['wrongshop@user.localhost', '123456', 'user'], + 'wrong shop account' => ['wrongshop_fe@user.localhost', '123456', 'user'], 'account ok' => ['noadmin@user.localhost', '123456', 'payment'], ]; } diff --git a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php index 5755e93..49ceed4 100644 --- a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php +++ b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php @@ -91,10 +91,13 @@ class PublicKeyCredentialListTest extends UnitTestCase } ); + /** @var PublicKeyCredentialSource|MockObject $pkcsMock */ + $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) + ->disableOriginalConstructor() + ->getMock(); + if ($doCreate) { - $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var PublicKeyCredential|MockObject $pkc */ $pkc = $this->getMockBuilder(PublicKeyCredential::class) ->onlyMethods(['allowDerivedDelete']) ->getMock(); @@ -169,10 +172,11 @@ class PublicKeyCredentialListTest extends UnitTestCase } ); + $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) + ->disableOriginalConstructor() + ->getMock(); + if ($doCreate) { - $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) - ->disableOriginalConstructor() - ->getMock(); foreach ($oxids as $oxid) { $pkc = $this->getMockBuilder(PublicKeyCredential::class) ->onlyMethods(['allowDerivedDelete']) diff --git a/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php b/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php index fbecfba..90e8ffa 100644 --- a/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php +++ b/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php @@ -18,6 +18,7 @@ namespace D3\Webauthn\tests\unit\Application\Model\Exceptions; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\WebauthnErrors; +use Exception; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; @@ -34,8 +35,8 @@ class WebauthnExceptionTest extends UnitTestCase */ public function canConstruct() { - /** @var \Exception|MockObject $previousMock */ - $previousMock = $this->getMockBuilder(\Exception::class) + /** @var Exception|MockObject $previousMock */ + $previousMock = $this->getMockBuilder(Exception::class) ->getMock(); /** @var WebauthnErrors|MockObject $translatorMock */ diff --git a/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php b/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php index 34fc078..dca0526 100644 --- a/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php +++ b/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php @@ -26,6 +26,7 @@ use OxidEsales\Eshop\Core\UtilsServer; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; +use stdClass; class WebauthnAfterLoginTest extends UnitTestCase { @@ -115,10 +116,11 @@ class WebauthnAfterLoginTest extends UnitTestCase * @param $requestLang * @param $sessionLang * @param $expectedLang + * @param $expectedAbbr * @return void * @throws ReflectionException * @dataProvider canChangeLanguageDataProvider - * @covers \D3\Webauthn\Application\Model\WebauthnAfterLogin::changeLanguage + * @covers \D3\Webauthn\Application\Model\WebauthnAfterLogin::changeLanguage */ public function canChangeLanguage($requestLang, $sessionLang, $expectedLang, $expectedAbbr) { @@ -189,7 +191,7 @@ class WebauthnAfterLoginTest extends UnitTestCase */ public function getConfiguredLanguageStub(): array { - $de_1 = oxNew(\stdClass::class); + $de_1 = oxNew(stdClass::class); $de_1->id = 0; $de_1->oxid = 'de'; $de_1->abbr = 'de'; @@ -198,7 +200,7 @@ class WebauthnAfterLoginTest extends UnitTestCase $de_1->sort = '1'; $de_1->selected = 0; - $en_2 = oxNew(\stdClass::class); + $en_2 = oxNew(stdClass::class); $en_2->id = 1; $en_2->oxid = 'en'; $en_2->abbr = 'en'; diff --git a/src/tests/unit/Application/Model/WebauthnLoginTest.php b/src/tests/unit/Application/Model/WebauthnLoginTest.php index 4b55d1e..38f53ba 100644 --- a/src/tests/unit/Application/Model/WebauthnLoginTest.php +++ b/src/tests/unit/Application/Model/WebauthnLoginTest.php @@ -987,25 +987,23 @@ class WebauthnLoginTest extends UnitTestCase /** * @test - * @param $canGetBasket * @return void * @throws ReflectionException - * @dataProvider canUpdateBasketDataProvider * @covers \D3\Webauthn\Application\Model\WebauthnLogin::updateBasket */ - public function canUpdateBasket($canGetBasket) + public function canUpdateBasket() { /** @var Basket|MockObject $basketMock */ $basketMock = $this->getMockBuilder(Basket::class) ->onlyMethods(['onUpdate']) ->getMock(); - $basketMock->expects($this->exactly((int) $canGetBasket))->method('onUpdate'); + $basketMock->expects($this->once())->method('onUpdate'); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) ->onlyMethods(['getBasket']) ->getMock(); - $sessionMock->method('getBasket')->willReturn($canGetBasket ? $basketMock : null); + $sessionMock->method('getBasket')->willReturn($basketMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) @@ -1030,17 +1028,6 @@ class WebauthnLoginTest extends UnitTestCase ); } - /** - * @return array - */ - public function canUpdateBasketDataProvider(): array - { - return [ - 'can get basket' => [true], - 'cant get basket' => [false], - ]; - } - /** * @test * @return void diff --git a/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php b/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php index 9546111..530b334 100644 --- a/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php +++ b/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php @@ -24,7 +24,6 @@ use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent; use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent_parent; use OxidEsales\Eshop\Application\Component\UserComponent; -use OxidEsales\Eshop\Application\Controller\Admin\LoginController; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Controller\BaseController; use OxidEsales\Eshop\Core\Registry; @@ -68,10 +67,11 @@ class UserComponentWebauthnTest extends UnitTestCase * @param $canUseWebauthn * @param $loggedin * @param $setVariableCount + * @param $doRedirect * @return void * @throws ReflectionException * @dataProvider canCheckloginDataProvider - * @covers \D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent::d3WebauthnLogin + * @covers \D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent::d3WebauthnLogin */ public function canWebauthnLogin($canUseWebauthn, $loggedin, $setVariableCount, $doRedirect) { diff --git a/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php index 9b22375..9882adf 100644 --- a/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php +++ b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php @@ -53,17 +53,16 @@ trait CheckoutTestTrait /** * @test * @param $hasUser - * @param $userId * @param $isActive * @param $sessionAuth * @param $expected * @return void * @throws ReflectionException * @dataProvider canGetUserDataProvider - * @covers \D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait::getUser - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController::getUser - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::getUser - * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::getUser + * @covers \D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait::getUser + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController::getUser + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::getUser + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::getUser */ public function canGetUser($hasUser, $isActive, $sessionAuth, $expected) { @@ -115,13 +114,20 @@ trait CheckoutTestTrait 'getUser' ); - $sut->setUser(null); + $sut->setUser(oxNew(User::class)); if ($expected === 'parent') { - $this->assertSame($return, $hasUser ? $this->userFixture : false); + $this->assertSame( $return, $hasUser ? $this->userFixture : false); } else { $this->assertSame($return, $expected); } + + // reset cache + $this->setValue( + $sut, + '_oActUser', + null + ); } /** diff --git a/src/tests/unit/Setup/ActionsTest.php b/src/tests/unit/Setup/ActionsTest.php index 98e4015..68de8c8 100644 --- a/src/tests/unit/Setup/ActionsTest.php +++ b/src/tests/unit/Setup/ActionsTest.php @@ -17,6 +17,7 @@ namespace D3\Webauthn\tests\unit\Setup; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Setup\Actions; +use Exception; use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface; use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database; @@ -474,7 +475,7 @@ class ActionsTest extends UnitTestCase $sut->method('hasSeoUrl')->willReturn($hasSeoUrl); $sut->expects($hasSeoUrl ? $this->never() : $this->once())->method('createSeoUrl')->will( $throwException ? - $this->throwException(oxNew(\Exception::class)) : + $this->throwException(oxNew(Exception::class)) : $this->returnValue(true) ); $sut->method('d3GetMockableLogger')->willReturn($loggerMock);