CRUDControllerTest.php 115 KB

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