CRUDControllerTest.php 116 KB

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