mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-31 09:18:24 +00:00 
			
		
		
		
	Complete AbstractStoredObjectVoterTest.php
This commit is contained in:
		| @@ -1,21 +1,33 @@ | ||||
| <?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; | ||||
|  | ||||
| use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; | ||||
| use Chill\DocStoreBundle\Entity\StoredObject; | ||||
| use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository; | ||||
| use Chill\DocStoreBundle\Repository\AssociatedEntityToStoredObjectInterface; | ||||
| use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter; | ||||
| use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum; | ||||
| use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoters\AbstractStoredObjectVoter; | ||||
| 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; | ||||
| use Symfony\Component\Security\Core\Security; | ||||
|  | ||||
| class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase | ||||
| /** | ||||
|  * @internal | ||||
|  * | ||||
|  * @coversNothing | ||||
|  */ | ||||
| class AbstractStoredObjectVoterTest extends TestCase | ||||
| { | ||||
|     private AssociatedEntityToStoredObjectInterface $repository; | ||||
|     private Security $security; | ||||
| @@ -23,30 +35,42 @@ class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase | ||||
|  | ||||
|     protected function setUp(): void | ||||
|     { | ||||
|         $this->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 | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user