Completed
Push — 3.x ( 11fcc7...8a92d8 )
by Grégoire
03:02
created

AdminTest   F

Complexity

Total Complexity 131

Size/Duplication

Total Lines 2419
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 24

Importance

Changes 0
Metric Value
wmc 131
lcom 1
cbo 24
dl 0
loc 2419
rs 0.8
c 0
b 0
f 0

117 Methods

Rating   Name   Duplication   Size   Complexity  
B testGetFilterFieldDescription() 0 71 4
A testGetSubjectNoRequest() 0 12 1
A testGetSideMenu() 0 24 1
A provideGetSubject() 0 10 1
A testGetSubjectFailed() 0 15 1
A testGetSubject() 0 18 1
A setUp() 0 6 1
A testConstructor() 0 10 1
A testGetClass() 0 23 1
A testGetClassException() 0 14 1
A testCheckAccessThrowsExceptionOnMadeUpAction() 0 15 1
A testCheckAccessThrowsAccessDeniedException() 0 24 1
A testHasAccessOnMadeUpAction() 0 10 1
A testHasAccess() 0 19 1
A testHasAccessAllowsAccess() 0 19 1
A testHasAccessAllowsAccessEditAction() 0 18 1
A testChildren() 0 22 1
A testConfigure() 0 15 1
A testConfigureWithValidParentAssociationMapping() 0 8 1
B provideGetBaseRoutePattern() 0 65 1
A testGetBaseRoutePattern() 0 5 1
A testGetBaseRoutePatternWithChildAdmin() 0 8 1
A testGetBaseRoutePatternWithTwoNestedChildAdmin() 0 18 1
A testGetBaseRoutePatternWithSpecifedPattern() 0 6 1
A testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern() 0 8 1
A testGetBaseRoutePatternWithUnreconizedClassname() 0 7 1
B provideGetBaseRouteName() 0 65 1
A testGetBaseRouteName() 0 6 1
B testGetBaseRouteNameWithChildAdmin() 0 85 1
A testGetBaseRouteNameWithUnreconizedClassname() 0 7 1
A testGetBaseRouteNameWithSpecifiedName() 0 6 1
A testGetBaseRouteNameWithChildAdminAndWithSpecifiedName() 0 8 1
A testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName() 0 22 1
A testToString() 0 17 1
A testIsAclEnabled() 0 10 1
A testSetUniqid() 0 9 1
A testGetObjectIdentifier() 0 6 1
A testTrans() 0 15 1
A testTransWithMessageDomain() 0 14 1
A testTransChoice() 0 15 1
A testTransChoiceWithMessageDomain() 0 14 1
A testSetFilterPersister() 0 11 1
A testGetRootCode() 0 17 1
A testGetRoot() 0 17 1
A testGetExportFields() 0 13 1
A testGetPersistentParametersWithNoExtension() 0 6 1
A testGetPersistentParametersWithInvalidExtension() 0 13 1
A testGetPersistentParametersWithValidExtension() 0 15 1
A testGetFormWithNonCollectionParentValue() 0 10 1
A testGetFormWithCollectionParentValue() 0 34 1
B testFormAddPostSubmitEventForPreValidation() 0 60 2
A testRemoveFieldFromFormGroup() 0 26 1
A testGetFilterParameters() 0 40 1
B testSubClass() 0 62 1
A testNonExistantSubclass() 0 16 1
A testOnlyOneSubclassNeededToBeActive() 0 8 1
A testAddSubClassIsDeprecated() 0 9 1
A testGetPerPageOptions() 0 13 2
A testGetLabelTranslatorStrategy() 0 10 1
A testGetRouteBuilder() 0 10 1
A testGetMenuFactory() 0 10 1
A testGetExtensions() 0 13 1
A testGetFilterTheme() 0 9 1
A testGetFormTheme() 0 10 1
A testGetValidator() 0 11 1
A testGetSecurityHandler() 0 10 1
A testGetSecurityInformation() 0 14 1
A testGetManagerType() 0 9 1
A testGetModelManager() 0 11 1
A testGetBaseCodeRoute() 0 9 1
A testGetRouteGenerator() 0 11 1
A testGetConfigurationPool() 0 13 1
A testGetShowBuilder() 0 11 1
A testGetListBuilder() 0 11 1
A testGetDatagridBuilder() 0 11 1
A testGetFormContractor() 0 11 1
A testGetRequest() 0 12 1
A testGetRequestWithException() 0 8 1
A testGetTranslationDomain() 0 9 1
A testGetTranslator() 0 11 1
A testGetShowGroups() 0 11 1
A testGetFormGroups() 0 11 1
A testGetMaxPageLinks() 0 9 1
A testGetMaxPerPage() 0 9 1
A testGetLabel() 0 9 1
A testGetBaseController() 0 9 1
A testGetTemplates() 0 17 1
A testGetTemplate1() 0 13 1
A testGetIdParameter() 0 31 1
A testGetExportFormats() 0 6 1
A testGetUrlsafeIdentifier() 0 15 1
A testDeterminedPerPageValue() 0 12 1
A testIsGranted() 0 33 5
A testSupportsPreviewMode() 0 6 1
A testGetPermissionsShow() 0 8 1
A testShowIn() 0 17 2
A testGetSubjectWithParentDescription() 0 28 1
A testGetActionButtonsList() 0 33 1
A testGetActionButtonsListCreateDisabled() 0 14 1
A testGetBatchActions() 0 54 2
A testShowMosaicButton() 0 9 1
A testShowMosaicButtonHideMosaic() 0 10 1
A testDefaultDashboardActionsArePresent() 0 31 3
B testDefaultFilters() 0 59 1
A testDefaultBreadcrumbsBuilder() 0 24 1
A testBreadcrumbsBuilderSetter() 0 10 1
A testGetBreadcrumbs() 0 10 1
A testBuildBreadcrumbs() 0 17 1
A testCreateQueryLegacyCallWorks() 0 14 1
A testGetDataSourceIterator() 0 42 2
A testCircularChildAdmin() 0 20 1
A testCircularChildAdminTripleLevel() 0 26 1
A testCircularChildAdminWithItself() 0 14 1
A testGetRootAncestor() 0 34 1
A testGetChildDepth() 0 34 1
A testGetCurrentLeafChildAdmin() 0 37 1
A createTagAdmin() 0 39 1

How to fix   Complexity   

Complex Class

Complex classes like AdminTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AdminTest, and based on these observations, apply Extract Interface, too.

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

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

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

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

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

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

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

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

Loading history...
1120
1121
        $translator = $this->createMock(TranslatorInterface::class);
1122
1123
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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

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

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

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

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

Loading history...
1125
    }
1126
1127
    public function testGetShowGroups(): void
1128
    {
1129
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1130
1131
        $this->assertFalse($admin->getShowGroups());
1132
1133
        $groups = ['foo', 'bar', 'baz'];
1134
1135
        $admin->setShowGroups($groups);
1136
        $this->assertSame($groups, $admin->getShowGroups());
1137
    }
1138
1139
    public function testGetFormGroups(): void
1140
    {
1141
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1142
1143
        $this->assertFalse($admin->getFormGroups());
1144
1145
        $groups = ['foo', 'bar', 'baz'];
1146
1147
        $admin->setFormGroups($groups);
1148
        $this->assertSame($groups, $admin->getFormGroups());
1149
    }
1150
1151
    public function testGetMaxPageLinks(): void
1152
    {
1153
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1154
1155
        $this->assertSame(25, $admin->getMaxPageLinks());
1156
1157
        $admin->setMaxPageLinks(14);
1158
        $this->assertSame(14, $admin->getMaxPageLinks());
1159
    }
1160
1161
    public function testGetMaxPerPage(): void
1162
    {
1163
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1164
1165
        $this->assertSame(32, $admin->getMaxPerPage());
1166
1167
        $admin->setMaxPerPage(94);
1168
        $this->assertSame(94, $admin->getMaxPerPage());
1169
    }
1170
1171
    public function testGetLabel(): void
1172
    {
1173
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1174
1175
        $this->assertNull($admin->getLabel());
1176
1177
        $admin->setLabel('FooLabel');
1178
        $this->assertSame('FooLabel', $admin->getLabel());
1179
    }
1180
1181
    public function testGetBaseController(): void
1182
    {
1183
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1184
1185
        $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName());
1186
1187
        $admin->setBaseControllerName('SonataNewsBundle:FooAdmin');
1188
        $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName());
1189
    }
1190
1191
    public function testGetTemplates(): void
1192
    {
1193
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1194
1195
        $templates = [
1196
            'list' => '@FooAdmin/CRUD/list.html.twig',
1197
            'show' => '@FooAdmin/CRUD/show.html.twig',
1198
            'edit' => '@FooAdmin/CRUD/edit.html.twig',
1199
        ];
1200
1201
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1202
        $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates);
1203
1204
        $admin->setTemplateRegistry($templateRegistry->reveal());
1205
1206
        $this->assertSame($templates, $admin->getTemplates());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...ctAdmin::getTemplates() has been deprecated with message: since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead

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

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

Loading history...
1207
    }
1208
1209
    public function testGetTemplate1(): void
1210
    {
1211
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1212
1213
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1214
        $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig');
1215
        $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig');
1216
1217
        $admin->setTemplateRegistry($templateRegistry->reveal());
1218
1219
        $this->assertSame('@FooAdmin/CRUD/edit.html.twig', $admin->getTemplate('edit'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead

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

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

Loading history...
1220
        $this->assertSame('@FooAdmin/CRUD/show.html.twig', $admin->getTemplate('show'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead

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

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

Loading history...
1221
    }
1222
1223
    public function testGetIdParameter(): void
1224
    {
1225
        $postAdmin = new PostAdmin(
1226
            'sonata.post.admin.post',
1227
            'NewsBundle\Entity\Post',
1228
            'SonataNewsBundle:PostAdmin'
1229
        );
1230
1231
        $this->assertSame('id', $postAdmin->getIdParameter());
1232
        $this->assertFalse($postAdmin->isChild());
1233
1234
        $commentAdmin = new CommentAdmin(
1235
            'sonata.post.admin.comment',
1236
            'Application\Sonata\NewsBundle\Entity\Comment',
1237
            'SonataNewsBundle:CommentAdmin'
1238
        );
1239
        $commentAdmin->setParent($postAdmin);
1240
1241
        $this->assertTrue($commentAdmin->isChild());
1242
        $this->assertSame('childId', $commentAdmin->getIdParameter());
1243
1244
        $commentVoteAdmin = new CommentVoteAdmin(
1245
            'sonata.post.admin.comment_vote',
1246
            'Application\Sonata\NewsBundle\Entity\CommentVote',
1247
            'SonataNewsBundle:CommentVoteAdmin'
1248
        );
1249
        $commentVoteAdmin->setParent($commentAdmin);
1250
1251
        $this->assertTrue($commentVoteAdmin->isChild());
1252
        $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter());
1253
    }
1254
1255
    public function testGetExportFormats(): void
1256
    {
1257
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1258
1259
        $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats());
1260
    }
1261
1262
    public function testGetUrlsafeIdentifier(): void
1263
    {
1264
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1265
1266
        $entity = new \stdClass();
1267
1268
        $modelManager = $this->createMock(ModelManagerInterface::class);
1269
        $modelManager->expects($this->once())
1270
            ->method('getUrlsafeIdentifier')
1271
            ->with($this->equalTo($entity))
1272
            ->willReturn('foo');
1273
        $admin->setModelManager($modelManager);
1274
1275
        $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity));
1276
    }
1277
1278
    public function testDeterminedPerPageValue(): void
1279
    {
1280
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1281
1282
        $this->assertFalse($admin->determinedPerPageValue('foo'));
1283
        $this->assertFalse($admin->determinedPerPageValue(123));
1284
        $this->assertTrue($admin->determinedPerPageValue(16));
1285
1286
        $admin->setPerPageOptions([101, 102, 103]);
1287
        $this->assertFalse($admin->determinedPerPageValue(16));
1288
        $this->assertTrue($admin->determinedPerPageValue(101));
1289
    }
1290
1291
    public function testIsGranted(): void
1292
    {
1293
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1294
1295
        $entity = new \stdClass();
1296
1297
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1298
        $securityHandler
1299
            ->method('isGranted')
1300
            ->willReturnCallback(static function (
1301
                AdminInterface $adminIn,
1302
                string $attributes,
1303
                $object = null
1304
            ) use (
1305
                $admin,
1306
                $entity
1307
            ): bool {
1308
                if ($admin === $adminIn && 'FOO' === $attributes) {
1309
                    if (($object === $admin) || ($object === $entity)) {
1310
                        return true;
1311
                    }
1312
                }
1313
1314
                return false;
1315
            });
1316
1317
        $admin->setSecurityHandler($securityHandler);
1318
1319
        $this->assertTrue($admin->isGranted('FOO'));
1320
        $this->assertTrue($admin->isGranted('FOO', $entity));
1321
        $this->assertFalse($admin->isGranted('BAR'));
1322
        $this->assertFalse($admin->isGranted('BAR', $entity));
1323
    }
1324
1325
    public function testSupportsPreviewMode(): void
1326
    {
1327
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1328
1329
        $this->assertFalse($admin->supportsPreviewMode());
1330
    }
1331
1332
    public function testGetPermissionsShow(): void
1333
    {
1334
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1335
1336
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
1337
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
1338
        $this->assertSame(['LIST'], $admin->getPermissionsShow('foo'));
1339
    }
1340
1341
    public function testShowIn(): void
1342
    {
1343
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1344
1345
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1346
        $securityHandler
1347
            ->method('isGranted')
1348
            ->willReturnCallback(static function (AdminInterface $adminIn, array $attributes, $object = null) use ($admin): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

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

Loading history...
1349
                return $admin === $adminIn && $attributes === ['LIST'];
1350
            });
1351
1352
        $admin->setSecurityHandler($securityHandler);
1353
1354
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
1355
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
1356
        $this->assertTrue($admin->showIn('foo'));
1357
    }
1358
1359
    public function testGetObjectIdentifier(): void
1360
    {
1361
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1362
1363
        $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
1364
    }
1365
1366
    /**
1367
     * @group legacy
1368
     */
1369
    public function testTrans(): void
1370
    {
1371
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1372
        $admin->setTranslationDomain('fooMessageDomain');
1373
1374
        $translator = $this->createMock(TranslatorInterface::class);
1375
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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

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

Loading history...
1376
1377
        $translator->expects($this->once())
1378
            ->method('trans')
1379
            ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1380
            ->willReturn('fooTranslated');
1381
1382
        $this->assertSame('fooTranslated', $admin->trans('foo'));
1383
    }
1384
1385
    /**
1386
     * @group legacy
1387
     */
1388
    public function testTransWithMessageDomain(): void
1389
    {
1390
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1391
1392
        $translator = $this->createMock(TranslatorInterface::class);
1393
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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

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

Loading history...
1394
1395
        $translator->expects($this->once())
1396
            ->method('trans')
1397
            ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1398
            ->willReturn('fooTranslated');
1399
1400
        $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain'));
1401
    }
1402
1403
    /**
1404
     * @group legacy
1405
     */
1406
    public function testTransChoice(): void
1407
    {
1408
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1409
        $admin->setTranslationDomain('fooMessageDomain');
1410
1411
        $translator = $this->createMock(TranslatorInterface::class);
1412
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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

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

Loading history...
1413
1414
        $translator->expects($this->once())
1415
            ->method('transChoice')
1416
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1417
            ->willReturn('fooTranslated');
1418
1419
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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

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

Loading history...
1420
    }
1421
1422
    /**
1423
     * @group legacy
1424
     */
1425
    public function testTransChoiceWithMessageDomain(): void
1426
    {
1427
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1428
1429
        $translator = $this->createMock(TranslatorInterface::class);
1430
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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

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

Loading history...
1431
1432
        $translator->expects($this->once())
1433
            ->method('transChoice')
1434
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1435
            ->willReturn('fooTranslated');
1436
1437
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, ['name' => 'Andrej'], 'fooMessageDomain'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since sonata-project/admin-bundle 3.9, to be removed with 4.0

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

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

Loading history...
1438
    }
1439
1440
    public function testSetFilterPersister(): void
1441
    {
1442
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1443
1444
        $filterPersister = $this->createMock('Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface');
1445
1446
        $this->assertAttributeSame(null, 'filterPersister', $admin);
1447
        $admin->setFilterPersister($filterPersister);
1448
        $this->assertAttributeSame($filterPersister, 'filterPersister', $admin);
1449
        $this->assertAttributeSame(true, 'persistFilters', $admin);
1450
    }
1451
1452
    public function testGetRootCode(): void
1453
    {
1454
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1455
1456
        $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
1457
1458
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1459
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1460
        $parentFieldDescription->expects($this->once())
1461
            ->method('getAdmin')
1462
            ->willReturn($parentAdmin);
1463
1464
        $this->assertNull($admin->getParentFieldDescription());
1465
        $admin->setParentFieldDescription($parentFieldDescription);
1466
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1467
        $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
1468
    }
1469
1470
    public function testGetRoot(): void
1471
    {
1472
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1473
1474
        $this->assertSame($admin, $admin->getRoot());
1475
1476
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1477
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1478
        $parentFieldDescription->expects($this->once())
1479
            ->method('getAdmin')
1480
            ->willReturn($parentAdmin);
1481
1482
        $this->assertNull($admin->getParentFieldDescription());
1483
        $admin->setParentFieldDescription($parentFieldDescription);
1484
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1485
        $this->assertSame($parentAdmin, $admin->getRoot());
1486
    }
1487
1488
    public function testGetExportFields(): void
1489
    {
1490
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1491
1492
        $modelManager = $this->createMock(ModelManagerInterface::class);
1493
        $modelManager->expects($this->once())
1494
            ->method('getExportFields')
1495
            ->with($this->equalTo('NewsBundle\Entity\Post'))
1496
            ->willReturn(['foo', 'bar']);
1497
1498
        $admin->setModelManager($modelManager);
1499
        $this->assertSame(['foo', 'bar'], $admin->getExportFields());
1500
    }
1501
1502
    public function testGetPersistentParametersWithNoExtension(): void
1503
    {
1504
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1505
1506
        $this->assertEmpty($admin->getPersistentParameters());
1507
    }
1508
1509
    public function testGetPersistentParametersWithInvalidExtension(): void
1510
    {
1511
        $this->expectException(\RuntimeException::class);
1512
1513
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1514
1515
        $extension = $this->createMock(AdminExtensionInterface::class);
1516
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn(null);
1517
1518
        $admin->addExtension($extension);
1519
1520
        $admin->getPersistentParameters();
1521
    }
1522
1523
    public function testGetPersistentParametersWithValidExtension(): void
1524
    {
1525
        $expected = [
1526
            'context' => 'foobar',
1527
        ];
1528
1529
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1530
1531
        $extension = $this->createMock(AdminExtensionInterface::class);
1532
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn($expected);
1533
1534
        $admin->addExtension($extension);
1535
1536
        $this->assertSame($expected, $admin->getPersistentParameters());
1537
    }
1538
1539
    public function testGetFormWithNonCollectionParentValue(): void
1540
    {
1541
        $post = new Post();
1542
        $tagAdmin = $this->createTagAdmin($post);
1543
        $tag = $tagAdmin->getSubject();
1544
1545
        $tag->setPosts(null);
1546
        $tagAdmin->getForm();
1547
        $this->assertSame($post, $tag->getPosts());
1548
    }
1549
1550
    public function testGetFormWithCollectionParentValue(): void
1551
    {
1552
        $post = new Post();
1553
        $tagAdmin = $this->createTagAdmin($post);
1554
        $tag = $tagAdmin->getSubject();
1555
1556
        // Case of a doctrine collection
1557
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1558
        $this->assertCount(0, $tag->getPosts());
1559
1560
        $tag->addPost(new Post());
1561
1562
        $this->assertCount(1, $tag->getPosts());
1563
1564
        $tagAdmin->getForm();
1565
1566
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1567
        $this->assertCount(2, $tag->getPosts());
1568
        $this->assertContains($post, $tag->getPosts());
1569
1570
        // Case of an array
1571
        $tag->setPosts([]);
1572
        $this->assertCount(0, $tag->getPosts());
1573
1574
        $tag->addPost(new Post());
1575
1576
        $this->assertCount(1, $tag->getPosts());
1577
1578
        $tagAdmin->getForm();
1579
1580
        $this->assertInternalType('array', $tag->getPosts());
1581
        $this->assertCount(2, $tag->getPosts());
1582
        $this->assertContains($post, $tag->getPosts());
1583
    }
1584
1585
    public function testFormAddPostSubmitEventForPreValidation(): void
1586
    {
1587
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1588
        $object = new \stdClass();
1589
1590
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
1591
        $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy);
1592
1593
        $validator = $this->createMock(ValidatorInterface::class);
1594
        $validator
1595
                ->method('getMetadataFor')
1596
                ->willReturn($this->createMock(MemberMetadata::class));
1597
        $modelAdmin->setValidator($validator);
1598
1599
        $modelManager = $this->createMock(ModelManagerInterface::class);
1600
        $modelManager
1601
            ->method('getNewFieldDescriptionInstance')
1602
            ->willReturn(new FieldDescription());
1603
        $modelAdmin->setModelManager($modelManager);
1604
1605
        // a Admin class to test that preValidate is called
1606
        $testAdminPreValidate = $this->createMock(AbstractAdmin::class);
1607
        $testAdminPreValidate->expects($this->once())
1608
                ->method('preValidate')
1609
                ->with($this->identicalTo($object));
1610
1611
        $event = $this->createMock(FormEvent::class);
1612
        $event
1613
                ->method('getData')
1614
                ->willReturn($object);
1615
1616
        $formBuild = $this->createMock(FormBuilder::class);
1617
        $formBuild->expects($this->once())
1618
                ->method('addEventListener')
1619
                ->with($this->identicalTo(FormEvents::POST_SUBMIT),
1620
                        $this->callback(static function ($callback) use ($testAdminPreValidate, $event): bool {
1621
                            if (\is_callable($callback)) {
1622
                                $closure = $callback->bindTo($testAdminPreValidate);
0 ignored issues
show
Bug introduced by
The method bindTo cannot be called on $callback (of type callable).

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

Loading history...
1623
                                $closure($event);
1624
1625
                                return true;
1626
                            }
1627
1628
                            return false;
1629
                        }),
1630
                        $this->greaterThan(0)
1631
                    );
1632
1633
        $formContractor = $this->createMock(FormContractorInterface::class);
1634
        $formContractor
1635
                ->method('getDefaultOptions')
1636
                ->willReturn([]);
1637
        $formContractor
1638
                ->method('getFormBuilder')
1639
                ->willReturn($formBuild);
1640
1641
        $modelAdmin->setFormContractor($formContractor);
1642
        $modelAdmin->defineFormBuilder($formBuild);
1643
        $modelAdmin->getForm();
1644
    }
1645
1646
    public function testRemoveFieldFromFormGroup(): void
1647
    {
1648
        $formGroups = [
1649
            'foobar' => [
1650
                'fields' => [
1651
                    'foo' => 'foo',
1652
                    'bar' => 'bar',
1653
                ],
1654
            ],
1655
        ];
1656
1657
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1658
        $admin->setFormGroups($formGroups);
1659
1660
        $admin->removeFieldFromFormGroup('foo');
1661
        $this->assertSame($admin->getFormGroups(), [
1662
            'foobar' => [
1663
                'fields' => [
1664
                    'bar' => 'bar',
1665
                ],
1666
            ],
1667
        ]);
1668
1669
        $admin->removeFieldFromFormGroup('bar');
1670
        $this->assertSame($admin->getFormGroups(), []);
1671
    }
1672
1673
    public function testGetFilterParameters(): void
1674
    {
1675
        $authorId = uniqid();
1676
1677
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1678
1679
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
1680
        $commentAdmin->setParentAssociationMapping('post.author');
1681
        $commentAdmin->setParent($postAdmin);
1682
1683
        $request = $this->createMock(Request::class);
1684
        $query = $this->createMock(ParameterBag::class);
1685
        $query
1686
            ->method('get')
1687
            ->willReturn([
1688
                'filter' => [
1689
                    '_page' => '1',
1690
                    '_per_page' => '32',
1691
                ],
1692
            ]);
1693
1694
        $request->query = $query;
1695
        $request
1696
            ->method('get')
1697
            ->willReturn($authorId);
1698
1699
        $commentAdmin->setRequest($request);
1700
1701
        $modelManager = $this->createMock(ModelManagerInterface::class);
1702
        $modelManager
1703
            ->method('getDefaultSortValues')
1704
            ->willReturn([]);
1705
1706
        $commentAdmin->setModelManager($modelManager);
1707
1708
        $parameters = $commentAdmin->getFilterParameters();
1709
1710
        $this->assertTrue(isset($parameters['post__author']));
1711
        $this->assertSame(['value' => $authorId], $parameters['post__author']);
1712
    }
1713
1714
    public function testGetFilterFieldDescription(): void
1715
    {
1716
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1717
1718
        $fooFieldDescription = new FieldDescription();
1719
        $barFieldDescription = new FieldDescription();
1720
        $bazFieldDescription = new FieldDescription();
1721
1722
        $modelManager = $this->createMock(ModelManagerInterface::class);
1723
        $modelManager->expects($this->exactly(3))
1724
            ->method('getNewFieldDescriptionInstance')
1725
            ->willReturnCallback(static function ($adminClass, string $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) {
0 ignored issues
show
Unused Code introduced by
The parameter $filterOptions is not used and could be removed.

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

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

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

Loading history...
2014
                return $admin === $adminIn && 'DELETE' === $attributes;
2015
            });
2016
        $admin->setSecurityHandler($securityHandler);
2017
2018
        $this->assertSame($expected, $admin->getBatchActions());
2019
    }
2020
2021
    /**
2022
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2023
     */
2024
    public function testShowMosaicButton(): void
2025
    {
2026
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
2027
        $listModes = $admin->getListModes();
2028
2029
        $admin->showMosaicButton(true);
2030
2031
        $this->assertSame($listModes, $admin->getListModes());
2032
    }
2033
2034
    /**
2035
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2036
     */
2037
    public function testShowMosaicButtonHideMosaic(): void
2038
    {
2039
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
2040
        $listModes = $admin->getListModes();
2041
        $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...
2042
2043
        $admin->showMosaicButton(false);
2044
2045
        $this->assertSame($expected, $admin->getListModes());
2046
    }
2047
2048
    /**
2049
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
2050
     * @dataProvider provideGetBaseRouteName
2051
     */
2052
    public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void
2053
    {
2054
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2055
2056
        $routeGenerator = new DefaultRouteGenerator(
2057
            $this->createMock(RouterInterface::class),
2058
            new RoutesCache($this->cacheTempFolder, true)
2059
        );
2060
2061
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
2062
        $admin->setRouteBuilder($pathInfo);
2063
        $admin->setRouteGenerator($routeGenerator);
2064
        $admin->initialize();
2065
2066
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
2067
        $templateRegistry->getTemplate('action_create')->willReturn('Foo.html.twig');
2068
2069
        $admin->setTemplateRegistry($templateRegistry->reveal());
2070
2071
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2072
        $securityHandler
2073
            ->method('isGranted')
2074
            ->willReturnCallback(static function (AdminInterface $adminIn, string $attributes, $object = null) use ($admin): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

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

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