mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Fixed: add scope to generated document ref #11
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
<?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 Service\DocGenerator;
|
||||
|
||||
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
||||
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
|
||||
use Chill\DocStoreBundle\Entity\DocumentCategory;
|
||||
use Chill\DocStoreBundle\Entity\PersonDocument;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
|
||||
use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
||||
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Service\DocGenerator\PersonContext;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Exception\Prediction\FailedPredictionException;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class PersonContextTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* Test that the build person context works in the case when 'form_show_scope' is false.
|
||||
*/
|
||||
public function testScopeDoNotShowScopeInForms()
|
||||
{
|
||||
$person = new Person();
|
||||
$docGen = (new DocGeneratorTemplate())
|
||||
->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
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user