|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the MsalsasVotingBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Manolo Salsas |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Msalsas\VotingBundle\Tests\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
16
|
|
|
use Msalsas\VotingBundle\Entity\ReferenceVotes; |
|
17
|
|
|
use Msalsas\VotingBundle\Entity\VoteNegative; |
|
18
|
|
|
use Msalsas\VotingBundle\Entity\VotePositive; |
|
19
|
|
|
use Msalsas\VotingBundle\Service\Voter; |
|
20
|
|
|
use Msalsas\VotingBundle\Tests\Mock\AnonymousUserMock; |
|
21
|
|
|
use Msalsas\VotingBundle\Tests\Mock\UserMock; |
|
22
|
|
|
use Msalsas\VotingBundle\Tests\Mock\VoteNegativeMock; |
|
23
|
|
|
use Msalsas\VotingBundle\Tests\Mock\VotePositiveMock; |
|
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
25
|
|
|
use Symfony\Component\Translation\Translator; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
28
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
29
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
30
|
|
|
|
|
31
|
|
|
class VoterTest extends WebTestCase |
|
32
|
|
|
{ |
|
33
|
|
|
protected $emMock; |
|
34
|
|
|
protected $requestStackMock; |
|
35
|
|
|
protected $translator; |
|
36
|
|
|
protected $userMock; |
|
37
|
|
|
protected $tokenStorageMock; |
|
38
|
|
|
|
|
39
|
|
|
public function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
parent::setUp(); |
|
42
|
|
|
$this->setDefaultMocks(); |
|
43
|
|
|
$this->translator = new Translator('en'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testAnonymousCannotVoteNegative() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->setAnonymousUserMocks(); |
|
49
|
|
|
|
|
50
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertSame(false, $voter->userCanVoteNegative(1)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testUserCanVoteNegative() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->setUserMocks(); |
|
58
|
|
|
|
|
59
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertSame(true, $voter->userCanVoteNegative(1)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testUserCannotVoteNegativeVotedPositive() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->setUserMocks(); |
|
67
|
|
|
$votePositiveMock = $this->getVotePositiveMock($this->userMock); |
|
68
|
|
|
$emRepositoryMock = $this->getRepositoryMock($votePositiveMock); |
|
69
|
|
|
|
|
70
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
71
|
|
|
$this->emMock->method('getRepository')->willReturn($emRepositoryMock); |
|
72
|
|
|
|
|
73
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertSame(false, $voter->userCanVoteNegative(1)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testUserCannotVoteNegativeVotedNegative() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->setUserMocks(); |
|
81
|
|
|
$positiveVoteEmRepositoryMock = $this->getRepositoryMock(null); |
|
82
|
|
|
$voteNegativeMock = $this->getVoteNegativeMock($this->userMock); |
|
83
|
|
|
$negativeVoteEmRepositoryMock = $this->getRepositoryMock($voteNegativeMock); |
|
84
|
|
|
|
|
85
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
86
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class]) |
|
87
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock); |
|
88
|
|
|
|
|
89
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertSame(false, $voter->userCanVoteNegative(1)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testGetVotes() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->setUserMocks(); |
|
97
|
|
|
|
|
98
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertSame(0, $voter->getUserPositiveVotes(1)); |
|
101
|
|
|
$this->assertSame(0, $voter->getPositiveVotes(1)); |
|
102
|
|
|
$this->assertSame(0, $voter->getNegativeVotes(1)); |
|
103
|
|
|
$this->assertSame(0, $voter->getAnonymousVotes(1)); |
|
104
|
|
|
$this->assertSame(false, $voter->getUserVote(1)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testGetVotesWithExistingReference() |
|
108
|
|
|
{ |
|
109
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
110
|
|
|
->getMock(); |
|
111
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
112
|
|
|
$referenceVotesMock->method('isPublished')->willReturn(true); |
|
113
|
|
|
$referenceVotesMock->method('getPositiveVotes')->willReturn(11); |
|
114
|
|
|
$referenceVotesMock->method('getUserVotes')->willReturn(6); |
|
115
|
|
|
$referenceVotesMock->method('getNegativeVotes')->willReturn(3); |
|
116
|
|
|
$referenceVotesMock->method('getAnonymousVotes')->willReturn(5); |
|
117
|
|
|
$referenceVotesMock->method('isPublished')->willReturn(true); |
|
118
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
119
|
|
|
$emRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
120
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
121
|
|
|
$this->emMock->method('getRepository')->willReturn($emRepositoryMock); |
|
122
|
|
|
|
|
123
|
|
|
$this->setUserMocks(); |
|
124
|
|
|
|
|
125
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertSame($referenceVotesMock, $voter->getUserVote(1)); |
|
128
|
|
|
$this->assertSame(6, $voter->getUserPositiveVotes(1)); |
|
129
|
|
|
$this->assertSame(5, $voter->getAnonymousVotes(1)); |
|
130
|
|
|
$this->assertSame(11, $voter->getPositiveVotes(1)); |
|
131
|
|
|
$this->assertSame(3, $voter->getNegativeVotes(1)); |
|
132
|
|
|
$this->assertSame(['irrelevant'], $voter->getNegativeReasons()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testGetUserVoteWhenNegative() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->setUserMocks(); |
|
138
|
|
|
$positiveVoteEmRepositoryMock = $this->getRepositoryMock(null); |
|
139
|
|
|
$voteNegativeMock = $this->getVoteNegativeMock($this->userMock); |
|
140
|
|
|
$negativeVoteEmRepositoryMock = $this->getRepositoryMock($voteNegativeMock); |
|
141
|
|
|
|
|
142
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
143
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class]) |
|
144
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock); |
|
145
|
|
|
|
|
146
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertSame($voteNegativeMock, $voter->getUserVote(1)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testGetAnonymousVoteWithVoteByIP() |
|
152
|
|
|
{ |
|
153
|
|
|
$this->setAnonymousUserMocks(); |
|
154
|
|
|
$votePositiveMock = $this->getVotePositiveMock($this->userMock); |
|
155
|
|
|
$positiveVoteEmRepositoryMock = $this->getRepositoryMock($votePositiveMock); |
|
156
|
|
|
$negativeVoteEmRepositoryMock = $this->getRepositoryMock(null); |
|
157
|
|
|
|
|
158
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
159
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class]) |
|
160
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock); |
|
161
|
|
|
|
|
162
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertSame($votePositiveMock, $voter->getUserVote(1)); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testVotePositiveWithUser() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->setUserMocks(); |
|
170
|
|
|
|
|
171
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertSame(array('irrelevant'), $voter->getNegativeReasons()); |
|
174
|
|
|
$this->assertSame(0, $voter->getPositiveVotes(1)); |
|
175
|
|
|
|
|
176
|
|
|
$votes = $voter->votePositive(1); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertSame(1, $votes); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function testVotePositiveWithUserAndExistingReference() |
|
182
|
|
|
{ |
|
183
|
|
|
$this->setUserMocks(); |
|
184
|
|
|
|
|
185
|
|
|
$positiveVoteEmRepositoryMock = $this->getRepositoryMock(null); |
|
186
|
|
|
$negativeVoteEmRepositoryMock = $this->getRepositoryMock(null); |
|
187
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
188
|
|
|
->getMock(); |
|
189
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
190
|
|
|
$referenceVotesMock->method('isPublished')->willReturn(true); |
|
191
|
|
|
$referenceVotesMock->method('getPositiveVotes')->willReturn(11); |
|
192
|
|
|
$referenceVotesMock->method('getUserVotes')->willReturn(6); |
|
193
|
|
|
$referenceVotesMock->method('getNegativeVotes')->willReturn(3); |
|
194
|
|
|
$referenceVotesMock->method('getAnonymousVotes')->willReturn(5); |
|
195
|
|
|
$referenceVotesMock->method('isPublished')->willReturn(true); |
|
196
|
|
|
$referenceVotesEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
197
|
|
|
$referenceVotesEmRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
198
|
|
|
|
|
199
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
200
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class], [ReferenceVotes::class]) |
|
201
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock, $referenceVotesEmRepositoryMock); |
|
202
|
|
|
|
|
203
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
204
|
|
|
|
|
205
|
|
|
$votes = $voter->votePositive(1); |
|
206
|
|
|
|
|
207
|
|
|
$this->assertSame(11, $votes); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function testVoteNegativeWithUserAndExistingReference() |
|
211
|
|
|
{ |
|
212
|
|
|
$this->setUserMocks(); |
|
213
|
|
|
|
|
214
|
|
|
$positiveVoteEmRepositoryMock = $this->getRepositoryMock(null); |
|
215
|
|
|
$negativeVoteEmRepositoryMock = $this->getRepositoryMock(null); |
|
216
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
217
|
|
|
->getMock(); |
|
218
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
219
|
|
|
$referenceVotesMock->method('isPublished')->willReturn(true); |
|
220
|
|
|
$referenceVotesMock->method('getPositiveVotes')->willReturn(11); |
|
221
|
|
|
$referenceVotesMock->method('getUserVotes')->willReturn(6); |
|
222
|
|
|
$referenceVotesMock->method('getNegativeVotes')->willReturn(3); |
|
223
|
|
|
$referenceVotesMock->method('getAnonymousVotes')->willReturn(5); |
|
224
|
|
|
$referenceVotesMock->method('isPublished')->willReturn(true); |
|
225
|
|
|
$referenceVotesEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
226
|
|
|
$referenceVotesEmRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
227
|
|
|
|
|
228
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
229
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class], [ReferenceVotes::class]) |
|
230
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock, $referenceVotesEmRepositoryMock); |
|
231
|
|
|
|
|
232
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
233
|
|
|
|
|
234
|
|
|
$votes = $voter->voteNegative(1, 'irrelevant'); |
|
235
|
|
|
|
|
236
|
|
|
$this->assertSame(3, $votes); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function testVoteNegativeWithUser() |
|
240
|
|
|
{ |
|
241
|
|
|
$referenceVotes = new ReferenceVotes(); |
|
242
|
|
|
$referenceVotes->setReference(1); |
|
243
|
|
|
|
|
244
|
|
|
$this->setUserMocks(); |
|
245
|
|
|
|
|
246
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
247
|
|
|
|
|
248
|
|
|
$this->assertSame(array('irrelevant'), $voter->getNegativeReasons()); |
|
249
|
|
|
$this->assertSame(0, $voter->getNegativeVotes($referenceVotes->getReference())); |
|
250
|
|
|
|
|
251
|
|
|
$votes = $voter->voteNegative(1, 'irrelevant'); |
|
252
|
|
|
|
|
253
|
|
|
$this->assertSame(1, $votes); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @expectedException \Symfony\Component\Finder\Exception\AccessDeniedException |
|
258
|
|
|
*/ |
|
259
|
|
|
public function testVoteNegativeWithUserWithoutReason() |
|
260
|
|
|
{ |
|
261
|
|
|
$this->setUserMocks(); |
|
262
|
|
|
|
|
263
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
264
|
|
|
|
|
265
|
|
|
$this->assertSame(array('irrelevant'), $voter->getNegativeReasons()); |
|
266
|
|
|
$this->assertSame(0, $voter->getNegativeVotes(1)); |
|
267
|
|
|
|
|
268
|
|
|
$voter->voteNegative(1, ''); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function testVotePositiveWithAnonymous() |
|
272
|
|
|
{ |
|
273
|
|
|
$this->setAnonymousUserMocks(); |
|
274
|
|
|
|
|
275
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
276
|
|
|
|
|
277
|
|
|
$this->assertSame(array('irrelevant'), $voter->getNegativeReasons()); |
|
278
|
|
|
$this->assertSame(0, $voter->getPositiveVotes(1)); |
|
279
|
|
|
|
|
280
|
|
|
$votes = $voter->votePositive(1); |
|
281
|
|
|
|
|
282
|
|
|
$this->assertSame(1, $votes); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
public function testVoteWithAnonymousOneAllowed() |
|
286
|
|
|
{ |
|
287
|
|
|
$this->setAnonymousUserMocks(); |
|
288
|
|
|
|
|
289
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
290
|
|
|
->getMock(); |
|
291
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
292
|
|
|
$referenceVotesMock->method('getPositiveVotes')->willReturn(10); |
|
293
|
|
|
|
|
294
|
|
|
$positiveVotesMock = $this->getMockBuilder(VotePositive::class) |
|
295
|
|
|
->getMock(); |
|
296
|
|
|
$positiveVotesMock->method('getReference')->willReturn(1); |
|
297
|
|
|
|
|
298
|
|
|
$negativeVotesMock = $this->getMockBuilder(VoteNegative::class) |
|
299
|
|
|
->getMock(); |
|
300
|
|
|
$negativeVotesMock->method('getReference')->willReturn(1); |
|
301
|
|
|
|
|
302
|
|
|
$positiveVoteEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
303
|
|
|
$negativeVoteEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
304
|
|
|
$referenceVotesEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
305
|
|
|
$positiveVoteEmRepositoryMock->expects($this->exactly(1))->method('findOneBy')->willReturn(null); |
|
306
|
|
|
$negativeVoteEmRepositoryMock->method('findOneBy')->willReturn($negativeVotesMock); |
|
307
|
|
|
$referenceVotesEmRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
308
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
309
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class], [ReferenceVotes::class], [ReferenceVotes::class]) |
|
310
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock, $referenceVotesEmRepositoryMock, $referenceVotesEmRepositoryMock); |
|
311
|
|
|
|
|
312
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 1, array('irrelevant')); |
|
313
|
|
|
|
|
314
|
|
|
$votes = $voter->votePositive(1); |
|
315
|
|
|
|
|
316
|
|
|
$this->assertSame(10, $votes); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
public function testVoteWithAnonymousTenPercentAllowed() |
|
320
|
|
|
{ |
|
321
|
|
|
$this->setAnonymousUserMocks(); |
|
322
|
|
|
|
|
323
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
324
|
|
|
->getMock(); |
|
325
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
326
|
|
|
$referenceVotesMock->method('getAnonymousVotes')->willReturn(1); |
|
327
|
|
|
$referenceVotesMock->method('getUserVotes')->willReturn(100); |
|
328
|
|
|
|
|
329
|
|
|
$positiveVotesMock = $this->getMockBuilder(VotePositive::class) |
|
330
|
|
|
->getMock(); |
|
331
|
|
|
$positiveVotesMock->method('getReference')->willReturn(1); |
|
332
|
|
|
|
|
333
|
|
|
$negativeVotesMock = $this->getMockBuilder(VoteNegative::class) |
|
334
|
|
|
->getMock(); |
|
335
|
|
|
$negativeVotesMock->method('getReference')->willReturn(1); |
|
336
|
|
|
|
|
337
|
|
|
$positiveVoteEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
338
|
|
|
$negativeVoteEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
339
|
|
|
$referenceVotesEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
340
|
|
|
$positiveVoteEmRepositoryMock->expects($this->exactly(1))->method('findOneBy')->willReturn(null); |
|
341
|
|
|
$negativeVoteEmRepositoryMock->method('findOneBy')->willReturn($negativeVotesMock); |
|
342
|
|
|
$referenceVotesEmRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
343
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
344
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class], [ReferenceVotes::class], [ReferenceVotes::class]) |
|
345
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock, $referenceVotesEmRepositoryMock, $referenceVotesEmRepositoryMock); |
|
346
|
|
|
|
|
347
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 10, 0, array('irrelevant')); |
|
348
|
|
|
|
|
349
|
|
|
$voter->votePositive(1); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* @expectedException \Symfony\Component\Finder\Exception\AccessDeniedException |
|
354
|
|
|
*/ |
|
355
|
|
|
public function testVoteWithAnonymousNotAllowed() |
|
356
|
|
|
{ |
|
357
|
|
|
$this->setAnonymousUserMocks(); |
|
358
|
|
|
|
|
359
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
360
|
|
|
->getMock(); |
|
361
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
362
|
|
|
$referenceVotesMock->method('getAnonymousVotes')->willReturn(1); |
|
363
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
364
|
|
|
$emRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
365
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
366
|
|
|
$this->emMock->method('getRepository')->willReturn($emRepositoryMock); |
|
367
|
|
|
|
|
368
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 0, array('irrelevant')); |
|
369
|
|
|
|
|
370
|
|
|
$this->assertSame(array('irrelevant'), $voter->getNegativeReasons()); |
|
371
|
|
|
$this->assertSame(0, $voter->getPositiveVotes(1)); |
|
372
|
|
|
|
|
373
|
|
|
$voter->votePositive(1); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* @expectedException \Symfony\Component\Finder\Exception\AccessDeniedException |
|
378
|
|
|
*/ |
|
379
|
|
|
public function testVotePositiveWithAnonymousPercentToZero() |
|
380
|
|
|
{ |
|
381
|
|
|
$this->setAnonymousUserMocks(); |
|
382
|
|
|
|
|
383
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
384
|
|
|
->getMock(); |
|
385
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
386
|
|
|
$referenceVotesMock->method('getAnonymousVotes')->willReturn(10); |
|
387
|
|
|
$referenceVotesMock->method('getUserVotes')->willReturn(1); |
|
388
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
389
|
|
|
$emRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
390
|
|
|
|
|
391
|
|
|
$positiveVotesMock = $this->getMockBuilder(VotePositive::class) |
|
392
|
|
|
->getMock(); |
|
393
|
|
|
$positiveVotesMock->method('getReference')->willReturn(1); |
|
394
|
|
|
|
|
395
|
|
|
$negativeVotesMock = $this->getMockBuilder(VoteNegative::class) |
|
396
|
|
|
->getMock(); |
|
397
|
|
|
$negativeVotesMock->method('getReference')->willReturn(1); |
|
398
|
|
|
|
|
399
|
|
|
$positiveVoteEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
400
|
|
|
$negativeVoteEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
401
|
|
|
$referenceVotesEmRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
402
|
|
|
$positiveVoteEmRepositoryMock->expects($this->exactly(1))->method('findOneBy')->willReturn(null); |
|
403
|
|
|
$negativeVoteEmRepositoryMock->method('findOneBy')->willReturn($negativeVotesMock); |
|
404
|
|
|
$referenceVotesEmRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
405
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
406
|
|
|
$this->emMock->method('getRepository')->withConsecutive([VotePositive::class], [VoteNegative::class], [ReferenceVotes::class]) |
|
407
|
|
|
->willReturnOnConsecutiveCalls($positiveVoteEmRepositoryMock, $negativeVoteEmRepositoryMock, $referenceVotesEmRepositoryMock); |
|
408
|
|
|
|
|
409
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 0, 0, array('irrelevant')); |
|
410
|
|
|
|
|
411
|
|
|
$voter->votePositive(1); |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* @expectedException \Symfony\Component\Finder\Exception\AccessDeniedException |
|
416
|
|
|
*/ |
|
417
|
|
|
public function testVoteNegativeWithAnonymous() |
|
418
|
|
|
{ |
|
419
|
|
|
$referenceVotes = new ReferenceVotes(); |
|
420
|
|
|
$referenceVotes->setReference(1); |
|
421
|
|
|
|
|
422
|
|
|
$this->setAnonymousUserMocks(); |
|
423
|
|
|
|
|
424
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
425
|
|
|
|
|
426
|
|
|
$this->assertSame(array('irrelevant'), $voter->getNegativeReasons()); |
|
427
|
|
|
$this->assertSame(0, $voter->getNegativeVotes($referenceVotes->getReference())); |
|
428
|
|
|
|
|
429
|
|
|
$voter->voteNegative(1, 'irrelevant'); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
/** |
|
433
|
|
|
* @expectedException \Symfony\Component\Finder\Exception\AccessDeniedException |
|
434
|
|
|
*/ |
|
435
|
|
|
public function testVotePositiveWithAnonymousAndNoIp() |
|
436
|
|
|
{ |
|
437
|
|
|
$referenceVotes = new ReferenceVotes(); |
|
438
|
|
|
$referenceVotes->setReference(1); |
|
439
|
|
|
|
|
440
|
|
|
$requestMock = $this->getMockBuilder(Request::class) |
|
441
|
|
|
->disableOriginalConstructor() |
|
442
|
|
|
->setMethods(array('getClientIp')) |
|
443
|
|
|
->getMock(); |
|
444
|
|
|
$requestMock->expects($this->any()) |
|
445
|
|
|
->method('getClientIp')->willReturn(null); |
|
446
|
|
|
|
|
447
|
|
|
$this->requestStackMock = $this->getMockBuilder(RequestStack::class) |
|
448
|
|
|
->disableOriginalConstructor() |
|
449
|
|
|
->setMethods(array('getCurrentRequest')) |
|
450
|
|
|
->getMock(); |
|
451
|
|
|
$this->requestStackMock->expects($this->any()) |
|
452
|
|
|
->method('getCurrentRequest')->willReturn($requestMock); |
|
453
|
|
|
|
|
454
|
|
|
$this->setAnonymousUserMocks(); |
|
455
|
|
|
|
|
456
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
457
|
|
|
|
|
458
|
|
|
$this->assertSame(array('irrelevant'), $voter->getNegativeReasons()); |
|
459
|
|
|
$this->assertSame(0, $voter->getPositiveVotes($referenceVotes->getReference())); |
|
460
|
|
|
|
|
461
|
|
|
$voter->votePositive($referenceVotes->getReference()); |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
public function testPublishReference() |
|
465
|
|
|
{ |
|
466
|
|
|
$referenceVotesMock = $this->getMockBuilder(ReferenceVotes::class) |
|
467
|
|
|
->getMock(); |
|
468
|
|
|
$referenceVotesMock->method('getReference')->willReturn(1); |
|
469
|
|
|
$referenceVotesMock->method('isPublished')->willReturn(true); |
|
470
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
471
|
|
|
$emRepositoryMock->method('findOneBy')->willReturn($referenceVotesMock); |
|
472
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
473
|
|
|
$this->emMock->method('getRepository')->willReturn($emRepositoryMock); |
|
474
|
|
|
|
|
475
|
|
|
$this->setUserMocks(); |
|
476
|
|
|
|
|
477
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
478
|
|
|
|
|
479
|
|
|
$voter->setPublished(1); |
|
480
|
|
|
$this->assertSame(true, $voter->isPublished(1)); |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
/** |
|
484
|
|
|
* @expectedException \Exception |
|
485
|
|
|
*/ |
|
486
|
|
|
public function testPublishNoReference() |
|
487
|
|
|
{ |
|
488
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
489
|
|
|
$emRepositoryMock->method('findOneBy')->willReturn(null); |
|
490
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
491
|
|
|
$this->emMock->method('getRepository')->willReturn($emRepositoryMock); |
|
492
|
|
|
|
|
493
|
|
|
$this->setUserMocks(); |
|
494
|
|
|
|
|
495
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
496
|
|
|
|
|
497
|
|
|
$voter->setPublished(1); |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
public function testIsPublishNoReference() |
|
501
|
|
|
{ |
|
502
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
503
|
|
|
$emRepositoryMock->method('findOneBy')->willReturn(null); |
|
504
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
505
|
|
|
$this->emMock->method('getRepository')->willReturn($emRepositoryMock); |
|
506
|
|
|
|
|
507
|
|
|
$this->setUserMocks(); |
|
508
|
|
|
|
|
509
|
|
|
$voter = new Voter($this->emMock, $this->tokenStorageMock, $this->requestStackMock, $this->translator, 50, 2, array('irrelevant')); |
|
510
|
|
|
|
|
511
|
|
|
$this->assertSame(false, $voter->isPublished(1)); |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
private function setDefaultMocks() |
|
515
|
|
|
{ |
|
516
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
517
|
|
|
$this->emMock = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
518
|
|
|
$this->emMock->method('getRepository')->willReturn($emRepositoryMock); |
|
519
|
|
|
|
|
520
|
|
|
$requestMock = $this->getMockBuilder(Request::class) |
|
521
|
|
|
->disableOriginalConstructor() |
|
522
|
|
|
->setMethods(array('getClientIp')) |
|
523
|
|
|
->getMock(); |
|
524
|
|
|
$requestMock->expects($this->any()) |
|
525
|
|
|
->method('getClientIp')->willReturn('127.0.0.1'); |
|
526
|
|
|
|
|
527
|
|
|
$this->requestStackMock = $this->getMockBuilder(RequestStack::class) |
|
528
|
|
|
->disableOriginalConstructor() |
|
529
|
|
|
->setMethods(array('getCurrentRequest')) |
|
530
|
|
|
->getMock(); |
|
531
|
|
|
$this->requestStackMock->expects($this->any()) |
|
532
|
|
|
->method('getCurrentRequest')->willReturn($requestMock); |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
private function setUserMocks() |
|
536
|
|
|
{ |
|
537
|
|
|
$this->userMock = $this->getMockBuilder(UserMock::class) |
|
538
|
|
|
->getMock(); |
|
539
|
|
|
|
|
540
|
|
|
$tokenMock = $this->getMockBuilder(TokenInterface::class) |
|
541
|
|
|
->disableOriginalConstructor() |
|
542
|
|
|
->getMock(); |
|
543
|
|
|
$tokenMock->method('getUser')->willReturn($this->userMock); |
|
544
|
|
|
|
|
545
|
|
|
$this->tokenStorageMock = $this->getMockBuilder(TokenStorageInterface::class) |
|
546
|
|
|
->disableOriginalConstructor() |
|
547
|
|
|
->getMock(); |
|
548
|
|
|
$this->tokenStorageMock->method('getToken')->willReturn($tokenMock); |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
private function setAnonymousUserMocks() |
|
552
|
|
|
{ |
|
553
|
|
|
$this->userMock = $this->getMockBuilder(AnonymousUserMock::class) |
|
554
|
|
|
->getMock(); |
|
555
|
|
|
|
|
556
|
|
|
$tokenMock = $this->getMockBuilder(TokenInterface::class) |
|
557
|
|
|
->disableOriginalConstructor() |
|
558
|
|
|
->getMock(); |
|
559
|
|
|
$tokenMock->method('getUser')->willReturn($this->userMock); |
|
560
|
|
|
|
|
561
|
|
|
$this->tokenStorageMock = $this->getMockBuilder(TokenStorageInterface::class) |
|
562
|
|
|
->disableOriginalConstructor() |
|
563
|
|
|
->getMock(); |
|
564
|
|
|
$this->tokenStorageMock->method('getToken')->willReturn($tokenMock); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
private function getRepositoryMock($classMock) |
|
568
|
|
|
{ |
|
569
|
|
|
$emRepositoryMock = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
|
570
|
|
|
$emRepositoryMock->method('findOneBy')->willReturn($classMock); |
|
571
|
|
|
|
|
572
|
|
|
return $emRepositoryMock; |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
private function getVotePositiveMock($user, $userIP = '127.0.0.1') |
|
576
|
|
|
{ |
|
577
|
|
|
return $this->getMockBuilder(VotePositiveMock::class) |
|
578
|
|
|
->setConstructorArgs(array($user, $userIP)) |
|
579
|
|
|
->getMock(); |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
private function getVoteNegativeMock($user, $reason = 'irrelevant', $userIP = '127.0.0.1') |
|
583
|
|
|
{ |
|
584
|
|
|
return $this->getMockBuilder(VoteNegativeMock::class) |
|
585
|
|
|
->setConstructorArgs(array($user, $reason, $userIP)) |
|
586
|
|
|
->getMock(); |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
} |