1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\TestBundle\Tests\Security; |
4
|
|
|
|
5
|
|
|
use Overwatch\TestBundle\DataFixtures\ORM\TestGroupFixtures; |
6
|
|
|
use Overwatch\TestBundle\Security\TestGroupVoter; |
7
|
|
|
use Overwatch\UserBundle\DataFixtures\ORM\UserFixtures; |
8
|
|
|
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase; |
9
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* TestGroupVoterTest |
13
|
|
|
* Functional test of TestGroupVoter |
14
|
|
|
*/ |
15
|
|
|
class TestGroupVoterTest extends DatabaseAwareTestCase |
16
|
|
|
{ |
17
|
|
|
private $voter; |
18
|
|
|
private $tokens = []; |
19
|
|
|
|
20
|
|
|
const FIREWALL = 'overwatch'; |
21
|
|
|
|
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
parent::setUp(); |
25
|
|
|
$this->voter = new TestGroupVoter; |
26
|
|
|
$this->tokens = [ |
27
|
|
|
'superadmin' => $this->createUserToken(UserFixtures::$users['user-1'], self::FIREWALL), |
28
|
|
|
'admin' => $this->createUserToken(UserFixtures::$users['user-3'], self::FIREWALL), |
29
|
|
|
'user' => $this->createUserToken(UserFixtures::$users['user-2'], self::FIREWALL) |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function testVoteGroupView() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->assertGranted($this->voter->vote( |
36
|
|
|
$this->tokens['superadmin'], |
37
|
|
|
TestGroupFixtures::$groups['group-1'], |
38
|
|
|
[TestGroupVoter::VIEW] |
39
|
|
|
)); |
40
|
|
|
|
41
|
|
|
$this->assertGranted($this->voter->vote( |
42
|
|
|
$this->tokens['user'], |
43
|
|
|
TestGroupFixtures::$groups['group-1'], |
44
|
|
|
[TestGroupVoter::VIEW] |
45
|
|
|
)); |
46
|
|
|
|
47
|
|
|
$this->assertDenied($this->voter->vote( |
48
|
|
|
$this->tokens['admin'], |
49
|
|
|
TestGroupFixtures::$groups['group-1'], |
50
|
|
|
[TestGroupVoter::VIEW] |
51
|
|
|
)); |
52
|
|
|
|
53
|
|
|
$this->assertGranted($this->voter->vote( |
54
|
|
|
$this->tokens['superadmin'], |
55
|
|
|
TestGroupFixtures::$groups['group-2'], |
56
|
|
|
[TestGroupVoter::VIEW] |
57
|
|
|
)); |
58
|
|
|
|
59
|
|
|
$this->assertDenied($this->voter->vote( |
60
|
|
|
$this->tokens['user'], |
61
|
|
|
TestGroupFixtures::$groups['group-2'], |
62
|
|
|
[TestGroupVoter::VIEW] |
63
|
|
|
)); |
64
|
|
|
|
65
|
|
|
$this->assertGranted($this->voter->vote( |
66
|
|
|
$this->tokens['admin'], |
67
|
|
|
TestGroupFixtures::$groups['group-2'], |
68
|
|
|
[TestGroupVoter::VIEW] |
69
|
|
|
)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testVoteGroupEdit() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->assertGranted($this->voter->vote( |
75
|
|
|
$this->tokens['superadmin'], |
76
|
|
|
TestGroupFixtures::$groups['group-1'], |
77
|
|
|
[TestGroupVoter::EDIT] |
78
|
|
|
)); |
79
|
|
|
|
80
|
|
|
$this->assertDenied($this->voter->vote( |
81
|
|
|
$this->tokens['user'], |
82
|
|
|
TestGroupFixtures::$groups['group-1'], |
83
|
|
|
[TestGroupVoter::EDIT] |
84
|
|
|
)); |
85
|
|
|
|
86
|
|
|
$this->assertDenied($this->voter->vote( |
87
|
|
|
$this->tokens['admin'], |
88
|
|
|
TestGroupFixtures::$groups['group-1'], |
89
|
|
|
[TestGroupVoter::EDIT] |
90
|
|
|
)); |
91
|
|
|
|
92
|
|
|
$this->assertGranted($this->voter->vote( |
93
|
|
|
$this->tokens['superadmin'], |
94
|
|
|
TestGroupFixtures::$groups['group-2'], |
95
|
|
|
[TestGroupVoter::EDIT] |
96
|
|
|
)); |
97
|
|
|
|
98
|
|
|
$this->assertDenied($this->voter->vote( |
99
|
|
|
$this->tokens['user'], |
100
|
|
|
TestGroupFixtures::$groups['group-2'], |
101
|
|
|
[TestGroupVoter::EDIT] |
102
|
|
|
)); |
103
|
|
|
|
104
|
|
|
$this->assertGranted($this->voter->vote( |
105
|
|
|
$this->tokens['admin'], |
106
|
|
|
TestGroupFixtures::$groups['group-2'], |
107
|
|
|
[TestGroupVoter::EDIT] |
108
|
|
|
)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testVoteInvalid() |
112
|
|
|
{ |
113
|
|
|
$this->assertAbstain($this->voter->vote( |
114
|
|
|
$this->tokens['superadmin'], |
115
|
|
|
new \stdClass, |
116
|
|
|
[TestGroupVoter::VIEW] |
117
|
|
|
)); |
118
|
|
|
|
119
|
|
|
$this->assertAbstain($this->voter->vote( |
120
|
|
|
$this->tokens['superadmin'], |
121
|
|
|
TestGroupFixtures::$groups['group-2'], |
122
|
|
|
['BACON'] //No authority to issue bacon rights. |
123
|
|
|
)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function assertGranted($vote) |
127
|
|
|
{ |
128
|
|
|
$this->assertEquals(VoterInterface::ACCESS_GRANTED, $vote); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function assertAbstain($vote) |
132
|
|
|
{ |
133
|
|
|
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $vote); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function assertDenied($vote) |
137
|
|
|
{ |
138
|
|
|
$this->assertEquals(VoterInterface::ACCESS_DENIED, $vote); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.