CRUDControllerTest.php 66 KB

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