add admin controller tests

Cette révision appartient à :
Daniel Seifert 2022-11-22 00:26:04 +01:00
Parent b995437483
révision 3bbfde7794
Signé par: DanielS
ID de la clé GPG: 6A513E13AEE66170
5 fichiers modifiés avec 594 ajouts et 89 suppressions

Voir le fichier

@ -15,23 +15,24 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Controller\Admin;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredential;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
use D3\TestingTools\Production\IsMockable;
use D3\Webauthn\Application\Controller\Traits\helpersTrait;
use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException;
use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class d3user_webauthn extends AdminDetailsController
{
use IsMockable;
use helpersTrait;
protected $_sSaveError = null;
protected $_sThisTemplate = 'd3user_webauthn.tpl';
@ -41,9 +42,9 @@ class d3user_webauthn extends AdminDetailsController
*/
public function render(): string
{
$this->addTplParam('readonly', !(oxNew(Webauthn::class)->isAvailable()));
$this->addTplParam('readonly', !$this->getWebauthnObject()->isAvailable());
parent::render();
$this->d3CallMockableParent('render');
$soxId = $this->getEditObjectId();
@ -74,10 +75,10 @@ class d3user_webauthn extends AdminDetailsController
$this->setPageType( 'requestnew' );
$this->setAuthnRegister();
} catch (Exception|ContainerExceptionInterface|NotFoundExceptionInterface|DoctrineDriverException $e) {
Registry::getUtilsView()->addErrorToDisplay($e);
Registry::getLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]);
Registry::getLogger()->debug($e->getTraceAsString());
Registry::getUtils()->redirect('index.php?cl=d3user_webauthn');
$this->getUtilsViewObject()->addErrorToDisplay($e);
$this->getLoggerObject()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]);
$this->getLoggerObject()->debug($e->getTraceAsString());
$this->getUtilsObject()->redirect('index.php?cl=d3user_webauthn');
}
}
@ -96,14 +97,13 @@ class d3user_webauthn extends AdminDetailsController
$credential = Registry::getRequest()->getRequestEscapedParameter('credential');
if ( strlen((string) $credential) ) {
/** @var Webauthn $webauthn */
$webauthn = oxNew( Webauthn::class );
$webauthn = $this->getWebauthnObject();
$webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter( 'keyname' ) );
}
} catch (WebauthnException|Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) {
Registry::getLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]);
Registry::getLogger()->debug($e->getTraceAsString());
Registry::getUtilsView()->addErrorToDisplay($e);
$this->getLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]);
$this->getLoggerObject()->debug($e->getTraceAsString());
$this->getUtilsViewObject()->addErrorToDisplay($e);
}
}
@ -117,15 +117,12 @@ class d3user_webauthn extends AdminDetailsController
}
/**
* @throws ContainerExceptionInterface
* @throws DoctrineDriverException
* @throws NotFoundExceptionInterface
* @throws DoctrineException
* @throws WebauthnException
*/
public function setAuthnRegister(): void
{
$authn = oxNew(Webauthn::class);
$authn = $this->getWebauthnObject();
$user = $this->getUserObject();
$user->load($this->getEditObjectId());
@ -154,16 +151,8 @@ class d3user_webauthn extends AdminDetailsController
$oUser = $this->getUserObject();
$oUser->load($userId);
$publicKeyCrendetials = oxNew(PublicKeyCredentialList::class);
return $publicKeyCrendetials->getAllFromUser($oUser)->getArray();
}
/**
* @return User
*/
public function getUserObject(): User
{
return oxNew(User::class);
$publicKeyCredentials = $this->getPublicKeyCredentialListObject();
return $publicKeyCredentials->getAllFromUser($oUser)->getArray();
}
/**
@ -171,8 +160,7 @@ class d3user_webauthn extends AdminDetailsController
*/
public function deleteKey(): void
{
/** @var PublicKeyCredential $credential */
$credential = oxNew(PublicKeyCredential::class);
$credential = $this->getPublicKeyCredentialObject();
$credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid'));
}
}

Voir le fichier

@ -0,0 +1,71 @@
<?php
namespace D3\Webauthn\Application\Controller\Traits;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredential;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
use D3\Webauthn\Application\Model\Webauthn;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsView;
use Psr\Log\LoggerInterface;
trait helpersTrait
{
/**
* @return User
*/
public function getUserObject(): User
{
return oxNew(User::class);
}
/**
* @return Webauthn
*/
public function getWebauthnObject(): Webauthn
{
return oxNew(Webauthn::class);
}
/**
* @return LoggerInterface
*/
public function getLoggerObject(): LoggerInterface
{
return Registry::getLogger();
}
/**
* @return Utils
*/
public function getUtilsObject(): Utils
{
return Registry::getUtils();
}
/**
* @return UtilsView
*/
public function getUtilsViewObject(): UtilsView
{
return Registry::getUtilsView();
}
/**
* @return PublicKeyCredentialList
*/
public function getPublicKeyCredentialListObject(): PublicKeyCredentialList
{
return oxNew(PublicKeyCredentialList::class);
}
/**
* @return PublicKeyCredential
*/
public function getPublicKeyCredentialObject(): PublicKeyCredential
{
return oxNew(PublicKeyCredential::class);
}
}

Voir le fichier

@ -16,26 +16,22 @@ declare(strict_types=1);
namespace D3\Webauthn\Application\Controller;
use D3\Webauthn\Application\Controller\Traits\accountTrait;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredential;
use D3\Webauthn\Application\Controller\Traits\helpersTrait;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\WebauthnErrors;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException;
use OxidEsales\Eshop\Application\Controller\AccountController;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\SeoEncoder;
use OxidEsales\Eshop\Core\UtilsView;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
class d3_account_webauthn extends AccountController
{
use accountTrait;
use helpersTrait;
protected $_sThisTemplate = 'd3_account_webauthn.tpl';
@ -47,7 +43,7 @@ class d3_account_webauthn extends AccountController
$sRet = parent::render();
$this->addTplParam('user', $this->getUser());
$this->addTplParam('readonly', (bool) !($this->getWebauthnObject()->isAvailable()));
$this->addTplParam('readonly', !($this->getWebauthnObject()->isAvailable()));
return $sRet;
}
@ -79,8 +75,8 @@ class d3_account_webauthn extends AccountController
$this->setAuthnRegister();
$this->setPageType('requestnew');
} catch (WebauthnException $e) {
$this->getLogger()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]);
$this->getLogger()->debug($e->getTraceAsString());
$this->getLoggerObject()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]);
$this->getLoggerObject()->debug($e->getTraceAsString());
$this->getUtilsViewObject()->addErrorToDisplay($e);
}
}
@ -130,7 +126,6 @@ class d3_account_webauthn extends AccountController
$credential = Registry::getRequest()->getRequestEscapedParameter('credential');
if (strlen((string) $credential)) {
/** @var Webauthn $webauthn */
$webauthn = $this->getWebauthnObject();
$webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname'));
}
@ -146,7 +141,6 @@ class d3_account_webauthn extends AccountController
{
$deleteId = Registry::getRequest()->getRequestEscapedParameter('deleteoxid');
if ($deleteId) {
/** @var PublicKeyCredential $credential */
$credential = $this->getPublicKeyCredentialObject();
$credential->delete($deleteId);
}
@ -173,44 +167,4 @@ class d3_account_webauthn extends AccountController
return $aPaths;
}
/**
* @return Webauthn
*/
public function getWebauthnObject(): Webauthn
{
return oxNew(Webauthn::class);
}
/**
* @return PublicKeyCredential
*/
public function getPublicKeyCredentialObject(): PublicKeyCredential
{
return oxNew(PublicKeyCredential::class);
}
/**
* @return PublicKeyCredentialList
*/
public function getPublicKeyCredentialListObject(): PublicKeyCredentialList
{
return oxNew(PublicKeyCredentialList::class);
}
/**
* @return LoggerInterface
*/
public function getLogger(): LoggerInterface
{
return Registry::getLogger();
}
/**
* @return UtilsView
*/
public function getUtilsViewObject(): UtilsView
{
return Registry::getUtilsView();
}
}

Voir le fichier

@ -0,0 +1,483 @@
<?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
*/
namespace D3\Totp\tests\unit\Application\Controller;
use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Controller\Admin\d3user_webauthn;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredential;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsView;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use ReflectionException;
class d3user_webauthnTest extends TestCase
{
use CanAccessRestricted;
public function setUp(): void
{
parent::setUp();
unset($_POST['error']);
unset($_POST['credential']);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::render
* @dataProvider canRenderDataProvider
*/
public function canRender($canLoadUser)
{
/** @var Webauthn|MockObject $webauthnMock */
$webauthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['isAvailable'])
->getMock();
$webauthnMock->method('isAvailable')->willReturn(false);
/** @var d3_User_Webauthn|MockObject $userMock */
$userMock = $this->getMockBuilder(d3_User_Webauthn::class)
->onlyMethods(['load', 'getId'])
->getMock();
$userMock->expects($this->atLeastOnce())->method('load')->with('editObjectId')->willReturn($canLoadUser);
$userMock->method('getId')->willReturn('editObjectId');
/** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([
'getWebauthnObject',
'd3CallMockableParent',
'getEditObjectId',
'getUserObject'
])
->getMock();
$sutMock->method('getWebauthnObject')->willReturn($webauthnMock);
$sutMock->method('d3CallMockableParent')->willReturn(true);
$sutMock->method('getEditObjectId')->willReturn('editObjectId');
$sutMock->method('getUserObject')->willReturn($userMock);
$this->setValue(
$sutMock,
'_sSaveError',
'saveErrorFixture'
);
$this->assertIsString(
$this->callMethod(
$sutMock,
'render'
)
);
$this->assertTrue($sutMock->getViewDataElement('readonly'));
$this->assertSame($canLoadUser ? 'editObjectId' : '-1', $sutMock->getViewDataElement('oxid'));
$this->assertSame($userMock, $sutMock->getViewDataElement('edit'));
$this->assertSame('saveErrorFixture', $sutMock->getViewDataElement('sSaveError'));
}
/**
* @return array
*/
public function canRenderDataProvider(): array
{
return [
'can load user' => [true],
'can not load user' => [false],
];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::requestNewCredential
*/
public function canRequestNewCredentialPassed()
{
/** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class)
->onlyMethods(['redirect'])
->getMock();
$utilsMock->expects($this->never())->method('redirect')->willReturn(true);
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->never())->method('error')->willReturn(true);
$loggerMock->expects($this->never())->method('debug')->willReturn(true);
/** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([
'setPageType',
'setAuthnRegister',
'getLoggerObject',
'getUtilsObject'
])
->getMock();
$sutMock->expects($this->atLeastOnce())->method('setPageType');
$sutMock->expects($this->atLeastOnce())->method('setAuthnRegister');
$sutMock->expects($this->never())->method('getLoggerObject')->willReturn($loggerMock);
$sutMock->expects($this->never())->method('getUtilsObject')->willReturn($utilsMock);
$this->callMethod(
$sutMock,
'requestNewCredential'
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::requestNewCredential
*/
public function canRequestNewCredentialFailed()
{
/** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class)
->onlyMethods(['redirect'])
->getMock();
$utilsMock->expects($this->once())->method('redirect')->willReturn(true);
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
/** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([
'setPageType',
'setAuthnRegister',
'getLoggerObject',
'getUtilsObject'
])
->getMock();
$sutMock->expects($this->atLeastOnce())->method('setPageType');
$sutMock->expects($this->atLeastOnce())->method('setAuthnRegister')->willThrowException(oxNew(WebauthnException::class));
$sutMock->expects($this->atLeastOnce())->method('getLoggerObject')->willReturn($loggerMock);
$sutMock->expects($this->atLeastOnce())->method('getUtilsObject')->willReturn($utilsMock);
$this->callMethod(
$sutMock,
'requestNewCredential'
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::saveAuthn
*/
public function canSaveAuthnHasError()
{
$_POST['error'] = 'msg';
/** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay'])
->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay');
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
/** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['getUtilsViewObject', 'getLoggerObject'])
->getMock();
$oControllerMock->method('getUtilsViewObject')->willReturn($utilsViewMock);
$oControllerMock->expects($this->atLeastOnce())->method('getLoggerObject')->willReturn($loggerMock);
$this->callMethod(
$oControllerMock,
'saveAuthn'
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::saveAuthn
*/
public function canSaveAuthnSuccess()
{
$_POST['credential'] = 'msg';
$_POST['keyname'] = 'key_name';
/** @var Webauthn|MockObject $webauthnMock */
$webauthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['saveAuthn'])
->getMock();
$webauthnMock->expects($this->once())->method('saveAuthn');
/** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay'])
->getMock();
$utilsViewMock->expects($this->never())->method('addErrorToDisplay');
/** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['getWebauthnObject', 'getUtilsViewObject'])
->getMock();
$oControllerMock->method('getWebauthnObject')->willReturn($webauthnMock);
$oControllerMock->method('getUtilsViewObject')->willReturn($utilsViewMock);
$this->callMethod(
$oControllerMock,
'saveAuthn'
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::saveAuthn
*/
public function canSaveAuthnFailed()
{
$_POST['credential'] = 'msg';
$_POST['keyname'] = 'key_name';
/** @var Webauthn|MockObject $webauthnMock */
$webauthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['saveAuthn'])
->getMock();
$webauthnMock->expects($this->once())->method('saveAuthn')
->willThrowException(oxNew(WebauthnException::class));
/** @var UtilsView|MockObject $utilsViewMock */
$utilsViewMock = $this->getMockBuilder(UtilsView::class)
->onlyMethods(['addErrorToDisplay'])
->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay');
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true);
$loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true);
/** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['getWebauthnObject', 'getUtilsViewObject', 'getLoggerObject'])
->getMock();
$oControllerMock->method('getWebauthnObject')->willReturn($webauthnMock);
$oControllerMock->method('getUtilsViewObject')->willReturn($utilsViewMock);
$oControllerMock->method('getLoggerObject')->willReturn($loggerMock);
$this->callMethod(
$oControllerMock,
'saveAuthn'
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::setPageType
*/
public function canSetPageType()
{
$sut = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['addTplParam'])
->getMock();
$sut->expects($this->atLeastOnce())->method('addTplParam');
$this->callMethod(
$sut,
'setPageType',
['pageTypeFixture']
);
}
/**
* @test
* @param $throwExc
* @return void
* @throws ReflectionException
* @dataProvider canSetAuthnRegisterDataProvider
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::setAuthnRegister
*/
public function canSetAuthnRegister($throwExc)
{
/** @var Webauthn|MockObject $webAuthnMock */
$webAuthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['getCreationOptions'])
->getMock();
if ($throwExc) {
$webAuthnMock->method('getCreationOptions')->willThrowException(oxNew(WebauthnException::class));
} else {
$webAuthnMock->method('getCreationOptions')->willReturn('options');
}
/** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['getWebauthnObject', 'addTplParam', 'getUser'])
->getMock();
$oControllerMock->method('getWebauthnObject')->willReturn($webAuthnMock);
$oControllerMock->expects($throwExc ? $this->never() : $this->atLeast(3))
->method('addTplParam');
$oControllerMock->method('getUser')->willReturn(oxNew(User::class));
if ($throwExc) {
$this->expectException(WebauthnException::class);
}
$this->callMethod(
$oControllerMock,
'setAuthnRegister'
);
}
/**
* @return array
*/
public function canSetAuthnRegisterDataProvider(): array
{
return [
'dont throw exception' => [false],
'throw exception' => [true],
];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::getCredentialList
*/
public function canGetCredentialList()
{
$oUser = oxNew(User::class);
$oUser->setId('foo');
$oUser->assign(
[
'oxpassword' => 'foo',
]
);
/** @var PublicKeyCredentialList|MockObject $publicKeyCredentialListMock */
$publicKeyCredentialListMock = $this->getMockBuilder(PublicKeyCredentialList::class)
->onlyMethods(['getAllFromUser'])
->getMock();
$publicKeyCredentialListMock->method('getAllFromUser')->with($oUser)->willReturnSelf();
/** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['getUserObject', 'getPublicKeyCredentialListObject'])
->getMock();
$oControllerMock->method('getUserObject')->willReturn($oUser);
$oControllerMock->method('getPublicKeyCredentialListObject')->willReturn($publicKeyCredentialListMock);
$this->assertIsArray(
$this->callMethod(
$oControllerMock,
'getCredentialList',
['myUserId']
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::getUserObject
*/
public function getUserObjectReturnsRightInstance()
{
/** @var d3user_webauthn $sut */
$sut = oxNew(d3user_webauthn::class);
$this->assertInstanceOf(
User::class,
$this->callMethod(
$sut,
'getUserObject'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::deleteKey
* @dataProvider canDeleteDataProvider
*/
public function canDelete($deleteId, $expected)
{
$_GET['deleteoxid'] = $deleteId;
/** @var PublicKeyCredential|MockObject $publicKeyCredentialMock */
$publicKeyCredentialMock = $this->getMockBuilder(PublicKeyCredential::class)
->disableOriginalConstructor()
->onlyMethods(['delete'])
->getMock();
$publicKeyCredentialMock->expects($expected)->method('delete')->with($this->identicalTo($deleteId))
->willReturn(true);
/** @var d3user_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods(['getPublicKeyCredentialObject'])
->getMock();
$oControllerMock->method('getPublicKeyCredentialObject')->willReturn($publicKeyCredentialMock);
$this->callMethod($oControllerMock, 'deleteKey');
}
/**
* @return array[]
*/
public function canDeleteDataProvider(): array
{
return [
'has delete id' => ['deleteId', $this->once()]
];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::getUtilsObject
*/
public function getUtilsObjectReturnsRightInstance()
{
/** @var d3user_webauthn $sut */
$sut = oxNew(d3user_webauthn::class);
$this->assertInstanceOf(
Utils::class,
$this->callMethod(
$sut,
'getUtilsObject'
)
);
}
}

Voir le fichier

@ -38,6 +38,9 @@ class d3_account_webauthnTest extends UnitTestCase
*/
public function setUp(): void
{
unset($_POST['error']);
unset($_POST['credential']);
parent::setUp();
$this->_oController = oxNew(d3_account_webauthn::class);
@ -183,12 +186,12 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'getLogger'])
->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'getLoggerObject'])
->getMock();
$oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister');
$oControllerMock->expects($this->atLeastOnce())->method('setPageType');
$oControllerMock->method('getUser')->willReturn($oUser);
$oControllerMock->method('getLogger')->willReturn($loggerMock);
$oControllerMock->method('getLoggerObject')->willReturn($loggerMock);
$this->_oController = $oControllerMock;
@ -221,13 +224,13 @@ class d3_account_webauthnTest extends UnitTestCase
/** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'getLogger'])
->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'getLoggerObject'])
->getMock();
$oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister')
->willThrowException(oxNew(WebauthnException::class));
$oControllerMock->expects($this->never())->method('setPageType');
$oControllerMock->method('getUser')->willReturn($oUser);
$oControllerMock->method('getLogger')->willReturn($loggerMock);
$oControllerMock->method('getLoggerObject')->willReturn($loggerMock);
$this->_oController = $oControllerMock;
@ -331,11 +334,17 @@ class d3_account_webauthnTest extends UnitTestCase
->getMock();
$utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay');
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
$loggerMock->expects($this->never())->method('error')->willReturn(true);
$loggerMock->expects($this->never())->method('debug')->willReturn(true);
/** @var d3_account_webauthn|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3_account_webauthn::class)
->onlyMethods(['getWebauthnObject', 'getUtilsViewObject'])
->onlyMethods(['getUtilsViewObject', 'getLoggerObject'])
->getMock();
$oControllerMock->method('getUtilsViewObject')->willReturn($utilsViewMock);
$oControllerMock->method('getLoggerObject')->willReturn($loggerMock);
$this->_oController = $oControllerMock;
@ -529,7 +538,7 @@ class d3_account_webauthnTest extends UnitTestCase
/**
* @test
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3_account_webauthn::getLogger
* @covers \D3\Webauthn\Application\Controller\d3_account_webauthn::getLoggerObject
*/
public function getLoggerObjectReturnsRightObject()
{
@ -537,7 +546,7 @@ class d3_account_webauthnTest extends UnitTestCase
LoggerInterface::class,
$this->callMethod(
$this->_oController,
'getLogger'
'getLoggerObject'
)
);
}