CRUDControllerTest.php 109 KB

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