CRUDControllerTest.php 137 KB

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