Issues (655)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/Controller/CRUDControllerTest.php (163 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Bridge\PhpUnit\ExpectDeprecationTrait;
43
use Symfony\Component\DependencyInjection\Container;
44
use Symfony\Component\DependencyInjection\ContainerInterface;
45
use Symfony\Component\Form\Form;
46
use Symfony\Component\Form\FormError;
47
use Symfony\Component\Form\FormRenderer;
48
use Symfony\Component\Form\FormView;
49
use Symfony\Component\HttpFoundation\JsonResponse;
50
use Symfony\Component\HttpFoundation\RedirectResponse;
51
use Symfony\Component\HttpFoundation\Request;
52
use Symfony\Component\HttpFoundation\RequestStack;
53
use Symfony\Component\HttpFoundation\Response;
54
use Symfony\Component\HttpFoundation\Session\Session;
55
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
56
use Symfony\Component\HttpFoundation\StreamedResponse;
57
use Symfony\Component\HttpKernel\Exception\HttpException;
58
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
59
use Symfony\Component\HttpKernel\KernelInterface;
60
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
61
use Symfony\Component\Security\Csrf\CsrfToken;
62
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
63
use Symfony\Contracts\Translation\TranslatorInterface;
64
use Twig\Environment;
65
66
/**
67
 * Test for CRUDController.
68
 *
69
 * @author Andrej Hudec <[email protected]>
70
 */
71
class CRUDControllerTest extends TestCase
72
{
73
    use ExpectDeprecationTrait;
74
75
    /**
76
     * @var CRUDController
77
     */
78
    private $controller;
79
80
    /**
81
     * @var Request
82
     */
83
    private $request;
84
85
    /**
86
     * @var AdminInterface
87
     */
88
    private $admin;
89
90
    /**
91
     * @var TemplateRegistryInterface
92
     */
93
    private $templateRegistry;
94
95
    /**
96
     * @var Pool
97
     */
98
    private $pool;
99
100
    /**
101
     * @var array
102
     */
103
    private $parameters;
104
105
    /**
106
     * @var Session
107
     */
108
    private $session;
109
110
    /**
111
     * @var AuditManagerInterface
112
     */
113
    private $auditManager;
114
115
    /**
116
     * @var ContainerInterface
117
     */
118
    private $container;
119
120
    /**
121
     * @var AdminObjectAclManipulator
122
     */
123
    private $adminObjectAclManipulator;
124
125
    /**
126
     * @var string
127
     */
128
    private $template;
129
130
    /**
131
     * @var array
132
     */
133
    private $protectedTestedMethods;
134
135
    /**
136
     * @var CsrfTokenManagerInterface
137
     */
138
    private $csrfProvider;
139
140
    /**
141
     * @var KernelInterface
142
     */
143
    private $kernel;
144
145
    /**
146
     * @var TranslatorInterface
147
     */
148
    private $translator;
149
150
    /**
151
     * @var LoggerInterface|MockObject
152
     */
153
    private $logger;
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    protected function setUp(): void
159
    {
160
        $this->container = new Container();
161
        $this->request = new Request();
162
        $this->pool = new Pool($this->container, 'title', 'logo.png');
163
        $this->pool->setAdminServiceIds(['foo.admin']);
164
        $this->request->attributes->set('_sonata_admin', 'foo.admin');
165
        $this->admin = $this->createMock(AdminInterface::class);
166
        $this->translator = $this->createMock(TranslatorInterface::class);
167
        $this->parameters = [];
168
        $this->template = '';
169
170
        $this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
171
172
        $templatingRenderReturnCallback = $this->returnCallback(function (
173
            string $name,
174
            array $context = []
175
        ): string {
176
            $this->template = $name;
177
178
            $this->parameters = $context;
179
180
            return '';
181
        });
182
183
        $this->session = new Session(new MockArraySessionStorage());
184
185
        $twig = $this->getMockBuilder(Environment::class)
186
            ->disableOriginalConstructor()
187
            ->getMock();
188
189
        $twig
190
            ->method('getRuntime')
191
            ->willReturn($this->createMock(FormRenderer::class));
192
193
        $twig
194
            ->method('render')
195
            ->will($templatingRenderReturnCallback);
196
197
        $exporter = new Exporter([new JsonWriter(sys_get_temp_dir().'/sonataadmin/export.json')]);
198
199
        $adminExporter = new AdminExporter($exporter);
200
201
        $this->auditManager = $this->createMock(AuditManagerInterface::class);
202
203
        $this->adminObjectAclManipulator = $this->getMockBuilder(AdminObjectAclManipulator::class)
204
            ->disableOriginalConstructor()
205
            ->getMock();
206
207
        $this->csrfProvider = $this->getMockBuilder(CsrfTokenManagerInterface::class)
208
            ->getMock();
209
210
        $this->csrfProvider
211
            ->method('getToken')
212
            ->willReturnCallback(static function (string $intention): CsrfToken {
213
                return new CsrfToken($intention, sprintf('csrf-token-123_%s', $intention));
214
            });
215
216
        $this->csrfProvider
217
            ->method('isTokenValid')
218
            ->willReturnCallback(static function (CsrfToken $token): bool {
219
                return $token->getValue() === sprintf('csrf-token-123_%s', $token->getId());
220
            });
221
222
        $this->logger = $this->createMock(LoggerInterface::class);
223
224
        $requestStack = new RequestStack();
225
        $requestStack->push($this->request);
226
227
        $this->kernel = $this->createMock(KernelInterface::class);
228
229
        $this->container->set('sonata.admin.pool', $this->pool);
230
        $this->container->set('request_stack', $requestStack);
231
        $this->container->set('foo.admin', $this->admin);
232
        $this->container->set('foo.admin.template_registry', $this->templateRegistry->reveal());
233
        $this->container->set('twig', $twig);
234
        $this->container->set('session', $this->session);
235
        $this->container->set('sonata.exporter.exporter', $exporter);
236
        $this->container->set('sonata.admin.admin_exporter', $adminExporter);
237
        $this->container->set('sonata.admin.audit.manager', $this->auditManager);
238
        $this->container->set('sonata.admin.object.manipulator.acl.admin', $this->adminObjectAclManipulator);
239
        $this->container->set('security.csrf.token_manager', $this->csrfProvider);
240
        $this->container->set('logger', $this->logger);
241
        $this->container->set('kernel', $this->kernel);
242
        $this->container->set('translator', $this->translator);
243
        $this->container->set('sonata.admin.breadcrumbs_builder', new BreadcrumbsBuilder([]));
244
245
        $this->container->setParameter(
246
            'security.role_hierarchy.roles',
247
            ['ROLE_SUPER_ADMIN' => ['ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN']]
248
        );
249
        $this->container->setParameter('sonata.admin.security.acl_user_manager', null);
250
251
        $this->templateRegistry->getTemplate('ajax')->willReturn('@SonataAdmin/ajax_layout.html.twig');
252
        $this->templateRegistry->getTemplate('layout')->willReturn('@SonataAdmin/standard_layout.html.twig');
253
        $this->templateRegistry->getTemplate('show')->willReturn('@SonataAdmin/CRUD/show.html.twig');
254
        $this->templateRegistry->getTemplate('show_compare')->willReturn('@SonataAdmin/CRUD/show_compare.html.twig');
255
        $this->templateRegistry->getTemplate('edit')->willReturn('@SonataAdmin/CRUD/edit.html.twig');
256
        $this->templateRegistry->getTemplate('dashboard')->willReturn('@SonataAdmin/Core/dashboard.html.twig');
257
        $this->templateRegistry->getTemplate('search')->willReturn('@SonataAdmin/Core/search.html.twig');
258
        $this->templateRegistry->getTemplate('list')->willReturn('@SonataAdmin/CRUD/list.html.twig');
259
        $this->templateRegistry->getTemplate('preview')->willReturn('@SonataAdmin/CRUD/preview.html.twig');
260
        $this->templateRegistry->getTemplate('history')->willReturn('@SonataAdmin/CRUD/history.html.twig');
261
        $this->templateRegistry->getTemplate('acl')->willReturn('@SonataAdmin/CRUD/acl.html.twig');
262
        $this->templateRegistry->getTemplate('delete')->willReturn('@SonataAdmin/CRUD/delete.html.twig');
263
        $this->templateRegistry->getTemplate('batch')->willReturn('@SonataAdmin/CRUD/list__batch.html.twig');
264
        $this->templateRegistry->getTemplate('batch_confirmation')->willReturn('@SonataAdmin/CRUD/batch_confirmation.html.twig');
265
266
        $this->admin
267
            ->method('getIdParameter')
268
            ->willReturn('id');
269
270
        $this->admin
271
            ->method('getAccessMapping')
272
            ->willReturn([]);
273
274
        $this->admin
275
            ->method('generateUrl')
276
            ->willReturnCallback(
277
                static function ($name, array $parameters = []) {
278
                    $result = $name;
279
                    if (!empty($parameters)) {
280
                        $result .= '?'.http_build_query($parameters);
281
                    }
282
283
                    return $result;
284
                }
285
            );
286
287
        $this->admin
288
            ->method('generateObjectUrl')
289
            ->willReturnCallback(
290
                static function (string $name, $object, array $parameters = []): string {
291
                    $result = sprintf('%s_%s', \get_class($object), $name);
292
                    if (!empty($parameters)) {
293
                        $result .= '?'.http_build_query($parameters);
294
                    }
295
296
                    return $result;
297
                }
298
            );
299
300
        $this->admin
301
            ->method('getCode')
302
            ->willReturn('foo.admin');
303
304
        $this->controller = new CRUDController();
305
        $this->controller->setContainer($this->container);
306
307
        // Make some methods public to test them
308
        $testedMethods = [
309
            'renderJson',
310
            'isXmlHttpRequest',
311
            'configure',
312
            'getBaseTemplate',
313
            'redirectTo',
314
            'addFlash',
315
        ];
316
        foreach ($testedMethods as $testedMethod) {
317
            $method = new \ReflectionMethod(CRUDController::class, $testedMethod);
318
            $method->setAccessible(true);
319
            $this->protectedTestedMethods[$testedMethod] = $method;
320
        }
321
    }
322
323
    public function testRenderJson1(): void
324
    {
325
        $data = ['example' => '123', 'foo' => 'bar'];
326
327
        $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
328
        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request);
329
330
        $this->assertSame($response->headers->get('Content-Type'), 'application/json');
331
        $this->assertSame(json_encode($data), $response->getContent());
332
    }
333
334
    public function testRenderJson2(): void
335
    {
336
        $data = ['example' => '123', 'foo' => 'bar'];
337
338
        $this->request->headers->set('Content-Type', 'multipart/form-data');
339
        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request);
340
341
        $this->assertSame($response->headers->get('Content-Type'), 'application/json');
342
        $this->assertSame(json_encode($data), $response->getContent());
343
    }
344
345
    public function testRenderJsonAjax(): void
346
    {
347
        $data = ['example' => '123', 'foo' => 'bar'];
348
349
        $this->request->attributes->set('_xml_http_request', true);
350
        $this->request->headers->set('Content-Type', 'multipart/form-data');
351
        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request);
352
353
        $this->assertSame($response->headers->get('Content-Type'), 'application/json');
354
        $this->assertSame(json_encode($data), $response->getContent());
355
    }
356
357
    public function testIsXmlHttpRequest(): void
358
    {
359
        $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
360
361
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
362
363
        $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
364
365
        $this->request->headers->remove('X-Requested-With');
366
        $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
367
368
        $this->request->attributes->set('_xml_http_request', true);
369
        $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
370
    }
371
372
    public function testConfigure(): void
373
    {
374
        $uniqueId = '';
375
376
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
377
            ->method('setUniqid')
378
            ->willReturnCallback(static function (string $uniqid) use (&$uniqueId): void {
379
                $uniqueId = $uniqid;
380
            });
381
382
        $this->request->query->set('uniqid', '123456');
383
        $this->protectedTestedMethods['configure']->invoke($this->controller);
384
385
        $this->assertSame('123456', $uniqueId);
386
    }
387
388
    public function testConfigureChild(): void
389
    {
390
        $uniqueId = '';
391
392
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
393
            ->method('setUniqid')
394
            ->willReturnCallback(static function (string $uniqid) use (&$uniqueId): void {
395
                $uniqueId = $uniqid;
396
            });
397
398
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
399
            ->method('isChild')
400
            ->willReturn(true);
401
402
        $adminParent = $this->getMockBuilder(AdminInterface::class)
403
            ->disableOriginalConstructor()
404
            ->getMock();
405
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
406
            ->method('getParent')
407
            ->willReturn($adminParent);
408
409
        $this->request->query->set('uniqid', '123456');
410
        $this->protectedTestedMethods['configure']->invoke($this->controller);
411
412
        $this->assertSame('123456', $uniqueId);
413
    }
414
415
    public function testConfigureWithException(): void
416
    {
417
        $this->expectException(\RuntimeException::class);
418
        $this->expectExceptionMessage(
419
            'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`'
420
        );
421
422
        $this->request->attributes->remove('_sonata_admin');
423
        $this->protectedTestedMethods['configure']->invoke($this->controller);
424
    }
425
426
    public function testConfigureWithException2(): void
427
    {
428
        $this->pool->setAdminServiceIds(['nonexistent.admin']);
429
        $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
430
431
        $this->expectException(\RuntimeException::class);
432
        $this->expectExceptionMessage('Unable to find the admin class related to the current controller (Sonata\AdminBundle\Controller\CRUDController)');
433
434
        $this->protectedTestedMethods['configure']->invoke($this->controller);
435
    }
436
437
    public function testGetBaseTemplate(): void
438
    {
439
        $this->assertSame(
440
            '@SonataAdmin/standard_layout.html.twig',
441
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
442
        );
443
444
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
445
        $this->assertSame(
446
            '@SonataAdmin/ajax_layout.html.twig',
447
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
448
        );
449
450
        $this->request->headers->remove('X-Requested-With');
451
        $this->assertSame(
452
            '@SonataAdmin/standard_layout.html.twig',
453
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
454
        );
455
456
        $this->request->attributes->set('_xml_http_request', true);
457
        $this->assertSame(
458
            '@SonataAdmin/ajax_layout.html.twig',
459
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
460
        );
461
    }
462
463
    public function testRender(): void
464
    {
465
        $this->parameters = [];
466
        $this->assertInstanceOf(
467
            Response::class,
468
            $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], null)
469
        );
470
        $this->assertSame($this->admin, $this->parameters['admin']);
471
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
472
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
473
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
474
    }
475
476
    public function testRenderWithResponse(): void
477
    {
478
        $this->parameters = [];
479
        $response = new Response();
480
        $response->headers->set('X-foo', 'bar');
481
        $responseResult = $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], $response);
482
483
        $this->assertSame($response, $responseResult);
484
        $this->assertSame('bar', $responseResult->headers->get('X-foo'));
485
        $this->assertSame($this->admin, $this->parameters['admin']);
486
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
487
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
488
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
489
    }
490
491
    public function testRenderCustomParams(): void
492
    {
493
        $this->parameters = [];
494
        $this->assertInstanceOf(
495
            Response::class,
496
            $this->controller->renderWithExtraParams(
497
                '@FooAdmin/foo.html.twig',
498
                ['foo' => 'bar'],
499
                null
500
            )
501
        );
502
        $this->assertSame($this->admin, $this->parameters['admin']);
503
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
504
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
505
        $this->assertSame('bar', $this->parameters['foo']);
506
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
507
    }
508
509
    public function testRenderAjax(): void
510
    {
511
        $this->parameters = [];
512
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
513
        $this->assertInstanceOf(
514
            Response::class,
515
            $this->controller->renderWithExtraParams(
516
                '@FooAdmin/foo.html.twig',
517
                ['foo' => 'bar'],
518
                null
519
            )
520
        );
521
        $this->assertSame($this->admin, $this->parameters['admin']);
522
        $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
523
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
524
        $this->assertSame('bar', $this->parameters['foo']);
525
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
526
    }
527
528
    public function testListActionAccessDenied(): void
529
    {
530
        $this->expectException(AccessDeniedException::class);
531
532
        $this->admin->expects($this->once())
533
            ->method('checkAccess')
534
            ->with($this->equalTo('list'))
535
            ->will($this->throwException(new AccessDeniedException()));
536
537
        $this->controller->listAction($this->request);
538
    }
539
540
    public function testPreList(): void
541
    {
542
        $this->admin
543
            ->method('hasRoute')
544
            ->with($this->equalTo('list'))
545
            ->willReturn(true);
546
547
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
548
            ->method('checkAccess')
549
            ->with($this->equalTo('list'));
550
551
        $controller = new PreCRUDController();
552
        $controller->setContainer($this->container);
553
554
        $response = $controller->listAction($this->request);
555
        $this->assertInstanceOf(Response::class, $response);
556
        $this->assertSame('preList called', $response->getContent());
557
    }
558
559
    public function testListAction(): void
560
    {
561
        $datagrid = $this->createMock(DatagridInterface::class);
562
563
        $this->admin
564
            ->method('hasRoute')
565
            ->with($this->equalTo('list'))
566
            ->willReturn(true);
567
568
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
569
            ->method('checkAccess')
570
            ->with($this->equalTo('list'));
571
572
        $form = $this->getMockBuilder(Form::class)
573
            ->disableOriginalConstructor()
574
            ->getMock();
575
576
        $form->expects($this->once())
577
            ->method('createView')
578
            ->willReturn($this->createMock(FormView::class));
579
580
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
581
            ->method('getDatagrid')
582
            ->willReturn($datagrid);
583
584
        $datagrid->expects($this->once())
585
            ->method('getForm')
586
            ->willReturn($form);
587
588
        $this->parameters = [];
589
        $this->assertInstanceOf(Response::class, $this->controller->listAction($this->request));
590
591
        $this->assertSame($this->admin, $this->parameters['admin']);
592
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
593
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
594
595
        $this->assertSame('list', $this->parameters['action']);
596
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
597
        $this->assertInstanceOf(DatagridInterface::class, $this->parameters['datagrid']);
598
        $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
599
        $this->assertSame([], $this->session->getFlashBag()->all());
600
        $this->assertSame('@SonataAdmin/CRUD/list.html.twig', $this->template);
601
    }
602
603
    public function testBatchActionDeleteAccessDenied(): void
604
    {
605
        $this->expectException(AccessDeniedException::class);
606
607
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
608
            ->method('checkAccess')
609
            ->with($this->equalTo('batchDelete'))
610
            ->will($this->throwException(new AccessDeniedException()));
611
612
        $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
613
    }
614
615
    public function testBatchActionDelete(): void
616
    {
617
        $modelManager = $this->createMock(ModelManagerInterface::class);
618
619
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
620
            ->method('checkAccess')
621
            ->with($this->equalTo('batchDelete'));
622
623
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
624
            ->method('getModelManager')
625
            ->willReturn($modelManager);
626
627
        $this->admin->expects($this->once())
628
            ->method('getFilterParameters')
629
            ->willReturn(['foo' => 'bar']);
630
631
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
632
633
        $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
634
635
        $this->assertInstanceOf(RedirectResponse::class, $result);
636
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
637
        $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
638
    }
639
640
    public function testBatchActionDeleteWithModelManagerException(): void
641
    {
642
        $modelManager = $this->createMock(ModelManagerInterface::class);
643
        $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete');
644
645
        $this->admin->expects($this->once())
646
            ->method('getModelManager')
647
            ->willReturn($modelManager);
648
649
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
650
            ->method('getFilterParameters')
651
            ->willReturn(['foo' => 'bar']);
652
653
        $this->expectTranslate('flash_batch_delete_error', [], 'SonataAdminBundle');
654
655
        $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
656
657
        $this->assertInstanceOf(RedirectResponse::class, $result);
658
        $this->assertSame(['flash_batch_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error'));
659
        $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
660
    }
661
662
    public function testBatchActionDeleteWithModelManagerExceptionInDebugMode(): void
663
    {
664
        $modelManager = $this->createMock(ModelManagerInterface::class);
665
        $this->expectException(ModelManagerException::class);
666
667
        $modelManager->expects($this->once())
668
            ->method('batchDelete')
669
            ->willReturnCallback(static function (): void {
670
                throw new ModelManagerException();
671
            });
672
673
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
674
            ->method('getModelManager')
675
            ->willReturn($modelManager);
676
677
        $this->kernel->expects($this->once())
678
            ->method('isDebug')
679
            ->willReturn(true);
680
681
        $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
682
    }
683
684
    public function testShowActionNotFoundException(): void
685
    {
686
        $this->expectException(NotFoundHttpException::class);
687
688
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
689
            ->method('getObject')
690
            ->willReturn(null);
691
692
        $this->controller->showAction($this->request);
693
    }
694
695
    public function testShowActionAccessDenied(): void
696
    {
697
        $this->expectException(AccessDeniedException::class);
698
699
        $this->admin->expects($this->once())
700
            ->method('getObject')
701
            ->willReturn(new \stdClass());
702
703
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
704
            ->method('checkAccess')
705
            ->with($this->equalTo('show'))
706
            ->will($this->throwException(new AccessDeniedException()));
707
708
        $this->controller->showAction($this->request);
709
    }
710
711
    public function testPreShow(): void
712
    {
713
        $object = new \stdClass();
714
        $object->foo = 123456;
715
716
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
717
            ->method('getObject')
718
            ->willReturn($object);
719
720
        $this->admin->expects($this->once())
721
            ->method('checkAccess')
722
            ->with($this->equalTo('show'));
723
724
        $controller = new PreCRUDController();
725
        $controller->setContainer($this->container);
726
727
        $response = $controller->showAction($this->request);
728
        $this->assertInstanceOf(Response::class, $response);
729
        $this->assertSame('preShow called: 123456', $response->getContent());
730
    }
731
732
    public function testShowAction(): void
733
    {
734
        $object = new \stdClass();
735
736
        $this->admin->expects($this->once())
737
            ->method('getObject')
738
            ->willReturn($object);
739
740
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
741
            ->method('checkAccess')
742
            ->with($this->equalTo('show'));
743
744
        $show = new FieldDescriptionCollection();
745
746
        $this->admin->expects($this->once())
747
            ->method('getShow')
748
            ->willReturn($show);
749
750
        $this->assertInstanceOf(Response::class, $this->controller->showAction($this->request));
751
752
        $this->assertSame($this->admin, $this->parameters['admin']);
753
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
754
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
755
756
        $this->assertSame('show', $this->parameters['action']);
757
        $this->assertInstanceOf(FieldDescriptionCollection::class, $this->parameters['elements']);
758
        $this->assertSame($object, $this->parameters['object']);
759
760
        $this->assertSame([], $this->session->getFlashBag()->all());
761
        $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template);
762
    }
763
764
    /**
765
     * @dataProvider getRedirectToTests
766
     */
767
    public function testRedirectTo(
768
        string $expected,
769
        string $route,
770
        array $queryParams,
771
        array $requestParams,
772
        bool $hasActiveSubclass
773
    ): void {
774
        $this->admin
775
            ->method('hasActiveSubclass')
776
            ->willReturn($hasActiveSubclass);
777
778
        $object = new \stdClass();
779
780
        foreach ($queryParams as $key => $value) {
781
            $this->request->query->set($key, $value);
782
        }
783
784
        foreach ($requestParams as $key => $value) {
785
            $this->request->request->set($key, $value);
786
        }
787
788
        $this->admin
789
            ->method('hasRoute')
790
            ->with($this->equalTo($route))
791
            ->willReturn(true);
792
793
        $this->admin
794
            ->method('hasAccess')
795
            ->with($this->equalTo($route))
796
            ->willReturn(true);
797
798
        $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
799
        $this->assertInstanceOf(RedirectResponse::class, $response);
800
        $this->assertSame($expected, $response->getTargetUrl());
801
    }
802
803
    public function testRedirectToWithObject(): void
804
    {
805
        $this->admin
806
            ->method('hasActiveSubclass')
807
            ->willReturn(false);
808
809
        $object = new \stdClass();
810
811
        $this->admin->expects($this->at(0))
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
812
            ->method('hasRoute')
813
            ->with($this->equalTo('edit'))
814
            ->willReturn(true);
815
816
        $this->admin
817
            ->method('hasAccess')
818
            ->with($this->equalTo('edit'), $object)
819
            ->willReturn(false);
820
821
        $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
822
        $this->assertInstanceOf(RedirectResponse::class, $response);
823
        $this->assertSame('list', $response->getTargetUrl());
824
    }
825
826
    public function getRedirectToTests()
827
    {
828
        return [
829
            ['stdClass_edit', 'edit', [], [], false],
830
            ['list', 'list', ['btn_update_and_list' => true], [], false],
831
            ['list', 'list', ['btn_create_and_list' => true], [], false],
832
            ['create', 'create', ['btn_create_and_create' => true], [], false],
833
            ['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], [], true],
834
            ['stdClass_edit?_tab=first_tab', 'edit', ['btn_update_and_edit' => true], ['_tab' => 'first_tab'], false],
835
        ];
836
    }
837
838
    public function testDeleteActionNotFoundException(): void
839
    {
840
        $this->expectException(NotFoundHttpException::class);
841
842
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
843
            ->method('getObject')
844
            ->willReturn(null);
845
846
        $this->controller->deleteAction($this->request);
847
    }
848
849
    public function testDeleteActionAccessDenied(): void
850
    {
851
        $this->expectException(AccessDeniedException::class);
852
853
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
854
            ->method('getObject')
855
            ->willReturn(new \stdClass());
856
857
        $this->admin->expects($this->once())
858
            ->method('checkAccess')
859
            ->with($this->equalTo('delete'))
860
            ->will($this->throwException(new AccessDeniedException()));
861
862
        $this->controller->deleteAction($this->request);
863
    }
864
865
    public function testPreDelete(): void
866
    {
867
        $object = new \stdClass();
868
        $object->foo = 123456;
869
870
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
871
            ->method('getObject')
872
            ->willReturn($object);
873
874
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
875
            ->method('checkAccess')
876
            ->with($this->equalTo('delete'));
877
878
        $controller = new PreCRUDController();
879
        $controller->setContainer($this->container);
880
881
        $response = $controller->deleteAction($this->request);
882
        $this->assertInstanceOf(Response::class, $response);
883
        $this->assertSame('preDelete called: 123456', $response->getContent());
884
    }
885
886
    public function testDeleteAction(): void
887
    {
888
        $object = new \stdClass();
889
890
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
891
            ->method('getObject')
892
            ->willReturn($object);
893
894
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
895
            ->method('checkAccess')
896
            ->with($this->equalTo('delete'));
897
898
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request));
899
900
        $this->assertSame($this->admin, $this->parameters['admin']);
901
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
902
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
903
904
        $this->assertSame('delete', $this->parameters['action']);
905
        $this->assertSame($object, $this->parameters['object']);
906
        $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
907
908
        $this->assertSame([], $this->session->getFlashBag()->all());
909
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
910
    }
911
912
    public function testDeleteActionNoCsrfToken(): void
913
    {
914
        $this->container->set('security.csrf.token_manager', null);
915
916
        $object = new \stdClass();
917
918
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
919
            ->method('getObject')
920
            ->willReturn($object);
921
922
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
923
            ->method('checkAccess')
924
            ->with($this->equalTo('delete'));
925
926
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request));
927
928
        $this->assertSame($this->admin, $this->parameters['admin']);
929
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
930
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
931
932
        $this->assertSame('delete', $this->parameters['action']);
933
        $this->assertSame($object, $this->parameters['object']);
934
        $this->assertFalse($this->parameters['csrf_token']);
935
936
        $this->assertSame([], $this->session->getFlashBag()->all());
937
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
938
    }
939
940
    public function testDeleteActionAjaxSuccess1(): void
941
    {
942
        $object = new \stdClass();
943
944
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
945
            ->method('getObject')
946
            ->willReturn($object);
947
948
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
949
            ->method('checkAccess')
950
            ->with($this->equalTo('delete'));
951
952
        $this->request->setMethod(Request::METHOD_DELETE);
953
954
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
955
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
956
957
        $response = $this->controller->deleteAction($this->request);
958
959
        $this->assertInstanceOf(Response::class, $response);
960
        $this->assertSame(json_encode(['result' => 'ok']), $response->getContent());
961
        $this->assertSame([], $this->session->getFlashBag()->all());
962
    }
963
964
    public function testDeleteActionAjaxSuccess2(): void
965
    {
966
        $object = new \stdClass();
967
968
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
969
            ->method('getObject')
970
            ->willReturn($object);
971
972
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
973
            ->method('checkAccess')
974
            ->with($this->equalTo('delete'));
975
976
        $this->request->setMethod(Request::METHOD_POST);
977
        $this->request->request->set('_method', Request::METHOD_DELETE);
978
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
979
980
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
981
982
        $response = $this->controller->deleteAction($this->request);
983
984
        $this->assertInstanceOf(Response::class, $response);
985
        $this->assertSame(json_encode(['result' => 'ok']), $response->getContent());
986
        $this->assertSame([], $this->session->getFlashBag()->all());
987
    }
988
989
    public function testDeleteActionAjaxError(): void
990
    {
991
        $object = new \stdClass();
992
993
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
994
            ->method('getObject')
995
            ->willReturn($object);
996
997
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
998
            ->method('checkAccess')
999
            ->with($this->equalTo('delete'));
1000
1001
        $this->admin
1002
            ->method('getClass')
1003
            ->willReturn(\stdClass::class);
1004
1005
        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
1006
1007
        $this->request->setMethod(Request::METHOD_DELETE);
1008
1009
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1010
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1011
1012
        $response = $this->controller->deleteAction($this->request);
1013
1014
        $this->assertInstanceOf(Response::class, $response);
1015
        $this->assertSame(json_encode(['result' => 'error']), $response->getContent());
1016
        $this->assertSame([], $this->session->getFlashBag()->all());
1017
    }
1018
1019
    public function testDeleteActionWithModelManagerExceptionInDebugMode(): void
1020
    {
1021
        $this->expectException(ModelManagerException::class);
1022
1023
        $object = new \stdClass();
1024
1025
        $this->admin->expects($this->once())
1026
            ->method('getObject')
1027
            ->willReturn($object);
1028
1029
        $this->admin->expects($this->once())
1030
            ->method('checkAccess')
1031
            ->with($this->equalTo('delete'));
1032
1033
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1034
            ->method('delete')
1035
            ->willReturnCallback(static function (): void {
1036
                throw new ModelManagerException();
1037
            });
1038
1039
        $this->kernel->expects($this->once())
1040
            ->method('isDebug')
1041
            ->willReturn(true);
1042
1043
        $this->request->setMethod(Request::METHOD_DELETE);
1044
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1045
1046
        $this->controller->deleteAction($this->request);
1047
    }
1048
1049
    /**
1050
     * @dataProvider getToStringValues
1051
     */
1052
    public function testDeleteActionSuccess1(string $expectedToStringValue, string $toStringValue): void
1053
    {
1054
        $object = new \stdClass();
1055
1056
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1057
            ->method('getObject')
1058
            ->willReturn($object);
1059
1060
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1061
            ->method('toString')
1062
            ->with($this->equalTo($object))
1063
            ->willReturn($toStringValue);
1064
1065
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1066
1067
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1068
            ->method('checkAccess')
1069
            ->with($this->equalTo('delete'));
1070
1071
        $this->request->setMethod(Request::METHOD_DELETE);
1072
1073
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1074
1075
        $response = $this->controller->deleteAction($this->request);
1076
1077
        $this->assertInstanceOf(RedirectResponse::class, $response);
1078
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1079
        $this->assertSame('list', $response->getTargetUrl());
1080
    }
1081
1082
    /**
1083
     * @dataProvider getToStringValues
1084
     */
1085
    public function testDeleteActionSuccess2(string $expectedToStringValue, string $toStringValue): void
1086
    {
1087
        $object = new \stdClass();
1088
1089
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1090
            ->method('getObject')
1091
            ->willReturn($object);
1092
1093
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1094
            ->method('checkAccess')
1095
            ->with($this->equalTo('delete'));
1096
1097
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1098
            ->method('toString')
1099
            ->with($this->equalTo($object))
1100
            ->willReturn($toStringValue);
1101
1102
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1103
1104
        $this->request->setMethod(Request::METHOD_POST);
1105
        $this->request->request->set('_method', Request::METHOD_DELETE);
1106
1107
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1108
1109
        $response = $this->controller->deleteAction($this->request);
1110
1111
        $this->assertInstanceOf(RedirectResponse::class, $response);
1112
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1113
        $this->assertSame('list', $response->getTargetUrl());
1114
    }
1115
1116
    /**
1117
     * @dataProvider getToStringValues
1118
     */
1119
    public function testDeleteActionSuccessNoCsrfTokenProvider(string $expectedToStringValue, string $toStringValue): void
1120
    {
1121
        $this->container->set('security.csrf.token_manager', null);
1122
1123
        $object = new \stdClass();
1124
1125
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1126
            ->method('getObject')
1127
            ->willReturn($object);
1128
1129
        $this->admin->expects($this->once())
1130
            ->method('checkAccess')
1131
            ->with($this->equalTo('delete'));
1132
1133
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1134
            ->method('toString')
1135
            ->with($this->equalTo($object))
1136
            ->willReturn($toStringValue);
1137
1138
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1139
1140
        $this->request->setMethod(Request::METHOD_POST);
1141
        $this->request->request->set('_method', Request::METHOD_DELETE);
1142
1143
        $response = $this->controller->deleteAction($this->request);
1144
1145
        $this->assertInstanceOf(RedirectResponse::class, $response);
1146
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1147
        $this->assertSame('list', $response->getTargetUrl());
1148
    }
1149
1150
    public function testDeleteActionWrongRequestMethod(): void
1151
    {
1152
        $object = new \stdClass();
1153
1154
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1155
            ->method('getObject')
1156
            ->willReturn($object);
1157
1158
        $this->admin->expects($this->once())
1159
            ->method('checkAccess')
1160
            ->with($this->equalTo('delete'));
1161
1162
        //without POST request parameter "_method" should not be used as real REST method
1163
        $this->request->query->set('_method', Request::METHOD_DELETE);
1164
1165
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction($this->request));
1166
1167
        $this->assertSame($this->admin, $this->parameters['admin']);
1168
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1169
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1170
1171
        $this->assertSame('delete', $this->parameters['action']);
1172
        $this->assertSame($object, $this->parameters['object']);
1173
        $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
1174
1175
        $this->assertSame([], $this->session->getFlashBag()->all());
1176
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
1177
    }
1178
1179
    /**
1180
     * @dataProvider getToStringValues
1181
     */
1182
    public function testDeleteActionError(string $expectedToStringValue, string $toStringValue): void
1183
    {
1184
        $object = new \stdClass();
1185
1186
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1187
            ->method('getObject')
1188
            ->willReturn($object);
1189
1190
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1191
            ->method('checkAccess')
1192
            ->with($this->equalTo('delete'));
1193
1194
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1195
            ->method('toString')
1196
            ->with($this->equalTo($object))
1197
            ->willReturn($toStringValue);
1198
1199
        $this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1200
1201
        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
1202
1203
        $this->request->setMethod(Request::METHOD_DELETE);
1204
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1205
1206
        $response = $this->controller->deleteAction($this->request);
1207
1208
        $this->assertInstanceOf(RedirectResponse::class, $response);
1209
        $this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error'));
1210
        $this->assertSame('list', $response->getTargetUrl());
1211
    }
1212
1213
    public function testDeleteActionInvalidCsrfToken(): void
1214
    {
1215
        $object = new \stdClass();
1216
1217
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1218
            ->method('getObject')
1219
            ->willReturn($object);
1220
1221
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1222
            ->method('checkAccess')
1223
            ->with($this->equalTo('delete'));
1224
1225
        $this->request->setMethod(Request::METHOD_POST);
1226
        $this->request->request->set('_method', Request::METHOD_DELETE);
1227
        $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
1228
1229
        try {
1230
            $this->controller->deleteAction($this->request);
1231
        } catch (HttpException $e) {
1232
            $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
1233
            $this->assertSame(400, $e->getStatusCode());
1234
        }
1235
    }
1236
1237
    public function testEditActionNotFoundException(): void
1238
    {
1239
        $this->expectException(NotFoundHttpException::class);
1240
1241
        $this->admin->expects($this->once())
1242
            ->method('getObject')
1243
            ->willReturn(null);
1244
1245
        $this->controller->editAction($this->request);
1246
    }
1247
1248
    public function testEditActionAccessDenied(): void
1249
    {
1250
        $this->expectException(AccessDeniedException::class);
1251
1252
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1253
            ->method('getObject')
1254
            ->willReturn(new \stdClass());
1255
1256
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1257
            ->method('checkAccess')
1258
            ->with($this->equalTo('edit'))
1259
            ->will($this->throwException(new AccessDeniedException()));
1260
1261
        $this->controller->editAction($this->request);
1262
    }
1263
1264
    public function testPreEdit(): void
1265
    {
1266
        $object = new \stdClass();
1267
        $object->foo = 123456;
1268
1269
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1270
            ->method('getObject')
1271
            ->willReturn($object);
1272
1273
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1274
            ->method('checkAccess')
1275
            ->with($this->equalTo('edit'));
1276
1277
        $controller = new PreCRUDController();
1278
        $controller->setContainer($this->container);
1279
1280
        $response = $controller->editAction($this->request);
1281
        $this->assertInstanceOf(Response::class, $response);
1282
        $this->assertSame('preEdit called: 123456', $response->getContent());
1283
    }
1284
1285
    public function testEditAction(): void
1286
    {
1287
        $object = new \stdClass();
1288
1289
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1290
            ->method('getObject')
1291
            ->willReturn($object);
1292
1293
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1294
            ->method('checkAccess')
1295
            ->with($this->equalTo('edit'));
1296
1297
        $form = $this->createMock(Form::class);
1298
1299
        $this->admin->expects($this->once())
1300
            ->method('getForm')
1301
            ->willReturn($form);
1302
1303
        $formView = $this->createMock(FormView::class);
1304
1305
        $form
1306
            ->method('createView')
1307
            ->willReturn($formView);
1308
1309
        $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request));
1310
1311
        $this->assertSame($this->admin, $this->parameters['admin']);
1312
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1313
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1314
1315
        $this->assertSame('edit', $this->parameters['action']);
1316
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1317
        $this->assertSame($object, $this->parameters['object']);
1318
        $this->assertSame([], $this->session->getFlashBag()->all());
1319
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1320
    }
1321
1322
    /**
1323
     * @dataProvider getToStringValues
1324
     */
1325
    public function testEditActionSuccess(string $expectedToStringValue, string $toStringValue): void
1326
    {
1327
        $object = new \stdClass();
1328
1329
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1330
            ->method('getObject')
1331
            ->willReturn($object);
1332
1333
        $this->admin->expects($this->once())
1334
            ->method('update')
1335
            ->willReturnArgument(0);
1336
1337
        $this->admin->expects($this->once())
1338
            ->method('checkAccess')
1339
            ->with($this->equalTo('edit'));
1340
1341
        $this->admin->expects($this->once())
1342
            ->method('hasRoute')
1343
            ->with($this->equalTo('edit'))
1344
            ->willReturn(true);
1345
1346
        $this->admin->expects($this->once())
1347
            ->method('hasAccess')
1348
            ->with($this->equalTo('edit'))
1349
            ->willReturn(true);
1350
1351
        $form = $this->createMock(Form::class);
1352
1353
        $form->expects($this->once())
1354
            ->method('getData')
1355
            ->willReturn($object);
1356
1357
        $this->admin->expects($this->once())
1358
            ->method('getForm')
1359
            ->willReturn($form);
1360
1361
        $form->expects($this->once())
1362
            ->method('isSubmitted')
1363
            ->willReturn(true);
1364
1365
        $form->expects($this->once())
1366
            ->method('isValid')
1367
            ->willReturn(true);
1368
1369
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1370
            ->method('toString')
1371
            ->with($this->equalTo($object))
1372
            ->willReturn($toStringValue);
1373
1374
        $this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1375
1376
        $this->request->setMethod(Request::METHOD_POST);
1377
1378
        $response = $this->controller->editAction($this->request);
1379
1380
        $this->assertInstanceOf(RedirectResponse::class, $response);
1381
        $this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1382
        $this->assertSame('stdClass_edit', $response->getTargetUrl());
1383
    }
1384
1385
    /**
1386
     * @dataProvider getToStringValues
1387
     */
1388
    public function testEditActionError(string $expectedToStringValue, string $toStringValue): void
1389
    {
1390
        $object = new \stdClass();
1391
1392
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1393
            ->method('getObject')
1394
            ->willReturn($object);
1395
1396
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1397
            ->method('checkAccess')
1398
            ->with($this->equalTo('edit'));
1399
1400
        $form = $this->createMock(Form::class);
1401
1402
        $this->admin->expects($this->once())
1403
            ->method('getForm')
1404
            ->willReturn($form);
1405
1406
        $form->expects($this->once())
1407
            ->method('isSubmitted')
1408
            ->willReturn(true);
1409
1410
        $form->expects($this->once())
1411
            ->method('isValid')
1412
            ->willReturn(false);
1413
1414
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1415
            ->method('toString')
1416
            ->with($this->equalTo($object))
1417
            ->willReturn($toStringValue);
1418
1419
        $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1420
1421
        $this->request->setMethod(Request::METHOD_POST);
1422
1423
        $formView = $this->createMock(FormView::class);
1424
1425
        $form
1426
            ->method('createView')
1427
            ->willReturn($formView);
1428
1429
        $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request));
1430
1431
        $this->assertSame($this->admin, $this->parameters['admin']);
1432
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1433
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1434
1435
        $this->assertSame('edit', $this->parameters['action']);
1436
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1437
        $this->assertSame($object, $this->parameters['object']);
1438
1439
        $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all());
1440
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1441
    }
1442
1443
    public function testEditActionAjaxSuccess(): void
1444
    {
1445
        $object = new \stdClass();
1446
1447
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1448
            ->method('getObject')
1449
            ->willReturn($object);
1450
1451
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1452
            ->method('update')
1453
            ->willReturnArgument(0);
1454
1455
        $this->admin->expects($this->once())
1456
            ->method('checkAccess')
1457
            ->with($this->equalTo('edit'));
1458
1459
        $form = $this->createMock(Form::class);
1460
1461
        $this->admin->expects($this->once())
1462
            ->method('getForm')
1463
            ->willReturn($form);
1464
1465
        $form->expects($this->once())
1466
            ->method('isSubmitted')
1467
            ->willReturn(true);
1468
1469
        $form->expects($this->once())
1470
            ->method('isValid')
1471
            ->willReturn(true);
1472
1473
        $form->expects($this->once())
1474
            ->method('getData')
1475
            ->willReturn($object);
1476
1477
        $this->admin
1478
            ->method('getNormalizedIdentifier')
1479
            ->with($this->equalTo($object))
1480
            ->willReturn('foo_normalized');
1481
1482
        $this->admin->expects($this->once())
1483
            ->method('toString')
1484
            ->willReturn('foo');
1485
1486
        $this->request->setMethod(Request::METHOD_POST);
1487
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1488
        $this->request->headers->set('Accept', 'application/json');
1489
1490
        $response = $this->controller->editAction($this->request);
1491
1492
        $this->assertInstanceOf(Response::class, $response);
1493
        $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent());
1494
        $this->assertSame([], $this->session->getFlashBag()->all());
1495
    }
1496
1497
    public function testEditActionAjaxError(): void
1498
    {
1499
        $object = new \stdClass();
1500
1501
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1502
            ->method('getObject')
1503
            ->willReturn($object);
1504
1505
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1506
            ->method('checkAccess')
1507
            ->with($this->equalTo('edit'));
1508
1509
        $form = $this->createMock(Form::class);
1510
1511
        $this->admin->expects($this->once())
1512
            ->method('getForm')
1513
            ->willReturn($form);
1514
1515
        $form->expects($this->once())
1516
            ->method('isSubmitted')
1517
            ->willReturn(true);
1518
1519
        $form->expects($this->once())
1520
            ->method('isValid')
1521
            ->willReturn(false);
1522
1523
        $formError = $this->createMock(FormError::class);
1524
        $formError->expects($this->atLeastOnce())
1525
            ->method('getMessage')
1526
            ->willReturn('Form error message');
1527
1528
        $form->expects($this->once())
1529
            ->method('getErrors')
1530
            ->with(true)
1531
            ->willReturn([$formError]);
1532
1533
        $this->request->setMethod(Request::METHOD_POST);
1534
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1535
        $this->request->headers->set('Accept', 'application/json');
1536
1537
        $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->editAction($this->request));
1538
        $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent());
1539
    }
1540
1541
    public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
1542
    {
1543
        $object = new \stdClass();
1544
1545
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1546
            ->method('getObject')
1547
            ->willReturn($object);
1548
1549
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1550
            ->method('checkAccess')
1551
            ->with($this->equalTo('edit'));
1552
1553
        $form = $this->createMock(Form::class);
1554
1555
        $this->admin->expects($this->once())
1556
            ->method('getForm')
1557
            ->willReturn($form);
1558
1559
        $form->expects($this->once())
1560
            ->method('isSubmitted')
1561
            ->willReturn(true);
1562
1563
        $form->expects($this->once())
1564
            ->method('isValid')
1565
            ->willReturn(false);
1566
1567
        $this->request->setMethod(Request::METHOD_POST);
1568
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1569
1570
        $formView = $this->createMock(FormView::class);
1571
        $form
1572
            ->method('createView')
1573
            ->willReturn($formView);
1574
1575
        $this->assertInstanceOf(Response::class, $response = $this->controller->editAction($this->request));
1576
        $this->assertSame(Response::HTTP_NOT_ACCEPTABLE, $response->getStatusCode());
1577
    }
1578
1579
    /**
1580
     * @dataProvider getToStringValues
1581
     */
1582
    public function testEditActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void
1583
    {
1584
        $object = new \stdClass();
1585
1586
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1587
            ->method('getObject')
1588
            ->willReturn($object);
1589
1590
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1591
            ->method('checkAccess')
1592
            ->with($this->equalTo('edit'));
1593
1594
        $this->admin
1595
            ->method('getClass')
1596
            ->willReturn(\stdClass::class);
1597
1598
        $form = $this->createMock(Form::class);
1599
1600
        $this->admin->expects($this->once())
1601
            ->method('getForm')
1602
            ->willReturn($form);
1603
1604
        $form->expects($this->once())
1605
            ->method('isValid')
1606
            ->willReturn(true);
1607
1608
        $form->expects($this->once())
1609
            ->method('getData')
1610
            ->willReturn($object);
1611
1612
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1613
            ->method('toString')
1614
            ->with($this->equalTo($object))
1615
            ->willReturn($toStringValue);
1616
1617
        $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1618
1619
        $form->expects($this->once())
1620
            ->method('isSubmitted')
1621
            ->willReturn(true);
1622
        $this->request->setMethod(Request::METHOD_POST);
1623
1624
        $formView = $this->createMock(FormView::class);
1625
1626
        $form
1627
            ->method('createView')
1628
            ->willReturn($formView);
1629
1630
        $this->assertLoggerLogsModelManagerException($this->admin, 'update');
1631
        $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request));
1632
1633
        $this->assertSame($this->admin, $this->parameters['admin']);
1634
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1635
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1636
1637
        $this->assertSame('edit', $this->parameters['action']);
1638
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1639
        $this->assertSame($object, $this->parameters['object']);
1640
1641
        $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all());
1642
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1643
    }
1644
1645
    public function testEditActionWithPreview(): void
1646
    {
1647
        $object = new \stdClass();
1648
1649
        $this->admin->expects($this->once())
1650
            ->method('getObject')
1651
            ->willReturn($object);
1652
1653
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1654
            ->method('checkAccess')
1655
            ->with($this->equalTo('edit'));
1656
1657
        $form = $this->createMock(Form::class);
1658
1659
        $this->admin->expects($this->once())
1660
            ->method('getForm')
1661
            ->willReturn($form);
1662
1663
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1664
            ->method('supportsPreviewMode')
1665
            ->willReturn(true);
1666
1667
        $formView = $this->createMock(FormView::class);
1668
1669
        $form
1670
            ->method('createView')
1671
            ->willReturn($formView);
1672
1673
        $form->expects($this->once())
1674
            ->method('isSubmitted')
1675
            ->willReturn(true);
1676
1677
        $form->expects($this->once())
1678
            ->method('isValid')
1679
            ->willReturn(true);
1680
1681
        $this->request->setMethod(Request::METHOD_POST);
1682
        $this->request->request->set('btn_preview', 'Preview');
1683
1684
        $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request));
1685
1686
        $this->assertSame($this->admin, $this->parameters['admin']);
1687
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1688
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1689
1690
        $this->assertSame('edit', $this->parameters['action']);
1691
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1692
        $this->assertSame($object, $this->parameters['object']);
1693
1694
        $this->assertSame([], $this->session->getFlashBag()->all());
1695
        $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template);
1696
    }
1697
1698
    public function testEditActionWithLockException(): void
1699
    {
1700
        $object = new \stdClass();
1701
        $class = \get_class($object);
1702
1703
        $this->admin
1704
            ->method('getObject')
1705
            ->willReturn($object);
1706
1707
        $this->admin
1708
            ->method('checkAccess')
1709
            ->with($this->equalTo('edit'));
1710
1711
        $this->admin
1712
            ->method('getClass')
1713
            ->willReturn($class);
1714
1715
        $form = $this->createMock(Form::class);
1716
1717
        $form
1718
            ->method('isValid')
1719
            ->willReturn(true);
1720
1721
        $form->expects($this->once())
1722
            ->method('getData')
1723
            ->willReturn($object);
1724
1725
        $this->admin
1726
            ->method('getForm')
1727
            ->willReturn($form);
1728
1729
        $form
1730
            ->method('isSubmitted')
1731
            ->willReturn(true);
1732
        $this->request->setMethod(Request::METHOD_POST);
1733
1734
        $this->admin
1735
            ->method('update')
1736
            ->will($this->throwException(new LockException()));
1737
1738
        $this->admin
1739
            ->method('toString')
1740
            ->with($this->equalTo($object))
1741
            ->willReturn($class);
1742
1743
        $formView = $this->createMock(FormView::class);
1744
1745
        $form
1746
            ->method('createView')
1747
            ->willReturn($formView);
1748
1749
        $this->expectTranslate('flash_lock_error', [
1750
            '%name%' => $class,
1751
            '%link_start%' => '<a href="stdClass_edit">',
1752
            '%link_end%' => '</a>',
1753
        ], 'SonataAdminBundle');
1754
1755
        $this->assertInstanceOf(Response::class, $this->controller->editAction($this->request));
1756
    }
1757
1758
    public function testCreateActionAccessDenied(): void
1759
    {
1760
        $this->expectException(AccessDeniedException::class);
1761
1762
        $this->admin->expects($this->once())
1763
            ->method('checkAccess')
1764
            ->with($this->equalTo('create'))
1765
            ->will($this->throwException(new AccessDeniedException()));
1766
1767
        $this->controller->createAction($this->request);
1768
    }
1769
1770
    public function testPreCreate(): void
1771
    {
1772
        $object = new \stdClass();
1773
        $object->foo = 123456;
1774
1775
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1776
            ->method('checkAccess')
1777
            ->with($this->equalTo('create'));
1778
1779
        $this->admin
1780
            ->method('getClass')
1781
            ->willReturn(\stdClass::class);
1782
1783
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1784
            ->method('getNewInstance')
1785
            ->willReturn($object);
1786
1787
        $controller = new PreCRUDController();
1788
        $controller->setContainer($this->container);
1789
1790
        $response = $controller->createAction($this->request);
1791
        $this->assertInstanceOf(Response::class, $response);
1792
        $this->assertSame('preCreate called: 123456', $response->getContent());
1793
    }
1794
1795
    public function testCreateAction(): void
1796
    {
1797
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1798
            ->method('checkAccess')
1799
            ->with($this->equalTo('create'));
1800
1801
        $object = new \stdClass();
1802
1803
        $this->admin
1804
            ->method('getClass')
1805
            ->willReturn(\stdClass::class);
1806
1807
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1808
            ->method('getNewInstance')
1809
            ->willReturn($object);
1810
1811
        $form = $this->createMock(Form::class);
1812
1813
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1814
            ->method('getForm')
1815
            ->willReturn($form);
1816
1817
        $formView = $this->createMock(FormView::class);
1818
1819
        $form
1820
            ->method('createView')
1821
            ->willReturn($formView);
1822
1823
        $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request));
1824
1825
        $this->assertSame($this->admin, $this->parameters['admin']);
1826
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1827
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1828
1829
        $this->assertSame('create', $this->parameters['action']);
1830
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1831
        $this->assertSame($object, $this->parameters['object']);
1832
1833
        $this->assertSame([], $this->session->getFlashBag()->all());
1834
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1835
    }
1836
1837
    /**
1838
     * @dataProvider getToStringValues
1839
     */
1840
    public function testCreateActionSuccess(string $expectedToStringValue, string $toStringValue): void
1841
    {
1842
        $object = new \stdClass();
1843
1844
        $this->admin->expects($this->exactly(2))
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1845
            ->method('checkAccess')
1846
            ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): void {
1847
                if ('edit' === $name) {
1848
                    return;
1849
                }
1850
1851
                if ('create' !== $name) {
1852
                    throw new AccessDeniedException();
1853
                }
1854
1855
                if (null === $objectIn) {
1856
                    return;
1857
                }
1858
1859
                if ($objectIn !== $object) {
1860
                    throw new AccessDeniedException();
1861
                }
1862
            });
1863
1864
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1865
            ->method('hasRoute')
1866
            ->with($this->equalTo('edit'))
1867
            ->willReturn(true);
1868
1869
        $this->admin->expects($this->once())
1870
            ->method('hasAccess')
1871
            ->with($this->equalTo('edit'))
1872
            ->willReturn(true);
1873
1874
        $this->admin->expects($this->once())
1875
            ->method('getNewInstance')
1876
            ->willReturn($object);
1877
1878
        $this->admin->expects($this->once())
1879
            ->method('create')
1880
            ->willReturnArgument(0);
1881
1882
        $form = $this->createMock(Form::class);
1883
1884
        $this->admin
1885
            ->method('getClass')
1886
            ->willReturn(\stdClass::class);
1887
1888
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1889
            ->method('getForm')
1890
            ->willReturn($form);
1891
1892
        $form->expects($this->once())
1893
            ->method('isSubmitted')
1894
            ->willReturn(true);
1895
1896
        $form->expects($this->once())
1897
            ->method('isValid')
1898
            ->willReturn(true);
1899
1900
        $form->expects($this->once())
1901
            ->method('getData')
1902
            ->willReturn($object);
1903
1904
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1905
            ->method('toString')
1906
            ->with($this->equalTo($object))
1907
            ->willReturn($toStringValue);
1908
1909
        $this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1910
1911
        $this->request->setMethod(Request::METHOD_POST);
1912
1913
        $response = $this->controller->createAction($this->request);
1914
1915
        $this->assertInstanceOf(RedirectResponse::class, $response);
1916
        $this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1917
        $this->assertSame('stdClass_edit', $response->getTargetUrl());
1918
    }
1919
1920
    public function testCreateActionAccessDenied2(): void
1921
    {
1922
        $this->expectException(AccessDeniedException::class);
1923
1924
        $object = new \stdClass();
1925
1926
        $this->admin
1927
            ->method('checkAccess')
1928
            ->willReturnCallback(static function (string $name, $object = null): void {
1929
                if ('create' !== $name) {
1930
                    throw new AccessDeniedException();
1931
                }
1932
                if (null === $object) {
1933
                    return;
1934
                }
1935
1936
                throw new AccessDeniedException();
1937
            });
1938
1939
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1940
            ->method('getNewInstance')
1941
            ->willReturn($object);
1942
1943
        $form = $this->createMock(Form::class);
1944
1945
        $this->admin
1946
            ->method('getClass')
1947
            ->willReturn(\stdClass::class);
1948
1949
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1950
            ->method('getForm')
1951
            ->willReturn($form);
1952
1953
        $form->expects($this->once())
1954
            ->method('isSubmitted')
1955
            ->willReturn(true);
1956
1957
        $form->expects($this->once())
1958
            ->method('getData')
1959
            ->willReturn($object);
1960
1961
        $form->expects($this->once())
1962
            ->method('isValid')
1963
            ->willReturn(true);
1964
1965
        $this->request->setMethod(Request::METHOD_POST);
1966
1967
        $this->controller->createAction($this->request);
1968
    }
1969
1970
    /**
1971
     * @dataProvider getToStringValues
1972
     */
1973
    public function testCreateActionError(string $expectedToStringValue, string $toStringValue): void
1974
    {
1975
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1976
            ->method('checkAccess')
1977
            ->with($this->equalTo('create'));
1978
1979
        $object = new \stdClass();
1980
1981
        $this->admin
1982
            ->method('getClass')
1983
            ->willReturn(\stdClass::class);
1984
1985
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1986
            ->method('getNewInstance')
1987
            ->willReturn($object);
1988
1989
        $form = $this->createMock(Form::class);
1990
1991
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
1992
            ->method('getForm')
1993
            ->willReturn($form);
1994
1995
        $form->expects($this->once())
1996
            ->method('isSubmitted')
1997
            ->willReturn(true);
1998
1999
        $form->expects($this->once())
2000
            ->method('isValid')
2001
            ->willReturn(false);
2002
2003
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2004
            ->method('toString')
2005
            ->with($this->equalTo($object))
2006
            ->willReturn($toStringValue);
2007
2008
        $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2009
2010
        $this->request->setMethod(Request::METHOD_POST);
2011
2012
        $formView = $this->createMock(FormView::class);
2013
2014
        $form
2015
            ->method('createView')
2016
            ->willReturn($formView);
2017
2018
        $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request));
2019
2020
        $this->assertSame($this->admin, $this->parameters['admin']);
2021
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2022
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2023
2024
        $this->assertSame('create', $this->parameters['action']);
2025
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2026
        $this->assertSame($object, $this->parameters['object']);
2027
2028
        $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all());
2029
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2030
    }
2031
2032
    /**
2033
     * @dataProvider getToStringValues
2034
     */
2035
    public function testCreateActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void
2036
    {
2037
        $this->admin->expects($this->exactly(2))
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2038
            ->method('checkAccess')
2039
            ->with($this->equalTo('create'));
2040
2041
        $this->admin
2042
            ->method('getClass')
2043
            ->willReturn(\stdClass::class);
2044
2045
        $object = new \stdClass();
2046
2047
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2048
            ->method('getNewInstance')
2049
            ->willReturn($object);
2050
2051
        $form = $this->createMock(Form::class);
2052
2053
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2054
            ->method('getForm')
2055
            ->willReturn($form);
2056
2057
        $form->expects($this->once())
2058
            ->method('isValid')
2059
            ->willReturn(true);
2060
2061
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2062
            ->method('toString')
2063
            ->with($this->equalTo($object))
2064
            ->willReturn($toStringValue);
2065
2066
        $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2067
2068
        $form->expects($this->once())
2069
            ->method('isSubmitted')
2070
            ->willReturn(true);
2071
2072
        $form->expects($this->once())
2073
            ->method('getData')
2074
            ->willReturn($object);
2075
2076
        $this->request->setMethod(Request::METHOD_POST);
2077
2078
        $formView = $this->createMock(FormView::class);
2079
2080
        $form
2081
            ->method('createView')
2082
            ->willReturn($formView);
2083
2084
        $this->assertLoggerLogsModelManagerException($this->admin, 'create');
2085
2086
        $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request));
2087
2088
        $this->assertSame($this->admin, $this->parameters['admin']);
2089
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2090
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2091
2092
        $this->assertSame('create', $this->parameters['action']);
2093
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2094
        $this->assertSame($object, $this->parameters['object']);
2095
2096
        $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all());
2097
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2098
    }
2099
2100
    public function testCreateActionAjaxSuccess(): void
2101
    {
2102
        $object = new \stdClass();
2103
2104
        $this->admin->expects($this->exactly(2))
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2105
            ->method('checkAccess')
2106
            ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): void {
2107
                if ('create' !== $name) {
2108
                    throw new AccessDeniedException();
2109
                }
2110
2111
                if (null === $objectIn) {
2112
                    return;
2113
                }
2114
2115
                if ($objectIn !== $object) {
2116
                    throw new AccessDeniedException();
2117
                }
2118
            });
2119
2120
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2121
            ->method('getNewInstance')
2122
            ->willReturn($object);
2123
2124
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2125
            ->method('create')
2126
            ->willReturnArgument(0);
2127
2128
        $form = $this->createMock(Form::class);
2129
2130
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2131
            ->method('getForm')
2132
            ->willReturn($form);
2133
2134
        $form->expects($this->once())
2135
            ->method('isSubmitted')
2136
            ->willReturn(true);
2137
2138
        $form->expects($this->once())
2139
            ->method('isValid')
2140
            ->willReturn(true);
2141
2142
        $form->expects($this->once())
2143
            ->method('getData')
2144
            ->willReturn($object);
2145
2146
        $this->admin
2147
            ->method('getClass')
2148
            ->willReturn(\stdClass::class);
2149
2150
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2151
            ->method('getNormalizedIdentifier')
2152
            ->with($this->equalTo($object))
2153
            ->willReturn('foo_normalized');
2154
2155
        $this->admin->expects($this->once())
2156
            ->method('toString')
2157
            ->willReturn('foo');
2158
2159
        $this->request->setMethod(Request::METHOD_POST);
2160
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2161
        $this->request->headers->set('Accept', 'application/json');
2162
2163
        $response = $this->controller->createAction($this->request);
2164
2165
        $this->assertInstanceOf(Response::class, $response);
2166
        $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent());
2167
        $this->assertSame([], $this->session->getFlashBag()->all());
2168
    }
2169
2170
    public function testCreateActionAjaxError(): void
2171
    {
2172
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2173
            ->method('checkAccess')
2174
            ->with($this->equalTo('create'));
2175
2176
        $object = new \stdClass();
2177
2178
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2179
            ->method('getNewInstance')
2180
            ->willReturn($object);
2181
2182
        $form = $this->createMock(Form::class);
2183
2184
        $this->admin
2185
            ->method('getClass')
2186
            ->willReturn(\stdClass::class);
2187
2188
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2189
            ->method('getForm')
2190
            ->willReturn($form);
2191
2192
        $form->expects($this->once())
2193
            ->method('isSubmitted')
2194
            ->willReturn(true);
2195
2196
        $form->expects($this->once())
2197
            ->method('isValid')
2198
            ->willReturn(false);
2199
2200
        $formError = $this->createMock(FormError::class);
2201
        $formError->expects($this->atLeastOnce())
2202
            ->method('getMessage')
2203
            ->willReturn('Form error message');
2204
2205
        $form->expects($this->once())
2206
            ->method('getErrors')
2207
            ->with(true)
2208
            ->willReturn([$formError]);
2209
2210
        $this->request->setMethod(Request::METHOD_POST);
2211
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2212
        $this->request->headers->set('Accept', 'application/json');
2213
2214
        $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->createAction($this->request));
2215
        $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent());
2216
    }
2217
2218
    public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void
2219
    {
2220
        $this->admin->expects($this->once())
2221
            ->method('checkAccess')
2222
            ->with($this->equalTo('create'));
2223
2224
        $object = new \stdClass();
2225
2226
        $this->admin->expects($this->once())
2227
            ->method('getNewInstance')
2228
            ->willReturn($object);
2229
2230
        $form = $this->createMock(Form::class);
2231
2232
        $this->admin
2233
            ->method('getClass')
2234
            ->willReturn(\stdClass::class);
2235
2236
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2237
            ->method('getForm')
2238
            ->willReturn($form);
2239
2240
        $form->expects($this->once())
2241
            ->method('isSubmitted')
2242
            ->willReturn(true);
2243
2244
        $form->expects($this->once())
2245
            ->method('isValid')
2246
            ->willReturn(false);
2247
2248
        $this->request->setMethod(Request::METHOD_POST);
2249
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2250
2251
        $formView = $this->createMock(FormView::class);
2252
        $form
2253
            ->method('createView')
2254
            ->willReturn($formView);
2255
2256
        $this->assertInstanceOf(Response::class, $response = $this->controller->createAction($this->request));
2257
        $this->assertSame(Response::HTTP_NOT_ACCEPTABLE, $response->getStatusCode());
2258
    }
2259
2260
    public function testCreateActionWithPreview(): void
2261
    {
2262
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2263
            ->method('checkAccess')
2264
            ->with($this->equalTo('create'));
2265
2266
        $object = new \stdClass();
2267
2268
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2269
            ->method('getNewInstance')
2270
            ->willReturn($object);
2271
2272
        $form = $this->createMock(Form::class);
2273
2274
        $this->admin
2275
            ->method('getClass')
2276
            ->willReturn(\stdClass::class);
2277
2278
        $this->admin->expects($this->once())
2279
            ->method('getForm')
2280
            ->willReturn($form);
2281
2282
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2283
            ->method('supportsPreviewMode')
2284
            ->willReturn(true);
2285
2286
        $formView = $this->createMock(FormView::class);
2287
2288
        $form
2289
            ->method('createView')
2290
            ->willReturn($formView);
2291
2292
        $form->expects($this->once())
2293
            ->method('isSubmitted')
2294
            ->willReturn(true);
2295
2296
        $form->expects($this->once())
2297
            ->method('isValid')
2298
            ->willReturn(true);
2299
2300
        $this->request->setMethod(Request::METHOD_POST);
2301
        $this->request->request->set('btn_preview', 'Preview');
2302
2303
        $this->assertInstanceOf(Response::class, $this->controller->createAction($this->request));
2304
2305
        $this->assertSame($this->admin, $this->parameters['admin']);
2306
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2307
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2308
2309
        $this->assertSame('create', $this->parameters['action']);
2310
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2311
        $this->assertSame($object, $this->parameters['object']);
2312
2313
        $this->assertSame([], $this->session->getFlashBag()->all());
2314
        $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template);
2315
    }
2316
2317
    public function testExportActionAccessDenied(): void
2318
    {
2319
        $this->expectException(AccessDeniedException::class);
2320
2321
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2322
            ->method('checkAccess')
2323
            ->with($this->equalTo('export'))
2324
            ->will($this->throwException(new AccessDeniedException()));
2325
2326
        $this->controller->exportAction($this->request);
2327
    }
2328
2329
    public function testExportActionWrongFormat(): void
2330
    {
2331
        $this->expectException(\RuntimeException::class);
2332
        $this->expectExceptionMessage(
2333
            'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`'
2334
        );
2335
2336
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2337
            ->method('checkAccess')
2338
            ->with($this->equalTo('export'));
2339
2340
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2341
            ->method('getExportFormats')
2342
            ->willReturn(['json']);
2343
2344
        $this->admin
2345
            ->method('getClass')
2346
            ->willReturn('Foo');
2347
2348
        $this->request->query->set('format', 'csv');
2349
2350
        $this->controller->exportAction($this->request);
2351
    }
2352
2353
    public function testExportAction(): void
2354
    {
2355
        $this->admin->expects($this->once())
2356
            ->method('checkAccess')
2357
            ->with($this->equalTo('export'));
2358
2359
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2360
            ->method('getExportFormats')
2361
            ->willReturn(['json']);
2362
2363
        $this->admin->expects($this->once())
2364
            ->method('getClass')
2365
            ->willReturn(\stdClass::class);
2366
2367
        $dataSourceIterator = $this->createMock(SourceIteratorInterface::class);
2368
2369
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2370
            ->method('getDataSourceIterator')
2371
            ->willReturn($dataSourceIterator);
2372
2373
        $this->request->query->set('format', 'json');
2374
2375
        $response = $this->controller->exportAction($this->request);
2376
        $this->assertInstanceOf(StreamedResponse::class, $response);
2377
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
2378
        $this->assertSame([], $this->session->getFlashBag()->all());
2379
    }
2380
2381
    public function testHistoryActionAccessDenied(): void
2382
    {
2383
        $this->expectException(AccessDeniedException::class);
2384
2385
        $this->admin
2386
            ->method('getObject')
2387
            ->willReturn(new \stdClass());
2388
2389
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2390
            ->method('checkAccess')
2391
            ->with($this->equalTo('history'))
2392
            ->will($this->throwException(new AccessDeniedException()));
2393
2394
        $this->controller->historyAction($this->request);
2395
    }
2396
2397
    public function testHistoryActionNotFoundException(): void
2398
    {
2399
        $this->expectException(NotFoundHttpException::class);
2400
2401
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2402
            ->method('getObject')
2403
            ->willReturn(null);
2404
2405
        $this->controller->historyAction($this->request);
2406
    }
2407
2408
    public function testHistoryActionNoReader(): void
2409
    {
2410
        $this->expectException(NotFoundHttpException::class);
2411
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
2412
2413
        $this->request->query->set('id', 123);
2414
2415
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2416
            ->method('checkAccess')
2417
            ->with($this->equalTo('history'));
2418
2419
        $object = new \stdClass();
2420
2421
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2422
            ->method('getObject')
2423
            ->willReturn($object);
2424
2425
        $this->admin
2426
            ->method('getClass')
2427
            ->willReturn('Foo');
2428
2429
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2430
            ->method('hasReader')
2431
            ->with($this->equalTo('Foo'))
2432
            ->willReturn(false);
2433
2434
        $this->controller->historyAction($this->request);
2435
    }
2436
2437
    public function testHistoryAction(): void
2438
    {
2439
        $this->request->query->set('id', 123);
2440
2441
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2442
            ->method('checkAccess')
2443
            ->with($this->equalTo('history'));
2444
2445
        $object = new \stdClass();
2446
2447
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2448
            ->method('getObject')
2449
            ->willReturn($object);
2450
2451
        $this->admin
2452
            ->method('getClass')
2453
            ->willReturn('Foo');
2454
2455
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2456
            ->method('hasReader')
2457
            ->with($this->equalTo('Foo'))
2458
            ->willReturn(true);
2459
2460
        $reader = $this->createMock(AuditReaderInterface::class);
2461
2462
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2463
            ->method('getReader')
2464
            ->with($this->equalTo('Foo'))
2465
            ->willReturn($reader);
2466
2467
        $reader->expects($this->once())
2468
            ->method('findRevisions')
2469
            ->with($this->equalTo('Foo'), $this->equalTo(123))
2470
            ->willReturn([]);
2471
2472
        $this->assertInstanceOf(Response::class, $this->controller->historyAction($this->request));
2473
2474
        $this->assertSame($this->admin, $this->parameters['admin']);
2475
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2476
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2477
2478
        $this->assertSame('history', $this->parameters['action']);
2479
        $this->assertSame([], $this->parameters['revisions']);
2480
        $this->assertSame($object, $this->parameters['object']);
2481
2482
        $this->assertSame([], $this->session->getFlashBag()->all());
2483
        $this->assertSame('@SonataAdmin/CRUD/history.html.twig', $this->template);
2484
    }
2485
2486
    public function testAclActionAclNotEnabled(): void
2487
    {
2488
        $this->expectException(NotFoundHttpException::class);
2489
        $this->expectExceptionMessage('ACL are not enabled for this admin');
2490
2491
        $this->controller->aclAction($this->request);
2492
    }
2493
2494
    public function testAclActionNotFoundException(): void
2495
    {
2496
        $this->expectException(NotFoundHttpException::class);
2497
2498
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2499
            ->method('isAclEnabled')
2500
            ->willReturn(true);
2501
2502
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2503
            ->method('getObject')
2504
            ->willReturn(null);
2505
2506
        $this->controller->aclAction($this->request);
2507
    }
2508
2509
    public function testAclActionAccessDenied(): void
2510
    {
2511
        $this->expectException(AccessDeniedException::class);
2512
2513
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2514
            ->method('isAclEnabled')
2515
            ->willReturn(true);
2516
2517
        $object = new \stdClass();
2518
2519
        $this->admin->expects($this->once())
2520
            ->method('getObject')
2521
            ->willReturn($object);
2522
2523
        $this->admin->expects($this->once())
2524
            ->method('checkAccess')
2525
            ->with($this->equalTo('acl'), $this->equalTo($object))
2526
            ->will($this->throwException(new AccessDeniedException()));
2527
2528
        $this->controller->aclAction($this->request);
2529
    }
2530
2531
    public function testAclAction(): void
2532
    {
2533
        $this->request->query->set('id', 123);
2534
2535
        $this->admin->expects($this->exactly(2))
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2536
            ->method('isAclEnabled')
2537
            ->willReturn(true);
2538
2539
        $object = new \stdClass();
2540
2541
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2542
            ->method('getObject')
2543
            ->willReturn($object);
2544
2545
        $this->admin
2546
            ->expects($this->once())
2547
            ->method('checkAccess');
2548
2549
        $this->admin
2550
            ->method('getSecurityInformation')
2551
            ->willReturn([]);
2552
2553
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...inObjectAclManipulator>.

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.

Loading history...
2554
            ->method('getMaskBuilderClass')
2555
            ->willReturn(AdminPermissionMap::class);
2556
2557
        $aclUsersForm = $this->getMockBuilder(Form::class)
2558
            ->disableOriginalConstructor()
2559
            ->getMock();
2560
2561
        $aclUsersForm->expects($this->once())
2562
            ->method('createView')
2563
            ->willReturn($this->createMock(FormView::class));
2564
2565
        $aclRolesForm = $this->getMockBuilder(Form::class)
2566
            ->disableOriginalConstructor()
2567
            ->getMock();
2568
2569
        $aclRolesForm->expects($this->once())
2570
            ->method('createView')
2571
            ->willReturn($this->createMock(FormView::class));
2572
2573
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...inObjectAclManipulator>.

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.

Loading history...
2574
            ->method('createAclUsersForm')
2575
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2576
            ->willReturn($aclUsersForm);
2577
2578
        $this->adminObjectAclManipulator->expects($this->once())
2579
            ->method('createAclRolesForm')
2580
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2581
            ->willReturn($aclRolesForm);
2582
2583
        $aclSecurityHandler = $this->createMock(AclSecurityHandlerInterface::class);
2584
2585
        $aclSecurityHandler
2586
            ->method('getObjectPermissions')
2587
            ->willReturn([]);
2588
2589
        $this->admin
2590
            ->method('getSecurityHandler')
2591
            ->willReturn($aclSecurityHandler);
2592
2593
        $this->assertInstanceOf(Response::class, $this->controller->aclAction($this->request));
2594
2595
        $this->assertSame($this->admin, $this->parameters['admin']);
2596
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2597
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2598
2599
        $this->assertSame('acl', $this->parameters['action']);
2600
        $this->assertSame([], $this->parameters['permissions']);
2601
        $this->assertSame($object, $this->parameters['object']);
2602
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']);
2603
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']);
2604
        $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']);
2605
        $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']);
2606
2607
        $this->assertSame([], $this->session->getFlashBag()->all());
2608
        $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template);
2609
    }
2610
2611
    public function testAclActionInvalidUpdate(): void
2612
    {
2613
        $this->request->query->set('id', 123);
2614
        $this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, []);
2615
2616
        $this->admin->expects($this->exactly(2))
2617
            ->method('isAclEnabled')
2618
            ->willReturn(true);
2619
2620
        $object = new \stdClass();
2621
2622
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2623
            ->method('getObject')
2624
            ->willReturn($object);
2625
2626
        $this->admin
2627
            ->expects($this->once())
2628
            ->method('checkAccess');
2629
2630
        $this->admin
2631
            ->method('getSecurityInformation')
2632
            ->willReturn([]);
2633
2634
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...inObjectAclManipulator>.

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.

Loading history...
2635
            ->method('getMaskBuilderClass')
2636
            ->willReturn(AdminPermissionMap::class);
2637
2638
        $aclUsersForm = $this->getMockBuilder(Form::class)
2639
            ->disableOriginalConstructor()
2640
            ->getMock();
2641
2642
        $aclUsersForm->expects($this->once())
2643
            ->method('isValid')
2644
            ->willReturn(false);
2645
2646
        $aclUsersForm->expects($this->once())
2647
            ->method('createView')
2648
            ->willReturn($this->createMock(FormView::class));
2649
2650
        $aclRolesForm = $this->getMockBuilder(Form::class)
2651
            ->disableOriginalConstructor()
2652
            ->getMock();
2653
2654
        $aclRolesForm->expects($this->once())
2655
            ->method('createView')
2656
            ->willReturn($this->createMock(FormView::class));
2657
2658
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...inObjectAclManipulator>.

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.

Loading history...
2659
            ->method('createAclUsersForm')
2660
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2661
            ->willReturn($aclUsersForm);
2662
2663
        $this->adminObjectAclManipulator->expects($this->once())
2664
            ->method('createAclRolesForm')
2665
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2666
            ->willReturn($aclRolesForm);
2667
2668
        $aclSecurityHandler = $this->createMock(AclSecurityHandlerInterface::class);
2669
2670
        $aclSecurityHandler
2671
            ->method('getObjectPermissions')
2672
            ->willReturn([]);
2673
2674
        $this->admin
2675
            ->method('getSecurityHandler')
2676
            ->willReturn($aclSecurityHandler);
2677
2678
        $this->request->setMethod(Request::METHOD_POST);
2679
2680
        $this->assertInstanceOf(Response::class, $this->controller->aclAction($this->request));
2681
2682
        $this->assertSame($this->admin, $this->parameters['admin']);
2683
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2684
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2685
2686
        $this->assertSame('acl', $this->parameters['action']);
2687
        $this->assertSame([], $this->parameters['permissions']);
2688
        $this->assertSame($object, $this->parameters['object']);
2689
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']);
2690
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']);
2691
        $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']);
2692
        $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']);
2693
2694
        $this->assertSame([], $this->session->getFlashBag()->all());
2695
        $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template);
2696
    }
2697
2698
    public function testAclActionSuccessfulUpdate(): void
2699
    {
2700
        $this->request->query->set('id', 123);
2701
        $this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, []);
2702
2703
        $this->admin->expects($this->exactly(2))
2704
            ->method('isAclEnabled')
2705
            ->willReturn(true);
2706
2707
        $object = new \stdClass();
2708
2709
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2710
            ->method('getObject')
2711
            ->willReturn($object);
2712
2713
        $this->admin
2714
            ->expects($this->once())
2715
            ->method('checkAccess');
2716
2717
        $this->admin
2718
            ->method('getSecurityInformation')
2719
            ->willReturn([]);
2720
2721
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...inObjectAclManipulator>.

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.

Loading history...
2722
            ->method('getMaskBuilderClass')
2723
            ->willReturn(AdminPermissionMap::class);
2724
2725
        $aclUsersForm = $this->getMockBuilder(Form::class)
2726
            ->disableOriginalConstructor()
2727
            ->getMock();
2728
2729
        $aclUsersForm
2730
            ->method('createView')
2731
            ->willReturn($this->createMock(FormView::class));
2732
2733
        $aclRolesForm = $this->getMockBuilder(Form::class)
2734
            ->disableOriginalConstructor()
2735
            ->getMock();
2736
2737
        $aclRolesForm
2738
            ->method('createView')
2739
            ->willReturn($this->createMock(FormView::class));
2740
2741
        $aclRolesForm->expects($this->once())
2742
            ->method('isValid')
2743
            ->willReturn(true);
2744
2745
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...inObjectAclManipulator>.

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.

Loading history...
2746
            ->method('createAclUsersForm')
2747
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2748
            ->willReturn($aclUsersForm);
2749
2750
        $this->adminObjectAclManipulator->expects($this->once())
2751
            ->method('createAclRolesForm')
2752
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2753
            ->willReturn($aclRolesForm);
2754
2755
        $aclSecurityHandler = $this->createMock(AclSecurityHandlerInterface::class);
2756
2757
        $aclSecurityHandler
2758
            ->method('getObjectPermissions')
2759
            ->willReturn([]);
2760
2761
        $this->admin
2762
            ->method('getSecurityHandler')
2763
            ->willReturn($aclSecurityHandler);
2764
2765
        $this->expectTranslate('flash_acl_edit_success', [], 'SonataAdminBundle');
2766
2767
        $this->request->setMethod(Request::METHOD_POST);
2768
2769
        $response = $this->controller->aclAction($this->request);
2770
2771
        $this->assertInstanceOf(RedirectResponse::class, $response);
2772
2773
        $this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
2774
        $this->assertSame('stdClass_acl', $response->getTargetUrl());
2775
    }
2776
2777
    public function testHistoryViewRevisionActionAccessDenied(): void
2778
    {
2779
        $this->admin
2780
            ->method('getObject')
2781
            ->willReturn(new \stdClass());
2782
2783
        $this->admin->expects($this->once())
2784
            ->method('checkAccess')
2785
            ->with($this->equalTo('historyViewRevision'))
2786
            ->will($this->throwException(new AccessDeniedException()));
2787
2788
        $this->expectException(AccessDeniedException::class);
2789
2790
        $this->controller->historyViewRevisionAction($this->request, null);
2791
    }
2792
2793
    public function testHistoryViewRevisionActionNotFoundException(): void
2794
    {
2795
        $this->request->query->set('id', 123);
2796
2797
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2798
            ->method('getObject')
2799
            ->willReturn(null);
2800
2801
        $this->expectException(NotFoundHttpException::class);
2802
        $this->expectExceptionMessage('unable to find the object with id: 123');
2803
2804
        $this->controller->historyViewRevisionAction($this->request, null);
2805
    }
2806
2807
    public function testHistoryViewRevisionActionNoReader(): void
2808
    {
2809
        $this->expectException(NotFoundHttpException::class);
2810
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
2811
2812
        $this->request->query->set('id', 123);
2813
2814
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2815
            ->method('checkAccess')
2816
            ->with($this->equalTo('historyViewRevision'));
2817
2818
        $object = new \stdClass();
2819
2820
        $this->admin->expects($this->once())
2821
            ->method('getObject')
2822
            ->willReturn($object);
2823
2824
        $this->admin
2825
            ->method('getClass')
2826
            ->willReturn('Foo');
2827
2828
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2829
            ->method('hasReader')
2830
            ->with($this->equalTo('Foo'))
2831
            ->willReturn(false);
2832
2833
        $this->controller->historyViewRevisionAction($this->request, null);
2834
    }
2835
2836
    public function testHistoryViewRevisionActionNotFoundRevision(): void
2837
    {
2838
        $this->expectException(NotFoundHttpException::class);
2839
        $this->expectExceptionMessage(
2840
            'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'
2841
        );
2842
2843
        $this->request->query->set('id', 123);
2844
2845
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2846
            ->method('checkAccess')
2847
            ->with($this->equalTo('historyViewRevision'));
2848
2849
        $object = new \stdClass();
2850
2851
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2852
            ->method('getObject')
2853
            ->willReturn($object);
2854
2855
        $this->admin
2856
            ->method('getClass')
2857
            ->willReturn('Foo');
2858
2859
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2860
            ->method('hasReader')
2861
            ->with($this->equalTo('Foo'))
2862
            ->willReturn(true);
2863
2864
        $reader = $this->createMock(AuditReaderInterface::class);
2865
2866
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2867
            ->method('getReader')
2868
            ->with($this->equalTo('Foo'))
2869
            ->willReturn($reader);
2870
2871
        $reader->expects($this->once())
2872
            ->method('find')
2873
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
2874
            ->willReturn(null);
2875
2876
        $this->controller->historyViewRevisionAction($this->request, 456);
2877
    }
2878
2879
    public function testHistoryViewRevisionAction(): void
2880
    {
2881
        $this->request->query->set('id', 123);
2882
2883
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2884
            ->method('checkAccess')
2885
            ->with($this->equalTo('historyViewRevision'));
2886
2887
        $object = new \stdClass();
2888
2889
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2890
            ->method('getObject')
2891
            ->willReturn($object);
2892
2893
        $this->admin
2894
            ->method('getClass')
2895
            ->willReturn('Foo');
2896
2897
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2898
            ->method('hasReader')
2899
            ->with($this->equalTo('Foo'))
2900
            ->willReturn(true);
2901
2902
        $reader = $this->createMock(AuditReaderInterface::class);
2903
2904
        $this->auditManager->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundl...\AuditManagerInterface>.

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.

Loading history...
2905
            ->method('getReader')
2906
            ->with($this->equalTo('Foo'))
2907
            ->willReturn($reader);
2908
2909
        $objectRevision = new \stdClass();
2910
        $objectRevision->revision = 456;
2911
2912
        $reader->expects($this->once())
2913
            ->method('find')
2914
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
2915
            ->willReturn($objectRevision);
2916
2917
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2918
            ->method('setSubject')
2919
            ->with($this->equalTo($objectRevision));
2920
2921
        $fieldDescriptionCollection = new FieldDescriptionCollection();
2922
        $this->admin->expects($this->once())
2923
            ->method('getShow')
2924
            ->willReturn($fieldDescriptionCollection);
2925
2926
        $this->assertInstanceOf(Response::class, $this->controller->historyViewRevisionAction($this->request, 456));
2927
2928
        $this->assertSame($this->admin, $this->parameters['admin']);
2929
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2930
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2931
2932
        $this->assertSame('show', $this->parameters['action']);
2933
        $this->assertSame($objectRevision, $this->parameters['object']);
2934
        $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
2935
2936
        $this->assertSame([], $this->session->getFlashBag()->all());
2937
        $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template);
2938
    }
2939
2940
    public function testHistoryCompareRevisionsActionAccessDenied(): void
2941
    {
2942
        $this->expectException(AccessDeniedException::class);
2943
2944
        $this->admin->expects($this->once())
2945
            ->method('checkAccess')
2946
            ->with($this->equalTo('historyCompareRevisions'))
2947
            ->will($this->throwException(new AccessDeniedException()));
2948
2949
        $this->controller->historyCompareRevisionsAction($this->request, null, null);
2950
    }
2951
2952
    public function testHistoryCompareRevisionsActionNotFoundException(): void
2953
    {
2954
        $this->expectException(NotFoundHttpException::class);
2955
        $this->expectExceptionMessage('unable to find the object with id: 123');
2956
2957
        $this->request->query->set('id', 123);
2958
2959
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2960
            ->method('checkAccess')
2961
            ->with($this->equalTo('historyCompareRevisions'));
2962
2963
        $this->admin->expects($this->once())
2964
            ->method('getObject')
2965
            ->willReturn(null);
2966
2967
        $this->controller->historyCompareRevisionsAction($this->request, null, null);
2968
    }
2969
2970
    public function testHistoryCompareRevisionsActionNoReader(): void
2971
    {
2972
        $this->expectException(NotFoundHttpException::class);
2973
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
2974
2975
        $this->request->query->set('id', 123);
2976
2977
        $this->admin->expects($this->once())
2978
            ->method('checkAccess')
2979
            ->with($this->equalTo('historyCompareRevisions'));
2980
2981
        $object = new \stdClass();
2982
2983
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
2984
            ->method('getObject')
2985
            ->willReturn($object);
2986
2987
        $this->admin
2988
            ->method('getClass')
2989
            ->willReturn('Foo');
2990
2991
        $this->auditManager->expects($this->once())
2992
            ->method('hasReader')
2993
            ->with($this->equalTo('Foo'))
2994
            ->willReturn(false);
2995
2996
        $this->controller->historyCompareRevisionsAction($this->request, null, null);
2997
    }
2998
2999
    public function testHistoryCompareRevisionsActionNotFoundBaseRevision(): void
3000
    {
3001
        $this->expectException(NotFoundHttpException::class);
3002
        $this->expectExceptionMessage(
3003
            'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'
3004
        );
3005
3006
        $this->request->query->set('id', 123);
3007
3008
        $this->admin->expects($this->once())
3009
            ->method('checkAccess')
3010
            ->with($this->equalTo('historyCompareRevisions'));
3011
3012
        $object = new \stdClass();
3013
3014
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3015
            ->method('getObject')
3016
            ->willReturn($object);
3017
3018
        $this->admin
3019
            ->method('getClass')
3020
            ->willReturn('Foo');
3021
3022
        $this->auditManager->expects($this->once())
3023
            ->method('hasReader')
3024
            ->with($this->equalTo('Foo'))
3025
            ->willReturn(true);
3026
3027
        $reader = $this->createMock(AuditReaderInterface::class);
3028
3029
        $this->auditManager->expects($this->once())
3030
            ->method('getReader')
3031
            ->with($this->equalTo('Foo'))
3032
            ->willReturn($reader);
3033
3034
        // once because it will not be found and therefore the second call won't be executed
3035
        $reader->expects($this->once())
3036
            ->method('find')
3037
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3038
            ->willReturn(null);
3039
3040
        $this->controller->historyCompareRevisionsAction($this->request, 456, 789);
3041
    }
3042
3043
    public function testHistoryCompareRevisionsActionNotFoundCompareRevision(): void
3044
    {
3045
        $this->expectException(NotFoundHttpException::class);
3046
        $this->expectExceptionMessage(
3047
            'unable to find the targeted object `123` from the revision `789` with classname : `Foo`'
3048
        );
3049
3050
        $this->request->query->set('id', 123);
3051
3052
        $this->admin->expects($this->once())
3053
            ->method('checkAccess')
3054
            ->with($this->equalTo('historyCompareRevisions'));
3055
3056
        $object = new \stdClass();
3057
3058
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3059
            ->method('getObject')
3060
            ->willReturn($object);
3061
3062
        $this->admin
3063
            ->method('getClass')
3064
            ->willReturn('Foo');
3065
3066
        $this->auditManager->expects($this->once())
3067
            ->method('hasReader')
3068
            ->with($this->equalTo('Foo'))
3069
            ->willReturn(true);
3070
3071
        $reader = $this->createMock(AuditReaderInterface::class);
3072
3073
        $this->auditManager->expects($this->once())
3074
            ->method('getReader')
3075
            ->with($this->equalTo('Foo'))
3076
            ->willReturn($reader);
3077
3078
        $objectRevision = new \stdClass();
3079
        $objectRevision->revision = 456;
3080
3081
        // first call should return, so the second call will throw an exception
3082
        $reader->expects($this->at(0))
3083
            ->method('find')
3084
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3085
            ->willReturn($objectRevision);
3086
3087
        $reader->expects($this->at(1))
3088
            ->method('find')
3089
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
3090
            ->willReturn(null);
3091
3092
        $this->controller->historyCompareRevisionsAction($this->request, 456, 789);
3093
    }
3094
3095
    public function testHistoryCompareRevisionsActionAction(): void
3096
    {
3097
        $this->request->query->set('id', 123);
3098
3099
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3100
            ->method('checkAccess')
3101
            ->with($this->equalTo('historyCompareRevisions'));
3102
3103
        $object = new \stdClass();
3104
3105
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3106
            ->method('getObject')
3107
            ->willReturn($object);
3108
3109
        $this->admin
3110
            ->method('getClass')
3111
            ->willReturn('Foo');
3112
3113
        $this->auditManager->expects($this->once())
3114
            ->method('hasReader')
3115
            ->with($this->equalTo('Foo'))
3116
            ->willReturn(true);
3117
3118
        $reader = $this->createMock(AuditReaderInterface::class);
3119
3120
        $this->auditManager->expects($this->once())
3121
            ->method('getReader')
3122
            ->with($this->equalTo('Foo'))
3123
            ->willReturn($reader);
3124
3125
        $objectRevision = new \stdClass();
3126
        $objectRevision->revision = 456;
3127
3128
        $compareObjectRevision = new \stdClass();
3129
        $compareObjectRevision->revision = 789;
3130
3131
        $reader->expects($this->at(0))
3132
            ->method('find')
3133
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3134
            ->willReturn($objectRevision);
3135
3136
        $reader->expects($this->at(1))
3137
            ->method('find')
3138
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
3139
            ->willReturn($compareObjectRevision);
3140
3141
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3142
            ->method('setSubject')
3143
            ->with($this->equalTo($objectRevision));
3144
3145
        $fieldDescriptionCollection = new FieldDescriptionCollection();
3146
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3147
            ->method('getShow')
3148
            ->willReturn($fieldDescriptionCollection);
3149
3150
        $this->assertInstanceOf(Response::class, $this->controller->historyCompareRevisionsAction($this->request, 456, 789));
3151
3152
        $this->assertSame($this->admin, $this->parameters['admin']);
3153
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3154
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3155
3156
        $this->assertSame('show', $this->parameters['action']);
3157
        $this->assertSame($objectRevision, $this->parameters['object']);
3158
        $this->assertSame($compareObjectRevision, $this->parameters['object_compare']);
3159
        $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
3160
3161
        $this->assertSame([], $this->session->getFlashBag()->all());
3162
        $this->assertSame('@SonataAdmin/CRUD/show_compare.html.twig', $this->template);
3163
    }
3164
3165
    public function testBatchActionWrongMethod(): void
3166
    {
3167
        $this->expectException(NotFoundHttpException::class);
3168
        $this->expectExceptionMessage('Invalid request method given "GET", POST expected');
3169
3170
        $this->controller->batchAction($this->request);
3171
    }
3172
3173
    public function testBatchActionActionNotDefined(): void
3174
    {
3175
        $this->expectException(\RuntimeException::class);
3176
        $this->expectExceptionMessage('The `foo` batch action is not defined');
3177
3178
        $batchActions = [];
3179
3180
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3181
            ->method('getBatchActions')
3182
            ->willReturn($batchActions);
3183
3184
        $this->request->setMethod(Request::METHOD_POST);
3185
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3186
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3187
3188
        $this->controller->batchAction($this->request);
3189
    }
3190
3191
    public function testBatchActionActionInvalidCsrfToken(): void
3192
    {
3193
        $this->request->setMethod(Request::METHOD_POST);
3194
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3195
        $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
3196
3197
        try {
3198
            $this->controller->batchAction($this->request);
3199
        } catch (HttpException $e) {
3200
            $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
3201
            $this->assertSame(400, $e->getStatusCode());
3202
        }
3203
    }
3204
3205
    public function testBatchActionMethodNotExist(): void
3206
    {
3207
        $this->expectException(\RuntimeException::class);
3208
        $this->expectExceptionMessage(
3209
            'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable'
3210
        );
3211
3212
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3213
3214
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3215
            ->method('getBatchActions')
3216
            ->willReturn($batchActions);
3217
3218
        $datagrid = $this->createMock(DatagridInterface::class);
3219
        $this->admin->expects($this->once())
3220
            ->method('getDatagrid')
3221
            ->willReturn($datagrid);
3222
3223
        $this->request->setMethod(Request::METHOD_POST);
3224
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3225
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3226
3227
        $this->controller->batchAction($this->request);
3228
    }
3229
3230
    public function testBatchActionWithoutConfirmation(): void
3231
    {
3232
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3233
3234
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3235
            ->method('getBatchActions')
3236
            ->willReturn($batchActions);
3237
3238
        $datagrid = $this->createMock(DatagridInterface::class);
3239
3240
        $query = $this->createMock(ProxyQueryInterface::class);
3241
        $datagrid->expects($this->once())
3242
            ->method('getQuery')
3243
            ->willReturn($query);
3244
3245
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3246
            ->method('getDatagrid')
3247
            ->willReturn($datagrid);
3248
3249
        $modelManager = $this->createMock(ModelManagerInterface::class);
3250
3251
        $this->admin->expects($this->once())
3252
            ->method('checkAccess')
3253
            ->with($this->equalTo('batchDelete'));
3254
3255
        $this->admin
3256
            ->method('getModelManager')
3257
            ->willReturn($modelManager);
3258
3259
        $this->admin
3260
            ->method('getClass')
3261
            ->willReturn('Foo');
3262
3263
        $modelManager->expects($this->once())
3264
            ->method('addIdentifiersToQuery')
3265
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3266
            ->willReturn(true);
3267
3268
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3269
3270
        $this->request->setMethod(Request::METHOD_POST);
3271
        $this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]));
3272
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3273
3274
        $result = $this->controller->batchAction($this->request);
3275
3276
        $this->assertInstanceOf(RedirectResponse::class, $result);
3277
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3278
        $this->assertSame('list', $result->getTargetUrl());
3279
    }
3280
3281
    public function testBatchActionWithoutConfirmation2(): void
3282
    {
3283
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3284
3285
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3286
            ->method('getBatchActions')
3287
            ->willReturn($batchActions);
3288
3289
        $datagrid = $this->createMock(DatagridInterface::class);
3290
3291
        $query = $this->createMock(ProxyQueryInterface::class);
3292
        $datagrid->expects($this->once())
3293
            ->method('getQuery')
3294
            ->willReturn($query);
3295
3296
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3297
            ->method('getDatagrid')
3298
            ->willReturn($datagrid);
3299
3300
        $modelManager = $this->createMock(ModelManagerInterface::class);
3301
3302
        $this->admin->expects($this->once())
3303
            ->method('checkAccess')
3304
            ->with($this->equalTo('batchDelete'));
3305
3306
        $this->admin
3307
            ->method('getModelManager')
3308
            ->willReturn($modelManager);
3309
3310
        $this->admin
3311
            ->method('getClass')
3312
            ->willReturn('Foo');
3313
3314
        $modelManager->expects($this->once())
3315
            ->method('addIdentifiersToQuery')
3316
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3317
            ->willReturn(true);
3318
3319
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3320
3321
        $this->request->setMethod(Request::METHOD_POST);
3322
        $this->request->request->set('action', 'delete');
3323
        $this->request->request->set('idx', ['123', '456']);
3324
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3325
3326
        $result = $this->controller->batchAction($this->request);
3327
3328
        $this->assertInstanceOf(RedirectResponse::class, $result);
3329
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3330
        $this->assertSame('list', $result->getTargetUrl());
3331
    }
3332
3333
    public function testBatchActionWithConfirmation(): void
3334
    {
3335
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]];
3336
3337
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3338
            ->method('getBatchActions')
3339
            ->willReturn($batchActions);
3340
3341
        $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false];
3342
3343
        $this->request->setMethod(Request::METHOD_POST);
3344
        $this->request->request->set('data', json_encode($data));
3345
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3346
3347
        $datagrid = $this->createMock(DatagridInterface::class);
3348
3349
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3350
            ->method('getDatagrid')
3351
            ->willReturn($datagrid);
3352
3353
        $form = $this->getMockBuilder(Form::class)
3354
            ->disableOriginalConstructor()
3355
            ->getMock();
3356
3357
        $form->expects($this->once())
3358
            ->method('createView')
3359
            ->willReturn($this->createMock(FormView::class));
3360
3361
        $datagrid->expects($this->once())
3362
            ->method('getForm')
3363
            ->willReturn($form);
3364
3365
        $this->assertInstanceOf(Response::class, $this->controller->batchAction($this->request));
3366
3367
        $this->assertSame($this->admin, $this->parameters['admin']);
3368
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3369
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3370
3371
        $this->assertSame('list', $this->parameters['action']);
3372
        $this->assertSame($datagrid, $this->parameters['datagrid']);
3373
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
3374
        $this->assertSame($data, $this->parameters['data']);
3375
        $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
3376
        $this->assertSame('Foo Bar', $this->parameters['action_label']);
3377
3378
        $this->assertSame([], $this->session->getFlashBag()->all());
3379
        $this->assertSame('@SonataAdmin/CRUD/batch_confirmation.html.twig', $this->template);
3380
    }
3381
3382
    public function testBatchActionNonRelevantAction(): void
3383
    {
3384
        $controller = new BatchAdminController();
3385
        $controller->setContainer($this->container);
3386
3387
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3388
3389
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3390
            ->method('getBatchActions')
3391
            ->willReturn($batchActions);
3392
3393
        $datagrid = $this->createMock(DatagridInterface::class);
3394
3395
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3396
            ->method('getDatagrid')
3397
            ->willReturn($datagrid);
3398
3399
        $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');
3400
3401
        $this->request->setMethod(Request::METHOD_POST);
3402
        $this->request->request->set('action', 'foo');
3403
        $this->request->request->set('idx', ['789']);
3404
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3405
3406
        $result = $controller->batchAction($this->request);
3407
3408
        $this->assertInstanceOf(RedirectResponse::class, $result);
3409
        $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
3410
        $this->assertSame('list', $result->getTargetUrl());
3411
    }
3412
3413
    public function testBatchActionWithCustomConfirmationTemplate(): void
3414
    {
3415
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true, 'template' => 'custom_template.html.twig']];
3416
3417
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3418
            ->method('getBatchActions')
3419
            ->willReturn($batchActions);
3420
3421
        $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false];
3422
3423
        $this->request->setMethod(Request::METHOD_POST);
3424
        $this->request->request->set('data', json_encode($data));
3425
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3426
3427
        $datagrid = $this->createMock(DatagridInterface::class);
3428
3429
        $this->admin->expects($this->once())
3430
            ->method('getDatagrid')
3431
            ->willReturn($datagrid);
3432
3433
        $form = $this->createMock(Form::class);
3434
3435
        $form->expects($this->once())
3436
            ->method('createView')
3437
            ->willReturn($this->createMock(FormView::class));
3438
3439
        $datagrid->expects($this->once())
3440
            ->method('getForm')
3441
            ->willReturn($form);
3442
3443
        $this->controller->batchAction($this->request);
3444
3445
        $this->assertSame('custom_template.html.twig', $this->template);
3446
    }
3447
3448
    public function testBatchActionNonRelevantAction2(): void
3449
    {
3450
        $controller = new BatchAdminController();
3451
        $controller->setContainer($this->container);
3452
3453
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3454
3455
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
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', 'foo');
3469
        $this->request->request->set('idx', ['999']);
3470
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3471
3472
        $result = $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 testBatchActionNoItems(): void
3480
    {
3481
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true]];
3482
3483
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3484
            ->method('getBatchActions')
3485
            ->willReturn($batchActions);
3486
3487
        $datagrid = $this->createMock(DatagridInterface::class);
3488
3489
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3490
            ->method('getDatagrid')
3491
            ->willReturn($datagrid);
3492
3493
        $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');
3494
3495
        $this->request->setMethod(Request::METHOD_POST);
3496
        $this->request->request->set('action', 'delete');
3497
        $this->request->request->set('idx', []);
3498
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3499
3500
        $result = $this->controller->batchAction($this->request);
3501
3502
        $this->assertInstanceOf(RedirectResponse::class, $result);
3503
        $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
3504
        $this->assertSame('list', $result->getTargetUrl());
3505
    }
3506
3507
    public function testBatchActionNoItemsEmptyQuery(): void
3508
    {
3509
        $controller = new BatchAdminController();
3510
        $controller->setContainer($this->container);
3511
3512
        $batchActions = ['bar' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3513
3514
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3515
            ->method('getBatchActions')
3516
            ->willReturn($batchActions);
3517
3518
        $datagrid = $this->createMock(DatagridInterface::class);
3519
3520
        $query = $this->createMock(ProxyQueryInterface::class);
3521
        $datagrid->expects($this->once())
3522
            ->method('getQuery')
3523
            ->willReturn($query);
3524
3525
        $this->admin->expects($this->once())
3526
            ->method('getDatagrid')
3527
            ->willReturn($datagrid);
3528
3529
        $modelManager = $this->createMock(ModelManagerInterface::class);
3530
3531
        $this->admin
3532
            ->method('getModelManager')
3533
            ->willReturn($modelManager);
3534
3535
        $this->admin
3536
            ->method('getClass')
3537
            ->willReturn('Foo');
3538
3539
        $this->request->setMethod(Request::METHOD_POST);
3540
        $this->request->request->set('action', 'bar');
3541
        $this->request->request->set('idx', []);
3542
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3543
3544
        $this->expectTranslate('flash_batch_no_elements_processed', [], 'SonataAdminBundle');
3545
        $result = $controller->batchAction($this->request);
3546
3547
        $this->assertInstanceOf(Response::class, $result);
3548
        $this->assertRegExp('/Redirecting to list/', $result->getContent());
3549
    }
3550
3551
    public function testBatchActionWithRequesData(): void
3552
    {
3553
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3554
3555
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3556
            ->method('getBatchActions')
3557
            ->willReturn($batchActions);
3558
3559
        $datagrid = $this->createMock(DatagridInterface::class);
3560
3561
        $query = $this->createMock(ProxyQueryInterface::class);
3562
        $datagrid->expects($this->once())
3563
            ->method('getQuery')
3564
            ->willReturn($query);
3565
3566
        $this->admin->expects($this->once())
3567
            ->method('getDatagrid')
3568
            ->willReturn($datagrid);
3569
3570
        $modelManager = $this->createMock(ModelManagerInterface::class);
3571
3572
        $this->admin->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
3573
            ->method('checkAccess')
3574
            ->with($this->equalTo('batchDelete'));
3575
3576
        $this->admin
3577
            ->method('getModelManager')
3578
            ->willReturn($modelManager);
3579
3580
        $this->admin
3581
            ->method('getClass')
3582
            ->willReturn('Foo');
3583
3584
        $modelManager->expects($this->once())
3585
            ->method('addIdentifiersToQuery')
3586
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3587
            ->willReturn(true);
3588
3589
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3590
3591
        $this->request->setMethod(Request::METHOD_POST);
3592
        $this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]));
3593
        $this->request->request->set('foo', 'bar');
3594
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3595
3596
        $result = $this->controller->batchAction($this->request);
3597
3598
        $this->assertInstanceOf(RedirectResponse::class, $result);
3599
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3600
        $this->assertSame('list', $result->getTargetUrl());
3601
        $this->assertSame('bar', $this->request->request->get('foo'));
3602
    }
3603
3604
    public function getCsrfProvider()
3605
    {
3606
        return $this->csrfProvider;
3607
    }
3608
3609
    public function getToStringValues()
3610
    {
3611
        return [
3612
            ['', ''],
3613
            ['Foo', 'Foo'],
3614
            ['&lt;a href=&quot;http://foo&quot;&gt;Bar&lt;/a&gt;', '<a href="http://foo">Bar</a>'],
3615
            ['&lt;&gt;&amp;&quot;&#039;abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/', '<>&"\'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/'],
3616
        ];
3617
    }
3618
3619
    private function assertLoggerLogsModelManagerException($subject, string $method): void
3620
    {
3621
        $exception = new ModelManagerException(
3622
            $message = 'message',
3623
            1234,
3624
            new \Exception($previousExceptionMessage = 'very useful message')
3625
        );
3626
3627
        $subject->expects($this->once())
3628
            ->method($method)
3629
            ->willReturnCallback(static function () use ($exception): void {
3630
                throw $exception;
3631
            });
3632
3633
        $this->logger->expects($this->once())
3634
            ->method('error')
3635
            ->with($message, [
3636
                'exception' => $exception,
3637
                'previous_exception_message' => $previousExceptionMessage,
3638
            ]);
3639
    }
3640
3641
    private function expectTranslate(
3642
        string $id,
3643
        array $parameters = [],
3644
        ?string $domain = null,
3645
        ?string $locale = null
3646
    ): void {
3647
        $this->translator->expects($this->once())
3648
            ->method('trans')
3649
            ->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale))
3650
            ->willReturn($id);
3651
    }
3652
}
3653