Completed
Push — master ( 8016bf...ca92e8 )
by Grégoire
14s
created

testBatchActionWithoutConfirmation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.069
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Controller\CRUDController;
24
use Sonata\AdminBundle\Datagrid\DatagridInterface;
25
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
26
use Sonata\AdminBundle\Exception\LockException;
27
use Sonata\AdminBundle\Exception\ModelManagerException;
28
use Sonata\AdminBundle\Export\Exporter as SonataExporter;
29
use Sonata\AdminBundle\Model\AuditManager;
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\AclSecurityHandler;
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\Bundle\FrameworkBundle\Controller\Controller;
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\Component\Translation\TranslatorInterface;
64
use Twig\Environment;
65
66
/**
67
 * Test for CRUDController.
68
 *
69
 * @author Andrej Hudec <[email protected]>
70
 *
71
 * @group legacy
72
 */
73
class CRUDControllerTest extends TestCase
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 AuditManager
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->getMockBuilder(AdminInterface::class)
166
            ->disableOriginalConstructor()
167
            ->getMock();
168
        $this->translator = $this->createMock(TranslatorInterface::class);
169
        $this->parameters = [];
170
        $this->template = '';
171
172
        $this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
173
174
        $templatingRenderReturnCallback = $this->returnCallback(function (
175
            string $name,
176
            array $context = []
177
        ): string {
178
            $this->template = $name;
179
180
            $this->parameters = $context;
181
182
            return '';
183
        });
184
185
        $this->session = new Session(new MockArraySessionStorage());
186
187
        $twig = $this->getMockBuilder(Environment::class)
188
            ->disableOriginalConstructor()
189
            ->getMock();
190
191
        $twig
192
            ->method('getRuntime')
193
            ->willReturn($this->createMock(FormRenderer::class));
194
195
        $twig
196
            ->method('render')
197
            ->will($templatingRenderReturnCallback);
198
199
        // NEXT_MAJOR : require sonata/exporter ^1.7 and remove conditional
200
        if (class_exists(Exporter::class)) {
201
            $exporter = new Exporter([new JsonWriter('/tmp/sonataadmin/export.json')]);
202
        } else {
203
            $exporter = $this->createMock(SonataExporter::class);
204
205
            $exporter
206
                ->method('getResponse')
207
                ->willReturn(new StreamedResponse());
208
        }
209
210
        $this->auditManager = $this->getMockBuilder(AuditManager::class)
211
            ->disableOriginalConstructor()
212
            ->getMock();
213
214
        $this->adminObjectAclManipulator = $this->getMockBuilder(AdminObjectAclManipulator::class)
215
            ->disableOriginalConstructor()
216
            ->getMock();
217
218
        $this->csrfProvider = $this->getMockBuilder(CsrfTokenManagerInterface::class)
219
            ->getMock();
220
221
        $this->csrfProvider
222
            ->method('getToken')
223
            ->willReturnCallback(static function (string $intention): CsrfToken {
224
                return new CsrfToken($intention, 'csrf-token-123_'.$intention);
225
            });
226
227
        $this->csrfProvider
228
            ->method('isTokenValid')
229
            ->willReturnCallback(static function (CsrfToken $token): bool {
230
                return $token->getValue() === 'csrf-token-123_'.$token->getId();
231
            });
232
233
        $this->logger = $this->createMock(LoggerInterface::class);
234
235
        $requestStack = new RequestStack();
236
        $requestStack->push($this->request);
237
238
        $this->kernel = $this->createMock(KernelInterface::class);
239
240
        $this->container->set('sonata.admin.pool', $this->pool);
241
        $this->container->set('request_stack', $requestStack);
242
        $this->container->set('foo.admin', $this->admin);
243
        $this->container->set('foo.admin.template_registry', $this->templateRegistry->reveal());
244
        $this->container->set('twig', $twig);
245
        $this->container->set('session', $this->session);
246
        $this->container->set('sonata.admin.exporter', $exporter);
247
        $this->container->set('sonata.admin.audit.manager', $this->auditManager);
248
        $this->container->set('sonata.admin.object.manipulator.acl.admin', $this->adminObjectAclManipulator);
249
        $this->container->set('security.csrf.token_manager', $this->csrfProvider);
250
        $this->container->set('logger', $this->logger);
251
        $this->container->set('kernel', $this->kernel);
252
        $this->container->set('translator', $this->translator);
253
        $this->container->set('sonata.admin.breadcrumbs_builder', new BreadcrumbsBuilder([]));
254
255
        $this->container->setParameter(
256
            'security.role_hierarchy.roles',
257
            ['ROLE_SUPER_ADMIN' => ['ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN']]
258
        );
259
        $this->container->setParameter('sonata.admin.security.acl_user_manager', null);
260
261
        $this->templateRegistry->getTemplate('ajax')->willReturn('@SonataAdmin/ajax_layout.html.twig');
262
        $this->templateRegistry->getTemplate('layout')->willReturn('@SonataAdmin/standard_layout.html.twig');
263
        $this->templateRegistry->getTemplate('show')->willReturn('@SonataAdmin/CRUD/show.html.twig');
264
        $this->templateRegistry->getTemplate('show_compare')->willReturn('@SonataAdmin/CRUD/show_compare.html.twig');
265
        $this->templateRegistry->getTemplate('edit')->willReturn('@SonataAdmin/CRUD/edit.html.twig');
266
        $this->templateRegistry->getTemplate('dashboard')->willReturn('@SonataAdmin/Core/dashboard.html.twig');
267
        $this->templateRegistry->getTemplate('search')->willReturn('@SonataAdmin/Core/search.html.twig');
268
        $this->templateRegistry->getTemplate('list')->willReturn('@SonataAdmin/CRUD/list.html.twig');
269
        $this->templateRegistry->getTemplate('preview')->willReturn('@SonataAdmin/CRUD/preview.html.twig');
270
        $this->templateRegistry->getTemplate('history')->willReturn('@SonataAdmin/CRUD/history.html.twig');
271
        $this->templateRegistry->getTemplate('acl')->willReturn('@SonataAdmin/CRUD/acl.html.twig');
272
        $this->templateRegistry->getTemplate('delete')->willReturn('@SonataAdmin/CRUD/delete.html.twig');
273
        $this->templateRegistry->getTemplate('batch')->willReturn('@SonataAdmin/CRUD/list__batch.html.twig');
274
        $this->templateRegistry->getTemplate('batch_confirmation')->willReturn('@SonataAdmin/CRUD/batch_confirmation.html.twig');
275
276
        $this->admin
277
            ->method('getIdParameter')
278
            ->willReturn('id');
279
280
        $this->admin
281
            ->method('getAccessMapping')
282
            ->willReturn([]);
283
284
        $this->admin
285
            ->method('generateUrl')
286
            ->willReturnCallback(
287
                static function ($name, array $parameters = []) {
288
                    $result = $name;
289
                    if (!empty($parameters)) {
290
                        $result .= '?'.http_build_query($parameters);
291
                    }
292
293
                    return $result;
294
                }
295
            );
296
297
        $this->admin
298
            ->method('generateObjectUrl')
299
            ->willReturnCallback(
300
                static function (string $name, $object, array $parameters = []): string {
301
                    $result = \get_class($object).'_'.$name;
302
                    if (!empty($parameters)) {
303
                        $result .= '?'.http_build_query($parameters);
304
                    }
305
306
                    return $result;
307
                }
308
            );
309
310
        $this->admin
311
            ->method('getCode')
312
            ->willReturn('foo.admin');
313
314
        $this->controller = new CRUDController();
315
        $this->controller->setContainer($this->container);
316
317
        // Make some methods public to test them
318
        $testedMethods = [
319
            'renderJson',
320
            'isXmlHttpRequest',
321
            'configure',
322
            'getBaseTemplate',
323
            'redirectTo',
324
            'addFlash',
325
        ];
326
        foreach ($testedMethods as $testedMethod) {
327
            // NEXT_MAJOR: Remove this check and only use CRUDController
328
            if (method_exists(CRUDController::class, $testedMethod)) {
329
                $method = new \ReflectionMethod(CRUDController::class, $testedMethod);
330
            } else {
331
                $method = new \ReflectionMethod(Controller::class, $testedMethod);
332
            }
333
334
            $method->setAccessible(true);
335
            $this->protectedTestedMethods[$testedMethod] = $method;
336
        }
337
    }
338
339
    public function testRenderJson1(): void
340
    {
341
        $data = ['example' => '123', 'foo' => 'bar'];
342
343
        $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
344
        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request);
345
346
        $this->assertSame($response->headers->get('Content-Type'), 'application/json');
347
        $this->assertSame(json_encode($data), $response->getContent());
348
    }
349
350
    public function testRenderJson2(): void
351
    {
352
        $data = ['example' => '123', 'foo' => 'bar'];
353
354
        $this->request->headers->set('Content-Type', 'multipart/form-data');
355
        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request);
356
357
        $this->assertSame($response->headers->get('Content-Type'), 'application/json');
358
        $this->assertSame(json_encode($data), $response->getContent());
359
    }
360
361
    public function testRenderJsonAjax(): void
362
    {
363
        $data = ['example' => '123', 'foo' => 'bar'];
364
365
        $this->request->attributes->set('_xml_http_request', true);
366
        $this->request->headers->set('Content-Type', 'multipart/form-data');
367
        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, [], $this->request);
368
369
        $this->assertSame($response->headers->get('Content-Type'), 'application/json');
370
        $this->assertSame(json_encode($data), $response->getContent());
371
    }
372
373
    public function testIsXmlHttpRequest(): void
374
    {
375
        $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
376
377
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
378
379
        $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
380
381
        $this->request->headers->remove('X-Requested-With');
382
        $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
383
384
        $this->request->attributes->set('_xml_http_request', true);
385
        $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
386
    }
387
388
    public function testConfigure(): void
389
    {
390
        $uniqueId = '';
391
392
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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 (int $uniqid) use (&$uniqueId): void {
395
                $uniqueId = $uniqid;
396
            });
397
398
        $this->request->query->set('uniqid', 123456);
399
        $this->protectedTestedMethods['configure']->invoke($this->controller);
400
401
        $this->assertSame(123456, $uniqueId);
402
    }
403
404
    public function testConfigureChild(): void
405
    {
406
        $uniqueId = '';
407
408
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
409
            ->method('setUniqid')
410
            ->willReturnCallback(static function ($uniqid) use (&$uniqueId): void {
411
                $uniqueId = $uniqid;
412
            });
413
414
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
415
            ->method('isChild')
416
            ->willReturn(true);
417
418
        $adminParent = $this->getMockBuilder(AdminInterface::class)
419
            ->disableOriginalConstructor()
420
            ->getMock();
421
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
422
            ->method('getParent')
423
            ->willReturn($adminParent);
424
425
        $this->request->query->set('uniqid', 123456);
426
        $this->protectedTestedMethods['configure']->invoke($this->controller);
427
428
        $this->assertSame(123456, $uniqueId);
429
    }
430
431
    public function testConfigureWithException(): void
432
    {
433
        $this->expectException(\RuntimeException::class);
434
        $this->expectExceptionMessage(
435
            'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`'
436
        );
437
438
        $this->request->attributes->remove('_sonata_admin');
439
        $this->protectedTestedMethods['configure']->invoke($this->controller);
440
    }
441
442
    public function testConfigureWithException2(): void
443
    {
444
        $this->expectException(\InvalidArgumentException::class);
445
        $this->expectExceptionMessage('You have requested a non-existent service "nonexistent.admin".');
446
447
        $this->pool->setAdminServiceIds(['nonexistent.admin']);
448
        $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
449
        $this->protectedTestedMethods['configure']->invoke($this->controller);
450
    }
451
452
    public function testGetBaseTemplate(): void
453
    {
454
        $this->assertSame(
455
            '@SonataAdmin/standard_layout.html.twig',
456
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
457
        );
458
459
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
460
        $this->assertSame(
461
            '@SonataAdmin/ajax_layout.html.twig',
462
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
463
        );
464
465
        $this->request->headers->remove('X-Requested-With');
466
        $this->assertSame(
467
            '@SonataAdmin/standard_layout.html.twig',
468
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
469
        );
470
471
        $this->request->attributes->set('_xml_http_request', true);
472
        $this->assertSame(
473
            '@SonataAdmin/ajax_layout.html.twig',
474
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
475
        );
476
    }
477
478
    public function testRender(): void
479
    {
480
        $this->parameters = [];
481
        $this->assertInstanceOf(
482
            Response::class,
483
            $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], null)
484
        );
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 testRenderWithResponse(): void
492
    {
493
        $this->parameters = [];
494
        $response = new Response();
495
        $response->headers->set('X-foo', 'bar');
496
        $responseResult = $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], $response);
497
498
        $this->assertSame($response, $responseResult);
499
        $this->assertSame('bar', $responseResult->headers->get('X-foo'));
500
        $this->assertSame($this->admin, $this->parameters['admin']);
501
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
502
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
503
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
504
    }
505
506
    public function testRenderCustomParams(): void
507
    {
508
        $this->parameters = [];
509
        $this->assertInstanceOf(
510
            Response::class,
511
            $this->controller->renderWithExtraParams(
512
                '@FooAdmin/foo.html.twig',
513
                ['foo' => 'bar'],
514
                null
515
            )
516
        );
517
        $this->assertSame($this->admin, $this->parameters['admin']);
518
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
519
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
520
        $this->assertSame('bar', $this->parameters['foo']);
521
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
522
    }
523
524
    public function testRenderAjax(): void
525
    {
526
        $this->parameters = [];
527
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
528
        $this->assertInstanceOf(
529
            Response::class,
530
            $this->controller->renderWithExtraParams(
531
                '@FooAdmin/foo.html.twig',
532
                ['foo' => 'bar'],
533
                null
534
            )
535
        );
536
        $this->assertSame($this->admin, $this->parameters['admin']);
537
        $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
538
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
539
        $this->assertSame('bar', $this->parameters['foo']);
540
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
541
    }
542
543
    public function testListActionAccessDenied(): void
544
    {
545
        $this->expectException(AccessDeniedException::class);
546
547
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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
            ->will($this->throwException(new AccessDeniedException()));
551
552
        $this->controller->listAction();
553
    }
554
555
    public function testPreList(): void
556
    {
557
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
558
            ->method('hasRoute')
559
            ->with($this->equalTo('list'))
560
            ->willReturn(true);
561
562
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
563
            ->method('checkAccess')
564
            ->with($this->equalTo('list'))
565
            ->willReturn(true);
566
567
        $controller = new PreCRUDController();
568
        $controller->setContainer($this->container);
569
570
        $response = $controller->listAction();
571
        $this->assertInstanceOf(Response::class, $response);
572
        $this->assertSame('preList called', $response->getContent());
573
    }
574
575
    public function testListAction(): void
576
    {
577
        $datagrid = $this->createMock(DatagridInterface::class);
578
579
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
580
            ->method('hasRoute')
581
            ->with($this->equalTo('list'))
582
            ->willReturn(true);
583
584
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
585
            ->method('checkAccess')
586
            ->with($this->equalTo('list'))
587
            ->willReturn(true);
588
589
        $form = $this->getMockBuilder(Form::class)
590
            ->disableOriginalConstructor()
591
            ->getMock();
592
593
        $form->expects($this->once())
594
            ->method('createView')
595
            ->willReturn($this->createMock(FormView::class));
596
597
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
598
            ->method('getDatagrid')
599
            ->willReturn($datagrid);
600
601
        $datagrid->expects($this->once())
602
            ->method('getForm')
603
            ->willReturn($form);
604
605
        $this->parameters = [];
606
        $this->assertInstanceOf(Response::class, $this->controller->listAction());
607
608
        $this->assertSame($this->admin, $this->parameters['admin']);
609
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
610
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
611
612
        $this->assertSame('list', $this->parameters['action']);
613
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
614
        $this->assertInstanceOf(DatagridInterface::class, $this->parameters['datagrid']);
615
        $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
616
        $this->assertSame([], $this->session->getFlashBag()->all());
617
        $this->assertSame('@SonataAdmin/CRUD/list.html.twig', $this->template);
618
    }
619
620
    public function testBatchActionDeleteAccessDenied(): void
621
    {
622
        $this->expectException(AccessDeniedException::class);
623
624
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
625
            ->method('checkAccess')
626
            ->with($this->equalTo('batchDelete'))
627
            ->will($this->throwException(new AccessDeniedException()));
628
629
        $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
630
    }
631
632
    public function testBatchActionDelete(): void
633
    {
634
        $modelManager = $this->createMock(ModelManagerInterface::class);
635
636
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
637
            ->method('checkAccess')
638
            ->with($this->equalTo('batchDelete'))
639
            ->willReturn(true);
640
641
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
642
            ->method('getModelManager')
643
            ->willReturn($modelManager);
644
645
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
646
            ->method('getFilterParameters')
647
            ->willReturn(['foo' => 'bar']);
648
649
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
650
651
        $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
652
653
        $this->assertInstanceOf(RedirectResponse::class, $result);
654
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
655
        $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
656
    }
657
658
    public function testBatchActionDeleteWithModelManagerException(): void
659
    {
660
        $modelManager = $this->createMock(ModelManagerInterface::class);
661
        $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete');
662
663
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
664
            ->method('getModelManager')
665
            ->willReturn($modelManager);
666
667
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
668
            ->method('getFilterParameters')
669
            ->willReturn(['foo' => 'bar']);
670
671
        $this->expectTranslate('flash_batch_delete_error', [], 'SonataAdminBundle');
672
673
        $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
674
675
        $this->assertInstanceOf(RedirectResponse::class, $result);
676
        $this->assertSame(['flash_batch_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error'));
677
        $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
678
    }
679
680
    public function testBatchActionDeleteWithModelManagerExceptionInDebugMode(): void
681
    {
682
        $modelManager = $this->createMock(ModelManagerInterface::class);
683
        $this->expectException(ModelManagerException::class);
684
685
        $modelManager->expects($this->once())
686
            ->method('batchDelete')
687
            ->willReturnCallback(static function (): void {
688
                throw new ModelManagerException();
689
            });
690
691
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
692
            ->method('getModelManager')
693
            ->willReturn($modelManager);
694
695
        $this->kernel->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...Kernel\KernelInterface>.

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...
696
            ->method('isDebug')
697
            ->willReturn(true);
698
699
        $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
700
    }
701
702
    public function testShowActionNotFoundException(): void
703
    {
704
        $this->expectException(NotFoundHttpException::class);
705
706
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
707
            ->method('getObject')
708
            ->willReturn(false);
709
710
        $this->controller->showAction(null);
711
    }
712
713
    public function testShowActionAccessDenied(): void
714
    {
715
        $this->expectException(AccessDeniedException::class);
716
717
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
718
            ->method('getObject')
719
            ->willReturn(new \stdClass());
720
721
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
722
            ->method('checkAccess')
723
            ->with($this->equalTo('show'))
724
            ->will($this->throwException(new AccessDeniedException()));
725
726
        $this->controller->showAction(null);
727
    }
728
729
    /**
730
     * @group legacy
731
     * @expectedDeprecation Calling this method without implementing "configureShowFields" is not supported since sonata-project/admin-bundle 3.40.0 and will no longer be possible in 4.0
732
     */
733
    public function testShowActionDeprecation(): void
734
    {
735
        $object = new \stdClass();
736
737
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
738
            ->method('getObject')
739
            ->willReturn($object);
740
741
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
742
            ->method('checkAccess')
743
            ->with($this->equalTo('show'))
744
            ->willReturn(true);
745
746
        $show = $this->createMock(FieldDescriptionCollection::class);
747
748
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
749
            ->method('getShow')
750
            ->willReturn($show);
751
752
        $show->expects($this->once())
753
            ->method('getElements')
754
            ->willReturn([]);
755
756
        $show->expects($this->once())
757
            ->method('count')
758
            ->willReturn(0);
759
760
        $this->controller->showAction(null);
761
    }
762
763
    public function testPreShow(): void
764
    {
765
        $object = new \stdClass();
766
        $object->foo = 123456;
767
768
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
769
            ->method('getObject')
770
            ->willReturn($object);
771
772
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
773
            ->method('checkAccess')
774
            ->with($this->equalTo('show'))
775
            ->willReturn(true);
776
777
        $controller = new PreCRUDController();
778
        $controller->setContainer($this->container);
779
780
        $response = $controller->showAction(null);
781
        $this->assertInstanceOf(Response::class, $response);
782
        $this->assertSame('preShow called: 123456', $response->getContent());
783
    }
784
785
    public function testShowAction(): void
786
    {
787
        $object = new \stdClass();
788
789
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
790
            ->method('getObject')
791
            ->willReturn($object);
792
793
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
794
            ->method('checkAccess')
795
            ->with($this->equalTo('show'))
796
            ->willReturn(true);
797
798
        $show = $this->createMock(FieldDescriptionCollection::class);
799
800
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
801
            ->method('getShow')
802
            ->willReturn($show);
803
804
        $show->expects($this->once())
805
            ->method('getElements')
806
            ->willReturn(['field' => 'fielddata']);
807
808
        $this->assertInstanceOf(Response::class, $this->controller->showAction(null));
809
810
        $this->assertSame($this->admin, $this->parameters['admin']);
811
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
812
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
813
814
        $this->assertSame('show', $this->parameters['action']);
815
        $this->assertInstanceOf(FieldDescriptionCollection::class, $this->parameters['elements']);
816
        $this->assertSame($object, $this->parameters['object']);
817
818
        $this->assertSame([], $this->session->getFlashBag()->all());
819
        $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template);
820
    }
821
822
    /**
823
     * @dataProvider getRedirectToTests
824
     */
825
    public function testRedirectTo(
826
        string $expected,
827
        string $route,
828
        array $queryParams,
829
        array $requestParams,
830
        bool $hasActiveSubclass
831
    ): void {
832
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
833
            ->method('hasActiveSubclass')
834
            ->willReturn($hasActiveSubclass);
835
836
        $object = new \stdClass();
837
838
        foreach ($queryParams as $key => $value) {
839
            $this->request->query->set($key, $value);
840
        }
841
842
        foreach ($requestParams as $key => $value) {
843
            $this->request->request->set($key, $value);
844
        }
845
846
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
847
            ->method('hasRoute')
848
            ->with($this->equalTo($route))
849
            ->willReturn(true);
850
851
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
852
            ->method('hasAccess')
853
            ->with($this->equalTo($route))
854
            ->willReturn(true);
855
856
        $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
857
        $this->assertInstanceOf(RedirectResponse::class, $response);
858
        $this->assertSame($expected, $response->getTargetUrl());
859
    }
860
861
    public function testRedirectToWithObject(): void
862
    {
863
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
864
            ->method('hasActiveSubclass')
865
            ->willReturn(false);
866
867
        $object = new \stdClass();
868
869
        $this->admin->expects($this->at(0))
0 ignored issues
show
Bug introduced by
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...
870
            ->method('hasRoute')
871
            ->with($this->equalTo('edit'))
872
            ->willReturn(true);
873
874
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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('hasAccess')
876
            ->with($this->equalTo('edit'), $object)
877
            ->willReturn(false);
878
879
        $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
880
        $this->assertInstanceOf(RedirectResponse::class, $response);
881
        $this->assertSame('list', $response->getTargetUrl());
882
    }
883
884
    public function getRedirectToTests()
885
    {
886
        return [
887
            ['stdClass_edit', 'edit', [], [], false],
888
            ['list', 'list', ['btn_update_and_list' => true], [], false],
889
            ['list', 'list', ['btn_create_and_list' => true], [], false],
890
            ['create', 'create', ['btn_create_and_create' => true], [], false],
891
            ['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], [], true],
892
            ['stdClass_edit?_tab=first_tab', 'edit', ['btn_update_and_edit' => true], ['_tab' => 'first_tab'], false],
893
        ];
894
    }
895
896
    public function testDeleteActionNotFoundException(): void
897
    {
898
        $this->expectException(NotFoundHttpException::class);
899
900
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
901
            ->method('getObject')
902
            ->willReturn(false);
903
904
        $this->controller->deleteAction(1);
905
    }
906
907
    public function testDeleteActionAccessDenied(): void
908
    {
909
        $this->expectException(AccessDeniedException::class);
910
911
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
912
            ->method('getObject')
913
            ->willReturn(new \stdClass());
914
915
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
916
            ->method('checkAccess')
917
            ->with($this->equalTo('delete'))
918
            ->will($this->throwException(new AccessDeniedException()));
919
920
        $this->controller->deleteAction(1);
921
    }
922
923
    public function testPreDelete(): void
924
    {
925
        $object = new \stdClass();
926
        $object->foo = 123456;
927
928
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
929
            ->method('getObject')
930
            ->willReturn($object);
931
932
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
933
            ->method('checkAccess')
934
            ->with($this->equalTo('delete'))
935
            ->willReturn(true);
936
937
        $controller = new PreCRUDController();
938
        $controller->setContainer($this->container);
939
940
        $response = $controller->deleteAction(null);
941
        $this->assertInstanceOf(Response::class, $response);
942
        $this->assertSame('preDelete called: 123456', $response->getContent());
943
    }
944
945
    public function testDeleteAction(): void
946
    {
947
        $object = new \stdClass();
948
949
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
950
            ->method('getObject')
951
            ->willReturn($object);
952
953
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
954
            ->method('checkAccess')
955
            ->with($this->equalTo('delete'))
956
            ->willReturn(true);
957
958
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1));
959
960
        $this->assertSame($this->admin, $this->parameters['admin']);
961
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
962
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
963
964
        $this->assertSame('delete', $this->parameters['action']);
965
        $this->assertSame($object, $this->parameters['object']);
966
        $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
967
968
        $this->assertSame([], $this->session->getFlashBag()->all());
969
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
970
    }
971
972
    public function testDeleteActionNoCsrfToken(): void
973
    {
974
        $this->container->set('security.csrf.token_manager', null);
975
976
        $object = new \stdClass();
977
978
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
979
            ->method('getObject')
980
            ->willReturn($object);
981
982
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
983
            ->method('checkAccess')
984
            ->with($this->equalTo('delete'))
985
            ->willReturn(true);
986
987
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1));
988
989
        $this->assertSame($this->admin, $this->parameters['admin']);
990
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
991
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
992
993
        $this->assertSame('delete', $this->parameters['action']);
994
        $this->assertSame($object, $this->parameters['object']);
995
        $this->assertFalse($this->parameters['csrf_token']);
996
997
        $this->assertSame([], $this->session->getFlashBag()->all());
998
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
999
    }
1000
1001
    public function testDeleteActionAjaxSuccess1(): void
1002
    {
1003
        $object = new \stdClass();
1004
1005
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1006
            ->method('getObject')
1007
            ->willReturn($object);
1008
1009
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1010
            ->method('checkAccess')
1011
            ->with($this->equalTo('delete'))
1012
            ->willReturn(true);
1013
1014
        $this->request->setMethod(Request::METHOD_DELETE);
1015
1016
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1017
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1018
1019
        $response = $this->controller->deleteAction(1);
1020
1021
        $this->assertInstanceOf(Response::class, $response);
1022
        $this->assertSame(json_encode(['result' => 'ok']), $response->getContent());
1023
        $this->assertSame([], $this->session->getFlashBag()->all());
1024
    }
1025
1026
    public function testDeleteActionAjaxSuccess2(): void
1027
    {
1028
        $object = new \stdClass();
1029
1030
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1031
            ->method('getObject')
1032
            ->willReturn($object);
1033
1034
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1035
            ->method('checkAccess')
1036
            ->with($this->equalTo('delete'))
1037
            ->willReturn(true);
1038
1039
        $this->request->setMethod(Request::METHOD_POST);
1040
        $this->request->request->set('_method', Request::METHOD_DELETE);
1041
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1042
1043
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1044
1045
        $response = $this->controller->deleteAction(1);
1046
1047
        $this->assertInstanceOf(Response::class, $response);
1048
        $this->assertSame(json_encode(['result' => 'ok']), $response->getContent());
1049
        $this->assertSame([], $this->session->getFlashBag()->all());
1050
    }
1051
1052
    public function testDeleteActionAjaxError(): void
1053
    {
1054
        $object = new \stdClass();
1055
1056
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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
Bug introduced by
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('checkAccess')
1062
            ->with($this->equalTo('delete'))
1063
            ->willReturn(true);
1064
1065
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1066
            ->method('getClass')
1067
            ->willReturn('stdClass');
1068
1069
        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
1070
1071
        $this->request->setMethod(Request::METHOD_DELETE);
1072
1073
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1074
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1075
1076
        $response = $this->controller->deleteAction(1);
1077
1078
        $this->assertInstanceOf(Response::class, $response);
1079
        $this->assertSame(json_encode(['result' => 'error']), $response->getContent());
1080
        $this->assertSame([], $this->session->getFlashBag()->all());
1081
    }
1082
1083
    public function testDeleteActionWithModelManagerExceptionInDebugMode(): void
1084
    {
1085
        $this->expectException(ModelManagerException::class);
1086
1087
        $object = new \stdClass();
1088
1089
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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
Bug introduced by
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
            ->willReturn(true);
1097
1098
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1099
            ->method('delete')
1100
            ->willReturnCallback(static function (): void {
1101
                throw new ModelManagerException();
1102
            });
1103
1104
        $this->kernel->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...Kernel\KernelInterface>.

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...
1105
            ->method('isDebug')
1106
            ->willReturn(true);
1107
1108
        $this->request->setMethod(Request::METHOD_DELETE);
1109
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1110
1111
        $this->controller->deleteAction(1);
1112
    }
1113
1114
    /**
1115
     * @dataProvider getToStringValues
1116
     */
1117
    public function testDeleteActionSuccess1(string $expectedToStringValue, string $toStringValue): void
1118
    {
1119
        $object = new \stdClass();
1120
1121
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1122
            ->method('getObject')
1123
            ->willReturn($object);
1124
1125
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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('toString')
1127
            ->with($this->equalTo($object))
1128
            ->willReturn($toStringValue);
1129
1130
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1131
1132
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1133
            ->method('checkAccess')
1134
            ->with($this->equalTo('delete'))
1135
            ->willReturn(true);
1136
1137
        $this->request->setMethod(Request::METHOD_DELETE);
1138
1139
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1140
1141
        $response = $this->controller->deleteAction(1);
1142
1143
        $this->assertInstanceOf(RedirectResponse::class, $response);
1144
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1145
        $this->assertSame('list', $response->getTargetUrl());
1146
    }
1147
1148
    /**
1149
     * @dataProvider getToStringValues
1150
     */
1151
    public function testDeleteActionSuccess2(string $expectedToStringValue, string $toStringValue): void
1152
    {
1153
        $object = new \stdClass();
1154
1155
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1156
            ->method('getObject')
1157
            ->willReturn($object);
1158
1159
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1160
            ->method('checkAccess')
1161
            ->with($this->equalTo('delete'))
1162
            ->willReturn(true);
1163
1164
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1165
            ->method('toString')
1166
            ->with($this->equalTo($object))
1167
            ->willReturn($toStringValue);
1168
1169
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1170
1171
        $this->request->setMethod(Request::METHOD_POST);
1172
        $this->request->request->set('_method', Request::METHOD_DELETE);
1173
1174
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1175
1176
        $response = $this->controller->deleteAction(1);
1177
1178
        $this->assertInstanceOf(RedirectResponse::class, $response);
1179
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1180
        $this->assertSame('list', $response->getTargetUrl());
1181
    }
1182
1183
    /**
1184
     * @dataProvider getToStringValues
1185
     */
1186
    public function testDeleteActionSuccessNoCsrfTokenProvider(string $expectedToStringValue, string $toStringValue): void
1187
    {
1188
        $this->container->set('security.csrf.token_manager', null);
1189
1190
        $object = new \stdClass();
1191
1192
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1193
            ->method('getObject')
1194
            ->willReturn($object);
1195
1196
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1197
            ->method('checkAccess')
1198
            ->with($this->equalTo('delete'))
1199
            ->willReturn(true);
1200
1201
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1202
            ->method('toString')
1203
            ->with($this->equalTo($object))
1204
            ->willReturn($toStringValue);
1205
1206
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1207
1208
        $this->request->setMethod(Request::METHOD_POST);
1209
        $this->request->request->set('_method', Request::METHOD_DELETE);
1210
1211
        $response = $this->controller->deleteAction(1);
1212
1213
        $this->assertInstanceOf(RedirectResponse::class, $response);
1214
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1215
        $this->assertSame('list', $response->getTargetUrl());
1216
    }
1217
1218
    public function testDeleteActionWrongRequestMethod(): void
1219
    {
1220
        $object = new \stdClass();
1221
1222
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1223
            ->method('getObject')
1224
            ->willReturn($object);
1225
1226
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1227
            ->method('checkAccess')
1228
            ->with($this->equalTo('delete'))
1229
            ->willReturn(true);
1230
1231
        //without POST request parameter "_method" should not be used as real REST method
1232
        $this->request->query->set('_method', Request::METHOD_DELETE);
1233
1234
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1));
1235
1236
        $this->assertSame($this->admin, $this->parameters['admin']);
1237
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1238
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1239
1240
        $this->assertSame('delete', $this->parameters['action']);
1241
        $this->assertSame($object, $this->parameters['object']);
1242
        $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
1243
1244
        $this->assertSame([], $this->session->getFlashBag()->all());
1245
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
1246
    }
1247
1248
    /**
1249
     * @dataProvider getToStringValues
1250
     */
1251
    public function testDeleteActionError(string $expectedToStringValue, string $toStringValue): void
1252
    {
1253
        $object = new \stdClass();
1254
1255
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1256
            ->method('getObject')
1257
            ->willReturn($object);
1258
1259
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1260
            ->method('checkAccess')
1261
            ->with($this->equalTo('delete'))
1262
            ->willReturn(true);
1263
1264
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1265
            ->method('toString')
1266
            ->with($this->equalTo($object))
1267
            ->willReturn($toStringValue);
1268
1269
        $this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1270
1271
        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
1272
1273
        $this->request->setMethod(Request::METHOD_DELETE);
1274
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1275
1276
        $response = $this->controller->deleteAction(1);
1277
1278
        $this->assertInstanceOf(RedirectResponse::class, $response);
1279
        $this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error'));
1280
        $this->assertSame('list', $response->getTargetUrl());
1281
    }
1282
1283
    public function testDeleteActionInvalidCsrfToken(): void
1284
    {
1285
        $object = new \stdClass();
1286
1287
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1288
            ->method('getObject')
1289
            ->willReturn($object);
1290
1291
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1292
            ->method('checkAccess')
1293
            ->with($this->equalTo('delete'))
1294
            ->willReturn(true);
1295
1296
        $this->request->setMethod(Request::METHOD_POST);
1297
        $this->request->request->set('_method', Request::METHOD_DELETE);
1298
        $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
1299
1300
        try {
1301
            $this->controller->deleteAction(1);
1302
        } catch (HttpException $e) {
1303
            $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
1304
            $this->assertSame(400, $e->getStatusCode());
1305
        }
1306
    }
1307
1308
    public function testEditActionNotFoundException(): void
1309
    {
1310
        $this->expectException(NotFoundHttpException::class);
1311
1312
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1313
            ->method('getObject')
1314
            ->willReturn(false);
1315
1316
        $this->controller->editAction(null);
1317
    }
1318
1319
    public function testEditActionRuntimeException(): void
1320
    {
1321
        $this->expectException(\RuntimeException::class);
1322
1323
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1324
            ->method('getObject')
1325
            ->willReturn(new \stdClass());
1326
1327
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1328
            ->method('checkAccess')
1329
            ->with($this->equalTo('edit'))
1330
            ->willReturn(true);
1331
1332
        $form = $this->createMock(Form::class);
1333
1334
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1335
            ->method('getForm')
1336
            ->willReturn($form);
1337
1338
        $form->expects($this->once())
1339
            ->method('all')
1340
            ->willReturn([]);
1341
1342
        $this->controller->editAction(null);
1343
    }
1344
1345
    public function testEditActionAccessDenied(): void
1346
    {
1347
        $this->expectException(AccessDeniedException::class);
1348
1349
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1350
            ->method('getObject')
1351
            ->willReturn(new \stdClass());
1352
1353
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1354
            ->method('checkAccess')
1355
            ->with($this->equalTo('edit'))
1356
            ->will($this->throwException(new AccessDeniedException()));
1357
1358
        $this->controller->editAction(null);
1359
    }
1360
1361
    public function testPreEdit(): void
1362
    {
1363
        $object = new \stdClass();
1364
        $object->foo = 123456;
1365
1366
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1367
            ->method('getObject')
1368
            ->willReturn($object);
1369
1370
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1371
            ->method('checkAccess')
1372
            ->with($this->equalTo('edit'))
1373
            ->willReturn(true);
1374
1375
        $controller = new PreCRUDController();
1376
        $controller->setContainer($this->container);
1377
1378
        $response = $controller->editAction(null);
1379
        $this->assertInstanceOf(Response::class, $response);
1380
        $this->assertSame('preEdit called: 123456', $response->getContent());
1381
    }
1382
1383
    public function testEditAction(): void
1384
    {
1385
        $object = new \stdClass();
1386
1387
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1388
            ->method('getObject')
1389
            ->willReturn($object);
1390
1391
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1392
            ->method('checkAccess')
1393
            ->with($this->equalTo('edit'))
1394
            ->willReturn(true);
1395
1396
        $form = $this->createMock(Form::class);
1397
1398
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1399
            ->method('getForm')
1400
            ->willReturn($form);
1401
1402
        $formView = $this->createMock(FormView::class);
1403
1404
        $form
1405
            ->method('createView')
1406
            ->willReturn($formView);
1407
1408
        $form->expects($this->once())
1409
            ->method('all')
1410
            ->willReturn(['field' => 'fielddata']);
1411
1412
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1413
1414
        $this->assertSame($this->admin, $this->parameters['admin']);
1415
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1416
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1417
1418
        $this->assertSame('edit', $this->parameters['action']);
1419
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1420
        $this->assertSame($object, $this->parameters['object']);
1421
        $this->assertSame([], $this->session->getFlashBag()->all());
1422
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1423
    }
1424
1425
    /**
1426
     * @dataProvider getToStringValues
1427
     */
1428
    public function testEditActionSuccess(string $expectedToStringValue, string $toStringValue): void
1429
    {
1430
        $object = new \stdClass();
1431
1432
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1433
            ->method('getObject')
1434
            ->willReturn($object);
1435
1436
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1437
            ->method('update')
1438
            ->willReturnArgument(0);
1439
1440
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1441
            ->method('checkAccess')
1442
            ->with($this->equalTo('edit'));
1443
1444
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1445
            ->method('hasRoute')
1446
            ->with($this->equalTo('edit'))
1447
            ->willReturn(true);
1448
1449
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1450
            ->method('hasAccess')
1451
            ->with($this->equalTo('edit'))
1452
            ->willReturn(true);
1453
1454
        $form = $this->createMock(Form::class);
1455
1456
        $form->expects($this->once())
1457
            ->method('getData')
1458
            ->willReturn($object);
1459
1460
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1461
            ->method('getForm')
1462
            ->willReturn($form);
1463
1464
        $form->expects($this->once())
1465
            ->method('isSubmitted')
1466
            ->willReturn(true);
1467
1468
        $form->expects($this->once())
1469
            ->method('isValid')
1470
            ->willReturn(true);
1471
1472
        $form->expects($this->once())
1473
            ->method('all')
1474
            ->willReturn(['field' => 'fielddata']);
1475
1476
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1477
            ->method('toString')
1478
            ->with($this->equalTo($object))
1479
            ->willReturn($toStringValue);
1480
1481
        $this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1482
1483
        $this->request->setMethod(Request::METHOD_POST);
1484
1485
        $response = $this->controller->editAction(null);
1486
1487
        $this->assertInstanceOf(RedirectResponse::class, $response);
1488
        $this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1489
        $this->assertSame('stdClass_edit', $response->getTargetUrl());
1490
    }
1491
1492
    /**
1493
     * @dataProvider getToStringValues
1494
     */
1495
    public function testEditActionError(string $expectedToStringValue, string $toStringValue): void
1496
    {
1497
        $object = new \stdClass();
1498
1499
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1500
            ->method('getObject')
1501
            ->willReturn($object);
1502
1503
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1504
            ->method('checkAccess')
1505
            ->with($this->equalTo('edit'))
1506
            ->willReturn(true);
1507
1508
        $form = $this->createMock(Form::class);
1509
1510
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1511
            ->method('getForm')
1512
            ->willReturn($form);
1513
1514
        $form->expects($this->once())
1515
            ->method('isSubmitted')
1516
            ->willReturn(true);
1517
1518
        $form->expects($this->once())
1519
            ->method('isValid')
1520
            ->willReturn(false);
1521
1522
        $form->expects($this->once())
1523
            ->method('all')
1524
            ->willReturn(['field' => 'fielddata']);
1525
1526
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1527
            ->method('toString')
1528
            ->with($this->equalTo($object))
1529
            ->willReturn($toStringValue);
1530
1531
        $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1532
1533
        $this->request->setMethod(Request::METHOD_POST);
1534
1535
        $formView = $this->createMock(FormView::class);
1536
1537
        $form
1538
            ->method('createView')
1539
            ->willReturn($formView);
1540
1541
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1542
1543
        $this->assertSame($this->admin, $this->parameters['admin']);
1544
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1545
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1546
1547
        $this->assertSame('edit', $this->parameters['action']);
1548
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1549
        $this->assertSame($object, $this->parameters['object']);
1550
1551
        $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all());
1552
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1553
    }
1554
1555
    public function testEditActionAjaxSuccess(): void
1556
    {
1557
        $object = new \stdClass();
1558
1559
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1560
            ->method('getObject')
1561
            ->willReturn($object);
1562
1563
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1564
            ->method('update')
1565
            ->willReturnArgument(0);
1566
1567
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1568
            ->method('checkAccess')
1569
            ->with($this->equalTo('edit'))
1570
            ->willReturn(true);
1571
1572
        $form = $this->createMock(Form::class);
1573
1574
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1575
            ->method('getForm')
1576
            ->willReturn($form);
1577
1578
        $form->expects($this->once())
1579
            ->method('isSubmitted')
1580
            ->willReturn(true);
1581
1582
        $form->expects($this->once())
1583
            ->method('isValid')
1584
            ->willReturn(true);
1585
1586
        $form->expects($this->once())
1587
            ->method('getData')
1588
            ->willReturn($object);
1589
1590
        $form->expects($this->once())
1591
            ->method('all')
1592
            ->willReturn(['field' => 'fielddata']);
1593
1594
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1595
            ->method('getNormalizedIdentifier')
1596
            ->with($this->equalTo($object))
1597
            ->willReturn('foo_normalized');
1598
1599
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1600
            ->method('toString')
1601
            ->willReturn('foo');
1602
1603
        $this->request->setMethod(Request::METHOD_POST);
1604
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1605
1606
        $response = $this->controller->editAction(null);
1607
1608
        $this->assertInstanceOf(Response::class, $response);
1609
        $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent());
1610
        $this->assertSame([], $this->session->getFlashBag()->all());
1611
    }
1612
1613
    public function testEditActionAjaxError(): void
1614
    {
1615
        $object = new \stdClass();
1616
1617
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1618
            ->method('getObject')
1619
            ->willReturn($object);
1620
1621
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1622
            ->method('checkAccess')
1623
            ->with($this->equalTo('edit'))
1624
            ->willReturn(true);
1625
1626
        $form = $this->createMock(Form::class);
1627
1628
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1629
            ->method('getForm')
1630
            ->willReturn($form);
1631
1632
        $form->expects($this->once())
1633
            ->method('isSubmitted')
1634
            ->willReturn(true);
1635
1636
        $form->expects($this->once())
1637
            ->method('isValid')
1638
            ->willReturn(false);
1639
1640
        $form->expects($this->once())
1641
            ->method('all')
1642
            ->willReturn(['field' => 'fielddata']);
1643
1644
        $formError = $this->createMock(FormError::class);
1645
        $formError->expects($this->atLeastOnce())
1646
            ->method('getMessage')
1647
            ->willReturn('Form error message');
1648
1649
        $form->expects($this->once())
1650
            ->method('getErrors')
1651
            ->with(true)
1652
            ->willReturn([$formError]);
1653
1654
        $this->request->setMethod(Request::METHOD_POST);
1655
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1656
        $this->request->headers->set('Accept', 'application/json');
1657
1658
        $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->editAction(null));
1659
        $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent());
1660
    }
1661
1662
    /**
1663
     * @legacy
1664
     * @expectedDeprecation In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`
1665
     */
1666
    public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
1667
    {
1668
        $object = new \stdClass();
1669
1670
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1671
            ->method('getObject')
1672
            ->willReturn($object);
1673
1674
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1675
            ->method('checkAccess')
1676
            ->with($this->equalTo('edit'))
1677
            ->willReturn(true);
1678
1679
        $form = $this->createMock(Form::class);
1680
1681
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1682
            ->method('getForm')
1683
            ->willReturn($form);
1684
1685
        $form->expects($this->once())
1686
            ->method('isSubmitted')
1687
            ->willReturn(true);
1688
1689
        $form->expects($this->once())
1690
            ->method('isValid')
1691
            ->willReturn(false);
1692
1693
        $form->expects($this->once())
1694
            ->method('all')
1695
            ->willReturn(['field' => 'fielddata']);
1696
1697
        $this->request->setMethod(Request::METHOD_POST);
1698
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1699
1700
        $formView = $this->createMock(FormView::class);
1701
        $form
1702
            ->method('createView')
1703
            ->willReturn($formView);
1704
1705
        $this->expectTranslate('flash_edit_error', [
1706
            '%name%' => '',
1707
        ], 'SonataAdminBundle');
1708
1709
        $this->assertInstanceOf(Response::class, $response = $this->controller->editAction(null));
1710
        $this->assertSame($this->admin, $this->parameters['admin']);
1711
        $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
1712
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1713
        $this->assertSame('edit', $this->parameters['action']);
1714
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1715
        $this->assertSame($object, $this->parameters['object']);
1716
        $this->assertSame(['sonata_flash_error' => [0 => 'flash_edit_error']], $this->session->getFlashBag()->all());
1717
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1718
    }
1719
1720
    /**
1721
     * @dataProvider getToStringValues
1722
     */
1723
    public function testEditActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void
1724
    {
1725
        $object = new \stdClass();
1726
1727
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1728
            ->method('getObject')
1729
            ->willReturn($object);
1730
1731
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1732
            ->method('checkAccess')
1733
            ->with($this->equalTo('edit'))
1734
            ->willReturn(true);
1735
1736
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1737
            ->method('getClass')
1738
            ->willReturn('stdClass');
1739
1740
        $form = $this->createMock(Form::class);
1741
1742
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1743
            ->method('getForm')
1744
            ->willReturn($form);
1745
1746
        $form->expects($this->once())
1747
            ->method('isValid')
1748
            ->willReturn(true);
1749
1750
        $form->expects($this->once())
1751
            ->method('getData')
1752
            ->willReturn($object);
1753
1754
        $form->expects($this->once())
1755
            ->method('all')
1756
            ->willReturn(['field' => 'fielddata']);
1757
1758
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1759
            ->method('toString')
1760
            ->with($this->equalTo($object))
1761
            ->willReturn($toStringValue);
1762
1763
        $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1764
1765
        $form->expects($this->once())
1766
            ->method('isSubmitted')
1767
            ->willReturn(true);
1768
        $this->request->setMethod(Request::METHOD_POST);
1769
1770
        $formView = $this->createMock(FormView::class);
1771
1772
        $form
1773
            ->method('createView')
1774
            ->willReturn($formView);
1775
1776
        $this->assertLoggerLogsModelManagerException($this->admin, 'update');
1777
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1778
1779
        $this->assertSame($this->admin, $this->parameters['admin']);
1780
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1781
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1782
1783
        $this->assertSame('edit', $this->parameters['action']);
1784
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1785
        $this->assertSame($object, $this->parameters['object']);
1786
1787
        $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all());
1788
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1789
    }
1790
1791
    public function testEditActionWithPreview(): void
1792
    {
1793
        $object = new \stdClass();
1794
1795
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1796
            ->method('getObject')
1797
            ->willReturn($object);
1798
1799
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1800
            ->method('checkAccess')
1801
            ->with($this->equalTo('edit'))
1802
            ->willReturn(true);
1803
1804
        $form = $this->createMock(Form::class);
1805
1806
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1807
            ->method('getForm')
1808
            ->willReturn($form);
1809
1810
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1811
            ->method('supportsPreviewMode')
1812
            ->willReturn(true);
1813
1814
        $formView = $this->createMock(FormView::class);
1815
1816
        $form
1817
            ->method('createView')
1818
            ->willReturn($formView);
1819
1820
        $form->expects($this->once())
1821
            ->method('isSubmitted')
1822
            ->willReturn(true);
1823
1824
        $form->expects($this->once())
1825
            ->method('isValid')
1826
            ->willReturn(true);
1827
1828
        $form->expects($this->once())
1829
            ->method('all')
1830
            ->willReturn(['field' => 'fielddata']);
1831
1832
        $this->request->setMethod(Request::METHOD_POST);
1833
        $this->request->request->set('btn_preview', 'Preview');
1834
1835
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1836
1837
        $this->assertSame($this->admin, $this->parameters['admin']);
1838
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1839
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1840
1841
        $this->assertSame('edit', $this->parameters['action']);
1842
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1843
        $this->assertSame($object, $this->parameters['object']);
1844
1845
        $this->assertSame([], $this->session->getFlashBag()->all());
1846
        $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template);
1847
    }
1848
1849
    public function testEditActionWithLockException(): void
1850
    {
1851
        $object = new \stdClass();
1852
        $class = \get_class($object);
1853
1854
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1855
            ->method('getObject')
1856
            ->willReturn($object);
1857
1858
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1859
            ->method('checkAccess')
1860
            ->with($this->equalTo('edit'))
1861
            ->willReturn(true);
1862
1863
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1864
            ->method('getClass')
1865
            ->willReturn($class);
1866
1867
        $form = $this->createMock(Form::class);
1868
1869
        $form
1870
            ->method('isValid')
1871
            ->willReturn(true);
1872
1873
        $form->expects($this->once())
1874
            ->method('getData')
1875
            ->willReturn($object);
1876
1877
        $form->expects($this->once())
1878
            ->method('all')
1879
            ->willReturn(['field' => 'fielddata']);
1880
1881
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1882
            ->method('getForm')
1883
            ->willReturn($form);
1884
1885
        $form
1886
            ->method('isSubmitted')
1887
            ->willReturn(true);
1888
        $this->request->setMethod(Request::METHOD_POST);
1889
1890
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1891
            ->method('update')
1892
            ->will($this->throwException(new LockException()));
1893
1894
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1895
            ->method('toString')
1896
            ->with($this->equalTo($object))
1897
            ->willReturn($class);
1898
1899
        $formView = $this->createMock(FormView::class);
1900
1901
        $form
1902
            ->method('createView')
1903
            ->willReturn($formView);
1904
1905
        $this->expectTranslate('flash_lock_error', [
1906
            '%name%' => $class,
1907
            '%link_start%' => '<a href="stdClass_edit">',
1908
            '%link_end%' => '</a>',
1909
        ], 'SonataAdminBundle');
1910
1911
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1912
    }
1913
1914
    public function testCreateActionAccessDenied(): void
1915
    {
1916
        $this->expectException(AccessDeniedException::class);
1917
1918
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1919
            ->method('checkAccess')
1920
            ->with($this->equalTo('create'))
1921
            ->will($this->throwException(new AccessDeniedException()));
1922
1923
        $this->controller->createAction();
1924
    }
1925
1926
    public function testCreateActionRuntimeException(): void
1927
    {
1928
        $this->expectException(\RuntimeException::class);
1929
1930
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1931
            ->method('checkAccess')
1932
            ->with($this->equalTo('create'))
1933
            ->willReturn(true);
1934
1935
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1936
            ->method('getClass')
1937
            ->willReturn('stdClass');
1938
1939
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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(new \stdClass());
1942
1943
        $form = $this->createMock(Form::class);
1944
1945
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1946
            ->method('getForm')
1947
            ->willReturn($form);
1948
1949
        $form->expects($this->once())
1950
            ->method('all')
1951
            ->willReturn([]);
1952
1953
        $this->controller->createAction();
1954
    }
1955
1956
    public function testPreCreate(): void
1957
    {
1958
        $object = new \stdClass();
1959
        $object->foo = 123456;
1960
1961
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1962
            ->method('checkAccess')
1963
            ->with($this->equalTo('create'))
1964
            ->willReturn(true);
1965
1966
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
1967
            ->method('getClass')
1968
            ->willReturn('stdClass');
1969
1970
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1971
            ->method('getNewInstance')
1972
            ->willReturn($object);
1973
1974
        $controller = new PreCRUDController();
1975
        $controller->setContainer($this->container);
1976
1977
        $response = $controller->createAction();
1978
        $this->assertInstanceOf(Response::class, $response);
1979
        $this->assertSame('preCreate called: 123456', $response->getContent());
1980
    }
1981
1982
    public function testCreateAction(): void
1983
    {
1984
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1985
            ->method('checkAccess')
1986
            ->with($this->equalTo('create'))
1987
            ->willReturn(true);
1988
1989
        $object = new \stdClass();
1990
1991
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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('getClass')
1993
            ->willReturn('stdClass');
1994
1995
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
1996
            ->method('getNewInstance')
1997
            ->willReturn($object);
1998
1999
        $form = $this->createMock(Form::class);
2000
2001
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2002
            ->method('getForm')
2003
            ->willReturn($form);
2004
2005
        $form->expects($this->once())
2006
            ->method('all')
2007
            ->willReturn(['field' => 'fielddata']);
2008
2009
        $formView = $this->createMock(FormView::class);
2010
2011
        $form
2012
            ->method('createView')
2013
            ->willReturn($formView);
2014
2015
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2016
2017
        $this->assertSame($this->admin, $this->parameters['admin']);
2018
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2019
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2020
2021
        $this->assertSame('create', $this->parameters['action']);
2022
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2023
        $this->assertSame($object, $this->parameters['object']);
2024
2025
        $this->assertSame([], $this->session->getFlashBag()->all());
2026
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2027
    }
2028
2029
    /**
2030
     * @dataProvider getToStringValues
2031
     */
2032
    public function testCreateActionSuccess(string $expectedToStringValue, string $toStringValue): void
2033
    {
2034
        $object = new \stdClass();
2035
2036
        $this->admin->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
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...
2037
            ->method('checkAccess')
2038
            ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): bool {
2039
                if ('edit' === $name) {
2040
                    return true;
2041
                }
2042
2043
                if ('create' !== $name) {
2044
                    return false;
2045
                }
2046
2047
                if (null === $objectIn) {
2048
                    return true;
2049
                }
2050
2051
                return $objectIn === $object;
2052
            });
2053
2054
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2055
            ->method('hasRoute')
2056
            ->with($this->equalTo('edit'))
2057
            ->willReturn(true);
2058
2059
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2060
            ->method('hasAccess')
2061
            ->with($this->equalTo('edit'))
2062
            ->willReturn(true);
2063
2064
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2065
            ->method('getNewInstance')
2066
            ->willReturn($object);
2067
2068
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2069
            ->method('create')
2070
            ->willReturnArgument(0);
2071
2072
        $form = $this->createMock(Form::class);
2073
2074
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2075
            ->method('getClass')
2076
            ->willReturn('stdClass');
2077
2078
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2079
            ->method('getForm')
2080
            ->willReturn($form);
2081
2082
        $form->expects($this->once())
2083
            ->method('all')
2084
            ->willReturn(['field' => 'fielddata']);
2085
2086
        $form->expects($this->once())
2087
            ->method('isSubmitted')
2088
            ->willReturn(true);
2089
2090
        $form->expects($this->once())
2091
            ->method('isValid')
2092
            ->willReturn(true);
2093
2094
        $form->expects($this->once())
2095
            ->method('getData')
2096
            ->willReturn($object);
2097
2098
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2099
            ->method('toString')
2100
            ->with($this->equalTo($object))
2101
            ->willReturn($toStringValue);
2102
2103
        $this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2104
2105
        $this->request->setMethod(Request::METHOD_POST);
2106
2107
        $response = $this->controller->createAction();
2108
2109
        $this->assertInstanceOf(RedirectResponse::class, $response);
2110
        $this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
2111
        $this->assertSame('stdClass_edit', $response->getTargetUrl());
2112
    }
2113
2114
    public function testCreateActionAccessDenied2(): void
2115
    {
2116
        $this->expectException(AccessDeniedException::class);
2117
2118
        $object = new \stdClass();
2119
2120
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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('checkAccess')
2122
            ->willReturnCallback(static function (string $name, $object = null): bool {
2123
                if ('create' !== $name) {
2124
                    throw new AccessDeniedException();
2125
                }
2126
                if (null === $object) {
2127
                    return true;
2128
                }
2129
2130
                throw new AccessDeniedException();
2131
            });
2132
2133
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2134
            ->method('getNewInstance')
2135
            ->willReturn($object);
2136
2137
        $form = $this->createMock(Form::class);
2138
2139
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2140
            ->method('getClass')
2141
            ->willReturn('stdClass');
2142
2143
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2144
            ->method('getForm')
2145
            ->willReturn($form);
2146
2147
        $form->expects($this->once())
2148
            ->method('all')
2149
            ->willReturn(['field' => 'fielddata']);
2150
2151
        $form->expects($this->once())
2152
            ->method('isSubmitted')
2153
            ->willReturn(true);
2154
2155
        $form->expects($this->once())
2156
            ->method('getData')
2157
            ->willReturn($object);
2158
2159
        $form->expects($this->once())
2160
            ->method('isValid')
2161
            ->willReturn(true);
2162
2163
        $this->request->setMethod(Request::METHOD_POST);
2164
2165
        $this->controller->createAction();
2166
    }
2167
2168
    /**
2169
     * @dataProvider getToStringValues
2170
     */
2171
    public function testCreateActionError(string $expectedToStringValue, string $toStringValue): void
2172
    {
2173
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2174
            ->method('checkAccess')
2175
            ->with($this->equalTo('create'))
2176
            ->willReturn(true);
2177
2178
        $object = new \stdClass();
2179
2180
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2181
            ->method('getClass')
2182
            ->willReturn('stdClass');
2183
2184
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2185
            ->method('getNewInstance')
2186
            ->willReturn($object);
2187
2188
        $form = $this->createMock(Form::class);
2189
2190
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2191
            ->method('getForm')
2192
            ->willReturn($form);
2193
2194
        $form->expects($this->once())
2195
            ->method('all')
2196
            ->willReturn(['field' => 'fielddata']);
2197
2198
        $form->expects($this->once())
2199
            ->method('isSubmitted')
2200
            ->willReturn(true);
2201
2202
        $form->expects($this->once())
2203
            ->method('isValid')
2204
            ->willReturn(false);
2205
2206
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2207
            ->method('toString')
2208
            ->with($this->equalTo($object))
2209
            ->willReturn($toStringValue);
2210
2211
        $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2212
2213
        $this->request->setMethod(Request::METHOD_POST);
2214
2215
        $formView = $this->createMock(FormView::class);
2216
2217
        $form
2218
            ->method('createView')
2219
            ->willReturn($formView);
2220
2221
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2222
2223
        $this->assertSame($this->admin, $this->parameters['admin']);
2224
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2225
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2226
2227
        $this->assertSame('create', $this->parameters['action']);
2228
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2229
        $this->assertSame($object, $this->parameters['object']);
2230
2231
        $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all());
2232
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2233
    }
2234
2235
    /**
2236
     * @dataProvider getToStringValues
2237
     */
2238
    public function testCreateActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void
2239
    {
2240
        $this->admin->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
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...
2241
            ->method('checkAccess')
2242
            ->with($this->equalTo('create'))
2243
            ->willReturn(true);
2244
2245
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2246
            ->method('getClass')
2247
            ->willReturn('stdClass');
2248
2249
        $object = new \stdClass();
2250
2251
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2252
            ->method('getNewInstance')
2253
            ->willReturn($object);
2254
2255
        $form = $this->createMock(Form::class);
2256
2257
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2258
            ->method('getForm')
2259
            ->willReturn($form);
2260
2261
        $form->expects($this->once())
2262
            ->method('all')
2263
            ->willReturn(['field' => 'fielddata']);
2264
2265
        $form->expects($this->once())
2266
            ->method('isValid')
2267
            ->willReturn(true);
2268
2269
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2270
            ->method('toString')
2271
            ->with($this->equalTo($object))
2272
            ->willReturn($toStringValue);
2273
2274
        $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2275
2276
        $form->expects($this->once())
2277
            ->method('isSubmitted')
2278
            ->willReturn(true);
2279
2280
        $form->expects($this->once())
2281
            ->method('getData')
2282
            ->willReturn($object);
2283
2284
        $this->request->setMethod(Request::METHOD_POST);
2285
2286
        $formView = $this->createMock(FormView::class);
2287
2288
        $form
2289
            ->method('createView')
2290
            ->willReturn($formView);
2291
2292
        $this->assertLoggerLogsModelManagerException($this->admin, 'create');
2293
2294
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2295
2296
        $this->assertSame($this->admin, $this->parameters['admin']);
2297
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2298
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2299
2300
        $this->assertSame('create', $this->parameters['action']);
2301
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2302
        $this->assertSame($object, $this->parameters['object']);
2303
2304
        $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all());
2305
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2306
    }
2307
2308
    public function testCreateActionAjaxSuccess(): void
2309
    {
2310
        $object = new \stdClass();
2311
2312
        $this->admin->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
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...
2313
            ->method('checkAccess')
2314
            ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): bool {
2315
                if ('create' !== $name) {
2316
                    return false;
2317
                }
2318
2319
                if (null === $objectIn) {
2320
                    return true;
2321
                }
2322
2323
                return $objectIn === $object;
2324
            });
2325
2326
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2327
            ->method('getNewInstance')
2328
            ->willReturn($object);
2329
2330
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2331
            ->method('create')
2332
            ->willReturnArgument(0);
2333
2334
        $form = $this->createMock(Form::class);
2335
2336
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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('getForm')
2338
            ->willReturn($form);
2339
2340
        $form->expects($this->once())
2341
            ->method('all')
2342
            ->willReturn(['field' => 'fielddata']);
2343
2344
        $form->expects($this->once())
2345
            ->method('isSubmitted')
2346
            ->willReturn(true);
2347
2348
        $form->expects($this->once())
2349
            ->method('isValid')
2350
            ->willReturn(true);
2351
2352
        $form->expects($this->once())
2353
            ->method('getData')
2354
            ->willReturn($object);
2355
2356
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2357
            ->method('getClass')
2358
            ->willReturn('stdClass');
2359
2360
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2361
            ->method('getNormalizedIdentifier')
2362
            ->with($this->equalTo($object))
2363
            ->willReturn('foo_normalized');
2364
2365
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2366
            ->method('toString')
2367
            ->willReturn('foo');
2368
2369
        $this->request->setMethod(Request::METHOD_POST);
2370
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2371
2372
        $response = $this->controller->createAction();
2373
2374
        $this->assertInstanceOf(Response::class, $response);
2375
        $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent());
2376
        $this->assertSame([], $this->session->getFlashBag()->all());
2377
    }
2378
2379
    public function testCreateActionAjaxError(): void
2380
    {
2381
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2382
            ->method('checkAccess')
2383
            ->with($this->equalTo('create'))
2384
            ->willReturn(true);
2385
2386
        $object = new \stdClass();
2387
2388
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2389
            ->method('getNewInstance')
2390
            ->willReturn($object);
2391
2392
        $form = $this->createMock(Form::class);
2393
2394
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2395
            ->method('getClass')
2396
            ->willReturn('stdClass');
2397
2398
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2399
            ->method('getForm')
2400
            ->willReturn($form);
2401
2402
        $form->expects($this->once())
2403
            ->method('all')
2404
            ->willReturn(['field' => 'fielddata']);
2405
2406
        $form->expects($this->once())
2407
            ->method('isSubmitted')
2408
            ->willReturn(true);
2409
2410
        $form->expects($this->once())
2411
            ->method('isValid')
2412
            ->willReturn(false);
2413
2414
        $formError = $this->createMock(FormError::class);
2415
        $formError->expects($this->atLeastOnce())
2416
            ->method('getMessage')
2417
            ->willReturn('Form error message');
2418
2419
        $form->expects($this->once())
2420
            ->method('getErrors')
2421
            ->with(true)
2422
            ->willReturn([$formError]);
2423
2424
        $this->request->setMethod(Request::METHOD_POST);
2425
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2426
        $this->request->headers->set('Accept', 'application/json');
2427
2428
        $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->createAction());
2429
        $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent());
2430
    }
2431
2432
    /**
2433
     * @legacy
2434
     * @expectedDeprecation In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`
2435
     */
2436
    public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void
2437
    {
2438
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2439
            ->method('checkAccess')
2440
            ->with($this->equalTo('create'))
2441
            ->willReturn(true);
2442
2443
        $object = new \stdClass();
2444
2445
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2446
            ->method('getNewInstance')
2447
            ->willReturn($object);
2448
2449
        $form = $this->createMock(Form::class);
2450
2451
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2452
            ->method('getClass')
2453
            ->willReturn('stdClass');
2454
2455
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2456
            ->method('getForm')
2457
            ->willReturn($form);
2458
2459
        $form->expects($this->once())
2460
            ->method('all')
2461
            ->willReturn(['field' => 'fielddata']);
2462
2463
        $form->expects($this->once())
2464
            ->method('isSubmitted')
2465
            ->willReturn(true);
2466
2467
        $form->expects($this->once())
2468
            ->method('isValid')
2469
            ->willReturn(false);
2470
2471
        $this->request->setMethod(Request::METHOD_POST);
2472
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2473
2474
        $formView = $this->createMock(FormView::class);
2475
        $form
2476
            ->method('createView')
2477
            ->willReturn($formView);
2478
2479
        $this->expectTranslate('flash_create_error', [
2480
            '%name%' => '',
2481
        ], 'SonataAdminBundle');
2482
2483
        $this->assertInstanceOf(Response::class, $response = $this->controller->createAction());
2484
        $this->assertSame($this->admin, $this->parameters['admin']);
2485
        $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
2486
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2487
        $this->assertSame('create', $this->parameters['action']);
2488
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2489
        $this->assertSame($object, $this->parameters['object']);
2490
        $this->assertSame(['sonata_flash_error' => [0 => 'flash_create_error']], $this->session->getFlashBag()->all());
2491
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2492
    }
2493
2494
    public function testCreateActionWithPreview(): void
2495
    {
2496
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2497
            ->method('checkAccess')
2498
            ->with($this->equalTo('create'))
2499
            ->willReturn(true);
2500
2501
        $object = new \stdClass();
2502
2503
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2504
            ->method('getNewInstance')
2505
            ->willReturn($object);
2506
2507
        $form = $this->createMock(Form::class);
2508
2509
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2510
            ->method('getClass')
2511
            ->willReturn('stdClass');
2512
2513
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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('getForm')
2515
            ->willReturn($form);
2516
2517
        $form->expects($this->once())
2518
            ->method('all')
2519
            ->willReturn(['field' => 'fielddata']);
2520
2521
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2522
            ->method('supportsPreviewMode')
2523
            ->willReturn(true);
2524
2525
        $formView = $this->createMock(FormView::class);
2526
2527
        $form
2528
            ->method('createView')
2529
            ->willReturn($formView);
2530
2531
        $form->expects($this->once())
2532
            ->method('isSubmitted')
2533
            ->willReturn(true);
2534
2535
        $form->expects($this->once())
2536
            ->method('isValid')
2537
            ->willReturn(true);
2538
2539
        $this->request->setMethod(Request::METHOD_POST);
2540
        $this->request->request->set('btn_preview', 'Preview');
2541
2542
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2543
2544
        $this->assertSame($this->admin, $this->parameters['admin']);
2545
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2546
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2547
2548
        $this->assertSame('create', $this->parameters['action']);
2549
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2550
        $this->assertSame($object, $this->parameters['object']);
2551
2552
        $this->assertSame([], $this->session->getFlashBag()->all());
2553
        $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template);
2554
    }
2555
2556
    public function testExportActionAccessDenied(): void
2557
    {
2558
        $this->expectException(AccessDeniedException::class);
2559
2560
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2561
            ->method('checkAccess')
2562
            ->with($this->equalTo('export'))
2563
            ->will($this->throwException(new AccessDeniedException()));
2564
2565
        $this->controller->exportAction($this->request);
2566
    }
2567
2568
    public function testExportActionWrongFormat(): void
2569
    {
2570
        $this->expectException(\RuntimeException::class);
2571
        $this->expectExceptionMessage(
2572
            'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`'
2573
        );
2574
2575
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2576
            ->method('checkAccess')
2577
            ->with($this->equalTo('export'))
2578
            ->willReturn(true);
2579
2580
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2581
            ->method('getExportFormats')
2582
            ->willReturn(['json']);
2583
2584
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2585
            ->method('getClass')
2586
            ->willReturn('Foo');
2587
2588
        $this->request->query->set('format', 'csv');
2589
2590
        $this->controller->exportAction($this->request);
2591
    }
2592
2593
    public function testExportAction(): void
2594
    {
2595
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2596
            ->method('checkAccess')
2597
            ->with($this->equalTo('export'))
2598
            ->willReturn(true);
2599
2600
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2601
            ->method('getExportFormats')
2602
            ->willReturn(['json']);
2603
2604
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2605
            ->method('getClass')
2606
            ->willReturn(\stdClass::class);
2607
2608
        $dataSourceIterator = $this->createMock(SourceIteratorInterface::class);
2609
2610
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2611
            ->method('getDataSourceIterator')
2612
            ->willReturn($dataSourceIterator);
2613
2614
        $this->request->query->set('format', 'json');
2615
2616
        $response = $this->controller->exportAction($this->request);
2617
        $this->assertInstanceOf(StreamedResponse::class, $response);
2618
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
2619
        $this->assertSame([], $this->session->getFlashBag()->all());
2620
    }
2621
2622
    public function testHistoryActionAccessDenied(): void
2623
    {
2624
        $this->expectException(AccessDeniedException::class);
2625
2626
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2627
            ->method('getObject')
2628
            ->willReturn(new \stdClass());
2629
2630
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2631
            ->method('checkAccess')
2632
            ->with($this->equalTo('history'))
2633
            ->will($this->throwException(new AccessDeniedException()));
2634
2635
        $this->controller->historyAction(null);
2636
    }
2637
2638
    public function testHistoryActionNotFoundException(): void
2639
    {
2640
        $this->expectException(NotFoundHttpException::class);
2641
2642
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2643
            ->method('getObject')
2644
            ->willReturn(false);
2645
2646
        $this->controller->historyAction(null);
2647
    }
2648
2649
    public function testHistoryActionNoReader(): void
2650
    {
2651
        $this->expectException(NotFoundHttpException::class);
2652
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
2653
2654
        $this->request->query->set('id', 123);
2655
2656
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2657
            ->method('checkAccess')
2658
            ->with($this->equalTo('history'))
2659
            ->willReturn(true);
2660
2661
        $object = new \stdClass();
2662
2663
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2664
            ->method('getObject')
2665
            ->willReturn($object);
2666
2667
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2668
            ->method('getClass')
2669
            ->willReturn('Foo');
2670
2671
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
2672
            ->method('hasReader')
2673
            ->with($this->equalTo('Foo'))
2674
            ->willReturn(false);
2675
2676
        $this->controller->historyAction(null);
2677
    }
2678
2679
    public function testHistoryAction(): void
2680
    {
2681
        $this->request->query->set('id', 123);
2682
2683
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2684
            ->method('checkAccess')
2685
            ->with($this->equalTo('history'))
2686
            ->willReturn(true);
2687
2688
        $object = new \stdClass();
2689
2690
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2691
            ->method('getObject')
2692
            ->willReturn($object);
2693
2694
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2695
            ->method('getClass')
2696
            ->willReturn('Foo');
2697
2698
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
2699
            ->method('hasReader')
2700
            ->with($this->equalTo('Foo'))
2701
            ->willReturn(true);
2702
2703
        $reader = $this->createMock(AuditReaderInterface::class);
2704
2705
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
2706
            ->method('getReader')
2707
            ->with($this->equalTo('Foo'))
2708
            ->willReturn($reader);
2709
2710
        $reader->expects($this->once())
2711
            ->method('findRevisions')
2712
            ->with($this->equalTo('Foo'), $this->equalTo(123))
2713
            ->willReturn([]);
2714
2715
        $this->assertInstanceOf(Response::class, $this->controller->historyAction(null));
2716
2717
        $this->assertSame($this->admin, $this->parameters['admin']);
2718
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2719
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2720
2721
        $this->assertSame('history', $this->parameters['action']);
2722
        $this->assertSame([], $this->parameters['revisions']);
2723
        $this->assertSame($object, $this->parameters['object']);
2724
2725
        $this->assertSame([], $this->session->getFlashBag()->all());
2726
        $this->assertSame('@SonataAdmin/CRUD/history.html.twig', $this->template);
2727
    }
2728
2729
    public function testAclActionAclNotEnabled(): void
2730
    {
2731
        $this->expectException(NotFoundHttpException::class);
2732
        $this->expectExceptionMessage('ACL are not enabled for this admin');
2733
2734
        $this->controller->aclAction(null);
2735
    }
2736
2737
    public function testAclActionNotFoundException(): void
2738
    {
2739
        $this->expectException(NotFoundHttpException::class);
2740
2741
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2742
            ->method('isAclEnabled')
2743
            ->willReturn(true);
2744
2745
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2746
            ->method('getObject')
2747
            ->willReturn(false);
2748
2749
        $this->controller->aclAction(null);
2750
    }
2751
2752
    public function testAclActionAccessDenied(): void
2753
    {
2754
        $this->expectException(AccessDeniedException::class);
2755
2756
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2757
            ->method('isAclEnabled')
2758
            ->willReturn(true);
2759
2760
        $object = new \stdClass();
2761
2762
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2763
            ->method('getObject')
2764
            ->willReturn($object);
2765
2766
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2767
            ->method('checkAccess')
2768
            ->with($this->equalTo('acl'), $this->equalTo($object))
2769
            ->will($this->throwException(new AccessDeniedException()));
2770
2771
        $this->controller->aclAction(null);
2772
    }
2773
2774
    public function testAclAction(): void
2775
    {
2776
        $this->request->query->set('id', 123);
2777
2778
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2779
            ->method('isAclEnabled')
2780
            ->willReturn(true);
2781
2782
        $object = new \stdClass();
2783
2784
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2785
            ->method('getObject')
2786
            ->willReturn($object);
2787
2788
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2789
            ->method('checkAccess')
2790
            ->willReturn(true);
2791
2792
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2793
            ->method('getSecurityInformation')
2794
            ->willReturn([]);
2795
2796
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2797
            ->method('getMaskBuilderClass')
2798
            ->willReturn(AdminPermissionMap::class);
2799
2800
        $aclUsersForm = $this->getMockBuilder(Form::class)
2801
            ->disableOriginalConstructor()
2802
            ->getMock();
2803
2804
        $aclUsersForm->expects($this->once())
2805
            ->method('createView')
2806
            ->willReturn($this->createMock(FormView::class));
2807
2808
        $aclRolesForm = $this->getMockBuilder(Form::class)
2809
            ->disableOriginalConstructor()
2810
            ->getMock();
2811
2812
        $aclRolesForm->expects($this->once())
2813
            ->method('createView')
2814
            ->willReturn($this->createMock(FormView::class));
2815
2816
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2817
            ->method('createAclUsersForm')
2818
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2819
            ->willReturn($aclUsersForm);
2820
2821
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2822
            ->method('createAclRolesForm')
2823
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2824
            ->willReturn($aclRolesForm);
2825
2826
        $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class)
2827
            ->disableOriginalConstructor()
2828
            ->getMock();
2829
2830
        $aclSecurityHandler
2831
            ->method('getObjectPermissions')
2832
            ->willReturn([]);
2833
2834
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2835
            ->method('getSecurityHandler')
2836
            ->willReturn($aclSecurityHandler);
2837
2838
        $this->assertInstanceOf(Response::class, $this->controller->aclAction(null));
2839
2840
        $this->assertSame($this->admin, $this->parameters['admin']);
2841
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2842
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2843
2844
        $this->assertSame('acl', $this->parameters['action']);
2845
        $this->assertSame([], $this->parameters['permissions']);
2846
        $this->assertSame($object, $this->parameters['object']);
2847
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']);
2848
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']);
2849
        $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']);
2850
        $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']);
2851
2852
        $this->assertSame([], $this->session->getFlashBag()->all());
2853
        $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template);
2854
    }
2855
2856
    public function testAclActionInvalidUpdate(): void
2857
    {
2858
        $this->request->query->set('id', 123);
2859
        $this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, []);
2860
2861
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2862
            ->method('isAclEnabled')
2863
            ->willReturn(true);
2864
2865
        $object = new \stdClass();
2866
2867
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2868
            ->method('getObject')
2869
            ->willReturn($object);
2870
2871
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2872
            ->method('checkAccess')
2873
            ->willReturn(true);
2874
2875
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2876
            ->method('getSecurityInformation')
2877
            ->willReturn([]);
2878
2879
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2880
            ->method('getMaskBuilderClass')
2881
            ->willReturn(AdminPermissionMap::class);
2882
2883
        $aclUsersForm = $this->getMockBuilder(Form::class)
2884
            ->disableOriginalConstructor()
2885
            ->getMock();
2886
2887
        $aclUsersForm->expects($this->once())
2888
            ->method('isValid')
2889
            ->willReturn(false);
2890
2891
        $aclUsersForm->expects($this->once())
2892
            ->method('createView')
2893
            ->willReturn($this->createMock(FormView::class));
2894
2895
        $aclRolesForm = $this->getMockBuilder(Form::class)
2896
            ->disableOriginalConstructor()
2897
            ->getMock();
2898
2899
        $aclRolesForm->expects($this->once())
2900
            ->method('createView')
2901
            ->willReturn($this->createMock(FormView::class));
2902
2903
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2904
            ->method('createAclUsersForm')
2905
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2906
            ->willReturn($aclUsersForm);
2907
2908
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2909
            ->method('createAclRolesForm')
2910
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2911
            ->willReturn($aclRolesForm);
2912
2913
        $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class)
2914
            ->disableOriginalConstructor()
2915
            ->getMock();
2916
2917
        $aclSecurityHandler
2918
            ->method('getObjectPermissions')
2919
            ->willReturn([]);
2920
2921
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2922
            ->method('getSecurityHandler')
2923
            ->willReturn($aclSecurityHandler);
2924
2925
        $this->request->setMethod(Request::METHOD_POST);
2926
2927
        $this->assertInstanceOf(Response::class, $this->controller->aclAction(null));
2928
2929
        $this->assertSame($this->admin, $this->parameters['admin']);
2930
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2931
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2932
2933
        $this->assertSame('acl', $this->parameters['action']);
2934
        $this->assertSame([], $this->parameters['permissions']);
2935
        $this->assertSame($object, $this->parameters['object']);
2936
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']);
2937
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']);
2938
        $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']);
2939
        $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']);
2940
2941
        $this->assertSame([], $this->session->getFlashBag()->all());
2942
        $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template);
2943
    }
2944
2945
    public function testAclActionSuccessfulUpdate(): void
2946
    {
2947
        $this->request->query->set('id', 123);
2948
        $this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, []);
2949
2950
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2951
            ->method('isAclEnabled')
2952
            ->willReturn(true);
2953
2954
        $object = new \stdClass();
2955
2956
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2957
            ->method('getObject')
2958
            ->willReturn($object);
2959
2960
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2961
            ->method('checkAccess')
2962
            ->willReturn(true);
2963
2964
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
2965
            ->method('getSecurityInformation')
2966
            ->willReturn([]);
2967
2968
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2969
            ->method('getMaskBuilderClass')
2970
            ->willReturn(AdminPermissionMap::class);
2971
2972
        $aclUsersForm = $this->getMockBuilder(Form::class)
2973
            ->disableOriginalConstructor()
2974
            ->getMock();
2975
2976
        $aclUsersForm
2977
            ->method('createView')
2978
            ->willReturn($this->createMock(FormView::class));
2979
2980
        $aclRolesForm = $this->getMockBuilder(Form::class)
2981
            ->disableOriginalConstructor()
2982
            ->getMock();
2983
2984
        $aclRolesForm
2985
            ->method('createView')
2986
            ->willReturn($this->createMock(FormView::class));
2987
2988
        $aclRolesForm->expects($this->once())
2989
            ->method('isValid')
2990
            ->willReturn(true);
2991
2992
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2993
            ->method('createAclUsersForm')
2994
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2995
            ->willReturn($aclUsersForm);
2996
2997
        $this->adminObjectAclManipulator->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
2998
            ->method('createAclRolesForm')
2999
            ->with($this->isInstanceOf(AdminObjectAclData::class))
3000
            ->willReturn($aclRolesForm);
3001
3002
        $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class)
3003
            ->disableOriginalConstructor()
3004
            ->getMock();
3005
3006
        $aclSecurityHandler
3007
            ->method('getObjectPermissions')
3008
            ->willReturn([]);
3009
3010
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3011
            ->method('getSecurityHandler')
3012
            ->willReturn($aclSecurityHandler);
3013
3014
        $this->expectTranslate('flash_acl_edit_success', [], 'SonataAdminBundle');
3015
3016
        $this->request->setMethod(Request::METHOD_POST);
3017
3018
        $response = $this->controller->aclAction(null);
3019
3020
        $this->assertInstanceOf(RedirectResponse::class, $response);
3021
3022
        $this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3023
        $this->assertSame('stdClass_acl', $response->getTargetUrl());
3024
    }
3025
3026
    public function testHistoryViewRevisionActionAccessDenied(): void
3027
    {
3028
        $this->expectException(AccessDeniedException::class);
3029
3030
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3031
            ->method('getObject')
3032
            ->willReturn(new \stdClass());
3033
3034
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3035
            ->method('checkAccess')
3036
            ->with($this->equalTo('historyViewRevision'))
3037
            ->will($this->throwException(new AccessDeniedException()));
3038
3039
        $this->controller->historyViewRevisionAction(null, null);
3040
    }
3041
3042
    public function testHistoryViewRevisionActionNotFoundException(): void
3043
    {
3044
        $this->expectException(NotFoundHttpException::class);
3045
        $this->expectExceptionMessage('unable to find the object with id: 123');
3046
3047
        $this->request->query->set('id', 123);
3048
3049
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3050
            ->method('getObject')
3051
            ->willReturn(false);
3052
3053
        $this->controller->historyViewRevisionAction(null, null);
3054
    }
3055
3056
    public function testHistoryViewRevisionActionNoReader(): void
3057
    {
3058
        $this->expectException(NotFoundHttpException::class);
3059
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
3060
3061
        $this->request->query->set('id', 123);
3062
3063
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3064
            ->method('checkAccess')
3065
            ->with($this->equalTo('historyViewRevision'))
3066
            ->willReturn(true);
3067
3068
        $object = new \stdClass();
3069
3070
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3071
            ->method('getObject')
3072
            ->willReturn($object);
3073
3074
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3075
            ->method('getClass')
3076
            ->willReturn('Foo');
3077
3078
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3079
            ->method('hasReader')
3080
            ->with($this->equalTo('Foo'))
3081
            ->willReturn(false);
3082
3083
        $this->controller->historyViewRevisionAction(null, null);
3084
    }
3085
3086
    public function testHistoryViewRevisionActionNotFoundRevision(): void
3087
    {
3088
        $this->expectException(NotFoundHttpException::class);
3089
        $this->expectExceptionMessage(
3090
            'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'
3091
        );
3092
3093
        $this->request->query->set('id', 123);
3094
3095
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3096
            ->method('checkAccess')
3097
            ->with($this->equalTo('historyViewRevision'))
3098
            ->willReturn(true);
3099
3100
        $object = new \stdClass();
3101
3102
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3103
            ->method('getObject')
3104
            ->willReturn($object);
3105
3106
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3107
            ->method('getClass')
3108
            ->willReturn('Foo');
3109
3110
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3111
            ->method('hasReader')
3112
            ->with($this->equalTo('Foo'))
3113
            ->willReturn(true);
3114
3115
        $reader = $this->createMock(AuditReaderInterface::class);
3116
3117
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3118
            ->method('getReader')
3119
            ->with($this->equalTo('Foo'))
3120
            ->willReturn($reader);
3121
3122
        $reader->expects($this->once())
3123
            ->method('find')
3124
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3125
            ->willReturn(null);
3126
3127
        $this->controller->historyViewRevisionAction(123, 456);
3128
    }
3129
3130
    public function testHistoryViewRevisionAction(): void
3131
    {
3132
        $this->request->query->set('id', 123);
3133
3134
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3135
            ->method('checkAccess')
3136
            ->with($this->equalTo('historyViewRevision'))
3137
            ->willReturn(true);
3138
3139
        $object = new \stdClass();
3140
3141
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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('getObject')
3143
            ->willReturn($object);
3144
3145
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3146
            ->method('getClass')
3147
            ->willReturn('Foo');
3148
3149
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3150
            ->method('hasReader')
3151
            ->with($this->equalTo('Foo'))
3152
            ->willReturn(true);
3153
3154
        $reader = $this->createMock(AuditReaderInterface::class);
3155
3156
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3157
            ->method('getReader')
3158
            ->with($this->equalTo('Foo'))
3159
            ->willReturn($reader);
3160
3161
        $objectRevision = new \stdClass();
3162
        $objectRevision->revision = 456;
3163
3164
        $reader->expects($this->once())
3165
            ->method('find')
3166
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3167
            ->willReturn($objectRevision);
3168
3169
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3170
            ->method('setSubject')
3171
            ->with($this->equalTo($objectRevision))
3172
            ->willReturn(null);
3173
3174
        $fieldDescriptionCollection = new FieldDescriptionCollection();
3175
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3176
            ->method('getShow')
3177
            ->willReturn($fieldDescriptionCollection);
3178
3179
        $this->assertInstanceOf(Response::class, $this->controller->historyViewRevisionAction(123, 456));
3180
3181
        $this->assertSame($this->admin, $this->parameters['admin']);
3182
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3183
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3184
3185
        $this->assertSame('show', $this->parameters['action']);
3186
        $this->assertSame($objectRevision, $this->parameters['object']);
3187
        $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
3188
3189
        $this->assertSame([], $this->session->getFlashBag()->all());
3190
        $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template);
3191
    }
3192
3193
    public function testHistoryCompareRevisionsActionAccessDenied(): void
3194
    {
3195
        $this->expectException(AccessDeniedException::class);
3196
3197
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3198
            ->method('checkAccess')
3199
            ->with($this->equalTo('historyCompareRevisions'))
3200
            ->will($this->throwException(new AccessDeniedException()));
3201
3202
        $this->controller->historyCompareRevisionsAction(null, null, null);
3203
    }
3204
3205
    public function testHistoryCompareRevisionsActionNotFoundException(): void
3206
    {
3207
        $this->expectException(NotFoundHttpException::class);
3208
        $this->expectExceptionMessage('unable to find the object with id: 123');
3209
3210
        $this->request->query->set('id', 123);
3211
3212
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3213
            ->method('checkAccess')
3214
            ->with($this->equalTo('historyCompareRevisions'))
3215
            ->willReturn(true);
3216
3217
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3218
            ->method('getObject')
3219
            ->willReturn(false);
3220
3221
        $this->controller->historyCompareRevisionsAction(null, null, null);
3222
    }
3223
3224
    public function testHistoryCompareRevisionsActionNoReader(): void
3225
    {
3226
        $this->expectException(NotFoundHttpException::class);
3227
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
3228
3229
        $this->request->query->set('id', 123);
3230
3231
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3232
            ->method('checkAccess')
3233
            ->with($this->equalTo('historyCompareRevisions'))
3234
            ->willReturn(true);
3235
3236
        $object = new \stdClass();
3237
3238
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3239
            ->method('getObject')
3240
            ->willReturn($object);
3241
3242
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3243
            ->method('getClass')
3244
            ->willReturn('Foo');
3245
3246
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3247
            ->method('hasReader')
3248
            ->with($this->equalTo('Foo'))
3249
            ->willReturn(false);
3250
3251
        $this->controller->historyCompareRevisionsAction(null, null, null);
3252
    }
3253
3254
    public function testHistoryCompareRevisionsActionNotFoundBaseRevision(): void
3255
    {
3256
        $this->expectException(NotFoundHttpException::class);
3257
        $this->expectExceptionMessage(
3258
            'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'
3259
        );
3260
3261
        $this->request->query->set('id', 123);
3262
3263
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3264
            ->method('checkAccess')
3265
            ->with($this->equalTo('historyCompareRevisions'))
3266
            ->willReturn(true);
3267
3268
        $object = new \stdClass();
3269
3270
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3271
            ->method('getObject')
3272
            ->willReturn($object);
3273
3274
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3275
            ->method('getClass')
3276
            ->willReturn('Foo');
3277
3278
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3279
            ->method('hasReader')
3280
            ->with($this->equalTo('Foo'))
3281
            ->willReturn(true);
3282
3283
        $reader = $this->createMock(AuditReaderInterface::class);
3284
3285
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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('getReader')
3287
            ->with($this->equalTo('Foo'))
3288
            ->willReturn($reader);
3289
3290
        // once because it will not be found and therefore the second call won't be executed
3291
        $reader->expects($this->once())
3292
            ->method('find')
3293
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3294
            ->willReturn(null);
3295
3296
        $this->controller->historyCompareRevisionsAction(123, 456, 789);
3297
    }
3298
3299
    public function testHistoryCompareRevisionsActionNotFoundCompareRevision(): void
3300
    {
3301
        $this->expectException(NotFoundHttpException::class);
3302
        $this->expectExceptionMessage(
3303
            'unable to find the targeted object `123` from the revision `789` with classname : `Foo`'
3304
        );
3305
3306
        $this->request->query->set('id', 123);
3307
3308
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3309
            ->method('checkAccess')
3310
            ->with($this->equalTo('historyCompareRevisions'))
3311
            ->willReturn(true);
3312
3313
        $object = new \stdClass();
3314
3315
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3316
            ->method('getObject')
3317
            ->willReturn($object);
3318
3319
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3320
            ->method('getClass')
3321
            ->willReturn('Foo');
3322
3323
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3324
            ->method('hasReader')
3325
            ->with($this->equalTo('Foo'))
3326
            ->willReturn(true);
3327
3328
        $reader = $this->createMock(AuditReaderInterface::class);
3329
3330
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3331
            ->method('getReader')
3332
            ->with($this->equalTo('Foo'))
3333
            ->willReturn($reader);
3334
3335
        $objectRevision = new \stdClass();
3336
        $objectRevision->revision = 456;
3337
3338
        // first call should return, so the second call will throw an exception
3339
        $reader->expects($this->at(0))
3340
            ->method('find')
3341
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3342
            ->willReturn($objectRevision);
3343
3344
        $reader->expects($this->at(1))
3345
            ->method('find')
3346
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
3347
            ->willReturn(null);
3348
3349
        $this->controller->historyCompareRevisionsAction(123, 456, 789);
3350
    }
3351
3352
    public function testHistoryCompareRevisionsActionAction(): void
3353
    {
3354
        $this->request->query->set('id', 123);
3355
3356
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3357
            ->method('checkAccess')
3358
            ->with($this->equalTo('historyCompareRevisions'))
3359
            ->willReturn(true);
3360
3361
        $object = new \stdClass();
3362
3363
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3364
            ->method('getObject')
3365
            ->willReturn($object);
3366
3367
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3368
            ->method('getClass')
3369
            ->willReturn('Foo');
3370
3371
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3372
            ->method('hasReader')
3373
            ->with($this->equalTo('Foo'))
3374
            ->willReturn(true);
3375
3376
        $reader = $this->createMock(AuditReaderInterface::class);
3377
3378
        $this->auditManager->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Model\AuditManager>.

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...
3379
            ->method('getReader')
3380
            ->with($this->equalTo('Foo'))
3381
            ->willReturn($reader);
3382
3383
        $objectRevision = new \stdClass();
3384
        $objectRevision->revision = 456;
3385
3386
        $compareObjectRevision = new \stdClass();
3387
        $compareObjectRevision->revision = 789;
3388
3389
        $reader->expects($this->at(0))
3390
            ->method('find')
3391
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3392
            ->willReturn($objectRevision);
3393
3394
        $reader->expects($this->at(1))
3395
            ->method('find')
3396
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
3397
            ->willReturn($compareObjectRevision);
3398
3399
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3400
            ->method('setSubject')
3401
            ->with($this->equalTo($objectRevision))
3402
            ->willReturn(null);
3403
3404
        $fieldDescriptionCollection = new FieldDescriptionCollection();
3405
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3406
            ->method('getShow')
3407
            ->willReturn($fieldDescriptionCollection);
3408
3409
        $this->assertInstanceOf(Response::class, $this->controller->historyCompareRevisionsAction(123, 456, 789));
3410
3411
        $this->assertSame($this->admin, $this->parameters['admin']);
3412
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3413
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3414
3415
        $this->assertSame('show', $this->parameters['action']);
3416
        $this->assertSame($objectRevision, $this->parameters['object']);
3417
        $this->assertSame($compareObjectRevision, $this->parameters['object_compare']);
3418
        $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
3419
3420
        $this->assertSame([], $this->session->getFlashBag()->all());
3421
        $this->assertSame('@SonataAdmin/CRUD/show_compare.html.twig', $this->template);
3422
    }
3423
3424
    public function testBatchActionWrongMethod(): void
3425
    {
3426
        $this->expectException(NotFoundHttpException::class);
3427
        $this->expectExceptionMessage('Invalid request method given "GET", POST expected');
3428
3429
        $this->controller->batchAction();
3430
    }
3431
3432
    public function testBatchActionActionNotDefined(): void
3433
    {
3434
        $this->expectException(\RuntimeException::class);
3435
        $this->expectExceptionMessage('The `foo` batch action is not defined');
3436
3437
        $batchActions = [];
3438
3439
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3440
            ->method('getBatchActions')
3441
            ->willReturn($batchActions);
3442
3443
        $this->request->setMethod(Request::METHOD_POST);
3444
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3445
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3446
3447
        $this->controller->batchAction();
3448
    }
3449
3450
    public function testBatchActionActionInvalidCsrfToken(): void
3451
    {
3452
        $this->request->setMethod(Request::METHOD_POST);
3453
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3454
        $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
3455
3456
        try {
3457
            $this->controller->batchAction();
3458
        } catch (HttpException $e) {
3459
            $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
3460
            $this->assertSame(400, $e->getStatusCode());
3461
        }
3462
    }
3463
3464
    /**
3465
     * NEXT_MAJOR: Remove this legacy group.
3466
     *
3467
     * @group legacy
3468
     */
3469
    public function testBatchActionMethodNotExist(): void
3470
    {
3471
        $this->expectException(\RuntimeException::class);
3472
        $this->expectExceptionMessage(
3473
            'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable'
3474
        );
3475
3476
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3477
3478
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3479
            ->method('getBatchActions')
3480
            ->willReturn($batchActions);
3481
3482
        $datagrid = $this->createMock(DatagridInterface::class);
3483
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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('getDatagrid')
3485
            ->willReturn($datagrid);
3486
3487
        $this->request->setMethod(Request::METHOD_POST);
3488
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3489
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3490
3491
        $this->controller->batchAction();
3492
    }
3493
3494
    public function testBatchActionWithoutConfirmation(): void
3495
    {
3496
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3497
3498
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3499
            ->method('getBatchActions')
3500
            ->willReturn($batchActions);
3501
3502
        $datagrid = $this->createMock(DatagridInterface::class);
3503
3504
        $query = $this->createMock(ProxyQueryInterface::class);
3505
        $datagrid->expects($this->once())
3506
            ->method('getQuery')
3507
            ->willReturn($query);
3508
3509
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3510
            ->method('getDatagrid')
3511
            ->willReturn($datagrid);
3512
3513
        $modelManager = $this->createMock(ModelManagerInterface::class);
3514
3515
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3516
            ->method('checkAccess')
3517
            ->with($this->equalTo('batchDelete'))
3518
            ->willReturn(true);
3519
3520
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3521
            ->method('getModelManager')
3522
            ->willReturn($modelManager);
3523
3524
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3525
            ->method('getClass')
3526
            ->willReturn('Foo');
3527
3528
        $modelManager->expects($this->once())
3529
            ->method('addIdentifiersToQuery')
3530
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3531
            ->willReturn(true);
3532
3533
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3534
3535
        $this->request->setMethod(Request::METHOD_POST);
3536
        $this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]));
3537
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3538
3539
        $result = $this->controller->batchAction();
3540
3541
        $this->assertInstanceOf(RedirectResponse::class, $result);
3542
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3543
        $this->assertSame('list', $result->getTargetUrl());
3544
    }
3545
3546
    public function testBatchActionWithoutConfirmation2(): void
3547
    {
3548
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3549
3550
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3551
            ->method('getBatchActions')
3552
            ->willReturn($batchActions);
3553
3554
        $datagrid = $this->createMock(DatagridInterface::class);
3555
3556
        $query = $this->createMock(ProxyQueryInterface::class);
3557
        $datagrid->expects($this->once())
3558
            ->method('getQuery')
3559
            ->willReturn($query);
3560
3561
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3562
            ->method('getDatagrid')
3563
            ->willReturn($datagrid);
3564
3565
        $modelManager = $this->createMock(ModelManagerInterface::class);
3566
3567
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3568
            ->method('checkAccess')
3569
            ->with($this->equalTo('batchDelete'))
3570
            ->willReturn(true);
3571
3572
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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('getModelManager')
3574
            ->willReturn($modelManager);
3575
3576
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3577
            ->method('getClass')
3578
            ->willReturn('Foo');
3579
3580
        $modelManager->expects($this->once())
3581
            ->method('addIdentifiersToQuery')
3582
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3583
            ->willReturn(true);
3584
3585
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3586
3587
        $this->request->setMethod(Request::METHOD_POST);
3588
        $this->request->request->set('action', 'delete');
3589
        $this->request->request->set('idx', ['123', '456']);
3590
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3591
3592
        $result = $this->controller->batchAction();
3593
3594
        $this->assertInstanceOf(RedirectResponse::class, $result);
3595
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3596
        $this->assertSame('list', $result->getTargetUrl());
3597
    }
3598
3599
    public function testBatchActionWithConfirmation(): void
3600
    {
3601
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]];
3602
3603
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3604
            ->method('getBatchActions')
3605
            ->willReturn($batchActions);
3606
3607
        $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false];
3608
3609
        $this->request->setMethod(Request::METHOD_POST);
3610
        $this->request->request->set('data', json_encode($data));
3611
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3612
3613
        $datagrid = $this->createMock(DatagridInterface::class);
3614
3615
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3616
            ->method('getDatagrid')
3617
            ->willReturn($datagrid);
3618
3619
        $form = $this->getMockBuilder(Form::class)
3620
            ->disableOriginalConstructor()
3621
            ->getMock();
3622
3623
        $form->expects($this->once())
3624
            ->method('createView')
3625
            ->willReturn($this->createMock(FormView::class));
3626
3627
        $datagrid->expects($this->once())
3628
            ->method('getForm')
3629
            ->willReturn($form);
3630
3631
        $this->assertInstanceOf(Response::class, $this->controller->batchAction());
3632
3633
        $this->assertSame($this->admin, $this->parameters['admin']);
3634
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3635
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3636
3637
        $this->assertSame('list', $this->parameters['action']);
3638
        $this->assertSame($datagrid, $this->parameters['datagrid']);
3639
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
3640
        $this->assertSame($data, $this->parameters['data']);
3641
        $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
3642
        $this->assertSame('Foo Bar', $this->parameters['action_label']);
3643
3644
        $this->assertSame([], $this->session->getFlashBag()->all());
3645
        $this->assertSame('@SonataAdmin/CRUD/batch_confirmation.html.twig', $this->template);
3646
    }
3647
3648
    public function testBatchActionNonRelevantAction(): void
3649
    {
3650
        $controller = new BatchAdminController();
3651
        $controller->setContainer($this->container);
3652
3653
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3654
3655
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3656
            ->method('getBatchActions')
3657
            ->willReturn($batchActions);
3658
3659
        $datagrid = $this->createMock(DatagridInterface::class);
3660
3661
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3662
            ->method('getDatagrid')
3663
            ->willReturn($datagrid);
3664
3665
        $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');
3666
3667
        $this->request->setMethod(Request::METHOD_POST);
3668
        $this->request->request->set('action', 'foo');
3669
        $this->request->request->set('idx', ['789']);
3670
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3671
3672
        $result = $controller->batchAction();
3673
3674
        $this->assertInstanceOf(RedirectResponse::class, $result);
3675
        $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
3676
        $this->assertSame('list', $result->getTargetUrl());
3677
    }
3678
3679
    public function testBatchActionWithCustomConfirmationTemplate(): void
3680
    {
3681
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true, 'template' => 'custom_template.html.twig']];
3682
3683
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3684
            ->method('getBatchActions')
3685
            ->willReturn($batchActions);
3686
3687
        $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false];
3688
3689
        $this->request->setMethod(Request::METHOD_POST);
3690
        $this->request->request->set('data', json_encode($data));
3691
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3692
3693
        $datagrid = $this->createMock(DatagridInterface::class);
3694
3695
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3696
            ->method('getDatagrid')
3697
            ->willReturn($datagrid);
3698
3699
        $form = $this->createMock(Form::class);
3700
3701
        $form->expects($this->once())
3702
            ->method('createView')
3703
            ->willReturn($this->createMock(FormView::class));
3704
3705
        $datagrid->expects($this->once())
3706
            ->method('getForm')
3707
            ->willReturn($form);
3708
3709
        $this->controller->batchAction();
3710
3711
        $this->assertSame('custom_template.html.twig', $this->template);
3712
    }
3713
3714
    public function testBatchActionNonRelevantAction2(): void
3715
    {
3716
        $controller = new BatchAdminController();
3717
        $controller->setContainer($this->container);
3718
3719
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3720
3721
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3722
            ->method('getBatchActions')
3723
            ->willReturn($batchActions);
3724
3725
        $datagrid = $this->createMock(DatagridInterface::class);
3726
3727
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3728
            ->method('getDatagrid')
3729
            ->willReturn($datagrid);
3730
3731
        $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');
3732
3733
        $this->request->setMethod(Request::METHOD_POST);
3734
        $this->request->request->set('action', 'foo');
3735
        $this->request->request->set('idx', ['999']);
3736
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3737
3738
        $result = $controller->batchAction();
3739
3740
        $this->assertInstanceOf(RedirectResponse::class, $result);
3741
        $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
3742
        $this->assertSame('list', $result->getTargetUrl());
3743
    }
3744
3745
    public function testBatchActionNoItems(): void
3746
    {
3747
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true]];
3748
3749
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3750
            ->method('getBatchActions')
3751
            ->willReturn($batchActions);
3752
3753
        $datagrid = $this->createMock(DatagridInterface::class);
3754
3755
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3756
            ->method('getDatagrid')
3757
            ->willReturn($datagrid);
3758
3759
        $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');
3760
3761
        $this->request->setMethod(Request::METHOD_POST);
3762
        $this->request->request->set('action', 'delete');
3763
        $this->request->request->set('idx', []);
3764
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3765
3766
        $result = $this->controller->batchAction();
3767
3768
        $this->assertInstanceOf(RedirectResponse::class, $result);
3769
        $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
3770
        $this->assertSame('list', $result->getTargetUrl());
3771
    }
3772
3773
    public function testBatchActionNoItemsEmptyQuery(): void
3774
    {
3775
        $controller = new BatchAdminController();
3776
        $controller->setContainer($this->container);
3777
3778
        $batchActions = ['bar' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3779
3780
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3781
            ->method('getBatchActions')
3782
            ->willReturn($batchActions);
3783
3784
        $datagrid = $this->createMock(DatagridInterface::class);
3785
3786
        $query = $this->createMock(ProxyQueryInterface::class);
3787
        $datagrid->expects($this->once())
3788
            ->method('getQuery')
3789
            ->willReturn($query);
3790
3791
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3792
            ->method('getDatagrid')
3793
            ->willReturn($datagrid);
3794
3795
        $modelManager = $this->createMock(ModelManagerInterface::class);
3796
3797
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3798
            ->method('getModelManager')
3799
            ->willReturn($modelManager);
3800
3801
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3802
            ->method('getClass')
3803
            ->willReturn('Foo');
3804
3805
        $this->request->setMethod(Request::METHOD_POST);
3806
        $this->request->request->set('action', 'bar');
3807
        $this->request->request->set('idx', []);
3808
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3809
3810
        $this->expectTranslate('flash_batch_no_elements_processed', [], 'SonataAdminBundle');
3811
        $result = $controller->batchAction();
3812
3813
        $this->assertInstanceOf(Response::class, $result);
3814
        $this->assertRegExp('/Redirecting to list/', $result->getContent());
3815
    }
3816
3817
    public function testBatchActionWithRequesData(): void
3818
    {
3819
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3820
3821
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3822
            ->method('getBatchActions')
3823
            ->willReturn($batchActions);
3824
3825
        $datagrid = $this->createMock(DatagridInterface::class);
3826
3827
        $query = $this->createMock(ProxyQueryInterface::class);
3828
        $datagrid->expects($this->once())
3829
            ->method('getQuery')
3830
            ->willReturn($query);
3831
3832
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3833
            ->method('getDatagrid')
3834
            ->willReturn($datagrid);
3835
3836
        $modelManager = $this->createMock(ModelManagerInterface::class);
3837
3838
        $this->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
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...
3839
            ->method('checkAccess')
3840
            ->with($this->equalTo('batchDelete'))
3841
            ->willReturn(true);
3842
3843
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3844
            ->method('getModelManager')
3845
            ->willReturn($modelManager);
3846
3847
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() 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...
3848
            ->method('getClass')
3849
            ->willReturn('Foo');
3850
3851
        $modelManager->expects($this->once())
3852
            ->method('addIdentifiersToQuery')
3853
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3854
            ->willReturn(true);
3855
3856
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3857
3858
        $this->request->setMethod(Request::METHOD_POST);
3859
        $this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]));
3860
        $this->request->request->set('foo', 'bar');
3861
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3862
3863
        $result = $this->controller->batchAction();
3864
3865
        $this->assertInstanceOf(RedirectResponse::class, $result);
3866
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3867
        $this->assertSame('list', $result->getTargetUrl());
3868
        $this->assertSame('bar', $this->request->request->get('foo'));
3869
    }
3870
3871
    public function getCsrfProvider()
3872
    {
3873
        return $this->csrfProvider;
3874
    }
3875
3876
    public function getToStringValues()
3877
    {
3878
        return [
3879
            ['', ''],
3880
            ['Foo', 'Foo'],
3881
            ['&lt;a href=&quot;http://foo&quot;&gt;Bar&lt;/a&gt;', '<a href="http://foo">Bar</a>'],
3882
            ['&lt;&gt;&amp;&quot;&#039;abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/', '<>&"\'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/'],
3883
        ];
3884
    }
3885
3886
    private function assertLoggerLogsModelManagerException($subject, string $method): void
3887
    {
3888
        $exception = new ModelManagerException(
3889
            $message = 'message',
3890
            1234,
3891
            new \Exception($previousExceptionMessage = 'very useful message')
3892
        );
3893
3894
        $subject->expects($this->once())
3895
            ->method($method)
3896
            ->willReturnCallback(static function () use ($exception): void {
3897
                throw $exception;
3898
            });
3899
3900
        $this->logger->expects($this->once())
3901
            ->method('error')
3902
            ->with($message, [
3903
                'exception' => $exception,
3904
                'previous_exception_message' => $previousExceptionMessage,
3905
            ]);
3906
    }
3907
3908
    private function expectTranslate(
3909
        string $id,
3910
        array $parameters = [],
3911
        ?string $domain = null,
3912
        ?string $locale = null
3913
    ): void {
3914
        $this->translator->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...on\TranslatorInterface>.

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...
3915
            ->method('trans')
3916
            ->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale))
3917
            ->willReturn($id);
3918
    }
3919
}
3920