Completed
Pull Request — 3.x (#5164)
by Marko
04:15
created

AdminTest::testConfigureActionButtonsDeprection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Admin;
13
14
use Doctrine\Common\Collections\Collection;
15
use Knp\Menu\FactoryInterface;
16
use Knp\Menu\ItemInterface;
17
use PHPUnit\Framework\TestCase;
18
use Prophecy\Argument;
19
use Sonata\AdminBundle\Admin\AbstractAdmin;
20
use Sonata\AdminBundle\Admin\AdminExtensionInterface as OldAdminExtensionInterface;
21
use Sonata\AdminBundle\Admin\AdminInterface;
22
use Sonata\AdminBundle\Admin\BreadcrumbsBuilder;
23
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
24
use Sonata\AdminBundle\Admin\Extension\AdminExtensionInterface;
25
use Sonata\AdminBundle\Admin\Extension\GetAccessMappingInterface;
26
use Sonata\AdminBundle\Admin\Extension\GetPersistentParametersInterface;
27
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
28
use Sonata\AdminBundle\Admin\Pool;
29
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
30
use Sonata\AdminBundle\Builder\FormContractorInterface;
31
use Sonata\AdminBundle\Builder\ListBuilderInterface;
32
use Sonata\AdminBundle\Builder\RouteBuilderInterface;
33
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
34
use Sonata\AdminBundle\Datagrid\DatagridInterface;
35
use Sonata\AdminBundle\Datagrid\PagerInterface;
36
use Sonata\AdminBundle\Model\AuditManagerInterface;
37
use Sonata\AdminBundle\Model\ModelManagerInterface;
38
use Sonata\AdminBundle\Route\DefaultRouteGenerator;
39
use Sonata\AdminBundle\Route\PathInfoBuilder;
40
use Sonata\AdminBundle\Route\RouteGeneratorInterface;
41
use Sonata\AdminBundle\Route\RoutesCache;
42
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
43
use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
44
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
45
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
46
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentVoteAdmin;
47
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentWithCustomRouteAdmin;
48
use Sonata\AdminBundle\Tests\Fixtures\Admin\Extension\DummyExtension;
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\CoreBundle\Model\Adapter\AdapterInterface;
63
use Symfony\Component\DependencyInjection\Container;
64
use Symfony\Component\DependencyInjection\ContainerInterface;
65
use Symfony\Component\Form\FormBuilder;
66
use Symfony\Component\Form\FormBuilderInterface;
67
use Symfony\Component\Form\FormEvent;
68
use Symfony\Component\Form\FormEvents;
69
use Symfony\Component\HttpFoundation\ParameterBag;
70
use Symfony\Component\HttpFoundation\Request;
71
use Symfony\Component\PropertyAccess\PropertyAccess;
72
use Symfony\Component\Routing\RouterInterface;
73
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
74
use Symfony\Component\Translation\TranslatorInterface;
75
use Symfony\Component\Validator\Mapping\MemberMetadata;
76
use Symfony\Component\Validator\Validator\ValidatorInterface;
77
78
class AdminTest extends TestCase
79
{
80
    protected $cacheTempFolder;
81
82
    public function setUp()
83
    {
84
        $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
85
86
        exec('rm -rf '.$this->cacheTempFolder);
87
    }
88
89
    /**
90
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct
91
     */
92
    public function testConstructor()
93
    {
94
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
95
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
96
97
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
98
        $this->assertInstanceOf(AbstractAdmin::class, $admin);
99
        $this->assertSame($class, $admin->getClass());
100
        $this->assertSame($baseControllerName, $admin->getBaseControllerName());
101
    }
102
103
    public function testGetClass()
104
    {
105
        $class = Post::class;
106
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
107
108
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
109
110
        $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class));
111
112
        $admin->setSubject(new BlogPost());
113
        $this->assertSame(BlogPost::class, $admin->getClass());
114
115
        $admin->setSubClasses(['foo']);
116
        $this->assertSame(BlogPost::class, $admin->getClass());
117
118
        $admin->setSubject(null);
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
119
        $admin->setSubClasses([]);
120
        $this->assertSame($class, $admin->getClass());
121
122
        $admin->setSubClasses(['foo' => 'bar']);
123
        $admin->setRequest(new Request(['subclass' => 'foo']));
124
        $this->assertSame('bar', $admin->getClass());
125
    }
126
127
    public function testGetClassException()
128
    {
129
        $this->expectException(\RuntimeException::class);
130
        $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass');
131
132
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
133
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
134
135
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
136
        $admin->setParentFieldDescription(new FieldDescription());
137
        $admin->setSubClasses(['foo' => 'bar']);
138
        $admin->setRequest(new Request(['subclass' => 'foo']));
139
        $admin->getClass();
140
    }
141
142
    public function testCheckAccessThrowsExceptionOnMadeUpAction()
143
    {
144
        $admin = new PostAdmin(
145
            'sonata.post.admin.post',
146
            'Application\Sonata\NewsBundle\Entity\Post',
147
            'SonataNewsBundle:PostAdmin'
148
        );
149
        $this->expectException(
150
            \InvalidArgumentException::class
151
        );
152
        $this->expectExceptionMessage(
153
            'Action "made-up" could not be found'
154
        );
155
        $admin->checkAccess('made-up');
156
    }
157
158
    public function testCheckAccessThrowsAccessDeniedException()
159
    {
160
        $admin = new PostAdmin(
161
            'sonata.post.admin.post',
162
            'Application\Sonata\NewsBundle\Entity\Post',
163
            'SonataNewsBundle:PostAdmin'
164
        );
165
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
166
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
167
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
168
        $customExtension = $this->prophesize(GetAccessMappingInterface::class);
169
        $customExtension->getAccessMapping($admin)->willReturn(
170
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
171
        );
172
        $admin->addAdminExtension($customExtension->reveal());
173
        $admin->setSecurityHandler($securityHandler->reveal());
174
        $this->expectException(
175
            AccessDeniedException::class
176
        );
177
        $this->expectExceptionMessage(
178
            'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE'
179
        );
180
        $admin->checkAccess('custom_action');
181
    }
182
183
    public function testHasAccessOnMadeUpAction()
184
    {
185
        $admin = new PostAdmin(
186
            'sonata.post.admin.post',
187
            'Application\Sonata\NewsBundle\Entity\Post',
188
            'SonataNewsBundle:PostAdmin'
189
        );
190
191
        $this->assertFalse($admin->hasAccess('made-up'));
192
    }
193
194
    public function testHasAccess()
195
    {
196
        $admin = new PostAdmin(
197
            'sonata.post.admin.post',
198
            'Application\Sonata\NewsBundle\Entity\Post',
199
            'SonataNewsBundle:PostAdmin'
200
        );
201
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
202
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
203
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
204
        $customExtension = $this->prophesize(GetAccessMappingInterface::class);
205
        $customExtension->getAccessMapping($admin)->willReturn(
206
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
207
        );
208
        $admin->addAdminExtension($customExtension->reveal());
209
        $admin->setSecurityHandler($securityHandler->reveal());
210
211
        $this->assertFalse($admin->hasAccess('custom_action'));
212
    }
213
214
    public function testHasAccessAllowsAccess()
215
    {
216
        $admin = new PostAdmin(
217
            'sonata.post.admin.post',
218
            'Application\Sonata\NewsBundle\Entity\Post',
219
            'SonataNewsBundle:PostAdmin'
220
        );
221
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
222
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
223
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true);
224
        $customExtension = $this->prophesize(GetAccessMappingInterface::class);
225
        $customExtension->getAccessMapping($admin)->willReturn(
226
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
227
        );
228
        $admin->addAdminExtension($customExtension->reveal());
229
        $admin->setSecurityHandler($securityHandler->reveal());
230
231
        $this->assertTrue($admin->hasAccess('custom_action'));
232
    }
233
234
    public function testHasAccessAllowsAccessEditAction()
235
    {
236
        $admin = new PostAdmin(
237
            'sonata.post.admin.post',
238
            'Application\Sonata\NewsBundle\Entity\Post',
239
            'SonataNewsBundle:PostAdmin'
240
        );
241
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
242
        $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true);
243
        $customExtension = $this->prophesize(GetAccessMappingInterface::class);
244
        $customExtension->getAccessMapping($admin)->willReturn(
245
            ['edit_action' => ['EDIT_ROLE']]
246
        );
247
        $admin->addAdminExtension($customExtension->reveal());
248
        $admin->setSecurityHandler($securityHandler->reveal());
249
250
        $this->assertTrue($admin->hasAccess('edit_action'));
251
    }
252
253
    /**
254
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild
255
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild
256
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild
257
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild
258
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren
259
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren
260
     */
261
    public function testChildren()
262
    {
263
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
264
        $this->assertFalse($postAdmin->hasChildren());
265
        $this->assertFalse($postAdmin->hasChild('comment'));
266
267
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
268
        $postAdmin->addChild($commentAdmin, 'post');
269
270
        $this->assertTrue($postAdmin->hasChildren());
271
        $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment'));
272
273
        $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode());
274
        $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute());
275
        $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent());
276
        $this->assertSame('post', $commentAdmin->getParentAssociationMapping());
277
278
        $this->assertFalse($postAdmin->isChild());
279
        $this->assertTrue($commentAdmin->isChild());
280
281
        $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren());
282
    }
283
284
    /**
285
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure
286
     */
287
    public function testConfigure()
288
    {
289
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
290
        $this->assertNotNull($admin->getUniqid());
291
292
        $admin->initialize();
293
        $this->assertNotNull($admin->getUniqid());
294
        $this->assertSame('Post', $admin->getClassnameLabel());
295
296
        $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
297
        $admin->setClassnameLabel('postcomment');
298
299
        $admin->initialize();
300
        $this->assertSame('postcomment', $admin->getClassnameLabel());
301
    }
302
303
    public function testConfigureWithValidParentAssociationMapping()
304
    {
305
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
306
        $admin->setParentAssociationMapping('Category');
307
308
        $admin->initialize();
309
        $this->assertSame('Category', $admin->getParentAssociationMapping());
310
    }
311
312
    public function provideGetBaseRoutePattern()
313
    {
314
        return [
315
            [
316
                'Application\Sonata\NewsBundle\Entity\Post',
317
                '/sonata/news/post',
318
            ],
319
            [
320
                'Application\Sonata\NewsBundle\Document\Post',
321
                '/sonata/news/post',
322
            ],
323
            [
324
                'MyApplication\MyBundle\Entity\Post',
325
                '/myapplication/my/post',
326
            ],
327
            [
328
                'MyApplication\MyBundle\Entity\Post\Category',
329
                '/myapplication/my/post-category',
330
            ],
331
            [
332
                'MyApplication\MyBundle\Entity\Product\Category',
333
                '/myapplication/my/product-category',
334
            ],
335
            [
336
                'MyApplication\MyBundle\Entity\Other\Product\Category',
337
                '/myapplication/my/other-product-category',
338
            ],
339
            [
340
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
341
                '/cmf/foo/menu',
342
            ],
343
            [
344
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
345
                '/cmf/foo/menu',
346
            ],
347
            [
348
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
349
                '/symfony/barbar/menu',
350
            ],
351
            [
352
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
353
                '/symfony/barbar/menu-item',
354
            ],
355
            [
356
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
357
                '/cmf/foo/menu',
358
            ],
359
            [
360
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
361
                '/cmf/foo/menu',
362
            ],
363
            [
364
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
365
                '/cmf/foo/menu',
366
            ],
367
            [
368
                'AppBundle\Entity\User',
369
                '/app/user',
370
            ],
371
            [
372
                'App\Entity\User',
373
                '/app/user',
374
            ],
375
        ];
376
    }
377
378
    /**
379
     * @dataProvider provideGetBaseRoutePattern
380
     */
381
    public function testGetBaseRoutePattern($objFqn, $expected)
382
    {
383
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
384
        $this->assertSame($expected, $admin->getBaseRoutePattern());
385
    }
386
387
    /**
388
     * @dataProvider provideGetBaseRoutePattern
389
     */
390
    public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected)
391
    {
392
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
393
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
394
        $commentAdmin->setParent($postAdmin);
395
396
        $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern());
397
    }
398
399
    /**
400
     * @dataProvider provideGetBaseRoutePattern
401
     */
402
    public function testGetBaseRoutePatternWithTwoNestedChildAdmin($objFqn, $expected)
403
    {
404
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
405
        $commentAdmin = new CommentAdmin(
406
            'sonata.post.admin.comment',
407
            'Application\Sonata\NewsBundle\Entity\Comment',
408
            'SonataNewsBundle:CommentAdmin'
409
        );
410
        $commentVoteAdmin = new CommentVoteAdmin(
411
            'sonata.post.admin.comment_vote',
412
            'Application\Sonata\NewsBundle\Entity\CommentVote',
413
            'SonataNewsBundle:CommentVoteAdmin'
414
        );
415
        $commentAdmin->setParent($postAdmin);
416
        $commentVoteAdmin->setParent($commentAdmin);
417
418
        $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern());
419
    }
420
421
    public function testGetBaseRoutePatternWithSpecifedPattern()
422
    {
423
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin');
424
425
        $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern());
426
    }
427
428
    public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern()
429
    {
430
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
431
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
432
        $commentAdmin->setParent($postAdmin);
433
434
        $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern());
435
    }
436
437
    public function testGetBaseRoutePatternWithUnreconizedClassname()
438
    {
439
        $this->expectException(\RuntimeException::class);
440
441
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
442
        $admin->getBaseRoutePattern();
443
    }
444
445
    public function provideGetBaseRouteName()
446
    {
447
        return [
448
            [
449
                'Application\Sonata\NewsBundle\Entity\Post',
450
                'admin_sonata_news_post',
451
            ],
452
            [
453
                'Application\Sonata\NewsBundle\Document\Post',
454
                'admin_sonata_news_post',
455
            ],
456
            [
457
                'MyApplication\MyBundle\Entity\Post',
458
                'admin_myapplication_my_post',
459
            ],
460
            [
461
                'MyApplication\MyBundle\Entity\Post\Category',
462
                'admin_myapplication_my_post_category',
463
            ],
464
            [
465
                'MyApplication\MyBundle\Entity\Product\Category',
466
                'admin_myapplication_my_product_category',
467
            ],
468
            [
469
                'MyApplication\MyBundle\Entity\Other\Product\Category',
470
                'admin_myapplication_my_other_product_category',
471
            ],
472
            [
473
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
474
                'admin_cmf_foo_menu',
475
            ],
476
            [
477
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
478
                'admin_cmf_foo_menu',
479
            ],
480
            [
481
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
482
                'admin_symfony_barbar_menu',
483
            ],
484
            [
485
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
486
                'admin_symfony_barbar_menu_item',
487
            ],
488
            [
489
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
490
                'admin_cmf_foo_menu',
491
            ],
492
            [
493
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
494
                'admin_cmf_foo_menu',
495
            ],
496
            [
497
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
498
                'admin_cmf_foo_menu',
499
            ],
500
            [
501
                'AppBundle\Entity\User',
502
                'admin_app_user',
503
            ],
504
            [
505
                'App\Entity\User',
506
                'admin_app_user',
507
            ],
508
        ];
509
    }
510
511
    /**
512
     * @dataProvider provideGetBaseRouteName
513
     */
514
    public function testGetBaseRouteName($objFqn, $expected)
515
    {
516
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
517
518
        $this->assertSame($expected, $admin->getBaseRouteName());
519
    }
520
521
    /**
522
     * @group legacy
523
     * @expectedDeprecation Calling "addChild" without second argument is deprecated since 3.35 and will not be allowed in 4.0.
524
     * @dataProvider provideGetBaseRouteName
525
     */
526
    public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected)
527
    {
528
        $routeGenerator = new DefaultRouteGenerator(
529
            $this->createMock(RouterInterface::class),
530
            new RoutesCache($this->cacheTempFolder, true)
531
        );
532
533
        $container = new Container();
534
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
535
536
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
537
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
538
        $container->set('sonata.post.admin.post', $postAdmin);
539
        $postAdmin->setConfigurationPool($pool);
540
        $postAdmin->setRouteBuilder($pathInfo);
541
        $postAdmin->setRouteGenerator($routeGenerator);
542
        $postAdmin->initialize();
543
544
        $commentAdmin = new CommentAdmin(
545
            'sonata.post.admin.comment',
546
            'Application\Sonata\NewsBundle\Entity\Comment',
547
            'SonataNewsBundle:CommentAdmin'
548
        );
549
        $container->set('sonata.post.admin.comment', $commentAdmin);
550
        $commentAdmin->setConfigurationPool($pool);
551
        $commentAdmin->setRouteBuilder($pathInfo);
552
        $commentAdmin->setRouteGenerator($routeGenerator);
553
        $commentAdmin->initialize();
554
555
        $postAdmin->addChild($commentAdmin, 'post');
556
557
        $commentVoteAdmin = new CommentVoteAdmin(
558
            'sonata.post.admin.comment_vote',
559
            'Application\Sonata\NewsBundle\Entity\CommentVote',
560
            'SonataNewsBundle:CommentVoteAdmin'
561
        );
562
563
        $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin);
564
        $commentVoteAdmin->setConfigurationPool($pool);
565
        $commentVoteAdmin->setRouteBuilder($pathInfo);
566
        $commentVoteAdmin->setRouteGenerator($routeGenerator);
567
        $commentVoteAdmin->initialize();
568
569
        $commentAdmin->addChild($commentVoteAdmin);
570
        $pool->setAdminServiceIds([
571
            'sonata.post.admin.post',
572
            'sonata.post.admin.comment',
573
            'sonata.post.admin.comment_vote',
574
        ]);
575
576
        $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
577
578
        $this->assertTrue($postAdmin->hasRoute('show'));
579
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show'));
580
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show'));
581
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show'));
582
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
583
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list'));
584
        $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
585
        $this->assertFalse($commentAdmin->hasRoute('edit'));
586
        $this->assertSame('post', $commentAdmin->getParentAssociationMapping());
587
588
        /*
589
         * Test the route name from request
590
         */
591
        $postListRequest = new Request(
592
            [],
593
            [],
594
            [
595
                '_route' => $postAdmin->getBaseRouteName().'_list',
596
            ]
597
        );
598
599
        $postAdmin->setRequest($postListRequest);
600
        $commentAdmin->setRequest($postListRequest);
601
602
        $this->assertTrue($postAdmin->isCurrentRoute('list'));
603
        $this->assertFalse($postAdmin->isCurrentRoute('create'));
604
        $this->assertFalse($commentAdmin->isCurrentRoute('list'));
605
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('list'));
606
        $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
607
        $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
608
        $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
609
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
610
    }
611
612
    public function testGetBaseRouteNameWithUnreconizedClassname()
613
    {
614
        $this->expectException(\RuntimeException::class);
615
616
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
617
        $admin->getBaseRouteName();
618
    }
619
620
    public function testGetBaseRouteNameWithSpecifiedName()
621
    {
622
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
623
624
        $this->assertSame('post_custom', $postAdmin->getBaseRouteName());
625
    }
626
627
    public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName()
628
    {
629
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
630
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
631
        $commentAdmin->setParent($postAdmin);
632
633
        $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName());
634
    }
635
636
    public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName()
637
    {
638
        $postAdmin = new PostAdmin(
639
            'sonata.post.admin.post',
640
            'Application\Sonata\NewsBundle\Entity\Post',
641
            'SonataNewsBundle:PostAdmin'
642
        );
643
        $commentAdmin = new CommentWithCustomRouteAdmin(
644
            'sonata.post.admin.comment_with_custom_route',
645
            'Application\Sonata\NewsBundle\Entity\Comment',
646
            'SonataNewsBundle:CommentWithCustomRouteAdmin'
647
        );
648
        $commentVoteAdmin = new CommentVoteAdmin(
649
            'sonata.post.admin.comment_vote',
650
            'Application\Sonata\NewsBundle\Entity\CommentVote',
651
            'SonataNewsBundle:CommentVoteAdmin'
652
        );
653
        $commentAdmin->setParent($postAdmin);
654
        $commentVoteAdmin->setParent($commentAdmin);
655
656
        $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName());
657
    }
658
659
    /**
660
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid
661
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid
662
     */
663
    public function testSetUniqid()
664
    {
665
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
666
667
        $uniqid = uniqid();
668
        $admin->setUniqid($uniqid);
669
670
        $this->assertSame($uniqid, $admin->getUniqid());
671
    }
672
673
    public function testUniqidConsistency()
674
    {
675
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
676
            'sonata.abstract.admin',
677
            'AbstractBundle\Entity\Foo',
678
            'SonataAbstractBundle:FooAdmin',
679
        ]);
680
        $admin->initialize();
681
682
        $uniqid = $admin->getUniqid();
683
        $admin->setUniqid(null);
684
685
        $this->assertSame($uniqid, $admin->getUniqid());
686
687
        $parentAdmin = $this->getMockForAbstractClass(AbstractAdmin::class, [
688
            'sonata.abstract.parent.admin',
689
            'AbstractBundle\Entity\Bar',
690
            'SonataAbstractBundle:BarAdmin',
691
        ]);
692
        $parentAdmin->initialize();
693
694
        $admin->setParent($parentAdmin);
695
        $admin->setUniqid(null);
696
697
        $this->assertNotSame($uniqid, $admin->getUniqid());
698
    }
699
700
    public function testToString()
701
    {
702
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
703
704
        $s = new \stdClass();
705
706
        $this->assertNotEmpty($admin->toString($s));
707
708
        $s = new FooToString();
709
        $this->assertSame('salut', $admin->toString($s));
710
711
        // To string method is implemented, but returns null
712
        $s = new FooToStringNull();
713
        $this->assertNotEmpty($admin->toString($s));
714
715
        $this->assertSame('', $admin->toString(false));
716
    }
717
718
    public function testIsAclEnabled()
719
    {
720
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
721
722
        $this->assertFalse($postAdmin->isAclEnabled());
723
724
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
725
        $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class));
726
        $this->assertTrue($commentAdmin->isAclEnabled());
727
    }
728
729
    /**
730
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses
731
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass
732
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses
733
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass
734
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
735
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass
736
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode
737
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass
738
     */
739
    public function testSubClass()
740
    {
741
        $admin = new PostAdmin(
742
            'sonata.post.admin.post',
743
            Post::class,
744
            'SonataNewsBundle:PostAdmin'
745
        );
746
        $this->assertFalse($admin->hasSubClass('test'));
747
        $this->assertFalse($admin->hasActiveSubClass());
748
        $this->assertCount(0, $admin->getSubClasses());
749
        $this->assertNull($admin->getActiveSubClass());
750
        $this->assertNull($admin->getActiveSubclassCode());
751
        $this->assertSame(Post::class, $admin->getClass());
752
753
        // Just for the record, if there is no inheritance set, the getSubject is not used
754
        // the getSubject can also lead to some issue
755
        $admin->setSubject(new BlogPost());
756
        $this->assertSame(BlogPost::class, $admin->getClass());
757
758
        $admin->setSubClasses([
759
            'extended1' => 'NewsBundle\Entity\PostExtended1',
760
            'extended2' => 'NewsBundle\Entity\PostExtended2',
761
        ]);
762
        $this->assertFalse($admin->hasSubClass('test'));
763
        $this->assertTrue($admin->hasSubClass('extended1'));
764
        $this->assertFalse($admin->hasActiveSubClass());
765
        $this->assertCount(2, $admin->getSubClasses());
766
        $this->assertNull($admin->getActiveSubClass());
767
        $this->assertNull($admin->getActiveSubclassCode());
768
        $this->assertSame(
769
            BlogPost::class,
770
            $admin->getClass(),
771
            'When there is no subclass in the query the class parameter should be returned'
772
        );
773
774
        $request = new Request(['subclass' => 'extended1']);
775
        $admin->setRequest($request);
776
        $this->assertFalse($admin->hasSubClass('test'));
777
        $this->assertTrue($admin->hasSubClass('extended1'));
778
        $this->assertTrue($admin->hasActiveSubClass());
779
        $this->assertCount(2, $admin->getSubClasses());
780
        $this->assertSame(
781
            'NewsBundle\Entity\PostExtended1',
782
            $admin->getActiveSubClass(),
783
            'It should return the curently active sub class.'
784
        );
785
        $this->assertSame('extended1', $admin->getActiveSubclassCode());
786
        $this->assertSame(
787
            'NewsBundle\Entity\PostExtended1',
788
            $admin->getClass(),
789
            'getClass() should return the name of the sub class when passed through a request query parameter.'
790
        );
791
792
        $request->query->set('subclass', 'inject');
793
        $this->assertNull($admin->getActiveSubclassCode());
794
    }
795
796
    public function testNonExistantSubclass()
797
    {
798
        $this->expectException(\RuntimeException::class);
799
800
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
801
        $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class));
802
803
        $admin->setRequest(new Request(['subclass' => 'inject']));
804
805
        $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']);
806
807
        $this->assertTrue($admin->hasActiveSubClass());
808
809
        $admin->getActiveSubClass();
810
    }
811
812
    /**
813
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
814
     */
815
    public function testOnlyOneSubclassNeededToBeActive()
816
    {
817
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
818
        $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']);
819
        $request = new Request(['subclass' => 'extended1']);
820
        $admin->setRequest($request);
821
        $this->assertTrue($admin->hasActiveSubClass());
822
    }
823
824
    /**
825
     * @group legacy
826
     * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since 3.30 and will be removed in 4.0.
827
     */
828
    public function testAddSubClassIsDeprecated()
829
    {
830
        $admin = new PostAdmin(
831
            'sonata.post.admin.post',
832
            Post::class,
833
            'SonataNewsBundle:PostAdmin'
834
        );
835
        $admin->addSubClass('whatever');
836
    }
837
838
    public function testGetPerPageOptions()
839
    {
840
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
841
842
        $perPageOptions = $admin->getPerPageOptions();
843
844
        foreach ($perPageOptions as $perPage) {
845
            $this->assertEquals(0, $perPage % 4);
846
        }
847
848
        $admin->setPerPageOptions([500, 1000]);
849
        $this->assertSame([500, 1000], $admin->getPerPageOptions());
850
    }
851
852
    public function testGetLabelTranslatorStrategy()
853
    {
854
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
855
856
        $this->assertNull($admin->getLabelTranslatorStrategy());
857
858
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
859
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
860
        $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy());
861
    }
862
863
    public function testGetRouteBuilder()
864
    {
865
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
866
867
        $this->assertNull($admin->getRouteBuilder());
868
869
        $routeBuilder = $this->createMock(RouteBuilderInterface::class);
870
        $admin->setRouteBuilder($routeBuilder);
871
        $this->assertSame($routeBuilder, $admin->getRouteBuilder());
872
    }
873
874
    public function testGetMenuFactory()
875
    {
876
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
877
878
        $this->assertNull($admin->getMenuFactory());
879
880
        $menuFactory = $this->createMock(FactoryInterface::class);
881
        $admin->setMenuFactory($menuFactory);
882
        $this->assertSame($menuFactory, $admin->getMenuFactory());
883
    }
884
885
    /**
886
     * @group legacy
887
     * @expectedDeprecation Extensions implementing "Sonata\AdminBundle\Admin\AdminExtensionInterface" are deprecated since 3.x and will be removed in 4.0. Implement single-method interfaces from "Sonata\AdminBundle\Admin\Extension" namespace.
888
     */
889
    public function testGetExtensions()
890
    {
891
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
892
893
        $this->assertSame([], $admin->getExtensions());
894
895
        $adminExtension1 = $this->createMock(OldAdminExtensionInterface::class);
896
        $adminExtension2 = $this->createMock(AdminExtensionInterface::class);
897
898
        $admin->addAdminExtension($adminExtension1);
899
        $admin->addAdminExtension($adminExtension2);
900
        $this->assertSame([$adminExtension1, $adminExtension2], $admin->getExtensions());
901
    }
902
903
    public function testGetFilterTheme()
904
    {
905
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
906
907
        $this->assertSame([], $admin->getFilterTheme());
908
909
        $admin->setFilterTheme(['FooTheme']);
910
        $this->assertSame(['FooTheme'], $admin->getFilterTheme());
911
    }
912
913
    public function testGetFormTheme()
914
    {
915
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
916
917
        $this->assertSame([], $admin->getFormTheme());
918
919
        $admin->setFormTheme(['FooTheme']);
920
921
        $this->assertSame(['FooTheme'], $admin->getFormTheme());
922
    }
923
924
    public function testGetValidator()
925
    {
926
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
927
928
        $this->assertNull($admin->getValidator());
929
930
        $validator = $this->getMockForAbstractClass(ValidatorInterface::class);
931
932
        $admin->setValidator($validator);
933
        $this->assertSame($validator, $admin->getValidator());
934
    }
935
936
    public function testGetSecurityHandler()
937
    {
938
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
939
940
        $this->assertNull($admin->getSecurityHandler());
941
942
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
943
        $admin->setSecurityHandler($securityHandler);
944
        $this->assertSame($securityHandler, $admin->getSecurityHandler());
945
    }
946
947
    public function testGetSecurityInformation()
948
    {
949
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
950
951
        $this->assertSame([], $admin->getSecurityInformation());
952
953
        $securityInformation = [
954
            'GUEST' => ['VIEW', 'LIST'],
955
            'STAFF' => ['EDIT', 'LIST', 'CREATE'],
956
        ];
957
958
        $admin->setSecurityInformation($securityInformation);
959
        $this->assertSame($securityInformation, $admin->getSecurityInformation());
960
    }
961
962
    public function testGetManagerType()
963
    {
964
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
965
966
        $this->assertNull($admin->getManagerType());
967
968
        $admin->setManagerType('foo_orm');
969
        $this->assertSame('foo_orm', $admin->getManagerType());
970
    }
971
972
    public function testGetModelManager()
973
    {
974
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
975
976
        $this->assertNull($admin->getModelManager());
977
978
        $modelManager = $this->createMock(ModelManagerInterface::class);
979
980
        $admin->setModelManager($modelManager);
981
        $this->assertSame($modelManager, $admin->getModelManager());
982
    }
983
984
    /**
985
     * NEXT_MAJOR: remove this method.
986
     *
987
     * @group legacy
988
     */
989
    public function testGetBaseCodeRoute()
990
    {
991
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
992
993
        $this->assertSame('', $admin->getBaseCodeRoute());
994
995
        $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 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...
996
        $this->assertSame('foo', $admin->getBaseCodeRoute());
997
    }
998
999
    // NEXT_MAJOR: uncomment this method.
1000
    // public function testGetBaseCodeRoute()
1001
    // {
1002
    //     $postAdmin = new PostAdmin(
1003
    //         'sonata.post.admin.post',
1004
    //         'NewsBundle\Entity\Post',
1005
    //         'SonataNewsBundle:PostAdmin'
1006
    //     );
1007
    //     $commentAdmin = new CommentAdmin(
1008
    //         'sonata.post.admin.comment',
1009
    //         'Application\Sonata\NewsBundle\Entity\Comment',
1010
    //         'SonataNewsBundle:CommentAdmin'
1011
    //     );
1012
    //
1013
    //     $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute());
1014
    //
1015
    //     $postAdmin->addChild($commentAdmin);
1016
    //
1017
    //     $this->assertSame(
1018
    //         'sonata.post.admin.post|sonata.post.admin.comment',
1019
    //         $commentAdmin->getBaseCodeRoute()
1020
    //     );
1021
    // }
1022
1023
    public function testGetRouteGenerator()
1024
    {
1025
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1026
1027
        $this->assertNull($admin->getRouteGenerator());
1028
1029
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
1030
1031
        $admin->setRouteGenerator($routeGenerator);
1032
        $this->assertSame($routeGenerator, $admin->getRouteGenerator());
1033
    }
1034
1035
    public function testGetConfigurationPool()
1036
    {
1037
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1038
1039
        $this->assertNull($admin->getConfigurationPool());
1040
1041
        $pool = $this->getMockBuilder(Pool::class)
1042
            ->disableOriginalConstructor()
1043
            ->getMock();
1044
1045
        $admin->setConfigurationPool($pool);
1046
        $this->assertSame($pool, $admin->getConfigurationPool());
1047
    }
1048
1049
    public function testGetShowBuilder()
1050
    {
1051
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1052
1053
        $this->assertNull($admin->getShowBuilder());
1054
1055
        $showBuilder = $this->createMock(ShowBuilderInterface::class);
1056
1057
        $admin->setShowBuilder($showBuilder);
1058
        $this->assertSame($showBuilder, $admin->getShowBuilder());
1059
    }
1060
1061
    public function testGetListBuilder()
1062
    {
1063
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1064
1065
        $this->assertNull($admin->getListBuilder());
1066
1067
        $listBuilder = $this->createMock(ListBuilderInterface::class);
1068
1069
        $admin->setListBuilder($listBuilder);
1070
        $this->assertSame($listBuilder, $admin->getListBuilder());
1071
    }
1072
1073
    public function testGetDatagridBuilder()
1074
    {
1075
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1076
1077
        $this->assertNull($admin->getDatagridBuilder());
1078
1079
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
1080
1081
        $admin->setDatagridBuilder($datagridBuilder);
1082
        $this->assertSame($datagridBuilder, $admin->getDatagridBuilder());
1083
    }
1084
1085
    public function testGetFormContractor()
1086
    {
1087
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1088
1089
        $this->assertNull($admin->getFormContractor());
1090
1091
        $formContractor = $this->createMock(FormContractorInterface::class);
1092
1093
        $admin->setFormContractor($formContractor);
1094
        $this->assertSame($formContractor, $admin->getFormContractor());
1095
    }
1096
1097
    public function testGetRequest()
1098
    {
1099
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1100
1101
        $this->assertFalse($admin->hasRequest());
1102
1103
        $request = new Request();
1104
1105
        $admin->setRequest($request);
1106
        $this->assertSame($request, $admin->getRequest());
1107
        $this->assertTrue($admin->hasRequest());
1108
    }
1109
1110
    public function testGetRequestWithException()
1111
    {
1112
        $this->expectException(\RuntimeException::class);
1113
        $this->expectExceptionMessage('The Request object has not been set');
1114
1115
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1116
        $admin->getRequest();
1117
    }
1118
1119
    public function testGetTranslationDomain()
1120
    {
1121
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1122
1123
        $this->assertSame('messages', $admin->getTranslationDomain());
1124
1125
        $admin->setTranslationDomain('foo');
1126
        $this->assertSame('foo', $admin->getTranslationDomain());
1127
    }
1128
1129
    /**
1130
     * @group legacy
1131
     */
1132
    public function testGetTranslator()
1133
    {
1134
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1135
1136
        $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 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...
1137
1138
        $translator = $this->createMock(TranslatorInterface::class);
1139
1140
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 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...
1141
        $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 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...
1142
    }
1143
1144
    public function testGetShowGroups()
1145
    {
1146
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1147
1148
        $this->assertFalse($admin->getShowGroups());
1149
1150
        $groups = ['foo', 'bar', 'baz'];
1151
1152
        $admin->setShowGroups($groups);
1153
        $this->assertSame($groups, $admin->getShowGroups());
1154
    }
1155
1156
    public function testGetFormGroups()
1157
    {
1158
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1159
1160
        $this->assertFalse($admin->getFormGroups());
1161
1162
        $groups = ['foo', 'bar', 'baz'];
1163
1164
        $admin->setFormGroups($groups);
1165
        $this->assertSame($groups, $admin->getFormGroups());
1166
    }
1167
1168
    public function testGetMaxPageLinks()
1169
    {
1170
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1171
1172
        $this->assertSame(25, $admin->getMaxPageLinks());
1173
1174
        $admin->setMaxPageLinks(14);
1175
        $this->assertSame(14, $admin->getMaxPageLinks());
1176
    }
1177
1178
    public function testGetMaxPerPage()
1179
    {
1180
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1181
1182
        $this->assertSame(32, $admin->getMaxPerPage());
1183
1184
        $admin->setMaxPerPage(94);
1185
        $this->assertSame(94, $admin->getMaxPerPage());
1186
    }
1187
1188
    public function testGetLabel()
1189
    {
1190
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1191
1192
        $this->assertNull($admin->getLabel());
1193
1194
        $admin->setLabel('FooLabel');
1195
        $this->assertSame('FooLabel', $admin->getLabel());
1196
    }
1197
1198
    public function testGetBaseController()
1199
    {
1200
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1201
1202
        $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName());
1203
1204
        $admin->setBaseControllerName('SonataNewsBundle:FooAdmin');
1205
        $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName());
1206
    }
1207
1208
    public function testGetTemplates()
1209
    {
1210
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1211
1212
        $templates = [
1213
            'list' => '@FooAdmin/CRUD/list.html.twig',
1214
            'show' => '@FooAdmin/CRUD/show.html.twig',
1215
            'edit' => '@FooAdmin/CRUD/edit.html.twig',
1216
        ];
1217
1218
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1219
        $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates);
1220
1221
        $admin->setTemplateRegistry($templateRegistry->reveal());
1222
1223
        $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 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 testGetTemplate1()
1227
    {
1228
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1229
1230
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1231
        $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig');
1232
        $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig');
1233
1234
        $admin->setTemplateRegistry($templateRegistry->reveal());
1235
1236
        $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 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...
1237
        $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 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...
1238
    }
1239
1240
    public function testGetIdParameter()
1241
    {
1242
        $postAdmin = new PostAdmin(
1243
            'sonata.post.admin.post',
1244
            'NewsBundle\Entity\Post',
1245
            'SonataNewsBundle:PostAdmin'
1246
        );
1247
1248
        $this->assertSame('id', $postAdmin->getIdParameter());
1249
        $this->assertFalse($postAdmin->isChild());
1250
1251
        $commentAdmin = new CommentAdmin(
1252
            'sonata.post.admin.comment',
1253
            'Application\Sonata\NewsBundle\Entity\Comment',
1254
            'SonataNewsBundle:CommentAdmin'
1255
        );
1256
        $commentAdmin->setParent($postAdmin);
1257
1258
        $this->assertTrue($commentAdmin->isChild());
1259
        $this->assertSame('childId', $commentAdmin->getIdParameter());
1260
1261
        $commentVoteAdmin = new CommentVoteAdmin(
1262
            'sonata.post.admin.comment_vote',
1263
            'Application\Sonata\NewsBundle\Entity\CommentVote',
1264
            'SonataNewsBundle:CommentVoteAdmin'
1265
        );
1266
        $commentVoteAdmin->setParent($commentAdmin);
1267
1268
        $this->assertTrue($commentVoteAdmin->isChild());
1269
        $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter());
1270
    }
1271
1272
    public function testGetExportFormats()
1273
    {
1274
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1275
1276
        $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats());
1277
    }
1278
1279
    public function testGetUrlsafeIdentifier()
1280
    {
1281
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1282
1283
        $entity = new \stdClass();
1284
1285
        $modelManager = $this->createMock(ModelManagerInterface::class);
1286
        $modelManager->expects($this->once())
1287
            ->method('getUrlsafeIdentifier')
1288
            ->with($this->equalTo($entity))
1289
            ->will($this->returnValue('foo'));
1290
        $admin->setModelManager($modelManager);
1291
1292
        $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity));
1293
    }
1294
1295
    public function testDeterminedPerPageValue()
1296
    {
1297
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1298
1299
        $this->assertFalse($admin->determinedPerPageValue('foo'));
1300
        $this->assertFalse($admin->determinedPerPageValue(123));
1301
        $this->assertTrue($admin->determinedPerPageValue(16));
1302
1303
        $admin->setPerPageOptions([101, 102, 103]);
1304
        $this->assertFalse($admin->determinedPerPageValue(16));
1305
        $this->assertTrue($admin->determinedPerPageValue(101));
1306
    }
1307
1308
    public function testIsGranted()
1309
    {
1310
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1311
1312
        $entity = new \stdClass();
1313
1314
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1315
        $securityHandler->expects($this->any())
1316
            ->method('isGranted')
1317
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin, $entity) {
1318
                if ($admin == $adminIn && 'FOO' == $attributes) {
1319
                    if (($object == $admin) || ($object == $entity)) {
1320
                        return true;
1321
                    }
1322
                }
1323
1324
                return false;
1325
            }));
1326
1327
        $admin->setSecurityHandler($securityHandler);
1328
1329
        $this->assertTrue($admin->isGranted('FOO'));
1330
        $this->assertTrue($admin->isGranted('FOO', $entity));
1331
        $this->assertFalse($admin->isGranted('BAR'));
1332
        $this->assertFalse($admin->isGranted('BAR', $entity));
1333
    }
1334
1335
    public function testSupportsPreviewMode()
1336
    {
1337
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1338
1339
        $this->assertFalse($admin->supportsPreviewMode());
1340
    }
1341
1342
    public function testGetPermissionsShow()
1343
    {
1344
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1345
1346
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
1347
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
1348
        $this->assertSame(['LIST'], $admin->getPermissionsShow('foo'));
1349
    }
1350
1351
    public function testShowIn()
1352
    {
1353
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1354
1355
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1356
        $securityHandler->expects($this->any())
1357
            ->method('isGranted')
1358
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
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...
1359
                if ($admin == $adminIn && $attributes == ['LIST']) {
1360
                    return true;
1361
                }
1362
1363
                return false;
1364
            }));
1365
1366
        $admin->setSecurityHandler($securityHandler);
1367
1368
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
1369
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
1370
        $this->assertTrue($admin->showIn('foo'));
1371
    }
1372
1373
    public function testGetObjectIdentifier()
1374
    {
1375
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1376
1377
        $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
1378
    }
1379
1380
    /**
1381
     * @group legacy
1382
     */
1383
    public function testTrans()
1384
    {
1385
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1386
        $admin->setTranslationDomain('fooMessageDomain');
1387
1388
        $translator = $this->createMock(TranslatorInterface::class);
1389
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 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...
1390
1391
        $translator->expects($this->once())
1392
            ->method('trans')
1393
            ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1394
            ->will($this->returnValue('fooTranslated'));
1395
1396
        $this->assertSame('fooTranslated', $admin->trans('foo'));
1397
    }
1398
1399
    /**
1400
     * @group legacy
1401
     */
1402
    public function testTransWithMessageDomain()
1403
    {
1404
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1405
1406
        $translator = $this->createMock(TranslatorInterface::class);
1407
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 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...
1408
1409
        $translator->expects($this->once())
1410
            ->method('trans')
1411
            ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1412
            ->will($this->returnValue('fooTranslated'));
1413
1414
        $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain'));
1415
    }
1416
1417
    /**
1418
     * @group legacy
1419
     */
1420
    public function testTransChoice()
1421
    {
1422
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1423
        $admin->setTranslationDomain('fooMessageDomain');
1424
1425
        $translator = $this->createMock(TranslatorInterface::class);
1426
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 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...
1427
1428
        $translator->expects($this->once())
1429
            ->method('transChoice')
1430
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1431
            ->will($this->returnValue('fooTranslated'));
1432
1433
        $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 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...
1434
    }
1435
1436
    /**
1437
     * @group legacy
1438
     */
1439
    public function testTransChoiceWithMessageDomain()
1440
    {
1441
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1442
1443
        $translator = $this->createMock(TranslatorInterface::class);
1444
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 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...
1445
1446
        $translator->expects($this->once())
1447
            ->method('transChoice')
1448
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1449
            ->will($this->returnValue('fooTranslated'));
1450
1451
        $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 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...
1452
    }
1453
1454
    public function testSetFilterPersister()
1455
    {
1456
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1457
1458
        $filterPersister = $this->createMock('Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface');
1459
1460
        $this->assertAttributeSame(null, 'filterPersister', $admin);
1461
        $admin->setFilterPersister($filterPersister);
1462
        $this->assertAttributeSame($filterPersister, 'filterPersister', $admin);
1463
        $this->assertAttributeSame(true, 'persistFilters', $admin);
1464
    }
1465
1466
    public function testGetRootCode()
1467
    {
1468
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1469
1470
        $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
1471
1472
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1473
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1474
        $parentFieldDescription->expects($this->once())
1475
            ->method('getAdmin')
1476
            ->will($this->returnValue($parentAdmin));
1477
1478
        $this->assertNull($admin->getParentFieldDescription());
1479
        $admin->setParentFieldDescription($parentFieldDescription);
1480
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1481
        $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
1482
    }
1483
1484
    public function testGetRoot()
1485
    {
1486
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1487
1488
        $this->assertSame($admin, $admin->getRoot());
1489
1490
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1491
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1492
        $parentFieldDescription->expects($this->once())
1493
            ->method('getAdmin')
1494
            ->will($this->returnValue($parentAdmin));
1495
1496
        $this->assertNull($admin->getParentFieldDescription());
1497
        $admin->setParentFieldDescription($parentFieldDescription);
1498
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1499
        $this->assertSame($parentAdmin, $admin->getRoot());
1500
    }
1501
1502
    public function testGetExportFields()
1503
    {
1504
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1505
1506
        $modelManager = $this->createMock(ModelManagerInterface::class);
1507
        $modelManager->expects($this->once())
1508
            ->method('getExportFields')
1509
            ->with($this->equalTo('NewsBundle\Entity\Post'))
1510
            ->will($this->returnValue(['foo', 'bar']));
1511
1512
        $admin->setModelManager($modelManager);
1513
        $this->assertSame(['foo', 'bar'], $admin->getExportFields());
1514
    }
1515
1516
    public function testGetPersistentParametersWithNoExtension()
1517
    {
1518
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1519
1520
        $this->assertEmpty($admin->getPersistentParameters());
1521
    }
1522
1523
    public function testGetPersistentParametersWithInvalidExtension()
1524
    {
1525
        $this->expectException(\RuntimeException::class);
1526
1527
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1528
1529
        $extension = $this->createMock(GetPersistentParametersInterface::class);
1530
        $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(null));
1531
1532
        $admin->addAdminExtension($extension);
1533
1534
        $admin->getPersistentParameters();
1535
    }
1536
1537
    public function testGetPersistentParametersWithValidExtension()
1538
    {
1539
        $expected = [
1540
            'context' => 'foobar',
1541
        ];
1542
1543
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1544
1545
        $extension = $this->createMock(GetPersistentParametersInterface::class);
1546
        $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue($expected));
1547
1548
        $admin->addAdminExtension($extension);
1549
1550
        $this->assertSame($expected, $admin->getPersistentParameters());
1551
    }
1552
1553
    public function testGetFormWithNonCollectionParentValue()
1554
    {
1555
        $post = new Post();
1556
        $tagAdmin = $this->createTagAdmin($post);
1557
        $tag = $tagAdmin->getSubject();
1558
1559
        $tag->setPosts(null);
1560
        $tagAdmin->getForm();
1561
        $this->assertSame($post, $tag->getPosts());
1562
    }
1563
1564
    public function testGetFormWithCollectionParentValue()
1565
    {
1566
        $post = new Post();
1567
        $tagAdmin = $this->createTagAdmin($post);
1568
        $tag = $tagAdmin->getSubject();
1569
1570
        // Case of a doctrine collection
1571
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1572
        $this->assertCount(0, $tag->getPosts());
1573
1574
        $tag->addPost(new Post());
1575
1576
        $this->assertCount(1, $tag->getPosts());
1577
1578
        $tagAdmin->getForm();
1579
1580
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1581
        $this->assertCount(2, $tag->getPosts());
1582
        $this->assertContains($post, $tag->getPosts());
1583
1584
        // Case of an array
1585
        $tag->setPosts([]);
1586
        $this->assertCount(0, $tag->getPosts());
1587
1588
        $tag->addPost(new Post());
1589
1590
        $this->assertCount(1, $tag->getPosts());
1591
1592
        $tagAdmin->getForm();
1593
1594
        $this->assertInternalType('array', $tag->getPosts());
1595
        $this->assertCount(2, $tag->getPosts());
1596
        $this->assertContains($post, $tag->getPosts());
1597
    }
1598
1599
    public function testFormAddPostSubmitEventForPreValidation()
1600
    {
1601
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1602
        $object = new \stdClass();
1603
1604
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
1605
        $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy);
1606
1607
        $validator = $this->createMock(ValidatorInterface::class);
1608
        $validator->expects($this->any())
1609
                ->method('getMetadataFor')
1610
                ->will($this->returnValue($this->createMock(MemberMetadata::class)));
1611
        $modelAdmin->setValidator($validator);
1612
1613
        $modelManager = $this->createMock(ModelManagerInterface::class);
1614
        $modelManager->expects($this->any())
1615
            ->method('getNewFieldDescriptionInstance')
1616
            ->will($this->returnValue(new FieldDescription()));
1617
        $modelAdmin->setModelManager($modelManager);
1618
1619
        // a Admin class to test that preValidate is called
1620
        $testAdminPreValidate = $this->createMock(AbstractAdmin::class, ['preValidate']);
1621
        $testAdminPreValidate->expects($this->once())
1622
                ->method('preValidate')
1623
                ->with($this->identicalTo($object));
1624
1625
        $event = $this->createMock(FormEvent::class);
1626
        $event->expects($this->any())
1627
                ->method('getData')
1628
                ->will($this->returnValue($object));
1629
1630
        $formBuild = $this->createMock(FormBuilder::class, ['addEventListener']);
1631
        $formBuild->expects($this->once())
1632
                ->method('addEventListener')
1633
                ->with($this->identicalTo(FormEvents::POST_SUBMIT),
1634
                        $this->callback(function ($callback) use ($testAdminPreValidate, $event) {
1635
                            if (is_callable($callback)) {
1636
                                $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...
1637
                                $closure($event);
1638
1639
                                return true;
1640
                            }
1641
1642
                            return false;
1643
                        }),
1644
                        $this->greaterThan(0)
1645
                    );
1646
1647
        $formContractor = $this->createMock(FormContractorInterface::class, ['getDefaultOptions', 'getFormBuilder']);
1648
        $formContractor->expects($this->any())
1649
                ->method('getDefaultOptions')
1650
                ->will($this->returnValue([]));
1651
        $formContractor->expects($this->any())
1652
                ->method('getFormBuilder')
1653
                ->will($this->returnValue($formBuild));
1654
1655
        $modelAdmin->setFormContractor($formContractor);
1656
        $modelAdmin->defineFormBuilder($formBuild);
1657
        $modelAdmin->getForm();
1658
    }
1659
1660
    public function testRemoveFieldFromFormGroup()
1661
    {
1662
        $formGroups = [
1663
            'foobar' => [
1664
                'fields' => [
1665
                    'foo' => 'foo',
1666
                    'bar' => 'bar',
1667
                ],
1668
            ],
1669
        ];
1670
1671
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1672
        $admin->setFormGroups($formGroups);
1673
1674
        $admin->removeFieldFromFormGroup('foo');
1675
        $this->assertSame($admin->getFormGroups(), [
1676
            'foobar' => [
1677
                'fields' => [
1678
                    'bar' => 'bar',
1679
                ],
1680
            ],
1681
        ]);
1682
1683
        $admin->removeFieldFromFormGroup('bar');
1684
        $this->assertSame($admin->getFormGroups(), []);
1685
    }
1686
1687
    public function testGetFilterParameters()
1688
    {
1689
        $authorId = uniqid();
1690
1691
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1692
1693
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
1694
        $commentAdmin->setParentAssociationMapping('post.author');
1695
        $commentAdmin->setParent($postAdmin);
1696
1697
        $request = $this->createMock(Request::class, ['get']);
1698
        $query = $this->createMock(ParameterBag::class, ['get']);
1699
        $query->expects($this->any())
1700
            ->method('get')
1701
            ->will($this->returnValue([]));
1702
        $request->query = $query;
1703
        $request->expects($this->any())
1704
            ->method('get')
1705
            ->will($this->returnValue($authorId));
1706
1707
        $commentAdmin->setRequest($request);
1708
1709
        $modelManager = $this->createMock(ModelManagerInterface::class);
1710
        $modelManager->expects($this->any())
1711
            ->method('getDefaultSortValues')
1712
            ->will($this->returnValue([]));
1713
1714
        $commentAdmin->setModelManager($modelManager);
1715
1716
        $parameters = $commentAdmin->getFilterParameters();
1717
1718
        $this->assertTrue(isset($parameters['post__author']));
1719
        $this->assertSame(['value' => $authorId], $parameters['post__author']);
1720
    }
1721
1722
    public function testGetFilterFieldDescription()
1723
    {
1724
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1725
1726
        $fooFieldDescription = new FieldDescription();
1727
        $barFieldDescription = new FieldDescription();
1728
        $bazFieldDescription = new FieldDescription();
1729
1730
        $modelManager = $this->createMock(ModelManagerInterface::class);
1731
        $modelManager->expects($this->exactly(3))
1732
            ->method('getNewFieldDescriptionInstance')
1733
            ->will($this->returnCallback(function ($adminClass, $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...
1734
                switch ($name) {
1735
                    case 'foo':
1736
                        $fieldDescription = $fooFieldDescription;
1737
1738
                        break;
1739
1740
                    case 'bar':
1741
                        $fieldDescription = $barFieldDescription;
1742
1743
                        break;
1744
1745
                    case 'baz':
1746
                        $fieldDescription = $bazFieldDescription;
1747
1748
                        break;
1749
1750
                    default:
1751
                        throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
1752
                        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...
1753
                }
1754
1755
                $fieldDescription->setName($name);
1756
1757
                return $fieldDescription;
1758
            }));
1759
1760
        $modelAdmin->setModelManager($modelManager);
1761
1762
        $pager = $this->createMock(PagerInterface::class);
1763
1764
        $datagrid = $this->createMock(DatagridInterface::class);
1765
        $datagrid->expects($this->once())
1766
            ->method('getPager')
1767
            ->will($this->returnValue($pager));
1768
1769
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
1770
        $datagridBuilder->expects($this->once())
1771
            ->method('getBaseDatagrid')
1772
            ->with($this->identicalTo($modelAdmin), [])
1773
            ->will($this->returnValue($datagrid));
1774
1775
        $datagridBuilder->expects($this->exactly(3))
1776
            ->method('addFilter')
1777
            ->will($this->returnCallback(function ($datagrid, $type, $fieldDescription, AdminInterface $admin) {
1778
                $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
1779
                $fieldDescription->mergeOption('field_options', ['required' => false]);
1780
            }));
1781
1782
        $modelAdmin->setDatagridBuilder($datagridBuilder);
1783
1784
        $this->assertSame(['foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription], $modelAdmin->getFilterFieldDescriptions());
1785
        $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
1786
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
1787
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
1788
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
1789
        $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
1790
        $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
1791
        $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
1792
    }
1793
1794
    public function testGetSubjectNoRequest()
1795
    {
1796
        $modelManager = $this->createMock(ModelManagerInterface::class);
1797
        $modelManager
1798
            ->expects($this->never())
1799
            ->method('find');
1800
1801
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1802
        $admin->setModelManager($modelManager);
1803
1804
        $this->assertNull($admin->getSubject());
1805
    }
1806
1807
    public function testGetSideMenu()
1808
    {
1809
        $item = $this->createMock(ItemInterface::class);
1810
        $item
1811
            ->expects($this->once())
1812
            ->method('setChildrenAttribute')
1813
            ->with('class', 'nav navbar-nav');
1814
        $item
1815
            ->expects($this->once())
1816
            ->method('setExtra')
1817
            ->with('translation_domain', 'foo_bar_baz');
1818
1819
        $menuFactory = $this->createMock(FactoryInterface::class);
1820
        $menuFactory
1821
            ->expects($this->once())
1822
            ->method('createItem')
1823
            ->will($this->returnValue($item));
1824
1825
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1826
        $modelAdmin->setMenuFactory($menuFactory);
1827
        $modelAdmin->setTranslationDomain('foo_bar_baz');
1828
1829
        $modelAdmin->getSideMenu('foo');
1830
    }
1831
1832
    /**
1833
     * @return array
1834
     */
1835
    public function provideGetSubject()
1836
    {
1837
        return [
1838
            [23],
1839
            ['azerty'],
1840
            ['4f69bbb5f14a13347f000092'],
1841
            ['0779ca8d-e2be-11e4-ac58-0242ac11000b'],
1842
            ['123'.AdapterInterface::ID_SEPARATOR.'my_type'], // composite keys are supported
1843
        ];
1844
    }
1845
1846
    /**
1847
     * @dataProvider provideGetSubject
1848
     */
1849
    public function testGetSubjectFailed($id)
1850
    {
1851
        $modelManager = $this->createMock(ModelManagerInterface::class);
1852
        $modelManager
1853
            ->expects($this->once())
1854
            ->method('find')
1855
            ->with('NewsBundle\Entity\Post', $id)
1856
            ->will($this->returnValue(null)); // entity not found
1857
1858
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1859
        $admin->setModelManager($modelManager);
1860
1861
        $admin->setRequest(new Request(['id' => $id]));
1862
        $this->assertNull($admin->getSubject());
1863
    }
1864
1865
    /**
1866
     * @dataProvider provideGetSubject
1867
     */
1868
    public function testGetSubject($id)
1869
    {
1870
        $entity = new Post();
1871
1872
        $modelManager = $this->createMock(ModelManagerInterface::class);
1873
        $modelManager
1874
            ->expects($this->once())
1875
            ->method('find')
1876
            ->with('NewsBundle\Entity\Post', $id)
1877
            ->will($this->returnValue($entity));
1878
1879
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1880
        $admin->setModelManager($modelManager);
1881
1882
        $admin->setRequest(new Request(['id' => $id]));
1883
        $this->assertSame($entity, $admin->getSubject());
1884
        $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
1885
    }
1886
1887
    public function testGetSubjectWithParentDescription()
1888
    {
1889
        $adminId = 1;
1890
1891
        $comment = new Comment();
1892
1893
        $modelManager = $this->createMock(ModelManagerInterface::class);
1894
        $modelManager
1895
            ->expects($this->any())
1896
            ->method('find')
1897
            ->with('NewsBundle\Entity\Comment', $adminId)
1898
            ->will($this->returnValue($comment));
1899
1900
        $request = new Request(['id' => $adminId]);
1901
1902
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1903
        $postAdmin->setRequest($request);
1904
1905
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
1906
        $commentAdmin->setRequest($request);
1907
        $commentAdmin->setModelManager($modelManager);
1908
1909
        $this->assertEquals($comment, $commentAdmin->getSubject());
1910
1911
        $commentAdmin->setSubject(null);
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
1912
        $commentAdmin->setParentFieldDescription(new FieldDescription());
1913
1914
        $this->assertNull($commentAdmin->getSubject());
1915
    }
1916
1917
    /**
1918
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1919
     */
1920
    public function testGetActionButtonsList()
1921
    {
1922
        $expected = [
1923
            'create' => [
1924
                'template' => 'Foo.html.twig',
1925
            ],
1926
        ];
1927
1928
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1929
1930
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1931
        $templateRegistry->getTemplate('button_create')->willReturn('Foo.html.twig');
1932
1933
        $admin->setTemplateRegistry($templateRegistry->reveal());
1934
1935
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
1936
        $securityHandler
1937
            ->expects($this->once())
1938
            ->method('isGranted')
1939
            ->with($admin, 'CREATE', $admin)
1940
            ->will($this->returnValue(true));
1941
        $admin->setSecurityHandler($securityHandler);
1942
1943
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
1944
        $routeGenerator
1945
            ->expects($this->once())
1946
            ->method('hasAdminRoute')
1947
            ->with($admin, 'create')
1948
            ->will($this->returnValue(true));
1949
        $admin->setRouteGenerator($routeGenerator);
1950
1951
        $this->assertSame($expected, $admin->getActionButtons('list', null));
1952
    }
1953
1954
    /**
1955
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1956
     */
1957
    public function testGetActionButtonsListCreateDisabled()
1958
    {
1959
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1960
1961
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
1962
        $securityHandler
1963
            ->expects($this->once())
1964
            ->method('isGranted')
1965
            ->with($admin, 'CREATE', $admin)
1966
            ->will($this->returnValue(false));
1967
        $admin->setSecurityHandler($securityHandler);
1968
1969
        $this->assertSame([], $admin->getActionButtons('list', null));
1970
    }
1971
1972
    /**
1973
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
1974
     */
1975
    public function testGetBatchActions()
1976
    {
1977
        $expected = [
1978
            'delete' => [
1979
                'label' => 'action_delete',
1980
                'translation_domain' => 'SonataAdminBundle',
1981
                'ask_confirmation' => true, // by default always true
1982
            ],
1983
            'foo' => [
1984
                'label' => 'action_foo',
1985
                'translation_domain' => 'SonataAdminBundle',
1986
            ],
1987
            'bar' => [
1988
                'label' => 'batch.label_bar',
1989
                'translation_domain' => 'SonataAdminBundle',
1990
            ],
1991
            'baz' => [
1992
                'label' => 'action_baz',
1993
                'translation_domain' => 'AcmeAdminBundle',
1994
            ],
1995
        ];
1996
1997
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
1998
1999
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
2000
        $labelTranslatorStrategy->expects($this->any())
2001
            ->method('getLabel')
2002
            ->will($this->returnCallback(function ($label, $context = '', $type = '') {
2003
                return $context.'.'.$type.'_'.$label;
2004
            }));
2005
2006
        $admin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
2007
        $admin->setRouteBuilder($pathInfo);
2008
        $admin->setTranslationDomain('SonataAdminBundle');
2009
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
2010
2011
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
2012
        $routeGenerator
2013
            ->expects($this->once())
2014
            ->method('hasAdminRoute')
2015
            ->with($admin, 'delete')
2016
            ->will($this->returnValue(true));
2017
        $admin->setRouteGenerator($routeGenerator);
2018
2019
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2020
        $securityHandler->expects($this->any())
2021
            ->method('isGranted')
2022
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
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...
2023
                if ($admin == $adminIn && 'DELETE' == $attributes) {
2024
                    return true;
2025
                }
2026
2027
                return false;
2028
            }));
2029
        $admin->setSecurityHandler($securityHandler);
2030
2031
        $this->assertSame($expected, $admin->getBatchActions());
2032
    }
2033
2034
    /**
2035
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2036
     */
2037
    public function testShowMosaicButton()
2038
    {
2039
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
2040
        $listModes = $admin->getListModes();
2041
2042
        $admin->showMosaicButton(true);
2043
2044
        $this->assertSame($listModes, $admin->getListModes());
2045
    }
2046
2047
    /**
2048
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2049
     */
2050
    public function testShowMosaicButtonHideMosaic()
2051
    {
2052
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
2053
        $listModes = $admin->getListModes();
2054
        $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...
2055
2056
        $admin->showMosaicButton(false);
2057
2058
        $this->assertSame($expected, $admin->getListModes());
2059
    }
2060
2061
    /**
2062
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
2063
     * @dataProvider provideGetBaseRouteName
2064
     */
2065
    public function testDefaultDashboardActionsArePresent($objFqn, $expected)
2066
    {
2067
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2068
2069
        $routeGenerator = new DefaultRouteGenerator(
2070
            $this->createMock(RouterInterface::class),
2071
            new RoutesCache($this->cacheTempFolder, true)
2072
        );
2073
2074
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
2075
        $admin->setRouteBuilder($pathInfo);
2076
        $admin->setRouteGenerator($routeGenerator);
2077
        $admin->initialize();
2078
2079
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
2080
        $templateRegistry->getTemplate('action_create')->willReturn('Foo.html.twig');
2081
2082
        $admin->setTemplateRegistry($templateRegistry->reveal());
2083
2084
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2085
        $securityHandler->expects($this->any())
2086
            ->method('isGranted')
2087
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
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...
2088
                if ($admin == $adminIn && ('CREATE' == $attributes || 'LIST' == $attributes)) {
2089
                    return true;
2090
                }
2091
2092
                return false;
2093
            }));
2094
2095
        $admin->setSecurityHandler($securityHandler);
2096
2097
        $this->assertArrayHasKey('list', $admin->getDashboardActions());
2098
        $this->assertArrayHasKey('create', $admin->getDashboardActions());
2099
    }
2100
2101
    public function testDefaultFilters()
2102
    {
2103
        $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
2104
2105
        $subjectId = uniqid();
2106
2107
        $request = $this->createMock(Request::class, ['get']);
2108
        $query = $this->createMock(ParameterBag::class, ['set', 'get']);
2109
        $query->expects($this->any())
2110
            ->method('get')
2111
            ->with($this->equalTo('filter'))
2112
            ->will($this->returnValue([
2113
                'a' => [
2114
                    'value' => 'b',
2115
                ],
2116
                'foo' => [
2117
                    'type' => '1',
2118
                    'value' => 'bar',
2119
                ],
2120
                'baz' => [
2121
                    'type' => '5',
2122
                    'value' => 'test',
2123
                ],
2124
            ]));
2125
        $request->query = $query;
2126
2127
        $request->expects($this->any())
2128
            ->method('get')
2129
            ->will($this->returnValue($subjectId));
2130
2131
        $admin->setRequest($request);
2132
2133
        $modelManager = $this->createMock(ModelManagerInterface::class);
2134
        $modelManager->expects($this->any())
2135
            ->method('getDefaultSortValues')
2136
            ->will($this->returnValue([]));
2137
2138
        $admin->setModelManager($modelManager);
2139
2140
        $this->assertEquals([
2141
            'foo' => [
2142
                'type' => '1',
2143
                'value' => 'bar',
2144
            ],
2145
            'baz' => [
2146
                'type' => '5',
2147
                'value' => 'test',
2148
            ],
2149
            '_page' => 1,
2150
            '_per_page' => 32,
2151
            'a' => [
2152
                'value' => 'b',
2153
            ],
2154
        ], $admin->getFilterParameters());
2155
2156
        $this->assertTrue($admin->isDefaultFilter('foo'));
2157
        $this->assertFalse($admin->isDefaultFilter('bar'));
2158
        $this->assertFalse($admin->isDefaultFilter('a'));
2159
    }
2160
2161
    /**
2162
     * @group legacy
2163
     */
2164
    public function testDefaultBreadcrumbsBuilder()
2165
    {
2166
        $container = $this->createMock(ContainerInterface::class);
2167
        $container->expects($this->once())
2168
            ->method('getParameter')
2169
            ->with('sonata.admin.configuration.breadcrumbs')
2170
            ->will($this->returnValue([]));
2171
2172
        $pool = $this->getMockBuilder(Pool::class)
2173
            ->disableOriginalConstructor()
2174
            ->getMock();
2175
        $pool->expects($this->once())
2176
            ->method('getContainer')
2177
            ->will($this->returnValue($container));
2178
2179
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2180
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2181
        ], '', true, true, true, ['getConfigurationPool']);
2182
        $admin->expects($this->once())
2183
            ->method('getConfigurationPool')
2184
            ->will($this->returnValue($pool));
2185
2186
        $this->assertInstanceOf(BreadcrumbsBuilder::class, $admin->getBreadcrumbsBuilder());
2187
    }
2188
2189
    /**
2190
     * @group legacy
2191
     */
2192
    public function testBreadcrumbsBuilderSetter()
2193
    {
2194
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2195
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2196
        ]);
2197
        $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->createMock(
2198
            BreadcrumbsBuilderInterface::class
2199
        )));
2200
        $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
2201
    }
2202
2203
    /**
2204
     * @group legacy
2205
     */
2206
    public function testGetBreadcrumbs()
2207
    {
2208
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2209
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2210
        ]);
2211
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2212
        $action = 'myaction';
2213
        $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
2214
        $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
2215
    }
2216
2217
    /**
2218
     * @group legacy
2219
     */
2220
    public function testBuildBreadcrumbs()
2221
    {
2222
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2223
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2224
        ]);
2225
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2226
        $action = 'myaction';
2227
        $menu = $this->createMock(ItemInterface::class);
2228
        $builder->buildBreadcrumbs($admin, $action, $menu)
2229
            ->shouldBeCalledTimes(1)
2230
            ->willReturn($menu);
2231
        $admin->setBreadcrumbsBuilder($builder->reveal());
2232
2233
        /* check the called is proxied only once */
2234
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2235
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2236
    }
2237
2238
    /**
2239
     * NEXT_MAJOR: remove this method.
2240
     *
2241
     * @group legacy
2242
     */
2243
    public function testCreateQueryLegacyCallWorks()
2244
    {
2245
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2246
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2247
        ]);
2248
        $modelManager = $this->createMock(ModelManagerInterface::class);
2249
        $modelManager->expects($this->once())
2250
            ->method('createQuery')
2251
            ->with('My\Class')
2252
            ->willReturn('a query');
2253
2254
        $admin->setModelManager($modelManager);
2255
        $this->assertSame('a query', $admin->createQuery('list'));
2256
    }
2257
2258
    public function testGetDataSourceIterator()
2259
    {
2260
        $datagrid = $this->createMock(DatagridInterface::class);
2261
        $datagrid->method('buildPager');
2262
2263
        $modelManager = $this->createMock(ModelManagerInterface::class);
2264
        $modelManager->method('getExportFields')->will($this->returnValue([
2265
            'field',
2266
            'foo',
2267
            'bar',
2268
        ]));
2269
        $modelManager->expects($this->once())->method('getDataSourceIterator')
2270
            ->with($this->equalTo($datagrid), $this->equalTo([
2271
                'Feld' => 'field',
2272
                1 => 'foo',
2273
                2 => 'bar',
2274
            ]));
2275
2276
        $admin = $this->getMockBuilder(AbstractAdmin::class)
2277
            ->disableOriginalConstructor()
2278
            ->setMethods(['getDatagrid', 'getTranslationLabel', 'trans'])
2279
            ->getMockForAbstractClass();
2280
        $admin->method('getDatagrid')->will($this->returnValue($datagrid));
2281
        $admin->setModelManager($modelManager);
2282
2283
        $admin->expects($this->any())
2284
            ->method('getTranslationLabel')
2285
            ->will($this->returnCallback(function ($label, $context = '', $type = '') {
2286
                return $context.'.'.$type.'_'.$label;
2287
            }));
2288
        $admin->expects($this->any())
2289
            ->method('trans')
2290
            ->will($this->returnCallback(function ($label) {
2291
                if ('export.label_field' == $label) {
2292
                    return 'Feld';
2293
                }
2294
2295
                return $label;
2296
            }));
2297
2298
        $admin->getDataSourceIterator();
2299
    }
2300
2301
    public function testCircularChildAdmin()
2302
    {
2303
        $this->expectException(
2304
            \RuntimeException::class,
2305
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment` admin.'
2306
        );
2307
2308
        $postAdmin = new PostAdmin(
2309
            'sonata.post.admin.post',
2310
            'Application\Sonata\NewsBundle\Entity\Post',
2311
            'SonataNewsBundle:PostAdmin'
2312
        );
2313
        $commentAdmin = new CommentAdmin(
2314
            'sonata.post.admin.comment',
2315
            'Application\Sonata\NewsBundle\Entity\Comment',
2316
            'SonataNewsBundle:CommentAdmin'
2317
        );
2318
        $postAdmin->addChild($commentAdmin, 'post');
2319
        $commentAdmin->addChild($postAdmin, 'comment');
2320
    }
2321
2322
    public function testCircularChildAdminTripleLevel()
2323
    {
2324
        $this->expectException(
2325
            \RuntimeException::class,
2326
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment_vote` admin.'
2327
        );
2328
2329
        $postAdmin = new PostAdmin(
2330
            'sonata.post.admin.post',
2331
            'Application\Sonata\NewsBundle\Entity\Post',
2332
            'SonataNewsBundle:PostAdmin'
2333
        );
2334
        $commentAdmin = new CommentAdmin(
2335
            'sonata.post.admin.comment',
2336
            'Application\Sonata\NewsBundle\Entity\Comment',
2337
            'SonataNewsBundle:CommentAdmin'
2338
        );
2339
        $commentVoteAdmin = new CommentVoteAdmin(
2340
            'sonata.post.admin.comment_vote',
2341
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2342
            'SonataNewsBundle:CommentVoteAdmin'
2343
        );
2344
        $postAdmin->addChild($commentAdmin, 'post');
2345
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2346
        $commentVoteAdmin->addChild($postAdmin, 'post');
2347
    }
2348
2349
    public function testCircularChildAdminWithItself()
2350
    {
2351
        $this->expectException(
2352
            \RuntimeException::class,
2353
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.post` admin.'
2354
        );
2355
2356
        $postAdmin = new PostAdmin(
2357
            'sonata.post.admin.post',
2358
            'Application\Sonata\NewsBundle\Entity\Post',
2359
            'SonataNewsBundle:PostAdmin'
2360
        );
2361
        $postAdmin->addChild($postAdmin);
2362
    }
2363
2364
    public function testGetRootAncestor()
2365
    {
2366
        $postAdmin = new PostAdmin(
2367
            'sonata.post.admin.post',
2368
            'Application\Sonata\NewsBundle\Entity\Post',
2369
            'SonataNewsBundle:PostAdmin'
2370
        );
2371
        $commentAdmin = new CommentAdmin(
2372
            'sonata.post.admin.comment',
2373
            'Application\Sonata\NewsBundle\Entity\Comment',
2374
            'SonataNewsBundle:CommentAdmin'
2375
        );
2376
        $commentVoteAdmin = new CommentVoteAdmin(
2377
            'sonata.post.admin.comment_vote',
2378
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2379
            'SonataNewsBundle:CommentVoteAdmin'
2380
        );
2381
2382
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2383
        $this->assertSame($commentAdmin, $commentAdmin->getRootAncestor());
2384
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2385
2386
        $postAdmin->addChild($commentAdmin, 'post');
2387
2388
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2389
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2390
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2391
2392
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2393
2394
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2395
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2396
        $this->assertSame($postAdmin, $commentVoteAdmin->getRootAncestor());
2397
    }
2398
2399
    public function testGetChildDepth()
2400
    {
2401
        $postAdmin = new PostAdmin(
2402
            'sonata.post.admin.post',
2403
            'Application\Sonata\NewsBundle\Entity\Post',
2404
            'SonataNewsBundle:PostAdmin'
2405
        );
2406
        $commentAdmin = new CommentAdmin(
2407
            'sonata.post.admin.comment',
2408
            'Application\Sonata\NewsBundle\Entity\Comment',
2409
            'SonataNewsBundle:CommentAdmin'
2410
        );
2411
        $commentVoteAdmin = new CommentVoteAdmin(
2412
            'sonata.post.admin.comment_vote',
2413
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2414
            'SonataNewsBundle:CommentVoteAdmin'
2415
        );
2416
2417
        $this->assertSame(0, $postAdmin->getChildDepth());
2418
        $this->assertSame(0, $commentAdmin->getChildDepth());
2419
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2420
2421
        $postAdmin->addChild($commentAdmin, 'post');
2422
2423
        $this->assertSame(0, $postAdmin->getChildDepth());
2424
        $this->assertSame(1, $commentAdmin->getChildDepth());
2425
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2426
2427
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2428
2429
        $this->assertSame(0, $postAdmin->getChildDepth());
2430
        $this->assertSame(1, $commentAdmin->getChildDepth());
2431
        $this->assertSame(2, $commentVoteAdmin->getChildDepth());
2432
    }
2433
2434
    public function testGetCurrentLeafChildAdmin()
2435
    {
2436
        $postAdmin = new PostAdmin(
2437
            'sonata.post.admin.post',
2438
            'Application\Sonata\NewsBundle\Entity\Post',
2439
            'SonataNewsBundle:PostAdmin'
2440
        );
2441
        $commentAdmin = new CommentAdmin(
2442
            'sonata.post.admin.comment',
2443
            'Application\Sonata\NewsBundle\Entity\Comment',
2444
            'SonataNewsBundle:CommentAdmin'
2445
        );
2446
        $commentVoteAdmin = new CommentVoteAdmin(
2447
            'sonata.post.admin.comment_vote',
2448
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2449
            'SonataNewsBundle:CommentVoteAdmin'
2450
        );
2451
2452
        $postAdmin->addChild($commentAdmin, 'post');
2453
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2454
2455
        $this->assertNull($postAdmin->getCurrentLeafChildAdmin());
2456
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2457
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2458
2459
        $commentAdmin->setCurrentChild(true);
2460
2461
        $this->assertSame($commentAdmin, $postAdmin->getCurrentLeafChildAdmin());
2462
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2463
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2464
2465
        $commentVoteAdmin->setCurrentChild(true);
2466
2467
        $this->assertSame($commentVoteAdmin, $postAdmin->getCurrentLeafChildAdmin());
2468
        $this->assertSame($commentVoteAdmin, $commentAdmin->getCurrentLeafChildAdmin());
2469
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2470
    }
2471
2472
    /**
2473
     * @group legacy
2474
     * @expectedDeprecation Calling "configureBatchActions" without implementing "Sonata\AdminBundle\Admin\Extension\ConfigureBatchActionsInterface" in your extension is deprecated.
2475
     */
2476
    public function testConfigureBatchActionsDeprection()
2477
    {
2478
        $admin = new PostAdmin(
2479
            'sonata.post.admin.post',
2480
            'Application\Sonata\NewsBundle\Entity\Post',
2481
            'SonataNewsBundle:PostAdmin'
2482
        );
2483
2484
        $routeGenerator = $this->prophesize(RouteGeneratorInterface::class);
2485
        $routeGenerator->hasAdminRoute(Argument::cetera())->willReturn(true);
2486
2487
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
2488
        $securityHandler->isGranted(Argument::cetera())->willReturn(true);
2489
2490
        $admin->setSecurityHandler($securityHandler->reveal());
2491
        $admin->setRouteGenerator($routeGenerator->reveal());
2492
        $admin->addAdminExtension(new DummyExtension());
2493
2494
        $admin->getBatchActions();
2495
    }
2496
2497
    /**
2498
     * @group legacy
2499
     * @expectedDeprecation Calling "configureActionButtons" without implementing "Sonata\AdminBundle\Admin\Extension\ConfigureActionButtonsInterface" in your extension is deprecated.
2500
     */
2501
    public function testConfigureActionButtonsDeprection()
2502
    {
2503
        $admin = new PostAdmin(
2504
            'sonata.post.admin.post',
2505
            'Application\Sonata\NewsBundle\Entity\Post',
2506
            'SonataNewsBundle:PostAdmin'
2507
        );
2508
2509
        $routeGenerator = $this->prophesize(RouteGeneratorInterface::class);
2510
        $routeGenerator->hasAdminRoute(Argument::cetera())->willReturn(true);
2511
2512
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
2513
        $securityHandler->isGranted(Argument::cetera())->willReturn(true);
2514
2515
        $admin->setSecurityHandler($securityHandler->reveal());
2516
        $admin->setRouteGenerator($routeGenerator->reveal());
2517
        $admin->addAdminExtension(new DummyExtension());
2518
2519
        $admin->getActionButtons('dummy');
2520
    }
2521
2522
    /**
2523
     * @group legacy
2524
     * @expectedDeprecation Calling "configureDefaultFilterValues" without implementing "Sonata\AdminBundle\Admin\Extension\ConfigureDefaultFilterValuesInterface" in your extension is deprecated.
2525
     */
2526
    public function testConfigureDefaultFilterValuesDeprection()
2527
    {
2528
        $admin = new PostAdmin(
2529
            'sonata.post.admin.post',
2530
            'Application\Sonata\NewsBundle\Entity\Post',
2531
            'SonataNewsBundle:PostAdmin'
2532
        );
2533
2534
        $admin->addAdminExtension(new DummyExtension());
2535
2536
        $admin->isDefaultFilter('dummy');
2537
    }
2538
2539
    private function createTagAdmin(Post $post)
2540
    {
2541
        $postAdmin = $this->getMockBuilder(PostAdmin::class)
2542
            ->disableOriginalConstructor()
2543
            ->getMock();
2544
2545
        $postAdmin->expects($this->any())->method('getObject')->will($this->returnValue($post));
2546
2547
        $formBuilder = $this->createMock(FormBuilderInterface::class);
2548
        $formBuilder->expects($this->any())->method('getForm')->will($this->returnValue(null));
2549
2550
        $tagAdmin = $this->getMockBuilder(TagAdmin::class)
2551
            ->setConstructorArgs([
2552
                'admin.tag',
2553
                Tag::class,
2554
                'MyBundle:MyController',
2555
            ])
2556
            ->setMethods(['getFormBuilder'])
2557
            ->getMock();
2558
2559
        $tagAdmin->expects($this->any())->method('getFormBuilder')->will($this->returnValue($formBuilder));
2560
        $tagAdmin->setParent($postAdmin);
2561
2562
        $tag = new Tag();
2563
        $tagAdmin->setSubject($tag);
2564
2565
        $request = $this->createMock(Request::class);
2566
        $tagAdmin->setRequest($request);
2567
2568
        $configurationPool = $this->getMockBuilder(Pool::class)
2569
            ->disableOriginalConstructor()
2570
            ->getMock();
2571
2572
        $configurationPool->expects($this->any())->method('getPropertyAccessor')->will($this->returnValue(PropertyAccess::createPropertyAccessor()));
2573
2574
        $tagAdmin->setConfigurationPool($configurationPool);
2575
2576
        return $tagAdmin;
2577
    }
2578
}
2579