mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
Complete AbstractStoredObjectVoterTest.php
This commit is contained in:
parent
719fabc878
commit
5d57ec8a3b
@ -1,21 +1,33 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
namespace Chill\DocStoreBundle\Tests\Security\Authorization;
|
namespace Chill\DocStoreBundle\Tests\Security\Authorization;
|
||||||
|
|
||||||
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
|
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
|
||||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||||
use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository;
|
|
||||||
use Chill\DocStoreBundle\Repository\AssociatedEntityToStoredObjectInterface;
|
use Chill\DocStoreBundle\Repository\AssociatedEntityToStoredObjectInterface;
|
||||||
use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter;
|
|
||||||
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
|
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
|
||||||
use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoters\AbstractStoredObjectVoter;
|
use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoters\AbstractStoredObjectVoter;
|
||||||
use Chill\DocStoreBundle\Service\WorkflowDocumentService;
|
use Chill\DocStoreBundle\Service\WorkflowDocumentService;
|
||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
use ChillDocStoreBundle\Security\Authorization\StoredObjectVoters\AccompanyingCourseStoredObjectVoter;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
|
||||||
class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
class AbstractStoredObjectVoterTest extends TestCase
|
||||||
{
|
{
|
||||||
private AssociatedEntityToStoredObjectInterface $repository;
|
private AssociatedEntityToStoredObjectInterface $repository;
|
||||||
private Security $security;
|
private Security $security;
|
||||||
@ -23,30 +35,42 @@ class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->repository = $this->createMock(AccompanyingCourseDocumentRepository::class);
|
$this->repository = $this->createMock(AssociatedEntityToStoredObjectInterface::class);
|
||||||
$this->security = $this->createMock(Security::class);
|
$this->security = $this->createMock(Security::class);
|
||||||
$this->workflowDocumentService = $this->createMock(WorkflowDocumentService::class);
|
$this->workflowDocumentService = $this->createMock(WorkflowDocumentService::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildStoredObjectVoter(bool $canBeAssociatedWithWorkflow, AssociatedEntityToStoredObjectInterface $repository, Security $security, ?WorkflowDocumentService $workflowDocumentService = null): AbstractStoredObjectVoter
|
||||||
|
{
|
||||||
// Anonymous class extending the abstract class
|
// Anonymous class extending the abstract class
|
||||||
$this->voter = new class($this->repository, $this->security, $this->workflowDocumentService) extends AbstractStoredObjectVoter {
|
return new class ($canBeAssociatedWithWorkflow, $repository, $security, $workflowDocumentService) extends AbstractStoredObjectVoter {
|
||||||
|
public function __construct(
|
||||||
|
private bool $canBeAssociatedWithWorkflow,
|
||||||
|
private AssociatedEntityToStoredObjectInterface $repository,
|
||||||
|
Security $security,
|
||||||
|
?WorkflowDocumentService $workflowDocumentService = null
|
||||||
|
) {
|
||||||
|
parent::__construct($security, $workflowDocumentService);
|
||||||
|
}
|
||||||
|
|
||||||
protected function attributeToRole($attribute): string
|
protected function attributeToRole($attribute): string
|
||||||
{
|
{
|
||||||
return AccompanyingCourseDocumentVoter::SEE_DETAILS;
|
return 'SOME_ROLE';
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getRepository(): AssociatedEntityToStoredObjectInterface
|
protected function getRepository(): AssociatedEntityToStoredObjectInterface
|
||||||
{
|
{
|
||||||
// TODO: Implement getRepository() method.
|
return $this->repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getClass(): string
|
protected function getClass(): string
|
||||||
{
|
{
|
||||||
// TODO: Implement getClass() method.
|
return \stdClass::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function canBeAssociatedWithWorkflow(): bool
|
protected function canBeAssociatedWithWorkflow(): bool
|
||||||
{
|
{
|
||||||
// TODO: Implement canBeAssociatedWithWorkflow() method.
|
return $this->canBeAssociatedWithWorkflow;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -61,7 +85,7 @@ class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase
|
|||||||
return [$user, $token, $subject, $entity];
|
return [$user, $token, $subject, $entity];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setupMocksForVoteOnAttribute(User $user, TokenInterface $token, bool $isGrantedForAccCourseDocument, AccompanyingCourseDocument $entity, bool $workflowAllowed): void
|
private function setupMocksForVoteOnAttribute(User $user, TokenInterface $token, bool $isGrantedForEntity, object $entity, bool $workflowAllowed): void
|
||||||
{
|
{
|
||||||
// Set up token to return user
|
// Set up token to return user
|
||||||
$token->method('getUser')->willReturn($user);
|
$token->method('getUser')->willReturn($user);
|
||||||
@ -69,24 +93,31 @@ class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase
|
|||||||
// Mock the return of an AccompanyingCourseDocument by the repository
|
// Mock the return of an AccompanyingCourseDocument by the repository
|
||||||
$this->repository->method('findAssociatedEntityToStoredObject')->willReturn($entity);
|
$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
|
// Mock scenario where user is allowed to see_details of the AccompanyingCourseDocument
|
||||||
$this->security->method('isGranted')->willReturnMap([
|
$this->security->method('isGranted')->willReturn($isGrantedForEntity);
|
||||||
[[AccompanyingCourseDocumentVoter::SEE_DETAILS, $entity], $isGrantedForAccCourseDocument],
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Mock case where user is blocked or not by workflow
|
// Mock case where user is blocked or not by workflow
|
||||||
$this->workflowDocumentService->method('notBlockedByWorkflow')->willReturn($workflowAllowed);
|
$this->workflowDocumentService->method('notBlockedByWorkflow')->willReturn($workflowAllowed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVoteOnAttributeAllowed(): void
|
public function testSupportsOnAttribute(): void
|
||||||
{
|
{
|
||||||
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
||||||
|
|
||||||
// Setup mocks for voteOnAttribute method
|
// Setup mocks for voteOnAttribute method
|
||||||
$this->setupMocksForVoteOnAttribute($user, $token, true, $entity, true);
|
$this->setupMocksForVoteOnAttribute($user, $token, true, $entity, true);
|
||||||
|
$voter = $this->buildStoredObjectVoter(true, $this->repository, $this->security, $this->workflowDocumentService);
|
||||||
|
|
||||||
|
self::assertTrue($voter->supports(StoredObjectRoleEnum::SEE, $subject));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVoteOnAttributeAllowedAndWorkflowAllowed(): void
|
||||||
|
{
|
||||||
|
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
||||||
|
|
||||||
|
// Setup mocks for voteOnAttribute method
|
||||||
|
$this->setupMocksForVoteOnAttribute($user, $token, true, $entity, true);
|
||||||
|
$voter = $this->buildStoredObjectVoter(true, $this->repository, $this->security, $this->workflowDocumentService);
|
||||||
|
|
||||||
// The voteOnAttribute method should return True when workflow is allowed
|
// The voteOnAttribute method should return True when workflow is allowed
|
||||||
self::assertTrue($voter->voteOnAttribute(StoredObjectRoleEnum::SEE, $subject, $token));
|
self::assertTrue($voter->voteOnAttribute(StoredObjectRoleEnum::SEE, $subject, $token));
|
||||||
@ -104,7 +135,7 @@ class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase
|
|||||||
self::assertFalse($voter->voteOnAttribute(StoredObjectRoleEnum::SEE, $subject, $token));
|
self::assertFalse($voter->voteOnAttribute(StoredObjectRoleEnum::SEE, $subject, $token));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVoteOnAttributeWhenBlockedByWorkflow(): void
|
public function testVoteOnAttributeAllowedWorkflowNotAllowed(): void
|
||||||
{
|
{
|
||||||
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
list($user, $token, $subject, $entity) = $this->setupMockObjects();
|
||||||
|
|
||||||
@ -119,16 +150,4 @@ class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase
|
|||||||
// Assert that access is denied when workflow is not allowed
|
// Assert that access is denied when workflow is not allowed
|
||||||
$this->assertFalse($result);
|
$this->assertFalse($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAbstractStoredObjectVoter(): void
|
|
||||||
{
|
|
||||||
$voter = new class extends AbstractStoredObjectVoter {
|
|
||||||
// Implement abstract methods here
|
|
||||||
public function someMethod() {
|
|
||||||
// method implementation
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Run tests on $voter
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user