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