CRUDControllerTest.php 86 KB

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