|
@@ -5,6 +5,7 @@ namespace Muzich\CoreBundle\Tests\lib\Security;
|
5
|
5
|
use Muzich\CoreBundle\lib\Test\Client;
|
6
|
6
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
7
|
7
|
use Muzich\CoreBundle\Security\Context as SecurityContext;
|
|
8
|
+use Symfony\Component\DomCrawler\Crawler;
|
8
|
9
|
|
9
|
10
|
class ContextTestCases
|
10
|
11
|
{
|
|
@@ -18,7 +19,7 @@ class ContextTestCases
|
18
|
19
|
$this->test = $test;
|
19
|
20
|
}
|
20
|
21
|
|
21
|
|
- private function responseSatisfyConditions($response, $success, $condition)
|
|
22
|
+ private function ajaxResponseSatisfyConditions($response, $success, $condition)
|
22
|
23
|
{
|
23
|
24
|
$response = json_decode($response, true);
|
24
|
25
|
|
|
@@ -45,6 +46,25 @@ class ContextTestCases
|
45
|
46
|
return false;
|
46
|
47
|
}
|
47
|
48
|
|
|
49
|
+ private function responseSatisfyConditions($response, $success, $condition, $user)
|
|
50
|
+ {
|
|
51
|
+ if (($response->getStatusCode() == 200 || $response->getStatusCode() == 302) && $success)
|
|
52
|
+ {
|
|
53
|
+ return true;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ if (($response->getStatusCode() != 302 && $response->getStatusCode() != 302) && !$success)
|
|
57
|
+ {
|
|
58
|
+ $security_context = new SecurityContext($user);
|
|
59
|
+ if ($condition && !$security_context->userIsInThisCondition($condition))
|
|
60
|
+ {
|
|
61
|
+ return false;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ return true;
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+
|
48
|
68
|
public function getAjaxRequestContentResponse($method, $url, $parameters = array())
|
49
|
69
|
{
|
50
|
70
|
$this->test->getClient()->request(
|
|
@@ -56,7 +76,7 @@ class ContextTestCases
|
56
|
76
|
|
57
|
77
|
public function addElementResponseIs($success, $condition)
|
58
|
78
|
{
|
59
|
|
- return $this->responseSatisfyConditions(
|
|
79
|
+ return $this->ajaxResponseSatisfyConditions(
|
60
|
80
|
$this->getAjaxRequestContentResponse(
|
61
|
81
|
'POST',
|
62
|
82
|
$this->test->generateUrl('element_add', array('_locale' => 'fr'))
|
|
@@ -68,7 +88,7 @@ class ContextTestCases
|
68
|
88
|
|
69
|
89
|
public function noteElementResponseIs($success, $condition)
|
70
|
90
|
{
|
71
|
|
- return $this->responseSatisfyConditions(
|
|
91
|
+ return $this->ajaxResponseSatisfyConditions(
|
72
|
92
|
$this->getAjaxRequestContentResponse(
|
73
|
93
|
'GET',
|
74
|
94
|
$this->test->generateUrl('ajax_element_add_vote_good', array(
|
|
@@ -83,7 +103,7 @@ class ContextTestCases
|
83
|
103
|
|
84
|
104
|
public function alertCommentResponseIs($success, $condition)
|
85
|
105
|
{
|
86
|
|
- return $this->responseSatisfyConditions(
|
|
106
|
+ return $this->ajaxResponseSatisfyConditions(
|
87
|
107
|
$this->getAjaxRequestContentResponse(
|
88
|
108
|
'GET',
|
89
|
109
|
$this->test->generateUrl('ajax_alert_comment', array(
|
|
@@ -99,7 +119,7 @@ class ContextTestCases
|
99
|
119
|
|
100
|
120
|
public function alertElementResponseIs($success, $condition)
|
101
|
121
|
{
|
102
|
|
- return $this->responseSatisfyConditions(
|
|
122
|
+ return $this->ajaxResponseSatisfyConditions(
|
103
|
123
|
$this->getAjaxRequestContentResponse(
|
104
|
124
|
'GET',
|
105
|
125
|
$this->test->generateUrl('ajax_report_element', array(
|
|
@@ -114,7 +134,7 @@ class ContextTestCases
|
114
|
134
|
|
115
|
135
|
public function addTagResponseIs($success, $condition)
|
116
|
136
|
{
|
117
|
|
- return $this->responseSatisfyConditions(
|
|
137
|
+ return $this->ajaxResponseSatisfyConditions(
|
118
|
138
|
$this->getAjaxRequestContentResponse(
|
119
|
139
|
'POST',
|
120
|
140
|
$this->test->generateUrl('ajax_add_tag'),
|
|
@@ -127,7 +147,7 @@ class ContextTestCases
|
127
|
147
|
|
128
|
148
|
public function proposeElementTagsResponseIs($success, $condition)
|
129
|
149
|
{
|
130
|
|
- return $this->responseSatisfyConditions(
|
|
150
|
+ return $this->ajaxResponseSatisfyConditions(
|
131
|
151
|
$this->getAjaxRequestContentResponse(
|
132
|
152
|
'POST',
|
133
|
153
|
$this->test->generateUrl('ajax_element_propose_tags_proceed',
|
|
@@ -161,26 +181,17 @@ class ContextTestCases
|
161
|
181
|
array()
|
162
|
182
|
);
|
163
|
183
|
|
164
|
|
- if ($this->test->getClient()->getResponse()->getStatusCode() == 200 && $success)
|
165
|
|
- {
|
166
|
|
- return true;
|
167
|
|
- }
|
168
|
|
-
|
169
|
|
- if ($this->test->getClient()->getResponse()->getStatusCode() != 200 && !$success)
|
170
|
|
- {
|
171
|
|
- $security_context = new SecurityContext($this->test->getUser());
|
172
|
|
- if ($condition && !$security_context->userIsInThisCondition($condition))
|
173
|
|
- {
|
174
|
|
- return false;
|
175
|
|
- }
|
176
|
|
-
|
177
|
|
- return true;
|
178
|
|
- }
|
|
184
|
+ return $this->responseSatisfyConditions(
|
|
185
|
+ $this->test->getClient()->getResponse(),
|
|
186
|
+ $success,
|
|
187
|
+ $condition,
|
|
188
|
+ $this->test->getUser()
|
|
189
|
+ );
|
179
|
190
|
}
|
180
|
191
|
|
181
|
192
|
public function addCommentResponseIs($success, $condition)
|
182
|
193
|
{
|
183
|
|
- return $this->responseSatisfyConditions(
|
|
194
|
+ return $this->ajaxResponseSatisfyConditions(
|
184
|
195
|
$this->getAjaxRequestContentResponse(
|
185
|
196
|
'POST',
|
186
|
197
|
$this->test->generateUrl('ajax_add_comment', array(
|
|
@@ -195,7 +206,7 @@ class ContextTestCases
|
195
|
206
|
|
196
|
207
|
public function addElementToFavoriteResponseIs($success, $condition)
|
197
|
208
|
{
|
198
|
|
- return $this->responseSatisfyConditions(
|
|
209
|
+ return $this->ajaxResponseSatisfyConditions(
|
199
|
210
|
$this->getAjaxRequestContentResponse(
|
200
|
211
|
'GET',
|
201
|
212
|
$this->test->generateUrl('favorite_add', array(
|
|
@@ -210,7 +221,7 @@ class ContextTestCases
|
210
|
221
|
|
211
|
222
|
public function followUserResponseIs($success, $condition)
|
212
|
223
|
{
|
213
|
|
- return $this->responseSatisfyConditions(
|
|
224
|
+ return $this->ajaxResponseSatisfyConditions(
|
214
|
225
|
$this->getAjaxRequestContentResponse(
|
215
|
226
|
'GET',
|
216
|
227
|
$this->test->generateUrl('follow', array(
|
|
@@ -226,7 +237,7 @@ class ContextTestCases
|
226
|
237
|
|
227
|
238
|
public function getFavoritesTagsResponseIs($success, $condition)
|
228
|
239
|
{
|
229
|
|
- return $this->responseSatisfyConditions(
|
|
240
|
+ return $this->ajaxResponseSatisfyConditions(
|
230
|
241
|
$this->getAjaxRequestContentResponse(
|
231
|
242
|
'GET',
|
232
|
243
|
$this->test->generateUrl('ajax_get_favorites_tags', array(
|
|
@@ -238,4 +249,220 @@ class ContextTestCases
|
238
|
249
|
);
|
239
|
250
|
}
|
240
|
251
|
|
|
252
|
+ public function playlistAddElementResponseIs($success, $condition)
|
|
253
|
+ {
|
|
254
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
255
|
+ $this->getAjaxRequestContentResponse(
|
|
256
|
+ 'GET',
|
|
257
|
+ $this->test->generateUrl('playlists_add_element', array(
|
|
258
|
+ 'playlist_id' => 0,
|
|
259
|
+ 'element_id' => 0,
|
|
260
|
+ '_locale' => 'fr'
|
|
261
|
+ ))
|
|
262
|
+ ),
|
|
263
|
+ $success,
|
|
264
|
+ $condition
|
|
265
|
+ );
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ public function playlistUpdateOrderResponseIs($success, $condition)
|
|
269
|
+ {
|
|
270
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
271
|
+ $this->getAjaxRequestContentResponse(
|
|
272
|
+ 'GET',
|
|
273
|
+ $this->test->generateUrl('playlist_update_order', array(
|
|
274
|
+ 'playlist_id' => 0,
|
|
275
|
+ '_locale' => 'fr'
|
|
276
|
+ ))
|
|
277
|
+ ),
|
|
278
|
+ $success,
|
|
279
|
+ $condition
|
|
280
|
+ );
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+ public function playlistRemoveElementResponseIs($success, $condition)
|
|
284
|
+ {
|
|
285
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
286
|
+ $this->getAjaxRequestContentResponse(
|
|
287
|
+ 'GET',
|
|
288
|
+ $this->test->generateUrl('playlist_remove_element', array(
|
|
289
|
+ 'playlist_id' => 0,
|
|
290
|
+ 'element_id' => 0,
|
|
291
|
+ '_locale' => 'fr'
|
|
292
|
+ ))
|
|
293
|
+ ),
|
|
294
|
+ $success,
|
|
295
|
+ $condition
|
|
296
|
+ );
|
|
297
|
+ }
|
|
298
|
+
|
|
299
|
+ public function playlistCreateResponseIs($success, $condition)
|
|
300
|
+ {
|
|
301
|
+ $this->playlistCreate(0, 'my_super_playlist');
|
|
302
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
303
|
+ $this->test->getClient()->getResponse()->getContent(),
|
|
304
|
+ $success,
|
|
305
|
+ $condition
|
|
306
|
+ );
|
|
307
|
+ }
|
|
308
|
+
|
|
309
|
+ public function playlistCreate($element_id, $playlist_name)
|
|
310
|
+ {
|
|
311
|
+ $this->test->goToPage($this->test->generateUrl('playlists_add_element_prompt', array(
|
|
312
|
+ 'element_id' => $element_id,
|
|
313
|
+ '_locale' => 'fr'
|
|
314
|
+ )));
|
|
315
|
+
|
|
316
|
+ $response = json_decode($this->test->client->getResponse()->getContent(), true);
|
|
317
|
+ $crawler = new Crawler($response['data']);
|
|
318
|
+
|
|
319
|
+ $extract = $crawler->filter('input[name="playlist[_token]"]')
|
|
320
|
+ ->extract(array('value'));
|
|
321
|
+ $csrf = $extract[0];
|
|
322
|
+
|
|
323
|
+ $this->test->crawler = $this->test->client->request(
|
|
324
|
+ 'POST',
|
|
325
|
+ $this->test->generateUrl('playlist_add_element_and_create', array(
|
|
326
|
+ 'element_id' => $element_id,
|
|
327
|
+ '_locale' => 'fr'
|
|
328
|
+ )),
|
|
329
|
+ array(
|
|
330
|
+ 'playlist' => array(
|
|
331
|
+ 'name' => $playlist_name,
|
|
332
|
+ '_token' => $csrf
|
|
333
|
+ )
|
|
334
|
+ ),
|
|
335
|
+ array(),
|
|
336
|
+ array('HTTP_X-Requested-With' => 'XMLHttpRequest')
|
|
337
|
+ );
|
|
338
|
+ }
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+ public function playlistCopyResponseIs($success, $condition)
|
|
342
|
+ {
|
|
343
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
344
|
+ $this->getAjaxRequestContentResponse(
|
|
345
|
+ 'GET',
|
|
346
|
+ $this->test->generateUrl('playlists_add_element_and_copy', array(
|
|
347
|
+ 'playlist_id' => 0,
|
|
348
|
+ 'element_id' => 0,
|
|
349
|
+ '_locale' => 'fr'
|
|
350
|
+ ))
|
|
351
|
+ ),
|
|
352
|
+ $success,
|
|
353
|
+ $condition
|
|
354
|
+ );
|
|
355
|
+ }
|
|
356
|
+
|
|
357
|
+ public function playlistDeleteResponseIs($success, $condition)
|
|
358
|
+ {
|
|
359
|
+ $this->test->getClient()->request(
|
|
360
|
+ 'GET',
|
|
361
|
+ $this->test->generateUrl('playlist_delete', array(
|
|
362
|
+ 'playlist_id' => 0,
|
|
363
|
+ 'element_id' => 0,
|
|
364
|
+ '_locale' => 'fr'
|
|
365
|
+ )),
|
|
366
|
+ array(),
|
|
367
|
+ array(),
|
|
368
|
+ array()
|
|
369
|
+ );
|
|
370
|
+
|
|
371
|
+ return $this->responseSatisfyConditions(
|
|
372
|
+ $this->test->getClient()->getResponse(),
|
|
373
|
+ $success,
|
|
374
|
+ $condition,
|
|
375
|
+ $this->test->getUser()
|
|
376
|
+ );
|
|
377
|
+ }
|
|
378
|
+
|
|
379
|
+ public function playlistUnpickResponseIs($success, $condition)
|
|
380
|
+ {
|
|
381
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
382
|
+ $this->getAjaxRequestContentResponse(
|
|
383
|
+ 'GET',
|
|
384
|
+ $this->test->generateUrl('playlist_unpick', array(
|
|
385
|
+ 'playlist_id' => 0,
|
|
386
|
+ '_locale' => 'fr'
|
|
387
|
+ ))
|
|
388
|
+ ),
|
|
389
|
+ $success,
|
|
390
|
+ $condition
|
|
391
|
+ );
|
|
392
|
+ }
|
|
393
|
+
|
|
394
|
+ public function playlistPickResponseIs($success, $condition)
|
|
395
|
+ {
|
|
396
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
397
|
+ $this->getAjaxRequestContentResponse(
|
|
398
|
+ 'GET',
|
|
399
|
+ $this->test->generateUrl('playlist_pick', array(
|
|
400
|
+ 'playlist_id' => 0,
|
|
401
|
+ '_locale' => 'fr'
|
|
402
|
+ ))
|
|
403
|
+ ),
|
|
404
|
+ $success,
|
|
405
|
+ $condition
|
|
406
|
+ );
|
|
407
|
+ }
|
|
408
|
+
|
|
409
|
+ public function playlistShowResponseIs($success, $condition)
|
|
410
|
+ {
|
|
411
|
+ $this->playlistShow('bux', 0);
|
|
412
|
+
|
|
413
|
+ return $this->responseSatisfyConditions(
|
|
414
|
+ $this->test->getClient()->getResponse(),
|
|
415
|
+ $success,
|
|
416
|
+ $condition,
|
|
417
|
+ $this->test->getUser()
|
|
418
|
+ );
|
|
419
|
+ }
|
|
420
|
+
|
|
421
|
+ public function playlistsShow($user_slug)
|
|
422
|
+ {
|
|
423
|
+ $this->test->goToPage($this->test->generateUrl('playlists_user', array(
|
|
424
|
+ 'user_slug' => $user_slug,
|
|
425
|
+ '_locale' => 'fr'
|
|
426
|
+ )));
|
|
427
|
+ }
|
|
428
|
+
|
|
429
|
+ public function playlistShow($user_slug, $playlist_id)
|
|
430
|
+ {
|
|
431
|
+ $this->test->goToPage($this->test->generateUrl('playlist', array(
|
|
432
|
+ 'user_slug' => $user_slug,
|
|
433
|
+ 'playlist_id' => $playlist_id,
|
|
434
|
+ '_locale' => 'fr'
|
|
435
|
+ )));
|
|
436
|
+ }
|
|
437
|
+
|
|
438
|
+ public function playlistAutoplayResponseIs($success, $condition)
|
|
439
|
+ {
|
|
440
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
441
|
+ $this->getAjaxRequestContentResponse(
|
|
442
|
+ 'GET',
|
|
443
|
+ $this->test->generateUrl('playlist_datas_for_autoplay', array(
|
|
444
|
+ 'playlist_id' => 0,
|
|
445
|
+ '_locale' => 'fr'
|
|
446
|
+ ))
|
|
447
|
+ ),
|
|
448
|
+ $success,
|
|
449
|
+ $condition
|
|
450
|
+ );
|
|
451
|
+ }
|
|
452
|
+
|
|
453
|
+ public function playlistPromptResponseIs($success, $condition)
|
|
454
|
+ {
|
|
455
|
+ return $this->ajaxResponseSatisfyConditions(
|
|
456
|
+ $this->getAjaxRequestContentResponse(
|
|
457
|
+ 'GET',
|
|
458
|
+ $this->test->generateUrl('playlists_add_element_prompt', array(
|
|
459
|
+ 'element_id' => 0,
|
|
460
|
+ '_locale' => 'fr'
|
|
461
|
+ ))
|
|
462
|
+ ),
|
|
463
|
+ $success,
|
|
464
|
+ $condition
|
|
465
|
+ );
|
|
466
|
+ }
|
|
467
|
+
|
241
|
468
|
}
|