2022-11-30 01:27:05 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace D3\Webauthn\tests\unit\Application\Model;
|
|
|
|
|
|
|
|
use D3\TestingTools\Development\CanAccessRestricted;
|
|
|
|
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
|
|
|
|
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
|
|
|
|
use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException;
|
|
|
|
use D3\Webauthn\Application\Model\Webauthn;
|
|
|
|
use D3\Webauthn\Application\Model\WebauthnConf;
|
|
|
|
use D3\Webauthn\Application\Model\WebauthnLogin;
|
2023-01-21 13:50:18 +01:00
|
|
|
use D3\Webauthn\tests\unit\WAUnitTestCase;
|
|
|
|
use Generator;
|
2022-11-30 01:27:05 +01:00
|
|
|
use OxidEsales\Eshop\Application\Component\UserComponent;
|
|
|
|
use OxidEsales\Eshop\Application\Model\Basket;
|
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
use OxidEsales\Eshop\Core\Config;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\CookieException;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\UserException;
|
|
|
|
use OxidEsales\Eshop\Core\Session;
|
|
|
|
use OxidEsales\Eshop\Core\SystemEventHandler;
|
|
|
|
use OxidEsales\Eshop\Core\Utils;
|
|
|
|
use OxidEsales\Eshop\Core\UtilsServer;
|
|
|
|
use OxidEsales\Eshop\Core\UtilsView;
|
|
|
|
use OxidEsales\TestingLibrary\UnitTestCase;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use ReflectionException;
|
|
|
|
use TypeError;
|
|
|
|
|
2023-01-21 13:50:18 +01:00
|
|
|
class WebauthnLoginTest extends WAUnitTestCase
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
|
|
|
use CanAccessRestricted;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::__construct
|
|
|
|
*/
|
|
|
|
public function canConstruct()
|
|
|
|
{
|
|
|
|
$credFixture = 'credentialFixture';
|
|
|
|
$errorFixture = 'errorFixture';
|
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->onlyMethods(['setCredential', 'setErrorMsg'])
|
|
|
|
->getMock();
|
|
|
|
$sut->expects($this->atLeastOnce())->method('setCredential')->with($credFixture);
|
|
|
|
$sut->expects($this->atLeastOnce())->method('setErrorMsg')->with($errorFixture);
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'__construct',
|
|
|
|
[$credFixture, $errorFixture]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::setCredential
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::getCredential
|
|
|
|
*/
|
|
|
|
public function canSetAndGetExistingCredentials()
|
|
|
|
{
|
|
|
|
$credFixture = 'credentialFixture';
|
|
|
|
|
|
|
|
/** @var WebauthnLogin $sut */
|
|
|
|
$sut = oxNew(WebauthnLogin::class, 'cred', 'err');
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'setCredential',
|
|
|
|
[$credFixture]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$credFixture,
|
2022-12-04 00:24:28 +01:00
|
|
|
$this->callMethod(
|
2022-11-30 01:27:05 +01:00
|
|
|
$sut,
|
|
|
|
'getCredential'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $credFixture
|
|
|
|
* @param $setException
|
|
|
|
* @param $getExpection
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider credentialErrorDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::setCredential
|
|
|
|
*/
|
|
|
|
public function cannotSetErrorCredentials($credFixture, $setException, $getExpection)
|
|
|
|
{
|
|
|
|
unset($getExpection);
|
|
|
|
|
|
|
|
/** @var WebauthnLogin $sut */
|
|
|
|
$sut = oxNew(WebauthnLogin::class, 'cred', 'err');
|
|
|
|
|
|
|
|
$this->expectException($setException);
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'setCredential',
|
|
|
|
[$credFixture]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $credFixture
|
|
|
|
* @param $setException
|
|
|
|
* @param $getExpection
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider credentialErrorDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::getCredential
|
|
|
|
*/
|
|
|
|
public function cannotGetErrorCredentials($credFixture, $setException, $getExpection)
|
|
|
|
{
|
|
|
|
unset($setException);
|
|
|
|
|
|
|
|
/** @var WebauthnLogin $sut */
|
|
|
|
$sut = oxNew(WebauthnLogin::class, 'cred', 'err');
|
|
|
|
|
|
|
|
$this->setValue(
|
|
|
|
$sut,
|
|
|
|
'credential',
|
|
|
|
$credFixture
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->expectException($getExpection);
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'getCredential'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function credentialErrorDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'empty credential' => ['', WebauthnGetException::class, WebauthnGetException::class];
|
|
|
|
yield 'spaced credential' => [' ', WebauthnGetException::class, WebauthnGetException::class];
|
|
|
|
yield 'null credential' => [null, TypeError::class, WebauthnGetException::class];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::setErrorMsg
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::getErrorMsg
|
|
|
|
*/
|
|
|
|
public function canAndGetErrorMessage()
|
|
|
|
{
|
|
|
|
$errorFixture = 'errorFixture';
|
|
|
|
|
|
|
|
/** @var WebauthnLogin $sut */
|
|
|
|
$sut = oxNew(WebauthnLogin::class, 'cred', 'err');
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'setErrorMsg',
|
|
|
|
[$errorFixture]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$errorFixture,
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'getErrorMsg'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::frontendLogin
|
|
|
|
* @dataProvider frontendLoginSuccessDataProvider
|
|
|
|
*/
|
|
|
|
public function frontendLoginSuccess($setCookie)
|
|
|
|
{
|
|
|
|
/** @var UserComponent|MockObject $userComponentMock */
|
|
|
|
$userComponentMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods(['setUser'])
|
|
|
|
->getMock();
|
|
|
|
$userComponentMock->expects($this->atLeast(2))->method('setUser');
|
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
|
|
|
|
'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie',
|
2023-01-21 13:50:18 +01:00
|
|
|
'getCredential', 'regenerateSessionId', ])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$sut->expects($this->exactly((int) $setCookie))->method('setSessionCookie');
|
|
|
|
$sut->expects($this->once())->method('setFrontendSession');
|
|
|
|
|
|
|
|
$this->assertEmpty(
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'frontendLogin',
|
|
|
|
[$userComponentMock, $setCookie]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function frontendLoginSuccessDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'setCookie' => [true];
|
|
|
|
yield 'dontSetCookie' => [false];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $exceptionClass
|
|
|
|
* @param $writeLog
|
|
|
|
* @param $setCookie
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::frontendLogin
|
|
|
|
* @dataProvider frontendLoginExceptionDataProvider
|
|
|
|
*/
|
|
|
|
public function frontendLoginException($exceptionClass, $writeLog, $setCookie)
|
|
|
|
{
|
|
|
|
/** @var UtilsView|MockObject $utilsViewMock */
|
|
|
|
$utilsViewMock = $this->getMockBuilder(UtilsView::class)
|
|
|
|
->onlyMethods(['addErrorToDisplay'])
|
|
|
|
->getMock();
|
|
|
|
$utilsViewMock->expects($this->once())->method('addErrorToDisplay');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods(['logout'])
|
|
|
|
->getMock();
|
|
|
|
$userMock->expects($this->once())->method('logout');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var LoggerInterface|MockObject $loggerMock */
|
|
|
|
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
|
|
|
|
$loggerMock->expects($writeLog)->method('error')->willReturn(true);
|
|
|
|
$loggerMock->expects($writeLog)->method('debug')->willReturn(true);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var UserComponent|MockObject $userComponentMock */
|
|
|
|
$userComponentMock = $this->getMockBuilder(UserComponent::class)
|
|
|
|
->onlyMethods(['setUser'])
|
|
|
|
->getMock();
|
|
|
|
$userComponentMock->expects($this->never())->method('setUser');
|
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
|
2022-11-30 01:27:05 +01:00
|
|
|
'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie',
|
2023-01-21 13:50:18 +01:00
|
|
|
'getCredential', 'regenerateSessionId', ])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass));
|
|
|
|
|
|
|
|
$this->expectException(WebauthnLoginErrorException::class);
|
|
|
|
|
|
|
|
$this->assertEmpty(
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'frontendLogin',
|
|
|
|
[$userComponentMock, $setCookie]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function frontendLoginExceptionDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'userException' => [UserException::class, $this->any(), false];
|
|
|
|
yield 'cookieException' => [CookieException::class, $this->any(), true];
|
|
|
|
yield 'webauthnException' => [WebauthnException::class, $this->atLeastOnce(), false];
|
|
|
|
yield 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce(), true];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::adminLogin
|
|
|
|
*/
|
|
|
|
public function adminLoginSuccess()
|
|
|
|
{
|
2022-12-04 00:24:28 +01:00
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
|
2022-12-04 00:24:28 +01:00
|
|
|
|
2022-11-30 01:27:05 +01:00
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
|
2023-01-21 13:50:18 +01:00
|
|
|
'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'admin_start',
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'adminLogin',
|
|
|
|
[1]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::adminLogin
|
|
|
|
* @dataProvider adminLoginExceptionDataProvider
|
|
|
|
*/
|
|
|
|
public function adminLoginException($exceptionClass, $writeLog)
|
|
|
|
{
|
2022-12-04 00:24:28 +01:00
|
|
|
/** @var SystemEventHandler|MockObject $systemEventHandlerMock */
|
|
|
|
$systemEventHandlerMock = $this->getMockBuilder(SystemEventHandler::class)
|
|
|
|
->onlyMethods(['onAdminLogin'])
|
|
|
|
->getMock();
|
|
|
|
$systemEventHandlerMock->expects($this->never())->method('onAdminLogin');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.SystemEventHandler::class, $systemEventHandlerMock);
|
2022-12-04 00:24:28 +01:00
|
|
|
|
2022-11-30 01:27:05 +01:00
|
|
|
/** @var UtilsView|MockObject $utilsViewMock */
|
|
|
|
$utilsViewMock = $this->getMockBuilder(UtilsView::class)
|
|
|
|
->onlyMethods(['addErrorToDisplay'])
|
|
|
|
->getMock();
|
|
|
|
$utilsViewMock->expects($this->once())->method('addErrorToDisplay');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods(['logout'])
|
|
|
|
->getMock();
|
|
|
|
$userMock->expects($this->once())->method('logout');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var LoggerInterface|MockObject $loggerMock */
|
|
|
|
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']);
|
|
|
|
$loggerMock->expects($writeLog)->method('error')->willReturn(true);
|
|
|
|
$loggerMock->expects($writeLog)->method('debug')->willReturn(true);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn',
|
2023-01-21 13:50:18 +01:00
|
|
|
'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass));
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'login',
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'adminLogin',
|
|
|
|
[1]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function adminLoginExceptionDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'userException' => [UserException::class, $this->any()];
|
|
|
|
yield 'cookieException' => [CookieException::class, $this->any()];
|
|
|
|
yield 'webauthnException' => [WebauthnException::class, $this->atLeastOnce()];
|
|
|
|
yield 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce()];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::handleErrorMessage
|
|
|
|
* @dataProvider canHandleErrorMessageDataProvider
|
|
|
|
*/
|
|
|
|
public function canHandleErrorMessage($message, $throwsException)
|
|
|
|
{
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->onlyMethods(['getErrorMsg'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$sut->method('getErrorMsg')->willReturn($message);
|
|
|
|
|
|
|
|
if ($throwsException) {
|
|
|
|
$this->expectException(WebauthnGetException::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty(
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'handleErrorMessage'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canHandleErrorMessageDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'has error message' => ['errorMessage', true];
|
|
|
|
yield 'empty error message' => ['', false];
|
|
|
|
yield 'error message null' => [null, false];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::assertAuthn
|
|
|
|
* @dataProvider canAssertAuthDataProvider
|
|
|
|
*/
|
|
|
|
public function canAssertAuthn($credential, $doAssert, $throwException)
|
|
|
|
{
|
|
|
|
/** @var Webauthn|MockObject $webauthnMock */
|
|
|
|
$webauthnMock = $this->getMockBuilder(Webauthn::class)
|
|
|
|
->onlyMethods(['assertAuthn'])
|
|
|
|
->getMock();
|
|
|
|
$webauthnMock->expects($doAssert)->method('assertAuthn');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
if ($throwException) {
|
|
|
|
$sut->method('getCredential')->willThrowException(oxNew(WebauthnGetException::class));
|
|
|
|
} else {
|
|
|
|
$sut->method('getCredential')->willReturn('credential');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($throwException) {
|
|
|
|
$this->expectException(WebauthnGetException::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty(
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'assertAuthn'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canAssertAuthDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'has credential' => ['credentialFixture', $this->atLeastOnce(), false];
|
|
|
|
yield 'no credential' => [null, $this->never(), true];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::setAdminSession
|
|
|
|
*/
|
|
|
|
public function canSetAdminSession()
|
|
|
|
{
|
|
|
|
/** @var Session|MockObject $sessionMock */
|
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['getVariable', 'initNewSession', 'setVariable'])
|
|
|
|
->getMock();
|
|
|
|
$sessionMock->method('getVariable')->willReturn('sessVariable');
|
|
|
|
$sessionMock->expects($this->once())->method('initNewSession');
|
|
|
|
$sessionMock->method('setVariable')->with(
|
|
|
|
$this->anything(),
|
|
|
|
$this->logicalOr(
|
|
|
|
$this->identicalTo('sessVariable'),
|
|
|
|
$this->identicalTo('userId')
|
|
|
|
)
|
|
|
|
);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$sessionMock,
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'setAdminSession',
|
|
|
|
['userId']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $setCookie
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider canSetSessionCookieDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::setSessionCookie
|
|
|
|
*/
|
|
|
|
public function canSetSessionCookie($setCookie)
|
|
|
|
{
|
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
/** @var UtilsServer|MockObject $utilsServerMock */
|
|
|
|
$utilsServerMock = $this->getMockBuilder(UtilsServer::class)
|
|
|
|
->onlyMethods(['setUserCookie'])
|
|
|
|
->getMock();
|
|
|
|
$utilsServerMock->expects($this->exactly((int) $setCookie))->method('setUserCookie');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var Config|MockObject $configMock */
|
|
|
|
$configMock = $this->getMockBuilder(Config::class)
|
|
|
|
->onlyMethods(['getConfigParam', 'getShopId'])
|
|
|
|
->getMock();
|
|
|
|
$configMock->method('getConfigParam')->with('blShowRememberMe')->willReturn($setCookie);
|
|
|
|
$configMock->method('getShopId')->willReturn(1);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'setSessionCookie',
|
|
|
|
[$userMock]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canSetSessionCookieDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'set cookie' => [true];
|
|
|
|
yield 'dont set cookie' => [false];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $isBackend
|
|
|
|
* @param $userLoaded
|
|
|
|
* @param $rights
|
|
|
|
* @param $throwsException
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::assertUser
|
|
|
|
* @dataProvider canAssertUserDataProvider
|
|
|
|
*/
|
|
|
|
public function canAssertUser($isBackend, $userLoaded, $rights, $throwsException)
|
|
|
|
{
|
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods(['isLoaded', 'getFieldData'])
|
|
|
|
->getMock();
|
|
|
|
$userMock->method('isLoaded')->willReturn($userLoaded);
|
|
|
|
$userMock->method('getFieldData')->with('oxrights')->willReturn($rights);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
if ($throwsException) {
|
|
|
|
$this->expectException(UserException::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
User::class,
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'assertUser',
|
|
|
|
['userId', $isBackend]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canAssertUserDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'frontend, user not loaded' => [false, false, 'malladmin', true];
|
|
|
|
yield 'backend, user not loaded' => [true, false, 'malladmin', true];
|
|
|
|
yield 'frontend, frontend user loaded' => [false, true, 'user', false];
|
|
|
|
yield 'backend, frontend user loaded' => [true, true, 'user', true];
|
|
|
|
yield 'frontend, backend user loaded' => [false, true, 'malladmin', false];
|
|
|
|
yield 'backend, backend user loaded' => [true, true, 'malladmin', false];
|
|
|
|
yield 'frontend, backend 2 user loaded' => [false, true, '2', false];
|
|
|
|
yield 'backend, backend 2 user loaded' => [true, true, '2', false];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $cookie
|
|
|
|
* @param $throwException
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider canHandleBackendCookieDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::handleBackendCookie
|
|
|
|
*/
|
|
|
|
public function canHandleBackendCookie($cookie, $throwException)
|
|
|
|
{
|
|
|
|
/** @var UtilsServer|MockObject $utilsServerMock */
|
|
|
|
$utilsServerMock = $this->getMockBuilder(UtilsServer::class)
|
|
|
|
->onlyMethods(['getOxCookie'])
|
|
|
|
->getMock();
|
|
|
|
$utilsServerMock->method('getOxCookie')->willReturn($cookie);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
if ($throwException) {
|
|
|
|
$this->expectException(CookieException::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty(
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'handleBackendCookie'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canHandleBackendCookieDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'has cookie' => ['cookiecontent', false];
|
|
|
|
yield 'has no cookie' => [null, true];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $rights
|
|
|
|
* @param $setVar
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider canHandleBackendSubshopRightsDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::handleBackendSubshopRights
|
|
|
|
*/
|
|
|
|
public function canHandleBackendSubshopRights($rights, $setVar)
|
|
|
|
{
|
|
|
|
/** @var Config|MockObject $configMock */
|
|
|
|
$configMock = $this->getMockBuilder(Config::class)
|
|
|
|
->onlyMethods(['setShopId'])
|
|
|
|
->getMock();
|
|
|
|
$configMock->expects($setVar)->method('setShopId');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods(['getFieldData'])
|
|
|
|
->getMock();
|
|
|
|
$userMock->method('getFieldData')->with('oxrights')->willReturn($rights);
|
|
|
|
|
|
|
|
/** @var Session|MockObject $sessionMock */
|
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable'])
|
|
|
|
->getMock();
|
|
|
|
$sessionMock->expects($setVar)->method('setVariable');
|
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'handleBackendSubshopRights',
|
|
|
|
[$userMock, $sessionMock]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canHandleBackendSubshopRightsDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'malladmin' => ['malladmin', $this->never()];
|
|
|
|
yield '1' => ['1', $this->atLeastOnce()];
|
|
|
|
yield '2' => ['2', $this->atLeastOnce()];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $sessionStarted
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider canRegenerateSessionIdDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::regenerateSessionId
|
|
|
|
*/
|
|
|
|
public function canRegenerateSessionId($sessionStarted)
|
|
|
|
{
|
|
|
|
/** @var Session|MockObject $sessionMock */
|
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['isSessionStarted', 'regenerateSessionId'])
|
|
|
|
->getMock();
|
|
|
|
$sessionMock->method('isSessionStarted')->willReturn($sessionStarted);
|
|
|
|
$sessionMock->expects($this->exactly((int) $sessionStarted))->method('regenerateSessionId');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'regenerateSessionId'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canRegenerateSessionIdDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'session started' => [true];
|
|
|
|
yield 'session not started' => [false];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $inBlockedGroup
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider canHandleBlockedUserDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::handleBlockedUser
|
|
|
|
*/
|
|
|
|
public function canHandleBlockedUser($inBlockedGroup)
|
|
|
|
{
|
|
|
|
/** @var Utils|MockObject $utilsMock */
|
|
|
|
$utilsMock = $this->getMockBuilder(Utils::class)
|
|
|
|
->onlyMethods(['redirect'])
|
|
|
|
->getMock();
|
|
|
|
$utilsMock->expects($this->exactly((int) $inBlockedGroup))->method('redirect');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var Config|MockObject $configMock */
|
|
|
|
$configMock = $this->getMockBuilder(Config::class)
|
|
|
|
->onlyMethods(['getShopHomeUrl'])
|
|
|
|
->getMock();
|
|
|
|
$configMock->method('getShopHomeUrl')->willReturn('homeurl');
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods(['inGroup'])
|
|
|
|
->getMock();
|
|
|
|
$userMock->method('inGroup')->willReturn($inBlockedGroup);
|
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'handleBlockedUser',
|
|
|
|
[$userMock]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canHandleBlockedUserDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'is in blocked group' => [true];
|
|
|
|
yield 'is not in blocked group' => [false];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::updateBasket
|
|
|
|
*/
|
2022-12-12 23:41:07 +01:00
|
|
|
public function canUpdateBasket()
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
|
|
|
/** @var Basket|MockObject $basketMock */
|
|
|
|
$basketMock = $this->getMockBuilder(Basket::class)
|
|
|
|
->onlyMethods(['onUpdate'])
|
|
|
|
->getMock();
|
2022-12-12 23:41:07 +01:00
|
|
|
$basketMock->expects($this->once())->method('onUpdate');
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var Session|MockObject $sessionMock */
|
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['getBasket'])
|
|
|
|
->getMock();
|
2022-12-12 23:41:07 +01:00
|
|
|
$sessionMock->method('getBasket')->willReturn($basketMock);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->disableOriginalConstructor()
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'updateBasket'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::isAdmin
|
|
|
|
*/
|
|
|
|
public function canGetIsAdmin()
|
|
|
|
{
|
|
|
|
/** @var WebauthnLogin $sut */
|
|
|
|
$sut = oxNew(WebauthnLogin::class, 'cred');
|
|
|
|
|
|
|
|
$this->assertIsBool(
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'isAdmin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @param $isAdmin
|
|
|
|
* @param $expected
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @dataProvider canGetUserIdDataProvider
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::getUserId
|
|
|
|
*/
|
|
|
|
public function canGetUserId($isAdmin, $expected)
|
|
|
|
{
|
|
|
|
/** @var Session|MockObject $sessionMock */
|
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['getVariable'])
|
|
|
|
->getMock();
|
|
|
|
$sessionMock->method('getVariable')->willReturnCallback(
|
|
|
|
function () {
|
|
|
|
$args = func_get_args();
|
2022-12-13 22:24:33 +01:00
|
|
|
if ($args[0] === WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) {
|
2022-11-30 01:27:05 +01:00
|
|
|
return 'adminUser';
|
2022-12-13 22:24:33 +01:00
|
|
|
} elseif ($args[0] === WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER) {
|
2022-11-30 01:27:05 +01:00
|
|
|
return 'frontendUser';
|
2022-12-13 22:24:33 +01:00
|
|
|
}
|
2022-11-30 01:27:05 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->disableOriginalConstructor()
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['isAdmin'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->getMock();
|
|
|
|
$sut->method('isAdmin')->willReturn($isAdmin);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'getUserId'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-21 13:50:18 +01:00
|
|
|
* @return Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
*/
|
2023-01-21 13:50:18 +01:00
|
|
|
public function canGetUserIdDataProvider(): Generator
|
2022-11-30 01:27:05 +01:00
|
|
|
{
|
2023-01-21 13:50:18 +01:00
|
|
|
yield 'admin' => [true, 'adminUser'];
|
|
|
|
yield 'frontend' => [false, 'frontendUser'];
|
2022-11-30 01:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\Webauthn\Application\Model\WebauthnLogin::setFrontendSession
|
|
|
|
*/
|
|
|
|
public function canSetFrontendSession()
|
|
|
|
{
|
|
|
|
/** @var Session|MockObject $sessionMock */
|
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
|
|
|
->onlyMethods(['setVariable'])
|
|
|
|
->getMock();
|
|
|
|
$sessionMock->expects($this->exactly(2))->method('setVariable')->withConsecutive(
|
|
|
|
[
|
|
|
|
$this->identicalTo(WebauthnConf::WEBAUTHN_SESSION_AUTH),
|
2022-12-13 22:24:33 +01:00
|
|
|
$this->identicalTo('credentialFixture'),
|
2022-11-30 01:27:05 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
$this->identicalTo(WebauthnConf::OXID_FRONTEND_AUTH),
|
2022-12-13 22:24:33 +01:00
|
|
|
$this->identicalTo('idFixture'),
|
2022-11-30 01:27:05 +01:00
|
|
|
]
|
|
|
|
);
|
2023-01-21 13:50:18 +01:00
|
|
|
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
|
2022-11-30 01:27:05 +01:00
|
|
|
|
|
|
|
/** @var User|MockObject $userMock */
|
|
|
|
$userMock = $this->getMockBuilder(User::class)
|
|
|
|
->onlyMethods(['getId'])
|
|
|
|
->getMock();
|
|
|
|
$userMock->method('getId')->willReturn('idFixture');
|
|
|
|
|
|
|
|
/** @var WebauthnLogin|MockObject $sut */
|
|
|
|
$sut = $this->getMockBuilder(WebauthnLogin::class)
|
|
|
|
->disableOriginalConstructor()
|
2023-01-21 13:50:18 +01:00
|
|
|
->onlyMethods(['getCredential'])
|
2022-11-30 01:27:05 +01:00
|
|
|
->getMock();
|
|
|
|
$sut->method('getCredential')->willReturn('credentialFixture');
|
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$sut,
|
|
|
|
'setFrontendSession',
|
|
|
|
[$userMock]
|
|
|
|
);
|
|
|
|
}
|
2022-12-13 22:24:33 +01:00
|
|
|
}
|