1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
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 Sonata\AdminBundle\Tests\Controller; |
13
|
|
|
|
14
|
|
|
use Exporter\Exporter; |
15
|
|
|
use Exporter\Writer\JsonWriter; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
18
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
19
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
20
|
|
|
use Sonata\AdminBundle\Controller\CRUDController; |
21
|
|
|
use Sonata\AdminBundle\Exception\LockException; |
22
|
|
|
use Sonata\AdminBundle\Exception\ModelManagerException; |
23
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController; |
24
|
|
|
use Sonata\AdminBundle\Tests\Fixtures\Controller\PreCRUDController; |
25
|
|
|
use Sonata\AdminBundle\Util\AdminObjectAclManipulator; |
26
|
|
|
use Symfony\Bridge\Twig\Extension\FormExtension; |
27
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
28
|
|
|
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
31
|
|
|
use Symfony\Component\HttpFoundation\Response; |
32
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
33
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
34
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
35
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
36
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
37
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
38
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
39
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Test for CRUDController. |
43
|
|
|
* |
44
|
|
|
* @author Andrej Hudec <[email protected]> |
45
|
|
|
* |
46
|
|
|
* @group legacy |
47
|
|
|
*/ |
48
|
|
|
class CRUDControllerTest extends TestCase |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var CRUDController |
52
|
|
|
*/ |
53
|
|
|
private $controller; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var Request |
57
|
|
|
*/ |
58
|
|
|
private $request; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var AbstractAdmin |
62
|
|
|
*/ |
63
|
|
|
private $admin; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var Pool |
67
|
|
|
*/ |
68
|
|
|
private $pool; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
private $parameters; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var Session |
77
|
|
|
*/ |
78
|
|
|
private $session; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var \Sonata\AdminBundle\Model\AuditManager |
82
|
|
|
*/ |
83
|
|
|
private $auditManager; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var ContainerInterface |
87
|
|
|
*/ |
88
|
|
|
private $container; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var AdminObjectAclManipulator |
92
|
|
|
*/ |
93
|
|
|
private $adminObjectAclManipulator; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
private $template; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var array |
102
|
|
|
*/ |
103
|
|
|
private $protectedTestedMethods; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var CsrfProviderInterface |
107
|
|
|
*/ |
108
|
|
|
private $csrfProvider; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var KernelInterface |
112
|
|
|
*/ |
113
|
|
|
private $kernel; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var TranslatorInterface |
117
|
|
|
*/ |
118
|
|
|
private $translator; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
protected function setUp() |
124
|
|
|
{ |
125
|
|
|
$this->container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
126
|
|
|
|
127
|
|
|
$this->request = new Request(); |
128
|
|
|
$this->pool = new Pool($this->container, 'title', 'logo.png'); |
129
|
|
|
$this->pool->setAdminServiceIds(['foo.admin']); |
130
|
|
|
$this->request->attributes->set('_sonata_admin', 'foo.admin'); |
131
|
|
|
$this->admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin') |
132
|
|
|
->disableOriginalConstructor() |
133
|
|
|
->getMock(); |
134
|
|
|
$this->translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface'); |
135
|
|
|
$this->parameters = []; |
136
|
|
|
$this->template = ''; |
137
|
|
|
|
138
|
|
|
// php 5.3 BC |
139
|
|
|
$params = &$this->parameters; |
140
|
|
|
$template = &$this->template; |
141
|
|
|
|
142
|
|
|
$templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine') |
143
|
|
|
->setConstructorArgs([$this->container, []]) |
144
|
|
|
->getMock(); |
145
|
|
|
|
146
|
|
|
$templatingRenderReturnCallback = $this->returnCallback(function ( |
147
|
|
|
$view, |
148
|
|
|
array $parameters = [], |
149
|
|
|
Response $response = null |
150
|
|
|
) use ( |
151
|
|
|
&$params, |
152
|
|
|
&$template |
153
|
|
|
) { |
154
|
|
|
$template = $view; |
155
|
|
|
|
156
|
|
|
if (null === $response) { |
157
|
|
|
$response = new Response(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$params = $parameters; |
161
|
|
|
|
162
|
|
|
return $response; |
163
|
|
|
}); |
164
|
|
|
|
165
|
|
|
// SF < 3.3.10 BC |
166
|
|
|
$templating->expects($this->any()) |
167
|
|
|
->method('renderResponse') |
168
|
|
|
->will($templatingRenderReturnCallback); |
169
|
|
|
|
170
|
|
|
$templating->expects($this->any()) |
171
|
|
|
->method('render') |
172
|
|
|
->will($templatingRenderReturnCallback); |
173
|
|
|
|
174
|
|
|
$this->session = new Session(new MockArraySessionStorage()); |
175
|
|
|
|
176
|
|
|
// php 5.3 BC |
177
|
|
|
$pool = $this->pool; |
178
|
|
|
$request = $this->request; |
|
|
|
|
179
|
|
|
$admin = $this->admin; |
180
|
|
|
$session = $this->session; |
181
|
|
|
$translator = $this->translator; |
182
|
|
|
|
183
|
|
|
$twig = $this->getMockBuilder('Twig_Environment') |
184
|
|
|
->disableOriginalConstructor() |
185
|
|
|
->getMock(); |
186
|
|
|
|
187
|
|
|
$twigRenderer = $this->createMock('Symfony\Bridge\Twig\Form\TwigRendererInterface'); |
188
|
|
|
|
189
|
|
|
$formExtension = new FormExtension($twigRenderer); |
190
|
|
|
|
191
|
|
|
$twig->expects($this->any()) |
192
|
|
|
->method('getExtension') |
193
|
|
|
->will($this->returnCallback(function ($name) use ($formExtension) { |
194
|
|
|
switch ($name) { |
195
|
|
|
case 'Symfony\Bridge\Twig\Extension\FormExtension': |
196
|
|
|
return $formExtension; |
197
|
|
|
} |
198
|
|
|
})); |
199
|
|
|
|
200
|
|
|
$twig->expects($this->any()) |
201
|
|
|
->method('getRuntime') |
202
|
|
|
->will($this->returnCallback(function ($name) use ($twigRenderer) { |
203
|
|
|
switch ($name) { |
204
|
|
|
case 'Symfony\Bridge\Twig\Form\TwigRenderer': |
205
|
|
|
if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) { |
206
|
|
|
return $twigRenderer; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
throw new \Twig_Error_Runtime('This runtime exists when Symony >= 3.2.'); |
210
|
|
|
} |
211
|
|
|
})); |
212
|
|
|
|
213
|
|
|
// NEXT_MAJOR : require sonata/exporter ^1.7 and remove conditional |
214
|
|
|
if (class_exists('Exporter\Exporter')) { |
215
|
|
|
$exporter = new Exporter([new JsonWriter('/tmp/sonataadmin/export.json')]); |
216
|
|
|
} else { |
217
|
|
|
$exporter = $this->createMock('Sonata\AdminBundle\Export\Exporter'); |
218
|
|
|
|
219
|
|
|
$exporter->expects($this->any()) |
220
|
|
|
->method('getResponse') |
221
|
|
|
->will($this->returnValue(new StreamedResponse())); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$this->auditManager = $this->getMockBuilder('Sonata\AdminBundle\Model\AuditManager') |
225
|
|
|
->disableOriginalConstructor() |
226
|
|
|
->getMock(); |
227
|
|
|
|
228
|
|
|
$this->adminObjectAclManipulator = $this->getMockBuilder('Sonata\AdminBundle\Util\AdminObjectAclManipulator') |
229
|
|
|
->disableOriginalConstructor() |
230
|
|
|
->getMock(); |
231
|
|
|
|
232
|
|
|
// php 5.3 BC |
233
|
|
|
$request = $this->request; |
234
|
|
|
$auditManager = $this->auditManager; |
235
|
|
|
$adminObjectAclManipulator = $this->adminObjectAclManipulator; |
236
|
|
|
|
237
|
|
|
// Prefer Symfony 2.x interfaces |
238
|
|
|
if (interface_exists('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface')) { |
239
|
|
|
$this->csrfProvider = $this->getMockBuilder( |
240
|
|
|
'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface' |
241
|
|
|
) |
242
|
|
|
->getMock(); |
243
|
|
|
|
244
|
|
|
$this->csrfProvider->expects($this->any()) |
245
|
|
|
->method('generateCsrfToken') |
246
|
|
|
->will($this->returnCallback(function ($intention) { |
247
|
|
|
return 'csrf-token-123_'.$intention; |
248
|
|
|
})); |
249
|
|
|
|
250
|
|
|
$this->csrfProvider->expects($this->any()) |
251
|
|
|
->method('isCsrfTokenValid') |
252
|
|
|
->will($this->returnCallback(function ($intention, $token) { |
253
|
|
|
if ($token == 'csrf-token-123_'.$intention) { |
254
|
|
|
return true; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return false; |
258
|
|
|
})); |
259
|
|
|
} else { |
260
|
|
|
$this->csrfProvider = $this->getMockBuilder( |
261
|
|
|
'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface' |
262
|
|
|
) |
263
|
|
|
->getMock(); |
264
|
|
|
|
265
|
|
|
$this->csrfProvider->expects($this->any()) |
266
|
|
|
->method('getToken') |
267
|
|
|
->will($this->returnCallback(function ($intention) { |
268
|
|
|
return new CsrfToken($intention, 'csrf-token-123_'.$intention); |
269
|
|
|
})); |
270
|
|
|
|
271
|
|
|
$this->csrfProvider->expects($this->any()) |
272
|
|
|
->method('isTokenValid') |
273
|
|
|
->will($this->returnCallback(function (CsrfToken $token) { |
274
|
|
|
if ($token->getValue() == 'csrf-token-123_'.$token->getId()) { |
275
|
|
|
return true; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return false; |
279
|
|
|
})); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// php 5.3 BC |
283
|
|
|
$csrfProvider = $this->csrfProvider; |
284
|
|
|
|
285
|
|
|
$this->logger = $this->createMock('Psr\Log\LoggerInterface'); |
286
|
|
|
$logger = $this->logger; // php 5.3 BC |
287
|
|
|
|
288
|
|
|
$requestStack = null; |
289
|
|
|
if (class_exists('Symfony\Component\HttpFoundation\RequestStack')) { |
290
|
|
|
$requestStack = new RequestStack(); |
291
|
|
|
$requestStack->push($request); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$this->kernel = $this->createMock('Symfony\Component\HttpKernel\KernelInterface'); |
295
|
|
|
$kernel = $this->kernel; // php 5.3 BC |
296
|
|
|
|
297
|
|
|
$this->container->expects($this->any()) |
298
|
|
|
->method('get') |
299
|
|
|
->will($this->returnCallback(function ($id) use ( |
300
|
|
|
$pool, |
301
|
|
|
$admin, |
302
|
|
|
$request, |
303
|
|
|
$templating, |
304
|
|
|
$twig, |
305
|
|
|
$session, |
306
|
|
|
$exporter, |
307
|
|
|
$auditManager, |
308
|
|
|
$adminObjectAclManipulator, |
309
|
|
|
$requestStack, |
310
|
|
|
$csrfProvider, |
311
|
|
|
$logger, |
312
|
|
|
$kernel, |
313
|
|
|
$translator |
314
|
|
|
) { |
315
|
|
|
switch ($id) { |
316
|
|
|
case 'sonata.admin.pool': |
317
|
|
|
return $pool; |
318
|
|
|
case 'request': |
319
|
|
|
return $request; |
320
|
|
|
case 'request_stack': |
321
|
|
|
return $requestStack; |
322
|
|
|
case 'foo.admin': |
323
|
|
|
return $admin; |
324
|
|
|
case 'templating': |
325
|
|
|
return $templating; |
326
|
|
|
case 'twig': |
327
|
|
|
return $twig; |
328
|
|
|
case 'session': |
329
|
|
|
return $session; |
330
|
|
|
case 'sonata.admin.exporter': |
331
|
|
|
return $exporter; |
332
|
|
|
case 'sonata.admin.audit.manager': |
333
|
|
|
return $auditManager; |
334
|
|
|
case 'sonata.admin.object.manipulator.acl.admin': |
335
|
|
|
return $adminObjectAclManipulator; |
336
|
|
|
case 'form.csrf_provider': |
337
|
|
|
case 'security.csrf.token_manager': |
338
|
|
|
return $csrfProvider; |
339
|
|
|
case 'logger': |
340
|
|
|
return $logger; |
341
|
|
|
case 'kernel': |
342
|
|
|
return $kernel; |
343
|
|
|
case 'translator': |
344
|
|
|
return $translator; |
345
|
|
|
} |
346
|
|
|
})); |
347
|
|
|
|
348
|
|
|
// php 5.3 |
349
|
|
|
$tthis = $this; |
350
|
|
|
|
351
|
|
|
$this->container->expects($this->any()) |
352
|
|
|
->method('has') |
353
|
|
|
->will($this->returnCallback(function ($id) use ($tthis) { |
354
|
|
|
if ($id == 'form.csrf_provider' && Kernel::MAJOR_VERSION == 2 && $tthis->getCsrfProvider() !== null) { |
355
|
|
|
return true; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
if ($id == 'security.csrf.token_manager' && Kernel::MAJOR_VERSION >= 3 && $tthis->getCsrfProvider() !== null) { |
359
|
|
|
return true; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if ($id == 'logger') { |
363
|
|
|
return true; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
if ($id == 'session') { |
367
|
|
|
return true; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
if ($id == 'templating') { |
371
|
|
|
return true; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
if ($id == 'translator') { |
375
|
|
|
return true; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return false; |
379
|
|
|
})); |
380
|
|
|
|
381
|
|
|
$this->container->expects($this->any()) |
382
|
|
|
->method('getParameter') |
383
|
|
|
->will($this->returnCallback(function ($name) { |
384
|
|
|
switch ($name) { |
385
|
|
|
case 'security.role_hierarchy.roles': |
386
|
|
|
return ['ROLE_SUPER_ADMIN' => ['ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN']]; |
387
|
|
|
} |
388
|
|
|
})); |
389
|
|
|
|
390
|
|
|
$this->admin->expects($this->any()) |
391
|
|
|
->method('getTemplate') |
392
|
|
|
->will($this->returnCallback(function ($name) { |
393
|
|
|
switch ($name) { |
394
|
|
|
case 'ajax': |
395
|
|
|
return 'SonataAdminBundle::ajax_layout.html.twig'; |
396
|
|
|
case 'layout': |
397
|
|
|
return 'SonataAdminBundle::standard_layout.html.twig'; |
398
|
|
|
case 'show': |
399
|
|
|
return 'SonataAdminBundle:CRUD:show.html.twig'; |
400
|
|
|
case 'show_compare': |
401
|
|
|
return 'SonataAdminBundle:CRUD:show_compare.html.twig'; |
402
|
|
|
case 'edit': |
403
|
|
|
return 'SonataAdminBundle:CRUD:edit.html.twig'; |
404
|
|
|
case 'dashboard': |
405
|
|
|
return 'SonataAdminBundle:Core:dashboard.html.twig'; |
406
|
|
|
case 'search': |
407
|
|
|
return 'SonataAdminBundle:Core:search.html.twig'; |
408
|
|
|
case 'list': |
409
|
|
|
return 'SonataAdminBundle:CRUD:list.html.twig'; |
410
|
|
|
case 'preview': |
411
|
|
|
return 'SonataAdminBundle:CRUD:preview.html.twig'; |
412
|
|
|
case 'history': |
413
|
|
|
return 'SonataAdminBundle:CRUD:history.html.twig'; |
414
|
|
|
case 'acl': |
415
|
|
|
return 'SonataAdminBundle:CRUD:acl.html.twig'; |
416
|
|
|
case 'delete': |
417
|
|
|
return 'SonataAdminBundle:CRUD:delete.html.twig'; |
418
|
|
|
case 'batch': |
419
|
|
|
return 'SonataAdminBundle:CRUD:list__batch.html.twig'; |
420
|
|
|
case 'batch_confirmation': |
421
|
|
|
return 'SonataAdminBundle:CRUD:batch_confirmation.html.twig'; |
422
|
|
|
} |
423
|
|
|
})); |
424
|
|
|
|
425
|
|
|
$this->admin->expects($this->any()) |
426
|
|
|
->method('getIdParameter') |
427
|
|
|
->will($this->returnValue('id')); |
428
|
|
|
|
429
|
|
|
$this->admin->expects($this->any()) |
430
|
|
|
->method('getAccessMapping') |
431
|
|
|
->will($this->returnValue([])); |
432
|
|
|
|
433
|
|
|
$this->admin->expects($this->any()) |
434
|
|
|
->method('generateUrl') |
435
|
|
|
->will( |
436
|
|
|
$this->returnCallback( |
437
|
|
|
function ($name, array $parameters = [], $absolute = false) { |
|
|
|
|
438
|
|
|
$result = $name; |
439
|
|
|
if (!empty($parameters)) { |
440
|
|
|
$result .= '?'.http_build_query($parameters); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
return $result; |
444
|
|
|
} |
445
|
|
|
) |
446
|
|
|
); |
447
|
|
|
|
448
|
|
|
$this->admin->expects($this->any()) |
449
|
|
|
->method('generateObjectUrl') |
450
|
|
|
->will( |
451
|
|
|
$this->returnCallback( |
452
|
|
|
function ($name, $object, array $parameters = [], $absolute = false) { |
|
|
|
|
453
|
|
|
$result = get_class($object).'_'.$name; |
454
|
|
|
if (!empty($parameters)) { |
455
|
|
|
$result .= '?'.http_build_query($parameters); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
return $result; |
459
|
|
|
} |
460
|
|
|
) |
461
|
|
|
); |
462
|
|
|
|
463
|
|
|
$this->controller = new CRUDController(); |
464
|
|
|
$this->controller->setContainer($this->container); |
465
|
|
|
|
466
|
|
|
// Make some methods public to test them |
467
|
|
|
$testedMethods = [ |
468
|
|
|
'renderJson', |
469
|
|
|
'isXmlHttpRequest', |
470
|
|
|
'configure', |
471
|
|
|
'getBaseTemplate', |
472
|
|
|
'redirectTo', |
473
|
|
|
'addFlash', |
474
|
|
|
]; |
475
|
|
|
foreach ($testedMethods as $testedMethod) { |
476
|
|
|
$method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod); |
477
|
|
|
$method->setAccessible(true); |
478
|
|
|
$this->protectedTestedMethods[$testedMethod] = $method; |
479
|
|
|
} |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
public function testRenderJson1() |
483
|
|
|
{ |
484
|
|
|
$data = ['example' => '123', 'foo' => 'bar']; |
485
|
|
|
|
486
|
|
|
$this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded'); |
487
|
|
|
$response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
488
|
|
|
|
489
|
|
|
$this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
490
|
|
|
$this->assertSame(json_encode($data), $response->getContent()); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
public function testRenderJson2() |
494
|
|
|
{ |
495
|
|
|
$data = ['example' => '123', 'foo' => 'bar']; |
496
|
|
|
|
497
|
|
|
$this->request->headers->set('Content-Type', 'multipart/form-data'); |
498
|
|
|
$response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
499
|
|
|
|
500
|
|
|
$this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
501
|
|
|
$this->assertSame(json_encode($data), $response->getContent()); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
public function testRenderJsonAjax() |
505
|
|
|
{ |
506
|
|
|
$data = ['example' => '123', 'foo' => 'bar']; |
507
|
|
|
|
508
|
|
|
$this->request->attributes->set('_xml_http_request', true); |
509
|
|
|
$this->request->headers->set('Content-Type', 'multipart/form-data'); |
510
|
|
|
$response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request); |
511
|
|
|
|
512
|
|
|
$this->assertSame($response->headers->get('Content-Type'), 'application/json'); |
513
|
|
|
$this->assertSame(json_encode($data), $response->getContent()); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
public function testIsXmlHttpRequest() |
517
|
|
|
{ |
518
|
|
|
$this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
519
|
|
|
|
520
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
521
|
|
|
|
522
|
|
|
$this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
523
|
|
|
|
524
|
|
|
$this->request->headers->remove('X-Requested-With'); |
525
|
|
|
$this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
526
|
|
|
|
527
|
|
|
$this->request->attributes->set('_xml_http_request', true); |
528
|
|
|
$this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request)); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
public function testConfigure() |
532
|
|
|
{ |
533
|
|
|
$uniqueId = ''; |
534
|
|
|
|
535
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
536
|
|
|
->method('setUniqid') |
537
|
|
|
->will($this->returnCallback(function ($uniqid) use (&$uniqueId) { |
538
|
|
|
$uniqueId = $uniqid; |
539
|
|
|
})); |
540
|
|
|
|
541
|
|
|
$this->request->query->set('uniqid', 123456); |
542
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
543
|
|
|
|
544
|
|
|
$this->assertSame(123456, $uniqueId); |
545
|
|
|
$this->assertAttributeSame($this->admin, 'admin', $this->controller); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
public function testConfigureChild() |
549
|
|
|
{ |
550
|
|
|
$uniqueId = ''; |
551
|
|
|
|
552
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
553
|
|
|
->method('setUniqid') |
554
|
|
|
->will($this->returnCallback(function ($uniqid) use (&$uniqueId) { |
555
|
|
|
$uniqueId = $uniqid; |
556
|
|
|
})); |
557
|
|
|
|
558
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
559
|
|
|
->method('isChild') |
560
|
|
|
->will($this->returnValue(true)); |
561
|
|
|
|
562
|
|
|
$adminParent = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin') |
563
|
|
|
->disableOriginalConstructor() |
564
|
|
|
->getMock(); |
565
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
566
|
|
|
->method('getParent') |
567
|
|
|
->will($this->returnValue($adminParent)); |
568
|
|
|
|
569
|
|
|
$this->request->query->set('uniqid', 123456); |
570
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
571
|
|
|
|
572
|
|
|
$this->assertSame(123456, $uniqueId); |
573
|
|
|
$this->assertAttributeInstanceOf(get_class($adminParent), 'admin', $this->controller); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
public function testConfigureWithException() |
577
|
|
|
{ |
578
|
|
|
$this->expectException( |
579
|
|
|
'RuntimeException', |
580
|
|
|
'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`' |
581
|
|
|
); |
582
|
|
|
|
583
|
|
|
$this->request->attributes->remove('_sonata_admin'); |
584
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
public function testConfigureWithException2() |
588
|
|
|
{ |
589
|
|
|
$this->expectException( |
590
|
|
|
'RuntimeException', |
591
|
|
|
'Unable to find the admin class related to the current controller '. |
592
|
|
|
'(Sonata\AdminBundle\Controller\CRUDController)' |
593
|
|
|
); |
594
|
|
|
|
595
|
|
|
$this->pool->setAdminServiceIds(['nonexistent.admin']); |
596
|
|
|
$this->request->attributes->set('_sonata_admin', 'nonexistent.admin'); |
597
|
|
|
$this->protectedTestedMethods['configure']->invoke($this->controller); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
public function testGetBaseTemplate() |
601
|
|
|
{ |
602
|
|
|
$this->assertSame( |
603
|
|
|
'SonataAdminBundle::standard_layout.html.twig', |
604
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
605
|
|
|
); |
606
|
|
|
|
607
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
608
|
|
|
$this->assertSame( |
609
|
|
|
'SonataAdminBundle::ajax_layout.html.twig', |
610
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
611
|
|
|
); |
612
|
|
|
|
613
|
|
|
$this->request->headers->remove('X-Requested-With'); |
614
|
|
|
$this->assertSame( |
615
|
|
|
'SonataAdminBundle::standard_layout.html.twig', |
616
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
617
|
|
|
); |
618
|
|
|
|
619
|
|
|
$this->request->attributes->set('_xml_http_request', true); |
620
|
|
|
$this->assertSame( |
621
|
|
|
'SonataAdminBundle::ajax_layout.html.twig', |
622
|
|
|
$this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request) |
623
|
|
|
); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
public function testRender() |
627
|
|
|
{ |
628
|
|
|
$this->parameters = []; |
629
|
|
|
$this->assertInstanceOf( |
630
|
|
|
'Symfony\Component\HttpFoundation\Response', |
631
|
|
|
$this->controller->render('FooAdminBundle::foo.html.twig', [], null, $this->request) |
|
|
|
|
632
|
|
|
); |
633
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
634
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
635
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
636
|
|
|
$this->assertSame('FooAdminBundle::foo.html.twig', $this->template); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
public function testRenderWithResponse() |
640
|
|
|
{ |
641
|
|
|
$this->parameters = []; |
642
|
|
|
$response = $response = new Response(); |
643
|
|
|
$response->headers->set('X-foo', 'bar'); |
644
|
|
|
$responseResult = $this->controller->render('FooAdminBundle::foo.html.twig', [], $response, $this->request); |
|
|
|
|
645
|
|
|
|
646
|
|
|
$this->assertSame($response, $responseResult); |
647
|
|
|
$this->assertSame('bar', $responseResult->headers->get('X-foo')); |
648
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
649
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
650
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
651
|
|
|
$this->assertSame('FooAdminBundle::foo.html.twig', $this->template); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
public function testRenderCustomParams() |
655
|
|
|
{ |
656
|
|
|
$this->parameters = []; |
657
|
|
|
$this->assertInstanceOf( |
658
|
|
|
'Symfony\Component\HttpFoundation\Response', |
659
|
|
|
$this->controller->render('FooAdminBundle::foo.html.twig', |
660
|
|
|
['foo' => 'bar'], null, $this->request) |
|
|
|
|
661
|
|
|
); |
662
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
663
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
664
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
665
|
|
|
$this->assertSame('bar', $this->parameters['foo']); |
666
|
|
|
$this->assertSame('FooAdminBundle::foo.html.twig', $this->template); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
public function testRenderAjax() |
670
|
|
|
{ |
671
|
|
|
$this->parameters = []; |
672
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
673
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', ['foo' => 'bar'], null, $this->request)); |
|
|
|
|
674
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
675
|
|
|
$this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']); |
676
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
677
|
|
|
$this->assertSame('bar', $this->parameters['foo']); |
678
|
|
|
$this->assertSame('FooAdminBundle::foo.html.twig', $this->template); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
public function testListActionAccessDenied() |
682
|
|
|
{ |
683
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
684
|
|
|
|
685
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
686
|
|
|
->method('checkAccess') |
687
|
|
|
->with($this->equalTo('list')) |
688
|
|
|
->will($this->throwException(new AccessDeniedException())); |
689
|
|
|
|
690
|
|
|
$this->controller->listAction($this->request); |
|
|
|
|
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
public function testPreList() |
694
|
|
|
{ |
695
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
696
|
|
|
->method('hasRoute') |
697
|
|
|
->with($this->equalTo('list')) |
698
|
|
|
->will($this->returnValue(true)); |
699
|
|
|
|
700
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
701
|
|
|
->method('checkAccess') |
702
|
|
|
->with($this->equalTo('list')) |
703
|
|
|
->will($this->returnValue(true)); |
704
|
|
|
|
705
|
|
|
$controller = new PreCRUDController(); |
706
|
|
|
$controller->setContainer($this->container); |
707
|
|
|
|
708
|
|
|
$response = $controller->listAction($this->request); |
|
|
|
|
709
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
710
|
|
|
$this->assertSame('preList called', $response->getContent()); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
public function testListAction() |
714
|
|
|
{ |
715
|
|
|
$datagrid = $this->createMock('Sonata\AdminBundle\Datagrid\DatagridInterface'); |
716
|
|
|
|
717
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
718
|
|
|
->method('hasRoute') |
719
|
|
|
->with($this->equalTo('list')) |
720
|
|
|
->will($this->returnValue(true)); |
721
|
|
|
|
722
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
723
|
|
|
->method('checkAccess') |
724
|
|
|
->with($this->equalTo('list')) |
725
|
|
|
->will($this->returnValue(true)); |
726
|
|
|
|
727
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
728
|
|
|
->disableOriginalConstructor() |
729
|
|
|
->getMock(); |
730
|
|
|
|
731
|
|
|
$form->expects($this->once()) |
732
|
|
|
->method('createView') |
733
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
734
|
|
|
|
735
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
736
|
|
|
->method('getDatagrid') |
737
|
|
|
->will($this->returnValue($datagrid)); |
738
|
|
|
|
739
|
|
|
$datagrid->expects($this->once()) |
740
|
|
|
->method('getForm') |
741
|
|
|
->will($this->returnValue($form)); |
742
|
|
|
|
743
|
|
|
$this->parameters = []; |
744
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->listAction($this->request)); |
|
|
|
|
745
|
|
|
|
746
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
747
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
748
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
749
|
|
|
|
750
|
|
|
$this->assertSame('list', $this->parameters['action']); |
751
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
752
|
|
|
$this->assertInstanceOf('Sonata\AdminBundle\Datagrid\DatagridInterface', $this->parameters['datagrid']); |
753
|
|
|
$this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']); |
754
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
755
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:list.html.twig', $this->template); |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
public function testBatchActionDeleteAccessDenied() |
759
|
|
|
{ |
760
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
761
|
|
|
|
762
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
763
|
|
|
->method('checkAccess') |
764
|
|
|
->with($this->equalTo('batchDelete')) |
765
|
|
|
->will($this->throwException(new AccessDeniedException())); |
766
|
|
|
|
767
|
|
|
$this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface')); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
public function testBatchActionDelete() |
771
|
|
|
{ |
772
|
|
|
$modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'); |
773
|
|
|
|
774
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
775
|
|
|
->method('checkAccess') |
776
|
|
|
->with($this->equalTo('batchDelete')) |
777
|
|
|
->will($this->returnValue(true)); |
778
|
|
|
|
779
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
780
|
|
|
->method('getModelManager') |
781
|
|
|
->will($this->returnValue($modelManager)); |
782
|
|
|
|
783
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
784
|
|
|
->method('getFilterParameters') |
785
|
|
|
->will($this->returnValue(['foo' => 'bar'])); |
786
|
|
|
|
787
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
788
|
|
|
|
789
|
|
|
$result = $this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface')); |
790
|
|
|
|
791
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
792
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
793
|
|
|
$this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
public function testBatchActionDeleteWithModelManagerException() |
797
|
|
|
{ |
798
|
|
|
$modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'); |
799
|
|
|
$this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete'); |
800
|
|
|
|
801
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
802
|
|
|
->method('getModelManager') |
803
|
|
|
->will($this->returnValue($modelManager)); |
804
|
|
|
|
805
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
806
|
|
|
->method('getFilterParameters') |
807
|
|
|
->will($this->returnValue(['foo' => 'bar'])); |
808
|
|
|
|
809
|
|
|
$this->expectTranslate('flash_batch_delete_error', [], 'SonataAdminBundle'); |
810
|
|
|
|
811
|
|
|
$result = $this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface')); |
812
|
|
|
|
813
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
814
|
|
|
$this->assertSame(['flash_batch_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
815
|
|
|
$this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl()); |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
public function testBatchActionDeleteWithModelManagerExceptionInDebugMode() |
819
|
|
|
{ |
820
|
|
|
$modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'); |
821
|
|
|
$this->expectException('Sonata\AdminBundle\Exception\ModelManagerException'); |
822
|
|
|
|
823
|
|
|
$modelManager->expects($this->once()) |
824
|
|
|
->method('batchDelete') |
825
|
|
|
->will($this->returnCallback(function () { |
826
|
|
|
throw new ModelManagerException(); |
827
|
|
|
})); |
828
|
|
|
|
829
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
830
|
|
|
->method('getModelManager') |
831
|
|
|
->will($this->returnValue($modelManager)); |
832
|
|
|
|
833
|
|
|
$this->kernel->expects($this->once()) |
|
|
|
|
834
|
|
|
->method('isDebug') |
835
|
|
|
->will($this->returnValue(true)); |
836
|
|
|
|
837
|
|
|
$this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface')); |
838
|
|
|
} |
839
|
|
|
|
840
|
|
|
public function testShowActionNotFoundException() |
841
|
|
|
{ |
842
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
843
|
|
|
|
844
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
845
|
|
|
->method('getObject') |
846
|
|
|
->will($this->returnValue(false)); |
847
|
|
|
|
848
|
|
|
$this->controller->showAction(null, $this->request); |
|
|
|
|
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
public function testShowActionAccessDenied() |
852
|
|
|
{ |
853
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
854
|
|
|
|
855
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
856
|
|
|
->method('getObject') |
857
|
|
|
->will($this->returnValue(new \stdClass())); |
858
|
|
|
|
859
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
860
|
|
|
->method('checkAccess') |
861
|
|
|
->with($this->equalTo('show')) |
862
|
|
|
->will($this->throwException(new AccessDeniedException())); |
863
|
|
|
|
864
|
|
|
$this->controller->showAction(null, $this->request); |
|
|
|
|
865
|
|
|
} |
866
|
|
|
|
867
|
|
|
public function testPreShow() |
868
|
|
|
{ |
869
|
|
|
$object = new \stdClass(); |
870
|
|
|
$object->foo = 123456; |
871
|
|
|
|
872
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
873
|
|
|
->method('getObject') |
874
|
|
|
->will($this->returnValue($object)); |
875
|
|
|
|
876
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
877
|
|
|
->method('checkAccess') |
878
|
|
|
->with($this->equalTo('show')) |
879
|
|
|
->will($this->returnValue(true)); |
880
|
|
|
|
881
|
|
|
$controller = new PreCRUDController(); |
882
|
|
|
$controller->setContainer($this->container); |
883
|
|
|
|
884
|
|
|
$response = $controller->showAction(null, $this->request); |
|
|
|
|
885
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
886
|
|
|
$this->assertSame('preShow called: 123456', $response->getContent()); |
887
|
|
|
} |
888
|
|
|
|
889
|
|
|
public function testShowAction() |
890
|
|
|
{ |
891
|
|
|
$object = new \stdClass(); |
892
|
|
|
|
893
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
894
|
|
|
->method('getObject') |
895
|
|
|
->will($this->returnValue($object)); |
896
|
|
|
|
897
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
898
|
|
|
->method('checkAccess') |
899
|
|
|
->with($this->equalTo('show')) |
900
|
|
|
->will($this->returnValue(true)); |
901
|
|
|
|
902
|
|
|
$show = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection'); |
903
|
|
|
|
904
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
905
|
|
|
->method('getShow') |
906
|
|
|
->will($this->returnValue($show)); |
907
|
|
|
|
908
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->showAction(null, $this->request)); |
|
|
|
|
909
|
|
|
|
910
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
911
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
912
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
913
|
|
|
|
914
|
|
|
$this->assertSame('show', $this->parameters['action']); |
915
|
|
|
$this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $this->parameters['elements']); |
916
|
|
|
$this->assertSame($object, $this->parameters['object']); |
917
|
|
|
|
918
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
919
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:show.html.twig', $this->template); |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
/** |
923
|
|
|
* @dataProvider getRedirectToTests |
924
|
|
|
*/ |
925
|
|
|
public function testRedirectTo($expected, $route, $queryParams, $hasActiveSubclass) |
926
|
|
|
{ |
927
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
928
|
|
|
->method('hasActiveSubclass') |
929
|
|
|
->will($this->returnValue($hasActiveSubclass)); |
930
|
|
|
|
931
|
|
|
$object = new \stdClass(); |
932
|
|
|
|
933
|
|
|
foreach ($queryParams as $key => $value) { |
934
|
|
|
$this->request->query->set($key, $value); |
935
|
|
|
} |
936
|
|
|
|
937
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
938
|
|
|
->method('hasRoute') |
939
|
|
|
->with($this->equalTo($route)) |
940
|
|
|
->will($this->returnValue(true)); |
941
|
|
|
|
942
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
943
|
|
|
->method('hasAccess') |
944
|
|
|
->with($this->equalTo($route)) |
945
|
|
|
->will($this->returnValue(true)); |
946
|
|
|
|
947
|
|
|
$response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
948
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
949
|
|
|
$this->assertSame($expected, $response->getTargetUrl()); |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
public function testRedirectToWithObject() |
953
|
|
|
{ |
954
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
955
|
|
|
->method('hasActiveSubclass') |
956
|
|
|
->will($this->returnValue(false)); |
957
|
|
|
|
958
|
|
|
$object = new \stdClass(); |
959
|
|
|
|
960
|
|
|
$this->admin->expects($this->at(0)) |
|
|
|
|
961
|
|
|
->method('hasRoute') |
962
|
|
|
->with($this->equalTo('edit')) |
963
|
|
|
->will($this->returnValue(true)); |
964
|
|
|
|
965
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
966
|
|
|
->method('hasAccess') |
967
|
|
|
->with($this->equalTo('edit'), $object) |
968
|
|
|
->will($this->returnValue(false)); |
969
|
|
|
|
970
|
|
|
$response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request); |
971
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
972
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
public function getRedirectToTests() |
976
|
|
|
{ |
977
|
|
|
return [ |
978
|
|
|
['stdClass_edit', 'edit', [], false], |
979
|
|
|
['list', 'list', ['btn_update_and_list' => true], false], |
980
|
|
|
['list', 'list', ['btn_create_and_list' => true], false], |
981
|
|
|
['create', 'create', ['btn_create_and_create' => true], false], |
982
|
|
|
['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], true], |
983
|
|
|
]; |
984
|
|
|
} |
985
|
|
|
|
986
|
|
|
public function testAddFlash() |
987
|
|
|
{ |
988
|
|
|
$this->protectedTestedMethods['addFlash']->invoke($this->controller, 'foo', 'bar'); |
989
|
|
|
$this->assertSame(['bar'], $this->session->getFlashBag()->get('foo')); |
990
|
|
|
} |
991
|
|
|
|
992
|
|
|
public function testDeleteActionNotFoundException() |
993
|
|
|
{ |
994
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
995
|
|
|
|
996
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
997
|
|
|
->method('getObject') |
998
|
|
|
->will($this->returnValue(false)); |
999
|
|
|
|
1000
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1001
|
|
|
} |
1002
|
|
|
|
1003
|
|
|
public function testDeleteActionAccessDenied() |
1004
|
|
|
{ |
1005
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
1006
|
|
|
|
1007
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1008
|
|
|
->method('getObject') |
1009
|
|
|
->will($this->returnValue(new \stdClass())); |
1010
|
|
|
|
1011
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1012
|
|
|
->method('checkAccess') |
1013
|
|
|
->with($this->equalTo('delete')) |
1014
|
|
|
->will($this->throwException(new AccessDeniedException())); |
1015
|
|
|
|
1016
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1017
|
|
|
} |
1018
|
|
|
|
1019
|
|
|
public function testPreDelete() |
1020
|
|
|
{ |
1021
|
|
|
$object = new \stdClass(); |
1022
|
|
|
$object->foo = 123456; |
1023
|
|
|
|
1024
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1025
|
|
|
->method('getObject') |
1026
|
|
|
->will($this->returnValue($object)); |
1027
|
|
|
|
1028
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1029
|
|
|
->method('checkAccess') |
1030
|
|
|
->with($this->equalTo('delete')) |
1031
|
|
|
->will($this->returnValue(true)); |
1032
|
|
|
|
1033
|
|
|
$controller = new PreCRUDController(); |
1034
|
|
|
$controller->setContainer($this->container); |
1035
|
|
|
|
1036
|
|
|
$response = $controller->deleteAction(null, $this->request); |
|
|
|
|
1037
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
1038
|
|
|
$this->assertSame('preDelete called: 123456', $response->getContent()); |
1039
|
|
|
} |
1040
|
|
|
|
1041
|
|
|
public function testDeleteAction() |
1042
|
|
|
{ |
1043
|
|
|
$object = new \stdClass(); |
1044
|
|
|
|
1045
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1046
|
|
|
->method('getObject') |
1047
|
|
|
->will($this->returnValue($object)); |
1048
|
|
|
|
1049
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1050
|
|
|
->method('checkAccess') |
1051
|
|
|
->with($this->equalTo('delete')) |
1052
|
|
|
->will($this->returnValue(true)); |
1053
|
|
|
|
1054
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request)); |
|
|
|
|
1055
|
|
|
|
1056
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1057
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1058
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1059
|
|
|
|
1060
|
|
|
$this->assertSame('delete', $this->parameters['action']); |
1061
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1062
|
|
|
$this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
1063
|
|
|
|
1064
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1065
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template); |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
public function testDeleteActionNoCsrfToken() |
1069
|
|
|
{ |
1070
|
|
|
$this->csrfProvider = null; |
1071
|
|
|
|
1072
|
|
|
$object = new \stdClass(); |
1073
|
|
|
|
1074
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1075
|
|
|
->method('getObject') |
1076
|
|
|
->will($this->returnValue($object)); |
1077
|
|
|
|
1078
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1079
|
|
|
->method('checkAccess') |
1080
|
|
|
->with($this->equalTo('delete')) |
1081
|
|
|
->will($this->returnValue(true)); |
1082
|
|
|
|
1083
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request)); |
|
|
|
|
1084
|
|
|
|
1085
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1086
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1087
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1088
|
|
|
|
1089
|
|
|
$this->assertSame('delete', $this->parameters['action']); |
1090
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1091
|
|
|
$this->assertSame(false, $this->parameters['csrf_token']); |
1092
|
|
|
|
1093
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1094
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
public function testDeleteActionAjaxSuccess1() |
1098
|
|
|
{ |
1099
|
|
|
$object = new \stdClass(); |
1100
|
|
|
|
1101
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1102
|
|
|
->method('getObject') |
1103
|
|
|
->will($this->returnValue($object)); |
1104
|
|
|
|
1105
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1106
|
|
|
->method('checkAccess') |
1107
|
|
|
->with($this->equalTo('delete')) |
1108
|
|
|
->will($this->returnValue(true)); |
1109
|
|
|
|
1110
|
|
|
$this->request->setMethod('DELETE'); |
1111
|
|
|
|
1112
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1113
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1114
|
|
|
|
1115
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1116
|
|
|
|
1117
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
1118
|
|
|
$this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
1119
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1120
|
|
|
} |
1121
|
|
|
|
1122
|
|
|
public function testDeleteActionAjaxSuccess2() |
1123
|
|
|
{ |
1124
|
|
|
$object = new \stdClass(); |
1125
|
|
|
|
1126
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1127
|
|
|
->method('getObject') |
1128
|
|
|
->will($this->returnValue($object)); |
1129
|
|
|
|
1130
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1131
|
|
|
->method('checkAccess') |
1132
|
|
|
->with($this->equalTo('delete')) |
1133
|
|
|
->will($this->returnValue(true)); |
1134
|
|
|
|
1135
|
|
|
$this->request->setMethod('POST'); |
1136
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1137
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1138
|
|
|
|
1139
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1140
|
|
|
|
1141
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1142
|
|
|
|
1143
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
1144
|
|
|
$this->assertSame(json_encode(['result' => 'ok']), $response->getContent()); |
1145
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1146
|
|
|
} |
1147
|
|
|
|
1148
|
|
|
public function testDeleteActionAjaxError() |
1149
|
|
|
{ |
1150
|
|
|
$object = new \stdClass(); |
1151
|
|
|
|
1152
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1153
|
|
|
->method('getObject') |
1154
|
|
|
->will($this->returnValue($object)); |
1155
|
|
|
|
1156
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1157
|
|
|
->method('checkAccess') |
1158
|
|
|
->with($this->equalTo('delete')) |
1159
|
|
|
->will($this->returnValue(true)); |
1160
|
|
|
|
1161
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1162
|
|
|
->method('getClass') |
1163
|
|
|
->will($this->returnValue('stdClass')); |
1164
|
|
|
|
1165
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
1166
|
|
|
|
1167
|
|
|
$this->request->setMethod('DELETE'); |
1168
|
|
|
|
1169
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1170
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1171
|
|
|
|
1172
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1173
|
|
|
|
1174
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
1175
|
|
|
$this->assertSame(json_encode(['result' => 'error']), $response->getContent()); |
1176
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1177
|
|
|
} |
1178
|
|
|
|
1179
|
|
|
public function testDeleteActionWithModelManagerExceptionInDebugMode() |
1180
|
|
|
{ |
1181
|
|
|
$this->expectException('Sonata\AdminBundle\Exception\ModelManagerException'); |
1182
|
|
|
|
1183
|
|
|
$object = new \stdClass(); |
1184
|
|
|
|
1185
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1186
|
|
|
->method('getObject') |
1187
|
|
|
->will($this->returnValue($object)); |
1188
|
|
|
|
1189
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1190
|
|
|
->method('checkAccess') |
1191
|
|
|
->with($this->equalTo('delete')) |
1192
|
|
|
->will($this->returnValue(true)); |
1193
|
|
|
|
1194
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1195
|
|
|
->method('delete') |
1196
|
|
|
->will($this->returnCallback(function () { |
1197
|
|
|
throw new ModelManagerException(); |
1198
|
|
|
})); |
1199
|
|
|
|
1200
|
|
|
$this->kernel->expects($this->once()) |
|
|
|
|
1201
|
|
|
->method('isDebug') |
1202
|
|
|
->will($this->returnValue(true)); |
1203
|
|
|
|
1204
|
|
|
$this->request->setMethod('DELETE'); |
1205
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1206
|
|
|
|
1207
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1208
|
|
|
} |
1209
|
|
|
|
1210
|
|
|
/** |
1211
|
|
|
* @dataProvider getToStringValues |
1212
|
|
|
*/ |
1213
|
|
|
public function testDeleteActionSuccess1($expectedToStringValue, $toStringValue) |
1214
|
|
|
{ |
1215
|
|
|
$object = new \stdClass(); |
1216
|
|
|
|
1217
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1218
|
|
|
->method('getObject') |
1219
|
|
|
->will($this->returnValue($object)); |
1220
|
|
|
|
1221
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1222
|
|
|
->method('toString') |
1223
|
|
|
->with($this->equalTo($object)) |
1224
|
|
|
->will($this->returnValue($toStringValue)); |
1225
|
|
|
|
1226
|
|
|
$this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1227
|
|
|
|
1228
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1229
|
|
|
->method('checkAccess') |
1230
|
|
|
->with($this->equalTo('delete')) |
1231
|
|
|
->will($this->returnValue(true)); |
1232
|
|
|
|
1233
|
|
|
$this->request->setMethod('DELETE'); |
1234
|
|
|
|
1235
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1236
|
|
|
|
1237
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1238
|
|
|
|
1239
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
1240
|
|
|
$this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1241
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1242
|
|
|
} |
1243
|
|
|
|
1244
|
|
|
/** |
1245
|
|
|
* @dataProvider getToStringValues |
1246
|
|
|
*/ |
1247
|
|
|
public function testDeleteActionSuccess2($expectedToStringValue, $toStringValue) |
1248
|
|
|
{ |
1249
|
|
|
$object = new \stdClass(); |
1250
|
|
|
|
1251
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1252
|
|
|
->method('getObject') |
1253
|
|
|
->will($this->returnValue($object)); |
1254
|
|
|
|
1255
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1256
|
|
|
->method('checkAccess') |
1257
|
|
|
->with($this->equalTo('delete')) |
1258
|
|
|
->will($this->returnValue(true)); |
1259
|
|
|
|
1260
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1261
|
|
|
->method('toString') |
1262
|
|
|
->with($this->equalTo($object)) |
1263
|
|
|
->will($this->returnValue($toStringValue)); |
1264
|
|
|
|
1265
|
|
|
$this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1266
|
|
|
|
1267
|
|
|
$this->request->setMethod('POST'); |
1268
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1269
|
|
|
|
1270
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1271
|
|
|
|
1272
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1273
|
|
|
|
1274
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
1275
|
|
|
$this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1276
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1277
|
|
|
} |
1278
|
|
|
|
1279
|
|
|
/** |
1280
|
|
|
* @dataProvider getToStringValues |
1281
|
|
|
*/ |
1282
|
|
|
public function testDeleteActionSuccessNoCsrfTokenProvider($expectedToStringValue, $toStringValue) |
1283
|
|
|
{ |
1284
|
|
|
$this->csrfProvider = null; |
1285
|
|
|
|
1286
|
|
|
$object = new \stdClass(); |
1287
|
|
|
|
1288
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1289
|
|
|
->method('getObject') |
1290
|
|
|
->will($this->returnValue($object)); |
1291
|
|
|
|
1292
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1293
|
|
|
->method('checkAccess') |
1294
|
|
|
->with($this->equalTo('delete')) |
1295
|
|
|
->will($this->returnValue(true)); |
1296
|
|
|
|
1297
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1298
|
|
|
->method('toString') |
1299
|
|
|
->with($this->equalTo($object)) |
1300
|
|
|
->will($this->returnValue($toStringValue)); |
1301
|
|
|
|
1302
|
|
|
$this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1303
|
|
|
|
1304
|
|
|
$this->request->setMethod('POST'); |
1305
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1306
|
|
|
|
1307
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1308
|
|
|
|
1309
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
1310
|
|
|
$this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1311
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1312
|
|
|
} |
1313
|
|
|
|
1314
|
|
|
public function testDeleteActionWrongRequestMethod() |
1315
|
|
|
{ |
1316
|
|
|
$object = new \stdClass(); |
1317
|
|
|
|
1318
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1319
|
|
|
->method('getObject') |
1320
|
|
|
->will($this->returnValue($object)); |
1321
|
|
|
|
1322
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1323
|
|
|
->method('checkAccess') |
1324
|
|
|
->with($this->equalTo('delete')) |
1325
|
|
|
->will($this->returnValue(true)); |
1326
|
|
|
|
1327
|
|
|
//without POST request parameter "_method" should not be used as real REST method |
1328
|
|
|
$this->request->query->set('_method', 'DELETE'); |
1329
|
|
|
|
1330
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request)); |
|
|
|
|
1331
|
|
|
|
1332
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1333
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1334
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1335
|
|
|
|
1336
|
|
|
$this->assertSame('delete', $this->parameters['action']); |
1337
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1338
|
|
|
$this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']); |
1339
|
|
|
|
1340
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1341
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template); |
1342
|
|
|
} |
1343
|
|
|
|
1344
|
|
|
/** |
1345
|
|
|
* @dataProvider getToStringValues |
1346
|
|
|
*/ |
1347
|
|
|
public function testDeleteActionError($expectedToStringValue, $toStringValue) |
1348
|
|
|
{ |
1349
|
|
|
$object = new \stdClass(); |
1350
|
|
|
|
1351
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1352
|
|
|
->method('getObject') |
1353
|
|
|
->will($this->returnValue($object)); |
1354
|
|
|
|
1355
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1356
|
|
|
->method('checkAccess') |
1357
|
|
|
->with($this->equalTo('delete')) |
1358
|
|
|
->will($this->returnValue(true)); |
1359
|
|
|
|
1360
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1361
|
|
|
->method('toString') |
1362
|
|
|
->with($this->equalTo($object)) |
1363
|
|
|
->will($this->returnValue($toStringValue)); |
1364
|
|
|
|
1365
|
|
|
$this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1366
|
|
|
|
1367
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'delete'); |
1368
|
|
|
|
1369
|
|
|
$this->request->setMethod('DELETE'); |
1370
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete'); |
1371
|
|
|
|
1372
|
|
|
$response = $this->controller->deleteAction(1, $this->request); |
|
|
|
|
1373
|
|
|
|
1374
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
1375
|
|
|
$this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error')); |
1376
|
|
|
$this->assertSame('list', $response->getTargetUrl()); |
1377
|
|
|
} |
1378
|
|
|
|
1379
|
|
|
public function testDeleteActionInvalidCsrfToken() |
1380
|
|
|
{ |
1381
|
|
|
$object = new \stdClass(); |
1382
|
|
|
|
1383
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1384
|
|
|
->method('getObject') |
1385
|
|
|
->will($this->returnValue($object)); |
1386
|
|
|
|
1387
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1388
|
|
|
->method('checkAccess') |
1389
|
|
|
->with($this->equalTo('delete')) |
1390
|
|
|
->will($this->returnValue(true)); |
1391
|
|
|
|
1392
|
|
|
$this->request->setMethod('POST'); |
1393
|
|
|
$this->request->request->set('_method', 'DELETE'); |
1394
|
|
|
$this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
1395
|
|
|
|
1396
|
|
|
try { |
1397
|
|
|
$this->controller->deleteAction(1, $this->request); |
|
|
|
|
1398
|
|
|
} catch (HttpException $e) { |
1399
|
|
|
$this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
1400
|
|
|
$this->assertSame(400, $e->getStatusCode()); |
1401
|
|
|
} |
1402
|
|
|
} |
1403
|
|
|
|
1404
|
|
|
public function testEditActionNotFoundException() |
1405
|
|
|
{ |
1406
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
1407
|
|
|
|
1408
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1409
|
|
|
->method('getObject') |
1410
|
|
|
->will($this->returnValue(false)); |
1411
|
|
|
|
1412
|
|
|
$this->controller->editAction(null, $this->request); |
|
|
|
|
1413
|
|
|
} |
1414
|
|
|
|
1415
|
|
|
public function testEditActionAccessDenied() |
1416
|
|
|
{ |
1417
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
1418
|
|
|
|
1419
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1420
|
|
|
->method('getObject') |
1421
|
|
|
->will($this->returnValue(new \stdClass())); |
1422
|
|
|
|
1423
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1424
|
|
|
->method('checkAccess') |
1425
|
|
|
->with($this->equalTo('edit')) |
1426
|
|
|
->will($this->throwException(new AccessDeniedException())); |
1427
|
|
|
|
1428
|
|
|
$this->controller->editAction(null, $this->request); |
|
|
|
|
1429
|
|
|
} |
1430
|
|
|
|
1431
|
|
|
public function testPreEdit() |
1432
|
|
|
{ |
1433
|
|
|
$object = new \stdClass(); |
1434
|
|
|
$object->foo = 123456; |
1435
|
|
|
|
1436
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1437
|
|
|
->method('getObject') |
1438
|
|
|
->will($this->returnValue($object)); |
1439
|
|
|
|
1440
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1441
|
|
|
->method('checkAccess') |
1442
|
|
|
->with($this->equalTo('edit')) |
1443
|
|
|
->will($this->returnValue(true)); |
1444
|
|
|
|
1445
|
|
|
$controller = new PreCRUDController(); |
1446
|
|
|
$controller->setContainer($this->container); |
1447
|
|
|
|
1448
|
|
|
$response = $controller->editAction(null, $this->request); |
|
|
|
|
1449
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
1450
|
|
|
$this->assertSame('preEdit called: 123456', $response->getContent()); |
1451
|
|
|
} |
1452
|
|
|
|
1453
|
|
|
public function testEditAction() |
1454
|
|
|
{ |
1455
|
|
|
$object = new \stdClass(); |
1456
|
|
|
|
1457
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1458
|
|
|
->method('getObject') |
1459
|
|
|
->will($this->returnValue($object)); |
1460
|
|
|
|
1461
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1462
|
|
|
->method('checkAccess') |
1463
|
|
|
->with($this->equalTo('edit')) |
1464
|
|
|
->will($this->returnValue(true)); |
1465
|
|
|
|
1466
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1467
|
|
|
->disableOriginalConstructor() |
1468
|
|
|
->getMock(); |
1469
|
|
|
|
1470
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1471
|
|
|
->method('getForm') |
1472
|
|
|
->will($this->returnValue($form)); |
1473
|
|
|
|
1474
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
1475
|
|
|
|
1476
|
|
|
$form->expects($this->any()) |
1477
|
|
|
->method('createView') |
1478
|
|
|
->will($this->returnValue($formView)); |
1479
|
|
|
|
1480
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request)); |
|
|
|
|
1481
|
|
|
|
1482
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1483
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1484
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1485
|
|
|
|
1486
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1487
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
1488
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1489
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1490
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
1491
|
|
|
} |
1492
|
|
|
|
1493
|
|
|
/** |
1494
|
|
|
* @dataProvider getToStringValues |
1495
|
|
|
*/ |
1496
|
|
|
public function testEditActionSuccess($expectedToStringValue, $toStringValue) |
1497
|
|
|
{ |
1498
|
|
|
$object = new \stdClass(); |
1499
|
|
|
|
1500
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1501
|
|
|
->method('getObject') |
1502
|
|
|
->will($this->returnValue($object)); |
1503
|
|
|
|
1504
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1505
|
|
|
->method('update') |
1506
|
|
|
->will($this->returnArgument(0)); |
1507
|
|
|
|
1508
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1509
|
|
|
->method('checkAccess') |
1510
|
|
|
->with($this->equalTo('edit')); |
1511
|
|
|
|
1512
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1513
|
|
|
->method('hasRoute') |
1514
|
|
|
->with($this->equalTo('edit')) |
1515
|
|
|
->will($this->returnValue(true)); |
1516
|
|
|
|
1517
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1518
|
|
|
->method('hasAccess') |
1519
|
|
|
->with($this->equalTo('edit')) |
1520
|
|
|
->will($this->returnValue(true)); |
1521
|
|
|
|
1522
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1523
|
|
|
->disableOriginalConstructor() |
1524
|
|
|
->getMock(); |
1525
|
|
|
|
1526
|
|
|
$form->expects($this->once()) |
1527
|
|
|
->method('getData') |
1528
|
|
|
->will($this->returnValue($object)); |
1529
|
|
|
|
1530
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1531
|
|
|
->method('getForm') |
1532
|
|
|
->will($this->returnValue($form)); |
1533
|
|
|
|
1534
|
|
|
$form->expects($this->once()) |
1535
|
|
|
->method('isSubmitted') |
1536
|
|
|
->will($this->returnValue(true)); |
1537
|
|
|
|
1538
|
|
|
$form->expects($this->once()) |
1539
|
|
|
->method('isValid') |
1540
|
|
|
->will($this->returnValue(true)); |
1541
|
|
|
|
1542
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1543
|
|
|
->method('toString') |
1544
|
|
|
->with($this->equalTo($object)) |
1545
|
|
|
->will($this->returnValue($toStringValue)); |
1546
|
|
|
|
1547
|
|
|
$this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1548
|
|
|
|
1549
|
|
|
$this->request->setMethod('POST'); |
1550
|
|
|
|
1551
|
|
|
$response = $this->controller->editAction(null, $this->request); |
|
|
|
|
1552
|
|
|
|
1553
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
1554
|
|
|
$this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
1555
|
|
|
$this->assertSame('stdClass_edit', $response->getTargetUrl()); |
1556
|
|
|
} |
1557
|
|
|
|
1558
|
|
|
/** |
1559
|
|
|
* @dataProvider getToStringValues |
1560
|
|
|
*/ |
1561
|
|
|
public function testEditActionError($expectedToStringValue, $toStringValue) |
1562
|
|
|
{ |
1563
|
|
|
$object = new \stdClass(); |
1564
|
|
|
|
1565
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1566
|
|
|
->method('getObject') |
1567
|
|
|
->will($this->returnValue($object)); |
1568
|
|
|
|
1569
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1570
|
|
|
->method('checkAccess') |
1571
|
|
|
->with($this->equalTo('edit')) |
1572
|
|
|
->will($this->returnValue(true)); |
1573
|
|
|
|
1574
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1575
|
|
|
->disableOriginalConstructor() |
1576
|
|
|
->getMock(); |
1577
|
|
|
|
1578
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1579
|
|
|
->method('getForm') |
1580
|
|
|
->will($this->returnValue($form)); |
1581
|
|
|
|
1582
|
|
|
$form->expects($this->once()) |
1583
|
|
|
->method('isSubmitted') |
1584
|
|
|
->will($this->returnValue(true)); |
1585
|
|
|
|
1586
|
|
|
$form->expects($this->once()) |
1587
|
|
|
->method('isValid') |
1588
|
|
|
->will($this->returnValue(false)); |
1589
|
|
|
|
1590
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1591
|
|
|
->method('toString') |
1592
|
|
|
->with($this->equalTo($object)) |
1593
|
|
|
->will($this->returnValue($toStringValue)); |
1594
|
|
|
|
1595
|
|
|
$this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1596
|
|
|
|
1597
|
|
|
$this->request->setMethod('POST'); |
1598
|
|
|
|
1599
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
1600
|
|
|
|
1601
|
|
|
$form->expects($this->any()) |
1602
|
|
|
->method('createView') |
1603
|
|
|
->will($this->returnValue($formView)); |
1604
|
|
|
|
1605
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request)); |
|
|
|
|
1606
|
|
|
|
1607
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1608
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1609
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1610
|
|
|
|
1611
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1612
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
1613
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1614
|
|
|
|
1615
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
1616
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
1617
|
|
|
} |
1618
|
|
|
|
1619
|
|
|
public function testEditActionAjaxSuccess() |
1620
|
|
|
{ |
1621
|
|
|
$object = new \stdClass(); |
1622
|
|
|
|
1623
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1624
|
|
|
->method('getObject') |
1625
|
|
|
->will($this->returnValue($object)); |
1626
|
|
|
|
1627
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1628
|
|
|
->method('update') |
1629
|
|
|
->will($this->returnArgument(0)); |
1630
|
|
|
|
1631
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1632
|
|
|
->method('checkAccess') |
1633
|
|
|
->with($this->equalTo('edit')) |
1634
|
|
|
->will($this->returnValue(true)); |
1635
|
|
|
|
1636
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1637
|
|
|
->disableOriginalConstructor() |
1638
|
|
|
->getMock(); |
1639
|
|
|
|
1640
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1641
|
|
|
->method('getForm') |
1642
|
|
|
->will($this->returnValue($form)); |
1643
|
|
|
|
1644
|
|
|
$form->expects($this->once()) |
1645
|
|
|
->method('isSubmitted') |
1646
|
|
|
->will($this->returnValue(true)); |
1647
|
|
|
|
1648
|
|
|
$form->expects($this->once()) |
1649
|
|
|
->method('isValid') |
1650
|
|
|
->will($this->returnValue(true)); |
1651
|
|
|
|
1652
|
|
|
$form->expects($this->once()) |
1653
|
|
|
->method('getData') |
1654
|
|
|
->will($this->returnValue($object)); |
1655
|
|
|
|
1656
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1657
|
|
|
->method('getNormalizedIdentifier') |
1658
|
|
|
->with($this->equalTo($object)) |
1659
|
|
|
->will($this->returnValue('foo_normalized')); |
1660
|
|
|
|
1661
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1662
|
|
|
->method('toString') |
1663
|
|
|
->will($this->returnValue('foo')); |
1664
|
|
|
|
1665
|
|
|
$this->request->setMethod('POST'); |
1666
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1667
|
|
|
|
1668
|
|
|
$response = $this->controller->editAction(null, $this->request); |
|
|
|
|
1669
|
|
|
|
1670
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
1671
|
|
|
$this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent()); |
1672
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1673
|
|
|
} |
1674
|
|
|
|
1675
|
|
|
public function testEditActionAjaxError() |
1676
|
|
|
{ |
1677
|
|
|
$object = new \stdClass(); |
1678
|
|
|
|
1679
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1680
|
|
|
->method('getObject') |
1681
|
|
|
->will($this->returnValue($object)); |
1682
|
|
|
|
1683
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1684
|
|
|
->method('checkAccess') |
1685
|
|
|
->with($this->equalTo('edit')) |
1686
|
|
|
->will($this->returnValue(true)); |
1687
|
|
|
|
1688
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1689
|
|
|
->disableOriginalConstructor() |
1690
|
|
|
->getMock(); |
1691
|
|
|
|
1692
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1693
|
|
|
->method('getForm') |
1694
|
|
|
->will($this->returnValue($form)); |
1695
|
|
|
|
1696
|
|
|
$form->expects($this->once()) |
1697
|
|
|
->method('isSubmitted') |
1698
|
|
|
->will($this->returnValue(true)); |
1699
|
|
|
|
1700
|
|
|
$form->expects($this->once()) |
1701
|
|
|
->method('isValid') |
1702
|
|
|
->will($this->returnValue(false)); |
1703
|
|
|
|
1704
|
|
|
$this->request->setMethod('POST'); |
1705
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
1706
|
|
|
|
1707
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
1708
|
|
|
|
1709
|
|
|
$form->expects($this->any()) |
1710
|
|
|
->method('createView') |
1711
|
|
|
->will($this->returnValue($formView)); |
1712
|
|
|
|
1713
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request)); |
|
|
|
|
1714
|
|
|
|
1715
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1716
|
|
|
$this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']); |
1717
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1718
|
|
|
|
1719
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1720
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
1721
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1722
|
|
|
|
1723
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1724
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
1725
|
|
|
} |
1726
|
|
|
|
1727
|
|
|
/** |
1728
|
|
|
* @dataProvider getToStringValues |
1729
|
|
|
*/ |
1730
|
|
|
public function testEditActionWithModelManagerException($expectedToStringValue, $toStringValue) |
1731
|
|
|
{ |
1732
|
|
|
$object = new \stdClass(); |
1733
|
|
|
|
1734
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1735
|
|
|
->method('getObject') |
1736
|
|
|
->will($this->returnValue($object)); |
1737
|
|
|
|
1738
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1739
|
|
|
->method('checkAccess') |
1740
|
|
|
->with($this->equalTo('edit')) |
1741
|
|
|
->will($this->returnValue(true)); |
1742
|
|
|
|
1743
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1744
|
|
|
->method('getClass') |
1745
|
|
|
->will($this->returnValue('stdClass')); |
1746
|
|
|
|
1747
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1748
|
|
|
->disableOriginalConstructor() |
1749
|
|
|
->getMock(); |
1750
|
|
|
|
1751
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1752
|
|
|
->method('getForm') |
1753
|
|
|
->will($this->returnValue($form)); |
1754
|
|
|
|
1755
|
|
|
$form->expects($this->once()) |
1756
|
|
|
->method('isValid') |
1757
|
|
|
->will($this->returnValue(true)); |
1758
|
|
|
|
1759
|
|
|
$form->expects($this->once()) |
1760
|
|
|
->method('getData') |
1761
|
|
|
->will($this->returnValue($object)); |
1762
|
|
|
|
1763
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1764
|
|
|
->method('toString') |
1765
|
|
|
->with($this->equalTo($object)) |
1766
|
|
|
->will($this->returnValue($toStringValue)); |
1767
|
|
|
|
1768
|
|
|
$this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
1769
|
|
|
|
1770
|
|
|
$form->expects($this->once()) |
1771
|
|
|
->method('isSubmitted') |
1772
|
|
|
->will($this->returnValue(true)); |
1773
|
|
|
$this->request->setMethod('POST'); |
1774
|
|
|
|
1775
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
1776
|
|
|
|
1777
|
|
|
$form->expects($this->any()) |
1778
|
|
|
->method('createView') |
1779
|
|
|
->will($this->returnValue($formView)); |
1780
|
|
|
|
1781
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'update'); |
1782
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request)); |
|
|
|
|
1783
|
|
|
|
1784
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1785
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1786
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1787
|
|
|
|
1788
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1789
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
1790
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1791
|
|
|
|
1792
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all()); |
1793
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
1794
|
|
|
} |
1795
|
|
|
|
1796
|
|
|
public function testEditActionWithPreview() |
1797
|
|
|
{ |
1798
|
|
|
$object = new \stdClass(); |
1799
|
|
|
|
1800
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1801
|
|
|
->method('getObject') |
1802
|
|
|
->will($this->returnValue($object)); |
1803
|
|
|
|
1804
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1805
|
|
|
->method('checkAccess') |
1806
|
|
|
->with($this->equalTo('edit')) |
1807
|
|
|
->will($this->returnValue(true)); |
1808
|
|
|
|
1809
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1810
|
|
|
->disableOriginalConstructor() |
1811
|
|
|
->getMock(); |
1812
|
|
|
|
1813
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1814
|
|
|
->method('getForm') |
1815
|
|
|
->will($this->returnValue($form)); |
1816
|
|
|
|
1817
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1818
|
|
|
->method('supportsPreviewMode') |
1819
|
|
|
->will($this->returnValue(true)); |
1820
|
|
|
|
1821
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
1822
|
|
|
|
1823
|
|
|
$form->expects($this->any()) |
1824
|
|
|
->method('createView') |
1825
|
|
|
->will($this->returnValue($formView)); |
1826
|
|
|
|
1827
|
|
|
$form->expects($this->once()) |
1828
|
|
|
->method('isSubmitted') |
1829
|
|
|
->will($this->returnValue(true)); |
1830
|
|
|
|
1831
|
|
|
$form->expects($this->once()) |
1832
|
|
|
->method('isValid') |
1833
|
|
|
->will($this->returnValue(true)); |
1834
|
|
|
|
1835
|
|
|
$this->request->setMethod('POST'); |
1836
|
|
|
$this->request->request->set('btn_preview', 'Preview'); |
1837
|
|
|
|
1838
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request)); |
|
|
|
|
1839
|
|
|
|
1840
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1841
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1842
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1843
|
|
|
|
1844
|
|
|
$this->assertSame('edit', $this->parameters['action']); |
1845
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
1846
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1847
|
|
|
|
1848
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1849
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:preview.html.twig', $this->template); |
1850
|
|
|
} |
1851
|
|
|
|
1852
|
|
|
public function testEditActionWithLockException() |
1853
|
|
|
{ |
1854
|
|
|
$object = new \stdClass(); |
1855
|
|
|
$class = get_class($object); |
1856
|
|
|
|
1857
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1858
|
|
|
->method('getObject') |
1859
|
|
|
->will($this->returnValue($object)); |
1860
|
|
|
|
1861
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1862
|
|
|
->method('checkAccess') |
1863
|
|
|
->with($this->equalTo('edit')) |
1864
|
|
|
->will($this->returnValue(true)); |
1865
|
|
|
|
1866
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1867
|
|
|
->method('getClass') |
1868
|
|
|
->will($this->returnValue($class)); |
1869
|
|
|
|
1870
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1871
|
|
|
->disableOriginalConstructor() |
1872
|
|
|
->getMock(); |
1873
|
|
|
|
1874
|
|
|
$form->expects($this->any()) |
1875
|
|
|
->method('isValid') |
1876
|
|
|
->will($this->returnValue(true)); |
1877
|
|
|
|
1878
|
|
|
$form->expects($this->once()) |
1879
|
|
|
->method('getData') |
1880
|
|
|
->will($this->returnValue($object)); |
1881
|
|
|
|
1882
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1883
|
|
|
->method('getForm') |
1884
|
|
|
->will($this->returnValue($form)); |
1885
|
|
|
|
1886
|
|
|
$form->expects($this->any()) |
1887
|
|
|
->method('isSubmitted') |
1888
|
|
|
->will($this->returnValue(true)); |
1889
|
|
|
$this->request->setMethod('POST'); |
1890
|
|
|
|
1891
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1892
|
|
|
->method('update') |
1893
|
|
|
->will($this->throwException(new LockException())); |
1894
|
|
|
|
1895
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1896
|
|
|
->method('toString') |
1897
|
|
|
->with($this->equalTo($object)) |
1898
|
|
|
->will($this->returnValue($class)); |
1899
|
|
|
|
1900
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
1901
|
|
|
|
1902
|
|
|
$form->expects($this->any()) |
1903
|
|
|
->method('createView') |
1904
|
|
|
->will($this->returnValue($formView)); |
1905
|
|
|
|
1906
|
|
|
$this->expectTranslate('flash_lock_error', [ |
1907
|
|
|
'%name%' => $class, |
1908
|
|
|
'%link_start%' => '<a href="stdClass_edit">', |
1909
|
|
|
'%link_end%' => '</a>', |
1910
|
|
|
], 'SonataAdminBundle'); |
1911
|
|
|
|
1912
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request)); |
|
|
|
|
1913
|
|
|
} |
1914
|
|
|
|
1915
|
|
|
public function testCreateActionAccessDenied() |
1916
|
|
|
{ |
1917
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
1918
|
|
|
|
1919
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1920
|
|
|
->method('checkAccess') |
1921
|
|
|
->with($this->equalTo('create')) |
1922
|
|
|
->will($this->throwException(new AccessDeniedException())); |
1923
|
|
|
|
1924
|
|
|
$this->controller->createAction($this->request); |
|
|
|
|
1925
|
|
|
} |
1926
|
|
|
|
1927
|
|
|
public function testPreCreate() |
1928
|
|
|
{ |
1929
|
|
|
$object = new \stdClass(); |
1930
|
|
|
$object->foo = 123456; |
1931
|
|
|
|
1932
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1933
|
|
|
->method('checkAccess') |
1934
|
|
|
->with($this->equalTo('create')) |
1935
|
|
|
->will($this->returnValue(true)); |
1936
|
|
|
|
1937
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1938
|
|
|
->method('getClass') |
1939
|
|
|
->will($this->returnValue('stdClass')); |
1940
|
|
|
|
1941
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1942
|
|
|
->method('getNewInstance') |
1943
|
|
|
->will($this->returnValue($object)); |
1944
|
|
|
|
1945
|
|
|
$controller = new PreCRUDController(); |
1946
|
|
|
$controller->setContainer($this->container); |
1947
|
|
|
|
1948
|
|
|
$response = $controller->createAction($this->request); |
|
|
|
|
1949
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
1950
|
|
|
$this->assertSame('preCreate called: 123456', $response->getContent()); |
1951
|
|
|
} |
1952
|
|
|
|
1953
|
|
|
public function testCreateAction() |
1954
|
|
|
{ |
1955
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1956
|
|
|
->method('checkAccess') |
1957
|
|
|
->with($this->equalTo('create')) |
1958
|
|
|
->will($this->returnValue(true)); |
1959
|
|
|
|
1960
|
|
|
$object = new \stdClass(); |
1961
|
|
|
|
1962
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
1963
|
|
|
->method('getClass') |
1964
|
|
|
->will($this->returnValue('stdClass')); |
1965
|
|
|
|
1966
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1967
|
|
|
->method('getNewInstance') |
1968
|
|
|
->will($this->returnValue($object)); |
1969
|
|
|
|
1970
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
1971
|
|
|
->disableOriginalConstructor() |
1972
|
|
|
->getMock(); |
1973
|
|
|
|
1974
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
1975
|
|
|
->method('getForm') |
1976
|
|
|
->will($this->returnValue($form)); |
1977
|
|
|
|
1978
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
1979
|
|
|
|
1980
|
|
|
$form->expects($this->any()) |
1981
|
|
|
->method('createView') |
1982
|
|
|
->will($this->returnValue($formView)); |
1983
|
|
|
|
1984
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request)); |
|
|
|
|
1985
|
|
|
|
1986
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
1987
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
1988
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
1989
|
|
|
|
1990
|
|
|
$this->assertSame('create', $this->parameters['action']); |
1991
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
1992
|
|
|
$this->assertSame($object, $this->parameters['object']); |
1993
|
|
|
|
1994
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
1995
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
1996
|
|
|
} |
1997
|
|
|
|
1998
|
|
|
/** |
1999
|
|
|
* @dataProvider getToStringValues |
2000
|
|
|
*/ |
2001
|
|
|
public function testCreateActionSuccess($expectedToStringValue, $toStringValue) |
2002
|
|
|
{ |
2003
|
|
|
$object = new \stdClass(); |
2004
|
|
|
|
2005
|
|
|
$this->admin->expects($this->exactly(2)) |
|
|
|
|
2006
|
|
|
->method('checkAccess') |
2007
|
|
|
->will($this->returnCallback(function ($name, $objectIn = null) use ($object) { |
2008
|
|
|
if ($name == 'edit') { |
2009
|
|
|
return true; |
2010
|
|
|
} |
2011
|
|
|
|
2012
|
|
|
if ($name != 'create') { |
2013
|
|
|
return false; |
2014
|
|
|
} |
2015
|
|
|
|
2016
|
|
|
if ($objectIn === null) { |
2017
|
|
|
return true; |
2018
|
|
|
} |
2019
|
|
|
|
2020
|
|
|
return $objectIn === $object; |
2021
|
|
|
})); |
2022
|
|
|
|
2023
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2024
|
|
|
->method('hasRoute') |
2025
|
|
|
->with($this->equalTo('edit')) |
2026
|
|
|
->will($this->returnValue(true)); |
2027
|
|
|
|
2028
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2029
|
|
|
->method('hasAccess') |
2030
|
|
|
->with($this->equalTo('edit')) |
2031
|
|
|
->will($this->returnValue(true)); |
2032
|
|
|
|
2033
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2034
|
|
|
->method('getNewInstance') |
2035
|
|
|
->will($this->returnValue($object)); |
2036
|
|
|
|
2037
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2038
|
|
|
->method('create') |
2039
|
|
|
->will($this->returnArgument(0)); |
2040
|
|
|
|
2041
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
2042
|
|
|
->disableOriginalConstructor() |
2043
|
|
|
->getMock(); |
2044
|
|
|
|
2045
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2046
|
|
|
->method('getClass') |
2047
|
|
|
->will($this->returnValue('stdClass')); |
2048
|
|
|
|
2049
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2050
|
|
|
->method('getForm') |
2051
|
|
|
->will($this->returnValue($form)); |
2052
|
|
|
|
2053
|
|
|
$form->expects($this->once()) |
2054
|
|
|
->method('isSubmitted') |
2055
|
|
|
->will($this->returnValue(true)); |
2056
|
|
|
|
2057
|
|
|
$form->expects($this->once()) |
2058
|
|
|
->method('isValid') |
2059
|
|
|
->will($this->returnValue(true)); |
2060
|
|
|
|
2061
|
|
|
$form->expects($this->once()) |
2062
|
|
|
->method('getData') |
2063
|
|
|
->will($this->returnValue($object)); |
2064
|
|
|
|
2065
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2066
|
|
|
->method('toString') |
2067
|
|
|
->with($this->equalTo($object)) |
2068
|
|
|
->will($this->returnValue($toStringValue)); |
2069
|
|
|
|
2070
|
|
|
$this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
2071
|
|
|
|
2072
|
|
|
$this->request->setMethod('POST'); |
2073
|
|
|
|
2074
|
|
|
$response = $this->controller->createAction($this->request); |
|
|
|
|
2075
|
|
|
|
2076
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
2077
|
|
|
$this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
2078
|
|
|
$this->assertSame('stdClass_edit', $response->getTargetUrl()); |
2079
|
|
|
} |
2080
|
|
|
|
2081
|
|
|
public function testCreateActionAccessDenied2() |
2082
|
|
|
{ |
2083
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
2084
|
|
|
|
2085
|
|
|
$object = new \stdClass(); |
2086
|
|
|
|
2087
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2088
|
|
|
->method('checkAccess') |
2089
|
|
|
->will($this->returnCallback(function ($name, $object = null) { |
2090
|
|
|
if ($name != 'create') { |
2091
|
|
|
throw new AccessDeniedException(); |
2092
|
|
|
} |
2093
|
|
|
if ($object === null) { |
2094
|
|
|
return true; |
2095
|
|
|
} |
2096
|
|
|
|
2097
|
|
|
throw new AccessDeniedException(); |
2098
|
|
|
})); |
2099
|
|
|
|
2100
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2101
|
|
|
->method('getNewInstance') |
2102
|
|
|
->will($this->returnValue($object)); |
2103
|
|
|
|
2104
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
2105
|
|
|
->disableOriginalConstructor() |
2106
|
|
|
->getMock(); |
2107
|
|
|
|
2108
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2109
|
|
|
->method('getClass') |
2110
|
|
|
->will($this->returnValue('stdClass')); |
2111
|
|
|
|
2112
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2113
|
|
|
->method('getForm') |
2114
|
|
|
->will($this->returnValue($form)); |
2115
|
|
|
|
2116
|
|
|
$form->expects($this->once()) |
2117
|
|
|
->method('isSubmitted') |
2118
|
|
|
->will($this->returnValue(true)); |
2119
|
|
|
|
2120
|
|
|
$form->expects($this->once()) |
2121
|
|
|
->method('getData') |
2122
|
|
|
->will($this->returnValue($object)); |
2123
|
|
|
|
2124
|
|
|
$form->expects($this->once()) |
2125
|
|
|
->method('isValid') |
2126
|
|
|
->will($this->returnValue(true)); |
2127
|
|
|
|
2128
|
|
|
$this->request->setMethod('POST'); |
2129
|
|
|
|
2130
|
|
|
$this->controller->createAction($this->request); |
|
|
|
|
2131
|
|
|
} |
2132
|
|
|
|
2133
|
|
|
/** |
2134
|
|
|
* @dataProvider getToStringValues |
2135
|
|
|
*/ |
2136
|
|
|
public function testCreateActionError($expectedToStringValue, $toStringValue) |
2137
|
|
|
{ |
2138
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2139
|
|
|
->method('checkAccess') |
2140
|
|
|
->with($this->equalTo('create')) |
2141
|
|
|
->will($this->returnValue(true)); |
2142
|
|
|
|
2143
|
|
|
$object = new \stdClass(); |
2144
|
|
|
|
2145
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2146
|
|
|
->method('getClass') |
2147
|
|
|
->will($this->returnValue('stdClass')); |
2148
|
|
|
|
2149
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2150
|
|
|
->method('getNewInstance') |
2151
|
|
|
->will($this->returnValue($object)); |
2152
|
|
|
|
2153
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
2154
|
|
|
->disableOriginalConstructor() |
2155
|
|
|
->getMock(); |
2156
|
|
|
|
2157
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2158
|
|
|
->method('getForm') |
2159
|
|
|
->will($this->returnValue($form)); |
2160
|
|
|
|
2161
|
|
|
$form->expects($this->once()) |
2162
|
|
|
->method('isSubmitted') |
2163
|
|
|
->will($this->returnValue(true)); |
2164
|
|
|
|
2165
|
|
|
$form->expects($this->once()) |
2166
|
|
|
->method('isValid') |
2167
|
|
|
->will($this->returnValue(false)); |
2168
|
|
|
|
2169
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2170
|
|
|
->method('toString') |
2171
|
|
|
->with($this->equalTo($object)) |
2172
|
|
|
->will($this->returnValue($toStringValue)); |
2173
|
|
|
|
2174
|
|
|
$this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
2175
|
|
|
|
2176
|
|
|
$this->request->setMethod('POST'); |
2177
|
|
|
|
2178
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
2179
|
|
|
|
2180
|
|
|
$form->expects($this->any()) |
2181
|
|
|
->method('createView') |
2182
|
|
|
->will($this->returnValue($formView)); |
2183
|
|
|
|
2184
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request)); |
|
|
|
|
2185
|
|
|
|
2186
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2187
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
2188
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2189
|
|
|
|
2190
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2191
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
2192
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2193
|
|
|
|
2194
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
2195
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
2196
|
|
|
} |
2197
|
|
|
|
2198
|
|
|
/** |
2199
|
|
|
* @dataProvider getToStringValues |
2200
|
|
|
*/ |
2201
|
|
|
public function testCreateActionWithModelManagerException($expectedToStringValue, $toStringValue) |
2202
|
|
|
{ |
2203
|
|
|
$this->admin->expects($this->exactly(2)) |
|
|
|
|
2204
|
|
|
->method('checkAccess') |
2205
|
|
|
->with($this->equalTo('create')) |
2206
|
|
|
->will($this->returnValue(true)); |
2207
|
|
|
|
2208
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2209
|
|
|
->method('getClass') |
2210
|
|
|
->will($this->returnValue('stdClass')); |
2211
|
|
|
|
2212
|
|
|
$object = new \stdClass(); |
2213
|
|
|
|
2214
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2215
|
|
|
->method('getNewInstance') |
2216
|
|
|
->will($this->returnValue($object)); |
2217
|
|
|
|
2218
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
2219
|
|
|
->disableOriginalConstructor() |
2220
|
|
|
->getMock(); |
2221
|
|
|
|
2222
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2223
|
|
|
->method('getForm') |
2224
|
|
|
->will($this->returnValue($form)); |
2225
|
|
|
|
2226
|
|
|
$form->expects($this->once()) |
2227
|
|
|
->method('isValid') |
2228
|
|
|
->will($this->returnValue(true)); |
2229
|
|
|
|
2230
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2231
|
|
|
->method('toString') |
2232
|
|
|
->with($this->equalTo($object)) |
2233
|
|
|
->will($this->returnValue($toStringValue)); |
2234
|
|
|
|
2235
|
|
|
$this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle'); |
2236
|
|
|
|
2237
|
|
|
$form->expects($this->once()) |
2238
|
|
|
->method('isSubmitted') |
2239
|
|
|
->will($this->returnValue(true)); |
2240
|
|
|
|
2241
|
|
|
$form->expects($this->once()) |
2242
|
|
|
->method('getData') |
2243
|
|
|
->will($this->returnValue($object)); |
2244
|
|
|
|
2245
|
|
|
$this->request->setMethod('POST'); |
2246
|
|
|
|
2247
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
2248
|
|
|
|
2249
|
|
|
$form->expects($this->any()) |
2250
|
|
|
->method('createView') |
2251
|
|
|
->will($this->returnValue($formView)); |
2252
|
|
|
|
2253
|
|
|
$this->assertLoggerLogsModelManagerException($this->admin, 'create'); |
2254
|
|
|
|
2255
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request)); |
|
|
|
|
2256
|
|
|
|
2257
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2258
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
2259
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2260
|
|
|
|
2261
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2262
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
2263
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2264
|
|
|
|
2265
|
|
|
$this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all()); |
2266
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
2267
|
|
|
} |
2268
|
|
|
|
2269
|
|
|
public function testCreateActionAjaxSuccess() |
2270
|
|
|
{ |
2271
|
|
|
$object = new \stdClass(); |
2272
|
|
|
|
2273
|
|
|
$this->admin->expects($this->exactly(2)) |
|
|
|
|
2274
|
|
|
->method('checkAccess') |
2275
|
|
|
->will($this->returnCallback(function ($name, $objectIn = null) use ($object) { |
2276
|
|
|
if ($name != 'create') { |
2277
|
|
|
return false; |
2278
|
|
|
} |
2279
|
|
|
|
2280
|
|
|
if ($objectIn === null) { |
2281
|
|
|
return true; |
2282
|
|
|
} |
2283
|
|
|
|
2284
|
|
|
return $objectIn === $object; |
2285
|
|
|
})); |
2286
|
|
|
|
2287
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2288
|
|
|
->method('getNewInstance') |
2289
|
|
|
->will($this->returnValue($object)); |
2290
|
|
|
|
2291
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2292
|
|
|
->method('create') |
2293
|
|
|
->will($this->returnArgument(0)); |
2294
|
|
|
|
2295
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
2296
|
|
|
->disableOriginalConstructor() |
2297
|
|
|
->getMock(); |
2298
|
|
|
|
2299
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2300
|
|
|
->method('getForm') |
2301
|
|
|
->will($this->returnValue($form)); |
2302
|
|
|
|
2303
|
|
|
$form->expects($this->once()) |
2304
|
|
|
->method('isSubmitted') |
2305
|
|
|
->will($this->returnValue(true)); |
2306
|
|
|
|
2307
|
|
|
$form->expects($this->once()) |
2308
|
|
|
->method('isValid') |
2309
|
|
|
->will($this->returnValue(true)); |
2310
|
|
|
|
2311
|
|
|
$form->expects($this->once()) |
2312
|
|
|
->method('getData') |
2313
|
|
|
->will($this->returnValue($object)); |
2314
|
|
|
|
2315
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2316
|
|
|
->method('getClass') |
2317
|
|
|
->will($this->returnValue('stdClass')); |
2318
|
|
|
|
2319
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2320
|
|
|
->method('getNormalizedIdentifier') |
2321
|
|
|
->with($this->equalTo($object)) |
2322
|
|
|
->will($this->returnValue('foo_normalized')); |
2323
|
|
|
|
2324
|
|
|
$this->request->setMethod('POST'); |
2325
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
2326
|
|
|
|
2327
|
|
|
$response = $this->controller->createAction($this->request); |
|
|
|
|
2328
|
|
|
|
2329
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
2330
|
|
|
$this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized']), $response->getContent()); |
2331
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2332
|
|
|
} |
2333
|
|
|
|
2334
|
|
|
public function testCreateActionAjaxError() |
2335
|
|
|
{ |
2336
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2337
|
|
|
->method('checkAccess') |
2338
|
|
|
->with($this->equalTo('create')) |
2339
|
|
|
->will($this->returnValue(true)); |
2340
|
|
|
|
2341
|
|
|
$object = new \stdClass(); |
2342
|
|
|
|
2343
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2344
|
|
|
->method('getNewInstance') |
2345
|
|
|
->will($this->returnValue($object)); |
2346
|
|
|
|
2347
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
2348
|
|
|
->disableOriginalConstructor() |
2349
|
|
|
->getMock(); |
2350
|
|
|
|
2351
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2352
|
|
|
->method('getClass') |
2353
|
|
|
->will($this->returnValue('stdClass')); |
2354
|
|
|
|
2355
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2356
|
|
|
->method('getForm') |
2357
|
|
|
->will($this->returnValue($form)); |
2358
|
|
|
|
2359
|
|
|
$form->expects($this->once()) |
2360
|
|
|
->method('isSubmitted') |
2361
|
|
|
->will($this->returnValue(true)); |
2362
|
|
|
|
2363
|
|
|
$form->expects($this->once()) |
2364
|
|
|
->method('isValid') |
2365
|
|
|
->will($this->returnValue(false)); |
2366
|
|
|
|
2367
|
|
|
$this->request->setMethod('POST'); |
2368
|
|
|
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
2369
|
|
|
|
2370
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
2371
|
|
|
|
2372
|
|
|
$form->expects($this->any()) |
2373
|
|
|
->method('createView') |
2374
|
|
|
->will($this->returnValue($formView)); |
2375
|
|
|
|
2376
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request)); |
|
|
|
|
2377
|
|
|
|
2378
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2379
|
|
|
$this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']); |
2380
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2381
|
|
|
|
2382
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2383
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
2384
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2385
|
|
|
|
2386
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2387
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template); |
2388
|
|
|
} |
2389
|
|
|
|
2390
|
|
|
public function testCreateActionWithPreview() |
2391
|
|
|
{ |
2392
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2393
|
|
|
->method('checkAccess') |
2394
|
|
|
->with($this->equalTo('create')) |
2395
|
|
|
->will($this->returnValue(true)); |
2396
|
|
|
|
2397
|
|
|
$object = new \stdClass(); |
2398
|
|
|
|
2399
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2400
|
|
|
->method('getNewInstance') |
2401
|
|
|
->will($this->returnValue($object)); |
2402
|
|
|
|
2403
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
2404
|
|
|
->disableOriginalConstructor() |
2405
|
|
|
->getMock(); |
2406
|
|
|
|
2407
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2408
|
|
|
->method('getClass') |
2409
|
|
|
->will($this->returnValue('stdClass')); |
2410
|
|
|
|
2411
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2412
|
|
|
->method('getForm') |
2413
|
|
|
->will($this->returnValue($form)); |
2414
|
|
|
|
2415
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2416
|
|
|
->method('supportsPreviewMode') |
2417
|
|
|
->will($this->returnValue(true)); |
2418
|
|
|
|
2419
|
|
|
$formView = $this->createMock('Symfony\Component\Form\FormView'); |
2420
|
|
|
|
2421
|
|
|
$form->expects($this->any()) |
2422
|
|
|
->method('createView') |
2423
|
|
|
->will($this->returnValue($formView)); |
2424
|
|
|
|
2425
|
|
|
$form->expects($this->once()) |
2426
|
|
|
->method('isSubmitted') |
2427
|
|
|
->will($this->returnValue(true)); |
2428
|
|
|
|
2429
|
|
|
$form->expects($this->once()) |
2430
|
|
|
->method('isValid') |
2431
|
|
|
->will($this->returnValue(true)); |
2432
|
|
|
|
2433
|
|
|
$this->request->setMethod('POST'); |
2434
|
|
|
$this->request->request->set('btn_preview', 'Preview'); |
2435
|
|
|
|
2436
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request)); |
|
|
|
|
2437
|
|
|
|
2438
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2439
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
2440
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2441
|
|
|
|
2442
|
|
|
$this->assertSame('create', $this->parameters['action']); |
2443
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
2444
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2445
|
|
|
|
2446
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2447
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:preview.html.twig', $this->template); |
2448
|
|
|
} |
2449
|
|
|
|
2450
|
|
|
public function testExportActionAccessDenied() |
2451
|
|
|
{ |
2452
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
2453
|
|
|
|
2454
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2455
|
|
|
->method('checkAccess') |
2456
|
|
|
->with($this->equalTo('export')) |
2457
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2458
|
|
|
|
2459
|
|
|
$this->controller->exportAction($this->request); |
2460
|
|
|
} |
2461
|
|
|
|
2462
|
|
|
public function testExportActionWrongFormat() |
2463
|
|
|
{ |
2464
|
|
|
$this->expectException('RuntimeException', 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`'); |
2465
|
|
|
|
2466
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2467
|
|
|
->method('checkAccess') |
2468
|
|
|
->with($this->equalTo('export')) |
2469
|
|
|
->will($this->returnValue(true)); |
2470
|
|
|
|
2471
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2472
|
|
|
->method('getExportFormats') |
2473
|
|
|
->will($this->returnValue(['json'])); |
2474
|
|
|
|
2475
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2476
|
|
|
->method('getClass') |
2477
|
|
|
->will($this->returnValue('Foo')); |
2478
|
|
|
|
2479
|
|
|
$this->request->query->set('format', 'csv'); |
2480
|
|
|
|
2481
|
|
|
$this->controller->exportAction($this->request); |
2482
|
|
|
} |
2483
|
|
|
|
2484
|
|
|
public function testExportAction() |
2485
|
|
|
{ |
2486
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2487
|
|
|
->method('checkAccess') |
2488
|
|
|
->with($this->equalTo('export')) |
2489
|
|
|
->will($this->returnValue(true)); |
2490
|
|
|
|
2491
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2492
|
|
|
->method('getExportFormats') |
2493
|
|
|
->will($this->returnValue(['json'])); |
2494
|
|
|
|
2495
|
|
|
$dataSourceIterator = $this->createMock('Exporter\Source\SourceIteratorInterface'); |
2496
|
|
|
|
2497
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2498
|
|
|
->method('getDataSourceIterator') |
2499
|
|
|
->will($this->returnValue($dataSourceIterator)); |
2500
|
|
|
|
2501
|
|
|
$this->request->query->set('format', 'json'); |
2502
|
|
|
|
2503
|
|
|
$response = $this->controller->exportAction($this->request); |
2504
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response); |
2505
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
2506
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2507
|
|
|
} |
2508
|
|
|
|
2509
|
|
|
public function testHistoryActionAccessDenied() |
2510
|
|
|
{ |
2511
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
2512
|
|
|
|
2513
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2514
|
|
|
->method('getObject') |
2515
|
|
|
->will($this->returnValue(new \StdClass())); |
2516
|
|
|
|
2517
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2518
|
|
|
->method('checkAccess') |
2519
|
|
|
->with($this->equalTo('history')) |
2520
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2521
|
|
|
|
2522
|
|
|
$this->controller->historyAction(null, $this->request); |
|
|
|
|
2523
|
|
|
} |
2524
|
|
|
|
2525
|
|
|
public function testHistoryActionNotFoundException() |
2526
|
|
|
{ |
2527
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
2528
|
|
|
|
2529
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2530
|
|
|
->method('getObject') |
2531
|
|
|
->will($this->returnValue(false)); |
2532
|
|
|
|
2533
|
|
|
$this->controller->historyAction(null, $this->request); |
|
|
|
|
2534
|
|
|
} |
2535
|
|
|
|
2536
|
|
|
public function testHistoryActionNoReader() |
2537
|
|
|
{ |
2538
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo'); |
2539
|
|
|
|
2540
|
|
|
$this->request->query->set('id', 123); |
2541
|
|
|
|
2542
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2543
|
|
|
->method('checkAccess') |
2544
|
|
|
->with($this->equalTo('history')) |
2545
|
|
|
->will($this->returnValue(true)); |
2546
|
|
|
|
2547
|
|
|
$object = new \stdClass(); |
2548
|
|
|
|
2549
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2550
|
|
|
->method('getObject') |
2551
|
|
|
->will($this->returnValue($object)); |
2552
|
|
|
|
2553
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2554
|
|
|
->method('getClass') |
2555
|
|
|
->will($this->returnValue('Foo')); |
2556
|
|
|
|
2557
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2558
|
|
|
->method('hasReader') |
2559
|
|
|
->with($this->equalTo('Foo')) |
2560
|
|
|
->will($this->returnValue(false)); |
2561
|
|
|
|
2562
|
|
|
$this->controller->historyAction(null, $this->request); |
|
|
|
|
2563
|
|
|
} |
2564
|
|
|
|
2565
|
|
|
public function testHistoryAction() |
2566
|
|
|
{ |
2567
|
|
|
$this->request->query->set('id', 123); |
2568
|
|
|
|
2569
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2570
|
|
|
->method('checkAccess') |
2571
|
|
|
->with($this->equalTo('history')) |
2572
|
|
|
->will($this->returnValue(true)); |
2573
|
|
|
|
2574
|
|
|
$object = new \stdClass(); |
2575
|
|
|
|
2576
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2577
|
|
|
->method('getObject') |
2578
|
|
|
->will($this->returnValue($object)); |
2579
|
|
|
|
2580
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2581
|
|
|
->method('getClass') |
2582
|
|
|
->will($this->returnValue('Foo')); |
2583
|
|
|
|
2584
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2585
|
|
|
->method('hasReader') |
2586
|
|
|
->with($this->equalTo('Foo')) |
2587
|
|
|
->will($this->returnValue(true)); |
2588
|
|
|
|
2589
|
|
|
$reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface'); |
2590
|
|
|
|
2591
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2592
|
|
|
->method('getReader') |
2593
|
|
|
->with($this->equalTo('Foo')) |
2594
|
|
|
->will($this->returnValue($reader)); |
2595
|
|
|
|
2596
|
|
|
$reader->expects($this->once()) |
2597
|
|
|
->method('findRevisions') |
2598
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123)) |
2599
|
|
|
->will($this->returnValue([])); |
2600
|
|
|
|
2601
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyAction(null, $this->request)); |
|
|
|
|
2602
|
|
|
|
2603
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2604
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
2605
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2606
|
|
|
|
2607
|
|
|
$this->assertSame('history', $this->parameters['action']); |
2608
|
|
|
$this->assertSame([], $this->parameters['revisions']); |
2609
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2610
|
|
|
|
2611
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2612
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:history.html.twig', $this->template); |
2613
|
|
|
} |
2614
|
|
|
|
2615
|
|
|
public function testAclActionAclNotEnabled() |
2616
|
|
|
{ |
2617
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'ACL are not enabled for this admin'); |
2618
|
|
|
|
2619
|
|
|
$this->controller->aclAction(null, $this->request); |
|
|
|
|
2620
|
|
|
} |
2621
|
|
|
|
2622
|
|
|
public function testAclActionNotFoundException() |
2623
|
|
|
{ |
2624
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException'); |
2625
|
|
|
|
2626
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2627
|
|
|
->method('isAclEnabled') |
2628
|
|
|
->will($this->returnValue(true)); |
2629
|
|
|
|
2630
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2631
|
|
|
->method('getObject') |
2632
|
|
|
->will($this->returnValue(false)); |
2633
|
|
|
|
2634
|
|
|
$this->controller->aclAction(null, $this->request); |
|
|
|
|
2635
|
|
|
} |
2636
|
|
|
|
2637
|
|
|
public function testAclActionAccessDenied() |
2638
|
|
|
{ |
2639
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
2640
|
|
|
|
2641
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2642
|
|
|
->method('isAclEnabled') |
2643
|
|
|
->will($this->returnValue(true)); |
2644
|
|
|
|
2645
|
|
|
$object = new \stdClass(); |
2646
|
|
|
|
2647
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2648
|
|
|
->method('getObject') |
2649
|
|
|
->will($this->returnValue($object)); |
2650
|
|
|
|
2651
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2652
|
|
|
->method('checkAccess') |
2653
|
|
|
->with($this->equalTo('acl'), $this->equalTo($object)) |
2654
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2655
|
|
|
|
2656
|
|
|
$this->controller->aclAction(null, $this->request); |
|
|
|
|
2657
|
|
|
} |
2658
|
|
|
|
2659
|
|
|
public function testAclAction() |
2660
|
|
|
{ |
2661
|
|
|
$this->request->query->set('id', 123); |
2662
|
|
|
|
2663
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2664
|
|
|
->method('isAclEnabled') |
2665
|
|
|
->will($this->returnValue(true)); |
2666
|
|
|
|
2667
|
|
|
$object = new \stdClass(); |
2668
|
|
|
|
2669
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2670
|
|
|
->method('getObject') |
2671
|
|
|
->will($this->returnValue($object)); |
2672
|
|
|
|
2673
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2674
|
|
|
->method('checkAccess') |
2675
|
|
|
->will($this->returnValue(true)); |
2676
|
|
|
|
2677
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2678
|
|
|
->method('getSecurityInformation') |
2679
|
|
|
->will($this->returnValue([])); |
2680
|
|
|
|
2681
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2682
|
|
|
->method('getMaskBuilderClass') |
2683
|
|
|
->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap')); |
2684
|
|
|
|
2685
|
|
|
$aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form') |
2686
|
|
|
->disableOriginalConstructor() |
2687
|
|
|
->getMock(); |
2688
|
|
|
|
2689
|
|
|
$aclUsersForm->expects($this->once()) |
2690
|
|
|
->method('createView') |
2691
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
2692
|
|
|
|
2693
|
|
|
$aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form') |
2694
|
|
|
->disableOriginalConstructor() |
2695
|
|
|
->getMock(); |
2696
|
|
|
|
2697
|
|
|
$aclRolesForm->expects($this->once()) |
2698
|
|
|
->method('createView') |
2699
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
2700
|
|
|
|
2701
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2702
|
|
|
->method('createAclUsersForm') |
2703
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData')) |
2704
|
|
|
->will($this->returnValue($aclUsersForm)); |
2705
|
|
|
|
2706
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2707
|
|
|
->method('createAclRolesForm') |
2708
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData')) |
2709
|
|
|
->will($this->returnValue($aclRolesForm)); |
2710
|
|
|
|
2711
|
|
|
$aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler') |
2712
|
|
|
->disableOriginalConstructor() |
2713
|
|
|
->getMock(); |
2714
|
|
|
|
2715
|
|
|
$aclSecurityHandler->expects($this->any()) |
2716
|
|
|
->method('getObjectPermissions') |
2717
|
|
|
->will($this->returnValue([])); |
2718
|
|
|
|
2719
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2720
|
|
|
->method('getSecurityHandler') |
2721
|
|
|
->will($this->returnValue($aclSecurityHandler)); |
2722
|
|
|
|
2723
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction(null, $this->request)); |
|
|
|
|
2724
|
|
|
|
2725
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2726
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
2727
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2728
|
|
|
|
2729
|
|
|
$this->assertSame('acl', $this->parameters['action']); |
2730
|
|
|
$this->assertSame([], $this->parameters['permissions']); |
2731
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2732
|
|
|
$this->assertInstanceOf('\ArrayIterator', $this->parameters['users']); |
2733
|
|
|
$this->assertInstanceOf('\ArrayIterator', $this->parameters['roles']); |
2734
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclUsersForm']); |
2735
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclRolesForm']); |
2736
|
|
|
|
2737
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2738
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:acl.html.twig', $this->template); |
2739
|
|
|
} |
2740
|
|
|
|
2741
|
|
|
public function testAclActionInvalidUpdate() |
2742
|
|
|
{ |
2743
|
|
|
$this->request->query->set('id', 123); |
2744
|
|
|
$this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, []); |
2745
|
|
|
|
2746
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2747
|
|
|
->method('isAclEnabled') |
2748
|
|
|
->will($this->returnValue(true)); |
2749
|
|
|
|
2750
|
|
|
$object = new \stdClass(); |
2751
|
|
|
|
2752
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2753
|
|
|
->method('getObject') |
2754
|
|
|
->will($this->returnValue($object)); |
2755
|
|
|
|
2756
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2757
|
|
|
->method('checkAccess') |
2758
|
|
|
->will($this->returnValue(true)); |
2759
|
|
|
|
2760
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2761
|
|
|
->method('getSecurityInformation') |
2762
|
|
|
->will($this->returnValue([])); |
2763
|
|
|
|
2764
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2765
|
|
|
->method('getMaskBuilderClass') |
2766
|
|
|
->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap')); |
2767
|
|
|
|
2768
|
|
|
$aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form') |
2769
|
|
|
->disableOriginalConstructor() |
2770
|
|
|
->getMock(); |
2771
|
|
|
|
2772
|
|
|
$aclUsersForm->expects($this->once()) |
2773
|
|
|
->method('isValid') |
2774
|
|
|
->will($this->returnValue(false)); |
2775
|
|
|
|
2776
|
|
|
$aclUsersForm->expects($this->once()) |
2777
|
|
|
->method('createView') |
2778
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
2779
|
|
|
|
2780
|
|
|
$aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form') |
2781
|
|
|
->disableOriginalConstructor() |
2782
|
|
|
->getMock(); |
2783
|
|
|
|
2784
|
|
|
$aclRolesForm->expects($this->once()) |
2785
|
|
|
->method('createView') |
2786
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
2787
|
|
|
|
2788
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2789
|
|
|
->method('createAclUsersForm') |
2790
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData')) |
2791
|
|
|
->will($this->returnValue($aclUsersForm)); |
2792
|
|
|
|
2793
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2794
|
|
|
->method('createAclRolesForm') |
2795
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData')) |
2796
|
|
|
->will($this->returnValue($aclRolesForm)); |
2797
|
|
|
|
2798
|
|
|
$aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler') |
2799
|
|
|
->disableOriginalConstructor() |
2800
|
|
|
->getMock(); |
2801
|
|
|
|
2802
|
|
|
$aclSecurityHandler->expects($this->any()) |
2803
|
|
|
->method('getObjectPermissions') |
2804
|
|
|
->will($this->returnValue([])); |
2805
|
|
|
|
2806
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2807
|
|
|
->method('getSecurityHandler') |
2808
|
|
|
->will($this->returnValue($aclSecurityHandler)); |
2809
|
|
|
|
2810
|
|
|
$this->request->setMethod('POST'); |
2811
|
|
|
|
2812
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction(null, $this->request)); |
|
|
|
|
2813
|
|
|
|
2814
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
2815
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
2816
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
2817
|
|
|
|
2818
|
|
|
$this->assertSame('acl', $this->parameters['action']); |
2819
|
|
|
$this->assertSame([], $this->parameters['permissions']); |
2820
|
|
|
$this->assertSame($object, $this->parameters['object']); |
2821
|
|
|
$this->assertInstanceOf('\ArrayIterator', $this->parameters['users']); |
2822
|
|
|
$this->assertInstanceOf('\ArrayIterator', $this->parameters['roles']); |
2823
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclUsersForm']); |
2824
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclRolesForm']); |
2825
|
|
|
|
2826
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
2827
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:acl.html.twig', $this->template); |
2828
|
|
|
} |
2829
|
|
|
|
2830
|
|
|
public function testAclActionSuccessfulUpdate() |
2831
|
|
|
{ |
2832
|
|
|
$this->request->query->set('id', 123); |
2833
|
|
|
$this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, []); |
2834
|
|
|
|
2835
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2836
|
|
|
->method('isAclEnabled') |
2837
|
|
|
->will($this->returnValue(true)); |
2838
|
|
|
|
2839
|
|
|
$object = new \stdClass(); |
2840
|
|
|
|
2841
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2842
|
|
|
->method('getObject') |
2843
|
|
|
->will($this->returnValue($object)); |
2844
|
|
|
|
2845
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2846
|
|
|
->method('checkAccess') |
2847
|
|
|
->will($this->returnValue(true)); |
2848
|
|
|
|
2849
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2850
|
|
|
->method('getSecurityInformation') |
2851
|
|
|
->will($this->returnValue([])); |
2852
|
|
|
|
2853
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2854
|
|
|
->method('getMaskBuilderClass') |
2855
|
|
|
->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap')); |
2856
|
|
|
|
2857
|
|
|
$aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form') |
2858
|
|
|
->disableOriginalConstructor() |
2859
|
|
|
->getMock(); |
2860
|
|
|
|
2861
|
|
|
$aclUsersForm->expects($this->any()) |
2862
|
|
|
->method('createView') |
2863
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
2864
|
|
|
|
2865
|
|
|
$aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form') |
2866
|
|
|
->disableOriginalConstructor() |
2867
|
|
|
->getMock(); |
2868
|
|
|
|
2869
|
|
|
$aclRolesForm->expects($this->any()) |
2870
|
|
|
->method('createView') |
2871
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
2872
|
|
|
|
2873
|
|
|
$aclRolesForm->expects($this->once()) |
2874
|
|
|
->method('isValid') |
2875
|
|
|
->will($this->returnValue(true)); |
2876
|
|
|
|
2877
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2878
|
|
|
->method('createAclUsersForm') |
2879
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData')) |
2880
|
|
|
->will($this->returnValue($aclUsersForm)); |
2881
|
|
|
|
2882
|
|
|
$this->adminObjectAclManipulator->expects($this->once()) |
|
|
|
|
2883
|
|
|
->method('createAclRolesForm') |
2884
|
|
|
->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData')) |
2885
|
|
|
->will($this->returnValue($aclRolesForm)); |
2886
|
|
|
|
2887
|
|
|
$aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler') |
2888
|
|
|
->disableOriginalConstructor() |
2889
|
|
|
->getMock(); |
2890
|
|
|
|
2891
|
|
|
$aclSecurityHandler->expects($this->any()) |
2892
|
|
|
->method('getObjectPermissions') |
2893
|
|
|
->will($this->returnValue([])); |
2894
|
|
|
|
2895
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2896
|
|
|
->method('getSecurityHandler') |
2897
|
|
|
->will($this->returnValue($aclSecurityHandler)); |
2898
|
|
|
|
2899
|
|
|
$this->expectTranslate('flash_acl_edit_success', [], 'SonataAdminBundle'); |
2900
|
|
|
|
2901
|
|
|
$this->request->setMethod('POST'); |
2902
|
|
|
|
2903
|
|
|
$response = $this->controller->aclAction(null, $this->request); |
|
|
|
|
2904
|
|
|
|
2905
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); |
2906
|
|
|
|
2907
|
|
|
$this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
2908
|
|
|
$this->assertSame('stdClass_acl', $response->getTargetUrl()); |
2909
|
|
|
} |
2910
|
|
|
|
2911
|
|
|
public function testHistoryViewRevisionActionAccessDenied() |
2912
|
|
|
{ |
2913
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
2914
|
|
|
|
2915
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2916
|
|
|
->method('getObject') |
2917
|
|
|
->will($this->returnValue(new \StdClass())); |
2918
|
|
|
|
2919
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2920
|
|
|
->method('checkAccess') |
2921
|
|
|
->with($this->equalTo('historyViewRevision')) |
2922
|
|
|
->will($this->throwException(new AccessDeniedException())); |
2923
|
|
|
|
2924
|
|
|
$this->controller->historyViewRevisionAction(null, null, $this->request); |
|
|
|
|
2925
|
|
|
} |
2926
|
|
|
|
2927
|
|
|
public function testHistoryViewRevisionActionNotFoundException() |
2928
|
|
|
{ |
2929
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id: 123'); |
2930
|
|
|
|
2931
|
|
|
$this->request->query->set('id', 123); |
2932
|
|
|
|
2933
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2934
|
|
|
->method('getObject') |
2935
|
|
|
->will($this->returnValue(false)); |
2936
|
|
|
|
2937
|
|
|
$this->controller->historyViewRevisionAction(null, null, $this->request); |
|
|
|
|
2938
|
|
|
} |
2939
|
|
|
|
2940
|
|
|
public function testHistoryViewRevisionActionNoReader() |
2941
|
|
|
{ |
2942
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo'); |
2943
|
|
|
|
2944
|
|
|
$this->request->query->set('id', 123); |
2945
|
|
|
|
2946
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2947
|
|
|
->method('checkAccess') |
2948
|
|
|
->with($this->equalTo('historyViewRevision')) |
2949
|
|
|
->will($this->returnValue(true)); |
2950
|
|
|
|
2951
|
|
|
$object = new \stdClass(); |
2952
|
|
|
|
2953
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2954
|
|
|
->method('getObject') |
2955
|
|
|
->will($this->returnValue($object)); |
2956
|
|
|
|
2957
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2958
|
|
|
->method('getClass') |
2959
|
|
|
->will($this->returnValue('Foo')); |
2960
|
|
|
|
2961
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2962
|
|
|
->method('hasReader') |
2963
|
|
|
->with($this->equalTo('Foo')) |
2964
|
|
|
->will($this->returnValue(false)); |
2965
|
|
|
|
2966
|
|
|
$this->controller->historyViewRevisionAction(null, null, $this->request); |
|
|
|
|
2967
|
|
|
} |
2968
|
|
|
|
2969
|
|
|
public function testHistoryViewRevisionActionNotFoundRevision() |
2970
|
|
|
{ |
2971
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'); |
2972
|
|
|
|
2973
|
|
|
$this->request->query->set('id', 123); |
2974
|
|
|
|
2975
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2976
|
|
|
->method('checkAccess') |
2977
|
|
|
->with($this->equalTo('historyViewRevision')) |
2978
|
|
|
->will($this->returnValue(true)); |
2979
|
|
|
|
2980
|
|
|
$object = new \stdClass(); |
2981
|
|
|
|
2982
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
2983
|
|
|
->method('getObject') |
2984
|
|
|
->will($this->returnValue($object)); |
2985
|
|
|
|
2986
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
2987
|
|
|
->method('getClass') |
2988
|
|
|
->will($this->returnValue('Foo')); |
2989
|
|
|
|
2990
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2991
|
|
|
->method('hasReader') |
2992
|
|
|
->with($this->equalTo('Foo')) |
2993
|
|
|
->will($this->returnValue(true)); |
2994
|
|
|
|
2995
|
|
|
$reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface'); |
2996
|
|
|
|
2997
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
2998
|
|
|
->method('getReader') |
2999
|
|
|
->with($this->equalTo('Foo')) |
3000
|
|
|
->will($this->returnValue($reader)); |
3001
|
|
|
|
3002
|
|
|
$reader->expects($this->once()) |
3003
|
|
|
->method('find') |
3004
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3005
|
|
|
->will($this->returnValue(null)); |
3006
|
|
|
|
3007
|
|
|
$this->controller->historyViewRevisionAction(123, 456, $this->request); |
|
|
|
|
3008
|
|
|
} |
3009
|
|
|
|
3010
|
|
|
public function testHistoryViewRevisionAction() |
3011
|
|
|
{ |
3012
|
|
|
$this->request->query->set('id', 123); |
3013
|
|
|
|
3014
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3015
|
|
|
->method('checkAccess') |
3016
|
|
|
->with($this->equalTo('historyViewRevision')) |
3017
|
|
|
->will($this->returnValue(true)); |
3018
|
|
|
|
3019
|
|
|
$object = new \stdClass(); |
3020
|
|
|
|
3021
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3022
|
|
|
->method('getObject') |
3023
|
|
|
->will($this->returnValue($object)); |
3024
|
|
|
|
3025
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3026
|
|
|
->method('getClass') |
3027
|
|
|
->will($this->returnValue('Foo')); |
3028
|
|
|
|
3029
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3030
|
|
|
->method('hasReader') |
3031
|
|
|
->with($this->equalTo('Foo')) |
3032
|
|
|
->will($this->returnValue(true)); |
3033
|
|
|
|
3034
|
|
|
$reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface'); |
3035
|
|
|
|
3036
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3037
|
|
|
->method('getReader') |
3038
|
|
|
->with($this->equalTo('Foo')) |
3039
|
|
|
->will($this->returnValue($reader)); |
3040
|
|
|
|
3041
|
|
|
$objectRevision = new \stdClass(); |
3042
|
|
|
$objectRevision->revision = 456; |
3043
|
|
|
|
3044
|
|
|
$reader->expects($this->once()) |
3045
|
|
|
->method('find') |
3046
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3047
|
|
|
->will($this->returnValue($objectRevision)); |
3048
|
|
|
|
3049
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3050
|
|
|
->method('setSubject') |
3051
|
|
|
->with($this->equalTo($objectRevision)) |
3052
|
|
|
->will($this->returnValue(null)); |
3053
|
|
|
|
3054
|
|
|
$fieldDescriptionCollection = new FieldDescriptionCollection(); |
3055
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3056
|
|
|
->method('getShow') |
3057
|
|
|
->will($this->returnValue($fieldDescriptionCollection)); |
3058
|
|
|
|
3059
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyViewRevisionAction(123, 456, $this->request)); |
|
|
|
|
3060
|
|
|
|
3061
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
3062
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
3063
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
3064
|
|
|
|
3065
|
|
|
$this->assertSame('show', $this->parameters['action']); |
3066
|
|
|
$this->assertSame($objectRevision, $this->parameters['object']); |
3067
|
|
|
$this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
3068
|
|
|
|
3069
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
3070
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:show.html.twig', $this->template); |
3071
|
|
|
} |
3072
|
|
|
|
3073
|
|
|
public function testHistoryCompareRevisionsActionAccessDenied() |
3074
|
|
|
{ |
3075
|
|
|
$this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException'); |
3076
|
|
|
|
3077
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3078
|
|
|
->method('checkAccess') |
3079
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3080
|
|
|
->will($this->throwException(new AccessDeniedException())); |
3081
|
|
|
|
3082
|
|
|
$this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
|
|
|
|
3083
|
|
|
} |
3084
|
|
|
|
3085
|
|
|
public function testHistoryCompareRevisionsActionNotFoundException() |
3086
|
|
|
{ |
3087
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id: 123'); |
3088
|
|
|
|
3089
|
|
|
$this->request->query->set('id', 123); |
3090
|
|
|
|
3091
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3092
|
|
|
->method('checkAccess') |
3093
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3094
|
|
|
->will($this->returnValue(true)); |
3095
|
|
|
|
3096
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3097
|
|
|
->method('getObject') |
3098
|
|
|
->will($this->returnValue(false)); |
3099
|
|
|
|
3100
|
|
|
$this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
|
|
|
|
3101
|
|
|
} |
3102
|
|
|
|
3103
|
|
|
public function testHistoryCompareRevisionsActionNoReader() |
3104
|
|
|
{ |
3105
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo'); |
3106
|
|
|
|
3107
|
|
|
$this->request->query->set('id', 123); |
3108
|
|
|
|
3109
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3110
|
|
|
->method('checkAccess') |
3111
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3112
|
|
|
->will($this->returnValue(true)); |
3113
|
|
|
|
3114
|
|
|
$object = new \stdClass(); |
3115
|
|
|
|
3116
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3117
|
|
|
->method('getObject') |
3118
|
|
|
->will($this->returnValue($object)); |
3119
|
|
|
|
3120
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3121
|
|
|
->method('getClass') |
3122
|
|
|
->will($this->returnValue('Foo')); |
3123
|
|
|
|
3124
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3125
|
|
|
->method('hasReader') |
3126
|
|
|
->with($this->equalTo('Foo')) |
3127
|
|
|
->will($this->returnValue(false)); |
3128
|
|
|
|
3129
|
|
|
$this->controller->historyCompareRevisionsAction(null, null, null, $this->request); |
|
|
|
|
3130
|
|
|
} |
3131
|
|
|
|
3132
|
|
|
public function testHistoryCompareRevisionsActionNotFoundBaseRevision() |
3133
|
|
|
{ |
3134
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'); |
3135
|
|
|
|
3136
|
|
|
$this->request->query->set('id', 123); |
3137
|
|
|
|
3138
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3139
|
|
|
->method('checkAccess') |
3140
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3141
|
|
|
->will($this->returnValue(true)); |
3142
|
|
|
|
3143
|
|
|
$object = new \stdClass(); |
3144
|
|
|
|
3145
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3146
|
|
|
->method('getObject') |
3147
|
|
|
->will($this->returnValue($object)); |
3148
|
|
|
|
3149
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3150
|
|
|
->method('getClass') |
3151
|
|
|
->will($this->returnValue('Foo')); |
3152
|
|
|
|
3153
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3154
|
|
|
->method('hasReader') |
3155
|
|
|
->with($this->equalTo('Foo')) |
3156
|
|
|
->will($this->returnValue(true)); |
3157
|
|
|
|
3158
|
|
|
$reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface'); |
3159
|
|
|
|
3160
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3161
|
|
|
->method('getReader') |
3162
|
|
|
->with($this->equalTo('Foo')) |
3163
|
|
|
->will($this->returnValue($reader)); |
3164
|
|
|
|
3165
|
|
|
// once because it will not be found and therefore the second call won't be executed |
3166
|
|
|
$reader->expects($this->once()) |
3167
|
|
|
->method('find') |
3168
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3169
|
|
|
->will($this->returnValue(null)); |
3170
|
|
|
|
3171
|
|
|
$this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request); |
|
|
|
|
3172
|
|
|
} |
3173
|
|
|
|
3174
|
|
|
public function testHistoryCompareRevisionsActionNotFoundCompareRevision() |
3175
|
|
|
{ |
3176
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `789` with classname : `Foo`'); |
3177
|
|
|
|
3178
|
|
|
$this->request->query->set('id', 123); |
3179
|
|
|
|
3180
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3181
|
|
|
->method('checkAccess') |
3182
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3183
|
|
|
->will($this->returnValue(true)); |
3184
|
|
|
|
3185
|
|
|
$object = new \stdClass(); |
3186
|
|
|
|
3187
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3188
|
|
|
->method('getObject') |
3189
|
|
|
->will($this->returnValue($object)); |
3190
|
|
|
|
3191
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3192
|
|
|
->method('getClass') |
3193
|
|
|
->will($this->returnValue('Foo')); |
3194
|
|
|
|
3195
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3196
|
|
|
->method('hasReader') |
3197
|
|
|
->with($this->equalTo('Foo')) |
3198
|
|
|
->will($this->returnValue(true)); |
3199
|
|
|
|
3200
|
|
|
$reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface'); |
3201
|
|
|
|
3202
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3203
|
|
|
->method('getReader') |
3204
|
|
|
->with($this->equalTo('Foo')) |
3205
|
|
|
->will($this->returnValue($reader)); |
3206
|
|
|
|
3207
|
|
|
$objectRevision = new \stdClass(); |
3208
|
|
|
$objectRevision->revision = 456; |
3209
|
|
|
|
3210
|
|
|
// first call should return, so the second call will throw an exception |
3211
|
|
|
$reader->expects($this->at(0)) |
3212
|
|
|
->method('find') |
3213
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3214
|
|
|
->will($this->returnValue($objectRevision)); |
3215
|
|
|
|
3216
|
|
|
$reader->expects($this->at(1)) |
3217
|
|
|
->method('find') |
3218
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
3219
|
|
|
->will($this->returnValue(null)); |
3220
|
|
|
|
3221
|
|
|
$this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request); |
|
|
|
|
3222
|
|
|
} |
3223
|
|
|
|
3224
|
|
|
public function testHistoryCompareRevisionsActionAction() |
3225
|
|
|
{ |
3226
|
|
|
$this->request->query->set('id', 123); |
3227
|
|
|
|
3228
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3229
|
|
|
->method('checkAccess') |
3230
|
|
|
->with($this->equalTo('historyCompareRevisions')) |
3231
|
|
|
->will($this->returnValue(true)); |
3232
|
|
|
|
3233
|
|
|
$object = new \stdClass(); |
3234
|
|
|
|
3235
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3236
|
|
|
->method('getObject') |
3237
|
|
|
->will($this->returnValue($object)); |
3238
|
|
|
|
3239
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3240
|
|
|
->method('getClass') |
3241
|
|
|
->will($this->returnValue('Foo')); |
3242
|
|
|
|
3243
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3244
|
|
|
->method('hasReader') |
3245
|
|
|
->with($this->equalTo('Foo')) |
3246
|
|
|
->will($this->returnValue(true)); |
3247
|
|
|
|
3248
|
|
|
$reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface'); |
3249
|
|
|
|
3250
|
|
|
$this->auditManager->expects($this->once()) |
|
|
|
|
3251
|
|
|
->method('getReader') |
3252
|
|
|
->with($this->equalTo('Foo')) |
3253
|
|
|
->will($this->returnValue($reader)); |
3254
|
|
|
|
3255
|
|
|
$objectRevision = new \stdClass(); |
3256
|
|
|
$objectRevision->revision = 456; |
3257
|
|
|
|
3258
|
|
|
$compareObjectRevision = new \stdClass(); |
3259
|
|
|
$compareObjectRevision->revision = 789; |
3260
|
|
|
|
3261
|
|
|
$reader->expects($this->at(0)) |
3262
|
|
|
->method('find') |
3263
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456)) |
3264
|
|
|
->will($this->returnValue($objectRevision)); |
3265
|
|
|
|
3266
|
|
|
$reader->expects($this->at(1)) |
3267
|
|
|
->method('find') |
3268
|
|
|
->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789)) |
3269
|
|
|
->will($this->returnValue($compareObjectRevision)); |
3270
|
|
|
|
3271
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3272
|
|
|
->method('setSubject') |
3273
|
|
|
->with($this->equalTo($objectRevision)) |
3274
|
|
|
->will($this->returnValue(null)); |
3275
|
|
|
|
3276
|
|
|
$fieldDescriptionCollection = new FieldDescriptionCollection(); |
3277
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3278
|
|
|
->method('getShow') |
3279
|
|
|
->will($this->returnValue($fieldDescriptionCollection)); |
3280
|
|
|
|
3281
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request)); |
|
|
|
|
3282
|
|
|
|
3283
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
3284
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
3285
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
3286
|
|
|
|
3287
|
|
|
$this->assertSame('show', $this->parameters['action']); |
3288
|
|
|
$this->assertSame($objectRevision, $this->parameters['object']); |
3289
|
|
|
$this->assertSame($compareObjectRevision, $this->parameters['object_compare']); |
3290
|
|
|
$this->assertSame($fieldDescriptionCollection, $this->parameters['elements']); |
3291
|
|
|
|
3292
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
3293
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:show_compare.html.twig', $this->template); |
3294
|
|
|
} |
3295
|
|
|
|
3296
|
|
|
public function testBatchActionWrongMethod() |
3297
|
|
|
{ |
3298
|
|
|
$this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'Invalid request type "GET", POST expected'); |
3299
|
|
|
|
3300
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3301
|
|
|
} |
3302
|
|
|
|
3303
|
|
|
/** |
3304
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3305
|
|
|
* |
3306
|
|
|
* @group legacy |
3307
|
|
|
*/ |
3308
|
|
|
public function testBatchActionActionNotDefined() |
3309
|
|
|
{ |
3310
|
|
|
$this->expectException('RuntimeException', 'The `foo` batch action is not defined'); |
3311
|
|
|
|
3312
|
|
|
$batchActions = []; |
3313
|
|
|
|
3314
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3315
|
|
|
->method('getBatchActions') |
3316
|
|
|
->will($this->returnValue($batchActions)); |
3317
|
|
|
|
3318
|
|
|
$this->request->setMethod('POST'); |
3319
|
|
|
$this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
3320
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3321
|
|
|
|
3322
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3323
|
|
|
} |
3324
|
|
|
|
3325
|
|
|
public function testBatchActionActionInvalidCsrfToken() |
3326
|
|
|
{ |
3327
|
|
|
$this->request->setMethod('POST'); |
3328
|
|
|
$this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
3329
|
|
|
$this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID'); |
3330
|
|
|
|
3331
|
|
|
try { |
3332
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3333
|
|
|
} catch (HttpException $e) { |
3334
|
|
|
$this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage()); |
3335
|
|
|
$this->assertSame(400, $e->getStatusCode()); |
3336
|
|
|
} |
3337
|
|
|
} |
3338
|
|
|
|
3339
|
|
|
/** |
3340
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3341
|
|
|
* |
3342
|
|
|
* @group legacy |
3343
|
|
|
*/ |
3344
|
|
|
public function testBatchActionMethodNotExist() |
3345
|
|
|
{ |
3346
|
|
|
$this->expectException('RuntimeException', 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable'); |
3347
|
|
|
|
3348
|
|
|
$batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3349
|
|
|
|
3350
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3351
|
|
|
->method('getBatchActions') |
3352
|
|
|
->will($this->returnValue($batchActions)); |
3353
|
|
|
|
3354
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3355
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3356
|
|
|
->method('getDatagrid') |
3357
|
|
|
->will($this->returnValue($datagrid)); |
3358
|
|
|
|
3359
|
|
|
$this->request->setMethod('POST'); |
3360
|
|
|
$this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false])); |
3361
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3362
|
|
|
|
3363
|
|
|
$this->controller->batchAction($this->request); |
|
|
|
|
3364
|
|
|
} |
3365
|
|
|
|
3366
|
|
|
/** |
3367
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3368
|
|
|
* |
3369
|
|
|
* @group legacy |
3370
|
|
|
*/ |
3371
|
|
|
public function testBatchActionWithoutConfirmation() |
3372
|
|
|
{ |
3373
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3374
|
|
|
|
3375
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3376
|
|
|
->method('getBatchActions') |
3377
|
|
|
->will($this->returnValue($batchActions)); |
3378
|
|
|
|
3379
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3380
|
|
|
|
3381
|
|
|
$query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface'); |
3382
|
|
|
$datagrid->expects($this->once()) |
3383
|
|
|
->method('getQuery') |
3384
|
|
|
->will($this->returnValue($query)); |
3385
|
|
|
|
3386
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3387
|
|
|
->method('getDatagrid') |
3388
|
|
|
->will($this->returnValue($datagrid)); |
3389
|
|
|
|
3390
|
|
|
$modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'); |
3391
|
|
|
|
3392
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3393
|
|
|
->method('checkAccess') |
3394
|
|
|
->with($this->equalTo('batchDelete')) |
3395
|
|
|
->will($this->returnValue(true)); |
3396
|
|
|
|
3397
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3398
|
|
|
->method('getModelManager') |
3399
|
|
|
->will($this->returnValue($modelManager)); |
3400
|
|
|
|
3401
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3402
|
|
|
->method('getClass') |
3403
|
|
|
->will($this->returnValue('Foo')); |
3404
|
|
|
|
3405
|
|
|
$modelManager->expects($this->once()) |
3406
|
|
|
->method('addIdentifiersToQuery') |
3407
|
|
|
->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
3408
|
|
|
->will($this->returnValue(true)); |
3409
|
|
|
|
3410
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
3411
|
|
|
|
3412
|
|
|
$this->request->setMethod('POST'); |
3413
|
|
|
$this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false])); |
3414
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3415
|
|
|
|
3416
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3417
|
|
|
|
3418
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
3419
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
3420
|
|
|
$this->assertSame('list?', $result->getTargetUrl()); |
3421
|
|
|
} |
3422
|
|
|
|
3423
|
|
|
/** |
3424
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3425
|
|
|
* |
3426
|
|
|
* @group legacy |
3427
|
|
|
*/ |
3428
|
|
|
public function testBatchActionWithoutConfirmation2() |
3429
|
|
|
{ |
3430
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3431
|
|
|
|
3432
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3433
|
|
|
->method('getBatchActions') |
3434
|
|
|
->will($this->returnValue($batchActions)); |
3435
|
|
|
|
3436
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3437
|
|
|
|
3438
|
|
|
$query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface'); |
3439
|
|
|
$datagrid->expects($this->once()) |
3440
|
|
|
->method('getQuery') |
3441
|
|
|
->will($this->returnValue($query)); |
3442
|
|
|
|
3443
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3444
|
|
|
->method('getDatagrid') |
3445
|
|
|
->will($this->returnValue($datagrid)); |
3446
|
|
|
|
3447
|
|
|
$modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'); |
3448
|
|
|
|
3449
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3450
|
|
|
->method('checkAccess') |
3451
|
|
|
->with($this->equalTo('batchDelete')) |
3452
|
|
|
->will($this->returnValue(true)); |
3453
|
|
|
|
3454
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3455
|
|
|
->method('getModelManager') |
3456
|
|
|
->will($this->returnValue($modelManager)); |
3457
|
|
|
|
3458
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3459
|
|
|
->method('getClass') |
3460
|
|
|
->will($this->returnValue('Foo')); |
3461
|
|
|
|
3462
|
|
|
$modelManager->expects($this->once()) |
3463
|
|
|
->method('addIdentifiersToQuery') |
3464
|
|
|
->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
3465
|
|
|
->will($this->returnValue(true)); |
3466
|
|
|
|
3467
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
3468
|
|
|
|
3469
|
|
|
$this->request->setMethod('POST'); |
3470
|
|
|
$this->request->request->set('action', 'delete'); |
3471
|
|
|
$this->request->request->set('idx', ['123', '456']); |
3472
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3473
|
|
|
|
3474
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3475
|
|
|
|
3476
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
3477
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
3478
|
|
|
$this->assertSame('list?', $result->getTargetUrl()); |
3479
|
|
|
} |
3480
|
|
|
|
3481
|
|
|
/** |
3482
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3483
|
|
|
* |
3484
|
|
|
* @group legacy |
3485
|
|
|
*/ |
3486
|
|
|
public function testBatchActionWithConfirmation() |
3487
|
|
|
{ |
3488
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]]; |
3489
|
|
|
|
3490
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3491
|
|
|
->method('getBatchActions') |
3492
|
|
|
->will($this->returnValue($batchActions)); |
3493
|
|
|
|
3494
|
|
|
$data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]; |
3495
|
|
|
|
3496
|
|
|
$this->request->setMethod('POST'); |
3497
|
|
|
$this->request->request->set('data', json_encode($data)); |
3498
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3499
|
|
|
|
3500
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3501
|
|
|
|
3502
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3503
|
|
|
->method('getDatagrid') |
3504
|
|
|
->will($this->returnValue($datagrid)); |
3505
|
|
|
|
3506
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
3507
|
|
|
->disableOriginalConstructor() |
3508
|
|
|
->getMock(); |
3509
|
|
|
|
3510
|
|
|
$form->expects($this->once()) |
3511
|
|
|
->method('createView') |
3512
|
|
|
->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView'))); |
3513
|
|
|
|
3514
|
|
|
$datagrid->expects($this->once()) |
3515
|
|
|
->method('getForm') |
3516
|
|
|
->will($this->returnValue($form)); |
3517
|
|
|
|
3518
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->batchAction($this->request)); |
|
|
|
|
3519
|
|
|
|
3520
|
|
|
$this->assertSame($this->admin, $this->parameters['admin']); |
3521
|
|
|
$this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']); |
3522
|
|
|
$this->assertSame($this->pool, $this->parameters['admin_pool']); |
3523
|
|
|
|
3524
|
|
|
$this->assertSame('list', $this->parameters['action']); |
3525
|
|
|
$this->assertSame($datagrid, $this->parameters['datagrid']); |
3526
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']); |
3527
|
|
|
$this->assertSame($data, $this->parameters['data']); |
3528
|
|
|
$this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']); |
3529
|
|
|
$this->assertSame('Foo Bar', $this->parameters['action_label']); |
3530
|
|
|
|
3531
|
|
|
$this->assertSame([], $this->session->getFlashBag()->all()); |
3532
|
|
|
$this->assertSame('SonataAdminBundle:CRUD:batch_confirmation.html.twig', $this->template); |
3533
|
|
|
} |
3534
|
|
|
|
3535
|
|
|
/** |
3536
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3537
|
|
|
* |
3538
|
|
|
* @group legacy |
3539
|
|
|
*/ |
3540
|
|
|
public function testBatchActionNonRelevantAction() |
3541
|
|
|
{ |
3542
|
|
|
$controller = new BatchAdminController(); |
3543
|
|
|
$controller->setContainer($this->container); |
3544
|
|
|
|
3545
|
|
|
$batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3546
|
|
|
|
3547
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3548
|
|
|
->method('getBatchActions') |
3549
|
|
|
->will($this->returnValue($batchActions)); |
3550
|
|
|
|
3551
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3552
|
|
|
|
3553
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3554
|
|
|
->method('getDatagrid') |
3555
|
|
|
->will($this->returnValue($datagrid)); |
3556
|
|
|
|
3557
|
|
|
$this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle'); |
3558
|
|
|
|
3559
|
|
|
$this->request->setMethod('POST'); |
3560
|
|
|
$this->request->request->set('action', 'foo'); |
3561
|
|
|
$this->request->request->set('idx', ['789']); |
3562
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3563
|
|
|
|
3564
|
|
|
$result = $controller->batchAction($this->request); |
|
|
|
|
3565
|
|
|
|
3566
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
3567
|
|
|
$this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info')); |
3568
|
|
|
$this->assertSame('list?', $result->getTargetUrl()); |
3569
|
|
|
} |
3570
|
|
|
|
3571
|
|
|
/** |
3572
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3573
|
|
|
* |
3574
|
|
|
* @group legacy |
3575
|
|
|
*/ |
3576
|
|
|
public function testBatchActionNonRelevantAction2() |
3577
|
|
|
{ |
3578
|
|
|
$controller = new BatchAdminController(); |
3579
|
|
|
$controller->setContainer($this->container); |
3580
|
|
|
|
3581
|
|
|
$batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3582
|
|
|
|
3583
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3584
|
|
|
->method('getBatchActions') |
3585
|
|
|
->will($this->returnValue($batchActions)); |
3586
|
|
|
|
3587
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3588
|
|
|
|
3589
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3590
|
|
|
->method('getDatagrid') |
3591
|
|
|
->will($this->returnValue($datagrid)); |
3592
|
|
|
|
3593
|
|
|
$this->expectTranslate('flash_foo_error', [], 'SonataAdminBundle'); |
3594
|
|
|
|
3595
|
|
|
$this->request->setMethod('POST'); |
3596
|
|
|
$this->request->request->set('action', 'foo'); |
3597
|
|
|
$this->request->request->set('idx', ['999']); |
3598
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3599
|
|
|
|
3600
|
|
|
$result = $controller->batchAction($this->request); |
|
|
|
|
3601
|
|
|
|
3602
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
3603
|
|
|
$this->assertSame(['flash_foo_error'], $this->session->getFlashBag()->get('sonata_flash_info')); |
3604
|
|
|
$this->assertSame('list?', $result->getTargetUrl()); |
3605
|
|
|
} |
3606
|
|
|
|
3607
|
|
|
/** |
3608
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3609
|
|
|
* |
3610
|
|
|
* @group legacy |
3611
|
|
|
*/ |
3612
|
|
|
public function testBatchActionNoItems() |
3613
|
|
|
{ |
3614
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true]]; |
3615
|
|
|
|
3616
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3617
|
|
|
->method('getBatchActions') |
3618
|
|
|
->will($this->returnValue($batchActions)); |
3619
|
|
|
|
3620
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3621
|
|
|
|
3622
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3623
|
|
|
->method('getDatagrid') |
3624
|
|
|
->will($this->returnValue($datagrid)); |
3625
|
|
|
|
3626
|
|
|
$this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle'); |
3627
|
|
|
|
3628
|
|
|
$this->request->setMethod('POST'); |
3629
|
|
|
$this->request->request->set('action', 'delete'); |
3630
|
|
|
$this->request->request->set('idx', []); |
3631
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3632
|
|
|
|
3633
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3634
|
|
|
|
3635
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
3636
|
|
|
$this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info')); |
3637
|
|
|
$this->assertSame('list?', $result->getTargetUrl()); |
3638
|
|
|
} |
3639
|
|
|
|
3640
|
|
|
/** |
3641
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3642
|
|
|
* |
3643
|
|
|
* @group legacy |
3644
|
|
|
*/ |
3645
|
|
|
public function testBatchActionNoItemsEmptyQuery() |
3646
|
|
|
{ |
3647
|
|
|
$controller = new BatchAdminController(); |
3648
|
|
|
$controller->setContainer($this->container); |
3649
|
|
|
|
3650
|
|
|
$batchActions = ['bar' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3651
|
|
|
|
3652
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3653
|
|
|
->method('getBatchActions') |
3654
|
|
|
->will($this->returnValue($batchActions)); |
3655
|
|
|
|
3656
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3657
|
|
|
|
3658
|
|
|
$query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface'); |
3659
|
|
|
$datagrid->expects($this->once()) |
3660
|
|
|
->method('getQuery') |
3661
|
|
|
->will($this->returnValue($query)); |
3662
|
|
|
|
3663
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3664
|
|
|
->method('getDatagrid') |
3665
|
|
|
->will($this->returnValue($datagrid)); |
3666
|
|
|
|
3667
|
|
|
$modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'); |
3668
|
|
|
|
3669
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3670
|
|
|
->method('getModelManager') |
3671
|
|
|
->will($this->returnValue($modelManager)); |
3672
|
|
|
|
3673
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3674
|
|
|
->method('getClass') |
3675
|
|
|
->will($this->returnValue('Foo')); |
3676
|
|
|
|
3677
|
|
|
$this->request->setMethod('POST'); |
3678
|
|
|
$this->request->request->set('action', 'bar'); |
3679
|
|
|
$this->request->request->set('idx', []); |
3680
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3681
|
|
|
|
3682
|
|
|
$result = $controller->batchAction($this->request); |
|
|
|
|
3683
|
|
|
|
3684
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $result); |
3685
|
|
|
$this->assertSame('batchActionBar executed', $result->getContent()); |
3686
|
|
|
} |
3687
|
|
|
|
3688
|
|
|
/** |
3689
|
|
|
* NEXT_MAJOR: Remove this legacy group. |
3690
|
|
|
* |
3691
|
|
|
* @group legacy |
3692
|
|
|
*/ |
3693
|
|
|
public function testBatchActionWithRequesData() |
3694
|
|
|
{ |
3695
|
|
|
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]]; |
3696
|
|
|
|
3697
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3698
|
|
|
->method('getBatchActions') |
3699
|
|
|
->will($this->returnValue($batchActions)); |
3700
|
|
|
|
3701
|
|
|
$datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface'); |
3702
|
|
|
|
3703
|
|
|
$query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface'); |
3704
|
|
|
$datagrid->expects($this->once()) |
3705
|
|
|
->method('getQuery') |
3706
|
|
|
->will($this->returnValue($query)); |
3707
|
|
|
|
3708
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3709
|
|
|
->method('getDatagrid') |
3710
|
|
|
->will($this->returnValue($datagrid)); |
3711
|
|
|
|
3712
|
|
|
$modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'); |
3713
|
|
|
|
3714
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
3715
|
|
|
->method('checkAccess') |
3716
|
|
|
->with($this->equalTo('batchDelete')) |
3717
|
|
|
->will($this->returnValue(true)); |
3718
|
|
|
|
3719
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3720
|
|
|
->method('getModelManager') |
3721
|
|
|
->will($this->returnValue($modelManager)); |
3722
|
|
|
|
3723
|
|
|
$this->admin->expects($this->any()) |
|
|
|
|
3724
|
|
|
->method('getClass') |
3725
|
|
|
->will($this->returnValue('Foo')); |
3726
|
|
|
|
3727
|
|
|
$modelManager->expects($this->once()) |
3728
|
|
|
->method('addIdentifiersToQuery') |
3729
|
|
|
->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456'])) |
3730
|
|
|
->will($this->returnValue(true)); |
3731
|
|
|
|
3732
|
|
|
$this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle'); |
3733
|
|
|
|
3734
|
|
|
$this->request->setMethod('POST'); |
3735
|
|
|
$this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false])); |
3736
|
|
|
$this->request->request->set('foo', 'bar'); |
3737
|
|
|
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch'); |
3738
|
|
|
|
3739
|
|
|
$result = $this->controller->batchAction($this->request); |
|
|
|
|
3740
|
|
|
|
3741
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result); |
3742
|
|
|
$this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success')); |
3743
|
|
|
$this->assertSame('list?', $result->getTargetUrl()); |
3744
|
|
|
$this->assertSame('bar', $this->request->request->get('foo')); |
3745
|
|
|
} |
3746
|
|
|
|
3747
|
|
|
public function getCsrfProvider() |
3748
|
|
|
{ |
3749
|
|
|
return $this->csrfProvider; |
3750
|
|
|
} |
3751
|
|
|
|
3752
|
|
|
public function getToStringValues() |
3753
|
|
|
{ |
3754
|
|
|
return [ |
3755
|
|
|
['', ''], |
3756
|
|
|
['Foo', 'Foo'], |
3757
|
|
|
['<a href="http://foo">Bar</a>', '<a href="http://foo">Bar</a>'], |
3758
|
|
|
['<>&"'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/', '<>&"\'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/'], |
3759
|
|
|
]; |
3760
|
|
|
} |
3761
|
|
|
|
3762
|
|
|
private function assertLoggerLogsModelManagerException($subject, $method) |
3763
|
|
|
{ |
3764
|
|
|
$exception = new ModelManagerException( |
3765
|
|
|
$message = 'message', |
3766
|
|
|
1234, |
3767
|
|
|
new \Exception($previousExceptionMessage = 'very useful message') |
3768
|
|
|
); |
3769
|
|
|
|
3770
|
|
|
$subject->expects($this->once()) |
3771
|
|
|
->method($method) |
3772
|
|
|
->will($this->returnCallback(function () use ($exception) { |
3773
|
|
|
throw $exception; |
3774
|
|
|
})); |
3775
|
|
|
|
3776
|
|
|
$this->logger->expects($this->once()) |
3777
|
|
|
->method('error') |
3778
|
|
|
->with($message, [ |
3779
|
|
|
'exception' => $exception, |
3780
|
|
|
'previous_exception_message' => $previousExceptionMessage, |
3781
|
|
|
]); |
3782
|
|
|
} |
3783
|
|
|
|
3784
|
|
|
private function expectTranslate($id, array $parameters = [], $domain = null, $locale = null) |
3785
|
|
|
{ |
3786
|
|
|
$this->translator->expects($this->once()) |
3787
|
|
|
->method('trans') |
3788
|
|
|
->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale)) |
3789
|
|
|
->will($this->returnValue($id)); |
3790
|
|
|
} |
3791
|
|
|
} |
3792
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.