CRUDControllerTest.php 89 KB

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