CRUDControllerTest.php 109 KB

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