Files
myep-team/tests/Security/Voter/DestinationVoterTest.php
T

87 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Security\Voter;
use App\Entity\Destination;
use App\Entity\Disposition;
use App\Entity\User;
use App\Repository\DispositionRepository;
use App\Security\Voter\DestinationVoter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
/**
* Who may fill in the house manager's questionnaire about a travel: a house manager of its house,
* once it has ended - on or after the go-live, and only if somebody worked on it. The house and
* the end date are also covered end to end by the HouseManager EditControllerTest.
*/
class DestinationVoterTest extends TestCase
{
private const string GO_LIVE = '2026-01-01';
public function testAHouseManagerOfTheHouseIsGrantedAnEndedTravel(): void
{
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->vote($this->travel('yesterday')));
}
/**
* @return iterable<string, array{0: Destination, 1?: bool, 2?: bool, 3?: list<string>}>
*/
public static function deniedCases(): iterable
{
yield 'travel not ended' => [self::travel('tomorrow')];
yield 'travel ended before the go-live' => [self::travel('2025-12-31')];
yield 'travel deleted' => [self::travel('yesterday')->setDeletedAt(new \DateTimeImmutable())];
yield 'nobody worked on the travel' => [self::travel('yesterday'), true, false];
yield 'not a house manager' => [self::travel('yesterday'), false];
yield 'house manager of another house' => [self::travel('yesterday'), true, true, ['AGR']];
}
/**
* @param list<string> $hotelCodes
*/
#[DataProvider('deniedCases')]
public function testDenied(Destination $destination, bool $houseManager = true, bool $placements = true, array $hotelCodes = ['DKI']): void
{
$this->assertSame(VoterInterface::ACCESS_DENIED, $this->vote($destination, $houseManager, $placements, $hotelCodes));
}
public function testOtherAttributesAndSubjectsAreNotVotedOn(): void
{
$voter = new DestinationVoter($this->createStub(Security::class), $this->createStub(DispositionRepository::class), self::GO_LIVE);
$token = $this->createStub(TokenInterface::class);
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $voter->vote($token, $this->travel('yesterday'), ['EDIT']));
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $voter->vote($token, new \stdClass(), [DestinationVoter::QUESTIONNAIRE]));
}
private static function travel(string $dateTo): Destination
{
return (new Destination())
->setHotelCode('DKI')
->setDateFrom(new \DateTimeImmutable($dateTo)->modify('-7 days'))
->setDateTo(new \DateTimeImmutable($dateTo)->setTime(0, 0))
;
}
/**
* @param list<string> $hotelCodes
*/
private function vote(Destination $destination, bool $houseManager = true, bool $placements = true, array $hotelCodes = ['DKI']): int
{
$security = $this->createStub(Security::class);
$security->method('isGranted')->willReturn($houseManager);
$repository = $this->createStub(DispositionRepository::class);
$repository->method('findForHouseManagerQuestionnaire')->willReturn($placements ? [$this->createStub(Disposition::class)] : []);
$token = $this->createStub(TokenInterface::class);
$token->method('getUser')->willReturn((new User())->setHotelCodes($hotelCodes));
return new DestinationVoter($security, $repository, self::GO_LIVE)->vote($token, $destination, [DestinationVoter::QUESTIONNAIRE]);
}
}