CRUDControllerTest.php 137 KB

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