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