CRUDControllerTest.php 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918
  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. use Symfony\Component\HttpKernel\Exception\HttpException;
  27. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
  28. use Sonata\AdminBundle\Admin\AdminInterface;
  29. /**
  30. * Test for CRUDController
  31. *
  32. * @author Andrej Hudec <pulzarraider@gmail.com>
  33. */
  34. class CRUDControllerTest extends \PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * @var CRUDController
  38. */
  39. private $controller;
  40. /**
  41. * @var Request
  42. */
  43. private $request;
  44. /**
  45. * @var AdminInterface
  46. */
  47. private $admin;
  48. /**
  49. * @var Pool
  50. */
  51. private $pool;
  52. /**
  53. * @var array
  54. */
  55. private $parameters;
  56. /**
  57. * @var Session
  58. */
  59. private $session;
  60. /**
  61. * @var Sonata\AdminBundle\Model\AuditManager
  62. */
  63. private $auditManager;
  64. /**
  65. * @var ContainerInterface
  66. */
  67. private $container;
  68. /**
  69. * @var AdminObjectAclManipulator
  70. */
  71. private $adminObjectAclManipulator;
  72. /**
  73. * @var string
  74. */
  75. private $template;
  76. /**
  77. * @var array
  78. */
  79. private $protectedTestedMethods;
  80. /**
  81. * @var CsrfProviderInterface
  82. */
  83. private $csrfProvider;
  84. /**
  85. * {@inheritDoc}
  86. */
  87. protected function setUp()
  88. {
  89. $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  90. $this->request = new Request();
  91. $this->pool = new Pool($this->container, 'title', 'logo.png');
  92. $this->pool->setAdminServiceIds(array('foo.admin'));
  93. $this->request->attributes->set('_sonata_admin', 'foo.admin');
  94. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  95. $this->parameters = array();
  96. $this->template = '';
  97. // php 5.3 BC
  98. $params = &$this->parameters;
  99. $template = &$this->template;
  100. $templating = $this->getMock(
  101. 'Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine',
  102. array(),
  103. array($this->container, array())
  104. );
  105. $templating->expects($this->any())
  106. ->method('renderResponse')
  107. ->will($this->returnCallback(function (
  108. $view,
  109. array $parameters = array(),
  110. Response $response = null
  111. ) use (
  112. &$params,
  113. &$template
  114. ) {
  115. $template = $view;
  116. if (null === $response) {
  117. $response = new Response();
  118. }
  119. $params = $parameters;
  120. return $response;
  121. }));
  122. $this->session = new Session(new MockArraySessionStorage());
  123. // php 5.3 BC
  124. $pool = $this->pool;
  125. $request = $this->request;
  126. $admin = $this->admin;
  127. $session = $this->session;
  128. $twig = $this->getMockBuilder('Twig_Environment')
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $twigRenderer = $this->getMock('Symfony\Bridge\Twig\Form\TwigRendererInterface');
  132. $formExtension = new FormExtension($twigRenderer);
  133. $twig->expects($this->any())
  134. ->method('getExtension')
  135. ->will($this->returnCallback(function ($name) use ($formExtension) {
  136. switch ($name) {
  137. case 'form':
  138. return $formExtension;
  139. }
  140. return null;
  141. }));
  142. $exporter = $this->getMock('Sonata\AdminBundle\Export\Exporter');
  143. $exporter->expects($this->any())
  144. ->method('getResponse')
  145. ->will($this->returnValue(new StreamedResponse()));
  146. $this->auditManager = $this->getMockBuilder('Sonata\AdminBundle\Model\AuditManager')
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. $this->adminObjectAclManipulator = $this->getMockBuilder('Sonata\AdminBundle\Util\AdminObjectAclManipulator')
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. // php 5.3 BC
  153. $auditManager = $this->auditManager;
  154. $adminObjectAclManipulator = $this->adminObjectAclManipulator;
  155. $this->csrfProvider = $this->getMockBuilder(
  156. 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'
  157. )
  158. ->getMock();
  159. $this->csrfProvider->expects($this->any())
  160. ->method('generateCsrfToken')
  161. ->will($this->returnCallback(function ($intention) {
  162. return 'csrf-token-123_'.$intention;
  163. }));
  164. $this->csrfProvider->expects($this->any())
  165. ->method('isCsrfTokenValid')
  166. ->will($this->returnCallback(function ($intention, $token) {
  167. if ($token == 'csrf-token-123_'.$intention) {
  168. return true;
  169. }
  170. return false;
  171. }));
  172. // php 5.3 BC
  173. $csrfProvider = $this->csrfProvider;
  174. $this->logger = $this->getMock('Psr\Log\LoggerInterface');
  175. $logger = $this->logger; // php 5.3 BC
  176. $requestStack = null;
  177. if (Kernel::MINOR_VERSION > 3) {
  178. $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
  179. $requestStack->push($request);
  180. }
  181. $this->container->expects($this->any())
  182. ->method('get')
  183. ->will($this->returnCallback(function ($id) use (
  184. $pool,
  185. $request,
  186. $admin,
  187. $templating,
  188. $twig,
  189. $session,
  190. $exporter,
  191. $auditManager,
  192. $adminObjectAclManipulator,
  193. $requestStack,
  194. $csrfProvider,
  195. $logger
  196. ) {
  197. switch ($id) {
  198. case 'sonata.admin.pool':
  199. return $pool;
  200. case 'request_stack':
  201. return $requestStack;
  202. case 'request':
  203. return $request;
  204. case 'foo.admin':
  205. return $admin;
  206. case 'templating':
  207. return $templating;
  208. case 'twig':
  209. return $twig;
  210. case 'session':
  211. return $session;
  212. case 'sonata.admin.exporter':
  213. return $exporter;
  214. case 'sonata.admin.audit.manager':
  215. return $auditManager;
  216. case 'sonata.admin.object.manipulator.acl.admin':
  217. return $adminObjectAclManipulator;
  218. case 'form.csrf_provider':
  219. return $csrfProvider;
  220. case 'logger':
  221. return $logger;
  222. }
  223. return null;
  224. }));
  225. // php 5.3
  226. $tthis = $this;
  227. $this->container->expects($this->any())
  228. ->method('has')
  229. ->will($this->returnCallback(function ($id) use ($tthis) {
  230. if ($id == 'form.csrf_provider' && $tthis->getCsrfProvider()!==null) {
  231. return true;
  232. }
  233. if ($id == 'logger') {
  234. return true;
  235. }
  236. return false;
  237. }));
  238. $this->admin->expects($this->any())
  239. ->method('getTemplate')
  240. ->will($this->returnCallback(function ($name) {
  241. switch ($name) {
  242. case 'ajax':
  243. return 'SonataAdminBundle::ajax_layout.html.twig';
  244. case 'layout':
  245. return 'SonataAdminBundle::standard_layout.html.twig';
  246. case 'show':
  247. return 'SonataAdminBundle:CRUD:show.html.twig';
  248. case 'show_compare':
  249. return 'SonataAdminBundle:CRUD:show_compare.html.twig';
  250. case 'edit':
  251. return 'SonataAdminBundle:CRUD:edit.html.twig';
  252. case 'dashboard':
  253. return 'SonataAdminBundle:Core:dashboard.html.twig';
  254. case 'search':
  255. return 'SonataAdminBundle:Core:search.html.twig';
  256. case 'list':
  257. return 'SonataAdminBundle:CRUD:list.html.twig';
  258. case 'preview':
  259. return 'SonataAdminBundle:CRUD:preview.html.twig';
  260. case 'history':
  261. return 'SonataAdminBundle:CRUD:history.html.twig';
  262. case 'acl':
  263. return 'SonataAdminBundle:CRUD:acl.html.twig';
  264. case 'delete':
  265. return 'SonataAdminBundle:CRUD:delete.html.twig';
  266. case 'batch':
  267. return 'SonataAdminBundle:CRUD:list__batch.html.twig';
  268. case 'batch_confirmation':
  269. return 'SonataAdminBundle:CRUD:batch_confirmation.html.twig';
  270. }
  271. return null;
  272. }));
  273. $this->admin->expects($this->any())
  274. ->method('getIdParameter')
  275. ->will($this->returnValue('id'));
  276. $this->admin->expects($this->any())
  277. ->method('generateUrl')
  278. ->will(
  279. $this->returnCallback(
  280. function ($name, array $parameters = array(), $absolute = false) {
  281. $result = $name;
  282. if (!empty($parameters)) {
  283. $result .= '?'.http_build_query($parameters);
  284. }
  285. return $result;
  286. }
  287. )
  288. );
  289. $this->admin->expects($this->any())
  290. ->method('generateObjectUrl')
  291. ->will(
  292. $this->returnCallback(
  293. function ($name, $object, array $parameters = array(), $absolute = false) {
  294. $result = get_class($object).'_'.$name;
  295. if (!empty($parameters)) {
  296. $result .= '?'.http_build_query($parameters);
  297. }
  298. return $result;
  299. }
  300. )
  301. );
  302. $this->controller = new CRUDController();
  303. $this->controller->setContainer($this->container);
  304. // Make some methods public to test them
  305. $testedMethods = array(
  306. 'renderJson',
  307. 'isXmlHttpRequest',
  308. 'configure',
  309. 'getBaseTemplate',
  310. 'redirectTo',
  311. 'addFlash'
  312. );
  313. foreach ($testedMethods as $testedMethod) {
  314. $method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod);
  315. $method->setAccessible(true);
  316. $this->protectedTestedMethods[$testedMethod] = $method;
  317. }
  318. }
  319. public function testRenderJson1()
  320. {
  321. $data = array('example'=>'123', 'foo'=>'bar');
  322. $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
  323. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
  324. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  325. $this->assertEquals(json_encode($data), $response->getContent());
  326. }
  327. public function testRenderJson2()
  328. {
  329. $data = array('example'=>'123', 'foo'=>'bar');
  330. $this->request->headers->set('Content-Type', 'multipart/form-data');
  331. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
  332. $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
  333. $this->assertEquals(json_encode($data), $response->getContent());
  334. }
  335. public function testRenderJsonAjax()
  336. {
  337. $data = array('example'=>'123', 'foo'=>'bar');
  338. $this->request->attributes->set('_xml_http_request', true);
  339. $this->request->headers->set('Content-Type', 'multipart/form-data');
  340. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
  341. $this->assertEquals($response->headers->get('Content-Type'), 'text/plain');
  342. $this->assertEquals(json_encode($data), $response->getContent());
  343. }
  344. public function testIsXmlHttpRequest()
  345. {
  346. $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  347. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  348. $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  349. $this->request->headers->remove('X-Requested-With');
  350. $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  351. $this->request->attributes->set('_xml_http_request', true);
  352. $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
  353. }
  354. public function testConfigure()
  355. {
  356. $uniqueId = '';
  357. $this->admin->expects($this->once())
  358. ->method('setUniqid')
  359. ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
  360. $uniqueId = $uniqid;
  361. }));
  362. $this->request->query->set('uniqid', 123456);
  363. $this->protectedTestedMethods['configure']->invoke($this->controller);
  364. $this->assertEquals(123456, $uniqueId);
  365. $this->assertAttributeEquals($this->admin, 'admin', $this->controller);
  366. }
  367. public function testConfigureChild()
  368. {
  369. $uniqueId = '';
  370. $this->admin->expects($this->once())
  371. ->method('setUniqid')
  372. ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
  373. $uniqueId = $uniqid;
  374. }));
  375. $this->admin->expects($this->once())
  376. ->method('isChild')
  377. ->will($this->returnValue(true));
  378. $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  379. $this->admin->expects($this->once())
  380. ->method('getParent')
  381. ->will($this->returnValue($adminParent));
  382. $this->request->query->set('uniqid', 123456);
  383. $this->protectedTestedMethods['configure']->invoke($this->controller);
  384. $this->assertEquals(123456, $uniqueId);
  385. $this->assertAttributeEquals($adminParent, 'admin', $this->controller);
  386. }
  387. public function testConfigureWithException()
  388. {
  389. $this->setExpectedException(
  390. 'RuntimeException',
  391. 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`'
  392. );
  393. $this->request->attributes->remove('_sonata_admin');
  394. $this->protectedTestedMethods['configure']->invoke($this->controller);
  395. }
  396. public function testConfigureWithException2()
  397. {
  398. $this->setExpectedException(
  399. 'RuntimeException',
  400. 'Unable to find the admin class related to the current controller ' .
  401. '(Sonata\AdminBundle\Controller\CRUDController)'
  402. );
  403. $this->pool->setAdminServiceIds(array('nonexistent.admin'));
  404. $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
  405. $this->protectedTestedMethods['configure']->invoke($this->controller);
  406. }
  407. public function testGetBaseTemplate()
  408. {
  409. $this->assertEquals(
  410. 'SonataAdminBundle::standard_layout.html.twig',
  411. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller)
  412. );
  413. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  414. $this->assertEquals(
  415. 'SonataAdminBundle::ajax_layout.html.twig',
  416. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller)
  417. );
  418. $this->request->headers->remove('X-Requested-With');
  419. $this->assertEquals(
  420. 'SonataAdminBundle::standard_layout.html.twig',
  421. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller)
  422. );
  423. $this->request->attributes->set('_xml_http_request', true);
  424. $this->assertEquals(
  425. 'SonataAdminBundle::ajax_layout.html.twig',
  426. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller)
  427. );
  428. }
  429. public function testRender()
  430. {
  431. $this->parameters = array();
  432. $this->assertInstanceOf(
  433. 'Symfony\Component\HttpFoundation\Response',
  434. $this->controller->render('FooAdminBundle::foo.html.twig', array())
  435. );
  436. $this->assertEquals($this->admin, $this->parameters['admin']);
  437. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  438. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  439. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  440. }
  441. public function testRenderWithResponse()
  442. {
  443. $this->parameters = array();
  444. $response = $response = new Response();
  445. $response->headers->set('X-foo', 'bar');
  446. $responseResult = $this->controller->render('FooAdminBundle::foo.html.twig', array(), $response);
  447. $this->assertEquals($response, $responseResult);
  448. $this->assertEquals('bar', $responseResult->headers->get('X-foo'));
  449. $this->assertEquals($this->admin, $this->parameters['admin']);
  450. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  451. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  452. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  453. }
  454. public function testRenderCustomParams()
  455. {
  456. $this->parameters = array();
  457. $this->assertInstanceOf(
  458. 'Symfony\Component\HttpFoundation\Response',
  459. $this->controller->render('FooAdminBundle::foo.html.twig',
  460. array('foo'=>'bar'))
  461. );
  462. $this->assertEquals($this->admin, $this->parameters['admin']);
  463. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  464. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  465. $this->assertEquals('bar', $this->parameters['foo']);
  466. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  467. }
  468. public function testRenderAjax()
  469. {
  470. $this->parameters = array();
  471. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  472. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo'=>'bar')));
  473. $this->assertEquals($this->admin, $this->parameters['admin']);
  474. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  475. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  476. $this->assertEquals('bar', $this->parameters['foo']);
  477. $this->assertEquals('FooAdminBundle::foo.html.twig', $this->template);
  478. }
  479. public function testListActionAccessDenied()
  480. {
  481. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  482. $this->admin->expects($this->once())
  483. ->method('isGranted')
  484. ->with($this->equalTo('LIST'))
  485. ->will($this->returnValue(false));
  486. $this->controller->listAction();
  487. }
  488. public function testListAction()
  489. {
  490. $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  491. $this->admin->expects($this->once())
  492. ->method('isGranted')
  493. ->with($this->equalTo('LIST'))
  494. ->will($this->returnValue(true));
  495. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  496. ->disableOriginalConstructor()
  497. ->getMock();
  498. $form->expects($this->once())
  499. ->method('createView')
  500. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  501. $this->admin->expects($this->once())
  502. ->method('getDatagrid')
  503. ->will($this->returnValue($datagrid));
  504. $datagrid->expects($this->once())
  505. ->method('getForm')
  506. ->will($this->returnValue($form));
  507. $this->parameters = array();
  508. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->listAction());
  509. $this->assertEquals($this->admin, $this->parameters['admin']);
  510. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  511. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  512. $this->assertEquals('list', $this->parameters['action']);
  513. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  514. $this->assertInstanceOf('Sonata\AdminBundle\Datagrid\DatagridInterface', $this->parameters['datagrid']);
  515. $this->assertEquals('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
  516. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  517. $this->assertEquals('SonataAdminBundle:CRUD:list.html.twig', $this->template);
  518. }
  519. public function testBatchActionDeleteAccessDenied()
  520. {
  521. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  522. $this->admin->expects($this->once())
  523. ->method('isGranted')
  524. ->with($this->equalTo('DELETE'))
  525. ->will($this->returnValue(false));
  526. $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  527. }
  528. public function testBatchActionDelete()
  529. {
  530. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  531. $this->admin->expects($this->once())
  532. ->method('isGranted')
  533. ->with($this->equalTo('DELETE'))
  534. ->will($this->returnValue(true));
  535. $this->admin->expects($this->once())
  536. ->method('getModelManager')
  537. ->will($this->returnValue($modelManager));
  538. $this->admin->expects($this->once())
  539. ->method('getFilterParameters')
  540. ->will($this->returnValue(array('foo'=>'bar')));
  541. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  542. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  543. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  544. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  545. }
  546. private function assertLoggerLogsModelManagerException($subject, $method)
  547. {
  548. $exception = new ModelManagerException(
  549. $message = 'message',
  550. 1234,
  551. new \Exception($previousExceptionMessage = 'very useful message')
  552. );
  553. $subject->expects($this->once())
  554. ->method($method)
  555. ->will($this->returnCallback(function () use ($exception) {
  556. throw $exception;
  557. }));
  558. $this->logger->expects($this->once())
  559. ->method('error')
  560. ->with($message, array(
  561. 'exception' => $exception,
  562. 'previous_exception_message' => $previousExceptionMessage
  563. ));
  564. }
  565. public function testBatchActionDeleteWithModelManagerException()
  566. {
  567. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  568. $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete');
  569. $this->admin->expects($this->once())
  570. ->method('getModelManager')
  571. ->will($this->returnValue($modelManager));
  572. $this->admin->expects($this->once())
  573. ->method('getFilterParameters')
  574. ->will($this->returnValue(array('foo'=>'bar')));
  575. $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  576. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  577. $this->assertSame(array('flash_batch_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  578. $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  579. }
  580. public function testShowActionNotFoundException()
  581. {
  582. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  583. $this->admin->expects($this->once())
  584. ->method('getObject')
  585. ->will($this->returnValue(false));
  586. $this->controller->showAction();
  587. }
  588. public function testShowActionAccessDenied()
  589. {
  590. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  591. $this->admin->expects($this->once())
  592. ->method('getObject')
  593. ->will($this->returnValue(new \stdClass()));
  594. $this->admin->expects($this->once())
  595. ->method('isGranted')
  596. ->with($this->equalTo('VIEW'))
  597. ->will($this->returnValue(false));
  598. $this->controller->showAction();
  599. }
  600. public function testShowAction()
  601. {
  602. $object = new \stdClass();
  603. $this->admin->expects($this->once())
  604. ->method('getObject')
  605. ->will($this->returnValue($object));
  606. $this->admin->expects($this->once())
  607. ->method('isGranted')
  608. ->with($this->equalTo('VIEW'))
  609. ->will($this->returnValue(true));
  610. $show = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
  611. $this->admin->expects($this->once())
  612. ->method('getShow')
  613. ->will($this->returnValue($show));
  614. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->showAction());
  615. $this->assertEquals($this->admin, $this->parameters['admin']);
  616. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  617. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  618. $this->assertEquals('show', $this->parameters['action']);
  619. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $this->parameters['elements']);
  620. $this->assertEquals($object, $this->parameters['object']);
  621. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  622. $this->assertEquals('SonataAdminBundle:CRUD:show.html.twig', $this->template);
  623. }
  624. /**
  625. * @dataProvider getRedirectToTests
  626. */
  627. public function testRedirectTo($expected, $queryParams, $hasActiveSubclass)
  628. {
  629. $this->admin->expects($this->any())
  630. ->method('hasActiveSubclass')
  631. ->will($this->returnValue($hasActiveSubclass));
  632. $object = new \stdClass();
  633. foreach ($queryParams as $key => $value) {
  634. $this->request->query->set($key, $value);
  635. }
  636. $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object);
  637. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  638. $this->assertEquals($expected, $response->getTargetUrl());
  639. }
  640. public function getRedirectToTests()
  641. {
  642. return array(
  643. array('stdClass_edit', array(), false),
  644. array('list', array('btn_update_and_list'=>true), false),
  645. array('list', array('btn_create_and_list'=>true), false),
  646. array('create', array('btn_create_and_create'=>true), false),
  647. array('create?subclass=foo', array('btn_create_and_create'=>true, 'subclass'=>'foo'), true),
  648. );
  649. }
  650. public function testAddFlash()
  651. {
  652. $this->protectedTestedMethods['addFlash']->invoke($this->controller, 'foo', 'bar');
  653. $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
  654. }
  655. public function testDeleteActionNotFoundException()
  656. {
  657. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  658. $this->admin->expects($this->once())
  659. ->method('getObject')
  660. ->will($this->returnValue(false));
  661. $this->controller->deleteAction(1);
  662. }
  663. public function testDeleteActionAccessDenied()
  664. {
  665. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  666. $this->admin->expects($this->once())
  667. ->method('getObject')
  668. ->will($this->returnValue(new \stdClass()));
  669. $this->admin->expects($this->once())
  670. ->method('isGranted')
  671. ->with($this->equalTo('DELETE'))
  672. ->will($this->returnValue(false));
  673. $this->controller->deleteAction(1);
  674. }
  675. public function testDeleteAction()
  676. {
  677. $object = new \stdClass();
  678. $this->admin->expects($this->once())
  679. ->method('getObject')
  680. ->will($this->returnValue($object));
  681. $this->admin->expects($this->once())
  682. ->method('isGranted')
  683. ->with($this->equalTo('DELETE'))
  684. ->will($this->returnValue(true));
  685. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  686. $this->assertEquals($this->admin, $this->parameters['admin']);
  687. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  688. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  689. $this->assertEquals('delete', $this->parameters['action']);
  690. $this->assertEquals($object, $this->parameters['object']);
  691. $this->assertEquals('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
  692. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  693. $this->assertEquals('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  694. }
  695. public function testDeleteActionNoCsrfToken()
  696. {
  697. $this->csrfProvider = null;
  698. $object = new \stdClass();
  699. $this->admin->expects($this->once())
  700. ->method('getObject')
  701. ->will($this->returnValue($object));
  702. $this->admin->expects($this->once())
  703. ->method('isGranted')
  704. ->with($this->equalTo('DELETE'))
  705. ->will($this->returnValue(true));
  706. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  707. $this->assertEquals($this->admin, $this->parameters['admin']);
  708. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  709. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  710. $this->assertEquals('delete', $this->parameters['action']);
  711. $this->assertEquals($object, $this->parameters['object']);
  712. $this->assertEquals('', $this->parameters['csrf_token']);
  713. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  714. $this->assertEquals('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  715. }
  716. public function testDeleteActionAjaxSuccess1()
  717. {
  718. $object = new \stdClass();
  719. $this->admin->expects($this->once())
  720. ->method('getObject')
  721. ->will($this->returnValue($object));
  722. $this->admin->expects($this->once())
  723. ->method('isGranted')
  724. ->with($this->equalTo('DELETE'))
  725. ->will($this->returnValue(true));
  726. $this->request->setMethod('DELETE');
  727. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  728. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  729. $response = $this->controller->deleteAction(1);
  730. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  731. $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
  732. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  733. }
  734. public function testDeleteActionAjaxSuccess2()
  735. {
  736. $object = new \stdClass();
  737. $this->admin->expects($this->once())
  738. ->method('getObject')
  739. ->will($this->returnValue($object));
  740. $this->admin->expects($this->once())
  741. ->method('isGranted')
  742. ->with($this->equalTo('DELETE'))
  743. ->will($this->returnValue(true));
  744. $this->request->setMethod('POST');
  745. $this->request->request->set('_method', 'DELETE');
  746. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  747. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  748. $response = $this->controller->deleteAction(1);
  749. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  750. $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
  751. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  752. }
  753. public function testDeleteActionAjaxError()
  754. {
  755. $object = new \stdClass();
  756. $this->admin->expects($this->once())
  757. ->method('getObject')
  758. ->will($this->returnValue($object));
  759. $this->admin->expects($this->once())
  760. ->method('isGranted')
  761. ->with($this->equalTo('DELETE'))
  762. ->will($this->returnValue(true));
  763. $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
  764. $this->request->setMethod('DELETE');
  765. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  766. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  767. $response = $this->controller->deleteAction(1);
  768. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  769. $this->assertEquals(json_encode(array('result'=>'error')), $response->getContent());
  770. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  771. }
  772. public function testDeleteActionSuccess1()
  773. {
  774. $object = new \stdClass();
  775. $this->admin->expects($this->once())
  776. ->method('getObject')
  777. ->will($this->returnValue($object));
  778. $this->admin->expects($this->once())
  779. ->method('toString')
  780. ->with($this->equalTo($object))
  781. ->will($this->returnValue('test'));
  782. $this->expectTranslate('flash_delete_success', array('%name%' => 'test'));
  783. $this->admin->expects($this->once())
  784. ->method('isGranted')
  785. ->with($this->equalTo('DELETE'))
  786. ->will($this->returnValue(true));
  787. $this->request->setMethod('DELETE');
  788. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  789. $response = $this->controller->deleteAction(1);
  790. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  791. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  792. $this->assertEquals('list', $response->getTargetUrl());
  793. }
  794. public function testDeleteActionSuccess2()
  795. {
  796. $object = new \stdClass();
  797. $this->admin->expects($this->once())
  798. ->method('getObject')
  799. ->will($this->returnValue($object));
  800. $this->admin->expects($this->once())
  801. ->method('isGranted')
  802. ->with($this->equalTo('DELETE'))
  803. ->will($this->returnValue(true));
  804. $this->expectTranslate('flash_delete_success');
  805. $this->request->setMethod('POST');
  806. $this->request->request->set('_method', 'DELETE');
  807. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  808. $response = $this->controller->deleteAction(1);
  809. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  810. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  811. $this->assertEquals('list', $response->getTargetUrl());
  812. }
  813. public function testDeleteActionSuccessNoCsrfTokenProvider()
  814. {
  815. $this->csrfProvider = null;
  816. $object = new \stdClass();
  817. $this->admin->expects($this->once())
  818. ->method('getObject')
  819. ->will($this->returnValue($object));
  820. $this->admin->expects($this->once())
  821. ->method('isGranted')
  822. ->with($this->equalTo('DELETE'))
  823. ->will($this->returnValue(true));
  824. $this->expectTranslate('flash_delete_success');
  825. $this->request->setMethod('POST');
  826. $this->request->request->set('_method', 'DELETE');
  827. $response = $this->controller->deleteAction(1);
  828. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  829. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  830. $this->assertEquals('list', $response->getTargetUrl());
  831. }
  832. public function testDeleteActionWrongRequestMethod()
  833. {
  834. $object = new \stdClass();
  835. $this->admin->expects($this->once())
  836. ->method('getObject')
  837. ->will($this->returnValue($object));
  838. $this->admin->expects($this->once())
  839. ->method('isGranted')
  840. ->with($this->equalTo('DELETE'))
  841. ->will($this->returnValue(true));
  842. //without POST request parameter "_method" should not be used as real REST method
  843. $this->request->query->set('_method', 'DELETE');
  844. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
  845. $this->assertEquals($this->admin, $this->parameters['admin']);
  846. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  847. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  848. $this->assertEquals('delete', $this->parameters['action']);
  849. $this->assertEquals($object, $this->parameters['object']);
  850. $this->assertEquals('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
  851. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  852. $this->assertEquals('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  853. }
  854. public function testDeleteActionError()
  855. {
  856. $object = new \stdClass();
  857. $this->admin->expects($this->once())
  858. ->method('getObject')
  859. ->will($this->returnValue($object));
  860. $this->admin->expects($this->once())
  861. ->method('isGranted')
  862. ->with($this->equalTo('DELETE'))
  863. ->will($this->returnValue(true));
  864. $this->expectTranslate('flash_delete_error');
  865. $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
  866. $this->request->setMethod('DELETE');
  867. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  868. $response = $this->controller->deleteAction(1);
  869. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  870. $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  871. $this->assertEquals('list', $response->getTargetUrl());
  872. }
  873. public function testDeleteActionInvalidCsrfToken()
  874. {
  875. $object = new \stdClass();
  876. $this->admin->expects($this->once())
  877. ->method('getObject')
  878. ->will($this->returnValue($object));
  879. $this->admin->expects($this->once())
  880. ->method('isGranted')
  881. ->with($this->equalTo('DELETE'))
  882. ->will($this->returnValue(true));
  883. $this->request->setMethod('POST');
  884. $this->request->request->set('_method', 'DELETE');
  885. $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
  886. try {
  887. $this->controller->deleteAction(1);
  888. } catch (HttpException $e) {
  889. $this->assertEquals('The csrf token is not valid, CSRF attack?', $e->getMessage());
  890. $this->assertEquals(400, $e->getStatusCode());
  891. }
  892. }
  893. public function testEditActionNotFoundException()
  894. {
  895. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  896. $this->admin->expects($this->once())
  897. ->method('getObject')
  898. ->will($this->returnValue(false));
  899. $this->controller->editAction();
  900. }
  901. public function testEditActionAccessDenied()
  902. {
  903. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  904. $this->admin->expects($this->once())
  905. ->method('getObject')
  906. ->will($this->returnValue(new \stdClass()));
  907. $this->admin->expects($this->once())
  908. ->method('isGranted')
  909. ->with($this->equalTo('EDIT'))
  910. ->will($this->returnValue(false));
  911. $this->controller->editAction();
  912. }
  913. public function testEditAction()
  914. {
  915. $object = new \stdClass();
  916. $this->admin->expects($this->once())
  917. ->method('getObject')
  918. ->will($this->returnValue($object));
  919. $this->admin->expects($this->once())
  920. ->method('isGranted')
  921. ->with($this->equalTo('EDIT'))
  922. ->will($this->returnValue(true));
  923. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  924. ->disableOriginalConstructor()
  925. ->getMock();
  926. $this->admin->expects($this->once())
  927. ->method('getForm')
  928. ->will($this->returnValue($form));
  929. $formView = $this->getMock('Symfony\Component\Form\FormView');
  930. $form->expects($this->any())
  931. ->method('createView')
  932. ->will($this->returnValue($formView));
  933. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  934. $this->assertEquals($this->admin, $this->parameters['admin']);
  935. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  936. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  937. $this->assertEquals('edit', $this->parameters['action']);
  938. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  939. $this->assertEquals($object, $this->parameters['object']);
  940. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  941. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  942. }
  943. public function testEditActionSuccess()
  944. {
  945. $object = new \stdClass();
  946. $this->admin->expects($this->once())
  947. ->method('getObject')
  948. ->will($this->returnValue($object));
  949. $this->admin->expects($this->once())
  950. ->method('update')
  951. ->will($this->returnArgument(0));
  952. $this->admin->expects($this->once())
  953. ->method('isGranted')
  954. ->with($this->equalTo('EDIT'))
  955. ->will($this->returnValue(true));
  956. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  957. ->disableOriginalConstructor()
  958. ->getMock();
  959. $this->admin->expects($this->once())
  960. ->method('getForm')
  961. ->will($this->returnValue($form));
  962. $form->expects($this->once())
  963. ->method('isValid')
  964. ->will($this->returnValue(true));
  965. $this->expectTranslate('flash_edit_success');
  966. $this->request->setMethod('POST');
  967. $response = $this->controller->editAction();
  968. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  969. $this->assertSame(array('flash_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  970. $this->assertEquals('stdClass_edit', $response->getTargetUrl());
  971. }
  972. public function testEditActionError()
  973. {
  974. $object = new \stdClass();
  975. $this->admin->expects($this->once())
  976. ->method('getObject')
  977. ->will($this->returnValue($object));
  978. $this->admin->expects($this->once())
  979. ->method('isGranted')
  980. ->with($this->equalTo('EDIT'))
  981. ->will($this->returnValue(true));
  982. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  983. ->disableOriginalConstructor()
  984. ->getMock();
  985. $this->admin->expects($this->once())
  986. ->method('getForm')
  987. ->will($this->returnValue($form));
  988. $form->expects($this->once())
  989. ->method('isValid')
  990. ->will($this->returnValue(false));
  991. $this->expectTranslate('flash_edit_error');
  992. $this->request->setMethod('POST');
  993. $formView = $this->getMock('Symfony\Component\Form\FormView');
  994. $form->expects($this->any())
  995. ->method('createView')
  996. ->will($this->returnValue($formView));
  997. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  998. $this->assertEquals($this->admin, $this->parameters['admin']);
  999. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1000. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1001. $this->assertEquals('edit', $this->parameters['action']);
  1002. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1003. $this->assertEquals($object, $this->parameters['object']);
  1004. $this->assertEquals(array('sonata_flash_error'=>array('flash_edit_error')), $this->session->getFlashBag()->all());
  1005. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1006. }
  1007. public function testEditActionAjaxSuccess()
  1008. {
  1009. $object = new \stdClass();
  1010. $this->admin->expects($this->once())
  1011. ->method('getObject')
  1012. ->will($this->returnValue($object));
  1013. $this->admin->expects($this->once())
  1014. ->method('update')
  1015. ->will($this->returnArgument(0));
  1016. $this->admin->expects($this->once())
  1017. ->method('isGranted')
  1018. ->with($this->equalTo('EDIT'))
  1019. ->will($this->returnValue(true));
  1020. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1021. ->disableOriginalConstructor()
  1022. ->getMock();
  1023. $this->admin->expects($this->once())
  1024. ->method('getForm')
  1025. ->will($this->returnValue($form));
  1026. $form->expects($this->once())
  1027. ->method('isValid')
  1028. ->will($this->returnValue(true));
  1029. $this->admin->expects($this->once())
  1030. ->method('getNormalizedIdentifier')
  1031. ->with($this->equalTo($object))
  1032. ->will($this->returnValue('foo_normalized'));
  1033. $this->request->setMethod('POST');
  1034. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1035. $response = $this->controller->editAction();
  1036. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  1037. $this->assertEquals(json_encode(array('result'=>'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  1038. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1039. }
  1040. public function testEditActionAjaxError()
  1041. {
  1042. $object = new \stdClass();
  1043. $this->admin->expects($this->once())
  1044. ->method('getObject')
  1045. ->will($this->returnValue($object));
  1046. $this->admin->expects($this->once())
  1047. ->method('isGranted')
  1048. ->with($this->equalTo('EDIT'))
  1049. ->will($this->returnValue(true));
  1050. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1051. ->disableOriginalConstructor()
  1052. ->getMock();
  1053. $this->admin->expects($this->once())
  1054. ->method('getForm')
  1055. ->will($this->returnValue($form));
  1056. $form->expects($this->once())
  1057. ->method('isValid')
  1058. ->will($this->returnValue(false));
  1059. $this->request->setMethod('POST');
  1060. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1061. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1062. $form->expects($this->any())
  1063. ->method('createView')
  1064. ->will($this->returnValue($formView));
  1065. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  1066. $this->assertEquals($this->admin, $this->parameters['admin']);
  1067. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  1068. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1069. $this->assertEquals('edit', $this->parameters['action']);
  1070. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1071. $this->assertEquals($object, $this->parameters['object']);
  1072. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1073. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1074. }
  1075. public function testEditActionWithPreview()
  1076. {
  1077. $object = new \stdClass();
  1078. $this->admin->expects($this->once())
  1079. ->method('getObject')
  1080. ->will($this->returnValue($object));
  1081. $this->admin->expects($this->once())
  1082. ->method('isGranted')
  1083. ->with($this->equalTo('EDIT'))
  1084. ->will($this->returnValue(true));
  1085. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1086. ->disableOriginalConstructor()
  1087. ->getMock();
  1088. $this->admin->expects($this->once())
  1089. ->method('getForm')
  1090. ->will($this->returnValue($form));
  1091. $this->admin->expects($this->once())
  1092. ->method('supportsPreviewMode')
  1093. ->will($this->returnValue(true));
  1094. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1095. $form->expects($this->any())
  1096. ->method('createView')
  1097. ->will($this->returnValue($formView));
  1098. $form->expects($this->once())
  1099. ->method('isValid')
  1100. ->will($this->returnValue(true));
  1101. $this->request->setMethod('POST');
  1102. $this->request->request->set('btn_preview', 'Preview');
  1103. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
  1104. $this->assertEquals($this->admin, $this->parameters['admin']);
  1105. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1106. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1107. $this->assertEquals('edit', $this->parameters['action']);
  1108. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1109. $this->assertEquals($object, $this->parameters['object']);
  1110. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1111. $this->assertEquals('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
  1112. }
  1113. public function testCreateActionAccessDenied()
  1114. {
  1115. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1116. $this->admin->expects($this->once())
  1117. ->method('isGranted')
  1118. ->with($this->equalTo('CREATE'))
  1119. ->will($this->returnValue(false));
  1120. $this->controller->createAction();
  1121. }
  1122. public function testCreateAction()
  1123. {
  1124. $this->admin->expects($this->once())
  1125. ->method('isGranted')
  1126. ->with($this->equalTo('CREATE'))
  1127. ->will($this->returnValue(true));
  1128. $object = new \stdClass();
  1129. $this->admin->expects($this->once())
  1130. ->method('getNewInstance')
  1131. ->will($this->returnValue($object));
  1132. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1133. ->disableOriginalConstructor()
  1134. ->getMock();
  1135. $this->admin->expects($this->once())
  1136. ->method('getForm')
  1137. ->will($this->returnValue($form));
  1138. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1139. $form->expects($this->any())
  1140. ->method('createView')
  1141. ->will($this->returnValue($formView));
  1142. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1143. $this->assertEquals($this->admin, $this->parameters['admin']);
  1144. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1145. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1146. $this->assertEquals('create', $this->parameters['action']);
  1147. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1148. $this->assertEquals($object, $this->parameters['object']);
  1149. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1150. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1151. }
  1152. public function testCreateActionSuccess()
  1153. {
  1154. $object = new \stdClass();
  1155. $this->admin->expects($this->exactly(2))
  1156. ->method('isGranted')
  1157. ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
  1158. if ($name != 'CREATE') {
  1159. return false;
  1160. }
  1161. if ($objectIn === null) {
  1162. return true;
  1163. }
  1164. return ($objectIn === $object);
  1165. }));
  1166. $this->admin->expects($this->once())
  1167. ->method('getNewInstance')
  1168. ->will($this->returnValue($object));
  1169. $this->admin->expects($this->once())
  1170. ->method('create')
  1171. ->will($this->returnArgument(0));
  1172. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1173. ->disableOriginalConstructor()
  1174. ->getMock();
  1175. $this->admin->expects($this->once())
  1176. ->method('getForm')
  1177. ->will($this->returnValue($form));
  1178. $form->expects($this->once())
  1179. ->method('isValid')
  1180. ->will($this->returnValue(true));
  1181. $this->expectTranslate('flash_create_success');
  1182. $this->request->setMethod('POST');
  1183. $response = $this->controller->createAction();
  1184. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1185. $this->assertSame(array('flash_create_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1186. $this->assertEquals('stdClass_edit', $response->getTargetUrl());
  1187. }
  1188. public function testCreateActionAccessDenied2()
  1189. {
  1190. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1191. $object = new \stdClass();
  1192. $this->admin->expects($this->any())
  1193. ->method('isGranted')
  1194. ->will($this->returnCallback(function ($name, $object = null) {
  1195. if ($name != 'CREATE') {
  1196. return false;
  1197. }
  1198. if ($object === null) {
  1199. return true;
  1200. }
  1201. return false;
  1202. }));
  1203. $this->admin->expects($this->once())
  1204. ->method('getNewInstance')
  1205. ->will($this->returnValue($object));
  1206. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1207. ->disableOriginalConstructor()
  1208. ->getMock();
  1209. $this->admin->expects($this->once())
  1210. ->method('getForm')
  1211. ->will($this->returnValue($form));
  1212. $form->expects($this->once())
  1213. ->method('isValid')
  1214. ->will($this->returnValue(true));
  1215. $this->request->setMethod('POST');
  1216. $this->controller->createAction();
  1217. }
  1218. public function testCreateActionError()
  1219. {
  1220. $this->admin->expects($this->once())
  1221. ->method('isGranted')
  1222. ->with($this->equalTo('CREATE'))
  1223. ->will($this->returnValue(true));
  1224. $object = new \stdClass();
  1225. $this->admin->expects($this->once())
  1226. ->method('getNewInstance')
  1227. ->will($this->returnValue($object));
  1228. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1229. ->disableOriginalConstructor()
  1230. ->getMock();
  1231. $this->admin->expects($this->once())
  1232. ->method('getForm')
  1233. ->will($this->returnValue($form));
  1234. $form->expects($this->once())
  1235. ->method('isValid')
  1236. ->will($this->returnValue(false));
  1237. $this->expectTranslate('flash_create_error');
  1238. $this->request->setMethod('POST');
  1239. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1240. $form->expects($this->any())
  1241. ->method('createView')
  1242. ->will($this->returnValue($formView));
  1243. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1244. $this->assertEquals($this->admin, $this->parameters['admin']);
  1245. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1246. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1247. $this->assertEquals('create', $this->parameters['action']);
  1248. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1249. $this->assertEquals($object, $this->parameters['object']);
  1250. $this->assertEquals(array('sonata_flash_error'=>array('flash_create_error')), $this->session->getFlashBag()->all());
  1251. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1252. }
  1253. public function testCreateActionAjaxSuccess()
  1254. {
  1255. $object = new \stdClass();
  1256. $this->admin->expects($this->exactly(2))
  1257. ->method('isGranted')
  1258. ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
  1259. if ($name != 'CREATE') {
  1260. return false;
  1261. }
  1262. if ($objectIn === null) {
  1263. return true;
  1264. }
  1265. return ($objectIn === $object);
  1266. }));
  1267. $this->admin->expects($this->once())
  1268. ->method('getNewInstance')
  1269. ->will($this->returnValue($object));
  1270. $this->admin->expects($this->once())
  1271. ->method('create')
  1272. ->will($this->returnArgument(0));
  1273. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1274. ->disableOriginalConstructor()
  1275. ->getMock();
  1276. $this->admin->expects($this->once())
  1277. ->method('getForm')
  1278. ->will($this->returnValue($form));
  1279. $form->expects($this->once())
  1280. ->method('isValid')
  1281. ->will($this->returnValue(true));
  1282. $this->admin->expects($this->once())
  1283. ->method('getNormalizedIdentifier')
  1284. ->with($this->equalTo($object))
  1285. ->will($this->returnValue('foo_normalized'));
  1286. $this->request->setMethod('POST');
  1287. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1288. $response = $this->controller->createAction();
  1289. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  1290. $this->assertEquals(json_encode(array('result'=>'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  1291. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1292. }
  1293. public function testCreateActionAjaxError()
  1294. {
  1295. $this->admin->expects($this->once())
  1296. ->method('isGranted')
  1297. ->with($this->equalTo('CREATE'))
  1298. ->will($this->returnValue(true));
  1299. $object = new \stdClass();
  1300. $this->admin->expects($this->once())
  1301. ->method('getNewInstance')
  1302. ->will($this->returnValue($object));
  1303. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1304. ->disableOriginalConstructor()
  1305. ->getMock();
  1306. $this->admin->expects($this->once())
  1307. ->method('getForm')
  1308. ->will($this->returnValue($form));
  1309. $form->expects($this->once())
  1310. ->method('isValid')
  1311. ->will($this->returnValue(false));
  1312. $this->request->setMethod('POST');
  1313. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1314. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1315. $form->expects($this->any())
  1316. ->method('createView')
  1317. ->will($this->returnValue($formView));
  1318. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1319. $this->assertEquals($this->admin, $this->parameters['admin']);
  1320. $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  1321. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1322. $this->assertEquals('create', $this->parameters['action']);
  1323. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1324. $this->assertEquals($object, $this->parameters['object']);
  1325. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1326. $this->assertEquals('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1327. }
  1328. public function testCreateActionWithPreview()
  1329. {
  1330. $this->admin->expects($this->once())
  1331. ->method('isGranted')
  1332. ->with($this->equalTo('CREATE'))
  1333. ->will($this->returnValue(true));
  1334. $object = new \stdClass();
  1335. $this->admin->expects($this->once())
  1336. ->method('getNewInstance')
  1337. ->will($this->returnValue($object));
  1338. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1339. ->disableOriginalConstructor()
  1340. ->getMock();
  1341. $this->admin->expects($this->once())
  1342. ->method('getForm')
  1343. ->will($this->returnValue($form));
  1344. $this->admin->expects($this->once())
  1345. ->method('supportsPreviewMode')
  1346. ->will($this->returnValue(true));
  1347. $formView = $this->getMock('Symfony\Component\Form\FormView');
  1348. $form->expects($this->any())
  1349. ->method('createView')
  1350. ->will($this->returnValue($formView));
  1351. $form->expects($this->once())
  1352. ->method('isValid')
  1353. ->will($this->returnValue(true));
  1354. $this->request->setMethod('POST');
  1355. $this->request->request->set('btn_preview', 'Preview');
  1356. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction());
  1357. $this->assertEquals($this->admin, $this->parameters['admin']);
  1358. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1359. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1360. $this->assertEquals('create', $this->parameters['action']);
  1361. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1362. $this->assertEquals($object, $this->parameters['object']);
  1363. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1364. $this->assertEquals('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
  1365. }
  1366. public function testExportActionAccessDenied()
  1367. {
  1368. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1369. $this->admin->expects($this->once())
  1370. ->method('isGranted')
  1371. ->with($this->equalTo('EXPORT'))
  1372. ->will($this->returnValue(false));
  1373. $this->controller->exportAction($this->request);
  1374. }
  1375. public function testExportActionWrongFormat()
  1376. {
  1377. $this->setExpectedException('RuntimeException', 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`');
  1378. $this->admin->expects($this->once())
  1379. ->method('isGranted')
  1380. ->with($this->equalTo('EXPORT'))
  1381. ->will($this->returnValue(true));
  1382. $this->admin->expects($this->once())
  1383. ->method('getExportFormats')
  1384. ->will($this->returnValue(array('json')));
  1385. $this->admin->expects($this->once())
  1386. ->method('getClass')
  1387. ->will($this->returnValue('Foo'));
  1388. $this->request->query->set('format', 'csv');
  1389. $this->controller->exportAction($this->request);
  1390. }
  1391. public function testExportAction()
  1392. {
  1393. $this->admin->expects($this->once())
  1394. ->method('isGranted')
  1395. ->with($this->equalTo('EXPORT'))
  1396. ->will($this->returnValue(true));
  1397. $this->admin->expects($this->once())
  1398. ->method('getExportFormats')
  1399. ->will($this->returnValue(array('json')));
  1400. $dataSourceIterator = $this->getMock('Exporter\Source\SourceIteratorInterface');
  1401. $this->admin->expects($this->once())
  1402. ->method('getDataSourceIterator')
  1403. ->will($this->returnValue($dataSourceIterator));
  1404. $this->request->query->set('format', 'json');
  1405. $response = $this->controller->exportAction($this->request);
  1406. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  1407. $this->assertEquals(200, $response->getStatusCode());
  1408. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1409. }
  1410. public function testHistoryActionAccessDenied()
  1411. {
  1412. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1413. $this->admin->expects($this->any())
  1414. ->method('getObject')
  1415. ->will($this->returnValue(new \StdClass()));
  1416. $this->admin->expects($this->once())
  1417. ->method('isGranted')
  1418. ->with($this->equalTo('EDIT'))
  1419. ->will($this->returnValue(false));
  1420. $this->controller->historyAction();
  1421. }
  1422. public function testHistoryActionNotFoundException()
  1423. {
  1424. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1425. $this->admin->expects($this->once())
  1426. ->method('getObject')
  1427. ->will($this->returnValue(false));
  1428. $this->controller->historyAction();
  1429. }
  1430. public function testHistoryActionNoReader()
  1431. {
  1432. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  1433. $this->request->query->set('id', 123);
  1434. $this->admin->expects($this->once())
  1435. ->method('isGranted')
  1436. ->with($this->equalTo('EDIT'))
  1437. ->will($this->returnValue(true));
  1438. $object = new \stdClass();
  1439. $this->admin->expects($this->once())
  1440. ->method('getObject')
  1441. ->will($this->returnValue($object));
  1442. $this->admin->expects($this->any())
  1443. ->method('getClass')
  1444. ->will($this->returnValue('Foo'));
  1445. $this->auditManager->expects($this->once())
  1446. ->method('hasReader')
  1447. ->with($this->equalTo('Foo'))
  1448. ->will($this->returnValue(false));
  1449. $this->controller->historyAction();
  1450. }
  1451. public function testHistoryAction()
  1452. {
  1453. $this->request->query->set('id', 123);
  1454. $this->admin->expects($this->once())
  1455. ->method('isGranted')
  1456. ->with($this->equalTo('EDIT'))
  1457. ->will($this->returnValue(true));
  1458. $object = new \stdClass();
  1459. $this->admin->expects($this->once())
  1460. ->method('getObject')
  1461. ->will($this->returnValue($object));
  1462. $this->admin->expects($this->any())
  1463. ->method('getClass')
  1464. ->will($this->returnValue('Foo'));
  1465. $this->auditManager->expects($this->once())
  1466. ->method('hasReader')
  1467. ->with($this->equalTo('Foo'))
  1468. ->will($this->returnValue(true));
  1469. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1470. $this->auditManager->expects($this->once())
  1471. ->method('getReader')
  1472. ->with($this->equalTo('Foo'))
  1473. ->will($this->returnValue($reader));
  1474. $reader->expects($this->once())
  1475. ->method('findRevisions')
  1476. ->with($this->equalTo('Foo'), $this->equalTo(123))
  1477. ->will($this->returnValue(array()));
  1478. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyAction());
  1479. $this->assertEquals($this->admin, $this->parameters['admin']);
  1480. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1481. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1482. $this->assertEquals('history', $this->parameters['action']);
  1483. $this->assertEquals(array(), $this->parameters['revisions']);
  1484. $this->assertEquals($object, $this->parameters['object']);
  1485. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1486. $this->assertEquals('SonataAdminBundle:CRUD:history.html.twig', $this->template);
  1487. }
  1488. public function testAclActionAclNotEnabled()
  1489. {
  1490. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'ACL are not enabled for this admin');
  1491. $this->controller->aclAction();
  1492. }
  1493. public function testAclActionNotFoundException()
  1494. {
  1495. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1496. $this->admin->expects($this->once())
  1497. ->method('isAclEnabled')
  1498. ->will($this->returnValue(true));
  1499. $this->admin->expects($this->once())
  1500. ->method('getObject')
  1501. ->will($this->returnValue(false));
  1502. $this->controller->aclAction();
  1503. }
  1504. public function testAclActionAccessDenied()
  1505. {
  1506. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1507. $this->admin->expects($this->once())
  1508. ->method('isAclEnabled')
  1509. ->will($this->returnValue(true));
  1510. $object = new \stdClass();
  1511. $this->admin->expects($this->once())
  1512. ->method('getObject')
  1513. ->will($this->returnValue($object));
  1514. $this->admin->expects($this->once())
  1515. ->method('isGranted')
  1516. ->with($this->equalTo('MASTER'), $this->equalTo($object))
  1517. ->will($this->returnValue(false));
  1518. $this->controller->aclAction();
  1519. }
  1520. public function testAclAction()
  1521. {
  1522. $this->request->query->set('id', 123);
  1523. $this->admin->expects($this->once())
  1524. ->method('isAclEnabled')
  1525. ->will($this->returnValue(true));
  1526. $object = new \stdClass();
  1527. $this->admin->expects($this->once())
  1528. ->method('getObject')
  1529. ->will($this->returnValue($object));
  1530. $this->admin->expects($this->any())
  1531. ->method('isGranted')
  1532. ->will($this->returnValue(true));
  1533. $this->adminObjectAclManipulator->expects($this->once())
  1534. ->method('getMaskBuilderClass')
  1535. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  1536. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1537. ->disableOriginalConstructor()
  1538. ->getMock();
  1539. $form->expects($this->once())
  1540. ->method('createView')
  1541. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  1542. $this->adminObjectAclManipulator->expects($this->once())
  1543. ->method('createForm')
  1544. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  1545. ->will($this->returnValue($form));
  1546. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  1547. ->disableOriginalConstructor()
  1548. ->getMock();
  1549. $aclSecurityHandler->expects($this->any())
  1550. ->method('getObjectPermissions')
  1551. ->will($this->returnValue(array()));
  1552. $this->admin->expects($this->any())
  1553. ->method('getSecurityHandler')
  1554. ->will($this->returnValue($aclSecurityHandler));
  1555. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction());
  1556. $this->assertEquals($this->admin, $this->parameters['admin']);
  1557. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1558. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1559. $this->assertEquals('acl', $this->parameters['action']);
  1560. $this->assertEquals(array(), $this->parameters['permissions']);
  1561. $this->assertEquals($object, $this->parameters['object']);
  1562. $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
  1563. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1564. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1565. $this->assertEquals('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
  1566. }
  1567. public function testAclActionInvalidUpdate()
  1568. {
  1569. $this->request->query->set('id', 123);
  1570. $this->admin->expects($this->once())
  1571. ->method('isAclEnabled')
  1572. ->will($this->returnValue(true));
  1573. $object = new \stdClass();
  1574. $this->admin->expects($this->once())
  1575. ->method('getObject')
  1576. ->will($this->returnValue($object));
  1577. $this->admin->expects($this->any())
  1578. ->method('isGranted')
  1579. ->will($this->returnValue(true));
  1580. $this->adminObjectAclManipulator->expects($this->once())
  1581. ->method('getMaskBuilderClass')
  1582. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  1583. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1584. ->disableOriginalConstructor()
  1585. ->getMock();
  1586. $form->expects($this->once())
  1587. ->method('isValid')
  1588. ->will($this->returnValue(false));
  1589. $form->expects($this->once())
  1590. ->method('createView')
  1591. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  1592. $this->adminObjectAclManipulator->expects($this->once())
  1593. ->method('createForm')
  1594. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  1595. ->will($this->returnValue($form));
  1596. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  1597. ->disableOriginalConstructor()
  1598. ->getMock();
  1599. $aclSecurityHandler->expects($this->any())
  1600. ->method('getObjectPermissions')
  1601. ->will($this->returnValue(array()));
  1602. $this->admin->expects($this->any())
  1603. ->method('getSecurityHandler')
  1604. ->will($this->returnValue($aclSecurityHandler));
  1605. $this->request->setMethod('POST');
  1606. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction());
  1607. $this->assertEquals($this->admin, $this->parameters['admin']);
  1608. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1609. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1610. $this->assertEquals('acl', $this->parameters['action']);
  1611. $this->assertEquals(array(), $this->parameters['permissions']);
  1612. $this->assertEquals($object, $this->parameters['object']);
  1613. $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
  1614. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1615. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1616. $this->assertEquals('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
  1617. }
  1618. public function testAclActionSuccessfulUpdate()
  1619. {
  1620. $this->request->query->set('id', 123);
  1621. $this->admin->expects($this->once())
  1622. ->method('isAclEnabled')
  1623. ->will($this->returnValue(true));
  1624. $object = new \stdClass();
  1625. $this->admin->expects($this->once())
  1626. ->method('getObject')
  1627. ->will($this->returnValue($object));
  1628. $this->admin->expects($this->any())
  1629. ->method('isGranted')
  1630. ->will($this->returnValue(true));
  1631. $this->adminObjectAclManipulator->expects($this->once())
  1632. ->method('getMaskBuilderClass')
  1633. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  1634. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1635. ->disableOriginalConstructor()
  1636. ->getMock();
  1637. $form->expects($this->any())
  1638. ->method('createView')
  1639. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  1640. $form->expects($this->once())
  1641. ->method('isValid')
  1642. ->will($this->returnValue(true));
  1643. $this->adminObjectAclManipulator->expects($this->once())
  1644. ->method('createForm')
  1645. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  1646. ->will($this->returnValue($form));
  1647. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  1648. ->disableOriginalConstructor()
  1649. ->getMock();
  1650. $aclSecurityHandler->expects($this->any())
  1651. ->method('getObjectPermissions')
  1652. ->will($this->returnValue(array()));
  1653. $this->admin->expects($this->any())
  1654. ->method('getSecurityHandler')
  1655. ->will($this->returnValue($aclSecurityHandler));
  1656. $this->request->setMethod('POST');
  1657. $response = $this->controller->aclAction();
  1658. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1659. $this->assertSame(array('flash_acl_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1660. $this->assertEquals('stdClass_acl', $response->getTargetUrl());
  1661. }
  1662. public function testHistoryViewRevisionActionAccessDenied()
  1663. {
  1664. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1665. $this->admin->expects($this->any())
  1666. ->method('getObject')
  1667. ->will($this->returnValue(new \StdClass()));
  1668. $this->admin->expects($this->once())
  1669. ->method('isGranted')
  1670. ->with($this->equalTo('EDIT'))
  1671. ->will($this->returnValue(false));
  1672. $this->controller->historyViewRevisionAction();
  1673. }
  1674. public function testHistoryViewRevisionActionNotFoundException()
  1675. {
  1676. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id : 123');
  1677. $this->request->query->set('id', 123);
  1678. $this->admin->expects($this->once())
  1679. ->method('getObject')
  1680. ->will($this->returnValue(false));
  1681. $this->controller->historyViewRevisionAction();
  1682. }
  1683. public function testHistoryViewRevisionActionNoReader()
  1684. {
  1685. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  1686. $this->request->query->set('id', 123);
  1687. $this->admin->expects($this->once())
  1688. ->method('isGranted')
  1689. ->with($this->equalTo('EDIT'))
  1690. ->will($this->returnValue(true));
  1691. $object = new \stdClass();
  1692. $this->admin->expects($this->once())
  1693. ->method('getObject')
  1694. ->will($this->returnValue($object));
  1695. $this->admin->expects($this->any())
  1696. ->method('getClass')
  1697. ->will($this->returnValue('Foo'));
  1698. $this->auditManager->expects($this->once())
  1699. ->method('hasReader')
  1700. ->with($this->equalTo('Foo'))
  1701. ->will($this->returnValue(false));
  1702. $this->controller->historyViewRevisionAction();
  1703. }
  1704. public function testHistoryViewRevisionActionNotFoundRevision()
  1705. {
  1706. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`');
  1707. $this->request->query->set('id', 123);
  1708. $this->admin->expects($this->once())
  1709. ->method('isGranted')
  1710. ->with($this->equalTo('EDIT'))
  1711. ->will($this->returnValue(true));
  1712. $object = new \stdClass();
  1713. $this->admin->expects($this->once())
  1714. ->method('getObject')
  1715. ->will($this->returnValue($object));
  1716. $this->admin->expects($this->any())
  1717. ->method('getClass')
  1718. ->will($this->returnValue('Foo'));
  1719. $this->auditManager->expects($this->once())
  1720. ->method('hasReader')
  1721. ->with($this->equalTo('Foo'))
  1722. ->will($this->returnValue(true));
  1723. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1724. $this->auditManager->expects($this->once())
  1725. ->method('getReader')
  1726. ->with($this->equalTo('Foo'))
  1727. ->will($this->returnValue($reader));
  1728. $reader->expects($this->once())
  1729. ->method('find')
  1730. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  1731. ->will($this->returnValue(null));
  1732. $this->controller->historyViewRevisionAction(123, 456);
  1733. }
  1734. public function testHistoryViewRevisionAction()
  1735. {
  1736. $this->request->query->set('id', 123);
  1737. $this->admin->expects($this->once())
  1738. ->method('isGranted')
  1739. ->with($this->equalTo('EDIT'))
  1740. ->will($this->returnValue(true));
  1741. $object = new \stdClass();
  1742. $this->admin->expects($this->once())
  1743. ->method('getObject')
  1744. ->will($this->returnValue($object));
  1745. $this->admin->expects($this->any())
  1746. ->method('getClass')
  1747. ->will($this->returnValue('Foo'));
  1748. $this->auditManager->expects($this->once())
  1749. ->method('hasReader')
  1750. ->with($this->equalTo('Foo'))
  1751. ->will($this->returnValue(true));
  1752. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1753. $this->auditManager->expects($this->once())
  1754. ->method('getReader')
  1755. ->with($this->equalTo('Foo'))
  1756. ->will($this->returnValue($reader));
  1757. $objectRevision = new \stdClass();
  1758. $objectRevision->revision = 456;
  1759. $reader->expects($this->once())
  1760. ->method('find')
  1761. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  1762. ->will($this->returnValue($objectRevision));
  1763. $this->admin->expects($this->once())
  1764. ->method('setSubject')
  1765. ->with($this->equalTo($objectRevision))
  1766. ->will($this->returnValue(null));
  1767. $fieldDescriptionCollection = new FieldDescriptionCollection();
  1768. $this->admin->expects($this->once())
  1769. ->method('getShow')
  1770. ->will($this->returnValue($fieldDescriptionCollection));
  1771. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyViewRevisionAction(123, 456));
  1772. $this->assertEquals($this->admin, $this->parameters['admin']);
  1773. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1774. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1775. $this->assertEquals('show', $this->parameters['action']);
  1776. $this->assertEquals($objectRevision, $this->parameters['object']);
  1777. $this->assertEquals($fieldDescriptionCollection, $this->parameters['elements']);
  1778. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1779. $this->assertEquals('SonataAdminBundle:CRUD:show.html.twig', $this->template);
  1780. }
  1781. public function testhistoryCompareRevisionsActionAccessDenied()
  1782. {
  1783. $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1784. $this->admin->expects($this->once())
  1785. ->method('isGranted')
  1786. ->with($this->equalTo('EDIT'))
  1787. ->will($this->returnValue(false));
  1788. $this->controller->historyCompareRevisionsAction();
  1789. }
  1790. public function testhistoryCompareRevisionsActionNotFoundException()
  1791. {
  1792. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id : 123');
  1793. $this->request->query->set('id', 123);
  1794. $this->admin->expects($this->once())
  1795. ->method('isGranted')
  1796. ->with($this->equalTo('EDIT'))
  1797. ->will($this->returnValue(true));
  1798. $this->admin->expects($this->once())
  1799. ->method('getObject')
  1800. ->will($this->returnValue(false));
  1801. $this->controller->historyCompareRevisionsAction();
  1802. }
  1803. public function testhistoryCompareRevisionsActionNoReader()
  1804. {
  1805. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  1806. $this->request->query->set('id', 123);
  1807. $this->admin->expects($this->once())
  1808. ->method('isGranted')
  1809. ->with($this->equalTo('EDIT'))
  1810. ->will($this->returnValue(true));
  1811. $object = new \stdClass();
  1812. $this->admin->expects($this->once())
  1813. ->method('getObject')
  1814. ->will($this->returnValue($object));
  1815. $this->admin->expects($this->any())
  1816. ->method('getClass')
  1817. ->will($this->returnValue('Foo'));
  1818. $this->auditManager->expects($this->once())
  1819. ->method('hasReader')
  1820. ->with($this->equalTo('Foo'))
  1821. ->will($this->returnValue(false));
  1822. $this->controller->historyCompareRevisionsAction();
  1823. }
  1824. public function testhistoryCompareRevisionsActionNotFoundBaseRevision()
  1825. {
  1826. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`');
  1827. $this->request->query->set('id', 123);
  1828. $this->admin->expects($this->once())
  1829. ->method('isGranted')
  1830. ->with($this->equalTo('EDIT'))
  1831. ->will($this->returnValue(true));
  1832. $object = new \stdClass();
  1833. $this->admin->expects($this->once())
  1834. ->method('getObject')
  1835. ->will($this->returnValue($object));
  1836. $this->admin->expects($this->any())
  1837. ->method('getClass')
  1838. ->will($this->returnValue('Foo'));
  1839. $this->auditManager->expects($this->once())
  1840. ->method('hasReader')
  1841. ->with($this->equalTo('Foo'))
  1842. ->will($this->returnValue(true));
  1843. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1844. $this->auditManager->expects($this->once())
  1845. ->method('getReader')
  1846. ->with($this->equalTo('Foo'))
  1847. ->will($this->returnValue($reader));
  1848. // once because it will not be found and therefore the second call won't be executed
  1849. $reader->expects($this->once())
  1850. ->method('find')
  1851. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  1852. ->will($this->returnValue(null));
  1853. $this->controller->historyCompareRevisionsAction(123, 456, 789);
  1854. }
  1855. public function testhistoryCompareRevisionsActionNotFoundCompareRevision()
  1856. {
  1857. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `789` with classname : `Foo`');
  1858. $this->request->query->set('id', 123);
  1859. $this->admin->expects($this->once())
  1860. ->method('isGranted')
  1861. ->with($this->equalTo('EDIT'))
  1862. ->will($this->returnValue(true));
  1863. $object = new \stdClass();
  1864. $this->admin->expects($this->once())
  1865. ->method('getObject')
  1866. ->will($this->returnValue($object));
  1867. $this->admin->expects($this->any())
  1868. ->method('getClass')
  1869. ->will($this->returnValue('Foo'));
  1870. $this->auditManager->expects($this->once())
  1871. ->method('hasReader')
  1872. ->with($this->equalTo('Foo'))
  1873. ->will($this->returnValue(true));
  1874. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1875. $this->auditManager->expects($this->once())
  1876. ->method('getReader')
  1877. ->with($this->equalTo('Foo'))
  1878. ->will($this->returnValue($reader));
  1879. $objectRevision = new \stdClass();
  1880. $objectRevision->revision = 456;
  1881. // first call should return, so the second call will throw an exception
  1882. $reader->expects($this->at(0))
  1883. ->method('find')
  1884. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  1885. ->will($this->returnValue($objectRevision));
  1886. $reader->expects($this->at(1))
  1887. ->method('find')
  1888. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
  1889. ->will($this->returnValue(null));
  1890. $this->controller->historyCompareRevisionsAction(123, 456, 789);
  1891. }
  1892. public function testhistoryCompareRevisionsActionAction()
  1893. {
  1894. $this->request->query->set('id', 123);
  1895. $this->admin->expects($this->once())
  1896. ->method('isGranted')
  1897. ->with($this->equalTo('EDIT'))
  1898. ->will($this->returnValue(true));
  1899. $object = new \stdClass();
  1900. $this->admin->expects($this->once())
  1901. ->method('getObject')
  1902. ->will($this->returnValue($object));
  1903. $this->admin->expects($this->any())
  1904. ->method('getClass')
  1905. ->will($this->returnValue('Foo'));
  1906. $this->auditManager->expects($this->once())
  1907. ->method('hasReader')
  1908. ->with($this->equalTo('Foo'))
  1909. ->will($this->returnValue(true));
  1910. $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1911. $this->auditManager->expects($this->once())
  1912. ->method('getReader')
  1913. ->with($this->equalTo('Foo'))
  1914. ->will($this->returnValue($reader));
  1915. $objectRevision = new \stdClass();
  1916. $objectRevision->revision = 456;
  1917. $compareObjectRevision = new \stdClass();
  1918. $compareObjectRevision->revision = 789;
  1919. $reader->expects($this->at(0))
  1920. ->method('find')
  1921. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  1922. ->will($this->returnValue($objectRevision));
  1923. $reader->expects($this->at(1))
  1924. ->method('find')
  1925. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
  1926. ->will($this->returnValue($compareObjectRevision));
  1927. $this->admin->expects($this->once())
  1928. ->method('setSubject')
  1929. ->with($this->equalTo($objectRevision))
  1930. ->will($this->returnValue(null));
  1931. $fieldDescriptionCollection = new FieldDescriptionCollection();
  1932. $this->admin->expects($this->once())
  1933. ->method('getShow')
  1934. ->will($this->returnValue($fieldDescriptionCollection));
  1935. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyCompareRevisionsAction(123, 456, 789));
  1936. $this->assertEquals($this->admin, $this->parameters['admin']);
  1937. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1938. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  1939. $this->assertEquals('show', $this->parameters['action']);
  1940. $this->assertEquals($objectRevision, $this->parameters['object']);
  1941. $this->assertEquals($compareObjectRevision, $this->parameters['object_compare']);
  1942. $this->assertEquals($fieldDescriptionCollection, $this->parameters['elements']);
  1943. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  1944. $this->assertEquals('SonataAdminBundle:CRUD:show_compare.html.twig', $this->template);
  1945. }
  1946. public function testBatchActionWrongMethod()
  1947. {
  1948. $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'Invalid request type "GET", POST expected');
  1949. $this->controller->batchAction();
  1950. }
  1951. public function testBatchActionActionNotDefined()
  1952. {
  1953. $this->setExpectedException('RuntimeException', 'The `foo` batch action is not defined');
  1954. $batchActions = array();
  1955. $this->admin->expects($this->once())
  1956. ->method('getBatchActions')
  1957. ->will($this->returnValue($batchActions));
  1958. $this->request->setMethod('POST');
  1959. $this->request->request->set('data', json_encode(array('action'=>'foo', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  1960. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1961. $this->controller->batchAction();
  1962. }
  1963. public function testBatchActionActionInvalidCsrfToken()
  1964. {
  1965. $this->request->setMethod('POST');
  1966. $this->request->request->set('data', json_encode(array('action'=>'foo', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  1967. $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
  1968. try {
  1969. $this->controller->batchAction();
  1970. } catch (HttpException $e) {
  1971. $this->assertEquals('The csrf token is not valid, CSRF attack?', $e->getMessage());
  1972. $this->assertEquals(400, $e->getStatusCode());
  1973. }
  1974. }
  1975. public function testBatchActionMethodNotExist()
  1976. {
  1977. $this->setExpectedException('RuntimeException', 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be created');
  1978. $batchActions = array('foo'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1979. $this->admin->expects($this->once())
  1980. ->method('getBatchActions')
  1981. ->will($this->returnValue($batchActions));
  1982. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1983. $this->admin->expects($this->once())
  1984. ->method('getDatagrid')
  1985. ->will($this->returnValue($datagrid));
  1986. $this->request->setMethod('POST');
  1987. $this->request->request->set('data', json_encode(array('action'=>'foo', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  1988. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  1989. $this->controller->batchAction();
  1990. }
  1991. public function testBatchActionWithoutConfirmation()
  1992. {
  1993. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  1994. $this->admin->expects($this->once())
  1995. ->method('getBatchActions')
  1996. ->will($this->returnValue($batchActions));
  1997. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  1998. $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  1999. $datagrid->expects($this->once())
  2000. ->method('getQuery')
  2001. ->will($this->returnValue($query));
  2002. $this->admin->expects($this->once())
  2003. ->method('getDatagrid')
  2004. ->will($this->returnValue($datagrid));
  2005. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  2006. $this->admin->expects($this->once())
  2007. ->method('isGranted')
  2008. ->with($this->equalTo('DELETE'))
  2009. ->will($this->returnValue(true));
  2010. $this->admin->expects($this->any())
  2011. ->method('getModelManager')
  2012. ->will($this->returnValue($modelManager));
  2013. $this->admin->expects($this->any())
  2014. ->method('getClass')
  2015. ->will($this->returnValue('Foo'));
  2016. $modelManager->expects($this->once())
  2017. ->method('addIdentifiersToQuery')
  2018. ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
  2019. ->will($this->returnValue(true));
  2020. $this->request->setMethod('POST');
  2021. $this->request->request->set('data', json_encode(array('action'=>'delete', 'idx'=>array('123', '456'), 'all_elements'=>false)));
  2022. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2023. $result = $this->controller->batchAction();
  2024. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2025. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  2026. $this->assertEquals('list?', $result->getTargetUrl());
  2027. }
  2028. public function testBatchActionWithoutConfirmation2()
  2029. {
  2030. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  2031. $this->admin->expects($this->once())
  2032. ->method('getBatchActions')
  2033. ->will($this->returnValue($batchActions));
  2034. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2035. $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  2036. $datagrid->expects($this->once())
  2037. ->method('getQuery')
  2038. ->will($this->returnValue($query));
  2039. $this->admin->expects($this->once())
  2040. ->method('getDatagrid')
  2041. ->will($this->returnValue($datagrid));
  2042. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  2043. $this->admin->expects($this->once())
  2044. ->method('isGranted')
  2045. ->with($this->equalTo('DELETE'))
  2046. ->will($this->returnValue(true));
  2047. $this->admin->expects($this->any())
  2048. ->method('getModelManager')
  2049. ->will($this->returnValue($modelManager));
  2050. $this->admin->expects($this->any())
  2051. ->method('getClass')
  2052. ->will($this->returnValue('Foo'));
  2053. $modelManager->expects($this->once())
  2054. ->method('addIdentifiersToQuery')
  2055. ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
  2056. ->will($this->returnValue(true));
  2057. $this->request->setMethod('POST');
  2058. $this->request->request->set('action', 'delete');
  2059. $this->request->request->set('idx', array('123', '456'));
  2060. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2061. $result = $this->controller->batchAction();
  2062. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2063. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  2064. $this->assertEquals('list?', $result->getTargetUrl());
  2065. }
  2066. public function testBatchActionWithConfirmation()
  2067. {
  2068. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => true));
  2069. $this->admin->expects($this->once())
  2070. ->method('getBatchActions')
  2071. ->will($this->returnValue($batchActions));
  2072. $data = array('action'=>'delete', 'idx'=>array('123', '456'), 'all_elements'=>false);
  2073. $this->request->setMethod('POST');
  2074. $this->request->request->set('data', json_encode($data));
  2075. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2076. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2077. $this->admin->expects($this->once())
  2078. ->method('getDatagrid')
  2079. ->will($this->returnValue($datagrid));
  2080. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  2081. ->disableOriginalConstructor()
  2082. ->getMock();
  2083. $form->expects($this->once())
  2084. ->method('createView')
  2085. ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
  2086. $datagrid->expects($this->once())
  2087. ->method('getForm')
  2088. ->will($this->returnValue($form));
  2089. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->batchAction());
  2090. $this->assertEquals($this->admin, $this->parameters['admin']);
  2091. $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  2092. $this->assertEquals($this->pool, $this->parameters['admin_pool']);
  2093. $this->assertEquals('list', $this->parameters['action']);
  2094. $this->assertEquals($datagrid, $this->parameters['datagrid']);
  2095. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  2096. $this->assertEquals($data, $this->parameters['data']);
  2097. $this->assertEquals('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
  2098. $this->assertEquals('Foo Bar', $this->parameters['action_label']);
  2099. $this->assertEquals(array(), $this->session->getFlashBag()->all());
  2100. $this->assertEquals('SonataAdminBundle:CRUD:batch_confirmation.html.twig', $this->template);
  2101. }
  2102. public function testBatchActionNonRelevantAction()
  2103. {
  2104. $controller = new BatchAdminController();
  2105. $controller->setContainer($this->container);
  2106. $batchActions = array('foo'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  2107. $this->admin->expects($this->once())
  2108. ->method('getBatchActions')
  2109. ->will($this->returnValue($batchActions));
  2110. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2111. $this->admin->expects($this->once())
  2112. ->method('getDatagrid')
  2113. ->will($this->returnValue($datagrid));
  2114. $this->request->setMethod('POST');
  2115. $this->request->request->set('action', 'foo');
  2116. $this->request->request->set('idx', array('789'));
  2117. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2118. $result = $controller->batchAction();
  2119. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2120. $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
  2121. $this->assertEquals('list?', $result->getTargetUrl());
  2122. }
  2123. public function testBatchActionNonRelevantAction2()
  2124. {
  2125. $controller = new BatchAdminController();
  2126. $controller->setContainer($this->container);
  2127. $batchActions = array('foo'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  2128. $this->admin->expects($this->once())
  2129. ->method('getBatchActions')
  2130. ->will($this->returnValue($batchActions));
  2131. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2132. $this->admin->expects($this->once())
  2133. ->method('getDatagrid')
  2134. ->will($this->returnValue($datagrid));
  2135. $this->request->setMethod('POST');
  2136. $this->request->request->set('action', 'foo');
  2137. $this->request->request->set('idx', array('999'));
  2138. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2139. $result = $controller->batchAction();
  2140. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2141. $this->assertSame(array('flash_foo_error'), $this->session->getFlashBag()->get('sonata_flash_info'));
  2142. $this->assertEquals('list?', $result->getTargetUrl());
  2143. }
  2144. public function testBatchActionNoItems()
  2145. {
  2146. $batchActions = array('delete'=>array('label'=>'Foo Bar', 'ask_confirmation' => true));
  2147. $this->admin->expects($this->once())
  2148. ->method('getBatchActions')
  2149. ->will($this->returnValue($batchActions));
  2150. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2151. $this->admin->expects($this->once())
  2152. ->method('getDatagrid')
  2153. ->will($this->returnValue($datagrid));
  2154. $this->request->setMethod('POST');
  2155. $this->request->request->set('action', 'delete');
  2156. $this->request->request->set('idx', array());
  2157. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2158. $result = $this->controller->batchAction();
  2159. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2160. $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
  2161. $this->assertEquals('list?', $result->getTargetUrl());
  2162. }
  2163. public function testBatchActionNoItemsEmptyQuery()
  2164. {
  2165. $controller = new BatchAdminController();
  2166. $controller->setContainer($this->container);
  2167. $batchActions = array('bar'=>array('label'=>'Foo Bar', 'ask_confirmation' => false));
  2168. $this->admin->expects($this->once())
  2169. ->method('getBatchActions')
  2170. ->will($this->returnValue($batchActions));
  2171. $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2172. $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  2173. $datagrid->expects($this->once())
  2174. ->method('getQuery')
  2175. ->will($this->returnValue($query));
  2176. $this->admin->expects($this->once())
  2177. ->method('getDatagrid')
  2178. ->will($this->returnValue($datagrid));
  2179. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  2180. $this->admin->expects($this->any())
  2181. ->method('getModelManager')
  2182. ->will($this->returnValue($modelManager));
  2183. $this->admin->expects($this->any())
  2184. ->method('getClass')
  2185. ->will($this->returnValue('Foo'));
  2186. $this->request->setMethod('POST');
  2187. $this->request->request->set('action', 'bar');
  2188. $this->request->request->set('idx', array());
  2189. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2190. $result = $controller->batchAction();
  2191. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $result);
  2192. $this->assertEquals('batchActionBar executed', $result->getContent());
  2193. }
  2194. public function getCsrfProvider()
  2195. {
  2196. return $this->csrfProvider;
  2197. }
  2198. private function expectTranslate()
  2199. {
  2200. $args = func_get_args();
  2201. // creates equalTo of all arguments passed to this function
  2202. $phpunit = $this; // PHP 5.3 compatibility
  2203. $argsCheck = array_map(function ($item) use ($phpunit) {
  2204. return $phpunit->equalTo($item);
  2205. }, func_get_args());
  2206. $mock = $this->admin->expects($this->once())->method('trans');
  2207. // passes all arguments to the 'with' of the $admin->trans method
  2208. $mock = call_user_func_array(array($mock, 'with'), $argsCheck);
  2209. $mock->will($this->returnValue($args[0]));
  2210. }
  2211. }