Completed
Pull Request — master (#4831)
by Jordi Sala
10:38 queued 05:56
created

AdminTest::testGetClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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