CRUDControllerTest.php 88 KB

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