123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160 |
- <?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\Twig\Extension;
- use Psr\Log\LoggerInterface;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Sonata\AdminBundle\Admin\Pool;
- use Sonata\AdminBundle\Exception\NoValueException;
- use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
- use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
- use Symfony\Bridge\Twig\Extension\RoutingExtension;
- use Symfony\Bridge\Twig\Extension\TranslationExtension;
- use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
- use Symfony\Component\Config\FileLocator;
- use Symfony\Component\Routing\Generator\UrlGenerator;
- use Symfony\Component\Routing\Loader\XmlFileLoader;
- use Symfony\Component\Routing\RequestContext;
- use Symfony\Component\Translation\Loader\XliffFileLoader;
- use Symfony\Component\Translation\MessageSelector;
- use Symfony\Component\Translation\Translator;
- /**
- * Test for SonataAdminExtension.
- *
- * @author Andrej Hudec <pulzarraider@gmail.com>
- */
- class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var SonataAdminExtension
- */
- private $twigExtension;
- /**
- * @var \Twig_Environment
- */
- private $environment;
- /**
- * @var AdminInterface
- */
- private $admin;
- /**
- * @var AdminInterface
- */
- private $adminBar;
- /**
- * @var FieldDescriptionInterface
- */
- private $fieldDescription;
- /**
- * @var \stdClass
- */
- private $object;
- /**
- * @var Pool
- */
- private $pool;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @var string[]
- */
- private $xEditableTypeMapping;
- public function setUp()
- {
- date_default_timezone_set('Europe/London');
- $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $this->pool = new Pool($container, '', '');
- $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
- $this->pool->setAdminClasses(array('fooClass' => array('sonata_admin_foo_service')));
- $this->logger = $this->getMock('Psr\Log\LoggerInterface');
- $this->xEditableTypeMapping = array(
- 'choice' => 'select',
- 'boolean' => 'select',
- 'text' => 'text',
- 'textarea' => 'textarea',
- 'html' => 'textarea',
- 'email' => 'email',
- 'string' => 'text',
- 'smallint' => 'text',
- 'bigint' => 'text',
- 'integer' => 'number',
- 'decimal' => 'number',
- 'currency' => 'number',
- 'percent' => 'number',
- 'url' => 'url',
- );
- $this->twigExtension = new SonataAdminExtension($this->pool, $this->logger);
- $this->twigExtension->setXEditableTypeMapping($this->xEditableTypeMapping);
- $loader = new StubFilesystemLoader(array(
- __DIR__.'/../../../Resources/views/CRUD',
- ));
- $this->environment = new \Twig_Environment($loader, array(
- 'strict_variables' => true,
- 'cache' => false,
- 'autoescape' => 'html',
- 'optimizations' => 0,
- ));
- $this->environment->addExtension($this->twigExtension);
- // translation extension
- $translator = new Translator('en', new MessageSelector());
- $translator->addLoader('xlf', new XliffFileLoader());
- $translator->addResource(
- 'xlf',
- __DIR__.'/../../../Resources/translations/SonataAdminBundle.en.xliff',
- 'en',
- 'SonataAdminBundle'
- );
- $this->environment->addExtension(new TranslationExtension($translator));
- // routing extension
- $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../../Resources/config/routing')));
- $routeCollection = $xmlFileLoader->load('sonata_admin.xml');
- $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../Fixtures/Resources/config/routing')));
- $testRouteCollection = $xmlFileLoader->load('routing.xml');
- $routeCollection->addCollection($testRouteCollection);
- $requestContext = new RequestContext();
- $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
- $this->environment->addExtension(new RoutingExtension($urlGenerator));
- $this->environment->addExtension(new \Twig_Extensions_Extension_Text());
- // initialize object
- $this->object = new \stdClass();
- // initialize admin
- $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $this->admin->expects($this->any())
- ->method('getCode')
- ->will($this->returnValue('xyz'));
- $this->admin->expects($this->any())
- ->method('id')
- ->with($this->equalTo($this->object))
- ->will($this->returnValue(12345));
- $this->admin->expects($this->any())
- ->method('getNormalizedIdentifier')
- ->with($this->equalTo($this->object))
- ->will($this->returnValue(12345));
- $this->admin->expects($this->any())
- ->method('trans')
- ->will($this->returnCallback(function ($id, $parameters = array(), $domain = null) use ($translator) {
- return $translator->trans($id, $parameters, $domain);
- }));
- $this->adminBar = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $this->adminBar->expects($this->any())
- ->method('isGranted')
- ->will($this->returnValue(true));
- $this->adminBar->expects($this->any())
- ->method('getNormalizedIdentifier')
- ->with($this->equalTo($this->object))
- ->will($this->returnValue(12345));
- // for php5.3 BC
- $admin = $this->admin;
- $adminBar = $this->adminBar;
- $container->expects($this->any())
- ->method('get')
- ->will($this->returnCallback(function ($id) use ($admin, $adminBar) {
- if ($id == 'sonata_admin_foo_service') {
- return $admin;
- } elseif ($id == 'sonata_admin_bar_service') {
- return $adminBar;
- }
- return;
- }));
- // initialize field description
- $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $this->fieldDescription->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('fd_name'));
- $this->fieldDescription->expects($this->any())
- ->method('getAdmin')
- ->will($this->returnValue($this->admin));
- $this->fieldDescription->expects($this->any())
- ->method('getLabel')
- ->will($this->returnValue('Data'));
- }
- /**
- * @dataProvider getRenderListElementTests
- */
- public function testRenderListElement($expected, $type, $value, array $options)
- {
- $this->admin->expects($this->any())
- ->method('isGranted')
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getTemplate')
- ->with($this->equalTo('base_list_field'))
- ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
- $this->fieldDescription->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue($value));
- $this->fieldDescription->expects($this->any())
- ->method('getType')
- ->will($this->returnValue($type));
- $this->fieldDescription->expects($this->any())
- ->method('getOptions')
- ->will($this->returnValue($options));
- $this->fieldDescription->expects($this->any())
- ->method('getOption')
- ->will($this->returnCallback(function ($name, $default = null) use ($options) {
- return isset($options[$name]) ? $options[$name] : $default;
- }));
- $this->fieldDescription->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnCallback(function () use ($type) {
- switch ($type) {
- case 'string':
- return 'SonataAdminBundle:CRUD:list_string.html.twig';
- case 'boolean':
- return 'SonataAdminBundle:CRUD:list_boolean.html.twig';
- case 'datetime':
- return 'SonataAdminBundle:CRUD:list_datetime.html.twig';
- case 'date':
- return 'SonataAdminBundle:CRUD:list_date.html.twig';
- case 'time':
- return 'SonataAdminBundle:CRUD:list_time.html.twig';
- case 'currency':
- return 'SonataAdminBundle:CRUD:list_currency.html.twig';
- case 'percent':
- return 'SonataAdminBundle:CRUD:list_percent.html.twig';
- case 'email':
- return 'SonataAdminBundle:CRUD:list_email.html.twig';
- case 'choice':
- return 'SonataAdminBundle:CRUD:list_choice.html.twig';
- case 'array':
- return 'SonataAdminBundle:CRUD:list_array.html.twig';
- case 'trans':
- return 'SonataAdminBundle:CRUD:list_trans.html.twig';
- case 'url':
- return 'SonataAdminBundle:CRUD:list_url.html.twig';
- case 'html':
- return 'SonataAdminBundle:CRUD:list_html.html.twig';
- case 'nonexistent':
- // template doesn`t exist
- return 'SonataAdminBundle:CRUD:list_nonexistent_template.html.twig';
- default:
- return false;
- }
- }));
- $this->assertSame(
- $this->removeExtraWhitespace($expected),
- $this->removeExtraWhitespace($this->twigExtension->renderListElement(
- $this->environment,
- $this->object,
- $this->fieldDescription
- ))
- );
- }
- /**
- * @dataProvider getDeprecatedRenderListElementTests
- * @group legacy
- */
- public function testDeprecatedRenderListElement($expected, $value, array $options)
- {
- $this->admin->expects($this->any())
- ->method('isGranted')
- ->will($this->returnValue(true));
- $this->admin->expects($this->any())
- ->method('getTemplate')
- ->with($this->equalTo('base_list_field'))
- ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
- $this->fieldDescription->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue($value));
- $this->fieldDescription->expects($this->any())
- ->method('getType')
- ->will($this->returnValue('nonexistent'));
- $this->fieldDescription->expects($this->any())
- ->method('getOptions')
- ->will($this->returnValue($options));
- $this->fieldDescription->expects($this->any())
- ->method('getOption')
- ->will($this->returnCallback(function ($name, $default = null) use ($options) {
- return isset($options[$name]) ? $options[$name] : $default;
- }));
- $this->fieldDescription->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
- $this->assertSame(
- $this->removeExtraWhitespace($expected),
- $this->removeExtraWhitespace($this->twigExtension->renderListElement(
- $this->environment,
- $this->object,
- $this->fieldDescription
- ))
- );
- }
- public function getDeprecatedRenderListElementTests()
- {
- return array(
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> Example </td>',
- 'Example',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> </td>',
- null,
- array(),
- ),
- );
- }
- public function getRenderListElementTests()
- {
- return array(
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>',
- 'string',
- 'Example',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> </td>',
- 'string',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>',
- 'text',
- 'Example',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> </td>',
- 'text',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>',
- 'textarea',
- 'Example',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>',
- 'textarea',
- null,
- array(),
- ),
- 'datetime field' => array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
- December 24, 2013 10:11
- </td>',
- 'datetime',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
- December 24, 2013 18:11
- </td>',
- 'datetime',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('UTC')),
- array('timezone' => 'Asia/Hong_Kong'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> </td>',
- 'datetime',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
- 24.12.2013 10:11:12
- </td>',
- 'datetime',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array('format' => 'd.m.Y H:i:s'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> </td>',
- 'datetime',
- null,
- array('format' => 'd.m.Y H:i:s'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
- 24.12.2013 18:11:12
- </td>',
- 'datetime',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('UTC')),
- array('format' => 'd.m.Y H:i:s', 'timezone' => 'Asia/Hong_Kong'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> </td>',
- 'datetime',
- null,
- array('format' => 'd.m.Y H:i:s', 'timezone' => 'Asia/Hong_Kong'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> December 24, 2013 </td>',
- 'date',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> </td>',
- 'date',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> 24.12.2013 </td>',
- 'date',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array('format' => 'd.m.Y'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> </td>',
- 'date',
- null,
- array('format' => 'd.m.Y'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> 10:11:12 </td>',
- 'time',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> </td>',
- 'time',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>',
- 'number', 10.746135,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> </td>',
- 'number',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>',
- 'integer',
- 5678,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> </td>',
- 'integer',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>',
- 'percent',
- 10.746135,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 0 % </td>',
- 'percent',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>',
- 'currency',
- 10.746135,
- array('currency' => 'EUR'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>',
- 'currency',
- null,
- array('currency' => 'EUR'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> GBP 51.23456 </td>',
- 'currency',
- 51.23456,
- array('currency' => 'GBP'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>',
- 'currency',
- null,
- array('currency' => 'GBP'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> </td>',
- 'email',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> <a href="mailto:admin@admin.com">admin@admin.com</a> </td>',
- 'email',
- 'admin@admin.com',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
- <a href="mailto:admin@admin.com">admin@admin.com</a> </td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => false),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
- <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme', 'body' => 'Message Body')).'">admin@admin.com</a> </td>',
- 'email',
- 'admin@admin.com',
- array('subject' => 'Main Theme', 'body' => 'Message Body'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
- <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme')).'">admin@admin.com</a> </td>',
- 'email',
- 'admin@admin.com',
- array('subject' => 'Main Theme'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
- <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('body' => 'Message Body')).'">admin@admin.com</a> </td>',
- 'email',
- 'admin@admin.com',
- array('body' => 'Message Body'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true, 'subject' => 'Main Theme', 'body' => 'Message Body'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true, 'body' => 'Message Body'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true, 'subject' => 'Main Theme'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345">
- [1 => First] [2 => Second]
- </td>',
- 'array',
- array(1 => 'First', 2 => 'Second'),
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> </td>',
- 'array',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
- <span class="label label-success">yes</span>
- </td>',
- 'boolean',
- true,
- array('editable' => false),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
- <span class="label label-danger">no</span>
- </td>',
- 'boolean',
- false,
- array('editable' => false),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
- <span class="label label-danger">no</span>
- </td>',
- 'boolean',
- null,
- array('editable' => false),
- ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value="1"
- data-title="Data"
- data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[{value: 0, text: 'no'},{value: 1, text: 'yes'}]"
- >
- <span class="label label-success">yes</span>
- </span>
- </td>
- EOT
- ,
- 'boolean',
- true,
- array('editable' => true),
- ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value=""
- data-title="Data"
- data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[{value: 0, text: 'no'},{value: 1, text: 'yes'}]"
- >
- <span class="label label-danger">no</span> </span>
- </td>
- EOT
- ,
- 'boolean',
- false,
- array('editable' => true),
- ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value=""
- data-title="Data"
- data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[{value: 0, text: 'no'},{value: 1, text: 'yes'}]" >
- <span class="label label-danger">no</span> </span>
- </td>
- EOT
- ,
- 'boolean',
- null,
- array('editable' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>',
- 'trans',
- 'action_delete',
- array('catalogue' => 'SonataAdminBundle'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> </td>',
- 'trans',
- null,
- array('catalogue' => 'SonataAdminBundle'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>',
- 'trans',
- 'action_delete',
- array('format' => '%s', 'catalogue' => 'SonataAdminBundle'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345">
- action.action_delete
- </td>',
- 'trans',
- 'action_delete',
- array('format' => 'action.%s'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345">
- action.action_delete
- </td>',
- 'trans',
- 'action_delete',
- array('format' => 'action.%s', 'catalogue' => 'SonataAdminBundle'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>',
- 'choice',
- 'Status1',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>',
- 'choice',
- array('Status1'),
- array('choices' => array(), 'multiple' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Alias1 </td>',
- 'choice',
- 'Status1',
- array('choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3')),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> </td>',
- 'choice',
- null,
- array('choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3')),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- NoValidKeyInChoices
- </td>',
- 'choice',
- 'NoValidKeyInChoices',
- array('choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3')),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Delete </td>',
- 'choice',
- 'Foo',
- array('catalogue' => 'SonataAdminBundle', 'choices' => array(
- 'Foo' => 'action_delete',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- )),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Alias1, Alias3 </td>',
- 'choice',
- array('Status1', 'Status3'),
- array('choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true), ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Alias1 | Alias3 </td>',
- 'choice',
- array('Status1', 'Status3'),
- array('choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true, 'delimiter' => ' | '), ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> </td>',
- 'choice',
- null,
- array('choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- NoValidKeyInChoices
- </td>',
- 'choice',
- array('NoValidKeyInChoices'),
- array('choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- NoValidKeyInChoices, Alias2
- </td>',
- 'choice',
- array('NoValidKeyInChoices', 'Status2'),
- array('choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Delete, Alias3 </td>',
- 'choice',
- array('Foo', 'Status3'),
- array('catalogue' => 'SonataAdminBundle', 'choices' => array(
- 'Foo' => 'action_delete',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- <b>Alias1</b>, <b>Alias3</b>
- </td>',
- 'choice',
- array('Status1', 'Status3'),
- array('choices' => array(
- 'Status1' => '<b>Alias1</b>',
- 'Status2' => '<b>Alias2</b>',
- 'Status3' => '<b>Alias3</b>',
- ), 'multiple' => true), ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value="Status1"
- data-title="Data"
- data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[]"
- >
- Status1
- </span>
- </td>
- EOT
- ,
- 'choice',
- 'Status1',
- array('editable' => true),
- ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value="Status1"
- data-title="Data"
- data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[{"value":"Status1","text":"Alias1"},{"value":"Status2","text":"Alias2"},{"value":"Status3","text":"Alias3"}]" >
- Alias1 </span>
- </td>
- EOT
- ,
- 'choice',
- 'Status1',
- array(
- 'editable' => true,
- 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ),
- ),
- ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value=""
- data-title="Data"
- data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[{"value":"Status1","text":"Alias1"},{"value":"Status2","text":"Alias2"},{"value":"Status3","text":"Alias3"}]" >
- </span>
- </td>
- EOT
- ,
- 'choice',
- null,
- array(
- 'editable' => true,
- 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ),
- ),
- ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value="NoValidKeyInChoices"
- data-title="Data" data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[{"value":"Status1","text":"Alias1"},{"value":"Status2","text":"Alias2"},{"value":"Status3","text":"Alias3"}]" >
- NoValidKeyInChoices
- </span>
- </td>
- EOT
- ,
- 'choice',
- 'NoValidKeyInChoices',
- array(
- 'editable' => true,
- 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ),
- ),
- ),
- array(
- <<<'EOT'
- <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
- <span
- class="x-editable"
- data-type="select"
- data-value="Foo"
- data-title="Data"
- data-pk="12345"
- data-url="/core/set-object-field-value?context=list&field=fd_name&objectId=12345&code=xyz"
- data-source="[{"value":"Foo","text":"Delete"},{"value":"Status2","text":"Alias2"},{"value":"Status3","text":"Alias3"}]" >
- Delete
- </span>
- </td>
- EOT
- ,
- 'choice',
- 'Foo',
- array(
- 'editable' => true,
- 'catalogue' => 'SonataAdminBundle',
- 'choices' => array(
- 'Foo' => 'action_delete',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ),
- ),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> </td>',
- 'url',
- null,
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> </td>',
- 'url',
- null,
- array('url' => 'http://example.com'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> </td>',
- 'url',
- null,
- array('route' => array('name' => 'sonata_admin_foo')),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://example.com">http://example.com</a>
- </td>',
- 'url',
- 'http://example.com',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="https://example.com">https://example.com</a>
- </td>',
- 'url',
- 'https://example.com',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://example.com">example.com</a>
- </td>',
- 'url',
- 'http://example.com',
- array('hide_protocol' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="https://example.com">example.com</a>
- </td>',
- 'url',
- 'https://example.com',
- array('hide_protocol' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://example.com">http://example.com</a>
- </td>',
- 'url',
- 'http://example.com',
- array('hide_protocol' => false),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="https://example.com">https://example.com</a>
- </td>',
- 'url',
- 'https://example.com',
- array('hide_protocol' => false),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://example.com">Foo</a>
- </td>',
- 'url',
- 'Foo',
- array('url' => 'http://example.com'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://example.com"><b>Foo</b></a>
- </td>',
- 'url',
- '<b>Foo</b>',
- array('url' => 'http://example.com'),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="/foo">Foo</a>
- </td>',
- 'url',
- 'Foo',
- array('route' => array('name' => 'sonata_admin_foo')),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://localhost/foo">Foo</a>
- </td>',
- 'url',
- 'Foo',
- array('route' => array('name' => 'sonata_admin_foo', 'absolute' => true)),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="/foo">foo/bar?a=b&c=123456789</a>
- </td>',
- 'url',
- 'http://foo/bar?a=b&c=123456789',
- array('route' => array('name' => 'sonata_admin_foo'),
- 'hide_protocol' => true, ),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://localhost/foo">foo/bar?a=b&c=123456789</a>
- </td>',
- 'url',
- 'http://foo/bar?a=b&c=123456789',
- array(
- 'route' => array('name' => 'sonata_admin_foo', 'absolute' => true),
- 'hide_protocol' => true,
- ),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="/foo/abcd/efgh?param3=ijkl">Foo</a>
- </td>',
- 'url',
- 'Foo',
- array(
- 'route' => array('name' => 'sonata_admin_foo_param',
- 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'), ),
- ),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://localhost/foo/abcd/efgh?param3=ijkl">Foo</a>
- </td>',
- 'url',
- 'Foo',
- array(
- 'route' => array('name' => 'sonata_admin_foo_param',
- 'absolute' => true,
- 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'), ),
- ),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a>
- </td>',
- 'url',
- 'Foo',
- array(
- 'route' => array('name' => 'sonata_admin_foo_object',
- 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'),
- 'identifier_parameter_name' => 'barId', ),
- ),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
- <a href="http://localhost/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a>
- </td>',
- 'url',
- 'Foo',
- array(
- 'route' => array('name' => 'sonata_admin_foo_object',
- 'absolute' => true,
- 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'),
- 'identifier_parameter_name' => 'barId', ),
- ),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
- <p><strong>Creating a Template for the Field</strong> and form</p>
- </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array(),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
- Creating a Template for the Field and form
- </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('strip' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
- Creating a Template for the Fi...
- </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => true),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a... </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => array('length' => 10)),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
- Creating a Template for the Field...
- </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => array('preserve' => true)),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
- Creating a Template for the Fi etc.
- </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => array('separator' => ' etc.')),
- ),
- array(
- '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
- Creating a Template for[...]
- </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array(
- 'truncate' => array(
- 'length' => 20,
- 'preserve' => true,
- 'separator' => '[...]',
- ),
- ),
- ),
- );
- }
- /**
- * @group legacy
- */
- public function testRenderListElementNonExistentTemplate()
- {
- $this->admin->expects($this->once())
- ->method('getTemplate')
- ->with($this->equalTo('base_list_field'))
- ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
- $this->fieldDescription->expects($this->once())
- ->method('getValue')
- ->will($this->returnValue('Foo'));
- $this->fieldDescription->expects($this->once())
- ->method('getFieldName')
- ->will($this->returnValue('Foo_name'));
- $this->fieldDescription->expects($this->exactly(2))
- ->method('getType')
- ->will($this->returnValue('nonexistent'));
- $this->fieldDescription->expects($this->once())
- ->method('getTemplate')
- ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
- $this->logger->expects($this->once())
- ->method('warning')
- ->with(($this->stringStartsWith($this->removeExtraWhitespace(
- 'An error occured trying to load the template
- "SonataAdminBundle:CRUD:list_nonexistent_template.html.twig"
- for the field "Foo_name", the default template
- "SonataAdminBundle:CRUD:base_list_field.html.twig" was used
- instead.'
- ))));
- $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription);
- }
- /**
- * @expectedException Twig_Error_Loader
- * @expectedExceptionMessage Unable to find template "base_list_nonexistent_field.html.twig"
- * @group legacy
- */
- public function testRenderListElementErrorLoadingTemplate()
- {
- $this->admin->expects($this->once())
- ->method('getTemplate')
- ->with($this->equalTo('base_list_field'))
- ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_nonexistent_field.html.twig'));
- $this->fieldDescription->expects($this->once())
- ->method('getTemplate')
- ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
- $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription);
- }
- /**
- * @dataProvider getRenderViewElementTests
- */
- public function testRenderViewElement($expected, $type, $value, array $options)
- {
- $this->admin->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
- $this->fieldDescription->expects($this->any())
- ->method('getValue')
- ->will($this->returnCallback(function () use ($value) {
- if ($value instanceof NoValueException) {
- throw $value;
- }
- return $value;
- }));
- $this->fieldDescription->expects($this->any())
- ->method('getType')
- ->will($this->returnValue($type));
- $this->fieldDescription->expects($this->any())
- ->method('getOptions')
- ->will($this->returnValue($options));
- $this->fieldDescription->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnCallback(function () use ($type) {
- switch ($type) {
- case 'boolean':
- return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
- case 'datetime':
- return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
- case 'date':
- return 'SonataAdminBundle:CRUD:show_date.html.twig';
- case 'time':
- return 'SonataAdminBundle:CRUD:show_time.html.twig';
- case 'currency':
- return 'SonataAdminBundle:CRUD:show_currency.html.twig';
- case 'percent':
- return 'SonataAdminBundle:CRUD:show_percent.html.twig';
- case 'email':
- return 'SonataAdminBundle:CRUD:show_email.html.twig';
- case 'choice':
- return 'SonataAdminBundle:CRUD:show_choice.html.twig';
- case 'array':
- return 'SonataAdminBundle:CRUD:show_array.html.twig';
- case 'trans':
- return 'SonataAdminBundle:CRUD:show_trans.html.twig';
- case 'url':
- return 'SonataAdminBundle:CRUD:show_url.html.twig';
- case 'html':
- return 'SonataAdminBundle:CRUD:show_html.html.twig';
- default:
- return false;
- }
- }));
- $this->assertSame($expected, trim(preg_replace(
- '/\s+/',
- ' ',
- $this->twigExtension->renderViewElement(
- $this->environment,
- $this->fieldDescription,
- $this->object
- )
- )));
- }
- public function getRenderViewElementTests()
- {
- return array(
- array('<th>Data</th> <td>Example</td>', 'string', 'Example', array('safe' => false)),
- array('<th>Data</th> <td>Example</td>', 'text', 'Example', array('safe' => false)),
- array('<th>Data</th> <td>Example</td>', 'textarea', 'Example', array('safe' => false)),
- array(
- '<th>Data</th> <td>December 24, 2013 10:11</td>',
- 'datetime',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array(),
- ),
- array(
- '<th>Data</th> <td>24.12.2013 10:11:12</td>',
- 'datetime',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array('format' => 'd.m.Y H:i:s'),
- ),
- array(
- '<th>Data</th> <td>December 24, 2013</td>',
- 'date',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array(),
- ),
- array(
- '<th>Data</th> <td>24.12.2013</td>',
- 'date',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array('format' => 'd.m.Y'),
- ),
- array(
- '<th>Data</th> <td>10:11:12</td>',
- 'time',
- new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
- array(),
- ),
- array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135, array('safe' => false)),
- array('<th>Data</th> <td>5678</td>', 'integer', 5678, array('safe' => false)),
- array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135, array()),
- array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
- array('<th>Data</th> <td> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
- array(
- '<th>Data</th> <td> [1 => First] <br> [2 => Second] </td>',
- 'array',
- array(1 => 'First', 2 => 'Second'),
- array('safe' => false),
- ),
- array(
- '<th>Data</th> <td> [1 => First] [2 => Second] </td>',
- 'array',
- array(1 => 'First', 2 => 'Second'),
- array('safe' => false, 'inline' => true),
- ),
- array(
- '<th>Data</th> <td><span class="label label-success">yes</span></td>',
- 'boolean',
- true,
- array(),
- ),
- array('<th>Data</th> <td><span class="label label-danger">no</span></td>', 'boolean', false, array()),
- array(
- '<th>Data</th> <td> Delete </td>',
- 'trans',
- 'action_delete',
- array('safe' => false, 'catalogue' => 'SonataAdminBundle'),
- ),
- array('<th>Data</th> <td>Status1</td>', 'choice', 'Status1', array('safe' => false)),
- array(
- '<th>Data</th> <td>Alias1</td>',
- 'choice',
- 'Status1',
- array('safe' => false, 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- )),
- ),
- array(
- '<th>Data</th> <td>NoValidKeyInChoices</td>',
- 'choice',
- 'NoValidKeyInChoices',
- array('safe' => false, 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- )),
- ),
- array(
- '<th>Data</th> <td>Delete</td>',
- 'choice',
- 'Foo',
- array('safe' => false, 'catalogue' => 'SonataAdminBundle', 'choices' => array(
- 'Foo' => 'action_delete',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- )),
- ),
- array(
- '<th>Data</th> <td>NoValidKeyInChoices</td>',
- 'choice',
- array('NoValidKeyInChoices'),
- array('safe' => false, 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<th>Data</th> <td>NoValidKeyInChoices, Alias2</td>',
- 'choice',
- array('NoValidKeyInChoices', 'Status2'),
- array('safe' => false, 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<th>Data</th> <td>Alias1, Alias3</td>',
- 'choice',
- array('Status1', 'Status3'),
- array('safe' => false, 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<th>Data</th> <td>Alias1 | Alias3</td>',
- 'choice',
- array('Status1', 'Status3'), array('safe' => false, 'choices' => array(
- 'Status1' => 'Alias1',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true, 'delimiter' => ' | '),
- ),
- array(
- '<th>Data</th> <td>Delete, Alias3</td>',
- 'choice',
- array('Foo', 'Status3'),
- array('safe' => false, 'catalogue' => 'SonataAdminBundle', 'choices' => array(
- 'Foo' => 'action_delete',
- 'Status2' => 'Alias2',
- 'Status3' => 'Alias3',
- ), 'multiple' => true),
- ),
- array(
- '<th>Data</th> <td><b>Alias1</b>, <b>Alias3</b></td>',
- 'choice',
- array('Status1', 'Status3'),
- array('safe' => true, 'choices' => array(
- 'Status1' => '<b>Alias1</b>',
- 'Status2' => '<b>Alias2</b>',
- 'Status3' => '<b>Alias3</b>',
- ), 'multiple' => true),
- ),
- array(
- '<th>Data</th> <td><b>Alias1</b>, <b>Alias3</b></td>',
- 'choice',
- array('Status1', 'Status3'),
- array('safe' => false, 'choices' => array(
- 'Status1' => '<b>Alias1</b>',
- 'Status2' => '<b>Alias2</b>',
- 'Status3' => '<b>Alias3</b>',
- ), 'multiple' => true),
- ),
- array(
- '<th>Data</th> <td><a href="http://example.com">http://example.com</a></td>',
- 'url',
- 'http://example.com',
- array('safe' => false),
- ),
- array(
- '<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>',
- 'url',
- 'https://example.com',
- array('safe' => false),
- ),
- array(
- '<th>Data</th> <td><a href="http://example.com">example.com</a></td>',
- 'url',
- 'http://example.com',
- array('safe' => false, 'hide_protocol' => true),
- ),
- array(
- '<th>Data</th> <td><a href="https://example.com">example.com</a></td>',
- 'url',
- 'https://example.com',
- array('safe' => false, 'hide_protocol' => true),
- ),
- array(
- '<th>Data</th> <td><a href="http://example.com">http://example.com</a></td>',
- 'url',
- 'http://example.com',
- array('safe' => false, 'hide_protocol' => false),
- ),
- array(
- '<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>',
- 'url',
- 'https://example.com',
- array('safe' => false,
- 'hide_protocol' => false, ),
- ),
- array(
- '<th>Data</th> <td><a href="http://example.com">Foo</a></td>',
- 'url',
- 'Foo',
- array('safe' => false, 'url' => 'http://example.com'),
- ),
- array(
- '<th>Data</th> <td><a href="http://example.com"><b>Foo</b></a></td>',
- 'url',
- '<b>Foo</b>',
- array('safe' => false, 'url' => 'http://example.com'),
- ),
- array(
- '<th>Data</th> <td><a href="http://example.com"><b>Foo</b></a></td>',
- 'url',
- '<b>Foo</b>',
- array('safe' => true, 'url' => 'http://example.com'),
- ),
- array(
- '<th>Data</th> <td><a href="/foo">Foo</a></td>',
- 'url',
- 'Foo',
- array('safe' => false, 'route' => array('name' => 'sonata_admin_foo')),
- ),
- array(
- '<th>Data</th> <td><a href="http://localhost/foo">Foo</a></td>',
- 'url',
- 'Foo',
- array('safe' => false, 'route' => array(
- 'name' => 'sonata_admin_foo',
- 'absolute' => true,
- )),
- ),
- array(
- '<th>Data</th> <td><a href="/foo">foo/bar?a=b&c=123456789</a></td>',
- 'url',
- 'http://foo/bar?a=b&c=123456789',
- array(
- 'safe' => false,
- 'route' => array('name' => 'sonata_admin_foo'),
- 'hide_protocol' => true,
- ),
- ),
- array(
- '<th>Data</th> <td><a href="http://localhost/foo">foo/bar?a=b&c=123456789</a></td>',
- 'url',
- 'http://foo/bar?a=b&c=123456789',
- array('safe' => false, 'route' => array(
- 'name' => 'sonata_admin_foo',
- 'absolute' => true,
- ), 'hide_protocol' => true),
- ),
- array(
- '<th>Data</th> <td><a href="/foo/abcd/efgh?param3=ijkl">Foo</a></td>',
- 'url',
- 'Foo',
- array('safe' => false, 'route' => array(
- 'name' => 'sonata_admin_foo_param',
- 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'),
- )),
- ),
- array(
- '<th>Data</th> <td><a href="http://localhost/foo/abcd/efgh?param3=ijkl">Foo</a></td>',
- 'url',
- 'Foo',
- array('safe' => false, 'route' => array(
- 'name' => 'sonata_admin_foo_param',
- 'absolute' => true,
- 'parameters' => array(
- 'param1' => 'abcd',
- 'param2' => 'efgh',
- 'param3' => 'ijkl',
- ),
- )),
- ),
- array(
- '<th>Data</th> <td><a href="/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a></td>',
- 'url',
- 'Foo',
- array('safe' => false, 'route' => array(
- 'name' => 'sonata_admin_foo_object',
- 'parameters' => array(
- 'param1' => 'abcd',
- 'param2' => 'efgh',
- 'param3' => 'ijkl',
- ),
- 'identifier_parameter_name' => 'barId',
- )),
- ),
- array(
- '<th>Data</th> <td><a href="http://localhost/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a></td>',
- 'url',
- 'Foo',
- array('safe' => false, 'route' => array(
- 'name' => 'sonata_admin_foo_object',
- 'absolute' => true,
- 'parameters' => array(
- 'param1' => 'abcd',
- 'param2' => 'efgh',
- 'param3' => 'ijkl',
- ),
- 'identifier_parameter_name' => 'barId',
- )),
- ),
- array(
- '<th>Data</th> <td> </td>',
- 'email',
- null,
- array(),
- ),
- array(
- '<th>Data</th> <td> <a href="mailto:admin@admin.com">admin@admin.com</a></td>',
- 'email',
- 'admin@admin.com',
- array(),
- ),
- array(
- '<th>Data</th> <td> <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme', 'body' => 'Message Body')).'">admin@admin.com</a></td>',
- 'email',
- 'admin@admin.com',
- array('subject' => 'Main Theme', 'body' => 'Message Body'),
- ),
- array(
- '<th>Data</th> <td> <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme')).'">admin@admin.com</a></td>',
- 'email',
- 'admin@admin.com',
- array('subject' => 'Main Theme'),
- ),
- array(
- '<th>Data</th> <td> <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('body' => 'Message Body')).'">admin@admin.com</a></td>',
- 'email',
- 'admin@admin.com',
- array('body' => 'Message Body'),
- ),
- array(
- '<th>Data</th> <td> admin@admin.com</td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true, 'subject' => 'Main Theme', 'body' => 'Message Body'),
- ),
- array(
- '<th>Data</th> <td> admin@admin.com</td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true, 'subject' => 'Main Theme'),
- ),
- array(
- '<th>Data</th> <td> admin@admin.com</td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true, 'body' => 'Message Body'),
- ),
- array(
- '<th>Data</th> <td> <a href="mailto:admin@admin.com">admin@admin.com</a></td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => false),
- ),
- array(
- '<th>Data</th> <td> admin@admin.com</td>',
- 'email',
- 'admin@admin.com',
- array('as_string' => true),
- ),
- array(
- '<th>Data</th> <td><p><strong>Creating a Template for the Field</strong> and form</p> </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array(),
- ),
- array(
- '<th>Data</th> <td>Creating a Template for the Field and form </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('strip' => true),
- ),
- array(
- '<th>Data</th> <td> Creating a Template for the Fi... </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => true),
- ),
- array(
- '<th>Data</th> <td> Creating a... </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => array('length' => 10)),
- ),
- array(
- '<th>Data</th> <td> Creating a Template for the Field... </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => array('preserve' => true)),
- ),
- array(
- '<th>Data</th> <td> Creating a Template for the Fi etc. </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array('truncate' => array('separator' => ' etc.')),
- ),
- array(
- '<th>Data</th> <td> Creating a Template for[...] </td>',
- 'html',
- '<p><strong>Creating a Template for the Field</strong> and form</p>',
- array(
- 'truncate' => array(
- 'length' => 20,
- 'preserve' => true,
- 'separator' => '[...]',
- ),
- ),
- ),
- // NoValueException
- array('<th>Data</th> <td></td>', 'string', new NoValueException(), array('safe' => false)),
- array('<th>Data</th> <td></td>', 'text', new NoValueException(), array('safe' => false)),
- array('<th>Data</th> <td></td>', 'textarea', new NoValueException(), array('safe' => false)),
- array('<th>Data</th> <td> </td>', 'datetime', new NoValueException(), array()),
- array(
- '<th>Data</th> <td> </td>',
- 'datetime',
- new NoValueException(),
- array('format' => 'd.m.Y H:i:s'),
- ),
- array('<th>Data</th> <td> </td>', 'date', new NoValueException(), array()),
- array('<th>Data</th> <td> </td>', 'date', new NoValueException(), array('format' => 'd.m.Y')),
- array('<th>Data</th> <td> </td>', 'time', new NoValueException(), array()),
- array('<th>Data</th> <td></td>', 'number', new NoValueException(), array('safe' => false)),
- array('<th>Data</th> <td></td>', 'integer', new NoValueException(), array('safe' => false)),
- array('<th>Data</th> <td> 0 % </td>', 'percent', new NoValueException(), array()),
- array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'EUR')),
- array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'GBP')),
- array('<th>Data</th> <td> </td>', 'array', new NoValueException(), array('safe' => false)),
- array(
- '<th>Data</th> <td><span class="label label-danger">no</span></td>',
- 'boolean',
- new NoValueException(),
- array(),
- ),
- array(
- '<th>Data</th> <td> </td>',
- 'trans',
- new NoValueException(),
- array('safe' => false, 'catalogue' => 'SonataAdminBundle'),
- ),
- array(
- '<th>Data</th> <td></td>',
- 'choice',
- new NoValueException(),
- array('safe' => false, 'choices' => array()),
- ),
- array(
- '<th>Data</th> <td></td>',
- 'choice',
- new NoValueException(),
- array('safe' => false, 'choices' => array(), 'multiple' => true),
- ),
- array('<th>Data</th> <td> </td>', 'url', new NoValueException(), array()),
- array(
- '<th>Data</th> <td> </td>',
- 'url',
- new NoValueException(),
- array('url' => 'http://example.com'),
- ),
- array(
- '<th>Data</th> <td> </td>',
- 'url',
- new NoValueException(),
- array('route' => array('name' => 'sonata_admin_foo')),
- ),
- );
- }
- public function testGetValueFromFieldDescription()
- {
- $object = new \stdClass();
- $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue('test123'));
- $this->assertSame('test123', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
- }
- public function testGetValueFromFieldDescriptionWithRemoveLoopException()
- {
- $object = $this->getMock('\ArrayAccess');
- $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- try {
- $this->assertSame(
- 'anything',
- $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription, array('loop' => true))
- );
- } catch (\RuntimeException $e) {
- $this->assertContains('remove the loop requirement', $e->getMessage());
- return;
- }
- $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
- }
- public function testGetValueFromFieldDescriptionWithNoValueException()
- {
- $object = new \stdClass();
- $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->any())
- ->method('getValue')
- ->will($this->returnCallback(function () {
- throw new NoValueException();
- }));
- $fieldDescription->expects($this->any())
- ->method('getAssociationAdmin')
- ->will($this->returnValue(null));
- $this->assertSame(null, $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
- }
- public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInstance()
- {
- $object = new \stdClass();
- $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription->expects($this->any())
- ->method('getValue')
- ->will($this->returnCallback(function () {
- throw new NoValueException();
- }));
- $fieldDescription->expects($this->any())
- ->method('getAssociationAdmin')
- ->will($this->returnValue($this->admin));
- $this->admin->expects($this->once())
- ->method('getNewInstance')
- ->will($this->returnValue('foo'));
- $this->assertSame('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
- }
- public function testOutput()
- {
- $this->fieldDescription->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
- $this->fieldDescription->expects($this->any())
- ->method('getFieldName')
- ->will($this->returnValue('fd_name'));
- $this->environment->disableDebug();
- $parameters = array(
- 'admin' => $this->admin,
- 'value' => 'foo',
- 'field_description' => $this->fieldDescription,
- 'object' => $this->object,
- );
- $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
- $this->assertSame(
- '<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
- $this->removeExtraWhitespace($this->twigExtension->output(
- $this->fieldDescription,
- $template,
- $parameters,
- $this->environment
- ))
- );
- $this->environment->enableDebug();
- $this->assertSame(
- $this->removeExtraWhitespace(<<<'EOT'
- <!-- START
- fieldName: fd_name
- template: SonataAdminBundle:CRUD:base_list_field.html.twig
- compiled template: SonataAdminBundle:CRUD:base_list_field.html.twig
- -->
- <td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>
- <!-- END - fieldName: fd_name -->
- EOT
- ),
- $this->removeExtraWhitespace(
- $this->twigExtension->output($this->fieldDescription, $template, $parameters, $this->environment)
- )
- );
- }
- public function testRenderRelationElementNoObject()
- {
- $this->assertSame('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
- }
- public function testRenderRelationElementToString()
- {
- $this->fieldDescription->expects($this->exactly(2))
- ->method('getOption')
- ->will($this->returnCallback(function ($value, $default = null) {
- if ($value == 'associated_property') {
- return $default;
- }
- }));
- $element = new FooToString();
- $this->assertSame('salut', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
- }
- /**
- * @group legacy
- */
- public function testDeprecatedRelationElementToString()
- {
- $this->fieldDescription->expects($this->exactly(2))
- ->method('getOption')
- ->will($this->returnCallback(function ($value, $default = null) {
- if ($value == 'associated_tostring') {
- return '__toString';
- }
- }));
- $element = new FooToString();
- $this->assertSame(
- 'salut',
- $this->twigExtension->renderRelationElement($element, $this->fieldDescription)
- );
- }
- /**
- * @group legacy
- */
- public function testRenderRelationElementCustomToString()
- {
- $this->fieldDescription->expects($this->exactly(2))
- ->method('getOption')
- ->will($this->returnCallback(function ($value, $default = null) {
- if ($value == 'associated_property') {
- return $default;
- }
- if ($value == 'associated_tostring') {
- return 'customToString';
- }
- }));
- $element = $this->getMock('stdClass', array('customToString'));
- $element->expects($this->any())
- ->method('customToString')
- ->will($this->returnValue('fooBar'));
- $this->assertSame('fooBar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
- }
- /**
- * @group legacy
- */
- public function testRenderRelationElementMethodNotExist()
- {
- $this->fieldDescription->expects($this->exactly(2))
- ->method('getOption')
- ->will($this->returnCallback(function ($value, $default = null) {
- if ($value == 'associated_tostring') {
- return 'nonExistedMethod';
- }
- }));
- $element = new \stdClass();
- try {
- $this->twigExtension->renderRelationElement($element, $this->fieldDescription);
- } catch (\RuntimeException $e) {
- $this->assertContains(
- 'You must define an `associated_property` option or create a `stdClass::__toString',
- $e->getMessage()
- );
- return;
- }
- $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
- }
- public function testRenderRelationElementWithPropertyPath()
- {
- $this->fieldDescription->expects($this->exactly(1))
- ->method('getOption')
- ->will($this->returnCallback(function ($value, $default = null) {
- if ($value == 'associated_property') {
- return 'foo';
- }
- }));
- $element = new \stdClass();
- $element->foo = 'bar';
- $this->assertSame('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
- }
- public function testRenderRelationElementWithClosure()
- {
- $this->fieldDescription->expects($this->exactly(1))
- ->method('getOption')
- ->will($this->returnCallback(function ($value, $default = null) {
- if ($value == 'associated_property') {
- return function ($element) {
- return 'closure '.$element->foo;
- };
- }
- }));
- $element = new \stdClass();
- $element->foo = 'bar';
- $this->assertSame(
- 'closure bar',
- $this->twigExtension->renderRelationElement($element, $this->fieldDescription)
- );
- }
- public function testGetUrlsafeIdentifier()
- {
- $entity = new \stdClass();
- // set admin to pool
- $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
- $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service')));
- $this->admin->expects($this->once())
- ->method('getUrlsafeIdentifier')
- ->with($this->equalTo($entity))
- ->will($this->returnValue(1234567));
- $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity));
- }
- public function testGetUrlsafeIdentifier_GivenAdmin_Foo()
- {
- $entity = new \stdClass();
- // set admin to pool
- $this->pool->setAdminServiceIds(array(
- 'sonata_admin_foo_service',
- 'sonata_admin_bar_service',
- ));
- $this->pool->setAdminClasses(array('stdClass' => array(
- 'sonata_admin_foo_service',
- 'sonata_admin_bar_service',
- )));
- $this->admin->expects($this->once())
- ->method('getUrlsafeIdentifier')
- ->with($this->equalTo($entity))
- ->will($this->returnValue(1234567));
- $this->adminBar->expects($this->never())
- ->method('getUrlsafeIdentifier');
- $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->admin));
- }
- public function testGetUrlsafeIdentifier_GivenAdmin_Bar()
- {
- $entity = new \stdClass();
- // set admin to pool
- $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
- $this->pool->setAdminClasses(array('stdClass' => array(
- 'sonata_admin_foo_service',
- 'sonata_admin_bar_service',
- )));
- $this->admin->expects($this->never())
- ->method('getUrlsafeIdentifier');
- $this->adminBar->expects($this->once())
- ->method('getUrlsafeIdentifier')
- ->with($this->equalTo($entity))
- ->will($this->returnValue(1234567));
- $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->adminBar));
- }
- /**
- * This method generates url part for Twig layout. Allows to keep BC for PHP 5.3.
- *
- * Remove this method for next major release only if PHP 5.3 support will be dropped.
- *
- * @param array $url
- *
- * @return string
- */
- private function buildTwigLikeUrl($url)
- {
- if (defined('PHP_QUERY_RFC3986')) {
- // add htmlspecialchars because twig add it auto
- return htmlspecialchars(http_build_query($url, '', '&', PHP_QUERY_RFC3986));
- }
- return htmlspecialchars(http_build_query($url, '', '&'));
- }
- private function removeExtraWhitespace($string)
- {
- return trim(preg_replace(
- '/\s+/',
- ' ',
- $string
- ));
- }
- }
|