1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Admin;
- use Sonata\AdminBundle\Admin\AbstractAdmin;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\Pool;
- use Sonata\AdminBundle\Route\DefaultRouteGenerator;
- use Sonata\AdminBundle\Route\RoutesCache;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentWithCustomRouteAdmin;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin;
- use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
- use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
- use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
- use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToStringNull;
- use Sonata\CoreBundle\Model\Adapter\AdapterInterface;
- use Symfony\Component\DependencyInjection\Container;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\PropertyAccess\PropertyAccess;
- class AdminTest extends \PHPUnit_Framework_TestCase
- {
- protected $cacheTempFolder;
- public function setUp()
- {
- $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
- exec('rm -rf '.$this->cacheTempFolder);
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::__construct
- */
- public function testConstructor()
- {
- $class = 'Application\Sonata\NewsBundle\Entity\Post';
- $baseControllerName = 'SonataNewsBundle:PostAdmin';
- $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
- $this->assertInstanceOf('Sonata\AdminBundle\Admin\AbstractAdmin', $admin);
- $this->assertSame($class, $admin->getClass());
- $this->assertSame($baseControllerName, $admin->getBaseControllerName());
- }
- public function testGetClass()
- {
- $class = 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post';
- $baseControllerName = 'SonataNewsBundle:PostAdmin';
- $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
- $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
- $this->assertSame(
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
- $admin->getClass()
- );
- $admin->setSubClasses(array('foo'));
- $this->assertSame(
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
- $admin->getClass()
- );
- $admin->setSubject(null);
- $admin->setSubClasses(array());
- $this->assertSame($class, $admin->getClass());
- $admin->setSubClasses(array('foo' => 'bar'));
- $admin->setRequest(new Request(array('subclass' => 'foo')));
- $this->assertSame('bar', $admin->getClass());
- }
- /**
- * @expectedException \RuntimeException
- * @expectedExceptionMessage Feature not implemented: an embedded admin cannot have subclass
- */
- public function testGetClassException()
- {
- $class = 'Application\Sonata\NewsBundle\Entity\Post';
- $baseControllerName = 'SonataNewsBundle:PostAdmin';
- $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
- $admin->setParentFieldDescription(new FieldDescription());
- $admin->setSubClasses(array('foo' => 'bar'));
- $admin->setRequest(new Request(array('subclass' => 'foo')));
- $admin->getClass();
- }
- public function testCheckAccessThrowsExceptionOnMadeUpAction()
- {
- $admin = new PostAdmin(
- 'sonata.post.admin.post',
- 'Application\Sonata\NewsBundle\Entity\Post',
- 'SonataNewsBundle:PostAdmin'
- );
- $this->setExpectedException(
- '\InvalidArgumentException',
- 'Action "made-up" could not be found'
- );
- $admin->checkAccess('made-up');
- }
- public function testCheckAccessThrowsAccessDeniedException()
- {
- $admin = new PostAdmin(
- 'sonata.post.admin.post',
- 'Application\Sonata\NewsBundle\Entity\Post',
- 'SonataNewsBundle:PostAdmin'
- );
- $securityHandler = $this->prophesize(
- 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
- );
- $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
- $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
- $customExtension = $this->prophesize(
- 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
- );
- $customExtension->getAccessMapping($admin)->willReturn(
- array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
- );
- $admin->addExtension($customExtension->reveal());
- $admin->setSecurityHandler($securityHandler->reveal());
- $this->setExpectedException(
- 'Symfony\Component\Security\Core\Exception\AccessDeniedException',
- 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE'
- );
- $admin->checkAccess('custom_action');
- }
- public function testHasAccessOnMadeUpAction()
- {
- $admin = new PostAdmin(
- 'sonata.post.admin.post',
- 'Application\Sonata\NewsBundle\Entity\Post',
- 'SonataNewsBundle:PostAdmin'
- );
- $this->assertFalse($admin->hasAccess('made-up'));
- }
- public function testHasAccess()
- {
- $admin = new PostAdmin(
- 'sonata.post.admin.post',
- 'Application\Sonata\NewsBundle\Entity\Post',
- 'SonataNewsBundle:PostAdmin'
- );
- $securityHandler = $this->prophesize(
- 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
- );
- $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
- $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
- $customExtension = $this->prophesize(
- 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
- );
- $customExtension->getAccessMapping($admin)->willReturn(
- array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
- );
- $admin->addExtension($customExtension->reveal());
- $admin->setSecurityHandler($securityHandler->reveal());
- $this->assertFalse($admin->hasAccess('custom_action'));
- }
- public function testHasAccessAllowsAccess()
- {
- $admin = new PostAdmin(
- 'sonata.post.admin.post',
- 'Application\Sonata\NewsBundle\Entity\Post',
- 'SonataNewsBundle:PostAdmin'
- );
- $securityHandler = $this->prophesize(
- 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
- );
- $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
- $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true);
- $customExtension = $this->prophesize(
- 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
- );
- $customExtension->getAccessMapping($admin)->willReturn(
- array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
- );
- $admin->addExtension($customExtension->reveal());
- $admin->setSecurityHandler($securityHandler->reveal());
- $this->assertTrue($admin->hasAccess('custom_action'));
- }
- public function testHasAccessAllowsAccessEditAction()
- {
- $admin = new PostAdmin(
- 'sonata.post.admin.post',
- 'Application\Sonata\NewsBundle\Entity\Post',
- 'SonataNewsBundle:PostAdmin'
- );
- $securityHandler = $this->prophesize(
- 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
- );
- $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true);
- $customExtension = $this->prophesize(
- 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
- );
- $customExtension->getAccessMapping($admin)->willReturn(
- array('edit_action' => array('EDIT_ROLE'))
- );
- $admin->addExtension($customExtension->reveal());
- $admin->setSecurityHandler($securityHandler->reveal());
- $this->assertTrue($admin->hasAccess('edit_action'));
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasChild
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::addChild
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getChild
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::isChild
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getChildren
- */
- public function testChildren()
- {
- $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertFalse($postAdmin->hasChildren());
- $this->assertFalse($postAdmin->hasChild('comment'));
- $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
- $postAdmin->addChild($commentAdmin);
- $this->assertTrue($postAdmin->hasChildren());
- $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment'));
- $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode());
- $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute());
- $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent());
- $this->assertFalse($postAdmin->isChild());
- $this->assertTrue($commentAdmin->isChild());
- $this->assertSame(array('sonata.post.admin.comment' => $commentAdmin), $postAdmin->getChildren());
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configure
- */
- public function testConfigure()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNotNull($admin->getUniqid());
- $admin->initialize();
- $this->assertNotNull($admin->getUniqid());
- $this->assertSame('Post', $admin->getClassnameLabel());
- $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
- $admin->setClassnameLabel('postcomment');
- $admin->initialize();
- $this->assertSame('postcomment', $admin->getClassnameLabel());
- }
- public function testConfigureWithValidParentAssociationMapping()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setParentAssociationMapping('Category');
- $admin->initialize();
- $this->assertSame('Category', $admin->getParentAssociationMapping());
- }
- public function provideGetBaseRoutePattern()
- {
- return array(
- array(
- 'Application\Sonata\NewsBundle\Entity\Post',
- '/sonata/news/post',
- ),
- array(
- 'Application\Sonata\NewsBundle\Document\Post',
- '/sonata/news/post',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Post',
- '/myapplication/my/post',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Post\Category',
- '/myapplication/my/post-category',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Product\Category',
- '/myapplication/my/product-category',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Other\Product\Category',
- '/myapplication/my/other-product-category',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
- '/cmf/foo/menu',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
- '/cmf/foo/menu',
- ),
- array(
- 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
- '/symfony/barbar/menu',
- ),
- array(
- 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
- '/symfony/barbar/menu-item',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
- '/cmf/foo/menu',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
- '/cmf/foo/menu',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
- '/cmf/foo/menu',
- ),
- array(
- 'AppBundle\Entity\User',
- '/app/user',
- ),
- array(
- 'App\Entity\User',
- '/app/user',
- ),
- );
- }
- /**
- * @dataProvider provideGetBaseRoutePattern
- */
- public function testGetBaseRoutePattern($objFqn, $expected)
- {
- $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
- $this->assertSame($expected, $admin->getBaseRoutePattern());
- }
- /**
- * @dataProvider provideGetBaseRoutePattern
- */
- public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected)
- {
- $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
- $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
- $commentAdmin->setParent($postAdmin);
- $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern());
- }
- public function testGetBaseRoutePatternWithSpecifedPattern()
- {
- $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin');
- $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern());
- }
- public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern()
- {
- $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
- $commentAdmin->setParent($postAdmin);
- $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern());
- }
- /**
- * @expectedException RuntimeException
- */
- public function testGetBaseRoutePatternWithUnreconizedClassname()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
- $admin->getBaseRoutePattern();
- }
- public function provideGetBaseRouteName()
- {
- return array(
- array(
- 'Application\Sonata\NewsBundle\Entity\Post',
- 'admin_sonata_news_post',
- ),
- array(
- 'Application\Sonata\NewsBundle\Document\Post',
- 'admin_sonata_news_post',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Post',
- 'admin_myapplication_my_post',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Post\Category',
- 'admin_myapplication_my_post_category',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Product\Category',
- 'admin_myapplication_my_product_category',
- ),
- array(
- 'MyApplication\MyBundle\Entity\Other\Product\Category',
- 'admin_myapplication_my_other_product_category',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
- 'admin_cmf_foo_menu',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
- 'admin_cmf_foo_menu',
- ),
- array(
- 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
- 'admin_symfony_barbar_menu',
- ),
- array(
- 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
- 'admin_symfony_barbar_menu_item',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
- 'admin_cmf_foo_menu',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
- 'admin_cmf_foo_menu',
- ),
- array(
- 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
- 'admin_cmf_foo_menu',
- ),
- array(
- 'AppBundle\Entity\User',
- 'admin_app_user',
- ),
- array(
- 'App\Entity\User',
- 'admin_app_user',
- ),
- );
- }
- /**
- * @dataProvider provideGetBaseRouteName
- */
- public function testGetBaseRouteName($objFqn, $expected)
- {
- $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
- $this->assertSame($expected, $admin->getBaseRouteName());
- }
- /**
- * @dataProvider provideGetBaseRouteName
- */
- public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected)
- {
- $routeGenerator = new DefaultRouteGenerator(
- $this->getMock('Symfony\Component\Routing\RouterInterface'),
- new RoutesCache($this->cacheTempFolder, true)
- );
- $container = new Container();
- $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
- $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
- $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
- $container->set('sonata.post.admin.post', $postAdmin);
- $postAdmin->setConfigurationPool($pool);
- $postAdmin->setRouteBuilder($pathInfo);
- $postAdmin->setRouteGenerator($routeGenerator);
- $postAdmin->initialize();
- $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
- $container->set('sonata.post.admin.comment', $commentAdmin);
- $commentAdmin->setConfigurationPool($pool);
- $commentAdmin->setRouteBuilder($pathInfo);
- $commentAdmin->setRouteGenerator($routeGenerator);
- $commentAdmin->initialize();
- $postAdmin->addChild($commentAdmin);
- $pool->setAdminServiceIds(array('sonata.post.admin.post', 'sonata.post.admin.comment'));
- $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
- $this->assertTrue($postAdmin->hasRoute('show'));
- $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show'));
- $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show'));
- $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
- $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
- $this->assertFalse($commentAdmin->hasRoute('edit'));
- /*
- * Test the route name from request
- */
- $postListRequest = new Request(
- array(),
- array(),
- array(
- '_route' => $postAdmin->getBaseRouteName().'_list',
- )
- );
- $postAdmin->setRequest($postListRequest);
- $commentAdmin->setRequest($postListRequest);
- $this->assertTrue($postAdmin->isCurrentRoute('list'));
- $this->assertFalse($postAdmin->isCurrentRoute('create'));
- $this->assertFalse($commentAdmin->isCurrentRoute('list'));
- $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
- $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
- }
- /**
- * @expectedException \RuntimeException
- */
- public function testGetBaseRouteNameWithUnreconizedClassname()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
- $admin->getBaseRouteName();
- }
- public function testGetBaseRouteNameWithSpecifiedName()
- {
- $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('post_custom', $postAdmin->getBaseRouteName());
- }
- public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName()
- {
- $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
- $commentAdmin->setParent($postAdmin);
- $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName());
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid
- */
- public function testUniqid()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $uniqid = uniqid();
- $admin->setUniqid($uniqid);
- $this->assertSame($uniqid, $admin->getUniqid());
- }
- public function testToString()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $s = new \stdClass();
- $this->assertNotEmpty($admin->toString($s));
- $s = new FooToString();
- $this->assertSame('salut', $admin->toString($s));
- // To string method is implemented, but returns null
- $s = new FooToStringNull();
- $this->assertNotEmpty($admin->toString($s));
- $this->assertSame('', $admin->toString(false));
- }
- public function testIsAclEnabled()
- {
- $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertFalse($postAdmin->isAclEnabled());
- $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
- $commentAdmin->setSecurityHandler($this->getMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface'));
- $this->assertTrue($commentAdmin->isAclEnabled());
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getClass
- */
- public function testSubClass()
- {
- $admin = new PostAdmin(
- 'sonata.post.admin.post',
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
- 'SonataNewsBundle:PostAdmin'
- );
- $this->assertFalse($admin->hasSubClass('test'));
- $this->assertFalse($admin->hasActiveSubClass());
- $this->assertCount(0, $admin->getSubClasses());
- $this->assertNull($admin->getActiveSubClass());
- $this->assertNull($admin->getActiveSubclassCode());
- $this->assertSame(
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
- $admin->getClass()
- );
- // Just for the record, if there is no inheritance set, the getSubject is not used
- // the getSubject can also lead to some issue
- $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
- $this->assertSame(
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
- $admin->getClass()
- );
- $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
- $this->assertFalse($admin->hasSubClass('test'));
- $this->assertTrue($admin->hasSubClass('extended1'));
- $this->assertFalse($admin->hasActiveSubClass());
- $this->assertCount(2, $admin->getSubClasses());
- $this->assertNull($admin->getActiveSubClass());
- $this->assertNull($admin->getActiveSubclassCode());
- $this->assertSame(
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
- $admin->getClass()
- );
- $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
- $admin->setRequest($request);
- $this->assertFalse($admin->hasSubClass('test'));
- $this->assertTrue($admin->hasSubClass('extended1'));
- $this->assertTrue($admin->hasActiveSubClass());
- $this->assertCount(2, $admin->getSubClasses());
- $this->assertSame(
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
- $admin->getActiveSubClass()
- );
- $this->assertSame('extended1', $admin->getActiveSubclassCode());
- $this->assertSame(
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
- $admin->getClass()
- );
- $request->query->set('subclass', 'inject');
- $this->assertNull($admin->getActiveSubclassCode());
- }
- /**
- * @expectedException \RuntimeException
- */
- public function testNonExistantSubclass()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setRequest(new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'inject')));
- $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
- $this->assertTrue($admin->hasActiveSubClass());
- $admin->getActiveSubClass();
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
- */
- public function testOnlyOneSubclassNeededToBeActive()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1'));
- $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
- $admin->setRequest($request);
- $this->assertTrue($admin->hasActiveSubClass());
- }
- public function testGetPerPageOptions()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array(16, 32, 64, 128, 192), $admin->getPerPageOptions());
- $admin->setPerPageOptions(array(500, 1000));
- $this->assertSame(array(500, 1000), $admin->getPerPageOptions());
- }
- public function testGetLabelTranslatorStrategy()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getLabelTranslatorStrategy());
- $labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
- $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
- $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy());
- }
- public function testGetRouteBuilder()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getRouteBuilder());
- $routeBuilder = $this->getMock('Sonata\AdminBundle\Builder\RouteBuilderInterface');
- $admin->setRouteBuilder($routeBuilder);
- $this->assertSame($routeBuilder, $admin->getRouteBuilder());
- }
- public function testGetMenuFactory()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getMenuFactory());
- $menuFactory = $this->getMock('Knp\Menu\FactoryInterface');
- $admin->setMenuFactory($menuFactory);
- $this->assertSame($menuFactory, $admin->getMenuFactory());
- }
- public function testGetExtensions()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array(), $admin->getExtensions());
- $adminExtension1 = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
- $adminExtension2 = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
- $admin->addExtension($adminExtension1);
- $admin->addExtension($adminExtension2);
- $this->assertSame(array($adminExtension1, $adminExtension2), $admin->getExtensions());
- }
- public function testGetFilterTheme()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array(), $admin->getFilterTheme());
- $admin->setFilterTheme(array('FooTheme'));
- $this->assertSame(array('FooTheme'), $admin->getFilterTheme());
- }
- public function testGetFormTheme()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array(), $admin->getFormTheme());
- $admin->setFormTheme(array('FooTheme'));
- $this->assertSame(array('FooTheme'), $admin->getFormTheme());
- }
- public function testGetValidator()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getValidator());
- $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
- $admin->setValidator($validator);
- $this->assertSame($validator, $admin->getValidator());
- }
- public function testGetSecurityHandler()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getSecurityHandler());
- $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
- $admin->setSecurityHandler($securityHandler);
- $this->assertSame($securityHandler, $admin->getSecurityHandler());
- }
- public function testGetSecurityInformation()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array(), $admin->getSecurityInformation());
- $securityInformation = array('ROLE_FOO', 'ROLE_BAR');
- $admin->setSecurityInformation($securityInformation);
- $this->assertSame($securityInformation, $admin->getSecurityInformation());
- }
- public function testGetManagerType()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getManagerType());
- $admin->setManagerType('foo_orm');
- $this->assertSame('foo_orm', $admin->getManagerType());
- }
- public function testGetModelManager()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getModelManager());
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $admin->setModelManager($modelManager);
- $this->assertSame($modelManager, $admin->getModelManager());
- }
- public function testGetBaseCodeRoute()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('', $admin->getBaseCodeRoute());
- $admin->setBaseCodeRoute('foo');
- $this->assertSame('foo', $admin->getBaseCodeRoute());
- }
- public function testGetRouteGenerator()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getRouteGenerator());
- $routeGenerator = $this->getMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
- $admin->setRouteGenerator($routeGenerator);
- $this->assertSame($routeGenerator, $admin->getRouteGenerator());
- }
- public function testGetConfigurationPool()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getConfigurationPool());
- $pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
- ->disableOriginalConstructor()
- ->getMock();
- $admin->setConfigurationPool($pool);
- $this->assertSame($pool, $admin->getConfigurationPool());
- }
- public function testGetShowBuilder()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getShowBuilder());
- $showBuilder = $this->getMock('Sonata\AdminBundle\Builder\ShowBuilderInterface');
- $admin->setShowBuilder($showBuilder);
- $this->assertSame($showBuilder, $admin->getShowBuilder());
- }
- public function testGetListBuilder()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getListBuilder());
- $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
- $admin->setListBuilder($listBuilder);
- $this->assertSame($listBuilder, $admin->getListBuilder());
- }
- public function testGetDatagridBuilder()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getDatagridBuilder());
- $datagridBuilder = $this->getMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
- $admin->setDatagridBuilder($datagridBuilder);
- $this->assertSame($datagridBuilder, $admin->getDatagridBuilder());
- }
- public function testGetFormContractor()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getFormContractor());
- $formContractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
- $admin->setFormContractor($formContractor);
- $this->assertSame($formContractor, $admin->getFormContractor());
- }
- public function testGetRequest()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertFalse($admin->hasRequest());
- $request = new Request();
- $admin->setRequest($request);
- $this->assertSame($request, $admin->getRequest());
- $this->assertTrue($admin->hasRequest());
- }
- public function testGetRequestWithException()
- {
- $this->setExpectedException('RuntimeException', 'The Request object has not been set');
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->getRequest();
- }
- public function testGetTranslationDomain()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('messages', $admin->getTranslationDomain());
- $admin->setTranslationDomain('foo');
- $this->assertSame('foo', $admin->getTranslationDomain());
- }
- public function testGetTranslator()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getTranslator());
- $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
- $admin->setTranslator($translator);
- $this->assertSame($translator, $admin->getTranslator());
- }
- public function testGetShowGroups()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(false, $admin->getShowGroups());
- $groups = array('foo', 'bar', 'baz');
- $admin->setShowGroups($groups);
- $this->assertSame($groups, $admin->getShowGroups());
- }
- public function testGetFormGroups()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(false, $admin->getFormGroups());
- $groups = array('foo', 'bar', 'baz');
- $admin->setFormGroups($groups);
- $this->assertSame($groups, $admin->getFormGroups());
- }
- public function testGetMaxPageLinks()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(25, $admin->getMaxPageLinks());
- $admin->setMaxPageLinks(14);
- $this->assertSame(14, $admin->getMaxPageLinks());
- }
- public function testGetMaxPerPage()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(32, $admin->getMaxPerPage());
- $admin->setMaxPerPage(94);
- $this->assertSame(94, $admin->getMaxPerPage());
- }
- public function testGetLabel()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getLabel());
- $admin->setLabel('FooLabel');
- $this->assertSame('FooLabel', $admin->getLabel());
- }
- public function testGetBaseController()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName());
- $admin->setBaseControllerName('SonataNewsBundle:FooAdmin');
- $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName());
- }
- public function testGetTemplates()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array(), $admin->getTemplates());
- $templates = array(
- 'list' => 'FooAdminBundle:CRUD:list.html.twig',
- 'show' => 'FooAdminBundle:CRUD:show.html.twig',
- 'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
- );
- $admin->setTemplates($templates);
- $this->assertSame($templates, $admin->getTemplates());
- }
- public function testGetTemplate1()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getTemplate('edit'));
- $admin->setTemplate('edit', 'FooAdminBundle:CRUD:edit.html.twig');
- $admin->setTemplate('show', 'FooAdminBundle:CRUD:show.html.twig');
- $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
- $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
- }
- public function testGetTemplate2()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertNull($admin->getTemplate('edit'));
- $templates = array(
- 'list' => 'FooAdminBundle:CRUD:list.html.twig',
- 'show' => 'FooAdminBundle:CRUD:show.html.twig',
- 'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
- );
- $admin->setTemplates($templates);
- $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
- $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
- }
- public function testGetIdParameter()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('id', $admin->getIdParameter());
- $this->assertFalse($admin->isChild());
- $parentAdmin = new PostAdmin('sonata.post.admin.post_parent', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostParentAdmin');
- $admin->setParent($parentAdmin);
- $this->assertTrue($admin->isChild());
- $this->assertSame('childId', $admin->getIdParameter());
- }
- public function testGetExportFormats()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array('json', 'xml', 'csv', 'xls'), $admin->getExportFormats());
- }
- public function testGetUrlsafeIdentifier()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $entity = new \stdClass();
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->once())
- ->method('getUrlsafeIdentifier')
- ->with($this->equalTo($entity))
- ->will($this->returnValue('foo'));
- $admin->setModelManager($modelManager);
- $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity));
- }
- public function testDeterminedPerPageValue()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertFalse($admin->determinedPerPageValue('foo'));
- $this->assertFalse($admin->determinedPerPageValue(123));
- $this->assertTrue($admin->determinedPerPageValue(16));
- $this->assertTrue($admin->determinedPerPageValue(32));
- $this->assertTrue($admin->determinedPerPageValue(64));
- $this->assertTrue($admin->determinedPerPageValue(128));
- $this->assertTrue($admin->determinedPerPageValue(192));
- $admin->setPerPageOptions(array(101, 102, 103));
- $this->assertFalse($admin->determinedPerPageValue(15));
- $this->assertFalse($admin->determinedPerPageValue(25));
- $this->assertFalse($admin->determinedPerPageValue(200));
- $this->assertTrue($admin->determinedPerPageValue(101));
- $this->assertTrue($admin->determinedPerPageValue(102));
- $this->assertTrue($admin->determinedPerPageValue(103));
- }
- public function testIsGranted()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $entity = new \stdClass();
- $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
- $securityHandler->expects($this->any())
- ->method('isGranted')
- ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin, $entity) {
- if ($admin == $adminIn && $attributes == 'FOO') {
- if (($object == $admin) || ($object == $entity)) {
- return true;
- }
- }
- return false;
- }));
- $admin->setSecurityHandler($securityHandler);
- $this->assertTrue($admin->isGranted('FOO'));
- $this->assertTrue($admin->isGranted('FOO', $entity));
- $this->assertFalse($admin->isGranted('BAR'));
- $this->assertFalse($admin->isGranted('BAR', $entity));
- }
- public function testSupportsPreviewMode()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertFalse($admin->supportsPreviewMode());
- }
- public function testGetPermissionsShow()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
- $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
- $this->assertSame(array('LIST'), $admin->getPermissionsShow('foo'));
- }
- public function testShowIn()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
- $securityHandler->expects($this->any())
- ->method('isGranted')
- ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
- if ($admin == $adminIn && $attributes == array('LIST')) {
- return true;
- }
- return false;
- }));
- $admin->setSecurityHandler($securityHandler);
- $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
- $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
- $this->assertTrue($admin->showIn('foo'));
- }
- public function testGetObjectIdentifier()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
- }
- public function testTransWithNoTranslator()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('foo', $admin->trans('foo'));
- }
- public function testTrans()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setTranslationDomain('fooMessageDomain');
- $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
- $admin->setTranslator($translator);
- $translator->expects($this->once())
- ->method('trans')
- ->with($this->equalTo('foo'), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
- ->will($this->returnValue('fooTranslated'));
- $this->assertSame('fooTranslated', $admin->trans('foo'));
- }
- public function testTransWithMessageDomain()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
- $admin->setTranslator($translator);
- $translator->expects($this->once())
- ->method('trans')
- ->with($this->equalTo('foo'), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
- ->will($this->returnValue('fooTranslated'));
- $this->assertSame('fooTranslated', $admin->trans('foo', array('name' => 'Andrej'), 'fooMessageDomain'));
- }
- public function testTransChoiceWithNoTranslator()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('foo', $admin->transChoice('foo', 2));
- }
- public function testTransChoice()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setTranslationDomain('fooMessageDomain');
- $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
- $admin->setTranslator($translator);
- $translator->expects($this->once())
- ->method('transChoice')
- ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
- ->will($this->returnValue('fooTranslated'));
- $this->assertSame('fooTranslated', $admin->transChoice('foo', 2));
- }
- public function testTransChoiceWithMessageDomain()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
- $admin->setTranslator($translator);
- $translator->expects($this->once())
- ->method('transChoice')
- ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
- ->will($this->returnValue('fooTranslated'));
- $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, array('name' => 'Andrej'), 'fooMessageDomain'));
- }
- public function testSetPersistFilters()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertAttributeSame(false, 'persistFilters', $admin);
- $admin->setPersistFilters(true);
- $this->assertAttributeSame(true, 'persistFilters', $admin);
- }
- public function testGetRootCode()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
- $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
- $parentFieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $parentFieldDescription->expects($this->once())
- ->method('getAdmin')
- ->will($this->returnValue($parentAdmin));
- $this->assertNull($admin->getParentFieldDescription());
- $admin->setParentFieldDescription($parentFieldDescription);
- $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
- $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
- }
- public function testGetRoot()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame($admin, $admin->getRoot());
- $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
- $parentFieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $parentFieldDescription->expects($this->once())
- ->method('getAdmin')
- ->will($this->returnValue($parentAdmin));
- $this->assertNull($admin->getParentFieldDescription());
- $admin->setParentFieldDescription($parentFieldDescription);
- $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
- $this->assertSame($parentAdmin, $admin->getRoot());
- }
- public function testGetExportFields()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->once())
- ->method('getExportFields')
- ->with($this->equalTo('NewsBundle\Entity\Post'))
- ->will($this->returnValue(array('foo', 'bar')));
- $admin->setModelManager($modelManager);
- $this->assertSame(array('foo', 'bar'), $admin->getExportFields());
- }
- public function testGetPersistentParametersWithNoExtension()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertEmpty($admin->getPersistentParameters());
- }
- /**
- * @expectedException \RuntimeException
- */
- public function testGetPersistentParametersWithInvalidExtension()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $extension = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
- $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(null));
- $admin->addExtension($extension);
- $admin->getPersistentParameters();
- }
- public function testGetPersistentParametersWithValidExtension()
- {
- $expected = array(
- 'context' => 'foobar',
- );
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $extension = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
- $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue($expected));
- $admin->addExtension($extension);
- $this->assertSame($expected, $admin->getPersistentParameters());
- }
- public function testGetFormWithNonCollectionParentValue()
- {
- $post = new Post();
- $tagAdmin = $this->createTagAdmin($post);
- $tag = $tagAdmin->getSubject();
- $tag->setPosts(null);
- $tagAdmin->getForm();
- $this->assertSame($post, $tag->getPosts());
- }
- public function testGetFormWithCollectionParentValue()
- {
- $post = new Post();
- $tagAdmin = $this->createTagAdmin($post);
- $tag = $tagAdmin->getSubject();
- // Case of a doctrine collection
- $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
- $this->assertCount(0, $tag->getPosts());
- $tag->addPost(new Post());
- $this->assertCount(1, $tag->getPosts());
- $tagAdmin->getForm();
- $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
- $this->assertCount(2, $tag->getPosts());
- $this->assertContains($post, $tag->getPosts());
- // Case of an array
- $tag->setPosts(array());
- $this->assertCount(0, $tag->getPosts());
- $tag->addPost(new Post());
- $this->assertCount(1, $tag->getPosts());
- $tagAdmin->getForm();
- $this->assertInternalType('array', $tag->getPosts());
- $this->assertCount(2, $tag->getPosts());
- $this->assertContains($post, $tag->getPosts());
- }
- public function testRemoveFieldFromFormGroup()
- {
- $formGroups = array(
- 'foobar' => array(
- 'fields' => array(
- 'foo' => 'foo',
- 'bar' => 'bar',
- ),
- ),
- );
- $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setFormGroups($formGroups);
- $admin->removeFieldFromFormGroup('foo');
- $this->assertSame($admin->getFormGroups(), array(
- 'foobar' => array(
- 'fields' => array(
- 'bar' => 'bar',
- ),
- ),
- ));
- $admin->removeFieldFromFormGroup('bar');
- $this->assertSame($admin->getFormGroups(), array());
- }
- public function testGetFilterParameters()
- {
- $authorId = uniqid();
- $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
- $commentAdmin->setParentAssociationMapping('post.author');
- $commentAdmin->setParent($postAdmin);
- $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array('get'));
- $request->expects($this->any())
- ->method('get')
- ->will($this->returnValue($authorId));
- $commentAdmin->setRequest($request);
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->any())
- ->method('getDefaultSortValues')
- ->will($this->returnValue(array()));
- $commentAdmin->setModelManager($modelManager);
- $parameters = $commentAdmin->getFilterParameters();
- $this->assertTrue(isset($parameters['post__author']));
- $this->assertSame(array('value' => $authorId), $parameters['post__author']);
- }
- public function testGetFilterFieldDescription()
- {
- $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
- $fooFieldDescription = new FieldDescription();
- $barFieldDescription = new FieldDescription();
- $bazFieldDescription = new FieldDescription();
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->exactly(3))
- ->method('getNewFieldDescriptionInstance')
- ->will($this->returnCallback(function ($adminClass, $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) {
- switch ($name) {
- case 'foo':
- $fieldDescription = $fooFieldDescription;
- break;
- case 'bar':
- $fieldDescription = $barFieldDescription;
- break;
- case 'baz':
- $fieldDescription = $bazFieldDescription;
- break;
- default:
- throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
- break;
- }
- $fieldDescription->setName($name);
- return $fieldDescription;
- }));
- $modelAdmin->setModelManager($modelManager);
- $pager = $this->getMock('Sonata\AdminBundle\Datagrid\PagerInterface');
- $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
- $datagrid->expects($this->once())
- ->method('getPager')
- ->will($this->returnValue($pager));
- $datagridBuilder = $this->getMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
- $datagridBuilder->expects($this->once())
- ->method('getBaseDatagrid')
- ->with($this->identicalTo($modelAdmin), array())
- ->will($this->returnValue($datagrid));
- $datagridBuilder->expects($this->exactly(3))
- ->method('addFilter')
- ->will($this->returnCallback(function ($datagrid, $type, $fieldDescription, AdminInterface $admin) {
- $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
- $fieldDescription->mergeOption('field_options', array('required' => false));
- }));
- $modelAdmin->setDatagridBuilder($datagridBuilder);
- $this->assertSame(array('foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription), $modelAdmin->getFilterFieldDescriptions());
- $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
- $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
- $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
- $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
- $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
- $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
- $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
- }
- public function testGetSubjectNoRequest()
- {
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager
- ->expects($this->never())
- ->method('find');
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setModelManager($modelManager);
- $this->assertNull($admin->getSubject());
- }
- public function testGetSideMenu()
- {
- $item = $this->getMock('Knp\Menu\ItemInterface');
- $item
- ->expects($this->once())
- ->method('setChildrenAttribute')
- ->with('class', 'nav navbar-nav');
- $item
- ->expects($this->once())
- ->method('setExtra')
- ->with('translation_domain', 'foo_bar_baz');
- $menuFactory = $this->getMock('Knp\Menu\FactoryInterface');
- $menuFactory
- ->expects($this->once())
- ->method('createItem')
- ->will($this->returnValue($item));
- $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
- $modelAdmin->setMenuFactory($menuFactory);
- $modelAdmin->setTranslationDomain('foo_bar_baz');
- $modelAdmin->getSideMenu('foo');
- }
- /**
- * @return array
- */
- public function provideGetSubject()
- {
- return array(
- array(23),
- array('azerty'),
- array('4f69bbb5f14a13347f000092'),
- array('0779ca8d-e2be-11e4-ac58-0242ac11000b'),
- array('123'.AdapterInterface::ID_SEPARATOR.'my_type'), // composite keys are supported
- );
- }
- /**
- * @dataProvider provideGetSubject
- */
- public function testGetSubjectFailed($id)
- {
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager
- ->expects($this->once())
- ->method('find')
- ->with('NewsBundle\Entity\Post', $id)
- ->will($this->returnValue(null)); // entity not found
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setModelManager($modelManager);
- $admin->setRequest(new Request(array('id' => $id)));
- $this->assertNull($admin->getSubject());
- }
- /**
- * @dataProvider provideGetSubject
- */
- public function testGetSubject($id)
- {
- $entity = new Post();
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager
- ->expects($this->once())
- ->method('find')
- ->with('NewsBundle\Entity\Post', $id)
- ->will($this->returnValue($entity));
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $admin->setModelManager($modelManager);
- $admin->setRequest(new Request(array('id' => $id)));
- $this->assertSame($entity, $admin->getSubject());
- $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
- */
- public function testGetActionButtonsList()
- {
- $expected = array(
- 'create' => array(
- 'template' => 'SonataAdminBundle:Button:create_button.html.twig',
- ),
- );
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $this->assertSame($expected, $admin->getActionButtons('list', null));
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
- */
- public function getBatchActions()
- {
- $expected = array(
- 'action' => array(
- 'label' => 'action_delete',
- 'translation_domain' => 'SonataAdminBundle',
- 'ask_confirmation' => true, // by default always true
- ),
- 'foo' => array(
- 'label' => 'action_foo',
- ),
- );
- $modelAdmin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
- $this->assertSame($expected, $modelAdmin->getBatchActions());
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
- */
- public function testShowMosaicButton()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $listModes = $admin->getListModes();
- $admin->showMosaicButton(true);
- $this->assertSame($listModes, $admin->getListModes());
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
- */
- public function testShowMosaicButtonHideMosaic()
- {
- $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
- $listModes = $admin->getListModes();
- $expected['list'] = $listModes['list'];
- $admin->showMosaicButton(false);
- $this->assertSame($expected, $admin->getListModes());
- }
- /**
- * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
- * @dataProvider provideGetBaseRouteName
- */
- public function testDefaultDashboardActionsArePresent($objFqn, $expected)
- {
- $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
- $routeGenerator = new DefaultRouteGenerator(
- $this->getMock('Symfony\Component\Routing\RouterInterface'),
- new RoutesCache($this->cacheTempFolder, true)
- );
- $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
- $admin->setRouteBuilder($pathInfo);
- $admin->setRouteGenerator($routeGenerator);
- $admin->initialize();
- $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
- $securityHandler->expects($this->any())
- ->method('isGranted')
- ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
- if ($admin == $adminIn && ($attributes == 'CREATE' || $attributes == 'LIST')) {
- return true;
- }
- return false;
- }));
- $admin->setSecurityHandler($securityHandler);
- $this->assertArrayHasKey('list', $admin->getDashboardActions());
- $this->assertArrayHasKey('create', $admin->getDashboardActions());
- }
- /**
- * @group legacy
- */
- public function testDefaultBreadcrumbsBuilder()
- {
- $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
- 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
- ));
- $this->assertInstanceOf(
- 'Sonata\AdminBundle\Admin\BreadcrumbsBuilder',
- $admin->getBreadcrumbsBuilder()
- );
- }
- /**
- * @group legacy
- */
- public function testBreadcrumbsBuilderSetter()
- {
- $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
- 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
- ));
- $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->getMock(
- 'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
- )));
- $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
- }
- /**
- * @group legacy
- */
- public function testGetBreadcrumbs()
- {
- $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
- 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
- ));
- $builder = $this->prophesize(
- 'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
- );
- $action = 'myaction';
- $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
- $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
- }
- /**
- * @group legacy
- */
- public function testBuildBreadcrumbs()
- {
- $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
- 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
- ));
- $builder = $this->prophesize(
- 'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
- );
- $action = 'myaction';
- $menu = $this->getMock('Knp\Menu\ItemInterface');
- $builder->buildBreadcrumbs($admin, $action, $menu)
- ->shouldBeCalledTimes(1)
- ->willReturn($menu);
- $admin->setBreadcrumbsBuilder($builder->reveal());
- /* check the called is proxied only once */
- $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
- $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
- }
- /**
- * NEXT_MAJOR: remove this method.
- *
- * @group legacy
- */
- public function testCreateQueryLegacyCallWorks()
- {
- $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
- 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
- ));
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $modelManager->expects($this->once())
- ->method('createQuery')
- ->with('My\Class')
- ->willReturn('a query');
- $admin->setModelManager($modelManager);
- $this->assertSame('a query', $admin->createQuery('list'));
- }
- private function createTagAdmin(Post $post)
- {
- $postAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin')
- ->disableOriginalConstructor()
- ->getMock();
- $postAdmin->expects($this->any())->method('getObject')->will($this->returnValue($post));
- $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
- $formBuilder->expects($this->any())->method('getForm')->will($this->returnValue(null));
- $tagAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\TagAdmin')
- ->setConstructorArgs(array(
- 'admin.tag',
- 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag',
- 'MyBundle:MyController',
- ))
- ->setMethods(array('getFormBuilder'))
- ->getMock();
- $tagAdmin->expects($this->any())->method('getFormBuilder')->will($this->returnValue($formBuilder));
- $tagAdmin->setParent($postAdmin);
- $tag = new Tag();
- $tagAdmin->setSubject($tag);
- $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
- $tagAdmin->setRequest($request);
- $configurationPool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
- ->disableOriginalConstructor()
- ->getMock();
- $configurationPool->expects($this->any())->method('getPropertyAccessor')->will($this->returnValue(PropertyAccess::createPropertyAccessor()));
- $tagAdmin->setConfigurationPool($configurationPool);
- return $tagAdmin;
- }
- }
|