AdminTest.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Admin;
  11. use Sonata\AdminBundle\Admin\AbstractAdmin;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\Pool;
  14. use Sonata\AdminBundle\Route\DefaultRouteGenerator;
  15. use Sonata\AdminBundle\Route\RoutesCache;
  16. use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
  17. use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentWithCustomRouteAdmin;
  18. use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
  19. use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
  20. use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
  21. use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin;
  22. use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
  23. use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
  24. use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
  25. use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToStringNull;
  26. use Sonata\CoreBundle\Model\Adapter\AdapterInterface;
  27. use Symfony\Component\DependencyInjection\Container;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\PropertyAccess\PropertyAccess;
  30. class AdminTest extends \PHPUnit_Framework_TestCase
  31. {
  32. protected $cacheTempFolder;
  33. public function setUp()
  34. {
  35. $this->cacheTempFolder = sys_get_temp_dir().'/sonata_test_route';
  36. exec('rm -rf '.$this->cacheTempFolder);
  37. }
  38. /**
  39. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::__construct
  40. */
  41. public function testConstructor()
  42. {
  43. $class = 'Application\Sonata\NewsBundle\Entity\Post';
  44. $baseControllerName = 'SonataNewsBundle:PostAdmin';
  45. $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
  46. $this->assertInstanceOf('Sonata\AdminBundle\Admin\AbstractAdmin', $admin);
  47. $this->assertSame($class, $admin->getClass());
  48. $this->assertSame($baseControllerName, $admin->getBaseControllerName());
  49. }
  50. public function testGetClass()
  51. {
  52. $class = 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post';
  53. $baseControllerName = 'SonataNewsBundle:PostAdmin';
  54. $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
  55. $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
  56. $this->assertSame(
  57. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
  58. $admin->getClass()
  59. );
  60. $admin->setSubClasses(array('foo'));
  61. $this->assertSame(
  62. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
  63. $admin->getClass()
  64. );
  65. $admin->setSubject(null);
  66. $admin->setSubClasses(array());
  67. $this->assertSame($class, $admin->getClass());
  68. $admin->setSubClasses(array('foo' => 'bar'));
  69. $admin->setRequest(new Request(array('subclass' => 'foo')));
  70. $this->assertSame('bar', $admin->getClass());
  71. }
  72. /**
  73. * @expectedException \RuntimeException
  74. * @expectedExceptionMessage Feature not implemented: an embedded admin cannot have subclass
  75. */
  76. public function testGetClassException()
  77. {
  78. $class = 'Application\Sonata\NewsBundle\Entity\Post';
  79. $baseControllerName = 'SonataNewsBundle:PostAdmin';
  80. $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
  81. $admin->setParentFieldDescription(new FieldDescription());
  82. $admin->setSubClasses(array('foo' => 'bar'));
  83. $admin->setRequest(new Request(array('subclass' => 'foo')));
  84. $admin->getClass();
  85. }
  86. public function testCheckAccessThrowsExceptionOnMadeUpAction()
  87. {
  88. $admin = new PostAdmin(
  89. 'sonata.post.admin.post',
  90. 'Application\Sonata\NewsBundle\Entity\Post',
  91. 'SonataNewsBundle:PostAdmin'
  92. );
  93. $this->setExpectedException(
  94. '\InvalidArgumentException',
  95. 'Action "made-up" could not be found'
  96. );
  97. $admin->checkAccess('made-up');
  98. }
  99. public function testCheckAccessThrowsAccessDeniedException()
  100. {
  101. $admin = new PostAdmin(
  102. 'sonata.post.admin.post',
  103. 'Application\Sonata\NewsBundle\Entity\Post',
  104. 'SonataNewsBundle:PostAdmin'
  105. );
  106. $securityHandler = $this->prophesize(
  107. 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
  108. );
  109. $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
  110. $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
  111. $customExtension = $this->prophesize(
  112. 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
  113. );
  114. $customExtension->getAccessMapping($admin)->willReturn(
  115. array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
  116. );
  117. $admin->addExtension($customExtension->reveal());
  118. $admin->setSecurityHandler($securityHandler->reveal());
  119. $this->setExpectedException(
  120. 'Symfony\Component\Security\Core\Exception\AccessDeniedException',
  121. 'Access Denied to the action custom_action and role EXTRA_CUSTOM_ROLE'
  122. );
  123. $admin->checkAccess('custom_action');
  124. }
  125. public function testHasAccessOnMadeUpAction()
  126. {
  127. $admin = new PostAdmin(
  128. 'sonata.post.admin.post',
  129. 'Application\Sonata\NewsBundle\Entity\Post',
  130. 'SonataNewsBundle:PostAdmin'
  131. );
  132. $this->assertFalse($admin->hasAccess('made-up'));
  133. }
  134. public function testHasAccess()
  135. {
  136. $admin = new PostAdmin(
  137. 'sonata.post.admin.post',
  138. 'Application\Sonata\NewsBundle\Entity\Post',
  139. 'SonataNewsBundle:PostAdmin'
  140. );
  141. $securityHandler = $this->prophesize(
  142. 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
  143. );
  144. $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
  145. $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(false);
  146. $customExtension = $this->prophesize(
  147. 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
  148. );
  149. $customExtension->getAccessMapping($admin)->willReturn(
  150. array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
  151. );
  152. $admin->addExtension($customExtension->reveal());
  153. $admin->setSecurityHandler($securityHandler->reveal());
  154. $this->assertFalse($admin->hasAccess('custom_action'));
  155. }
  156. public function testHasAccessAllowsAccess()
  157. {
  158. $admin = new PostAdmin(
  159. 'sonata.post.admin.post',
  160. 'Application\Sonata\NewsBundle\Entity\Post',
  161. 'SonataNewsBundle:PostAdmin'
  162. );
  163. $securityHandler = $this->prophesize(
  164. 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
  165. );
  166. $securityHandler->isGranted($admin, 'CUSTOM_ROLE', $admin)->willReturn(true);
  167. $securityHandler->isGranted($admin, 'EXTRA_CUSTOM_ROLE', $admin)->willReturn(true);
  168. $customExtension = $this->prophesize(
  169. 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
  170. );
  171. $customExtension->getAccessMapping($admin)->willReturn(
  172. array('custom_action' => array('CUSTOM_ROLE', 'EXTRA_CUSTOM_ROLE'))
  173. );
  174. $admin->addExtension($customExtension->reveal());
  175. $admin->setSecurityHandler($securityHandler->reveal());
  176. $this->assertTrue($admin->hasAccess('custom_action'));
  177. }
  178. public function testHasAccessAllowsAccessEditAction()
  179. {
  180. $admin = new PostAdmin(
  181. 'sonata.post.admin.post',
  182. 'Application\Sonata\NewsBundle\Entity\Post',
  183. 'SonataNewsBundle:PostAdmin'
  184. );
  185. $securityHandler = $this->prophesize(
  186. 'Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface'
  187. );
  188. $securityHandler->isGranted($admin, 'EDIT_ROLE', $admin)->willReturn(true);
  189. $customExtension = $this->prophesize(
  190. 'Sonata\AdminBundle\Admin\AbstractAdminExtension'
  191. );
  192. $customExtension->getAccessMapping($admin)->willReturn(
  193. array('edit_action' => array('EDIT_ROLE'))
  194. );
  195. $admin->addExtension($customExtension->reveal());
  196. $admin->setSecurityHandler($securityHandler->reveal());
  197. $this->assertTrue($admin->hasAccess('edit_action'));
  198. }
  199. /**
  200. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasChild
  201. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::addChild
  202. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getChild
  203. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::isChild
  204. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasChildren
  205. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getChildren
  206. */
  207. public function testChildren()
  208. {
  209. $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  210. $this->assertFalse($postAdmin->hasChildren());
  211. $this->assertFalse($postAdmin->hasChild('comment'));
  212. $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
  213. $postAdmin->addChild($commentAdmin);
  214. $this->assertTrue($postAdmin->hasChildren());
  215. $this->assertTrue($postAdmin->hasChild('sonata.post.admin.comment'));
  216. $this->assertSame('sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getCode());
  217. $this->assertSame('sonata.post.admin.post|sonata.post.admin.comment', $postAdmin->getChild('sonata.post.admin.comment')->getBaseCodeRoute());
  218. $this->assertSame($postAdmin, $postAdmin->getChild('sonata.post.admin.comment')->getParent());
  219. $this->assertFalse($postAdmin->isChild());
  220. $this->assertTrue($commentAdmin->isChild());
  221. $this->assertSame(array('sonata.post.admin.comment' => $commentAdmin), $postAdmin->getChildren());
  222. }
  223. /**
  224. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configure
  225. */
  226. public function testConfigure()
  227. {
  228. $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  229. $this->assertNotNull($admin->getUniqid());
  230. $admin->initialize();
  231. $this->assertNotNull($admin->getUniqid());
  232. $this->assertSame('Post', $admin->getClassnameLabel());
  233. $admin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
  234. $admin->setClassnameLabel('postcomment');
  235. $admin->initialize();
  236. $this->assertSame('postcomment', $admin->getClassnameLabel());
  237. }
  238. public function testConfigureWithValidParentAssociationMapping()
  239. {
  240. $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  241. $admin->setParentAssociationMapping('Category');
  242. $admin->initialize();
  243. $this->assertSame('Category', $admin->getParentAssociationMapping());
  244. }
  245. public function provideGetBaseRoutePattern()
  246. {
  247. return array(
  248. array(
  249. 'Application\Sonata\NewsBundle\Entity\Post',
  250. '/sonata/news/post',
  251. ),
  252. array(
  253. 'Application\Sonata\NewsBundle\Document\Post',
  254. '/sonata/news/post',
  255. ),
  256. array(
  257. 'MyApplication\MyBundle\Entity\Post',
  258. '/myapplication/my/post',
  259. ),
  260. array(
  261. 'MyApplication\MyBundle\Entity\Post\Category',
  262. '/myapplication/my/post-category',
  263. ),
  264. array(
  265. 'MyApplication\MyBundle\Entity\Product\Category',
  266. '/myapplication/my/product-category',
  267. ),
  268. array(
  269. 'MyApplication\MyBundle\Entity\Other\Product\Category',
  270. '/myapplication/my/other-product-category',
  271. ),
  272. array(
  273. 'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
  274. '/cmf/foo/menu',
  275. ),
  276. array(
  277. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
  278. '/cmf/foo/menu',
  279. ),
  280. array(
  281. 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
  282. '/symfony/barbar/menu',
  283. ),
  284. array(
  285. 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
  286. '/symfony/barbar/menu-item',
  287. ),
  288. array(
  289. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
  290. '/cmf/foo/menu',
  291. ),
  292. array(
  293. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
  294. '/cmf/foo/menu',
  295. ),
  296. array(
  297. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
  298. '/cmf/foo/menu',
  299. ),
  300. array(
  301. 'AppBundle\Entity\User',
  302. '/app/user',
  303. ),
  304. array(
  305. 'App\Entity\User',
  306. '/app/user',
  307. ),
  308. );
  309. }
  310. /**
  311. * @dataProvider provideGetBaseRoutePattern
  312. */
  313. public function testGetBaseRoutePattern($objFqn, $expected)
  314. {
  315. $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
  316. $this->assertSame($expected, $admin->getBaseRoutePattern());
  317. }
  318. /**
  319. * @dataProvider provideGetBaseRoutePattern
  320. */
  321. public function testGetBaseRoutePatternWithChildAdmin($objFqn, $expected)
  322. {
  323. $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
  324. $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
  325. $commentAdmin->setParent($postAdmin);
  326. $this->assertSame($expected.'/{id}/comment', $commentAdmin->getBaseRoutePattern());
  327. }
  328. public function testGetBaseRoutePatternWithSpecifedPattern()
  329. {
  330. $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostWithCustomRouteAdmin');
  331. $this->assertSame('/post-custom', $postAdmin->getBaseRoutePattern());
  332. }
  333. public function testGetBaseRoutePatternWithChildAdminAndWithSpecifedPattern()
  334. {
  335. $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  336. $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
  337. $commentAdmin->setParent($postAdmin);
  338. $this->assertSame('/sonata/news/post/{id}/comment-custom', $commentAdmin->getBaseRoutePattern());
  339. }
  340. /**
  341. * @expectedException RuntimeException
  342. */
  343. public function testGetBaseRoutePatternWithUnreconizedClassname()
  344. {
  345. $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
  346. $admin->getBaseRoutePattern();
  347. }
  348. public function provideGetBaseRouteName()
  349. {
  350. return array(
  351. array(
  352. 'Application\Sonata\NewsBundle\Entity\Post',
  353. 'admin_sonata_news_post',
  354. ),
  355. array(
  356. 'Application\Sonata\NewsBundle\Document\Post',
  357. 'admin_sonata_news_post',
  358. ),
  359. array(
  360. 'MyApplication\MyBundle\Entity\Post',
  361. 'admin_myapplication_my_post',
  362. ),
  363. array(
  364. 'MyApplication\MyBundle\Entity\Post\Category',
  365. 'admin_myapplication_my_post_category',
  366. ),
  367. array(
  368. 'MyApplication\MyBundle\Entity\Product\Category',
  369. 'admin_myapplication_my_product_category',
  370. ),
  371. array(
  372. 'MyApplication\MyBundle\Entity\Other\Product\Category',
  373. 'admin_myapplication_my_other_product_category',
  374. ),
  375. array(
  376. 'Symfony\Cmf\Bundle\FooBundle\Document\Menu',
  377. 'admin_cmf_foo_menu',
  378. ),
  379. array(
  380. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu',
  381. 'admin_cmf_foo_menu',
  382. ),
  383. array(
  384. 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu',
  385. 'admin_symfony_barbar_menu',
  386. ),
  387. array(
  388. 'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu\Item',
  389. 'admin_symfony_barbar_menu_item',
  390. ),
  391. array(
  392. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\Orm\Menu',
  393. 'admin_cmf_foo_menu',
  394. ),
  395. array(
  396. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\MongoDB\Menu',
  397. 'admin_cmf_foo_menu',
  398. ),
  399. array(
  400. 'Symfony\Cmf\Bundle\FooBundle\Doctrine\CouchDB\Menu',
  401. 'admin_cmf_foo_menu',
  402. ),
  403. array(
  404. 'AppBundle\Entity\User',
  405. 'admin_app_user',
  406. ),
  407. array(
  408. 'App\Entity\User',
  409. 'admin_app_user',
  410. ),
  411. );
  412. }
  413. /**
  414. * @dataProvider provideGetBaseRouteName
  415. */
  416. public function testGetBaseRouteName($objFqn, $expected)
  417. {
  418. $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
  419. $this->assertSame($expected, $admin->getBaseRouteName());
  420. }
  421. /**
  422. * @dataProvider provideGetBaseRouteName
  423. */
  424. public function testGetBaseRouteNameWithChildAdmin($objFqn, $expected)
  425. {
  426. $routeGenerator = new DefaultRouteGenerator(
  427. $this->getMock('Symfony\Component\Routing\RouterInterface'),
  428. new RoutesCache($this->cacheTempFolder, true)
  429. );
  430. $container = new Container();
  431. $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
  432. $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
  433. $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
  434. $container->set('sonata.post.admin.post', $postAdmin);
  435. $postAdmin->setConfigurationPool($pool);
  436. $postAdmin->setRouteBuilder($pathInfo);
  437. $postAdmin->setRouteGenerator($routeGenerator);
  438. $postAdmin->initialize();
  439. $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
  440. $container->set('sonata.post.admin.comment', $commentAdmin);
  441. $commentAdmin->setConfigurationPool($pool);
  442. $commentAdmin->setRouteBuilder($pathInfo);
  443. $commentAdmin->setRouteGenerator($routeGenerator);
  444. $commentAdmin->initialize();
  445. $postAdmin->addChild($commentAdmin);
  446. $pool->setAdminServiceIds(array('sonata.post.admin.post', 'sonata.post.admin.comment'));
  447. $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
  448. $this->assertTrue($postAdmin->hasRoute('show'));
  449. $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post.show'));
  450. $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.show'));
  451. $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
  452. $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
  453. $this->assertFalse($commentAdmin->hasRoute('edit'));
  454. /*
  455. * Test the route name from request
  456. */
  457. $postListRequest = new Request(
  458. array(),
  459. array(),
  460. array(
  461. '_route' => $postAdmin->getBaseRouteName().'_list',
  462. )
  463. );
  464. $postAdmin->setRequest($postListRequest);
  465. $commentAdmin->setRequest($postListRequest);
  466. $this->assertTrue($postAdmin->isCurrentRoute('list'));
  467. $this->assertFalse($postAdmin->isCurrentRoute('create'));
  468. $this->assertFalse($commentAdmin->isCurrentRoute('list'));
  469. $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
  470. $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
  471. }
  472. /**
  473. * @expectedException \RuntimeException
  474. */
  475. public function testGetBaseRouteNameWithUnreconizedClassname()
  476. {
  477. $admin = new PostAdmin('sonata.post.admin.post', 'News\Thing\Post', 'SonataNewsBundle:PostAdmin');
  478. $admin->getBaseRouteName();
  479. }
  480. public function testGetBaseRouteNameWithSpecifiedName()
  481. {
  482. $postAdmin = new PostWithCustomRouteAdmin('sonata.post.admin.post_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  483. $this->assertSame('post_custom', $postAdmin->getBaseRouteName());
  484. }
  485. public function testGetBaseRouteNameWithChildAdminAndWithSpecifiedName()
  486. {
  487. $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  488. $commentAdmin = new CommentWithCustomRouteAdmin('sonata.post.admin.comment_with_custom_route', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentWithCustomRouteAdmin');
  489. $commentAdmin->setParent($postAdmin);
  490. $this->assertSame('admin_sonata_news_post_comment_custom', $commentAdmin->getBaseRouteName());
  491. }
  492. /**
  493. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::setUniqid
  494. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getUniqid
  495. */
  496. public function testUniqid()
  497. {
  498. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  499. $uniqid = uniqid();
  500. $admin->setUniqid($uniqid);
  501. $this->assertSame($uniqid, $admin->getUniqid());
  502. }
  503. public function testToString()
  504. {
  505. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  506. $s = new \stdClass();
  507. $this->assertNotEmpty($admin->toString($s));
  508. $s = new FooToString();
  509. $this->assertSame('salut', $admin->toString($s));
  510. // To string method is implemented, but returns null
  511. $s = new FooToStringNull();
  512. $this->assertNotEmpty($admin->toString($s));
  513. $this->assertSame('', $admin->toString(false));
  514. }
  515. public function testIsAclEnabled()
  516. {
  517. $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  518. $this->assertFalse($postAdmin->isAclEnabled());
  519. $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
  520. $commentAdmin->setSecurityHandler($this->getMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface'));
  521. $this->assertTrue($commentAdmin->isAclEnabled());
  522. }
  523. /**
  524. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getSubClasses
  525. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getSubClass
  526. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::setSubClasses
  527. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasSubClass
  528. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
  529. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubClass
  530. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getActiveSubclassCode
  531. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getClass
  532. */
  533. public function testSubClass()
  534. {
  535. $admin = new PostAdmin(
  536. 'sonata.post.admin.post',
  537. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
  538. 'SonataNewsBundle:PostAdmin'
  539. );
  540. $this->assertFalse($admin->hasSubClass('test'));
  541. $this->assertFalse($admin->hasActiveSubClass());
  542. $this->assertCount(0, $admin->getSubClasses());
  543. $this->assertNull($admin->getActiveSubClass());
  544. $this->assertNull($admin->getActiveSubclassCode());
  545. $this->assertSame(
  546. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post',
  547. $admin->getClass()
  548. );
  549. // Just for the record, if there is no inheritance set, the getSubject is not used
  550. // the getSubject can also lead to some issue
  551. $admin->setSubject(new \Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost());
  552. $this->assertSame(
  553. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
  554. $admin->getClass()
  555. );
  556. $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
  557. $this->assertFalse($admin->hasSubClass('test'));
  558. $this->assertTrue($admin->hasSubClass('extended1'));
  559. $this->assertFalse($admin->hasActiveSubClass());
  560. $this->assertCount(2, $admin->getSubClasses());
  561. $this->assertNull($admin->getActiveSubClass());
  562. $this->assertNull($admin->getActiveSubclassCode());
  563. $this->assertSame(
  564. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
  565. $admin->getClass()
  566. );
  567. $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
  568. $admin->setRequest($request);
  569. $this->assertFalse($admin->hasSubClass('test'));
  570. $this->assertTrue($admin->hasSubClass('extended1'));
  571. $this->assertTrue($admin->hasActiveSubClass());
  572. $this->assertCount(2, $admin->getSubClasses());
  573. $this->assertSame(
  574. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
  575. $admin->getActiveSubClass()
  576. );
  577. $this->assertSame('extended1', $admin->getActiveSubclassCode());
  578. $this->assertSame(
  579. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost',
  580. $admin->getClass()
  581. );
  582. $request->query->set('subclass', 'inject');
  583. $this->assertNull($admin->getActiveSubclassCode());
  584. }
  585. /**
  586. * @expectedException \RuntimeException
  587. */
  588. public function testNonExistantSubclass()
  589. {
  590. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  591. $admin->setRequest(new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'inject')));
  592. $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1', 'extended2' => 'NewsBundle\Entity\PostExtended2'));
  593. $this->assertTrue($admin->hasActiveSubClass());
  594. $admin->getActiveSubClass();
  595. }
  596. /**
  597. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::hasActiveSubClass
  598. */
  599. public function testOnlyOneSubclassNeededToBeActive()
  600. {
  601. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  602. $admin->setSubClasses(array('extended1' => 'NewsBundle\Entity\PostExtended1'));
  603. $request = new \Symfony\Component\HttpFoundation\Request(array('subclass' => 'extended1'));
  604. $admin->setRequest($request);
  605. $this->assertTrue($admin->hasActiveSubClass());
  606. }
  607. public function testGetPerPageOptions()
  608. {
  609. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  610. $this->assertSame(array(16, 32, 64, 128, 192), $admin->getPerPageOptions());
  611. $admin->setPerPageOptions(array(500, 1000));
  612. $this->assertSame(array(500, 1000), $admin->getPerPageOptions());
  613. }
  614. public function testGetLabelTranslatorStrategy()
  615. {
  616. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  617. $this->assertNull($admin->getLabelTranslatorStrategy());
  618. $labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
  619. $admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
  620. $this->assertSame($labelTranslatorStrategy, $admin->getLabelTranslatorStrategy());
  621. }
  622. public function testGetRouteBuilder()
  623. {
  624. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  625. $this->assertNull($admin->getRouteBuilder());
  626. $routeBuilder = $this->getMock('Sonata\AdminBundle\Builder\RouteBuilderInterface');
  627. $admin->setRouteBuilder($routeBuilder);
  628. $this->assertSame($routeBuilder, $admin->getRouteBuilder());
  629. }
  630. public function testGetMenuFactory()
  631. {
  632. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  633. $this->assertNull($admin->getMenuFactory());
  634. $menuFactory = $this->getMock('Knp\Menu\FactoryInterface');
  635. $admin->setMenuFactory($menuFactory);
  636. $this->assertSame($menuFactory, $admin->getMenuFactory());
  637. }
  638. public function testGetExtensions()
  639. {
  640. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  641. $this->assertSame(array(), $admin->getExtensions());
  642. $adminExtension1 = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
  643. $adminExtension2 = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
  644. $admin->addExtension($adminExtension1);
  645. $admin->addExtension($adminExtension2);
  646. $this->assertSame(array($adminExtension1, $adminExtension2), $admin->getExtensions());
  647. }
  648. public function testGetFilterTheme()
  649. {
  650. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  651. $this->assertSame(array(), $admin->getFilterTheme());
  652. $admin->setFilterTheme(array('FooTheme'));
  653. $this->assertSame(array('FooTheme'), $admin->getFilterTheme());
  654. }
  655. public function testGetFormTheme()
  656. {
  657. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  658. $this->assertSame(array(), $admin->getFormTheme());
  659. $admin->setFormTheme(array('FooTheme'));
  660. $this->assertSame(array('FooTheme'), $admin->getFormTheme());
  661. }
  662. public function testGetValidator()
  663. {
  664. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  665. $this->assertNull($admin->getValidator());
  666. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  667. $admin->setValidator($validator);
  668. $this->assertSame($validator, $admin->getValidator());
  669. }
  670. public function testGetSecurityHandler()
  671. {
  672. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  673. $this->assertNull($admin->getSecurityHandler());
  674. $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
  675. $admin->setSecurityHandler($securityHandler);
  676. $this->assertSame($securityHandler, $admin->getSecurityHandler());
  677. }
  678. public function testGetSecurityInformation()
  679. {
  680. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  681. $this->assertSame(array(), $admin->getSecurityInformation());
  682. $securityInformation = array('ROLE_FOO', 'ROLE_BAR');
  683. $admin->setSecurityInformation($securityInformation);
  684. $this->assertSame($securityInformation, $admin->getSecurityInformation());
  685. }
  686. public function testGetManagerType()
  687. {
  688. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  689. $this->assertNull($admin->getManagerType());
  690. $admin->setManagerType('foo_orm');
  691. $this->assertSame('foo_orm', $admin->getManagerType());
  692. }
  693. public function testGetModelManager()
  694. {
  695. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  696. $this->assertNull($admin->getModelManager());
  697. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  698. $admin->setModelManager($modelManager);
  699. $this->assertSame($modelManager, $admin->getModelManager());
  700. }
  701. public function testGetBaseCodeRoute()
  702. {
  703. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  704. $this->assertSame('', $admin->getBaseCodeRoute());
  705. $admin->setBaseCodeRoute('foo');
  706. $this->assertSame('foo', $admin->getBaseCodeRoute());
  707. }
  708. public function testGetRouteGenerator()
  709. {
  710. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  711. $this->assertNull($admin->getRouteGenerator());
  712. $routeGenerator = $this->getMock('Sonata\AdminBundle\Route\RouteGeneratorInterface');
  713. $admin->setRouteGenerator($routeGenerator);
  714. $this->assertSame($routeGenerator, $admin->getRouteGenerator());
  715. }
  716. public function testGetConfigurationPool()
  717. {
  718. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  719. $this->assertNull($admin->getConfigurationPool());
  720. $pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
  721. ->disableOriginalConstructor()
  722. ->getMock();
  723. $admin->setConfigurationPool($pool);
  724. $this->assertSame($pool, $admin->getConfigurationPool());
  725. }
  726. public function testGetShowBuilder()
  727. {
  728. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  729. $this->assertNull($admin->getShowBuilder());
  730. $showBuilder = $this->getMock('Sonata\AdminBundle\Builder\ShowBuilderInterface');
  731. $admin->setShowBuilder($showBuilder);
  732. $this->assertSame($showBuilder, $admin->getShowBuilder());
  733. }
  734. public function testGetListBuilder()
  735. {
  736. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  737. $this->assertNull($admin->getListBuilder());
  738. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  739. $admin->setListBuilder($listBuilder);
  740. $this->assertSame($listBuilder, $admin->getListBuilder());
  741. }
  742. public function testGetDatagridBuilder()
  743. {
  744. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  745. $this->assertNull($admin->getDatagridBuilder());
  746. $datagridBuilder = $this->getMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  747. $admin->setDatagridBuilder($datagridBuilder);
  748. $this->assertSame($datagridBuilder, $admin->getDatagridBuilder());
  749. }
  750. public function testGetFormContractor()
  751. {
  752. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  753. $this->assertNull($admin->getFormContractor());
  754. $formContractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
  755. $admin->setFormContractor($formContractor);
  756. $this->assertSame($formContractor, $admin->getFormContractor());
  757. }
  758. public function testGetRequest()
  759. {
  760. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  761. $this->assertFalse($admin->hasRequest());
  762. $request = new Request();
  763. $admin->setRequest($request);
  764. $this->assertSame($request, $admin->getRequest());
  765. $this->assertTrue($admin->hasRequest());
  766. }
  767. public function testGetRequestWithException()
  768. {
  769. $this->setExpectedException('RuntimeException', 'The Request object has not been set');
  770. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  771. $admin->getRequest();
  772. }
  773. public function testGetTranslationDomain()
  774. {
  775. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  776. $this->assertSame('messages', $admin->getTranslationDomain());
  777. $admin->setTranslationDomain('foo');
  778. $this->assertSame('foo', $admin->getTranslationDomain());
  779. }
  780. public function testGetTranslator()
  781. {
  782. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  783. $this->assertNull($admin->getTranslator());
  784. $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
  785. $admin->setTranslator($translator);
  786. $this->assertSame($translator, $admin->getTranslator());
  787. }
  788. public function testGetShowGroups()
  789. {
  790. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  791. $this->assertSame(false, $admin->getShowGroups());
  792. $groups = array('foo', 'bar', 'baz');
  793. $admin->setShowGroups($groups);
  794. $this->assertSame($groups, $admin->getShowGroups());
  795. }
  796. public function testGetFormGroups()
  797. {
  798. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  799. $this->assertSame(false, $admin->getFormGroups());
  800. $groups = array('foo', 'bar', 'baz');
  801. $admin->setFormGroups($groups);
  802. $this->assertSame($groups, $admin->getFormGroups());
  803. }
  804. public function testGetMaxPageLinks()
  805. {
  806. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  807. $this->assertSame(25, $admin->getMaxPageLinks());
  808. $admin->setMaxPageLinks(14);
  809. $this->assertSame(14, $admin->getMaxPageLinks());
  810. }
  811. public function testGetMaxPerPage()
  812. {
  813. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  814. $this->assertSame(32, $admin->getMaxPerPage());
  815. $admin->setMaxPerPage(94);
  816. $this->assertSame(94, $admin->getMaxPerPage());
  817. }
  818. public function testGetLabel()
  819. {
  820. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  821. $this->assertNull($admin->getLabel());
  822. $admin->setLabel('FooLabel');
  823. $this->assertSame('FooLabel', $admin->getLabel());
  824. }
  825. public function testGetBaseController()
  826. {
  827. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  828. $this->assertSame('SonataNewsBundle:PostAdmin', $admin->getBaseControllerName());
  829. $admin->setBaseControllerName('SonataNewsBundle:FooAdmin');
  830. $this->assertSame('SonataNewsBundle:FooAdmin', $admin->getBaseControllerName());
  831. }
  832. public function testGetTemplates()
  833. {
  834. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  835. $this->assertSame(array(), $admin->getTemplates());
  836. $templates = array(
  837. 'list' => 'FooAdminBundle:CRUD:list.html.twig',
  838. 'show' => 'FooAdminBundle:CRUD:show.html.twig',
  839. 'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
  840. );
  841. $admin->setTemplates($templates);
  842. $this->assertSame($templates, $admin->getTemplates());
  843. }
  844. public function testGetTemplate1()
  845. {
  846. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  847. $this->assertNull($admin->getTemplate('edit'));
  848. $admin->setTemplate('edit', 'FooAdminBundle:CRUD:edit.html.twig');
  849. $admin->setTemplate('show', 'FooAdminBundle:CRUD:show.html.twig');
  850. $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
  851. $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
  852. }
  853. public function testGetTemplate2()
  854. {
  855. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  856. $this->assertNull($admin->getTemplate('edit'));
  857. $templates = array(
  858. 'list' => 'FooAdminBundle:CRUD:list.html.twig',
  859. 'show' => 'FooAdminBundle:CRUD:show.html.twig',
  860. 'edit' => 'FooAdminBundle:CRUD:edit.html.twig',
  861. );
  862. $admin->setTemplates($templates);
  863. $this->assertSame('FooAdminBundle:CRUD:edit.html.twig', $admin->getTemplate('edit'));
  864. $this->assertSame('FooAdminBundle:CRUD:show.html.twig', $admin->getTemplate('show'));
  865. }
  866. public function testGetIdParameter()
  867. {
  868. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  869. $this->assertSame('id', $admin->getIdParameter());
  870. $this->assertFalse($admin->isChild());
  871. $parentAdmin = new PostAdmin('sonata.post.admin.post_parent', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostParentAdmin');
  872. $admin->setParent($parentAdmin);
  873. $this->assertTrue($admin->isChild());
  874. $this->assertSame('childId', $admin->getIdParameter());
  875. }
  876. public function testGetExportFormats()
  877. {
  878. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  879. $this->assertSame(array('json', 'xml', 'csv', 'xls'), $admin->getExportFormats());
  880. }
  881. public function testGetUrlsafeIdentifier()
  882. {
  883. $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  884. $entity = new \stdClass();
  885. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  886. $modelManager->expects($this->once())
  887. ->method('getUrlsafeIdentifier')
  888. ->with($this->equalTo($entity))
  889. ->will($this->returnValue('foo'));
  890. $admin->setModelManager($modelManager);
  891. $this->assertSame('foo', $admin->getUrlsafeIdentifier($entity));
  892. }
  893. public function testDeterminedPerPageValue()
  894. {
  895. $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  896. $this->assertFalse($admin->determinedPerPageValue('foo'));
  897. $this->assertFalse($admin->determinedPerPageValue(123));
  898. $this->assertTrue($admin->determinedPerPageValue(16));
  899. $this->assertTrue($admin->determinedPerPageValue(32));
  900. $this->assertTrue($admin->determinedPerPageValue(64));
  901. $this->assertTrue($admin->determinedPerPageValue(128));
  902. $this->assertTrue($admin->determinedPerPageValue(192));
  903. $admin->setPerPageOptions(array(101, 102, 103));
  904. $this->assertFalse($admin->determinedPerPageValue(15));
  905. $this->assertFalse($admin->determinedPerPageValue(25));
  906. $this->assertFalse($admin->determinedPerPageValue(200));
  907. $this->assertTrue($admin->determinedPerPageValue(101));
  908. $this->assertTrue($admin->determinedPerPageValue(102));
  909. $this->assertTrue($admin->determinedPerPageValue(103));
  910. }
  911. public function testIsGranted()
  912. {
  913. $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  914. $entity = new \stdClass();
  915. $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
  916. $securityHandler->expects($this->any())
  917. ->method('isGranted')
  918. ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin, $entity) {
  919. if ($admin == $adminIn && $attributes == 'FOO') {
  920. if (($object == $admin) || ($object == $entity)) {
  921. return true;
  922. }
  923. }
  924. return false;
  925. }));
  926. $admin->setSecurityHandler($securityHandler);
  927. $this->assertTrue($admin->isGranted('FOO'));
  928. $this->assertTrue($admin->isGranted('FOO', $entity));
  929. $this->assertFalse($admin->isGranted('BAR'));
  930. $this->assertFalse($admin->isGranted('BAR', $entity));
  931. }
  932. public function testSupportsPreviewMode()
  933. {
  934. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  935. $this->assertFalse($admin->supportsPreviewMode());
  936. }
  937. public function testGetPermissionsShow()
  938. {
  939. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  940. $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_DASHBOARD));
  941. $this->assertSame(array('LIST'), $admin->getPermissionsShow(AbstractAdmin::CONTEXT_MENU));
  942. $this->assertSame(array('LIST'), $admin->getPermissionsShow('foo'));
  943. }
  944. public function testShowIn()
  945. {
  946. $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  947. $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface');
  948. $securityHandler->expects($this->any())
  949. ->method('isGranted')
  950. ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
  951. if ($admin == $adminIn && $attributes == array('LIST')) {
  952. return true;
  953. }
  954. return false;
  955. }));
  956. $admin->setSecurityHandler($securityHandler);
  957. $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD));
  958. $this->assertTrue($admin->showIn(AbstractAdmin::CONTEXT_MENU));
  959. $this->assertTrue($admin->showIn('foo'));
  960. }
  961. public function testGetObjectIdentifier()
  962. {
  963. $admin = new PostAdmin('sonata.post.admin.post', 'Acme\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  964. $this->assertSame('sonata.post.admin.post', $admin->getObjectIdentifier());
  965. }
  966. public function testTransWithNoTranslator()
  967. {
  968. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  969. $this->assertSame('foo', $admin->trans('foo'));
  970. }
  971. public function testTrans()
  972. {
  973. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  974. $admin->setTranslationDomain('fooMessageDomain');
  975. $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
  976. $admin->setTranslator($translator);
  977. $translator->expects($this->once())
  978. ->method('trans')
  979. ->with($this->equalTo('foo'), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
  980. ->will($this->returnValue('fooTranslated'));
  981. $this->assertSame('fooTranslated', $admin->trans('foo'));
  982. }
  983. public function testTransWithMessageDomain()
  984. {
  985. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  986. $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
  987. $admin->setTranslator($translator);
  988. $translator->expects($this->once())
  989. ->method('trans')
  990. ->with($this->equalTo('foo'), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
  991. ->will($this->returnValue('fooTranslated'));
  992. $this->assertSame('fooTranslated', $admin->trans('foo', array('name' => 'Andrej'), 'fooMessageDomain'));
  993. }
  994. public function testTransChoiceWithNoTranslator()
  995. {
  996. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  997. $this->assertSame('foo', $admin->transChoice('foo', 2));
  998. }
  999. public function testTransChoice()
  1000. {
  1001. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1002. $admin->setTranslationDomain('fooMessageDomain');
  1003. $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
  1004. $admin->setTranslator($translator);
  1005. $translator->expects($this->once())
  1006. ->method('transChoice')
  1007. ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array()), $this->equalTo('fooMessageDomain'))
  1008. ->will($this->returnValue('fooTranslated'));
  1009. $this->assertSame('fooTranslated', $admin->transChoice('foo', 2));
  1010. }
  1011. public function testTransChoiceWithMessageDomain()
  1012. {
  1013. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1014. $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
  1015. $admin->setTranslator($translator);
  1016. $translator->expects($this->once())
  1017. ->method('transChoice')
  1018. ->with($this->equalTo('foo'), $this->equalTo(2), $this->equalTo(array('name' => 'Andrej')), $this->equalTo('fooMessageDomain'))
  1019. ->will($this->returnValue('fooTranslated'));
  1020. $this->assertSame('fooTranslated', $admin->transChoice('foo', 2, array('name' => 'Andrej'), 'fooMessageDomain'));
  1021. }
  1022. public function testSetPersistFilters()
  1023. {
  1024. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1025. $this->assertAttributeSame(false, 'persistFilters', $admin);
  1026. $admin->setPersistFilters(true);
  1027. $this->assertAttributeSame(true, 'persistFilters', $admin);
  1028. }
  1029. public function testGetRootCode()
  1030. {
  1031. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1032. $this->assertSame('sonata.post.admin.post', $admin->getRootCode());
  1033. $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
  1034. $parentFieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  1035. $parentFieldDescription->expects($this->once())
  1036. ->method('getAdmin')
  1037. ->will($this->returnValue($parentAdmin));
  1038. $this->assertNull($admin->getParentFieldDescription());
  1039. $admin->setParentFieldDescription($parentFieldDescription);
  1040. $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
  1041. $this->assertSame('sonata.post.admin.post.parent', $admin->getRootCode());
  1042. }
  1043. public function testGetRoot()
  1044. {
  1045. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1046. $this->assertSame($admin, $admin->getRoot());
  1047. $parentAdmin = new PostAdmin('sonata.post.admin.post.parent', 'NewsBundle\Entity\PostParent', 'SonataNewsBundle:PostParentAdmin');
  1048. $parentFieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  1049. $parentFieldDescription->expects($this->once())
  1050. ->method('getAdmin')
  1051. ->will($this->returnValue($parentAdmin));
  1052. $this->assertNull($admin->getParentFieldDescription());
  1053. $admin->setParentFieldDescription($parentFieldDescription);
  1054. $this->assertSame($parentFieldDescription, $admin->getParentFieldDescription());
  1055. $this->assertSame($parentAdmin, $admin->getRoot());
  1056. }
  1057. public function testGetExportFields()
  1058. {
  1059. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1060. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1061. $modelManager->expects($this->once())
  1062. ->method('getExportFields')
  1063. ->with($this->equalTo('NewsBundle\Entity\Post'))
  1064. ->will($this->returnValue(array('foo', 'bar')));
  1065. $admin->setModelManager($modelManager);
  1066. $this->assertSame(array('foo', 'bar'), $admin->getExportFields());
  1067. }
  1068. public function testGetPersistentParametersWithNoExtension()
  1069. {
  1070. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1071. $this->assertEmpty($admin->getPersistentParameters());
  1072. }
  1073. /**
  1074. * @expectedException \RuntimeException
  1075. */
  1076. public function testGetPersistentParametersWithInvalidExtension()
  1077. {
  1078. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1079. $extension = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
  1080. $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue(null));
  1081. $admin->addExtension($extension);
  1082. $admin->getPersistentParameters();
  1083. }
  1084. public function testGetPersistentParametersWithValidExtension()
  1085. {
  1086. $expected = array(
  1087. 'context' => 'foobar',
  1088. );
  1089. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1090. $extension = $this->getMock('Sonata\AdminBundle\Admin\AdminExtensionInterface');
  1091. $extension->expects($this->once())->method('getPersistentParameters')->will($this->returnValue($expected));
  1092. $admin->addExtension($extension);
  1093. $this->assertSame($expected, $admin->getPersistentParameters());
  1094. }
  1095. public function testGetFormWithNonCollectionParentValue()
  1096. {
  1097. $post = new Post();
  1098. $tagAdmin = $this->createTagAdmin($post);
  1099. $tag = $tagAdmin->getSubject();
  1100. $tag->setPosts(null);
  1101. $tagAdmin->getForm();
  1102. $this->assertSame($post, $tag->getPosts());
  1103. }
  1104. public function testGetFormWithCollectionParentValue()
  1105. {
  1106. $post = new Post();
  1107. $tagAdmin = $this->createTagAdmin($post);
  1108. $tag = $tagAdmin->getSubject();
  1109. // Case of a doctrine collection
  1110. $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
  1111. $this->assertCount(0, $tag->getPosts());
  1112. $tag->addPost(new Post());
  1113. $this->assertCount(1, $tag->getPosts());
  1114. $tagAdmin->getForm();
  1115. $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $tag->getPosts());
  1116. $this->assertCount(2, $tag->getPosts());
  1117. $this->assertContains($post, $tag->getPosts());
  1118. // Case of an array
  1119. $tag->setPosts(array());
  1120. $this->assertCount(0, $tag->getPosts());
  1121. $tag->addPost(new Post());
  1122. $this->assertCount(1, $tag->getPosts());
  1123. $tagAdmin->getForm();
  1124. $this->assertInternalType('array', $tag->getPosts());
  1125. $this->assertCount(2, $tag->getPosts());
  1126. $this->assertContains($post, $tag->getPosts());
  1127. }
  1128. public function testRemoveFieldFromFormGroup()
  1129. {
  1130. $formGroups = array(
  1131. 'foobar' => array(
  1132. 'fields' => array(
  1133. 'foo' => 'foo',
  1134. 'bar' => 'bar',
  1135. ),
  1136. ),
  1137. );
  1138. $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1139. $admin->setFormGroups($formGroups);
  1140. $admin->removeFieldFromFormGroup('foo');
  1141. $this->assertSame($admin->getFormGroups(), array(
  1142. 'foobar' => array(
  1143. 'fields' => array(
  1144. 'bar' => 'bar',
  1145. ),
  1146. ),
  1147. ));
  1148. $admin->removeFieldFromFormGroup('bar');
  1149. $this->assertSame($admin->getFormGroups(), array());
  1150. }
  1151. public function testGetFilterParameters()
  1152. {
  1153. $authorId = uniqid();
  1154. $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1155. $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
  1156. $commentAdmin->setParentAssociationMapping('post.author');
  1157. $commentAdmin->setParent($postAdmin);
  1158. $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array('get'));
  1159. $request->expects($this->any())
  1160. ->method('get')
  1161. ->will($this->returnValue($authorId));
  1162. $commentAdmin->setRequest($request);
  1163. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1164. $modelManager->expects($this->any())
  1165. ->method('getDefaultSortValues')
  1166. ->will($this->returnValue(array()));
  1167. $commentAdmin->setModelManager($modelManager);
  1168. $parameters = $commentAdmin->getFilterParameters();
  1169. $this->assertTrue(isset($parameters['post__author']));
  1170. $this->assertSame(array('value' => $authorId), $parameters['post__author']);
  1171. }
  1172. public function testGetFilterFieldDescription()
  1173. {
  1174. $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
  1175. $fooFieldDescription = new FieldDescription();
  1176. $barFieldDescription = new FieldDescription();
  1177. $bazFieldDescription = new FieldDescription();
  1178. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1179. $modelManager->expects($this->exactly(3))
  1180. ->method('getNewFieldDescriptionInstance')
  1181. ->will($this->returnCallback(function ($adminClass, $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) {
  1182. switch ($name) {
  1183. case 'foo':
  1184. $fieldDescription = $fooFieldDescription;
  1185. break;
  1186. case 'bar':
  1187. $fieldDescription = $barFieldDescription;
  1188. break;
  1189. case 'baz':
  1190. $fieldDescription = $bazFieldDescription;
  1191. break;
  1192. default:
  1193. throw new \RuntimeException(sprintf('Unknown filter name "%s"', $name));
  1194. break;
  1195. }
  1196. $fieldDescription->setName($name);
  1197. return $fieldDescription;
  1198. }));
  1199. $modelAdmin->setModelManager($modelManager);
  1200. $pager = $this->getMock('Sonata\AdminBundle\Datagrid\PagerInterface');
  1201. $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  1202. $datagrid->expects($this->once())
  1203. ->method('getPager')
  1204. ->will($this->returnValue($pager));
  1205. $datagridBuilder = $this->getMock('Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  1206. $datagridBuilder->expects($this->once())
  1207. ->method('getBaseDatagrid')
  1208. ->with($this->identicalTo($modelAdmin), array())
  1209. ->will($this->returnValue($datagrid));
  1210. $datagridBuilder->expects($this->exactly(3))
  1211. ->method('addFilter')
  1212. ->will($this->returnCallback(function ($datagrid, $type, $fieldDescription, AdminInterface $admin) {
  1213. $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
  1214. $fieldDescription->mergeOption('field_options', array('required' => false));
  1215. }));
  1216. $modelAdmin->setDatagridBuilder($datagridBuilder);
  1217. $this->assertSame(array('foo' => $fooFieldDescription, 'bar' => $barFieldDescription, 'baz' => $bazFieldDescription), $modelAdmin->getFilterFieldDescriptions());
  1218. $this->assertFalse($modelAdmin->hasFilterFieldDescription('fooBar'));
  1219. $this->assertTrue($modelAdmin->hasFilterFieldDescription('foo'));
  1220. $this->assertTrue($modelAdmin->hasFilterFieldDescription('bar'));
  1221. $this->assertTrue($modelAdmin->hasFilterFieldDescription('baz'));
  1222. $this->assertSame($fooFieldDescription, $modelAdmin->getFilterFieldDescription('foo'));
  1223. $this->assertSame($barFieldDescription, $modelAdmin->getFilterFieldDescription('bar'));
  1224. $this->assertSame($bazFieldDescription, $modelAdmin->getFilterFieldDescription('baz'));
  1225. }
  1226. public function testGetSubjectNoRequest()
  1227. {
  1228. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1229. $modelManager
  1230. ->expects($this->never())
  1231. ->method('find');
  1232. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1233. $admin->setModelManager($modelManager);
  1234. $this->assertNull($admin->getSubject());
  1235. }
  1236. public function testGetSideMenu()
  1237. {
  1238. $item = $this->getMock('Knp\Menu\ItemInterface');
  1239. $item
  1240. ->expects($this->once())
  1241. ->method('setChildrenAttribute')
  1242. ->with('class', 'nav navbar-nav');
  1243. $item
  1244. ->expects($this->once())
  1245. ->method('setExtra')
  1246. ->with('translation_domain', 'foo_bar_baz');
  1247. $menuFactory = $this->getMock('Knp\Menu\FactoryInterface');
  1248. $menuFactory
  1249. ->expects($this->once())
  1250. ->method('createItem')
  1251. ->will($this->returnValue($item));
  1252. $modelAdmin = new ModelAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
  1253. $modelAdmin->setMenuFactory($menuFactory);
  1254. $modelAdmin->setTranslationDomain('foo_bar_baz');
  1255. $modelAdmin->getSideMenu('foo');
  1256. }
  1257. /**
  1258. * @return array
  1259. */
  1260. public function provideGetSubject()
  1261. {
  1262. return array(
  1263. array(23),
  1264. array('azerty'),
  1265. array('4f69bbb5f14a13347f000092'),
  1266. array('0779ca8d-e2be-11e4-ac58-0242ac11000b'),
  1267. array('123'.AdapterInterface::ID_SEPARATOR.'my_type'), // composite keys are supported
  1268. );
  1269. }
  1270. /**
  1271. * @dataProvider provideGetSubject
  1272. */
  1273. public function testGetSubjectFailed($id)
  1274. {
  1275. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1276. $modelManager
  1277. ->expects($this->once())
  1278. ->method('find')
  1279. ->with('NewsBundle\Entity\Post', $id)
  1280. ->will($this->returnValue(null)); // entity not found
  1281. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1282. $admin->setModelManager($modelManager);
  1283. $admin->setRequest(new Request(array('id' => $id)));
  1284. $this->assertNull($admin->getSubject());
  1285. }
  1286. /**
  1287. * @dataProvider provideGetSubject
  1288. */
  1289. public function testGetSubject($id)
  1290. {
  1291. $entity = new Post();
  1292. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1293. $modelManager
  1294. ->expects($this->once())
  1295. ->method('find')
  1296. ->with('NewsBundle\Entity\Post', $id)
  1297. ->will($this->returnValue($entity));
  1298. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1299. $admin->setModelManager($modelManager);
  1300. $admin->setRequest(new Request(array('id' => $id)));
  1301. $this->assertSame($entity, $admin->getSubject());
  1302. $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
  1303. }
  1304. /**
  1305. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
  1306. */
  1307. public function testGetActionButtonsList()
  1308. {
  1309. $expected = array(
  1310. 'create' => array(
  1311. 'template' => 'SonataAdminBundle:Button:create_button.html.twig',
  1312. ),
  1313. );
  1314. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1315. $this->assertSame($expected, $admin->getActionButtons('list', null));
  1316. }
  1317. /**
  1318. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::configureBatchActions
  1319. */
  1320. public function getBatchActions()
  1321. {
  1322. $expected = array(
  1323. 'action' => array(
  1324. 'label' => 'action_delete',
  1325. 'translation_domain' => 'SonataAdminBundle',
  1326. 'ask_confirmation' => true, // by default always true
  1327. ),
  1328. 'foo' => array(
  1329. 'label' => 'action_foo',
  1330. ),
  1331. );
  1332. $modelAdmin = new PostAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'SonataFooBundle:ModelAdmin');
  1333. $this->assertSame($expected, $modelAdmin->getBatchActions());
  1334. }
  1335. /**
  1336. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
  1337. */
  1338. public function testShowMosaicButton()
  1339. {
  1340. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1341. $listModes = $admin->getListModes();
  1342. $admin->showMosaicButton(true);
  1343. $this->assertSame($listModes, $admin->getListModes());
  1344. }
  1345. /**
  1346. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::showMosaicButton
  1347. */
  1348. public function testShowMosaicButtonHideMosaic()
  1349. {
  1350. $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
  1351. $listModes = $admin->getListModes();
  1352. $expected['list'] = $listModes['list'];
  1353. $admin->showMosaicButton(false);
  1354. $this->assertSame($expected, $admin->getListModes());
  1355. }
  1356. /**
  1357. * @covers Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions
  1358. * @dataProvider provideGetBaseRouteName
  1359. */
  1360. public function testDefaultDashboardActionsArePresent($objFqn, $expected)
  1361. {
  1362. $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
  1363. $routeGenerator = new DefaultRouteGenerator(
  1364. $this->getMock('Symfony\Component\Routing\RouterInterface'),
  1365. new RoutesCache($this->cacheTempFolder, true)
  1366. );
  1367. $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
  1368. $admin->setRouteBuilder($pathInfo);
  1369. $admin->setRouteGenerator($routeGenerator);
  1370. $admin->initialize();
  1371. $securityHandler = $this->getMock('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
  1372. $securityHandler->expects($this->any())
  1373. ->method('isGranted')
  1374. ->will($this->returnCallback(function (AdminInterface $adminIn, $attributes, $object = null) use ($admin) {
  1375. if ($admin == $adminIn && ($attributes == 'CREATE' || $attributes == 'LIST')) {
  1376. return true;
  1377. }
  1378. return false;
  1379. }));
  1380. $admin->setSecurityHandler($securityHandler);
  1381. $this->assertArrayHasKey('list', $admin->getDashboardActions());
  1382. $this->assertArrayHasKey('create', $admin->getDashboardActions());
  1383. }
  1384. /**
  1385. * @group legacy
  1386. */
  1387. public function testDefaultBreadcrumbsBuilder()
  1388. {
  1389. $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
  1390. 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
  1391. ));
  1392. $this->assertInstanceOf(
  1393. 'Sonata\AdminBundle\Admin\BreadcrumbsBuilder',
  1394. $admin->getBreadcrumbsBuilder()
  1395. );
  1396. }
  1397. /**
  1398. * @group legacy
  1399. */
  1400. public function testBreadcrumbsBuilderSetter()
  1401. {
  1402. $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
  1403. 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
  1404. ));
  1405. $this->assertSame($admin, $admin->setBreadcrumbsBuilder($builder = $this->getMock(
  1406. 'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
  1407. )));
  1408. $this->assertSame($builder, $admin->getBreadcrumbsBuilder());
  1409. }
  1410. /**
  1411. * @group legacy
  1412. */
  1413. public function testGetBreadcrumbs()
  1414. {
  1415. $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
  1416. 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
  1417. ));
  1418. $builder = $this->prophesize(
  1419. 'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
  1420. );
  1421. $action = 'myaction';
  1422. $builder->getBreadcrumbs($admin, $action)->shouldBeCalled();
  1423. $admin->setBreadcrumbsBuilder($builder->reveal())->getBreadcrumbs($action);
  1424. }
  1425. /**
  1426. * @group legacy
  1427. */
  1428. public function testBuildBreadcrumbs()
  1429. {
  1430. $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
  1431. 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
  1432. ));
  1433. $builder = $this->prophesize(
  1434. 'Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface'
  1435. );
  1436. $action = 'myaction';
  1437. $menu = $this->getMock('Knp\Menu\ItemInterface');
  1438. $builder->buildBreadcrumbs($admin, $action, $menu)
  1439. ->shouldBeCalledTimes(1)
  1440. ->willReturn($menu);
  1441. $admin->setBreadcrumbsBuilder($builder->reveal());
  1442. /* check the called is proxied only once */
  1443. $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
  1444. $this->assertSame($menu, $admin->buildBreadcrumbs($action, $menu));
  1445. }
  1446. /**
  1447. * NEXT_MAJOR: remove this method.
  1448. *
  1449. * @group legacy
  1450. */
  1451. public function testCreateQueryLegacyCallWorks()
  1452. {
  1453. $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AbstractAdmin', array(
  1454. 'admin.my_code', 'My\Class', 'MyBundle:ClassAdmin',
  1455. ));
  1456. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1457. $modelManager->expects($this->once())
  1458. ->method('createQuery')
  1459. ->with('My\Class')
  1460. ->willReturn('a query');
  1461. $admin->setModelManager($modelManager);
  1462. $this->assertSame('a query', $admin->createQuery('list'));
  1463. }
  1464. private function createTagAdmin(Post $post)
  1465. {
  1466. $postAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin')
  1467. ->disableOriginalConstructor()
  1468. ->getMock();
  1469. $postAdmin->expects($this->any())->method('getObject')->will($this->returnValue($post));
  1470. $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  1471. $formBuilder->expects($this->any())->method('getForm')->will($this->returnValue(null));
  1472. $tagAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\TagAdmin')
  1473. ->setConstructorArgs(array(
  1474. 'admin.tag',
  1475. 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag',
  1476. 'MyBundle:MyController',
  1477. ))
  1478. ->setMethods(array('getFormBuilder'))
  1479. ->getMock();
  1480. $tagAdmin->expects($this->any())->method('getFormBuilder')->will($this->returnValue($formBuilder));
  1481. $tagAdmin->setParent($postAdmin);
  1482. $tag = new Tag();
  1483. $tagAdmin->setSubject($tag);
  1484. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  1485. $tagAdmin->setRequest($request);
  1486. $configurationPool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')
  1487. ->disableOriginalConstructor()
  1488. ->getMock();
  1489. $configurationPool->expects($this->any())->method('getPropertyAccessor')->will($this->returnValue(PropertyAccess::createPropertyAccessor()));
  1490. $tagAdmin->setConfigurationPool($configurationPool);
  1491. return $tagAdmin;
  1492. }
  1493. }