webauthn/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php

132 lines
4.2 KiB
PHP
Raw Normal View History

2022-12-01 00:45:39 +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\Modules\Application\Controller;
use D3\TestingTools\Development\CanAccessRestricted;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf;
use OxidEsales\Eshop\Application\Controller\OrderController;
use OxidEsales\Eshop\Application\Controller\PaymentController;
use OxidEsales\Eshop\Application\Controller\UserController;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Session;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
trait CheckoutTestTrait
{
use CanAccessRestricted;
protected $userFixtureId = 'userIdFixture1';
/** @var User */
protected $userFixture;
public function setUp(): void
{
2023-01-21 13:50:18 +01:00
parent::setUp();
$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
{
2023-01-21 13:50:18 +01:00
parent::tearDown();
$this->userFixture->delete($this->userFixtureId);
}
2022-12-01 00:45:39 +01:00
/**
* @test
*
2022-12-01 00:45:39 +01:00
* @param $hasUser
* @param $isAvailable
2022-12-01 00:45:39 +01:00
* @param $isActive
* @param $sessionAuth
* @param $expected
*
2022-12-01 00:45:39 +01:00
* @return void
* @throws ReflectionException
* @dataProvider canGetUserDataProvider
2022-12-12 23:41:07 +01:00
* @covers \D3\Webauthn\Application\Controller\Traits\checkoutGetUserTrait::getUser
* @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController::getUser
* @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController::getUser
* @covers \D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController::getUser
2022-12-01 00:45:39 +01:00
*/
public function canGetUser($hasUser, $isAvailable, $isActive, $sessionAuth, $expected)
2022-12-01 00:45:39 +01:00
{
/** @var Session|MockObject $sessionMock */
$sessionMock = $this->getMockBuilder(Session::class)
->onlyMethods(['getVariable'])
->getMock();
$sessionMock->method('getVariable')
->with($this->identicalTo(WebauthnConf::WEBAUTHN_SESSION_AUTH))->willReturn($sessionAuth);
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock);
2022-12-01 00:45:39 +01:00
/** @var Webauthn|MockObject $webauthnMock */
$webauthnMock = $this->getMockBuilder(Webauthn::class)
->onlyMethods(['isAvailable', 'isActive'])
2022-12-01 00:45:39 +01:00
->getMock();
$webauthnMock->method('isAvailable')->willReturn($isAvailable);
2022-12-01 00:45:39 +01:00
$webauthnMock->method('isActive')->willReturn($isActive);
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->set(Webauthn::class, $webauthnMock);
2022-12-01 00:45:39 +01:00
2023-01-21 13:50:18 +01:00
/** @var PaymentController|OrderController|UserController $sut */
$sut = oxNew($this->sutClass);
if ($hasUser) {
$sut->setUser($this->userFixture);
}
2022-12-01 00:45:39 +01:00
$return = $this->callMethod(
$sut,
'getUser'
);
2022-12-12 23:41:07 +01:00
$sut->setUser(oxNew(User::class));
2022-12-01 00:45:39 +01:00
if ($expected === 'parent') {
2022-12-13 22:24:33 +01:00
$this->assertSame($return, $hasUser ? $this->userFixture : false);
2022-12-01 00:45:39 +01:00
} else {
$this->assertSame($return, $expected);
}
2022-12-12 23:41:07 +01:00
// reset cache
$this->setValue(
$sut,
'_oActUser',
null
);
2022-12-01 00:45:39 +01:00
}
/**
* @return array
*/
public function canGetUserDataProvider(): array
{
return [
'no (valid) user' => [false, true, false, null, 'parent'],
'webauthn not available'=> [true, false, false, null, 'parent'],
'webauthn not active' => [true, true, false, null, 'parent'],
'has webauthn auth' => [true, true, true, 'userIdFixture', 'parent'],
'no webauthn auth' => [true, true, true, null, false],
2022-12-01 00:45:39 +01:00
];
}
2022-12-13 22:24:33 +01:00
}