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