Completed
Pull Request — 3.x (#6148)
by Vincent
03:05
created

AdminTest   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 2526
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 53
dl 0
loc 2526
rs 5.76
c 0
b 0
f 0

123 Methods

Rating   Name   Duplication   Size   Complexity  
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 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 hp$0 ➔ persistFilters() 0 4 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 testSetUniqid() 0 9 1
A testToString() 0 17 1
A testIsAclEnabled() 0 10 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 testGetShowGroups() 0 12 1
A testGetFormGroups() 0 12 1
A testDeterminedPerPageValue() 0 8 1
B testIsGranted() 0 54 5
A testSupportsPreviewMode() 0 6 1
A testGetPermissionsShow() 0 8 1
A testShowIn() 0 17 2
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 14 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 20 1
A testGetFormWithArrayParentValue() 0 20 1
B testFormAddPostSubmitEventForPreValidation() 0 62 2
A testRemoveFieldFromFormGroup() 0 26 1
A testGetFilterParameters() 0 40 1
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 19 1
A testGetSubjectWithParentDescription() 0 29 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 15 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 testAdminWithoutControllerName() 0 6 1
A testAdminAvoidInifiniteLoop() 0 30 1
A testDeprecatedAbstractAdminConstructorArgs() 0 4 1
A getDeprecatedAbstractAdminConstructorArgs() 0 14 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.

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\Datagrid\ProxyQueryInterface;
36
use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface;
37
use Sonata\AdminBundle\Model\AuditManagerInterface;
38
use Sonata\AdminBundle\Model\ModelManagerInterface;
39
use Sonata\AdminBundle\Route\DefaultRouteGenerator;
40
use Sonata\AdminBundle\Route\PathInfoBuilder;
41
use Sonata\AdminBundle\Route\RouteGeneratorInterface;
42
use Sonata\AdminBundle\Route\RoutesCache;
43
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
44
use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
45
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
46
use Sonata\AdminBundle\Tests\App\Builder\DatagridBuilder;
47
use Sonata\AdminBundle\Tests\App\Builder\FormContractor;
48
use Sonata\AdminBundle\Tests\App\Builder\ListBuilder;
49
use Sonata\AdminBundle\Tests\App\Builder\ShowBuilder;
50
use Sonata\AdminBundle\Tests\App\Datagrid\Pager;
51
use Sonata\AdminBundle\Tests\Fixtures\Admin\AvoidInfiniteLoopAdmin;
52
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
53
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentVoteAdmin;
54
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentWithCustomRouteAdmin;
55
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
56
use Sonata\AdminBundle\Tests\Fixtures\Admin\FilteredAdmin;
57
use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
58
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
59
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin;
60
use Sonata\AdminBundle\Tests\Fixtures\Admin\TagAdmin;
61
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost;
62
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Comment;
63
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
64
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
65
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
66
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToStringNull;
67
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
68
use Sonata\Doctrine\Adapter\AdapterInterface;
69
use Symfony\Component\DependencyInjection\Container;
70
use Symfony\Component\DependencyInjection\ContainerInterface;
71
use Symfony\Component\Filesystem\Filesystem;
72
use Symfony\Component\Form\FormBuilder;
73
use Symfony\Component\Form\FormBuilderInterface;
74
use Symfony\Component\Form\FormEvent;
75
use Symfony\Component\Form\FormEvents;
76
use Symfony\Component\Form\FormFactory;
77
use Symfony\Component\Form\FormRegistry;
78
use Symfony\Component\Form\ResolvedFormTypeFactory;
79
use Symfony\Component\HttpFoundation\ParameterBag;
80
use Symfony\Component\HttpFoundation\Request;
81
use Symfony\Component\PropertyAccess\PropertyAccess;
82
use Symfony\Component\Routing\RouterInterface;
83
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
84
use Symfony\Component\Translation\TranslatorInterface;
85
use Symfony\Component\Validator\Mapping\MemberMetadata;
86
use Symfony\Component\Validator\Validator\ValidatorInterface;
87
88
class AdminTest extends TestCase
89
{
90
    protected $cacheTempFolder;
91
92
    protected function setUp(): void
93
    {
94
        $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
95
        $filesystem = new Filesystem();
96
        $filesystem->remove($this->cacheTempFolder);
97
    }
98
99
    /**
100
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct
101
     */
102
    public function testConstructor(): void
103
    {
104
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
105
        $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController';
106
107
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
108
        $this->assertInstanceOf(AbstractAdmin::class, $admin);
109
        $this->assertSame($class, $admin->getClass());
110
        $this->assertSame($baseControllerName, $admin->getBaseControllerName());
111
    }
112
113
    public function testGetClass(): void
114
    {
115
        $class = Post::class;
116
        $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController';
117
118
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
119
120
        $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class));
121
122
        $admin->setSubject(new BlogPost());
123
        $this->assertSame(BlogPost::class, $admin->getClass());
124
125
        $admin->setSubClasses(['foo']);
126
        $this->assertSame(BlogPost::class, $admin->getClass());
127
128
        $admin->setSubject(null);
129
        $admin->setSubClasses([]);
130
        $this->assertSame($class, $admin->getClass());
131
132
        $admin->setSubClasses(['foo' => 'bar']);
133
        $admin->setRequest(new Request(['subclass' => 'foo']));
134
        $this->assertSame('bar', $admin->getClass());
135
    }
136
137
    public function testGetClassException(): void
138
    {
139
        $this->expectException(\RuntimeException::class);
140
        $this->expectExceptionMessage('Feature not implemented: an embedded admin cannot have subclass');
141
142
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
143
        $baseControllerName = 'Sonata\NewsBundle\Controller\PostAdminController';
144
145
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
146
        $admin->setParentFieldDescription(new FieldDescription());
147
        $admin->setSubClasses(['foo' => 'bar']);
148
        $admin->setRequest(new Request(['subclass' => 'foo']));
149
        $admin->getClass();
150
    }
151
152
    public function testCheckAccessThrowsExceptionOnMadeUpAction(): void
153
    {
154
        $admin = new PostAdmin(
155
            'sonata.post.admin.post',
156
            'Application\Sonata\NewsBundle\Entity\Post',
157
            'Sonata\NewsBundle\Controller\PostAdminController'
158
        );
159
        $this->expectException(
160
            \InvalidArgumentException::class
161
        );
162
        $this->expectExceptionMessage(
163
            'Action "made-up" could not be found'
164
        );
165
        $admin->checkAccess('made-up');
166
    }
167
168
    public function testCheckAccessThrowsAccessDeniedException(): void
169
    {
170
        $admin = new PostAdmin(
171
            'sonata.post.admin.post',
172
            'Application\Sonata\NewsBundle\Entity\Post',
173
            'Sonata\NewsBundle\Controller\PostAdminController'
174
        );
175
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
176
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
177
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
178
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
179
        $customExtension->getAccessMapping($admin)->willReturn(
180
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
181
        );
182
        $admin->addExtension($customExtension->reveal());
183
        $admin->setSecurityHandler($securityHandler->reveal());
184
        $this->expectException(
185
            AccessDeniedException::class
186
        );
187
        $this->expectExceptionMessage(
188
            'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE'
189
        );
190
        $admin->checkAccess('custom_action');
191
    }
192
193
    public function testHasAccessOnMadeUpAction(): void
194
    {
195
        $admin = new PostAdmin(
196
            'sonata.post.admin.post',
197
            'Application\Sonata\NewsBundle\Entity\Post',
198
            'Sonata\NewsBundle\Controller\PostAdminController'
199
        );
200
201
        $this->assertFalse($admin->hasAccess('made-up'));
202
    }
203
204
    public function testHasAccess(): void
205
    {
206
        $admin = new PostAdmin(
207
            'sonata.post.admin.post',
208
            'Application\Sonata\NewsBundle\Entity\Post',
209
            'Sonata\NewsBundle\Controller\PostAdminController'
210
        );
211
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
212
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
213
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
214
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
215
        $customExtension->getAccessMapping($admin)->willReturn(
216
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
217
        );
218
        $admin->addExtension($customExtension->reveal());
219
        $admin->setSecurityHandler($securityHandler->reveal());
220
221
        $this->assertFalse($admin->hasAccess('custom_action'));
222
    }
223
224
    public function testHasAccessAllowsAccess(): void
225
    {
226
        $admin = new PostAdmin(
227
            'sonata.post.admin.post',
228
            'Application\Sonata\NewsBundle\Entity\Post',
229
            'Sonata\NewsBundle\Controller\PostAdminController'
230
        );
231
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
232
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
233
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true);
234
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
235
        $customExtension->getAccessMapping($admin)->willReturn(
236
            ['custom_action' => ['CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE']]
237
        );
238
        $admin->addExtension($customExtension->reveal());
239
        $admin->setSecurityHandler($securityHandler->reveal());
240
241
        $this->assertTrue($admin->hasAccess('custom_action'));
242
    }
243
244
    public function testHasAccessAllowsAccessEditAction(): void
245
    {
246
        $admin = new PostAdmin(
247
            'sonata.post.admin.post',
248
            'Application\Sonata\NewsBundle\Entity\Post',
249
            'Sonata\NewsBundle\Controller\PostAdminController'
250
        );
251
        $securityHandler = $this->prophesize(SecurityHandlerInterface::class);
252
        $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true);
253
        $customExtension = $this->prophesize(AbstractAdminExtension::class);
254
        $customExtension->getAccessMapping($admin)->willReturn(
255
            ['edit_action' => ['EDIT_ROLE']]
256
        );
257
        $admin->addExtension($customExtension->reveal());
258
        $admin->setSecurityHandler($securityHandler->reveal());
259
260
        $this->assertTrue($admin->hasAccess('edit_action'));
261
    }
262
263
    /**
264
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild
265
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild
266
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild
267
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild
268
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren
269
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren
270
     */
271
    public function testChildren(): void
272
    {
273
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
274
        $this->assertFalse($postAdmin->hasChildren());
275
        $this->assertFalse($postAdmin->hasChild('comment'));
276
277
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
278
        $postAdmin->addChild($commentAdmin, 'post');
279
280
        $this->assertTrue($postAdmin->hasChildren());
281
        $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment'));
282
283
        $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode());
284
        $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute());
285
        $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent());
286
        $this->assertSame('post', $commentAdmin->getParentAssociationMapping());
287
288
        $this->assertFalse($postAdmin->isChild());
289
        $this->assertTrue($commentAdmin->isChild());
290
291
        $this->assertSame(['sonata.post.admin.comment' => $commentAdmin], $postAdmin->getChildren());
292
    }
293
294
    /**
295
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure
296
     */
297
    public function testConfigure(): void
298
    {
299
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
300
        $this->assertNotNull($admin->getUniqid());
301
302
        $admin->initialize();
303
        $this->assertNotNull($admin->getUniqid());
304
        $this->assertSame('Post', $admin->getClassnameLabel());
305
306
        $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
307
        $admin->setClassnameLabel('postcomment');
308
309
        $admin->initialize();
310
        $this->assertSame('postcomment', $admin->getClassnameLabel());
311
    }
312
313
    public function testConfigureWithValidParentAssociationMapping(): void
314
    {
315
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
316
        $admin->setParentAssociationMapping('Category');
317
318
        $admin->initialize();
319
        $this->assertSame('Category', $admin->getParentAssociationMapping());
320
    }
321
322
    public function provideGetBaseRoutePattern()
323
    {
324
        return [
325
            [
326
                'Application\Sonata\NewsBundle\Entity\Post',
327
                '/sonata/news/post',
328
            ],
329
            [
330
                'Application\Sonata\NewsBundle\Document\Post',
331
                '/sonata/news/post',
332
            ],
333
            [
334
                'MyApplication\MyBundle\Entity\Post',
335
                '/myapplication/my/post',
336
            ],
337
            [
338
                'MyApplication\MyBundle\Entity\Post\Category',
339
                '/myapplication/my/post-category',
340
            ],
341
            [
342
                'MyApplication\MyBundle\Entity\Product\Category',
343
                '/myapplication/my/product-category',
344
            ],
345
            [
346
                'MyApplication\MyBundle\Entity\Other\Product\Category',
347
                '/myapplication/my/other-product-category',
348
            ],
349
            [
350
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
351
                '/cmf/foo/menu',
352
            ],
353
            [
354
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
355
                '/cmf/foo/menu',
356
            ],
357
            [
358
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
359
                '/symfony/barbar/menu',
360
            ],
361
            [
362
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
363
                '/symfony/barbar/menu-item',
364
            ],
365
            [
366
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
367
                '/cmf/foo/menu',
368
            ],
369
            [
370
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
371
                '/cmf/foo/menu',
372
            ],
373
            [
374
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
375
                '/cmf/foo/menu',
376
            ],
377
            [
378
                'AppBundle\Entity\User',
379
                '/app/user',
380
            ],
381
            [
382
                'App\Entity\User',
383
                '/app/user',
384
            ],
385
        ];
386
    }
387
388
    /**
389
     * @dataProvider provideGetBaseRoutePattern
390
     */
391
    public function testGetBaseRoutePattern(string $objFqn, string $expected): void
392
    {
393
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
394
        $this->assertSame($expected, $admin->getBaseRoutePattern());
395
    }
396
397
    /**
398
     * @dataProvider provideGetBaseRoutePattern
399
     */
400
    public function testGetBaseRoutePatternWithChildAdmin(string $objFqn, string $expected): void
401
    {
402
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
403
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
404
        $commentAdmin->setParent($postAdmin);
405
406
        $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern());
407
    }
408
409
    /**
410
     * @dataProvider provideGetBaseRoutePattern
411
     */
412
    public function testGetBaseRoutePatternWithTwoNestedChildAdmin(string $objFqn, string $expected): void
413
    {
414
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
415
        $commentAdmin = new CommentAdmin(
416
            'sonata.post.admin.comment',
417
            'Application\Sonata\NewsBundle\Entity\Comment',
418
            'Sonata\NewsBundle\Controller\CommentAdminController'
419
        );
420
        $commentVoteAdmin = new CommentVoteAdmin(
421
            'sonata.post.admin.comment_vote',
422
            'Application\Sonata\NewsBundle\Entity\CommentVote',
423
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
424
        );
425
        $commentAdmin->setParent($postAdmin);
426
        $commentVoteAdmin->setParent($commentAdmin);
427
428
        $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern());
429
    }
430
431
    public function testGetBaseRoutePatternWithSpecifedPattern(): void
432
    {
433
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostWithCustomRouteAdminController');
434
435
        $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern());
436
    }
437
438
    public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern(): void
439
    {
440
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
441
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController');
442
        $commentAdmin->setParent($postAdmin);
443
444
        $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern());
445
    }
446
447
    public function testGetBaseRoutePatternWithUnreconizedClassname(): void
448
    {
449
        $this->expectException(\RuntimeException::class);
450
451
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
452
        $admin->getBaseRoutePattern();
453
    }
454
455
    public function provideGetBaseRouteName()
456
    {
457
        return [
458
            [
459
                'Application\Sonata\NewsBundle\Entity\Post',
460
                'admin_sonata_news_post',
461
            ],
462
            [
463
                'Application\Sonata\NewsBundle\Document\Post',
464
                'admin_sonata_news_post',
465
            ],
466
            [
467
                'MyApplication\MyBundle\Entity\Post',
468
                'admin_myapplication_my_post',
469
            ],
470
            [
471
                'MyApplication\MyBundle\Entity\Post\Category',
472
                'admin_myapplication_my_post_category',
473
            ],
474
            [
475
                'MyApplication\MyBundle\Entity\Product\Category',
476
                'admin_myapplication_my_product_category',
477
            ],
478
            [
479
                'MyApplication\MyBundle\Entity\Other\Product\Category',
480
                'admin_myapplication_my_other_product_category',
481
            ],
482
            [
483
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
484
                'admin_cmf_foo_menu',
485
            ],
486
            [
487
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
488
                'admin_cmf_foo_menu',
489
            ],
490
            [
491
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
492
                'admin_symfony_barbar_menu',
493
            ],
494
            [
495
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
496
                'admin_symfony_barbar_menu_item',
497
            ],
498
            [
499
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
500
                'admin_cmf_foo_menu',
501
            ],
502
            [
503
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
504
                'admin_cmf_foo_menu',
505
            ],
506
            [
507
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
508
                'admin_cmf_foo_menu',
509
            ],
510
            [
511
                'AppBundle\Entity\User',
512
                'admin_app_user',
513
            ],
514
            [
515
                'App\Entity\User',
516
                'admin_app_user',
517
            ],
518
        ];
519
    }
520
521
    /**
522
     * @dataProvider provideGetBaseRouteName
523
     */
524
    public function testGetBaseRouteName(string $objFqn, string $expected): void
525
    {
526
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
527
528
        $this->assertSame($expected, $admin->getBaseRouteName());
529
    }
530
531
    /**
532
     * @group legacy
533
     * @expectedDeprecation Calling "addChild" without second argument is deprecated since sonata-project/admin-bundle 3.35 and will not be allowed in 4.0.
534
     * @dataProvider provideGetBaseRouteName
535
     */
536
    public function testGetBaseRouteNameWithChildAdmin(string $objFqn, string $expected): void
537
    {
538
        $routeGenerator = new DefaultRouteGenerator(
539
            $this->createMock(RouterInterface::class),
540
            new RoutesCache($this->cacheTempFolder, true)
541
        );
542
543
        $container = new Container();
544
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
545
546
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
547
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
548
        $container->set('sonata.post.admin.post', $postAdmin);
549
        $postAdmin->setConfigurationPool($pool);
550
        $postAdmin->setRouteBuilder($pathInfo);
551
        $postAdmin->setRouteGenerator($routeGenerator);
552
        $postAdmin->initialize();
553
554
        $commentAdmin = new CommentAdmin(
555
            'sonata.post.admin.comment',
556
            'Application\Sonata\NewsBundle\Entity\Comment',
557
            'Sonata\NewsBundle\Controller\CommentAdminController'
558
        );
559
        $container->set('sonata.post.admin.comment', $commentAdmin);
560
        $commentAdmin->setConfigurationPool($pool);
561
        $commentAdmin->setRouteBuilder($pathInfo);
562
        $commentAdmin->setRouteGenerator($routeGenerator);
563
        $commentAdmin->initialize();
564
565
        $postAdmin->addChild($commentAdmin, 'post');
566
567
        $commentVoteAdmin = new CommentVoteAdmin(
568
            'sonata.post.admin.comment_vote',
569
            'Application\Sonata\NewsBundle\Entity\CommentVote',
570
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
571
        );
572
573
        $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin);
574
        $commentVoteAdmin->setConfigurationPool($pool);
575
        $commentVoteAdmin->setRouteBuilder($pathInfo);
576
        $commentVoteAdmin->setRouteGenerator($routeGenerator);
577
        $commentVoteAdmin->initialize();
578
579
        $commentAdmin->addChild($commentVoteAdmin);
580
        $pool->setAdminServiceIds([
581
            'sonata.post.admin.post',
582
            'sonata.post.admin.comment',
583
            'sonata.post.admin.comment_vote',
584
        ]);
585
586
        $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
587
588
        $this->assertTrue($postAdmin->hasRoute('show'));
589
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show'));
590
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show'));
591
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show'));
592
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
593
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list'));
594
        $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
595
        $this->assertFalse($commentAdmin->hasRoute('edit'));
596
        $this->assertSame('post', $commentAdmin->getParentAssociationMapping());
597
598
        /*
599
         * Test the route name from request
600
         */
601
        $postListRequest = new Request(
602
            [],
603
            [],
604
            [
605
                '_route' => $postAdmin->getBaseRouteName().'_list',
606
            ]
607
        );
608
609
        $postAdmin->setRequest($postListRequest);
610
        $commentAdmin->setRequest($postListRequest);
611
612
        $this->assertTrue($postAdmin->isCurrentRoute('list'));
613
        $this->assertFalse($postAdmin->isCurrentRoute('create'));
614
        $this->assertFalse($commentAdmin->isCurrentRoute('list'));
615
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('list'));
616
        $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
617
        $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
618
        $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
619
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
620
    }
621
622
    public function testGetBaseRouteNameWithUnreconizedClassname(): void
623
    {
624
        $this->expectException(\RuntimeException::class);
625
626
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
627
        $admin->getBaseRouteName();
628
    }
629
630
    public function testGetBaseRouteNameWithSpecifiedName(): void
631
    {
632
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
633
634
        $this->assertSame('post_custom', $postAdmin->getBaseRouteName());
635
    }
636
637
    public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName(): void
638
    {
639
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
640
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController');
641
        $commentAdmin->setParent($postAdmin);
642
643
        $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName());
644
    }
645
646
    public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName(): void
647
    {
648
        $postAdmin = new PostAdmin(
649
            'sonata.post.admin.post',
650
            'Application\Sonata\NewsBundle\Entity\Post',
651
            'Sonata\NewsBundle\Controller\PostAdminController'
652
        );
653
        $commentAdmin = new CommentWithCustomRouteAdmin(
654
            'sonata.post.admin.comment_with_custom_route',
655
            'Application\Sonata\NewsBundle\Entity\Comment',
656
            'Sonata\NewsBundle\Controller\CommentWithCustomRouteAdminController'
657
        );
658
        $commentVoteAdmin = new CommentVoteAdmin(
659
            'sonata.post.admin.comment_vote',
660
            'Application\Sonata\NewsBundle\Entity\CommentVote',
661
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
662
        );
663
        $commentAdmin->setParent($postAdmin);
664
        $commentVoteAdmin->setParent($commentAdmin);
665
666
        $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName());
667
    }
668
669
    /**
670
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid
671
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid
672
     */
673
    public function testSetUniqid(): void
674
    {
675
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
676
677
        $uniqid = uniqid();
678
        $admin->setUniqid($uniqid);
679
680
        $this->assertSame($uniqid, $admin->getUniqid());
681
    }
682
683
    public function testToString(): void
684
    {
685
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
686
687
        $s = new \stdClass();
688
689
        $this->assertNotEmpty($admin->toString($s));
690
691
        $s = new FooToString();
692
        $this->assertSame('salut', $admin->toString($s));
693
694
        // To string method is implemented, but returns null
695
        $s = new FooToStringNull();
696
        $this->assertNotEmpty($admin->toString($s));
697
698
        $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...
699
    }
700
701
    public function testIsAclEnabled(): void
702
    {
703
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
704
705
        $this->assertFalse($postAdmin->isAclEnabled());
706
707
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
708
        $commentAdmin->setSecurityHandler($this->createMock(AclSecurityHandlerInterface::class));
709
        $this->assertTrue($commentAdmin->isAclEnabled());
710
    }
711
712
    /**
713
     * @group legacy
714
     *
715
     * @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.
716
     *
717
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses
718
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass
719
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses
720
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass
721
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
722
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass
723
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode
724
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass
725
     */
726
    public function testSubClass(): void
727
    {
728
        // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations
729
        $admin = new PostAdmin(
730
            'sonata.post.admin.post',
731
            Post::class,
732
            'Sonata\NewsBundle\Controller\PostAdminController'
733
        );
734
        $this->assertFalse($admin->hasSubClass('test'));
735
        $this->assertFalse($admin->hasActiveSubClass());
736
        $this->assertCount(0, $admin->getSubClasses());
737
        $this->assertNull($admin->getActiveSubClass());
738
        $this->assertNull($admin->getActiveSubclassCode());
739
        $this->assertSame(Post::class, $admin->getClass());
740
741
        // Just for the record, if there is no inheritance set, the getSubject is not used
742
        // the getSubject can also lead to some issue
743
        $admin->setSubject(new BlogPost());
744
        $this->assertSame(BlogPost::class, $admin->getClass());
745
746
        $admin->setSubClasses([
747
            'extended1' => 'NewsBundle\Entity\PostExtended1',
748
            'extended2' => 'NewsBundle\Entity\PostExtended2',
749
        ]);
750
        $this->assertFalse($admin->hasSubClass('test'));
751
        $this->assertTrue($admin->hasSubClass('extended1'));
752
        $this->assertFalse($admin->hasActiveSubClass());
753
        $this->assertCount(2, $admin->getSubClasses());
754
        // NEXT_MAJOR: remove the following 2 `assertNull()` assertions
755
        $this->assertNull($admin->getActiveSubClass());
756
        $this->assertNull($admin->getActiveSubclassCode());
757
        $this->assertSame(
758
            BlogPost::class,
759
            $admin->getClass(),
760
            'When there is no subclass in the query the class parameter should be returned'
761
        );
762
763
        $request = new Request(['subclass' => 'extended1']);
764
        $admin->setRequest($request);
765
        $this->assertFalse($admin->hasSubClass('test'));
766
        $this->assertTrue($admin->hasSubClass('extended1'));
767
        $this->assertTrue($admin->hasActiveSubClass());
768
        $this->assertCount(2, $admin->getSubClasses());
769
        $this->assertSame(
770
            'NewsBundle\Entity\PostExtended1',
771
            $admin->getActiveSubClass(),
772
            'It should return the curently active sub class.'
773
        );
774
        $this->assertSame('extended1', $admin->getActiveSubclassCode());
775
        $this->assertSame(
776
            'NewsBundle\Entity\PostExtended1',
777
            $admin->getClass(),
778
            'getClass() should return the name of the sub class when passed through a request query parameter.'
779
        );
780
781
        $request->query->set('subclass', 'inject');
782
783
        $this->assertNull($admin->getActiveSubclassCode());
784
        // NEXT_MAJOR: remove the previous `assertNull()` assertion and uncomment the following lines
785
        // $this->expectException(\LogicException::class);
786
        // $this->expectExceptionMessage(sprintf('Admin "%s" has no active subclass.', PostAdmin::class));
787
    }
788
789
    /**
790
     * @group legacy
791
     *
792
     * @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.
793
     */
794
    public function testNonExistantSubclass(): void
795
    {
796
        // NEXT_MAJOR: Remove the "@group" and "@expectedDeprecation" annotations
797
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
798
        $admin->setModelManager($this->getMockForAbstractClass(ModelManagerInterface::class));
799
800
        $admin->setRequest(new Request(['subclass' => 'inject']));
801
802
        $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2']);
803
804
        $this->assertTrue($admin->hasActiveSubClass());
805
806
        $this->expectException(\RuntimeException::class);
807
808
        $admin->getActiveSubClass();
809
    }
810
811
    /**
812
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
813
     */
814
    public function testOnlyOneSubclassNeededToBeActive(): void
815
    {
816
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
817
        $admin->setSubClasses(['extended1' => 'NewsBundle\Entity\PostExtended1']);
818
        $request = new Request(['subclass' => 'extended1']);
819
        $admin->setRequest($request);
820
        $this->assertTrue($admin->hasActiveSubClass());
821
    }
822
823
    /**
824
     * @group legacy
825
     * @expectedDeprecation Method "Sonata\AdminBundle\Admin\AbstractAdmin::addSubClass" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0.
826
     */
827
    public function testAddSubClassIsDeprecated(): void
828
    {
829
        $admin = new PostAdmin(
830
            'sonata.post.admin.post',
831
            Post::class,
832
            'Sonata\NewsBundle\Controller\PostAdminController'
833
        );
834
        $admin->addSubClass('whatever');
835
    }
836
837
    /**
838
     * @group legacy
839
     */
840
    public function testGetPerPageOptions(): void
841
    {
842
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
843
844
        $perPageOptions = $admin->getPerPageOptions();
845
846
        foreach ($perPageOptions as $perPage) {
847
            $this->assertSame(0, $perPage % 4);
848
        }
849
850
        $admin->setPerPageOptions([500, 1000]);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...in::setPerPageOptions() has been deprecated with message: since sonata-project/admin-bundle 3.67, to be removed in 4.0. Set custom per page options.

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...
851
        $this->assertSame([500, 1000], $admin->getPerPageOptions());
852
    }
853
854
    public function testGetLabelTranslatorStrategy(): void
855
    {
856
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
857
858
        $this->assertNull($admin->getLabelTranslatorStrategy());
859
860
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
861
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
862
        $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy());
863
    }
864
865
    public function testGetRouteBuilder(): void
866
    {
867
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
868
869
        $this->assertNull($admin->getRouteBuilder());
870
871
        $routeBuilder = $this->createMock(RouteBuilderInterface::class);
872
        $admin->setRouteBuilder($routeBuilder);
873
        $this->assertSame($routeBuilder, $admin->getRouteBuilder());
874
    }
875
876
    public function testGetMenuFactory(): void
877
    {
878
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
879
880
        $this->assertNull($admin->getMenuFactory());
881
882
        $menuFactory = $this->createMock(FactoryInterface::class);
883
        $admin->setMenuFactory($menuFactory);
884
        $this->assertSame($menuFactory, $admin->getMenuFactory());
885
    }
886
887
    public function testGetExtensions(): void
888
    {
889
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
890
891
        $this->assertSame([], $admin->getExtensions());
892
893
        $adminExtension1 = $this->createMock(AdminExtensionInterface::class);
894
        $adminExtension2 = $this->createMock(AdminExtensionInterface::class);
895
896
        $admin->addExtension($adminExtension1);
897
        $admin->addExtension($adminExtension2);
898
        $this->assertSame([$adminExtension1, $adminExtension2], $admin->getExtensions());
899
    }
900
901
    public function testGetFilterTheme(): void
902
    {
903
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
904
905
        $this->assertSame([], $admin->getFilterTheme());
906
907
        $admin->setFilterTheme(['FooTheme']);
908
        $this->assertSame(['FooTheme'], $admin->getFilterTheme());
909
    }
910
911
    public function testGetFormTheme(): void
912
    {
913
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
914
915
        $this->assertSame([], $admin->getFormTheme());
916
917
        $admin->setFormTheme(['FooTheme']);
918
919
        $this->assertSame(['FooTheme'], $admin->getFormTheme());
920
    }
921
922
    public function testGetValidator(): void
923
    {
924
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
925
926
        $this->assertNull($admin->getValidator());
927
928
        $validator = $this->getMockForAbstractClass(ValidatorInterface::class);
929
930
        $admin->setValidator($validator);
931
        $this->assertSame($validator, $admin->getValidator());
932
    }
933
934
    public function testGetSecurityHandler(): void
935
    {
936
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
937
938
        $this->assertNull($admin->getSecurityHandler());
939
940
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
941
        $admin->setSecurityHandler($securityHandler);
942
        $this->assertSame($securityHandler, $admin->getSecurityHandler());
943
    }
944
945
    public function testGetSecurityInformation(): void
946
    {
947
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
948
949
        $this->assertSame([], $admin->getSecurityInformation());
950
951
        $securityInformation = [
952
            'GUEST' => ['VIEW', 'LIST'],
953
            'STAFF' => ['EDIT', 'LIST', 'CREATE'],
954
        ];
955
956
        $admin->setSecurityInformation($securityInformation);
957
        $this->assertSame($securityInformation, $admin->getSecurityInformation());
958
    }
959
960
    public function testGetManagerType(): void
961
    {
962
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
963
964
        $this->assertNull($admin->getManagerType());
965
966
        $admin->setManagerType('foo_orm');
967
        $this->assertSame('foo_orm', $admin->getManagerType());
968
    }
969
970
    public function testGetModelManager(): void
971
    {
972
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
973
974
        $this->assertNull($admin->getModelManager());
975
976
        $modelManager = $this->createMock(ModelManagerInterface::class);
977
978
        $admin->setModelManager($modelManager);
979
        $this->assertSame($modelManager, $admin->getModelManager());
980
    }
981
982
    /**
983
     * NEXT_MAJOR: remove this method.
984
     *
985
     * @group legacy
986
     */
987
    public function testGetBaseCodeRoute(): void
988
    {
989
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
990
991
        $this->assertSame('', $admin->getBaseCodeRoute());
992
993
        $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...
994
        $this->assertSame('foo', $admin->getBaseCodeRoute());
995
    }
996
997
    // NEXT_MAJOR: uncomment this method.
998
    // public function testGetBaseCodeRoute()
999
    // {
1000
    //     $postAdmin = new PostAdmin(
1001
    //         'sonata.post.admin.post',
1002
    //         'NewsBundle\Entity\Post',
1003
    //         'Sonata\NewsBundle\Controller\PostAdminController'
1004
    //     );
1005
    //     $commentAdmin = new CommentAdmin(
1006
    //         'sonata.post.admin.comment',
1007
    //         'Application\Sonata\NewsBundle\Entity\Comment',
1008
    //         'Sonata\NewsBundle\Controller\CommentAdminController'
1009
    //     );
1010
    //
1011
    //     $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute());
1012
    //
1013
    //     $postAdmin->addChild($commentAdmin);
1014
    //
1015
    //     $this->assertSame(
1016
    //         'sonata.post.admin.post|sonata.post.admin.comment',
1017
    //         $commentAdmin->getBaseCodeRoute()
1018
    //     );
1019
    // }
1020
1021
    public function testGetRouteGenerator(): void
1022
    {
1023
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1024
1025
        $this->assertNull($admin->getRouteGenerator());
1026
1027
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
1028
1029
        $admin->setRouteGenerator($routeGenerator);
1030
        $this->assertSame($routeGenerator, $admin->getRouteGenerator());
1031
    }
1032
1033
    public function testGetConfigurationPool(): void
1034
    {
1035
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1036
1037
        $this->assertNull($admin->getConfigurationPool());
1038
1039
        $pool = $this->getMockBuilder(Pool::class)
1040
            ->disableOriginalConstructor()
1041
            ->getMock();
1042
1043
        $admin->setConfigurationPool($pool);
1044
        $this->assertSame($pool, $admin->getConfigurationPool());
1045
    }
1046
1047
    public function testGetShowBuilder(): void
1048
    {
1049
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1050
1051
        $this->assertNull($admin->getShowBuilder());
1052
1053
        $showBuilder = $this->createMock(ShowBuilderInterface::class);
1054
1055
        $admin->setShowBuilder($showBuilder);
1056
        $this->assertSame($showBuilder, $admin->getShowBuilder());
1057
    }
1058
1059
    public function testGetListBuilder(): void
1060
    {
1061
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1062
1063
        $this->assertNull($admin->getListBuilder());
1064
1065
        $listBuilder = $this->createMock(ListBuilderInterface::class);
1066
1067
        $admin->setListBuilder($listBuilder);
1068
        $this->assertSame($listBuilder, $admin->getListBuilder());
1069
    }
1070
1071
    public function testGetDatagridBuilder(): void
1072
    {
1073
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1074
1075
        $this->assertNull($admin->getDatagridBuilder());
1076
1077
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
1078
1079
        $admin->setDatagridBuilder($datagridBuilder);
1080
        $this->assertSame($datagridBuilder, $admin->getDatagridBuilder());
1081
    }
1082
1083
    public function testGetFormContractor(): void
1084
    {
1085
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1086
1087
        $this->assertNull($admin->getFormContractor());
1088
1089
        $formContractor = $this->createMock(FormContractorInterface::class);
1090
1091
        $admin->setFormContractor($formContractor);
1092
        $this->assertSame($formContractor, $admin->getFormContractor());
1093
    }
1094
1095
    public function testGetRequest(): void
1096
    {
1097
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1098
1099
        $this->assertFalse($admin->hasRequest());
1100
1101
        $request = new Request();
1102
1103
        $admin->setRequest($request);
1104
        $this->assertSame($request, $admin->getRequest());
1105
        $this->assertTrue($admin->hasRequest());
1106
    }
1107
1108
    public function testGetRequestWithException(): void
1109
    {
1110
        $this->expectException(\RuntimeException::class);
1111
        $this->expectExceptionMessage('The Request object has not been set');
1112
1113
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1114
        $admin->getRequest();
1115
    }
1116
1117
    public function testGetTranslationDomain(): void
1118
    {
1119
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1120
1121
        $this->assertSame('messages', $admin->getTranslationDomain());
1122
1123
        $admin->setTranslationDomain('foo');
1124
        $this->assertSame('foo', $admin->getTranslationDomain());
1125
    }
1126
1127
    /**
1128
     * @group legacy
1129
     */
1130
    public function testGetTranslator(): void
1131
    {
1132
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1133
1134
        $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...
1135
1136
        $translator = $this->createMock(TranslatorInterface::class);
1137
1138
        $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...
1139
        $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...
1140
    }
1141
1142
    public function testGetShowGroups(): void
1143
    {
1144
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1145
1146
        // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call.
1147
        $this->assertFalse($admin->getShowGroups('sonata_deprecation_mute'));
1148
1149
        $groups = ['foo', 'bar', 'baz'];
1150
1151
        $admin->setShowGroups($groups);
1152
        $this->assertSame($groups, $admin->getShowGroups());
1153
    }
1154
1155
    public function testGetFormGroups(): void
1156
    {
1157
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1158
1159
        // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call.
1160
        $this->assertFalse($admin->getFormGroups('sonata_deprecation_mute'));
1161
1162
        $groups = ['foo', 'bar', 'baz'];
1163
1164
        $admin->setFormGroups($groups);
1165
        $this->assertSame($groups, $admin->getFormGroups());
1166
    }
1167
1168
    public function testGetMaxPageLinks(): void
1169
    {
1170
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1171
1172
        $this->assertSame(25, $admin->getMaxPageLinks());
1173
1174
        $admin->setMaxPageLinks(14);
1175
        $this->assertSame(14, $admin->getMaxPageLinks());
1176
    }
1177
1178
    /**
1179
     * @group legacy
1180
     */
1181
    public function testGetMaxPerPage(): void
1182
    {
1183
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1184
1185
        $this->assertSame(32, $admin->getMaxPerPage());
1186
1187
        $admin->setMaxPerPage(94);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setMaxPerPage() has been deprecated with message: since sonata-project/admin-bundle 3.67, to 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...
1188
        $this->assertSame(94, $admin->getMaxPerPage());
1189
    }
1190
1191
    public function testGetLabel(): void
1192
    {
1193
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1194
1195
        $this->assertNull($admin->getLabel());
1196
1197
        $admin->setLabel('FooLabel');
1198
        $this->assertSame('FooLabel', $admin->getLabel());
1199
    }
1200
1201
    public function testGetBaseController(): void
1202
    {
1203
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1204
1205
        $this->assertSame('Sonata\NewsBundle\Controller\PostAdminController', $admin->getBaseControllerName());
1206
1207
        $admin->setBaseControllerName('Sonata\NewsBundle\Controller\FooAdminController');
1208
        $this->assertSame('Sonata\NewsBundle\Controller\FooAdminController', $admin->getBaseControllerName());
1209
    }
1210
1211
    public function testGetTemplates(): void
1212
    {
1213
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1214
1215
        $templates = [
1216
            'list' => '@FooAdmin/CRUD/list.html.twig',
1217
            'show' => '@FooAdmin/CRUD/show.html.twig',
1218
            'edit' => '@FooAdmin/CRUD/edit.html.twig',
1219
        ];
1220
1221
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1222
        $templateRegistry->getTemplates()->shouldBeCalled()->willReturn($templates);
1223
1224
        $admin->setTemplateRegistry($templateRegistry->reveal());
1225
1226
        $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...
1227
    }
1228
1229
    public function testGetTemplate1(): void
1230
    {
1231
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1232
1233
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1234
        $templateRegistry->getTemplate('edit')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/edit.html.twig');
1235
        $templateRegistry->getTemplate('show')->shouldBeCalled()->willReturn('@FooAdmin/CRUD/show.html.twig');
1236
1237
        $admin->setTemplateRegistry($templateRegistry->reveal());
1238
1239
        $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...
1240
        $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...
1241
    }
1242
1243
    public function testGetIdParameter(): void
1244
    {
1245
        $postAdmin = new PostAdmin(
1246
            'sonata.post.admin.post',
1247
            'NewsBundle\Entity\Post',
1248
            'Sonata\NewsBundle\Controller\PostAdminController'
1249
        );
1250
1251
        $this->assertSame('id', $postAdmin->getIdParameter());
1252
        $this->assertFalse($postAdmin->isChild());
1253
1254
        $commentAdmin = new CommentAdmin(
1255
            'sonata.post.admin.comment',
1256
            'Application\Sonata\NewsBundle\Entity\Comment',
1257
            'Sonata\NewsBundle\Controller\CommentAdminController'
1258
        );
1259
        $commentAdmin->setParent($postAdmin);
1260
1261
        $this->assertTrue($commentAdmin->isChild());
1262
        $this->assertSame('childId', $commentAdmin->getIdParameter());
1263
1264
        $commentVoteAdmin = new CommentVoteAdmin(
1265
            'sonata.post.admin.comment_vote',
1266
            'Application\Sonata\NewsBundle\Entity\CommentVote',
1267
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
1268
        );
1269
        $commentVoteAdmin->setParent($commentAdmin);
1270
1271
        $this->assertTrue($commentVoteAdmin->isChild());
1272
        $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter());
1273
    }
1274
1275
    public function testGetExportFormats(): void
1276
    {
1277
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1278
1279
        $this->assertSame(['json', 'xml', 'csv', 'xls'], $admin->getExportFormats());
1280
    }
1281
1282
    public function testGetUrlsafeIdentifier(): void
1283
    {
1284
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1285
1286
        $model = new \stdClass();
1287
1288
        $modelManager = $this->createMock(ModelManagerInterface::class);
1289
        $modelManager->expects($this->once())
1290
            ->method('getUrlSafeIdentifier')
1291
            ->with($this->equalTo($model))
1292
            ->willReturn('foo');
1293
        $admin->setModelManager($modelManager);
1294
1295
        $this->assertSame('foo', $admin->getUrlSafeIdentifier($model));
1296
    }
1297
1298
    public function testDeterminedPerPageValue(): void
1299
    {
1300
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1301
1302
        $this->assertFalse($admin->determinedPerPageValue('foo'));
1303
        $this->assertFalse($admin->determinedPerPageValue(123));
1304
        $this->assertTrue($admin->determinedPerPageValue(16));
1305
    }
1306
1307
    public function testIsGranted(): void
1308
    {
1309
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1310
        $modelManager = $this->createStub(ModelManagerInterface::class);
1311
        $modelManager
1312
            ->method('getNormalizedIdentifier')
1313
            ->willReturnCallback(static function (?object $model = null): ?string {
1314
                return $model ? $model->id : null;
1315
            });
1316
1317
        $admin->setModelManager($modelManager);
1318
1319
        $entity1 = new \stdClass();
1320
        $entity1->id = '1';
1321
1322
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1323
        $securityHandler
1324
            ->expects($this->exactly(6))
1325
            ->method('isGranted')
1326
            ->willReturnCallback(static function (
1327
                AdminInterface $adminIn,
1328
                string $attributes,
1329
                ?object $object = null
1330
            ) use (
1331
                $admin,
1332
                $entity1
1333
            ): bool {
1334
                return $admin === $adminIn && 'FOO' === $attributes &&
1335
                    ($object === $admin || $object === $entity1);
1336
            });
1337
1338
        $admin->setSecurityHandler($securityHandler);
1339
1340
        $this->assertTrue($admin->isGranted('FOO'));
1341
        $this->assertTrue($admin->isGranted('FOO'));
1342
        $this->assertTrue($admin->isGranted('FOO', $entity1));
1343
        $this->assertTrue($admin->isGranted('FOO', $entity1));
1344
        $this->assertFalse($admin->isGranted('BAR'));
1345
        $this->assertFalse($admin->isGranted('BAR'));
1346
        $this->assertFalse($admin->isGranted('BAR', $entity1));
1347
        $this->assertFalse($admin->isGranted('BAR', $entity1));
1348
1349
        $entity2 = new \stdClass();
1350
        $entity2->id = '2';
1351
1352
        $this->assertFalse($admin->isGranted('BAR', $entity2));
1353
        $this->assertFalse($admin->isGranted('BAR', $entity2));
1354
1355
        $entity3 = new \stdClass();
1356
        $entity3->id = '3';
1357
1358
        $this->assertFalse($admin->isGranted('BAR', $entity3));
1359
        $this->assertFalse($admin->isGranted('BAR', $entity3));
1360
    }
1361
1362
    public function testSupportsPreviewMode(): void
1363
    {
1364
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1365
1366
        $this->assertFalse($admin->supportsPreviewMode());
1367
    }
1368
1369
    public function testGetPermissionsShow(): void
1370
    {
1371
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1372
1373
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
1374
        $this->assertSame(['LIST'], $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
1375
        $this->assertSame(['LIST'], $admin->getPermissionsShow('foo'));
1376
    }
1377
1378
    public function testShowIn(): void
1379
    {
1380
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1381
1382
        $securityHandler = $this->createMock(AclSecurityHandlerInterface::class);
1383
        $securityHandler
1384
            ->method('isGranted')
1385
            ->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...
1386
                return $admin === $adminIn && $attributes === ['LIST'];
1387
            });
1388
1389
        $admin->setSecurityHandler($securityHandler);
1390
1391
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
1392
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
1393
        $this->assertTrue($admin->showIn('foo'));
1394
    }
1395
1396
    public function testGetObjectIdentifier(): void
1397
    {
1398
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1399
1400
        $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
1401
    }
1402
1403
    /**
1404
     * @group legacy
1405
     */
1406
    public function testTrans(): void
1407
    {
1408
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
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('trans')
1416
            ->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1417
            ->willReturn('fooTranslated');
1418
1419
        $this->assertSame('fooTranslated', $admin->trans('foo'));
1420
    }
1421
1422
    /**
1423
     * @group legacy
1424
     */
1425
    public function testTransWithMessageDomain(): void
1426
    {
1427
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
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('trans')
1434
            ->with($this->equalTo('foo'), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1435
            ->willReturn('fooTranslated');
1436
1437
        $this->assertSame('fooTranslated', $admin->trans('foo', ['name' => 'Andrej'], 'fooMessageDomain'));
1438
    }
1439
1440
    /**
1441
     * @group legacy
1442
     */
1443
    public function testTransChoice(): void
1444
    {
1445
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1446
        $admin->setTranslationDomain('fooMessageDomain');
1447
1448
        $translator = $this->createMock(TranslatorInterface::class);
1449
        $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...
1450
1451
        $translator->expects($this->once())
1452
            ->method('transChoice')
1453
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo([]), $this->equalTo('fooMessageDomain'))
1454
            ->willReturn('fooTranslated');
1455
1456
        $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...
1457
    }
1458
1459
    /**
1460
     * @group legacy
1461
     */
1462
    public function testTransChoiceWithMessageDomain(): void
1463
    {
1464
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1465
1466
        $translator = $this->createMock(TranslatorInterface::class);
1467
        $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...
1468
1469
        $translator->expects($this->once())
1470
            ->method('transChoice')
1471
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(['name' => 'Andrej']), $this->equalTo('fooMessageDomain'))
1472
            ->willReturn('fooTranslated');
1473
1474
        $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...
1475
    }
1476
1477
    public function testSetFilterPersister(): void
1478
    {
1479
        $admin = new class('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle\Controller\PostAdminController') extends PostAdmin {
1480
            public function persistFilters(): bool
1481
            {
1482
                return $this->persistFilters;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\AdminBundle\Admin...tAdmin::$persistFilters has been deprecated with message: since sonata-project/admin-bundle 3.34, to be removed in 4.0.

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

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

Loading history...
1483
            }
1484
        };
1485
1486
        $filterPersister = $this->createMock(FilterPersisterInterface::class);
1487
1488
        $admin->setFilterPersister($filterPersister);
1489
        $this->assertTrue($admin->persistFilters());
1490
    }
1491
1492
    public function testGetRootCode(): void
1493
    {
1494
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1495
1496
        $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
1497
1498
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController');
1499
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1500
        $parentFieldDescription->expects($this->once())
1501
            ->method('getAdmin')
1502
            ->willReturn($parentAdmin);
1503
1504
        $this->assertFalse($admin->hasParentFieldDescription());
1505
        $admin->setParentFieldDescription($parentFieldDescription);
1506
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1507
        $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
1508
    }
1509
1510
    public function testGetRoot(): void
1511
    {
1512
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1513
1514
        $this->assertSame($admin, $admin->getRoot());
1515
1516
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'Sonata\NewsBundle\Controller\PostParentAdminController');
1517
        $parentFieldDescription = $this->createMock(FieldDescriptionInterface::class);
1518
        $parentFieldDescription->expects($this->once())
1519
            ->method('getAdmin')
1520
            ->willReturn($parentAdmin);
1521
1522
        $this->assertFalse($admin->hasParentFieldDescription());
1523
        $admin->setParentFieldDescription($parentFieldDescription);
1524
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1525
        $this->assertSame($parentAdmin, $admin->getRoot());
1526
    }
1527
1528
    public function testGetExportFields(): void
1529
    {
1530
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1531
1532
        $modelManager = $this->createMock(ModelManagerInterface::class);
1533
        $modelManager->expects($this->once())
1534
            ->method('getExportFields')
1535
            ->with($this->equalTo('NewsBundle\Entity\Post'))
1536
            ->willReturn(['foo', 'bar']);
1537
1538
        $admin->setModelManager($modelManager);
1539
        $this->assertSame(['foo', 'bar'], $admin->getExportFields());
1540
    }
1541
1542
    public function testGetPersistentParametersWithNoExtension(): void
1543
    {
1544
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1545
1546
        $this->assertEmpty($admin->getPersistentParameters());
1547
    }
1548
1549
    public function testGetPersistentParametersWithInvalidExtension(): void
1550
    {
1551
        $this->expectException(\RuntimeException::class);
1552
1553
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1554
1555
        $extension = $this->createMock(AdminExtensionInterface::class);
1556
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn(null);
1557
1558
        $admin->addExtension($extension);
1559
1560
        $admin->getPersistentParameters();
1561
    }
1562
1563
    public function testGetPersistentParametersWithValidExtension(): void
1564
    {
1565
        $expected = [
1566
            'context' => 'foobar',
1567
        ];
1568
1569
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1570
1571
        $extension = $this->createMock(AdminExtensionInterface::class);
1572
        $extension->expects($this->once())->method('getPersistentParameters')->willReturn($expected);
1573
1574
        $admin->addExtension($extension);
1575
1576
        $this->assertSame($expected, $admin->getPersistentParameters());
1577
    }
1578
1579
    public function testGetFormWithNonCollectionParentValue(): void
1580
    {
1581
        $post = new Post();
1582
        $tagAdmin = $this->createTagAdmin($post);
1583
        $tag = $tagAdmin->getSubject();
1584
1585
        $tag->setPosts(null);
1586
        $tagAdmin->getForm();
1587
        $this->assertSame($post, $tag->getPosts());
1588
    }
1589
1590
    public function testGetFormWithCollectionParentValue(): void
1591
    {
1592
        $post = new Post();
1593
        $tagAdmin = $this->createTagAdmin($post);
1594
        $tag = $tagAdmin->getSubject();
1595
1596
        // Case of a doctrine collection
1597
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1598
        $this->assertCount(0, $tag->getPosts());
1599
1600
        $tag->addPost(new Post());
1601
1602
        $this->assertCount(1, $tag->getPosts());
1603
1604
        $tagAdmin->getForm();
1605
1606
        $this->assertInstanceOf(Collection::class, $tag->getPosts());
1607
        $this->assertCount(2, $tag->getPosts());
1608
        $this->assertContains($post, $tag->getPosts());
1609
    }
1610
1611
    public function testGetFormWithArrayParentValue(): void
1612
    {
1613
        $post = new Post();
1614
        $tagAdmin = $this->createTagAdmin($post);
1615
        $tag = $tagAdmin->getSubject();
1616
1617
        // Case of an array
1618
        $tag->setPosts([]);
1619
        $this->assertCount(0, $tag->getPosts());
1620
1621
        $tag->addPost(new Post());
1622
1623
        $this->assertCount(1, $tag->getPosts());
1624
1625
        $tagAdmin->getForm();
1626
1627
        $this->assertIsArray($tag->getPosts());
1628
        $this->assertCount(2, $tag->getPosts());
1629
        $this->assertContains($post, $tag->getPosts());
1630
    }
1631
1632
    public function testFormAddPostSubmitEventForPreValidation(): void
1633
    {
1634
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', \stdClass::class, 'Sonata\FooBundle\Controller\ModelAdminController');
1635
        $object = new \stdClass();
1636
1637
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
1638
        $modelAdmin->setLabelTranslatorStrategy($labelTranslatorStrategy);
1639
1640
        $validator = $this->createMock(ValidatorInterface::class);
1641
        $validator
1642
                ->method('getMetadataFor')
1643
                ->willReturn($this->createMock(MemberMetadata::class));
1644
        $modelAdmin->setValidator($validator);
1645
1646
        $modelManager = $this->createMock(ModelManagerInterface::class);
1647
        $modelManager
1648
            ->method('getNewFieldDescriptionInstance')
1649
            ->willReturn(new FieldDescription());
1650
        $modelAdmin->setModelManager($modelManager);
1651
1652
        // a Admin class to test that preValidate is called
1653
        $testAdminPreValidate = $this->createMock(AbstractAdmin::class);
1654
        $testAdminPreValidate->expects($this->once())
1655
                ->method('preValidate')
1656
                ->with($this->identicalTo($object));
1657
1658
        $event = $this->createMock(FormEvent::class);
1659
        $event
1660
                ->method('getData')
1661
                ->willReturn($object);
1662
1663
        $formBuild = $this->createMock(FormBuilder::class);
1664
        $formBuild->expects($this->once())
1665
                ->method('addEventListener')
1666
                ->with(
1667
                    $this->identicalTo(FormEvents::POST_SUBMIT),
1668
                    $this->callback(static function ($callback) use ($testAdminPreValidate, $event): bool {
1669
                        if (\is_callable($callback)) {
1670
                            $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...
1671
                            $closure($event);
1672
1673
                            return true;
1674
                        }
1675
1676
                        return false;
1677
                    }),
1678
                    $this->greaterThan(0)
1679
                );
1680
1681
        $formContractor = $this->createMock(FormContractorInterface::class);
1682
        $formContractor
1683
                ->method('getDefaultOptions')
1684
                ->willReturn([]);
1685
        $formContractor
1686
                ->method('getFormBuilder')
1687
                ->willReturn($formBuild);
1688
1689
        $modelAdmin->setFormContractor($formContractor);
1690
        $modelAdmin->setSubject($object);
1691
        $modelAdmin->defineFormBuilder($formBuild);
1692
        $modelAdmin->getForm();
1693
    }
1694
1695
    public function testRemoveFieldFromFormGroup(): void
1696
    {
1697
        $formGroups = [
1698
            'foobar' => [
1699
                'fields' => [
1700
                    'foo' => 'foo',
1701
                    'bar' => 'bar',
1702
                ],
1703
            ],
1704
        ];
1705
1706
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1707
        $admin->setFormGroups($formGroups);
1708
1709
        $admin->removeFieldFromFormGroup('foo');
1710
        $this->assertSame($admin->getFormGroups(), [
1711
            'foobar' => [
1712
                'fields' => [
1713
                    'bar' => 'bar',
1714
                ],
1715
            ],
1716
        ]);
1717
1718
        $admin->removeFieldFromFormGroup('bar');
1719
        $this->assertSame($admin->getFormGroups(), []);
1720
    }
1721
1722
    public function testGetFilterParameters(): void
1723
    {
1724
        $authorId = uniqid();
1725
1726
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1727
1728
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
1729
        $commentAdmin->setParentAssociationMapping('post.author');
1730
        $commentAdmin->setParent($postAdmin);
1731
1732
        $request = $this->createMock(Request::class);
1733
        $query = $this->createMock(ParameterBag::class);
1734
        $query
1735
            ->method('get')
1736
            ->willReturn([
1737
                'filter' => [
1738
                    '_page' => '1',
1739
                    '_per_page' => '32',
1740
                ],
1741
            ]);
1742
1743
        $request->query = $query;
1744
        $request
1745
            ->method('get')
1746
            ->willReturn($authorId);
1747
1748
        $commentAdmin->setRequest($request);
1749
1750
        $modelManager = $this->createMock(ModelManagerInterface::class);
1751
        $modelManager
1752
            ->method('getDefaultSortValues')
1753
            ->willReturn([]);
1754
1755
        $commentAdmin->setModelManager($modelManager);
1756
1757
        $parameters = $commentAdmin->getFilterParameters();
1758
1759
        $this->assertTrue(isset($parameters['post__author']));
1760
        $this->assertSame(['value' => $authorId], $parameters['post__author']);
1761
    }
1762
1763
    public function testGetFilterFieldDescription(): void
1764
    {
1765
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1766
1767
        $fooFieldDescription = new FieldDescription();
1768
        $barFieldDescription = new FieldDescription();
1769
        $bazFieldDescription = new FieldDescription();
1770
1771
        $modelManager = $this->createMock(ModelManagerInterface::class);
1772
        $modelManager->expects($this->exactly(3))
1773
            ->method('getNewFieldDescriptionInstance')
1774
            ->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...
1775
                switch ($name) {
1776
                    case 'foo':
1777
                        $fieldDescription = $fooFieldDescription;
1778
1779
                        break;
1780
1781
                    case 'bar':
1782
                        $fieldDescription = $barFieldDescription;
1783
1784
                        break;
1785
1786
                    case 'baz':
1787
                        $fieldDescription = $bazFieldDescription;
1788
1789
                        break;
1790
1791
                    default:
1792
                        throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
1793
                        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...
1794
                }
1795
1796
                $fieldDescription->setName($name);
1797
1798
                return $fieldDescription;
1799
            });
1800
1801
        $modelAdmin->setModelManager($modelManager);
1802
1803
        $pager = $this->createMock(PagerInterface::class);
1804
1805
        $datagrid = $this->createMock(DatagridInterface::class);
1806
        $datagrid->expects($this->once())
1807
            ->method('getPager')
1808
            ->willReturn($pager);
1809
1810
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
1811
        $datagridBuilder->expects($this->once())
1812
            ->method('getBaseDatagrid')
1813
            ->with($this->identicalTo($modelAdmin), [])
1814
            ->willReturn($datagrid);
1815
1816
        $datagridBuilder->expects($this->exactly(3))
1817
            ->method('addFilter')
1818
            ->willReturnCallback(static function ($datagrid, $type, $fieldDescription, AdminInterface $admin): void {
1819
                $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
1820
                $fieldDescription->mergeOption('field_options', ['required' => false]);
1821
            });
1822
1823
        $modelAdmin->setDatagridBuilder($datagridBuilder);
1824
1825
        $this->assertSame(['foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription], $modelAdmin->getFilterFieldDescriptions());
1826
        $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
1827
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
1828
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
1829
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
1830
        $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
1831
        $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
1832
        $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
1833
    }
1834
1835
    public function testGetSubjectNoRequest(): void
1836
    {
1837
        $modelManager = $this->createMock(ModelManagerInterface::class);
1838
        $modelManager
1839
            ->expects($this->never())
1840
            ->method('find');
1841
1842
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1843
        $admin->setModelManager($modelManager);
1844
1845
        $this->assertFalse($admin->hasSubject());
1846
    }
1847
1848
    public function testGetSideMenu(): void
1849
    {
1850
        $item = $this->createMock(ItemInterface::class);
1851
        $item
1852
            ->expects($this->once())
1853
            ->method('setChildrenAttribute')
1854
            ->with('class', 'nav navbar-nav');
1855
        $item
1856
            ->expects($this->once())
1857
            ->method('setExtra')
1858
            ->with('translation_domain', 'foo_bar_baz');
1859
1860
        $menuFactory = $this->createMock(FactoryInterface::class);
1861
        $menuFactory
1862
            ->expects($this->once())
1863
            ->method('createItem')
1864
            ->willReturn($item);
1865
1866
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
1867
        $modelAdmin->setMenuFactory($menuFactory);
1868
        $modelAdmin->setTranslationDomain('foo_bar_baz');
1869
1870
        $modelAdmin->getSideMenu('foo');
1871
    }
1872
1873
    /**
1874
     * @return array
1875
     */
1876
    public function provideGetSubject()
1877
    {
1878
        return [
1879
            [23],
1880
            ['azerty'],
1881
            ['4f69bbb5f14a13347f000092'],
1882
            ['0779ca8d-e2be-11e4-ac58-0242ac11000b'],
1883
            ['123'.AdapterInterface::ID_SEPARATOR.'my_type'], // composite keys are supported
1884
        ];
1885
    }
1886
1887
    /**
1888
     * @dataProvider provideGetSubject
1889
     */
1890
    public function testGetSubjectFailed($id): void
1891
    {
1892
        $modelManager = $this->createMock(ModelManagerInterface::class);
1893
        $modelManager
1894
            ->expects($this->once())
1895
            ->method('find')
1896
            ->with('NewsBundle\Entity\Post', $id)
1897
            ->willReturn(null); // entity not found
1898
1899
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1900
        $admin->setModelManager($modelManager);
1901
1902
        $admin->setRequest(new Request(['id' => $id]));
1903
        $this->assertFalse($admin->hasSubject());
1904
    }
1905
1906
    /**
1907
     * @dataProvider provideGetSubject
1908
     */
1909
    public function testGetSubject($id): void
1910
    {
1911
        $model = new Post();
1912
1913
        $modelManager = $this->createMock(ModelManagerInterface::class);
1914
        $modelManager
1915
            ->expects($this->once())
1916
            ->method('find')
1917
            ->with('NewsBundle\Entity\Post', $id)
1918
            ->willReturn($model);
1919
1920
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1921
        $admin->setModelManager($modelManager);
1922
1923
        $admin->setRequest(new Request(['id' => $id]));
1924
        $this->assertTrue($admin->hasSubject());
1925
        $this->assertSame($model, $admin->getSubject());
1926
        $this->assertSame($model, $admin->getSubject()); // model manager must be used only once
1927
    }
1928
1929
    public function testGetSubjectWithParentDescription(): void
1930
    {
1931
        $adminId = 1;
1932
1933
        $comment = new Comment();
1934
1935
        $modelManager = $this->createMock(ModelManagerInterface::class);
1936
        $modelManager
1937
            ->method('find')
1938
            ->with('NewsBundle\Entity\Comment', $adminId)
1939
            ->willReturn($comment);
1940
1941
        $request = new Request(['id' => $adminId]);
1942
1943
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1944
        $postAdmin->setRequest($request);
1945
1946
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'Sonata\NewsBundle\Controller\CommentAdminController');
1947
        $commentAdmin->setRequest($request);
1948
        $commentAdmin->setModelManager($modelManager);
1949
1950
        $this->assertTrue($commentAdmin->hasSubject());
1951
        $this->assertSame($comment, $commentAdmin->getSubject());
1952
1953
        $commentAdmin->setSubject(null);
1954
        $commentAdmin->setParentFieldDescription(new FieldDescription());
1955
1956
        $this->assertFalse($commentAdmin->hasSubject());
1957
    }
1958
1959
    /**
1960
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1961
     */
1962
    public function testGetActionButtonsList(): void
1963
    {
1964
        $expected = [
1965
            'create' => [
1966
                'template' => 'Foo.html.twig',
1967
            ],
1968
        ];
1969
1970
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
1971
1972
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
1973
        $templateRegistry->getTemplate('button_create')->willReturn('Foo.html.twig');
1974
1975
        $admin->setTemplateRegistry($templateRegistry->reveal());
1976
1977
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
1978
        $securityHandler
1979
            ->expects($this->once())
1980
            ->method('isGranted')
1981
            ->with($admin, 'CREATE', $admin)
1982
            ->willReturn(true);
1983
        $admin->setSecurityHandler($securityHandler);
1984
1985
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
1986
        $routeGenerator
1987
            ->expects($this->once())
1988
            ->method('hasAdminRoute')
1989
            ->with($admin, 'create')
1990
            ->willReturn(true);
1991
        $admin->setRouteGenerator($routeGenerator);
1992
1993
        $this->assertSame($expected, $admin->getActionButtons('list', null));
1994
    }
1995
1996
    /**
1997
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1998
     */
1999
    public function testGetActionButtonsListCreateDisabled(): void
2000
    {
2001
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
2002
2003
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2004
        $securityHandler
2005
            ->expects($this->once())
2006
            ->method('isGranted')
2007
            ->with($admin, 'CREATE', $admin)
2008
            ->willReturn(false);
2009
        $admin->setSecurityHandler($securityHandler);
2010
2011
        $this->assertSame([], $admin->getActionButtons('list', null));
2012
    }
2013
2014
    /**
2015
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
2016
     */
2017
    public function testGetBatchActions(): void
2018
    {
2019
        $expected = [
2020
            'delete' => [
2021
                'label' => 'action_delete',
2022
                'translation_domain' => 'SonataAdminBundle',
2023
                'ask_confirmation' => true, // by default always true
2024
            ],
2025
            'foo' => [
2026
                'label' => 'action_foo',
2027
                'translation_domain' => 'SonataAdminBundle',
2028
            ],
2029
            'bar' => [
2030
                'label' => 'batch.label_bar',
2031
                'translation_domain' => 'SonataAdminBundle',
2032
            ],
2033
            'baz' => [
2034
                'label' => 'action_baz',
2035
                'translation_domain' => 'AcmeAdminBundle',
2036
            ],
2037
        ];
2038
2039
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2040
2041
        $labelTranslatorStrategy = $this->createMock(LabelTranslatorStrategyInterface::class);
2042
        $labelTranslatorStrategy
2043
            ->method('getLabel')
2044
            ->willReturnCallback(static function (string $label, string $context = '', string $type = ''): string {
2045
                return $context.'.'.$type.'_'.$label;
2046
            });
2047
2048
        $admin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
2049
        $admin->setRouteBuilder($pathInfo);
2050
        $admin->setTranslationDomain('SonataAdminBundle');
2051
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
2052
2053
        $routeGenerator = $this->createMock(RouteGeneratorInterface::class);
2054
        $routeGenerator
2055
            ->expects($this->once())
2056
            ->method('hasAdminRoute')
2057
            ->with($admin, 'delete')
2058
            ->willReturn(true);
2059
        $admin->setRouteGenerator($routeGenerator);
2060
2061
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2062
        $securityHandler
2063
            ->method('isGranted')
2064
            ->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...
2065
                return $admin === $adminIn && 'DELETE' === $attributes;
2066
            });
2067
        $admin->setSecurityHandler($securityHandler);
2068
2069
        $this->assertSame($expected, $admin->getBatchActions());
2070
    }
2071
2072
    /**
2073
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2074
     */
2075
    public function testShowMosaicButton(): void
2076
    {
2077
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
2078
        $listModes = $admin->getListModes();
2079
2080
        $admin->showMosaicButton(true);
2081
2082
        $this->assertSame($listModes, $admin->getListModes());
2083
    }
2084
2085
    /**
2086
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
2087
     */
2088
    public function testShowMosaicButtonHideMosaic(): void
2089
    {
2090
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController');
2091
        $listModes = $admin->getListModes();
2092
        $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...
2093
2094
        $admin->showMosaicButton(false);
2095
2096
        $this->assertSame($expected, $admin->getListModes());
2097
    }
2098
2099
    /**
2100
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
2101
     * @dataProvider provideGetBaseRouteName
2102
     */
2103
    public function testDefaultDashboardActionsArePresent(string $objFqn, string $expected): void
2104
    {
2105
        $pathInfo = new PathInfoBuilder($this->createMock(AuditManagerInterface::class));
2106
2107
        $routeGenerator = new DefaultRouteGenerator(
2108
            $this->createMock(RouterInterface::class),
2109
            new RoutesCache($this->cacheTempFolder, true)
2110
        );
2111
2112
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'Sonata\NewsBundle\Controller\PostAdminController');
2113
        $admin->setRouteBuilder($pathInfo);
2114
        $admin->setRouteGenerator($routeGenerator);
2115
        $admin->initialize();
2116
2117
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
2118
        $templateRegistry->getTemplate('action_create')->willReturn('Foo.html.twig');
2119
2120
        $admin->setTemplateRegistry($templateRegistry->reveal());
2121
2122
        $securityHandler = $this->createMock(SecurityHandlerInterface::class);
2123
        $securityHandler
2124
            ->method('isGranted')
2125
            ->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...
2126
                return $admin === $adminIn && ('CREATE' === $attributes || 'LIST' === $attributes);
2127
            });
2128
2129
        $admin->setSecurityHandler($securityHandler);
2130
2131
        $this->assertArrayHasKey('list', $admin->getDashboardActions());
2132
        $this->assertArrayHasKey('create', $admin->getDashboardActions());
2133
    }
2134
2135
    public function testDefaultFilters(): void
2136
    {
2137
        $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController');
2138
2139
        $subjectId = uniqid();
2140
2141
        $request = $this->createMock(Request::class);
2142
        $query = $this->createMock(ParameterBag::class);
2143
        $query
2144
            ->method('get')
2145
            ->with($this->equalTo('filter'))
2146
            ->willReturn([
2147
                'a' => [
2148
                    'value' => 'b',
2149
                ],
2150
                'foo' => [
2151
                    'type' => '1',
2152
                    'value' => 'bar',
2153
                ],
2154
                'baz' => [
2155
                    'type' => '5',
2156
                    'value' => 'test',
2157
                ],
2158
            ]);
2159
        $request->query = $query;
2160
2161
        $request
2162
            ->method('get')
2163
            ->willReturn($subjectId);
2164
2165
        $admin->setRequest($request);
2166
2167
        $modelManager = $this->createMock(ModelManagerInterface::class);
2168
        $modelManager
2169
            ->method('getDefaultSortValues')
2170
            ->willReturn([]);
2171
2172
        $admin->setModelManager($modelManager);
2173
2174
        $this->assertSame([
2175
            '_page' => 1,
2176
            '_per_page' => 32,
2177
            'foo' => [
2178
                'type' => '1',
2179
                'value' => 'bar',
2180
            ],
2181
            'baz' => [
2182
                'type' => '5',
2183
                'value' => 'test',
2184
            ],
2185
            'a' => [
2186
                'value' => 'b',
2187
            ],
2188
        ], $admin->getFilterParameters());
2189
2190
        $this->assertTrue($admin->isDefaultFilter('foo'));
2191
        $this->assertFalse($admin->isDefaultFilter('bar'));
2192
        $this->assertFalse($admin->isDefaultFilter('a'));
2193
    }
2194
2195
    /**
2196
     * @group legacy
2197
     */
2198
    public function testDefaultBreadcrumbsBuilder(): void
2199
    {
2200
        $container = $this->createMock(ContainerInterface::class);
2201
        $container->expects($this->once())
2202
            ->method('getParameter')
2203
            ->with('sonata.admin.configuration.breadcrumbs')
2204
            ->willReturn([]);
2205
2206
        $pool = $this->getMockBuilder(Pool::class)
2207
            ->disableOriginalConstructor()
2208
            ->getMock();
2209
        $pool->expects($this->once())
2210
            ->method('getContainer')
2211
            ->willReturn($container);
2212
2213
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2214
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2215
        ], '', true, true, true, ['getConfigurationPool']);
2216
        $admin->expects($this->once())
2217
            ->method('getConfigurationPool')
2218
            ->willReturn($pool);
2219
2220
        $this->assertInstanceOf(BreadcrumbsBuilder::class, $admin->getBreadcrumbsBuilder());
2221
    }
2222
2223
    /**
2224
     * @group legacy
2225
     */
2226
    public function testBreadcrumbsBuilderSetter(): void
2227
    {
2228
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2229
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2230
        ]);
2231
        $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->createMock(
2232
            BreadcrumbsBuilderInterface::class
2233
        )));
2234
        $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
2235
    }
2236
2237
    /**
2238
     * @group legacy
2239
     */
2240
    public function testGetBreadcrumbs(): void
2241
    {
2242
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2243
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2244
        ]);
2245
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2246
        $action = 'myaction';
2247
        $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
2248
        $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
2249
    }
2250
2251
    /**
2252
     * @group legacy
2253
     */
2254
    public function testBuildBreadcrumbs(): void
2255
    {
2256
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2257
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2258
        ]);
2259
        $builder = $this->prophesize(BreadcrumbsBuilderInterface::class);
2260
        $action = 'myaction';
2261
        $menu = $this->createMock(ItemInterface::class);
2262
        $builder->buildBreadcrumbs($admin, $action, $menu)
2263
            ->shouldBeCalledTimes(1)
2264
            ->willReturn($menu);
2265
        $admin->setBreadcrumbsBuilder($builder->reveal());
2266
2267
        /* check the called is proxied only once */
2268
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2269
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2270
    }
2271
2272
    /**
2273
     * NEXT_MAJOR: remove this method.
2274
     *
2275
     * @group legacy
2276
     */
2277
    public function testCreateQueryLegacyCallWorks(): void
2278
    {
2279
        $admin = $this->getMockForAbstractClass(AbstractAdmin::class, [
2280
            'admin.my_code', 'My\Class', 'MyBundle\ClassAdminController',
2281
        ]);
2282
        $query = $this->createMock(ProxyQueryInterface::class);
2283
        $modelManager = $this->createMock(ModelManagerInterface::class);
2284
        $modelManager->expects($this->once())
2285
            ->method('createQuery')
2286
            ->with('My\Class')
2287
            ->willReturn($query);
2288
2289
        $admin->setModelManager($modelManager);
2290
        $this->assertSame($query, $admin->createQuery('list'));
2291
    }
2292
2293
    public function testGetDataSourceIterator(): void
2294
    {
2295
        $datagrid = $this->createMock(DatagridInterface::class);
2296
        $datagrid->method('buildPager');
2297
2298
        $modelManager = $this->createMock(ModelManagerInterface::class);
2299
        $modelManager->method('getExportFields')->willReturn([
2300
            'field',
2301
            'foo',
2302
            'bar',
2303
        ]);
2304
        $modelManager->expects($this->once())->method('getDataSourceIterator')
2305
            ->with($this->equalTo($datagrid), $this->equalTo([
2306
                'Feld' => 'field',
2307
                1 => 'foo',
2308
                2 => 'bar',
2309
            ]));
2310
2311
        $admin = $this->getMockBuilder(AbstractAdmin::class)
2312
            ->disableOriginalConstructor()
2313
            ->setMethods(['getDatagrid', 'getTranslationLabel', 'trans'])
2314
            ->getMockForAbstractClass();
2315
        $admin->method('getDatagrid')->willReturn($datagrid);
2316
        $admin->setModelManager($modelManager);
2317
2318
        $admin
2319
            ->method('getTranslationLabel')
2320
            ->willReturnCallback(static function (string $label, string $context = '', string $type = ''): string {
2321
                return $context.'.'.$type.'_'.$label;
2322
            });
2323
        $admin
2324
            ->method('trans')
2325
            ->willReturnCallback(static function (string $label): string {
2326
                if ('export.label_field' === $label) {
2327
                    return 'Feld';
2328
                }
2329
2330
                return $label;
2331
            });
2332
2333
        $admin->getDataSourceIterator();
2334
    }
2335
2336
    public function testCircularChildAdmin(): void
2337
    {
2338
        $this->expectException(\RuntimeException::class);
2339
        $this->expectExceptionMessage(
2340
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment` admin.'
2341
        );
2342
2343
        $postAdmin = new PostAdmin(
2344
            'sonata.post.admin.post',
2345
            'Application\Sonata\NewsBundle\Entity\Post',
2346
            'Sonata\NewsBundle\Controller\PostAdminController'
2347
        );
2348
        $commentAdmin = new CommentAdmin(
2349
            'sonata.post.admin.comment',
2350
            'Application\Sonata\NewsBundle\Entity\Comment',
2351
            'Sonata\NewsBundle\Controller\CommentAdminController'
2352
        );
2353
        $postAdmin->addChild($commentAdmin, 'post');
2354
        $commentAdmin->addChild($postAdmin, 'comment');
2355
    }
2356
2357
    public function testCircularChildAdminTripleLevel(): void
2358
    {
2359
        $this->expectException(\RuntimeException::class);
2360
        $this->expectExceptionMessage(
2361
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment_vote` admin.'
2362
        );
2363
2364
        $postAdmin = new PostAdmin(
2365
            'sonata.post.admin.post',
2366
            'Application\Sonata\NewsBundle\Entity\Post',
2367
            'Sonata\NewsBundle\Controller\PostAdminController'
2368
        );
2369
        $commentAdmin = new CommentAdmin(
2370
            'sonata.post.admin.comment',
2371
            'Application\Sonata\NewsBundle\Entity\Comment',
2372
            'Sonata\NewsBundle\Controller\CommentAdminController'
2373
        );
2374
        $commentVoteAdmin = new CommentVoteAdmin(
2375
            'sonata.post.admin.comment_vote',
2376
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2377
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2378
        );
2379
        $postAdmin->addChild($commentAdmin, 'post');
2380
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2381
        $commentVoteAdmin->addChild($postAdmin, 'post');
2382
    }
2383
2384
    public function testCircularChildAdminWithItself(): void
2385
    {
2386
        $this->expectException(\RuntimeException::class);
2387
        $this->expectExceptionMessage(
2388
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.post` admin.'
2389
        );
2390
2391
        $postAdmin = new PostAdmin(
2392
            'sonata.post.admin.post',
2393
            'Application\Sonata\NewsBundle\Entity\Post',
2394
            'Sonata\NewsBundle\Controller\PostAdminController'
2395
        );
2396
        $postAdmin->addChild($postAdmin);
2397
    }
2398
2399
    public function testGetRootAncestor(): void
2400
    {
2401
        $postAdmin = new PostAdmin(
2402
            'sonata.post.admin.post',
2403
            'Application\Sonata\NewsBundle\Entity\Post',
2404
            'Sonata\NewsBundle\Controller\PostAdminController'
2405
        );
2406
        $commentAdmin = new CommentAdmin(
2407
            'sonata.post.admin.comment',
2408
            'Application\Sonata\NewsBundle\Entity\Comment',
2409
            'Sonata\NewsBundle\Controller\CommentAdminController'
2410
        );
2411
        $commentVoteAdmin = new CommentVoteAdmin(
2412
            'sonata.post.admin.comment_vote',
2413
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2414
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2415
        );
2416
2417
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2418
        $this->assertSame($commentAdmin, $commentAdmin->getRootAncestor());
2419
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2420
2421
        $postAdmin->addChild($commentAdmin, 'post');
2422
2423
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2424
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2425
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2426
2427
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2428
2429
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2430
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2431
        $this->assertSame($postAdmin, $commentVoteAdmin->getRootAncestor());
2432
    }
2433
2434
    public function testGetChildDepth(): void
2435
    {
2436
        $postAdmin = new PostAdmin(
2437
            'sonata.post.admin.post',
2438
            'Application\Sonata\NewsBundle\Entity\Post',
2439
            'Sonata\NewsBundle\Controller\PostAdminController'
2440
        );
2441
        $commentAdmin = new CommentAdmin(
2442
            'sonata.post.admin.comment',
2443
            'Application\Sonata\NewsBundle\Entity\Comment',
2444
            'Sonata\NewsBundle\Controller\CommentAdminController'
2445
        );
2446
        $commentVoteAdmin = new CommentVoteAdmin(
2447
            'sonata.post.admin.comment_vote',
2448
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2449
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2450
        );
2451
2452
        $this->assertSame(0, $postAdmin->getChildDepth());
2453
        $this->assertSame(0, $commentAdmin->getChildDepth());
2454
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2455
2456
        $postAdmin->addChild($commentAdmin, 'post');
2457
2458
        $this->assertSame(0, $postAdmin->getChildDepth());
2459
        $this->assertSame(1, $commentAdmin->getChildDepth());
2460
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2461
2462
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2463
2464
        $this->assertSame(0, $postAdmin->getChildDepth());
2465
        $this->assertSame(1, $commentAdmin->getChildDepth());
2466
        $this->assertSame(2, $commentVoteAdmin->getChildDepth());
2467
    }
2468
2469
    public function testGetCurrentLeafChildAdmin(): void
2470
    {
2471
        $postAdmin = new PostAdmin(
2472
            'sonata.post.admin.post',
2473
            'Application\Sonata\NewsBundle\Entity\Post',
2474
            'Sonata\NewsBundle\Controller\PostAdminController'
2475
        );
2476
        $commentAdmin = new CommentAdmin(
2477
            'sonata.post.admin.comment',
2478
            'Application\Sonata\NewsBundle\Entity\Comment',
2479
            'Sonata\NewsBundle\Controller\CommentAdminController'
2480
        );
2481
        $commentVoteAdmin = new CommentVoteAdmin(
2482
            'sonata.post.admin.comment_vote',
2483
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2484
            'Sonata\NewsBundle\Controller\CommentVoteAdminController'
2485
        );
2486
2487
        $postAdmin->addChild($commentAdmin, 'post');
2488
        $commentAdmin->addChild($commentVoteAdmin, 'comment');
2489
2490
        $this->assertNull($postAdmin->getCurrentLeafChildAdmin());
2491
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2492
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2493
2494
        $commentAdmin->setCurrentChild(true);
2495
2496
        $this->assertSame($commentAdmin, $postAdmin->getCurrentLeafChildAdmin());
2497
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2498
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2499
2500
        $commentVoteAdmin->setCurrentChild(true);
2501
2502
        $this->assertSame($commentVoteAdmin, $postAdmin->getCurrentLeafChildAdmin());
2503
        $this->assertSame($commentVoteAdmin, $commentAdmin->getCurrentLeafChildAdmin());
2504
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2505
    }
2506
2507
    public function testAdminWithoutControllerName(): void
2508
    {
2509
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', null);
2510
2511
        $this->assertNull($admin->getBaseControllerName());
2512
    }
2513
2514
    public function testAdminAvoidInifiniteLoop(): void
2515
    {
2516
        $this->expectNotToPerformAssertions();
2517
2518
        $formFactory = new FormFactory(new FormRegistry([], new ResolvedFormTypeFactory()));
2519
2520
        $admin = new AvoidInfiniteLoopAdmin('code', \stdClass::class, null);
2521
        $admin->setSubject(new \stdClass());
2522
2523
        $admin->setFormContractor(new FormContractor($formFactory));
2524
2525
        $admin->setShowBuilder(new ShowBuilder());
2526
2527
        $admin->setListBuilder(new ListBuilder());
2528
2529
        $pager = $this->createMock(PagerInterface::class);
2530
        $admin->setDatagridBuilder(new DatagridBuilder($formFactory, $pager));
2531
2532
        $validator = $this->createMock(ValidatorInterface::class);
2533
        $validator->method('getMetadataFor')->willReturn($this->createMock(MemberMetadata::class));
2534
        $admin->setValidator($validator);
2535
2536
        $routeGenerator = $this->getMockForAbstractClass(RouteGeneratorInterface::class);
2537
        $admin->setRouteGenerator($routeGenerator);
2538
2539
        $admin->getForm();
2540
        $admin->getShow();
2541
        $admin->getList();
2542
        $admin->getDatagrid();
2543
    }
2544
2545
    /**
2546
     * NEXT_MAJOR: Remove this test and its data provider.
2547
     *
2548
     * @group legacy
2549
     *
2550
     * @dataProvider getDeprecatedAbstractAdminConstructorArgs
2551
     *
2552
     * @expectedDeprecation Passing other type than string%S as argument %d for method Sonata\AdminBundle\Admin\AbstractAdmin::__construct() is deprecated since sonata-project/admin-bundle 3.65. It will accept only string%S in version 4.0.
2553
     */
2554
    public function testDeprecatedAbstractAdminConstructorArgs($code, $class, $baseControllerName): void
2555
    {
2556
        new PostAdmin($code, $class, $baseControllerName);
2557
    }
2558
2559
    public function getDeprecatedAbstractAdminConstructorArgs(): iterable
2560
    {
2561
        yield from [
2562
            ['sonata.post.admin.post', null, null],
2563
            [null, null, null],
2564
            ['sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', false],
2565
            ['sonata.post.admin.post', false, false],
2566
            [false, false, false],
2567
            [true, true, true],
2568
            [new \stdClass(), new \stdClass(), new \stdClass()],
2569
            [0, 0, 0],
2570
            [1, 1, 1],
2571
        ];
2572
    }
2573
2574
    private function createTagAdmin(Post $post): TagAdmin
2575
    {
2576
        $postAdmin = $this->getMockBuilder(PostAdmin::class)
2577
            ->disableOriginalConstructor()
2578
            ->getMock();
2579
2580
        $postAdmin->method('getObject')->willReturn($post);
2581
2582
        $formBuilder = $this->createMock(FormBuilderInterface::class);
2583
        $formBuilder->method('getForm')->willReturn(null);
2584
2585
        $tagAdmin = $this->getMockBuilder(TagAdmin::class)
2586
            ->setConstructorArgs([
2587
                'admin.tag',
2588
                Tag::class,
2589
                'MyBundle\MyController',
2590
            ])
2591
            ->setMethods(['getFormBuilder'])
2592
            ->getMock();
2593
2594
        $tagAdmin->method('getFormBuilder')->willReturn($formBuilder);
2595
        $tagAdmin->setParent($postAdmin);
2596
2597
        $tag = new Tag();
2598
        $tagAdmin->setSubject($tag);
2599
2600
        $request = $this->createMock(Request::class);
2601
        $tagAdmin->setRequest($request);
2602
2603
        $configurationPool = $this->getMockBuilder(Pool::class)
2604
            ->disableOriginalConstructor()
2605
            ->getMock();
2606
2607
        $configurationPool->method('getPropertyAccessor')->willReturn(PropertyAccess::createPropertyAccessor());
2608
2609
        $tagAdmin->setConfigurationPool($configurationPool);
2610
2611
        return $tagAdmin;
2612
    }
2613
}
2614