|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Tactician\Bundle\Tests\Security\Voter; |
|
4
|
|
|
|
|
5
|
|
|
use League\Tactician\Bundle\Security\Voter\HandleCommandVoter; |
|
6
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
|
7
|
|
|
use Mockery; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Role\Role; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Unit test for the handle command voter |
|
16
|
|
|
* |
|
17
|
|
|
* @author Ron Rademaker |
|
18
|
|
|
*/ |
|
19
|
|
|
class HandleCommandVoterTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Tests the vote method. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $attribute |
|
25
|
|
|
* @param mixed $subject |
|
26
|
|
|
* @param array $roles |
|
27
|
|
|
* @param array $mapping |
|
28
|
|
|
* @param int $expected |
|
29
|
|
|
* |
|
30
|
|
|
* @dataProvider provideTestVoteData |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testVote(string $attribute, $subject, array $roles, array $mapping, int $expected) |
|
33
|
|
|
{ |
|
34
|
|
|
$voter = new HandleCommandVoter($mapping); |
|
35
|
|
|
$tokenMock = Mockery::mock(TokenInterface::class); |
|
36
|
|
|
$tokenMock->shouldReceive('getRoles')->andReturn($roles); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals($expected, $voter->vote($tokenMock, $subject, [$attribute])); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Gets the testdata for the vote test. |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function provideTestVoteData() |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
// Testcase: default access is false |
|
50
|
|
|
['handle', new FakeCommand, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_DENIED], |
|
51
|
|
|
|
|
52
|
|
|
// Testcase: abstain when not handling a command, but using the handle attribute |
|
53
|
|
|
['handle', null, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_ABSTAIN], |
|
54
|
|
|
|
|
55
|
|
|
// Testcase: abstain when not handling a command and not using the handle attribute |
|
56
|
|
|
['create', null, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_ABSTAIN], |
|
57
|
|
|
|
|
58
|
|
|
// Testcase: abstain when not handling a command |
|
59
|
|
|
['create', new FakeCommand, [new Role('ROLE_ADMIN')], [FakeCommand::class => ['ROLE_ADMIN']], VoterInterface::ACCESS_ABSTAIN], |
|
60
|
|
|
|
|
61
|
|
|
// Testcase: default is unrelated to roles |
|
62
|
|
|
['handle', new FakeCommand, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_DENIED], |
|
63
|
|
|
|
|
64
|
|
|
// Testcase: deny access if incorrect role |
|
65
|
|
|
['handle', new FakeCommand, [new Role('ROLE_ADMIN')], [FakeCommand::class => ['ROLE_USER']], VoterInterface::ACCESS_DENIED], |
|
66
|
|
|
|
|
67
|
|
|
// Testcase: grant access if decision manager returns true and the command is in the mapping |
|
68
|
|
|
['handle', new FakeCommand, [new Role('ROLE_USER')], [FakeCommand::class => ['ROLE_USER']], VoterInterface::ACCESS_GRANTED], |
|
69
|
|
|
|
|
70
|
|
|
// Testcase: deny access if the command is not in the mapping (i.e. a default deny access case) |
|
71
|
|
|
['handle', new FakeCommand, [new Role('ROLE_USER')], ['someOtherCommand' => ['ROLE_USER']], VoterInterface::ACCESS_DENIED], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|