|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Tests\Authorization\Voter; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; |
| 16 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 17 | +use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
| 18 | +use Symfony\Component\Security\Core\Authorization\Voter\ClosureVoter; |
| 19 | +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
| 20 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 21 | + |
| 22 | +class ClosureVoterTest extends TestCase |
| 23 | +{ |
| 24 | + private ClosureVoter $voter; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->voter = new ClosureVoter( |
| 29 | + $this->createMock(AccessDecisionManagerInterface::class), |
| 30 | + $this->createMock(AuthenticationTrustResolverInterface::class), |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function testEmptyAttributeAbstains() |
| 35 | + { |
| 36 | + $this->assertSame(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote( |
| 37 | + $this->createMock(TokenInterface::class), |
| 38 | + null, |
| 39 | + []) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testClosureReturningFalseDeniesAccess() |
| 44 | + { |
| 45 | + $token = $this->createMock(TokenInterface::class); |
| 46 | + $token->method('getRoleNames')->willReturn([]); |
| 47 | + $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); |
| 48 | + |
| 49 | + $this->assertSame(VoterInterface::ACCESS_DENIED, $this->voter->vote( |
| 50 | + $token, |
| 51 | + null, |
| 52 | + [fn (...$vars) => false] |
| 53 | + )); |
| 54 | + } |
| 55 | + |
| 56 | + public function testClosureReturningTrueGrantsAccess() |
| 57 | + { |
| 58 | + $token = $this->createMock(TokenInterface::class); |
| 59 | + $token->method('getRoleNames')->willReturn([]); |
| 60 | + $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); |
| 61 | + |
| 62 | + $this->assertSame(VoterInterface::ACCESS_GRANTED, $this->voter->vote( |
| 63 | + $token, |
| 64 | + null, |
| 65 | + [fn (...$vars) => true] |
| 66 | + )); |
| 67 | + } |
| 68 | + |
| 69 | + public function testArgumentsContent() |
| 70 | + { |
| 71 | + $token = $this->createMock(TokenInterface::class); |
| 72 | + $token->method('getRoleNames')->willReturn(['MY_ROLE', 'ANOTHER_ROLE']); |
| 73 | + $token->method('getUser')->willReturn($this->createMock(UserInterface::class)); |
| 74 | + |
| 75 | + $outerSubject = new \stdClass(); |
| 76 | + |
| 77 | + $this->voter->vote( |
| 78 | + $token, |
| 79 | + $outerSubject, |
| 80 | + [function (...$vars) use ($outerSubject) { |
| 81 | + $this->assertInstanceOf(TokenInterface::class, $vars['token']); |
| 82 | + $this->assertSame($outerSubject, $vars['subject']); |
| 83 | + |
| 84 | + $this->assertInstanceOf(AccessDecisionManagerInterface::class, $vars['accessDecisionManager']); |
| 85 | + $this->assertInstanceOf(AuthenticationTrustResolverInterface::class, $vars['trustResolver']); |
| 86 | + |
| 87 | + return true; |
| 88 | + }] |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments