Completed
Push — 3.x ( 3da661...ec6eec )
by Grégoire
03:21
created

testDeprecatedAbstractAdminConstructorArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Admin;
15
16
use Doctrine\Common\Collections\Collection;
17
use Knp\Menu\FactoryInterface;
18
use Knp\Menu\ItemInterface;
19
use PHPUnit\Framework\TestCase;
20
use Sonata\AdminBundle\Admin\AbstractAdmin;
21
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
22
use Sonata\AdminBundle\Admin\AdminExtensionInterface;
23
use Sonata\AdminBundle\Admin\AdminInterface;
24
use Sonata\AdminBundle\Admin\BreadcrumbsBuilder;
25
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
26
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
27
use Sonata\AdminBundle\Admin\Pool;
28
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
29
use Sonata\AdminBundle\Builder\FormContractorInterface;
30
use Sonata\AdminBundle\Builder\ListBuilderInterface;
31
use Sonata\AdminBundle\Builder\RouteBuilderInterface;
32
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
33
use Sonata\AdminBundle\Datagrid\DatagridInterface;
34
use Sonata\AdminBundle\Datagrid\PagerInterface;
35
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
36
use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface;
37
use Sonata\AdminBundle\Model\AuditManagerInterface;
38
use Sonata\AdminBundle\Model\ModelManagerInterface;
39
use Sonata\AdminBundle\Route\DefaultRouteGenerator;
40
use Sonata\AdminBundle\Route\PathInfoBuilder;
41
use Sonata\AdminBundle\Route\RouteGeneratorInterface;
42
use Sonata\AdminBundle\Route\RoutesCache;
43
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
44
use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
45
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
46
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
47
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentVoteAdmin;
48
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentWithCustomRouteAdmin;
49
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
50
use Sonata\AdminBundle\Tests\Fixtures\Admin\FilteredAdmin;
51
use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
52
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
53
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin;
54
use Sonata\AdminBundle\Tests\Fixtures\Admin\TagAdmin;
55
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost;
56
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Comment;
57
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
58
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
59
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
60
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToStringNull;
61
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
62
use Sonata\Doctrine\Adapter\AdapterInterface;
63
use Symfony\Component\DependencyInjection\Container;
64
use Symfony\Component\DependencyInjection\ContainerInterface;
65
use Symfony\Component\Filesystem\Filesystem;
66
use Symfony\Component\Form\FormBuilder;
67
use Symfony\Component\Form\FormBuilderInterface;
68
use Symfony\Component\Form\FormEvent;
69
use Symfony\Component\Form\FormEvents;
70
use Symfony\Component\HttpFoundation\ParameterBag;
71
use Symfony\Component\HttpFoundation\Request;
72
use Symfony\Component\PropertyAccess\PropertyAccess;
73
use Symfony\Component\Routing\RouterInterface;
74
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
75
use Symfony\Component\Translation\TranslatorInterface;
76
use Symfony\Component\Validator\Mapping\MemberMetadata;
77
use Symfony\Component\Validator\Validator\ValidatorInterface;
78
79
class AdminTest extends TestCase
80
{
81
    protected $cacheTempFolder;
82
83
    public function setUp(): void
84
    {
85
        $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
86
        $filesystem = new Filesystem();
87
        $filesystem->remove($this->cacheTempFolder);
88
    }
89
90
    /**
91
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct
92
     */
93
    public function testConstructor(): void
94
    {
95
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
96
        $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController';
97
98
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
99
        $this->assertInstanceOf(AbstractAdmin::class, $admin);
100
        $this->assertSame($class, $admin->getClass());
101
        $this->assertSame($baseControllerName, $admin->getBaseControllerName());
102
    }
103
104
    public function testGetClass(): void
105
    {
106
        $class = Post::class;
107
        $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController';
108
109
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
110
111
        $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class));
112
113
        $admin->setSubject(new BlogPost());
114
        $this->assertSame(BlogPost::class, $admin->getClass());
115
116
        $admin->setSubClasses(['foo']);
117
        $this->assertSame(BlogPost::class, $admin->getClass());
118
119
        $admin->setSubject(null);
120
        $admin->setSubClasses([]);
121
        $this->assertSame($class, $admin->getClass());
122
123
        $admin->setSubClasses(['foo' => 'bar']);
124
        $admin->setRequest(new Request(['subclass' => 'foo']));
125
        $this->assertSame('bar', $admin->getClass());
126
    }
127
128
    public function testGetClassException(): void
129
    {
130
        $this->expectException(\RuntimeException::class);
131
        $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass');
132
133
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
134
        $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController';
135
136
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
137
        $admin->setParentFieldDescription(new FieldDescription());
138
        $admin->setSubClasses(['foo' => 'bar']);
139
        $admin->setRequest(new Request(['subclass' => 'foo']));
140
        $admin->getClass();
141
    }
142
143
    public function testCheckAccessThrowsExceptionOnMadeUpAction(): void
144
    {
145
        $admin = new PostAdmin(
146
            'sonata.post.admin.post',
147
            'Application\Sonata\NewsBundle\Entity\Post',
148
            'Sonata\NewsBundle\Controller\PostAdminController'
149
        );
150
        $this->expectException(
151
            \InvalidArgumentException::class
152
        );
153
        $this->expectExceptionMessage(
154
            'Action "made-up" could not be found'
155
        );
156
        $admin->checkAccess('made-up');
157
    }
158
159
    public function testCheckAccessThrowsAccessDeniedException(): void
160
    {
161
        $admin = new PostAdmin(
162
            'sonata.post.admin.post',
163
            'Application\Sonata\NewsBundle\Entity\Post',
164
            'Sonata\NewsBundle\Controller\PostAdminController'
165
        );
166
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
167
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
168
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
169
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
170
        $customExtension->getAccessMapping($admin)->willReturn(
171
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
172
        );
173
        $admin->addExtension($customExtension->reveal());
174
        $admin->setSecurityHandler($securityHandler->reveal());
175
        $this->expectException(
176
            AccessDeniedException::class
177
        );
178
        $this->expectExceptionMessage(
179
            'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE'
180
        );
181
        $admin->checkAccess('custom_action');
182
    }
183
184
    public function testHasAccessOnMadeUpAction(): void
185
    {
186
        $admin = new PostAdmin(
187
            'sonata.post.admin.post',
188
            'Application\Sonata\NewsBundle\Entity\Post',
189
            'Sonata\NewsBundle\Controller\PostAdminController'
190
        );
191
192
        $this->assertFalse($admin->hasAccess('made-up'));
193
    }
194
195
    public function testHasAccess(): void
196
    {
197
        $admin = new PostAdmin(
198
            'sonata.post.admin.post',
199
            'Application\Sonata\NewsBundle\Entity\Post',
200
            'Sonata\NewsBundle\Controller\PostAdminController'
201
        );
202
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
203
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
204
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
205
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
206
        $customExtension->getAccessMapping($admin)->willReturn(
207
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
208
        );
209
        $admin->addExtension($customExtension->reveal());
210
        $admin->setSecurityHandler($securityHandler->reveal());
211
212
        $this->assertFalse($admin->hasAccess('custom_action'));
213
    }
214
215
    public function testHasAccessAllowsAccess(): void
216
    {
217
        $admin = new PostAdmin(
218
            'sonata.post.admin.post',
219
            'Application\Sonata\NewsBundle\Entity\Post',
220
            'Sonata\NewsBundle\Controller\PostAdminController'
221
        );
222
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
223
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
224
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true);
225
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
226
        $customExtension->getAccessMapping($admin)->willReturn(
227
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
228
        );
229
        $admin->addExtension($customExtension->reveal());
230
        $admin->setSecurityHandler($securityHandler->reveal());
231
232
        $this->assertTrue($admin->hasAccess('custom_action'));
233
    }
234
235
    public function testHasAccessAllowsAccessEditAction(): void
236
    {
237
        $admin = new PostAdmin(
238
            'sonata.post.admin.post',
239
            'Application\Sonata\NewsBundle\Entity\Post',
240
            'Sonata\NewsBundle\Controller\PostAdminController'
241
        );
242
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
243
        $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true);
244
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
245
        $customExtension->getAccessMapping($admin)->willReturn(
246
            ['edit_action' => ['EDIT_ROLE']]
247
        );
248
        $admin->addExtension($customExtension->reveal());
249
        $admin->setSecurityHandler($securityHandler->reveal());
250
251
        $this->assertTrue($admin->hasAccess('edit_action'));
252
    }
253
254
    /**
255
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild
256
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild
257
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild
258
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild
259
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren
260
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren
261
     */
262
    public function testChildren(): void
263
    {
264
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
265
        $this->assertFalse($postAdmin->hasChildren());
266
        $this->assertFalse($postAdmin->hasChild('comment'));
267
268
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
269
        $postAdmin->addChild($commentAdmin, 'post');
270
271
        $this->assertTrue($postAdmin->hasChildren());
272
        $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment'));
273
274
        $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode());
275
        $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute());
276
        $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent());
277
        $this->assertSame('post', $commentAdmin->getParentAssociationMapping());
278
279
        $this->assertFalse($postAdmin->isChild());
280
        $this->assertTrue($commentAdmin->isChild());
281
282
        $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren());
283
    }
284
285
    /**
286
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure
287
     */
288
    public function testConfigure(): void
289
    {
290
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
291
        $this->assertNotNull($admin->getUniqid());
292
293
        $admin->initialize();
294
        $this->assertNotNull($admin->getUniqid());
295
        $this->assertSame('Post', $admin->getClassnameLabel());
296
297
        $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
298
        $admin->setClassnameLabel('postcomment');
299
300
        $admin->initialize();
301
        $this->assertSame('postcomment', $admin->getClassnameLabel());
302
    }
303
304
    public function testConfigureWithValidParentAssociationMapping(): void
305
    {
306
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
307
        $admin->setParentAssociationMapping('Category');
308
309
        $admin->initialize();
310
        $this->assertSame('Category', $admin->getParentAssociationMapping());
311
    }
312
313
    public function provideGetBaseRoutePattern()
314
    {
315
        return [
316
            [
317
                'Application\Sonata\NewsBundle\Entity\Post',
318
                '/sonata/news/post',
319
            ],
320
            [
321
                'Application\Sonata\NewsBundle\Document\Post',
322
                '/sonata/news/post',
323
            ],
324
            [
325
                'MyApplication\MyBundle\Entity\Post',
326
                '/myapplication/my/post',
327
            ],
328
            [
329
                'MyApplication\MyBundle\Entity\Post\Category',
330
                '/myapplication/my/post-category',
331
            ],
332
            [
333
                'MyApplication\MyBundle\Entity\Product\Category',
334
                '/myapplication/my/product-category',
335
            ],
336
            [
337
                'MyApplication\MyBundle\Entity\Other\Product\Category',
338
                '/myapplication/my/other-product-category',
339
            ],
340
            [
341
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
342
                '/cmf/foo/menu',
343
            ],
344
            [
345
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
346
                '/cmf/foo/menu',
347
            ],
348
            [
349
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
350
                '/symfony/barbar/menu',
351
            ],
352
            [
353
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
354
                '/symfony/barbar/menu-item',
355
            ],
356
            [
357
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
358
                '/cmf/foo/menu',
359
            ],
360
            [
361
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
362
                '/cmf/foo/menu',
363
            ],
364
            [
365
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
366
                '/cmf/foo/menu',
367
            ],
368
            [
369
                'AppBundle\Entity\User',
370
                '/app/user',
371
            ],
372
            [
373
                'App\Entity\User',
374
                '/app/user',
375
            ],
376
        ];
377
    }
378
379
    /**
380
     * @dataProvider provideGetBaseRoutePattern
381
     */
382
    public function testGetBaseRoutePattern(string $objFqn, string $expected): void
383
    {
384
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
385
        $this->assertSame($expected, $admin->getBaseRoutePattern());
386
    }
387
388
    /**
389
     * @dataProvider provideGetBaseRoutePattern
390
     */
391
    public function testGetBaseRoutePatternWithChildAdmin(string $objFqn, string $expected): void
392
    {
393
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
394
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
395
        $commentAdmin->setParent($postAdmin);
396
397
        $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern());
398
    }
399
400
    /**
401
     * @dataProvider provideGetBaseRoutePattern
402
     */
403
    public function testGetBaseRoutePatternWithTwoNestedChildAdmin(string $objFqn, string $expected): void
404
    {
405
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
406
        $commentAdmin = new CommentAdmin(
407
            'sonata.post.admin.comment',
408
            'Application\Sonata\NewsBundle\Entity\Comment',
409
            'Sonata\NewsBundle\Controller\CommentAdminController'
410
        );
411
        $commentVoteAdmin = new CommentVoteAdmin(
412
            'sonata.post.admin.comment_vote',
413
            'Application\Sonata\NewsBundle\Entity\CommentVote',
414
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
415
        );
416
        $commentAdmin->setParent($postAdmin);
417
        $commentVoteAdmin->setParent($commentAdmin);
418
419
        $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern());
420
    }
421
422
    public function testGetBaseRoutePatternWithSpecifedPattern(): void
423
    {
424
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostWithCustomRouteAdminController');
425
426
        $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern());
427
    }
428
429
    public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void
430
    {
431
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
432
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController');
433
        $commentAdmin->setParent($postAdmin);
434
435
        $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern());
436
    }
437
438
    public function testGetBaseRoutePatternWithUnreconizedClassname(): void
439
    {
440
        $this->expectException(\RuntimeException::class);
441
442
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
443
        $admin->getBaseRoutePattern();
444
    }
445
446
    public function provideGetBaseRouteName()
447
    {
448
        return [
449
            [
450
                'Application\Sonata\NewsBundle\Entity\Post',
451
                'admin_sonata_news_post',
452
            ],
453
            [
454
                'Application\Sonata\NewsBundle\Document\Post',
455
                'admin_sonata_news_post',
456
            ],
457
            [
458
                'MyApplication\MyBundle\Entity\Post',
459
                'admin_myapplication_my_post',
460
            ],
461
            [
462
                'MyApplication\MyBundle\Entity\Post\Category',
463
                'admin_myapplication_my_post_category',
464
            ],
465
            [
466
                'MyApplication\MyBundle\Entity\Product\Category',
467
                'admin_myapplication_my_product_category',
468
            ],
469
            [
470
                'MyApplication\MyBundle\Entity\Other\Product\Category',
471
                'admin_myapplication_my_other_product_category',
472
            ],
473
            [
474
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
475
                'admin_cmf_foo_menu',
476
            ],
477
            [
478
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
479
                'admin_cmf_foo_menu',
480
            ],
481
            [
482
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
483
                'admin_symfony_barbar_menu',
484
            ],
485
            [
486
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
487
                'admin_symfony_barbar_menu_item',
488
            ],
489
            [
490
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
491
                'admin_cmf_foo_menu',
492
            ],
493
            [
494
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
495
                'admin_cmf_foo_menu',
496
            ],
497
            [
498
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
499
                'admin_cmf_foo_menu',
500
            ],
501
            [
502
                'AppBundle\Entity\User',
503
                'admin_app_user',
504
            ],
505
            [
506
                'App\Entity\User',
507
                'admin_app_user',
508
            ],
509
        ];
510
    }
511
512
    /**
513
     * @dataProvider provideGetBaseRouteName
514
     */
515
    public function testGetBaseRouteName(string $objFqn, string $expected): void
516
    {
517
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
518
519
        $this->assertSame($expected, $admin->getBaseRouteName());
520
    }
521
522
    /**
523
     * @group legacy
524
     * @expectedDeprecation Calling "addChild" without second argument is deprecated since sonata-project/admin-bundle 3.35 and will not be allowed in 4.0.
525
     * @dataProvider provideGetBaseRouteName
526
     */
527
    public function testGetBaseRouteNameWithChildAdmin(string $objFqn, string $expected): void
528
    {
529
        $routeGenerator = new DefaultRouteGenerator(
530
            $this->createMock(RouterInterface::class),
531
            new RoutesCache($this->cacheTempFolder, true)
532
        );
533
534
        $container = new Container();
535
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
536
537
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
538
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
539
        $container->set('sonata.post.admin.post', $postAdmin);
540
        $postAdmin->setConfigurationPool($pool);
541
        $postAdmin->setRouteBuilder($pathInfo);
542
        $postAdmin->setRouteGenerator($routeGenerator);
543
        $postAdmin->initialize();
544
545
        $commentAdmin = new CommentAdmin(
546
            'sonata.post.admin.comment',
547
            'Application\Sonata\NewsBundle\Entity\Comment',
548
            'Sonata\NewsBundle\Controller\CommentAdminController'
549
        );
550
        $container->set('sonata.post.admin.comment', $commentAdmin);
551
        $commentAdmin->setConfigurationPool($pool);
552
        $commentAdmin->setRouteBuilder($pathInfo);
553
        $commentAdmin->setRouteGenerator($routeGenerator);
554
        $commentAdmin->initialize();
555
556
        $postAdmin->addChild($commentAdmin, 'post');
557
558
        $commentVoteAdmin = new CommentVoteAdmin(
559
            'sonata.post.admin.comment_vote',
560
            'Application\Sonata\NewsBundle\Entity\CommentVote',
561
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
562
        );
563
564
        $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin);
565
        $commentVoteAdmin->setConfigurationPool($pool);
566
        $commentVoteAdmin->setRouteBuilder($pathInfo);
567
        $commentVoteAdmin->setRouteGenerator($routeGenerator);
568
        $commentVoteAdmin->initialize();
569
570
        $commentAdmin->addChild($commentVoteAdmin);
571
        $pool->setAdminServiceIds([
572
            'sonata.post.admin.post',
573
            'sonata.post.admin.comment',
574
            'sonata.post.admin.comment_vote',
575
        ]);
576
577
        $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
578
579
        $this->assertTrue($postAdmin->hasRoute('show'));
580
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show'));
581
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show'));
582
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show'));
583
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
584
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list'));
585
        $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
586
        $this->assertFalse($commentAdmin->hasRoute('edit'));
587
        $this->assertSame('post', $commentAdmin->getParentAssociationMapping());
588
589
        /*
590
         * Test the route name from request
591
         */
592
        $postListRequest = new Request(
593
            [],
594
            [],
595
            [
596
                '_route' => $postAdmin->getBaseRouteName().'_list',
597
            ]
598
        );
599
600
        $postAdmin->setRequest($postListRequest);
601
        $commentAdmin->setRequest($postListRequest);
602
603
        $this->assertTrue($postAdmin->isCurrentRoute('list'));
604
        $this->assertFalse($postAdmin->isCurrentRoute('create'));
605
        $this->assertFalse($commentAdmin->isCurrentRoute('list'));
606
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('list'));
607
        $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
608
        $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
609
        $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
610
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
611
    }
612
613
    public function testGetBaseRouteNameWithUnreconizedClassname(): void
614
    {
615
        $this->expectException(\RuntimeException::class);
616
617
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
618
        $admin->getBaseRouteName();
619
    }
620
621
    public function testGetBaseRouteNameWithSpecifiedName(): void
622
    {
623
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
624
625
        $this->assertSame('post_custom', $postAdmin->getBaseRouteName());
626
    }
627
628
    public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void
629
    {
630
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
631
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController');
632
        $commentAdmin->setParent($postAdmin);
633
634
        $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName());
635
    }
636
637
    public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void
638
    {
639
        $postAdmin = new PostAdmin(
640
            'sonata.post.admin.post',
641
            'Application\Sonata\NewsBundle\Entity\Post',
642
            'Sonata\NewsBundle\Controller\PostAdminController'
643
        );
644
        $commentAdmin = new CommentWithCustomRouteAdmin(
645
            'sonata.post.admin.comment_with_custom_route',
646
            'Application\Sonata\NewsBundle\Entity\Comment',
647
            'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'
648
        );
649
        $commentVoteAdmin = new CommentVoteAdmin(
650
            'sonata.post.admin.comment_vote',
651
            'Application\Sonata\NewsBundle\Entity\CommentVote',
652
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
653
        );
654
        $commentAdmin->setParent($postAdmin);
655
        $commentVoteAdmin->setParent($commentAdmin);
656
657
        $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName());
658
    }
659
660
    /**
661
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid
662
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid
663
     */
664
    public function testSetUniqid(): void
665
    {
666
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
667
668
        $uniqid = uniqid();
669
        $admin->setUniqid($uniqid);
670
671
        $this->assertSame($uniqid, $admin->getUniqid());
672
    }
673
674
    public function testToString(): void
675
    {
676
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
677
678
        $s = new \stdClass();
679
680
        $this->assertNotEmpty($admin->toString($s));
681
682
        $s = new FooToString();
683
        $this->assertSame('salut', $admin->toString($s));
684
685
        // To string method is implemented, but returns null
686
        $s = new FooToStringNull();
687
        $this->assertNotEmpty($admin->toString($s));
688
689
        $this->assertSame('', $admin->toString(false));
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
690
    }
691
692
    public function testIsAclEnabled(): void
693
    {
694
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
695
696
        $this->assertFalse($postAdmin->isAclEnabled());
697
698
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
699
        $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class));
700
        $this->assertTrue($commentAdmin->isAclEnabled());
701
    }
702
703
    /**
704
     * @group legacy
705
     *
706
     * @expectedDeprecation Calling Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. Use Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass() to know if there is an active subclass.
707
     *
708
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses
709
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass
710
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses
711
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass
712
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
713
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass
714
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode
715
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass
716
     */
717
    public function testSubClass(): void
718
    {
719
        // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations
720
        $admin = new PostAdmin(
721
            'sonata.post.admin.post',
722
            Post::class,
723
            'Sonata\NewsBundle\Controller\PostAdminController'
724
        );
725
        $this->assertFalse($admin->hasSubClass('test'));
726
        $this->assertFalse($admin->hasActiveSubClass());
727
        $this->assertCount(0, $admin->getSubClasses());
728
        $this->assertNull($admin->getActiveSubClass());
729
        $this->assertNull($admin->getActiveSubclassCode());
730
        $this->assertSame(Post::class, $admin->getClass());
731
732
        // Just for the record, if there is no inheritance set, the getSubject is not used
733
        // the getSubject can also lead to some issue
734
        $admin->setSubject(new BlogPost());
735
        $this->assertSame(BlogPost::class, $admin->getClass());
736
737
        $admin->setSubClasses([
738
            'extended1' => 'NewsBundle\Entity\PostExtended1',
739
            'extended2' => 'NewsBundle\Entity\PostExtended2',
740
        ]);
741
        $this->assertFalse($admin->hasSubClass('test'));
742
        $this->assertTrue($admin->hasSubClass('extended1'));
743
        $this->assertFalse($admin->hasActiveSubClass());
744
        $this->assertCount(2, $admin->getSubClasses());
745
        // NEXT_MAJOR: remove the following 2 `assertNull()` assertions
746
        $this->assertNull($admin->getActiveSubClass());
747
        $this->assertNull($admin->getActiveSubclassCode());
748
        $this->assertSame(
749
            BlogPost::class,
750
            $admin->getClass(),
751
            'When there is no subclass in the query the class parameter should be returned'
752
        );
753
754
        $request = new Request(['subclass' => 'extended1']);
755
        $admin->setRequest($request);
756
        $this->assertFalse($admin->hasSubClass('test'));
757
        $this->assertTrue($admin->hasSubClass('extended1'));
758
        $this->assertTrue($admin->hasActiveSubClass());
759
        $this->assertCount(2, $admin->getSubClasses());
760
        $this->assertSame(
761
            'NewsBundle\Entity\PostExtended1',
762
            $admin->getActiveSubClass(),
763
            'It should return the curently active sub class.'
764
        );
765
        $this->assertSame('extended1', $admin->getActiveSubclassCode());
766
        $this->assertSame(
767
            'NewsBundle\Entity\PostExtended1',
768
            $admin->getClass(),
769
            'getClass() should return the name of the sub class when passed through a request query parameter.'
770
        );
771
772
        $request->query->set('subclass', 'inject');
773
774
        $this->assertNull($admin->getActiveSubclassCode());
775
        // NEXT_MAJOR: remove the previous `assertNull()` assertion and uncomment the following lines
776
        // $this->expectException(\LogicException::class);
777
        // $this->expectExceptionMessage(sprintf('Admin "%s" has no active subclass.', PostAdmin::class));
778
    }
779
780
    /**
781
     * @group legacy
782
     *
783
     * @expectedDeprecation Calling Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. Use Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass() to know if there is an active subclass.
784
     */
785
    public function testNonExistantSubclass(): void
786
    {
787
        // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations
788
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
789
        $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class));
790
791
        $admin->setRequest(new Request(['subclass' => 'inject']));
792
793
        $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']);
794
795
        $this->assertTrue($admin->hasActiveSubClass());
796
797
        $this->expectException(\RuntimeException::class);
798
799
        $admin->getActiveSubClass();
800
    }
801
802
    /**
803
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
804
     */
805
    public function testOnlyOneSubclassNeededToBeActive(): void
806
    {
807
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
808
        $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']);
809
        $request = new Request(['subclass' => 'extended1']);
810
        $admin->setRequest($request);
811
        $this->assertTrue($admin->hasActiveSubClass());
812
    }
813
814
    /**
815
     * @group legacy
816
     * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0.
817
     */
818
    public function testAddSubClassIsDeprecated(): void
819
    {
820
        $admin = new PostAdmin(
821
            'sonata.post.admin.post',
822
            Post::class,
823
            'Sonata\NewsBundle\Controller\PostAdminController'
824
        );
825
        $admin->addSubClass('whatever');
826
    }
827
828
    public function testGetPerPageOptions(): void
829
    {
830
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
831
832
        $perPageOptions = $admin->getPerPageOptions();
833
834
        foreach ($perPageOptions as $perPage) {
835
            $this->assertSame(0, $perPage % 4);
836
        }
837
838
        $admin->setPerPageOptions([500, 1000]);
839
        $this->assertSame([500, 1000], $admin->getPerPageOptions());
840
    }
841
842
    public function testGetLabelTranslatorStrategy(): void
843
    {
844
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
845
846
        $this->assertNull($admin->getLabelTranslatorStrategy());
847
848
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
849
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
850
        $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy());
851
    }
852
853
    public function testGetRouteBuilder(): void
854
    {
855
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
856
857
        $this->assertNull($admin->getRouteBuilder());
858
859
        $routeBuilder = $this->createMock(RouteBuilderInterface::class);
860
        $admin->setRouteBuilder($routeBuilder);
861
        $this->assertSame($routeBuilder, $admin->getRouteBuilder());
862
    }
863
864
    public function testGetMenuFactory(): void
865
    {
866
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
867
868
        $this->assertNull($admin->getMenuFactory());
869
870
        $menuFactory = $this->createMock(FactoryInterface::class);
871
        $admin->setMenuFactory($menuFactory);
872
        $this->assertSame($menuFactory, $admin->getMenuFactory());
873
    }
874
875
    public function testGetExtensions(): void
876
    {
877
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
878
879
        $this->assertSame([], $admin->getExtensions());
880
881
        $adminExtension1 = $this->createMock(AdminExtensionInterface::class);
882
        $adminExtension2 = $this->createMock(AdminExtensionInterface::class);
883
884
        $admin->addExtension($adminExtension1);
885
        $admin->addExtension($adminExtension2);
886
        $this->assertSame([$adminExtension1, $adminExtension2], $admin->getExtensions());
887
    }
888
889
    public function testGetFilterTheme(): void
890
    {
891
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
892
893
        $this->assertSame([], $admin->getFilterTheme());
894
895
        $admin->setFilterTheme(['FooTheme']);
896
        $this->assertSame(['FooTheme'], $admin->getFilterTheme());
897
    }
898
899
    public function testGetFormTheme(): void
900
    {
901
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
902
903
        $this->assertSame([], $admin->getFormTheme());
904
905
        $admin->setFormTheme(['FooTheme']);
906
907
        $this->assertSame(['FooTheme'], $admin->getFormTheme());
908
    }
909
910
    public function testGetValidator(): void
911
    {
912
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
913
914
        $this->assertNull($admin->getValidator());
915
916
        $validator = $this->getMockForAbstractClass(ValidatorInterface::class);
917
918
        $admin->setValidator($validator);
919
        $this->assertSame($validator, $admin->getValidator());
920
    }
921
922
    public function testGetSecurityHandler(): void
923
    {
924
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
925
926
        $this->assertNull($admin->getSecurityHandler());
927
928
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
929
        $admin->setSecurityHandler($securityHandler);
930
        $this->assertSame($securityHandler, $admin->getSecurityHandler());
931
    }
932
933
    public function testGetSecurityInformation(): void
934
    {
935
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
936
937
        $this->assertSame([], $admin->getSecurityInformation());
938
939
        $securityInformation = [
940
            'GUEST' => ['VIEW', 'LIST'],
941
            'STAFF' => ['EDIT', 'LIST', 'CREATE'],
942
        ];
943
944
        $admin->setSecurityInformation($securityInformation);
945
        $this->assertSame($securityInformation, $admin->getSecurityInformation());
946
    }
947
948
    public function testGetManagerType(): void
949
    {
950
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
951
952
        $this->assertNull($admin->getManagerType());
953
954
        $admin->setManagerType('foo_orm');
955
        $this->assertSame('foo_orm', $admin->getManagerType());
956
    }
957
958
    public function testGetModelManager(): void
959
    {
960
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
961
962
        $this->assertNull($admin->getModelManager());
963
964
        $modelManager = $this->createMock(ModelManagerInterface::class);
965
966
        $admin->setModelManager($modelManager);
967
        $this->assertSame($modelManager, $admin->getModelManager());
968
    }
969
970
    /**
971
     * NEXT_MAJOR: remove this method.
972
     *
973
     * @group legacy
974
     */
975
    public function testGetBaseCodeRoute(): void
976
    {
977
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
978
979
        $this->assertSame('', $admin->getBaseCodeRoute());
980
981
        $admin->setBaseCodeRoute('foo');
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...min::setBaseCodeRoute() has been deprecated with message: This method is deprecated since sonata-project/admin-bundle 3.24 and will be removed in 4.0

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...
982
        $this->assertSame('foo', $admin->getBaseCodeRoute());
983
    }
984
985
    // NEXT_MAJOR: uncomment this method.
986
    // public function testGetBaseCodeRoute()
987
    // {
988
    //     $postAdmin = new PostAdmin(
989
    //         'sonata.post.admin.post',
990
    //         'NewsBundle\Entity\Post',
991
    //         'Sonata\NewsBundle\Controller\PostAdminController'
992
    //     );
993
    //     $commentAdmin = new CommentAdmin(
994
    //         'sonata.post.admin.comment',
995
    //         'Application\Sonata\NewsBundle\Entity\Comment',
996
    //         'Sonata\NewsBundle\Controller\CommentAdminController'
997
    //     );
998
    //
999
    //     $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute());
1000
    //
1001
    //     $postAdmin->addChild($commentAdmin);
1002
    //
1003
    //     $this->assertSame(
1004
    //         'sonata.post.admin.post|sonata.post.admin.comment',
1005
    //         $commentAdmin->getBaseCodeRoute()
1006
    //     );
1007
    // }
1008
1009
    public function testGetRouteGenerator(): void
1010
    {
1011
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1012
1013
        $this->assertNull($admin->getRouteGenerator());
1014
1015
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
1016
1017
        $admin->setRouteGenerator($routeGenerator);
1018
        $this->assertSame($routeGenerator, $admin->getRouteGenerator());
1019
    }
1020
1021
    public function testGetConfigurationPool(): void
1022
    {
1023
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1024
1025
        $this->assertNull($admin->getConfigurationPool());
1026
1027
        $pool = $this->getMockBuilder(Pool::class)
1028
            ->disableOriginalConstructor()
1029
            ->getMock();
1030
1031
        $admin->setConfigurationPool($pool);
1032
        $this->assertSame($pool, $admin->getConfigurationPool());
1033
    }
1034
1035
    public function testGetShowBuilder(): void
1036
    {
1037
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1038
1039
        $this->assertNull($admin->getShowBuilder());
1040
1041
        $showBuilder = $this->createMock(ShowBuilderInterface::class);
1042
1043
        $admin->setShowBuilder($showBuilder);
1044
        $this->assertSame($showBuilder, $admin->getShowBuilder());
1045
    }
1046
1047
    public function testGetListBuilder(): void
1048
    {
1049
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1050
1051
        $this->assertNull($admin->getListBuilder());
1052
1053
        $listBuilder = $this->createMock(ListBuilderInterface::class);
1054
1055
        $admin->setListBuilder($listBuilder);
1056
        $this->assertSame($listBuilder, $admin->getListBuilder());
1057
    }
1058
1059
    public function testGetDatagridBuilder(): void
1060
    {
1061
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1062
1063
        $this->assertNull($admin->getDatagridBuilder());
1064
1065
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
1066
1067
        $admin->setDatagridBuilder($datagridBuilder);
1068
        $this->assertSame($datagridBuilder, $admin->getDatagridBuilder());
1069
    }
1070
1071
    public function testGetFormContractor(): void
1072
    {
1073
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1074
1075
        $this->assertNull($admin->getFormContractor());
1076
1077
        $formContractor = $this->createMock(FormContractorInterface::class);
1078
1079
        $admin->setFormContractor($formContractor);
1080
        $this->assertSame($formContractor, $admin->getFormContractor());
1081
    }
1082
1083
    public function testGetRequest(): void
1084
    {
1085
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1086
1087
        $this->assertFalse($admin->hasRequest());
1088
1089
        $request = new Request();
1090
1091
        $admin->setRequest($request);
1092
        $this->assertSame($request, $admin->getRequest());
1093
        $this->assertTrue($admin->hasRequest());
1094
    }
1095
1096
    public function testGetRequestWithException(): void
1097
    {
1098
        $this->expectException(\RuntimeException::class);
1099
        $this->expectExceptionMessage('The Request object has not been set');
1100
1101
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1102
        $admin->getRequest();
1103
    }
1104
1105
    public function testGetTranslationDomain(): void
1106
    {
1107
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1108
1109
        $this->assertSame('messages', $admin->getTranslationDomain());
1110
1111
        $admin->setTranslationDomain('foo');
1112
        $this->assertSame('foo', $admin->getTranslationDomain());
1113
    }
1114
1115
    /**
1116
     * @group legacy
1117
     */
1118
    public function testGetTranslator(): void
1119
    {
1120
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1121
1122
        $this->assertNull($admin->getTranslator());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::getTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1123
1124
        $translator = $this->createMock(TranslatorInterface::class);
1125
1126
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1127
        $this->assertSame($translator, $admin->getTranslator());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::getTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1128
    }
1129
1130
    public function testGetShowGroups(): void
1131
    {
1132
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1133
1134
        // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call.
1135
        $this->assertFalse($admin->getShowGroups('sonata_deprecation_mute'));
1136
1137
        $groups = ['foo', 'bar', 'baz'];
1138
1139
        $admin->setShowGroups($groups);
1140
        $this->assertSame($groups, $admin->getShowGroups());
1141
    }
1142
1143
    public function testGetFormGroups(): void
1144
    {
1145
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1146
1147
        // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call.
1148
        $this->assertFalse($admin->getFormGroups('sonata_deprecation_mute'));
1149
1150
        $groups = ['foo', 'bar', 'baz'];
1151
1152
        $admin->setFormGroups($groups);
1153
        $this->assertSame($groups, $admin->getFormGroups());
1154
    }
1155
1156
    public function testGetMaxPageLinks(): void
1157
    {
1158
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1159
1160
        $this->assertSame(25, $admin->getMaxPageLinks());
1161
1162
        $admin->setMaxPageLinks(14);
1163
        $this->assertSame(14, $admin->getMaxPageLinks());
1164
    }
1165
1166
    public function testGetMaxPerPage(): void
1167
    {
1168
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1169
1170
        $this->assertSame(32, $admin->getMaxPerPage());
1171
1172
        $admin->setMaxPerPage(94);
1173
        $this->assertSame(94, $admin->getMaxPerPage());
1174
    }
1175
1176
    public function testGetLabel(): void
1177
    {
1178
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1179
1180
        $this->assertNull($admin->getLabel());
1181
1182
        $admin->setLabel('FooLabel');
1183
        $this->assertSame('FooLabel', $admin->getLabel());
1184
    }
1185
1186
    public function testGetBaseController(): void
1187
    {
1188
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1189
1190
        $this->assertSame('Sonata\NewsBundle\Controller\PostAdminController', $admin->getBaseControllerName());
1191
1192
        $admin->setBaseControllerName('Sonata\NewsBundle\Controller\FooAdminController');
1193
        $this->assertSame('Sonata\NewsBundle\Controller\FooAdminController', $admin->getBaseControllerName());
1194
    }
1195
1196
    public function testGetTemplates(): void
1197
    {
1198
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1199
1200
        $templates = [
1201
            'list' => '@FooAdmin/CRUD/list.html.twig',
1202
            'show' => '@FooAdmin/CRUD/show.html.twig',
1203
            'edit' => '@FooAdmin/CRUD/edit.html.twig',
1204
        ];
1205
1206
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1207
        $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates);
1208
1209
        $admin->setTemplateRegistry($templateRegistry->reveal());
1210
1211
        $this->assertSame($templates, $admin->getTemplates());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...ctAdmin::getTemplates() has been deprecated with message: since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services 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...
1212
    }
1213
1214
    public function testGetTemplate1(): void
1215
    {
1216
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1217
1218
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1219
        $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig');
1220
        $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig');
1221
1222
        $admin->setTemplateRegistry($templateRegistry->reveal());
1223
1224
        $this->assertSame('@FooAdmin/CRUD/edit.html.twig', $admin->getTemplate('edit'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services 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...
1225
        $this->assertSame('@FooAdmin/CRUD/show.html.twig', $admin->getTemplate('show'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services 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...
1226
    }
1227
1228
    public function testGetIdParameter(): void
1229
    {
1230
        $postAdmin = new PostAdmin(
1231
            'sonata.post.admin.post',
1232
            'NewsBundle\Entity\Post',
1233
            'Sonata\NewsBundle\Controller\PostAdminController'
1234
        );
1235
1236
        $this->assertSame('id', $postAdmin->getIdParameter());
1237
        $this->assertFalse($postAdmin->isChild());
1238
1239
        $commentAdmin = new CommentAdmin(
1240
            'sonata.post.admin.comment',
1241
            'Application\Sonata\NewsBundle\Entity\Comment',
1242
            'Sonata\NewsBundle\Controller\CommentAdminController'
1243
        );
1244
        $commentAdmin->setParent($postAdmin);
1245
1246
        $this->assertTrue($commentAdmin->isChild());
1247
        $this->assertSame('childId', $commentAdmin->getIdParameter());
1248
1249
        $commentVoteAdmin = new CommentVoteAdmin(
1250
            'sonata.post.admin.comment_vote',
1251
            'Application\Sonata\NewsBundle\Entity\CommentVote',
1252
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
1253
        );
1254
        $commentVoteAdmin->setParent($commentAdmin);
1255
1256
        $this->assertTrue($commentVoteAdmin->isChild());
1257
        $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter());
1258
    }
1259
1260
    public function testGetExportFormats(): void
1261
    {
1262
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1263
1264
        $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats());
1265
    }
1266
1267
    public function testGetUrlsafeIdentifier(): void
1268
    {
1269
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1270
1271
        $entity = new \stdClass();
1272
1273
        $modelManager = $this->createMock(ModelManagerInterface::class);
1274
        $modelManager->expects($this->once())
1275
            ->method('getUrlSafeIdentifier')
1276
            ->with($this->equalTo($entity))
1277
            ->willReturn('foo');
1278
        $admin->setModelManager($modelManager);
1279
1280
        $this->assertSame('foo', $admin->getUrlSafeIdentifier($entity));
1281
    }
1282
1283
    public function testDeterminedPerPageValue(): void
1284
    {
1285
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1286
1287
        $this->assertFalse($admin->determinedPerPageValue('foo'));
1288
        $this->assertFalse($admin->determinedPerPageValue(123));
1289
        $this->assertTrue($admin->determinedPerPageValue(16));
1290
1291
        $admin->setPerPageOptions([101, 102, 103]);
1292
        $this->assertFalse($admin->determinedPerPageValue(16));
1293
        $this->assertTrue($admin->determinedPerPageValue(101));
1294
    }
1295
1296
    public function testIsGranted(): void
1297
    {
1298
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1299
        $modelManager = $this->createStub(ModelManagerInterface::class);
1300
        $modelManager
1301
            ->method('getNormalizedIdentifier')
1302
            ->willReturnCallback(static function (?object $entity = null): ?string {
1303
                return $entity ? $entity->id : null;
1304
            });
1305
1306
        $admin->setModelManager($modelManager);
1307
1308
        $entity1 = new \stdClass();
1309
        $entity1->id = '1';
1310
1311
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1312
        $securityHandler
1313
            ->expects($this->exactly(6))
1314
            ->method('isGranted')
1315
            ->willReturnCallback(static function (
1316
                AdminInterface $adminIn,
1317
                string $attributes,
1318
                ?object $object = null
1319
            ) use (
1320
                $admin,
1321
                $entity1
1322
            ): bool {
1323
                return $admin === $adminIn && 'FOO' === $attributes &&
1324
                    ($object === $admin || $object === $entity1);
1325
            });
1326
1327
        $admin->setSecurityHandler($securityHandler);
1328
1329
        $this->assertTrue($admin->isGranted('FOO'));
1330
        $this->assertTrue($admin->isGranted('FOO'));
1331
        $this->assertTrue($admin->isGranted('FOO', $entity1));
1332
        $this->assertTrue($admin->isGranted('FOO', $entity1));
1333
        $this->assertFalse($admin->isGranted('BAR'));
1334
        $this->assertFalse($admin->isGranted('BAR'));
1335
        $this->assertFalse($admin->isGranted('BAR', $entity1));
1336
        $this->assertFalse($admin->isGranted('BAR', $entity1));
1337
1338
        $entity2 = new \stdClass();
1339
        $entity2->id = '2';
1340
1341
        $this->assertFalse($admin->isGranted('BAR', $entity2));
1342
        $this->assertFalse($admin->isGranted('BAR', $entity2));
1343
1344
        $entity3 = new \stdClass();
1345
        $entity3->id = '3';
1346
1347
        $this->assertFalse($admin->isGranted('BAR', $entity3));
1348
        $this->assertFalse($admin->isGranted('BAR', $entity3));
1349
    }
1350
1351
    public function testSupportsPreviewMode(): void
1352
    {
1353
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1354
1355
        $this->assertFalse($admin->supportsPreviewMode());
1356
    }
1357
1358
    public function testGetPermissionsShow(): void
1359
    {
1360
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1361
1362
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
1363
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
1364
        $this->assertSame(['LIST'], $admin->getPermissionsShow('foo'));
1365
    }
1366
1367
    public function testShowIn(): void
1368
    {
1369
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1370
1371
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1372
        $securityHandler
1373
            ->method('isGranted')
1374
            ->willReturnCallback(static function (AdminInterface $adminIn, array $attributes, $object = null) use ($admin): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1375
                return $admin === $adminIn && $attributes === ['LIST'];
1376
            });
1377
1378
        $admin->setSecurityHandler($securityHandler);
1379
1380
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
1381
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
1382
        $this->assertTrue($admin->showIn('foo'));
1383
    }
1384
1385
    public function testGetObjectIdentifier(): void
1386
    {
1387
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1388
1389
        $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
1390
    }
1391
1392
    /**
1393
     * @group legacy
1394
     */
1395
    public function testTrans(): void
1396
    {
1397
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1398
        $admin->setTranslationDomain('fooMessageDomain');
1399
1400
        $translator = $this->createMock(TranslatorInterface::class);
1401
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1402
1403
        $translator->expects($this->once())
1404
            ->method('trans')
1405
            ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1406
            ->willReturn('fooTranslated');
1407
1408
        $this->assertSame('fooTranslated', $admin->trans('foo'));
1409
    }
1410
1411
    /**
1412
     * @group legacy
1413
     */
1414
    public function testTransWithMessageDomain(): void
1415
    {
1416
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1417
1418
        $translator = $this->createMock(TranslatorInterface::class);
1419
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1420
1421
        $translator->expects($this->once())
1422
            ->method('trans')
1423
            ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1424
            ->willReturn('fooTranslated');
1425
1426
        $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain'));
1427
    }
1428
1429
    /**
1430
     * @group legacy
1431
     */
1432
    public function testTransChoice(): void
1433
    {
1434
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1435
        $admin->setTranslationDomain('fooMessageDomain');
1436
1437
        $translator = $this->createMock(TranslatorInterface::class);
1438
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1439
1440
        $translator->expects($this->once())
1441
            ->method('transChoice')
1442
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1443
            ->willReturn('fooTranslated');
1444
1445
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1446
    }
1447
1448
    /**
1449
     * @group legacy
1450
     */
1451
    public function testTransChoiceWithMessageDomain(): void
1452
    {
1453
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1454
1455
        $translator = $this->createMock(TranslatorInterface::class);
1456
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1457
1458
        $translator->expects($this->once())
1459
            ->method('transChoice')
1460
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1461
            ->willReturn('fooTranslated');
1462
1463
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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...
1464
    }
1465
1466
    public function testSetFilterPersister(): void
1467
    {
1468
        $admin = new class('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle\Controller\PostAdminController') extends PostAdmin {
1469
            public function persistFilters(): bool
1470
            {
1471
                return $this->persistFilters;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\AdminBundle\Admin...tAdmin::$persistFilters has been deprecated with message: since sonata-project/admin-bundle 3.34, to be removed in 4.0.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
1472
            }
1473
        };
1474
1475
        $filterPersister = $this->createMock(FilterPersisterInterface::class);
1476
1477
        $admin->setFilterPersister($filterPersister);
1478
        $this->assertTrue($admin->persistFilters());
1479
    }
1480
1481
    public function testGetRootCode(): void
1482
    {
1483
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1484
1485
        $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
1486
1487
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController');
1488
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1489
        $parentFieldDescription->expects($this->once())
1490
            ->method('getAdmin')
1491
            ->willReturn($parentAdmin);
1492
1493
        $this->assertNull($admin->getParentFieldDescription());
1494
        $admin->setParentFieldDescription($parentFieldDescription);
1495
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1496
        $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
1497
    }
1498
1499
    public function testGetRoot(): void
1500
    {
1501
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1502
1503
        $this->assertSame($admin, $admin->getRoot());
1504
1505
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController');
1506
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1507
        $parentFieldDescription->expects($this->once())
1508
            ->method('getAdmin')
1509
            ->willReturn($parentAdmin);
1510
1511
        $this->assertNull($admin->getParentFieldDescription());
1512
        $admin->setParentFieldDescription($parentFieldDescription);
1513
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1514
        $this->assertSame($parentAdmin, $admin->getRoot());
1515
    }
1516
1517
    public function testGetExportFields(): void
1518
    {
1519
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1520
1521
        $modelManager = $this->createMock(ModelManagerInterface::class);
1522
        $modelManager->expects($this->once())
1523
            ->method('getExportFields')
1524
            ->with($this->equalTo('NewsBundle\Entity\Post'))
1525
            ->willReturn(['foo', 'bar']);
1526
1527
        $admin->setModelManager($modelManager);
1528
        $this->assertSame(['foo', 'bar'], $admin->getExportFields());
1529
    }
1530
1531
    public function testGetPersistentParametersWithNoExtension(): void
1532
    {
1533
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1534
1535
        $this->assertEmpty($admin->getPersistentParameters());
1536
    }
1537
1538
    public function testGetPersistentParametersWithInvalidExtension(): void
1539
    {
1540
        $this->expectException(\RuntimeException::class);
1541
1542
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1543
1544
        $extension = $this->createMock(AdminExtensionInterface::class);
1545
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn(null);
1546
1547
        $admin->addExtension($extension);
1548
1549
        $admin->getPersistentParameters();
1550
    }
1551
1552
    public function testGetPersistentParametersWithValidExtension(): void
1553
    {
1554
        $expected = [
1555
            'context' => 'foobar',
1556
        ];
1557
1558
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1559
1560
        $extension = $this->createMock(AdminExtensionInterface::class);
1561
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn($expected);
1562
1563
        $admin->addExtension($extension);
1564
1565
        $this->assertSame($expected, $admin->getPersistentParameters());
1566
    }
1567
1568
    public function testGetFormWithNonCollectionParentValue(): void
1569
    {
1570
        $post = new Post();
1571
        $tagAdmin = $this->createTagAdmin($post);
1572
        $tag = $tagAdmin->getSubject();
1573
1574
        $tag->setPosts(null);
1575
        $tagAdmin->getForm();
1576
        $this->assertSame($post, $tag->getPosts());
1577
    }
1578
1579
    public function testGetFormWithCollectionParentValue(): void
1580
    {
1581
        $post = new Post();
1582
        $tagAdmin = $this->createTagAdmin($post);
1583
        $tag = $tagAdmin->getSubject();
1584
1585
        // Case of a doctrine collection
1586
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1587
        $this->assertCount(0, $tag->getPosts());
1588
1589
        $tag->addPost(new Post());
1590
1591
        $this->assertCount(1, $tag->getPosts());
1592
1593
        $tagAdmin->getForm();
1594
1595
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1596
        $this->assertCount(2, $tag->getPosts());
1597
        $this->assertContains($post, $tag->getPosts());
1598
1599
        // Case of an array
1600
        $tag->setPosts([]);
1601
        $this->assertCount(0, $tag->getPosts());
1602
1603
        $tag->addPost(new Post());
1604
1605
        $this->assertCount(1, $tag->getPosts());
1606
1607
        $tagAdmin->getForm();
1608
1609
        $this->assertIsArray($tag->getPosts());
1610
        $this->assertCount(2, $tag->getPosts());
1611
        $this->assertContains($post, $tag->getPosts());
1612
    }
1613
1614
    public function testFormAddPostSubmitEventForPreValidation(): void
1615
    {
1616
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1617
        $object = new \stdClass();
1618
1619
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
1620
        $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy);
1621
1622
        $validator = $this->createMock(ValidatorInterface::class);
1623
        $validator
1624
                ->method('getMetadataFor')
1625
                ->willReturn($this->createMock(MemberMetadata::class));
1626
        $modelAdmin->setValidator($validator);
1627
1628
        $modelManager = $this->createMock(ModelManagerInterface::class);
1629
        $modelManager
1630
            ->method('getNewFieldDescriptionInstance')
1631
            ->willReturn(new FieldDescription());
1632
        $modelAdmin->setModelManager($modelManager);
1633
1634
        // a Admin class to test that preValidate is called
1635
        $testAdminPreValidate = $this->createMock(AbstractAdmin::class);
1636
        $testAdminPreValidate->expects($this->once())
1637
                ->method('preValidate')
1638
                ->with($this->identicalTo($object));
1639
1640
        $event = $this->createMock(FormEvent::class);
1641
        $event
1642
                ->method('getData')
1643
                ->willReturn($object);
1644
1645
        $formBuild = $this->createMock(FormBuilder::class);
1646
        $formBuild->expects($this->once())
1647
                ->method('addEventListener')
1648
                ->with(
1649
                    $this->identicalTo(FormEvents::POST_SUBMIT),
1650
                    $this->callback(static function ($callback) use ($testAdminPreValidate, $event): bool {
1651
                        if (\is_callable($callback)) {
1652
                            $closure = $callback->bindTo($testAdminPreValidate);
0 ignored issues
show
Bug introduced by
The method bindTo cannot be called on $callback (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
1653
                            $closure($event);
1654
1655
                            return true;
1656
                        }
1657
1658
                        return false;
1659
                    }),
1660
                    $this->greaterThan(0)
1661
                );
1662
1663
        $formContractor = $this->createMock(FormContractorInterface::class);
1664
        $formContractor
1665
                ->method('getDefaultOptions')
1666
                ->willReturn([]);
1667
        $formContractor
1668
                ->method('getFormBuilder')
1669
                ->willReturn($formBuild);
1670
1671
        $modelAdmin->setFormContractor($formContractor);
1672
        $modelAdmin->defineFormBuilder($formBuild);
1673
        $modelAdmin->getForm();
1674
    }
1675
1676
    public function testRemoveFieldFromFormGroup(): void
1677
    {
1678
        $formGroups = [
1679
            'foobar' => [
1680
                'fields' => [
1681
                    'foo' => 'foo',
1682
                    'bar' => 'bar',
1683
                ],
1684
            ],
1685
        ];
1686
1687
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1688
        $admin->setFormGroups($formGroups);
1689
1690
        $admin->removeFieldFromFormGroup('foo');
1691
        $this->assertSame($admin->getFormGroups(), [
1692
            'foobar' => [
1693
                'fields' => [
1694
                    'bar' => 'bar',
1695
                ],
1696
            ],
1697
        ]);
1698
1699
        $admin->removeFieldFromFormGroup('bar');
1700
        $this->assertSame($admin->getFormGroups(), []);
1701
    }
1702
1703
    public function testGetFilterParameters(): void
1704
    {
1705
        $authorId = uniqid();
1706
1707
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1708
1709
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
1710
        $commentAdmin->setParentAssociationMapping('post.author');
1711
        $commentAdmin->setParent($postAdmin);
1712
1713
        $request = $this->createMock(Request::class);
1714
        $query = $this->createMock(ParameterBag::class);
1715
        $query
1716
            ->method('get')
1717
            ->willReturn([
1718
                'filter' => [
1719
                    '_page' => '1',
1720
                    '_per_page' => '32',
1721
                ],
1722
            ]);
1723
1724
        $request->query = $query;
1725
        $request
1726
            ->method('get')
1727
            ->willReturn($authorId);
1728
1729
        $commentAdmin->setRequest($request);
1730
1731
        $modelManager = $this->createMock(ModelManagerInterface::class);
1732
        $modelManager
1733
            ->method('getDefaultSortValues')
1734
            ->willReturn([]);
1735
1736
        $commentAdmin->setModelManager($modelManager);
1737
1738
        $parameters = $commentAdmin->getFilterParameters();
1739
1740
        $this->assertTrue(isset($parameters['post__author']));
1741
        $this->assertSame(['value' => $authorId], $parameters['post__author']);
1742
    }
1743
1744
    public function testGetFilterFieldDescription(): void
1745
    {
1746
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1747
1748
        $fooFieldDescription = new FieldDescription();
1749
        $barFieldDescription = new FieldDescription();
1750
        $bazFieldDescription = new FieldDescription();
1751
1752
        $modelManager = $this->createMock(ModelManagerInterface::class);
1753
        $modelManager->expects($this->exactly(3))
1754
            ->method('getNewFieldDescriptionInstance')
1755
            ->willReturnCallback(static function ($adminClass, string $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) {
0 ignored issues
show
Unused Code introduced by
The parameter $filterOptions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1756
                switch ($name) {
1757
                    case 'foo':
1758
                        $fieldDescription = $fooFieldDescription;
1759
1760
                        break;
1761
1762
                    case 'bar':
1763
                        $fieldDescription = $barFieldDescription;
1764
1765
                        break;
1766
1767
                    case 'baz':
1768
                        $fieldDescription = $bazFieldDescription;
1769
1770
                        break;
1771
1772
                    default:
1773
                        throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
1774
                        break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1775
                }
1776
1777
                $fieldDescription->setName($name);
1778
1779
                return $fieldDescription;
1780
            });
1781
1782
        $modelAdmin->setModelManager($modelManager);
1783
1784
        $pager = $this->createMock(PagerInterface::class);
1785
1786
        $datagrid = $this->createMock(DatagridInterface::class);
1787
        $datagrid->expects($this->once())
1788
            ->method('getPager')
1789
            ->willReturn($pager);
1790
1791
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
1792
        $datagridBuilder->expects($this->once())
1793
            ->method('getBaseDatagrid')
1794
            ->with($this->identicalTo($modelAdmin), [])
1795
            ->willReturn($datagrid);
1796
1797
        $datagridBuilder->expects($this->exactly(3))
1798
            ->method('addFilter')
1799
            ->willReturnCallback(static function ($datagrid, $type, $fieldDescription, AdminInterface $admin): void {
1800
                $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
1801
                $fieldDescription->mergeOption('field_options', ['required' => false]);
1802
            });
1803
1804
        $modelAdmin->setDatagridBuilder($datagridBuilder);
1805
1806
        $this->assertSame(['foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription], $modelAdmin->getFilterFieldDescriptions());
1807
        $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
1808
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
1809
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
1810
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
1811
        $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
1812
        $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
1813
        $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
1814
    }
1815
1816
    public function testGetSubjectNoRequest(): void
1817
    {
1818
        $modelManager = $this->createMock(ModelManagerInterface::class);
1819
        $modelManager
1820
            ->expects($this->never())
1821
            ->method('find');
1822
1823
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1824
        $admin->setModelManager($modelManager);
1825
1826
        $this->assertNull($admin->getSubject());
1827
    }
1828
1829
    public function testGetSideMenu(): void
1830
    {
1831
        $item = $this->createMock(ItemInterface::class);
1832
        $item
1833
            ->expects($this->once())
1834
            ->method('setChildrenAttribute')
1835
            ->with('class', 'nav navbar-nav');
1836
        $item
1837
            ->expects($this->once())
1838
            ->method('setExtra')
1839
            ->with('translation_domain', 'foo_bar_baz');
1840
1841
        $menuFactory = $this->createMock(FactoryInterface::class);
1842
        $menuFactory
1843
            ->expects($this->once())
1844
            ->method('createItem')
1845
            ->willReturn($item);
1846
1847
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1848
        $modelAdmin->setMenuFactory($menuFactory);
1849
        $modelAdmin->setTranslationDomain('foo_bar_baz');
1850
1851
        $modelAdmin->getSideMenu('foo');
1852
    }
1853
1854
    /**
1855
     * @return array
1856
     */
1857
    public function provideGetSubject()
1858
    {
1859
        return [
1860
            [23],
1861
            ['azerty'],
1862
            ['4f69bbb5f14a13347f000092'],
1863
            ['0779ca8d-e2be-11e4-ac58-0242ac11000b'],
1864
            ['123'.AdapterInterface::ID_SEPARATOR.'my_type'], // composite keys are supported
1865
        ];
1866
    }
1867
1868
    /**
1869
     * @dataProvider provideGetSubject
1870
     */
1871
    public function testGetSubjectFailed($id): void
1872
    {
1873
        $modelManager = $this->createMock(ModelManagerInterface::class);
1874
        $modelManager
1875
            ->expects($this->once())
1876
            ->method('find')
1877
            ->with('NewsBundle\Entity\Post', $id)
1878
            ->willReturn(null); // entity not found
1879
1880
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1881
        $admin->setModelManager($modelManager);
1882
1883
        $admin->setRequest(new Request(['id' => $id]));
1884
        $this->assertNull($admin->getSubject());
1885
    }
1886
1887
    /**
1888
     * @dataProvider provideGetSubject
1889
     */
1890
    public function testGetSubject($id): void
1891
    {
1892
        $entity = new Post();
1893
1894
        $modelManager = $this->createMock(ModelManagerInterface::class);
1895
        $modelManager
1896
            ->expects($this->once())
1897
            ->method('find')
1898
            ->with('NewsBundle\Entity\Post', $id)
1899
            ->willReturn($entity);
1900
1901
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1902
        $admin->setModelManager($modelManager);
1903
1904
        $admin->setRequest(new Request(['id' => $id]));
1905
        $this->assertSame($entity, $admin->getSubject());
1906
        $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
1907
    }
1908
1909
    public function testGetSubjectWithParentDescription(): void
1910
    {
1911
        $adminId = 1;
1912
1913
        $comment = new Comment();
1914
1915
        $modelManager = $this->createMock(ModelManagerInterface::class);
1916
        $modelManager
1917
            ->method('find')
1918
            ->with('NewsBundle\Entity\Comment', $adminId)
1919
            ->willReturn($comment);
1920
1921
        $request = new Request(['id' => $adminId]);
1922
1923
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1924
        $postAdmin->setRequest($request);
1925
1926
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
1927
        $commentAdmin->setRequest($request);
1928
        $commentAdmin->setModelManager($modelManager);
1929
1930
        $this->assertSame($comment, $commentAdmin->getSubject());
1931
1932
        $commentAdmin->setSubject(null);
1933
        $commentAdmin->setParentFieldDescription(new FieldDescription());
1934
1935
        $this->assertNull($commentAdmin->getSubject());
1936
    }
1937
1938
    /**
1939
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1940
     */
1941
    public function testGetActionButtonsList(): void
1942
    {
1943
        $expected = [
1944
            'create' => [
1945
                'template' => 'Foo.html.twig',
1946
            ],
1947
        ];
1948
1949
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1950
1951
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1952
        $templateRegistry->getTemplate('button_create')->willReturn('Foo.html.twig');
1953
1954
        $admin->setTemplateRegistry($templateRegistry->reveal());
1955
1956
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
1957
        $securityHandler
1958
            ->expects($this->once())
1959
            ->method('isGranted')
1960
            ->with($admin, 'CREATE', $admin)
1961
            ->willReturn(true);
1962
        $admin->setSecurityHandler($securityHandler);
1963
1964
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
1965
        $routeGenerator
1966
            ->expects($this->once())
1967
            ->method('hasAdminRoute')
1968
            ->with($admin, 'create')
1969
            ->willReturn(true);
1970
        $admin->setRouteGenerator($routeGenerator);
1971
1972
        $this->assertSame($expected, $admin->getActionButtons('list', null));
1973
    }
1974
1975
    /**
1976
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1977
     */
1978
    public function testGetActionButtonsListCreateDisabled(): void
1979
    {
1980
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1981
1982
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
1983
        $securityHandler
1984
            ->expects($this->once())
1985
            ->method('isGranted')
1986
            ->with($admin, 'CREATE', $admin)
1987
            ->willReturn(false);
1988
        $admin->setSecurityHandler($securityHandler);
1989
1990
        $this->assertSame([], $admin->getActionButtons('list', null));
1991
    }
1992
1993
    /**
1994
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
1995
     */
1996
    public function testGetBatchActions(): void
1997
    {
1998
        $expected = [
1999
            'delete' => [
2000
                'label' => 'action_delete',
2001
                'translation_domain' => 'SonataAdminBundle',
2002
                'ask_confirmation' => true, // by default always true
2003
            ],
2004
            'foo' => [
2005
                'label' => 'action_foo',
2006
                'translation_domain' => 'SonataAdminBundle',
2007
            ],
2008
            'bar' => [
2009
                'label' => 'batch.label_bar',
2010
                'translation_domain' => 'SonataAdminBundle',
2011
            ],
2012
            'baz' => [
2013
                'label' => 'action_baz',
2014
                'translation_domain' => 'AcmeAdminBundle',
2015
            ],
2016
        ];
2017
2018
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2019
2020
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
2021
        $labelTranslatorStrategy
2022
            ->method('getLabel')
2023
            ->willReturnCallback(static function (string $label, string $context = '', string $type = ''): string {
2024
                return $context.'.'.$type.'_'.$label;
2025
            });
2026
2027
        $admin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
2028
        $admin->setRouteBuilder($pathInfo);
2029
        $admin->setTranslationDomain('SonataAdminBundle');
2030
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
2031
2032
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
2033
        $routeGenerator
2034
            ->expects($this->once())
2035
            ->method('hasAdminRoute')
2036
            ->with($admin, 'delete')
2037
            ->willReturn(true);
2038
        $admin->setRouteGenerator($routeGenerator);
2039
2040
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2041
        $securityHandler
2042
            ->method('isGranted')
2043
            ->willReturnCallback(static function (AdminInterface $adminIn, string $attributes, $object = null) use ($admin): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2044
                return $admin === $adminIn && 'DELETE' === $attributes;
2045
            });
2046
        $admin->setSecurityHandler($securityHandler);
2047
2048
        $this->assertSame($expected, $admin->getBatchActions());
2049
    }
2050
2051
    /**
2052
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2053
     */
2054
    public function testShowMosaicButton(): void
2055
    {
2056
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
2057
        $listModes = $admin->getListModes();
2058
2059
        $admin->showMosaicButton(true);
2060
2061
        $this->assertSame($listModes, $admin->getListModes());
2062
    }
2063
2064
    /**
2065
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2066
     */
2067
    public function testShowMosaicButtonHideMosaic(): void
2068
    {
2069
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
2070
        $listModes = $admin->getListModes();
2071
        $expected['list'] = $listModes['list'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$expected was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expected = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
2072
2073
        $admin->showMosaicButton(false);
2074
2075
        $this->assertSame($expected, $admin->getListModes());
2076
    }
2077
2078
    /**
2079
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
2080
     * @dataProvider provideGetBaseRouteName
2081
     */
2082
    public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void
2083
    {
2084
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2085
2086
        $routeGenerator = new DefaultRouteGenerator(
2087
            $this->createMock(RouterInterface::class),
2088
            new RoutesCache($this->cacheTempFolder, true)
2089
        );
2090
2091
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
2092
        $admin->setRouteBuilder($pathInfo);
2093
        $admin->setRouteGenerator($routeGenerator);
2094
        $admin->initialize();
2095
2096
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
2097
        $templateRegistry->getTemplate('action_create')->willReturn('Foo.html.twig');
2098
2099
        $admin->setTemplateRegistry($templateRegistry->reveal());
2100
2101
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2102
        $securityHandler
2103
            ->method('isGranted')
2104
            ->willReturnCallback(static function (AdminInterface $adminIn, string $attributes, $object = null) use ($admin): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2105
                return $admin === $adminIn && ('CREATE' === $attributes || 'LIST' === $attributes);
2106
            });
2107
2108
        $admin->setSecurityHandler($securityHandler);
2109
2110
        $this->assertArrayHasKey('list', $admin->getDashboardActions());
2111
        $this->assertArrayHasKey('create', $admin->getDashboardActions());
2112
    }
2113
2114
    public function testDefaultFilters(): void
2115
    {
2116
        $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
2117
2118
        $subjectId = uniqid();
2119
2120
        $request = $this->createMock(Request::class);
2121
        $query = $this->createMock(ParameterBag::class);
2122
        $query
2123
            ->method('get')
2124
            ->with($this->equalTo('filter'))
2125
            ->willReturn([
2126
                'a' => [
2127
                    'value' => 'b',
2128
                ],
2129
                'foo' => [
2130
                    'type' => '1',
2131
                    'value' => 'bar',
2132
                ],
2133
                'baz' => [
2134
                    'type' => '5',
2135
                    'value' => 'test',
2136
                ],
2137
            ]);
2138
        $request->query = $query;
2139
2140
        $request
2141
            ->method('get')
2142
            ->willReturn($subjectId);
2143
2144
        $admin->setRequest($request);
2145
2146
        $modelManager = $this->createMock(ModelManagerInterface::class);
2147
        $modelManager
2148
            ->method('getDefaultSortValues')
2149
            ->willReturn([]);
2150
2151
        $admin->setModelManager($modelManager);
2152
2153
        $this->assertSame([
2154
            '_page' => 1,
2155
            '_per_page' => 32,
2156
            'foo' => [
2157
                'type' => '1',
2158
                'value' => 'bar',
2159
            ],
2160
            'baz' => [
2161
                'type' => '5',
2162
                'value' => 'test',
2163
            ],
2164
            'a' => [
2165
                'value' => 'b',
2166
            ],
2167
        ], $admin->getFilterParameters());
2168
2169
        $this->assertTrue($admin->isDefaultFilter('foo'));
2170
        $this->assertFalse($admin->isDefaultFilter('bar'));
2171
        $this->assertFalse($admin->isDefaultFilter('a'));
2172
    }
2173
2174
    /**
2175
     * @group legacy
2176
     */
2177
    public function testDefaultBreadcrumbsBuilder(): void
2178
    {
2179
        $container = $this->createMock(ContainerInterface::class);
2180
        $container->expects($this->once())
2181
            ->method('getParameter')
2182
            ->with('sonata.admin.configuration.breadcrumbs')
2183
            ->willReturn([]);
2184
2185
        $pool = $this->getMockBuilder(Pool::class)
2186
            ->disableOriginalConstructor()
2187
            ->getMock();
2188
        $pool->expects($this->once())
2189
            ->method('getContainer')
2190
            ->willReturn($container);
2191
2192
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2193
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2194
        ], '', true, true, true, ['getConfigurationPool']);
2195
        $admin->expects($this->once())
2196
            ->method('getConfigurationPool')
2197
            ->willReturn($pool);
2198
2199
        $this->assertInstanceOf(BreadcrumbsBuilder::class, $admin->getBreadcrumbsBuilder());
2200
    }
2201
2202
    /**
2203
     * @group legacy
2204
     */
2205
    public function testBreadcrumbsBuilderSetter(): void
2206
    {
2207
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2208
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2209
        ]);
2210
        $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->createMock(
2211
            BreadcrumbsBuilderInterface::class
2212
        )));
2213
        $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
2214
    }
2215
2216
    /**
2217
     * @group legacy
2218
     */
2219
    public function testGetBreadcrumbs(): void
2220
    {
2221
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2222
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2223
        ]);
2224
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2225
        $action = 'myaction';
2226
        $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
2227
        $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
2228
    }
2229
2230
    /**
2231
     * @group legacy
2232
     */
2233
    public function testBuildBreadcrumbs(): void
2234
    {
2235
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2236
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2237
        ]);
2238
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2239
        $action = 'myaction';
2240
        $menu = $this->createMock(ItemInterface::class);
2241
        $builder->buildBreadcrumbs($admin, $action, $menu)
2242
            ->shouldBeCalledTimes(1)
2243
            ->willReturn($menu);
2244
        $admin->setBreadcrumbsBuilder($builder->reveal());
2245
2246
        /* check the called is proxied only once */
2247
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2248
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2249
    }
2250
2251
    /**
2252
     * NEXT_MAJOR: remove this method.
2253
     *
2254
     * @group legacy
2255
     */
2256
    public function testCreateQueryLegacyCallWorks(): void
2257
    {
2258
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2259
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2260
        ]);
2261
        $query = $this->createMock(ProxyQueryInterface::class);
2262
        $modelManager = $this->createMock(ModelManagerInterface::class);
2263
        $modelManager->expects($this->once())
2264
            ->method('createQuery')
2265
            ->with('My\Class')
2266
            ->willReturn($query);
2267
2268
        $admin->setModelManager($modelManager);
2269
        $this->assertSame($query, $admin->createQuery('list'));
2270
    }
2271
2272
    public function testGetDataSourceIterator(): void
2273
    {
2274
        $datagrid = $this->createMock(DatagridInterface::class);
2275
        $datagrid->method('buildPager');
2276
2277
        $modelManager = $this->createMock(ModelManagerInterface::class);
2278
        $modelManager->method('getExportFields')->willReturn([
2279
            'field',
2280
            'foo',
2281
            'bar',
2282
        ]);
2283
        $modelManager->expects($this->once())->method('getDataSourceIterator')
2284
            ->with($this->equalTo($datagrid), $this->equalTo([
2285
                'Feld' => 'field',
2286
                1 => 'foo',
2287
                2 => 'bar',
2288
            ]));
2289
2290
        $admin = $this->getMockBuilder(AbstractAdmin::class)
2291
            ->disableOriginalConstructor()
2292
            ->setMethods(['getDatagrid', 'getTranslationLabel', 'trans'])
2293
            ->getMockForAbstractClass();
2294
        $admin->method('getDatagrid')->willReturn($datagrid);
2295
        $admin->setModelManager($modelManager);
2296
2297
        $admin
2298
            ->method('getTranslationLabel')
2299
            ->willReturnCallback(static function (string $label, string $context = '', string $type = ''): string {
2300
                return $context.'.'.$type.'_'.$label;
2301
            });
2302
        $admin
2303
            ->method('trans')
2304
            ->willReturnCallback(static function (string $label): string {
2305
                if ('export.label_field' === $label) {
2306
                    return 'Feld';
2307
                }
2308
2309
                return $label;
2310
            });
2311
2312
        $admin->getDataSourceIterator();
2313
    }
2314
2315
    public function testCircularChildAdmin(): void
2316
    {
2317
        $this->expectException(\RuntimeException::class);
2318
        $this->expectExceptionMessage(
2319
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment` admin.'
2320
        );
2321
2322
        $postAdmin = new PostAdmin(
2323
            'sonata.post.admin.post',
2324
            'Application\Sonata\NewsBundle\Entity\Post',
2325
            'Sonata\NewsBundle\Controller\PostAdminController'
2326
        );
2327
        $commentAdmin = new CommentAdmin(
2328
            'sonata.post.admin.comment',
2329
            'Application\Sonata\NewsBundle\Entity\Comment',
2330
            'Sonata\NewsBundle\Controller\CommentAdminController'
2331
        );
2332
        $postAdmin->addChild($commentAdmin, 'post');
2333
        $commentAdmin->addChild($postAdmin, 'comment');
2334
    }
2335
2336
    public function testCircularChildAdminTripleLevel(): void
2337
    {
2338
        $this->expectException(\RuntimeException::class);
2339
        $this->expectExceptionMessage(
2340
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment_vote` admin.'
2341
        );
2342
2343
        $postAdmin = new PostAdmin(
2344
            'sonata.post.admin.post',
2345
            'Application\Sonata\NewsBundle\Entity\Post',
2346
            'Sonata\NewsBundle\Controller\PostAdminController'
2347
        );
2348
        $commentAdmin = new CommentAdmin(
2349
            'sonata.post.admin.comment',
2350
            'Application\Sonata\NewsBundle\Entity\Comment',
2351
            'Sonata\NewsBundle\Controller\CommentAdminController'
2352
        );
2353
        $commentVoteAdmin = new CommentVoteAdmin(
2354
            'sonata.post.admin.comment_vote',
2355
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2356
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2357
        );
2358
        $postAdmin->addChild($commentAdmin, 'post');
2359
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2360
        $commentVoteAdmin->addChild($postAdmin, 'post');
2361
    }
2362
2363
    public function testCircularChildAdminWithItself(): void
2364
    {
2365
        $this->expectException(\RuntimeException::class);
2366
        $this->expectExceptionMessage(
2367
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.post` admin.'
2368
        );
2369
2370
        $postAdmin = new PostAdmin(
2371
            'sonata.post.admin.post',
2372
            'Application\Sonata\NewsBundle\Entity\Post',
2373
            'Sonata\NewsBundle\Controller\PostAdminController'
2374
        );
2375
        $postAdmin->addChild($postAdmin);
2376
    }
2377
2378
    public function testGetRootAncestor(): void
2379
    {
2380
        $postAdmin = new PostAdmin(
2381
            'sonata.post.admin.post',
2382
            'Application\Sonata\NewsBundle\Entity\Post',
2383
            'Sonata\NewsBundle\Controller\PostAdminController'
2384
        );
2385
        $commentAdmin = new CommentAdmin(
2386
            'sonata.post.admin.comment',
2387
            'Application\Sonata\NewsBundle\Entity\Comment',
2388
            'Sonata\NewsBundle\Controller\CommentAdminController'
2389
        );
2390
        $commentVoteAdmin = new CommentVoteAdmin(
2391
            'sonata.post.admin.comment_vote',
2392
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2393
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2394
        );
2395
2396
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2397
        $this->assertSame($commentAdmin, $commentAdmin->getRootAncestor());
2398
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2399
2400
        $postAdmin->addChild($commentAdmin, 'post');
2401
2402
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2403
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2404
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2405
2406
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2407
2408
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2409
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2410
        $this->assertSame($postAdmin, $commentVoteAdmin->getRootAncestor());
2411
    }
2412
2413
    public function testGetChildDepth(): void
2414
    {
2415
        $postAdmin = new PostAdmin(
2416
            'sonata.post.admin.post',
2417
            'Application\Sonata\NewsBundle\Entity\Post',
2418
            'Sonata\NewsBundle\Controller\PostAdminController'
2419
        );
2420
        $commentAdmin = new CommentAdmin(
2421
            'sonata.post.admin.comment',
2422
            'Application\Sonata\NewsBundle\Entity\Comment',
2423
            'Sonata\NewsBundle\Controller\CommentAdminController'
2424
        );
2425
        $commentVoteAdmin = new CommentVoteAdmin(
2426
            'sonata.post.admin.comment_vote',
2427
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2428
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2429
        );
2430
2431
        $this->assertSame(0, $postAdmin->getChildDepth());
2432
        $this->assertSame(0, $commentAdmin->getChildDepth());
2433
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2434
2435
        $postAdmin->addChild($commentAdmin, 'post');
2436
2437
        $this->assertSame(0, $postAdmin->getChildDepth());
2438
        $this->assertSame(1, $commentAdmin->getChildDepth());
2439
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2440
2441
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2442
2443
        $this->assertSame(0, $postAdmin->getChildDepth());
2444
        $this->assertSame(1, $commentAdmin->getChildDepth());
2445
        $this->assertSame(2, $commentVoteAdmin->getChildDepth());
2446
    }
2447
2448
    public function testGetCurrentLeafChildAdmin(): void
2449
    {
2450
        $postAdmin = new PostAdmin(
2451
            'sonata.post.admin.post',
2452
            'Application\Sonata\NewsBundle\Entity\Post',
2453
            'Sonata\NewsBundle\Controller\PostAdminController'
2454
        );
2455
        $commentAdmin = new CommentAdmin(
2456
            'sonata.post.admin.comment',
2457
            'Application\Sonata\NewsBundle\Entity\Comment',
2458
            'Sonata\NewsBundle\Controller\CommentAdminController'
2459
        );
2460
        $commentVoteAdmin = new CommentVoteAdmin(
2461
            'sonata.post.admin.comment_vote',
2462
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2463
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2464
        );
2465
2466
        $postAdmin->addChild($commentAdmin, 'post');
2467
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2468
2469
        $this->assertNull($postAdmin->getCurrentLeafChildAdmin());
2470
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2471
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2472
2473
        $commentAdmin->setCurrentChild(true);
2474
2475
        $this->assertSame($commentAdmin, $postAdmin->getCurrentLeafChildAdmin());
2476
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2477
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2478
2479
        $commentVoteAdmin->setCurrentChild(true);
2480
2481
        $this->assertSame($commentVoteAdmin, $postAdmin->getCurrentLeafChildAdmin());
2482
        $this->assertSame($commentVoteAdmin, $commentAdmin->getCurrentLeafChildAdmin());
2483
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2484
    }
2485
2486
    public function testAdminWithoutControllerName(): void
2487
    {
2488
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', null);
2489
2490
        $this->assertNull($admin->getBaseControllerName());
2491
    }
2492
2493
    /**
2494
     * NEXT_MAJOR: Remove this test and its data provider.
2495
     *
2496
     * @group legacy
2497
     *
2498
     * @dataProvider getDeprecatedAbstractAdminConstructorArgs
2499
     *
2500
     * @expectedDeprecation Passing other type than string%S as argument %d for method Sonata\AdminBundle\Admin\AbstractAdmin::__construct() is deprecated since sonata-project/admin-bundle 3.x. It will accept only string%S in version 4.0.
2501
     */
2502
    public function testDeprecatedAbstractAdminConstructorArgs($code, $class, $baseControllerName): void
2503
    {
2504
        new PostAdmin($code, $class, $baseControllerName);
2505
    }
2506
2507
    public function getDeprecatedAbstractAdminConstructorArgs(): iterable
2508
    {
2509
        yield from [
2510
            ['sonata.post.admin.post', null, null],
2511
            [null, null, null],
2512
            ['sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', false],
2513
            ['sonata.post.admin.post', false, false],
2514
            [false, false, false],
2515
            [true, true, true],
2516
            [new \stdClass(), new \stdClass(), new \stdClass()],
2517
            [0, 0, 0],
2518
            [1, 1, 1],
2519
        ];
2520
    }
2521
2522
    private function createTagAdmin(Post $post): TagAdmin
2523
    {
2524
        $postAdmin = $this->getMockBuilder(PostAdmin::class)
2525
            ->disableOriginalConstructor()
2526
            ->getMock();
2527
2528
        $postAdmin->method('getObject')->willReturn($post);
2529
2530
        $formBuilder = $this->createMock(FormBuilderInterface::class);
2531
        $formBuilder->method('getForm')->willReturn(null);
2532
2533
        $tagAdmin = $this->getMockBuilder(TagAdmin::class)
2534
            ->setConstructorArgs([
2535
                'admin.tag',
2536
                Tag::class,
2537
                'MyBundle\MyController',
2538
            ])
2539
            ->setMethods(['getFormBuilder'])
2540
            ->getMock();
2541
2542
        $tagAdmin->method('getFormBuilder')->willReturn($formBuilder);
2543
        $tagAdmin->setParent($postAdmin);
2544
2545
        $tag = new Tag();
2546
        $tagAdmin->setSubject($tag);
2547
2548
        $request = $this->createMock(Request::class);
2549
        $tagAdmin->setRequest($request);
2550
2551
        $configurationPool = $this->getMockBuilder(Pool::class)
2552
            ->disableOriginalConstructor()
2553
            ->getMock();
2554
2555
        $configurationPool->method('getPropertyAccessor')->willReturn(PropertyAccess::createPropertyAccessor());
2556
2557
        $tagAdmin->setConfigurationPool($configurationPool);
2558
2559
        return $tagAdmin;
2560
    }
2561
}
2562