1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\TestBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Overwatch\TestBundle\DataFixtures\ORM\TestGroupFixtures; |
6
|
|
|
use Overwatch\TestBundle\Entity\TestGroup; |
7
|
|
|
use Overwatch\UserBundle\DataFixtures\ORM\UserFixtures; |
8
|
|
|
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* TestGroupApiControllerTest |
13
|
|
|
* Functional test of API methods provided by the APIController |
14
|
|
|
*/ |
15
|
|
|
class TestGroupApiControllerTest extends DatabaseAwareTestCase |
16
|
|
|
{ |
17
|
|
|
public function testCreateGroup() |
18
|
|
|
{ |
19
|
|
|
$newGroupName = 'New Group 1'; |
20
|
|
|
|
21
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
22
|
|
|
$this->makeJsonRequest( |
23
|
|
|
'POST', |
24
|
|
|
'/api/groups', |
25
|
|
|
[ |
26
|
|
|
'name' => $newGroupName, |
27
|
|
|
] |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$group = $this->em->getRepository("Overwatch\TestBundle\Entity\TestGroup")->findOneBy([ |
|
|
|
|
31
|
|
|
'name' => $newGroupName |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->assertNotNull($group); |
35
|
|
|
|
36
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
37
|
|
|
$this->assertEquals($newGroupName, $this->getResponseContent()->name); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testCreateGroupInsufficentPerms() |
41
|
|
|
{ |
42
|
|
|
$newGroupName = 'New Group 2'; |
43
|
|
|
|
44
|
|
|
$this->logIn('ROLE_ADMIN'); |
45
|
|
|
$this->makeJsonRequest( |
46
|
|
|
'POST', |
47
|
|
|
'/api/groups', |
48
|
|
|
[ |
49
|
|
|
'name' => $newGroupName, |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$group = $this->em->getRepository("Overwatch\TestBundle\Entity\TestGroup")->findOneBy([ |
|
|
|
|
54
|
|
|
'name' => $newGroupName |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$this->assertNull($group); |
58
|
|
|
$this->assertForbidden($this->client->getResponse()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testCreateGroupNoName() |
62
|
|
|
{ |
63
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
64
|
|
|
$this->makeJsonRequest( |
65
|
|
|
'POST', |
66
|
|
|
'/api/groups', |
67
|
|
|
[] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$group = $this->em->getRepository("Overwatch\TestBundle\Entity\TestGroup")->findOneBy([ |
|
|
|
|
71
|
|
|
'name' => null |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
$this->assertNull($group); |
75
|
|
|
$this->assertEquals(422, $this->client->getResponse()->getStatusCode()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testGetAllGroups() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
81
|
|
|
$this->client->request('GET', '/api/groups'); |
82
|
|
|
|
83
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
84
|
|
|
$this->assertCount(3, $this->getResponseContent()); |
85
|
|
|
$this->assertCollectionContainsObject( |
86
|
|
|
$this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-1']->getId()), |
|
|
|
|
87
|
|
|
$this->getResponseContent() |
88
|
|
|
); |
89
|
|
|
$this->assertCollectionContainsObject( |
90
|
|
|
$this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-2']->getId()), |
|
|
|
|
91
|
|
|
$this->getResponseContent() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testGetAllGroupsAsUser() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$this->loginAs( |
98
|
|
|
$this->em->find("Overwatch\UserBundle\Entity\User", UserFixtures::$users['user-2']->getId()), |
|
|
|
|
99
|
|
|
'overwatch' |
100
|
|
|
); |
101
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must use a new client |
102
|
|
|
$this->client->request('GET', '/api/groups'); |
103
|
|
|
|
104
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
105
|
|
|
$this->assertCount(1, $this->getResponseContent()); |
106
|
|
|
$this->assertCollectionContainsObject( |
107
|
|
|
$this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-1']->getId()), |
|
|
|
|
108
|
|
|
$this->getResponseContent() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function testGetGroup() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
115
|
|
|
$this->client->request('GET', '/api/groups/' . TestGroupFixtures::$groups['group-1']->getId()); |
116
|
|
|
|
117
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
118
|
|
|
$this->assertJsonStringEqualsJsonString( |
119
|
|
|
json_encode( |
120
|
|
|
$this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-1']->getId()) |
|
|
|
|
121
|
|
|
), |
122
|
|
|
$this->getResponseContent(true) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testGetGroupInsufficentPerms() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$this->logIn('ROLE_ADMIN'); |
129
|
|
|
$this->client->request('GET', '/api/groups/' . TestGroupFixtures::$groups['group-1']->getId()); |
130
|
|
|
|
131
|
|
|
$this->assertForbidden($this->client->getResponse()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function testGetGroupInvalidGroup() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
137
|
|
|
$this->client->request('GET', '/api/groups/1000'); |
138
|
|
|
|
139
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function testUpdateGroup() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$newName = 'Renamed Group'; |
145
|
|
|
|
146
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
147
|
|
|
$this->makeJsonRequest( |
148
|
|
|
'PUT', |
149
|
|
|
'/api/groups/' . TestGroupFixtures::$groups['group-2']->getId(), |
150
|
|
|
[ |
151
|
|
|
'name' => $newName |
152
|
|
|
] |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-2']->getId()); |
|
|
|
|
156
|
|
|
|
157
|
|
|
$this->assertEquals($newName, $group->getName()); |
158
|
|
|
|
159
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
160
|
|
|
$this->assertJsonStringEqualsJsonString( |
161
|
|
|
json_encode($group), |
162
|
|
|
$this->getResponseContent(true) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function testUpdateGroupInsufficentPerms() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->logIn('ROLE_USER'); |
169
|
|
|
$this->client->request('PUT', '/api/groups/' . TestGroupFixtures::$groups['group-2']->getId()); |
170
|
|
|
|
171
|
|
|
$this->assertForbidden($this->client->getResponse()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
public function testUpdateGroupInvalidGroup() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
177
|
|
|
$this->client->request('PUT', '/api/groups/1000'); |
178
|
|
|
|
179
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
public function testDeleteGroup() |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
185
|
|
|
$this->client->request('DELETE', '/api/groups/' . TestGroupFixtures::$groups['group-3']->getId()); |
186
|
|
|
|
187
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $this->client->getResponse()->getStatusCode()); |
188
|
|
|
$this->assertNull($this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-3']->getId())); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
View Code Duplication |
public function testDeleteGroupPopulatedGroup() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
194
|
|
|
$this->client->request('DELETE', '/api/groups/' . TestGroupFixtures::$groups['group-2']->getId()); |
195
|
|
|
|
196
|
|
|
$this->assertEquals(Response::HTTP_UNPROCESSABLE_ENTITY, $this->client->getResponse()->getStatusCode()); |
197
|
|
|
$this->assertNotNull($this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-2']->getId())); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testDeleteGroupInsufficentPerms() |
201
|
|
|
{ |
202
|
|
|
$this->logIn('ROLE_ADMIN'); |
203
|
|
|
$this->client->request('DELETE', '/api/groups/' . TestGroupFixtures::$groups['group-3']->getId()); |
204
|
|
|
|
205
|
|
|
$this->assertForbidden($this->client->getResponse()); |
206
|
|
|
$this->assertNotNull($this->em->find("Overwatch\TestBundle\Entity\TestGroup", TestGroupFixtures::$groups['group-2']->getId())); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
public function testDeleteGroupInvalidGroup() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
212
|
|
|
$this->client->request('DELETE', '/api/groups/1000'); |
213
|
|
|
|
214
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
public function testAddUserToGroup() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$user = UserFixtures::$users['user-2']; |
220
|
|
|
$group = TestGroupFixtures::$groups['group-3']; |
221
|
|
|
|
222
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
223
|
|
|
$this->client->request('POST', '/api/groups/' . $group->getId() . '/user/' . $user->getId()); |
224
|
|
|
|
225
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", $group->getId()); |
|
|
|
|
226
|
|
|
|
227
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
228
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($user), $this->getResponseContent(true)); |
229
|
|
|
$this->assertCount(1, $group->getUsers()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testAddUserToGroupInsufficentPerms() |
233
|
|
|
{ |
234
|
|
|
$user = UserFixtures::$users['user-2']; |
235
|
|
|
$group = TestGroupFixtures::$groups['group-3']; |
236
|
|
|
|
237
|
|
|
$this->logIn('ROLE_ADMIN'); |
238
|
|
|
$this->client->request('POST', '/api/groups/' . $group->getId() . '/user/' . $user->getId()); |
239
|
|
|
|
240
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", $group->getId()); |
|
|
|
|
241
|
|
|
|
242
|
|
|
$this->assertForbidden($this->client->getResponse()); |
243
|
|
|
$this->assertCount(0, $group->getUsers()); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testAddUserToGroupInvalidGroup() |
247
|
|
|
{ |
248
|
|
|
$user = UserFixtures::$users['user-2']; |
249
|
|
|
|
250
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
251
|
|
|
$this->client->request('POST', '/api/groups/1000/user/' . $user->getId()); |
252
|
|
|
|
253
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
View Code Duplication |
public function testAddUserToGroupInvalidUser() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$group = TestGroupFixtures::$groups['group-3']; |
259
|
|
|
|
260
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
261
|
|
|
$this->client->request('POST', '/api/groups/' . $group->getId() . '/user/1000'); |
262
|
|
|
|
263
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", $group->getId()); |
|
|
|
|
264
|
|
|
|
265
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
266
|
|
|
$this->assertCount(0, $group->getUsers()); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
View Code Duplication |
public function testRemoveUserFromGroup() |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$user = UserFixtures::$users['user-2']; |
272
|
|
|
$group = TestGroupFixtures::$groups['group-1']; |
273
|
|
|
|
274
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
275
|
|
|
$this->client->request('DELETE', '/api/groups/' . $group->getId() . '/user/' . $user->getId()); |
276
|
|
|
|
277
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", $group->getId()); |
|
|
|
|
278
|
|
|
|
279
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
280
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($user), $this->getResponseContent(true)); |
281
|
|
|
$this->assertCount(1, $group->getUsers()); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function testRemoveUserFromGroupInsufficentPerms() |
285
|
|
|
{ |
286
|
|
|
$user = UserFixtures::$users['user-2']; |
287
|
|
|
$group = TestGroupFixtures::$groups['group-1']; |
288
|
|
|
|
289
|
|
|
$this->logIn('ROLE_ADMIN'); |
290
|
|
|
$this->client->request('DELETE', '/api/groups/' . $group->getId() . '/user/' . $user->getId()); |
291
|
|
|
|
292
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", $group->getId()); |
|
|
|
|
293
|
|
|
|
294
|
|
|
$this->assertForbidden($this->client->getResponse()); |
295
|
|
|
$this->assertCount(2, $group->getUsers()); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
View Code Duplication |
public function testRemoveUserFromGroupInvalidGroup() |
|
|
|
|
299
|
|
|
{ |
300
|
|
|
$user = UserFixtures::$users['user-2']; |
301
|
|
|
$group = TestGroupFixtures::$groups['group-1']; |
302
|
|
|
|
303
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
304
|
|
|
$this->client->request('DELETE', '/api/groups/1000/user/' . $user->getId()); |
305
|
|
|
|
306
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", $group->getId()); |
|
|
|
|
307
|
|
|
|
308
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
309
|
|
|
$this->assertCount(2, $group->getUsers()); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
View Code Duplication |
public function testRemoveUserFromGroupInvalidUser() |
|
|
|
|
313
|
|
|
{ |
314
|
|
|
$group = TestGroupFixtures::$groups['group-1']; |
315
|
|
|
|
316
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
317
|
|
|
$this->client->request('DELETE', '/api/groups/' . $group->getId() . '/user/1000'); |
318
|
|
|
|
319
|
|
|
$group = $this->em->find("Overwatch\TestBundle\Entity\TestGroup", $group->getId()); |
|
|
|
|
320
|
|
|
|
321
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
322
|
|
|
$this->assertCount(2, $group->getUsers()); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.