123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Controller;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
- use Sonata\AdminBundle\Admin\Pool;
- use Sonata\AdminBundle\Controller\CRUDController;
- use Sonata\AdminBundle\Exception\LockException;
- use Sonata\AdminBundle\Exception\ModelManagerException;
- use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController;
- use Sonata\AdminBundle\Tests\Fixtures\Controller\PreCRUDController;
- use Sonata\AdminBundle\Util\AdminObjectAclManipulator;
- use Symfony\Bridge\Twig\Extension\FormExtension;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\Session\Session;
- use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
- use Symfony\Component\HttpFoundation\StreamedResponse;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Component\HttpKernel\Kernel;
- use Symfony\Component\HttpKernel\KernelInterface;
- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
- /**
- * Test for CRUDController.
- *
- * @author Andrej Hudec <pulzarraider@gmail.com>
- */
- class CRUDControllerTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var CRUDController
- */
- private $controller;
- /**
- * @var Request
- */
- private $request;
- /**
- * @var AdminInterface
- */
- private $admin;
- /**
- * @var Pool
- */
- private $pool;
- /**
- * @var array
- */
- private $parameters;
- /**
- * @var Session
- */
- private $session;
- /**
- * @var \Sonata\AdminBundle\Model\AuditManager
- */
- private $auditManager;
- /**
- * @var ContainerInterface
- */
- private $container;
- /**
- * @var AdminObjectAclManipulator
- */
- private $adminObjectAclManipulator;
- /**
- * @var string
- */
- private $template;
- /**
- * @var array
- */
- private $protectedTestedMethods;
- /**
- * @var CsrfProviderInterface
- */
- private $csrfProvider;
- /**
- * @var KernelInterface
- */
- private $kernel;
- /**
- * {@inheritdoc}
- */
- protected function setUp()
- {
- $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $this->request = new Request();
- $this->pool = new Pool($this->container, 'title', 'logo.png');
- $this->pool->setAdminServiceIds(array('foo.admin'));
- $this->request->attributes->set('_sonata_admin', 'foo.admin');
- $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $this->parameters = array();
- $this->template = '';
- // php 5.3 BC
- $params = &$this->parameters;
- $template = &$this->template;
- $templating = $this->getMock(
- 'Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine',
- array(),
- array($this->container, array())
- );
- $templating->expects($this->any())
- ->method('renderResponse')
- ->will($this->returnCallback(function (
- $view,
- array $parameters = array(),
- Response $response = null
- ) use (
- &$params,
- &$template
- ) {
- $template = $view;
- if (null === $response) {
- $response = new Response();
- }
- $params = $parameters;
- return $response;
- }));
- $this->session = new Session(new MockArraySessionStorage());
- // php 5.3 BC
- $pool = $this->pool;
- $request = $this->request;
- $admin = $this->admin;
- $session = $this->session;
- $twig = $this->getMockBuilder('Twig_Environment')
- ->disableOriginalConstructor()
- ->getMock();
- $twigRenderer = $this->getMock('Symfony\Bridge\Twig\Form\TwigRendererInterface');
- $formExtension = new FormExtension($twigRenderer);
- $twig->expects($this->any())
- ->method('getExtension')
- ->will($this->returnCallback(function ($name) use ($formExtension) {
- switch ($name) {
- case 'form':
- return $formExtension;
- }
- return;
- }));
- $exporter = $this->getMock('Sonata\AdminBundle\Export\Exporter');
- $exporter->expects($this->any())
- ->method('getResponse')
- ->will($this->returnValue(new StreamedResponse()));
- $this->auditManager = $this->getMockBuilder('Sonata\AdminBundle\Model\AuditManager')
- ->disableOriginalConstructor()
- ->getMock();
- $this->adminObjectAclManipulator = $this->getMockBuilder('Sonata\AdminBundle\Util\AdminObjectAclManipulator')
- ->disableOriginalConstructor()
- ->getMock();
- // php 5.3 BC
- $request = $this->request;
- $auditManager = $this->auditManager;
- $adminObjectAclManipulator = $this->adminObjectAclManipulator;
- $this->csrfProvider = $this->getMockBuilder(
- 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'
- )
- ->getMock();
- $this->csrfProvider->expects($this->any())
- ->method('generateCsrfToken')
- ->will($this->returnCallback(function ($intention) {
- return 'csrf-token-123_'.$intention;
- }));
- $this->csrfProvider->expects($this->any())
- ->method('isCsrfTokenValid')
- ->will($this->returnCallback(function ($intention, $token) {
- if ($token == 'csrf-token-123_'.$intention) {
- return true;
- }
- return false;
- }));
- // php 5.3 BC
- $csrfProvider = $this->csrfProvider;
- $this->logger = $this->getMock('Psr\Log\LoggerInterface');
- $logger = $this->logger; // php 5.3 BC
- $requestStack = null;
- if (Kernel::MINOR_VERSION > 3) {
- $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
- $requestStack->push($request);
- }
- $this->kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
- $kernel = $this->kernel; // php 5.3 BC
- $this->container->expects($this->any())
- ->method('get')
- ->will($this->returnCallback(function ($id) use (
- $pool,
- $admin,
- $request,
- $templating,
- $twig,
- $session,
- $exporter,
- $auditManager,
- $adminObjectAclManipulator,
- $requestStack,
- $csrfProvider,
- $logger,
- $kernel
- ) {
- switch ($id) {
- case 'sonata.admin.pool':
- return $pool;
- case 'request':
- return $request;
- case 'request_stack':
- return $requestStack;
- case 'foo.admin':
- return $admin;
- case 'templating':
- return $templating;
- case 'twig':
- return $twig;
- case 'session':
- return $session;
- case 'sonata.admin.exporter':
- return $exporter;
- case 'sonata.admin.audit.manager':
- return $auditManager;
- case 'sonata.admin.object.manipulator.acl.admin':
- return $adminObjectAclManipulator;
- case 'form.csrf_provider':
- return $csrfProvider;
- case 'logger':
- return $logger;
- case 'kernel':
- return $kernel;
- }
- return;
- }));
- // php 5.3
- $tthis = $this;
- $this->container->expects($this->any())
- ->method('has')
- ->will($this->returnCallback(function ($id) use ($tthis) {
- if ($id == 'form.csrf_provider' && $tthis->getCsrfProvider() !== null) {
- return true;
- }
- if ($id == 'logger') {
- return true;
- }
- if ($id == 'session') {
- return true;
- }
- return false;
- }));
- $this->container->expects($this->any())
- ->method('getParameter')
- ->will($this->returnCallback(function ($name) {
- switch ($name) {
- case 'security.role_hierarchy.roles':
- return array('ROLE_SUPER_ADMIN' => array('ROLE_USER', 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN'));
- }
- return;
- }));
- $this->admin->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnCallback(function ($name) {
- switch ($name) {
- case 'ajax':
- return 'SonataAdminBundle::ajax_layout.html.twig';
- case 'layout':
- return 'SonataAdminBundle::standard_layout.html.twig';
- case 'show':
- return 'SonataAdminBundle:CRUD:show.html.twig';
- case 'show_compare':
- return 'SonataAdminBundle:CRUD:show_compare.html.twig';
- case 'edit':
- return 'SonataAdminBundle:CRUD:edit.html.twig';
- case 'dashboard':
- return 'SonataAdminBundle:Core:dashboard.html.twig';
- case 'search':
- return 'SonataAdminBundle:Core:search.html.twig';
- case 'list':
- return 'SonataAdminBundle:CRUD:list.html.twig';
- case 'preview':
- return 'SonataAdminBundle:CRUD:preview.html.twig';
- case 'history':
- return 'SonataAdminBundle:CRUD:history.html.twig';
- case 'acl':
- return 'SonataAdminBundle:CRUD:acl.html.twig';
- case 'delete':
- return 'SonataAdminBundle:CRUD:delete.html.twig';
- case 'batch':
- return 'SonataAdminBundle:CRUD:list__batch.html.twig';
- case 'batch_confirmation':
- return 'SonataAdminBundle:CRUD:batch_confirmation.html.twig';
- }
- return;
- }));
- $this->admin->expects($this->any())
- ->method('getIdParameter')
- ->will($this->returnValue('id'));
- $this->admin->expects($this->any())
- ->method('getAccessMapping')
- ->will($this->returnValue(array()));
- $this->admin->expects($this->any())
- ->method('generateUrl')
- ->will(
- $this->returnCallback(
- function ($name, array $parameters = array(), $absolute = false) {
- $result = $name;
- if (!empty($parameters)) {
- $result .= '?'.http_build_query($parameters);
- }
- return $result;
- }
- )
- );
- $this->admin->expects($this->any())
- ->method('generateObjectUrl')
- ->will(
- $this->returnCallback(
- function ($name, $object, array $parameters = array(), $absolute = false) {
- $result = get_class($object).'_'.$name;
- if (!empty($parameters)) {
- $result .= '?'.http_build_query($parameters);
- }
- return $result;
- }
- )
- );
- $this->controller = new CRUDController();
- $this->controller->setContainer($this->container);
- // Make some methods public to test them
- $testedMethods = array(
- 'renderJson',
- 'isXmlHttpRequest',
- 'configure',
- 'getBaseTemplate',
- 'redirectTo',
- 'addFlash',
- );
- foreach ($testedMethods as $testedMethod) {
- $method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod);
- $method->setAccessible(true);
- $this->protectedTestedMethods[$testedMethod] = $method;
- }
- }
- public function testRenderJson1()
- {
- $data = array('example' => '123', 'foo' => 'bar');
- $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
- $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, array(), $this->request);
- $this->assertSame($response->headers->get('Content-Type'), 'application/json');
- $this->assertSame(json_encode($data), $response->getContent());
- }
- public function testRenderJson2()
- {
- $data = array('example' => '123', 'foo' => 'bar');
- $this->request->headers->set('Content-Type', 'multipart/form-data');
- $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, array(), $this->request);
- $this->assertSame($response->headers->get('Content-Type'), 'application/json');
- $this->assertSame(json_encode($data), $response->getContent());
- }
- public function testRenderJsonAjax()
- {
- $data = array('example' => '123', 'foo' => 'bar');
- $this->request->attributes->set('_xml_http_request', true);
- $this->request->headers->set('Content-Type', 'multipart/form-data');
- $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data, 200, array(), $this->request);
- $this->assertSame($response->headers->get('Content-Type'), 'application/json');
- $this->assertSame(json_encode($data), $response->getContent());
- }
- public function testIsXmlHttpRequest()
- {
- $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
- $this->request->headers->remove('X-Requested-With');
- $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
- $this->request->attributes->set('_xml_http_request', true);
- $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller, $this->request));
- }
- public function testConfigure()
- {
- $uniqueId = '';
- $this->admin->expects($this->once())
- ->method('setUniqid')
- ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
- $uniqueId = $uniqid;
- }));
- $this->request->query->set('uniqid', 123456);
- $this->protectedTestedMethods['configure']->invoke($this->controller);
- $this->assertSame(123456, $uniqueId);
- $this->assertAttributeSame($this->admin, 'admin', $this->controller);
- }
- public function testConfigureChild()
- {
- $uniqueId = '';
- $this->admin->expects($this->once())
- ->method('setUniqid')
- ->will($this->returnCallback(function ($uniqid) use (&$uniqueId) {
- $uniqueId = $uniqid;
- }));
- $this->admin->expects($this->once())
- ->method('isChild')
- ->will($this->returnValue(true));
- $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $this->admin->expects($this->once())
- ->method('getParent')
- ->will($this->returnValue($adminParent));
- $this->request->query->set('uniqid', 123456);
- $this->protectedTestedMethods['configure']->invoke($this->controller);
- $this->assertSame(123456, $uniqueId);
- $this->assertAttributeInstanceOf(get_class($adminParent), 'admin', $this->controller);
- }
- public function testConfigureWithException()
- {
- $this->setExpectedException(
- 'RuntimeException',
- 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`'
- );
- $this->request->attributes->remove('_sonata_admin');
- $this->protectedTestedMethods['configure']->invoke($this->controller);
- }
- public function testConfigureWithException2()
- {
- $this->setExpectedException(
- 'RuntimeException',
- 'Unable to find the admin class related to the current controller '.
- '(Sonata\AdminBundle\Controller\CRUDController)'
- );
- $this->pool->setAdminServiceIds(array('nonexistent.admin'));
- $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
- $this->protectedTestedMethods['configure']->invoke($this->controller);
- }
- public function testGetBaseTemplate()
- {
- $this->assertSame(
- 'SonataAdminBundle::standard_layout.html.twig',
- $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
- );
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $this->assertSame(
- 'SonataAdminBundle::ajax_layout.html.twig',
- $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
- );
- $this->request->headers->remove('X-Requested-With');
- $this->assertSame(
- 'SonataAdminBundle::standard_layout.html.twig',
- $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
- );
- $this->request->attributes->set('_xml_http_request', true);
- $this->assertSame(
- 'SonataAdminBundle::ajax_layout.html.twig',
- $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller, $this->request)
- );
- }
- public function testRender()
- {
- $this->parameters = array();
- $this->assertInstanceOf(
- 'Symfony\Component\HttpFoundation\Response',
- $this->controller->render('FooAdminBundle::foo.html.twig', array(), null, $this->request)
- );
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
- }
- public function testRenderWithResponse()
- {
- $this->parameters = array();
- $response = $response = new Response();
- $response->headers->set('X-foo', 'bar');
- $responseResult = $this->controller->render('FooAdminBundle::foo.html.twig', array(), $response, $this->request);
- $this->assertSame($response, $responseResult);
- $this->assertSame('bar', $responseResult->headers->get('X-foo'));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
- }
- public function testRenderCustomParams()
- {
- $this->parameters = array();
- $this->assertInstanceOf(
- 'Symfony\Component\HttpFoundation\Response',
- $this->controller->render('FooAdminBundle::foo.html.twig',
- array('foo' => 'bar'), null, $this->request)
- );
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('bar', $this->parameters['foo']);
- $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
- }
- public function testRenderAjax()
- {
- $this->parameters = array();
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->render('FooAdminBundle::foo.html.twig', array('foo' => 'bar'), null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('bar', $this->parameters['foo']);
- $this->assertSame('FooAdminBundle::foo.html.twig', $this->template);
- }
- public function testListActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('list'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->listAction($this->request);
- }
- public function testPreList()
- {
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('list'))
- ->will($this->returnValue(true));
- $controller = new PreCRUDController();
- $controller->setContainer($this->container);
- $response = $controller->listAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame('preList called', $response->getContent());
- }
- public function testListAction()
- {
- $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('list'))
- ->will($this->returnValue(true));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $form->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $datagrid->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $this->parameters = array();
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->listAction($this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('list', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertInstanceOf('Sonata\AdminBundle\Datagrid\DatagridInterface', $this->parameters['datagrid']);
- $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:list.html.twig', $this->template);
- }
- public function testBatchActionDeleteAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('batchDelete'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
- }
- public function testBatchActionDelete()
- {
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('batchDelete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $this->admin->expects($this->once())
- ->method('getFilterParameters')
- ->will($this->returnValue(array('foo' => 'bar')));
- $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
- }
- private function assertLoggerLogsModelManagerException($subject, $method)
- {
- $exception = new ModelManagerException(
- $message = 'message',
- 1234,
- new \Exception($previousExceptionMessage = 'very useful message')
- );
- $subject->expects($this->once())
- ->method($method)
- ->will($this->returnCallback(function () use ($exception) {
- throw $exception;
- }));
- $this->logger->expects($this->once())
- ->method('error')
- ->with($message, array(
- 'exception' => $exception,
- 'previous_exception_message' => $previousExceptionMessage,
- ));
- }
- public function testBatchActionDeleteWithModelManagerException()
- {
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete');
- $this->admin->expects($this->once())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $this->admin->expects($this->once())
- ->method('getFilterParameters')
- ->will($this->returnValue(array('foo' => 'bar')));
- $result = $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_batch_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
- $this->assertSame('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
- }
- public function testBatchActionDeleteWithModelManagerExceptionInDebugMode()
- {
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->setExpectedException('Sonata\AdminBundle\Exception\ModelManagerException');
- $modelManager->expects($this->once())
- ->method('batchDelete')
- ->will($this->returnCallback(function () {
- throw new ModelManagerException();
- }));
- $this->admin->expects($this->once())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $this->kernel->expects($this->once())
- ->method('isDebug')
- ->will($this->returnValue(true));
- $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
- }
- public function testShowActionNotFoundException()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(false));
- $this->controller->showAction(null, $this->request);
- }
- public function testShowActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(new \stdClass()));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('show'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->showAction(null, $this->request);
- }
- public function testPreShow()
- {
- $object = new \stdClass();
- $object->foo = 123456;
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('show'))
- ->will($this->returnValue(true));
- $controller = new PreCRUDController();
- $controller->setContainer($this->container);
- $response = $controller->showAction(null, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame('preShow called: 123456', $response->getContent());
- }
- public function testShowAction()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('show'))
- ->will($this->returnValue(true));
- $show = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
- $this->admin->expects($this->once())
- ->method('getShow')
- ->will($this->returnValue($show));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->showAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('show', $this->parameters['action']);
- $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $this->parameters['elements']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:show.html.twig', $this->template);
- }
- /**
- * @dataProvider getRedirectToTests
- */
- public function testRedirectTo($expected, $queryParams, $hasActiveSubclass)
- {
- $this->admin->expects($this->any())
- ->method('hasActiveSubclass')
- ->will($this->returnValue($hasActiveSubclass));
- $object = new \stdClass();
- foreach ($queryParams as $key => $value) {
- $this->request->query->set($key, $value);
- }
- $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame($expected, $response->getTargetUrl());
- }
- public function getRedirectToTests()
- {
- return array(
- array('stdClass_edit', array(), false),
- array('list', array('btn_update_and_list' => true), false),
- array('list', array('btn_create_and_list' => true), false),
- array('create', array('btn_create_and_create' => true), false),
- array('create?subclass=foo', array('btn_create_and_create' => true, 'subclass' => 'foo'), true),
- );
- }
- public function testAddFlash()
- {
- $this->protectedTestedMethods['addFlash']->invoke($this->controller, 'foo', 'bar');
- $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
- }
- public function testDeleteActionNotFoundException()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(false));
- $this->controller->deleteAction(1, $this->request);
- }
- public function testDeleteActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(new \stdClass()));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->deleteAction(1, $this->request);
- }
- public function testPreDelete()
- {
- $object = new \stdClass();
- $object->foo = 123456;
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $controller = new PreCRUDController();
- $controller->setContainer($this->container);
- $response = $controller->deleteAction(null, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame('preDelete called: 123456', $response->getContent());
- }
- public function testDeleteAction()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('delete', $this->parameters['action']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
- }
- public function testDeleteActionNoCsrfToken()
- {
- $this->csrfProvider = null;
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('delete', $this->parameters['action']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(false, $this->parameters['csrf_token']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
- }
- public function testDeleteActionAjaxSuccess1()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->request->setMethod('DELETE');
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
- $response = $this->controller->deleteAction(1, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame(json_encode(array('result' => 'ok')), $response->getContent());
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- }
- public function testDeleteActionAjaxSuccess2()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->request->request->set('_method', 'DELETE');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $response = $this->controller->deleteAction(1, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame(json_encode(array('result' => 'ok')), $response->getContent());
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- }
- public function testDeleteActionAjaxError()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
- $this->request->setMethod('DELETE');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $response = $this->controller->deleteAction(1, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame(json_encode(array('result' => 'error')), $response->getContent());
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- }
- public function testDeleteActionWithModelManagerExceptionInDebugMode()
- {
- $this->setExpectedException('Sonata\AdminBundle\Exception\ModelManagerException');
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('delete')
- ->will($this->returnCallback(function () {
- throw new ModelManagerException();
- }));
- $this->kernel->expects($this->once())
- ->method('isDebug')
- ->will($this->returnValue(true));
- $this->request->setMethod('DELETE');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
- $this->controller->deleteAction(1, $this->request);
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testDeleteActionSuccess1($expectedToStringValue, $toStringValue)
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_delete_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->request->setMethod('DELETE');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
- $response = $this->controller->deleteAction(1, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('list', $response->getTargetUrl());
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testDeleteActionSuccess2($expectedToStringValue, $toStringValue)
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_delete_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->request->setMethod('POST');
- $this->request->request->set('_method', 'DELETE');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
- $response = $this->controller->deleteAction(1, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('list', $response->getTargetUrl());
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testDeleteActionSuccessNoCsrfTokenProvider($expectedToStringValue, $toStringValue)
- {
- $this->csrfProvider = null;
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_delete_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->request->setMethod('POST');
- $this->request->request->set('_method', 'DELETE');
- $response = $this->controller->deleteAction(1, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('list', $response->getTargetUrl());
- }
- public function testDeleteActionWrongRequestMethod()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- //without POST request parameter "_method" should not be used as real REST method
- $this->request->query->set('_method', 'DELETE');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('delete', $this->parameters['action']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame('csrf-token-123_sonata.delete', $this->parameters['csrf_token']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:delete.html.twig', $this->template);
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testDeleteActionError($expectedToStringValue, $toStringValue)
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_delete_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
- $this->request->setMethod('DELETE');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
- $response = $this->controller->deleteAction(1, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
- $this->assertSame('list', $response->getTargetUrl());
- }
- public function testDeleteActionInvalidCsrfToken()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('delete'))
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->request->request->set('_method', 'DELETE');
- $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
- try {
- $this->controller->deleteAction(1, $this->request);
- } catch (HttpException $e) {
- $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
- $this->assertSame(400, $e->getStatusCode());
- }
- }
- public function testEditActionNotFoundException()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(false));
- $this->controller->editAction(null, $this->request);
- }
- public function testEditActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(new \stdClass()));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->editAction(null, $this->request);
- }
- public function testPreEdit()
- {
- $object = new \stdClass();
- $object->foo = 123456;
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $controller = new PreCRUDController();
- $controller->setContainer($this->container);
- $response = $controller->editAction(null, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame('preEdit called: 123456', $response->getContent());
- }
- public function testEditAction()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('edit', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testEditActionSuccess($expectedToStringValue, $toStringValue)
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('update')
- ->will($this->returnArgument(0));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_edit_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->request->setMethod('POST');
- $response = $this->controller->editAction(null, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame(array('flash_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('stdClass_edit', $response->getTargetUrl());
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testEditActionError($expectedToStringValue, $toStringValue)
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(false));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_edit_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->request->setMethod('POST');
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('edit', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array('sonata_flash_error' => array('flash_edit_error')), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- public function testEditActionAjaxSuccess()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('update')
- ->will($this->returnArgument(0));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('getNormalizedIdentifier')
- ->with($this->equalTo($object))
- ->will($this->returnValue('foo_normalized'));
- $this->admin->expects($this->once())
- ->method('toString')
- ->will($this->returnValue('foo'));
- $this->request->setMethod('POST');
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $response = $this->controller->editAction(null, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame(json_encode(array('result' => 'ok', 'objectId' => 'foo_normalized', 'objectName' => 'foo')), $response->getContent());
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- }
- public function testEditActionAjaxError()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(false));
- $this->request->setMethod('POST');
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('edit', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testEditActionWithModelManagerException($expectedToStringValue, $toStringValue)
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_edit_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertLoggerLogsModelManagerException($this->admin, 'update');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('edit', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array('sonata_flash_error' => array('flash_edit_error')), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- public function testEditActionWithPreview()
- {
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $this->admin->expects($this->once())
- ->method('supportsPreviewMode')
- ->will($this->returnValue(true));
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->request->request->set('btn_preview', 'Preview');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('edit', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
- }
- public function testEditActionWithLockException()
- {
- $object = new \stdClass();
- $class = get_class($object);
- $this->admin->expects($this->any())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('checkAccess')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue($class));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $form->expects($this->any())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->any())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->admin->expects($this->any())
- ->method('update')
- ->will($this->throwException(new LockException()));
- $this->admin->expects($this->any())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($class));
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->expectTranslate('flash_lock_error', array(
- '%name%' => $class,
- '%link_start%' => '<a href="stdClass_edit">',
- '%link_end%' => '</a>',
- ), 'SonataAdminBundle');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction(null, $this->request));
- }
- public function testCreateActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('create'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->createAction($this->request);
- }
- public function testPreCreate()
- {
- $object = new \stdClass();
- $object->foo = 123456;
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('create'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $controller = new PreCRUDController();
- $controller->setContainer($this->container);
- $response = $controller->createAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame('preCreate called: 123456', $response->getContent());
- }
- public function testCreateAction()
- {
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('create'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('create', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testCreateActionSuccess($expectedToStringValue, $toStringValue)
- {
- $object = new \stdClass();
- $this->admin->expects($this->exactly(2))
- ->method('checkAccess')
- ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
- if ($name != 'create') {
- return false;
- }
- if ($objectIn === null) {
- return true;
- }
- return ($objectIn === $object);
- }));
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('create')
- ->will($this->returnArgument(0));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_create_success', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->request->setMethod('POST');
- $response = $this->controller->createAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame(array('flash_create_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('stdClass_edit', $response->getTargetUrl());
- }
- public function testCreateActionAccessDenied2()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $object = new \stdClass();
- $this->admin->expects($this->any())
- ->method('checkAccess')
- ->will($this->returnCallback(function ($name, $object = null) {
- if ($name != 'create') {
- throw new AccessDeniedException();
- }
- if ($object === null) {
- return true;
- }
- throw new AccessDeniedException();
- }));
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->controller->createAction($this->request);
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testCreateActionError($expectedToStringValue, $toStringValue)
- {
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('create'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(false));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_create_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $this->request->setMethod('POST');
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('create', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array('sonata_flash_error' => array('flash_create_error')), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- /**
- * @dataProvider getToStringValues
- */
- public function testCreateActionWithModelManagerException($expectedToStringValue, $toStringValue)
- {
- $this->admin->expects($this->exactly(2))
- ->method('checkAccess')
- ->with($this->equalTo('create'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('toString')
- ->with($this->equalTo($object))
- ->will($this->returnValue($toStringValue));
- $this->expectTranslate('flash_create_error', array('%name%' => $expectedToStringValue), 'SonataAdminBundle');
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertLoggerLogsModelManagerException($this->admin, 'create');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('create', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array('sonata_flash_error' => array('flash_create_error')), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- public function testCreateActionAjaxSuccess()
- {
- $object = new \stdClass();
- $this->admin->expects($this->exactly(2))
- ->method('checkAccess')
- ->will($this->returnCallback(function ($name, $objectIn = null) use ($object) {
- if ($name != 'create') {
- return false;
- }
- if ($objectIn === null) {
- return true;
- }
- return ($objectIn === $object);
- }));
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('create')
- ->will($this->returnArgument(0));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getNormalizedIdentifier')
- ->with($this->equalTo($object))
- ->will($this->returnValue('foo_normalized'));
- $this->request->setMethod('POST');
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $response = $this->controller->createAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
- $this->assertSame(json_encode(array('result' => 'ok', 'objectId' => 'foo_normalized')), $response->getContent());
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- }
- public function testCreateActionAjaxError()
- {
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('create'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(false));
- $this->request->setMethod('POST');
- $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('create', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:edit.html.twig', $this->template);
- }
- public function testCreateActionWithPreview()
- {
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('create'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue($object));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('stdClass'));
- $this->admin->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $this->admin->expects($this->once())
- ->method('supportsPreviewMode')
- ->will($this->returnValue(true));
- $formView = $this->getMock('Symfony\Component\Form\FormView');
- $form->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($formView));
- $form->expects($this->once())
- ->method('isSubmitted')
- ->will($this->returnValue(true));
- $form->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->request->request->set('btn_preview', 'Preview');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->createAction($this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('create', $this->parameters['action']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:preview.html.twig', $this->template);
- }
- public function testExportActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('export'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->exportAction($this->request);
- }
- public function testExportActionWrongFormat()
- {
- $this->setExpectedException('RuntimeException', 'Export in format `csv` is not allowed for class: `Foo`. Allowed formats are: `json`');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('export'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('getExportFormats')
- ->will($this->returnValue(array('json')));
- $this->admin->expects($this->once())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->request->query->set('format', 'csv');
- $this->controller->exportAction($this->request);
- }
- public function testExportAction()
- {
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('export'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('getExportFormats')
- ->will($this->returnValue(array('json')));
- $dataSourceIterator = $this->getMock('Exporter\Source\SourceIteratorInterface');
- $this->admin->expects($this->once())
- ->method('getDataSourceIterator')
- ->will($this->returnValue($dataSourceIterator));
- $this->request->query->set('format', 'json');
- $response = $this->controller->exportAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
- $this->assertSame(200, $response->getStatusCode());
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- }
- public function testHistoryActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->any())
- ->method('getObject')
- ->will($this->returnValue(new \StdClass()));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('history'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->historyAction(null, $this->request);
- }
- public function testHistoryActionNotFoundException()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(false));
- $this->controller->historyAction(null, $this->request);
- }
- public function testHistoryActionNoReader()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('history'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(false));
- $this->controller->historyAction(null, $this->request);
- }
- public function testHistoryAction()
- {
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('history'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(true));
- $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
- $this->auditManager->expects($this->once())
- ->method('getReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue($reader));
- $reader->expects($this->once())
- ->method('findRevisions')
- ->with($this->equalTo('Foo'), $this->equalTo(123))
- ->will($this->returnValue(array()));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('history', $this->parameters['action']);
- $this->assertSame(array(), $this->parameters['revisions']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:history.html.twig', $this->template);
- }
- public function testAclActionAclNotEnabled()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'ACL are not enabled for this admin');
- $this->controller->aclAction(null, $this->request);
- }
- public function testAclActionNotFoundException()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
- $this->admin->expects($this->once())
- ->method('isAclEnabled')
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(false));
- $this->controller->aclAction(null, $this->request);
- }
- public function testAclActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('isAclEnabled')
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('acl'), $this->equalTo($object))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->aclAction(null, $this->request);
- }
- public function testAclAction()
- {
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('isAclEnabled')
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('checkAccess')
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getSecurityInformation')
- ->will($this->returnValue(array()));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('getMaskBuilderClass')
- ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
- $aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $aclUsersForm->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $aclRolesForm->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('createAclUsersForm')
- ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
- ->will($this->returnValue($aclUsersForm));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('createAclRolesForm')
- ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
- ->will($this->returnValue($aclRolesForm));
- $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
- ->disableOriginalConstructor()
- ->getMock();
- $aclSecurityHandler->expects($this->any())
- ->method('getObjectPermissions')
- ->will($this->returnValue(array()));
- $this->admin->expects($this->any())
- ->method('getSecurityHandler')
- ->will($this->returnValue($aclSecurityHandler));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('acl', $this->parameters['action']);
- $this->assertSame(array(), $this->parameters['permissions']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
- $this->assertInstanceOf('\ArrayIterator', $this->parameters['roles']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclUsersForm']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclRolesForm']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
- }
- public function testAclActionInvalidUpdate()
- {
- $this->request->query->set('id', 123);
- $this->request->request->set(AdminObjectAclManipulator::ACL_USERS_FORM_NAME, array());
- $this->admin->expects($this->once())
- ->method('isAclEnabled')
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('checkAccess')
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getSecurityInformation')
- ->will($this->returnValue(array()));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('getMaskBuilderClass')
- ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
- $aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $aclUsersForm->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(false));
- $aclUsersForm->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $aclRolesForm->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('createAclUsersForm')
- ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
- ->will($this->returnValue($aclUsersForm));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('createAclRolesForm')
- ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
- ->will($this->returnValue($aclRolesForm));
- $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
- ->disableOriginalConstructor()
- ->getMock();
- $aclSecurityHandler->expects($this->any())
- ->method('getObjectPermissions')
- ->will($this->returnValue(array()));
- $this->admin->expects($this->any())
- ->method('getSecurityHandler')
- ->will($this->returnValue($aclSecurityHandler));
- $this->request->setMethod('POST');
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->aclAction(null, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('acl', $this->parameters['action']);
- $this->assertSame(array(), $this->parameters['permissions']);
- $this->assertSame($object, $this->parameters['object']);
- $this->assertInstanceOf('\ArrayIterator', $this->parameters['users']);
- $this->assertInstanceOf('\ArrayIterator', $this->parameters['roles']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclUsersForm']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['aclRolesForm']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:acl.html.twig', $this->template);
- }
- public function testAclActionSuccessfulUpdate()
- {
- $this->request->query->set('id', 123);
- $this->request->request->set(AdminObjectAclManipulator::ACL_ROLES_FORM_NAME, array());
- $this->admin->expects($this->once())
- ->method('isAclEnabled')
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('checkAccess')
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getSecurityInformation')
- ->will($this->returnValue(array()));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('getMaskBuilderClass')
- ->will($this->returnValue('\Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap'));
- $aclUsersForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $aclUsersForm->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $aclRolesForm = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $aclRolesForm->expects($this->any())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $aclRolesForm->expects($this->once())
- ->method('isValid')
- ->will($this->returnValue(true));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('createAclUsersForm')
- ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
- ->will($this->returnValue($aclUsersForm));
- $this->adminObjectAclManipulator->expects($this->once())
- ->method('createAclRolesForm')
- ->with($this->isInstanceOf('Sonata\AdminBundle\Util\AdminObjectAclData'))
- ->will($this->returnValue($aclRolesForm));
- $aclSecurityHandler = $this->getMockBuilder('Sonata\AdminBundle\Security\Handler\AclSecurityHandler')
- ->disableOriginalConstructor()
- ->getMock();
- $aclSecurityHandler->expects($this->any())
- ->method('getObjectPermissions')
- ->will($this->returnValue(array()));
- $this->admin->expects($this->any())
- ->method('getSecurityHandler')
- ->will($this->returnValue($aclSecurityHandler));
- $this->request->setMethod('POST');
- $response = $this->controller->aclAction(null, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame(array('flash_acl_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('stdClass_acl', $response->getTargetUrl());
- }
- public function testHistoryViewRevisionActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->any())
- ->method('getObject')
- ->will($this->returnValue(new \StdClass()));
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyViewRevision'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->historyViewRevisionAction(null, null, $this->request);
- }
- public function testHistoryViewRevisionActionNotFoundException()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id : 123');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(false));
- $this->controller->historyViewRevisionAction(null, null, $this->request);
- }
- public function testHistoryViewRevisionActionNoReader()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyViewRevision'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(false));
- $this->controller->historyViewRevisionAction(null, null, $this->request);
- }
- public function testHistoryViewRevisionActionNotFoundRevision()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyViewRevision'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(true));
- $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
- $this->auditManager->expects($this->once())
- ->method('getReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue($reader));
- $reader->expects($this->once())
- ->method('find')
- ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
- ->will($this->returnValue(null));
- $this->controller->historyViewRevisionAction(123, 456, $this->request);
- }
- public function testHistoryViewRevisionAction()
- {
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyViewRevision'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(true));
- $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
- $this->auditManager->expects($this->once())
- ->method('getReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue($reader));
- $objectRevision = new \stdClass();
- $objectRevision->revision = 456;
- $reader->expects($this->once())
- ->method('find')
- ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
- ->will($this->returnValue($objectRevision));
- $this->admin->expects($this->once())
- ->method('setSubject')
- ->with($this->equalTo($objectRevision))
- ->will($this->returnValue(null));
- $fieldDescriptionCollection = new FieldDescriptionCollection();
- $this->admin->expects($this->once())
- ->method('getShow')
- ->will($this->returnValue($fieldDescriptionCollection));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyViewRevisionAction(123, 456, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('show', $this->parameters['action']);
- $this->assertSame($objectRevision, $this->parameters['object']);
- $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:show.html.twig', $this->template);
- }
- public function testHistoryCompareRevisionsActionAccessDenied()
- {
- $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyCompareRevisions'))
- ->will($this->throwException(new AccessDeniedException()));
- $this->controller->historyCompareRevisionsAction(null, null, null, $this->request);
- }
- public function testHistoryCompareRevisionsActionNotFoundException()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the object with id : 123');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyCompareRevisions'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue(false));
- $this->controller->historyCompareRevisionsAction(null, null, null, $this->request);
- }
- public function testHistoryCompareRevisionsActionNoReader()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the audit reader for class : Foo');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyCompareRevisions'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(false));
- $this->controller->historyCompareRevisionsAction(null, null, null, $this->request);
- }
- public function testHistoryCompareRevisionsActionNotFoundBaseRevision()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `456` with classname : `Foo`');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyCompareRevisions'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(true));
- $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
- $this->auditManager->expects($this->once())
- ->method('getReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue($reader));
- // once because it will not be found and therefore the second call won't be executed
- $reader->expects($this->once())
- ->method('find')
- ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
- ->will($this->returnValue(null));
- $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request);
- }
- public function testHistoryCompareRevisionsActionNotFoundCompareRevision()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'unable to find the targeted object `123` from the revision `789` with classname : `Foo`');
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyCompareRevisions'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(true));
- $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
- $this->auditManager->expects($this->once())
- ->method('getReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue($reader));
- $objectRevision = new \stdClass();
- $objectRevision->revision = 456;
- // first call should return, so the second call will throw an exception
- $reader->expects($this->at(0))
- ->method('find')
- ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
- ->will($this->returnValue($objectRevision));
- $reader->expects($this->at(1))
- ->method('find')
- ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
- ->will($this->returnValue(null));
- $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request);
- }
- public function testHistoryCompareRevisionsActionAction()
- {
- $this->request->query->set('id', 123);
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('historyCompareRevisions'))
- ->will($this->returnValue(true));
- $object = new \stdClass();
- $this->admin->expects($this->once())
- ->method('getObject')
- ->will($this->returnValue($object));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->auditManager->expects($this->once())
- ->method('hasReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue(true));
- $reader = $this->getMock('Sonata\AdminBundle\Model\AuditReaderInterface');
- $this->auditManager->expects($this->once())
- ->method('getReader')
- ->with($this->equalTo('Foo'))
- ->will($this->returnValue($reader));
- $objectRevision = new \stdClass();
- $objectRevision->revision = 456;
- $compareObjectRevision = new \stdClass();
- $compareObjectRevision->revision = 789;
- $reader->expects($this->at(0))
- ->method('find')
- ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(456))
- ->will($this->returnValue($objectRevision));
- $reader->expects($this->at(1))
- ->method('find')
- ->with($this->equalTo('Foo'), $this->equalTo(123), $this->equalTo(789))
- ->will($this->returnValue($compareObjectRevision));
- $this->admin->expects($this->once())
- ->method('setSubject')
- ->with($this->equalTo($objectRevision))
- ->will($this->returnValue(null));
- $fieldDescriptionCollection = new FieldDescriptionCollection();
- $this->admin->expects($this->once())
- ->method('getShow')
- ->will($this->returnValue($fieldDescriptionCollection));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->historyCompareRevisionsAction(123, 456, 789, $this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('show', $this->parameters['action']);
- $this->assertSame($objectRevision, $this->parameters['object']);
- $this->assertSame($compareObjectRevision, $this->parameters['object_compare']);
- $this->assertSame($fieldDescriptionCollection, $this->parameters['elements']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:show_compare.html.twig', $this->template);
- }
- public function testBatchActionWrongMethod()
- {
- $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', 'Invalid request type "GET", POST expected');
- $this->controller->batchAction($this->request);
- }
- public function testBatchActionActionNotDefined()
- {
- $this->setExpectedException('RuntimeException', 'The `foo` batch action is not defined');
- $batchActions = array();
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $this->request->setMethod('POST');
- $this->request->request->set('data', json_encode(array('action' => 'foo', 'idx' => array('123', '456'), 'all_elements' => false)));
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $this->controller->batchAction($this->request);
- }
- public function testBatchActionActionInvalidCsrfToken()
- {
- $this->request->setMethod('POST');
- $this->request->request->set('data', json_encode(array('action' => 'foo', 'idx' => array('123', '456'), 'all_elements' => false)));
- $this->request->request->set('_sonata_csrf_token', 'CSRF-INVALID');
- try {
- $this->controller->batchAction($this->request);
- } catch (HttpException $e) {
- $this->assertSame('The csrf token is not valid, CSRF attack?', $e->getMessage());
- $this->assertSame(400, $e->getStatusCode());
- }
- }
- public function testBatchActionMethodNotExist()
- {
- $this->setExpectedException('RuntimeException', 'A `Sonata\AdminBundle\Controller\CRUDController::batchActionFoo` method must be callable');
- $batchActions = array('foo' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $this->request->setMethod('POST');
- $this->request->request->set('data', json_encode(array('action' => 'foo', 'idx' => array('123', '456'), 'all_elements' => false)));
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $this->controller->batchAction($this->request);
- }
- public function testBatchActionWithoutConfirmation()
- {
- $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
- $datagrid->expects($this->once())
- ->method('getQuery')
- ->will($this->returnValue($query));
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('batchDelete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $modelManager->expects($this->once())
- ->method('addIdentifiersToQuery')
- ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->request->request->set('data', json_encode(array('action' => 'delete', 'idx' => array('123', '456'), 'all_elements' => false)));
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $result = $this->controller->batchAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('list?', $result->getTargetUrl());
- }
- public function testBatchActionWithoutConfirmation2()
- {
- $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
- $datagrid->expects($this->once())
- ->method('getQuery')
- ->will($this->returnValue($query));
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('batchDelete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $modelManager->expects($this->once())
- ->method('addIdentifiersToQuery')
- ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->request->request->set('action', 'delete');
- $this->request->request->set('idx', array('123', '456'));
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $result = $this->controller->batchAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('list?', $result->getTargetUrl());
- }
- public function testBatchActionWithConfirmation()
- {
- $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => true));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $data = array('action' => 'delete', 'idx' => array('123', '456'), 'all_elements' => false);
- $this->request->setMethod('POST');
- $this->request->request->set('data', json_encode($data));
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $form = $this->getMockBuilder('Symfony\Component\Form\Form')
- ->disableOriginalConstructor()
- ->getMock();
- $form->expects($this->once())
- ->method('createView')
- ->will($this->returnValue($this->getMock('Symfony\Component\Form\FormView')));
- $datagrid->expects($this->once())
- ->method('getForm')
- ->will($this->returnValue($form));
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->batchAction($this->request));
- $this->assertSame($this->admin, $this->parameters['admin']);
- $this->assertSame('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
- $this->assertSame($this->pool, $this->parameters['admin_pool']);
- $this->assertSame('list', $this->parameters['action']);
- $this->assertSame($datagrid, $this->parameters['datagrid']);
- $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
- $this->assertSame($data, $this->parameters['data']);
- $this->assertSame('csrf-token-123_sonata.batch', $this->parameters['csrf_token']);
- $this->assertSame('Foo Bar', $this->parameters['action_label']);
- $this->assertSame(array(), $this->session->getFlashBag()->all());
- $this->assertSame('SonataAdminBundle:CRUD:batch_confirmation.html.twig', $this->template);
- }
- public function testBatchActionNonRelevantAction()
- {
- $controller = new BatchAdminController();
- $controller->setContainer($this->container);
- $batchActions = array('foo' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $this->request->setMethod('POST');
- $this->request->request->set('action', 'foo');
- $this->request->request->set('idx', array('789'));
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $result = $controller->batchAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
- $this->assertSame('list?', $result->getTargetUrl());
- }
- public function testBatchActionNonRelevantAction2()
- {
- $controller = new BatchAdminController();
- $controller->setContainer($this->container);
- $batchActions = array('foo' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $this->request->setMethod('POST');
- $this->request->request->set('action', 'foo');
- $this->request->request->set('idx', array('999'));
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $result = $controller->batchAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_foo_error'), $this->session->getFlashBag()->get('sonata_flash_info'));
- $this->assertSame('list?', $result->getTargetUrl());
- }
- public function testBatchActionNoItems()
- {
- $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => true));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $this->request->setMethod('POST');
- $this->request->request->set('action', 'delete');
- $this->request->request->set('idx', array());
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $result = $this->controller->batchAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_batch_empty'), $this->session->getFlashBag()->get('sonata_flash_info'));
- $this->assertSame('list?', $result->getTargetUrl());
- }
- public function testBatchActionNoItemsEmptyQuery()
- {
- $controller = new BatchAdminController();
- $controller->setContainer($this->container);
- $batchActions = array('bar' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
- $datagrid->expects($this->once())
- ->method('getQuery')
- ->will($this->returnValue($query));
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->admin->expects($this->any())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $this->request->setMethod('POST');
- $this->request->request->set('action', 'bar');
- $this->request->request->set('idx', array());
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $result = $controller->batchAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $result);
- $this->assertSame('batchActionBar executed', $result->getContent());
- }
- public function testBatchActionWithRequesData()
- {
- $batchActions = array('delete' => array('label' => 'Foo Bar', 'ask_confirmation' => false));
- $this->admin->expects($this->once())
- ->method('getBatchActions')
- ->will($this->returnValue($batchActions));
- $datagrid = $this->getMock('\Sonata\AdminBundle\Datagrid\DatagridInterface');
- $query = $this->getMock('\Sonata\AdminBundle\Datagrid\ProxyQueryInterface');
- $datagrid->expects($this->once())
- ->method('getQuery')
- ->will($this->returnValue($query));
- $this->admin->expects($this->once())
- ->method('getDatagrid')
- ->will($this->returnValue($datagrid));
- $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->admin->expects($this->once())
- ->method('checkAccess')
- ->with($this->equalTo('batchDelete'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Foo'));
- $modelManager->expects($this->once())
- ->method('addIdentifiersToQuery')
- ->with($this->equalTo('Foo'), $this->equalTo($query), $this->equalTo(array('123', '456')))
- ->will($this->returnValue(true));
- $this->request->setMethod('POST');
- $this->request->request->set('data', json_encode(array('action' => 'delete', 'idx' => array('123', '456'), 'all_elements' => false)));
- $this->request->request->set('foo', 'bar');
- $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');
- $result = $this->controller->batchAction($this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $result);
- $this->assertSame(array('flash_batch_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
- $this->assertSame('list?', $result->getTargetUrl());
- $this->assertSame('bar', $this->request->request->get('foo'));
- }
- public function getCsrfProvider()
- {
- return $this->csrfProvider;
- }
- public function getToStringValues()
- {
- return array(
- array('', ''),
- array('Foo', 'Foo'),
- array('<a href="http://foo">Bar</a>', '<a href="http://foo">Bar</a>'),
- array('<>&"'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/', '<>&"\'abcdefghijklmnopqrstuvwxyz*-+.,?_()[]\/'),
- );
- }
- private function expectTranslate($id, array $parameters = array(), $domain = null, $locale = null)
- {
- $this->admin->expects($this->once())
- ->method('trans')
- ->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale))
- ->will($this->returnValue($id));
- }
- }
|