CRUDControllerTest.php 60 KB

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