1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\Acl\Voter; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
6
|
|
|
|
7
|
|
|
use OroCRM\Bundle\MagentoBundle\Acl\Voter\CustomerVoter; |
8
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
9
|
|
|
|
10
|
|
|
class CustomerVoterTest extends AbstractTwoWaySyncVoterTest |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var CustomerVoter |
14
|
|
|
*/ |
15
|
|
|
protected $voter; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
$this->voter = new CustomerVoter($this->doctrineHelper); |
22
|
|
|
$this->voter->setSettingsProvider($this->settingsProvider); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function tearDown() |
26
|
|
|
{ |
27
|
|
|
unset($this->voter, $this->doctrineHelper); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param object $object |
32
|
|
|
* @param string $className |
33
|
|
|
* @param array $attributes |
34
|
|
|
* @param bool $hasApplicableChannels |
35
|
|
|
* @param bool $isChannelApplicable |
36
|
|
|
* @param bool $expected |
37
|
|
|
* |
38
|
|
|
* @dataProvider attributesDataProvider |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function testVote($object, $className, $attributes, $hasApplicableChannels, $isChannelApplicable, $expected) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$this->doctrineHelper->expects($this->any()) |
|
|
|
|
43
|
|
|
->method('getEntityClass') |
44
|
|
|
->with($object) |
45
|
|
|
->will($this->returnValue($className)); |
46
|
|
|
|
47
|
|
|
$this->voter->setClassName('OroCRM\Bundle\MagentoBundle\Entity\Customer'); |
48
|
|
|
|
49
|
|
|
$this->doctrineHelper->expects($this->any()) |
50
|
|
|
->method('getSingleEntityIdentifier') |
51
|
|
|
->with($object, false) |
52
|
|
|
->will($this->returnValue(1)); |
53
|
|
|
|
54
|
|
|
$this->settingsProvider->expects($this->any()) |
55
|
|
|
->method('isChannelApplicable') |
56
|
|
|
->will($this->returnValue($isChannelApplicable)); |
57
|
|
|
|
58
|
|
|
$this->settingsProvider->expects($this->any()) |
59
|
|
|
->method('hasApplicableChannels') |
60
|
|
|
->will($this->returnValue($hasApplicableChannels)); |
61
|
|
|
|
62
|
|
|
/** @var TokenInterface $token */ |
63
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
64
|
|
|
$this->assertEquals( |
65
|
|
|
$expected, |
66
|
|
|
$this->voter->vote($token, $object, $attributes) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
* |
73
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
74
|
|
|
*/ |
75
|
|
|
public function attributesDataProvider() |
76
|
|
|
{ |
77
|
|
|
$className = 'OroCRM\Bundle\MagentoBundle\Entity\Customer'; |
78
|
|
|
$objectIdentityClass = 'Symfony\Component\Security\Acl\Model\ObjectIdentityInterface'; |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
return [ |
82
|
|
|
// has not applicable channels |
83
|
|
|
[ |
84
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
85
|
|
|
$objectIdentityClass, ['VIEW'], |
86
|
|
|
false, |
87
|
|
|
false, |
88
|
|
|
CustomerVoter::ACCESS_ABSTAIN |
89
|
|
|
], |
90
|
|
|
[ |
91
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
92
|
|
|
$objectIdentityClass, |
93
|
|
|
['CREATE'], |
94
|
|
|
false, |
95
|
|
|
false, |
96
|
|
|
CustomerVoter::ACCESS_DENIED |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
100
|
|
|
$objectIdentityClass, |
101
|
|
|
['EDIT'], |
102
|
|
|
false, |
103
|
|
|
false, |
104
|
|
|
CustomerVoter::ACCESS_DENIED |
105
|
|
|
], |
106
|
|
|
[ |
107
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
108
|
|
|
$objectIdentityClass, |
109
|
|
|
['DELETE'], |
110
|
|
|
false, |
111
|
|
|
false, |
112
|
|
|
CustomerVoter::ACCESS_ABSTAIN |
113
|
|
|
], |
114
|
|
|
[ |
115
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
116
|
|
|
$objectIdentityClass, |
117
|
|
|
['ASSIGN'], |
118
|
|
|
false, |
119
|
|
|
false, |
120
|
|
|
CustomerVoter::ACCESS_ABSTAIN |
121
|
|
|
], |
122
|
|
|
// channel not applicable |
123
|
|
|
[$this->getCustomer(), $className, ['VIEW'], true, false, CustomerVoter::ACCESS_ABSTAIN], |
124
|
|
|
[$this->getCustomer(), $className, ['CREATE'], true, false, CustomerVoter::ACCESS_DENIED], |
125
|
|
|
[$this->getCustomer(), $className, ['EDIT'], true, false, CustomerVoter::ACCESS_DENIED], |
126
|
|
|
[$this->getCustomer(), $className, ['DELETE'], true, false, CustomerVoter::ACCESS_ABSTAIN], |
127
|
|
|
[$this->getCustomer(), $className, ['ASSIGN'], true, false, CustomerVoter::ACCESS_ABSTAIN], |
128
|
|
|
// applicable but without origin id |
129
|
|
|
[$this->getCustomer(), $className, ['VIEW'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
130
|
|
|
[$this->getCustomer(), $className, ['CREATE'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
131
|
|
|
[$this->getCustomer(), $className, ['EDIT'], true, true, CustomerVoter::ACCESS_DENIED], |
132
|
|
|
[$this->getCustomer(), $className, ['DELETE'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
133
|
|
|
[$this->getCustomer(), $className, ['ASSIGN'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
134
|
|
|
// applicable with origin id |
135
|
|
|
[$this->getCustomer(1), $className, ['VIEW'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
136
|
|
|
[$this->getCustomer(1), $className, ['CREATE'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
137
|
|
|
[$this->getCustomer(1), $className, ['EDIT'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
138
|
|
|
[$this->getCustomer(1), $className, ['DELETE'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
139
|
|
|
[$this->getCustomer(1), $className, ['ASSIGN'], true, true, CustomerVoter::ACCESS_ABSTAIN], |
140
|
|
|
// applicable but without channels |
141
|
|
|
[$this->getCustomer(1), $className, ['VIEW'], false, true, CustomerVoter::ACCESS_ABSTAIN], |
142
|
|
|
[$this->getCustomer(1), $className, ['CREATE'], false, true, CustomerVoter::ACCESS_DENIED], |
143
|
|
|
[$this->getCustomer(1), $className, ['EDIT'], false, true, CustomerVoter::ACCESS_DENIED], |
144
|
|
|
[$this->getCustomer(1), $className, ['DELETE'], false, true, CustomerVoter::ACCESS_ABSTAIN], |
145
|
|
|
[$this->getCustomer(1), $className, ['ASSIGN'], false, true, CustomerVoter::ACCESS_ABSTAIN] |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $objectIdentityClass |
151
|
|
|
* @param string $className |
152
|
|
|
* |
153
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Customer |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function getObjectIdentity($objectIdentityClass, $className) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$objectIdentity = $this->getMock($objectIdentityClass); |
158
|
|
|
$objectIdentity->expects($this->any()) |
159
|
|
|
->method('getType') |
160
|
|
|
->will($this->returnValue($className)); |
161
|
|
|
|
162
|
|
|
return $objectIdentity; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param int $originId |
167
|
|
|
* |
168
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Customer |
169
|
|
|
*/ |
170
|
|
|
protected function getCustomer($originId = null) |
171
|
|
|
{ |
172
|
|
|
$customer = $this->getMock('OroCRM\Bundle\MagentoBundle\Entity\Customer'); |
173
|
|
|
$channel = $this->getMock('Oro\Bundle\IntegrationBundle\Entity\Channel'); |
174
|
|
|
$customer->expects($this->any()) |
175
|
|
|
->method('getChannel') |
176
|
|
|
->will($this->returnValue($channel)); |
177
|
|
|
|
178
|
|
|
if ($originId) { |
|
|
|
|
179
|
|
|
$customer->expects($this->any()) |
180
|
|
|
->method('getOriginId') |
181
|
|
|
->will($this->returnValue($originId)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $customer; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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.