adjust mockable function calls, make compatible to same class extensions from TOTP plugin

This commit is contained in:
Daniel Seifert 2022-12-07 12:03:24 +01:00
parent 318338552c
commit 001610645c
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
23 changed files with 84 additions and 56 deletions

View File

@ -48,7 +48,7 @@ class d3user_webauthn extends AdminDetailsController
{
$this->addTplParam('readonly', !$this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable());
$this->d3CallMockableParent('render');
$this->d3CallMockableFunction([AdminDetailsController::class, 'render']);
$soxId = $this->getEditObjectId();

View File

@ -81,7 +81,7 @@ class d3webauthnadminlogin extends AdminController
$afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class);
$afterLogin->changeLanguage();
return $this->d3CallMockableParent('render');
return $this->d3CallMockableFunction([AdminController::class, 'render']);
}
/**

View File

@ -38,9 +38,10 @@ trait checkoutGetUserTrait
*/
public function getUser()
{
$user = $this->d3CallMockableParent('getUser');
/** @var User|null $user */
$user = $this->d3CallMockableFunction([$this->parentClass, 'getUser']);
if ($user && $user->getId()) {
if ($user && $user->isLoaded() && $user->getId()) {
$webauthn = $this->d3GetMockableOxNewObject(Webauthn::class);
if ($webauthn->isActive($user->getId())

View File

@ -45,7 +45,7 @@ class d3webauthnlogin extends FrontendController
);
return array_merge(
$this->d3CallMockableParent('getNavigationParams'),
$this->d3CallMockableFunction([FrontendController::class, 'getNavigationParams']),
$navparams,
['cl' => $navparams['actcontrol']]
);
@ -73,7 +73,7 @@ class d3webauthnlogin extends FrontendController
$this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS));
return $this->d3CallMockableParent('render');
return $this->d3CallMockableFunction([FrontendController::class, 'render']);
}
/**

View File

@ -38,7 +38,7 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo
public function __construct()
{
$this->d3CallMockableParent('__construct', [PublicKeyCredential::class]);
$this->d3CallMockableFunction([ListModel::class, '__construct'], [PublicKeyCredential::class]);
}
/**

View File

@ -30,8 +30,11 @@ class WebauthnException extends StandardException
{
$this->setDetailedErrorMessage($sMessage);
$this->d3CallMockableParent(
'__construct',
$this->d3CallMockableFunction(
[
StandardException::class,
'__construct'
],
[
$this->getErrorMessageTranslator()->translateError($sMessage, $this->getRequestType()),
$iCode,

View File

@ -26,8 +26,11 @@ class RelyingPartyEntity extends PublicKeyCredentialRpEntity
public function __construct()
{
$this->d3CallMockableParent(
'__construct',
$this->d3CallMockableFunction(
[
PublicKeyCredentialRpEntity::class,
'__construct'
],
[
$this->getActiveShop()->getFieldData('oxname'),
$this->getRPShopUrl()

View File

@ -36,8 +36,11 @@ class UserEntity extends PublicKeyCredentialUserEntity
throw $e;
}
$this->d3CallMockableParent(
'__construct',
$this->d3CallMockableFunction(
[
PublicKeyCredentialUserEntity::class,
'__construct'
],
[
strtolower($user->getFieldData('oxusername')),
$user->getId(),

View File

@ -78,7 +78,7 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent
}
}
return $this->d3CallMockableParent('checklogin');
return $this->d3CallMockableFunction([d3_LoginController_Webauthn_parent::class, 'checklogin']);
}
/**

View File

@ -20,4 +20,6 @@ use D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait;
class d3_webauthn_OrderController extends d3_webauthn_OrderController_parent
{
use checkoutGetUserTrait;
private $parentClass = d3_webauthn_OrderController_parent::class;
}

View File

@ -20,4 +20,6 @@ use D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait;
class d3_webauthn_PaymentController extends d3_webauthn_PaymentController_parent
{
use checkoutGetUserTrait;
private $parentClass = d3_webauthn_PaymentController_parent::class;
}

View File

@ -20,4 +20,6 @@ use D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait;
class d3_webauthn_UserController extends d3_webauthn_UserController_parent
{
use checkoutGetUserTrait;
private $parentClass = d3_webauthn_UserController_parent::class;
}

View File

@ -36,7 +36,7 @@ class d3_User_Webauthn extends d3_User_Webauthn_parent
public function logout()
{
$return = $this->d3CallMockableParent('logout');
$return = $this->d3CallMockableFunction([d3_User_Webauthn_parent::class, 'logout']);
$session = $this->d3GetMockableRegistryObject(Session::class);
$session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH);

View File

@ -68,12 +68,12 @@ class d3user_webauthnTest extends TestCase
/** @var d3user_webauthn|MockObject $sutMock */
$sutMock = $this->getMockBuilder(d3user_webauthn::class)
->onlyMethods([
'd3CallMockableParent',
'd3CallMockableFunction',
'getEditObjectId',
'd3GetMockableOxNewObject'
])
->getMock();
$sutMock->method('d3CallMockableParent')->willReturn(true);
$sutMock->method('d3CallMockableFunction')->willReturn(true);
$sutMock->method('getEditObjectId')->willReturn('editObjectId');
$sutMock->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userMock, $webauthnMock) {

View File

@ -96,7 +96,7 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent', 'd3GetMockableOxNewObject',
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction', 'd3GetMockableOxNewObject',
'generateCredentialRequest', 'addTplParam'])
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
@ -112,7 +112,7 @@ class d3webauthnadminloginTest extends d3webauthnloginTest
}
}
);
$sut->method('d3CallMockableParent')->willReturn('myTemplate.tpl');
$sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl');
// "any" because redirect doesn't stop execution
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('generateCredentialRequest');

View File

@ -52,7 +52,7 @@ class d3webauthnloginTest extends UnitTestCase
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent'])
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction'])
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
@ -65,7 +65,7 @@ class d3webauthnloginTest extends UnitTestCase
}
}
);
$sut->method('d3CallMockableParent')->willReturn(['defKey1' => 'devValues1']);
$sut->method('d3CallMockableFunction')->willReturn(['defKey1' => 'devValues1']);
$this->assertSame(
[
@ -111,7 +111,7 @@ class d3webauthnloginTest extends UnitTestCase
/** @var d3webauthnlogin|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClassName)
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableParent',
->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction',
'generateCredentialRequest', 'addTplParam'])
->getMock();
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
@ -127,7 +127,7 @@ class d3webauthnloginTest extends UnitTestCase
}
}
);
$sut->method('d3CallMockableParent')->willReturn('myTemplate.tpl');
$sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl');
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())
->method('generateCredentialRequest');
$sut->expects($startRedirect ? $this->any() : $this->atLeastOnce())

View File

@ -42,9 +42,9 @@ class PublicKeyCredentialListTest extends UnitTestCase
/** @var PublicKeyCredentialList|MockObject $sut */
$sut = $this->getMockBuilder(PublicKeyCredentialList::class)
->disableOriginalConstructor()
->onlyMethods(['d3CallMockableParent'])
->onlyMethods(['d3CallMockableFunction'])
->getMock();
$sut->expects($this->once())->method('d3CallMockableParent')->with(
$sut->expects($this->once())->method('d3CallMockableFunction')->with(
$this->anything(),
$this->containsIdentical(PublicKeyCredential::class)
);

View File

@ -47,13 +47,13 @@ class WebauthnExceptionTest extends UnitTestCase
/** @var WebauthnException|MockObject $sut */
$sut = $this->getMockBuilder(WebauthnException::class)
->onlyMethods(['setDetailedErrorMessage', 'getErrorMessageTranslator', 'getRequestType',
'd3CallMockableParent'])
'd3CallMockableFunction'])
->disableOriginalConstructor()
->getMock();
$sut->expects($this->once())->method('setDetailedErrorMessage');
$sut->method('getErrorMessageTranslator')->willReturn($translatorMock);
$sut->method('getRequestType')->willReturn('requestType');
$sut->expects($this->once())->method('d3CallMockableParent')->with(
$sut->expects($this->once())->method('d3CallMockableFunction')->with(
$this->anything(),
$this->identicalTo(['translatedError', 255, $previousMock])
);

View File

@ -45,9 +45,9 @@ class RelyingPartyEntityTest extends TestCase
/** @var RelyingPartyEntity|MockObject $sut */
$sut = $this->getMockBuilder(RelyingPartyEntity::class)
->disableOriginalConstructor()
->onlyMethods(['d3CallMockableParent', 'getActiveShop', 'getRPShopUrl'])
->onlyMethods(['d3CallMockableFunction', 'getActiveShop', 'getRPShopUrl'])
->getMock();
$sut->expects($this->once())->method('d3CallMockableParent')->with(
$sut->expects($this->once())->method('d3CallMockableFunction')->with(
$this->anything(),
$this->identicalTo(['myShopName', 'myShopUrl'])
);
@ -156,9 +156,9 @@ class RelyingPartyEntityTest extends TestCase
/** @var RelyingPartyEntity|MockObject $sut */
$sut = $this->getMockBuilder(RelyingPartyEntity::class)
->onlyMethods(['d3CallMockableParent']) // must mock, because can't disable constructor
->onlyMethods(['d3CallMockableFunction']) // must mock, because can't disable constructor
->getMock();
$sut->method('d3CallMockableParent')->willReturn(true);
$sut->method('d3CallMockableFunction')->willReturn(true);
$this->assertSame(
$expected,

View File

@ -52,9 +52,9 @@ class UserEntityTest extends UnitTestCase
/** @var UserEntity|MockObject $sut */
$sut = $this->getMockBuilder(UserEntity::class)
->disableOriginalConstructor()
->onlyMethods(['d3CallMockableParent'])
->onlyMethods(['d3CallMockableFunction'])
->getMock();
$sut->expects($runParent ? $this->once() : $this->never())->method('d3CallMockableParent')->with(
$sut->expects($runParent ? $this->once() : $this->never())->method('d3CallMockableFunction')->with(
$this->anything(),
$this->identicalTo([
'usernamefixture',

View File

@ -247,12 +247,12 @@ class LoginControllerWebauthnTest extends UnitTestCase
/** @var LoginController|MockObject $sut */
$sut = $this->getMockBuilder(LoginController::class)
->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableParent', 'hasWebauthnButNotLoggedin',
->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'hasWebauthnButNotLoggedin',
'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject'
])
->getMock();
$sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn);
$sut->method('d3CallMockableParent')->willReturn('parentReturn');
$sut->method('d3CallMockableFunction')->willReturn('parentReturn');
$sut->method('hasWebauthnButNotLoggedin')->willReturn($loggedin);
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($userMock) {

View File

@ -31,6 +31,25 @@ trait CheckoutTestTrait
{
use CanAccessRestricted;
protected $userFixtureId = 'userIdFixture1';
/** @var User */
protected $userFixture;
public function setUp(): void
{
$this->userFixture = oxNew(User::class);
$this->userFixture->setId($this->userFixtureId);
$this->userFixture->assign(['oxlname' => __METHOD__]);
$this->userFixture->save();
$this->userFixture->load($this->userFixtureId);
}
public function tearDown(): void
{
$this->userFixture->delete($this->userFixtureId);
}
/**
* @test
* @param $hasUser
@ -46,18 +65,8 @@ trait CheckoutTestTrait
* @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::getUser
* @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::getUser
*/
public function canGetUser($hasUser, $userId, $isActive, $sessionAuth, $expected)
public function canGetUser($hasUser, $isActive, $sessionAuth, $expected)
{
if ($hasUser) {
/** @var User|MockObject $userMock */
$userMock = $this->getMockBuilder(User::class)
->onlyMethods(['getId'])
->getMock();
$userMock->method('getId')->willReturn($userId);
} else {
$userMock = false;
}
/** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['getVariable'])
@ -73,7 +82,7 @@ trait CheckoutTestTrait
/** @var PaymentController|OrderController|UserController|MockObject $sut */
$sut = $this->getMockBuilder($this->sutClass)
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3CallMockableParent'])
->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject'])
->getMock();
$sut->method('d3GetMockableOxNewObject')->willReturnCallback(
function () use ($webauthnMock) {
@ -97,15 +106,19 @@ trait CheckoutTestTrait
}
}
);
$sut->method('d3CallMockableParent')->willReturn($userMock);
if ($hasUser) {
$sut->setUser($this->userFixture);
}
$return = $this->callMethod(
$sut,
'getUser'
);
$sut->setUser(null);
if ($expected === 'parent') {
$this->assertSame($return, $userMock);
$this->assertSame($return, $hasUser ? $this->userFixture : false);
} else {
$this->assertSame($return, $expected);
}
@ -117,11 +130,10 @@ trait CheckoutTestTrait
public function canGetUserDataProvider(): array
{
return [
'no user' => [false, null, false, null, 'parent'],
'no user id' => [true, 'null', false, null, 'parent'],
'webauthn not active' => [true, 'userIdFixture', false, null, 'parent'],
'has webauthn auth' => [true, 'userIdFixture', true, 'userIdFixture', 'parent'],
'no webauthn auth' => [true, 'userIdFixture', true, null, false],
'no (valid) user' => [false, false, null, 'parent'],
'webauthn not active' => [true, false, null, 'parent'],
'has webauthn auth' => [true, true, 'userIdFixture', 'parent'],
'no webauthn auth' => [true, true, null, false],
];
}
}

View File

@ -43,9 +43,9 @@ class UserWebauthnTest extends UnitTestCase
/** @var User|MockObject $sut */
$sut = $this->getMockBuilder(User::class)
->onlyMethods(['d3CallMockableParent', 'd3GetMockableRegistryObject'])
->onlyMethods(['d3CallMockableFunction', 'd3GetMockableRegistryObject'])
->getMock();
$sut->method('d3CallMockableParent')->willReturn(true);
$sut->method('d3CallMockableFunction')->willReturn(true);
$sut->method('d3GetMockableRegistryObject')->willReturnCallback(
function () use ($sessionMock) {
$args = func_get_args();