Completed
Push — 3.x ( df94e1...036501 )
by Grégoire
02:53
created

AdminTest::testIsGranted()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 8.6925
c 0
b 0
f 0
cc 5
nc 1
nop 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\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
        $this->assertFalse($admin->getShowGroups());
1135
1136
        $groups = ['foo', 'bar', 'baz'];
1137
1138
        $admin->setShowGroups($groups);
1139
        $this->assertSame($groups, $admin->getShowGroups());
1140
    }
1141
1142
    public function testGetFormGroups(): void
1143
    {
1144
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1145
1146
        $this->assertFalse($admin->getFormGroups());
1147
1148
        $groups = ['foo', 'bar', 'baz'];
1149
1150
        $admin->setFormGroups($groups);
1151
        $this->assertSame($groups, $admin->getFormGroups());
1152
    }
1153
1154
    public function testGetMaxPageLinks(): void
1155
    {
1156
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1157
1158
        $this->assertSame(25, $admin->getMaxPageLinks());
1159
1160
        $admin->setMaxPageLinks(14);
1161
        $this->assertSame(14, $admin->getMaxPageLinks());
1162
    }
1163
1164
    public function testGetMaxPerPage(): void
1165
    {
1166
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1167
1168
        $this->assertSame(32, $admin->getMaxPerPage());
1169
1170
        $admin->setMaxPerPage(94);
1171
        $this->assertSame(94, $admin->getMaxPerPage());
1172
    }
1173
1174
    public function testGetLabel(): void
1175
    {
1176
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1177
1178
        $this->assertNull($admin->getLabel());
1179
1180
        $admin->setLabel('FooLabel');
1181
        $this->assertSame('FooLabel', $admin->getLabel());
1182
    }
1183
1184
    public function testGetBaseController(): void
1185
    {
1186
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1187
1188
        $this->assertSame('Sonata\NewsBundle\Controller\PostAdminController', $admin->getBaseControllerName());
1189
1190
        $admin->setBaseControllerName('Sonata\NewsBundle\Controller\FooAdminController');
1191
        $this->assertSame('Sonata\NewsBundle\Controller\FooAdminController', $admin->getBaseControllerName());
1192
    }
1193
1194
    public function testGetTemplates(): void
1195
    {
1196
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1197
1198
        $templates = [
1199
            'list' => '@FooAdmin/CRUD/list.html.twig',
1200
            'show' => '@FooAdmin/CRUD/show.html.twig',
1201
            'edit' => '@FooAdmin/CRUD/edit.html.twig',
1202
        ];
1203
1204
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1205
        $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates);
1206
1207
        $admin->setTemplateRegistry($templateRegistry->reveal());
1208
1209
        $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...
1210
    }
1211
1212
    public function testGetTemplate1(): void
1213
    {
1214
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1215
1216
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1217
        $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig');
1218
        $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig');
1219
1220
        $admin->setTemplateRegistry($templateRegistry->reveal());
1221
1222
        $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...
1223
        $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...
1224
    }
1225
1226
    public function testGetIdParameter(): void
1227
    {
1228
        $postAdmin = new PostAdmin(
1229
            'sonata.post.admin.post',
1230
            'NewsBundle\Entity\Post',
1231
            'Sonata\NewsBundle\Controller\PostAdminController'
1232
        );
1233
1234
        $this->assertSame('id', $postAdmin->getIdParameter());
1235
        $this->assertFalse($postAdmin->isChild());
1236
1237
        $commentAdmin = new CommentAdmin(
1238
            'sonata.post.admin.comment',
1239
            'Application\Sonata\NewsBundle\Entity\Comment',
1240
            'Sonata\NewsBundle\Controller\CommentAdminController'
1241
        );
1242
        $commentAdmin->setParent($postAdmin);
1243
1244
        $this->assertTrue($commentAdmin->isChild());
1245
        $this->assertSame('childId', $commentAdmin->getIdParameter());
1246
1247
        $commentVoteAdmin = new CommentVoteAdmin(
1248
            'sonata.post.admin.comment_vote',
1249
            'Application\Sonata\NewsBundle\Entity\CommentVote',
1250
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
1251
        );
1252
        $commentVoteAdmin->setParent($commentAdmin);
1253
1254
        $this->assertTrue($commentVoteAdmin->isChild());
1255
        $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter());
1256
    }
1257
1258
    public function testGetExportFormats(): void
1259
    {
1260
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1261
1262
        $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats());
1263
    }
1264
1265
    public function testGetUrlsafeIdentifier(): void
1266
    {
1267
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1268
1269
        $entity = new \stdClass();
1270
1271
        $modelManager = $this->createMock(ModelManagerInterface::class);
1272
        $modelManager->expects($this->once())
1273
            ->method('getUrlsafeIdentifier')
1274
            ->with($this->equalTo($entity))
1275
            ->willReturn('foo');
1276
        $admin->setModelManager($modelManager);
1277
1278
        $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity));
1279
    }
1280
1281
    public function testDeterminedPerPageValue(): void
1282
    {
1283
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1284
1285
        $this->assertFalse($admin->determinedPerPageValue('foo'));
1286
        $this->assertFalse($admin->determinedPerPageValue(123));
1287
        $this->assertTrue($admin->determinedPerPageValue(16));
1288
1289
        $admin->setPerPageOptions([101, 102, 103]);
1290
        $this->assertFalse($admin->determinedPerPageValue(16));
1291
        $this->assertTrue($admin->determinedPerPageValue(101));
1292
    }
1293
1294
    public function testIsGranted(): void
1295
    {
1296
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1297
        $modelManager = $this->createStub(ModelManagerInterface::class);
1298
        $modelManager
1299
            ->method('getNormalizedIdentifier')
1300
            ->willReturnCallback(static function (?object $entity = null): ?string {
1301
                return $entity ? $entity->id : null;
1302
            });
1303
1304
        $admin->setModelManager($modelManager);
1305
1306
        $entity1 = new \stdClass();
1307
        $entity1->id = '1';
1308
1309
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1310
        $securityHandler
1311
            ->expects($this->exactly(6))
1312
            ->method('isGranted')
1313
            ->willReturnCallback(static function (
1314
                AdminInterface $adminIn,
1315
                string $attributes,
1316
                ?object $object = null
1317
            ) use (
1318
                $admin,
1319
                $entity1
1320
            ): bool {
1321
                return $admin === $adminIn && 'FOO' === $attributes &&
1322
                    ($object === $admin || $object === $entity1);
1323
            });
1324
1325
        $admin->setSecurityHandler($securityHandler);
1326
1327
        $this->assertTrue($admin->isGranted('FOO'));
1328
        $this->assertTrue($admin->isGranted('FOO'));
1329
        $this->assertTrue($admin->isGranted('FOO', $entity1));
1330
        $this->assertTrue($admin->isGranted('FOO', $entity1));
1331
        $this->assertFalse($admin->isGranted('BAR'));
1332
        $this->assertFalse($admin->isGranted('BAR'));
1333
        $this->assertFalse($admin->isGranted('BAR', $entity1));
1334
        $this->assertFalse($admin->isGranted('BAR', $entity1));
1335
1336
        $entity2 = new \stdClass();
1337
        $entity2->id = '2';
1338
1339
        $this->assertFalse($admin->isGranted('BAR', $entity2));
1340
        $this->assertFalse($admin->isGranted('BAR', $entity2));
1341
1342
        $entity3 = new \stdClass();
1343
        $entity3->id = '3';
1344
1345
        $this->assertFalse($admin->isGranted('BAR', $entity3));
1346
        $this->assertFalse($admin->isGranted('BAR', $entity3));
1347
    }
1348
1349
    public function testSupportsPreviewMode(): void
1350
    {
1351
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1352
1353
        $this->assertFalse($admin->supportsPreviewMode());
1354
    }
1355
1356
    public function testGetPermissionsShow(): void
1357
    {
1358
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1359
1360
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
1361
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
1362
        $this->assertSame(['LIST'], $admin->getPermissionsShow('foo'));
1363
    }
1364
1365
    public function testShowIn(): void
1366
    {
1367
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1368
1369
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1370
        $securityHandler
1371
            ->method('isGranted')
1372
            ->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...
1373
                return $admin === $adminIn && $attributes === ['LIST'];
1374
            });
1375
1376
        $admin->setSecurityHandler($securityHandler);
1377
1378
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
1379
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
1380
        $this->assertTrue($admin->showIn('foo'));
1381
    }
1382
1383
    public function testGetObjectIdentifier(): void
1384
    {
1385
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1386
1387
        $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
1388
    }
1389
1390
    /**
1391
     * @group legacy
1392
     */
1393
    public function testTrans(): void
1394
    {
1395
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1396
        $admin->setTranslationDomain('fooMessageDomain');
1397
1398
        $translator = $this->createMock(TranslatorInterface::class);
1399
        $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...
1400
1401
        $translator->expects($this->once())
1402
            ->method('trans')
1403
            ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1404
            ->willReturn('fooTranslated');
1405
1406
        $this->assertSame('fooTranslated', $admin->trans('foo'));
1407
    }
1408
1409
    /**
1410
     * @group legacy
1411
     */
1412
    public function testTransWithMessageDomain(): void
1413
    {
1414
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1415
1416
        $translator = $this->createMock(TranslatorInterface::class);
1417
        $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...
1418
1419
        $translator->expects($this->once())
1420
            ->method('trans')
1421
            ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1422
            ->willReturn('fooTranslated');
1423
1424
        $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain'));
1425
    }
1426
1427
    /**
1428
     * @group legacy
1429
     */
1430
    public function testTransChoice(): void
1431
    {
1432
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1433
        $admin->setTranslationDomain('fooMessageDomain');
1434
1435
        $translator = $this->createMock(TranslatorInterface::class);
1436
        $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...
1437
1438
        $translator->expects($this->once())
1439
            ->method('transChoice')
1440
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1441
            ->willReturn('fooTranslated');
1442
1443
        $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...
1444
    }
1445
1446
    /**
1447
     * @group legacy
1448
     */
1449
    public function testTransChoiceWithMessageDomain(): void
1450
    {
1451
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1452
1453
        $translator = $this->createMock(TranslatorInterface::class);
1454
        $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...
1455
1456
        $translator->expects($this->once())
1457
            ->method('transChoice')
1458
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1459
            ->willReturn('fooTranslated');
1460
1461
        $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...
1462
    }
1463
1464
    public function testSetFilterPersister(): void
1465
    {
1466
        $admin = new class('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle\Controller\PostAdminController') extends PostAdmin {
1467
            public function persistFilters(): bool
1468
            {
1469
                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...
1470
            }
1471
        };
1472
1473
        $filterPersister = $this->createMock(FilterPersisterInterface::class);
1474
1475
        $admin->setFilterPersister($filterPersister);
1476
        $this->assertTrue($admin->persistFilters());
1477
    }
1478
1479
    public function testGetRootCode(): void
1480
    {
1481
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1482
1483
        $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
1484
1485
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController');
1486
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1487
        $parentFieldDescription->expects($this->once())
1488
            ->method('getAdmin')
1489
            ->willReturn($parentAdmin);
1490
1491
        $this->assertNull($admin->getParentFieldDescription());
1492
        $admin->setParentFieldDescription($parentFieldDescription);
1493
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1494
        $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
1495
    }
1496
1497
    public function testGetRoot(): void
1498
    {
1499
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1500
1501
        $this->assertSame($admin, $admin->getRoot());
1502
1503
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController');
1504
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1505
        $parentFieldDescription->expects($this->once())
1506
            ->method('getAdmin')
1507
            ->willReturn($parentAdmin);
1508
1509
        $this->assertNull($admin->getParentFieldDescription());
1510
        $admin->setParentFieldDescription($parentFieldDescription);
1511
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1512
        $this->assertSame($parentAdmin, $admin->getRoot());
1513
    }
1514
1515
    public function testGetExportFields(): void
1516
    {
1517
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1518
1519
        $modelManager = $this->createMock(ModelManagerInterface::class);
1520
        $modelManager->expects($this->once())
1521
            ->method('getExportFields')
1522
            ->with($this->equalTo('NewsBundle\Entity\Post'))
1523
            ->willReturn(['foo', 'bar']);
1524
1525
        $admin->setModelManager($modelManager);
1526
        $this->assertSame(['foo', 'bar'], $admin->getExportFields());
1527
    }
1528
1529
    public function testGetPersistentParametersWithNoExtension(): void
1530
    {
1531
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1532
1533
        $this->assertEmpty($admin->getPersistentParameters());
1534
    }
1535
1536
    public function testGetPersistentParametersWithInvalidExtension(): void
1537
    {
1538
        $this->expectException(\RuntimeException::class);
1539
1540
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1541
1542
        $extension = $this->createMock(AdminExtensionInterface::class);
1543
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn(null);
1544
1545
        $admin->addExtension($extension);
1546
1547
        $admin->getPersistentParameters();
1548
    }
1549
1550
    public function testGetPersistentParametersWithValidExtension(): void
1551
    {
1552
        $expected = [
1553
            'context' => 'foobar',
1554
        ];
1555
1556
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1557
1558
        $extension = $this->createMock(AdminExtensionInterface::class);
1559
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn($expected);
1560
1561
        $admin->addExtension($extension);
1562
1563
        $this->assertSame($expected, $admin->getPersistentParameters());
1564
    }
1565
1566
    public function testGetFormWithNonCollectionParentValue(): void
1567
    {
1568
        $post = new Post();
1569
        $tagAdmin = $this->createTagAdmin($post);
1570
        $tag = $tagAdmin->getSubject();
1571
1572
        $tag->setPosts(null);
1573
        $tagAdmin->getForm();
1574
        $this->assertSame($post, $tag->getPosts());
1575
    }
1576
1577
    public function testGetFormWithCollectionParentValue(): void
1578
    {
1579
        $post = new Post();
1580
        $tagAdmin = $this->createTagAdmin($post);
1581
        $tag = $tagAdmin->getSubject();
1582
1583
        // Case of a doctrine collection
1584
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1585
        $this->assertCount(0, $tag->getPosts());
1586
1587
        $tag->addPost(new Post());
1588
1589
        $this->assertCount(1, $tag->getPosts());
1590
1591
        $tagAdmin->getForm();
1592
1593
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1594
        $this->assertCount(2, $tag->getPosts());
1595
        $this->assertContains($post, $tag->getPosts());
1596
1597
        // Case of an array
1598
        $tag->setPosts([]);
1599
        $this->assertCount(0, $tag->getPosts());
1600
1601
        $tag->addPost(new Post());
1602
1603
        $this->assertCount(1, $tag->getPosts());
1604
1605
        $tagAdmin->getForm();
1606
1607
        $this->assertIsArray($tag->getPosts());
1608
        $this->assertCount(2, $tag->getPosts());
1609
        $this->assertContains($post, $tag->getPosts());
1610
    }
1611
1612
    public function testFormAddPostSubmitEventForPreValidation(): void
1613
    {
1614
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1615
        $object = new \stdClass();
1616
1617
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
1618
        $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy);
1619
1620
        $validator = $this->createMock(ValidatorInterface::class);
1621
        $validator
1622
                ->method('getMetadataFor')
1623
                ->willReturn($this->createMock(MemberMetadata::class));
1624
        $modelAdmin->setValidator($validator);
1625
1626
        $modelManager = $this->createMock(ModelManagerInterface::class);
1627
        $modelManager
1628
            ->method('getNewFieldDescriptionInstance')
1629
            ->willReturn(new FieldDescription());
1630
        $modelAdmin->setModelManager($modelManager);
1631
1632
        // a Admin class to test that preValidate is called
1633
        $testAdminPreValidate = $this->createMock(AbstractAdmin::class);
1634
        $testAdminPreValidate->expects($this->once())
1635
                ->method('preValidate')
1636
                ->with($this->identicalTo($object));
1637
1638
        $event = $this->createMock(FormEvent::class);
1639
        $event
1640
                ->method('getData')
1641
                ->willReturn($object);
1642
1643
        $formBuild = $this->createMock(FormBuilder::class);
1644
        $formBuild->expects($this->once())
1645
                ->method('addEventListener')
1646
                ->with(
1647
                    $this->identicalTo(FormEvents::POST_SUBMIT),
1648
                    $this->callback(static function ($callback) use ($testAdminPreValidate, $event): bool {
1649
                        if (\is_callable($callback)) {
1650
                            $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...
1651
                            $closure($event);
1652
1653
                            return true;
1654
                        }
1655
1656
                        return false;
1657
                    }),
1658
                    $this->greaterThan(0)
1659
                );
1660
1661
        $formContractor = $this->createMock(FormContractorInterface::class);
1662
        $formContractor
1663
                ->method('getDefaultOptions')
1664
                ->willReturn([]);
1665
        $formContractor
1666
                ->method('getFormBuilder')
1667
                ->willReturn($formBuild);
1668
1669
        $modelAdmin->setFormContractor($formContractor);
1670
        $modelAdmin->defineFormBuilder($formBuild);
1671
        $modelAdmin->getForm();
1672
    }
1673
1674
    public function testRemoveFieldFromFormGroup(): void
1675
    {
1676
        $formGroups = [
1677
            'foobar' => [
1678
                'fields' => [
1679
                    'foo' => 'foo',
1680
                    'bar' => 'bar',
1681
                ],
1682
            ],
1683
        ];
1684
1685
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1686
        $admin->setFormGroups($formGroups);
1687
1688
        $admin->removeFieldFromFormGroup('foo');
1689
        $this->assertSame($admin->getFormGroups(), [
1690
            'foobar' => [
1691
                'fields' => [
1692
                    'bar' => 'bar',
1693
                ],
1694
            ],
1695
        ]);
1696
1697
        $admin->removeFieldFromFormGroup('bar');
1698
        $this->assertSame($admin->getFormGroups(), []);
1699
    }
1700
1701
    public function testGetFilterParameters(): void
1702
    {
1703
        $authorId = uniqid();
1704
1705
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1706
1707
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
1708
        $commentAdmin->setParentAssociationMapping('post.author');
1709
        $commentAdmin->setParent($postAdmin);
1710
1711
        $request = $this->createMock(Request::class);
1712
        $query = $this->createMock(ParameterBag::class);
1713
        $query
1714
            ->method('get')
1715
            ->willReturn([
1716
                'filter' => [
1717
                    '_page' => '1',
1718
                    '_per_page' => '32',
1719
                ],
1720
            ]);
1721
1722
        $request->query = $query;
1723
        $request
1724
            ->method('get')
1725
            ->willReturn($authorId);
1726
1727
        $commentAdmin->setRequest($request);
1728
1729
        $modelManager = $this->createMock(ModelManagerInterface::class);
1730
        $modelManager
1731
            ->method('getDefaultSortValues')
1732
            ->willReturn([]);
1733
1734
        $commentAdmin->setModelManager($modelManager);
1735
1736
        $parameters = $commentAdmin->getFilterParameters();
1737
1738
        $this->assertTrue(isset($parameters['post__author']));
1739
        $this->assertSame(['value' => $authorId], $parameters['post__author']);
1740
    }
1741
1742
    public function testGetFilterFieldDescription(): void
1743
    {
1744
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1745
1746
        $fooFieldDescription = new FieldDescription();
1747
        $barFieldDescription = new FieldDescription();
1748
        $bazFieldDescription = new FieldDescription();
1749
1750
        $modelManager = $this->createMock(ModelManagerInterface::class);
1751
        $modelManager->expects($this->exactly(3))
1752
            ->method('getNewFieldDescriptionInstance')
1753
            ->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...
1754
                switch ($name) {
1755
                    case 'foo':
1756
                        $fieldDescription = $fooFieldDescription;
1757
1758
                        break;
1759
1760
                    case 'bar':
1761
                        $fieldDescription = $barFieldDescription;
1762
1763
                        break;
1764
1765
                    case 'baz':
1766
                        $fieldDescription = $bazFieldDescription;
1767
1768
                        break;
1769
1770
                    default:
1771
                        throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
1772
                        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...
1773
                }
1774
1775
                $fieldDescription->setName($name);
1776
1777
                return $fieldDescription;
1778
            });
1779
1780
        $modelAdmin->setModelManager($modelManager);
1781
1782
        $pager = $this->createMock(PagerInterface::class);
1783
1784
        $datagrid = $this->createMock(DatagridInterface::class);
1785
        $datagrid->expects($this->once())
1786
            ->method('getPager')
1787
            ->willReturn($pager);
1788
1789
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
1790
        $datagridBuilder->expects($this->once())
1791
            ->method('getBaseDatagrid')
1792
            ->with($this->identicalTo($modelAdmin), [])
1793
            ->willReturn($datagrid);
1794
1795
        $datagridBuilder->expects($this->exactly(3))
1796
            ->method('addFilter')
1797
            ->willReturnCallback(static function ($datagrid, $type, $fieldDescription, AdminInterface $admin): void {
1798
                $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
1799
                $fieldDescription->mergeOption('field_options', ['required' => false]);
1800
            });
1801
1802
        $modelAdmin->setDatagridBuilder($datagridBuilder);
1803
1804
        $this->assertSame(['foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription], $modelAdmin->getFilterFieldDescriptions());
1805
        $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
1806
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
1807
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
1808
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
1809
        $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
1810
        $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
1811
        $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
1812
    }
1813
1814
    public function testGetSubjectNoRequest(): void
1815
    {
1816
        $modelManager = $this->createMock(ModelManagerInterface::class);
1817
        $modelManager
1818
            ->expects($this->never())
1819
            ->method('find');
1820
1821
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1822
        $admin->setModelManager($modelManager);
1823
1824
        $this->assertNull($admin->getSubject());
1825
    }
1826
1827
    public function testGetSideMenu(): void
1828
    {
1829
        $item = $this->createMock(ItemInterface::class);
1830
        $item
1831
            ->expects($this->once())
1832
            ->method('setChildrenAttribute')
1833
            ->with('class', 'nav navbar-nav');
1834
        $item
1835
            ->expects($this->once())
1836
            ->method('setExtra')
1837
            ->with('translation_domain', 'foo_bar_baz');
1838
1839
        $menuFactory = $this->createMock(FactoryInterface::class);
1840
        $menuFactory
1841
            ->expects($this->once())
1842
            ->method('createItem')
1843
            ->willReturn($item);
1844
1845
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1846
        $modelAdmin->setMenuFactory($menuFactory);
1847
        $modelAdmin->setTranslationDomain('foo_bar_baz');
1848
1849
        $modelAdmin->getSideMenu('foo');
1850
    }
1851
1852
    /**
1853
     * @return array
1854
     */
1855
    public function provideGetSubject()
1856
    {
1857
        return [
1858
            [23],
1859
            ['azerty'],
1860
            ['4f69bbb5f14a13347f000092'],
1861
            ['0779ca8d-e2be-11e4-ac58-0242ac11000b'],
1862
            ['123'.AdapterInterface::ID_SEPARATOR.'my_type'], // composite keys are supported
1863
        ];
1864
    }
1865
1866
    /**
1867
     * @dataProvider provideGetSubject
1868
     */
1869
    public function testGetSubjectFailed($id): void
1870
    {
1871
        $modelManager = $this->createMock(ModelManagerInterface::class);
1872
        $modelManager
1873
            ->expects($this->once())
1874
            ->method('find')
1875
            ->with('NewsBundle\Entity\Post', $id)
1876
            ->willReturn(null); // entity not found
1877
1878
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1879
        $admin->setModelManager($modelManager);
1880
1881
        $admin->setRequest(new Request(['id' => $id]));
1882
        $this->assertNull($admin->getSubject());
1883
    }
1884
1885
    /**
1886
     * @dataProvider provideGetSubject
1887
     */
1888
    public function testGetSubject($id): void
1889
    {
1890
        $entity = new Post();
1891
1892
        $modelManager = $this->createMock(ModelManagerInterface::class);
1893
        $modelManager
1894
            ->expects($this->once())
1895
            ->method('find')
1896
            ->with('NewsBundle\Entity\Post', $id)
1897
            ->willReturn($entity);
1898
1899
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1900
        $admin->setModelManager($modelManager);
1901
1902
        $admin->setRequest(new Request(['id' => $id]));
1903
        $this->assertSame($entity, $admin->getSubject());
1904
        $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
1905
    }
1906
1907
    public function testGetSubjectWithParentDescription(): void
1908
    {
1909
        $adminId = 1;
1910
1911
        $comment = new Comment();
1912
1913
        $modelManager = $this->createMock(ModelManagerInterface::class);
1914
        $modelManager
1915
            ->method('find')
1916
            ->with('NewsBundle\Entity\Comment', $adminId)
1917
            ->willReturn($comment);
1918
1919
        $request = new Request(['id' => $adminId]);
1920
1921
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1922
        $postAdmin->setRequest($request);
1923
1924
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
1925
        $commentAdmin->setRequest($request);
1926
        $commentAdmin->setModelManager($modelManager);
1927
1928
        $this->assertSame($comment, $commentAdmin->getSubject());
1929
1930
        $commentAdmin->setSubject(null);
1931
        $commentAdmin->setParentFieldDescription(new FieldDescription());
1932
1933
        $this->assertNull($commentAdmin->getSubject());
1934
    }
1935
1936
    /**
1937
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1938
     */
1939
    public function testGetActionButtonsList(): void
1940
    {
1941
        $expected = [
1942
            'create' => [
1943
                'template' => 'Foo.html.twig',
1944
            ],
1945
        ];
1946
1947
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1948
1949
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1950
        $templateRegistry->getTemplate('button_create')->willReturn('Foo.html.twig');
1951
1952
        $admin->setTemplateRegistry($templateRegistry->reveal());
1953
1954
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
1955
        $securityHandler
1956
            ->expects($this->once())
1957
            ->method('isGranted')
1958
            ->with($admin, 'CREATE', $admin)
1959
            ->willReturn(true);
1960
        $admin->setSecurityHandler($securityHandler);
1961
1962
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
1963
        $routeGenerator
1964
            ->expects($this->once())
1965
            ->method('hasAdminRoute')
1966
            ->with($admin, 'create')
1967
            ->willReturn(true);
1968
        $admin->setRouteGenerator($routeGenerator);
1969
1970
        $this->assertSame($expected, $admin->getActionButtons('list', null));
1971
    }
1972
1973
    /**
1974
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1975
     */
1976
    public function testGetActionButtonsListCreateDisabled(): void
1977
    {
1978
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1979
1980
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
1981
        $securityHandler
1982
            ->expects($this->once())
1983
            ->method('isGranted')
1984
            ->with($admin, 'CREATE', $admin)
1985
            ->willReturn(false);
1986
        $admin->setSecurityHandler($securityHandler);
1987
1988
        $this->assertSame([], $admin->getActionButtons('list', null));
1989
    }
1990
1991
    /**
1992
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
1993
     */
1994
    public function testGetBatchActions(): void
1995
    {
1996
        $expected = [
1997
            'delete' => [
1998
                'label' => 'action_delete',
1999
                'translation_domain' => 'SonataAdminBundle',
2000
                'ask_confirmation' => true, // by default always true
2001
            ],
2002
            'foo' => [
2003
                'label' => 'action_foo',
2004
                'translation_domain' => 'SonataAdminBundle',
2005
            ],
2006
            'bar' => [
2007
                'label' => 'batch.label_bar',
2008
                'translation_domain' => 'SonataAdminBundle',
2009
            ],
2010
            'baz' => [
2011
                'label' => 'action_baz',
2012
                'translation_domain' => 'AcmeAdminBundle',
2013
            ],
2014
        ];
2015
2016
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2017
2018
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
2019
        $labelTranslatorStrategy
2020
            ->method('getLabel')
2021
            ->willReturnCallback(static function (string $label, string $context = '', string $type = ''): string {
2022
                return $context.'.'.$type.'_'.$label;
2023
            });
2024
2025
        $admin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
2026
        $admin->setRouteBuilder($pathInfo);
2027
        $admin->setTranslationDomain('SonataAdminBundle');
2028
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
2029
2030
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
2031
        $routeGenerator
2032
            ->expects($this->once())
2033
            ->method('hasAdminRoute')
2034
            ->with($admin, 'delete')
2035
            ->willReturn(true);
2036
        $admin->setRouteGenerator($routeGenerator);
2037
2038
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2039
        $securityHandler
2040
            ->method('isGranted')
2041
            ->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...
2042
                return $admin === $adminIn && 'DELETE' === $attributes;
2043
            });
2044
        $admin->setSecurityHandler($securityHandler);
2045
2046
        $this->assertSame($expected, $admin->getBatchActions());
2047
    }
2048
2049
    /**
2050
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2051
     */
2052
    public function testShowMosaicButton(): void
2053
    {
2054
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
2055
        $listModes = $admin->getListModes();
2056
2057
        $admin->showMosaicButton(true);
2058
2059
        $this->assertSame($listModes, $admin->getListModes());
2060
    }
2061
2062
    /**
2063
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2064
     */
2065
    public function testShowMosaicButtonHideMosaic(): void
2066
    {
2067
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
2068
        $listModes = $admin->getListModes();
2069
        $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...
2070
2071
        $admin->showMosaicButton(false);
2072
2073
        $this->assertSame($expected, $admin->getListModes());
2074
    }
2075
2076
    /**
2077
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
2078
     * @dataProvider provideGetBaseRouteName
2079
     */
2080
    public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void
2081
    {
2082
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2083
2084
        $routeGenerator = new DefaultRouteGenerator(
2085
            $this->createMock(RouterInterface::class),
2086
            new RoutesCache($this->cacheTempFolder, true)
2087
        );
2088
2089
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
2090
        $admin->setRouteBuilder($pathInfo);
2091
        $admin->setRouteGenerator($routeGenerator);
2092
        $admin->initialize();
2093
2094
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
2095
        $templateRegistry->getTemplate('action_create')->willReturn('Foo.html.twig');
2096
2097
        $admin->setTemplateRegistry($templateRegistry->reveal());
2098
2099
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2100
        $securityHandler
2101
            ->method('isGranted')
2102
            ->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...
2103
                return $admin === $adminIn && ('CREATE' === $attributes || 'LIST' === $attributes);
2104
            });
2105
2106
        $admin->setSecurityHandler($securityHandler);
2107
2108
        $this->assertArrayHasKey('list', $admin->getDashboardActions());
2109
        $this->assertArrayHasKey('create', $admin->getDashboardActions());
2110
    }
2111
2112
    public function testDefaultFilters(): void
2113
    {
2114
        $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
2115
2116
        $subjectId = uniqid();
2117
2118
        $request = $this->createMock(Request::class);
2119
        $query = $this->createMock(ParameterBag::class);
2120
        $query
2121
            ->method('get')
2122
            ->with($this->equalTo('filter'))
2123
            ->willReturn([
2124
                'a' => [
2125
                    'value' => 'b',
2126
                ],
2127
                'foo' => [
2128
                    'type' => '1',
2129
                    'value' => 'bar',
2130
                ],
2131
                'baz' => [
2132
                    'type' => '5',
2133
                    'value' => 'test',
2134
                ],
2135
            ]);
2136
        $request->query = $query;
2137
2138
        $request
2139
            ->method('get')
2140
            ->willReturn($subjectId);
2141
2142
        $admin->setRequest($request);
2143
2144
        $modelManager = $this->createMock(ModelManagerInterface::class);
2145
        $modelManager
2146
            ->method('getDefaultSortValues')
2147
            ->willReturn([]);
2148
2149
        $admin->setModelManager($modelManager);
2150
2151
        $this->assertSame([
2152
            '_page' => 1,
2153
            '_per_page' => 32,
2154
            'foo' => [
2155
                'type' => '1',
2156
                'value' => 'bar',
2157
            ],
2158
            'baz' => [
2159
                'type' => '5',
2160
                'value' => 'test',
2161
            ],
2162
            'a' => [
2163
                'value' => 'b',
2164
            ],
2165
        ], $admin->getFilterParameters());
2166
2167
        $this->assertTrue($admin->isDefaultFilter('foo'));
2168
        $this->assertFalse($admin->isDefaultFilter('bar'));
2169
        $this->assertFalse($admin->isDefaultFilter('a'));
2170
    }
2171
2172
    /**
2173
     * @group legacy
2174
     */
2175
    public function testDefaultBreadcrumbsBuilder(): void
2176
    {
2177
        $container = $this->createMock(ContainerInterface::class);
2178
        $container->expects($this->once())
2179
            ->method('getParameter')
2180
            ->with('sonata.admin.configuration.breadcrumbs')
2181
            ->willReturn([]);
2182
2183
        $pool = $this->getMockBuilder(Pool::class)
2184
            ->disableOriginalConstructor()
2185
            ->getMock();
2186
        $pool->expects($this->once())
2187
            ->method('getContainer')
2188
            ->willReturn($container);
2189
2190
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2191
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2192
        ], '', true, true, true, ['getConfigurationPool']);
2193
        $admin->expects($this->once())
2194
            ->method('getConfigurationPool')
2195
            ->willReturn($pool);
2196
2197
        $this->assertInstanceOf(BreadcrumbsBuilder::class, $admin->getBreadcrumbsBuilder());
2198
    }
2199
2200
    /**
2201
     * @group legacy
2202
     */
2203
    public function testBreadcrumbsBuilderSetter(): void
2204
    {
2205
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2206
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2207
        ]);
2208
        $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->createMock(
2209
            BreadcrumbsBuilderInterface::class
2210
        )));
2211
        $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
2212
    }
2213
2214
    /**
2215
     * @group legacy
2216
     */
2217
    public function testGetBreadcrumbs(): void
2218
    {
2219
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2220
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2221
        ]);
2222
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2223
        $action = 'myaction';
2224
        $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
2225
        $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
2226
    }
2227
2228
    /**
2229
     * @group legacy
2230
     */
2231
    public function testBuildBreadcrumbs(): void
2232
    {
2233
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2234
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2235
        ]);
2236
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2237
        $action = 'myaction';
2238
        $menu = $this->createMock(ItemInterface::class);
2239
        $builder->buildBreadcrumbs($admin, $action, $menu)
2240
            ->shouldBeCalledTimes(1)
2241
            ->willReturn($menu);
2242
        $admin->setBreadcrumbsBuilder($builder->reveal());
2243
2244
        /* check the called is proxied only once */
2245
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2246
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2247
    }
2248
2249
    /**
2250
     * NEXT_MAJOR: remove this method.
2251
     *
2252
     * @group legacy
2253
     */
2254
    public function testCreateQueryLegacyCallWorks(): void
2255
    {
2256
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2257
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2258
        ]);
2259
        $query = $this->createMock(ProxyQueryInterface::class);
2260
        $modelManager = $this->createMock(ModelManagerInterface::class);
2261
        $modelManager->expects($this->once())
2262
            ->method('createQuery')
2263
            ->with('My\Class')
2264
            ->willReturn($query);
2265
2266
        $admin->setModelManager($modelManager);
2267
        $this->assertSame($query, $admin->createQuery('list'));
2268
    }
2269
2270
    public function testGetDataSourceIterator(): void
2271
    {
2272
        $datagrid = $this->createMock(DatagridInterface::class);
2273
        $datagrid->method('buildPager');
2274
2275
        $modelManager = $this->createMock(ModelManagerInterface::class);
2276
        $modelManager->method('getExportFields')->willReturn([
2277
            'field',
2278
            'foo',
2279
            'bar',
2280
        ]);
2281
        $modelManager->expects($this->once())->method('getDataSourceIterator')
2282
            ->with($this->equalTo($datagrid), $this->equalTo([
2283
                'Feld' => 'field',
2284
                1 => 'foo',
2285
                2 => 'bar',
2286
            ]));
2287
2288
        $admin = $this->getMockBuilder(AbstractAdmin::class)
2289
            ->disableOriginalConstructor()
2290
            ->setMethods(['getDatagrid', 'getTranslationLabel', 'trans'])
2291
            ->getMockForAbstractClass();
2292
        $admin->method('getDatagrid')->willReturn($datagrid);
2293
        $admin->setModelManager($modelManager);
2294
2295
        $admin
2296
            ->method('getTranslationLabel')
2297
            ->willReturnCallback(static function (string $label, string $context = '', string $type = ''): string {
2298
                return $context.'.'.$type.'_'.$label;
2299
            });
2300
        $admin
2301
            ->method('trans')
2302
            ->willReturnCallback(static function (string $label): string {
2303
                if ('export.label_field' === $label) {
2304
                    return 'Feld';
2305
                }
2306
2307
                return $label;
2308
            });
2309
2310
        $admin->getDataSourceIterator();
2311
    }
2312
2313
    public function testCircularChildAdmin(): void
2314
    {
2315
        $this->expectException(\RuntimeException::class);
2316
        $this->expectExceptionMessage(
2317
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment` admin.'
2318
        );
2319
2320
        $postAdmin = new PostAdmin(
2321
            'sonata.post.admin.post',
2322
            'Application\Sonata\NewsBundle\Entity\Post',
2323
            'Sonata\NewsBundle\Controller\PostAdminController'
2324
        );
2325
        $commentAdmin = new CommentAdmin(
2326
            'sonata.post.admin.comment',
2327
            'Application\Sonata\NewsBundle\Entity\Comment',
2328
            'Sonata\NewsBundle\Controller\CommentAdminController'
2329
        );
2330
        $postAdmin->addChild($commentAdmin, 'post');
2331
        $commentAdmin->addChild($postAdmin, 'comment');
2332
    }
2333
2334
    public function testCircularChildAdminTripleLevel(): void
2335
    {
2336
        $this->expectException(\RuntimeException::class);
2337
        $this->expectExceptionMessage(
2338
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment_vote` admin.'
2339
        );
2340
2341
        $postAdmin = new PostAdmin(
2342
            'sonata.post.admin.post',
2343
            'Application\Sonata\NewsBundle\Entity\Post',
2344
            'Sonata\NewsBundle\Controller\PostAdminController'
2345
        );
2346
        $commentAdmin = new CommentAdmin(
2347
            'sonata.post.admin.comment',
2348
            'Application\Sonata\NewsBundle\Entity\Comment',
2349
            'Sonata\NewsBundle\Controller\CommentAdminController'
2350
        );
2351
        $commentVoteAdmin = new CommentVoteAdmin(
2352
            'sonata.post.admin.comment_vote',
2353
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2354
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2355
        );
2356
        $postAdmin->addChild($commentAdmin, 'post');
2357
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2358
        $commentVoteAdmin->addChild($postAdmin, 'post');
2359
    }
2360
2361
    public function testCircularChildAdminWithItself(): void
2362
    {
2363
        $this->expectException(\RuntimeException::class);
2364
        $this->expectExceptionMessage(
2365
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.post` admin.'
2366
        );
2367
2368
        $postAdmin = new PostAdmin(
2369
            'sonata.post.admin.post',
2370
            'Application\Sonata\NewsBundle\Entity\Post',
2371
            'Sonata\NewsBundle\Controller\PostAdminController'
2372
        );
2373
        $postAdmin->addChild($postAdmin);
2374
    }
2375
2376
    public function testGetRootAncestor(): void
2377
    {
2378
        $postAdmin = new PostAdmin(
2379
            'sonata.post.admin.post',
2380
            'Application\Sonata\NewsBundle\Entity\Post',
2381
            'Sonata\NewsBundle\Controller\PostAdminController'
2382
        );
2383
        $commentAdmin = new CommentAdmin(
2384
            'sonata.post.admin.comment',
2385
            'Application\Sonata\NewsBundle\Entity\Comment',
2386
            'Sonata\NewsBundle\Controller\CommentAdminController'
2387
        );
2388
        $commentVoteAdmin = new CommentVoteAdmin(
2389
            'sonata.post.admin.comment_vote',
2390
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2391
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2392
        );
2393
2394
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2395
        $this->assertSame($commentAdmin, $commentAdmin->getRootAncestor());
2396
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2397
2398
        $postAdmin->addChild($commentAdmin, 'post');
2399
2400
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2401
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2402
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2403
2404
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2405
2406
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2407
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2408
        $this->assertSame($postAdmin, $commentVoteAdmin->getRootAncestor());
2409
    }
2410
2411
    public function testGetChildDepth(): void
2412
    {
2413
        $postAdmin = new PostAdmin(
2414
            'sonata.post.admin.post',
2415
            'Application\Sonata\NewsBundle\Entity\Post',
2416
            'Sonata\NewsBundle\Controller\PostAdminController'
2417
        );
2418
        $commentAdmin = new CommentAdmin(
2419
            'sonata.post.admin.comment',
2420
            'Application\Sonata\NewsBundle\Entity\Comment',
2421
            'Sonata\NewsBundle\Controller\CommentAdminController'
2422
        );
2423
        $commentVoteAdmin = new CommentVoteAdmin(
2424
            'sonata.post.admin.comment_vote',
2425
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2426
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2427
        );
2428
2429
        $this->assertSame(0, $postAdmin->getChildDepth());
2430
        $this->assertSame(0, $commentAdmin->getChildDepth());
2431
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2432
2433
        $postAdmin->addChild($commentAdmin, 'post');
2434
2435
        $this->assertSame(0, $postAdmin->getChildDepth());
2436
        $this->assertSame(1, $commentAdmin->getChildDepth());
2437
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2438
2439
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2440
2441
        $this->assertSame(0, $postAdmin->getChildDepth());
2442
        $this->assertSame(1, $commentAdmin->getChildDepth());
2443
        $this->assertSame(2, $commentVoteAdmin->getChildDepth());
2444
    }
2445
2446
    public function testGetCurrentLeafChildAdmin(): void
2447
    {
2448
        $postAdmin = new PostAdmin(
2449
            'sonata.post.admin.post',
2450
            'Application\Sonata\NewsBundle\Entity\Post',
2451
            'Sonata\NewsBundle\Controller\PostAdminController'
2452
        );
2453
        $commentAdmin = new CommentAdmin(
2454
            'sonata.post.admin.comment',
2455
            'Application\Sonata\NewsBundle\Entity\Comment',
2456
            'Sonata\NewsBundle\Controller\CommentAdminController'
2457
        );
2458
        $commentVoteAdmin = new CommentVoteAdmin(
2459
            'sonata.post.admin.comment_vote',
2460
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2461
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2462
        );
2463
2464
        $postAdmin->addChild($commentAdmin, 'post');
2465
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2466
2467
        $this->assertNull($postAdmin->getCurrentLeafChildAdmin());
2468
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2469
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2470
2471
        $commentAdmin->setCurrentChild(true);
2472
2473
        $this->assertSame($commentAdmin, $postAdmin->getCurrentLeafChildAdmin());
2474
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2475
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2476
2477
        $commentVoteAdmin->setCurrentChild(true);
2478
2479
        $this->assertSame($commentVoteAdmin, $postAdmin->getCurrentLeafChildAdmin());
2480
        $this->assertSame($commentVoteAdmin, $commentAdmin->getCurrentLeafChildAdmin());
2481
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2482
    }
2483
2484
    private function createTagAdmin(Post $post): TagAdmin
2485
    {
2486
        $postAdmin = $this->getMockBuilder(PostAdmin::class)
2487
            ->disableOriginalConstructor()
2488
            ->getMock();
2489
2490
        $postAdmin->method('getObject')->willReturn($post);
2491
2492
        $formBuilder = $this->createMock(FormBuilderInterface::class);
2493
        $formBuilder->method('getForm')->willReturn(null);
2494
2495
        $tagAdmin = $this->getMockBuilder(TagAdmin::class)
2496
            ->setConstructorArgs([
2497
                'admin.tag',
2498
                Tag::class,
2499
                'MyBundle\MyController',
2500
            ])
2501
            ->setMethods(['getFormBuilder'])
2502
            ->getMock();
2503
2504
        $tagAdmin->method('getFormBuilder')->willReturn($formBuilder);
2505
        $tagAdmin->setParent($postAdmin);
2506
2507
        $tag = new Tag();
2508
        $tagAdmin->setSubject($tag);
2509
2510
        $request = $this->createMock(Request::class);
2511
        $tagAdmin->setRequest($request);
2512
2513
        $configurationPool = $this->getMockBuilder(Pool::class)
2514
            ->disableOriginalConstructor()
2515
            ->getMock();
2516
2517
        $configurationPool->method('getPropertyAccessor')->willReturn(PropertyAccess::createPropertyAccessor());
2518
2519
        $tagAdmin->setConfigurationPool($configurationPool);
2520
2521
        return $tagAdmin;
2522
    }
2523
}
2524