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

303 lines
11 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Security\Voter;
use App\Entity\Application;
use App\Entity\Assignment;
use App\Entity\Destination;
use App\Entity\Disposition;
use App\Entity\Teamer;
use App\Entity\User;
use App\Security\Voter\DispositionVoter;
use PHPUnit\Framework\MockObject\Stub;
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;
/**
* The documents and the feedback simply do not exist on a skip-formalities assignment, so the
* voter is where that is enforced: it closes the blank-PDF routes and the upload and feedback
* screens at once, and every template that gates on is_granted() follows without a change.
*/
class DispositionVoterTest extends TestCase
{
private Security&Stub $security;
protected function setUp(): void
{
$this->security = $this->createStub(Security::class);
}
#[\PHPUnit\Framework\Attributes\DataProvider('documentAndFeedbackAttributes')]
public function testAnAdminIsDeniedOnASkipFormalitiesAssignment(string $attribute): void
{
$this->security->method('isGranted')->willReturn(true);
$this->assertSame(
VoterInterface::ACCESS_DENIED,
$this->vote($attribute, $this->createDisposition(skipFormalities: true)),
);
}
/**
* The same admin on a normal assignment must still get through - the flag is the only thing
* that may take these away.
*/
#[\PHPUnit\Framework\Attributes\DataProvider('documentAndFeedbackAttributes')]
public function testTheSameAdminIsGrantedOnANormalAssignment(string $attribute): void
{
$this->security->method('isGranted')->willReturn(true);
$this->assertSame(
VoterInterface::ACCESS_GRANTED,
$this->vote($attribute, $this->createDisposition(skipFormalities: false)),
);
}
/**
* @return array<string, array{string}>
*/
public static function documentAndFeedbackAttributes(): array
{
return [
'contract pdf and upload' => [DispositionVoter::CONTRACT],
'invoice pdf and upload' => [DispositionVoter::INVOICE],
'office upload on behalf of the teamer' => [DispositionVoter::CONTRACT_SUPPLEMENTARY],
'office upload of either document' => [DispositionVoter::ADMIN_DOCUMENT_UPLOAD],
'providing the feedback' => [DispositionVoter::FEEDBACK],
];
}
/**
* Viewing the placement and calling it off are unrelated to the formalities and have to keep
* working, or an admin could no longer cancel a teamer on such an assignment.
*/
public function testViewingAndCallingOffAreUnaffected(): void
{
$this->security->method('isGranted')->willReturn(true);
$disposition = $this->createDisposition(skipFormalities: true);
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->vote(DispositionVoter::VIEW, $disposition));
$this->assertSame(VoterInterface::ACCESS_GRANTED, $this->vote(DispositionVoter::CALL_OFF, $disposition));
}
/**
* The receipt window is the one permission in this voter that is not derived from a workflow
* transition, because there is none to derive it from: while the Honorarnote is being checked
* the state machine offers nothing at all, and that is exactly when receipts must stay
* manageable.
*/
#[\PHPUnit\Framework\Attributes\DataProvider('receiptWindowStatuses')]
public function testTheReceiptWindowIsTheTwoInvoiceStates(string $status, bool $expected): void
{
$this->security->method('isGranted')->willReturn(true);
$disposition = $this->createDisposition(skipFormalities: false)->setStatus($status);
$this->assertSame(
$expected ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,
$this->vote(DispositionVoter::MANAGE_RECEIPTS, $disposition),
);
}
/**
* @return array<string, array{string, bool}>
*/
public static function receiptWindowStatuses(): array
{
return [
'invoice may still be uploaded' => [Disposition::STATUS_ENDED, true],
'invoice is being checked' => [Disposition::STATUS_CHECKING_INVOICE, true],
'nothing has happened yet' => [Disposition::STATUS_NEW, false],
'contract is being checked' => [Disposition::STATUS_CHECKING_CONTRACT, false],
'assignment is still ahead' => [Disposition::STATUS_CONFIRMED, false],
'invoice was accepted' => [Disposition::STATUS_COMPLETED, false],
'placement was cancelled' => [Disposition::STATUS_CALLED_OFF, false],
];
}
public function testReceiptsAreDeniedOnASkipFormalitiesAssignment(): void
{
$this->security->method('isGranted')->willReturn(true);
$disposition = $this
->createDisposition(skipFormalities: true)
->setStatus(Disposition::STATUS_CHECKING_INVOICE)
;
$this->assertSame(
VoterInterface::ACCESS_DENIED,
$this->vote(DispositionVoter::MANAGE_RECEIPTS, $disposition),
);
}
/**
* The owning teamer gets in without being administrative; anyone else's teamer does not.
*/
#[\PHPUnit\Framework\Attributes\DataProvider('receiptOwnership')]
public function testOnlyTheOwningTeamerMayManageTheirReceipts(bool $owning, bool $expected): void
{
$this->security
->method('isGranted')
->willReturnCallback(fn (string $role): bool => 'ROLE_TEAMER' === $role)
;
$teamer = new Teamer();
$assignment = (new Assignment())->setSkipFormalities(false);
$disposition = (new Disposition(new Application($assignment, $teamer)))
->setStatus(Disposition::STATUS_CHECKING_INVOICE)
;
$user = (new User())->setTeamer($owning ? $teamer : new Teamer());
$token = $this->createStub(TokenInterface::class);
$token->method('getUser')->willReturn($user);
$this->assertSame(
$expected ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,
(new DispositionVoter($this->security, null))->vote($token, $disposition, [DispositionVoter::MANAGE_RECEIPTS]),
);
}
/**
* @return array<string, array{bool, bool}>
*/
public static function receiptOwnership(): array
{
return [
'their own placement' => [true, true],
'somebody else\'s placement' => [false, false],
];
}
public function testTheTeamerMayFillInTheQuestionnaireAfterAnEscort(): void
{
$this->assertQuestionnaire(true, $this->escortDisposition());
}
/**
* Ending today already counts as over, as in DispositionRepository's feedback queries.
*/
public function testTheQuestionnaireOpensOnTheLastDay(): void
{
$this->assertQuestionnaire(true, $this->escortDisposition(dateTo: 'today'));
}
/**
* @param \Closure(Disposition): mixed $change
*/
#[\PHPUnit\Framework\Attributes\DataProvider('questionnaireDenials')]
public function testTheQuestionnaireIsDenied(\Closure $change): void
{
$disposition = $this->escortDisposition();
$change($disposition);
$this->assertQuestionnaire(false, $disposition);
}
/**
* @return array<string, array{\Closure(Disposition): mixed}>
*/
public static function questionnaireDenials(): array
{
return [
'no bus escort' => [static fn (Disposition $d) => $d->getAssignment()->setPickup(0)],
'assignment not over yet' => [static fn (Disposition $d) => $d->getAssignment()->getDestination()->setDateTo(new \DateTimeImmutable('tomorrow'))],
'ended before go-live' => [static fn (Disposition $d) => $d->getAssignment()->getDestination()->setDateTo(new \DateTimeImmutable('2025-12-31'))],
'skip formalities' => [static fn (Disposition $d) => $d->getAssignment()->setSkipFormalities(true)],
'called off' => [static fn (Disposition $d) => $d->setStatus(Disposition::STATUS_CALLED_OFF)],
];
}
public function testTheQuestionnaireIsOfferedToNobodyWithoutAGoLiveDate(): void
{
$this->assertQuestionnaire(false, $this->escortDisposition(), goLive: null);
}
public function testAnotherTeamerMayNotFillInTheQuestionnaire(): void
{
$this->assertQuestionnaire(false, $this->escortDisposition(), user: (new User())->setTeamer(new Teamer()));
}
/**
* The office reads submitted responses through ROLE_ADMINISTRATIVE; filling one in on the
* teamer's behalf is not a thing.
*/
public function testTheOfficeMayNotFillInTheQuestionnaire(): void
{
$this->security
->method('isGranted')
->willReturnCallback(fn (string $role): bool => 'ROLE_ADMINISTRATIVE' === $role)
;
$disposition = $this->escortDisposition();
$this->assertSame(
VoterInterface::ACCESS_DENIED,
$this->questionnaireVote($disposition, (new User())->setTeamer($disposition->getTeamer()), '2026-01-01'),
);
}
public function testAMalformedGoLiveDateFailsLoudly(): void
{
$this->expectException(\InvalidArgumentException::class);
new DispositionVoter($this->security, '01.01.2026');
}
private function assertQuestionnaire(bool $expected, Disposition $disposition, ?string $goLive = '2026-01-01', ?User $user = null): void
{
$this->security
->method('isGranted')
->willReturnCallback(fn (string $role): bool => 'ROLE_TEAMER' === $role)
;
$this->assertSame(
$expected ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED,
$this->questionnaireVote($disposition, $user ?? (new User())->setTeamer($disposition->getTeamer()), $goLive),
);
}
private function questionnaireVote(Disposition $disposition, User $user, ?string $goLive): int
{
$token = $this->createStub(TokenInterface::class);
$token->method('getUser')->willReturn($user);
return (new DispositionVoter($this->security, $goLive))->vote($token, $disposition, [DispositionVoter::QUESTIONNAIRE]);
}
/**
* A bus escort that ended yesterday, after the go-live date the tests use.
*/
private function escortDisposition(string $dateTo = 'yesterday'): Disposition
{
$destination = (new Destination())->setDateTo(new \DateTimeImmutable($dateTo));
$assignment = (new Assignment())
->setDestination($destination)
->setPickup(4711)
->setSkipFormalities(false)
;
return (new Disposition(new Application($assignment, new Teamer())))->setStatus(Disposition::STATUS_ENDED);
}
private function vote(string $attribute, Disposition $disposition): int
{
return (new DispositionVoter($this->security, null))->vote(
$this->createStub(TokenInterface::class),
$disposition,
[$attribute],
);
}
private function createDisposition(bool $skipFormalities): Disposition
{
$assignment = (new Assignment())->setSkipFormalities($skipFormalities);
return new Disposition(new Application($assignment, new Teamer()));
}
}