CRUDControllerTest.php 87 KB

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