Skip to content

Commit

Permalink
[Tests] Aligned PermissionTest with Base changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jul 17, 2024
1 parent 4bb9894 commit ca03e99
Showing 1 changed file with 27 additions and 44 deletions.
71 changes: 27 additions & 44 deletions tests/lib/Repository/Service/Mock/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
use Ibexa\Contracts\Core\Persistence\User\Policy;
use Ibexa\Contracts\Core\Persistence\User\Role;
use Ibexa\Contracts\Core\Persistence\User\RoleAssignment;
use Ibexa\Contracts\Core\Repository\PermissionResolver as APIPermissionResolver;
use Ibexa\Contracts\Core\Repository\Repository;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\Base\Exceptions\InvalidArgumentValue;
use Ibexa\Core\Base\Exceptions\NotFound\LimitationNotFoundException;
use Ibexa\Core\Repository\Permission\PermissionResolver;
use Ibexa\Core\Repository\Repository as CoreRepository;
use Ibexa\Core\Repository\Values\User\UserReference;
use Ibexa\Tests\Core\Repository\Service\Mock\Base as BaseServiceMockTest;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Mock test case for PermissionResolver.
Expand Down Expand Up @@ -107,20 +108,20 @@ public function testHasAccessReturnsTrue(array $roles, array $roleAssignments)
{
/** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */
$userHandlerMock = $this->getPersistenceMock()->userHandler();
$mockedService = $this->getPermissionResolverMock(null);
$mockedService = $this->getPermissionResolverWithMockedMethods([]);

$userHandlerMock
->expects(self::once())
->method('loadRoleAssignmentsByGroupId')
->with(self::equalTo(10), self::equalTo(true))
->will(self::returnValue($roleAssignments));
->willReturn($roleAssignments);

foreach ($roleAssignments as $at => $roleAssignment) {
$userHandlerMock
->expects(self::at($at + 1))
->method('loadRole')
->with($roleAssignment->roleId)
->will(self::returnValue($roles[$roleAssignment->roleId]));
->willReturn($roles[$roleAssignment->roleId]);
}

$result = $mockedService->hasAccess('dummy-module', 'dummy-function');
Expand Down Expand Up @@ -178,7 +179,7 @@ public function testHasAccessReturnsFalse(array $roles, array $roleAssignments)
{
/** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */
$userHandlerMock = $this->getPersistenceMock()->userHandler();
$service = $this->getPermissionResolverMock(null);
$service = $this->getPermissionResolverWithMockedMethods([]);

$userHandlerMock
->expects(self::once())
Expand Down Expand Up @@ -206,7 +207,7 @@ public function testHasAccessReturnsFalseButSudoSoTrue()
{
/** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */
$userHandlerMock = $this->getPersistenceMock()->userHandler();
$service = $this->getPermissionResolverMock(null);
$service = $this->getPermissionResolverWithMockedMethods([]);
$repositoryMock = $this->getRepositoryMock();
$repositoryMock
->expects(self::any())
Expand Down Expand Up @@ -291,7 +292,7 @@ public function testHasAccessReturnsPermissionSets(array $roles, array $roleAssi
/** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */
$userHandlerMock = $this->getPersistenceMock()->userHandler();
$roleDomainMapper = $this->getRoleDomainMapperMock(['buildDomainPolicyObject']);
$permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']);
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']);

$permissionResolverMock
->expects(self::once())
Expand Down Expand Up @@ -407,7 +408,7 @@ public function testHasAccessReturnsLimitationNotFoundException(array $roles, ar
/** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */
$userHandlerMock = $this->getPersistenceMock()->userHandler();
$roleDomainMapper = $this->getRoleDomainMapperMock();
$permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']);
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']);

$permissionResolverMock
->expects(self::once())
Expand Down Expand Up @@ -519,7 +520,7 @@ public function testHasAccessReturnsInvalidArgumentValueException(array $roles,
{
$this->expectException(InvalidArgumentValue::class);

$permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']);
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']);

/** @var $role \Ibexa\Contracts\Core\Persistence\User\Role */
foreach ($roles as $role) {
Expand Down Expand Up @@ -587,7 +588,7 @@ public function testHasAccessReturnsPermissionSetsWithRoleLimitation(array $role
$limitationTypeMock = $this->createMock(Type::class);
$limitationService = $this->getLimitationServiceMock();
$roleDomainMapper = $this->getRoleDomainMapperMock();
$permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']);
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']);

$permissionResolverMock
->expects(self::once())
Expand Down Expand Up @@ -690,13 +691,13 @@ public function providerForTestCanUserSimple()
*/
public function testCanUserSimple($permissionSets, $result)
{
$permissionResolverMock = $this->getPermissionResolverMock(['hasAccess']);
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['hasAccess']);

$permissionResolverMock
->expects(self::once())
->method('hasAccess')
->with(self::equalTo('test-module'), self::equalTo('test-function'))
->will(self::returnValue($permissionSets));
->willReturn($permissionSets);

/** @var $valueObject \Ibexa\Contracts\Core\Repository\Values\ValueObject */
$valueObject = $this->getMockForAbstractClass(ValueObject::class);
Expand All @@ -714,7 +715,7 @@ public function testCanUserSimple($permissionSets, $result)
*/
public function testCanUserWithoutLimitations()
{
$permissionResolverMock = $this->getPermissionResolverMock(
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods(
[
'hasAccess',
'getCurrentUserReference',
Expand Down Expand Up @@ -891,7 +892,7 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli
/** @var $valueObject \Ibexa\Contracts\Core\Repository\Values\ValueObject */
$valueObject = $this->createMock(ValueObject::class);
$limitationServiceMock = $this->getLimitationServiceMock();
$permissionResolverMock = $this->getPermissionResolverMock(
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods(
[
'hasAccess',
'getCurrentUserReference',
Expand All @@ -903,7 +904,7 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli
->expects(self::once())
->method('hasAccess')
->with(self::equalTo('test-module'), self::equalTo('test-function'))
->will(self::returnValue($permissionSets));
->willReturn($permissionSets);

$userRef = new UserReference(14);
$permissionResolverMock
Expand Down Expand Up @@ -974,9 +975,9 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli
/**
* Test for the setCurrentUserReference() and getCurrentUserReference() methods.
*/
public function testSetAndGetCurrentUserReference()
public function testSetAndGetCurrentUserReference(): void
{
$permissionResolverMock = $this->getPermissionResolverMock(null);
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods([]);
$userReferenceMock = $this->getUserReferenceMock();

$userReferenceMock
Expand All @@ -997,19 +998,19 @@ public function testSetAndGetCurrentUserReference()
*/
public function testGetCurrentUserReferenceReturnsAnonymousUser()
{
$permissionResolverMock = $this->getPermissionResolverMock(null);
$permissionResolverMock = $this->getPermissionResolverWithMockedMethods([]);

self::assertEquals(new UserReference(10), $permissionResolverMock->getCurrentUserReference());
}

protected $permissionResolverMock;
protected MockObject & APIPermissionResolver $permissionResolverMock;

/**
* @return \Ibexa\Contracts\Core\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject
* @param string[] $methods
*/
protected function getPermissionResolverMock($methods = [])
protected function getPermissionResolverWithMockedMethods(array $methods): MockObject & APIPermissionResolver
{
if ($this->permissionResolverMock === null) {
if (!isset($this->permissionResolverMock)) {
$configResolverMock = $this->createMock(ConfigResolverInterface::class);
$configResolverMock
->method('getParameter')
Expand All @@ -1018,7 +1019,7 @@ protected function getPermissionResolverMock($methods = [])

$this->permissionResolverMock = $this
->getMockBuilder(PermissionResolver::class)
->setMethods($methods)
->onlyMethods($methods)
->setConstructorArgs(
[
$this->getRoleDomainMapperMock(),
Expand Down Expand Up @@ -1051,32 +1052,14 @@ protected function getPermissionResolverMock($methods = [])
return $this->permissionResolverMock;
}

protected $userReferenceMock;
protected MockObject & UserReference $userReferenceMock;

protected function getUserReferenceMock()
protected function getUserReferenceMock(): MockObject & UserReference
{
if ($this->userReferenceMock === null) {
if (!isset($this->userReferenceMock)) {
$this->userReferenceMock = $this->createMock(UserReference::class);
}

return $this->userReferenceMock;
}

protected $repositoryMock;

/**
* @return \Ibexa\Contracts\Core\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getRepositoryMock(): Repository
{
if ($this->repositoryMock === null) {
$this->repositoryMock = $this
->getMockBuilder(CoreRepository::class)
->onlyMethods(['getPermissionResolver'])
->disableOriginalConstructor()
->getMock();
}

return $this->repositoryMock;
}
}

0 comments on commit ca03e99

Please sign in to comment.