setName(['fr' => 'template']); $parameter = new ParameterBag(['chill_main' => ['acl' => ['form_show_scopes' => false]]]); $em = $this->prophesize(EntityManagerInterface::class); $em->persist(Argument::type(PersonDocument::class)) ->should(static function ($calls, $object, $method) { if (1 !== count($calls)) { throw new FailedPredictionException(sprintf('the persist should be called exactly once, %d receivved', count($calls))); } /** @var PersonDocument $personDocument */ $personDocument = $calls[0]->getArguments()[0]; if (null !== $personDocument->getScope()) { throw new FailedPredictionException('the person document should not have any scope'); } }); $personContext = $this->buildPersonContext( null, null, null, null, $em->reveal(), null, $parameter ); $this->assertFalse($personContext->hasPublicForm($docGen, $person)); $personContext->storeGenerated( $docGen, new StoredObject(), $person, [] ); } public function testScopeScopeMustBeShownInFormsAndUserAccessMultipleScope() { $person = new Person(); $docGen = (new DocGeneratorTemplate()) ->setName(['fr' => 'template']); $scope = new Scope(); $em = $this->prophesize(EntityManagerInterface::class); $em->persist(Argument::type(PersonDocument::class)) ->should(static function ($calls, $object, $method) use ($scope) { if (1 !== count($calls)) { throw new FailedPredictionException(sprintf('the persist should be called exactly once, %d receivved', count($calls))); } /** @var PersonDocument $personDocument */ $personDocument = $calls[0]->getArguments()[0]; if ($personDocument->getScope() !== $scope) { throw new FailedPredictionException('the person document should show the exactly prepared scope'); } }); $authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class); $authorizationHelper->getReachableScopes(Argument::type(UserInterface::class), PersonDocumentVoter::CREATE, Argument::type('array')) ->willReturn([$scope, new Scope()]); $personContext = $this->buildPersonContext( $authorizationHelper->reveal(), null, null, null, $em->reveal(), ); $this->assertTrue($personContext->hasPublicForm($docGen, $person)); $personContext->storeGenerated( $docGen, new StoredObject(), $person, ['scope' => $scope] ); } public function testScopeScopeMustBeShownInFormsAndUserAccessOneScope() { $person = new Person(); $docGen = (new DocGeneratorTemplate()) ->setName(['fr' => 'template']); $scope = new Scope(); $em = $this->prophesize(EntityManagerInterface::class); $em->persist(Argument::type(PersonDocument::class)) ->should(static function ($calls, $object, $method) use ($scope) { if (1 !== count($calls)) { throw new FailedPredictionException(sprintf('the persist should be called exactly once, %d receivved', count($calls))); } /** @var PersonDocument $personDocument */ $personDocument = $calls[0]->getArguments()[0]; if ($personDocument->getScope() !== $scope) { throw new FailedPredictionException('the person document should show the exactly prepared scope'); } }); $authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class); $authorizationHelper->getReachableScopes(Argument::type(UserInterface::class), PersonDocumentVoter::CREATE, Argument::type('array')) ->willReturn([$scope]); $personContext = $this->buildPersonContext( $authorizationHelper->reveal(), null, null, null, $em->reveal(), ); $this->assertTrue($personContext->hasPublicForm($docGen, $person)); $personContext->storeGenerated( $docGen, new StoredObject(), $person, ['scope' => $scope] ); } private function buildPersonContext( ?AuthorizationHelperInterface $authorizationHelper = null, ?BaseContextData $baseContextData = null, ?CenterResolverManagerInterface $centerResolverManager = null, ?DocumentCategoryRepository $documentCategoryRepository = null, ?EntityManagerInterface $em = null, ?NormalizerInterface $normalizer = null, ?ParameterBagInterface $parameterBag = null, ?Security $security = null, ?TranslatorInterface $translator = null, ?TranslatableStringHelperInterface $translatableStringHelper = null ): PersonContext { if (null === $authorizationHelper) { $authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class)->reveal(); } if (null === $baseContextData) { $baseContextData = $this->prophesize(BaseContextData::class)->reveal(); } if (null === $centerResolverManager) { $centerResolverManager = $this->prophesize(CenterResolverManagerInterface::class); $centerResolverManager->resolveCenters(Argument::any(), Argument::any()) ->willReturn([new Center()]); $centerResolverManager = $centerResolverManager->reveal(); } if (null === $documentCategoryRepository) { $documentCategoryRepository = $this->prophesize(DocumentCategoryRepository::class); $documentCategoryRepository->find(Argument::type('integer'))->willReturn( new DocumentCategory(PersonDocument::class, 1) ); $documentCategoryRepository = $documentCategoryRepository->reveal(); } if (null === $em) { $em = $this->prophesize(EntityManagerInterface::class)->reveal(); } if (null === $normalizer) { $normalizer = $this->prophesize(NormalizerInterface::class); $normalizer->normalize(Argument::type(Person::class), 'docgen', Argument::any()) ->willReturn(['type' => 'person']); $normalizer = $normalizer->reveal(); } if (null === $parameterBag) { $parameterBag = new ParameterBag(['chill_main' => ['acl' => ['form_show_scopes' => true]]]); } if (null === $security) { $security = $this->prophesize(Security::class); $security->getUser()->willReturn(new User()); $security = $security->reveal(); } if (null === $translator) { $translator = $this->prophesize(TranslatorInterface::class)->reveal(); } if (null === $translatableStringHelper) { $translatableStringHelper = $this->prophesize(TranslatableStringHelperInterface::class); // return only the 'fr' key $translatableStringHelper->localize(Argument::type('array'))->will(static function ($args) { return $args[0]['fr']; }); $translatableStringHelper = $translatableStringHelper->reveal(); } return new PersonContext( $authorizationHelper, $baseContextData, $centerResolverManager, $documentCategoryRepository, $em, $normalizer, $parameterBag, $security, $translator, $translatableStringHelper ); } }