CRUDControllerTest.php 138 KB

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