AdminTest.php 64 KB

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