CRUDControllerTest.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. /**
  23. * Test for CRUDController
  24. *
  25. * @author Andrej Hudec <pulzarraider@gmail.com>
  26. */
  27. class CRUDControllerTest extends \PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * @var CRUDController
  31. */
  32. private $controller;
  33. /**
  34. * @var Request
  35. */
  36. private $request;
  37. /**
  38. * @var Sonata\AdminBundle\Admin\AdminInterface
  39. */
  40. private $admin;
  41. /**
  42. * @var Pool
  43. */
  44. private $pool;
  45. /**
  46. * @var array
  47. */
  48. private $parameters;
  49. /**
  50. * @var Session
  51. */
  52. private $session;
  53. /**
  54. * @var Sonata\AdminBundle\Model\AuditManager
  55. */
  56. private $auditManager;
  57. /**
  58. * {@inheritDoc}
  59. */
  60. protected function setUp()
  61. {
  62. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  63. $this->request = new Request();
  64. $this->pool = new Pool($container, 'title', 'logo.png');
  65. $this->request->attributes->set('_sonata_admin', 'foo.admin');
  66. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  67. $this->parameters = array();
  68. // php 5.3 BC
  69. $params = &$this->parameters;
  70. $templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine', array(), array($container, array()));
  71. $templating->expects($this->any())
  72. ->method('renderResponse')
  73. ->will($this->returnCallback(function($view, array $parameters = array(), Response $response = null) use (&$params) {
  74. if (null === $response) {
  75. $response = new Response();
  76. }
  77. $params = $parameters;
  78. return $response;
  79. }));
  80. $this->session = new Session(new MockArraySessionStorage());
  81. // php 5.3 BC
  82. $pool = $this->pool;
  83. $request = $this->request;
  84. $admin = $this->admin;
  85. $session = $this->session;
  86. $twig = $this->getMockBuilder('Twig_Environment')
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $twigRenderer = $this->getMock('Symfony\Bridge\Twig\Form\TwigRendererInterface');
  90. $formExtension = new FormExtension($twigRenderer);
  91. $twig->expects($this->any())
  92. ->method('getExtension')
  93. ->will($this->returnCallback(function($name) use ($formExtension) {
  94. switch ($name) {
  95. case 'form':
  96. return $formExtension;
  97. }
  98. return null;
  99. }));
  100. $exporter = $this->getMock('Sonata\AdminBundle\Export\Exporter');
  101. $exporter->expects($this->any())
  102. ->method('getResponse')
  103. ->will($this->returnValue(new StreamedResponse()));
  104. $this->auditManager = $this->getMockBuilder('Sonata\AdminBundle\Model\AuditManager')
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. // php 5.3 BC
  108. $auditManager = $this->auditManager;
  109. $container->expects($this->any())
  110. ->method('get')
  111. ->will($this->returnCallback(function($id) use ($pool, $request, $admin, $templating, $twig, $session, $exporter, $auditManager) {
  112. switch ($id) {
  113. case 'sonata.admin.pool':
  114. return $pool;
  115. case 'request':
  116. return $request;
  117. case 'foo.admin':
  118. return $admin;
  119. case 'templating':
  120. return $templating;
  121. case 'twig':
  122. return $twig;
  123. case 'session':
  124. return $session;
  125. case 'sonata.admin.exporter':
  126. return $exporter;
  127. case 'sonata.admin.audit.manager':
  128. return $auditManager;
  129. }
  130. return null;
  131. }));
  132. $this->admin->expects($this->any())
  133. ->method('getTemplate')
  134. ->will($this->returnCallback(function($name) {
  135. switch ($name) {
  136. case 'ajax':
  137. return 'SonataAdminBundle::ajax_layout.html.twig';
  138. case 'layout':
  139. return 'SonataAdminBundle::standard_layout.html.twig';
  140. case 'show':
  141. return 'SonataAdminBundle:CRUD:show.html.twig';
  142. case 'edit':
  143. return 'SonataAdminBundle:CRUD:edit.html.twig';
  144. }
  145. return null;
  146. }));
  147. $this->admin->expects($this->any())
  148. ->method('getIdParameter')
  149. ->will($this->returnValue('id'));
  150. $this->admin->expects($this->any())
  151. ->method('generateUrl')
  152. ->will($this->returnCallback(function($name, array $parameters = array(), $absolute = false) {
  153. $result = $name;
  154. if (!empty($parameters)) {
  155. $result .= '?'.http_build_query($parameters);
  156. }
  157. return $result;
  158. }));
  159. $this->admin->expects($this->any())
  160. ->method('generateObjectUrl')
  161. ->will($this->returnCallback(function($name, $object, array $parameters = array(), $absolute = false) {
  162. $result = get_class($object).'_'.$name;
  163. if (!empty($parameters)) {
  164. $result .= '?'.http_build_query($parameters);
  165. }
  166. return $result;
  167. }));
  168. $this->controller = new CRUDController();
  169. $this->controller->setContainer($container);
  170. }
  171. public function testRenderJson1()
  172. {
  173. $data = array('example'=>'123', 'foo'=>'bar');
  174. $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
  175. $response = $this->controller->renderJson($data);
  176. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  177. $this->assertEquals(json_encode($data), $response->getContent());
  178. }
  179. public function testRenderJson2()
  180. {
  181. $data = array('example'=>'123', 'foo'=>'bar');
  182. $this->request->headers->set('Content-Type', 'multipart/form-data');
  183. $response = $this->controller->renderJson($data);
  184. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  185. $this->assertEquals(json_encode($data), $response->getContent());
  186. }
  187. public function testRenderJsonAjax()
  188. {
  189. $data = array('example'=>'123', 'foo'=>'bar');
  190. $this->request->attributes->set('_xml_http_request', true);
  191. $this->request->headers->set('Content-Type', 'multipart/form-data');
  192. $response = $this->controller->renderJson($data);
  193. $this->assertEquals($response->headers->get('Content-Type'), 'text/plain');
  194. $this->assertEquals(json_encode($data), $response->getContent());
  195. }
  196. public function testIsXmlHttpRequest()
  197. {
  198. $this->assertFalse($this->controller->isXmlHttpRequest());
  199. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  200. $this->assertTrue($this->controller->isXmlHttpRequest());
  201. $this->request->headers->remove('X-Requested-With');
  202. $this->assertFalse($this->controller->isXmlHttpRequest());
  203. $this->request->attributes->set('_xml_http_request', true);
  204. $this->assertTrue($this->controller->isXmlHttpRequest());
  205. }
  206. public function testConfigure()
  207. {
  208. $uniqueId = '';
  209. $this->admin->expects($this->once())
  210. ->method('setUniqid')
  211. ->will($this->returnCallback(function($uniqid) use (&$uniqueId) {
  212. $uniqueId = $uniqid;
  213. }));
  214. $this->request->query->set('uniqid', 123456);
  215. $this->controller->configure();
  216. $this->assertEquals(123456, $uniqueId);
  217. $this->assertAttributeEquals($this->admin, 'admin', $this->controller);
  218. }
  219. public function testConfigureChild()
  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->admin->expects($this->once())
  228. ->method('isChild')
  229. ->will($this->returnValue(true));
  230. $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  231. $this->admin->expects($this->once())
  232. ->method('getParent')
  233. ->will($this->returnValue($adminParent));
  234. $this->request->query->set('uniqid', 123456);
  235. $this->controller->configure();
  236. $this->assertEquals(123456, $uniqueId);
  237. $this->assertAttributeEquals($adminParent, 'admin', $this->controller);
  238. }
  239. public function testConfigureWithException()
  240. {
  241. $this->setExpectedException('RuntimeException', 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`');
  242. $this->request->attributes->remove('_sonata_admin');
  243. $this->controller->configure();
  244. }
  245. public function testConfigureWithException2()
  246. {
  247. $this->setExpectedException('RuntimeException', 'Unable to find the admin class related to the current controller (Sonata\AdminBundle\Controller\CRUDController)');
  248. $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
  249. $this->controller->configure();
  250. }
  251. public function testGetBaseTemplate()
  252. {
  253. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->controller->getBaseTemplate());
  254. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  255. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->controller->getBaseTemplate());
  256. $this->request->headers->remove('X-Requested-With');
  257. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->controller->getBaseTemplate());
  258. $this->request->attributes->set('_xml_http_request', true);
  259. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->controller->getBaseTemplate());
  260. }
  261. public function testRender()
  262. {
  263. $this->parameters = array();
  264. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array()));
  265. $this->assertEquals($this->admin, $this->parameters['admin']);
  266. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  267. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  268. }
  269. public function testRenderWithResponse()
  270. {
  271. $this->parameters = array();
  272. $response = $response = new Response();
  273. $response->headers->set('X-foo', 'bar');
  274. $responseResult = $this->controller->render('FooAdminBundle::foo.html.twig', array(), $response);
  275. $this->assertEquals($response, $responseResult);
  276. $this->assertEquals('bar', $responseResult->headers->get('X-foo'));
  277. $this->assertEquals($this->admin, $this->parameters['admin']);
  278. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  279. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  280. }
  281. public function testRenderCustomParams()
  282. {
  283. $this->parameters = array();
  284. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo'=>'bar')));
  285. $this->assertEquals($this->admin, $this->parameters['admin']);
  286. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  287. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  288. $this->assertEquals('bar', $this->parameters['foo']);
  289. }
  290. public function testRenderAjax()
  291. {
  292. $this->parameters = array();
  293. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  294. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo'=>'bar')));
  295. $this->assertEquals($this->admin, $this->parameters['admin']);
  296. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  297. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  298. $this->assertEquals('bar', $this->parameters['foo']);
  299. }
  300. public function testListActionAccessDenied()
  301. {
  302. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  303. $this->admin->expects($this->once())
  304. ->method('isGranted')
  305. ->with($this->equalTo('LIST'))
  306. ->will($this->returnValue(false));
  307. $this->controller->listAction();
  308. }
  309. public function testListAction()
  310. {
  311. $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  312. $this->admin->expects($this->once())
  313. ->method('isGranted')
  314. ->with($this->equalTo('LIST'))
  315. ->will($this->returnValue(true));
  316. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  317. ->disableOriginalConstructor()
  318. ->getMock();
  319. $form->expects($this->once())
  320. ->method('createView')
  321. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  322. $this->admin->expects($this->once())
  323. ->method('getDatagrid')
  324. ->will($this->returnValue($datagrid));
  325. $datagrid->expects($this->once())
  326. ->method('getForm')
  327. ->will($this->returnValue($form));
  328. $this->parameters = array();
  329. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->listAction());
  330. $this->assertEquals($this->admin, $this->parameters['admin']);
  331. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  332. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  333. $this->assertEquals('list', $this->parameters['action']);
  334. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  335. $this->assertInstanceOf('Sonata\AdminBundle\Datagrid\DatagridInterface', $this->parameters['datagrid']);
  336. $this->assertEquals('', $this->parameters['csrf_token']);
  337. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  338. }
  339. public function testBatchActionDeleteAccessDenied()
  340. {
  341. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  342. $this->admin->expects($this->once())
  343. ->method('isGranted')
  344. ->with($this->equalTo('DELETE'))
  345. ->will($this->returnValue(false));
  346. $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  347. }
  348. public function testBatchActionDelete()
  349. {
  350. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  351. $this->admin->expects($this->once())
  352. ->method('isGranted')
  353. ->with($this->equalTo('DELETE'))
  354. ->will($this->returnValue(true));
  355. $this->admin->expects($this->once())
  356. ->method('getModelManager')
  357. ->will($this->returnValue($modelManager));
  358. $this->admin->expects($this->once())
  359. ->method('getFilterParameters')
  360. ->will($this->returnValue(array('foo'=>'bar')));
  361. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  362. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  363. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  364. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  365. }
  366. public function testBatchActionDeleteWithModelManagerException()
  367. {
  368. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  369. $modelManager->expects($this->once())
  370. ->method('batchDelete')
  371. ->will($this->returnCallback(function() {
  372. throw new ModelManagerException();
  373. }));
  374. $this->admin->expects($this->once())
  375. ->method('getModelManager')
  376. ->will($this->returnValue($modelManager));
  377. $this->admin->expects($this->once())
  378. ->method('getFilterParameters')
  379. ->will($this->returnValue(array('foo'=>'bar')));
  380. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  381. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  382. $this->assertSame(array('flash_batch_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  383. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  384. }
  385. public function testShowActionNotFoundException()
  386. {
  387. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  388. $this->admin->expects($this->once())
  389. ->method('getObject')
  390. ->will($this->returnValue(false));
  391. $this->controller->showAction();
  392. }
  393. public function testShowActionAccessDenied()
  394. {
  395. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  396. $this->admin->expects($this->once())
  397. ->method('getObject')
  398. ->will($this->returnValue(new \stdClass()));
  399. $this->admin->expects($this->once())
  400. ->method('isGranted')
  401. ->with($this->equalTo('VIEW'))
  402. ->will($this->returnValue(false));
  403. $this->controller->showAction();
  404. }
  405. public function testShowAction()
  406. {
  407. $object = new \stdClass();
  408. $this->admin->expects($this->once())
  409. ->method('getObject')
  410. ->will($this->returnValue($object));
  411. $this->admin->expects($this->once())
  412. ->method('isGranted')
  413. ->with($this->equalTo('VIEW'))
  414. ->will($this->returnValue(true));
  415. $show = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
  416. $this->admin->expects($this->once())
  417. ->method('getShow')
  418. ->will($this->returnValue($show));
  419. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->showAction());
  420. $this->assertEquals($this->admin, $this->parameters['admin']);
  421. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  422. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  423. $this->assertEquals('show', $this->parameters['action']);
  424. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $this->parameters['elements']);
  425. $this->assertEquals($object, $this->parameters['object']);
  426. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  427. }
  428. /**
  429. * @dataProvider getRedirectToTests
  430. */
  431. public function testRedirectTo($expected, $queryParams, $hasActiveSubclass)
  432. {
  433. $this->admin->expects($this->any())
  434. ->method('hasActiveSubclass')
  435. ->will($this->returnValue($hasActiveSubclass));
  436. $object = new \stdClass();
  437. foreach ($queryParams as $key => $value) {
  438. $this->request->query->set($key, $value);
  439. }
  440. $response = $this->controller->redirectTo($object);
  441. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  442. $this->assertEquals($expected, $response->getTargetUrl());
  443. }
  444. public function getRedirectToTests()
  445. {
  446. return array(
  447. array('stdClass_edit', array(), false),
  448. array('list', array('btn_update_and_list'=>true), false),
  449. array('list', array('btn_create_and_list'=>true), false),
  450. array('create', array('btn_create_and_create'=>true), false),
  451. array('create?subclass=foo', array('btn_create_and_create'=>true, 'subclass'=>'foo'), true),
  452. );
  453. }
  454. public function testAddFlash()
  455. {
  456. $this->controller->addFlash('foo', 'bar');
  457. $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
  458. }
  459. public function testDeleteActionNotFoundException()
  460. {
  461. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  462. $this->admin->expects($this->once())
  463. ->method('getObject')
  464. ->will($this->returnValue(false));
  465. $this->controller->deleteAction(1);
  466. }
  467. public function testDeleteActionAccessDenied()
  468. {
  469. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  470. $this->admin->expects($this->once())
  471. ->method('getObject')
  472. ->will($this->returnValue(new \stdClass()));
  473. $this->admin->expects($this->once())
  474. ->method('isGranted')
  475. ->with($this->equalTo('DELETE'))
  476. ->will($this->returnValue(false));
  477. $this->controller->deleteAction(1);
  478. }
  479. public function testDeleteAction()
  480. {
  481. $object = new \stdClass();
  482. $this->admin->expects($this->once())
  483. ->method('getObject')
  484. ->will($this->returnValue($object));
  485. $this->admin->expects($this->once())
  486. ->method('isGranted')
  487. ->with($this->equalTo('DELETE'))
  488. ->will($this->returnValue(true));
  489. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  490. $this->assertEquals($this->admin, $this->parameters['admin']);
  491. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  492. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  493. $this->assertEquals('delete', $this->parameters['action']);
  494. $this->assertEquals($object, $this->parameters['object']);
  495. $this->assertEquals('', $this->parameters['csrf_token']);
  496. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  497. }
  498. public function testDeleteActionAjaxSuccess1()
  499. {
  500. $object = new \stdClass();
  501. $this->admin->expects($this->once())
  502. ->method('getObject')
  503. ->will($this->returnValue($object));
  504. $this->admin->expects($this->once())
  505. ->method('isGranted')
  506. ->with($this->equalTo('DELETE'))
  507. ->will($this->returnValue(true));
  508. $this->request->setMethod('DELETE');
  509. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  510. $response = $this->controller->deleteAction(1);
  511. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  512. $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
  513. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  514. }
  515. public function testDeleteActionAjaxSuccess2()
  516. {
  517. $object = new \stdClass();
  518. $this->admin->expects($this->once())
  519. ->method('getObject')
  520. ->will($this->returnValue($object));
  521. $this->admin->expects($this->once())
  522. ->method('isGranted')
  523. ->with($this->equalTo('DELETE'))
  524. ->will($this->returnValue(true));
  525. $this->request->setMethod('POST');
  526. $this->request->request->set('_method', 'DELETE');
  527. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  528. $response = $this->controller->deleteAction(1);
  529. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  530. $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
  531. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  532. }
  533. public function testDeleteActionAjaxError()
  534. {
  535. $object = new \stdClass();
  536. $this->admin->expects($this->once())
  537. ->method('getObject')
  538. ->will($this->returnValue($object));
  539. $this->admin->expects($this->once())
  540. ->method('isGranted')
  541. ->with($this->equalTo('DELETE'))
  542. ->will($this->returnValue(true));
  543. $this->admin->expects($this->once())
  544. ->method('delete')
  545. ->will($this->returnCallback(function() {
  546. throw new ModelManagerException();
  547. }));
  548. $this->request->setMethod('DELETE');
  549. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  550. $response = $this->controller->deleteAction(1);
  551. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  552. $this->assertEquals(json_encode(array('result'=>'error')), $response->getContent());
  553. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  554. }
  555. public function testDeleteActionSuccess1()
  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. $response = $this->controller->deleteAction(1);
  567. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  568. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  569. $this->assertEquals('list', $response->getTargetUrl());
  570. }
  571. public function testDeleteActionSuccess2()
  572. {
  573. $object = new \stdClass();
  574. $this->admin->expects($this->once())
  575. ->method('getObject')
  576. ->will($this->returnValue($object));
  577. $this->admin->expects($this->once())
  578. ->method('isGranted')
  579. ->with($this->equalTo('DELETE'))
  580. ->will($this->returnValue(true));
  581. $this->request->setMethod('POST');
  582. $this->request->request->set('_method', 'DELETE');
  583. $response = $this->controller->deleteAction(1);
  584. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  585. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  586. $this->assertEquals('list', $response->getTargetUrl());
  587. }
  588. public function testDeleteActionWrongRequestMethod()
  589. {
  590. $object = new \stdClass();
  591. $this->admin->expects($this->once())
  592. ->method('getObject')
  593. ->will($this->returnValue($object));
  594. $this->admin->expects($this->once())
  595. ->method('isGranted')
  596. ->with($this->equalTo('DELETE'))
  597. ->will($this->returnValue(true));
  598. //without POST request parameter "_method" should not be used as real REST method
  599. $this->request->query->set('_method', 'DELETE');
  600. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  601. $this->assertEquals($this->admin, $this->parameters['admin']);
  602. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  603. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  604. $this->assertEquals('delete', $this->parameters['action']);
  605. $this->assertEquals($object, $this->parameters['object']);
  606. $this->assertEquals('', $this->parameters['csrf_token']);
  607. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  608. }
  609. public function testDeleteActionError()
  610. {
  611. $object = new \stdClass();
  612. $this->admin->expects($this->once())
  613. ->method('getObject')
  614. ->will($this->returnValue($object));
  615. $this->admin->expects($this->once())
  616. ->method('isGranted')
  617. ->with($this->equalTo('DELETE'))
  618. ->will($this->returnValue(true));
  619. $this->admin->expects($this->once())
  620. ->method('delete')
  621. ->will($this->returnCallback(function() {
  622. throw new ModelManagerException();
  623. }));
  624. $this->request->setMethod('DELETE');
  625. $response = $this->controller->deleteAction(1);
  626. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  627. $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  628. $this->assertEquals('list', $response->getTargetUrl());
  629. }
  630. public function testEditActionNotFoundException()
  631. {
  632. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  633. $this->admin->expects($this->once())
  634. ->method('getObject')
  635. ->will($this->returnValue(false));
  636. $this->controller->editAction();
  637. }
  638. public function testEditActionAccessDenied()
  639. {
  640. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  641. $this->admin->expects($this->once())
  642. ->method('getObject')
  643. ->will($this->returnValue(new \stdClass()));
  644. $this->admin->expects($this->once())
  645. ->method('isGranted')
  646. ->with($this->equalTo('EDIT'))
  647. ->will($this->returnValue(false));
  648. $this->controller->editAction();
  649. }
  650. public function testEditAction()
  651. {
  652. $object = new \stdClass();
  653. $this->admin->expects($this->once())
  654. ->method('getObject')
  655. ->will($this->returnValue($object));
  656. $this->admin->expects($this->once())
  657. ->method('isGranted')
  658. ->with($this->equalTo('EDIT'))
  659. ->will($this->returnValue(true));
  660. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  661. ->disableOriginalConstructor()
  662. ->getMock();
  663. $this->admin->expects($this->once())
  664. ->method('getForm')
  665. ->will($this->returnValue($form));
  666. $formView = $this->getMock('Symfony\Component\Form\FormView');
  667. $form->expects($this->any())
  668. ->method('createView')
  669. ->will($this->returnValue($formView));
  670. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  671. $this->assertEquals($this->admin, $this->parameters['admin']);
  672. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  673. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  674. $this->assertEquals('edit', $this->parameters['action']);
  675. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  676. $this->assertEquals($object, $this->parameters['object']);
  677. }
  678. public function testEditActionSuccess()
  679. {
  680. $object = new \stdClass();
  681. $this->admin->expects($this->once())
  682. ->method('getObject')
  683. ->will($this->returnValue($object));
  684. $this->admin->expects($this->once())
  685. ->method('isGranted')
  686. ->with($this->equalTo('EDIT'))
  687. ->will($this->returnValue(true));
  688. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  689. ->disableOriginalConstructor()
  690. ->getMock();
  691. $this->admin->expects($this->once())
  692. ->method('getForm')
  693. ->will($this->returnValue($form));
  694. $form->expects($this->once())
  695. ->method('isValid')
  696. ->will($this->returnValue(true));
  697. $this->request->setMethod('POST');
  698. $response = $this->controller->editAction();
  699. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  700. $this->assertSame(array('flash_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  701. $this->assertEquals('stdClass_edit', $response->getTargetUrl());
  702. }
  703. public function testEditActionError()
  704. {
  705. $object = new \stdClass();
  706. $this->admin->expects($this->once())
  707. ->method('getObject')
  708. ->will($this->returnValue($object));
  709. $this->admin->expects($this->once())
  710. ->method('isGranted')
  711. ->with($this->equalTo('EDIT'))
  712. ->will($this->returnValue(true));
  713. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  714. ->disableOriginalConstructor()
  715. ->getMock();
  716. $this->admin->expects($this->once())
  717. ->method('getForm')
  718. ->will($this->returnValue($form));
  719. $form->expects($this->once())
  720. ->method('isValid')
  721. ->will($this->returnValue(false));
  722. $this->request->setMethod('POST');
  723. $formView = $this->getMock('Symfony\Component\Form\FormView');
  724. $form->expects($this->any())
  725. ->method('createView')
  726. ->will($this->returnValue($formView));
  727. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  728. $this->assertEquals($this->admin, $this->parameters['admin']);
  729. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  730. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  731. $this->assertEquals('edit', $this->parameters['action']);
  732. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  733. $this->assertEquals($object, $this->parameters['object']);
  734. }
  735. public function testEditActionAjaxSuccess()
  736. {
  737. $object = new \stdClass();
  738. $this->admin->expects($this->once())
  739. ->method('getObject')
  740. ->will($this->returnValue($object));
  741. $this->admin->expects($this->once())
  742. ->method('isGranted')
  743. ->with($this->equalTo('EDIT'))
  744. ->will($this->returnValue(true));
  745. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  746. ->disableOriginalConstructor()
  747. ->getMock();
  748. $this->admin->expects($this->once())
  749. ->method('getForm')
  750. ->will($this->returnValue($form));
  751. $form->expects($this->once())
  752. ->method('isValid')
  753. ->will($this->returnValue(true));
  754. $this->admin->expects($this->once())
  755. ->method('getNormalizedIdentifier')
  756. ->with($this->equalTo($object))
  757. ->will($this->returnValue('foo_normalized'));
  758. $this->request->setMethod('POST');
  759. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  760. $response = $this->controller->editAction();
  761. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  762. $this->assertEquals(json_encode(array('result'=>'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  763. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  764. }
  765. public function testEditActionAjaxError()
  766. {
  767. $object = new \stdClass();
  768. $this->admin->expects($this->once())
  769. ->method('getObject')
  770. ->will($this->returnValue($object));
  771. $this->admin->expects($this->once())
  772. ->method('isGranted')
  773. ->with($this->equalTo('EDIT'))
  774. ->will($this->returnValue(true));
  775. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  776. ->disableOriginalConstructor()
  777. ->getMock();
  778. $this->admin->expects($this->once())
  779. ->method('getForm')
  780. ->will($this->returnValue($form));
  781. $form->expects($this->once())
  782. ->method('isValid')
  783. ->will($this->returnValue(false));
  784. $this->request->setMethod('POST');
  785. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  786. $formView = $this->getMock('Symfony\Component\Form\FormView');
  787. $form->expects($this->any())
  788. ->method('createView')
  789. ->will($this->returnValue($formView));
  790. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  791. $this->assertEquals($this->admin, $this->parameters['admin']);
  792. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  793. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  794. $this->assertEquals('edit', $this->parameters['action']);
  795. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  796. $this->assertEquals($object, $this->parameters['object']);
  797. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  798. }
  799. public function testCreateActionAccessDenied()
  800. {
  801. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  802. $this->admin->expects($this->once())
  803. ->method('isGranted')
  804. ->with($this->equalTo('CREATE'))
  805. ->will($this->returnValue(false));
  806. $this->controller->createAction();
  807. }
  808. public function testCreateAction()
  809. {
  810. $this->admin->expects($this->once())
  811. ->method('isGranted')
  812. ->with($this->equalTo('CREATE'))
  813. ->will($this->returnValue(true));
  814. $object = new \stdClass();
  815. $this->admin->expects($this->once())
  816. ->method('getNewInstance')
  817. ->will($this->returnValue($object));
  818. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  819. ->disableOriginalConstructor()
  820. ->getMock();
  821. $this->admin->expects($this->once())
  822. ->method('getForm')
  823. ->will($this->returnValue($form));
  824. $formView = $this->getMock('Symfony\Component\Form\FormView');
  825. $form->expects($this->any())
  826. ->method('createView')
  827. ->will($this->returnValue($formView));
  828. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  829. $this->assertEquals($this->admin, $this->parameters['admin']);
  830. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  831. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  832. $this->assertEquals('create', $this->parameters['action']);
  833. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  834. $this->assertEquals($object, $this->parameters['object']);
  835. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  836. }
  837. public function testCreateActionSuccess()
  838. {
  839. $this->admin->expects($this->once())
  840. ->method('isGranted')
  841. ->with($this->equalTo('CREATE'))
  842. ->will($this->returnValue(true));
  843. $object = new \stdClass();
  844. $this->admin->expects($this->once())
  845. ->method('getNewInstance')
  846. ->will($this->returnValue($object));
  847. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  848. ->disableOriginalConstructor()
  849. ->getMock();
  850. $this->admin->expects($this->once())
  851. ->method('getForm')
  852. ->will($this->returnValue($form));
  853. $form->expects($this->once())
  854. ->method('isValid')
  855. ->will($this->returnValue(true));
  856. $this->request->setMethod('POST');
  857. $response = $this->controller->createAction();
  858. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  859. $this->assertSame(array('flash_create_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  860. $this->assertEquals('stdClass_edit', $response->getTargetUrl());
  861. }
  862. public function testCreateActionError()
  863. {
  864. $this->admin->expects($this->once())
  865. ->method('isGranted')
  866. ->with($this->equalTo('CREATE'))
  867. ->will($this->returnValue(true));
  868. $object = new \stdClass();
  869. $this->admin->expects($this->once())
  870. ->method('getNewInstance')
  871. ->will($this->returnValue($object));
  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. $form->expects($this->once())
  879. ->method('isValid')
  880. ->will($this->returnValue(false));
  881. $this->request->setMethod('POST');
  882. $formView = $this->getMock('Symfony\Component\Form\FormView');
  883. $form->expects($this->any())
  884. ->method('createView')
  885. ->will($this->returnValue($formView));
  886. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  887. $this->assertEquals($this->admin, $this->parameters['admin']);
  888. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  889. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  890. $this->assertEquals('create', $this->parameters['action']);
  891. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  892. $this->assertEquals($object, $this->parameters['object']);
  893. }
  894. public function testCreateActionAjaxSuccess()
  895. {
  896. $this->admin->expects($this->once())
  897. ->method('isGranted')
  898. ->with($this->equalTo('CREATE'))
  899. ->will($this->returnValue(true));
  900. $object = new \stdClass();
  901. $this->admin->expects($this->once())
  902. ->method('getNewInstance')
  903. ->will($this->returnValue($object));
  904. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  905. ->disableOriginalConstructor()
  906. ->getMock();
  907. $this->admin->expects($this->once())
  908. ->method('getForm')
  909. ->will($this->returnValue($form));
  910. $form->expects($this->once())
  911. ->method('isValid')
  912. ->will($this->returnValue(true));
  913. $this->admin->expects($this->once())
  914. ->method('getNormalizedIdentifier')
  915. ->with($this->equalTo($object))
  916. ->will($this->returnValue('foo_normalized'));
  917. $this->request->setMethod('POST');
  918. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  919. $response = $this->controller->createAction();
  920. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  921. $this->assertEquals(json_encode(array('result'=>'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  922. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  923. }
  924. public function testCreateActionAjaxError()
  925. {
  926. $this->admin->expects($this->once())
  927. ->method('isGranted')
  928. ->with($this->equalTo('CREATE'))
  929. ->will($this->returnValue(true));
  930. $object = new \stdClass();
  931. $this->admin->expects($this->once())
  932. ->method('getNewInstance')
  933. ->will($this->returnValue($object));
  934. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  935. ->disableOriginalConstructor()
  936. ->getMock();
  937. $this->admin->expects($this->once())
  938. ->method('getForm')
  939. ->will($this->returnValue($form));
  940. $form->expects($this->once())
  941. ->method('isValid')
  942. ->will($this->returnValue(false));
  943. $this->request->setMethod('POST');
  944. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  945. $formView = $this->getMock('Symfony\Component\Form\FormView');
  946. $form->expects($this->any())
  947. ->method('createView')
  948. ->will($this->returnValue($formView));
  949. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  950. $this->assertEquals($this->admin, $this->parameters['admin']);
  951. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  952. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  953. $this->assertEquals('create', $this->parameters['action']);
  954. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  955. $this->assertEquals($object, $this->parameters['object']);
  956. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  957. }
  958. public function testExportActionAccessDenied()
  959. {
  960. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  961. $this->admin->expects($this->once())
  962. ->method('isGranted')
  963. ->with($this->equalTo('EXPORT'))
  964. ->will($this->returnValue(false));
  965. $this->controller->exportAction($this->request);
  966. }
  967. public function testExportActionWrongFormat()
  968. {
  969. $this->setExpectedException('RuntimeException', 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`');
  970. $this->admin->expects($this->once())
  971. ->method('isGranted')
  972. ->with($this->equalTo('EXPORT'))
  973. ->will($this->returnValue(true));
  974. $this->admin->expects($this->once())
  975. ->method('getExportFormats')
  976. ->will($this->returnValue(array('json')));
  977. $this->admin->expects($this->once())
  978. ->method('getClass')
  979. ->will($this->returnValue('Foo'));
  980. $this->request->query->set('format', 'csv');
  981. $this->controller->exportAction($this->request);
  982. }
  983. public function testExportAction()
  984. {
  985. $this->admin->expects($this->once())
  986. ->method('isGranted')
  987. ->with($this->equalTo('EXPORT'))
  988. ->will($this->returnValue(true));
  989. $this->admin->expects($this->once())
  990. ->method('getExportFormats')
  991. ->will($this->returnValue(array('json')));
  992. $dataSourceIterator = $this->getMock('Exporter\Source\SourceIteratorInterface');
  993. $this->admin->expects($this->once())
  994. ->method('getDataSourceIterator')
  995. ->will($this->returnValue($dataSourceIterator));
  996. $this->request->query->set('format', 'json');
  997. $response = $this->controller->exportAction($this->request);
  998. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  999. $this->assertEquals(200, $response->getStatusCode());
  1000. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1001. }
  1002. public function testHistoryActionAccessDenied()
  1003. {
  1004. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1005. $this->admin->expects($this->once())
  1006. ->method('isGranted')
  1007. ->with($this->equalTo('EDIT'))
  1008. ->will($this->returnValue(false));
  1009. $this->controller->historyAction();
  1010. }
  1011. public function testHistoryActionNotFoundException()
  1012. {
  1013. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1014. $this->admin->expects($this->once())
  1015. ->method('isGranted')
  1016. ->with($this->equalTo('EDIT'))
  1017. ->will($this->returnValue(true));
  1018. $this->admin->expects($this->once())
  1019. ->method('getObject')
  1020. ->will($this->returnValue(false));
  1021. $this->controller->historyAction();
  1022. }
  1023. public function testHistoryActionNoReader()
  1024. {
  1025. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  1026. $this->request->query->set('id', 123);
  1027. $this->admin->expects($this->once())
  1028. ->method('isGranted')
  1029. ->with($this->equalTo('EDIT'))
  1030. ->will($this->returnValue(true));
  1031. $object = new \stdClass();
  1032. $this->admin->expects($this->once())
  1033. ->method('getObject')
  1034. ->will($this->returnValue($object));
  1035. $this->admin->expects($this->any())
  1036. ->method('getClass')
  1037. ->will($this->returnValue('Foo'));
  1038. $this->auditManager->expects($this->once())
  1039. ->method('hasReader')
  1040. ->with($this->equalTo('Foo'))
  1041. ->will($this->returnValue(false));
  1042. $this->controller->historyAction();
  1043. }
  1044. public function testHistoryAction()
  1045. {
  1046. $this->request->query->set('id', 123);
  1047. $this->admin->expects($this->once())
  1048. ->method('isGranted')
  1049. ->with($this->equalTo('EDIT'))
  1050. ->will($this->returnValue(true));
  1051. $object = new \stdClass();
  1052. $this->admin->expects($this->once())
  1053. ->method('getObject')
  1054. ->will($this->returnValue($object));
  1055. $this->admin->expects($this->any())
  1056. ->method('getClass')
  1057. ->will($this->returnValue('Foo'));
  1058. $this->auditManager->expects($this->once())
  1059. ->method('hasReader')
  1060. ->with($this->equalTo('Foo'))
  1061. ->will($this->returnValue(true));
  1062. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1063. $this->auditManager->expects($this->once())
  1064. ->method('getReader')
  1065. ->with($this->equalTo('Foo'))
  1066. ->will($this->returnValue($reader));
  1067. $reader->expects($this->once())
  1068. ->method('findRevisions')
  1069. ->with($this->equalTo('Foo'), $this->equalTo(123))
  1070. ->will($this->returnValue(array()));
  1071. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyAction());
  1072. $this->assertEquals($this->admin, $this->parameters['admin']);
  1073. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1074. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1075. $this->assertEquals('history', $this->parameters['action']);
  1076. $this->assertEquals(array(), $this->parameters['revisions']);
  1077. $this->assertEquals($object, $this->parameters['object']);
  1078. }
  1079. }