webauthn/src/tests/unit/Application/Controller/d3webauthnloginTest.php

435 lignes
15 KiB
PHP
Brut Vue normale Historique

2022-11-17 00:27:43 +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\Controller;
2022-11-17 00:27:43 +01:00
use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Controller\d3webauthnlogin;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf;
use OxidEsales\Eshop\Core\Registry;
2022-11-17 00:27:43 +01:00
use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\Utils;
2022-11-27 01:02:23 +01:00
use OxidEsales\TestingLibrary\UnitTestCase;
2022-11-17 00:27:43 +01:00
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use ReflectionException;
2022-11-27 01:02:23 +01:00
class d3webauthnloginTest extends UnitTestCase
2022-11-17 00:27:43 +01:00
{
use CanAccessRestricted;
protected $sutClassName = d3webauthnlogin::class;
2022-11-17 00:27:43 +01:00
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::getNavigationParams
*/
public function canGetNavigationParams()
{
/** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['getVariable'])
->getMock();
$sessionMock->method('getVariable')->willReturn([
2022-12-13 22:24:33 +01:00
'key1' => 'variable1',
2022-11-17 00:27:43 +01:00
]);
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction'])
2022-11-17 00:27:43 +01:00
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3CallMockableFunction')->willReturn(['defKey1' => 'devValues1']);
2022-11-17 00:27:43 +01:00
$this->assertSame(
[
'defKey1' => 'devValues1',
'key1' => 'variable1',
2022-12-13 22:24:33 +01:00
'cl' => null,
2022-11-17 00:27:43 +01:00
],
$this->callMethod(
$sut,
'getNavigationParams'
)
);
}
/**
* @test
* @param $auth
* @param $userFromLogin
* @param $startRedirect
* @param $redirectController
2022-11-17 00:27:43 +01:00
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::render
2022-11-17 00:27:43 +01:00
* @dataProvider canRenderDataProvider
*/
2022-11-27 01:02:23 +01:00
public function canRender($auth, $userFromLogin, $startRedirect, $redirectController)
2022-11-17 00:27:43 +01:00
{
/** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['hasVariable'])
->getMock();
$sessionMock->method('hasVariable')->willReturnMap([
[WebauthnConf::WEBAUTHN_SESSION_AUTH, $auth],
2022-12-13 22:24:33 +01:00
[WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $userFromLogin],
2022-11-17 00:27:43 +01:00
]);
/** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class)
->onlyMethods(['redirect'])
->getMock();
$utilsMock->expects($startRedirect ? $this->once() : $this->never())
->method('redirect')->with('index.php?cl='.$redirectController)->willReturn(true);
2022-11-17 00:27:43 +01:00
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction',
2022-12-13 22:24:33 +01:00
'generateCredentialRequest', 'addTplParam', ])
2022-11-17 00:27:43 +01:00
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl');
2022-11-17 00:27:43 +01:00
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('generateCredentialRequest');
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('addTplParam')->with('navFormParams')->willReturn(true);
$this->assertSame(
'myTemplate.tpl',
2022-11-17 00:27:43 +01:00
$this->callMethod(
$sut,
'render'
)
);
}
/**
* @return array
*/
public function canRenderDataProvider(): array
{
return [
2022-11-27 01:02:23 +01:00
'has request' => [false, true, false, 'start'],
'has auth' => [true, true, true, 'start'],
'missing user' => [false, false, true, 'start'],
2022-11-17 00:27:43 +01:00
];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::generateCredentialRequest
*/
2022-11-27 01:02:23 +01:00
public function canGenerateCredentialRequest($userSessionVarName = WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER)
2022-11-17 00:27:43 +01:00
{
$currUserFixture = 'currentUserFixture';
/** @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 Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['getVariable'])
->getMock();
$sessionMock->method('getVariable')->willReturnMap([
2022-12-13 22:24:33 +01:00
[$userSessionVarName, $currUserFixture],
2022-11-17 00:27:43 +01:00
]);
/** @var Webauthn|MockObject $webAuthnMock */
$webAuthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['getRequestOptions'])
->getMock();
$webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture)
->willReturn('success');
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableOxNewObject', 'addTplParam', 'd3GetMockableLogger'])
2022-11-17 00:27:43 +01:00
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
$sut->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-17 00:27:43 +01:00
$sut->expects($this->atLeast(2))
->method('addTplParam')->willReturn(true);
$sut->method('d3GetMockableLogger')->willReturn($loggerMock);
2022-11-17 00:27:43 +01:00
$this->callMethod(
$sut,
'generateCredentialRequest'
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::generateCredentialRequest
*/
2022-11-27 01:02:23 +01:00
public function generateCredentialRequestFailed($redirectClass = 'start', $userVarName = WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER)
2022-11-17 00:27:43 +01:00
{
$currUserFixture = 'currentUserFixture';
/** @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 Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['getVariable', 'setVariable'])
->getMock();
$sessionMock->method('getVariable')->willReturnMap([
2022-12-13 22:24:33 +01:00
[$userVarName, $currUserFixture],
2022-11-17 00:27:43 +01:00
]);
$sessionMock->expects($this->once())->method('setVariable')->with(WebauthnConf::GLOBAL_SWITCH)
->willReturn(true);
/** @var Webauthn|MockObject $webAuthnMock */
$webAuthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['getRequestOptions'])
->getMock();
$webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture)
->willThrowException(oxNew(WebauthnException::class, 'foobar0'));
/** @var Utils|MockObject $utilsMock */
$utilsMock = $this->getMockBuilder(Utils::class)
->onlyMethods(['redirect'])
->getMock();
$utilsMock->expects($this->once())->method('redirect')
->with('index.php?cl='.$redirectClass)->willReturn(true);
2022-11-17 00:27:43 +01:00
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam',
2022-12-13 22:24:33 +01:00
'd3GetMockableLogger', 'd3GetMockableRegistryObject', ])
2022-11-17 00:27:43 +01:00
->getMock();
$sut->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-17 00:27:43 +01:00
$sut->expects($this->never())
->method('addTplParam')->willReturn(true);
$sut->expects($this->atLeast(2))->method('d3GetMockableLogger')->willReturn($loggerMock);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($utilsMock, $sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Utils::class:
return $utilsMock;
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
2022-11-17 00:27:43 +01:00
$this->callMethod(
$sut,
'generateCredentialRequest'
);
}
/**
* @test
* @return void
* @throws ReflectionException
2022-11-27 01:02:23 +01:00
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::d3GetPreviousClass
2022-11-17 00:27:43 +01:00
*/
2022-11-27 01:02:23 +01:00
public function canGetPreviousClass($sessionVarName = WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS)
2022-11-17 00:27:43 +01:00
{
$currClassFixture = 'currentClassFixture';
/** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['getVariable'])
->getMock();
$sessionMock->method('getVariable')->willReturnMap([
2022-12-13 22:24:33 +01:00
[$sessionVarName, $currClassFixture],
2022-11-17 00:27:43 +01:00
]);
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject'])
2022-11-17 00:27:43 +01:00
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();
switch ($args[0]) {
case Session::class:
return $sessionMock;
default:
return Registry::get($args[0]);
}
}
);
2022-11-17 00:27:43 +01:00
$this->assertSame(
$currClassFixture,
$this->callMethod(
$sut,
2022-11-27 01:02:23 +01:00
'd3GetPreviousClass'
2022-11-17 00:27:43 +01:00
)
);
}
/**
* @test
* @param $currClass
* @param $isOrderStep
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::previousClassIsOrderStep
* @dataProvider canPreviousClassIsOrderStepDataProvider
*/
public function canPreviousClassIsOrderStep($currClass, $isOrderStep)
{
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
2022-11-27 01:02:23 +01:00
->onlyMethods(['d3GetPreviousClass'])
2022-11-17 00:27:43 +01:00
->getMock();
2022-11-27 01:02:23 +01:00
$sut->method('d3GetPreviousClass')->willReturn($currClass);
2022-11-17 00:27:43 +01:00
$this->assertSame(
$isOrderStep,
$this->callMethod(
$sut,
'previousClassIsOrderStep'
)
);
}
/**
* @return array[]
*/
public function canPreviousClassIsOrderStepDataProvider(): array
{
return [
'checkout class' => ['payment', true],
'no checkout class' => ['details', false],
'unknown class' => ['unknown', false],
];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::getIsOrderStep
* @dataProvider canGetIsOrderStepDataProvider
*/
public function canGetIsOrderStep($boolean)
{
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
2022-11-17 00:27:43 +01:00
->onlyMethods(['previousClassIsOrderStep'])
->getMock();
$sut->expects($this->atLeastOnce())->method('previousClassIsOrderStep')->willReturn($boolean);
$this->assertSame(
$boolean,
$this->callMethod(
$sut,
'getIsOrderStep'
)
);
}
/**
* @return array
*/
public function canGetIsOrderStepDataProvider(): array
{
return [
[true],
2022-12-13 22:24:33 +01:00
[false],
2022-11-17 00:27:43 +01:00
];
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Webauthn\Application\Controller\d3webauthnlogin::getBreadCrumb
*/
public function canGetBreadCrumb()
{
$sut = oxNew($this->sutClassName);
2022-11-17 00:27:43 +01:00
$this->assertIsArray(
$this->callMethod(
$sut,
2022-11-17 00:27:43 +01:00
'getBreadCrumb'
)
);
}
2022-12-13 22:24:33 +01:00
}