Completed
Push — 3.x ( bfb61a...068249 )
by Vincent
27:02
created

testBatchActionWithConfirmation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
421
            ->method('setUniqid')
422
            ->willReturnCallback(static function (int $uniqid) use (&$uniqueId): void {
423
                $uniqueId = $uniqid;
424
            });
425
426
        $this->request->query->set('uniqid', 123456);
427
        $this->protectedTestedMethods['configure']->invoke($this->controller);
428
429
        $this->assertSame(123456, $uniqueId);
430
    }
431
432
    public function testConfigureChild(): void
433
    {
434
        $uniqueId = '';
435
436
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
437
            ->method('setUniqid')
438
            ->willReturnCallback(static function ($uniqid) use (&$uniqueId): void {
439
                $uniqueId = $uniqid;
440
            });
441
442
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
443
            ->method('isChild')
444
            ->willReturn(true);
445
446
        $adminParent = $this->getMockBuilder(AbstractAdmin::class)
447
            ->disableOriginalConstructor()
448
            ->getMock();
449
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
450
            ->method('getParent')
451
            ->willReturn($adminParent);
452
453
        $this->request->query->set('uniqid', 123456);
454
        $this->protectedTestedMethods['configure']->invoke($this->controller);
455
456
        $this->assertSame(123456, $uniqueId);
457
    }
458
459
    public function testConfigureWithException(): void
460
    {
461
        $this->expectException(\RuntimeException::class);
462
        $this->expectExceptionMessage(
463
            'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`'
464
        );
465
466
        $this->request->attributes->remove('_sonata_admin');
467
        $this->protectedTestedMethods['configure']->invoke($this->controller);
468
    }
469
470
    public function testConfigureWithException2(): void
471
    {
472
        $this->expectException(\InvalidArgumentException::class);
473
        $this->expectExceptionMessage('You have requested a non-existent service "nonexistent.admin".');
474
475
        $this->pool->setAdminServiceIds(['nonexistent.admin']);
476
        $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
477
        $this->protectedTestedMethods['configure']->invoke($this->controller);
478
    }
479
480
    public function testGetBaseTemplate(): void
481
    {
482
        $this->assertSame(
483
            '@SonataAdmin/standard_layout.html.twig',
484
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
485
        );
486
487
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
488
        $this->assertSame(
489
            '@SonataAdmin/ajax_layout.html.twig',
490
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
491
        );
492
493
        $this->request->headers->remove('X-Requested-With');
494
        $this->assertSame(
495
            '@SonataAdmin/standard_layout.html.twig',
496
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
497
        );
498
499
        $this->request->attributes->set('_xml_http_request', true);
500
        $this->assertSame(
501
            '@SonataAdmin/ajax_layout.html.twig',
502
            $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
503
        );
504
    }
505
506
    public function testRender(): void
507
    {
508
        $this->parameters = [];
509
        $this->assertInstanceOf(
510
            Response::class,
511
            $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], null)
512
        );
513
        $this->assertSame($this->admin, $this->parameters['admin']);
514
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
515
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
516
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
517
    }
518
519
    public function testRenderWithResponse(): void
520
    {
521
        $this->parameters = [];
522
        $response = new Response();
523
        $response->headers->set('X-foo', 'bar');
524
        $responseResult = $this->controller->renderWithExtraParams('@FooAdmin/foo.html.twig', [], $response);
525
526
        $this->assertSame($response, $responseResult);
527
        $this->assertSame('bar', $responseResult->headers->get('X-foo'));
528
        $this->assertSame($this->admin, $this->parameters['admin']);
529
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
530
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
531
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
532
    }
533
534
    public function testRenderCustomParams(): void
535
    {
536
        $this->parameters = [];
537
        $this->assertInstanceOf(
538
            Response::class,
539
            $this->controller->renderWithExtraParams(
540
                '@FooAdmin/foo.html.twig',
541
                ['foo' => 'bar'],
542
                null
543
            )
544
        );
545
        $this->assertSame($this->admin, $this->parameters['admin']);
546
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
547
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
548
        $this->assertSame('bar', $this->parameters['foo']);
549
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
550
    }
551
552
    public function testRenderAjax(): void
553
    {
554
        $this->parameters = [];
555
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
556
        $this->assertInstanceOf(
557
            Response::class,
558
            $this->controller->renderWithExtraParams(
559
                '@FooAdmin/foo.html.twig',
560
                ['foo' => 'bar'],
561
                null
562
            )
563
        );
564
        $this->assertSame($this->admin, $this->parameters['admin']);
565
        $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
566
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
567
        $this->assertSame('bar', $this->parameters['foo']);
568
        $this->assertSame('@FooAdmin/foo.html.twig', $this->template);
569
    }
570
571
    public function testListActionAccessDenied(): void
572
    {
573
        $this->expectException(AccessDeniedException::class);
574
575
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
576
            ->method('checkAccess')
577
            ->with($this->equalTo('list'))
578
            ->will($this->throwException(new AccessDeniedException()));
579
580
        $this->controller->listAction();
581
    }
582
583
    public function testPreList(): void
584
    {
585
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
586
            ->method('hasRoute')
587
            ->with($this->equalTo('list'))
588
            ->willReturn(true);
589
590
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
591
            ->method('checkAccess')
592
            ->with($this->equalTo('list'))
593
            ->willReturn(true);
594
595
        $controller = new PreCRUDController();
596
        $controller->setContainer($this->container);
597
598
        $response = $controller->listAction();
599
        $this->assertInstanceOf(Response::class, $response);
600
        $this->assertSame('preList called', $response->getContent());
601
    }
602
603
    public function testListAction(): void
604
    {
605
        $datagrid = $this->createMock(DatagridInterface::class);
606
607
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
608
            ->method('hasRoute')
609
            ->with($this->equalTo('list'))
610
            ->willReturn(true);
611
612
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
613
            ->method('checkAccess')
614
            ->with($this->equalTo('list'))
615
            ->willReturn(true);
616
617
        $form = $this->getMockBuilder(Form::class)
618
            ->disableOriginalConstructor()
619
            ->getMock();
620
621
        $form->expects($this->once())
622
            ->method('createView')
623
            ->willReturn($this->createMock(FormView::class));
624
625
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
626
            ->method('getDatagrid')
627
            ->willReturn($datagrid);
628
629
        $datagrid->expects($this->once())
630
            ->method('getForm')
631
            ->willReturn($form);
632
633
        $this->parameters = [];
634
        $this->assertInstanceOf(Response::class, $this->controller->listAction());
635
636
        $this->assertSame($this->admin, $this->parameters['admin']);
637
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
638
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
639
640
        $this->assertSame('list', $this->parameters['action']);
641
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
642
        $this->assertInstanceOf(DatagridInterface::class, $this->parameters['datagrid']);
643
        $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
644
        $this->assertSame([], $this->session->getFlashBag()->all());
645
        $this->assertSame('@SonataAdmin/CRUD/list.html.twig', $this->template);
646
    }
647
648
    public function testBatchActionDeleteAccessDenied(): void
649
    {
650
        $this->expectException(AccessDeniedException::class);
651
652
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
653
            ->method('checkAccess')
654
            ->with($this->equalTo('batchDelete'))
655
            ->will($this->throwException(new AccessDeniedException()));
656
657
        $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
658
    }
659
660
    public function testBatchActionDelete(): void
661
    {
662
        $modelManager = $this->createMock(ModelManagerInterface::class);
663
664
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
665
            ->method('checkAccess')
666
            ->with($this->equalTo('batchDelete'))
667
            ->willReturn(true);
668
669
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
670
            ->method('getModelManager')
671
            ->willReturn($modelManager);
672
673
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
674
            ->method('getFilterParameters')
675
            ->willReturn(['foo' => 'bar']);
676
677
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
678
679
        $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
680
681
        $this->assertInstanceOf(RedirectResponse::class, $result);
682
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
683
        $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
684
    }
685
686
    public function testBatchActionDeleteWithModelManagerException(): void
687
    {
688
        $modelManager = $this->createMock(ModelManagerInterface::class);
689
        $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete');
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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->admin->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getFilterParameters')
697
            ->willReturn(['foo' => 'bar']);
698
699
        $this->expectTranslate('flash_batch_delete_error', [], 'SonataAdminBundle');
700
701
        $result = $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
702
703
        $this->assertInstanceOf(RedirectResponse::class, $result);
704
        $this->assertSame(['flash_batch_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error'));
705
        $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
706
    }
707
708
    public function testBatchActionDeleteWithModelManagerExceptionInDebugMode(): void
709
    {
710
        $modelManager = $this->createMock(ModelManagerInterface::class);
711
        $this->expectException(ModelManagerException::class);
712
713
        $modelManager->expects($this->once())
714
            ->method('batchDelete')
715
            ->willReturnCallback(static function (): void {
716
                throw new ModelManagerException();
717
            });
718
719
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
720
            ->method('getModelManager')
721
            ->willReturn($modelManager);
722
723
        $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...
724
            ->method('isDebug')
725
            ->willReturn(true);
726
727
        $this->controller->batchActionDelete($this->createMock(ProxyQueryInterface::class));
728
    }
729
730
    public function testShowActionNotFoundException(): void
731
    {
732
        $this->expectException(NotFoundHttpException::class);
733
734
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
735
            ->method('getObject')
736
            ->willReturn(false);
737
738
        $this->controller->showAction(null);
739
    }
740
741
    public function testShowActionAccessDenied(): void
742
    {
743
        $this->expectException(AccessDeniedException::class);
744
745
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
746
            ->method('getObject')
747
            ->willReturn(new \stdClass());
748
749
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
750
            ->method('checkAccess')
751
            ->with($this->equalTo('show'))
752
            ->will($this->throwException(new AccessDeniedException()));
753
754
        $this->controller->showAction(null);
755
    }
756
757
    /**
758
     * @group legacy
759
     * @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
760
     */
761
    public function testShowActionDeprecation(): void
762
    {
763
        $object = new \stdClass();
764
765
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
766
            ->method('getObject')
767
            ->willReturn($object);
768
769
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
770
            ->method('checkAccess')
771
            ->with($this->equalTo('show'))
772
            ->willReturn(true);
773
774
        $show = $this->createMock(FieldDescriptionCollection::class);
775
776
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
777
            ->method('getShow')
778
            ->willReturn($show);
779
780
        $show->expects($this->once())
781
            ->method('getElements')
782
            ->willReturn([]);
783
784
        $show->expects($this->once())
785
            ->method('count')
786
            ->willReturn(0);
787
788
        $this->controller->showAction(null);
789
    }
790
791
    public function testPreShow(): void
792
    {
793
        $object = new \stdClass();
794
        $object->foo = 123456;
795
796
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
797
            ->method('getObject')
798
            ->willReturn($object);
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
802
            ->with($this->equalTo('show'))
803
            ->willReturn(true);
804
805
        $controller = new PreCRUDController();
806
        $controller->setContainer($this->container);
807
808
        $response = $controller->showAction(null);
809
        $this->assertInstanceOf(Response::class, $response);
810
        $this->assertSame('preShow called: 123456', $response->getContent());
811
    }
812
813
    public function testShowAction(): void
814
    {
815
        $object = new \stdClass();
816
817
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
818
            ->method('getObject')
819
            ->willReturn($object);
820
821
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
822
            ->method('checkAccess')
823
            ->with($this->equalTo('show'))
824
            ->willReturn(true);
825
826
        $show = $this->createMock(FieldDescriptionCollection::class);
827
828
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
829
            ->method('getShow')
830
            ->willReturn($show);
831
832
        $show->expects($this->once())
833
            ->method('getElements')
834
            ->willReturn(['field' => 'fielddata']);
835
836
        $this->assertInstanceOf(Response::class, $this->controller->showAction(null));
837
838
        $this->assertSame($this->admin, $this->parameters['admin']);
839
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
840
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
841
842
        $this->assertSame('show', $this->parameters['action']);
843
        $this->assertInstanceOf(FieldDescriptionCollection::class, $this->parameters['elements']);
844
        $this->assertSame($object, $this->parameters['object']);
845
846
        $this->assertSame([], $this->session->getFlashBag()->all());
847
        $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template);
848
    }
849
850
    /**
851
     * @dataProvider getRedirectToTests
852
     */
853
    public function testRedirectTo(
854
        string $expected,
855
        string $route,
856
        array $queryParams,
857
        array $requestParams,
858
        bool $hasActiveSubclass
859
    ): void {
860
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
861
            ->method('hasActiveSubclass')
862
            ->willReturn($hasActiveSubclass);
863
864
        $object = new \stdClass();
865
866
        foreach ($queryParams as $key => $value) {
867
            $this->request->query->set($key, $value);
868
        }
869
870
        foreach ($requestParams as $key => $value) {
871
            $this->request->request->set($key, $value);
872
        }
873
874
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('hasRoute')
876
            ->with($this->equalTo($route))
877
            ->willReturn(true);
878
879
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
880
            ->method('hasAccess')
881
            ->with($this->equalTo($route))
882
            ->willReturn(true);
883
884
        $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
885
        $this->assertInstanceOf(RedirectResponse::class, $response);
886
        $this->assertSame($expected, $response->getTargetUrl());
887
    }
888
889
    public function testRedirectToWithObject(): void
890
    {
891
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
892
            ->method('hasActiveSubclass')
893
            ->willReturn(false);
894
895
        $object = new \stdClass();
896
897
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
898
            ->method('hasRoute')
899
            ->with($this->equalTo('edit'))
900
            ->willReturn(true);
901
902
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
903
            ->method('hasAccess')
904
            ->with($this->equalTo('edit'), $object)
905
            ->willReturn(false);
906
907
        $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
908
        $this->assertInstanceOf(RedirectResponse::class, $response);
909
        $this->assertSame('list', $response->getTargetUrl());
910
    }
911
912
    public function getRedirectToTests()
913
    {
914
        return [
915
            ['stdClass_edit', 'edit', [], [], false],
916
            ['list', 'list', ['btn_update_and_list' => true], [], false],
917
            ['list', 'list', ['btn_create_and_list' => true], [], false],
918
            ['create', 'create', ['btn_create_and_create' => true], [], false],
919
            ['create?subclass=foo', 'create', ['btn_create_and_create' => true, 'subclass' => 'foo'], [], true],
920
            ['stdClass_edit?_tab=first_tab', 'edit', ['btn_update_and_edit' => true], ['_tab' => 'first_tab'], false],
921
        ];
922
    }
923
924
    public function testDeleteActionNotFoundException(): void
925
    {
926
        $this->expectException(NotFoundHttpException::class);
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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(false);
931
932
        $this->controller->deleteAction(1);
933
    }
934
935
    public function testDeleteActionAccessDenied(): void
936
    {
937
        $this->expectException(AccessDeniedException::class);
938
939
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
940
            ->method('getObject')
941
            ->willReturn(new \stdClass());
942
943
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
944
            ->method('checkAccess')
945
            ->with($this->equalTo('delete'))
946
            ->will($this->throwException(new AccessDeniedException()));
947
948
        $this->controller->deleteAction(1);
949
    }
950
951
    public function testPreDelete(): void
952
    {
953
        $object = new \stdClass();
954
        $object->foo = 123456;
955
956
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
957
            ->method('getObject')
958
            ->willReturn($object);
959
960
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
961
            ->method('checkAccess')
962
            ->with($this->equalTo('delete'))
963
            ->willReturn(true);
964
965
        $controller = new PreCRUDController();
966
        $controller->setContainer($this->container);
967
968
        $response = $controller->deleteAction(null);
969
        $this->assertInstanceOf(Response::class, $response);
970
        $this->assertSame('preDelete called: 123456', $response->getContent());
971
    }
972
973
    public function testDeleteAction(): void
974
    {
975
        $object = new \stdClass();
976
977
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
978
            ->method('getObject')
979
            ->willReturn($object);
980
981
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
982
            ->method('checkAccess')
983
            ->with($this->equalTo('delete'))
984
            ->willReturn(true);
985
986
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1));
987
988
        $this->assertSame($this->admin, $this->parameters['admin']);
989
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
990
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
991
992
        $this->assertSame('delete', $this->parameters['action']);
993
        $this->assertSame($object, $this->parameters['object']);
994
        $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
995
996
        $this->assertSame([], $this->session->getFlashBag()->all());
997
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
998
    }
999
1000
    /**
1001
     * @group legacy
1002
     * @expectedDeprecation Accessing a child that isn't connected to a given parent is deprecated since sonata-project/admin-bundle 3.34 and won't be allowed in 4.0.
1003
     */
1004
    public function testDeleteActionChildDeprecation(): void
1005
    {
1006
        $object = new \stdClass();
1007
        $object->parent = 'test';
1008
1009
        $object2 = new \stdClass();
1010
1011
        $admin = $this->createMock(PostAdmin::class);
1012
1013
        $admin->expects($this->once())
1014
            ->method('getObject')
1015
            ->willReturn($object2);
1016
1017
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1018
            ->method('getObject')
1019
            ->willReturn($object);
1020
1021
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1022
            ->method('getParent')
1023
            ->willReturn($admin);
1024
1025
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1026
            ->method('getParentAssociationMapping')
1027
            ->willReturn('parent');
1028
1029
        $this->controller->deleteAction(1);
1030
    }
1031
1032
    public function testDeleteActionNoParentMappings(): void
1033
    {
1034
        $object = new \stdClass();
1035
1036
        $admin = $this->createMock(PostAdmin::class);
1037
1038
        $admin->expects($this->never())
1039
            ->method('getObject');
1040
1041
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1042
            ->method('getObject')
1043
            ->willReturn($object);
1044
1045
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1046
            ->method('getParent')
1047
            ->willReturn($admin);
1048
1049
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1050
            ->method('getParentAssociationMapping')
1051
            ->willReturn(false);
1052
1053
        $this->controller->deleteAction(1);
1054
    }
1055
1056
    public function testDeleteActionNoCsrfToken(): void
1057
    {
1058
        $this->container->set('security.csrf.token_manager', null);
1059
1060
        $object = new \stdClass();
1061
1062
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1063
            ->method('getObject')
1064
            ->willReturn($object);
1065
1066
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1067
            ->method('checkAccess')
1068
            ->with($this->equalTo('delete'))
1069
            ->willReturn(true);
1070
1071
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1));
1072
1073
        $this->assertSame($this->admin, $this->parameters['admin']);
1074
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1075
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1076
1077
        $this->assertSame('delete', $this->parameters['action']);
1078
        $this->assertSame($object, $this->parameters['object']);
1079
        $this->assertFalse($this->parameters['csrf_token']);
1080
1081
        $this->assertSame([], $this->session->getFlashBag()->all());
1082
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
1083
    }
1084
1085
    public function testDeleteActionAjaxSuccess1(): void
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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->request->setMethod(Request::METHOD_DELETE);
1099
1100
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1101
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1102
1103
        $response = $this->controller->deleteAction(1);
1104
1105
        $this->assertInstanceOf(Response::class, $response);
1106
        $this->assertSame(json_encode(['result' => 'ok']), $response->getContent());
1107
        $this->assertSame([], $this->session->getFlashBag()->all());
1108
    }
1109
1110
    public function testDeleteActionAjaxSuccess2(): void
1111
    {
1112
        $object = new \stdClass();
1113
1114
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1115
            ->method('getObject')
1116
            ->willReturn($object);
1117
1118
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1119
            ->method('checkAccess')
1120
            ->with($this->equalTo('delete'))
1121
            ->willReturn(true);
1122
1123
        $this->request->setMethod(Request::METHOD_POST);
1124
        $this->request->request->set('_method', Request::METHOD_DELETE);
1125
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1126
1127
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1128
1129
        $response = $this->controller->deleteAction(1);
1130
1131
        $this->assertInstanceOf(Response::class, $response);
1132
        $this->assertSame(json_encode(['result' => 'ok']), $response->getContent());
1133
        $this->assertSame([], $this->session->getFlashBag()->all());
1134
    }
1135
1136
    public function testDeleteActionAjaxError(): void
1137
    {
1138
        $object = new \stdClass();
1139
1140
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1141
            ->method('getObject')
1142
            ->willReturn($object);
1143
1144
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1145
            ->method('checkAccess')
1146
            ->with($this->equalTo('delete'))
1147
            ->willReturn(true);
1148
1149
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1150
            ->method('getClass')
1151
            ->willReturn('stdClass');
1152
1153
        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
1154
1155
        $this->request->setMethod(Request::METHOD_DELETE);
1156
1157
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1158
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1159
1160
        $response = $this->controller->deleteAction(1);
1161
1162
        $this->assertInstanceOf(Response::class, $response);
1163
        $this->assertSame(json_encode(['result' => 'error']), $response->getContent());
1164
        $this->assertSame([], $this->session->getFlashBag()->all());
1165
    }
1166
1167
    public function testDeleteActionWithModelManagerExceptionInDebugMode(): void
1168
    {
1169
        $this->expectException(ModelManagerException::class);
1170
1171
        $object = new \stdClass();
1172
1173
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1174
            ->method('getObject')
1175
            ->willReturn($object);
1176
1177
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1178
            ->method('checkAccess')
1179
            ->with($this->equalTo('delete'))
1180
            ->willReturn(true);
1181
1182
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1183
            ->method('delete')
1184
            ->willReturnCallback(static function (): void {
1185
                throw new ModelManagerException();
1186
            });
1187
1188
        $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...
1189
            ->method('isDebug')
1190
            ->willReturn(true);
1191
1192
        $this->request->setMethod(Request::METHOD_DELETE);
1193
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1194
1195
        $this->controller->deleteAction(1);
1196
    }
1197
1198
    /**
1199
     * @dataProvider getToStringValues
1200
     */
1201
    public function testDeleteActionSuccess1(string $expectedToStringValue, string $toStringValue): void
1202
    {
1203
        $object = new \stdClass();
1204
1205
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1206
            ->method('getObject')
1207
            ->willReturn($object);
1208
1209
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1210
            ->method('toString')
1211
            ->with($this->equalTo($object))
1212
            ->willReturn($toStringValue);
1213
1214
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1215
1216
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1217
            ->method('checkAccess')
1218
            ->with($this->equalTo('delete'))
1219
            ->willReturn(true);
1220
1221
        $this->request->setMethod(Request::METHOD_DELETE);
1222
1223
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1224
1225
        $response = $this->controller->deleteAction(1);
1226
1227
        $this->assertInstanceOf(RedirectResponse::class, $response);
1228
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1229
        $this->assertSame('list', $response->getTargetUrl());
1230
    }
1231
1232
    /**
1233
     * @dataProvider getToStringValues
1234
     */
1235
    public function testDeleteActionSuccess2(string $expectedToStringValue, string $toStringValue): void
1236
    {
1237
        $object = new \stdClass();
1238
1239
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1240
            ->method('getObject')
1241
            ->willReturn($object);
1242
1243
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1244
            ->method('checkAccess')
1245
            ->with($this->equalTo('delete'))
1246
            ->willReturn(true);
1247
1248
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1249
            ->method('toString')
1250
            ->with($this->equalTo($object))
1251
            ->willReturn($toStringValue);
1252
1253
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1254
1255
        $this->request->setMethod(Request::METHOD_POST);
1256
        $this->request->request->set('_method', Request::METHOD_DELETE);
1257
1258
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1259
1260
        $response = $this->controller->deleteAction(1);
1261
1262
        $this->assertInstanceOf(RedirectResponse::class, $response);
1263
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1264
        $this->assertSame('list', $response->getTargetUrl());
1265
    }
1266
1267
    /**
1268
     * @dataProvider getToStringValues
1269
     */
1270
    public function testDeleteActionSuccessNoCsrfTokenProvider(string $expectedToStringValue, string $toStringValue): void
1271
    {
1272
        $this->container->set('security.csrf.token_manager', null);
1273
1274
        $object = new \stdClass();
1275
1276
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1277
            ->method('getObject')
1278
            ->willReturn($object);
1279
1280
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1281
            ->method('checkAccess')
1282
            ->with($this->equalTo('delete'))
1283
            ->willReturn(true);
1284
1285
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1286
            ->method('toString')
1287
            ->with($this->equalTo($object))
1288
            ->willReturn($toStringValue);
1289
1290
        $this->expectTranslate('flash_delete_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1291
1292
        $this->request->setMethod(Request::METHOD_POST);
1293
        $this->request->request->set('_method', Request::METHOD_DELETE);
1294
1295
        $response = $this->controller->deleteAction(1);
1296
1297
        $this->assertInstanceOf(RedirectResponse::class, $response);
1298
        $this->assertSame(['flash_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1299
        $this->assertSame('list', $response->getTargetUrl());
1300
    }
1301
1302
    public function testDeleteActionWrongRequestMethod(): void
1303
    {
1304
        $object = new \stdClass();
1305
1306
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1307
            ->method('getObject')
1308
            ->willReturn($object);
1309
1310
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1311
            ->method('checkAccess')
1312
            ->with($this->equalTo('delete'))
1313
            ->willReturn(true);
1314
1315
        //without POST request parameter "_method" should not be used as real REST method
1316
        $this->request->query->set('_method', Request::METHOD_DELETE);
1317
1318
        $this->assertInstanceOf(Response::class, $this->controller->deleteAction(1));
1319
1320
        $this->assertSame($this->admin, $this->parameters['admin']);
1321
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1322
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1323
1324
        $this->assertSame('delete', $this->parameters['action']);
1325
        $this->assertSame($object, $this->parameters['object']);
1326
        $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
1327
1328
        $this->assertSame([], $this->session->getFlashBag()->all());
1329
        $this->assertSame('@SonataAdmin/CRUD/delete.html.twig', $this->template);
1330
    }
1331
1332
    /**
1333
     * @dataProvider getToStringValues
1334
     */
1335
    public function testDeleteActionError(string $expectedToStringValue, string $toStringValue): void
1336
    {
1337
        $object = new \stdClass();
1338
1339
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1340
            ->method('getObject')
1341
            ->willReturn($object);
1342
1343
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1344
            ->method('checkAccess')
1345
            ->with($this->equalTo('delete'))
1346
            ->willReturn(true);
1347
1348
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1349
            ->method('toString')
1350
            ->with($this->equalTo($object))
1351
            ->willReturn($toStringValue);
1352
1353
        $this->expectTranslate('flash_delete_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1354
1355
        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
1356
1357
        $this->request->setMethod(Request::METHOD_DELETE);
1358
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
1359
1360
        $response = $this->controller->deleteAction(1);
1361
1362
        $this->assertInstanceOf(RedirectResponse::class, $response);
1363
        $this->assertSame(['flash_delete_error'], $this->session->getFlashBag()->get('sonata_flash_error'));
1364
        $this->assertSame('list', $response->getTargetUrl());
1365
    }
1366
1367
    public function testDeleteActionInvalidCsrfToken(): void
1368
    {
1369
        $object = new \stdClass();
1370
1371
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1372
            ->method('getObject')
1373
            ->willReturn($object);
1374
1375
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1376
            ->method('checkAccess')
1377
            ->with($this->equalTo('delete'))
1378
            ->willReturn(true);
1379
1380
        $this->request->setMethod(Request::METHOD_POST);
1381
        $this->request->request->set('_method', Request::METHOD_DELETE);
1382
        $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
1383
1384
        try {
1385
            $this->controller->deleteAction(1);
1386
        } catch (HttpException $e) {
1387
            $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
1388
            $this->assertSame(400, $e->getStatusCode());
1389
        }
1390
    }
1391
1392
    public function testEditActionNotFoundException(): void
1393
    {
1394
        $this->expectException(NotFoundHttpException::class);
1395
1396
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1397
            ->method('getObject')
1398
            ->willReturn(false);
1399
1400
        $this->controller->editAction(null);
1401
    }
1402
1403
    public function testEditActionRuntimeException(): void
1404
    {
1405
        $this->expectException(\RuntimeException::class);
1406
1407
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1408
            ->method('getObject')
1409
            ->willReturn(new \stdClass());
1410
1411
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1412
            ->method('checkAccess')
1413
            ->with($this->equalTo('edit'))
1414
            ->willReturn(true);
1415
1416
        $form = $this->createMock(Form::class);
1417
1418
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1419
            ->method('getForm')
1420
            ->willReturn($form);
1421
1422
        $form->expects($this->once())
1423
            ->method('all')
1424
            ->willReturn([]);
1425
1426
        $this->controller->editAction(null);
1427
    }
1428
1429
    public function testEditActionAccessDenied(): void
1430
    {
1431
        $this->expectException(AccessDeniedException::class);
1432
1433
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1434
            ->method('getObject')
1435
            ->willReturn(new \stdClass());
1436
1437
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1438
            ->method('checkAccess')
1439
            ->with($this->equalTo('edit'))
1440
            ->will($this->throwException(new AccessDeniedException()));
1441
1442
        $this->controller->editAction(null);
1443
    }
1444
1445
    public function testPreEdit(): void
1446
    {
1447
        $object = new \stdClass();
1448
        $object->foo = 123456;
1449
1450
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1451
            ->method('getObject')
1452
            ->willReturn($object);
1453
1454
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1455
            ->method('checkAccess')
1456
            ->with($this->equalTo('edit'))
1457
            ->willReturn(true);
1458
1459
        $controller = new PreCRUDController();
1460
        $controller->setContainer($this->container);
1461
1462
        $response = $controller->editAction(null);
1463
        $this->assertInstanceOf(Response::class, $response);
1464
        $this->assertSame('preEdit called: 123456', $response->getContent());
1465
    }
1466
1467
    public function testEditAction(): void
1468
    {
1469
        $object = new \stdClass();
1470
1471
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1472
            ->method('getObject')
1473
            ->willReturn($object);
1474
1475
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1476
            ->method('checkAccess')
1477
            ->with($this->equalTo('edit'))
1478
            ->willReturn(true);
1479
1480
        $form = $this->createMock(Form::class);
1481
1482
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1483
            ->method('getForm')
1484
            ->willReturn($form);
1485
1486
        $formView = $this->createMock(FormView::class);
1487
1488
        $form
1489
            ->method('createView')
1490
            ->willReturn($formView);
1491
1492
        $form->expects($this->once())
1493
            ->method('all')
1494
            ->willReturn(['field' => 'fielddata']);
1495
1496
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1497
1498
        $this->assertSame($this->admin, $this->parameters['admin']);
1499
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1500
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1501
1502
        $this->assertSame('edit', $this->parameters['action']);
1503
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1504
        $this->assertSame($object, $this->parameters['object']);
1505
        $this->assertSame([], $this->session->getFlashBag()->all());
1506
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1507
    }
1508
1509
    /**
1510
     * @dataProvider getToStringValues
1511
     */
1512
    public function testEditActionSuccess(string $expectedToStringValue, string $toStringValue): void
1513
    {
1514
        $object = new \stdClass();
1515
1516
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1517
            ->method('getObject')
1518
            ->willReturn($object);
1519
1520
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1521
            ->method('update')
1522
            ->willReturnArgument(0);
1523
1524
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1525
            ->method('checkAccess')
1526
            ->with($this->equalTo('edit'));
1527
1528
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1529
            ->method('hasRoute')
1530
            ->with($this->equalTo('edit'))
1531
            ->willReturn(true);
1532
1533
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1534
            ->method('hasAccess')
1535
            ->with($this->equalTo('edit'))
1536
            ->willReturn(true);
1537
1538
        $form = $this->createMock(Form::class);
1539
1540
        $form->expects($this->once())
1541
            ->method('getData')
1542
            ->willReturn($object);
1543
1544
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1545
            ->method('getForm')
1546
            ->willReturn($form);
1547
1548
        $form->expects($this->once())
1549
            ->method('isSubmitted')
1550
            ->willReturn(true);
1551
1552
        $form->expects($this->once())
1553
            ->method('isValid')
1554
            ->willReturn(true);
1555
1556
        $form->expects($this->once())
1557
            ->method('all')
1558
            ->willReturn(['field' => 'fielddata']);
1559
1560
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1561
            ->method('toString')
1562
            ->with($this->equalTo($object))
1563
            ->willReturn($toStringValue);
1564
1565
        $this->expectTranslate('flash_edit_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1566
1567
        $this->request->setMethod(Request::METHOD_POST);
1568
1569
        $response = $this->controller->editAction(null);
1570
1571
        $this->assertInstanceOf(RedirectResponse::class, $response);
1572
        $this->assertSame(['flash_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
1573
        $this->assertSame('stdClass_edit', $response->getTargetUrl());
1574
    }
1575
1576
    /**
1577
     * @dataProvider getToStringValues
1578
     */
1579
    public function testEditActionError(string $expectedToStringValue, string $toStringValue): void
1580
    {
1581
        $object = new \stdClass();
1582
1583
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1584
            ->method('getObject')
1585
            ->willReturn($object);
1586
1587
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1588
            ->method('checkAccess')
1589
            ->with($this->equalTo('edit'))
1590
            ->willReturn(true);
1591
1592
        $form = $this->createMock(Form::class);
1593
1594
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getForm')
1596
            ->willReturn($form);
1597
1598
        $form->expects($this->once())
1599
            ->method('isSubmitted')
1600
            ->willReturn(true);
1601
1602
        $form->expects($this->once())
1603
            ->method('isValid')
1604
            ->willReturn(false);
1605
1606
        $form->expects($this->once())
1607
            ->method('all')
1608
            ->willReturn(['field' => 'fielddata']);
1609
1610
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1611
            ->method('toString')
1612
            ->with($this->equalTo($object))
1613
            ->willReturn($toStringValue);
1614
1615
        $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1616
1617
        $this->request->setMethod(Request::METHOD_POST);
1618
1619
        $formView = $this->createMock(FormView::class);
1620
1621
        $form
1622
            ->method('createView')
1623
            ->willReturn($formView);
1624
1625
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1626
1627
        $this->assertSame($this->admin, $this->parameters['admin']);
1628
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1629
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1630
1631
        $this->assertSame('edit', $this->parameters['action']);
1632
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1633
        $this->assertSame($object, $this->parameters['object']);
1634
1635
        $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all());
1636
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1637
    }
1638
1639
    public function testEditActionAjaxSuccess(): void
1640
    {
1641
        $object = new \stdClass();
1642
1643
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1644
            ->method('getObject')
1645
            ->willReturn($object);
1646
1647
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1648
            ->method('update')
1649
            ->willReturnArgument(0);
1650
1651
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1652
            ->method('checkAccess')
1653
            ->with($this->equalTo('edit'))
1654
            ->willReturn(true);
1655
1656
        $form = $this->createMock(Form::class);
1657
1658
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1659
            ->method('getForm')
1660
            ->willReturn($form);
1661
1662
        $form->expects($this->once())
1663
            ->method('isSubmitted')
1664
            ->willReturn(true);
1665
1666
        $form->expects($this->once())
1667
            ->method('isValid')
1668
            ->willReturn(true);
1669
1670
        $form->expects($this->once())
1671
            ->method('getData')
1672
            ->willReturn($object);
1673
1674
        $form->expects($this->once())
1675
            ->method('all')
1676
            ->willReturn(['field' => 'fielddata']);
1677
1678
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1679
            ->method('getNormalizedIdentifier')
1680
            ->with($this->equalTo($object))
1681
            ->willReturn('foo_normalized');
1682
1683
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1684
            ->method('toString')
1685
            ->willReturn('foo');
1686
1687
        $this->request->setMethod(Request::METHOD_POST);
1688
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1689
1690
        $response = $this->controller->editAction(null);
1691
1692
        $this->assertInstanceOf(Response::class, $response);
1693
        $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent());
1694
        $this->assertSame([], $this->session->getFlashBag()->all());
1695
    }
1696
1697
    public function testEditActionAjaxError(): void
1698
    {
1699
        $object = new \stdClass();
1700
1701
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1702
            ->method('getObject')
1703
            ->willReturn($object);
1704
1705
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1706
            ->method('checkAccess')
1707
            ->with($this->equalTo('edit'))
1708
            ->willReturn(true);
1709
1710
        $form = $this->createMock(Form::class);
1711
1712
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1713
            ->method('getForm')
1714
            ->willReturn($form);
1715
1716
        $form->expects($this->once())
1717
            ->method('isSubmitted')
1718
            ->willReturn(true);
1719
1720
        $form->expects($this->once())
1721
            ->method('isValid')
1722
            ->willReturn(false);
1723
1724
        $form->expects($this->once())
1725
            ->method('all')
1726
            ->willReturn(['field' => 'fielddata']);
1727
1728
        $formError = $this->createMock(FormError::class);
1729
        $formError->expects($this->atLeastOnce())
1730
            ->method('getMessage')
1731
            ->willReturn('Form error message');
1732
1733
        $form->expects($this->once())
1734
            ->method('getErrors')
1735
            ->with(true)
1736
            ->willReturn([$formError]);
1737
1738
        $this->request->setMethod(Request::METHOD_POST);
1739
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1740
        $this->request->headers->set('Accept', 'application/json');
1741
1742
        $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->editAction(null));
1743
        $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent());
1744
    }
1745
1746
    /**
1747
     * @legacy
1748
     * @expectedDeprecation In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`
1749
     */
1750
    public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
1751
    {
1752
        $object = new \stdClass();
1753
1754
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1755
            ->method('getObject')
1756
            ->willReturn($object);
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
1760
            ->with($this->equalTo('edit'))
1761
            ->willReturn(true);
1762
1763
        $form = $this->createMock(Form::class);
1764
1765
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1766
            ->method('getForm')
1767
            ->willReturn($form);
1768
1769
        $form->expects($this->once())
1770
            ->method('isSubmitted')
1771
            ->willReturn(true);
1772
1773
        $form->expects($this->once())
1774
            ->method('isValid')
1775
            ->willReturn(false);
1776
1777
        $form->expects($this->once())
1778
            ->method('all')
1779
            ->willReturn(['field' => 'fielddata']);
1780
1781
        $this->request->setMethod(Request::METHOD_POST);
1782
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
1783
1784
        $formView = $this->createMock(FormView::class);
1785
        $form
1786
            ->method('createView')
1787
            ->willReturn($formView);
1788
1789
        $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...
1790
            ->method('trans')
1791
            ->willReturn('flash message');
1792
1793
        $this->assertInstanceOf(Response::class, $response = $this->controller->editAction(null));
1794
        $this->assertSame($this->admin, $this->parameters['admin']);
1795
        $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
1796
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1797
        $this->assertSame('edit', $this->parameters['action']);
1798
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1799
        $this->assertSame($object, $this->parameters['object']);
1800
        $this->assertSame([
1801
            'sonata_flash_error' => [0 => 'flash message'],
1802
        ], $this->session->getFlashBag()->all());
1803
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1804
    }
1805
1806
    /**
1807
     * @dataProvider getToStringValues
1808
     */
1809
    public function testEditActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void
1810
    {
1811
        $object = new \stdClass();
1812
1813
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1814
            ->method('getObject')
1815
            ->willReturn($object);
1816
1817
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1818
            ->method('checkAccess')
1819
            ->with($this->equalTo('edit'))
1820
            ->willReturn(true);
1821
1822
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1823
            ->method('getClass')
1824
            ->willReturn('stdClass');
1825
1826
        $form = $this->createMock(Form::class);
1827
1828
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1829
            ->method('getForm')
1830
            ->willReturn($form);
1831
1832
        $form->expects($this->once())
1833
            ->method('isValid')
1834
            ->willReturn(true);
1835
1836
        $form->expects($this->once())
1837
            ->method('getData')
1838
            ->willReturn($object);
1839
1840
        $form->expects($this->once())
1841
            ->method('all')
1842
            ->willReturn(['field' => 'fielddata']);
1843
1844
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1845
            ->method('toString')
1846
            ->with($this->equalTo($object))
1847
            ->willReturn($toStringValue);
1848
1849
        $this->expectTranslate('flash_edit_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
1850
1851
        $form->expects($this->once())
1852
            ->method('isSubmitted')
1853
            ->willReturn(true);
1854
        $this->request->setMethod(Request::METHOD_POST);
1855
1856
        $formView = $this->createMock(FormView::class);
1857
1858
        $form
1859
            ->method('createView')
1860
            ->willReturn($formView);
1861
1862
        $this->assertLoggerLogsModelManagerException($this->admin, 'update');
1863
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1864
1865
        $this->assertSame($this->admin, $this->parameters['admin']);
1866
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1867
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1868
1869
        $this->assertSame('edit', $this->parameters['action']);
1870
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1871
        $this->assertSame($object, $this->parameters['object']);
1872
1873
        $this->assertSame(['sonata_flash_error' => ['flash_edit_error']], $this->session->getFlashBag()->all());
1874
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
1875
    }
1876
1877
    public function testEditActionWithPreview(): void
1878
    {
1879
        $object = new \stdClass();
1880
1881
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getObject')
1883
            ->willReturn($object);
1884
1885
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1886
            ->method('checkAccess')
1887
            ->with($this->equalTo('edit'))
1888
            ->willReturn(true);
1889
1890
        $form = $this->createMock(Form::class);
1891
1892
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1893
            ->method('getForm')
1894
            ->willReturn($form);
1895
1896
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1897
            ->method('supportsPreviewMode')
1898
            ->willReturn(true);
1899
1900
        $formView = $this->createMock(FormView::class);
1901
1902
        $form
1903
            ->method('createView')
1904
            ->willReturn($formView);
1905
1906
        $form->expects($this->once())
1907
            ->method('isSubmitted')
1908
            ->willReturn(true);
1909
1910
        $form->expects($this->once())
1911
            ->method('isValid')
1912
            ->willReturn(true);
1913
1914
        $form->expects($this->once())
1915
            ->method('all')
1916
            ->willReturn(['field' => 'fielddata']);
1917
1918
        $this->request->setMethod(Request::METHOD_POST);
1919
        $this->request->request->set('btn_preview', 'Preview');
1920
1921
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1922
1923
        $this->assertSame($this->admin, $this->parameters['admin']);
1924
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
1925
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
1926
1927
        $this->assertSame('edit', $this->parameters['action']);
1928
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
1929
        $this->assertSame($object, $this->parameters['object']);
1930
1931
        $this->assertSame([], $this->session->getFlashBag()->all());
1932
        $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template);
1933
    }
1934
1935
    public function testEditActionWithLockException(): void
1936
    {
1937
        $object = new \stdClass();
1938
        $class = \get_class($object);
1939
1940
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1941
            ->method('getObject')
1942
            ->willReturn($object);
1943
1944
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1945
            ->method('checkAccess')
1946
            ->with($this->equalTo('edit'))
1947
            ->willReturn(true);
1948
1949
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1950
            ->method('getClass')
1951
            ->willReturn($class);
1952
1953
        $form = $this->createMock(Form::class);
1954
1955
        $form
1956
            ->method('isValid')
1957
            ->willReturn(true);
1958
1959
        $form->expects($this->once())
1960
            ->method('getData')
1961
            ->willReturn($object);
1962
1963
        $form->expects($this->once())
1964
            ->method('all')
1965
            ->willReturn(['field' => 'fielddata']);
1966
1967
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1968
            ->method('getForm')
1969
            ->willReturn($form);
1970
1971
        $form
1972
            ->method('isSubmitted')
1973
            ->willReturn(true);
1974
        $this->request->setMethod(Request::METHOD_POST);
1975
1976
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1977
            ->method('update')
1978
            ->will($this->throwException(new LockException()));
1979
1980
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1981
            ->method('toString')
1982
            ->with($this->equalTo($object))
1983
            ->willReturn($class);
1984
1985
        $formView = $this->createMock(FormView::class);
1986
1987
        $form
1988
            ->method('createView')
1989
            ->willReturn($formView);
1990
1991
        $this->expectTranslate('flash_lock_error', [
1992
            '%name%' => $class,
1993
            '%link_start%' => '<a href="stdClass_edit">',
1994
            '%link_end%' => '</a>',
1995
        ], 'SonataAdminBundle');
1996
1997
        $this->assertInstanceOf(Response::class, $this->controller->editAction(null));
1998
    }
1999
2000
    public function testCreateActionAccessDenied(): void
2001
    {
2002
        $this->expectException(AccessDeniedException::class);
2003
2004
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2005
            ->method('checkAccess')
2006
            ->with($this->equalTo('create'))
2007
            ->will($this->throwException(new AccessDeniedException()));
2008
2009
        $this->controller->createAction();
2010
    }
2011
2012
    public function testCreateActionRuntimeException(): void
2013
    {
2014
        $this->expectException(\RuntimeException::class);
2015
2016
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2017
            ->method('checkAccess')
2018
            ->with($this->equalTo('create'))
2019
            ->willReturn(true);
2020
2021
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2022
            ->method('getClass')
2023
            ->willReturn('stdClass');
2024
2025
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2026
            ->method('getNewInstance')
2027
            ->willReturn(new \stdClass());
2028
2029
        $form = $this->createMock(Form::class);
2030
2031
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2032
            ->method('getForm')
2033
            ->willReturn($form);
2034
2035
        $form->expects($this->once())
2036
            ->method('all')
2037
            ->willReturn([]);
2038
2039
        $this->controller->createAction();
2040
    }
2041
2042
    public function testPreCreate(): void
2043
    {
2044
        $object = new \stdClass();
2045
        $object->foo = 123456;
2046
2047
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2048
            ->method('checkAccess')
2049
            ->with($this->equalTo('create'))
2050
            ->willReturn(true);
2051
2052
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2053
            ->method('getClass')
2054
            ->willReturn('stdClass');
2055
2056
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2057
            ->method('getNewInstance')
2058
            ->willReturn($object);
2059
2060
        $controller = new PreCRUDController();
2061
        $controller->setContainer($this->container);
2062
2063
        $response = $controller->createAction();
2064
        $this->assertInstanceOf(Response::class, $response);
2065
        $this->assertSame('preCreate called: 123456', $response->getContent());
2066
    }
2067
2068
    public function testCreateAction(): void
2069
    {
2070
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2071
            ->method('checkAccess')
2072
            ->with($this->equalTo('create'))
2073
            ->willReturn(true);
2074
2075
        $object = new \stdClass();
2076
2077
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2078
            ->method('getClass')
2079
            ->willReturn('stdClass');
2080
2081
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2082
            ->method('getNewInstance')
2083
            ->willReturn($object);
2084
2085
        $form = $this->createMock(Form::class);
2086
2087
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2088
            ->method('getForm')
2089
            ->willReturn($form);
2090
2091
        $form->expects($this->once())
2092
            ->method('all')
2093
            ->willReturn(['field' => 'fielddata']);
2094
2095
        $formView = $this->createMock(FormView::class);
2096
2097
        $form
2098
            ->method('createView')
2099
            ->willReturn($formView);
2100
2101
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2102
2103
        $this->assertSame($this->admin, $this->parameters['admin']);
2104
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2105
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2106
2107
        $this->assertSame('create', $this->parameters['action']);
2108
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2109
        $this->assertSame($object, $this->parameters['object']);
2110
2111
        $this->assertSame([], $this->session->getFlashBag()->all());
2112
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2113
    }
2114
2115
    /**
2116
     * @dataProvider getToStringValues
2117
     */
2118
    public function testCreateActionSuccess(string $expectedToStringValue, string $toStringValue): void
2119
    {
2120
        $object = new \stdClass();
2121
2122
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2123
            ->method('checkAccess')
2124
            ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): bool {
2125
                if ('edit' === $name) {
2126
                    return true;
2127
                }
2128
2129
                if ('create' !== $name) {
2130
                    return false;
2131
                }
2132
2133
                if (null === $objectIn) {
2134
                    return true;
2135
                }
2136
2137
                return $objectIn === $object;
2138
            });
2139
2140
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2141
            ->method('hasRoute')
2142
            ->with($this->equalTo('edit'))
2143
            ->willReturn(true);
2144
2145
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2146
            ->method('hasAccess')
2147
            ->with($this->equalTo('edit'))
2148
            ->willReturn(true);
2149
2150
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2151
            ->method('getNewInstance')
2152
            ->willReturn($object);
2153
2154
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2155
            ->method('create')
2156
            ->willReturnArgument(0);
2157
2158
        $form = $this->createMock(Form::class);
2159
2160
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2161
            ->method('getClass')
2162
            ->willReturn('stdClass');
2163
2164
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2165
            ->method('getForm')
2166
            ->willReturn($form);
2167
2168
        $form->expects($this->once())
2169
            ->method('all')
2170
            ->willReturn(['field' => 'fielddata']);
2171
2172
        $form->expects($this->once())
2173
            ->method('isSubmitted')
2174
            ->willReturn(true);
2175
2176
        $form->expects($this->once())
2177
            ->method('isValid')
2178
            ->willReturn(true);
2179
2180
        $form->expects($this->once())
2181
            ->method('getData')
2182
            ->willReturn($object);
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('toString')
2186
            ->with($this->equalTo($object))
2187
            ->willReturn($toStringValue);
2188
2189
        $this->expectTranslate('flash_create_success', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2190
2191
        $this->request->setMethod(Request::METHOD_POST);
2192
2193
        $response = $this->controller->createAction();
2194
2195
        $this->assertInstanceOf(RedirectResponse::class, $response);
2196
        $this->assertSame(['flash_create_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
2197
        $this->assertSame('stdClass_edit', $response->getTargetUrl());
2198
    }
2199
2200
    public function testCreateActionAccessDenied2(): void
2201
    {
2202
        $this->expectException(AccessDeniedException::class);
2203
2204
        $object = new \stdClass();
2205
2206
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
2208
            ->willReturnCallback(static function (string $name, $object = null): bool {
2209
                if ('create' !== $name) {
2210
                    throw new AccessDeniedException();
2211
                }
2212
                if (null === $object) {
2213
                    return true;
2214
                }
2215
2216
                throw new AccessDeniedException();
2217
            });
2218
2219
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2220
            ->method('getNewInstance')
2221
            ->willReturn($object);
2222
2223
        $form = $this->createMock(Form::class);
2224
2225
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2226
            ->method('getClass')
2227
            ->willReturn('stdClass');
2228
2229
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2230
            ->method('getForm')
2231
            ->willReturn($form);
2232
2233
        $form->expects($this->once())
2234
            ->method('all')
2235
            ->willReturn(['field' => 'fielddata']);
2236
2237
        $form->expects($this->once())
2238
            ->method('isSubmitted')
2239
            ->willReturn(true);
2240
2241
        $form->expects($this->once())
2242
            ->method('getData')
2243
            ->willReturn($object);
2244
2245
        $form->expects($this->once())
2246
            ->method('isValid')
2247
            ->willReturn(true);
2248
2249
        $this->request->setMethod(Request::METHOD_POST);
2250
2251
        $this->controller->createAction();
2252
    }
2253
2254
    /**
2255
     * @dataProvider getToStringValues
2256
     */
2257
    public function testCreateActionError(string $expectedToStringValue, string $toStringValue): void
2258
    {
2259
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2260
            ->method('checkAccess')
2261
            ->with($this->equalTo('create'))
2262
            ->willReturn(true);
2263
2264
        $object = new \stdClass();
2265
2266
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2267
            ->method('getClass')
2268
            ->willReturn('stdClass');
2269
2270
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2271
            ->method('getNewInstance')
2272
            ->willReturn($object);
2273
2274
        $form = $this->createMock(Form::class);
2275
2276
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2277
            ->method('getForm')
2278
            ->willReturn($form);
2279
2280
        $form->expects($this->once())
2281
            ->method('all')
2282
            ->willReturn(['field' => 'fielddata']);
2283
2284
        $form->expects($this->once())
2285
            ->method('isSubmitted')
2286
            ->willReturn(true);
2287
2288
        $form->expects($this->once())
2289
            ->method('isValid')
2290
            ->willReturn(false);
2291
2292
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2293
            ->method('toString')
2294
            ->with($this->equalTo($object))
2295
            ->willReturn($toStringValue);
2296
2297
        $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2298
2299
        $this->request->setMethod(Request::METHOD_POST);
2300
2301
        $formView = $this->createMock(FormView::class);
2302
2303
        $form
2304
            ->method('createView')
2305
            ->willReturn($formView);
2306
2307
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2308
2309
        $this->assertSame($this->admin, $this->parameters['admin']);
2310
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2311
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2312
2313
        $this->assertSame('create', $this->parameters['action']);
2314
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2315
        $this->assertSame($object, $this->parameters['object']);
2316
2317
        $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all());
2318
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2319
    }
2320
2321
    /**
2322
     * @dataProvider getToStringValues
2323
     */
2324
    public function testCreateActionWithModelManagerException(string $expectedToStringValue, string $toStringValue): void
2325
    {
2326
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
2328
            ->with($this->equalTo('create'))
2329
            ->willReturn(true);
2330
2331
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2332
            ->method('getClass')
2333
            ->willReturn('stdClass');
2334
2335
        $object = new \stdClass();
2336
2337
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2338
            ->method('getNewInstance')
2339
            ->willReturn($object);
2340
2341
        $form = $this->createMock(Form::class);
2342
2343
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2344
            ->method('getForm')
2345
            ->willReturn($form);
2346
2347
        $form->expects($this->once())
2348
            ->method('all')
2349
            ->willReturn(['field' => 'fielddata']);
2350
2351
        $form->expects($this->once())
2352
            ->method('isValid')
2353
            ->willReturn(true);
2354
2355
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2356
            ->method('toString')
2357
            ->with($this->equalTo($object))
2358
            ->willReturn($toStringValue);
2359
2360
        $this->expectTranslate('flash_create_error', ['%name%' => $expectedToStringValue], 'SonataAdminBundle');
2361
2362
        $form->expects($this->once())
2363
            ->method('isSubmitted')
2364
            ->willReturn(true);
2365
2366
        $form->expects($this->once())
2367
            ->method('getData')
2368
            ->willReturn($object);
2369
2370
        $this->request->setMethod(Request::METHOD_POST);
2371
2372
        $formView = $this->createMock(FormView::class);
2373
2374
        $form
2375
            ->method('createView')
2376
            ->willReturn($formView);
2377
2378
        $this->assertLoggerLogsModelManagerException($this->admin, 'create');
2379
2380
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2381
2382
        $this->assertSame($this->admin, $this->parameters['admin']);
2383
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2384
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2385
2386
        $this->assertSame('create', $this->parameters['action']);
2387
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2388
        $this->assertSame($object, $this->parameters['object']);
2389
2390
        $this->assertSame(['sonata_flash_error' => ['flash_create_error']], $this->session->getFlashBag()->all());
2391
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2392
    }
2393
2394
    public function testCreateActionAjaxSuccess(): void
2395
    {
2396
        $object = new \stdClass();
2397
2398
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
2400
            ->willReturnCallback(static function (string $name, $objectIn = null) use ($object): bool {
2401
                if ('create' !== $name) {
2402
                    return false;
2403
                }
2404
2405
                if (null === $objectIn) {
2406
                    return true;
2407
                }
2408
2409
                return $objectIn === $object;
2410
            });
2411
2412
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2413
            ->method('getNewInstance')
2414
            ->willReturn($object);
2415
2416
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2417
            ->method('create')
2418
            ->willReturnArgument(0);
2419
2420
        $form = $this->createMock(Form::class);
2421
2422
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2423
            ->method('getForm')
2424
            ->willReturn($form);
2425
2426
        $form->expects($this->once())
2427
            ->method('all')
2428
            ->willReturn(['field' => 'fielddata']);
2429
2430
        $form->expects($this->once())
2431
            ->method('isSubmitted')
2432
            ->willReturn(true);
2433
2434
        $form->expects($this->once())
2435
            ->method('isValid')
2436
            ->willReturn(true);
2437
2438
        $form->expects($this->once())
2439
            ->method('getData')
2440
            ->willReturn($object);
2441
2442
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2443
            ->method('getClass')
2444
            ->willReturn('stdClass');
2445
2446
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2447
            ->method('getNormalizedIdentifier')
2448
            ->with($this->equalTo($object))
2449
            ->willReturn('foo_normalized');
2450
2451
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('toString')
2453
            ->willReturn('foo');
2454
2455
        $this->request->setMethod(Request::METHOD_POST);
2456
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2457
2458
        $response = $this->controller->createAction();
2459
2460
        $this->assertInstanceOf(Response::class, $response);
2461
        $this->assertSame(json_encode(['result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo']), $response->getContent());
2462
        $this->assertSame([], $this->session->getFlashBag()->all());
2463
    }
2464
2465
    public function testCreateActionAjaxError(): void
2466
    {
2467
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2468
            ->method('checkAccess')
2469
            ->with($this->equalTo('create'))
2470
            ->willReturn(true);
2471
2472
        $object = new \stdClass();
2473
2474
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2475
            ->method('getNewInstance')
2476
            ->willReturn($object);
2477
2478
        $form = $this->createMock(Form::class);
2479
2480
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2481
            ->method('getClass')
2482
            ->willReturn('stdClass');
2483
2484
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2485
            ->method('getForm')
2486
            ->willReturn($form);
2487
2488
        $form->expects($this->once())
2489
            ->method('all')
2490
            ->willReturn(['field' => 'fielddata']);
2491
2492
        $form->expects($this->once())
2493
            ->method('isSubmitted')
2494
            ->willReturn(true);
2495
2496
        $form->expects($this->once())
2497
            ->method('isValid')
2498
            ->willReturn(false);
2499
2500
        $formError = $this->createMock(FormError::class);
2501
        $formError->expects($this->atLeastOnce())
2502
            ->method('getMessage')
2503
            ->willReturn('Form error message');
2504
2505
        $form->expects($this->once())
2506
            ->method('getErrors')
2507
            ->with(true)
2508
            ->willReturn([$formError]);
2509
2510
        $this->request->setMethod(Request::METHOD_POST);
2511
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2512
        $this->request->headers->set('Accept', 'application/json');
2513
2514
        $this->assertInstanceOf(JsonResponse::class, $response = $this->controller->createAction());
2515
        $this->assertJsonStringEqualsJsonString('{"result":"error","errors":["Form error message"]}', $response->getContent());
2516
    }
2517
2518
    /**
2519
     * @legacy
2520
     * @expectedDeprecation In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`
2521
     */
2522
    public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void
2523
    {
2524
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2525
            ->method('checkAccess')
2526
            ->with($this->equalTo('create'))
2527
            ->willReturn(true);
2528
2529
        $object = new \stdClass();
2530
2531
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2532
            ->method('getNewInstance')
2533
            ->willReturn($object);
2534
2535
        $form = $this->createMock(Form::class);
2536
2537
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2538
            ->method('getClass')
2539
            ->willReturn('stdClass');
2540
2541
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2542
            ->method('getForm')
2543
            ->willReturn($form);
2544
2545
        $form->expects($this->once())
2546
            ->method('all')
2547
            ->willReturn(['field' => 'fielddata']);
2548
2549
        $form->expects($this->once())
2550
            ->method('isSubmitted')
2551
            ->willReturn(true);
2552
2553
        $form->expects($this->once())
2554
            ->method('isValid')
2555
            ->willReturn(false);
2556
2557
        $this->request->setMethod(Request::METHOD_POST);
2558
        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
2559
2560
        $formView = $this->createMock(FormView::class);
2561
        $form
2562
            ->method('createView')
2563
            ->willReturn($formView);
2564
2565
        $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...
2566
            ->method('trans')
2567
            ->willReturn('flash message');
2568
2569
        $this->assertInstanceOf(Response::class, $response = $this->controller->createAction());
2570
        $this->assertSame($this->admin, $this->parameters['admin']);
2571
        $this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
2572
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2573
        $this->assertSame('create', $this->parameters['action']);
2574
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2575
        $this->assertSame($object, $this->parameters['object']);
2576
        $this->assertSame([
2577
            'sonata_flash_error' => [0 => 'flash message'],
2578
        ], $this->session->getFlashBag()->all());
2579
        $this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
2580
    }
2581
2582
    public function testCreateActionWithPreview(): void
2583
    {
2584
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
2586
            ->with($this->equalTo('create'))
2587
            ->willReturn(true);
2588
2589
        $object = new \stdClass();
2590
2591
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2592
            ->method('getNewInstance')
2593
            ->willReturn($object);
2594
2595
        $form = $this->createMock(Form::class);
2596
2597
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2598
            ->method('getClass')
2599
            ->willReturn('stdClass');
2600
2601
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2602
            ->method('getForm')
2603
            ->willReturn($form);
2604
2605
        $form->expects($this->once())
2606
            ->method('all')
2607
            ->willReturn(['field' => 'fielddata']);
2608
2609
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2610
            ->method('supportsPreviewMode')
2611
            ->willReturn(true);
2612
2613
        $formView = $this->createMock(FormView::class);
2614
2615
        $form
2616
            ->method('createView')
2617
            ->willReturn($formView);
2618
2619
        $form->expects($this->once())
2620
            ->method('isSubmitted')
2621
            ->willReturn(true);
2622
2623
        $form->expects($this->once())
2624
            ->method('isValid')
2625
            ->willReturn(true);
2626
2627
        $this->request->setMethod(Request::METHOD_POST);
2628
        $this->request->request->set('btn_preview', 'Preview');
2629
2630
        $this->assertInstanceOf(Response::class, $this->controller->createAction());
2631
2632
        $this->assertSame($this->admin, $this->parameters['admin']);
2633
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2634
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2635
2636
        $this->assertSame('create', $this->parameters['action']);
2637
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
2638
        $this->assertSame($object, $this->parameters['object']);
2639
2640
        $this->assertSame([], $this->session->getFlashBag()->all());
2641
        $this->assertSame('@SonataAdmin/CRUD/preview.html.twig', $this->template);
2642
    }
2643
2644
    public function testExportActionAccessDenied(): void
2645
    {
2646
        $this->expectException(AccessDeniedException::class);
2647
2648
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2649
            ->method('checkAccess')
2650
            ->with($this->equalTo('export'))
2651
            ->will($this->throwException(new AccessDeniedException()));
2652
2653
        $this->controller->exportAction($this->request);
2654
    }
2655
2656
    public function testExportActionWrongFormat(): void
2657
    {
2658
        $this->expectException(\RuntimeException::class);
2659
        $this->expectExceptionMessage(
2660
            'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`'
2661
        );
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
2665
            ->with($this->equalTo('export'))
2666
            ->willReturn(true);
2667
2668
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2669
            ->method('getExportFormats')
2670
            ->willReturn(['json']);
2671
2672
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2673
            ->method('getClass')
2674
            ->willReturn('Foo');
2675
2676
        $this->request->query->set('format', 'csv');
2677
2678
        $this->controller->exportAction($this->request);
2679
    }
2680
2681
    public function testExportAction(): void
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('export'))
2686
            ->willReturn(true);
2687
2688
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2689
            ->method('getExportFormats')
2690
            ->willReturn(['json']);
2691
2692
        $dataSourceIterator = $this->createMock(SourceIteratorInterface::class);
2693
2694
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getDataSourceIterator')
2696
            ->willReturn($dataSourceIterator);
2697
2698
        $this->request->query->set('format', 'json');
2699
2700
        $response = $this->controller->exportAction($this->request);
2701
        $this->assertInstanceOf(StreamedResponse::class, $response);
2702
        $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
2703
        $this->assertSame([], $this->session->getFlashBag()->all());
2704
    }
2705
2706
    public function testHistoryActionAccessDenied(): void
2707
    {
2708
        $this->expectException(AccessDeniedException::class);
2709
2710
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2711
            ->method('getObject')
2712
            ->willReturn(new \stdClass());
2713
2714
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2715
            ->method('checkAccess')
2716
            ->with($this->equalTo('history'))
2717
            ->will($this->throwException(new AccessDeniedException()));
2718
2719
        $this->controller->historyAction(null);
2720
    }
2721
2722
    public function testHistoryActionNotFoundException(): void
2723
    {
2724
        $this->expectException(NotFoundHttpException::class);
2725
2726
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2727
            ->method('getObject')
2728
            ->willReturn(false);
2729
2730
        $this->controller->historyAction(null);
2731
    }
2732
2733
    public function testHistoryActionNoReader(): void
2734
    {
2735
        $this->expectException(NotFoundHttpException::class);
2736
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
2737
2738
        $this->request->query->set('id', 123);
2739
2740
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2741
            ->method('checkAccess')
2742
            ->with($this->equalTo('history'))
2743
            ->willReturn(true);
2744
2745
        $object = new \stdClass();
2746
2747
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2748
            ->method('getObject')
2749
            ->willReturn($object);
2750
2751
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2752
            ->method('getClass')
2753
            ->willReturn('Foo');
2754
2755
        $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...
2756
            ->method('hasReader')
2757
            ->with($this->equalTo('Foo'))
2758
            ->willReturn(false);
2759
2760
        $this->controller->historyAction(null);
2761
    }
2762
2763
    public function testHistoryAction(): void
2764
    {
2765
        $this->request->query->set('id', 123);
2766
2767
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2768
            ->method('checkAccess')
2769
            ->with($this->equalTo('history'))
2770
            ->willReturn(true);
2771
2772
        $object = new \stdClass();
2773
2774
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2775
            ->method('getObject')
2776
            ->willReturn($object);
2777
2778
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getClass')
2780
            ->willReturn('Foo');
2781
2782
        $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...
2783
            ->method('hasReader')
2784
            ->with($this->equalTo('Foo'))
2785
            ->willReturn(true);
2786
2787
        $reader = $this->createMock(AuditReaderInterface::class);
2788
2789
        $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...
2790
            ->method('getReader')
2791
            ->with($this->equalTo('Foo'))
2792
            ->willReturn($reader);
2793
2794
        $reader->expects($this->once())
2795
            ->method('findRevisions')
2796
            ->with($this->equalTo('Foo'), $this->equalTo(123))
2797
            ->willReturn([]);
2798
2799
        $this->assertInstanceOf(Response::class, $this->controller->historyAction(null));
2800
2801
        $this->assertSame($this->admin, $this->parameters['admin']);
2802
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2803
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2804
2805
        $this->assertSame('history', $this->parameters['action']);
2806
        $this->assertSame([], $this->parameters['revisions']);
2807
        $this->assertSame($object, $this->parameters['object']);
2808
2809
        $this->assertSame([], $this->session->getFlashBag()->all());
2810
        $this->assertSame('@SonataAdmin/CRUD/history.html.twig', $this->template);
2811
    }
2812
2813
    public function testAclActionAclNotEnabled(): void
2814
    {
2815
        $this->expectException(NotFoundHttpException::class);
2816
        $this->expectExceptionMessage('ACL are not enabled for this admin');
2817
2818
        $this->controller->aclAction(null);
2819
    }
2820
2821
    public function testAclActionNotFoundException(): void
2822
    {
2823
        $this->expectException(NotFoundHttpException::class);
2824
2825
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2826
            ->method('isAclEnabled')
2827
            ->willReturn(true);
2828
2829
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2830
            ->method('getObject')
2831
            ->willReturn(false);
2832
2833
        $this->controller->aclAction(null);
2834
    }
2835
2836
    public function testAclActionAccessDenied(): void
2837
    {
2838
        $this->expectException(AccessDeniedException::class);
2839
2840
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2841
            ->method('isAclEnabled')
2842
            ->willReturn(true);
2843
2844
        $object = new \stdClass();
2845
2846
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2847
            ->method('getObject')
2848
            ->willReturn($object);
2849
2850
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2851
            ->method('checkAccess')
2852
            ->with($this->equalTo('acl'), $this->equalTo($object))
2853
            ->will($this->throwException(new AccessDeniedException()));
2854
2855
        $this->controller->aclAction(null);
2856
    }
2857
2858
    public function testAclAction(): void
2859
    {
2860
        $this->request->query->set('id', 123);
2861
2862
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2863
            ->method('isAclEnabled')
2864
            ->willReturn(true);
2865
2866
        $object = new \stdClass();
2867
2868
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2869
            ->method('getObject')
2870
            ->willReturn($object);
2871
2872
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2873
            ->method('checkAccess')
2874
            ->willReturn(true);
2875
2876
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2877
            ->method('getSecurityInformation')
2878
            ->willReturn([]);
2879
2880
        $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...
2881
            ->method('getMaskBuilderClass')
2882
            ->willReturn(AdminPermissionMap::class);
2883
2884
        $aclUsersForm = $this->getMockBuilder(Form::class)
2885
            ->disableOriginalConstructor()
2886
            ->getMock();
2887
2888
        $aclUsersForm->expects($this->once())
2889
            ->method('createView')
2890
            ->willReturn($this->createMock(FormView::class));
2891
2892
        $aclRolesForm = $this->getMockBuilder(Form::class)
2893
            ->disableOriginalConstructor()
2894
            ->getMock();
2895
2896
        $aclRolesForm->expects($this->once())
2897
            ->method('createView')
2898
            ->willReturn($this->createMock(FormView::class));
2899
2900
        $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...
2901
            ->method('createAclUsersForm')
2902
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2903
            ->willReturn($aclUsersForm);
2904
2905
        $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...
2906
            ->method('createAclRolesForm')
2907
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2908
            ->willReturn($aclRolesForm);
2909
2910
        $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class)
2911
            ->disableOriginalConstructor()
2912
            ->getMock();
2913
2914
        $aclSecurityHandler
2915
            ->method('getObjectPermissions')
2916
            ->willReturn([]);
2917
2918
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2919
            ->method('getSecurityHandler')
2920
            ->willReturn($aclSecurityHandler);
2921
2922
        $this->assertInstanceOf(Response::class, $this->controller->aclAction(null));
2923
2924
        $this->assertSame($this->admin, $this->parameters['admin']);
2925
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
2926
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
2927
2928
        $this->assertSame('acl', $this->parameters['action']);
2929
        $this->assertSame([], $this->parameters['permissions']);
2930
        $this->assertSame($object, $this->parameters['object']);
2931
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']);
2932
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']);
2933
        $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']);
2934
        $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']);
2935
2936
        $this->assertSame([], $this->session->getFlashBag()->all());
2937
        $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template);
2938
    }
2939
2940
    public function testAclActionInvalidUpdate(): void
2941
    {
2942
        $this->request->query->set('id', 123);
2943
        $this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, []);
2944
2945
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2946
            ->method('isAclEnabled')
2947
            ->willReturn(true);
2948
2949
        $object = new \stdClass();
2950
2951
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2952
            ->method('getObject')
2953
            ->willReturn($object);
2954
2955
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2956
            ->method('checkAccess')
2957
            ->willReturn(true);
2958
2959
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2960
            ->method('getSecurityInformation')
2961
            ->willReturn([]);
2962
2963
        $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...
2964
            ->method('getMaskBuilderClass')
2965
            ->willReturn(AdminPermissionMap::class);
2966
2967
        $aclUsersForm = $this->getMockBuilder(Form::class)
2968
            ->disableOriginalConstructor()
2969
            ->getMock();
2970
2971
        $aclUsersForm->expects($this->once())
2972
            ->method('isValid')
2973
            ->willReturn(false);
2974
2975
        $aclUsersForm->expects($this->once())
2976
            ->method('createView')
2977
            ->willReturn($this->createMock(FormView::class));
2978
2979
        $aclRolesForm = $this->getMockBuilder(Form::class)
2980
            ->disableOriginalConstructor()
2981
            ->getMock();
2982
2983
        $aclRolesForm->expects($this->once())
2984
            ->method('createView')
2985
            ->willReturn($this->createMock(FormView::class));
2986
2987
        $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...
2988
            ->method('createAclUsersForm')
2989
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2990
            ->willReturn($aclUsersForm);
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('createAclRolesForm')
2994
            ->with($this->isInstanceOf(AdminObjectAclData::class))
2995
            ->willReturn($aclRolesForm);
2996
2997
        $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class)
2998
            ->disableOriginalConstructor()
2999
            ->getMock();
3000
3001
        $aclSecurityHandler
3002
            ->method('getObjectPermissions')
3003
            ->willReturn([]);
3004
3005
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3006
            ->method('getSecurityHandler')
3007
            ->willReturn($aclSecurityHandler);
3008
3009
        $this->request->setMethod(Request::METHOD_POST);
3010
3011
        $this->assertInstanceOf(Response::class, $this->controller->aclAction(null));
3012
3013
        $this->assertSame($this->admin, $this->parameters['admin']);
3014
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3015
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3016
3017
        $this->assertSame('acl', $this->parameters['action']);
3018
        $this->assertSame([], $this->parameters['permissions']);
3019
        $this->assertSame($object, $this->parameters['object']);
3020
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['users']);
3021
        $this->assertInstanceOf(\ArrayIterator::class, $this->parameters['roles']);
3022
        $this->assertInstanceOf(FormView::class, $this->parameters['aclUsersForm']);
3023
        $this->assertInstanceOf(FormView::class, $this->parameters['aclRolesForm']);
3024
3025
        $this->assertSame([], $this->session->getFlashBag()->all());
3026
        $this->assertSame('@SonataAdmin/CRUD/acl.html.twig', $this->template);
3027
    }
3028
3029
    public function testAclActionSuccessfulUpdate(): void
3030
    {
3031
        $this->request->query->set('id', 123);
3032
        $this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, []);
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('isAclEnabled')
3036
            ->willReturn(true);
3037
3038
        $object = new \stdClass();
3039
3040
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3041
            ->method('getObject')
3042
            ->willReturn($object);
3043
3044
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3045
            ->method('checkAccess')
3046
            ->willReturn(true);
3047
3048
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3049
            ->method('getSecurityInformation')
3050
            ->willReturn([]);
3051
3052
        $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...
3053
            ->method('getMaskBuilderClass')
3054
            ->willReturn(AdminPermissionMap::class);
3055
3056
        $aclUsersForm = $this->getMockBuilder(Form::class)
3057
            ->disableOriginalConstructor()
3058
            ->getMock();
3059
3060
        $aclUsersForm
3061
            ->method('createView')
3062
            ->willReturn($this->createMock(FormView::class));
3063
3064
        $aclRolesForm = $this->getMockBuilder(Form::class)
3065
            ->disableOriginalConstructor()
3066
            ->getMock();
3067
3068
        $aclRolesForm
3069
            ->method('createView')
3070
            ->willReturn($this->createMock(FormView::class));
3071
3072
        $aclRolesForm->expects($this->once())
3073
            ->method('isValid')
3074
            ->willReturn(true);
3075
3076
        $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...
3077
            ->method('createAclUsersForm')
3078
            ->with($this->isInstanceOf(AdminObjectAclData::class))
3079
            ->willReturn($aclUsersForm);
3080
3081
        $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...
3082
            ->method('createAclRolesForm')
3083
            ->with($this->isInstanceOf(AdminObjectAclData::class))
3084
            ->willReturn($aclRolesForm);
3085
3086
        $aclSecurityHandler = $this->getMockBuilder(AclSecurityHandler::class)
3087
            ->disableOriginalConstructor()
3088
            ->getMock();
3089
3090
        $aclSecurityHandler
3091
            ->method('getObjectPermissions')
3092
            ->willReturn([]);
3093
3094
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3095
            ->method('getSecurityHandler')
3096
            ->willReturn($aclSecurityHandler);
3097
3098
        $this->expectTranslate('flash_acl_edit_success', [], 'SonataAdminBundle');
3099
3100
        $this->request->setMethod(Request::METHOD_POST);
3101
3102
        $response = $this->controller->aclAction(null);
3103
3104
        $this->assertInstanceOf(RedirectResponse::class, $response);
3105
3106
        $this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3107
        $this->assertSame('stdClass_acl', $response->getTargetUrl());
3108
    }
3109
3110
    public function testHistoryViewRevisionActionAccessDenied(): void
3111
    {
3112
        $this->expectException(AccessDeniedException::class);
3113
3114
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3115
            ->method('getObject')
3116
            ->willReturn(new \stdClass());
3117
3118
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3119
            ->method('checkAccess')
3120
            ->with($this->equalTo('historyViewRevision'))
3121
            ->will($this->throwException(new AccessDeniedException()));
3122
3123
        $this->controller->historyViewRevisionAction(null, null);
3124
    }
3125
3126
    public function testHistoryViewRevisionActionNotFoundException(): void
3127
    {
3128
        $this->expectException(NotFoundHttpException::class);
3129
        $this->expectExceptionMessage('unable to find the object with id: 123');
3130
3131
        $this->request->query->set('id', 123);
3132
3133
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3134
            ->method('getObject')
3135
            ->willReturn(false);
3136
3137
        $this->controller->historyViewRevisionAction(null, null);
3138
    }
3139
3140
    public function testHistoryViewRevisionActionNoReader(): void
3141
    {
3142
        $this->expectException(NotFoundHttpException::class);
3143
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
3144
3145
        $this->request->query->set('id', 123);
3146
3147
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3148
            ->method('checkAccess')
3149
            ->with($this->equalTo('historyViewRevision'))
3150
            ->willReturn(true);
3151
3152
        $object = new \stdClass();
3153
3154
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3155
            ->method('getObject')
3156
            ->willReturn($object);
3157
3158
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3159
            ->method('getClass')
3160
            ->willReturn('Foo');
3161
3162
        $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...
3163
            ->method('hasReader')
3164
            ->with($this->equalTo('Foo'))
3165
            ->willReturn(false);
3166
3167
        $this->controller->historyViewRevisionAction(null, null);
3168
    }
3169
3170
    public function testHistoryViewRevisionActionNotFoundRevision(): void
3171
    {
3172
        $this->expectException(NotFoundHttpException::class);
3173
        $this->expectExceptionMessage(
3174
            'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'
3175
        );
3176
3177
        $this->request->query->set('id', 123);
3178
3179
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3180
            ->method('checkAccess')
3181
            ->with($this->equalTo('historyViewRevision'))
3182
            ->willReturn(true);
3183
3184
        $object = new \stdClass();
3185
3186
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3187
            ->method('getObject')
3188
            ->willReturn($object);
3189
3190
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3191
            ->method('getClass')
3192
            ->willReturn('Foo');
3193
3194
        $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...
3195
            ->method('hasReader')
3196
            ->with($this->equalTo('Foo'))
3197
            ->willReturn(true);
3198
3199
        $reader = $this->createMock(AuditReaderInterface::class);
3200
3201
        $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...
3202
            ->method('getReader')
3203
            ->with($this->equalTo('Foo'))
3204
            ->willReturn($reader);
3205
3206
        $reader->expects($this->once())
3207
            ->method('find')
3208
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3209
            ->willReturn(null);
3210
3211
        $this->controller->historyViewRevisionAction(123, 456);
3212
    }
3213
3214
    public function testHistoryViewRevisionAction(): void
3215
    {
3216
        $this->request->query->set('id', 123);
3217
3218
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3219
            ->method('checkAccess')
3220
            ->with($this->equalTo('historyViewRevision'))
3221
            ->willReturn(true);
3222
3223
        $object = new \stdClass();
3224
3225
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3226
            ->method('getObject')
3227
            ->willReturn($object);
3228
3229
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3230
            ->method('getClass')
3231
            ->willReturn('Foo');
3232
3233
        $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...
3234
            ->method('hasReader')
3235
            ->with($this->equalTo('Foo'))
3236
            ->willReturn(true);
3237
3238
        $reader = $this->createMock(AuditReaderInterface::class);
3239
3240
        $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...
3241
            ->method('getReader')
3242
            ->with($this->equalTo('Foo'))
3243
            ->willReturn($reader);
3244
3245
        $objectRevision = new \stdClass();
3246
        $objectRevision->revision = 456;
3247
3248
        $reader->expects($this->once())
3249
            ->method('find')
3250
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3251
            ->willReturn($objectRevision);
3252
3253
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3254
            ->method('setSubject')
3255
            ->with($this->equalTo($objectRevision))
3256
            ->willReturn(null);
3257
3258
        $fieldDescriptionCollection = new FieldDescriptionCollection();
3259
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3260
            ->method('getShow')
3261
            ->willReturn($fieldDescriptionCollection);
3262
3263
        $this->assertInstanceOf(Response::class, $this->controller->historyViewRevisionAction(123, 456));
3264
3265
        $this->assertSame($this->admin, $this->parameters['admin']);
3266
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3267
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3268
3269
        $this->assertSame('show', $this->parameters['action']);
3270
        $this->assertSame($objectRevision, $this->parameters['object']);
3271
        $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
3272
3273
        $this->assertSame([], $this->session->getFlashBag()->all());
3274
        $this->assertSame('@SonataAdmin/CRUD/show.html.twig', $this->template);
3275
    }
3276
3277
    public function testHistoryCompareRevisionsActionAccessDenied(): void
3278
    {
3279
        $this->expectException(AccessDeniedException::class);
3280
3281
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3282
            ->method('checkAccess')
3283
            ->with($this->equalTo('historyCompareRevisions'))
3284
            ->will($this->throwException(new AccessDeniedException()));
3285
3286
        $this->controller->historyCompareRevisionsAction(null, null, null);
3287
    }
3288
3289
    public function testHistoryCompareRevisionsActionNotFoundException(): void
3290
    {
3291
        $this->expectException(NotFoundHttpException::class);
3292
        $this->expectExceptionMessage('unable to find the object with id: 123');
3293
3294
        $this->request->query->set('id', 123);
3295
3296
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3297
            ->method('checkAccess')
3298
            ->with($this->equalTo('historyCompareRevisions'))
3299
            ->willReturn(true);
3300
3301
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3302
            ->method('getObject')
3303
            ->willReturn(false);
3304
3305
        $this->controller->historyCompareRevisionsAction(null, null, null);
3306
    }
3307
3308
    public function testHistoryCompareRevisionsActionNoReader(): void
3309
    {
3310
        $this->expectException(NotFoundHttpException::class);
3311
        $this->expectExceptionMessage('unable to find the audit reader for class : Foo');
3312
3313
        $this->request->query->set('id', 123);
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('checkAccess')
3317
            ->with($this->equalTo('historyCompareRevisions'))
3318
            ->willReturn(true);
3319
3320
        $object = new \stdClass();
3321
3322
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3323
            ->method('getObject')
3324
            ->willReturn($object);
3325
3326
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3327
            ->method('getClass')
3328
            ->willReturn('Foo');
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('hasReader')
3332
            ->with($this->equalTo('Foo'))
3333
            ->willReturn(false);
3334
3335
        $this->controller->historyCompareRevisionsAction(null, null, null);
3336
    }
3337
3338
    public function testHistoryCompareRevisionsActionNotFoundBaseRevision(): void
3339
    {
3340
        $this->expectException(NotFoundHttpException::class);
3341
        $this->expectExceptionMessage(
3342
            'unable to find the targeted object `123` from the revision `456` with classname : `Foo`'
3343
        );
3344
3345
        $this->request->query->set('id', 123);
3346
3347
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3348
            ->method('checkAccess')
3349
            ->with($this->equalTo('historyCompareRevisions'))
3350
            ->willReturn(true);
3351
3352
        $object = new \stdClass();
3353
3354
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3355
            ->method('getObject')
3356
            ->willReturn($object);
3357
3358
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3359
            ->method('getClass')
3360
            ->willReturn('Foo');
3361
3362
        $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...
3363
            ->method('hasReader')
3364
            ->with($this->equalTo('Foo'))
3365
            ->willReturn(true);
3366
3367
        $reader = $this->createMock(AuditReaderInterface::class);
3368
3369
        $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...
3370
            ->method('getReader')
3371
            ->with($this->equalTo('Foo'))
3372
            ->willReturn($reader);
3373
3374
        // once because it will not be found and therefore the second call won't be executed
3375
        $reader->expects($this->once())
3376
            ->method('find')
3377
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3378
            ->willReturn(null);
3379
3380
        $this->controller->historyCompareRevisionsAction(123, 456, 789);
3381
    }
3382
3383
    public function testHistoryCompareRevisionsActionNotFoundCompareRevision(): void
3384
    {
3385
        $this->expectException(NotFoundHttpException::class);
3386
        $this->expectExceptionMessage(
3387
            'unable to find the targeted object `123` from the revision `789` with classname : `Foo`'
3388
        );
3389
3390
        $this->request->query->set('id', 123);
3391
3392
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3393
            ->method('checkAccess')
3394
            ->with($this->equalTo('historyCompareRevisions'))
3395
            ->willReturn(true);
3396
3397
        $object = new \stdClass();
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getObject')
3401
            ->willReturn($object);
3402
3403
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3404
            ->method('getClass')
3405
            ->willReturn('Foo');
3406
3407
        $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...
3408
            ->method('hasReader')
3409
            ->with($this->equalTo('Foo'))
3410
            ->willReturn(true);
3411
3412
        $reader = $this->createMock(AuditReaderInterface::class);
3413
3414
        $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...
3415
            ->method('getReader')
3416
            ->with($this->equalTo('Foo'))
3417
            ->willReturn($reader);
3418
3419
        $objectRevision = new \stdClass();
3420
        $objectRevision->revision = 456;
3421
3422
        // first call should return, so the second call will throw an exception
3423
        $reader->expects($this->at(0))
3424
            ->method('find')
3425
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3426
            ->willReturn($objectRevision);
3427
3428
        $reader->expects($this->at(1))
3429
            ->method('find')
3430
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
3431
            ->willReturn(null);
3432
3433
        $this->controller->historyCompareRevisionsAction(123, 456, 789);
3434
    }
3435
3436
    public function testHistoryCompareRevisionsActionAction(): void
3437
    {
3438
        $this->request->query->set('id', 123);
3439
3440
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3441
            ->method('checkAccess')
3442
            ->with($this->equalTo('historyCompareRevisions'))
3443
            ->willReturn(true);
3444
3445
        $object = new \stdClass();
3446
3447
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3448
            ->method('getObject')
3449
            ->willReturn($object);
3450
3451
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3452
            ->method('getClass')
3453
            ->willReturn('Foo');
3454
3455
        $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...
3456
            ->method('hasReader')
3457
            ->with($this->equalTo('Foo'))
3458
            ->willReturn(true);
3459
3460
        $reader = $this->createMock(AuditReaderInterface::class);
3461
3462
        $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...
3463
            ->method('getReader')
3464
            ->with($this->equalTo('Foo'))
3465
            ->willReturn($reader);
3466
3467
        $objectRevision = new \stdClass();
3468
        $objectRevision->revision = 456;
3469
3470
        $compareObjectRevision = new \stdClass();
3471
        $compareObjectRevision->revision = 789;
3472
3473
        $reader->expects($this->at(0))
3474
            ->method('find')
3475
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
3476
            ->willReturn($objectRevision);
3477
3478
        $reader->expects($this->at(1))
3479
            ->method('find')
3480
            ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
3481
            ->willReturn($compareObjectRevision);
3482
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('setSubject')
3485
            ->with($this->equalTo($objectRevision))
3486
            ->willReturn(null);
3487
3488
        $fieldDescriptionCollection = new FieldDescriptionCollection();
3489
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3490
            ->method('getShow')
3491
            ->willReturn($fieldDescriptionCollection);
3492
3493
        $this->assertInstanceOf(Response::class, $this->controller->historyCompareRevisionsAction(123, 456, 789));
3494
3495
        $this->assertSame($this->admin, $this->parameters['admin']);
3496
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3497
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3498
3499
        $this->assertSame('show', $this->parameters['action']);
3500
        $this->assertSame($objectRevision, $this->parameters['object']);
3501
        $this->assertSame($compareObjectRevision, $this->parameters['object_compare']);
3502
        $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
3503
3504
        $this->assertSame([], $this->session->getFlashBag()->all());
3505
        $this->assertSame('@SonataAdmin/CRUD/show_compare.html.twig', $this->template);
3506
    }
3507
3508
    public function testBatchActionWrongMethod(): void
3509
    {
3510
        $this->expectException(NotFoundHttpException::class);
3511
        $this->expectExceptionMessage('Invalid request method given "GET", POST expected');
3512
3513
        $this->controller->batchAction();
3514
    }
3515
3516
    /**
3517
     * NEXT_MAJOR: Remove this legacy group.
3518
     *
3519
     * @group legacy
3520
     */
3521
    public function testBatchActionActionNotDefined(): void
3522
    {
3523
        $this->expectException(\RuntimeException::class);
3524
        $this->expectExceptionMessage('The `foo` batch action is not defined');
3525
3526
        $batchActions = [];
3527
3528
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3529
            ->method('getBatchActions')
3530
            ->willReturn($batchActions);
3531
3532
        $this->request->setMethod(Request::METHOD_POST);
3533
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3534
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3535
3536
        $this->controller->batchAction();
3537
    }
3538
3539
    public function testBatchActionActionInvalidCsrfToken(): void
3540
    {
3541
        $this->request->setMethod(Request::METHOD_POST);
3542
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3543
        $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
3544
3545
        try {
3546
            $this->controller->batchAction();
3547
        } catch (HttpException $e) {
3548
            $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
3549
            $this->assertSame(400, $e->getStatusCode());
3550
        }
3551
    }
3552
3553
    /**
3554
     * NEXT_MAJOR: Remove this legacy group.
3555
     *
3556
     * @group legacy
3557
     */
3558
    public function testBatchActionMethodNotExist(): void
3559
    {
3560
        $this->expectException(\RuntimeException::class);
3561
        $this->expectExceptionMessage(
3562
            'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable'
3563
        );
3564
3565
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getBatchActions')
3569
            ->willReturn($batchActions);
3570
3571
        $datagrid = $this->createMock(DatagridInterface::class);
3572
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getDatagrid')
3574
            ->willReturn($datagrid);
3575
3576
        $this->request->setMethod(Request::METHOD_POST);
3577
        $this->request->request->set('data', json_encode(['action' => 'foo', 'idx' => ['123', '456'], 'all_elements' => false]));
3578
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3579
3580
        $this->controller->batchAction();
3581
    }
3582
3583
    /**
3584
     * NEXT_MAJOR: Remove this legacy group.
3585
     *
3586
     * @group legacy
3587
     */
3588
    public function testBatchActionWithoutConfirmation(): void
3589
    {
3590
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3591
3592
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3593
            ->method('getBatchActions')
3594
            ->willReturn($batchActions);
3595
3596
        $datagrid = $this->createMock(DatagridInterface::class);
3597
3598
        $query = $this->createMock(ProxyQueryInterface::class);
3599
        $datagrid->expects($this->once())
3600
            ->method('getQuery')
3601
            ->willReturn($query);
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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself 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('getDatagrid')
3605
            ->willReturn($datagrid);
3606
3607
        $modelManager = $this->createMock(ModelManagerInterface::class);
3608
3609
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3610
            ->method('checkAccess')
3611
            ->with($this->equalTo('batchDelete'))
3612
            ->willReturn(true);
3613
3614
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3615
            ->method('getModelManager')
3616
            ->willReturn($modelManager);
3617
3618
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3619
            ->method('getClass')
3620
            ->willReturn('Foo');
3621
3622
        $modelManager->expects($this->once())
3623
            ->method('addIdentifiersToQuery')
3624
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3625
            ->willReturn(true);
3626
3627
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3628
3629
        $this->request->setMethod(Request::METHOD_POST);
3630
        $this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]));
3631
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3632
3633
        $result = $this->controller->batchAction();
3634
3635
        $this->assertInstanceOf(RedirectResponse::class, $result);
3636
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3637
        $this->assertSame('list', $result->getTargetUrl());
3638
    }
3639
3640
    /**
3641
     * NEXT_MAJOR: Remove this legacy group.
3642
     *
3643
     * @group legacy
3644
     */
3645
    public function testBatchActionWithoutConfirmation2(): void
3646
    {
3647
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3648
3649
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3650
            ->method('getBatchActions')
3651
            ->willReturn($batchActions);
3652
3653
        $datagrid = $this->createMock(DatagridInterface::class);
3654
3655
        $query = $this->createMock(ProxyQueryInterface::class);
3656
        $datagrid->expects($this->once())
3657
            ->method('getQuery')
3658
            ->willReturn($query);
3659
3660
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3661
            ->method('getDatagrid')
3662
            ->willReturn($datagrid);
3663
3664
        $modelManager = $this->createMock(ModelManagerInterface::class);
3665
3666
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3667
            ->method('checkAccess')
3668
            ->with($this->equalTo('batchDelete'))
3669
            ->willReturn(true);
3670
3671
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3672
            ->method('getModelManager')
3673
            ->willReturn($modelManager);
3674
3675
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3676
            ->method('getClass')
3677
            ->willReturn('Foo');
3678
3679
        $modelManager->expects($this->once())
3680
            ->method('addIdentifiersToQuery')
3681
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3682
            ->willReturn(true);
3683
3684
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3685
3686
        $this->request->setMethod(Request::METHOD_POST);
3687
        $this->request->request->set('action', 'delete');
3688
        $this->request->request->set('idx', ['123', '456']);
3689
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3690
3691
        $result = $this->controller->batchAction();
3692
3693
        $this->assertInstanceOf(RedirectResponse::class, $result);
3694
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3695
        $this->assertSame('list', $result->getTargetUrl());
3696
    }
3697
3698
    /**
3699
     * NEXT_MAJOR: Remove this legacy group.
3700
     *
3701
     * @group legacy
3702
     */
3703
    public function testBatchActionWithConfirmation(): void
3704
    {
3705
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]];
3706
3707
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3708
            ->method('getBatchActions')
3709
            ->willReturn($batchActions);
3710
3711
        $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false];
3712
3713
        $this->request->setMethod(Request::METHOD_POST);
3714
        $this->request->request->set('data', json_encode($data));
3715
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3716
3717
        $datagrid = $this->createMock(DatagridInterface::class);
3718
3719
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3720
            ->method('getDatagrid')
3721
            ->willReturn($datagrid);
3722
3723
        $form = $this->getMockBuilder(Form::class)
3724
            ->disableOriginalConstructor()
3725
            ->getMock();
3726
3727
        $form->expects($this->once())
3728
            ->method('createView')
3729
            ->willReturn($this->createMock(FormView::class));
3730
3731
        $datagrid->expects($this->once())
3732
            ->method('getForm')
3733
            ->willReturn($form);
3734
3735
        $this->assertInstanceOf(Response::class, $this->controller->batchAction());
3736
3737
        $this->assertSame($this->admin, $this->parameters['admin']);
3738
        $this->assertSame('@SonataAdmin/standard_layout.html.twig', $this->parameters['base_template']);
3739
        $this->assertSame($this->pool, $this->parameters['admin_pool']);
3740
3741
        $this->assertSame('list', $this->parameters['action']);
3742
        $this->assertSame($datagrid, $this->parameters['datagrid']);
3743
        $this->assertInstanceOf(FormView::class, $this->parameters['form']);
3744
        $this->assertSame($data, $this->parameters['data']);
3745
        $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
3746
        $this->assertSame('Foo Bar', $this->parameters['action_label']);
3747
3748
        $this->assertSame([], $this->session->getFlashBag()->all());
3749
        $this->assertSame('@SonataAdmin/CRUD/batch_confirmation.html.twig', $this->template);
3750
    }
3751
3752
    /**
3753
     * NEXT_MAJOR: Remove this legacy group.
3754
     *
3755
     * @group legacy
3756
     */
3757
    public function testBatchActionNonRelevantAction(): void
3758
    {
3759
        $controller = new BatchAdminController();
3760
        $controller->setContainer($this->container);
3761
3762
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3763
3764
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3765
            ->method('getBatchActions')
3766
            ->willReturn($batchActions);
3767
3768
        $datagrid = $this->createMock(DatagridInterface::class);
3769
3770
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3771
            ->method('getDatagrid')
3772
            ->willReturn($datagrid);
3773
3774
        $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');
3775
3776
        $this->request->setMethod(Request::METHOD_POST);
3777
        $this->request->request->set('action', 'foo');
3778
        $this->request->request->set('idx', ['789']);
3779
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3780
3781
        $result = $controller->batchAction();
3782
3783
        $this->assertInstanceOf(RedirectResponse::class, $result);
3784
        $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
3785
        $this->assertSame('list', $result->getTargetUrl());
3786
    }
3787
3788
    public function testBatchActionWithCustomConfirmationTemplate(): void
3789
    {
3790
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true, 'template' => 'custom_template.html.twig']];
3791
3792
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3793
            ->method('getBatchActions')
3794
            ->willReturn($batchActions);
3795
3796
        $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false];
3797
3798
        $this->request->setMethod(Request::METHOD_POST);
3799
        $this->request->request->set('data', json_encode($data));
3800
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3801
3802
        $datagrid = $this->createMock(DatagridInterface::class);
3803
3804
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3805
            ->method('getDatagrid')
3806
            ->willReturn($datagrid);
3807
3808
        $form = $this->createMock(Form::class);
3809
3810
        $form->expects($this->once())
3811
            ->method('createView')
3812
            ->willReturn($this->createMock(FormView::class));
3813
3814
        $datagrid->expects($this->once())
3815
            ->method('getForm')
3816
            ->willReturn($form);
3817
3818
        $this->controller->batchAction();
3819
3820
        $this->assertSame('custom_template.html.twig', $this->template);
3821
    }
3822
3823
    /**
3824
     * NEXT_MAJOR: Remove this legacy group.
3825
     *
3826
     * @group legacy
3827
     */
3828
    public function testBatchActionNonRelevantAction2(): void
3829
    {
3830
        $controller = new BatchAdminController();
3831
        $controller->setContainer($this->container);
3832
3833
        $batchActions = ['foo' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3834
3835
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3836
            ->method('getBatchActions')
3837
            ->willReturn($batchActions);
3838
3839
        $datagrid = $this->createMock(DatagridInterface::class);
3840
3841
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3842
            ->method('getDatagrid')
3843
            ->willReturn($datagrid);
3844
3845
        $this->expectTranslate('flash_foo_error', [], 'SonataAdminBundle');
3846
3847
        $this->request->setMethod(Request::METHOD_POST);
3848
        $this->request->request->set('action', 'foo');
3849
        $this->request->request->set('idx', ['999']);
3850
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3851
3852
        $result = $controller->batchAction();
3853
3854
        $this->assertInstanceOf(RedirectResponse::class, $result);
3855
        $this->assertSame(['flash_foo_error'], $this->session->getFlashBag()->get('sonata_flash_info'));
3856
        $this->assertSame('list', $result->getTargetUrl());
3857
    }
3858
3859
    /**
3860
     * NEXT_MAJOR: Remove this legacy group.
3861
     *
3862
     * @group legacy
3863
     */
3864
    public function testBatchActionNoItems(): void
3865
    {
3866
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => true]];
3867
3868
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3869
            ->method('getBatchActions')
3870
            ->willReturn($batchActions);
3871
3872
        $datagrid = $this->createMock(DatagridInterface::class);
3873
3874
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3875
            ->method('getDatagrid')
3876
            ->willReturn($datagrid);
3877
3878
        $this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');
3879
3880
        $this->request->setMethod(Request::METHOD_POST);
3881
        $this->request->request->set('action', 'delete');
3882
        $this->request->request->set('idx', []);
3883
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3884
3885
        $result = $this->controller->batchAction();
3886
3887
        $this->assertInstanceOf(RedirectResponse::class, $result);
3888
        $this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
3889
        $this->assertSame('list', $result->getTargetUrl());
3890
    }
3891
3892
    /**
3893
     * NEXT_MAJOR: Remove this legacy group.
3894
     *
3895
     * @group legacy
3896
     */
3897
    public function testBatchActionNoItemsEmptyQuery(): void
3898
    {
3899
        $controller = new BatchAdminController();
3900
        $controller->setContainer($this->container);
3901
3902
        $batchActions = ['bar' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3903
3904
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3905
            ->method('getBatchActions')
3906
            ->willReturn($batchActions);
3907
3908
        $datagrid = $this->createMock(DatagridInterface::class);
3909
3910
        $query = $this->createMock(ProxyQueryInterface::class);
3911
        $datagrid->expects($this->once())
3912
            ->method('getQuery')
3913
            ->willReturn($query);
3914
3915
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3916
            ->method('getDatagrid')
3917
            ->willReturn($datagrid);
3918
3919
        $modelManager = $this->createMock(ModelManagerInterface::class);
3920
3921
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3922
            ->method('getModelManager')
3923
            ->willReturn($modelManager);
3924
3925
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3926
            ->method('getClass')
3927
            ->willReturn('Foo');
3928
3929
        $this->request->setMethod(Request::METHOD_POST);
3930
        $this->request->request->set('action', 'bar');
3931
        $this->request->request->set('idx', []);
3932
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3933
3934
        $this->expectTranslate('flash_batch_no_elements_processed', [], 'SonataAdminBundle');
3935
        $result = $controller->batchAction();
3936
3937
        $this->assertInstanceOf(Response::class, $result);
3938
        $this->assertRegExp('/Redirecting to list/', $result->getContent());
3939
    }
3940
3941
    /**
3942
     * NEXT_MAJOR: Remove this legacy group.
3943
     *
3944
     * @group legacy
3945
     */
3946
    public function testBatchActionWithRequesData(): void
3947
    {
3948
        $batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];
3949
3950
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3951
            ->method('getBatchActions')
3952
            ->willReturn($batchActions);
3953
3954
        $datagrid = $this->createMock(DatagridInterface::class);
3955
3956
        $query = $this->createMock(ProxyQueryInterface::class);
3957
        $datagrid->expects($this->once())
3958
            ->method('getQuery')
3959
            ->willReturn($query);
3960
3961
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3962
            ->method('getDatagrid')
3963
            ->willReturn($datagrid);
3964
3965
        $modelManager = $this->createMock(ModelManagerInterface::class);
3966
3967
        $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\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3968
            ->method('checkAccess')
3969
            ->with($this->equalTo('batchDelete'))
3970
            ->willReturn(true);
3971
3972
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3973
            ->method('getModelManager')
3974
            ->willReturn($modelManager);
3975
3976
        $this->admin
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AbstractAdmin>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
3977
            ->method('getClass')
3978
            ->willReturn('Foo');
3979
3980
        $modelManager->expects($this->once())
3981
            ->method('addIdentifiersToQuery')
3982
            ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(['123', '456']))
3983
            ->willReturn(true);
3984
3985
        $this->expectTranslate('flash_batch_delete_success', [], 'SonataAdminBundle');
3986
3987
        $this->request->setMethod(Request::METHOD_POST);
3988
        $this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]));
3989
        $this->request->request->set('foo', 'bar');
3990
        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
3991
3992
        $result = $this->controller->batchAction();
3993
3994
        $this->assertInstanceOf(RedirectResponse::class, $result);
3995
        $this->assertSame(['flash_batch_delete_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
3996
        $this->assertSame('list', $result->getTargetUrl());
3997
        $this->assertSame('bar', $this->request->request->get('foo'));
3998
    }
3999
4000
    /**
4001
     * @expectedDeprecation Method Sonata\AdminBundle\Controller\CRUDController::render has been renamed to Sonata\AdminBundle\Controller\CRUDController::renderWithExtraParams.
4002
     */
4003
    public function testRenderIsDeprecated(): void
4004
    {
4005
        $this->controller->render('toto.html.twig');
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Contr...RUDController::render() has been deprecated with message: since sonata-project/admin-bundle 3.27, to be removed in 4.0. Use Sonata\AdminBundle\Controller\CRUDController::renderWithExtraParams() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
4006
    }
4007
4008
    public function getCsrfProvider()
4009
    {
4010
        return $this->csrfProvider;
4011
    }
4012
4013
    public function getToStringValues()
4014
    {
4015
        return [
4016
            ['', ''],
4017
            ['Foo', 'Foo'],
4018
            ['&lt;a href=&quot;http://foo&quot;&gt;Bar&lt;/a&gt;', '<a href="http://foo">Bar</a>'],
4019
            ['&lt;&gt;&amp;&quot;&#039;abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/', '<>&"\'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/'],
4020
        ];
4021
    }
4022
4023
    private function assertLoggerLogsModelManagerException($subject, string $method): void
4024
    {
4025
        $exception = new ModelManagerException(
4026
            $message = 'message',
4027
            1234,
4028
            new \Exception($previousExceptionMessage = 'very useful message')
4029
        );
4030
4031
        $subject->expects($this->once())
4032
            ->method($method)
4033
            ->willReturnCallback(static function () use ($exception): void {
4034
                throw $exception;
4035
            });
4036
4037
        $this->logger->expects($this->once())
4038
            ->method('error')
4039
            ->with($message, [
4040
                'exception' => $exception,
4041
                'previous_exception_message' => $previousExceptionMessage,
4042
            ]);
4043
    }
4044
4045
    private function expectTranslate(
4046
        string $id,
4047
        array $parameters = [],
4048
        ?string $domain = null,
4049
        ?string $locale = null
4050
    ): void {
4051
        $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...
4052
            ->method('trans')
4053
            ->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale))
4054
            ->willReturn($id);
4055
    }
4056
}
4057