CRUDControllerTest.php 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732
  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 Exporter\Exporter;
  12. use Exporter\Writer\JsonWriter;
  13. use Sonata\AdminBundle\Admin\AbstractAdmin;
  14. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  15. use Sonata\AdminBundle\Admin\Pool;
  16. use Sonata\AdminBundle\Controller\CRUDController;
  17. use Sonata\AdminBundle\Exception\LockException;
  18. use Sonata\AdminBundle\Exception\ModelManagerException;
  19. use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController;
  20. use Sonata\AdminBundle\Tests\Fixtures\Controller\PreCRUDController;
  21. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  22. use Sonata\AdminBundle\Util\AdminObjectAclManipulator;
  23. use Symfony\Bridge\Twig\Extension\FormExtension;
  24. use Symfony\Component\DependencyInjection\ContainerInterface;
  25. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\RequestStack;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\HttpFoundation\Session\Session;
  30. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  31. use Symfony\Component\HttpFoundation\StreamedResponse;
  32. use Symfony\Component\HttpKernel\Exception\HttpException;
  33. use Symfony\Component\HttpKernel\Kernel;
  34. use Symfony\Component\HttpKernel\KernelInterface;
  35. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  36. use Symfony\Component\Security\Csrf\CsrfToken;
  37. /**
  38. * Test for CRUDController.
  39. *
  40. * @author Andrej Hudec <pulzarraider@gmail.com>
  41. */
  42. class CRUDControllerTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var CRUDController
  46. */
  47. private $controller;
  48. /**
  49. * @var Request
  50. */
  51. private $request;
  52. /**
  53. * @var AbstractAdmin
  54. */
  55. private $admin;
  56. /**
  57. * @var Pool
  58. */
  59. private $pool;
  60. /**
  61. * @var array
  62. */
  63. private $parameters;
  64. /**
  65. * @var Session
  66. */
  67. private $session;
  68. /**
  69. * @var \Sonata\AdminBundle\Model\AuditManager
  70. */
  71. private $auditManager;
  72. /**
  73. * @var ContainerInterface
  74. */
  75. private $container;
  76. /**
  77. * @var AdminObjectAclManipulator
  78. */
  79. private $adminObjectAclManipulator;
  80. /**
  81. * @var string
  82. */
  83. private $template;
  84. /**
  85. * @var array
  86. */
  87. private $protectedTestedMethods;
  88. /**
  89. * @var CsrfProviderInterface
  90. */
  91. private $csrfProvider;
  92. /**
  93. * @var KernelInterface
  94. */
  95. private $kernel;
  96. /**
  97. * @var TranslatorInterface
  98. */
  99. private $translator;
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function setUp()
  104. {
  105. $this->container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
  106. $this->request = new Request();
  107. $this->pool = new Pool($this->container, 'title', 'logo.png');
  108. $this->pool->setAdminServiceIds(array('foo.admin'));
  109. $this->request->attributes->set('_sonata_admin', 'foo.admin');
  110. $this->admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $this->translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
  114. $this->parameters = array();
  115. $this->template = '';
  116. // php 5.3 BC
  117. $params = &$this->parameters;
  118. $template = &$this->template;
  119. $templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine')
  120. ->setConstructorArgs(array($this->container, array()))
  121. ->getMock();
  122. $templating->expects($this->any())
  123. ->method('renderResponse')
  124. ->will($this->returnCallback(function (
  125. $view,
  126. array $parameters = array(),
  127. Response $response = null
  128. ) use (
  129. &$params,
  130. &$template
  131. ) {
  132. $template = $view;
  133. if (null === $response) {
  134. $response = new Response();
  135. }
  136. $params = $parameters;
  137. return $response;
  138. }));
  139. $this->session = new Session(new MockArraySessionStorage());
  140. // php 5.3 BC
  141. $pool = $this->pool;
  142. $request = $this->request;
  143. $admin = $this->admin;
  144. $session = $this->session;
  145. $translator = $this->translator;
  146. $twig = $this->getMockBuilder('Twig_Environment')
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. $twigRenderer = $this->createMock('Symfony\Bridge\Twig\Form\TwigRendererInterface');
  150. $formExtension = new FormExtension($twigRenderer);
  151. $twig->expects($this->any())
  152. ->method('getExtension')
  153. ->will($this->returnCallback(function ($name) use ($formExtension) {
  154. switch ($name) {
  155. case 'Symfony\Bridge\Twig\Extension\FormExtension':
  156. return $formExtension;
  157. }
  158. }));
  159. $twig->expects($this->any())
  160. ->method('getRuntime')
  161. ->will($this->returnCallback(function ($name) use ($twigRenderer) {
  162. switch ($name) {
  163. case 'Symfony\Bridge\Twig\Form\TwigRenderer':
  164. if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
  165. return $twigRenderer;
  166. }
  167. throw new \Twig_Error_Runtime('This runtime exists when Symony >= 3.2.');
  168. }
  169. }));
  170. // NEXT_MAJOR : require sonata/exporter ^1.7 and remove conditional
  171. if (class_exists('Exporter\Exporter')) {
  172. $exporter = new Exporter(array(new JsonWriter('/tmp/sonataadmin/export.json')));
  173. } else {
  174. $exporter = $this->createMock('Sonata\AdminBundle\Export\Exporter');
  175. $exporter->expects($this->any())
  176. ->method('getResponse')
  177. ->will($this->returnValue(new StreamedResponse()));
  178. }
  179. $this->auditManager = $this->getMockBuilder('Sonata\AdminBundle\Model\AuditManager')
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $this->adminObjectAclManipulator = $this->getMockBuilder('Sonata\AdminBundle\Util\AdminObjectAclManipulator')
  183. ->disableOriginalConstructor()
  184. ->getMock();
  185. // php 5.3 BC
  186. $request = $this->request;
  187. $auditManager = $this->auditManager;
  188. $adminObjectAclManipulator = $this->adminObjectAclManipulator;
  189. // Prefer Symfony 2.x interfaces
  190. if (interface_exists('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface')) {
  191. $this->csrfProvider = $this->getMockBuilder(
  192. 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'
  193. )
  194. ->getMock();
  195. $this->csrfProvider->expects($this->any())
  196. ->method('generateCsrfToken')
  197. ->will($this->returnCallback(function ($intention) {
  198. return 'csrf-token-123_'.$intention;
  199. }));
  200. $this->csrfProvider->expects($this->any())
  201. ->method('isCsrfTokenValid')
  202. ->will($this->returnCallback(function ($intention, $token) {
  203. if ($token == 'csrf-token-123_'.$intention) {
  204. return true;
  205. }
  206. return false;
  207. }));
  208. } else {
  209. $this->csrfProvider = $this->getMockBuilder(
  210. 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'
  211. )
  212. ->getMock();
  213. $this->csrfProvider->expects($this->any())
  214. ->method('getToken')
  215. ->will($this->returnCallback(function ($intention) {
  216. return new CsrfToken($intention, 'csrf-token-123_'.$intention);
  217. }));
  218. $this->csrfProvider->expects($this->any())
  219. ->method('isTokenValid')
  220. ->will($this->returnCallback(function (CsrfToken $token) {
  221. if ($token->getValue() == 'csrf-token-123_'.$token->getId()) {
  222. return true;
  223. }
  224. return false;
  225. }));
  226. }
  227. // php 5.3 BC
  228. $csrfProvider = $this->csrfProvider;
  229. $this->logger = $this->createMock('Psr\Log\LoggerInterface');
  230. $logger = $this->logger; // php 5.3 BC
  231. $requestStack = null;
  232. if (class_exists('Symfony\Component\HttpFoundation\RequestStack')) {
  233. $requestStack = new RequestStack();
  234. $requestStack->push($request);
  235. }
  236. $this->kernel = $this->createMock('Symfony\Component\HttpKernel\KernelInterface');
  237. $kernel = $this->kernel; // php 5.3 BC
  238. $this->container->expects($this->any())
  239. ->method('get')
  240. ->will($this->returnCallback(function ($id) use (
  241. $pool,
  242. $admin,
  243. $request,
  244. $templating,
  245. $twig,
  246. $session,
  247. $exporter,
  248. $auditManager,
  249. $adminObjectAclManipulator,
  250. $requestStack,
  251. $csrfProvider,
  252. $logger,
  253. $kernel,
  254. $translator
  255. ) {
  256. switch ($id) {
  257. case 'sonata.admin.pool':
  258. return $pool;
  259. case 'request':
  260. return $request;
  261. case 'request_stack':
  262. return $requestStack;
  263. case 'foo.admin':
  264. return $admin;
  265. case 'templating':
  266. return $templating;
  267. case 'twig':
  268. return $twig;
  269. case 'session':
  270. return $session;
  271. case 'sonata.admin.exporter':
  272. return $exporter;
  273. case 'sonata.admin.audit.manager':
  274. return $auditManager;
  275. case 'sonata.admin.object.manipulator.acl.admin':
  276. return $adminObjectAclManipulator;
  277. case 'form.csrf_provider':
  278. case 'security.csrf.token_manager':
  279. return $csrfProvider;
  280. case 'logger':
  281. return $logger;
  282. case 'kernel':
  283. return $kernel;
  284. case 'translator':
  285. return $translator;
  286. }
  287. }));
  288. // php 5.3
  289. $tthis = $this;
  290. $this->container->expects($this->any())
  291. ->method('has')
  292. ->will($this->returnCallback(function ($id) use ($tthis) {
  293. if ($id == 'form.csrf_provider' && Kernel::MAJOR_VERSION == 2 && $tthis->getCsrfProvider() !== null) {
  294. return true;
  295. }
  296. if ($id == 'security.csrf.token_manager' && Kernel::MAJOR_VERSION >= 3 && $tthis->getCsrfProvider() !== null) {
  297. return true;
  298. }
  299. if ($id == 'logger') {
  300. return true;
  301. }
  302. if ($id == 'session') {
  303. return true;
  304. }
  305. if ($id == 'templating') {
  306. return true;
  307. }
  308. if ($id == 'translator') {
  309. return true;
  310. }
  311. return false;
  312. }));
  313. $this->container->expects($this->any())
  314. ->method('getParameter')
  315. ->will($this->returnCallback(function ($name) {
  316. switch ($name) {
  317. case 'security.role_hierarchy.roles':
  318. return array('ROLE_SUPER_ADMIN' => array('ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN'));
  319. }
  320. }));
  321. $this->admin->expects($this->any())
  322. ->method('getTemplate')
  323. ->will($this->returnCallback(function ($name) {
  324. switch ($name) {
  325. case 'ajax':
  326. return 'SonataAdminBundle::ajax_layout.html.twig';
  327. case 'layout':
  328. return 'SonataAdminBundle::standard_layout.html.twig';
  329. case 'show':
  330. return 'SonataAdminBundle:CRUD:show.html.twig';
  331. case 'show_compare':
  332. return 'SonataAdminBundle:CRUD:show_compare.html.twig';
  333. case 'edit':
  334. return 'SonataAdminBundle:CRUD:edit.html.twig';
  335. case 'dashboard':
  336. return 'SonataAdminBundle:Core:dashboard.html.twig';
  337. case 'search':
  338. return 'SonataAdminBundle:Core:search.html.twig';
  339. case 'list':
  340. return 'SonataAdminBundle:CRUD:list.html.twig';
  341. case 'preview':
  342. return 'SonataAdminBundle:CRUD:preview.html.twig';
  343. case 'history':
  344. return 'SonataAdminBundle:CRUD:history.html.twig';
  345. case 'acl':
  346. return 'SonataAdminBundle:CRUD:acl.html.twig';
  347. case 'delete':
  348. return 'SonataAdminBundle:CRUD:delete.html.twig';
  349. case 'batch':
  350. return 'SonataAdminBundle:CRUD:list__batch.html.twig';
  351. case 'batch_confirmation':
  352. return 'SonataAdminBundle:CRUD:batch_confirmation.html.twig';
  353. }
  354. }));
  355. $this->admin->expects($this->any())
  356. ->method('getIdParameter')
  357. ->will($this->returnValue('id'));
  358. $this->admin->expects($this->any())
  359. ->method('getAccessMapping')
  360. ->will($this->returnValue(array()));
  361. $this->admin->expects($this->any())
  362. ->method('generateUrl')
  363. ->will(
  364. $this->returnCallback(
  365. function ($name, array $parameters = array(), $absolute = false) {
  366. $result = $name;
  367. if (!empty($parameters)) {
  368. $result .= '?'.http_build_query($parameters);
  369. }
  370. return $result;
  371. }
  372. )
  373. );
  374. $this->admin->expects($this->any())
  375. ->method('generateObjectUrl')
  376. ->will(
  377. $this->returnCallback(
  378. function ($name, $object, array $parameters = array(), $absolute = false) {
  379. $result = get_class($object).'_'.$name;
  380. if (!empty($parameters)) {
  381. $result .= '?'.http_build_query($parameters);
  382. }
  383. return $result;
  384. }
  385. )
  386. );
  387. $this->controller = new CRUDController();
  388. $this->controller->setContainer($this->container);
  389. // Make some methods public to test them
  390. $testedMethods = array(
  391. 'renderJson',
  392. 'isXmlHttpRequest',
  393. 'configure',
  394. 'getBaseTemplate',
  395. 'redirectTo',
  396. 'addFlash',
  397. );
  398. foreach ($testedMethods as $testedMethod) {
  399. $method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod);
  400. $method->setAccessible(true);
  401. $this->protectedTestedMethods[$testedMethod] = $method;
  402. }
  403. }
  404. public function testRenderJson1()
  405. {
  406. $data = array('example' => '123', 'foo' => 'bar');
  407. $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
  408. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, array(), $this->request);
  409. $this->assertSame($response->headers->get('Content-Type'), 'application/json');
  410. $this->assertSame(json_encode($data), $response->getContent());
  411. }
  412. public function testRenderJson2()
  413. {
  414. $data = array('example' => '123', 'foo' => 'bar');
  415. $this->request->headers->set('Content-Type', 'multipart/form-data');
  416. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, array(), $this->request);
  417. $this->assertSame($response->headers->get('Content-Type'), 'application/json');
  418. $this->assertSame(json_encode($data), $response->getContent());
  419. }
  420. public function testRenderJsonAjax()
  421. {
  422. $data = array('example' => '123', 'foo' => 'bar');
  423. $this->request->attributes->set('_xml_http_request', true);
  424. $this->request->headers->set('Content-Type', 'multipart/form-data');
  425. $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, array(), $this->request);
  426. $this->assertSame($response->headers->get('Content-Type'), 'application/json');
  427. $this->assertSame(json_encode($data), $response->getContent());
  428. }
  429. public function testIsXmlHttpRequest()
  430. {
  431. $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
  432. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  433. $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
  434. $this->request->headers->remove('X-Requested-With');
  435. $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
  436. $this->request->attributes->set('_xml_http_request', true);
  437. $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
  438. }
  439. public function testConfigure()
  440. {
  441. $uniqueId = '';
  442. $this->admin->expects($this->once())
  443. ->method('setUniqid')
  444. ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
  445. $uniqueId = $uniqid;
  446. }));
  447. $this->request->query->set('uniqid', 123456);
  448. $this->protectedTestedMethods['configure']->invoke($this->controller);
  449. $this->assertSame(123456, $uniqueId);
  450. $this->assertAttributeSame($this->admin, 'admin', $this->controller);
  451. }
  452. public function testConfigureChild()
  453. {
  454. $uniqueId = '';
  455. $this->admin->expects($this->once())
  456. ->method('setUniqid')
  457. ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
  458. $uniqueId = $uniqid;
  459. }));
  460. $this->admin->expects($this->once())
  461. ->method('isChild')
  462. ->will($this->returnValue(true));
  463. $adminParent = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
  464. ->disableOriginalConstructor()
  465. ->getMock();
  466. $this->admin->expects($this->once())
  467. ->method('getParent')
  468. ->will($this->returnValue($adminParent));
  469. $this->request->query->set('uniqid', 123456);
  470. $this->protectedTestedMethods['configure']->invoke($this->controller);
  471. $this->assertSame(123456, $uniqueId);
  472. $this->assertAttributeInstanceOf(get_class($adminParent), 'admin', $this->controller);
  473. }
  474. public function testConfigureWithException()
  475. {
  476. $this->expectException(
  477. 'RuntimeException',
  478. 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`'
  479. );
  480. $this->request->attributes->remove('_sonata_admin');
  481. $this->protectedTestedMethods['configure']->invoke($this->controller);
  482. }
  483. public function testConfigureWithException2()
  484. {
  485. $this->expectException(
  486. 'RuntimeException',
  487. 'Unable to find the admin class related to the current controller '.
  488. '(Sonata\AdminBundle\Controller\CRUDController)'
  489. );
  490. $this->pool->setAdminServiceIds(array('nonexistent.admin'));
  491. $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
  492. $this->protectedTestedMethods['configure']->invoke($this->controller);
  493. }
  494. public function testGetBaseTemplate()
  495. {
  496. $this->assertSame(
  497. 'SonataAdminBundle::standard_layout.html.twig',
  498. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
  499. );
  500. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  501. $this->assertSame(
  502. 'SonataAdminBundle::ajax_layout.html.twig',
  503. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
  504. );
  505. $this->request->headers->remove('X-Requested-With');
  506. $this->assertSame(
  507. 'SonataAdminBundle::standard_layout.html.twig',
  508. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
  509. );
  510. $this->request->attributes->set('_xml_http_request', true);
  511. $this->assertSame(
  512. 'SonataAdminBundle::ajax_layout.html.twig',
  513. $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
  514. );
  515. }
  516. public function testRender()
  517. {
  518. $this->parameters = array();
  519. $this->assertInstanceOf(
  520. 'Symfony\Component\HttpFoundation\Response',
  521. $this->controller->render('FooAdminBundle::foo.html.twig', array(), null, $this->request)
  522. );
  523. $this->assertSame($this->admin, $this->parameters['admin']);
  524. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  525. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  526. $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
  527. }
  528. public function testRenderWithResponse()
  529. {
  530. $this->parameters = array();
  531. $response = $response = new Response();
  532. $response->headers->set('X-foo', 'bar');
  533. $responseResult = $this->controller->render('FooAdminBundle::foo.html.twig', array(), $response, $this->request);
  534. $this->assertSame($response, $responseResult);
  535. $this->assertSame('bar', $responseResult->headers->get('X-foo'));
  536. $this->assertSame($this->admin, $this->parameters['admin']);
  537. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  538. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  539. $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
  540. }
  541. public function testRenderCustomParams()
  542. {
  543. $this->parameters = array();
  544. $this->assertInstanceOf(
  545. 'Symfony\Component\HttpFoundation\Response',
  546. $this->controller->render('FooAdminBundle::foo.html.twig',
  547. array('foo' => 'bar'), null, $this->request)
  548. );
  549. $this->assertSame($this->admin, $this->parameters['admin']);
  550. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  551. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  552. $this->assertSame('bar', $this->parameters['foo']);
  553. $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
  554. }
  555. public function testRenderAjax()
  556. {
  557. $this->parameters = array();
  558. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  559. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo' => 'bar'), null, $this->request));
  560. $this->assertSame($this->admin, $this->parameters['admin']);
  561. $this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  562. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  563. $this->assertSame('bar', $this->parameters['foo']);
  564. $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
  565. }
  566. public function testListActionAccessDenied()
  567. {
  568. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  569. $this->admin->expects($this->once())
  570. ->method('checkAccess')
  571. ->with($this->equalTo('list'))
  572. ->will($this->throwException(new AccessDeniedException()));
  573. $this->controller->listAction($this->request);
  574. }
  575. public function testPreList()
  576. {
  577. $this->admin->expects($this->any())
  578. ->method('hasRoute')
  579. ->with($this->equalTo('list'))
  580. ->will($this->returnValue(true));
  581. $this->admin->expects($this->once())
  582. ->method('checkAccess')
  583. ->with($this->equalTo('list'))
  584. ->will($this->returnValue(true));
  585. $controller = new PreCRUDController();
  586. $controller->setContainer($this->container);
  587. $response = $controller->listAction($this->request);
  588. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  589. $this->assertSame('preList called', $response->getContent());
  590. }
  591. public function testListAction()
  592. {
  593. $datagrid = $this->createMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
  594. $this->admin->expects($this->any())
  595. ->method('hasRoute')
  596. ->with($this->equalTo('list'))
  597. ->will($this->returnValue(true));
  598. $this->admin->expects($this->once())
  599. ->method('checkAccess')
  600. ->with($this->equalTo('list'))
  601. ->will($this->returnValue(true));
  602. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  603. ->disableOriginalConstructor()
  604. ->getMock();
  605. $form->expects($this->once())
  606. ->method('createView')
  607. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  608. $this->admin->expects($this->once())
  609. ->method('getDatagrid')
  610. ->will($this->returnValue($datagrid));
  611. $datagrid->expects($this->once())
  612. ->method('getForm')
  613. ->will($this->returnValue($form));
  614. $this->parameters = array();
  615. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->listAction($this->request));
  616. $this->assertSame($this->admin, $this->parameters['admin']);
  617. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  618. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  619. $this->assertSame('list', $this->parameters['action']);
  620. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  621. $this->assertInstanceOf('Sonata\AdminBundle\Datagrid\DatagridInterface', $this->parameters['datagrid']);
  622. $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
  623. $this->assertSame(array(), $this->session->getFlashBag()->all());
  624. $this->assertSame('SonataAdminBundle:CRUD:list.html.twig', $this->template);
  625. }
  626. public function testBatchActionDeleteAccessDenied()
  627. {
  628. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  629. $this->admin->expects($this->once())
  630. ->method('checkAccess')
  631. ->with($this->equalTo('batchDelete'))
  632. ->will($this->throwException(new AccessDeniedException()));
  633. $this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  634. }
  635. public function testBatchActionDelete()
  636. {
  637. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  638. $this->admin->expects($this->once())
  639. ->method('checkAccess')
  640. ->with($this->equalTo('batchDelete'))
  641. ->will($this->returnValue(true));
  642. $this->admin->expects($this->once())
  643. ->method('getModelManager')
  644. ->will($this->returnValue($modelManager));
  645. $this->admin->expects($this->once())
  646. ->method('getFilterParameters')
  647. ->will($this->returnValue(array('foo' => 'bar')));
  648. $result = $this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  649. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  650. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  651. $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  652. }
  653. public function testBatchActionDeleteWithModelManagerException()
  654. {
  655. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  656. $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete');
  657. $this->admin->expects($this->once())
  658. ->method('getModelManager')
  659. ->will($this->returnValue($modelManager));
  660. $this->admin->expects($this->once())
  661. ->method('getFilterParameters')
  662. ->will($this->returnValue(array('foo' => 'bar')));
  663. $result = $this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  664. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  665. $this->assertSame(array('flash_batch_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  666. $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
  667. }
  668. public function testBatchActionDeleteWithModelManagerExceptionInDebugMode()
  669. {
  670. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  671. $this->expectException('Sonata\AdminBundle\Exception\ModelManagerException');
  672. $modelManager->expects($this->once())
  673. ->method('batchDelete')
  674. ->will($this->returnCallback(function () {
  675. throw new ModelManagerException();
  676. }));
  677. $this->admin->expects($this->once())
  678. ->method('getModelManager')
  679. ->will($this->returnValue($modelManager));
  680. $this->kernel->expects($this->once())
  681. ->method('isDebug')
  682. ->will($this->returnValue(true));
  683. $this->controller->batchActionDelete($this->createMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
  684. }
  685. public function testShowActionNotFoundException()
  686. {
  687. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  688. $this->admin->expects($this->once())
  689. ->method('getObject')
  690. ->will($this->returnValue(false));
  691. $this->controller->showAction(null, $this->request);
  692. }
  693. public function testShowActionAccessDenied()
  694. {
  695. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  696. $this->admin->expects($this->once())
  697. ->method('getObject')
  698. ->will($this->returnValue(new \stdClass()));
  699. $this->admin->expects($this->once())
  700. ->method('checkAccess')
  701. ->with($this->equalTo('show'))
  702. ->will($this->throwException(new AccessDeniedException()));
  703. $this->controller->showAction(null, $this->request);
  704. }
  705. public function testPreShow()
  706. {
  707. $object = new \stdClass();
  708. $object->foo = 123456;
  709. $this->admin->expects($this->once())
  710. ->method('getObject')
  711. ->will($this->returnValue($object));
  712. $this->admin->expects($this->once())
  713. ->method('checkAccess')
  714. ->with($this->equalTo('show'))
  715. ->will($this->returnValue(true));
  716. $controller = new PreCRUDController();
  717. $controller->setContainer($this->container);
  718. $response = $controller->showAction(null, $this->request);
  719. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  720. $this->assertSame('preShow called: 123456', $response->getContent());
  721. }
  722. public function testShowAction()
  723. {
  724. $object = new \stdClass();
  725. $this->admin->expects($this->once())
  726. ->method('getObject')
  727. ->will($this->returnValue($object));
  728. $this->admin->expects($this->once())
  729. ->method('checkAccess')
  730. ->with($this->equalTo('show'))
  731. ->will($this->returnValue(true));
  732. $show = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
  733. $this->admin->expects($this->once())
  734. ->method('getShow')
  735. ->will($this->returnValue($show));
  736. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->showAction(null, $this->request));
  737. $this->assertSame($this->admin, $this->parameters['admin']);
  738. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  739. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  740. $this->assertSame('show', $this->parameters['action']);
  741. $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $this->parameters['elements']);
  742. $this->assertSame($object, $this->parameters['object']);
  743. $this->assertSame(array(), $this->session->getFlashBag()->all());
  744. $this->assertSame('SonataAdminBundle:CRUD:show.html.twig', $this->template);
  745. }
  746. /**
  747. * @dataProvider getRedirectToTests
  748. */
  749. public function testRedirectTo($expected, $route, $queryParams, $hasActiveSubclass)
  750. {
  751. $this->admin->expects($this->any())
  752. ->method('hasActiveSubclass')
  753. ->will($this->returnValue($hasActiveSubclass));
  754. $object = new \stdClass();
  755. foreach ($queryParams as $key => $value) {
  756. $this->request->query->set($key, $value);
  757. }
  758. $this->admin->expects($this->any())
  759. ->method('hasRoute')
  760. ->with($this->equalTo($route))
  761. ->will($this->returnValue(true));
  762. $this->admin->expects($this->any())
  763. ->method('hasAccess')
  764. ->with($this->equalTo($route))
  765. ->will($this->returnValue(true));
  766. $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
  767. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  768. $this->assertSame($expected, $response->getTargetUrl());
  769. }
  770. public function testRedirectToWithObject()
  771. {
  772. $this->admin->expects($this->any())
  773. ->method('hasActiveSubclass')
  774. ->will($this->returnValue(false));
  775. $object = new \stdClass();
  776. $this->admin->expects($this->at(0))
  777. ->method('hasRoute')
  778. ->with($this->equalTo('edit'))
  779. ->will($this->returnValue(true));
  780. $this->admin->expects($this->any())
  781. ->method('hasAccess')
  782. ->with($this->equalTo('edit'), $object)
  783. ->will($this->returnValue(false));
  784. $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
  785. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  786. $this->assertSame('list', $response->getTargetUrl());
  787. }
  788. public function getRedirectToTests()
  789. {
  790. return array(
  791. array('stdClass_edit', 'edit', array(), false),
  792. array('list', 'list', array('btn_update_and_list' => true), false),
  793. array('list', 'list', array('btn_create_and_list' => true), false),
  794. array('create', 'create', array('btn_create_and_create' => true), false),
  795. array('create?subclass=foo', 'create', array('btn_create_and_create' => true, 'subclass' => 'foo'), true),
  796. );
  797. }
  798. public function testAddFlash()
  799. {
  800. $this->protectedTestedMethods['addFlash']->invoke($this->controller, 'foo', 'bar');
  801. $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
  802. }
  803. public function testDeleteActionNotFoundException()
  804. {
  805. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  806. $this->admin->expects($this->once())
  807. ->method('getObject')
  808. ->will($this->returnValue(false));
  809. $this->controller->deleteAction(1, $this->request);
  810. }
  811. public function testDeleteActionAccessDenied()
  812. {
  813. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  814. $this->admin->expects($this->once())
  815. ->method('getObject')
  816. ->will($this->returnValue(new \stdClass()));
  817. $this->admin->expects($this->once())
  818. ->method('checkAccess')
  819. ->with($this->equalTo('delete'))
  820. ->will($this->throwException(new AccessDeniedException()));
  821. $this->controller->deleteAction(1, $this->request);
  822. }
  823. public function testPreDelete()
  824. {
  825. $object = new \stdClass();
  826. $object->foo = 123456;
  827. $this->admin->expects($this->once())
  828. ->method('getObject')
  829. ->will($this->returnValue($object));
  830. $this->admin->expects($this->once())
  831. ->method('checkAccess')
  832. ->with($this->equalTo('delete'))
  833. ->will($this->returnValue(true));
  834. $controller = new PreCRUDController();
  835. $controller->setContainer($this->container);
  836. $response = $controller->deleteAction(null, $this->request);
  837. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  838. $this->assertSame('preDelete called: 123456', $response->getContent());
  839. }
  840. public function testDeleteAction()
  841. {
  842. $object = new \stdClass();
  843. $this->admin->expects($this->once())
  844. ->method('getObject')
  845. ->will($this->returnValue($object));
  846. $this->admin->expects($this->once())
  847. ->method('checkAccess')
  848. ->with($this->equalTo('delete'))
  849. ->will($this->returnValue(true));
  850. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request));
  851. $this->assertSame($this->admin, $this->parameters['admin']);
  852. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  853. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  854. $this->assertSame('delete', $this->parameters['action']);
  855. $this->assertSame($object, $this->parameters['object']);
  856. $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
  857. $this->assertSame(array(), $this->session->getFlashBag()->all());
  858. $this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  859. }
  860. public function testDeleteActionNoCsrfToken()
  861. {
  862. $this->csrfProvider = null;
  863. $object = new \stdClass();
  864. $this->admin->expects($this->once())
  865. ->method('getObject')
  866. ->will($this->returnValue($object));
  867. $this->admin->expects($this->once())
  868. ->method('checkAccess')
  869. ->with($this->equalTo('delete'))
  870. ->will($this->returnValue(true));
  871. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request));
  872. $this->assertSame($this->admin, $this->parameters['admin']);
  873. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  874. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  875. $this->assertSame('delete', $this->parameters['action']);
  876. $this->assertSame($object, $this->parameters['object']);
  877. $this->assertSame(false, $this->parameters['csrf_token']);
  878. $this->assertSame(array(), $this->session->getFlashBag()->all());
  879. $this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  880. }
  881. public function testDeleteActionAjaxSuccess1()
  882. {
  883. $object = new \stdClass();
  884. $this->admin->expects($this->once())
  885. ->method('getObject')
  886. ->will($this->returnValue($object));
  887. $this->admin->expects($this->once())
  888. ->method('checkAccess')
  889. ->with($this->equalTo('delete'))
  890. ->will($this->returnValue(true));
  891. $this->request->setMethod('DELETE');
  892. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  893. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  894. $response = $this->controller->deleteAction(1, $this->request);
  895. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  896. $this->assertSame(json_encode(array('result' => 'ok')), $response->getContent());
  897. $this->assertSame(array(), $this->session->getFlashBag()->all());
  898. }
  899. public function testDeleteActionAjaxSuccess2()
  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('checkAccess')
  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-token-123_sonata.delete');
  912. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  913. $response = $this->controller->deleteAction(1, $this->request);
  914. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  915. $this->assertSame(json_encode(array('result' => 'ok')), $response->getContent());
  916. $this->assertSame(array(), $this->session->getFlashBag()->all());
  917. }
  918. public function testDeleteActionAjaxError()
  919. {
  920. $object = new \stdClass();
  921. $this->admin->expects($this->once())
  922. ->method('getObject')
  923. ->will($this->returnValue($object));
  924. $this->admin->expects($this->once())
  925. ->method('checkAccess')
  926. ->with($this->equalTo('delete'))
  927. ->will($this->returnValue(true));
  928. $this->admin->expects($this->any())
  929. ->method('getClass')
  930. ->will($this->returnValue('stdClass'));
  931. $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
  932. $this->request->setMethod('DELETE');
  933. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  934. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  935. $response = $this->controller->deleteAction(1, $this->request);
  936. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  937. $this->assertSame(json_encode(array('result' => 'error')), $response->getContent());
  938. $this->assertSame(array(), $this->session->getFlashBag()->all());
  939. }
  940. public function testDeleteActionWithModelManagerExceptionInDebugMode()
  941. {
  942. $this->expectException('Sonata\AdminBundle\Exception\ModelManagerException');
  943. $object = new \stdClass();
  944. $this->admin->expects($this->once())
  945. ->method('getObject')
  946. ->will($this->returnValue($object));
  947. $this->admin->expects($this->once())
  948. ->method('checkAccess')
  949. ->with($this->equalTo('delete'))
  950. ->will($this->returnValue(true));
  951. $this->admin->expects($this->once())
  952. ->method('delete')
  953. ->will($this->returnCallback(function () {
  954. throw new ModelManagerException();
  955. }));
  956. $this->kernel->expects($this->once())
  957. ->method('isDebug')
  958. ->will($this->returnValue(true));
  959. $this->request->setMethod('DELETE');
  960. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  961. $this->controller->deleteAction(1, $this->request);
  962. }
  963. /**
  964. * @dataProvider getToStringValues
  965. */
  966. public function testDeleteActionSuccess1($expectedToStringValue, $toStringValue)
  967. {
  968. $object = new \stdClass();
  969. $this->admin->expects($this->once())
  970. ->method('getObject')
  971. ->will($this->returnValue($object));
  972. $this->admin->expects($this->once())
  973. ->method('toString')
  974. ->with($this->equalTo($object))
  975. ->will($this->returnValue($toStringValue));
  976. $this->expectTranslate('flash_delete_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  977. $this->admin->expects($this->once())
  978. ->method('checkAccess')
  979. ->with($this->equalTo('delete'))
  980. ->will($this->returnValue(true));
  981. $this->request->setMethod('DELETE');
  982. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  983. $response = $this->controller->deleteAction(1, $this->request);
  984. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  985. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  986. $this->assertSame('list', $response->getTargetUrl());
  987. }
  988. /**
  989. * @dataProvider getToStringValues
  990. */
  991. public function testDeleteActionSuccess2($expectedToStringValue, $toStringValue)
  992. {
  993. $object = new \stdClass();
  994. $this->admin->expects($this->once())
  995. ->method('getObject')
  996. ->will($this->returnValue($object));
  997. $this->admin->expects($this->once())
  998. ->method('checkAccess')
  999. ->with($this->equalTo('delete'))
  1000. ->will($this->returnValue(true));
  1001. $this->admin->expects($this->once())
  1002. ->method('toString')
  1003. ->with($this->equalTo($object))
  1004. ->will($this->returnValue($toStringValue));
  1005. $this->expectTranslate('flash_delete_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1006. $this->request->setMethod('POST');
  1007. $this->request->request->set('_method', 'DELETE');
  1008. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  1009. $response = $this->controller->deleteAction(1, $this->request);
  1010. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1011. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1012. $this->assertSame('list', $response->getTargetUrl());
  1013. }
  1014. /**
  1015. * @dataProvider getToStringValues
  1016. */
  1017. public function testDeleteActionSuccessNoCsrfTokenProvider($expectedToStringValue, $toStringValue)
  1018. {
  1019. $this->csrfProvider = null;
  1020. $object = new \stdClass();
  1021. $this->admin->expects($this->once())
  1022. ->method('getObject')
  1023. ->will($this->returnValue($object));
  1024. $this->admin->expects($this->once())
  1025. ->method('checkAccess')
  1026. ->with($this->equalTo('delete'))
  1027. ->will($this->returnValue(true));
  1028. $this->admin->expects($this->once())
  1029. ->method('toString')
  1030. ->with($this->equalTo($object))
  1031. ->will($this->returnValue($toStringValue));
  1032. $this->expectTranslate('flash_delete_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1033. $this->request->setMethod('POST');
  1034. $this->request->request->set('_method', 'DELETE');
  1035. $response = $this->controller->deleteAction(1, $this->request);
  1036. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1037. $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1038. $this->assertSame('list', $response->getTargetUrl());
  1039. }
  1040. public function testDeleteActionWrongRequestMethod()
  1041. {
  1042. $object = new \stdClass();
  1043. $this->admin->expects($this->once())
  1044. ->method('getObject')
  1045. ->will($this->returnValue($object));
  1046. $this->admin->expects($this->once())
  1047. ->method('checkAccess')
  1048. ->with($this->equalTo('delete'))
  1049. ->will($this->returnValue(true));
  1050. //without POST request parameter "_method" should not be used as real REST method
  1051. $this->request->query->set('_method', 'DELETE');
  1052. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request));
  1053. $this->assertSame($this->admin, $this->parameters['admin']);
  1054. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1055. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1056. $this->assertSame('delete', $this->parameters['action']);
  1057. $this->assertSame($object, $this->parameters['object']);
  1058. $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
  1059. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1060. $this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
  1061. }
  1062. /**
  1063. * @dataProvider getToStringValues
  1064. */
  1065. public function testDeleteActionError($expectedToStringValue, $toStringValue)
  1066. {
  1067. $object = new \stdClass();
  1068. $this->admin->expects($this->once())
  1069. ->method('getObject')
  1070. ->will($this->returnValue($object));
  1071. $this->admin->expects($this->once())
  1072. ->method('checkAccess')
  1073. ->with($this->equalTo('delete'))
  1074. ->will($this->returnValue(true));
  1075. $this->admin->expects($this->once())
  1076. ->method('toString')
  1077. ->with($this->equalTo($object))
  1078. ->will($this->returnValue($toStringValue));
  1079. $this->expectTranslate('flash_delete_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1080. $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
  1081. $this->request->setMethod('DELETE');
  1082. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
  1083. $response = $this->controller->deleteAction(1, $this->request);
  1084. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1085. $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
  1086. $this->assertSame('list', $response->getTargetUrl());
  1087. }
  1088. public function testDeleteActionInvalidCsrfToken()
  1089. {
  1090. $object = new \stdClass();
  1091. $this->admin->expects($this->once())
  1092. ->method('getObject')
  1093. ->will($this->returnValue($object));
  1094. $this->admin->expects($this->once())
  1095. ->method('checkAccess')
  1096. ->with($this->equalTo('delete'))
  1097. ->will($this->returnValue(true));
  1098. $this->request->setMethod('POST');
  1099. $this->request->request->set('_method', 'DELETE');
  1100. $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
  1101. try {
  1102. $this->controller->deleteAction(1, $this->request);
  1103. } catch (HttpException $e) {
  1104. $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
  1105. $this->assertSame(400, $e->getStatusCode());
  1106. }
  1107. }
  1108. public function testEditActionNotFoundException()
  1109. {
  1110. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1111. $this->admin->expects($this->once())
  1112. ->method('getObject')
  1113. ->will($this->returnValue(false));
  1114. $this->controller->editAction(null, $this->request);
  1115. }
  1116. public function testEditActionAccessDenied()
  1117. {
  1118. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1119. $this->admin->expects($this->once())
  1120. ->method('getObject')
  1121. ->will($this->returnValue(new \stdClass()));
  1122. $this->admin->expects($this->once())
  1123. ->method('checkAccess')
  1124. ->with($this->equalTo('edit'))
  1125. ->will($this->throwException(new AccessDeniedException()));
  1126. $this->controller->editAction(null, $this->request);
  1127. }
  1128. public function testPreEdit()
  1129. {
  1130. $object = new \stdClass();
  1131. $object->foo = 123456;
  1132. $this->admin->expects($this->once())
  1133. ->method('getObject')
  1134. ->will($this->returnValue($object));
  1135. $this->admin->expects($this->once())
  1136. ->method('checkAccess')
  1137. ->with($this->equalTo('edit'))
  1138. ->will($this->returnValue(true));
  1139. $controller = new PreCRUDController();
  1140. $controller->setContainer($this->container);
  1141. $response = $controller->editAction(null, $this->request);
  1142. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  1143. $this->assertSame('preEdit called: 123456', $response->getContent());
  1144. }
  1145. public function testEditAction()
  1146. {
  1147. $object = new \stdClass();
  1148. $this->admin->expects($this->once())
  1149. ->method('getObject')
  1150. ->will($this->returnValue($object));
  1151. $this->admin->expects($this->once())
  1152. ->method('checkAccess')
  1153. ->with($this->equalTo('edit'))
  1154. ->will($this->returnValue(true));
  1155. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1156. ->disableOriginalConstructor()
  1157. ->getMock();
  1158. $this->admin->expects($this->once())
  1159. ->method('getForm')
  1160. ->will($this->returnValue($form));
  1161. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1162. $form->expects($this->any())
  1163. ->method('createView')
  1164. ->will($this->returnValue($formView));
  1165. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
  1166. $this->assertSame($this->admin, $this->parameters['admin']);
  1167. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1168. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1169. $this->assertSame('edit', $this->parameters['action']);
  1170. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1171. $this->assertSame($object, $this->parameters['object']);
  1172. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1173. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1174. }
  1175. /**
  1176. * @dataProvider getToStringValues
  1177. */
  1178. public function testEditActionSuccess($expectedToStringValue, $toStringValue)
  1179. {
  1180. $object = new \stdClass();
  1181. $this->admin->expects($this->once())
  1182. ->method('getObject')
  1183. ->will($this->returnValue($object));
  1184. $this->admin->expects($this->once())
  1185. ->method('update')
  1186. ->will($this->returnArgument(0));
  1187. $this->admin->expects($this->once())
  1188. ->method('checkAccess')
  1189. ->with($this->equalTo('edit'));
  1190. $this->admin->expects($this->once())
  1191. ->method('hasRoute')
  1192. ->with($this->equalTo('edit'))
  1193. ->will($this->returnValue(true));
  1194. $this->admin->expects($this->once())
  1195. ->method('hasAccess')
  1196. ->with($this->equalTo('edit'))
  1197. ->will($this->returnValue(true));
  1198. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1199. ->disableOriginalConstructor()
  1200. ->getMock();
  1201. $this->admin->expects($this->once())
  1202. ->method('getForm')
  1203. ->will($this->returnValue($form));
  1204. $form->expects($this->once())
  1205. ->method('isSubmitted')
  1206. ->will($this->returnValue(true));
  1207. $form->expects($this->once())
  1208. ->method('isValid')
  1209. ->will($this->returnValue(true));
  1210. $this->admin->expects($this->once())
  1211. ->method('toString')
  1212. ->with($this->equalTo($object))
  1213. ->will($this->returnValue($toStringValue));
  1214. $this->expectTranslate('flash_edit_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1215. $this->request->setMethod('POST');
  1216. $response = $this->controller->editAction(null, $this->request);
  1217. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1218. $this->assertSame(array('flash_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1219. $this->assertSame('stdClass_edit', $response->getTargetUrl());
  1220. }
  1221. /**
  1222. * @dataProvider getToStringValues
  1223. */
  1224. public function testEditActionError($expectedToStringValue, $toStringValue)
  1225. {
  1226. $object = new \stdClass();
  1227. $this->admin->expects($this->once())
  1228. ->method('getObject')
  1229. ->will($this->returnValue($object));
  1230. $this->admin->expects($this->once())
  1231. ->method('checkAccess')
  1232. ->with($this->equalTo('edit'))
  1233. ->will($this->returnValue(true));
  1234. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1235. ->disableOriginalConstructor()
  1236. ->getMock();
  1237. $this->admin->expects($this->once())
  1238. ->method('getForm')
  1239. ->will($this->returnValue($form));
  1240. $form->expects($this->once())
  1241. ->method('isSubmitted')
  1242. ->will($this->returnValue(true));
  1243. $form->expects($this->once())
  1244. ->method('isValid')
  1245. ->will($this->returnValue(false));
  1246. $this->admin->expects($this->once())
  1247. ->method('toString')
  1248. ->with($this->equalTo($object))
  1249. ->will($this->returnValue($toStringValue));
  1250. $this->expectTranslate('flash_edit_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1251. $this->request->setMethod('POST');
  1252. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1253. $form->expects($this->any())
  1254. ->method('createView')
  1255. ->will($this->returnValue($formView));
  1256. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
  1257. $this->assertSame($this->admin, $this->parameters['admin']);
  1258. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1259. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1260. $this->assertSame('edit', $this->parameters['action']);
  1261. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1262. $this->assertSame($object, $this->parameters['object']);
  1263. $this->assertSame(array('sonata_flash_error' => array('flash_edit_error')), $this->session->getFlashBag()->all());
  1264. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1265. }
  1266. public function testEditActionAjaxSuccess()
  1267. {
  1268. $object = new \stdClass();
  1269. $this->admin->expects($this->once())
  1270. ->method('getObject')
  1271. ->will($this->returnValue($object));
  1272. $this->admin->expects($this->once())
  1273. ->method('update')
  1274. ->will($this->returnArgument(0));
  1275. $this->admin->expects($this->once())
  1276. ->method('checkAccess')
  1277. ->with($this->equalTo('edit'))
  1278. ->will($this->returnValue(true));
  1279. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1280. ->disableOriginalConstructor()
  1281. ->getMock();
  1282. $this->admin->expects($this->once())
  1283. ->method('getForm')
  1284. ->will($this->returnValue($form));
  1285. $form->expects($this->once())
  1286. ->method('isSubmitted')
  1287. ->will($this->returnValue(true));
  1288. $form->expects($this->once())
  1289. ->method('isValid')
  1290. ->will($this->returnValue(true));
  1291. $this->admin->expects($this->once())
  1292. ->method('getNormalizedIdentifier')
  1293. ->with($this->equalTo($object))
  1294. ->will($this->returnValue('foo_normalized'));
  1295. $this->admin->expects($this->once())
  1296. ->method('toString')
  1297. ->will($this->returnValue('foo'));
  1298. $this->request->setMethod('POST');
  1299. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1300. $response = $this->controller->editAction(null, $this->request);
  1301. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  1302. $this->assertSame(json_encode(array('result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo')), $response->getContent());
  1303. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1304. }
  1305. public function testEditActionAjaxError()
  1306. {
  1307. $object = new \stdClass();
  1308. $this->admin->expects($this->once())
  1309. ->method('getObject')
  1310. ->will($this->returnValue($object));
  1311. $this->admin->expects($this->once())
  1312. ->method('checkAccess')
  1313. ->with($this->equalTo('edit'))
  1314. ->will($this->returnValue(true));
  1315. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1316. ->disableOriginalConstructor()
  1317. ->getMock();
  1318. $this->admin->expects($this->once())
  1319. ->method('getForm')
  1320. ->will($this->returnValue($form));
  1321. $form->expects($this->once())
  1322. ->method('isSubmitted')
  1323. ->will($this->returnValue(true));
  1324. $form->expects($this->once())
  1325. ->method('isValid')
  1326. ->will($this->returnValue(false));
  1327. $this->request->setMethod('POST');
  1328. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1329. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1330. $form->expects($this->any())
  1331. ->method('createView')
  1332. ->will($this->returnValue($formView));
  1333. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
  1334. $this->assertSame($this->admin, $this->parameters['admin']);
  1335. $this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  1336. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1337. $this->assertSame('edit', $this->parameters['action']);
  1338. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1339. $this->assertSame($object, $this->parameters['object']);
  1340. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1341. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1342. }
  1343. /**
  1344. * @dataProvider getToStringValues
  1345. */
  1346. public function testEditActionWithModelManagerException($expectedToStringValue, $toStringValue)
  1347. {
  1348. $object = new \stdClass();
  1349. $this->admin->expects($this->once())
  1350. ->method('getObject')
  1351. ->will($this->returnValue($object));
  1352. $this->admin->expects($this->once())
  1353. ->method('checkAccess')
  1354. ->with($this->equalTo('edit'))
  1355. ->will($this->returnValue(true));
  1356. $this->admin->expects($this->any())
  1357. ->method('getClass')
  1358. ->will($this->returnValue('stdClass'));
  1359. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1360. ->disableOriginalConstructor()
  1361. ->getMock();
  1362. $this->admin->expects($this->once())
  1363. ->method('getForm')
  1364. ->will($this->returnValue($form));
  1365. $form->expects($this->once())
  1366. ->method('isValid')
  1367. ->will($this->returnValue(true));
  1368. $this->admin->expects($this->once())
  1369. ->method('toString')
  1370. ->with($this->equalTo($object))
  1371. ->will($this->returnValue($toStringValue));
  1372. $this->expectTranslate('flash_edit_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1373. $form->expects($this->once())
  1374. ->method('isSubmitted')
  1375. ->will($this->returnValue(true));
  1376. $this->request->setMethod('POST');
  1377. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1378. $form->expects($this->any())
  1379. ->method('createView')
  1380. ->will($this->returnValue($formView));
  1381. $this->assertLoggerLogsModelManagerException($this->admin, 'update');
  1382. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
  1383. $this->assertSame($this->admin, $this->parameters['admin']);
  1384. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1385. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1386. $this->assertSame('edit', $this->parameters['action']);
  1387. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1388. $this->assertSame($object, $this->parameters['object']);
  1389. $this->assertSame(array('sonata_flash_error' => array('flash_edit_error')), $this->session->getFlashBag()->all());
  1390. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1391. }
  1392. public function testEditActionWithPreview()
  1393. {
  1394. $object = new \stdClass();
  1395. $this->admin->expects($this->once())
  1396. ->method('getObject')
  1397. ->will($this->returnValue($object));
  1398. $this->admin->expects($this->once())
  1399. ->method('checkAccess')
  1400. ->with($this->equalTo('edit'))
  1401. ->will($this->returnValue(true));
  1402. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1403. ->disableOriginalConstructor()
  1404. ->getMock();
  1405. $this->admin->expects($this->once())
  1406. ->method('getForm')
  1407. ->will($this->returnValue($form));
  1408. $this->admin->expects($this->once())
  1409. ->method('supportsPreviewMode')
  1410. ->will($this->returnValue(true));
  1411. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1412. $form->expects($this->any())
  1413. ->method('createView')
  1414. ->will($this->returnValue($formView));
  1415. $form->expects($this->once())
  1416. ->method('isSubmitted')
  1417. ->will($this->returnValue(true));
  1418. $form->expects($this->once())
  1419. ->method('isValid')
  1420. ->will($this->returnValue(true));
  1421. $this->request->setMethod('POST');
  1422. $this->request->request->set('btn_preview', 'Preview');
  1423. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
  1424. $this->assertSame($this->admin, $this->parameters['admin']);
  1425. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1426. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1427. $this->assertSame('edit', $this->parameters['action']);
  1428. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1429. $this->assertSame($object, $this->parameters['object']);
  1430. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1431. $this->assertSame('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
  1432. }
  1433. public function testEditActionWithLockException()
  1434. {
  1435. $object = new \stdClass();
  1436. $class = get_class($object);
  1437. $this->admin->expects($this->any())
  1438. ->method('getObject')
  1439. ->will($this->returnValue($object));
  1440. $this->admin->expects($this->any())
  1441. ->method('checkAccess')
  1442. ->with($this->equalTo('edit'))
  1443. ->will($this->returnValue(true));
  1444. $this->admin->expects($this->any())
  1445. ->method('getClass')
  1446. ->will($this->returnValue($class));
  1447. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1448. ->disableOriginalConstructor()
  1449. ->getMock();
  1450. $form->expects($this->any())
  1451. ->method('isValid')
  1452. ->will($this->returnValue(true));
  1453. $this->admin->expects($this->any())
  1454. ->method('getForm')
  1455. ->will($this->returnValue($form));
  1456. $form->expects($this->any())
  1457. ->method('isSubmitted')
  1458. ->will($this->returnValue(true));
  1459. $this->request->setMethod('POST');
  1460. $this->admin->expects($this->any())
  1461. ->method('update')
  1462. ->will($this->throwException(new LockException()));
  1463. $this->admin->expects($this->any())
  1464. ->method('toString')
  1465. ->with($this->equalTo($object))
  1466. ->will($this->returnValue($class));
  1467. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1468. $form->expects($this->any())
  1469. ->method('createView')
  1470. ->will($this->returnValue($formView));
  1471. $this->expectTranslate('flash_lock_error', array(
  1472. '%name%' => $class,
  1473. '%link_start%' => '<a href="stdClass_edit">',
  1474. '%link_end%' => '</a>',
  1475. ), 'SonataAdminBundle');
  1476. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
  1477. }
  1478. public function testCreateActionAccessDenied()
  1479. {
  1480. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1481. $this->admin->expects($this->once())
  1482. ->method('checkAccess')
  1483. ->with($this->equalTo('create'))
  1484. ->will($this->throwException(new AccessDeniedException()));
  1485. $this->controller->createAction($this->request);
  1486. }
  1487. public function testPreCreate()
  1488. {
  1489. $object = new \stdClass();
  1490. $object->foo = 123456;
  1491. $this->admin->expects($this->once())
  1492. ->method('checkAccess')
  1493. ->with($this->equalTo('create'))
  1494. ->will($this->returnValue(true));
  1495. $this->admin->expects($this->any())
  1496. ->method('getClass')
  1497. ->will($this->returnValue('stdClass'));
  1498. $this->admin->expects($this->once())
  1499. ->method('getNewInstance')
  1500. ->will($this->returnValue($object));
  1501. $controller = new PreCRUDController();
  1502. $controller->setContainer($this->container);
  1503. $response = $controller->createAction($this->request);
  1504. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  1505. $this->assertSame('preCreate called: 123456', $response->getContent());
  1506. }
  1507. public function testCreateAction()
  1508. {
  1509. $this->admin->expects($this->once())
  1510. ->method('checkAccess')
  1511. ->with($this->equalTo('create'))
  1512. ->will($this->returnValue(true));
  1513. $object = new \stdClass();
  1514. $this->admin->expects($this->any())
  1515. ->method('getClass')
  1516. ->will($this->returnValue('stdClass'));
  1517. $this->admin->expects($this->once())
  1518. ->method('getNewInstance')
  1519. ->will($this->returnValue($object));
  1520. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1521. ->disableOriginalConstructor()
  1522. ->getMock();
  1523. $this->admin->expects($this->once())
  1524. ->method('getForm')
  1525. ->will($this->returnValue($form));
  1526. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1527. $form->expects($this->any())
  1528. ->method('createView')
  1529. ->will($this->returnValue($formView));
  1530. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
  1531. $this->assertSame($this->admin, $this->parameters['admin']);
  1532. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1533. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1534. $this->assertSame('create', $this->parameters['action']);
  1535. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1536. $this->assertSame($object, $this->parameters['object']);
  1537. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1538. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1539. }
  1540. /**
  1541. * @dataProvider getToStringValues
  1542. */
  1543. public function testCreateActionSuccess($expectedToStringValue, $toStringValue)
  1544. {
  1545. $object = new \stdClass();
  1546. $this->admin->expects($this->exactly(2))
  1547. ->method('checkAccess')
  1548. ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
  1549. if ($name == 'edit') {
  1550. return true;
  1551. }
  1552. if ($name != 'create') {
  1553. return false;
  1554. }
  1555. if ($objectIn === null) {
  1556. return true;
  1557. }
  1558. return $objectIn === $object;
  1559. }));
  1560. $this->admin->expects($this->once())
  1561. ->method('hasRoute')
  1562. ->with($this->equalTo('edit'))
  1563. ->will($this->returnValue(true));
  1564. $this->admin->expects($this->once())
  1565. ->method('hasAccess')
  1566. ->with($this->equalTo('edit'))
  1567. ->will($this->returnValue(true));
  1568. $this->admin->expects($this->once())
  1569. ->method('getNewInstance')
  1570. ->will($this->returnValue($object));
  1571. $this->admin->expects($this->once())
  1572. ->method('create')
  1573. ->will($this->returnArgument(0));
  1574. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1575. ->disableOriginalConstructor()
  1576. ->getMock();
  1577. $this->admin->expects($this->any())
  1578. ->method('getClass')
  1579. ->will($this->returnValue('stdClass'));
  1580. $this->admin->expects($this->once())
  1581. ->method('getForm')
  1582. ->will($this->returnValue($form));
  1583. $form->expects($this->once())
  1584. ->method('isSubmitted')
  1585. ->will($this->returnValue(true));
  1586. $form->expects($this->once())
  1587. ->method('isValid')
  1588. ->will($this->returnValue(true));
  1589. $this->admin->expects($this->once())
  1590. ->method('toString')
  1591. ->with($this->equalTo($object))
  1592. ->will($this->returnValue($toStringValue));
  1593. $this->expectTranslate('flash_create_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1594. $this->request->setMethod('POST');
  1595. $response = $this->controller->createAction($this->request);
  1596. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  1597. $this->assertSame(array('flash_create_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  1598. $this->assertSame('stdClass_edit', $response->getTargetUrl());
  1599. }
  1600. public function testCreateActionAccessDenied2()
  1601. {
  1602. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1603. $object = new \stdClass();
  1604. $this->admin->expects($this->any())
  1605. ->method('checkAccess')
  1606. ->will($this->returnCallback(function ($name, $object = null) {
  1607. if ($name != 'create') {
  1608. throw new AccessDeniedException();
  1609. }
  1610. if ($object === null) {
  1611. return true;
  1612. }
  1613. throw new AccessDeniedException();
  1614. }));
  1615. $this->admin->expects($this->once())
  1616. ->method('getNewInstance')
  1617. ->will($this->returnValue($object));
  1618. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1619. ->disableOriginalConstructor()
  1620. ->getMock();
  1621. $this->admin->expects($this->any())
  1622. ->method('getClass')
  1623. ->will($this->returnValue('stdClass'));
  1624. $this->admin->expects($this->once())
  1625. ->method('getForm')
  1626. ->will($this->returnValue($form));
  1627. $form->expects($this->once())
  1628. ->method('isSubmitted')
  1629. ->will($this->returnValue(true));
  1630. $form->expects($this->once())
  1631. ->method('isValid')
  1632. ->will($this->returnValue(true));
  1633. $this->request->setMethod('POST');
  1634. $this->controller->createAction($this->request);
  1635. }
  1636. /**
  1637. * @dataProvider getToStringValues
  1638. */
  1639. public function testCreateActionError($expectedToStringValue, $toStringValue)
  1640. {
  1641. $this->admin->expects($this->once())
  1642. ->method('checkAccess')
  1643. ->with($this->equalTo('create'))
  1644. ->will($this->returnValue(true));
  1645. $object = new \stdClass();
  1646. $this->admin->expects($this->any())
  1647. ->method('getClass')
  1648. ->will($this->returnValue('stdClass'));
  1649. $this->admin->expects($this->once())
  1650. ->method('getNewInstance')
  1651. ->will($this->returnValue($object));
  1652. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1653. ->disableOriginalConstructor()
  1654. ->getMock();
  1655. $this->admin->expects($this->once())
  1656. ->method('getForm')
  1657. ->will($this->returnValue($form));
  1658. $form->expects($this->once())
  1659. ->method('isSubmitted')
  1660. ->will($this->returnValue(true));
  1661. $form->expects($this->once())
  1662. ->method('isValid')
  1663. ->will($this->returnValue(false));
  1664. $this->admin->expects($this->once())
  1665. ->method('toString')
  1666. ->with($this->equalTo($object))
  1667. ->will($this->returnValue($toStringValue));
  1668. $this->expectTranslate('flash_create_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1669. $this->request->setMethod('POST');
  1670. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1671. $form->expects($this->any())
  1672. ->method('createView')
  1673. ->will($this->returnValue($formView));
  1674. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
  1675. $this->assertSame($this->admin, $this->parameters['admin']);
  1676. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1677. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1678. $this->assertSame('create', $this->parameters['action']);
  1679. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1680. $this->assertSame($object, $this->parameters['object']);
  1681. $this->assertSame(array('sonata_flash_error' => array('flash_create_error')), $this->session->getFlashBag()->all());
  1682. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1683. }
  1684. /**
  1685. * @dataProvider getToStringValues
  1686. */
  1687. public function testCreateActionWithModelManagerException($expectedToStringValue, $toStringValue)
  1688. {
  1689. $this->admin->expects($this->exactly(2))
  1690. ->method('checkAccess')
  1691. ->with($this->equalTo('create'))
  1692. ->will($this->returnValue(true));
  1693. $this->admin->expects($this->any())
  1694. ->method('getClass')
  1695. ->will($this->returnValue('stdClass'));
  1696. $object = new \stdClass();
  1697. $this->admin->expects($this->once())
  1698. ->method('getNewInstance')
  1699. ->will($this->returnValue($object));
  1700. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1701. ->disableOriginalConstructor()
  1702. ->getMock();
  1703. $this->admin->expects($this->once())
  1704. ->method('getForm')
  1705. ->will($this->returnValue($form));
  1706. $form->expects($this->once())
  1707. ->method('isValid')
  1708. ->will($this->returnValue(true));
  1709. $this->admin->expects($this->once())
  1710. ->method('toString')
  1711. ->with($this->equalTo($object))
  1712. ->will($this->returnValue($toStringValue));
  1713. $this->expectTranslate('flash_create_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
  1714. $form->expects($this->once())
  1715. ->method('isSubmitted')
  1716. ->will($this->returnValue(true));
  1717. $this->request->setMethod('POST');
  1718. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1719. $form->expects($this->any())
  1720. ->method('createView')
  1721. ->will($this->returnValue($formView));
  1722. $this->assertLoggerLogsModelManagerException($this->admin, 'create');
  1723. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
  1724. $this->assertSame($this->admin, $this->parameters['admin']);
  1725. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1726. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1727. $this->assertSame('create', $this->parameters['action']);
  1728. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1729. $this->assertSame($object, $this->parameters['object']);
  1730. $this->assertSame(array('sonata_flash_error' => array('flash_create_error')), $this->session->getFlashBag()->all());
  1731. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1732. }
  1733. public function testCreateActionAjaxSuccess()
  1734. {
  1735. $object = new \stdClass();
  1736. $this->admin->expects($this->exactly(2))
  1737. ->method('checkAccess')
  1738. ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
  1739. if ($name != 'create') {
  1740. return false;
  1741. }
  1742. if ($objectIn === null) {
  1743. return true;
  1744. }
  1745. return $objectIn === $object;
  1746. }));
  1747. $this->admin->expects($this->once())
  1748. ->method('getNewInstance')
  1749. ->will($this->returnValue($object));
  1750. $this->admin->expects($this->once())
  1751. ->method('create')
  1752. ->will($this->returnArgument(0));
  1753. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1754. ->disableOriginalConstructor()
  1755. ->getMock();
  1756. $this->admin->expects($this->once())
  1757. ->method('getForm')
  1758. ->will($this->returnValue($form));
  1759. $form->expects($this->once())
  1760. ->method('isSubmitted')
  1761. ->will($this->returnValue(true));
  1762. $form->expects($this->once())
  1763. ->method('isValid')
  1764. ->will($this->returnValue(true));
  1765. $this->admin->expects($this->any())
  1766. ->method('getClass')
  1767. ->will($this->returnValue('stdClass'));
  1768. $this->admin->expects($this->once())
  1769. ->method('getNormalizedIdentifier')
  1770. ->with($this->equalTo($object))
  1771. ->will($this->returnValue('foo_normalized'));
  1772. $this->request->setMethod('POST');
  1773. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1774. $response = $this->controller->createAction($this->request);
  1775. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  1776. $this->assertSame(json_encode(array('result' => 'ok', 'objectId' => 'foo_normalized')), $response->getContent());
  1777. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1778. }
  1779. public function testCreateActionAjaxError()
  1780. {
  1781. $this->admin->expects($this->once())
  1782. ->method('checkAccess')
  1783. ->with($this->equalTo('create'))
  1784. ->will($this->returnValue(true));
  1785. $object = new \stdClass();
  1786. $this->admin->expects($this->once())
  1787. ->method('getNewInstance')
  1788. ->will($this->returnValue($object));
  1789. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1790. ->disableOriginalConstructor()
  1791. ->getMock();
  1792. $this->admin->expects($this->any())
  1793. ->method('getClass')
  1794. ->will($this->returnValue('stdClass'));
  1795. $this->admin->expects($this->once())
  1796. ->method('getForm')
  1797. ->will($this->returnValue($form));
  1798. $form->expects($this->once())
  1799. ->method('isSubmitted')
  1800. ->will($this->returnValue(true));
  1801. $form->expects($this->once())
  1802. ->method('isValid')
  1803. ->will($this->returnValue(false));
  1804. $this->request->setMethod('POST');
  1805. $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1806. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1807. $form->expects($this->any())
  1808. ->method('createView')
  1809. ->will($this->returnValue($formView));
  1810. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
  1811. $this->assertSame($this->admin, $this->parameters['admin']);
  1812. $this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
  1813. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1814. $this->assertSame('create', $this->parameters['action']);
  1815. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1816. $this->assertSame($object, $this->parameters['object']);
  1817. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1818. $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
  1819. }
  1820. public function testCreateActionWithPreview()
  1821. {
  1822. $this->admin->expects($this->once())
  1823. ->method('checkAccess')
  1824. ->with($this->equalTo('create'))
  1825. ->will($this->returnValue(true));
  1826. $object = new \stdClass();
  1827. $this->admin->expects($this->once())
  1828. ->method('getNewInstance')
  1829. ->will($this->returnValue($object));
  1830. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  1831. ->disableOriginalConstructor()
  1832. ->getMock();
  1833. $this->admin->expects($this->any())
  1834. ->method('getClass')
  1835. ->will($this->returnValue('stdClass'));
  1836. $this->admin->expects($this->once())
  1837. ->method('getForm')
  1838. ->will($this->returnValue($form));
  1839. $this->admin->expects($this->once())
  1840. ->method('supportsPreviewMode')
  1841. ->will($this->returnValue(true));
  1842. $formView = $this->createMock('Symfony\Component\Form\FormView');
  1843. $form->expects($this->any())
  1844. ->method('createView')
  1845. ->will($this->returnValue($formView));
  1846. $form->expects($this->once())
  1847. ->method('isSubmitted')
  1848. ->will($this->returnValue(true));
  1849. $form->expects($this->once())
  1850. ->method('isValid')
  1851. ->will($this->returnValue(true));
  1852. $this->request->setMethod('POST');
  1853. $this->request->request->set('btn_preview', 'Preview');
  1854. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
  1855. $this->assertSame($this->admin, $this->parameters['admin']);
  1856. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1857. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1858. $this->assertSame('create', $this->parameters['action']);
  1859. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  1860. $this->assertSame($object, $this->parameters['object']);
  1861. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1862. $this->assertSame('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
  1863. }
  1864. public function testExportActionAccessDenied()
  1865. {
  1866. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1867. $this->admin->expects($this->once())
  1868. ->method('checkAccess')
  1869. ->with($this->equalTo('export'))
  1870. ->will($this->throwException(new AccessDeniedException()));
  1871. $this->controller->exportAction($this->request);
  1872. }
  1873. public function testExportActionWrongFormat()
  1874. {
  1875. $this->expectException('RuntimeException', 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`');
  1876. $this->admin->expects($this->once())
  1877. ->method('checkAccess')
  1878. ->with($this->equalTo('export'))
  1879. ->will($this->returnValue(true));
  1880. $this->admin->expects($this->once())
  1881. ->method('getExportFormats')
  1882. ->will($this->returnValue(array('json')));
  1883. $this->admin->expects($this->once())
  1884. ->method('getClass')
  1885. ->will($this->returnValue('Foo'));
  1886. $this->request->query->set('format', 'csv');
  1887. $this->controller->exportAction($this->request);
  1888. }
  1889. public function testExportAction()
  1890. {
  1891. $this->admin->expects($this->once())
  1892. ->method('checkAccess')
  1893. ->with($this->equalTo('export'))
  1894. ->will($this->returnValue(true));
  1895. $this->admin->expects($this->once())
  1896. ->method('getExportFormats')
  1897. ->will($this->returnValue(array('json')));
  1898. $dataSourceIterator = $this->createMock('Exporter\Source\SourceIteratorInterface');
  1899. $this->admin->expects($this->once())
  1900. ->method('getDataSourceIterator')
  1901. ->will($this->returnValue($dataSourceIterator));
  1902. $this->request->query->set('format', 'json');
  1903. $response = $this->controller->exportAction($this->request);
  1904. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  1905. $this->assertSame(200, $response->getStatusCode());
  1906. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1907. }
  1908. public function testHistoryActionAccessDenied()
  1909. {
  1910. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  1911. $this->admin->expects($this->any())
  1912. ->method('getObject')
  1913. ->will($this->returnValue(new \StdClass()));
  1914. $this->admin->expects($this->once())
  1915. ->method('checkAccess')
  1916. ->with($this->equalTo('history'))
  1917. ->will($this->throwException(new AccessDeniedException()));
  1918. $this->controller->historyAction(null, $this->request);
  1919. }
  1920. public function testHistoryActionNotFoundException()
  1921. {
  1922. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1923. $this->admin->expects($this->once())
  1924. ->method('getObject')
  1925. ->will($this->returnValue(false));
  1926. $this->controller->historyAction(null, $this->request);
  1927. }
  1928. public function testHistoryActionNoReader()
  1929. {
  1930. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  1931. $this->request->query->set('id', 123);
  1932. $this->admin->expects($this->once())
  1933. ->method('checkAccess')
  1934. ->with($this->equalTo('history'))
  1935. ->will($this->returnValue(true));
  1936. $object = new \stdClass();
  1937. $this->admin->expects($this->once())
  1938. ->method('getObject')
  1939. ->will($this->returnValue($object));
  1940. $this->admin->expects($this->any())
  1941. ->method('getClass')
  1942. ->will($this->returnValue('Foo'));
  1943. $this->auditManager->expects($this->once())
  1944. ->method('hasReader')
  1945. ->with($this->equalTo('Foo'))
  1946. ->will($this->returnValue(false));
  1947. $this->controller->historyAction(null, $this->request);
  1948. }
  1949. public function testHistoryAction()
  1950. {
  1951. $this->request->query->set('id', 123);
  1952. $this->admin->expects($this->once())
  1953. ->method('checkAccess')
  1954. ->with($this->equalTo('history'))
  1955. ->will($this->returnValue(true));
  1956. $object = new \stdClass();
  1957. $this->admin->expects($this->once())
  1958. ->method('getObject')
  1959. ->will($this->returnValue($object));
  1960. $this->admin->expects($this->any())
  1961. ->method('getClass')
  1962. ->will($this->returnValue('Foo'));
  1963. $this->auditManager->expects($this->once())
  1964. ->method('hasReader')
  1965. ->with($this->equalTo('Foo'))
  1966. ->will($this->returnValue(true));
  1967. $reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  1968. $this->auditManager->expects($this->once())
  1969. ->method('getReader')
  1970. ->with($this->equalTo('Foo'))
  1971. ->will($this->returnValue($reader));
  1972. $reader->expects($this->once())
  1973. ->method('findRevisions')
  1974. ->with($this->equalTo('Foo'), $this->equalTo(123))
  1975. ->will($this->returnValue(array()));
  1976. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyAction(null, $this->request));
  1977. $this->assertSame($this->admin, $this->parameters['admin']);
  1978. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  1979. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  1980. $this->assertSame('history', $this->parameters['action']);
  1981. $this->assertSame(array(), $this->parameters['revisions']);
  1982. $this->assertSame($object, $this->parameters['object']);
  1983. $this->assertSame(array(), $this->session->getFlashBag()->all());
  1984. $this->assertSame('SonataAdminBundle:CRUD:history.html.twig', $this->template);
  1985. }
  1986. public function testAclActionAclNotEnabled()
  1987. {
  1988. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'ACL are not enabled for this admin');
  1989. $this->controller->aclAction(null, $this->request);
  1990. }
  1991. public function testAclActionNotFoundException()
  1992. {
  1993. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
  1994. $this->admin->expects($this->once())
  1995. ->method('isAclEnabled')
  1996. ->will($this->returnValue(true));
  1997. $this->admin->expects($this->once())
  1998. ->method('getObject')
  1999. ->will($this->returnValue(false));
  2000. $this->controller->aclAction(null, $this->request);
  2001. }
  2002. public function testAclActionAccessDenied()
  2003. {
  2004. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  2005. $this->admin->expects($this->once())
  2006. ->method('isAclEnabled')
  2007. ->will($this->returnValue(true));
  2008. $object = new \stdClass();
  2009. $this->admin->expects($this->once())
  2010. ->method('getObject')
  2011. ->will($this->returnValue($object));
  2012. $this->admin->expects($this->once())
  2013. ->method('checkAccess')
  2014. ->with($this->equalTo('acl'), $this->equalTo($object))
  2015. ->will($this->throwException(new AccessDeniedException()));
  2016. $this->controller->aclAction(null, $this->request);
  2017. }
  2018. public function testAclAction()
  2019. {
  2020. $this->request->query->set('id', 123);
  2021. $this->admin->expects($this->once())
  2022. ->method('isAclEnabled')
  2023. ->will($this->returnValue(true));
  2024. $object = new \stdClass();
  2025. $this->admin->expects($this->once())
  2026. ->method('getObject')
  2027. ->will($this->returnValue($object));
  2028. $this->admin->expects($this->any())
  2029. ->method('checkAccess')
  2030. ->will($this->returnValue(true));
  2031. $this->admin->expects($this->any())
  2032. ->method('getSecurityInformation')
  2033. ->will($this->returnValue(array()));
  2034. $this->adminObjectAclManipulator->expects($this->once())
  2035. ->method('getMaskBuilderClass')
  2036. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  2037. $aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  2038. ->disableOriginalConstructor()
  2039. ->getMock();
  2040. $aclUsersForm->expects($this->once())
  2041. ->method('createView')
  2042. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  2043. $aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  2044. ->disableOriginalConstructor()
  2045. ->getMock();
  2046. $aclRolesForm->expects($this->once())
  2047. ->method('createView')
  2048. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  2049. $this->adminObjectAclManipulator->expects($this->once())
  2050. ->method('createAclUsersForm')
  2051. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  2052. ->will($this->returnValue($aclUsersForm));
  2053. $this->adminObjectAclManipulator->expects($this->once())
  2054. ->method('createAclRolesForm')
  2055. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  2056. ->will($this->returnValue($aclRolesForm));
  2057. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  2058. ->disableOriginalConstructor()
  2059. ->getMock();
  2060. $aclSecurityHandler->expects($this->any())
  2061. ->method('getObjectPermissions')
  2062. ->will($this->returnValue(array()));
  2063. $this->admin->expects($this->any())
  2064. ->method('getSecurityHandler')
  2065. ->will($this->returnValue($aclSecurityHandler));
  2066. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction(null, $this->request));
  2067. $this->assertSame($this->admin, $this->parameters['admin']);
  2068. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  2069. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  2070. $this->assertSame('acl', $this->parameters['action']);
  2071. $this->assertSame(array(), $this->parameters['permissions']);
  2072. $this->assertSame($object, $this->parameters['object']);
  2073. $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
  2074. $this->assertInstanceOf('\ArrayIterator', $this->parameters['roles']);
  2075. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclUsersForm']);
  2076. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclRolesForm']);
  2077. $this->assertSame(array(), $this->session->getFlashBag()->all());
  2078. $this->assertSame('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
  2079. }
  2080. public function testAclActionInvalidUpdate()
  2081. {
  2082. $this->request->query->set('id', 123);
  2083. $this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, array());
  2084. $this->admin->expects($this->once())
  2085. ->method('isAclEnabled')
  2086. ->will($this->returnValue(true));
  2087. $object = new \stdClass();
  2088. $this->admin->expects($this->once())
  2089. ->method('getObject')
  2090. ->will($this->returnValue($object));
  2091. $this->admin->expects($this->any())
  2092. ->method('checkAccess')
  2093. ->will($this->returnValue(true));
  2094. $this->admin->expects($this->any())
  2095. ->method('getSecurityInformation')
  2096. ->will($this->returnValue(array()));
  2097. $this->adminObjectAclManipulator->expects($this->once())
  2098. ->method('getMaskBuilderClass')
  2099. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  2100. $aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  2101. ->disableOriginalConstructor()
  2102. ->getMock();
  2103. $aclUsersForm->expects($this->once())
  2104. ->method('isValid')
  2105. ->will($this->returnValue(false));
  2106. $aclUsersForm->expects($this->once())
  2107. ->method('createView')
  2108. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  2109. $aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  2110. ->disableOriginalConstructor()
  2111. ->getMock();
  2112. $aclRolesForm->expects($this->once())
  2113. ->method('createView')
  2114. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  2115. $this->adminObjectAclManipulator->expects($this->once())
  2116. ->method('createAclUsersForm')
  2117. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  2118. ->will($this->returnValue($aclUsersForm));
  2119. $this->adminObjectAclManipulator->expects($this->once())
  2120. ->method('createAclRolesForm')
  2121. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  2122. ->will($this->returnValue($aclRolesForm));
  2123. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  2124. ->disableOriginalConstructor()
  2125. ->getMock();
  2126. $aclSecurityHandler->expects($this->any())
  2127. ->method('getObjectPermissions')
  2128. ->will($this->returnValue(array()));
  2129. $this->admin->expects($this->any())
  2130. ->method('getSecurityHandler')
  2131. ->will($this->returnValue($aclSecurityHandler));
  2132. $this->request->setMethod('POST');
  2133. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction(null, $this->request));
  2134. $this->assertSame($this->admin, $this->parameters['admin']);
  2135. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  2136. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  2137. $this->assertSame('acl', $this->parameters['action']);
  2138. $this->assertSame(array(), $this->parameters['permissions']);
  2139. $this->assertSame($object, $this->parameters['object']);
  2140. $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
  2141. $this->assertInstanceOf('\ArrayIterator', $this->parameters['roles']);
  2142. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclUsersForm']);
  2143. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclRolesForm']);
  2144. $this->assertSame(array(), $this->session->getFlashBag()->all());
  2145. $this->assertSame('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
  2146. }
  2147. public function testAclActionSuccessfulUpdate()
  2148. {
  2149. $this->request->query->set('id', 123);
  2150. $this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, array());
  2151. $this->admin->expects($this->once())
  2152. ->method('isAclEnabled')
  2153. ->will($this->returnValue(true));
  2154. $object = new \stdClass();
  2155. $this->admin->expects($this->once())
  2156. ->method('getObject')
  2157. ->will($this->returnValue($object));
  2158. $this->admin->expects($this->any())
  2159. ->method('checkAccess')
  2160. ->will($this->returnValue(true));
  2161. $this->admin->expects($this->any())
  2162. ->method('getSecurityInformation')
  2163. ->will($this->returnValue(array()));
  2164. $this->adminObjectAclManipulator->expects($this->once())
  2165. ->method('getMaskBuilderClass')
  2166. ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
  2167. $aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  2168. ->disableOriginalConstructor()
  2169. ->getMock();
  2170. $aclUsersForm->expects($this->any())
  2171. ->method('createView')
  2172. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  2173. $aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  2174. ->disableOriginalConstructor()
  2175. ->getMock();
  2176. $aclRolesForm->expects($this->any())
  2177. ->method('createView')
  2178. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  2179. $aclRolesForm->expects($this->once())
  2180. ->method('isValid')
  2181. ->will($this->returnValue(true));
  2182. $this->adminObjectAclManipulator->expects($this->once())
  2183. ->method('createAclUsersForm')
  2184. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  2185. ->will($this->returnValue($aclUsersForm));
  2186. $this->adminObjectAclManipulator->expects($this->once())
  2187. ->method('createAclRolesForm')
  2188. ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
  2189. ->will($this->returnValue($aclRolesForm));
  2190. $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
  2191. ->disableOriginalConstructor()
  2192. ->getMock();
  2193. $aclSecurityHandler->expects($this->any())
  2194. ->method('getObjectPermissions')
  2195. ->will($this->returnValue(array()));
  2196. $this->admin->expects($this->any())
  2197. ->method('getSecurityHandler')
  2198. ->will($this->returnValue($aclSecurityHandler));
  2199. $this->request->setMethod('POST');
  2200. $response = $this->controller->aclAction(null, $this->request);
  2201. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  2202. $this->assertSame(array('flash_acl_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  2203. $this->assertSame('stdClass_acl', $response->getTargetUrl());
  2204. }
  2205. public function testHistoryViewRevisionActionAccessDenied()
  2206. {
  2207. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  2208. $this->admin->expects($this->any())
  2209. ->method('getObject')
  2210. ->will($this->returnValue(new \StdClass()));
  2211. $this->admin->expects($this->once())
  2212. ->method('checkAccess')
  2213. ->with($this->equalTo('historyViewRevision'))
  2214. ->will($this->throwException(new AccessDeniedException()));
  2215. $this->controller->historyViewRevisionAction(null, null, $this->request);
  2216. }
  2217. public function testHistoryViewRevisionActionNotFoundException()
  2218. {
  2219. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id : 123');
  2220. $this->request->query->set('id', 123);
  2221. $this->admin->expects($this->once())
  2222. ->method('getObject')
  2223. ->will($this->returnValue(false));
  2224. $this->controller->historyViewRevisionAction(null, null, $this->request);
  2225. }
  2226. public function testHistoryViewRevisionActionNoReader()
  2227. {
  2228. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  2229. $this->request->query->set('id', 123);
  2230. $this->admin->expects($this->once())
  2231. ->method('checkAccess')
  2232. ->with($this->equalTo('historyViewRevision'))
  2233. ->will($this->returnValue(true));
  2234. $object = new \stdClass();
  2235. $this->admin->expects($this->once())
  2236. ->method('getObject')
  2237. ->will($this->returnValue($object));
  2238. $this->admin->expects($this->any())
  2239. ->method('getClass')
  2240. ->will($this->returnValue('Foo'));
  2241. $this->auditManager->expects($this->once())
  2242. ->method('hasReader')
  2243. ->with($this->equalTo('Foo'))
  2244. ->will($this->returnValue(false));
  2245. $this->controller->historyViewRevisionAction(null, null, $this->request);
  2246. }
  2247. public function testHistoryViewRevisionActionNotFoundRevision()
  2248. {
  2249. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`');
  2250. $this->request->query->set('id', 123);
  2251. $this->admin->expects($this->once())
  2252. ->method('checkAccess')
  2253. ->with($this->equalTo('historyViewRevision'))
  2254. ->will($this->returnValue(true));
  2255. $object = new \stdClass();
  2256. $this->admin->expects($this->once())
  2257. ->method('getObject')
  2258. ->will($this->returnValue($object));
  2259. $this->admin->expects($this->any())
  2260. ->method('getClass')
  2261. ->will($this->returnValue('Foo'));
  2262. $this->auditManager->expects($this->once())
  2263. ->method('hasReader')
  2264. ->with($this->equalTo('Foo'))
  2265. ->will($this->returnValue(true));
  2266. $reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  2267. $this->auditManager->expects($this->once())
  2268. ->method('getReader')
  2269. ->with($this->equalTo('Foo'))
  2270. ->will($this->returnValue($reader));
  2271. $reader->expects($this->once())
  2272. ->method('find')
  2273. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  2274. ->will($this->returnValue(null));
  2275. $this->controller->historyViewRevisionAction(123, 456, $this->request);
  2276. }
  2277. public function testHistoryViewRevisionAction()
  2278. {
  2279. $this->request->query->set('id', 123);
  2280. $this->admin->expects($this->once())
  2281. ->method('checkAccess')
  2282. ->with($this->equalTo('historyViewRevision'))
  2283. ->will($this->returnValue(true));
  2284. $object = new \stdClass();
  2285. $this->admin->expects($this->once())
  2286. ->method('getObject')
  2287. ->will($this->returnValue($object));
  2288. $this->admin->expects($this->any())
  2289. ->method('getClass')
  2290. ->will($this->returnValue('Foo'));
  2291. $this->auditManager->expects($this->once())
  2292. ->method('hasReader')
  2293. ->with($this->equalTo('Foo'))
  2294. ->will($this->returnValue(true));
  2295. $reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  2296. $this->auditManager->expects($this->once())
  2297. ->method('getReader')
  2298. ->with($this->equalTo('Foo'))
  2299. ->will($this->returnValue($reader));
  2300. $objectRevision = new \stdClass();
  2301. $objectRevision->revision = 456;
  2302. $reader->expects($this->once())
  2303. ->method('find')
  2304. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  2305. ->will($this->returnValue($objectRevision));
  2306. $this->admin->expects($this->once())
  2307. ->method('setSubject')
  2308. ->with($this->equalTo($objectRevision))
  2309. ->will($this->returnValue(null));
  2310. $fieldDescriptionCollection = new FieldDescriptionCollection();
  2311. $this->admin->expects($this->once())
  2312. ->method('getShow')
  2313. ->will($this->returnValue($fieldDescriptionCollection));
  2314. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyViewRevisionAction(123, 456, $this->request));
  2315. $this->assertSame($this->admin, $this->parameters['admin']);
  2316. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  2317. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  2318. $this->assertSame('show', $this->parameters['action']);
  2319. $this->assertSame($objectRevision, $this->parameters['object']);
  2320. $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
  2321. $this->assertSame(array(), $this->session->getFlashBag()->all());
  2322. $this->assertSame('SonataAdminBundle:CRUD:show.html.twig', $this->template);
  2323. }
  2324. public function testHistoryCompareRevisionsActionAccessDenied()
  2325. {
  2326. $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
  2327. $this->admin->expects($this->once())
  2328. ->method('checkAccess')
  2329. ->with($this->equalTo('historyCompareRevisions'))
  2330. ->will($this->throwException(new AccessDeniedException()));
  2331. $this->controller->historyCompareRevisionsAction(null, null, null, $this->request);
  2332. }
  2333. public function testHistoryCompareRevisionsActionNotFoundException()
  2334. {
  2335. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id : 123');
  2336. $this->request->query->set('id', 123);
  2337. $this->admin->expects($this->once())
  2338. ->method('checkAccess')
  2339. ->with($this->equalTo('historyCompareRevisions'))
  2340. ->will($this->returnValue(true));
  2341. $this->admin->expects($this->once())
  2342. ->method('getObject')
  2343. ->will($this->returnValue(false));
  2344. $this->controller->historyCompareRevisionsAction(null, null, null, $this->request);
  2345. }
  2346. public function testHistoryCompareRevisionsActionNoReader()
  2347. {
  2348. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
  2349. $this->request->query->set('id', 123);
  2350. $this->admin->expects($this->once())
  2351. ->method('checkAccess')
  2352. ->with($this->equalTo('historyCompareRevisions'))
  2353. ->will($this->returnValue(true));
  2354. $object = new \stdClass();
  2355. $this->admin->expects($this->once())
  2356. ->method('getObject')
  2357. ->will($this->returnValue($object));
  2358. $this->admin->expects($this->any())
  2359. ->method('getClass')
  2360. ->will($this->returnValue('Foo'));
  2361. $this->auditManager->expects($this->once())
  2362. ->method('hasReader')
  2363. ->with($this->equalTo('Foo'))
  2364. ->will($this->returnValue(false));
  2365. $this->controller->historyCompareRevisionsAction(null, null, null, $this->request);
  2366. }
  2367. public function testHistoryCompareRevisionsActionNotFoundBaseRevision()
  2368. {
  2369. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`');
  2370. $this->request->query->set('id', 123);
  2371. $this->admin->expects($this->once())
  2372. ->method('checkAccess')
  2373. ->with($this->equalTo('historyCompareRevisions'))
  2374. ->will($this->returnValue(true));
  2375. $object = new \stdClass();
  2376. $this->admin->expects($this->once())
  2377. ->method('getObject')
  2378. ->will($this->returnValue($object));
  2379. $this->admin->expects($this->any())
  2380. ->method('getClass')
  2381. ->will($this->returnValue('Foo'));
  2382. $this->auditManager->expects($this->once())
  2383. ->method('hasReader')
  2384. ->with($this->equalTo('Foo'))
  2385. ->will($this->returnValue(true));
  2386. $reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  2387. $this->auditManager->expects($this->once())
  2388. ->method('getReader')
  2389. ->with($this->equalTo('Foo'))
  2390. ->will($this->returnValue($reader));
  2391. // once because it will not be found and therefore the second call won't be executed
  2392. $reader->expects($this->once())
  2393. ->method('find')
  2394. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  2395. ->will($this->returnValue(null));
  2396. $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request);
  2397. }
  2398. public function testHistoryCompareRevisionsActionNotFoundCompareRevision()
  2399. {
  2400. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `789` with classname : `Foo`');
  2401. $this->request->query->set('id', 123);
  2402. $this->admin->expects($this->once())
  2403. ->method('checkAccess')
  2404. ->with($this->equalTo('historyCompareRevisions'))
  2405. ->will($this->returnValue(true));
  2406. $object = new \stdClass();
  2407. $this->admin->expects($this->once())
  2408. ->method('getObject')
  2409. ->will($this->returnValue($object));
  2410. $this->admin->expects($this->any())
  2411. ->method('getClass')
  2412. ->will($this->returnValue('Foo'));
  2413. $this->auditManager->expects($this->once())
  2414. ->method('hasReader')
  2415. ->with($this->equalTo('Foo'))
  2416. ->will($this->returnValue(true));
  2417. $reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  2418. $this->auditManager->expects($this->once())
  2419. ->method('getReader')
  2420. ->with($this->equalTo('Foo'))
  2421. ->will($this->returnValue($reader));
  2422. $objectRevision = new \stdClass();
  2423. $objectRevision->revision = 456;
  2424. // first call should return, so the second call will throw an exception
  2425. $reader->expects($this->at(0))
  2426. ->method('find')
  2427. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  2428. ->will($this->returnValue($objectRevision));
  2429. $reader->expects($this->at(1))
  2430. ->method('find')
  2431. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
  2432. ->will($this->returnValue(null));
  2433. $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request);
  2434. }
  2435. public function testHistoryCompareRevisionsActionAction()
  2436. {
  2437. $this->request->query->set('id', 123);
  2438. $this->admin->expects($this->once())
  2439. ->method('checkAccess')
  2440. ->with($this->equalTo('historyCompareRevisions'))
  2441. ->will($this->returnValue(true));
  2442. $object = new \stdClass();
  2443. $this->admin->expects($this->once())
  2444. ->method('getObject')
  2445. ->will($this->returnValue($object));
  2446. $this->admin->expects($this->any())
  2447. ->method('getClass')
  2448. ->will($this->returnValue('Foo'));
  2449. $this->auditManager->expects($this->once())
  2450. ->method('hasReader')
  2451. ->with($this->equalTo('Foo'))
  2452. ->will($this->returnValue(true));
  2453. $reader = $this->createMock('Sonata\AdminBundle\Model\AuditReaderInterface');
  2454. $this->auditManager->expects($this->once())
  2455. ->method('getReader')
  2456. ->with($this->equalTo('Foo'))
  2457. ->will($this->returnValue($reader));
  2458. $objectRevision = new \stdClass();
  2459. $objectRevision->revision = 456;
  2460. $compareObjectRevision = new \stdClass();
  2461. $compareObjectRevision->revision = 789;
  2462. $reader->expects($this->at(0))
  2463. ->method('find')
  2464. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
  2465. ->will($this->returnValue($objectRevision));
  2466. $reader->expects($this->at(1))
  2467. ->method('find')
  2468. ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
  2469. ->will($this->returnValue($compareObjectRevision));
  2470. $this->admin->expects($this->once())
  2471. ->method('setSubject')
  2472. ->with($this->equalTo($objectRevision))
  2473. ->will($this->returnValue(null));
  2474. $fieldDescriptionCollection = new FieldDescriptionCollection();
  2475. $this->admin->expects($this->once())
  2476. ->method('getShow')
  2477. ->will($this->returnValue($fieldDescriptionCollection));
  2478. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request));
  2479. $this->assertSame($this->admin, $this->parameters['admin']);
  2480. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  2481. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  2482. $this->assertSame('show', $this->parameters['action']);
  2483. $this->assertSame($objectRevision, $this->parameters['object']);
  2484. $this->assertSame($compareObjectRevision, $this->parameters['object_compare']);
  2485. $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
  2486. $this->assertSame(array(), $this->session->getFlashBag()->all());
  2487. $this->assertSame('SonataAdminBundle:CRUD:show_compare.html.twig', $this->template);
  2488. }
  2489. public function testBatchActionWrongMethod()
  2490. {
  2491. $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'Invalid request type "GET", POST expected');
  2492. $this->controller->batchAction($this->request);
  2493. }
  2494. /**
  2495. * NEXT_MAJOR: Remove this legacy group.
  2496. *
  2497. * @group legacy
  2498. */
  2499. public function testBatchActionActionNotDefined()
  2500. {
  2501. $this->expectException('RuntimeException', 'The `foo` batch action is not defined');
  2502. $batchActions = array();
  2503. $this->admin->expects($this->once())
  2504. ->method('getBatchActions')
  2505. ->will($this->returnValue($batchActions));
  2506. $this->request->setMethod('POST');
  2507. $this->request->request->set('data', json_encode(array('action' => 'foo', 'idx' => array('123', '456'), 'all_elements' => false)));
  2508. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2509. $this->controller->batchAction($this->request);
  2510. }
  2511. public function testBatchActionActionInvalidCsrfToken()
  2512. {
  2513. $this->request->setMethod('POST');
  2514. $this->request->request->set('data', json_encode(array('action' => 'foo', 'idx' => array('123', '456'), 'all_elements' => false)));
  2515. $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
  2516. try {
  2517. $this->controller->batchAction($this->request);
  2518. } catch (HttpException $e) {
  2519. $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
  2520. $this->assertSame(400, $e->getStatusCode());
  2521. }
  2522. }
  2523. /**
  2524. * NEXT_MAJOR: Remove this legacy group.
  2525. *
  2526. * @group legacy
  2527. */
  2528. public function testBatchActionMethodNotExist()
  2529. {
  2530. $this->expectException('RuntimeException', 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable');
  2531. $batchActions = array('foo' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
  2532. $this->admin->expects($this->once())
  2533. ->method('getBatchActions')
  2534. ->will($this->returnValue($batchActions));
  2535. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2536. $this->admin->expects($this->once())
  2537. ->method('getDatagrid')
  2538. ->will($this->returnValue($datagrid));
  2539. $this->request->setMethod('POST');
  2540. $this->request->request->set('data', json_encode(array('action' => 'foo', 'idx' => array('123', '456'), 'all_elements' => false)));
  2541. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2542. $this->controller->batchAction($this->request);
  2543. }
  2544. /**
  2545. * NEXT_MAJOR: Remove this legacy group.
  2546. *
  2547. * @group legacy
  2548. */
  2549. public function testBatchActionWithoutConfirmation()
  2550. {
  2551. $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
  2552. $this->admin->expects($this->once())
  2553. ->method('getBatchActions')
  2554. ->will($this->returnValue($batchActions));
  2555. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2556. $query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  2557. $datagrid->expects($this->once())
  2558. ->method('getQuery')
  2559. ->will($this->returnValue($query));
  2560. $this->admin->expects($this->once())
  2561. ->method('getDatagrid')
  2562. ->will($this->returnValue($datagrid));
  2563. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  2564. $this->admin->expects($this->once())
  2565. ->method('checkAccess')
  2566. ->with($this->equalTo('batchDelete'))
  2567. ->will($this->returnValue(true));
  2568. $this->admin->expects($this->any())
  2569. ->method('getModelManager')
  2570. ->will($this->returnValue($modelManager));
  2571. $this->admin->expects($this->any())
  2572. ->method('getClass')
  2573. ->will($this->returnValue('Foo'));
  2574. $modelManager->expects($this->once())
  2575. ->method('addIdentifiersToQuery')
  2576. ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
  2577. ->will($this->returnValue(true));
  2578. $this->request->setMethod('POST');
  2579. $this->request->request->set('data', json_encode(array('action' => 'delete', 'idx' => array('123', '456'), 'all_elements' => false)));
  2580. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2581. $result = $this->controller->batchAction($this->request);
  2582. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2583. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  2584. $this->assertSame('list?', $result->getTargetUrl());
  2585. }
  2586. /**
  2587. * NEXT_MAJOR: Remove this legacy group.
  2588. *
  2589. * @group legacy
  2590. */
  2591. public function testBatchActionWithoutConfirmation2()
  2592. {
  2593. $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
  2594. $this->admin->expects($this->once())
  2595. ->method('getBatchActions')
  2596. ->will($this->returnValue($batchActions));
  2597. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2598. $query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  2599. $datagrid->expects($this->once())
  2600. ->method('getQuery')
  2601. ->will($this->returnValue($query));
  2602. $this->admin->expects($this->once())
  2603. ->method('getDatagrid')
  2604. ->will($this->returnValue($datagrid));
  2605. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  2606. $this->admin->expects($this->once())
  2607. ->method('checkAccess')
  2608. ->with($this->equalTo('batchDelete'))
  2609. ->will($this->returnValue(true));
  2610. $this->admin->expects($this->any())
  2611. ->method('getModelManager')
  2612. ->will($this->returnValue($modelManager));
  2613. $this->admin->expects($this->any())
  2614. ->method('getClass')
  2615. ->will($this->returnValue('Foo'));
  2616. $modelManager->expects($this->once())
  2617. ->method('addIdentifiersToQuery')
  2618. ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
  2619. ->will($this->returnValue(true));
  2620. $this->request->setMethod('POST');
  2621. $this->request->request->set('action', 'delete');
  2622. $this->request->request->set('idx', array('123', '456'));
  2623. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2624. $result = $this->controller->batchAction($this->request);
  2625. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2626. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  2627. $this->assertSame('list?', $result->getTargetUrl());
  2628. }
  2629. /**
  2630. * NEXT_MAJOR: Remove this legacy group.
  2631. *
  2632. * @group legacy
  2633. */
  2634. public function testBatchActionWithConfirmation()
  2635. {
  2636. $batchActions = array('delete' => array('label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true));
  2637. $this->admin->expects($this->once())
  2638. ->method('getBatchActions')
  2639. ->will($this->returnValue($batchActions));
  2640. $data = array('action' => 'delete', 'idx' => array('123', '456'), 'all_elements' => false);
  2641. $this->request->setMethod('POST');
  2642. $this->request->request->set('data', json_encode($data));
  2643. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2644. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2645. $this->admin->expects($this->once())
  2646. ->method('getDatagrid')
  2647. ->will($this->returnValue($datagrid));
  2648. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  2649. ->disableOriginalConstructor()
  2650. ->getMock();
  2651. $form->expects($this->once())
  2652. ->method('createView')
  2653. ->will($this->returnValue($this->createMock('Symfony\Component\Form\FormView')));
  2654. $datagrid->expects($this->once())
  2655. ->method('getForm')
  2656. ->will($this->returnValue($form));
  2657. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->batchAction($this->request));
  2658. $this->assertSame($this->admin, $this->parameters['admin']);
  2659. $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
  2660. $this->assertSame($this->pool, $this->parameters['admin_pool']);
  2661. $this->assertSame('list', $this->parameters['action']);
  2662. $this->assertSame($datagrid, $this->parameters['datagrid']);
  2663. $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
  2664. $this->assertSame($data, $this->parameters['data']);
  2665. $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
  2666. $this->assertSame('Foo Bar', $this->parameters['action_label']);
  2667. $this->assertSame(array(), $this->session->getFlashBag()->all());
  2668. $this->assertSame('SonataAdminBundle:CRUD:batch_confirmation.html.twig', $this->template);
  2669. }
  2670. /**
  2671. * NEXT_MAJOR: Remove this legacy group.
  2672. *
  2673. * @group legacy
  2674. */
  2675. public function testBatchActionNonRelevantAction()
  2676. {
  2677. $controller = new BatchAdminController();
  2678. $controller->setContainer($this->container);
  2679. $batchActions = array('foo' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
  2680. $this->admin->expects($this->once())
  2681. ->method('getBatchActions')
  2682. ->will($this->returnValue($batchActions));
  2683. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2684. $this->admin->expects($this->once())
  2685. ->method('getDatagrid')
  2686. ->will($this->returnValue($datagrid));
  2687. $this->request->setMethod('POST');
  2688. $this->request->request->set('action', 'foo');
  2689. $this->request->request->set('idx', array('789'));
  2690. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2691. $result = $controller->batchAction($this->request);
  2692. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2693. $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
  2694. $this->assertSame('list?', $result->getTargetUrl());
  2695. }
  2696. /**
  2697. * NEXT_MAJOR: Remove this legacy group.
  2698. *
  2699. * @group legacy
  2700. */
  2701. public function testBatchActionNonRelevantAction2()
  2702. {
  2703. $controller = new BatchAdminController();
  2704. $controller->setContainer($this->container);
  2705. $batchActions = array('foo' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
  2706. $this->admin->expects($this->once())
  2707. ->method('getBatchActions')
  2708. ->will($this->returnValue($batchActions));
  2709. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2710. $this->admin->expects($this->once())
  2711. ->method('getDatagrid')
  2712. ->will($this->returnValue($datagrid));
  2713. $this->request->setMethod('POST');
  2714. $this->request->request->set('action', 'foo');
  2715. $this->request->request->set('idx', array('999'));
  2716. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2717. $result = $controller->batchAction($this->request);
  2718. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2719. $this->assertSame(array('flash_foo_error'), $this->session->getFlashBag()->get('sonata_flash_info'));
  2720. $this->assertSame('list?', $result->getTargetUrl());
  2721. }
  2722. /**
  2723. * NEXT_MAJOR: Remove this legacy group.
  2724. *
  2725. * @group legacy
  2726. */
  2727. public function testBatchActionNoItems()
  2728. {
  2729. $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => true));
  2730. $this->admin->expects($this->once())
  2731. ->method('getBatchActions')
  2732. ->will($this->returnValue($batchActions));
  2733. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2734. $this->admin->expects($this->once())
  2735. ->method('getDatagrid')
  2736. ->will($this->returnValue($datagrid));
  2737. $this->request->setMethod('POST');
  2738. $this->request->request->set('action', 'delete');
  2739. $this->request->request->set('idx', array());
  2740. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2741. $result = $this->controller->batchAction($this->request);
  2742. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2743. $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
  2744. $this->assertSame('list?', $result->getTargetUrl());
  2745. }
  2746. /**
  2747. * NEXT_MAJOR: Remove this legacy group.
  2748. *
  2749. * @group legacy
  2750. */
  2751. public function testBatchActionNoItemsEmptyQuery()
  2752. {
  2753. $controller = new BatchAdminController();
  2754. $controller->setContainer($this->container);
  2755. $batchActions = array('bar' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
  2756. $this->admin->expects($this->once())
  2757. ->method('getBatchActions')
  2758. ->will($this->returnValue($batchActions));
  2759. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2760. $query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  2761. $datagrid->expects($this->once())
  2762. ->method('getQuery')
  2763. ->will($this->returnValue($query));
  2764. $this->admin->expects($this->once())
  2765. ->method('getDatagrid')
  2766. ->will($this->returnValue($datagrid));
  2767. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  2768. $this->admin->expects($this->any())
  2769. ->method('getModelManager')
  2770. ->will($this->returnValue($modelManager));
  2771. $this->admin->expects($this->any())
  2772. ->method('getClass')
  2773. ->will($this->returnValue('Foo'));
  2774. $this->request->setMethod('POST');
  2775. $this->request->request->set('action', 'bar');
  2776. $this->request->request->set('idx', array());
  2777. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2778. $result = $controller->batchAction($this->request);
  2779. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $result);
  2780. $this->assertSame('batchActionBar executed', $result->getContent());
  2781. }
  2782. /**
  2783. * NEXT_MAJOR: Remove this legacy group.
  2784. *
  2785. * @group legacy
  2786. */
  2787. public function testBatchActionWithRequesData()
  2788. {
  2789. $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
  2790. $this->admin->expects($this->once())
  2791. ->method('getBatchActions')
  2792. ->will($this->returnValue($batchActions));
  2793. $datagrid = $this->createMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
  2794. $query = $this->createMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
  2795. $datagrid->expects($this->once())
  2796. ->method('getQuery')
  2797. ->will($this->returnValue($query));
  2798. $this->admin->expects($this->once())
  2799. ->method('getDatagrid')
  2800. ->will($this->returnValue($datagrid));
  2801. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  2802. $this->admin->expects($this->once())
  2803. ->method('checkAccess')
  2804. ->with($this->equalTo('batchDelete'))
  2805. ->will($this->returnValue(true));
  2806. $this->admin->expects($this->any())
  2807. ->method('getModelManager')
  2808. ->will($this->returnValue($modelManager));
  2809. $this->admin->expects($this->any())
  2810. ->method('getClass')
  2811. ->will($this->returnValue('Foo'));
  2812. $modelManager->expects($this->once())
  2813. ->method('addIdentifiersToQuery')
  2814. ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
  2815. ->will($this->returnValue(true));
  2816. $this->request->setMethod('POST');
  2817. $this->request->request->set('data', json_encode(array('action' => 'delete', 'idx' => array('123', '456'), 'all_elements' => false)));
  2818. $this->request->request->set('foo', 'bar');
  2819. $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
  2820. $result = $this->controller->batchAction($this->request);
  2821. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
  2822. $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
  2823. $this->assertSame('list?', $result->getTargetUrl());
  2824. $this->assertSame('bar', $this->request->request->get('foo'));
  2825. }
  2826. public function getCsrfProvider()
  2827. {
  2828. return $this->csrfProvider;
  2829. }
  2830. public function getToStringValues()
  2831. {
  2832. return array(
  2833. array('', ''),
  2834. array('Foo', 'Foo'),
  2835. array('&lt;a href=&quot;http://foo&quot;&gt;Bar&lt;/a&gt;', '<a href="http://foo">Bar</a>'),
  2836. array('&lt;&gt;&amp;&quot;&#039;abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/', '<>&"\'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/'),
  2837. );
  2838. }
  2839. private function assertLoggerLogsModelManagerException($subject, $method)
  2840. {
  2841. $exception = new ModelManagerException(
  2842. $message = 'message',
  2843. 1234,
  2844. new \Exception($previousExceptionMessage = 'very useful message')
  2845. );
  2846. $subject->expects($this->once())
  2847. ->method($method)
  2848. ->will($this->returnCallback(function () use ($exception) {
  2849. throw $exception;
  2850. }));
  2851. $this->logger->expects($this->once())
  2852. ->method('error')
  2853. ->with($message, array(
  2854. 'exception' => $exception,
  2855. 'previous_exception_message' => $previousExceptionMessage,
  2856. ));
  2857. }
  2858. private function expectTranslate($id, array $parameters = array(), $domain = null, $locale = null)
  2859. {
  2860. $this->translator->expects($this->once())
  2861. ->method('trans')
  2862. ->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale))
  2863. ->will($this->returnValue($id));
  2864. }
  2865. }