Julien Fastré 420dd4f868
Add SHARE permission to SavedExportVoter with tests
Introduced the SHARE attribute and updated SavedExportVoter to handle it. Added new functionality to check if a SavedExport is shared with a specific user and included corresponding unit tests for both the voter and entity behaviors.
2025-04-14 10:59:47 +02:00

115 lines
3.4 KiB
PHP

<?php
namespace Chill\MainBundle\Tests\Security\Authorization;
use Chill\MainBundle\Entity\SavedExport;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Security\Authorization\SavedExportVoter;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Security;
class SavedExportVoterTest extends TestCase
{
use ProphecyTrait;
/**
* @dataProvider voteProvider
*/
public function testVote(string $attribute, SavedExport $savedExport, User $user, $expectedResult, bool|null $isGranted = null): void
{
$security = $this->prophesize(Security::class);
if (null !== $isGranted) {
$security->isGranted(Argument::any(), Argument::any())->willReturn($isGranted);
}
$export = $this->prophesize(ExportInterface::class);
$exportManager = $this->prophesize(ExportManager::class);
$exportManager->getExport($savedExport->getExportAlias())->willReturn($export->reveal());
$voter = new SavedExportVoter($exportManager->reveal(), $security->reveal());
$token = new UsernamePasswordToken($user, 'default', ['ROLE_USER']);
self::assertEquals($expectedResult, $voter->vote($token, $savedExport, [$attribute]));
}
public static function voteProvider(): iterable
{
$alls = [SavedExportVoter::GENERATE, SavedExportVoter::GENERATE, SavedExportVoter::EDIT, SavedExportVoter::DELETE];
$userA = new User();
$userB = new User();
$userC = new User();
$group = new UserGroup();
$group->addUser($userC);
$savedExport = new SavedExport();
$savedExport->setExportAlias('dummy_export');
$savedExport->setUser($userA);
foreach ($alls as $attribute) {
yield [
$attribute,
$savedExport,
$userA,
VoterInterface::ACCESS_GRANTED,
true,
];
}
yield [
SavedExportVoter::GENERATE,
$savedExport,
$userA,
VoterInterface::ACCESS_DENIED,
false,
];
foreach ($alls as $attribute) {
yield [
$attribute,
$savedExport,
$userB,
VoterInterface::ACCESS_DENIED,
true,
];
}
$savedExport = new SavedExport();
$savedExport->setExportAlias('dummy_export');
$savedExport->setUser($userA);
$savedExport->addShare($userB);
yield [
SavedExportVoter::GENERATE,
$savedExport,
$userB,
VoterInterface::ACCESS_DENIED,
false,
];
yield [
SavedExportVoter::GENERATE,
$savedExport,
$userB,
VoterInterface::ACCESS_GRANTED,
true,
];
foreach ([SavedExportVoter::EDIT, SavedExportVoter::DELETE] as $attribute) {
yield [
$attribute,
$savedExport,
$userB,
VoterInterface::ACCESS_DENIED,
true,
];
}
}
}