diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AbstractStoredObjectVoterTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AbstractStoredObjectVoterTest.php index c384e48b9..24370c2db 100644 --- a/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AbstractStoredObjectVoterTest.php +++ b/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AbstractStoredObjectVoterTest.php @@ -1,21 +1,33 @@ repository = $this->createMock(AccompanyingCourseDocumentRepository::class); + $this->repository = $this->createMock(AssociatedEntityToStoredObjectInterface::class); $this->security = $this->createMock(Security::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 - $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 { - return AccompanyingCourseDocumentVoter::SEE_DETAILS; + return 'SOME_ROLE'; } protected function getRepository(): AssociatedEntityToStoredObjectInterface { - // TODO: Implement getRepository() method. + return $this->repository; } protected function getClass(): string { - // TODO: Implement getClass() method. + return \stdClass::class; } 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]; } - 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 $token->method('getUser')->willReturn($user); @@ -69,24 +93,31 @@ class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase // 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], - ]); + $this->security->method('isGranted')->willReturn($isGrantedForEntity); // Mock case where user is blocked or not by workflow $this->workflowDocumentService->method('notBlockedByWorkflow')->willReturn($workflowAllowed); } - public function testVoteOnAttributeAllowed(): void + public function testSupportsOnAttribute(): 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); + + 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 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)); } - public function testVoteOnAttributeWhenBlockedByWorkflow(): void + public function testVoteOnAttributeAllowedWorkflowNotAllowed(): void { 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 $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 - } - }