CRUDControllerTest.php 86 KB

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