mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
Add test for AccompayingCourseStoredObjectVoter
Mainly to check the voteOnAttribute method, by mocking a scenario where a person is allowed to see/edit an AccompanyingCourseDocument and not.
This commit is contained in:
parent
bd36735cb1
commit
d3956319ca
@ -0,0 +1,106 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\DocStoreBundle\Tests\Security\Authorization;
|
||||||
|
|
||||||
|
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
|
||||||
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||||
|
use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository;
|
||||||
|
use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter;
|
||||||
|
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
|
||||||
|
use Chill\DocStoreBundle\Service\WorkflowDocumentService;
|
||||||
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use ChillDocStoreBundle\Security\Authorization\StoredObjectVoters\AccompanyingCourseStoredObjectVoter;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||||
|
|
||||||
|
class AccompanyingCourseStoredObjectVoterTest extends PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
private $repository;
|
||||||
|
private $security;
|
||||||
|
private $workflowDocumentService;
|
||||||
|
private $voter;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->repository = $this->createMock(AccompanyingCourseDocumentRepository::class);
|
||||||
|
$this->security = $this->createMock(Security::class);
|
||||||
|
$this->workflowDocumentService = $this->createMock(WorkflowDocumentService::class);
|
||||||
|
|
||||||
|
$this->voter = new AccompanyingCourseStoredObjectVoter(
|
||||||
|
$this->repository,
|
||||||
|
$this->security,
|
||||||
|
$this->workflowDocumentService
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setupMockObjects(): array
|
||||||
|
{
|
||||||
|
$user = $this->createMock(User::class);
|
||||||
|
$token = $this->createMock(TokenInterface::class);
|
||||||
|
$subject = $this->createMock(StoredObject::class);
|
||||||
|
$entity = $this->createMock(AccompanyingCourseDocument::class);
|
||||||
|
|
||||||
|
return [$user, $token, $subject, $entity];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setupMocksForVoteOnAttribute(User $user, TokenInterface $token, bool $isGrantedForAccCourseDocument, AccompanyingCourseDocument $entity, bool $workflowAllowed): void
|
||||||
|
{
|
||||||
|
// Set up token to return user
|
||||||
|
$token->method('getUser')->willReturn($user);
|
||||||
|
|
||||||
|
// Mock the return of an AccompanyingCourseDocument by the repository
|
||||||
|
$this->repository->method('findAssociatedEntityToStoredObject')->willReturn($entity);
|
||||||
|
|
||||||
|
// Mock attributeToRole to return appropriate role
|
||||||
|
$this->voter->method('attributeToRole')->willReturn(AccompanyingCourseDocumentVoter::SEE_DETAILS);
|
||||||
|
|
||||||
|
// Mock scenario where user is allowed to see_details of the AccompanyingCourseDocument
|
||||||
|
$this->security->method('isGranted')->willReturnMap([
|
||||||
|
[[AccompanyingCourseDocumentVoter::SEE_DETAILS, $entity], $isGrantedForAccCourseDocument],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Mock case where user is blocked or not by workflow
|
||||||
|
$this->workflowDocumentService->method('notBlockedByWorkflow')->willReturn($workflowAllowed);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVoteOnAttributeAllowed(): void
|
||||||
|
{
|
||||||
|
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
||||||
|
|
||||||
|
// Setup mocks for voteOnAttribute method
|
||||||
|
$this->setupMocksForVoteOnAttribute($user, $token, true, $entity, true);
|
||||||
|
|
||||||
|
// The voteOnAttribute method should return True when workflow is allowed
|
||||||
|
$attributeSee = StoredObjectRoleEnum::SEE;
|
||||||
|
$attributeEdit = StoredObjectRoleEnum::EDIT;
|
||||||
|
$this->assertTrue($this->voter->voteOnAttribute($attributeSee, $subject, $token));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVoteOnAttributeNotAllowed(): void
|
||||||
|
{
|
||||||
|
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
||||||
|
|
||||||
|
// Setup mocks for voteOnAttribute method where isGranted() returns false
|
||||||
|
$this->setupMocksForVoteOnAttribute($user, $token, false, $entity, true);
|
||||||
|
|
||||||
|
// The voteOnAttribute method should return True when workflow is allowed
|
||||||
|
$attributeSee = StoredObjectRoleEnum::SEE;
|
||||||
|
$attributeEdit = StoredObjectRoleEnum::EDIT;
|
||||||
|
$this->assertTrue($this->voter->voteOnAttribute($attributeSee, $subject, $token));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVoteOnAttributeWhenBlockedByWorkflow(): void
|
||||||
|
{
|
||||||
|
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
||||||
|
|
||||||
|
// Setup mocks for voteOnAttribute method
|
||||||
|
$this->setupMocksForVoteOnAttribute($user, $token, $subject, $entity, false);
|
||||||
|
|
||||||
|
// Test voteOnAttribute method
|
||||||
|
$attribute = StoredObjectRoleEnum::SEE;
|
||||||
|
$result = $this->voter->voteOnAttribute($attribute, $subject, $token);
|
||||||
|
|
||||||
|
// Assert that access is denied when workflow is not allowed
|
||||||
|
$this->assertFalse($result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user