12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716 |
- <?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 Exporter\Exporter;
- use Exporter\Writer\JsonWriter;
- 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\RequestStack;
- 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;
- use Symfony\Component\Security\Csrf\CsrfToken;
- /**
- * 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;
- /**
- * @var TranslatorInterface
- */
- private $translator;
- /**
- * {@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->translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
- $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;
- $translator = $this->translator;
- $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 'Symfony\Bridge\Twig\Extension\FormExtension':
- return $formExtension;
- }
- }));
- // NEXT_MAJOR : require sonata/exporter ^1.7 and remove conditional
- if (class_exists('Exporter\Exporter')) {
- $exporter = new Exporter(array(new JsonWriter('/tmp/sonataadmin/export.json')));
- } else {
- $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;
- // Prefer Symfony 2.x interfaces
- if (interface_exists('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface')) {
- $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;
- }));
- } else {
- $this->csrfProvider = $this->getMockBuilder(
- 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'
- )
- ->getMock();
- $this->csrfProvider->expects($this->any())
- ->method('getToken')
- ->will($this->returnCallback(function ($intention) {
- return new CsrfToken($intention, 'csrf-token-123_'.$intention);
- }));
- $this->csrfProvider->expects($this->any())
- ->method('isTokenValid')
- ->will($this->returnCallback(function (CsrfToken $token) {
- if ($token->getValue() == 'csrf-token-123_'.$token->getId()) {
- 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 (class_exists('Symfony\Component\HttpFoundation\RequestStack')) {
- $requestStack = new 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,
- $translator
- ) {
- 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':
- case 'security.csrf.token_manager':
- return $csrfProvider;
- case 'logger':
- return $logger;
- case 'kernel':
- return $kernel;
- case 'translator':
- return $translator;
- }
- }));
- // php 5.3
- $tthis = $this;
- $this->container->expects($this->any())
- ->method('has')
- ->will($this->returnCallback(function ($id) use ($tthis) {
- if ($id == 'form.csrf_provider' && Kernel::MAJOR_VERSION == 2 && $tthis->getCsrfProvider() !== null) {
- return true;
- }
- if ($id == 'security.csrf.token_manager' && Kernel::MAJOR_VERSION >= 3 && $tthis->getCsrfProvider() !== null) {
- return true;
- }
- if ($id == 'logger') {
- return true;
- }
- if ($id == 'session') {
- return true;
- }
- if ($id == 'templating') {
- return true;
- }
- if ($id == 'translator') {
- 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'));
- }
- }));
- $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';
- }
- }));
- $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->any())
- ->method('hasRoute')
- ->with($this->equalTo('list'))
- ->will($this->returnValue(true));
- $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->any())
- ->method('hasRoute')
- ->with($this->equalTo('list'))
- ->will($this->returnValue(true));
- $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());
- }
- 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, $route, $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);
- }
- $this->admin->expects($this->any())
- ->method('hasRoute')
- ->with($this->equalTo($route))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('isGranted')
- ->with($this->equalTo(strtoupper($route)))
- ->will($this->returnValue(true));
- $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame($expected, $response->getTargetUrl());
- }
- public function testRedirectToWithObject()
- {
- $this->admin->expects($this->any())
- ->method('hasActiveSubclass')
- ->will($this->returnValue(false));
- $object = new \stdClass();
- $this->admin->expects($this->at(0))
- ->method('hasRoute')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('isGranted')
- ->with($this->equalTo(strtoupper('edit')), $object)
- ->will($this->returnValue(false));
- $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object, $this->request);
- $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
- $this->assertSame('list', $response->getTargetUrl());
- }
- public function getRedirectToTests()
- {
- return array(
- array('stdClass_edit', 'edit', array(), false),
- array('list', 'list', array('btn_update_and_list' => true), false),
- array('list', 'list', array('btn_create_and_list' => true), false),
- array('create', 'create', array('btn_create_and_create' => true), false),
- array('create?subclass=foo', 'create', 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'));
- $this->admin->expects($this->once())
- ->method('hasRoute')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('isGranted')
- ->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 == 'edit') {
- return true;
- }
- if ($name != 'create') {
- return false;
- }
- if ($objectIn === null) {
- return true;
- }
- return $objectIn === $object;
- }));
- $this->admin->expects($this->once())
- ->method('hasRoute')
- ->with($this->equalTo('edit'))
- ->will($this->returnValue(true));
- $this->admin->expects($this->once())
- ->method('isGranted')
- ->with($this->equalTo('EDIT'))
- ->will($this->returnValue(true));
- $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);
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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());
- }
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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);
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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());
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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());
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- public function testBatchActionWithConfirmation()
- {
- $batchActions = array('delete' => array('label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', '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);
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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());
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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());
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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());
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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());
- }
- /**
- * NEXT_MAJOR: Remove this legacy group.
- *
- * @group legacy
- */
- 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 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,
- ));
- }
- private function expectTranslate($id, array $parameters = array(), $domain = null, $locale = null)
- {
- $this->translator->expects($this->once())
- ->method('trans')
- ->with($this->equalTo($id), $this->equalTo($parameters), $this->equalTo($domain), $this->equalTo($locale))
- ->will($this->returnValue($id));
- }
- }
|