CRUDControllerTest.php 137 KB

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