2022-11-22 00:26:04 +01:00
|
|
|
<?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
|
|
|
|
*/
|
|
|
|
|
2022-11-23 00:18:09 +01:00
|
|
|
namespace D3\Webauthn\tests\unit\Application\Controller\Admin;
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
use D3\TestingTools\Development\CanAccessRestricted;
|
2022-12-04 00:24:28 +01:00
|
|
|
use D3\TestingTools\Production\IsMockable;
|
2022-11-22 00:26:04 +01:00
|
|
|
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;
|
2022-12-13 22:16:45 +01:00
|
|
|
use Exception;
|
2022-11-22 00:26:04 +01:00
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
2022-12-04 00:24:28 +01:00
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2022-11-22 00:26:04 +01:00
|
|
|
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
|
|
|
|
{
|
2022-12-04 00:24:28 +01:00
|
|
|
use IsMockable;
|
2022-11-22 00:26:04 +01:00
|
|
|
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([
|
2022-12-07 12:03:24 +01:00
|
|
|
'd3CallMockableFunction',
|
2022-11-22 00:26:04 +01:00
|
|
|
'getEditObjectId',
|
2022-12-13 22:24:33 +01:00
|
|
|
'd3GetMockableOxNewObject',
|
2022-11-22 00:26:04 +01:00
|
|
|
])
|
|
|
|
->getMock();
|
2022-12-07 12:03:24 +01:00
|
|
|
$sutMock->method('d3CallMockableFunction')->willReturn(true);
|
2022-11-22 00:26:04 +01:00
|
|
|
$sutMock->method('getEditObjectId')->willReturn('editObjectId');
|
2022-12-04 00:24:28 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$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',
|
2022-12-04 00:24:28 +01:00
|
|
|
'd3GetMockableLogger',
|
2022-12-13 22:24:33 +01:00
|
|
|
'd3GetMockableRegistryObject',
|
2022-11-22 00:26:04 +01:00
|
|
|
])
|
|
|
|
->getMock();
|
|
|
|
$sutMock->expects($this->atLeastOnce())->method('setPageType');
|
|
|
|
$sutMock->expects($this->atLeastOnce())->method('setAuthnRegister');
|
2022-12-04 00:24:28 +01:00
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$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',
|
2022-12-04 00:24:28 +01:00
|
|
|
'd3GetMockableLogger',
|
2022-12-13 22:24:33 +01:00
|
|
|
'd3GetMockableRegistryObject',
|
2022-11-22 00:26:04 +01:00
|
|
|
])
|
|
|
|
->getMock();
|
|
|
|
$sutMock->expects($this->atLeastOnce())->method('setPageType');
|
|
|
|
$sutMock->expects($this->atLeastOnce())->method('setAuthnRegister')->willThrowException(oxNew(WebauthnException::class));
|
2022-12-04 00:24:28 +01:00
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$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)
|
2022-12-04 00:24:28 +01:00
|
|
|
->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger'])
|
2022-11-22 00:26:04 +01:00
|
|
|
->getMock();
|
2022-12-04 00:24:28 +01:00
|
|
|
$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);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$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)
|
2022-12-04 00:24:28 +01:00
|
|
|
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
|
2022-11-22 00:26:04 +01:00
|
|
|
->getMock();
|
2022-12-04 00:24:28 +01:00
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$oControllerMock,
|
|
|
|
'saveAuthn'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2022-12-13 22:16:45 +01:00
|
|
|
* @param string $excClass
|
2022-11-22 00:26:04 +01:00
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
2022-12-13 22:16:45 +01:00
|
|
|
* @dataProvider canSaveAuthnFailedDataProvider
|
2022-11-22 00:26:04 +01:00
|
|
|
* @covers \D3\Webauthn\Application\Controller\Admin\d3user_webauthn::saveAuthn
|
|
|
|
*/
|
2022-12-13 22:16:45 +01:00
|
|
|
public function canSaveAuthnFailed(string $excClass)
|
2022-11-22 00:26:04 +01:00
|
|
|
{
|
|
|
|
$_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')
|
2022-12-13 22:16:45 +01:00
|
|
|
->willThrowException(oxNew($excClass));
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
/** @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)
|
2022-12-04 00:24:28 +01:00
|
|
|
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3GetMockableLogger'])
|
2022-11-22 00:26:04 +01:00
|
|
|
->getMock();
|
2022-12-04 00:24:28 +01:00
|
|
|
$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);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$oControllerMock,
|
|
|
|
'saveAuthn'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-13 22:16:45 +01:00
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function canSaveAuthnFailedDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'webauthn exception' => [WebauthnException::class],
|
|
|
|
'common exception' => [Exception::class],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-11-22 00:26:04 +01:00
|
|
|
/**
|
|
|
|
* @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)
|
2022-12-04 00:24:28 +01:00
|
|
|
->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser'])
|
2022-11-22 00:26:04 +01:00
|
|
|
->getMock();
|
2022-12-04 00:24:28 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-11-22 00:26:04 +01:00
|
|
|
$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)
|
2022-12-04 00:24:28 +01:00
|
|
|
->onlyMethods(['d3GetMockableOxNewObject'])
|
2022-11-22 00:26:04 +01:00
|
|
|
->getMock();
|
2022-12-04 00:24:28 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$this->assertIsArray(
|
|
|
|
$this->callMethod(
|
|
|
|
$oControllerMock,
|
|
|
|
'getCredentialList',
|
|
|
|
['myUserId']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
2022-12-04 00:24:28 +01:00
|
|
|
->onlyMethods(['d3GetMockableOxNewObject'])
|
2022-11-22 00:26:04 +01:00
|
|
|
->getMock();
|
2022-12-04 00:24:28 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-11-22 00:26:04 +01:00
|
|
|
|
|
|
|
$this->callMethod($oControllerMock, 'deleteKey');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function canDeleteDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
2022-12-13 22:24:33 +01:00
|
|
|
'has delete id' => ['deleteId', $this->once()],
|
2022-11-22 00:26:04 +01:00
|
|
|
];
|
|
|
|
}
|
2022-12-13 22:24:33 +01:00
|
|
|
}
|