Completed
Push — 3.x ( f7534c...26d69b )
by Grégoire
04:05
created

AdminTest::testGetRootAncestor()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Admin;
13
14
use Sonata\AdminBundle\Admin\AbstractAdmin;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\Pool;
17
use Sonata\AdminBundle\Route\DefaultRouteGenerator;
18
use Sonata\AdminBundle\Route\RoutesCache;
19
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
20
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentVoteAdmin;
21
use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentWithCustomRouteAdmin;
22
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
23
use Sonata\AdminBundle\Tests\Fixtures\Admin\FilteredAdmin;
24
use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
25
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
26
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin;
27
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Comment;
28
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
29
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
30
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
31
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToStringNull;
32
use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
33
use Sonata\CoreBundle\Model\Adapter\AdapterInterface;
34
use Symfony\Component\DependencyInjection\Container;
35
use Symfony\Component\HttpFoundation\Request;
36
use Symfony\Component\PropertyAccess\PropertyAccess;
37
38
class AdminTest extends PHPUnit_Framework_TestCase
39
{
40
    protected $cacheTempFolder;
41
42
    public function setUp()
43
    {
44
        $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
45
46
        exec('rm -rf '.$this->cacheTempFolder);
47
    }
48
49
    /**
50
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::__construct
51
     */
52
    public function testConstructor()
53
    {
54
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
55
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
56
57
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
58
        $this->assertInstanceOf('Sonata\AdminBundle\Admin\AbstractAdmin', $admin);
59
        $this->assertSame($class, $admin->getClass());
60
        $this->assertSame($baseControllerName, $admin->getBaseControllerName());
61
    }
62
63
    public function testGetClass()
64
    {
65
        $class = 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post';
66
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
67
68
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
69
70
        $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
71
        $this->assertSame(
72
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
73
            $admin->getClass()
74
        );
75
76
        $admin->setSubClasses(array('foo'));
77
        $this->assertSame(
78
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
79
            $admin->getClass()
80
        );
81
82
        $admin->setSubject(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
        $admin->setSubClasses(array());
84
        $this->assertSame($class, $admin->getClass());
85
86
        $admin->setSubClasses(array('foo' => 'bar'));
87
        $admin->setRequest(new Request(array('subclass' => 'foo')));
88
        $this->assertSame('bar', $admin->getClass());
89
    }
90
91
    /**
92
     * @expectedException \RuntimeException
93
     * @expectedExceptionMessage Feature not implemented: an embedded admin cannot have subclass
94
     */
95
    public function testGetClassException()
96
    {
97
        $class = 'Application\Sonata\NewsBundle\Entity\Post';
98
        $baseControllerName = 'SonataNewsBundle:PostAdmin';
99
100
        $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
101
        $admin->setParentFieldDescription(new FieldDescription());
102
        $admin->setSubClasses(array('foo' => 'bar'));
103
        $admin->setRequest(new Request(array('subclass' => 'foo')));
104
        $admin->getClass();
105
    }
106
107
    public function testCheckAccessThrowsExceptionOnMadeUpAction()
108
    {
109
        $admin = new PostAdmin(
110
            'sonata.post.admin.post',
111
            'Application\Sonata\NewsBundle\Entity\Post',
112
            'SonataNewsBundle:PostAdmin'
113
        );
114
        $this->setExpectedException(
115
            '\InvalidArgumentException',
116
            'Action "made-up" could not be found'
117
        );
118
        $admin->checkAccess('made-up');
119
    }
120
121
    public function testCheckAccessThrowsAccessDeniedException()
122
    {
123
        $admin = new PostAdmin(
124
            'sonata.post.admin.post',
125
            'Application\Sonata\NewsBundle\Entity\Post',
126
            'SonataNewsBundle:PostAdmin'
127
        );
128
        $securityHandler = $this->prophesize(
129
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
130
        );
131
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
132
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
133
        $customExtension = $this->prophesize(
134
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
135
        );
136
        $customExtension->getAccessMapping($admin)->willReturn(
137
            array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
138
        );
139
        $admin->addExtension($customExtension->reveal());
140
        $admin->setSecurityHandler($securityHandler->reveal());
141
        $this->setExpectedException(
142
            'Symfony\Component\Security\Core\Exception\AccessDeniedException',
143
            'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE'
144
        );
145
        $admin->checkAccess('custom_action');
146
    }
147
148
    public function testHasAccessOnMadeUpAction()
149
    {
150
        $admin = new PostAdmin(
151
            'sonata.post.admin.post',
152
            'Application\Sonata\NewsBundle\Entity\Post',
153
            'SonataNewsBundle:PostAdmin'
154
        );
155
156
        $this->assertFalse($admin->hasAccess('made-up'));
157
    }
158
159
    public function testHasAccess()
160
    {
161
        $admin = new PostAdmin(
162
            'sonata.post.admin.post',
163
            'Application\Sonata\NewsBundle\Entity\Post',
164
            'SonataNewsBundle:PostAdmin'
165
        );
166
        $securityHandler = $this->prophesize(
167
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
168
        );
169
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
170
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
171
        $customExtension = $this->prophesize(
172
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
173
        );
174
        $customExtension->getAccessMapping($admin)->willReturn(
175
            array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
176
        );
177
        $admin->addExtension($customExtension->reveal());
178
        $admin->setSecurityHandler($securityHandler->reveal());
179
180
        $this->assertFalse($admin->hasAccess('custom_action'));
181
    }
182
183
    public function testHasAccessAllowsAccess()
184
    {
185
        $admin = new PostAdmin(
186
            'sonata.post.admin.post',
187
            'Application\Sonata\NewsBundle\Entity\Post',
188
            'SonataNewsBundle:PostAdmin'
189
        );
190
        $securityHandler = $this->prophesize(
191
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
192
        );
193
        $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
194
        $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true);
195
        $customExtension = $this->prophesize(
196
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
197
        );
198
        $customExtension->getAccessMapping($admin)->willReturn(
199
            array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
200
        );
201
        $admin->addExtension($customExtension->reveal());
202
        $admin->setSecurityHandler($securityHandler->reveal());
203
204
        $this->assertTrue($admin->hasAccess('custom_action'));
205
    }
206
207
    public function testHasAccessAllowsAccessEditAction()
208
    {
209
        $admin = new PostAdmin(
210
            'sonata.post.admin.post',
211
            'Application\Sonata\NewsBundle\Entity\Post',
212
            'SonataNewsBundle:PostAdmin'
213
        );
214
        $securityHandler = $this->prophesize(
215
            'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
216
        );
217
        $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true);
218
        $customExtension = $this->prophesize(
219
            'Sonata\AdminBundle\Admin\AbstractAdminExtension'
220
        );
221
        $customExtension->getAccessMapping($admin)->willReturn(
222
            array('edit_action' => array('EDIT_ROLE'))
223
        );
224
        $admin->addExtension($customExtension->reveal());
225
        $admin->setSecurityHandler($securityHandler->reveal());
226
227
        $this->assertTrue($admin->hasAccess('edit_action'));
228
    }
229
230
    /**
231
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChild
232
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::addChild
233
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChild
234
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::isChild
235
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren
236
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getChildren
237
     */
238
    public function testChildren()
239
    {
240
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
241
        $this->assertFalse($postAdmin->hasChildren());
242
        $this->assertFalse($postAdmin->hasChild('comment'));
243
244
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
245
        $postAdmin->addChild($commentAdmin);
246
        $this->assertTrue($postAdmin->hasChildren());
247
        $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment'));
248
249
        $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode());
250
        $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute());
251
        $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent());
252
253
        $this->assertFalse($postAdmin->isChild());
254
        $this->assertTrue($commentAdmin->isChild());
255
256
        $this->assertSame(array('sonata.post.admin.comment' => $commentAdmin), $postAdmin->getChildren());
257
    }
258
259
    /**
260
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configure
261
     */
262
    public function testConfigure()
263
    {
264
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
265
        $this->assertNotNull($admin->getUniqid());
266
267
        $admin->initialize();
268
        $this->assertNotNull($admin->getUniqid());
269
        $this->assertSame('Post', $admin->getClassnameLabel());
270
271
        $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
272
        $admin->setClassnameLabel('postcomment');
273
274
        $admin->initialize();
275
        $this->assertSame('postcomment', $admin->getClassnameLabel());
276
    }
277
278
    public function testConfigureWithValidParentAssociationMapping()
279
    {
280
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
281
        $admin->setParentAssociationMapping('Category');
282
283
        $admin->initialize();
284
        $this->assertSame('Category', $admin->getParentAssociationMapping());
285
    }
286
287
    public function provideGetBaseRoutePattern()
288
    {
289
        return array(
290
            array(
291
                'Application\Sonata\NewsBundle\Entity\Post',
292
                '/sonata/news/post',
293
            ),
294
            array(
295
                'Application\Sonata\NewsBundle\Document\Post',
296
                '/sonata/news/post',
297
            ),
298
            array(
299
                'MyApplication\MyBundle\Entity\Post',
300
                '/myapplication/my/post',
301
            ),
302
            array(
303
                'MyApplication\MyBundle\Entity\Post\Category',
304
                '/myapplication/my/post-category',
305
            ),
306
            array(
307
                'MyApplication\MyBundle\Entity\Product\Category',
308
                '/myapplication/my/product-category',
309
            ),
310
            array(
311
                'MyApplication\MyBundle\Entity\Other\Product\Category',
312
                '/myapplication/my/other-product-category',
313
            ),
314
            array(
315
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
316
                '/cmf/foo/menu',
317
            ),
318
            array(
319
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
320
                '/cmf/foo/menu',
321
            ),
322
            array(
323
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
324
                '/symfony/barbar/menu',
325
            ),
326
            array(
327
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
328
                '/symfony/barbar/menu-item',
329
            ),
330
            array(
331
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
332
                '/cmf/foo/menu',
333
            ),
334
            array(
335
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
336
                '/cmf/foo/menu',
337
            ),
338
            array(
339
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
340
                '/cmf/foo/menu',
341
            ),
342
            array(
343
                'AppBundle\Entity\User',
344
                '/app/user',
345
            ),
346
            array(
347
                'App\Entity\User',
348
                '/app/user',
349
            ),
350
        );
351
    }
352
353
    /**
354
     * @dataProvider provideGetBaseRoutePattern
355
     */
356
    public function testGetBaseRoutePattern($objFqn, $expected)
357
    {
358
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
359
        $this->assertSame($expected, $admin->getBaseRoutePattern());
360
    }
361
362
    /**
363
     * @dataProvider provideGetBaseRoutePattern
364
     */
365
    public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected)
366
    {
367
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
368
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
369
        $commentAdmin->setParent($postAdmin);
370
371
        $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern());
372
    }
373
374
    /**
375
     * @dataProvider provideGetBaseRoutePattern
376
     */
377
    public function testGetBaseRoutePatternWithTwoNestedChildAdmin($objFqn, $expected)
378
    {
379
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
380
        $commentAdmin = new CommentAdmin(
381
            'sonata.post.admin.comment',
382
            'Application\Sonata\NewsBundle\Entity\Comment',
383
            'SonataNewsBundle:CommentAdmin'
384
        );
385
        $commentVoteAdmin = new CommentVoteAdmin(
386
            'sonata.post.admin.comment_vote',
387
            'Application\Sonata\NewsBundle\Entity\CommentVote',
388
            'SonataNewsBundle:CommentVoteAdmin'
389
        );
390
        $commentAdmin->setParent($postAdmin);
391
        $commentVoteAdmin->setParent($commentAdmin);
392
393
        $this->assertSame($expected.'/{id}/comment/{childId}/commentvote', $commentVoteAdmin->getBaseRoutePattern());
394
    }
395
396
    public function testGetBaseRoutePatternWithSpecifedPattern()
397
    {
398
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin');
399
400
        $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern());
401
    }
402
403
    public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern()
404
    {
405
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
406
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
407
        $commentAdmin->setParent($postAdmin);
408
409
        $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern());
410
    }
411
412
    /**
413
     * @expectedException \RuntimeException
414
     */
415
    public function testGetBaseRoutePatternWithUnreconizedClassname()
416
    {
417
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
418
        $admin->getBaseRoutePattern();
419
    }
420
421
    public function provideGetBaseRouteName()
422
    {
423
        return array(
424
            array(
425
                'Application\Sonata\NewsBundle\Entity\Post',
426
                'admin_sonata_news_post',
427
            ),
428
            array(
429
                'Application\Sonata\NewsBundle\Document\Post',
430
                'admin_sonata_news_post',
431
            ),
432
            array(
433
                'MyApplication\MyBundle\Entity\Post',
434
                'admin_myapplication_my_post',
435
            ),
436
            array(
437
                'MyApplication\MyBundle\Entity\Post\Category',
438
                'admin_myapplication_my_post_category',
439
            ),
440
            array(
441
                'MyApplication\MyBundle\Entity\Product\Category',
442
                'admin_myapplication_my_product_category',
443
            ),
444
            array(
445
                'MyApplication\MyBundle\Entity\Other\Product\Category',
446
                'admin_myapplication_my_other_product_category',
447
            ),
448
            array(
449
                'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
450
                'admin_cmf_foo_menu',
451
            ),
452
            array(
453
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
454
                'admin_cmf_foo_menu',
455
            ),
456
            array(
457
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
458
                'admin_symfony_barbar_menu',
459
            ),
460
            array(
461
                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
462
                'admin_symfony_barbar_menu_item',
463
            ),
464
            array(
465
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
466
                'admin_cmf_foo_menu',
467
            ),
468
            array(
469
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
470
                'admin_cmf_foo_menu',
471
            ),
472
            array(
473
                'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
474
                'admin_cmf_foo_menu',
475
            ),
476
            array(
477
                'AppBundle\Entity\User',
478
                'admin_app_user',
479
            ),
480
            array(
481
                'App\Entity\User',
482
                'admin_app_user',
483
            ),
484
        );
485
    }
486
487
    /**
488
     * @dataProvider provideGetBaseRouteName
489
     */
490
    public function testGetBaseRouteName($objFqn, $expected)
491
    {
492
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
493
494
        $this->assertSame($expected, $admin->getBaseRouteName());
495
    }
496
497
    /**
498
     * @dataProvider provideGetBaseRouteName
499
     */
500
    public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected)
501
    {
502
        $routeGenerator = new DefaultRouteGenerator(
503
            $this->createMock('Symfony\Component\Routing\RouterInterface'),
504
            new RoutesCache($this->cacheTempFolder, true)
505
        );
506
507
        $container = new Container();
508
        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
509
510
        $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->createMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
511
        $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
512
        $container->set('sonata.post.admin.post', $postAdmin);
513
        $postAdmin->setConfigurationPool($pool);
514
        $postAdmin->setRouteBuilder($pathInfo);
515
        $postAdmin->setRouteGenerator($routeGenerator);
516
        $postAdmin->initialize();
517
518
        $commentAdmin = new CommentAdmin(
519
            'sonata.post.admin.comment',
520
            'Application\Sonata\NewsBundle\Entity\Comment',
521
            'SonataNewsBundle:CommentAdmin'
522
        );
523
        $container->set('sonata.post.admin.comment', $commentAdmin);
524
        $commentAdmin->setConfigurationPool($pool);
525
        $commentAdmin->setRouteBuilder($pathInfo);
526
        $commentAdmin->setRouteGenerator($routeGenerator);
527
        $commentAdmin->initialize();
528
529
        $postAdmin->addChild($commentAdmin);
530
531
        $commentVoteAdmin = new CommentVoteAdmin(
532
            'sonata.post.admin.comment_vote',
533
            'Application\Sonata\NewsBundle\Entity\CommentVote',
534
            'SonataNewsBundle:CommentVoteAdmin'
535
        );
536
        $container->set('sonata.post.admin.comment_vote', $commentVoteAdmin);
537
        $commentVoteAdmin->setConfigurationPool($pool);
538
        $commentVoteAdmin->setRouteBuilder($pathInfo);
539
        $commentVoteAdmin->setRouteGenerator($routeGenerator);
540
        $commentVoteAdmin->initialize();
541
542
        $commentAdmin->addChild($commentVoteAdmin);
543
        $pool->setAdminServiceIds(array(
544
            'sonata.post.admin.post',
545
            'sonata.post.admin.comment',
546
            'sonata.post.admin.comment_vote',
547
        ));
548
549
        $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
550
551
        $this->assertTrue($postAdmin->hasRoute('show'));
552
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show'));
553
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show'));
554
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment|sonata.post.admin.comment_vote.show'));
555
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
556
        $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment|sonata.post.admin.comment_vote.list'));
557
        $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
558
        $this->assertFalse($commentAdmin->hasRoute('edit'));
559
560
        /*
561
         * Test the route name from request
562
         */
563
        $postListRequest = new Request(
564
            array(),
565
            array(),
566
            array(
567
                '_route' => $postAdmin->getBaseRouteName().'_list',
568
            )
569
        );
570
571
        $postAdmin->setRequest($postListRequest);
572
        $commentAdmin->setRequest($postListRequest);
573
574
        $this->assertTrue($postAdmin->isCurrentRoute('list'));
575
        $this->assertFalse($postAdmin->isCurrentRoute('create'));
576
        $this->assertFalse($commentAdmin->isCurrentRoute('list'));
577
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('list'));
578
        $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
579
        $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
580
        $this->assertTrue($commentVoteAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
581
        $this->assertFalse($commentVoteAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
582
    }
583
584
    /**
585
     * @expectedException \RuntimeException
586
     */
587
    public function testGetBaseRouteNameWithUnreconizedClassname()
588
    {
589
        $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
590
        $admin->getBaseRouteName();
591
    }
592
593
    public function testGetBaseRouteNameWithSpecifiedName()
594
    {
595
        $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
596
597
        $this->assertSame('post_custom', $postAdmin->getBaseRouteName());
598
    }
599
600
    public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName()
601
    {
602
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
603
        $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
604
        $commentAdmin->setParent($postAdmin);
605
606
        $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName());
607
    }
608
609
    public function testGetBaseRouteNameWithTwoNestedChildAdminAndWithSpecifiedName()
610
    {
611
        $postAdmin = new PostAdmin(
612
            'sonata.post.admin.post',
613
            'Application\Sonata\NewsBundle\Entity\Post',
614
            'SonataNewsBundle:PostAdmin'
615
        );
616
        $commentAdmin = new CommentWithCustomRouteAdmin(
617
            'sonata.post.admin.comment_with_custom_route',
618
            'Application\Sonata\NewsBundle\Entity\Comment',
619
            'SonataNewsBundle:CommentWithCustomRouteAdmin'
620
        );
621
        $commentVoteAdmin = new CommentVoteAdmin(
622
            'sonata.post.admin.comment_vote',
623
            'Application\Sonata\NewsBundle\Entity\CommentVote',
624
            'SonataNewsBundle:CommentVoteAdmin'
625
        );
626
        $commentAdmin->setParent($postAdmin);
627
        $commentVoteAdmin->setParent($commentAdmin);
628
629
        $this->assertSame('admin_sonata_news_post_comment_custom_commentvote', $commentVoteAdmin->getBaseRouteName());
630
    }
631
632
    /**
633
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid
634
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid
635
     */
636
    public function testUniqid()
637
    {
638
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
639
640
        $uniqid = uniqid();
641
        $admin->setUniqid($uniqid);
642
643
        $this->assertSame($uniqid, $admin->getUniqid());
644
    }
645
646
    public function testToString()
647
    {
648
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
649
650
        $s = new \stdClass();
651
652
        $this->assertNotEmpty($admin->toString($s));
653
654
        $s = new FooToString();
655
        $this->assertSame('salut', $admin->toString($s));
656
657
        // To string method is implemented, but returns null
658
        $s = new FooToStringNull();
659
        $this->assertNotEmpty($admin->toString($s));
660
661
        $this->assertSame('', $admin->toString(false));
662
    }
663
664
    public function testIsAclEnabled()
665
    {
666
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
667
668
        $this->assertFalse($postAdmin->isAclEnabled());
669
670
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
671
        $commentAdmin->setSecurityHandler($this->createMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface'));
672
        $this->assertTrue($commentAdmin->isAclEnabled());
673
    }
674
675
    /**
676
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses
677
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass
678
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses
679
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass
680
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
681
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass
682
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode
683
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getClass
684
     */
685
    public function testSubClass()
686
    {
687
        $admin = new PostAdmin(
688
            'sonata.post.admin.post',
689
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
690
            'SonataNewsBundle:PostAdmin'
691
        );
692
        $this->assertFalse($admin->hasSubClass('test'));
693
        $this->assertFalse($admin->hasActiveSubClass());
694
        $this->assertCount(0, $admin->getSubClasses());
695
        $this->assertNull($admin->getActiveSubClass());
696
        $this->assertNull($admin->getActiveSubclassCode());
697
        $this->assertSame(
698
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
699
            $admin->getClass()
700
        );
701
702
        // Just for the record, if there is no inheritance set, the getSubject is not used
703
        // the getSubject can also lead to some issue
704
        $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
705
        $this->assertSame(
706
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
707
            $admin->getClass()
708
        );
709
710
        $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
711
        $this->assertFalse($admin->hasSubClass('test'));
712
        $this->assertTrue($admin->hasSubClass('extended1'));
713
        $this->assertFalse($admin->hasActiveSubClass());
714
        $this->assertCount(2, $admin->getSubClasses());
715
        $this->assertNull($admin->getActiveSubClass());
716
        $this->assertNull($admin->getActiveSubclassCode());
717
        $this->assertSame(
718
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
719
            $admin->getClass()
720
        );
721
722
        $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
723
        $admin->setRequest($request);
724
        $this->assertFalse($admin->hasSubClass('test'));
725
        $this->assertTrue($admin->hasSubClass('extended1'));
726
        $this->assertTrue($admin->hasActiveSubClass());
727
        $this->assertCount(2, $admin->getSubClasses());
728
        $this->assertSame(
729
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
730
            $admin->getActiveSubClass()
731
        );
732
        $this->assertSame('extended1', $admin->getActiveSubclassCode());
733
        $this->assertSame(
734
            'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
735
            $admin->getClass()
736
        );
737
738
        $request->query->set('subclass', 'inject');
739
        $this->assertNull($admin->getActiveSubclassCode());
740
    }
741
742
    /**
743
     * @expectedException \RuntimeException
744
     */
745
    public function testNonExistantSubclass()
746
    {
747
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
748
        $admin->setRequest(new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'inject')));
749
750
        $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
751
752
        $this->assertTrue($admin->hasActiveSubClass());
753
754
        $admin->getActiveSubClass();
755
    }
756
757
    /**
758
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
759
     */
760
    public function testOnlyOneSubclassNeededToBeActive()
761
    {
762
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
763
        $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1'));
764
        $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
765
        $admin->setRequest($request);
766
        $this->assertTrue($admin->hasActiveSubClass());
767
    }
768
769
    public function testGetPerPageOptions()
770
    {
771
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
772
773
        $this->assertSame(array(16, 32, 64, 128, 192), $admin->getPerPageOptions());
774
        $admin->setPerPageOptions(array(500, 1000));
775
        $this->assertSame(array(500, 1000), $admin->getPerPageOptions());
776
    }
777
778
    public function testGetLabelTranslatorStrategy()
779
    {
780
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
781
782
        $this->assertNull($admin->getLabelTranslatorStrategy());
783
784
        $labelTranslatorStrategy = $this->createMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
785
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
786
        $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy());
787
    }
788
789
    public function testGetRouteBuilder()
790
    {
791
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
792
793
        $this->assertNull($admin->getRouteBuilder());
794
795
        $routeBuilder = $this->createMock('Sonata\AdminBundle\Builder\RouteBuilderInterface');
796
        $admin->setRouteBuilder($routeBuilder);
797
        $this->assertSame($routeBuilder, $admin->getRouteBuilder());
798
    }
799
800
    public function testGetMenuFactory()
801
    {
802
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
803
804
        $this->assertNull($admin->getMenuFactory());
805
806
        $menuFactory = $this->createMock('Knp\Menu\FactoryInterface');
807
        $admin->setMenuFactory($menuFactory);
808
        $this->assertSame($menuFactory, $admin->getMenuFactory());
809
    }
810
811
    public function testGetExtensions()
812
    {
813
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
814
815
        $this->assertSame(array(), $admin->getExtensions());
816
817
        $adminExtension1 = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
818
        $adminExtension2 = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
819
820
        $admin->addExtension($adminExtension1);
821
        $admin->addExtension($adminExtension2);
822
        $this->assertSame(array($adminExtension1, $adminExtension2), $admin->getExtensions());
823
    }
824
825
    public function testGetFilterTheme()
826
    {
827
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
828
829
        $this->assertSame(array(), $admin->getFilterTheme());
830
831
        $admin->setFilterTheme(array('FooTheme'));
832
        $this->assertSame(array('FooTheme'), $admin->getFilterTheme());
833
    }
834
835
    public function testGetFormTheme()
836
    {
837
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
838
839
        $this->assertSame(array(), $admin->getFormTheme());
840
841
        $admin->setFormTheme(array('FooTheme'));
842
843
        $this->assertSame(array('FooTheme'), $admin->getFormTheme());
844
    }
845
846
    public function testGetValidator()
847
    {
848
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
849
850
        $this->assertNull($admin->getValidator());
851
852
        $validatorClass = '\Symfony\Component\Validator\ValidatorInterface';
853
854
        if (interface_exists('Symfony\Component\Validator\Validator\ValidatorInterface')) {
855
            $validatorClass = 'Symfony\Component\Validator\Validator\ValidatorInterface';
856
        }
857
858
        $validator = $this->getMockForAbstractClass($validatorClass);
859
860
        $admin->setValidator($validator);
861
        $this->assertSame($validator, $admin->getValidator());
862
    }
863
864
    public function testGetSecurityHandler()
865
    {
866
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
867
868
        $this->assertNull($admin->getSecurityHandler());
869
870
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
871
        $admin->setSecurityHandler($securityHandler);
872
        $this->assertSame($securityHandler, $admin->getSecurityHandler());
873
    }
874
875
    public function testGetSecurityInformation()
876
    {
877
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
878
879
        $this->assertSame(array(), $admin->getSecurityInformation());
880
881
        $securityInformation = array(
882
            'GUEST' => array('VIEW', 'LIST'),
883
            'STAFF' => array('EDIT', 'LIST', 'CREATE'),
884
        );
885
886
        $admin->setSecurityInformation($securityInformation);
887
        $this->assertSame($securityInformation, $admin->getSecurityInformation());
888
    }
889
890
    public function testGetManagerType()
891
    {
892
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
893
894
        $this->assertNull($admin->getManagerType());
895
896
        $admin->setManagerType('foo_orm');
897
        $this->assertSame('foo_orm', $admin->getManagerType());
898
    }
899
900
    public function testGetModelManager()
901
    {
902
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
903
904
        $this->assertNull($admin->getModelManager());
905
906
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
907
908
        $admin->setModelManager($modelManager);
909
        $this->assertSame($modelManager, $admin->getModelManager());
910
    }
911
912
    /**
913
     * NEXT_MAJOR: remove this method.
914
     *
915
     * @group legacy
916
     */
917
    public function testGetBaseCodeRoute()
918
    {
919
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
920
921
        $this->assertSame('', $admin->getBaseCodeRoute());
922
923
        $admin->setBaseCodeRoute('foo');
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...min::setBaseCodeRoute() has been deprecated with message: This method is deprecated since 3.x 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...
924
        $this->assertSame('foo', $admin->getBaseCodeRoute());
925
    }
926
927
    // NEXT_MAJOR: uncomment this method.
928
    // public function testGetBaseCodeRoute()
929
    // {
930
    //     $postAdmin = new PostAdmin(
931
    //         'sonata.post.admin.post',
932
    //         'NewsBundle\Entity\Post',
933
    //         'SonataNewsBundle:PostAdmin'
934
    //     );
935
    //     $commentAdmin = new CommentAdmin(
936
    //         'sonata.post.admin.comment',
937
    //         'Application\Sonata\NewsBundle\Entity\Comment',
938
    //         'SonataNewsBundle:CommentAdmin'
939
    //     );
940
    //
941
    //     $this->assertSame($postAdmin->getCode(), $postAdmin->getBaseCodeRoute());
942
    //
943
    //     $postAdmin->addChild($commentAdmin);
944
    //
945
    //     $this->assertSame(
946
    //         'sonata.post.admin.post|sonata.post.admin.comment',
947
    //         $commentAdmin->getBaseCodeRoute()
948
    //     );
949
    // }
950
951
    public function testGetRouteGenerator()
952
    {
953
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
954
955
        $this->assertNull($admin->getRouteGenerator());
956
957
        $routeGenerator = $this->createMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
958
959
        $admin->setRouteGenerator($routeGenerator);
960
        $this->assertSame($routeGenerator, $admin->getRouteGenerator());
961
    }
962
963
    public function testGetConfigurationPool()
964
    {
965
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
966
967
        $this->assertNull($admin->getConfigurationPool());
968
969
        $pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
970
            ->disableOriginalConstructor()
971
            ->getMock();
972
973
        $admin->setConfigurationPool($pool);
974
        $this->assertSame($pool, $admin->getConfigurationPool());
975
    }
976
977
    public function testGetShowBuilder()
978
    {
979
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
980
981
        $this->assertNull($admin->getShowBuilder());
982
983
        $showBuilder = $this->createMock('Sonata\AdminBundle\Builder\ShowBuilderInterface');
984
985
        $admin->setShowBuilder($showBuilder);
986
        $this->assertSame($showBuilder, $admin->getShowBuilder());
987
    }
988
989
    public function testGetListBuilder()
990
    {
991
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
992
993
        $this->assertNull($admin->getListBuilder());
994
995
        $listBuilder = $this->createMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
996
997
        $admin->setListBuilder($listBuilder);
998
        $this->assertSame($listBuilder, $admin->getListBuilder());
999
    }
1000
1001
    public function testGetDatagridBuilder()
1002
    {
1003
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1004
1005
        $this->assertNull($admin->getDatagridBuilder());
1006
1007
        $datagridBuilder = $this->createMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
1008
1009
        $admin->setDatagridBuilder($datagridBuilder);
1010
        $this->assertSame($datagridBuilder, $admin->getDatagridBuilder());
1011
    }
1012
1013
    public function testGetFormContractor()
1014
    {
1015
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1016
1017
        $this->assertNull($admin->getFormContractor());
1018
1019
        $formContractor = $this->createMock('Sonata\AdminBundle\Builder\FormContractorInterface');
1020
1021
        $admin->setFormContractor($formContractor);
1022
        $this->assertSame($formContractor, $admin->getFormContractor());
1023
    }
1024
1025
    public function testGetRequest()
1026
    {
1027
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1028
1029
        $this->assertFalse($admin->hasRequest());
1030
1031
        $request = new Request();
1032
1033
        $admin->setRequest($request);
1034
        $this->assertSame($request, $admin->getRequest());
1035
        $this->assertTrue($admin->hasRequest());
1036
    }
1037
1038
    public function testGetRequestWithException()
1039
    {
1040
        $this->setExpectedException('RuntimeException', 'The Request object has not been set');
1041
1042
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1043
        $admin->getRequest();
1044
    }
1045
1046
    public function testGetTranslationDomain()
1047
    {
1048
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1049
1050
        $this->assertSame('messages', $admin->getTranslationDomain());
1051
1052
        $admin->setTranslationDomain('foo');
1053
        $this->assertSame('foo', $admin->getTranslationDomain());
1054
    }
1055
1056
    /**
1057
     * @group legacy
1058
     */
1059
    public function testGetTranslator()
1060
    {
1061
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1062
1063
        $this->assertNull($admin->getTranslator());
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::getTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

Loading history...
1064
1065
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1066
1067
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

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

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

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

Loading history...
1069
    }
1070
1071
    public function testGetShowGroups()
1072
    {
1073
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1074
1075
        $this->assertSame(false, $admin->getShowGroups());
1076
1077
        $groups = array('foo', 'bar', 'baz');
1078
1079
        $admin->setShowGroups($groups);
1080
        $this->assertSame($groups, $admin->getShowGroups());
1081
    }
1082
1083
    public function testGetFormGroups()
1084
    {
1085
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1086
1087
        $this->assertSame(false, $admin->getFormGroups());
1088
1089
        $groups = array('foo', 'bar', 'baz');
1090
1091
        $admin->setFormGroups($groups);
1092
        $this->assertSame($groups, $admin->getFormGroups());
1093
    }
1094
1095
    public function testGetMaxPageLinks()
1096
    {
1097
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1098
1099
        $this->assertSame(25, $admin->getMaxPageLinks());
1100
1101
        $admin->setMaxPageLinks(14);
1102
        $this->assertSame(14, $admin->getMaxPageLinks());
1103
    }
1104
1105
    public function testGetMaxPerPage()
1106
    {
1107
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1108
1109
        $this->assertSame(32, $admin->getMaxPerPage());
1110
1111
        $admin->setMaxPerPage(94);
1112
        $this->assertSame(94, $admin->getMaxPerPage());
1113
    }
1114
1115
    public function testGetLabel()
1116
    {
1117
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1118
1119
        $this->assertNull($admin->getLabel());
1120
1121
        $admin->setLabel('FooLabel');
1122
        $this->assertSame('FooLabel', $admin->getLabel());
1123
    }
1124
1125
    public function testGetBaseController()
1126
    {
1127
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1128
1129
        $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName());
1130
1131
        $admin->setBaseControllerName('SonataNewsBundle:FooAdmin');
1132
        $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName());
1133
    }
1134
1135
    public function testGetTemplates()
1136
    {
1137
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1138
1139
        $this->assertSame(array(), $admin->getTemplates());
1140
1141
        $templates = array(
1142
            'list' => 'FooAdminBundle:CRUD:list.html.twig',
1143
            'show' => 'FooAdminBundle:CRUD:show.html.twig',
1144
            'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
1145
        );
1146
1147
        $admin->setTemplates($templates);
1148
        $this->assertSame($templates, $admin->getTemplates());
1149
    }
1150
1151
    public function testGetTemplate1()
1152
    {
1153
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1154
1155
        $this->assertNull($admin->getTemplate('edit'));
1156
1157
        $admin->setTemplate('edit', 'FooAdminBundle:CRUD:edit.html.twig');
1158
        $admin->setTemplate('show', 'FooAdminBundle:CRUD:show.html.twig');
1159
1160
        $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
1161
        $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
1162
    }
1163
1164
    public function testGetTemplate2()
1165
    {
1166
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1167
1168
        $this->assertNull($admin->getTemplate('edit'));
1169
1170
        $templates = array(
1171
            'list' => 'FooAdminBundle:CRUD:list.html.twig',
1172
            'show' => 'FooAdminBundle:CRUD:show.html.twig',
1173
            'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
1174
        );
1175
1176
        $admin->setTemplates($templates);
1177
1178
        $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
1179
        $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
1180
    }
1181
1182
    public function testGetIdParameter()
1183
    {
1184
        $postAdmin = new PostAdmin(
1185
            'sonata.post.admin.post',
1186
            'NewsBundle\Entity\Post',
1187
            'SonataNewsBundle:PostAdmin'
1188
        );
1189
1190
        $this->assertSame('id', $postAdmin->getIdParameter());
1191
        $this->assertFalse($postAdmin->isChild());
1192
1193
        $commentAdmin = new CommentAdmin(
1194
            'sonata.post.admin.comment',
1195
            'Application\Sonata\NewsBundle\Entity\Comment',
1196
            'SonataNewsBundle:CommentAdmin'
1197
        );
1198
        $commentAdmin->setParent($postAdmin);
1199
1200
        $this->assertTrue($commentAdmin->isChild());
1201
        $this->assertSame('childId', $commentAdmin->getIdParameter());
1202
1203
        $commentVoteAdmin = new CommentVoteAdmin(
1204
            'sonata.post.admin.comment_vote',
1205
            'Application\Sonata\NewsBundle\Entity\CommentVote',
1206
            'SonataNewsBundle:CommentVoteAdmin'
1207
        );
1208
        $commentVoteAdmin->setParent($commentAdmin);
1209
1210
        $this->assertTrue($commentVoteAdmin->isChild());
1211
        $this->assertSame('childChildId', $commentVoteAdmin->getIdParameter());
1212
    }
1213
1214
    public function testGetExportFormats()
1215
    {
1216
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1217
1218
        $this->assertSame(array('json', 'xml', 'csv', 'xls'), $admin->getExportFormats());
1219
    }
1220
1221
    public function testGetUrlsafeIdentifier()
1222
    {
1223
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1224
1225
        $entity = new \stdClass();
1226
1227
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1228
        $modelManager->expects($this->once())
1229
            ->method('getUrlsafeIdentifier')
1230
            ->with($this->equalTo($entity))
1231
            ->will($this->returnValue('foo'));
1232
        $admin->setModelManager($modelManager);
1233
1234
        $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity));
1235
    }
1236
1237
    public function testDeterminedPerPageValue()
1238
    {
1239
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1240
1241
        $this->assertFalse($admin->determinedPerPageValue('foo'));
1242
        $this->assertFalse($admin->determinedPerPageValue(123));
1243
        $this->assertTrue($admin->determinedPerPageValue(16));
1244
        $this->assertTrue($admin->determinedPerPageValue(32));
1245
        $this->assertTrue($admin->determinedPerPageValue(64));
1246
        $this->assertTrue($admin->determinedPerPageValue(128));
1247
        $this->assertTrue($admin->determinedPerPageValue(192));
1248
1249
        $admin->setPerPageOptions(array(101, 102, 103));
1250
        $this->assertFalse($admin->determinedPerPageValue(15));
1251
        $this->assertFalse($admin->determinedPerPageValue(25));
1252
        $this->assertFalse($admin->determinedPerPageValue(200));
1253
        $this->assertTrue($admin->determinedPerPageValue(101));
1254
        $this->assertTrue($admin->determinedPerPageValue(102));
1255
        $this->assertTrue($admin->determinedPerPageValue(103));
1256
    }
1257
1258
    public function testIsGranted()
1259
    {
1260
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1261
1262
        $entity = new \stdClass();
1263
1264
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
1265
        $securityHandler->expects($this->any())
1266
            ->method('isGranted')
1267
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin, $entity) {
1268
                if ($admin == $adminIn && $attributes == 'FOO') {
1269
                    if (($object == $admin) || ($object == $entity)) {
1270
                        return true;
1271
                    }
1272
                }
1273
1274
                return false;
1275
            }));
1276
1277
        $admin->setSecurityHandler($securityHandler);
1278
1279
        $this->assertTrue($admin->isGranted('FOO'));
1280
        $this->assertTrue($admin->isGranted('FOO', $entity));
1281
        $this->assertFalse($admin->isGranted('BAR'));
1282
        $this->assertFalse($admin->isGranted('BAR', $entity));
1283
    }
1284
1285
    public function testSupportsPreviewMode()
1286
    {
1287
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1288
1289
        $this->assertFalse($admin->supportsPreviewMode());
1290
    }
1291
1292
    public function testGetPermissionsShow()
1293
    {
1294
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1295
1296
        $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
1297
        $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
1298
        $this->assertSame(array('LIST'), $admin->getPermissionsShow('foo'));
1299
    }
1300
1301
    public function testShowIn()
1302
    {
1303
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1304
1305
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
1306
        $securityHandler->expects($this->any())
1307
            ->method('isGranted')
1308
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

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

Loading history...
1309
                if ($admin == $adminIn && $attributes == array('LIST')) {
1310
                    return true;
1311
                }
1312
1313
                return false;
1314
            }));
1315
1316
        $admin->setSecurityHandler($securityHandler);
1317
1318
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
1319
        $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
1320
        $this->assertTrue($admin->showIn('foo'));
1321
    }
1322
1323
    public function testGetObjectIdentifier()
1324
    {
1325
        $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1326
1327
        $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
1328
    }
1329
1330
    /**
1331
     * @group legacy
1332
     */
1333
    public function testTrans()
1334
    {
1335
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1336
        $admin->setTranslationDomain('fooMessageDomain');
1337
1338
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1339
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

Loading history...
1340
1341
        $translator->expects($this->once())
1342
            ->method('trans')
1343
            ->with($this->equalTo('foo'), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
1344
            ->will($this->returnValue('fooTranslated'));
1345
1346
        $this->assertSame('fooTranslated', $admin->trans('foo'));
1347
    }
1348
1349
    /**
1350
     * @group legacy
1351
     */
1352
    public function testTransWithMessageDomain()
1353
    {
1354
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1355
1356
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1357
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

Loading history...
1358
1359
        $translator->expects($this->once())
1360
            ->method('trans')
1361
            ->with($this->equalTo('foo'), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
1362
            ->will($this->returnValue('fooTranslated'));
1363
1364
        $this->assertSame('fooTranslated', $admin->trans('foo', array('name' => 'Andrej'), 'fooMessageDomain'));
1365
    }
1366
1367
    /**
1368
     * @group legacy
1369
     */
1370
    public function testTransChoice()
1371
    {
1372
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1373
        $admin->setTranslationDomain('fooMessageDomain');
1374
1375
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1376
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

Loading history...
1377
1378
        $translator->expects($this->once())
1379
            ->method('transChoice')
1380
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
1381
            ->will($this->returnValue('fooTranslated'));
1382
1383
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

Loading history...
1384
    }
1385
1386
    /**
1387
     * @group legacy
1388
     */
1389
    public function testTransChoiceWithMessageDomain()
1390
    {
1391
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1392
1393
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
1394
        $admin->setTranslator($translator);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...tAdmin::setTranslator() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

Loading history...
1395
1396
        $translator->expects($this->once())
1397
            ->method('transChoice')
1398
            ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
1399
            ->will($this->returnValue('fooTranslated'));
1400
1401
        $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, array('name' => 'Andrej'), 'fooMessageDomain'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::transChoice() has been deprecated with message: since 3.9, to be removed with 4.0

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

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

Loading history...
1402
    }
1403
1404
    public function testSetPersistFilters()
1405
    {
1406
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1407
1408
        $this->assertAttributeSame(false, 'persistFilters', $admin);
1409
        $admin->setPersistFilters(true);
1410
        $this->assertAttributeSame(true, 'persistFilters', $admin);
1411
    }
1412
1413
    public function testGetRootCode()
1414
    {
1415
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1416
1417
        $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
1418
1419
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1420
        $parentFieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
1421
        $parentFieldDescription->expects($this->once())
1422
            ->method('getAdmin')
1423
            ->will($this->returnValue($parentAdmin));
1424
1425
        $this->assertNull($admin->getParentFieldDescription());
1426
        $admin->setParentFieldDescription($parentFieldDescription);
1427
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1428
        $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
1429
    }
1430
1431
    public function testGetRoot()
1432
    {
1433
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1434
1435
        $this->assertSame($admin, $admin->getRoot());
1436
1437
        $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
1438
        $parentFieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
1439
        $parentFieldDescription->expects($this->once())
1440
            ->method('getAdmin')
1441
            ->will($this->returnValue($parentAdmin));
1442
1443
        $this->assertNull($admin->getParentFieldDescription());
1444
        $admin->setParentFieldDescription($parentFieldDescription);
1445
        $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
1446
        $this->assertSame($parentAdmin, $admin->getRoot());
1447
    }
1448
1449
    public function testGetExportFields()
1450
    {
1451
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1452
1453
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1454
        $modelManager->expects($this->once())
1455
            ->method('getExportFields')
1456
            ->with($this->equalTo('NewsBundle\Entity\Post'))
1457
            ->will($this->returnValue(array('foo', 'bar')));
1458
1459
        $admin->setModelManager($modelManager);
1460
        $this->assertSame(array('foo', 'bar'), $admin->getExportFields());
1461
    }
1462
1463
    public function testGetPersistentParametersWithNoExtension()
1464
    {
1465
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1466
1467
        $this->assertEmpty($admin->getPersistentParameters());
1468
    }
1469
1470
    /**
1471
     * @expectedException \RuntimeException
1472
     */
1473
    public function testGetPersistentParametersWithInvalidExtension()
1474
    {
1475
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1476
1477
        $extension = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
1478
        $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(null));
1479
1480
        $admin->addExtension($extension);
1481
1482
        $admin->getPersistentParameters();
1483
    }
1484
1485
    public function testGetPersistentParametersWithValidExtension()
1486
    {
1487
        $expected = array(
1488
            'context' => 'foobar',
1489
        );
1490
1491
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1492
1493
        $extension = $this->createMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
1494
        $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue($expected));
1495
1496
        $admin->addExtension($extension);
1497
1498
        $this->assertSame($expected, $admin->getPersistentParameters());
1499
    }
1500
1501
    public function testGetFormWithNonCollectionParentValue()
1502
    {
1503
        $post = new Post();
1504
        $tagAdmin = $this->createTagAdmin($post);
1505
        $tag = $tagAdmin->getSubject();
1506
1507
        $tag->setPosts(null);
1508
        $tagAdmin->getForm();
1509
        $this->assertSame($post, $tag->getPosts());
1510
    }
1511
1512
    public function testGetFormWithCollectionParentValue()
1513
    {
1514
        $post = new Post();
1515
        $tagAdmin = $this->createTagAdmin($post);
1516
        $tag = $tagAdmin->getSubject();
1517
1518
        // Case of a doctrine collection
1519
        $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
1520
        $this->assertCount(0, $tag->getPosts());
1521
1522
        $tag->addPost(new Post());
1523
1524
        $this->assertCount(1, $tag->getPosts());
1525
1526
        $tagAdmin->getForm();
1527
1528
        $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
1529
        $this->assertCount(2, $tag->getPosts());
1530
        $this->assertContains($post, $tag->getPosts());
1531
1532
        // Case of an array
1533
        $tag->setPosts(array());
1534
        $this->assertCount(0, $tag->getPosts());
1535
1536
        $tag->addPost(new Post());
1537
1538
        $this->assertCount(1, $tag->getPosts());
1539
1540
        $tagAdmin->getForm();
1541
1542
        $this->assertInternalType('array', $tag->getPosts());
1543
        $this->assertCount(2, $tag->getPosts());
1544
        $this->assertContains($post, $tag->getPosts());
1545
    }
1546
1547
    public function testRemoveFieldFromFormGroup()
1548
    {
1549
        $formGroups = array(
1550
            'foobar' => array(
1551
                'fields' => array(
1552
                    'foo' => 'foo',
1553
                    'bar' => 'bar',
1554
                ),
1555
            ),
1556
        );
1557
1558
        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1559
        $admin->setFormGroups($formGroups);
1560
1561
        $admin->removeFieldFromFormGroup('foo');
1562
        $this->assertSame($admin->getFormGroups(), array(
1563
            'foobar' => array(
1564
                'fields' => array(
1565
                    'bar' => 'bar',
1566
                ),
1567
            ),
1568
        ));
1569
1570
        $admin->removeFieldFromFormGroup('bar');
1571
        $this->assertSame($admin->getFormGroups(), array());
1572
    }
1573
1574
    public function testGetFilterParameters()
1575
    {
1576
        $authorId = uniqid();
1577
1578
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1579
1580
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
1581
        $commentAdmin->setParentAssociationMapping('post.author');
1582
        $commentAdmin->setParent($postAdmin);
1583
1584
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request', array('get'));
1585
        $query = $this->createMock('Symfony\Component\HttpFoundation\ParameterBag', array('get'));
1586
        $query->expects($this->any())
1587
            ->method('get')
1588
            ->will($this->returnValue(array()));
1589
        $request->query = $query;
1590
        $request->expects($this->any())
1591
            ->method('get')
1592
            ->will($this->returnValue($authorId));
1593
1594
        $commentAdmin->setRequest($request);
1595
1596
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1597
        $modelManager->expects($this->any())
1598
            ->method('getDefaultSortValues')
1599
            ->will($this->returnValue(array()));
1600
1601
        $commentAdmin->setModelManager($modelManager);
1602
1603
        $parameters = $commentAdmin->getFilterParameters();
1604
1605
        $this->assertTrue(isset($parameters['post__author']));
1606
        $this->assertSame(array('value' => $authorId), $parameters['post__author']);
1607
    }
1608
1609
    public function testGetFilterFieldDescription()
1610
    {
1611
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1612
1613
        $fooFieldDescription = new FieldDescription();
1614
        $barFieldDescription = new FieldDescription();
1615
        $bazFieldDescription = new FieldDescription();
1616
1617
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1618
        $modelManager->expects($this->exactly(3))
1619
            ->method('getNewFieldDescriptionInstance')
1620
            ->will($this->returnCallback(function ($adminClass, $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) {
0 ignored issues
show
Unused Code introduced by
The parameter $filterOptions is not used and could be removed.

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

Loading history...
1621
                switch ($name) {
1622
                    case 'foo':
1623
                        $fieldDescription = $fooFieldDescription;
1624
1625
                        break;
1626
1627
                    case 'bar':
1628
                        $fieldDescription = $barFieldDescription;
1629
1630
                        break;
1631
1632
                    case 'baz':
1633
                        $fieldDescription = $bazFieldDescription;
1634
1635
                        break;
1636
1637
                    default:
1638
                        throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
1639
                        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...
1640
                }
1641
1642
                $fieldDescription->setName($name);
1643
1644
                return $fieldDescription;
1645
            }));
1646
1647
        $modelAdmin->setModelManager($modelManager);
1648
1649
        $pager = $this->createMock('Sonata\AdminBundle\Datagrid\PagerInterface');
1650
1651
        $datagrid = $this->createMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
1652
        $datagrid->expects($this->once())
1653
            ->method('getPager')
1654
            ->will($this->returnValue($pager));
1655
1656
        $datagridBuilder = $this->createMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
1657
        $datagridBuilder->expects($this->once())
1658
            ->method('getBaseDatagrid')
1659
            ->with($this->identicalTo($modelAdmin), array())
1660
            ->will($this->returnValue($datagrid));
1661
1662
        $datagridBuilder->expects($this->exactly(3))
1663
            ->method('addFilter')
1664
            ->will($this->returnCallback(function ($datagrid, $type, $fieldDescription, AdminInterface $admin) {
1665
                $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
1666
                $fieldDescription->mergeOption('field_options', array('required' => false));
1667
            }));
1668
1669
        $modelAdmin->setDatagridBuilder($datagridBuilder);
1670
1671
        $this->assertSame(array('foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription), $modelAdmin->getFilterFieldDescriptions());
1672
        $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
1673
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
1674
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
1675
        $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
1676
        $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
1677
        $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
1678
        $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
1679
    }
1680
1681
    public function testGetSubjectNoRequest()
1682
    {
1683
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1684
        $modelManager
1685
            ->expects($this->never())
1686
            ->method('find');
1687
1688
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1689
        $admin->setModelManager($modelManager);
1690
1691
        $this->assertNull($admin->getSubject());
1692
    }
1693
1694
    public function testGetSideMenu()
1695
    {
1696
        $item = $this->createMock('Knp\Menu\ItemInterface');
1697
        $item
1698
            ->expects($this->once())
1699
            ->method('setChildrenAttribute')
1700
            ->with('class', 'nav navbar-nav');
1701
        $item
1702
            ->expects($this->once())
1703
            ->method('setExtra')
1704
            ->with('translation_domain', 'foo_bar_baz');
1705
1706
        $menuFactory = $this->createMock('Knp\Menu\FactoryInterface');
1707
        $menuFactory
1708
            ->expects($this->once())
1709
            ->method('createItem')
1710
            ->will($this->returnValue($item));
1711
1712
        $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1713
        $modelAdmin->setMenuFactory($menuFactory);
1714
        $modelAdmin->setTranslationDomain('foo_bar_baz');
1715
1716
        $modelAdmin->getSideMenu('foo');
1717
    }
1718
1719
    /**
1720
     * @return array
1721
     */
1722
    public function provideGetSubject()
1723
    {
1724
        return array(
1725
            array(23),
1726
            array('azerty'),
1727
            array('4f69bbb5f14a13347f000092'),
1728
            array('0779ca8d-e2be-11e4-ac58-0242ac11000b'),
1729
            array('123'.AdapterInterface::ID_SEPARATOR.'my_type'), // composite keys are supported
1730
        );
1731
    }
1732
1733
    /**
1734
     * @dataProvider provideGetSubject
1735
     */
1736
    public function testGetSubjectFailed($id)
1737
    {
1738
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1739
        $modelManager
1740
            ->expects($this->once())
1741
            ->method('find')
1742
            ->with('NewsBundle\Entity\Post', $id)
1743
            ->will($this->returnValue(null)); // entity not found
1744
1745
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1746
        $admin->setModelManager($modelManager);
1747
1748
        $admin->setRequest(new Request(array('id' => $id)));
1749
        $this->assertNull($admin->getSubject());
1750
    }
1751
1752
    /**
1753
     * @dataProvider provideGetSubject
1754
     */
1755
    public function testGetSubject($id)
1756
    {
1757
        $entity = new Post();
1758
1759
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1760
        $modelManager
1761
            ->expects($this->once())
1762
            ->method('find')
1763
            ->with('NewsBundle\Entity\Post', $id)
1764
            ->will($this->returnValue($entity));
1765
1766
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1767
        $admin->setModelManager($modelManager);
1768
1769
        $admin->setRequest(new Request(array('id' => $id)));
1770
        $this->assertSame($entity, $admin->getSubject());
1771
        $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
1772
    }
1773
1774
    public function testGetSubjectWithParentDescription()
1775
    {
1776
        $adminId = 1;
1777
1778
        $comment = new Comment();
1779
1780
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
1781
        $modelManager
1782
            ->expects($this->any())
1783
            ->method('find')
1784
            ->with('NewsBundle\Entity\Comment', $adminId)
1785
            ->will($this->returnValue($comment));
1786
1787
        $request = new Request(array('id' => $adminId));
1788
1789
        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1790
        $postAdmin->setRequest($request);
1791
1792
        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
1793
        $commentAdmin->setRequest($request);
1794
        $commentAdmin->setModelManager($modelManager);
1795
1796
        $this->assertEquals($comment, $commentAdmin->getSubject());
1797
1798
        $commentAdmin->setSubject(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1799
        $commentAdmin->setParentFieldDescription(new FieldDescription());
1800
1801
        $this->assertNull($commentAdmin->getSubject());
1802
    }
1803
1804
    /**
1805
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1806
     */
1807
    public function testGetActionButtonsList()
1808
    {
1809
        $expected = array(
1810
            'create' => array(
1811
                'template' => 'Foo.html.twig',
1812
            ),
1813
        );
1814
1815
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1816
1817
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1818
        $securityHandler
1819
            ->expects($this->once())
1820
            ->method('isGranted')
1821
            ->with($admin, 'CREATE', $admin)
1822
            ->will($this->returnValue(true));
1823
        $admin->setSecurityHandler($securityHandler);
1824
1825
        $routeGenerator = $this->createMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
1826
        $routeGenerator
1827
            ->expects($this->once())
1828
            ->method('hasAdminRoute')
1829
            ->with($admin, 'create')
1830
            ->will($this->returnValue(true));
1831
        $admin->setRouteGenerator($routeGenerator);
1832
1833
        $admin->setTemplate('button_create', 'Foo.html.twig');
1834
1835
        $this->assertSame($expected, $admin->getActionButtons('list', null));
1836
    }
1837
1838
    /**
1839
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
1840
     */
1841
    public function testGetActionButtonsListCreateDisabled()
1842
    {
1843
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1844
1845
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1846
        $securityHandler
1847
            ->expects($this->once())
1848
            ->method('isGranted')
1849
            ->with($admin, 'CREATE', $admin)
1850
            ->will($this->returnValue(false));
1851
        $admin->setSecurityHandler($securityHandler);
1852
1853
        $this->assertSame(array(), $admin->getActionButtons('list', null));
1854
    }
1855
1856
    /**
1857
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
1858
     */
1859
    public function testGetBatchActions()
1860
    {
1861
        $expected = array(
1862
            'delete' => array(
1863
                'label' => 'action_delete',
1864
                'translation_domain' => 'SonataAdminBundle',
1865
                'ask_confirmation' => true, // by default always true
1866
            ),
1867
            'foo' => array(
1868
                'label' => 'action_foo',
1869
                'translation_domain' => 'SonataAdminBundle',
1870
            ),
1871
            'bar' => array(
1872
                'label' => 'batch.label_bar',
1873
                'translation_domain' => 'SonataAdminBundle',
1874
            ),
1875
            'baz' => array(
1876
                'label' => 'action_baz',
1877
                'translation_domain' => 'AcmeAdminBundle',
1878
            ),
1879
        );
1880
1881
        $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->createMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
1882
1883
        $labelTranslatorStrategy = $this->createMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
1884
        $labelTranslatorStrategy->expects($this->any())
1885
            ->method('getLabel')
1886
            ->will($this->returnCallback(function ($label, $context = '', $type = '') {
1887
                return $context.'.'.$type.'_'.$label;
1888
            }));
1889
1890
        $admin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1891
        $admin->setRouteBuilder($pathInfo);
1892
        $admin->setTranslationDomain('SonataAdminBundle');
1893
        $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
1894
1895
        $routeGenerator = $this->createMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
1896
        $routeGenerator
1897
            ->expects($this->once())
1898
            ->method('hasAdminRoute')
1899
            ->with($admin, 'delete')
1900
            ->will($this->returnValue(true));
1901
        $admin->setRouteGenerator($routeGenerator);
1902
1903
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1904
        $securityHandler->expects($this->any())
1905
            ->method('isGranted')
1906
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

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

Loading history...
1907
                if ($admin == $adminIn && $attributes == 'DELETE') {
1908
                    return true;
1909
                }
1910
1911
                return false;
1912
            }));
1913
        $admin->setSecurityHandler($securityHandler);
1914
1915
        $this->assertSame($expected, $admin->getBatchActions());
1916
    }
1917
1918
    /**
1919
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
1920
     */
1921
    public function testShowMosaicButton()
1922
    {
1923
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1924
        $listModes = $admin->getListModes();
1925
1926
        $admin->showMosaicButton(true);
1927
1928
        $this->assertSame($listModes, $admin->getListModes());
1929
    }
1930
1931
    /**
1932
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
1933
     */
1934
    public function testShowMosaicButtonHideMosaic()
1935
    {
1936
        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
1937
        $listModes = $admin->getListModes();
1938
        $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...
1939
1940
        $admin->showMosaicButton(false);
1941
1942
        $this->assertSame($expected, $admin->getListModes());
1943
    }
1944
1945
    /**
1946
     * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
1947
     * @dataProvider provideGetBaseRouteName
1948
     */
1949
    public function testDefaultDashboardActionsArePresent($objFqn, $expected)
1950
    {
1951
        $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->createMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
1952
1953
        $routeGenerator = new DefaultRouteGenerator(
1954
            $this->createMock('Symfony\Component\Routing\RouterInterface'),
1955
            new RoutesCache($this->cacheTempFolder, true)
1956
        );
1957
1958
        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
1959
        $admin->setRouteBuilder($pathInfo);
1960
        $admin->setRouteGenerator($routeGenerator);
1961
        $admin->initialize();
1962
1963
        $securityHandler = $this->createMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
1964
        $securityHandler->expects($this->any())
1965
            ->method('isGranted')
1966
            ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

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

Loading history...
1967
                if ($admin == $adminIn && ($attributes == 'CREATE' || $attributes == 'LIST')) {
1968
                    return true;
1969
                }
1970
1971
                return false;
1972
            }));
1973
1974
        $admin->setSecurityHandler($securityHandler);
1975
1976
        $this->assertArrayHasKey('list', $admin->getDashboardActions());
1977
        $this->assertArrayHasKey('create', $admin->getDashboardActions());
1978
    }
1979
1980
    public function testDefaultFilters()
1981
    {
1982
        $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
1983
1984
        $subjectId = uniqid();
1985
1986
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request', array('get'));
1987
        $query = $this->createMock('Symfony\Component\HttpFoundation\ParameterBag', array('set', 'get'));
1988
        $query->expects($this->any())
1989
            ->method('get')
1990
            ->with($this->equalTo('filter'))
1991
            ->will($this->returnValue(array(
1992
                'a' => array(
1993
                    'value' => 'b',
1994
                ),
1995
                'foo' => array(
1996
                    'type' => '1',
1997
                    'value' => 'bar',
1998
                ),
1999
                'baz' => array(
2000
                    'type' => '5',
2001
                    'value' => 'test',
2002
                ),
2003
            )));
2004
        $request->query = $query;
2005
2006
        $request->expects($this->any())
2007
            ->method('get')
2008
            ->will($this->returnValue($subjectId));
2009
2010
        $admin->setRequest($request);
2011
2012
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
2013
        $modelManager->expects($this->any())
2014
            ->method('getDefaultSortValues')
2015
            ->will($this->returnValue(array()));
2016
2017
        $admin->setModelManager($modelManager);
2018
2019
        $this->assertEquals(array(
2020
            'foo' => array(
2021
                'type' => '1',
2022
                'value' => 'bar',
2023
            ),
2024
            'baz' => array(
2025
                'type' => '5',
2026
                'value' => 'test',
2027
            ),
2028
            '_page' => 1,
2029
            '_per_page' => 32,
2030
            'a' => array(
2031
                'value' => 'b',
2032
            ),
2033
        ), $admin->getFilterParameters());
2034
2035
        $this->assertTrue($admin->isDefaultFilter('foo'));
2036
        $this->assertFalse($admin->isDefaultFilter('bar'));
2037
        $this->assertFalse($admin->isDefaultFilter('a'));
2038
    }
2039
2040
    /**
2041
     * @group legacy
2042
     */
2043
    public function testDefaultBreadcrumbsBuilder()
2044
    {
2045
        $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
2046
        $container->expects($this->once())
2047
            ->method('getParameter')
2048
            ->with('sonata.admin.configuration.breadcrumbs')
2049
            ->will($this->returnValue(array()));
2050
2051
        $pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
2052
            ->disableOriginalConstructor()
2053
            ->getMock();
2054
        $pool->expects($this->once())
2055
            ->method('getContainer')
2056
            ->will($this->returnValue($container));
2057
2058
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
2059
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2060
        ), '', true, true, true, array('getConfigurationPool'));
2061
        $admin->expects($this->once())
2062
            ->method('getConfigurationPool')
2063
            ->will($this->returnValue($pool));
2064
2065
        $this->assertInstanceOf(
2066
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilder',
2067
            $admin->getBreadcrumbsBuilder()
2068
        );
2069
    }
2070
2071
    /**
2072
     * @group legacy
2073
     */
2074
    public function testBreadcrumbsBuilderSetter()
2075
    {
2076
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
2077
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2078
        ));
2079
        $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->createMock(
2080
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
2081
        )));
2082
        $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
2083
    }
2084
2085
    /**
2086
     * @group legacy
2087
     */
2088
    public function testGetBreadcrumbs()
2089
    {
2090
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
2091
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2092
        ));
2093
        $builder = $this->prophesize(
2094
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
2095
        );
2096
        $action = 'myaction';
2097
        $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
2098
        $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
2099
    }
2100
2101
    /**
2102
     * @group legacy
2103
     */
2104
    public function testBuildBreadcrumbs()
2105
    {
2106
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
2107
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2108
        ));
2109
        $builder = $this->prophesize(
2110
            'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
2111
        );
2112
        $action = 'myaction';
2113
        $menu = $this->createMock('Knp\Menu\ItemInterface');
2114
        $builder->buildBreadcrumbs($admin, $action, $menu)
2115
            ->shouldBeCalledTimes(1)
2116
            ->willReturn($menu);
2117
        $admin->setBreadcrumbsBuilder($builder->reveal());
2118
2119
        /* check the called is proxied only once */
2120
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2121
        $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
2122
    }
2123
2124
    /**
2125
     * NEXT_MAJOR: remove this method.
2126
     *
2127
     * @group legacy
2128
     */
2129
    public function testCreateQueryLegacyCallWorks()
2130
    {
2131
        $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
2132
            'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
2133
        ));
2134
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
2135
        $modelManager->expects($this->once())
2136
            ->method('createQuery')
2137
            ->with('My\Class')
2138
            ->willReturn('a query');
2139
2140
        $admin->setModelManager($modelManager);
2141
        $this->assertSame('a query', $admin->createQuery('list'));
2142
    }
2143
2144
    public function testGetDataSourceIterator()
2145
    {
2146
        $datagrid = $this->createMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
2147
        $datagrid->method('buildPager');
2148
2149
        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
2150
        $modelManager->method('getExportFields')->will($this->returnValue(array(
2151
            'field',
2152
            'foo',
2153
            'bar',
2154
        )));
2155
        $modelManager->expects($this->once())->method('getDataSourceIterator')
2156
            ->with($this->equalTo($datagrid), $this->equalTo(array(
2157
                'Feld' => 'field',
2158
                1 => 'foo',
2159
                2 => 'bar',
2160
            )));
2161
2162
        $admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
2163
            ->disableOriginalConstructor()
2164
            ->setMethods(array('getDatagrid', 'getTranslationLabel', 'trans'))
2165
            ->getMockForAbstractClass();
2166
        $admin->method('getDatagrid')->will($this->returnValue($datagrid));
2167
        $admin->setModelManager($modelManager);
2168
2169
        $admin->expects($this->any())
2170
            ->method('getTranslationLabel')
2171
            ->will($this->returnCallback(function ($label, $context = '', $type = '') {
2172
                return $context.'.'.$type.'_'.$label;
2173
            }));
2174
        $admin->expects($this->any())
2175
            ->method('trans')
2176
            ->will($this->returnCallback(function ($label) {
2177
                if ($label == 'export.label_field') {
2178
                    return 'Feld';
2179
                }
2180
2181
                return $label;
2182
            }));
2183
2184
        $admin->getDataSourceIterator();
2185
    }
2186
2187
    public function testCircularChildAdmin()
2188
    {
2189
        $this->expectException(
2190
            'RuntimeException',
2191
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment` admin.'
2192
        );
2193
2194
        $postAdmin = new PostAdmin(
2195
            'sonata.post.admin.post',
2196
            'Application\Sonata\NewsBundle\Entity\Post',
2197
            'SonataNewsBundle:PostAdmin'
2198
        );
2199
        $commentAdmin = new CommentAdmin(
2200
            'sonata.post.admin.comment',
2201
            'Application\Sonata\NewsBundle\Entity\Comment',
2202
            'SonataNewsBundle:CommentAdmin'
2203
        );
2204
        $postAdmin->addChild($commentAdmin);
2205
        $commentAdmin->addChild($postAdmin);
2206
    }
2207
2208
    public function testCircularChildAdminTripleLevel()
2209
    {
2210
        $this->expectException(
2211
            'RuntimeException',
2212
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.comment_vote` admin.'
2213
        );
2214
2215
        $postAdmin = new PostAdmin(
2216
            'sonata.post.admin.post',
2217
            'Application\Sonata\NewsBundle\Entity\Post',
2218
            'SonataNewsBundle:PostAdmin'
2219
        );
2220
        $commentAdmin = new CommentAdmin(
2221
            'sonata.post.admin.comment',
2222
            'Application\Sonata\NewsBundle\Entity\Comment',
2223
            'SonataNewsBundle:CommentAdmin'
2224
        );
2225
        $commentVoteAdmin = new CommentVoteAdmin(
2226
            'sonata.post.admin.comment_vote',
2227
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2228
            'SonataNewsBundle:CommentVoteAdmin'
2229
        );
2230
        $postAdmin->addChild($commentAdmin);
2231
        $commentAdmin->addChild($commentVoteAdmin);
2232
        $commentVoteAdmin->addChild($postAdmin);
2233
    }
2234
2235
    public function testCircularChildAdminWithItself()
2236
    {
2237
        $this->expectException(
2238
            'RuntimeException',
2239
            'Circular reference detected! The child admin `sonata.post.admin.post` is already in the parent tree of the `sonata.post.admin.post` admin.'
2240
        );
2241
2242
        $postAdmin = new PostAdmin(
2243
            'sonata.post.admin.post',
2244
            'Application\Sonata\NewsBundle\Entity\Post',
2245
            'SonataNewsBundle:PostAdmin'
2246
        );
2247
        $postAdmin->addChild($postAdmin);
2248
    }
2249
2250
    public function testGetRootAncestor()
2251
    {
2252
        $postAdmin = new PostAdmin(
2253
            'sonata.post.admin.post',
2254
            'Application\Sonata\NewsBundle\Entity\Post',
2255
            'SonataNewsBundle:PostAdmin'
2256
        );
2257
        $commentAdmin = new CommentAdmin(
2258
            'sonata.post.admin.comment',
2259
            'Application\Sonata\NewsBundle\Entity\Comment',
2260
            'SonataNewsBundle:CommentAdmin'
2261
        );
2262
        $commentVoteAdmin = new CommentVoteAdmin(
2263
            'sonata.post.admin.comment_vote',
2264
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2265
            'SonataNewsBundle:CommentVoteAdmin'
2266
        );
2267
2268
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2269
        $this->assertSame($commentAdmin, $commentAdmin->getRootAncestor());
2270
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2271
2272
        $postAdmin->addChild($commentAdmin);
2273
2274
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2275
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2276
        $this->assertSame($commentVoteAdmin, $commentVoteAdmin->getRootAncestor());
2277
2278
        $commentAdmin->addChild($commentVoteAdmin);
2279
2280
        $this->assertSame($postAdmin, $postAdmin->getRootAncestor());
2281
        $this->assertSame($postAdmin, $commentAdmin->getRootAncestor());
2282
        $this->assertSame($postAdmin, $commentVoteAdmin->getRootAncestor());
2283
    }
2284
2285
    public function testGetChildDepth()
2286
    {
2287
        $postAdmin = new PostAdmin(
2288
            'sonata.post.admin.post',
2289
            'Application\Sonata\NewsBundle\Entity\Post',
2290
            'SonataNewsBundle:PostAdmin'
2291
        );
2292
        $commentAdmin = new CommentAdmin(
2293
            'sonata.post.admin.comment',
2294
            'Application\Sonata\NewsBundle\Entity\Comment',
2295
            'SonataNewsBundle:CommentAdmin'
2296
        );
2297
        $commentVoteAdmin = new CommentVoteAdmin(
2298
            'sonata.post.admin.comment_vote',
2299
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2300
            'SonataNewsBundle:CommentVoteAdmin'
2301
        );
2302
2303
        $this->assertSame(0, $postAdmin->getChildDepth());
2304
        $this->assertSame(0, $commentAdmin->getChildDepth());
2305
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2306
2307
        $postAdmin->addChild($commentAdmin);
2308
2309
        $this->assertSame(0, $postAdmin->getChildDepth());
2310
        $this->assertSame(1, $commentAdmin->getChildDepth());
2311
        $this->assertSame(0, $commentVoteAdmin->getChildDepth());
2312
2313
        $commentAdmin->addChild($commentVoteAdmin);
2314
2315
        $this->assertSame(0, $postAdmin->getChildDepth());
2316
        $this->assertSame(1, $commentAdmin->getChildDepth());
2317
        $this->assertSame(2, $commentVoteAdmin->getChildDepth());
2318
    }
2319
2320
    public function testGetCurrentLeafChildAdmin()
2321
    {
2322
        $postAdmin = new PostAdmin(
2323
            'sonata.post.admin.post',
2324
            'Application\Sonata\NewsBundle\Entity\Post',
2325
            'SonataNewsBundle:PostAdmin'
2326
        );
2327
        $commentAdmin = new CommentAdmin(
2328
            'sonata.post.admin.comment',
2329
            'Application\Sonata\NewsBundle\Entity\Comment',
2330
            'SonataNewsBundle:CommentAdmin'
2331
        );
2332
        $commentVoteAdmin = new CommentVoteAdmin(
2333
            'sonata.post.admin.comment_vote',
2334
            'Application\Sonata\NewsBundle\Entity\CommentVote',
2335
            'SonataNewsBundle:CommentVoteAdmin'
2336
        );
2337
2338
        $postAdmin->addChild($commentAdmin);
2339
        $commentAdmin->addChild($commentVoteAdmin);
2340
2341
        $this->assertNull($postAdmin->getCurrentLeafChildAdmin());
2342
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2343
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2344
2345
        $commentAdmin->setCurrentChild(true);
2346
2347
        $this->assertSame($commentAdmin, $postAdmin->getCurrentLeafChildAdmin());
2348
        $this->assertNull($commentAdmin->getCurrentLeafChildAdmin());
2349
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2350
2351
        $commentVoteAdmin->setCurrentChild(true);
2352
2353
        $this->assertSame($commentVoteAdmin, $postAdmin->getCurrentLeafChildAdmin());
2354
        $this->assertSame($commentVoteAdmin, $commentAdmin->getCurrentLeafChildAdmin());
2355
        $this->assertNull($commentVoteAdmin->getCurrentLeafChildAdmin());
2356
    }
2357
2358
    private function createTagAdmin(Post $post)
2359
    {
2360
        $postAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin')
2361
            ->disableOriginalConstructor()
2362
            ->getMock();
2363
2364
        $postAdmin->expects($this->any())->method('getObject')->will($this->returnValue($post));
2365
2366
        $formBuilder = $this->createMock('Symfony\Component\Form\FormBuilderInterface');
2367
        $formBuilder->expects($this->any())->method('getForm')->will($this->returnValue(null));
2368
2369
        $tagAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\TagAdmin')
2370
            ->setConstructorArgs(array(
2371
                'admin.tag',
2372
                'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag',
2373
                'MyBundle:MyController',
2374
            ))
2375
            ->setMethods(array('getFormBuilder'))
2376
            ->getMock();
2377
2378
        $tagAdmin->expects($this->any())->method('getFormBuilder')->will($this->returnValue($formBuilder));
2379
        $tagAdmin->setParent($postAdmin);
2380
2381
        $tag = new Tag();
2382
        $tagAdmin->setSubject($tag);
2383
2384
        $request = $this->createMock('Symfony\Component\HttpFoundation\Request');
2385
        $tagAdmin->setRequest($request);
2386
2387
        $configurationPool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
2388
            ->disableOriginalConstructor()
2389
            ->getMock();
2390
2391
        $configurationPool->expects($this->any())->method('getPropertyAccessor')->will($this->returnValue(PropertyAccess::createPropertyAccessor()));
2392
2393
        $tagAdmin->setConfigurationPool($configurationPool);
2394
2395
        return $tagAdmin;
2396
    }
2397
}
2398