CRUDControllerTest.php 138 KB

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