Skip to content

Commit

Permalink
EZP-30934: Removed usage of deprecated getCurrentUser method (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniTou authored and lserwatka committed Sep 24, 2019
1 parent 2aa72cb commit aee9259
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 55 deletions.
32 changes: 23 additions & 9 deletions src/ContextProvider/RoleIdentify.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/
namespace EzSystems\PlatformHttpCacheBundle\ContextProvider;

use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\UserService;
use FOS\HttpCache\UserContext\ContextProvider;
use FOS\HttpCache\UserContext\UserContext;

Expand All @@ -22,28 +24,40 @@
*/
class RoleIdentify implements ContextProvider
{
/**
* @var \eZ\Publish\Core\Repository\Repository
*/
/** @var \eZ\Publish\Core\Repository\Repository */
protected $repository;

public function __construct(Repository $repository)
{
/** @var \eZ\Publish\API\Repository\PermissionResolver */
private $permissionResolver;

/** @var \eZ\Publish\API\Repository\UserService */
private $userService;

public function __construct(
Repository $repository,
PermissionResolver $permissionResolver,
UserService $userService
) {
$this->repository = $repository;
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
}

public function updateUserContext(UserContext $context)
{
$user = $this->repository->getCurrentUser();
$user = $this->userService->loadUser(
$this->permissionResolver->getCurrentUserReference()->getUserId()
);

/** @var \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $roleAssignments */
$roleAssignments = $this->repository->sudo(
function (Repository $repository) use ($user) {
return $repository->getRoleService()->getRoleAssignmentsForUser($user, true);
}
);

$roleIds = array();
$limitationValues = array();
$roleIds = [];
$limitationValues = [];
/** @var \eZ\Publish\API\Repository\Values\User\UserRoleAssignment $roleAssignment */
foreach ($roleAssignments as $roleAssignment) {
$roleId = $roleAssignment->getRole()->id;
Expand All @@ -52,7 +66,7 @@ function (Repository $repository) use ($user) {
// If a limitation is present, store the limitation values by roleId
if ($limitation !== null) {
$limitationValuesKey = sprintf('%s-%s', $roleId, $limitation->getIdentifier());
$limitationValues[$limitationValuesKey] = array();
$limitationValues[$limitationValuesKey] = [];
foreach ($limitation->limitationValues as $value) {
$limitationValues[$limitationValuesKey][] = $value;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ services:

ezplatform.http_cache.user_context_provider.role_identify:
class: EzSystems\PlatformHttpCacheBundle\ContextProvider\RoleIdentify
arguments: ["@ezpublish.api.repository"]
arguments:
- '@ezpublish.api.repository'
- '@eZ\Publish\API\Repository\PermissionResolver'
- '@ezpublish.api.service.user'
tags:
- { name: fos_http_cache.user_context_provider }

Expand Down
109 changes: 64 additions & 45 deletions tests/ContextProvider/RoleIdentifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
namespace EzSystems\PlatformHttpCacheBundle\Tests\ContextProvider;

use eZ\Publish\API\Repository\RoleService;
use eZ\Publish\API\Repository\UserService;
use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation;
use eZ\Publish\API\Repository\Values\User\Role;
use eZ\Publish\API\Repository\Values\User\User as APIUser;
use eZ\Publish\API\Repository\Values\User\UserReference;
use eZ\Publish\Core\Repository\Repository;
use eZ\Publish\Core\Repository\Helper\LimitationService;
use eZ\Publish\Core\Repository\Helper\RoleDomainMapper;
use eZ\Publish\Core\Repository\Permission\PermissionResolver;
use eZ\Publish\Core\Repository\Repository;
use eZ\Publish\Core\Repository\Values\User\UserRoleAssignment;
use eZ\Publish\SPI\Persistence\User\Handler as SPIUserHandler;
use EzSystems\PlatformHttpCacheBundle\ContextProvider\RoleIdentify;
Expand All @@ -27,22 +28,22 @@
class RoleIdentifyTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\Repository
* @var \eZ\Publish\API\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
*/
private $repositoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\RoleService
* @var \eZ\Publish\API\Repository\RoleService|\PHPUnit_Framework_MockObject_MockObject
*/
private $roleServiceMock;

protected function setUp()
protected function setUp(): void
{
parent::setUp();
$this->repositoryMock = $this
->getMockBuilder(Repository::class)
->disableOriginalConstructor()
->setMethods(array('getRoleService', 'getCurrentUser', 'getPermissionResolver'))
->setMethods(['getRoleService', 'getPermissionResolver'])
->getMock();

$this->roleServiceMock = $this->createMock(RoleService::class);
Expand All @@ -51,112 +52,123 @@ protected function setUp()
->expects($this->any())
->method('getRoleService')
->willReturn($this->roleServiceMock);
$this->repositoryMock
->expects($this->any())
->method('getPermissionResolver')
->willReturn($this->getPermissionResolverMock());
}

public function testSetIdentity()
{
$user = $this->createMock(APIUser::class);
$userReference = $this->createMock(UserReference::class);
$userContext = new UserContext();

$permissionResolver = $this->getPermissionResolverMock();
$permissionResolver
->method('getCurrentUserReference')
->willReturn($userReference);

$this->repositoryMock
->expects($this->once())
->method('getCurrentUser')
->expects($this->any())
->method('getPermissionResolver')
->willReturn($permissionResolver);

$userService = $this->getUserServiceMock();
$userService
->method('loadUser')
->willReturn($user);

$roleId1 = 123;
$roleId2 = 456;
$roleId3 = 789;
$limitationForRole2 = $this->generateLimitationMock(
array(
'limitationValues' => array('/1/2', '/1/2/43'),
)
[
'limitationValues' => ['/1/2', '/1/2/43'],
]
);
$limitationForRole3 = $this->generateLimitationMock(
array(
'limitationValues' => array('foo', 'bar'),
)
[
'limitationValues' => ['foo', 'bar'],
]
);
$returnedRoleAssignments = array(
$returnedRoleAssignments = [
$this->generateRoleAssignmentMock(
array(
[
'role' => $this->generateRoleMock(
array(
[
'id' => $roleId1,
)
]
),
)
]
),
$this->generateRoleAssignmentMock(
array(
[
'role' => $this->generateRoleMock(
array(
[
'id' => $roleId2,
)
]
),
'limitation' => $limitationForRole2,
)
]
),
$this->generateRoleAssignmentMock(
array(
[
'role' => $this->generateRoleMock(
array(
[
'id' => $roleId3,
)
]
),
'limitation' => $limitationForRole3,
)
]
),
);
];

$this->roleServiceMock
->expects($this->once())
->method('getRoleAssignmentsForUser')
->with($user, true)
->willReturn($returnedRoleAssignments);

$this->assertSame(array(), $userContext->getParameters());
$contextProvider = new RoleIdentify($this->repositoryMock);
$this->assertSame([], $userContext->getParameters());
$contextProvider = new RoleIdentify(
$this->repositoryMock,
$permissionResolver,
$userService
);
$contextProvider->updateUserContext($userContext);
$userContextParams = $userContext->getParameters();
$this->assertArrayHasKey('roleIdList', $userContextParams);
$this->assertSame(array($roleId1, $roleId2, $roleId3), $userContextParams['roleIdList']);
$this->assertSame([$roleId1, $roleId2, $roleId3], $userContextParams['roleIdList']);
$this->assertArrayHasKey('roleLimitationList', $userContextParams);
$limitationIdentifierForRole2 = \get_class($limitationForRole2);
$limitationIdentifierForRole3 = \get_class($limitationForRole3);
$this->assertSame(
array(
"$roleId2-$limitationIdentifierForRole2" => array('/1/2', '/1/2/43'),
"$roleId3-$limitationIdentifierForRole3" => array('foo', 'bar'),
),
[
"$roleId2-$limitationIdentifierForRole2" => ['/1/2', '/1/2/43'],
"$roleId3-$limitationIdentifierForRole3" => ['foo', 'bar'],
],
$userContextParams['roleLimitationList']
);
}

private function generateRoleAssignmentMock(array $properties = array())
private function generateRoleAssignmentMock(array $properties = [])
{
return $this
->getMockBuilder(UserRoleAssignment::class)
->setConstructorArgs(array($properties))
->setConstructorArgs([$properties])
->getMockForAbstractClass();
}

private function generateRoleMock(array $properties = array())
private function generateRoleMock(array $properties = [])
{
return $this
->getMockBuilder(Role::class)
->setConstructorArgs(array($properties))
->setConstructorArgs([$properties])
->getMockForAbstractClass();
}

private function generateLimitationMock(array $properties = array())
private function generateLimitationMock(array $properties = [])
{
$limitationMock = $this
->getMockBuilder(RoleLimitation::class)
->setConstructorArgs(array($properties))
->setConstructorArgs([$properties])
->getMockForAbstractClass();
$limitationMock
->expects($this->any())
Expand All @@ -170,7 +182,7 @@ protected function getPermissionResolverMock()
{
return $this
->getMockBuilder(PermissionResolver::class)
->setMethods(null)
->setMethods(['getCurrentUserReference'])
->setConstructorArgs(
[
$this->createMock(RoleDomainMapper::class),
Expand All @@ -181,4 +193,11 @@ protected function getPermissionResolverMock()
)
->getMock();
}

protected function getUserServiceMock()
{
return $this
->getMockBuilder(UserService::class)
->getMockForAbstractClass();
}
}

0 comments on commit aee9259

Please sign in to comment.