From f3d8e55db171a55dd186cd553a9dd74f39f94457 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Thu, 1 Dec 2022 00:45:39 +0100 Subject: [PATCH] add further tests --- .../Traits/checkoutGetUserTrait.php | 5 +- src/Application/Model/RelyingPartyEntity.php | 74 ++++- .../Admin/d3webauthnadminloginTest.php | 5 - .../Model/RelyingPartyEntityTest.php | 257 ++++++++++++++++++ .../Controller/AccountControllerTest.php | 26 ++ .../AccountDownloadsControllerTest.php | 26 ++ .../AccountNewsletterControllerTest.php | 26 ++ .../AccountNoticeListControllerTest.php | 26 ++ .../Controller/AccountOrderControllerTest.php | 26 ++ .../AccountPasswordControllerTest.php | 26 ++ .../AccountRecommlistControllerTest.php | 26 ++ .../AccountReviewControllerTest.php | 26 ++ .../Controller/AccountTestTrait.php | 73 +++++ .../Controller/AccountUserControllerTest.php | 26 ++ .../AccountWishlistControllerTest.php | 26 ++ .../Controller/CheckoutTestTrait.php | 152 +++++++++++ .../Controller/OrderControllerTest.php | 26 ++ .../Controller/PaymentControllerTest.php | 26 ++ .../Controller/UserControllerTest.php | 26 ++ 19 files changed, 891 insertions(+), 13 deletions(-) create mode 100644 src/tests/unit/Application/Model/RelyingPartyEntityTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountTestTrait.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php create mode 100644 src/tests/unit/Modules/Application/Controller/OrderControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php create mode 100644 src/tests/unit/Modules/Application/Controller/UserControllerTest.php diff --git a/src/Application/Controller/Traits/checkoutGetUserTrait.php b/src/Application/Controller/Traits/checkoutGetUserTrait.php index 1902498..9ca55f4 100755 --- a/src/Application/Controller/Traits/checkoutGetUserTrait.php +++ b/src/Application/Controller/Traits/checkoutGetUserTrait.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Controller\Traits; +use D3\TestingTools\Production\IsMockable; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; use Doctrine\DBAL\Driver\Exception; @@ -27,6 +28,8 @@ use Psr\Container\NotFoundExceptionInterface; trait checkoutGetUserTrait { + use IsMockable; + /** * @return false|User * @throws ContainerExceptionInterface @@ -36,7 +39,7 @@ trait checkoutGetUserTrait */ public function getUser() { - $user = parent::getUser(); + $user = $this->d3CallMockableParent('getUser'); if ($user && $user->getId()) { $webauthn = $this->d3GetWebauthnObject(); diff --git a/src/Application/Model/RelyingPartyEntity.php b/src/Application/Model/RelyingPartyEntity.php index a94c997..b769ad0 100755 --- a/src/Application/Model/RelyingPartyEntity.php +++ b/src/Application/Model/RelyingPartyEntity.php @@ -15,20 +15,80 @@ declare(strict_types=1); namespace D3\Webauthn\Application\Model; +use D3\TestingTools\Production\IsMockable; +use OxidEsales\Eshop\Application\Model\Shop; +use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Registry; use Webauthn\PublicKeyCredentialRpEntity; class RelyingPartyEntity extends PublicKeyCredentialRpEntity { + use IsMockable; + public function __construct() { - $shopUrl = is_string(Registry::getConfig()->getConfigParam('d3webauthn_diffshopurl')) ? - trim(Registry::getConfig()->getConfigParam('d3webauthn_diffshopurl')) : - null; - - parent::__construct( - Registry::getConfig()->getActiveShop()->getFieldData('oxname'), - $shopUrl ?: preg_replace('/(^www\.)(.*)/mi', '$2', $_SERVER['HTTP_HOST']) + $this->d3CallMockableParent( + '__construct', + [ + $this->getActiveShop()->getFieldData('oxname'), + $this->getRPShopUrl() + ] ); +/** + parent::__construct( + $this->getActiveShop()->getFieldData('oxname'), + $this->getRPShopUrl() + ); + */ + } + + /** + * @return bool + */ + public function hasConfiguredShopUrl(): bool + { + return (bool) strlen(trim((string) $this->getConfiguredShopUrl())); + } + + /** + * @return mixed + */ + public function getConfiguredShopUrl() + { + return $this->getConfig()->getConfigParam('d3webauthn_diffshopurl'); + } + + /** + * @return string + */ + public function getShopUrlByHost(): string + { + return preg_replace('/(^www\.)(.*)/mi', '$2', $_SERVER['HTTP_HOST']); + } + + /** + * @return string|null + */ + public function getRPShopUrl(): ?string + { + return $this->hasConfiguredShopUrl() ? + trim($this->getConfiguredShopUrl()) : + $this->getShopUrlByHost(); + } + + /** + * @return Config + */ + public function getConfig(): Config + { + return Registry::getConfig(); + } + + /** + * @return Shop + */ + public function getActiveShop(): Shop + { + return Registry::getConfig()->getActiveShop(); } } \ No newline at end of file diff --git a/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php b/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php index 3c3e72c..1c742c4 100644 --- a/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php +++ b/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php @@ -18,15 +18,11 @@ namespace D3\Webauthn\tests\unit\Application\Controller\Admin; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin; use D3\Webauthn\Application\Controller\d3webauthnlogin; -use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; -use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnAfterLogin; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\tests\unit\Application\Controller\d3webauthnloginTest; -use OxidEsales\Eshop\Application\Controller\Admin\LoginController; -use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\SystemEventHandler; @@ -34,7 +30,6 @@ use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsServer; use OxidEsales\Eshop\Core\UtilsView; use PHPUnit\Framework\MockObject\MockObject; -use Psr\Log\LoggerInterface; use ReflectionException; class d3webauthnadminloginTest extends d3webauthnloginTest diff --git a/src/tests/unit/Application/Model/RelyingPartyEntityTest.php b/src/tests/unit/Application/Model/RelyingPartyEntityTest.php new file mode 100644 index 0000000..b2d20b2 --- /dev/null +++ b/src/tests/unit/Application/Model/RelyingPartyEntityTest.php @@ -0,0 +1,257 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Application\Model; + +use D3\TestingTools\Development\CanAccessRestricted; +use D3\Webauthn\Application\Model\RelyingPartyEntity; +use OxidEsales\Eshop\Application\Model\Shop; +use OxidEsales\Eshop\Core\Config; +use OxidEsales\TestingLibrary\UnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class RelyingPartyEntityTest extends UnitTestCase +{ + use CanAccessRestricted; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::__construct + */ + public function canConstruct() + { + /** @var Shop|MockObject $shopMock */ + $shopMock = $this->getMockBuilder(Shop::class) + ->onlyMethods(['getFieldData']) + ->getMock(); + $shopMock->method('getFieldData')->with($this->identicalTo('oxname'))->willReturn('myShopName'); + + /** @var RelyingPartyEntity|MockObject $sut */ + $sut = $this->getMockBuilder(RelyingPartyEntity::class) + ->disableOriginalConstructor() + ->onlyMethods(['d3CallMockableParent', 'getActiveShop', 'getRPShopUrl']) + ->getMock(); + $sut->expects($this->once())->method('d3CallMockableParent')->with( + $this->anything(), + $this->identicalTo(['myShopName', 'myShopUrl']) + ); + $sut->method('getActiveShop')->willReturn($shopMock); + $sut->method('getRPShopUrl')->willReturn('myShopUrl'); + + $this->callMethod( + $sut, + '__construct' + ); + } + + /** + * @test + * @param $configuredShopUrl + * @param $expected + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::hasConfiguredShopUrl + * @dataProvider checkHasConfiguredShopUrlDataProvider + */ + public function checkHasConfiguredShopUrl($configuredShopUrl, $expected) + { + /** @var RelyingPartyEntity|MockObject $sut */ + $sut = $this->getMockBuilder(RelyingPartyEntity::class) + ->disableOriginalConstructor() + ->onlyMethods(['getConfiguredShopUrl']) + ->getMock(); + $sut->method('getConfiguredShopUrl')->willReturn($configuredShopUrl); + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'hasConfiguredShopUrl' + ) + ); + } + + /** + * @return array + */ + public function checkHasConfiguredShopUrlDataProvider(): array + { + return [ + 'null' => [null, false], + 'empty string' => ['', false], + 'space string' => [' ', false], + 'non empty string' => ['content', true] + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::getConfiguredShopUrl + */ + public function canGetConfiguredShopUrl() + { + $fixture = 'configuredShopUrl'; + + /** @var Config|MockObject $configMock */ + $configMock = $this->getMockBuilder(Config::class) + ->onlyMethods(['getConfigParam']) + ->getMock(); + $configMock->method('getConfigParam')->with($this->identicalTo('d3webauthn_diffshopurl')) + ->willReturn($fixture); + + /** @var RelyingPartyEntity|MockObject $sut */ + $sut = $this->getMockBuilder(RelyingPartyEntity::class) + ->disableOriginalConstructor() + ->onlyMethods(['getConfig']) + ->getMock(); + $sut->method('getConfig')->willReturn($configMock); + + $this->assertSame( + $fixture, + $this->callMethod( + $sut, + 'getConfiguredShopUrl' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::getShopUrlByHost + * @dataProvider canGetShopUrlByHostDataProvider + */ + public function canGetShopUrlByHost($host, $expected) + { + $_SERVER['HTTP_HOST'] = $host; + + /** @var RelyingPartyEntity|MockObject $sut */ + $sut = $this->getMockBuilder(RelyingPartyEntity::class) + ->onlyMethods(['d3CallMockableParent']) // must mock, because can't disable constructor + ->getMock(); + $sut->method('d3CallMockableParent')->willReturn(true); + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'getShopUrlByHost' + ) + ); + } + + /** + * @return array + */ + public function canGetShopUrlByHostDataProvider(): array + { + return [ + 'base url' => ['mydomain.com', 'mydomain.com'], + 'www url' => ['www.mydomain.com', 'mydomain.com'], + 'www2 url' => ['www2.mydomain.com', 'www2.mydomain.com'], + 'subdomain url' => ['subd.mydomain.com', 'subd.mydomain.com'], + 'subdomain www url' => ['subd.www.mydomain.com', 'subd.www.mydomain.com'], + 'multipart TLD' => ['www.mydomain.co.uk', 'mydomain.co.uk'], + ]; + } + + /** + * @test + * @param $hasConfiguredUrl + * @param $configuredUrl + * @param $hostUrl + * @param $expected + * @return void + * @throws ReflectionException + * @dataProvider canGetRPShopUrlDataProvider + * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::getRPShopUrl + */ + public function canGetRPShopUrl($hasConfiguredUrl, $configuredUrl, $hostUrl, $expected) + { + /** @var RelyingPartyEntity|MockObject $sut */ + $sut = $this->getMockBuilder(RelyingPartyEntity::class) + ->disableOriginalConstructor() + ->onlyMethods(['hasConfiguredShopUrl', 'getConfiguredShopUrl', 'getShopUrlByHost']) + ->getMock(); + $sut->method('hasConfiguredShopUrl')->willReturn($hasConfiguredUrl); + $sut->method('getConfiguredShopUrl')->willReturn($configuredUrl); + $sut->method('getShopUrlByHost')->willReturn($hostUrl); + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'getRPShopUrl' + ) + ); + } + + /** + * @return array + */ + public function canGetRPShopUrlDataProvider(): array + { + return [ + 'configured' => [true, ' subd.mydomain.com', 'www.myhost.de', 'subd.mydomain.com'], + 'not configured'=> [false, ' subd.mydomain.com', 'www.myhost.de', 'www.myhost.de'] + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::getConfig + */ + public function canGetConfig() + { + /** @var RelyingPartyEntity $sut */ + $sut = oxNew(RelyingPartyEntity::class); + + $this->assertInstanceOf( + Config::class, + $this->callMethod( + $sut, + 'getConfig' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Model\RelyingPartyEntity::getActiveShop + */ + public function canGetActiveShop() + { + /** @var RelyingPartyEntity $sut */ + $sut = oxNew(RelyingPartyEntity::class); + + $this->assertInstanceOf( + Shop::class, + $this->callMethod( + $sut, + 'getActiveShop' + ) + ); + } +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountControllerTest.php new file mode 100644 index 0000000..9e1d027 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php new file mode 100644 index 0000000..f3e378f --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountDownloadsController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountDownloadsControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountDownloadsController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php new file mode 100644 index 0000000..119c907 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountNewsletterController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountNewsletterControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountNewsletterController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php new file mode 100644 index 0000000..4ede2d5 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountNoticeListController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountNoticeListControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountNoticeListController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php new file mode 100644 index 0000000..23f1499 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountOrderController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountOrderControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountOrderController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php new file mode 100644 index 0000000..6c31428 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountPasswordController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountPasswordControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountPasswordController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php new file mode 100644 index 0000000..de4f10a --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountRecommlistController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountRecommlistControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountRecommlistController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php new file mode 100644 index 0000000..a9a83de --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountReviewController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountReviewControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountReviewController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountTestTrait.php b/src/tests/unit/Modules/Application/Controller/AccountTestTrait.php new file mode 100644 index 0000000..8f47864 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountTestTrait.php @@ -0,0 +1,73 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use D3\TestingTools\Development\CanAccessRestricted; +use OxidEsales\Eshop\Application\Controller\AccountController; +use OxidEsales\Eshop\Application\Controller\AccountDownloadsController; +use OxidEsales\Eshop\Application\Controller\AccountNewsletterController; +use OxidEsales\Eshop\Application\Controller\AccountNoticeListController; +use OxidEsales\Eshop\Application\Controller\AccountOrderController; +use OxidEsales\Eshop\Application\Controller\AccountPasswordController; +use OxidEsales\Eshop\Application\Controller\AccountRecommlistController; +use OxidEsales\Eshop\Application\Controller\AccountReviewController; +use OxidEsales\Eshop\Application\Controller\AccountUserController; +use OxidEsales\Eshop\Application\Controller\AccountWishlistController; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +trait AccountTestTrait +{ + use CanAccessRestricted; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountDownloadsController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountNewsletterController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountNoticeListController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountOrderController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountPasswordController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountRecommlistController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountReviewController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountUserController_Webauthn::__construct + * @covers \D3\Webauthn\Modules\Application\Controller\d3_AccountWishlistController_Webauthn::__construct + */ + public function canConstruct() + { + /** @var AccountController|AccountDownloadsController|AccountNewsletterController|AccountNoticeListController|AccountOrderController|AccountPasswordController|AccountRecommlistController|AccountReviewController|AccountUserController|AccountWishlistController|MockObject $sut */ + $sut = $this->getMockBuilder($this->sutClass) + ->onlyMethods(['addTplParam']) + ->disableOriginalConstructor() + ->getMock(); + $sut->expects($this->atLeastOnce())->method('addTplParam')->with($this->identicalTo('oxLoginTpl')); + + $this->callMethod( + $sut, + '__construct' + ); + + $this->assertSame( + 'd3webauthnaccountlogin.tpl', + $this->getValue( + $sut, + '_sThisLoginTemplate' + ) + ); + } +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php new file mode 100644 index 0000000..b885ef2 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountUserController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountUserControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountUserController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php new file mode 100644 index 0000000..015fe2b --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\AccountWishlistController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class AccountWishlistControllerTest extends UnitTestCase +{ + use AccountTestTrait; + + protected $sutClass = AccountWishlistController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php new file mode 100644 index 0000000..b7531a2 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php @@ -0,0 +1,152 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use D3\TestingTools\Development\CanAccessRestricted; +use D3\Webauthn\Application\Model\Webauthn; +use D3\Webauthn\Application\Model\WebauthnConf; +use OxidEsales\Eshop\Application\Controller\OrderController; +use OxidEsales\Eshop\Application\Controller\PaymentController; +use OxidEsales\Eshop\Application\Controller\UserController; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Session; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +trait CheckoutTestTrait +{ + use CanAccessRestricted; + + /** + * @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 + */ + public function canGetUser($hasUser, $userId, $isActive, $sessionAuth, $expected) + { + if ($hasUser) { + /** @var User|MockObject $userMock */ + $userMock = $this->getMockBuilder(User::class) + ->onlyMethods(['getId']) + ->getMock(); + $userMock->method('getId')->willReturn($userId); + } else { + $userMock = false; + } + + /** @var Session|MockObject $sessionMock */ + $sessionMock = $this->getMockBuilder(Session::class) + ->onlyMethods(['getVariable']) + ->getMock(); + $sessionMock->method('getVariable') + ->with($this->identicalTo(WebauthnConf::WEBAUTHN_SESSION_AUTH))->willReturn($sessionAuth); + + /** @var Webauthn|MockObject $webauthnMock */ + $webauthnMock = $this->getMockBuilder(Webauthn::class) + ->onlyMethods(['isActive']) + ->getMock(); + $webauthnMock->method('isActive')->willReturn($isActive); + + /** @var PaymentController|OrderController|UserController|MockObject $sut */ + $sut = $this->getMockBuilder($this->sutClass) + ->onlyMethods(['d3GetWebauthnObject', 'd3WebauthnGetSessionObject', 'd3CallMockableParent']) + ->getMock(); + $sut->method('d3GetWebauthnObject')->willReturn($webauthnMock); + $sut->method('d3WebauthnGetSessionObject')->willReturn($sessionMock); + $sut->method('d3CallMockableParent')->willReturn($userMock); + + $return = $this->callMethod( + $sut, + 'getUser' + ); + + if ($expected === 'parent') { + $this->assertSame($return, $userMock); + } else { + $this->assertSame($return, $expected); + } + } + + /** + * @return array + */ + public function canGetUserDataProvider(): array + { + return [ + 'no user' => [false, null, false, null, 'parent'], + 'no user id' => [true, 'null', false, null, 'parent'], + 'webauthn not active' => [true, 'userIdFixture', false, null, 'parent'], + 'has webauthn auth' => [true, 'userIdFixture', true, 'userIdFixture', 'parent'], + 'no webauthn auth' => [true, 'userIdFixture', true, null, false], + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait::d3GetWebauthnObject + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController::d3GetWebauthnObject + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::d3GetWebauthnObject + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::d3GetWebauthnObject + */ + public function canGetWebauthnObject() + { + /** @var PaymentController|OrderController|UserController $sut */ + $sut = oxNew($this->sutClass); + + $this->assertInstanceOf( + Webauthn::class, + $this->callMethod( + $sut, + 'd3GetWebauthnObject' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait::d3WebauthnGetSessionObject + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController::d3WebauthnGetSessionObject + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::d3WebauthnGetSessionObject + * @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::d3WebauthnGetSessionObject + */ + public function canGetSessionObject() + { + /** @var PaymentController|OrderController|UserController $sut */ + $sut = oxNew($this->sutClass); + + $this->assertInstanceOf( + Session::class, + $this->callMethod( + $sut, + 'd3WebauthnGetSessionObject' + ) + ); + } +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/OrderControllerTest.php b/src/tests/unit/Modules/Application/Controller/OrderControllerTest.php new file mode 100644 index 0000000..1ea2cdd --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/OrderControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\OrderController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class OrderControllerTest extends UnitTestCase +{ + use CheckoutTestTrait; + + protected $sutClass = OrderController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php b/src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php new file mode 100644 index 0000000..79df5dc --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\PaymentController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class PaymentControllerTest extends UnitTestCase +{ + use CheckoutTestTrait; + + protected $sutClass = PaymentController::class; +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Application/Controller/UserControllerTest.php b/src/tests/unit/Modules/Application/Controller/UserControllerTest.php new file mode 100644 index 0000000..f7ac3b6 --- /dev/null +++ b/src/tests/unit/Modules/Application/Controller/UserControllerTest.php @@ -0,0 +1,26 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules\Application\Controller; + +use OxidEsales\Eshop\Application\Controller\UserController; +use OxidEsales\TestingLibrary\UnitTestCase; + +class UserControllerTest extends UnitTestCase +{ + use CheckoutTestTrait; + + protected $sutClass = UserController::class; +} \ No newline at end of file