CRUDControllerTest.php 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Sonata\AdminBundle\Controller\CRUDController;
  15. use Sonata\AdminBundle\Admin\Pool;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Bridge\Twig\Extension\FormExtension;
  18. use Symfony\Component\HttpFoundation\Session\Session;
  19. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  20. use Sonata\AdminBundle\Exception\ModelManagerException;
  21. use Symfony\Component\HttpFoundation\StreamedResponse;
  22. use Sonata\AdminBundle\Util\AdminObjectAclManipulator;
  23. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  24. use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController;
  25. use Symfony\Component\HttpKernel\Kernel;
  26. use Symfony\Component\HttpKernel\Exception\HttpException;
  27. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
  28. use Sonata\AdminBundle\Admin\AdminInterface;
  29. /**
  30. * Test for CRUDController
  31. *
  32. * @author Andrej Hudec <pulzarraider@gmail.com>
  33. */
  34. class CRUDControllerTest extends \PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * @var CRUDController
  38. */
  39. private $controller;
  40. /**
  41. * @var Request
  42. */
  43. private $request;
  44. /**
  45. * @var AdminInterface
  46. */
  47. private $admin;
  48. /**
  49. * @var Pool
  50. */
  51. private $pool;
  52. /**
  53. * @var array
  54. */
  55. private $parameters;
  56. /**
  57. * @var Session
  58. */
  59. private $session;
  60. /**
  61. * @var Sonata\AdminBundle\Model\AuditManager
  62. */
  63. private $auditManager;
  64. /**
  65. * @var ContainerInterface
  66. */
  67. private $container;
  68. /**
  69. * @var AdminObjectAclManipulator
  70. */
  71. private $adminObjectAclManipulator;
  72. /**
  73. * @var string
  74. */
  75. private $template;
  76. /**
  77. * @var array
  78. */
  79. private $protectedTestedMethods;
  80. /**
  81. * @var CsrfProviderInterface
  82. */
  83. private $csrfProvider;
  84. /**
  85. * {@inheritDoc}
  86. */
  87. protected function setUp()
  88. {
  89. $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  90. $this->request = new Request();
  91. $this->pool = new Pool($this->container, 'title', 'logo.png');
  92. $this->request->attributes->set('_sonata_admin', 'foo.admin');
  93. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  94. $this->parameters = array();
  95. $this->template = '';
  96. // php 5.3 BC
  97. $params = &$this->parameters;
  98. $template = &$this->template;
  99. $templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine', array(), array($this->container, array()));
  100. $templating->expects($this->any())
  101. ->method('renderResponse')
  102. ->will($this->returnCallback(function($view, array $parameters = array(), Response $response = null) use (&$params, &$template) {
  103. $template = $view;
  104. if (null === $response) {
  105. $response = new Response();
  106. }
  107. $params = $parameters;
  108. return $response;
  109. }));
  110. $this->session = new Session(new MockArraySessionStorage());
  111. // php 5.3 BC
  112. $pool = $this->pool;
  113. $request = $this->request;
  114. $admin = $this->admin;
  115. $session = $this->session;
  116. $twig = $this->getMockBuilder('Twig_Environment')
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $twigRenderer = $this->getMock('Symfony\Bridge\Twig\Form\TwigRendererInterface');
  120. $formExtension = new FormExtension($twigRenderer);
  121. $twig->expects($this->any())
  122. ->method('getExtension')
  123. ->will($this->returnCallback(function ($name) use ($formExtension) {
  124. switch ($name) {
  125. case 'form':
  126. return $formExtension;
  127. }
  128. return null;
  129. }));
  130. $exporter = $this->getMock('Sonata\AdminBundle\Export\Exporter');
  131. $exporter->expects($this->any())
  132. ->method('getResponse')
  133. ->will($this->returnValue(new StreamedResponse()));
  134. $this->auditManager = $this->getMockBuilder('Sonata\AdminBundle\Model\AuditManager')
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $this->adminObjectAclManipulator = $this->getMockBuilder('Sonata\AdminBundle\Util\AdminObjectAclManipulator')
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. // php 5.3 BC
  141. $auditManager = $this->auditManager;
  142. $adminObjectAclManipulator = $this->adminObjectAclManipulator;
  143. $this->csrfProvider = $this->getMockBuilder('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface')
  144. ->getMock();
  145. $this->csrfProvider->expects($this->any())
  146. ->method('generateCsrfToken')
  147. ->will($this->returnCallback(function ($intention) {
  148. return 'csrf-token-123_'.$intention;
  149. }));
  150. $this->csrfProvider->expects($this->any())
  151. ->method('isCsrfTokenValid')
  152. ->will($this->returnCallback(function ($intention, $token) {
  153. if ($token == 'csrf-token-123_'.$intention) {
  154. return true;
  155. }
  156. return false;
  157. }));
  158. // php 5.3 BC
  159. $csrfProvider = $this->csrfProvider;
  160. $requestStack = null;
  161. if (Kernel::MINOR_VERSION > 3) {
  162. $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
  163. $requestStack->push($request);
  164. }
  165. $this->container->expects($this->any())
  166. ->method('get')
  167. ->will($this->returnCallback(function ($id) use ($pool, $request, $admin, $templating, $twig, $session, $exporter, $auditManager, $adminObjectAclManipulator, $requestStack, $csrfProvider) {
  168. switch ($id) {
  169. case 'sonata.admin.pool':
  170. return $pool;
  171. case 'request_stack':
  172. return $requestStack;
  173. case 'request':
  174. return $request;
  175. case 'foo.admin':
  176. return $admin;
  177. case 'templating':
  178. return $templating;
  179. case 'twig':
  180. return $twig;
  181. case 'session':
  182. return $session;
  183. case 'sonata.admin.exporter':
  184. return $exporter;
  185. case 'sonata.admin.audit.manager':
  186. return $auditManager;
  187. case 'sonata.admin.object.manipulator.acl.admin':
  188. return $adminObjectAclManipulator;
  189. case 'form.csrf_provider':
  190. return $csrfProvider;
  191. }
  192. return null;
  193. }));
  194. // php 5.3
  195. $tthis = $this;
  196. $this->container->expects($this->any())
  197. ->method('has')
  198. ->will($this->returnCallback(function ($id) use ($tthis) {
  199. if ($id == 'form.csrf_provider' && $tthis->getCsrfProvider()!==null) {
  200. return true;
  201. }
  202. return false;
  203. }));
  204. $this->admin->expects($this->any())
  205. ->method('getTemplate')
  206. ->will($this->returnCallback(function ($name) {
  207. switch ($name) {
  208. case 'ajax':
  209. return 'SonataAdminBundle::ajax_layout.html.twig';
  210. case 'layout':
  211. return 'SonataAdminBundle::standard_layout.html.twig';
  212. case 'show':
  213. return 'SonataAdminBundle:CRUD:show.html.twig';
  214. case 'edit':
  215. return 'SonataAdminBundle:CRUD:edit.html.twig';
  216. case 'dashboard':
  217. return 'SonataAdminBundle:Core:dashboard.html.twig';
  218. case 'search':
  219. return 'SonataAdminBundle:Core:search.html.twig';
  220. case 'list':
  221. return 'SonataAdminBundle:CRUD:list.html.twig';
  222. case 'preview':
  223. return 'SonataAdminBundle:CRUD:preview.html.twig';
  224. case 'history':
  225. return 'SonataAdminBundle:CRUD:history.html.twig';
  226. case 'acl':
  227. return 'SonataAdminBundle:CRUD:acl.html.twig';
  228. case 'delete':
  229. return 'SonataAdminBundle:CRUD:delete.html.twig';
  230. case 'batch':
  231. return 'SonataAdminBundle:CRUD:list__batch.html.twig';
  232. case 'batch_confirmation':
  233. return 'SonataAdminBundle:CRUD:batch_confirmation.html.twig';
  234. }
  235. return null;
  236. }));
  237. $this->admin->expects($this->any())
  238. ->method('getIdParameter')
  239. ->will($this->returnValue('id'));
  240. $this->admin->expects($this->any())
  241. ->method('generateUrl')
  242. ->will($this->returnCallback(function($name, array $parameters = array(), $absolute = false) {
  243. $result = $name;
  244. if (!empty($parameters)) {
  245. $result .= '?'.http_build_query($parameters);
  246. }
  247. return $result;
  248. }));
  249. $this->admin->expects($this->any())
  250. ->method('generateObjectUrl')
  251. ->will($this->returnCallback(function($name, $object, array $parameters = array(), $absolute = false) {
  252. $result = get_class($object).'_'.$name;
  253. if (!empty($parameters)) {
  254. $result .= '?'.http_build_query($parameters);
  255. }
  256. return $result;
  257. }));
  258. $this->controller = new CRUDController();
  259. $this->controller->setContainer($this->container);
  260. // Make some methods public to test them
  261. $testedMethods = array('renderJson', 'isXmlHttpRequest', 'configure', 'getBaseTemplate', 'redirectTo', 'addFlash');
  262. foreach ($testedMethods as $testedMethod) {
  263. $method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod);
  264. $method->setAccessible(true);
  265. $this->protectedTestedMethods[$testedMethod] = $method;
  266. }
  267. }
  268. public function testRenderJson1()
  269. {
  270. $data = array('example'=>'123', 'foo'=>'bar');
  271. $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
  272. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
  273. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  274. $this->assertEquals(json_encode($data), $response->getContent());
  275. }
  276. public function testRenderJson2()
  277. {
  278. $data = array('example'=>'123', 'foo'=>'bar');
  279. $this->request->headers->set('Content-Type', 'multipart/form-data');
  280. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
  281. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  282. $this->assertEquals(json_encode($data), $response->getContent());
  283. }
  284. public function testRenderJsonAjax()
  285. {
  286. $data = array('example'=>'123', 'foo'=>'bar');
  287. $this->request->attributes->set('_xml_http_request', true);
  288. $this->request->headers->set('Content-Type', 'multipart/form-data');
  289. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
  290. $this->assertEquals($response->headers->get('Content-Type'), 'text/plain');
  291. $this->assertEquals(json_encode($data), $response->getContent());
  292. }
  293. public function testIsXmlHttpRequest()
  294. {
  295. $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  296. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  297. $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  298. $this->request->headers->remove('X-Requested-With');
  299. $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  300. $this->request->attributes->set('_xml_http_request', true);
  301. $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  302. }
  303. public function testConfigure()
  304. {
  305. $uniqueId = '';
  306. $this->admin->expects($this->once())
  307. ->method('setUniqid')
  308. ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
  309. $uniqueId = $uniqid;
  310. }));
  311. $this->request->query->set('uniqid', 123456);
  312. $this->protectedTestedMethods['configure']->invoke($this->controller);
  313. $this->assertEquals(123456, $uniqueId);
  314. $this->assertAttributeEquals($this->admin, 'admin', $this->controller);
  315. }
  316. public function testConfigureChild()
  317. {
  318. $uniqueId = '';
  319. $this->admin->expects($this->once())
  320. ->method('setUniqid')
  321. ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
  322. $uniqueId = $uniqid;
  323. }));
  324. $this->admin->expects($this->once())
  325. ->method('isChild')
  326. ->will($this->returnValue(true));
  327. $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  328. $this->admin->expects($this->once())
  329. ->method('getParent')
  330. ->will($this->returnValue($adminParent));
  331. $this->request->query->set('uniqid', 123456);
  332. $this->protectedTestedMethods['configure']->invoke($this->controller);
  333. $this->assertEquals(123456, $uniqueId);
  334. $this->assertAttributeEquals($adminParent, 'admin', $this->controller);
  335. }
  336. public function testConfigureWithException()
  337. {
  338. $this->setExpectedException('RuntimeException', 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`');
  339. $this->request->attributes->remove('_sonata_admin');
  340. $this->protectedTestedMethods['configure']->invoke($this->controller);
  341. }
  342. public function testConfigureWithException2()
  343. {
  344. $this->setExpectedException('RuntimeException', 'Unable to find the admin class related to the current controller (Sonata\AdminBundle\Controller\CRUDController)');
  345. $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
  346. $this->protectedTestedMethods['configure']->invoke($this->controller);
  347. }
  348. public function testGetBaseTemplate()
  349. {
  350. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
  351. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  352. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
  353. $this->request->headers->remove('X-Requested-With');
  354. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
  355. $this->request->attributes->set('_xml_http_request', true);
  356. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
  357. }
  358. public function testRender()
  359. {
  360. $this->parameters = array();
  361. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array()));
  362. $this->assertEquals($this->admin, $this->parameters['admin']);
  363. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  364. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  365. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  366. }
  367. public function testRenderWithResponse()
  368. {
  369. $this->parameters = array();
  370. $response = $response = new Response();
  371. $response->headers->set('X-foo', 'bar');
  372. $responseResult = $this->controller->render('FooAdminBundle::foo.html.twig', array(), $response);
  373. $this->assertEquals($response, $responseResult);
  374. $this->assertEquals('bar', $responseResult->headers->get('X-foo'));
  375. $this->assertEquals($this->admin, $this->parameters['admin']);
  376. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  377. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  378. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  379. }
  380. public function testRenderCustomParams()
  381. {
  382. $this->parameters = array();
  383. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo'=>'bar')));
  384. $this->assertEquals($this->admin, $this->parameters['admin']);
  385. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  386. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  387. $this->assertEquals('bar', $this->parameters['foo']);
  388. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  389. }
  390. public function testRenderAjax()
  391. {
  392. $this->parameters = array();
  393. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  394. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo'=>'bar')));
  395. $this->assertEquals($this->admin, $this->parameters['admin']);
  396. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  397. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  398. $this->assertEquals('bar', $this->parameters['foo']);
  399. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  400. }
  401. public function testListActionAccessDenied()
  402. {
  403. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  404. $this->admin->expects($this->once())
  405. ->method('isGranted')
  406. ->with($this->equalTo('LIST'))
  407. ->will($this->returnValue(false));
  408. $this->controller->listAction();
  409. }
  410. public function testListAction()
  411. {
  412. $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  413. $this->admin->expects($this->once())
  414. ->method('isGranted')
  415. ->with($this->equalTo('LIST'))
  416. ->will($this->returnValue(true));
  417. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  418. ->disableOriginalConstructor()
  419. ->getMock();
  420. $form->expects($this->once())
  421. ->method('createView')
  422. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  423. $this->admin->expects($this->once())
  424. ->method('getDatagrid')
  425. ->will($this->returnValue($datagrid));
  426. $datagrid->expects($this->once())
  427. ->method('getForm')
  428. ->will($this->returnValue($form));
  429. $this->parameters = array();
  430. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->listAction());
  431. $this->assertEquals($this->admin, $this->parameters['admin']);
  432. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  433. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  434. $this->assertEquals('list', $this->parameters['action']);
  435. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  436. $this->assertInstanceOf('Sonata\AdminBundle\Datagrid\DatagridInterface', $this->parameters['datagrid']);
  437. $this->assertEquals('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
  438. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  439. $this->assertEquals('SonataAdminBundle:CRUD:list.html.twig', $this->template);
  440. }
  441. public function testBatchActionDeleteAccessDenied()
  442. {
  443. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  444. $this->admin->expects($this->once())
  445. ->method('isGranted')
  446. ->with($this->equalTo('DELETE'))
  447. ->will($this->returnValue(false));
  448. $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  449. }
  450. public function testBatchActionDelete()
  451. {
  452. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  453. $this->admin->expects($this->once())
  454. ->method('isGranted')
  455. ->with($this->equalTo('DELETE'))
  456. ->will($this->returnValue(true));
  457. $this->admin->expects($this->once())
  458. ->method('getModelManager')
  459. ->will($this->returnValue($modelManager));
  460. $this->admin->expects($this->once())
  461. ->method('getFilterParameters')
  462. ->will($this->returnValue(array('foo'=>'bar')));
  463. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  464. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  465. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  466. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  467. }
  468. public function testBatchActionDeleteWithModelManagerException()
  469. {
  470. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  471. $modelManager->expects($this->once())
  472. ->method('batchDelete')
  473. ->will($this->returnCallback(function () {
  474. throw new ModelManagerException();
  475. }));
  476. $this->admin->expects($this->once())
  477. ->method('getModelManager')
  478. ->will($this->returnValue($modelManager));
  479. $this->admin->expects($this->once())
  480. ->method('getFilterParameters')
  481. ->will($this->returnValue(array('foo'=>'bar')));
  482. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  483. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  484. $this->assertSame(array('flash_batch_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  485. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  486. }
  487. public function testShowActionNotFoundException()
  488. {
  489. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  490. $this->admin->expects($this->once())
  491. ->method('getObject')
  492. ->will($this->returnValue(false));
  493. $this->controller->showAction();
  494. }
  495. public function testShowActionAccessDenied()
  496. {
  497. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  498. $this->admin->expects($this->once())
  499. ->method('getObject')
  500. ->will($this->returnValue(new \stdClass()));
  501. $this->admin->expects($this->once())
  502. ->method('isGranted')
  503. ->with($this->equalTo('VIEW'))
  504. ->will($this->returnValue(false));
  505. $this->controller->showAction();
  506. }
  507. public function testShowAction()
  508. {
  509. $object = new \stdClass();
  510. $this->admin->expects($this->once())
  511. ->method('getObject')
  512. ->will($this->returnValue($object));
  513. $this->admin->expects($this->once())
  514. ->method('isGranted')
  515. ->with($this->equalTo('VIEW'))
  516. ->will($this->returnValue(true));
  517. $show = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
  518. $this->admin->expects($this->once())
  519. ->method('getShow')
  520. ->will($this->returnValue($show));
  521. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->showAction());
  522. $this->assertEquals($this->admin, $this->parameters['admin']);
  523. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  524. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  525. $this->assertEquals('show', $this->parameters['action']);
  526. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $this->parameters['elements']);
  527. $this->assertEquals($object, $this->parameters['object']);
  528. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  529. $this->assertEquals('SonataAdminBundle:CRUD:show.html.twig', $this->template);
  530. }
  531. /**
  532. * @dataProvider getRedirectToTests
  533. */
  534. public function testRedirectTo($expected, $queryParams, $hasActiveSubclass)
  535. {
  536. $this->admin->expects($this->any())
  537. ->method('hasActiveSubclass')
  538. ->will($this->returnValue($hasActiveSubclass));
  539. $object = new \stdClass();
  540. foreach ($queryParams as $key => $value) {
  541. $this->request->query->set($key, $value);
  542. }
  543. $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object);
  544. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  545. $this->assertEquals($expected, $response->getTargetUrl());
  546. }
  547. public function getRedirectToTests()
  548. {
  549. return array(
  550. array('stdClass_edit', array(), false),
  551. array('list', array('btn_update_and_list'=>true), false),
  552. array('list', array('btn_create_and_list'=>true), false),
  553. array('create', array('btn_create_and_create'=>true), false),
  554. array('create?subclass=foo', array('btn_create_and_create'=>true, 'subclass'=>'foo'), true),
  555. );
  556. }
  557. public function testAddFlash()
  558. {
  559. $this->protectedTestedMethods['addFlash']->invoke($this->controller, 'foo', 'bar');
  560. $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
  561. }
  562. public function testDeleteActionNotFoundException()
  563. {
  564. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  565. $this->admin->expects($this->once())
  566. ->method('getObject')
  567. ->will($this->returnValue(false));
  568. $this->controller->deleteAction(1);
  569. }
  570. public function testDeleteActionAccessDenied()
  571. {
  572. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  573. $this->admin->expects($this->once())
  574. ->method('getObject')
  575. ->will($this->returnValue(new \stdClass()));
  576. $this->admin->expects($this->once())
  577. ->method('isGranted')
  578. ->with($this->equalTo('DELETE'))
  579. ->will($this->returnValue(false));
  580. $this->controller->deleteAction(1);
  581. }
  582. public function testDeleteAction()
  583. {
  584. $object = new \stdClass();
  585. $this->admin->expects($this->once())
  586. ->method('getObject')
  587. ->will($this->returnValue($object));
  588. $this->admin->expects($this->once())
  589. ->method('isGranted')
  590. ->with($this->equalTo('DELETE'))
  591. ->will($this->returnValue(true));
  592. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  593. $this->assertEquals($this->admin, $this->parameters['admin']);
  594. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  595. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  596. $this->assertEquals('delete', $this->parameters['action']);
  597. $this->assertEquals($object, $this->parameters['object']);
  598. $this->assertEquals('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
  599. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  600. $this->assertEquals('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  601. }
  602. public function testDeleteActionNoCsrfToken()
  603. {
  604. $this->csrfProvider = null;
  605. $object = new \stdClass();
  606. $this->admin->expects($this->once())
  607. ->method('getObject')
  608. ->will($this->returnValue($object));
  609. $this->admin->expects($this->once())
  610. ->method('isGranted')
  611. ->with($this->equalTo('DELETE'))
  612. ->will($this->returnValue(true));
  613. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  614. $this->assertEquals($this->admin, $this->parameters['admin']);
  615. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  616. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  617. $this->assertEquals('delete', $this->parameters['action']);
  618. $this->assertEquals($object, $this->parameters['object']);
  619. $this->assertEquals('', $this->parameters['csrf_token']);
  620. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  621. $this->assertEquals('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  622. }
  623. public function testDeleteActionAjaxSuccess1()
  624. {
  625. $object = new \stdClass();
  626. $this->admin->expects($this->once())
  627. ->method('getObject')
  628. ->will($this->returnValue($object));
  629. $this->admin->expects($this->once())
  630. ->method('isGranted')
  631. ->with($this->equalTo('DELETE'))
  632. ->will($this->returnValue(true));
  633. $this->request->setMethod('DELETE');
  634. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  635. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  636. $response = $this->controller->deleteAction(1);
  637. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  638. $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
  639. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  640. }
  641. public function testDeleteActionAjaxSuccess2()
  642. {
  643. $object = new \stdClass();
  644. $this->admin->expects($this->once())
  645. ->method('getObject')
  646. ->will($this->returnValue($object));
  647. $this->admin->expects($this->once())
  648. ->method('isGranted')
  649. ->with($this->equalTo('DELETE'))
  650. ->will($this->returnValue(true));
  651. $this->request->setMethod('POST');
  652. $this->request->request->set('_method', 'DELETE');
  653. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  654. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  655. $response = $this->controller->deleteAction(1);
  656. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  657. $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
  658. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  659. }
  660. public function testDeleteActionAjaxError()
  661. {
  662. $object = new \stdClass();
  663. $this->admin->expects($this->once())
  664. ->method('getObject')
  665. ->will($this->returnValue($object));
  666. $this->admin->expects($this->once())
  667. ->method('isGranted')
  668. ->with($this->equalTo('DELETE'))
  669. ->will($this->returnValue(true));
  670. $this->admin->expects($this->once())
  671. ->method('delete')
  672. ->will($this->returnCallback(function () {
  673. throw new ModelManagerException();
  674. }));
  675. $this->request->setMethod('DELETE');
  676. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  677. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  678. $response = $this->controller->deleteAction(1);
  679. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  680. $this->assertEquals(json_encode(array('result'=>'error')), $response->getContent());
  681. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  682. }
  683. public function testDeleteActionSuccess1()
  684. {
  685. $object = new \stdClass();
  686. $this->admin->expects($this->once())
  687. ->method('getObject')
  688. ->will($this->returnValue($object));
  689. $this->admin->expects($this->once())
  690. ->method('toString')
  691. ->with($this->equalTo($object))
  692. ->will($this->returnValue('test'));
  693. $this->expectTranslate('flash_delete_success', array('%name%' => 'test'));
  694. $this->admin->expects($this->once())
  695. ->method('isGranted')
  696. ->with($this->equalTo('DELETE'))
  697. ->will($this->returnValue(true));
  698. $this->request->setMethod('DELETE');
  699. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  700. $response = $this->controller->deleteAction(1);
  701. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  702. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  703. $this->assertEquals('list', $response->getTargetUrl());
  704. }
  705. public function testDeleteActionSuccess2()
  706. {
  707. $object = new \stdClass();
  708. $this->admin->expects($this->once())
  709. ->method('getObject')
  710. ->will($this->returnValue($object));
  711. $this->admin->expects($this->once())
  712. ->method('isGranted')
  713. ->with($this->equalTo('DELETE'))
  714. ->will($this->returnValue(true));
  715. $this->expectTranslate('flash_delete_success');
  716. $this->request->setMethod('POST');
  717. $this->request->request->set('_method', 'DELETE');
  718. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  719. $response = $this->controller->deleteAction(1);
  720. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  721. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  722. $this->assertEquals('list', $response->getTargetUrl());
  723. }
  724. public function testDeleteActionSuccessNoCsrfTokenProvider()
  725. {
  726. $this->csrfProvider = null;
  727. $object = new \stdClass();
  728. $this->admin->expects($this->once())
  729. ->method('getObject')
  730. ->will($this->returnValue($object));
  731. $this->admin->expects($this->once())
  732. ->method('isGranted')
  733. ->with($this->equalTo('DELETE'))
  734. ->will($this->returnValue(true));
  735. $this->expectTranslate('flash_delete_success');
  736. $this->request->setMethod('POST');
  737. $this->request->request->set('_method', 'DELETE');
  738. $response = $this->controller->deleteAction(1);
  739. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  740. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  741. $this->assertEquals('list', $response->getTargetUrl());
  742. }
  743. public function testDeleteActionWrongRequestMethod()
  744. {
  745. $object = new \stdClass();
  746. $this->admin->expects($this->once())
  747. ->method('getObject')
  748. ->will($this->returnValue($object));
  749. $this->admin->expects($this->once())
  750. ->method('isGranted')
  751. ->with($this->equalTo('DELETE'))
  752. ->will($this->returnValue(true));
  753. //without POST request parameter "_method" should not be used as real REST method
  754. $this->request->query->set('_method', 'DELETE');
  755. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  756. $this->assertEquals($this->admin, $this->parameters['admin']);
  757. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  758. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  759. $this->assertEquals('delete', $this->parameters['action']);
  760. $this->assertEquals($object, $this->parameters['object']);
  761. $this->assertEquals('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
  762. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  763. $this->assertEquals('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  764. }
  765. public function testDeleteActionError()
  766. {
  767. $object = new \stdClass();
  768. $this->admin->expects($this->once())
  769. ->method('getObject')
  770. ->will($this->returnValue($object));
  771. $this->admin->expects($this->once())
  772. ->method('isGranted')
  773. ->with($this->equalTo('DELETE'))
  774. ->will($this->returnValue(true));
  775. $this->expectTranslate('flash_delete_error');
  776. $this->admin->expects($this->once())
  777. ->method('delete')
  778. ->will($this->returnCallback(function () {
  779. throw new ModelManagerException();
  780. }));
  781. $this->request->setMethod('DELETE');
  782. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  783. $response = $this->controller->deleteAction(1);
  784. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  785. $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  786. $this->assertEquals('list', $response->getTargetUrl());
  787. }
  788. public function testDeleteActionInvalidCsrfToken()
  789. {
  790. $object = new \stdClass();
  791. $this->admin->expects($this->once())
  792. ->method('getObject')
  793. ->will($this->returnValue($object));
  794. $this->admin->expects($this->once())
  795. ->method('isGranted')
  796. ->with($this->equalTo('DELETE'))
  797. ->will($this->returnValue(true));
  798. $this->request->setMethod('POST');
  799. $this->request->request->set('_method', 'DELETE');
  800. $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
  801. try {
  802. $this->controller->deleteAction(1);
  803. } catch (HttpException $e) {
  804. $this->assertEquals('The csrf token is not valid, CSRF attack?', $e->getMessage());
  805. $this->assertEquals(400, $e->getStatusCode());
  806. }
  807. }
  808. public function testEditActionNotFoundException()
  809. {
  810. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  811. $this->admin->expects($this->once())
  812. ->method('getObject')
  813. ->will($this->returnValue(false));
  814. $this->controller->editAction();
  815. }
  816. public function testEditActionAccessDenied()
  817. {
  818. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  819. $this->admin->expects($this->once())
  820. ->method('getObject')
  821. ->will($this->returnValue(new \stdClass()));
  822. $this->admin->expects($this->once())
  823. ->method('isGranted')
  824. ->with($this->equalTo('EDIT'))
  825. ->will($this->returnValue(false));
  826. $this->controller->editAction();
  827. }
  828. public function testEditAction()
  829. {
  830. $object = new \stdClass();
  831. $this->admin->expects($this->once())
  832. ->method('getObject')
  833. ->will($this->returnValue($object));
  834. $this->admin->expects($this->once())
  835. ->method('isGranted')
  836. ->with($this->equalTo('EDIT'))
  837. ->will($this->returnValue(true));
  838. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  839. ->disableOriginalConstructor()
  840. ->getMock();
  841. $this->admin->expects($this->once())
  842. ->method('getForm')
  843. ->will($this->returnValue($form));
  844. $formView = $this->getMock('Symfony\Component\Form\FormView');
  845. $form->expects($this->any())
  846. ->method('createView')
  847. ->will($this->returnValue($formView));
  848. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  849. $this->assertEquals($this->admin, $this->parameters['admin']);
  850. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  851. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  852. $this->assertEquals('edit', $this->parameters['action']);
  853. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  854. $this->assertEquals($object, $this->parameters['object']);
  855. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  856. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  857. }
  858. public function testEditActionSuccess()
  859. {
  860. $object = new \stdClass();
  861. $this->admin->expects($this->once())
  862. ->method('getObject')
  863. ->will($this->returnValue($object));
  864. $this->admin->expects($this->once())
  865. ->method('update')
  866. ->will($this->returnArgument(0));
  867. $this->admin->expects($this->once())
  868. ->method('isGranted')
  869. ->with($this->equalTo('EDIT'))
  870. ->will($this->returnValue(true));
  871. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  872. ->disableOriginalConstructor()
  873. ->getMock();
  874. $this->admin->expects($this->once())
  875. ->method('getForm')
  876. ->will($this->returnValue($form));
  877. $form->expects($this->once())
  878. ->method('isValid')
  879. ->will($this->returnValue(true));
  880. $this->expectTranslate('flash_edit_success');
  881. $this->request->setMethod('POST');
  882. $response = $this->controller->editAction();
  883. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  884. $this->assertSame(array('flash_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  885. $this->assertEquals('stdClass_edit', $response->getTargetUrl());
  886. }
  887. public function testEditActionError()
  888. {
  889. $object = new \stdClass();
  890. $this->admin->expects($this->once())
  891. ->method('getObject')
  892. ->will($this->returnValue($object));
  893. $this->admin->expects($this->once())
  894. ->method('isGranted')
  895. ->with($this->equalTo('EDIT'))
  896. ->will($this->returnValue(true));
  897. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  898. ->disableOriginalConstructor()
  899. ->getMock();
  900. $this->admin->expects($this->once())
  901. ->method('getForm')
  902. ->will($this->returnValue($form));
  903. $form->expects($this->once())
  904. ->method('isValid')
  905. ->will($this->returnValue(false));
  906. $this->expectTranslate('flash_edit_error');
  907. $this->request->setMethod('POST');
  908. $formView = $this->getMock('Symfony\Component\Form\FormView');
  909. $form->expects($this->any())
  910. ->method('createView')
  911. ->will($this->returnValue($formView));
  912. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  913. $this->assertEquals($this->admin, $this->parameters['admin']);
  914. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  915. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  916. $this->assertEquals('edit', $this->parameters['action']);
  917. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  918. $this->assertEquals($object, $this->parameters['object']);
  919. $this->assertEquals(array('sonata_flash_error'=>array('flash_edit_error')), $this->session->getFlashBag()->all());
  920. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  921. }
  922. public function testEditActionAjaxSuccess()
  923. {
  924. $object = new \stdClass();
  925. $this->admin->expects($this->once())
  926. ->method('getObject')
  927. ->will($this->returnValue($object));
  928. $this->admin->expects($this->once())
  929. ->method('update')
  930. ->will($this->returnArgument(0));
  931. $this->admin->expects($this->once())
  932. ->method('isGranted')
  933. ->with($this->equalTo('EDIT'))
  934. ->will($this->returnValue(true));
  935. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  936. ->disableOriginalConstructor()
  937. ->getMock();
  938. $this->admin->expects($this->once())
  939. ->method('getForm')
  940. ->will($this->returnValue($form));
  941. $form->expects($this->once())
  942. ->method('isValid')
  943. ->will($this->returnValue(true));
  944. $this->admin->expects($this->once())
  945. ->method('getNormalizedIdentifier')
  946. ->with($this->equalTo($object))
  947. ->will($this->returnValue('foo_normalized'));
  948. $this->request->setMethod('POST');
  949. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  950. $response = $this->controller->editAction();
  951. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  952. $this->assertEquals(json_encode(array('result'=>'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  953. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  954. }
  955. public function testEditActionAjaxError()
  956. {
  957. $object = new \stdClass();
  958. $this->admin->expects($this->once())
  959. ->method('getObject')
  960. ->will($this->returnValue($object));
  961. $this->admin->expects($this->once())
  962. ->method('isGranted')
  963. ->with($this->equalTo('EDIT'))
  964. ->will($this->returnValue(true));
  965. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  966. ->disableOriginalConstructor()
  967. ->getMock();
  968. $this->admin->expects($this->once())
  969. ->method('getForm')
  970. ->will($this->returnValue($form));
  971. $form->expects($this->once())
  972. ->method('isValid')
  973. ->will($this->returnValue(false));
  974. $this->request->setMethod('POST');
  975. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  976. $formView = $this->getMock('Symfony\Component\Form\FormView');
  977. $form->expects($this->any())
  978. ->method('createView')
  979. ->will($this->returnValue($formView));
  980. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  981. $this->assertEquals($this->admin, $this->parameters['admin']);
  982. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  983. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  984. $this->assertEquals('edit', $this->parameters['action']);
  985. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  986. $this->assertEquals($object, $this->parameters['object']);
  987. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  988. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  989. }
  990. public function testEditActionWithPreview()
  991. {
  992. $object = new \stdClass();
  993. $this->admin->expects($this->once())
  994. ->method('getObject')
  995. ->will($this->returnValue($object));
  996. $this->admin->expects($this->once())
  997. ->method('isGranted')
  998. ->with($this->equalTo('EDIT'))
  999. ->will($this->returnValue(true));
  1000. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1001. ->disableOriginalConstructor()
  1002. ->getMock();
  1003. $this->admin->expects($this->once())
  1004. ->method('getForm')
  1005. ->will($this->returnValue($form));
  1006. $this->admin->expects($this->once())
  1007. ->method('supportsPreviewMode')
  1008. ->will($this->returnValue(true));
  1009. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1010. $form->expects($this->any())
  1011. ->method('createView')
  1012. ->will($this->returnValue($formView));
  1013. $form->expects($this->once())
  1014. ->method('isValid')
  1015. ->will($this->returnValue(true));
  1016. $this->request->setMethod('POST');
  1017. $this->request->request->set('btn_preview', 'Preview');
  1018. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  1019. $this->assertEquals($this->admin, $this->parameters['admin']);
  1020. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1021. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1022. $this->assertEquals('edit', $this->parameters['action']);
  1023. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1024. $this->assertEquals($object, $this->parameters['object']);
  1025. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1026. $this->assertEquals('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
  1027. }
  1028. public function testCreateActionAccessDenied()
  1029. {
  1030. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1031. $this->admin->expects($this->once())
  1032. ->method('isGranted')
  1033. ->with($this->equalTo('CREATE'))
  1034. ->will($this->returnValue(false));
  1035. $this->controller->createAction();
  1036. }
  1037. public function testCreateAction()
  1038. {
  1039. $this->admin->expects($this->once())
  1040. ->method('isGranted')
  1041. ->with($this->equalTo('CREATE'))
  1042. ->will($this->returnValue(true));
  1043. $object = new \stdClass();
  1044. $this->admin->expects($this->once())
  1045. ->method('getNewInstance')
  1046. ->will($this->returnValue($object));
  1047. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1048. ->disableOriginalConstructor()
  1049. ->getMock();
  1050. $this->admin->expects($this->once())
  1051. ->method('getForm')
  1052. ->will($this->returnValue($form));
  1053. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1054. $form->expects($this->any())
  1055. ->method('createView')
  1056. ->will($this->returnValue($formView));
  1057. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1058. $this->assertEquals($this->admin, $this->parameters['admin']);
  1059. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1060. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1061. $this->assertEquals('create', $this->parameters['action']);
  1062. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1063. $this->assertEquals($object, $this->parameters['object']);
  1064. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1065. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1066. }
  1067. public function testCreateActionSuccess()
  1068. {
  1069. $object = new \stdClass();
  1070. $this->admin->expects($this->exactly(2))
  1071. ->method('isGranted')
  1072. ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
  1073. if ($name != 'CREATE') {
  1074. return false;
  1075. }
  1076. if ($objectIn === null) {
  1077. return true;
  1078. }
  1079. return ($objectIn === $object);
  1080. }));
  1081. $this->admin->expects($this->once())
  1082. ->method('getNewInstance')
  1083. ->will($this->returnValue($object));
  1084. $this->admin->expects($this->once())
  1085. ->method('create')
  1086. ->will($this->returnArgument(0));
  1087. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1088. ->disableOriginalConstructor()
  1089. ->getMock();
  1090. $this->admin->expects($this->once())
  1091. ->method('getForm')
  1092. ->will($this->returnValue($form));
  1093. $form->expects($this->once())
  1094. ->method('isValid')
  1095. ->will($this->returnValue(true));
  1096. $this->expectTranslate('flash_create_success');
  1097. $this->request->setMethod('POST');
  1098. $response = $this->controller->createAction();
  1099. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1100. $this->assertSame(array('flash_create_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1101. $this->assertEquals('stdClass_edit', $response->getTargetUrl());
  1102. }
  1103. public function testCreateActionAccessDenied2()
  1104. {
  1105. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1106. $object = new \stdClass();
  1107. $this->admin->expects($this->any())
  1108. ->method('isGranted')
  1109. ->will($this->returnCallback(function ($name, $object = null) {
  1110. if ($name != 'CREATE') {
  1111. return false;
  1112. }
  1113. if ($object === null) {
  1114. return true;
  1115. }
  1116. return false;
  1117. }));
  1118. $this->admin->expects($this->once())
  1119. ->method('getNewInstance')
  1120. ->will($this->returnValue($object));
  1121. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1122. ->disableOriginalConstructor()
  1123. ->getMock();
  1124. $this->admin->expects($this->once())
  1125. ->method('getForm')
  1126. ->will($this->returnValue($form));
  1127. $form->expects($this->once())
  1128. ->method('isValid')
  1129. ->will($this->returnValue(true));
  1130. $this->request->setMethod('POST');
  1131. $this->controller->createAction();
  1132. }
  1133. public function testCreateActionError()
  1134. {
  1135. $this->admin->expects($this->once())
  1136. ->method('isGranted')
  1137. ->with($this->equalTo('CREATE'))
  1138. ->will($this->returnValue(true));
  1139. $object = new \stdClass();
  1140. $this->admin->expects($this->once())
  1141. ->method('getNewInstance')
  1142. ->will($this->returnValue($object));
  1143. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1144. ->disableOriginalConstructor()
  1145. ->getMock();
  1146. $this->admin->expects($this->once())
  1147. ->method('getForm')
  1148. ->will($this->returnValue($form));
  1149. $form->expects($this->once())
  1150. ->method('isValid')
  1151. ->will($this->returnValue(false));
  1152. $this->expectTranslate('flash_create_error');
  1153. $this->request->setMethod('POST');
  1154. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1155. $form->expects($this->any())
  1156. ->method('createView')
  1157. ->will($this->returnValue($formView));
  1158. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1159. $this->assertEquals($this->admin, $this->parameters['admin']);
  1160. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1161. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1162. $this->assertEquals('create', $this->parameters['action']);
  1163. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1164. $this->assertEquals($object, $this->parameters['object']);
  1165. $this->assertEquals(array('sonata_flash_error'=>array('flash_create_error')), $this->session->getFlashBag()->all());
  1166. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1167. }
  1168. public function testCreateActionAjaxSuccess()
  1169. {
  1170. $object = new \stdClass();
  1171. $this->admin->expects($this->exactly(2))
  1172. ->method('isGranted')
  1173. ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
  1174. if ($name != 'CREATE') {
  1175. return false;
  1176. }
  1177. if ($objectIn === null) {
  1178. return true;
  1179. }
  1180. return ($objectIn === $object);
  1181. }));
  1182. $this->admin->expects($this->once())
  1183. ->method('getNewInstance')
  1184. ->will($this->returnValue($object));
  1185. $this->admin->expects($this->once())
  1186. ->method('create')
  1187. ->will($this->returnArgument(0));
  1188. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1189. ->disableOriginalConstructor()
  1190. ->getMock();
  1191. $this->admin->expects($this->once())
  1192. ->method('getForm')
  1193. ->will($this->returnValue($form));
  1194. $form->expects($this->once())
  1195. ->method('isValid')
  1196. ->will($this->returnValue(true));
  1197. $this->admin->expects($this->once())
  1198. ->method('getNormalizedIdentifier')
  1199. ->with($this->equalTo($object))
  1200. ->will($this->returnValue('foo_normalized'));
  1201. $this->request->setMethod('POST');
  1202. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1203. $response = $this->controller->createAction();
  1204. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  1205. $this->assertEquals(json_encode(array('result'=>'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  1206. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1207. }
  1208. public function testCreateActionAjaxError()
  1209. {
  1210. $this->admin->expects($this->once())
  1211. ->method('isGranted')
  1212. ->with($this->equalTo('CREATE'))
  1213. ->will($this->returnValue(true));
  1214. $object = new \stdClass();
  1215. $this->admin->expects($this->once())
  1216. ->method('getNewInstance')
  1217. ->will($this->returnValue($object));
  1218. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1219. ->disableOriginalConstructor()
  1220. ->getMock();
  1221. $this->admin->expects($this->once())
  1222. ->method('getForm')
  1223. ->will($this->returnValue($form));
  1224. $form->expects($this->once())
  1225. ->method('isValid')
  1226. ->will($this->returnValue(false));
  1227. $this->request->setMethod('POST');
  1228. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1229. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1230. $form->expects($this->any())
  1231. ->method('createView')
  1232. ->will($this->returnValue($formView));
  1233. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1234. $this->assertEquals($this->admin, $this->parameters['admin']);
  1235. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  1236. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1237. $this->assertEquals('create', $this->parameters['action']);
  1238. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1239. $this->assertEquals($object, $this->parameters['object']);
  1240. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1241. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1242. }
  1243. public function testCreateActionWithPreview()
  1244. {
  1245. $this->admin->expects($this->once())
  1246. ->method('isGranted')
  1247. ->with($this->equalTo('CREATE'))
  1248. ->will($this->returnValue(true));
  1249. $object = new \stdClass();
  1250. $this->admin->expects($this->once())
  1251. ->method('getNewInstance')
  1252. ->will($this->returnValue($object));
  1253. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1254. ->disableOriginalConstructor()
  1255. ->getMock();
  1256. $this->admin->expects($this->once())
  1257. ->method('getForm')
  1258. ->will($this->returnValue($form));
  1259. $this->admin->expects($this->once())
  1260. ->method('supportsPreviewMode')
  1261. ->will($this->returnValue(true));
  1262. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1263. $form->expects($this->any())
  1264. ->method('createView')
  1265. ->will($this->returnValue($formView));
  1266. $form->expects($this->once())
  1267. ->method('isValid')
  1268. ->will($this->returnValue(true));
  1269. $this->request->setMethod('POST');
  1270. $this->request->request->set('btn_preview', 'Preview');
  1271. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1272. $this->assertEquals($this->admin, $this->parameters['admin']);
  1273. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1274. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1275. $this->assertEquals('create', $this->parameters['action']);
  1276. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1277. $this->assertEquals($object, $this->parameters['object']);
  1278. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1279. $this->assertEquals('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
  1280. }
  1281. public function testExportActionAccessDenied()
  1282. {
  1283. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1284. $this->admin->expects($this->once())
  1285. ->method('isGranted')
  1286. ->with($this->equalTo('EXPORT'))
  1287. ->will($this->returnValue(false));
  1288. $this->controller->exportAction($this->request);
  1289. }
  1290. public function testExportActionWrongFormat()
  1291. {
  1292. $this->setExpectedException('RuntimeException', 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`');
  1293. $this->admin->expects($this->once())
  1294. ->method('isGranted')
  1295. ->with($this->equalTo('EXPORT'))
  1296. ->will($this->returnValue(true));
  1297. $this->admin->expects($this->once())
  1298. ->method('getExportFormats')
  1299. ->will($this->returnValue(array('json')));
  1300. $this->admin->expects($this->once())
  1301. ->method('getClass')
  1302. ->will($this->returnValue('Foo'));
  1303. $this->request->query->set('format', 'csv');
  1304. $this->controller->exportAction($this->request);
  1305. }
  1306. public function testExportAction()
  1307. {
  1308. $this->admin->expects($this->once())
  1309. ->method('isGranted')
  1310. ->with($this->equalTo('EXPORT'))
  1311. ->will($this->returnValue(true));
  1312. $this->admin->expects($this->once())
  1313. ->method('getExportFormats')
  1314. ->will($this->returnValue(array('json')));
  1315. $dataSourceIterator = $this->getMock('Exporter\Source\SourceIteratorInterface');
  1316. $this->admin->expects($this->once())
  1317. ->method('getDataSourceIterator')
  1318. ->will($this->returnValue($dataSourceIterator));
  1319. $this->request->query->set('format', 'json');
  1320. $response = $this->controller->exportAction($this->request);
  1321. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  1322. $this->assertEquals(200, $response->getStatusCode());
  1323. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1324. }
  1325. public function testHistoryActionAccessDenied()
  1326. {
  1327. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1328. $this->admin->expects($this->once())
  1329. ->method('isGranted')
  1330. ->with($this->equalTo('EDIT'))
  1331. ->will($this->returnValue(false));
  1332. $this->controller->historyAction();
  1333. }
  1334. public function testHistoryActionNotFoundException()
  1335. {
  1336. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1337. $this->admin->expects($this->once())
  1338. ->method('isGranted')
  1339. ->with($this->equalTo('EDIT'))
  1340. ->will($this->returnValue(true));
  1341. $this->admin->expects($this->once())
  1342. ->method('getObject')
  1343. ->will($this->returnValue(false));
  1344. $this->controller->historyAction();
  1345. }
  1346. public function testHistoryActionNoReader()
  1347. {
  1348. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  1349. $this->request->query->set('id', 123);
  1350. $this->admin->expects($this->once())
  1351. ->method('isGranted')
  1352. ->with($this->equalTo('EDIT'))
  1353. ->will($this->returnValue(true));
  1354. $object = new \stdClass();
  1355. $this->admin->expects($this->once())
  1356. ->method('getObject')
  1357. ->will($this->returnValue($object));
  1358. $this->admin->expects($this->any())
  1359. ->method('getClass')
  1360. ->will($this->returnValue('Foo'));
  1361. $this->auditManager->expects($this->once())
  1362. ->method('hasReader')
  1363. ->with($this->equalTo('Foo'))
  1364. ->will($this->returnValue(false));
  1365. $this->controller->historyAction();
  1366. }
  1367. public function testHistoryAction()
  1368. {
  1369. $this->request->query->set('id', 123);
  1370. $this->admin->expects($this->once())
  1371. ->method('isGranted')
  1372. ->with($this->equalTo('EDIT'))
  1373. ->will($this->returnValue(true));
  1374. $object = new \stdClass();
  1375. $this->admin->expects($this->once())
  1376. ->method('getObject')
  1377. ->will($this->returnValue($object));
  1378. $this->admin->expects($this->any())
  1379. ->method('getClass')
  1380. ->will($this->returnValue('Foo'));
  1381. $this->auditManager->expects($this->once())
  1382. ->method('hasReader')
  1383. ->with($this->equalTo('Foo'))
  1384. ->will($this->returnValue(true));
  1385. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1386. $this->auditManager->expects($this->once())
  1387. ->method('getReader')
  1388. ->with($this->equalTo('Foo'))
  1389. ->will($this->returnValue($reader));
  1390. $reader->expects($this->once())
  1391. ->method('findRevisions')
  1392. ->with($this->equalTo('Foo'), $this->equalTo(123))
  1393. ->will($this->returnValue(array()));
  1394. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyAction());
  1395. $this->assertEquals($this->admin, $this->parameters['admin']);
  1396. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1397. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1398. $this->assertEquals('history', $this->parameters['action']);
  1399. $this->assertEquals(array(), $this->parameters['revisions']);
  1400. $this->assertEquals($object, $this->parameters['object']);
  1401. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1402. $this->assertEquals('SonataAdminBundle:CRUD:history.html.twig', $this->template);
  1403. }
  1404. public function testAclActionAclNotEnabled()
  1405. {
  1406. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'ACL are not enabled for this admin');
  1407. $this->controller->aclAction();
  1408. }
  1409. public function testAclActionNotFoundException()
  1410. {
  1411. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1412. $this->admin->expects($this->once())
  1413. ->method('isAclEnabled')
  1414. ->will($this->returnValue(true));
  1415. $this->admin->expects($this->once())
  1416. ->method('getObject')
  1417. ->will($this->returnValue(false));
  1418. $this->controller->aclAction();
  1419. }
  1420. public function testAclActionAccessDenied()
  1421. {
  1422. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1423. $this->admin->expects($this->once())
  1424. ->method('isAclEnabled')
  1425. ->will($this->returnValue(true));
  1426. $object = new \stdClass();
  1427. $this->admin->expects($this->once())
  1428. ->method('getObject')
  1429. ->will($this->returnValue($object));
  1430. $this->admin->expects($this->once())
  1431. ->method('isGranted')
  1432. ->with($this->equalTo('MASTER'), $this->equalTo($object))
  1433. ->will($this->returnValue(false));
  1434. $this->controller->aclAction();
  1435. }
  1436. public function testAclAction()
  1437. {
  1438. $this->request->query->set('id', 123);
  1439. $this->admin->expects($this->once())
  1440. ->method('isAclEnabled')
  1441. ->will($this->returnValue(true));
  1442. $object = new \stdClass();
  1443. $this->admin->expects($this->once())
  1444. ->method('getObject')
  1445. ->will($this->returnValue($object));
  1446. $this->admin->expects($this->any())
  1447. ->method('isGranted')
  1448. ->will($this->returnValue(true));
  1449. $this->adminObjectAclManipulator->expects($this->once())
  1450. ->method('getMaskBuilderClass')
  1451. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  1452. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1453. ->disableOriginalConstructor()
  1454. ->getMock();
  1455. $form->expects($this->once())
  1456. ->method('createView')
  1457. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  1458. $this->adminObjectAclManipulator->expects($this->once())
  1459. ->method('createForm')
  1460. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  1461. ->will($this->returnValue($form));
  1462. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  1463. ->disableOriginalConstructor()
  1464. ->getMock();
  1465. $aclSecurityHandler->expects($this->any())
  1466. ->method('getObjectPermissions')
  1467. ->will($this->returnValue(array()));
  1468. $this->admin->expects($this->any())
  1469. ->method('getSecurityHandler')
  1470. ->will($this->returnValue($aclSecurityHandler));
  1471. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction());
  1472. $this->assertEquals($this->admin, $this->parameters['admin']);
  1473. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1474. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1475. $this->assertEquals('acl', $this->parameters['action']);
  1476. $this->assertEquals(array(), $this->parameters['permissions']);
  1477. $this->assertEquals($object, $this->parameters['object']);
  1478. $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
  1479. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1480. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1481. $this->assertEquals('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
  1482. }
  1483. public function testAclActionInvalidUpdate()
  1484. {
  1485. $this->request->query->set('id', 123);
  1486. $this->admin->expects($this->once())
  1487. ->method('isAclEnabled')
  1488. ->will($this->returnValue(true));
  1489. $object = new \stdClass();
  1490. $this->admin->expects($this->once())
  1491. ->method('getObject')
  1492. ->will($this->returnValue($object));
  1493. $this->admin->expects($this->any())
  1494. ->method('isGranted')
  1495. ->will($this->returnValue(true));
  1496. $this->adminObjectAclManipulator->expects($this->once())
  1497. ->method('getMaskBuilderClass')
  1498. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  1499. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1500. ->disableOriginalConstructor()
  1501. ->getMock();
  1502. $form->expects($this->once())
  1503. ->method('isValid')
  1504. ->will($this->returnValue(false));
  1505. $form->expects($this->once())
  1506. ->method('createView')
  1507. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  1508. $this->adminObjectAclManipulator->expects($this->once())
  1509. ->method('createForm')
  1510. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  1511. ->will($this->returnValue($form));
  1512. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  1513. ->disableOriginalConstructor()
  1514. ->getMock();
  1515. $aclSecurityHandler->expects($this->any())
  1516. ->method('getObjectPermissions')
  1517. ->will($this->returnValue(array()));
  1518. $this->admin->expects($this->any())
  1519. ->method('getSecurityHandler')
  1520. ->will($this->returnValue($aclSecurityHandler));
  1521. $this->request->setMethod('POST');
  1522. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction());
  1523. $this->assertEquals($this->admin, $this->parameters['admin']);
  1524. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1525. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1526. $this->assertEquals('acl', $this->parameters['action']);
  1527. $this->assertEquals(array(), $this->parameters['permissions']);
  1528. $this->assertEquals($object, $this->parameters['object']);
  1529. $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
  1530. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1531. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1532. $this->assertEquals('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
  1533. }
  1534. public function testAclActionSuccessfulUpdate()
  1535. {
  1536. $this->request->query->set('id', 123);
  1537. $this->admin->expects($this->once())
  1538. ->method('isAclEnabled')
  1539. ->will($this->returnValue(true));
  1540. $object = new \stdClass();
  1541. $this->admin->expects($this->once())
  1542. ->method('getObject')
  1543. ->will($this->returnValue($object));
  1544. $this->admin->expects($this->any())
  1545. ->method('isGranted')
  1546. ->will($this->returnValue(true));
  1547. $this->adminObjectAclManipulator->expects($this->once())
  1548. ->method('getMaskBuilderClass')
  1549. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  1550. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1551. ->disableOriginalConstructor()
  1552. ->getMock();
  1553. $form->expects($this->any())
  1554. ->method('createView')
  1555. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  1556. $form->expects($this->once())
  1557. ->method('isValid')
  1558. ->will($this->returnValue(true));
  1559. $this->adminObjectAclManipulator->expects($this->once())
  1560. ->method('createForm')
  1561. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  1562. ->will($this->returnValue($form));
  1563. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  1564. ->disableOriginalConstructor()
  1565. ->getMock();
  1566. $aclSecurityHandler->expects($this->any())
  1567. ->method('getObjectPermissions')
  1568. ->will($this->returnValue(array()));
  1569. $this->admin->expects($this->any())
  1570. ->method('getSecurityHandler')
  1571. ->will($this->returnValue($aclSecurityHandler));
  1572. $this->request->setMethod('POST');
  1573. $response = $this->controller->aclAction();
  1574. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1575. $this->assertSame(array('flash_acl_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1576. $this->assertEquals('stdClass_acl', $response->getTargetUrl());
  1577. }
  1578. public function testHistoryViewRevisionActionAccessDenied()
  1579. {
  1580. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1581. $this->admin->expects($this->once())
  1582. ->method('isGranted')
  1583. ->with($this->equalTo('EDIT'))
  1584. ->will($this->returnValue(false));
  1585. $this->controller->historyViewRevisionAction();
  1586. }
  1587. public function testHistoryViewRevisionActionNotFoundException()
  1588. {
  1589. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id : 123');
  1590. $this->request->query->set('id', 123);
  1591. $this->admin->expects($this->once())
  1592. ->method('isGranted')
  1593. ->with($this->equalTo('EDIT'))
  1594. ->will($this->returnValue(true));
  1595. $this->admin->expects($this->once())
  1596. ->method('getObject')
  1597. ->will($this->returnValue(false));
  1598. $this->controller->historyViewRevisionAction();
  1599. }
  1600. public function testHistoryViewRevisionActionNoReader()
  1601. {
  1602. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  1603. $this->request->query->set('id', 123);
  1604. $this->admin->expects($this->once())
  1605. ->method('isGranted')
  1606. ->with($this->equalTo('EDIT'))
  1607. ->will($this->returnValue(true));
  1608. $object = new \stdClass();
  1609. $this->admin->expects($this->once())
  1610. ->method('getObject')
  1611. ->will($this->returnValue($object));
  1612. $this->admin->expects($this->any())
  1613. ->method('getClass')
  1614. ->will($this->returnValue('Foo'));
  1615. $this->auditManager->expects($this->once())
  1616. ->method('hasReader')
  1617. ->with($this->equalTo('Foo'))
  1618. ->will($this->returnValue(false));
  1619. $this->controller->historyViewRevisionAction();
  1620. }
  1621. public function testHistoryViewRevisionActionNotFoundRevision()
  1622. {
  1623. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`');
  1624. $this->request->query->set('id', 123);
  1625. $this->admin->expects($this->once())
  1626. ->method('isGranted')
  1627. ->with($this->equalTo('EDIT'))
  1628. ->will($this->returnValue(true));
  1629. $object = new \stdClass();
  1630. $this->admin->expects($this->once())
  1631. ->method('getObject')
  1632. ->will($this->returnValue($object));
  1633. $this->admin->expects($this->any())
  1634. ->method('getClass')
  1635. ->will($this->returnValue('Foo'));
  1636. $this->auditManager->expects($this->once())
  1637. ->method('hasReader')
  1638. ->with($this->equalTo('Foo'))
  1639. ->will($this->returnValue(true));
  1640. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1641. $this->auditManager->expects($this->once())
  1642. ->method('getReader')
  1643. ->with($this->equalTo('Foo'))
  1644. ->will($this->returnValue($reader));
  1645. $reader->expects($this->once())
  1646. ->method('find')
  1647. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  1648. ->will($this->returnValue(null));
  1649. $this->controller->historyViewRevisionAction(123, 456);
  1650. }
  1651. public function testHistoryViewRevisionAction()
  1652. {
  1653. $this->request->query->set('id', 123);
  1654. $this->admin->expects($this->once())
  1655. ->method('isGranted')
  1656. ->with($this->equalTo('EDIT'))
  1657. ->will($this->returnValue(true));
  1658. $object = new \stdClass();
  1659. $this->admin->expects($this->once())
  1660. ->method('getObject')
  1661. ->will($this->returnValue($object));
  1662. $this->admin->expects($this->any())
  1663. ->method('getClass')
  1664. ->will($this->returnValue('Foo'));
  1665. $this->auditManager->expects($this->once())
  1666. ->method('hasReader')
  1667. ->with($this->equalTo('Foo'))
  1668. ->will($this->returnValue(true));
  1669. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1670. $this->auditManager->expects($this->once())
  1671. ->method('getReader')
  1672. ->with($this->equalTo('Foo'))
  1673. ->will($this->returnValue($reader));
  1674. $objectRevision = new \stdClass();
  1675. $objectRevision->revision = 456;
  1676. $reader->expects($this->once())
  1677. ->method('find')
  1678. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  1679. ->will($this->returnValue($objectRevision));
  1680. $this->admin->expects($this->once())
  1681. ->method('setSubject')
  1682. ->with($this->equalTo($objectRevision))
  1683. ->will($this->returnValue(null));
  1684. $fieldDescriptionCollection = new FieldDescriptionCollection();
  1685. $this->admin->expects($this->once())
  1686. ->method('getShow')
  1687. ->will($this->returnValue($fieldDescriptionCollection));
  1688. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyViewRevisionAction(123, 456));
  1689. $this->assertEquals($this->admin, $this->parameters['admin']);
  1690. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1691. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1692. $this->assertEquals('show', $this->parameters['action']);
  1693. $this->assertEquals($objectRevision, $this->parameters['object']);
  1694. $this->assertEquals($fieldDescriptionCollection, $this->parameters['elements']);
  1695. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1696. $this->assertEquals('SonataAdminBundle:CRUD:show.html.twig', $this->template);
  1697. }
  1698. public function testBatchActionWrongMethod()
  1699. {
  1700. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'Invalid request type "GET", POST expected');
  1701. $this->controller->batchAction();
  1702. }
  1703. public function testBatchActionActionNotDefined()
  1704. {
  1705. $this->setExpectedException('RuntimeException', 'The `foo` batch action is not defined');
  1706. $batchActions = array();
  1707. $this->admin->expects($this->once())
  1708. ->method('getBatchActions')
  1709. ->will($this->returnValue($batchActions));
  1710. $this->request->setMethod('POST');
  1711. $this->request->request->set('data', json_encode(array('action'=>'foo', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  1712. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1713. $this->controller->batchAction();
  1714. }
  1715. public function testBatchActionActionInvalidCsrfToken()
  1716. {
  1717. $this->request->setMethod('POST');
  1718. $this->request->request->set('data', json_encode(array('action'=>'foo', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  1719. $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
  1720. try {
  1721. $this->controller->batchAction();
  1722. } catch (HttpException $e) {
  1723. $this->assertEquals('The csrf token is not valid, CSRF attack?', $e->getMessage());
  1724. $this->assertEquals(400, $e->getStatusCode());
  1725. }
  1726. }
  1727. public function testBatchActionMethodNotExist()
  1728. {
  1729. $this->setExpectedException('RuntimeException', 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be created');
  1730. $batchActions = array('foo'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1731. $this->admin->expects($this->once())
  1732. ->method('getBatchActions')
  1733. ->will($this->returnValue($batchActions));
  1734. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1735. $this->admin->expects($this->once())
  1736. ->method('getDatagrid')
  1737. ->will($this->returnValue($datagrid));
  1738. $this->request->setMethod('POST');
  1739. $this->request->request->set('data', json_encode(array('action'=>'foo', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  1740. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1741. $this->controller->batchAction();
  1742. }
  1743. public function testBatchActionWithoutConfirmation()
  1744. {
  1745. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1746. $this->admin->expects($this->once())
  1747. ->method('getBatchActions')
  1748. ->will($this->returnValue($batchActions));
  1749. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1750. $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  1751. $datagrid->expects($this->once())
  1752. ->method('getQuery')
  1753. ->will($this->returnValue($query));
  1754. $this->admin->expects($this->once())
  1755. ->method('getDatagrid')
  1756. ->will($this->returnValue($datagrid));
  1757. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1758. $this->admin->expects($this->once())
  1759. ->method('isGranted')
  1760. ->with($this->equalTo('DELETE'))
  1761. ->will($this->returnValue(true));
  1762. $this->admin->expects($this->any())
  1763. ->method('getModelManager')
  1764. ->will($this->returnValue($modelManager));
  1765. $this->admin->expects($this->any())
  1766. ->method('getClass')
  1767. ->will($this->returnValue('Foo'));
  1768. $modelManager->expects($this->once())
  1769. ->method('addIdentifiersToQuery')
  1770. ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
  1771. ->will($this->returnValue(true));
  1772. $this->request->setMethod('POST');
  1773. $this->request->request->set('data', json_encode(array('action'=>'delete', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  1774. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1775. $result = $this->controller->batchAction();
  1776. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  1777. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1778. $this->assertEquals('list?', $result->getTargetUrl());
  1779. }
  1780. public function testBatchActionWithoutConfirmation2()
  1781. {
  1782. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1783. $this->admin->expects($this->once())
  1784. ->method('getBatchActions')
  1785. ->will($this->returnValue($batchActions));
  1786. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1787. $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  1788. $datagrid->expects($this->once())
  1789. ->method('getQuery')
  1790. ->will($this->returnValue($query));
  1791. $this->admin->expects($this->once())
  1792. ->method('getDatagrid')
  1793. ->will($this->returnValue($datagrid));
  1794. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1795. $this->admin->expects($this->once())
  1796. ->method('isGranted')
  1797. ->with($this->equalTo('DELETE'))
  1798. ->will($this->returnValue(true));
  1799. $this->admin->expects($this->any())
  1800. ->method('getModelManager')
  1801. ->will($this->returnValue($modelManager));
  1802. $this->admin->expects($this->any())
  1803. ->method('getClass')
  1804. ->will($this->returnValue('Foo'));
  1805. $modelManager->expects($this->once())
  1806. ->method('addIdentifiersToQuery')
  1807. ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
  1808. ->will($this->returnValue(true));
  1809. $this->request->setMethod('POST');
  1810. $this->request->request->set('action', 'delete');
  1811. $this->request->request->set('idx', array('123', '456'));
  1812. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1813. $result = $this->controller->batchAction();
  1814. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  1815. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1816. $this->assertEquals('list?', $result->getTargetUrl());
  1817. }
  1818. public function testBatchActionWithConfirmation()
  1819. {
  1820. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => true));
  1821. $this->admin->expects($this->once())
  1822. ->method('getBatchActions')
  1823. ->will($this->returnValue($batchActions));
  1824. $this->admin->expects($this->once())
  1825. ->method('getTranslationLabel')
  1826. ->with($this->equalTo('delete'), $this->equalTo('action'))
  1827. ->will($this->returnValue('delete_action'));
  1828. $data = array('action'=>'delete', 'idx'=>array('123', '456'), 'all_elements'=>false);
  1829. $this->request->setMethod('POST');
  1830. $this->request->request->set('data', json_encode($data));
  1831. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1832. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1833. $this->admin->expects($this->once())
  1834. ->method('getDatagrid')
  1835. ->will($this->returnValue($datagrid));
  1836. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1837. ->disableOriginalConstructor()
  1838. ->getMock();
  1839. $form->expects($this->once())
  1840. ->method('createView')
  1841. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  1842. $datagrid->expects($this->once())
  1843. ->method('getForm')
  1844. ->will($this->returnValue($form));
  1845. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->batchAction());
  1846. $this->assertEquals($this->admin, $this->parameters['admin']);
  1847. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1848. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1849. $this->assertEquals('list', $this->parameters['action']);
  1850. $this->assertEquals($datagrid, $this->parameters['datagrid']);
  1851. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1852. $this->assertEquals($data, $this->parameters['data']);
  1853. $this->assertEquals('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
  1854. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1855. $this->assertEquals('SonataAdminBundle:CRUD:batch_confirmation.html.twig', $this->template);
  1856. }
  1857. public function testBatchActionNonRelevantAction()
  1858. {
  1859. $controller = new BatchAdminController();
  1860. $controller->setContainer($this->container);
  1861. $batchActions = array('foo'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1862. $this->admin->expects($this->once())
  1863. ->method('getBatchActions')
  1864. ->will($this->returnValue($batchActions));
  1865. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1866. $this->admin->expects($this->once())
  1867. ->method('getDatagrid')
  1868. ->will($this->returnValue($datagrid));
  1869. $this->request->setMethod('POST');
  1870. $this->request->request->set('action', 'foo');
  1871. $this->request->request->set('idx', array('789'));
  1872. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1873. $result = $controller->batchAction();
  1874. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  1875. $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
  1876. $this->assertEquals('list?', $result->getTargetUrl());
  1877. }
  1878. public function testBatchActionNonRelevantAction2()
  1879. {
  1880. $controller = new BatchAdminController();
  1881. $controller->setContainer($this->container);
  1882. $batchActions = array('foo'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1883. $this->admin->expects($this->once())
  1884. ->method('getBatchActions')
  1885. ->will($this->returnValue($batchActions));
  1886. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1887. $this->admin->expects($this->once())
  1888. ->method('getDatagrid')
  1889. ->will($this->returnValue($datagrid));
  1890. $this->request->setMethod('POST');
  1891. $this->request->request->set('action', 'foo');
  1892. $this->request->request->set('idx', array('999'));
  1893. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1894. $result = $controller->batchAction();
  1895. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  1896. $this->assertSame(array('flash_foo_error'), $this->session->getFlashBag()->get('sonata_flash_info'));
  1897. $this->assertEquals('list?', $result->getTargetUrl());
  1898. }
  1899. public function testBatchActionNoItems()
  1900. {
  1901. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => true));
  1902. $this->admin->expects($this->once())
  1903. ->method('getBatchActions')
  1904. ->will($this->returnValue($batchActions));
  1905. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1906. $this->admin->expects($this->once())
  1907. ->method('getDatagrid')
  1908. ->will($this->returnValue($datagrid));
  1909. $this->request->setMethod('POST');
  1910. $this->request->request->set('action', 'delete');
  1911. $this->request->request->set('idx', array());
  1912. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1913. $result = $this->controller->batchAction();
  1914. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  1915. $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
  1916. $this->assertEquals('list?', $result->getTargetUrl());
  1917. }
  1918. public function testBatchActionNoItemsEmptyQuery()
  1919. {
  1920. $controller = new BatchAdminController();
  1921. $controller->setContainer($this->container);
  1922. $batchActions = array('bar'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1923. $this->admin->expects($this->once())
  1924. ->method('getBatchActions')
  1925. ->will($this->returnValue($batchActions));
  1926. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1927. $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  1928. $datagrid->expects($this->once())
  1929. ->method('getQuery')
  1930. ->will($this->returnValue($query));
  1931. $this->admin->expects($this->once())
  1932. ->method('getDatagrid')
  1933. ->will($this->returnValue($datagrid));
  1934. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  1935. $this->admin->expects($this->any())
  1936. ->method('getModelManager')
  1937. ->will($this->returnValue($modelManager));
  1938. $this->admin->expects($this->any())
  1939. ->method('getClass')
  1940. ->will($this->returnValue('Foo'));
  1941. $this->request->setMethod('POST');
  1942. $this->request->request->set('action', 'bar');
  1943. $this->request->request->set('idx', array());
  1944. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1945. $result = $controller->batchAction();
  1946. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $result);
  1947. $this->assertEquals('batchActionBar executed', $result->getContent());
  1948. }
  1949. public function getCsrfProvider()
  1950. {
  1951. return $this->csrfProvider;
  1952. }
  1953. private function expectTranslate()
  1954. {
  1955. $args = func_get_args();
  1956. // creates equalTo of all arguments passed to this function
  1957. $phpunit = $this; // PHP 5.3 compatibility
  1958. $argsCheck = array_map(function ($item) use ($phpunit) {
  1959. return $phpunit->equalTo($item);
  1960. }, func_get_args());
  1961. $mock = $this->admin->expects($this->once())->method('trans');
  1962. // passes all arguments to the 'with' of the $admin->trans method
  1963. $mock = call_user_func_array(array($mock, 'with'), $argsCheck);
  1964. $mock->will($this->returnValue($args[0]));
  1965. }
  1966. }