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\Entity\NewsletterSubscriber; |
8
|
|
|
use OroCRM\Bundle\MagentoBundle\Acl\Voter\NewsletterSubscriberVoter; |
9
|
|
|
|
10
|
|
|
class NewsletterSubscriberVoterTest extends AbstractTwoWaySyncVoterTest |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var NewsletterSubscriberVoter |
14
|
|
|
*/ |
15
|
|
|
protected $voter; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
$this->voter = new NewsletterSubscriberVoter($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\NewsletterSubscriber'); |
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\NewsletterSubscriber'; |
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, |
86
|
|
|
['VIEW'], |
87
|
|
|
false, |
88
|
|
|
false, |
89
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN, |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
93
|
|
|
$objectIdentityClass, |
94
|
|
|
['CREATE'], |
95
|
|
|
false, |
96
|
|
|
false, |
97
|
|
|
NewsletterSubscriberVoter::ACCESS_DENIED, |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
101
|
|
|
$objectIdentityClass, |
102
|
|
|
['EDIT'], |
103
|
|
|
false, |
104
|
|
|
false, |
105
|
|
|
NewsletterSubscriberVoter::ACCESS_DENIED, |
106
|
|
|
], |
107
|
|
|
[ |
108
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
109
|
|
|
$objectIdentityClass, |
110
|
|
|
['DELETE'], |
111
|
|
|
false, |
112
|
|
|
false, |
113
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN |
114
|
|
|
], |
115
|
|
|
[ |
116
|
|
|
$this->getObjectIdentity($objectIdentityClass, $className), |
117
|
|
|
$objectIdentityClass, |
118
|
|
|
['ASSIGN'], |
119
|
|
|
false, |
120
|
|
|
false, |
121
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN |
122
|
|
|
], |
123
|
|
|
// channel not applicable |
124
|
|
|
[$this->getSubscriber(), $className, ['VIEW'], true, false, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
125
|
|
|
[$this->getSubscriber(), $className, ['CREATE'], true, false, NewsletterSubscriberVoter::ACCESS_DENIED], |
126
|
|
|
[$this->getSubscriber(), $className, ['EDIT'], true, false, NewsletterSubscriberVoter::ACCESS_DENIED], |
127
|
|
|
[$this->getSubscriber(), $className, ['DELETE'], true, false, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
128
|
|
|
[$this->getSubscriber(), $className, ['ASSIGN'], true, false, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
129
|
|
|
// without customer |
130
|
|
|
[$this->getSubscriber(), $className, ['VIEW'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
131
|
|
|
[$this->getSubscriber(), $className, ['CREATE'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
132
|
|
|
[$this->getSubscriber(), $className, ['EDIT'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
133
|
|
|
[$this->getSubscriber(), $className, ['DELETE'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
134
|
|
|
[$this->getSubscriber(), $className, ['ASSIGN'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
135
|
|
|
// with customer and without origin id |
136
|
|
|
[$this->getSubscriber(true), $className, ['VIEW'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
137
|
|
|
[$this->getSubscriber(true), $className, ['CREATE'], true, true, NewsletterSubscriberVoter::ACCESS_DENIED], |
138
|
|
|
[$this->getSubscriber(true), $className, ['EDIT'], true, true, NewsletterSubscriberVoter::ACCESS_DENIED], |
139
|
|
|
[$this->getSubscriber(true), $className, ['DELETE'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
140
|
|
|
[$this->getSubscriber(true), $className, ['ASSIGN'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
141
|
|
|
// applicable with origin id |
142
|
|
|
[ |
143
|
|
|
$this->getSubscriber(true, 1), |
144
|
|
|
$className, |
145
|
|
|
['VIEW'], |
146
|
|
|
true, |
147
|
|
|
true, |
148
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN |
149
|
|
|
], |
150
|
|
|
[ |
151
|
|
|
$this->getSubscriber(true, 1), |
152
|
|
|
$className, |
153
|
|
|
['CREATE'], |
154
|
|
|
true, |
155
|
|
|
true, |
156
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN |
157
|
|
|
], |
158
|
|
|
[ |
159
|
|
|
$this->getSubscriber(true, 1), |
160
|
|
|
$className, |
161
|
|
|
['EDIT'], |
162
|
|
|
true, |
163
|
|
|
true, |
164
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN |
165
|
|
|
], |
166
|
|
|
[ |
167
|
|
|
$this->getSubscriber(true, 1), |
168
|
|
|
$className, |
169
|
|
|
['DELETE'], |
170
|
|
|
true, |
171
|
|
|
true, |
172
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN |
173
|
|
|
], |
174
|
|
|
[ |
175
|
|
|
$this->getSubscriber(true, 1), |
176
|
|
|
$className, |
177
|
|
|
['ASSIGN'], |
178
|
|
|
true, |
179
|
|
|
true, |
180
|
|
|
NewsletterSubscriberVoter::ACCESS_ABSTAIN |
181
|
|
|
], |
182
|
|
|
// applicable but without channels |
183
|
|
|
[$this->getSubscriber(1), $className, ['VIEW'], false, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
|
|
|
|
184
|
|
|
[$this->getSubscriber(1), $className, ['CREATE'], false, true, NewsletterSubscriberVoter::ACCESS_DENIED], |
|
|
|
|
185
|
|
|
[$this->getSubscriber(1), $className, ['EDIT'], false, true, NewsletterSubscriberVoter::ACCESS_DENIED], |
|
|
|
|
186
|
|
|
[$this->getSubscriber(1), $className, ['DELETE'], false, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN], |
|
|
|
|
187
|
|
|
[$this->getSubscriber(1), $className, ['ASSIGN'], false, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN] |
|
|
|
|
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $objectIdentityClass |
193
|
|
|
* @param string $className |
194
|
|
|
* |
195
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|NewsletterSubscriber |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function getObjectIdentity($objectIdentityClass, $className) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$objectIdentity = $this->getMock($objectIdentityClass); |
200
|
|
|
$objectIdentity->expects($this->any()) |
201
|
|
|
->method('getType') |
202
|
|
|
->will($this->returnValue($className)); |
203
|
|
|
|
204
|
|
|
return $objectIdentity; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param bool $hasCustomer |
209
|
|
|
* @param int $customerOriginId |
210
|
|
|
* |
211
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|NewsletterSubscriber |
212
|
|
|
*/ |
213
|
|
|
protected function getSubscriber($hasCustomer = false, $customerOriginId = null) |
214
|
|
|
{ |
215
|
|
|
$newsletterSubscriber = $this->getMock('OroCRM\Bundle\MagentoBundle\Entity\NewsletterSubscriber'); |
216
|
|
|
|
217
|
|
|
$channel = $this->getMock('Oro\Bundle\IntegrationBundle\Entity\Channel'); |
218
|
|
|
$newsletterSubscriber->expects($this->any()) |
219
|
|
|
->method('getChannel') |
220
|
|
|
->will($this->returnValue($channel)); |
221
|
|
|
|
222
|
|
|
if ($hasCustomer) { |
223
|
|
|
$customer = $this->getMock('OroCRM\Bundle\MagentoBundle\Entity\Customer'); |
224
|
|
|
|
225
|
|
|
$newsletterSubscriber->expects($this->any()) |
226
|
|
|
->method('getCustomer') |
227
|
|
|
->will($this->returnValue($customer)); |
228
|
|
|
|
229
|
|
|
if ($customerOriginId) { |
|
|
|
|
230
|
|
|
$customer->expects($this->once()) |
231
|
|
|
->method('getOriginId') |
232
|
|
|
->will($this->returnValue($customerOriginId)); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $newsletterSubscriber; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
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.