1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Validates the correct behavior of the voter |
4
|
|
|
*/ |
5
|
|
|
namespace Graviton\SecurityBundle\Voter; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Graviton\TestBundle\Test\GravitonTestCase; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
class OwnContextVoterTest extends GravitonTestCase |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* validates isGranted |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function testIsGrantedNoValidUser() |
26
|
|
|
{ |
27
|
|
|
$attribute = 'VIEW'; |
28
|
|
|
$object = new \stdClass(); |
29
|
|
|
|
30
|
|
|
$voter = $this->getVoterProxy(array('voteOnAttribute')); |
31
|
|
|
|
32
|
|
|
$token = $this |
33
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') |
34
|
|
|
->getMock(); |
35
|
|
|
|
36
|
|
|
$this->assertFalse($voter->voteOnAttribute($attribute, $object, $token)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* validates isGranted |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function testIsGranted() |
45
|
|
|
{ |
46
|
|
|
$object = new \stdClass(); |
47
|
|
|
|
48
|
|
|
$contractDouble = $this->getContractDouble(['getAccount', 'getCustomer']); |
49
|
|
|
$contractDouble |
50
|
|
|
->expects($this->any()) |
51
|
|
|
->method('getAccount') |
52
|
|
|
->willReturn(new ArrayCollection([$object])); |
53
|
|
|
$contractDouble |
54
|
|
|
->expects($this->any()) |
55
|
|
|
->method('getCustomer') |
56
|
|
|
->willReturn($object); |
57
|
|
|
|
58
|
|
|
$userDouble = $this->getSimpleTestDouble( |
59
|
|
|
'\Graviton\SecurityBundle\Entities\SecurityContract', |
60
|
|
|
array('getContract') |
61
|
|
|
); |
62
|
|
|
$userDouble |
63
|
|
|
->expects($this->once()) |
64
|
|
|
->method('getContract') |
65
|
|
|
->willReturn($contractDouble); |
66
|
|
|
|
67
|
|
|
$token = $this |
68
|
|
|
->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') |
69
|
|
|
->getMock(); |
70
|
|
|
$token->expects($this->once()) |
71
|
|
|
->method('getUser') |
72
|
|
|
->willReturn($userDouble); |
73
|
|
|
|
74
|
|
|
$voter = $this->getVoterProxy(array('voteOnAttribute')); |
75
|
|
|
|
76
|
|
|
$this->assertFalse($voter->voteOnAttribute('VIEW', $object, $token)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* verifies grandByAccount |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function testGrantByAccountGrandAccess() |
85
|
|
|
{ |
86
|
|
|
$contractDouble = $this->getContractDouble(array('getAccount')); |
87
|
|
|
$contractDouble |
88
|
|
|
->expects($this->any()) |
89
|
|
|
->method('getAccount') |
90
|
|
|
->willReturn(new ArrayCollection([new \stdClass])); |
91
|
|
|
|
92
|
|
|
$voter = $this->getVoterProxy(array('grantByAccount')); |
93
|
|
|
|
94
|
|
|
$this->assertFalse($voter->grantByAccount($contractDouble, new \stdClass)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* verifies grandByAccount |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function testGrantByAccountDenyAccess() |
103
|
|
|
{ |
104
|
|
|
$accountDouble = $this->getMockBuilder('\GravitonDyn\AccountBundle\Document\Account') |
105
|
|
|
->disableOriginalConstructor() |
106
|
|
|
->getMock(); |
107
|
|
|
|
108
|
|
|
$contractDouble = $this->getContractDouble(array('getAccount')); |
109
|
|
|
$contractDouble |
110
|
|
|
->expects($this->any()) |
111
|
|
|
->method('getAccount') |
112
|
|
|
->willReturn(new ArrayCollection([$accountDouble])); |
113
|
|
|
|
114
|
|
|
$voter = $this->getVoterProxy(array('grantByAccount')); |
115
|
|
|
|
116
|
|
|
$this->assertTrue($voter->grantByAccount($contractDouble, $accountDouble)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* verifies grantByCustomer |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function testGrantByCustomerGrandAccess() |
125
|
|
|
{ |
126
|
|
|
$contractDouble = $this->getContractDouble(array('getCustomer')); |
127
|
|
|
$contractDouble |
128
|
|
|
->expects($this->any()) |
129
|
|
|
->method('getCustomer') |
130
|
|
|
->willReturn(new \stdClass); |
131
|
|
|
|
132
|
|
|
$voter = $this->getVoterProxy(array('grantByCustomer')); |
133
|
|
|
|
134
|
|
|
$this->assertFalse($voter->grantByCustomer($contractDouble, new \stdClass)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* verifies grantByCustomer |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function testGrantByCustomerDenyAccess() |
143
|
|
|
{ |
144
|
|
|
$customerDouble = $this->getMockBuilder('\GravitonDyn\CustomerBundle\Document\Customer') |
145
|
|
|
->disableOriginalConstructor() |
146
|
|
|
->getMock(); |
147
|
|
|
|
148
|
|
|
$contractDouble = $this->getContractDouble(array('getCustomer')); |
149
|
|
|
$contractDouble |
150
|
|
|
->expects($this->any()) |
151
|
|
|
->method('getCustomer') |
152
|
|
|
->willReturn($customerDouble); |
153
|
|
|
|
154
|
|
|
$voter = $this->getVoterProxy(array('grantByCustomer')); |
155
|
|
|
|
156
|
|
|
$this->assertTrue($voter->grantByCustomer($contractDouble, $customerDouble)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Provides test double of the Contract entity. |
161
|
|
|
* |
162
|
|
|
* @param array $methods Set of methods to be doubled. |
163
|
|
|
* |
164
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\GravitonDyn\ContractBundle\Document\Contract |
165
|
|
|
*/ |
166
|
|
|
public function getContractDouble(array $methods = array()) |
167
|
|
|
{ |
168
|
|
|
return $this->getSimpleTestDouble('\GravitonDyn\ContractBundle\Document\Contract', $methods); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Provides a proxy instance of the OwnContextVoter. |
173
|
|
|
* |
174
|
|
|
* @param array $methods Set of methods to be doubled. |
175
|
|
|
* |
176
|
|
|
* @return object |
177
|
|
|
*/ |
178
|
|
|
private function getVoterProxy(array $methods = array()) |
179
|
|
|
{ |
180
|
|
|
$voter = $this->getProxyBuilder('\Graviton\SecurityBundle\Voter\OwnContextVoter') |
181
|
|
|
->setMethods($methods) |
182
|
|
|
->getProxy(); |
183
|
|
|
return $voter; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|