SonataAdminExtensionTest.php 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Twig\Extension;
  11. use Psr\Log\LoggerInterface;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. use Sonata\AdminBundle\Exception\NoValueException;
  16. use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
  17. use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
  18. use Symfony\Bridge\Twig\Extension\RoutingExtension;
  19. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  20. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  21. use Symfony\Component\Config\FileLocator;
  22. use Symfony\Component\Routing\Generator\UrlGenerator;
  23. use Symfony\Component\Routing\Loader\XmlFileLoader;
  24. use Symfony\Component\Routing\RequestContext;
  25. use Symfony\Component\Translation\Loader\XliffFileLoader;
  26. use Symfony\Component\Translation\MessageSelector;
  27. use Symfony\Component\Translation\Translator;
  28. use Symfony\Component\Translation\TranslatorInterface;
  29. /**
  30. * Test for SonataAdminExtension.
  31. *
  32. * @author Andrej Hudec <pulzarraider@gmail.com>
  33. */
  34. class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * @var SonataAdminExtension
  38. */
  39. private $twigExtension;
  40. /**
  41. * @var \Twig_Environment
  42. */
  43. private $environment;
  44. /**
  45. * @var AdminInterface
  46. */
  47. private $admin;
  48. /**
  49. * @var AdminInterface
  50. */
  51. private $adminBar;
  52. /**
  53. * @var FieldDescriptionInterface
  54. */
  55. private $fieldDescription;
  56. /**
  57. * @var \stdClass
  58. */
  59. private $object;
  60. /**
  61. * @var Pool
  62. */
  63. private $pool;
  64. /**
  65. * @var LoggerInterface
  66. */
  67. private $logger;
  68. /**
  69. * @var string[]
  70. */
  71. private $xEditableTypeMapping;
  72. /**
  73. * @var TranslatorInterface
  74. */
  75. private $translator;
  76. public function setUp()
  77. {
  78. date_default_timezone_set('Europe/London');
  79. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  80. $this->pool = new Pool($container, '', '');
  81. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
  82. $this->pool->setAdminClasses(array('fooClass' => array('sonata_admin_foo_service')));
  83. $this->logger = $this->getMock('Psr\Log\LoggerInterface');
  84. $this->xEditableTypeMapping = array(
  85. 'choice' => 'select',
  86. 'boolean' => 'select',
  87. 'text' => 'text',
  88. 'textarea' => 'textarea',
  89. 'html' => 'textarea',
  90. 'email' => 'email',
  91. 'string' => 'text',
  92. 'smallint' => 'text',
  93. 'bigint' => 'text',
  94. 'integer' => 'number',
  95. 'decimal' => 'number',
  96. 'currency' => 'number',
  97. 'percent' => 'number',
  98. 'url' => 'url',
  99. );
  100. // translation extension
  101. $translator = new Translator('en', new MessageSelector());
  102. $translator->addLoader('xlf', new XliffFileLoader());
  103. $translator->addResource(
  104. 'xlf',
  105. __DIR__.'/../../../Resources/translations/SonataAdminBundle.en.xliff',
  106. 'en',
  107. 'SonataAdminBundle'
  108. );
  109. $this->translator = $translator;
  110. $this->twigExtension = new SonataAdminExtension($this->pool, $this->logger, $this->translator);
  111. $this->twigExtension->setXEditableTypeMapping($this->xEditableTypeMapping);
  112. $loader = new StubFilesystemLoader(array(
  113. __DIR__.'/../../../Resources/views/CRUD',
  114. ));
  115. $this->environment = new \Twig_Environment($loader, array(
  116. 'strict_variables' => true,
  117. 'cache' => false,
  118. 'autoescape' => 'html',
  119. 'optimizations' => 0,
  120. ));
  121. $this->environment->addExtension($this->twigExtension);
  122. $this->environment->addExtension(new TranslationExtension($translator));
  123. // routing extension
  124. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../../Resources/config/routing')));
  125. $routeCollection = $xmlFileLoader->load('sonata_admin.xml');
  126. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../Fixtures/Resources/config/routing')));
  127. $testRouteCollection = $xmlFileLoader->load('routing.xml');
  128. $routeCollection->addCollection($testRouteCollection);
  129. $requestContext = new RequestContext();
  130. $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
  131. $this->environment->addExtension(new RoutingExtension($urlGenerator));
  132. $this->environment->addExtension(new \Twig_Extensions_Extension_Text());
  133. // initialize object
  134. $this->object = new \stdClass();
  135. // initialize admin
  136. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  137. $this->admin->expects($this->any())
  138. ->method('getCode')
  139. ->will($this->returnValue('xyz'));
  140. $this->admin->expects($this->any())
  141. ->method('id')
  142. ->with($this->equalTo($this->object))
  143. ->will($this->returnValue(12345));
  144. $this->admin->expects($this->any())
  145. ->method('getNormalizedIdentifier')
  146. ->with($this->equalTo($this->object))
  147. ->will($this->returnValue(12345));
  148. $this->admin->expects($this->any())
  149. ->method('trans')
  150. ->will($this->returnCallback(function ($id, $parameters = array(), $domain = null) use ($translator) {
  151. return $translator->trans($id, $parameters, $domain);
  152. }));
  153. $this->adminBar = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  154. $this->adminBar->expects($this->any())
  155. ->method('isGranted')
  156. ->will($this->returnValue(true));
  157. $this->adminBar->expects($this->any())
  158. ->method('getNormalizedIdentifier')
  159. ->with($this->equalTo($this->object))
  160. ->will($this->returnValue(12345));
  161. // for php5.3 BC
  162. $admin = $this->admin;
  163. $adminBar = $this->adminBar;
  164. $container->expects($this->any())
  165. ->method('get')
  166. ->will($this->returnCallback(function ($id) use ($admin, $adminBar) {
  167. if ($id == 'sonata_admin_foo_service') {
  168. return $admin;
  169. } elseif ($id == 'sonata_admin_bar_service') {
  170. return $adminBar;
  171. }
  172. return;
  173. }));
  174. // initialize field description
  175. $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  176. $this->fieldDescription->expects($this->any())
  177. ->method('getName')
  178. ->will($this->returnValue('fd_name'));
  179. $this->fieldDescription->expects($this->any())
  180. ->method('getAdmin')
  181. ->will($this->returnValue($this->admin));
  182. $this->fieldDescription->expects($this->any())
  183. ->method('getLabel')
  184. ->will($this->returnValue('Data'));
  185. }
  186. /**
  187. * @dataProvider getRenderListElementTests
  188. */
  189. public function testRenderListElement($expected, $type, $value, array $options)
  190. {
  191. $this->admin->expects($this->any())
  192. ->method('getPersistentParameters')
  193. ->will($this->returnValue(array('context' => 'foo')));
  194. $this->admin->expects($this->any())
  195. ->method('isGranted')
  196. ->will($this->returnValue(true));
  197. $this->admin->expects($this->any())
  198. ->method('getTemplate')
  199. ->with($this->equalTo('base_list_field'))
  200. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  201. $this->fieldDescription->expects($this->any())
  202. ->method('getValue')
  203. ->will($this->returnValue($value));
  204. $this->fieldDescription->expects($this->any())
  205. ->method('getType')
  206. ->will($this->returnValue($type));
  207. $this->fieldDescription->expects($this->any())
  208. ->method('getOptions')
  209. ->will($this->returnValue($options));
  210. $this->fieldDescription->expects($this->any())
  211. ->method('getOption')
  212. ->will($this->returnCallback(function ($name, $default = null) use ($options) {
  213. return isset($options[$name]) ? $options[$name] : $default;
  214. }));
  215. $this->fieldDescription->expects($this->any())
  216. ->method('getTemplate')
  217. ->will($this->returnCallback(function () use ($type) {
  218. switch ($type) {
  219. case 'string':
  220. return 'SonataAdminBundle:CRUD:list_string.html.twig';
  221. case 'boolean':
  222. return 'SonataAdminBundle:CRUD:list_boolean.html.twig';
  223. case 'datetime':
  224. return 'SonataAdminBundle:CRUD:list_datetime.html.twig';
  225. case 'date':
  226. return 'SonataAdminBundle:CRUD:list_date.html.twig';
  227. case 'time':
  228. return 'SonataAdminBundle:CRUD:list_time.html.twig';
  229. case 'currency':
  230. return 'SonataAdminBundle:CRUD:list_currency.html.twig';
  231. case 'percent':
  232. return 'SonataAdminBundle:CRUD:list_percent.html.twig';
  233. case 'email':
  234. return 'SonataAdminBundle:CRUD:list_email.html.twig';
  235. case 'choice':
  236. return 'SonataAdminBundle:CRUD:list_choice.html.twig';
  237. case 'array':
  238. return 'SonataAdminBundle:CRUD:list_array.html.twig';
  239. case 'trans':
  240. return 'SonataAdminBundle:CRUD:list_trans.html.twig';
  241. case 'url':
  242. return 'SonataAdminBundle:CRUD:list_url.html.twig';
  243. case 'html':
  244. return 'SonataAdminBundle:CRUD:list_html.html.twig';
  245. case 'nonexistent':
  246. // template doesn`t exist
  247. return 'SonataAdminBundle:CRUD:list_nonexistent_template.html.twig';
  248. default:
  249. return false;
  250. }
  251. }));
  252. $this->assertSame(
  253. $this->removeExtraWhitespace($expected),
  254. $this->removeExtraWhitespace($this->twigExtension->renderListElement(
  255. $this->environment,
  256. $this->object,
  257. $this->fieldDescription
  258. ))
  259. );
  260. }
  261. /**
  262. * @dataProvider getDeprecatedRenderListElementTests
  263. * @group legacy
  264. */
  265. public function testDeprecatedRenderListElement($expected, $value, array $options)
  266. {
  267. $this->admin->expects($this->any())
  268. ->method('isGranted')
  269. ->will($this->returnValue(true));
  270. $this->admin->expects($this->any())
  271. ->method('getTemplate')
  272. ->with($this->equalTo('base_list_field'))
  273. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  274. $this->fieldDescription->expects($this->any())
  275. ->method('getValue')
  276. ->will($this->returnValue($value));
  277. $this->fieldDescription->expects($this->any())
  278. ->method('getType')
  279. ->will($this->returnValue('nonexistent'));
  280. $this->fieldDescription->expects($this->any())
  281. ->method('getOptions')
  282. ->will($this->returnValue($options));
  283. $this->fieldDescription->expects($this->any())
  284. ->method('getOption')
  285. ->will($this->returnCallback(function ($name, $default = null) use ($options) {
  286. return isset($options[$name]) ? $options[$name] : $default;
  287. }));
  288. $this->fieldDescription->expects($this->any())
  289. ->method('getTemplate')
  290. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  291. $this->assertSame(
  292. $this->removeExtraWhitespace($expected),
  293. $this->removeExtraWhitespace($this->twigExtension->renderListElement(
  294. $this->environment,
  295. $this->object,
  296. $this->fieldDescription
  297. ))
  298. );
  299. }
  300. public function getDeprecatedRenderListElementTests()
  301. {
  302. return array(
  303. array(
  304. '<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> Example </td>',
  305. 'Example',
  306. array(),
  307. ),
  308. array(
  309. '<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> </td>',
  310. null,
  311. array(),
  312. ),
  313. );
  314. }
  315. public function getRenderListElementTests()
  316. {
  317. return array(
  318. array(
  319. '<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>',
  320. 'string',
  321. 'Example',
  322. array(),
  323. ),
  324. array(
  325. '<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> </td>',
  326. 'string',
  327. null,
  328. array(),
  329. ),
  330. array(
  331. '<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>',
  332. 'text',
  333. 'Example',
  334. array(),
  335. ),
  336. array(
  337. '<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> </td>',
  338. 'text',
  339. null,
  340. array(),
  341. ),
  342. array(
  343. '<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>',
  344. 'textarea',
  345. 'Example',
  346. array(),
  347. ),
  348. array(
  349. '<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>',
  350. 'textarea',
  351. null,
  352. array(),
  353. ),
  354. 'datetime field' => array(
  355. '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
  356. December 24, 2013 10:11
  357. </td>',
  358. 'datetime',
  359. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  360. array(),
  361. ),
  362. array(
  363. '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
  364. December 24, 2013 18:11
  365. </td>',
  366. 'datetime',
  367. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('UTC')),
  368. array('timezone' => 'Asia/Hong_Kong'),
  369. ),
  370. array(
  371. '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>',
  372. 'datetime',
  373. null,
  374. array(),
  375. ),
  376. array(
  377. '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
  378. 24.12.2013 10:11:12
  379. </td>',
  380. 'datetime',
  381. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  382. array('format' => 'd.m.Y H:i:s'),
  383. ),
  384. array(
  385. '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>',
  386. 'datetime',
  387. null,
  388. array('format' => 'd.m.Y H:i:s'),
  389. ),
  390. array(
  391. '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345">
  392. 24.12.2013 18:11:12
  393. </td>',
  394. 'datetime',
  395. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('UTC')),
  396. array('format' => 'd.m.Y H:i:s', 'timezone' => 'Asia/Hong_Kong'),
  397. ),
  398. array(
  399. '<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>',
  400. 'datetime',
  401. null,
  402. array('format' => 'd.m.Y H:i:s', 'timezone' => 'Asia/Hong_Kong'),
  403. ),
  404. array(
  405. '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> December 24, 2013 </td>',
  406. 'date',
  407. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  408. array(),
  409. ),
  410. array(
  411. '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>',
  412. 'date',
  413. null,
  414. array(),
  415. ),
  416. array(
  417. '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> 24.12.2013 </td>',
  418. 'date',
  419. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  420. array('format' => 'd.m.Y'),
  421. ),
  422. array(
  423. '<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>',
  424. 'date',
  425. null,
  426. array('format' => 'd.m.Y'),
  427. ),
  428. array(
  429. '<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> 10:11:12 </td>',
  430. 'time',
  431. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  432. array(),
  433. ),
  434. array(
  435. '<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> &nbsp; </td>',
  436. 'time',
  437. null,
  438. array(),
  439. ),
  440. array(
  441. '<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>',
  442. 'number', 10.746135,
  443. array(),
  444. ),
  445. array(
  446. '<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> </td>',
  447. 'number',
  448. null,
  449. array(),
  450. ),
  451. array(
  452. '<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>',
  453. 'integer',
  454. 5678,
  455. array(),
  456. ),
  457. array(
  458. '<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> </td>',
  459. 'integer',
  460. null,
  461. array(),
  462. ),
  463. array(
  464. '<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>',
  465. 'percent',
  466. 10.746135,
  467. array(),
  468. ),
  469. array(
  470. '<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 0 % </td>',
  471. 'percent',
  472. null,
  473. array(),
  474. ),
  475. array(
  476. '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>',
  477. 'currency',
  478. 10.746135,
  479. array('currency' => 'EUR'),
  480. ),
  481. array(
  482. '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>',
  483. 'currency',
  484. null,
  485. array('currency' => 'EUR'),
  486. ),
  487. array(
  488. '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> GBP 51.23456 </td>',
  489. 'currency',
  490. 51.23456,
  491. array('currency' => 'GBP'),
  492. ),
  493. array(
  494. '<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>',
  495. 'currency',
  496. null,
  497. array('currency' => 'GBP'),
  498. ),
  499. array(
  500. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> &nbsp; </td>',
  501. 'email',
  502. null,
  503. array(),
  504. ),
  505. array(
  506. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> <a href="mailto:admin@admin.com">admin@admin.com</a> </td>',
  507. 'email',
  508. 'admin@admin.com',
  509. array(),
  510. ),
  511. array(
  512. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
  513. <a href="mailto:admin@admin.com">admin@admin.com</a> </td>',
  514. 'email',
  515. 'admin@admin.com',
  516. array('as_string' => false),
  517. ),
  518. array(
  519. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
  520. 'email',
  521. 'admin@admin.com',
  522. array('as_string' => true),
  523. ),
  524. array(
  525. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
  526. <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme', 'body' => 'Message Body')).'">admin@admin.com</a> </td>',
  527. 'email',
  528. 'admin@admin.com',
  529. array('subject' => 'Main Theme', 'body' => 'Message Body'),
  530. ),
  531. array(
  532. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
  533. <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme')).'">admin@admin.com</a> </td>',
  534. 'email',
  535. 'admin@admin.com',
  536. array('subject' => 'Main Theme'),
  537. ),
  538. array(
  539. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345">
  540. <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('body' => 'Message Body')).'">admin@admin.com</a> </td>',
  541. 'email',
  542. 'admin@admin.com',
  543. array('body' => 'Message Body'),
  544. ),
  545. array(
  546. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
  547. 'email',
  548. 'admin@admin.com',
  549. array('as_string' => true, 'subject' => 'Main Theme', 'body' => 'Message Body'),
  550. ),
  551. array(
  552. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
  553. 'email',
  554. 'admin@admin.com',
  555. array('as_string' => true, 'body' => 'Message Body'),
  556. ),
  557. array(
  558. '<td class="sonata-ba-list-field sonata-ba-list-field-email" objectId="12345"> admin@admin.com </td>',
  559. 'email',
  560. 'admin@admin.com',
  561. array('as_string' => true, 'subject' => 'Main Theme'),
  562. ),
  563. array(
  564. '<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345">
  565. [1 => First] [2 => Second]
  566. </td>',
  567. 'array',
  568. array(1 => 'First', 2 => 'Second'),
  569. array(),
  570. ),
  571. array(
  572. '<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> </td>',
  573. 'array',
  574. null,
  575. array(),
  576. ),
  577. array(
  578. '<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
  579. <span class="label label-success">yes</span>
  580. </td>',
  581. 'boolean',
  582. true,
  583. array('editable' => false),
  584. ),
  585. array(
  586. '<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
  587. <span class="label label-danger">no</span>
  588. </td>',
  589. 'boolean',
  590. false,
  591. array('editable' => false),
  592. ),
  593. array(
  594. '<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
  595. <span class="label label-danger">no</span>
  596. </td>',
  597. 'boolean',
  598. null,
  599. array('editable' => false),
  600. ),
  601. array(
  602. <<<'EOT'
  603. <td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
  604. <span
  605. class="x-editable"
  606. data-type="select"
  607. data-value="1"
  608. data-title="Data"
  609. data-pk="12345"
  610. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  611. data-source="[{value: 0, text: 'no'},{value: 1, text: 'yes'}]"
  612. >
  613. <span class="label label-success">yes</span>
  614. </span>
  615. </td>
  616. EOT
  617. ,
  618. 'boolean',
  619. true,
  620. array('editable' => true),
  621. ),
  622. array(
  623. <<<'EOT'
  624. <td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
  625. <span
  626. class="x-editable"
  627. data-type="select"
  628. data-value=""
  629. data-title="Data"
  630. data-pk="12345"
  631. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  632. data-source="[{value: 0, text: 'no'},{value: 1, text: 'yes'}]"
  633. >
  634. <span class="label label-danger">no</span> </span>
  635. </td>
  636. EOT
  637. ,
  638. 'boolean',
  639. false,
  640. array('editable' => true),
  641. ),
  642. array(
  643. <<<'EOT'
  644. <td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345">
  645. <span
  646. class="x-editable"
  647. data-type="select"
  648. data-value=""
  649. data-title="Data"
  650. data-pk="12345"
  651. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  652. data-source="[{value: 0, text: 'no'},{value: 1, text: 'yes'}]" >
  653. <span class="label label-danger">no</span> </span>
  654. </td>
  655. EOT
  656. ,
  657. 'boolean',
  658. null,
  659. array('editable' => true),
  660. ),
  661. array(
  662. '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>',
  663. 'trans',
  664. 'action_delete',
  665. array('catalogue' => 'SonataAdminBundle'),
  666. ),
  667. array(
  668. '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> </td>',
  669. 'trans',
  670. null,
  671. array('catalogue' => 'SonataAdminBundle'),
  672. ),
  673. array(
  674. '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>',
  675. 'trans',
  676. 'action_delete',
  677. array('format' => '%s', 'catalogue' => 'SonataAdminBundle'),
  678. ),
  679. array(
  680. '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345">
  681. action.action_delete
  682. </td>',
  683. 'trans',
  684. 'action_delete',
  685. array('format' => 'action.%s'),
  686. ),
  687. array(
  688. '<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345">
  689. action.action_delete
  690. </td>',
  691. 'trans',
  692. 'action_delete',
  693. array('format' => 'action.%s', 'catalogue' => 'SonataAdminBundle'),
  694. ),
  695. array(
  696. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>',
  697. 'choice',
  698. 'Status1',
  699. array(),
  700. ),
  701. array(
  702. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>',
  703. 'choice',
  704. array('Status1'),
  705. array('choices' => array(), 'multiple' => true),
  706. ),
  707. array(
  708. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Alias1 </td>',
  709. 'choice',
  710. 'Status1',
  711. array('choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3')),
  712. ),
  713. array(
  714. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> </td>',
  715. 'choice',
  716. null,
  717. array('choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3')),
  718. ),
  719. array(
  720. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  721. NoValidKeyInChoices
  722. </td>',
  723. 'choice',
  724. 'NoValidKeyInChoices',
  725. array('choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3')),
  726. ),
  727. array(
  728. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Delete </td>',
  729. 'choice',
  730. 'Foo',
  731. array('catalogue' => 'SonataAdminBundle', 'choices' => array(
  732. 'Foo' => 'action_delete',
  733. 'Status2' => 'Alias2',
  734. 'Status3' => 'Alias3',
  735. )),
  736. ),
  737. array(
  738. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Alias1, Alias3 </td>',
  739. 'choice',
  740. array('Status1', 'Status3'),
  741. array('choices' => array(
  742. 'Status1' => 'Alias1',
  743. 'Status2' => 'Alias2',
  744. 'Status3' => 'Alias3',
  745. ), 'multiple' => true), ),
  746. array(
  747. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Alias1 | Alias3 </td>',
  748. 'choice',
  749. array('Status1', 'Status3'),
  750. array('choices' => array(
  751. 'Status1' => 'Alias1',
  752. 'Status2' => 'Alias2',
  753. 'Status3' => 'Alias3',
  754. ), 'multiple' => true, 'delimiter' => ' | '), ),
  755. array(
  756. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> </td>',
  757. 'choice',
  758. null,
  759. array('choices' => array(
  760. 'Status1' => 'Alias1',
  761. 'Status2' => 'Alias2',
  762. 'Status3' => 'Alias3',
  763. ), 'multiple' => true),
  764. ),
  765. array(
  766. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  767. NoValidKeyInChoices
  768. </td>',
  769. 'choice',
  770. array('NoValidKeyInChoices'),
  771. array('choices' => array(
  772. 'Status1' => 'Alias1',
  773. 'Status2' => 'Alias2',
  774. 'Status3' => 'Alias3',
  775. ), 'multiple' => true),
  776. ),
  777. array(
  778. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  779. NoValidKeyInChoices, Alias2
  780. </td>',
  781. 'choice',
  782. array('NoValidKeyInChoices', 'Status2'),
  783. array('choices' => array(
  784. 'Status1' => 'Alias1',
  785. 'Status2' => 'Alias2',
  786. 'Status3' => 'Alias3',
  787. ), 'multiple' => true),
  788. ),
  789. array(
  790. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Delete, Alias3 </td>',
  791. 'choice',
  792. array('Foo', 'Status3'),
  793. array('catalogue' => 'SonataAdminBundle', 'choices' => array(
  794. 'Foo' => 'action_delete',
  795. 'Status2' => 'Alias2',
  796. 'Status3' => 'Alias3',
  797. ), 'multiple' => true),
  798. ),
  799. array(
  800. '<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  801. &lt;b&gt;Alias1&lt;/b&gt;, &lt;b&gt;Alias3&lt;/b&gt;
  802. </td>',
  803. 'choice',
  804. array('Status1', 'Status3'),
  805. array('choices' => array(
  806. 'Status1' => '<b>Alias1</b>',
  807. 'Status2' => '<b>Alias2</b>',
  808. 'Status3' => '<b>Alias3</b>',
  809. ), 'multiple' => true), ),
  810. array(
  811. <<<'EOT'
  812. <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  813. <span
  814. class="x-editable"
  815. data-type="select"
  816. data-value="Status1"
  817. data-title="Data"
  818. data-pk="12345"
  819. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  820. data-source="[]"
  821. >
  822. Status1
  823. </span>
  824. </td>
  825. EOT
  826. ,
  827. 'choice',
  828. 'Status1',
  829. array('editable' => true),
  830. ),
  831. array(
  832. <<<'EOT'
  833. <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  834. <span
  835. class="x-editable"
  836. data-type="select"
  837. data-value="Status1"
  838. data-title="Data"
  839. data-pk="12345"
  840. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  841. data-source="[{&quot;value&quot;:&quot;Status1&quot;,&quot;text&quot;:&quot;Alias1&quot;},{&quot;value&quot;:&quot;Status2&quot;,&quot;text&quot;:&quot;Alias2&quot;},{&quot;value&quot;:&quot;Status3&quot;,&quot;text&quot;:&quot;Alias3&quot;}]" >
  842. Alias1 </span>
  843. </td>
  844. EOT
  845. ,
  846. 'choice',
  847. 'Status1',
  848. array(
  849. 'editable' => true,
  850. 'choices' => array(
  851. 'Status1' => 'Alias1',
  852. 'Status2' => 'Alias2',
  853. 'Status3' => 'Alias3',
  854. ),
  855. ),
  856. ),
  857. array(
  858. <<<'EOT'
  859. <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  860. <span
  861. class="x-editable"
  862. data-type="select"
  863. data-value=""
  864. data-title="Data"
  865. data-pk="12345"
  866. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  867. data-source="[{&quot;value&quot;:&quot;Status1&quot;,&quot;text&quot;:&quot;Alias1&quot;},{&quot;value&quot;:&quot;Status2&quot;,&quot;text&quot;:&quot;Alias2&quot;},{&quot;value&quot;:&quot;Status3&quot;,&quot;text&quot;:&quot;Alias3&quot;}]" >
  868. </span>
  869. </td>
  870. EOT
  871. ,
  872. 'choice',
  873. null,
  874. array(
  875. 'editable' => true,
  876. 'choices' => array(
  877. 'Status1' => 'Alias1',
  878. 'Status2' => 'Alias2',
  879. 'Status3' => 'Alias3',
  880. ),
  881. ),
  882. ),
  883. array(
  884. <<<'EOT'
  885. <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  886. <span
  887. class="x-editable"
  888. data-type="select"
  889. data-value="NoValidKeyInChoices"
  890. data-title="Data" data-pk="12345"
  891. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  892. data-source="[{&quot;value&quot;:&quot;Status1&quot;,&quot;text&quot;:&quot;Alias1&quot;},{&quot;value&quot;:&quot;Status2&quot;,&quot;text&quot;:&quot;Alias2&quot;},{&quot;value&quot;:&quot;Status3&quot;,&quot;text&quot;:&quot;Alias3&quot;}]" >
  893. NoValidKeyInChoices
  894. </span>
  895. </td>
  896. EOT
  897. ,
  898. 'choice',
  899. 'NoValidKeyInChoices',
  900. array(
  901. 'editable' => true,
  902. 'choices' => array(
  903. 'Status1' => 'Alias1',
  904. 'Status2' => 'Alias2',
  905. 'Status3' => 'Alias3',
  906. ),
  907. ),
  908. ),
  909. array(
  910. <<<'EOT'
  911. <td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345">
  912. <span
  913. class="x-editable"
  914. data-type="select"
  915. data-value="Foo"
  916. data-title="Data"
  917. data-pk="12345"
  918. data-url="/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;code=xyz"
  919. data-source="[{&quot;value&quot;:&quot;Foo&quot;,&quot;text&quot;:&quot;action_delete&quot;},{&quot;value&quot;:&quot;Status2&quot;,&quot;text&quot;:&quot;Alias2&quot;},{&quot;value&quot;:&quot;Status3&quot;,&quot;text&quot;:&quot;Alias3&quot;}]" >
  920. Delete
  921. </span>
  922. </td>
  923. EOT
  924. ,
  925. 'choice',
  926. 'Foo',
  927. array(
  928. 'editable' => true,
  929. 'catalogue' => 'SonataAdminBundle',
  930. 'choices' => array(
  931. 'Foo' => 'action_delete',
  932. 'Status2' => 'Alias2',
  933. 'Status3' => 'Alias3',
  934. ),
  935. ),
  936. ),
  937. array(
  938. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>',
  939. 'url',
  940. null,
  941. array(),
  942. ),
  943. array(
  944. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>',
  945. 'url',
  946. null,
  947. array('url' => 'http://example.com'),
  948. ),
  949. array(
  950. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>',
  951. 'url',
  952. null,
  953. array('route' => array('name' => 'sonata_admin_foo')),
  954. ),
  955. array(
  956. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  957. <a href="http://example.com">http://example.com</a>
  958. </td>',
  959. 'url',
  960. 'http://example.com',
  961. array(),
  962. ),
  963. array(
  964. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  965. <a href="https://example.com">https://example.com</a>
  966. </td>',
  967. 'url',
  968. 'https://example.com',
  969. array(),
  970. ),
  971. array(
  972. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  973. <a href="http://example.com">example.com</a>
  974. </td>',
  975. 'url',
  976. 'http://example.com',
  977. array('hide_protocol' => true),
  978. ),
  979. array(
  980. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  981. <a href="https://example.com">example.com</a>
  982. </td>',
  983. 'url',
  984. 'https://example.com',
  985. array('hide_protocol' => true),
  986. ),
  987. array(
  988. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  989. <a href="http://example.com">http://example.com</a>
  990. </td>',
  991. 'url',
  992. 'http://example.com',
  993. array('hide_protocol' => false),
  994. ),
  995. array(
  996. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  997. <a href="https://example.com">https://example.com</a>
  998. </td>',
  999. 'url',
  1000. 'https://example.com',
  1001. array('hide_protocol' => false),
  1002. ),
  1003. array(
  1004. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1005. <a href="http://example.com">Foo</a>
  1006. </td>',
  1007. 'url',
  1008. 'Foo',
  1009. array('url' => 'http://example.com'),
  1010. ),
  1011. array(
  1012. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1013. <a href="http://example.com">&lt;b&gt;Foo&lt;/b&gt;</a>
  1014. </td>',
  1015. 'url',
  1016. '<b>Foo</b>',
  1017. array('url' => 'http://example.com'),
  1018. ),
  1019. array(
  1020. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1021. <a href="/foo">Foo</a>
  1022. </td>',
  1023. 'url',
  1024. 'Foo',
  1025. array('route' => array('name' => 'sonata_admin_foo')),
  1026. ),
  1027. array(
  1028. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1029. <a href="http://localhost/foo">Foo</a>
  1030. </td>',
  1031. 'url',
  1032. 'Foo',
  1033. array('route' => array('name' => 'sonata_admin_foo', 'absolute' => true)),
  1034. ),
  1035. array(
  1036. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1037. <a href="/foo">foo/bar?a=b&amp;c=123456789</a>
  1038. </td>',
  1039. 'url',
  1040. 'http://foo/bar?a=b&c=123456789',
  1041. array('route' => array('name' => 'sonata_admin_foo'),
  1042. 'hide_protocol' => true, ),
  1043. ),
  1044. array(
  1045. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1046. <a href="http://localhost/foo">foo/bar?a=b&amp;c=123456789</a>
  1047. </td>',
  1048. 'url',
  1049. 'http://foo/bar?a=b&c=123456789',
  1050. array(
  1051. 'route' => array('name' => 'sonata_admin_foo', 'absolute' => true),
  1052. 'hide_protocol' => true,
  1053. ),
  1054. ),
  1055. array(
  1056. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1057. <a href="/foo/abcd/efgh?param3=ijkl">Foo</a>
  1058. </td>',
  1059. 'url',
  1060. 'Foo',
  1061. array(
  1062. 'route' => array('name' => 'sonata_admin_foo_param',
  1063. 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'), ),
  1064. ),
  1065. ),
  1066. array(
  1067. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1068. <a href="http://localhost/foo/abcd/efgh?param3=ijkl">Foo</a>
  1069. </td>',
  1070. 'url',
  1071. 'Foo',
  1072. array(
  1073. 'route' => array('name' => 'sonata_admin_foo_param',
  1074. 'absolute' => true,
  1075. 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'), ),
  1076. ),
  1077. ),
  1078. array(
  1079. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1080. <a href="/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a>
  1081. </td>',
  1082. 'url',
  1083. 'Foo',
  1084. array(
  1085. 'route' => array('name' => 'sonata_admin_foo_object',
  1086. 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'),
  1087. 'identifier_parameter_name' => 'barId', ),
  1088. ),
  1089. ),
  1090. array(
  1091. '<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345">
  1092. <a href="http://localhost/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a>
  1093. </td>',
  1094. 'url',
  1095. 'Foo',
  1096. array(
  1097. 'route' => array('name' => 'sonata_admin_foo_object',
  1098. 'absolute' => true,
  1099. 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'),
  1100. 'identifier_parameter_name' => 'barId', ),
  1101. ),
  1102. ),
  1103. array(
  1104. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
  1105. <p><strong>Creating a Template for the Field</strong> and form</p>
  1106. </td>',
  1107. 'html',
  1108. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1109. array(),
  1110. ),
  1111. array(
  1112. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
  1113. Creating a Template for the Field and form
  1114. </td>',
  1115. 'html',
  1116. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1117. array('strip' => true),
  1118. ),
  1119. array(
  1120. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
  1121. Creating a Template for the Fi...
  1122. </td>',
  1123. 'html',
  1124. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1125. array('truncate' => true),
  1126. ),
  1127. array(
  1128. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a... </td>',
  1129. 'html',
  1130. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1131. array('truncate' => array('length' => 10)),
  1132. ),
  1133. array(
  1134. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
  1135. Creating a Template for the Field...
  1136. </td>',
  1137. 'html',
  1138. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1139. array('truncate' => array('preserve' => true)),
  1140. ),
  1141. array(
  1142. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
  1143. Creating a Template for the Fi etc.
  1144. </td>',
  1145. 'html',
  1146. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1147. array('truncate' => array('separator' => ' etc.')),
  1148. ),
  1149. array(
  1150. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345">
  1151. Creating a Template for[...]
  1152. </td>',
  1153. 'html',
  1154. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1155. array(
  1156. 'truncate' => array(
  1157. 'length' => 20,
  1158. 'preserve' => true,
  1159. 'separator' => '[...]',
  1160. ),
  1161. ),
  1162. ),
  1163. array(
  1164. <<<'EOT'
  1165. <td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345">
  1166. <div
  1167. class="sonata-readmore"
  1168. data-readmore-height="40"
  1169. data-readmore-more="Read more"
  1170. data-readmore-less="Close">A very long string</div>
  1171. </td>
  1172. EOT
  1173. ,
  1174. 'text',
  1175. 'A very long string',
  1176. array(
  1177. 'collapse' => true,
  1178. ),
  1179. ),
  1180. array(
  1181. <<<'EOT'
  1182. <td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345">
  1183. <div
  1184. class="sonata-readmore"
  1185. data-readmore-height="10"
  1186. data-readmore-more="More"
  1187. data-readmore-less="Less">A very long string</div>
  1188. </td>
  1189. EOT
  1190. ,
  1191. 'text',
  1192. 'A very long string',
  1193. array(
  1194. 'collapse' => array(
  1195. 'height' => 10,
  1196. 'more' => 'More',
  1197. 'less' => 'Less',
  1198. ),
  1199. ),
  1200. ),
  1201. );
  1202. }
  1203. /**
  1204. * @group legacy
  1205. */
  1206. public function testRenderListElementNonExistentTemplate()
  1207. {
  1208. $this->admin->expects($this->once())
  1209. ->method('getTemplate')
  1210. ->with($this->equalTo('base_list_field'))
  1211. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  1212. $this->fieldDescription->expects($this->once())
  1213. ->method('getValue')
  1214. ->will($this->returnValue('Foo'));
  1215. $this->fieldDescription->expects($this->once())
  1216. ->method('getFieldName')
  1217. ->will($this->returnValue('Foo_name'));
  1218. $this->fieldDescription->expects($this->exactly(2))
  1219. ->method('getType')
  1220. ->will($this->returnValue('nonexistent'));
  1221. $this->fieldDescription->expects($this->once())
  1222. ->method('getTemplate')
  1223. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  1224. $this->logger->expects($this->once())
  1225. ->method('warning')
  1226. ->with(($this->stringStartsWith($this->removeExtraWhitespace(
  1227. 'An error occured trying to load the template
  1228. "SonataAdminBundle:CRUD:list_nonexistent_template.html.twig"
  1229. for the field "Foo_name", the default template
  1230. "SonataAdminBundle:CRUD:base_list_field.html.twig" was used
  1231. instead.'
  1232. ))));
  1233. $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription);
  1234. }
  1235. /**
  1236. * @expectedException \Twig_Error_Loader
  1237. * @expectedExceptionMessage Unable to find template "base_list_nonexistent_field.html.twig"
  1238. * @group legacy
  1239. */
  1240. public function testRenderListElementErrorLoadingTemplate()
  1241. {
  1242. $this->admin->expects($this->once())
  1243. ->method('getTemplate')
  1244. ->with($this->equalTo('base_list_field'))
  1245. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_nonexistent_field.html.twig'));
  1246. $this->fieldDescription->expects($this->once())
  1247. ->method('getTemplate')
  1248. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  1249. $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription);
  1250. }
  1251. /**
  1252. * @dataProvider getRenderViewElementTests
  1253. */
  1254. public function testRenderViewElement($expected, $type, $value, array $options)
  1255. {
  1256. $this->admin->expects($this->any())
  1257. ->method('getTemplate')
  1258. ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
  1259. $this->fieldDescription->expects($this->any())
  1260. ->method('getValue')
  1261. ->will($this->returnCallback(function () use ($value) {
  1262. if ($value instanceof NoValueException) {
  1263. throw $value;
  1264. }
  1265. return $value;
  1266. }));
  1267. $this->fieldDescription->expects($this->any())
  1268. ->method('getType')
  1269. ->will($this->returnValue($type));
  1270. $this->fieldDescription->expects($this->any())
  1271. ->method('getOptions')
  1272. ->will($this->returnValue($options));
  1273. $this->fieldDescription->expects($this->any())
  1274. ->method('getTemplate')
  1275. ->will($this->returnCallback(function () use ($type) {
  1276. switch ($type) {
  1277. case 'boolean':
  1278. return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
  1279. case 'datetime':
  1280. return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
  1281. case 'date':
  1282. return 'SonataAdminBundle:CRUD:show_date.html.twig';
  1283. case 'time':
  1284. return 'SonataAdminBundle:CRUD:show_time.html.twig';
  1285. case 'currency':
  1286. return 'SonataAdminBundle:CRUD:show_currency.html.twig';
  1287. case 'percent':
  1288. return 'SonataAdminBundle:CRUD:show_percent.html.twig';
  1289. case 'email':
  1290. return 'SonataAdminBundle:CRUD:show_email.html.twig';
  1291. case 'choice':
  1292. return 'SonataAdminBundle:CRUD:show_choice.html.twig';
  1293. case 'array':
  1294. return 'SonataAdminBundle:CRUD:show_array.html.twig';
  1295. case 'trans':
  1296. return 'SonataAdminBundle:CRUD:show_trans.html.twig';
  1297. case 'url':
  1298. return 'SonataAdminBundle:CRUD:show_url.html.twig';
  1299. case 'html':
  1300. return 'SonataAdminBundle:CRUD:show_html.html.twig';
  1301. default:
  1302. return false;
  1303. }
  1304. }));
  1305. $this->assertSame(
  1306. $this->removeExtraWhitespace($expected),
  1307. $this->removeExtraWhitespace(
  1308. $this->twigExtension->renderViewElement(
  1309. $this->environment,
  1310. $this->fieldDescription,
  1311. $this->object
  1312. )
  1313. )
  1314. );
  1315. }
  1316. public function getRenderViewElementTests()
  1317. {
  1318. return array(
  1319. array('<th>Data</th> <td>Example</td>', 'string', 'Example', array('safe' => false)),
  1320. array('<th>Data</th> <td>Example</td>', 'text', 'Example', array('safe' => false)),
  1321. array('<th>Data</th> <td>Example</td>', 'textarea', 'Example', array('safe' => false)),
  1322. array(
  1323. '<th>Data</th> <td>December 24, 2013 10:11</td>',
  1324. 'datetime',
  1325. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array(),
  1326. ),
  1327. array(
  1328. '<th>Data</th> <td>24.12.2013 10:11:12</td>',
  1329. 'datetime',
  1330. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  1331. array('format' => 'd.m.Y H:i:s'),
  1332. ),
  1333. array(
  1334. '<th>Data</th> <td>December 24, 2013</td>',
  1335. 'date',
  1336. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  1337. array(),
  1338. ),
  1339. array(
  1340. '<th>Data</th> <td>24.12.2013</td>',
  1341. 'date',
  1342. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  1343. array('format' => 'd.m.Y'),
  1344. ),
  1345. array(
  1346. '<th>Data</th> <td>10:11:12</td>',
  1347. 'time',
  1348. new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')),
  1349. array(),
  1350. ),
  1351. array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135, array('safe' => false)),
  1352. array('<th>Data</th> <td>5678</td>', 'integer', 5678, array('safe' => false)),
  1353. array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135, array()),
  1354. array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  1355. array('<th>Data</th> <td> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  1356. array(
  1357. '<th>Data</th> <td> [1 => First] <br> [2 => Second] </td>',
  1358. 'array',
  1359. array(1 => 'First', 2 => 'Second'),
  1360. array('safe' => false),
  1361. ),
  1362. array(
  1363. '<th>Data</th> <td> [1 => First] [2 => Second] </td>',
  1364. 'array',
  1365. array(1 => 'First', 2 => 'Second'),
  1366. array('safe' => false, 'inline' => true),
  1367. ),
  1368. array(
  1369. '<th>Data</th> <td><span class="label label-success">yes</span></td>',
  1370. 'boolean',
  1371. true,
  1372. array(),
  1373. ),
  1374. array('<th>Data</th> <td><span class="label label-danger">no</span></td>', 'boolean', false, array()),
  1375. array(
  1376. '<th>Data</th> <td> Delete </td>',
  1377. 'trans',
  1378. 'action_delete',
  1379. array('safe' => false, 'catalogue' => 'SonataAdminBundle'),
  1380. ),
  1381. array('<th>Data</th> <td>Status1</td>', 'choice', 'Status1', array('safe' => false)),
  1382. array(
  1383. '<th>Data</th> <td>Alias1</td>',
  1384. 'choice',
  1385. 'Status1',
  1386. array('safe' => false, 'choices' => array(
  1387. 'Status1' => 'Alias1',
  1388. 'Status2' => 'Alias2',
  1389. 'Status3' => 'Alias3',
  1390. )),
  1391. ),
  1392. array(
  1393. '<th>Data</th> <td>NoValidKeyInChoices</td>',
  1394. 'choice',
  1395. 'NoValidKeyInChoices',
  1396. array('safe' => false, 'choices' => array(
  1397. 'Status1' => 'Alias1',
  1398. 'Status2' => 'Alias2',
  1399. 'Status3' => 'Alias3',
  1400. )),
  1401. ),
  1402. array(
  1403. '<th>Data</th> <td>Delete</td>',
  1404. 'choice',
  1405. 'Foo',
  1406. array('safe' => false, 'catalogue' => 'SonataAdminBundle', 'choices' => array(
  1407. 'Foo' => 'action_delete',
  1408. 'Status2' => 'Alias2',
  1409. 'Status3' => 'Alias3',
  1410. )),
  1411. ),
  1412. array(
  1413. '<th>Data</th> <td>NoValidKeyInChoices</td>',
  1414. 'choice',
  1415. array('NoValidKeyInChoices'),
  1416. array('safe' => false, 'choices' => array(
  1417. 'Status1' => 'Alias1',
  1418. 'Status2' => 'Alias2',
  1419. 'Status3' => 'Alias3',
  1420. ), 'multiple' => true),
  1421. ),
  1422. array(
  1423. '<th>Data</th> <td>NoValidKeyInChoices, Alias2</td>',
  1424. 'choice',
  1425. array('NoValidKeyInChoices', 'Status2'),
  1426. array('safe' => false, 'choices' => array(
  1427. 'Status1' => 'Alias1',
  1428. 'Status2' => 'Alias2',
  1429. 'Status3' => 'Alias3',
  1430. ), 'multiple' => true),
  1431. ),
  1432. array(
  1433. '<th>Data</th> <td>Alias1, Alias3</td>',
  1434. 'choice',
  1435. array('Status1', 'Status3'),
  1436. array('safe' => false, 'choices' => array(
  1437. 'Status1' => 'Alias1',
  1438. 'Status2' => 'Alias2',
  1439. 'Status3' => 'Alias3',
  1440. ), 'multiple' => true),
  1441. ),
  1442. array(
  1443. '<th>Data</th> <td>Alias1 | Alias3</td>',
  1444. 'choice',
  1445. array('Status1', 'Status3'), array('safe' => false, 'choices' => array(
  1446. 'Status1' => 'Alias1',
  1447. 'Status2' => 'Alias2',
  1448. 'Status3' => 'Alias3',
  1449. ), 'multiple' => true, 'delimiter' => ' | '),
  1450. ),
  1451. array(
  1452. '<th>Data</th> <td>Delete, Alias3</td>',
  1453. 'choice',
  1454. array('Foo', 'Status3'),
  1455. array('safe' => false, 'catalogue' => 'SonataAdminBundle', 'choices' => array(
  1456. 'Foo' => 'action_delete',
  1457. 'Status2' => 'Alias2',
  1458. 'Status3' => 'Alias3',
  1459. ), 'multiple' => true),
  1460. ),
  1461. array(
  1462. '<th>Data</th> <td><b>Alias1</b>, <b>Alias3</b></td>',
  1463. 'choice',
  1464. array('Status1', 'Status3'),
  1465. array('safe' => true, 'choices' => array(
  1466. 'Status1' => '<b>Alias1</b>',
  1467. 'Status2' => '<b>Alias2</b>',
  1468. 'Status3' => '<b>Alias3</b>',
  1469. ), 'multiple' => true),
  1470. ),
  1471. array(
  1472. '<th>Data</th> <td>&lt;b&gt;Alias1&lt;/b&gt;, &lt;b&gt;Alias3&lt;/b&gt;</td>',
  1473. 'choice',
  1474. array('Status1', 'Status3'),
  1475. array('safe' => false, 'choices' => array(
  1476. 'Status1' => '<b>Alias1</b>',
  1477. 'Status2' => '<b>Alias2</b>',
  1478. 'Status3' => '<b>Alias3</b>',
  1479. ), 'multiple' => true),
  1480. ),
  1481. array(
  1482. '<th>Data</th> <td><a href="http://example.com">http://example.com</a></td>',
  1483. 'url',
  1484. 'http://example.com',
  1485. array('safe' => false),
  1486. ),
  1487. array(
  1488. '<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>',
  1489. 'url',
  1490. 'https://example.com',
  1491. array('safe' => false),
  1492. ),
  1493. array(
  1494. '<th>Data</th> <td><a href="http://example.com">example.com</a></td>',
  1495. 'url',
  1496. 'http://example.com',
  1497. array('safe' => false, 'hide_protocol' => true),
  1498. ),
  1499. array(
  1500. '<th>Data</th> <td><a href="https://example.com">example.com</a></td>',
  1501. 'url',
  1502. 'https://example.com',
  1503. array('safe' => false, 'hide_protocol' => true),
  1504. ),
  1505. array(
  1506. '<th>Data</th> <td><a href="http://example.com">http://example.com</a></td>',
  1507. 'url',
  1508. 'http://example.com',
  1509. array('safe' => false, 'hide_protocol' => false),
  1510. ),
  1511. array(
  1512. '<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>',
  1513. 'url',
  1514. 'https://example.com',
  1515. array('safe' => false,
  1516. 'hide_protocol' => false, ),
  1517. ),
  1518. array(
  1519. '<th>Data</th> <td><a href="http://example.com">Foo</a></td>',
  1520. 'url',
  1521. 'Foo',
  1522. array('safe' => false, 'url' => 'http://example.com'),
  1523. ),
  1524. array(
  1525. '<th>Data</th> <td><a href="http://example.com">&lt;b&gt;Foo&lt;/b&gt;</a></td>',
  1526. 'url',
  1527. '<b>Foo</b>',
  1528. array('safe' => false, 'url' => 'http://example.com'),
  1529. ),
  1530. array(
  1531. '<th>Data</th> <td><a href="http://example.com"><b>Foo</b></a></td>',
  1532. 'url',
  1533. '<b>Foo</b>',
  1534. array('safe' => true, 'url' => 'http://example.com'),
  1535. ),
  1536. array(
  1537. '<th>Data</th> <td><a href="/foo">Foo</a></td>',
  1538. 'url',
  1539. 'Foo',
  1540. array('safe' => false, 'route' => array('name' => 'sonata_admin_foo')),
  1541. ),
  1542. array(
  1543. '<th>Data</th> <td><a href="http://localhost/foo">Foo</a></td>',
  1544. 'url',
  1545. 'Foo',
  1546. array('safe' => false, 'route' => array(
  1547. 'name' => 'sonata_admin_foo',
  1548. 'absolute' => true,
  1549. )),
  1550. ),
  1551. array(
  1552. '<th>Data</th> <td><a href="/foo">foo/bar?a=b&amp;c=123456789</a></td>',
  1553. 'url',
  1554. 'http://foo/bar?a=b&c=123456789',
  1555. array(
  1556. 'safe' => false,
  1557. 'route' => array('name' => 'sonata_admin_foo'),
  1558. 'hide_protocol' => true,
  1559. ),
  1560. ),
  1561. array(
  1562. '<th>Data</th> <td><a href="http://localhost/foo">foo/bar?a=b&amp;c=123456789</a></td>',
  1563. 'url',
  1564. 'http://foo/bar?a=b&c=123456789',
  1565. array('safe' => false, 'route' => array(
  1566. 'name' => 'sonata_admin_foo',
  1567. 'absolute' => true,
  1568. ), 'hide_protocol' => true),
  1569. ),
  1570. array(
  1571. '<th>Data</th> <td><a href="/foo/abcd/efgh?param3=ijkl">Foo</a></td>',
  1572. 'url',
  1573. 'Foo',
  1574. array('safe' => false, 'route' => array(
  1575. 'name' => 'sonata_admin_foo_param',
  1576. 'parameters' => array('param1' => 'abcd', 'param2' => 'efgh', 'param3' => 'ijkl'),
  1577. )),
  1578. ),
  1579. array(
  1580. '<th>Data</th> <td><a href="http://localhost/foo/abcd/efgh?param3=ijkl">Foo</a></td>',
  1581. 'url',
  1582. 'Foo',
  1583. array('safe' => false, 'route' => array(
  1584. 'name' => 'sonata_admin_foo_param',
  1585. 'absolute' => true,
  1586. 'parameters' => array(
  1587. 'param1' => 'abcd',
  1588. 'param2' => 'efgh',
  1589. 'param3' => 'ijkl',
  1590. ),
  1591. )),
  1592. ),
  1593. array(
  1594. '<th>Data</th> <td><a href="/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a></td>',
  1595. 'url',
  1596. 'Foo',
  1597. array('safe' => false, 'route' => array(
  1598. 'name' => 'sonata_admin_foo_object',
  1599. 'parameters' => array(
  1600. 'param1' => 'abcd',
  1601. 'param2' => 'efgh',
  1602. 'param3' => 'ijkl',
  1603. ),
  1604. 'identifier_parameter_name' => 'barId',
  1605. )),
  1606. ),
  1607. array(
  1608. '<th>Data</th> <td><a href="http://localhost/foo/obj/abcd/12345/efgh?param3=ijkl">Foo</a></td>',
  1609. 'url',
  1610. 'Foo',
  1611. array('safe' => false, 'route' => array(
  1612. 'name' => 'sonata_admin_foo_object',
  1613. 'absolute' => true,
  1614. 'parameters' => array(
  1615. 'param1' => 'abcd',
  1616. 'param2' => 'efgh',
  1617. 'param3' => 'ijkl',
  1618. ),
  1619. 'identifier_parameter_name' => 'barId',
  1620. )),
  1621. ),
  1622. array(
  1623. '<th>Data</th> <td> &nbsp;</td>',
  1624. 'email',
  1625. null,
  1626. array(),
  1627. ),
  1628. array(
  1629. '<th>Data</th> <td> <a href="mailto:admin@admin.com">admin@admin.com</a></td>',
  1630. 'email',
  1631. 'admin@admin.com',
  1632. array(),
  1633. ),
  1634. array(
  1635. '<th>Data</th> <td> <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme', 'body' => 'Message Body')).'">admin@admin.com</a></td>',
  1636. 'email',
  1637. 'admin@admin.com',
  1638. array('subject' => 'Main Theme', 'body' => 'Message Body'),
  1639. ),
  1640. array(
  1641. '<th>Data</th> <td> <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('subject' => 'Main Theme')).'">admin@admin.com</a></td>',
  1642. 'email',
  1643. 'admin@admin.com',
  1644. array('subject' => 'Main Theme'),
  1645. ),
  1646. array(
  1647. '<th>Data</th> <td> <a href="mailto:admin@admin.com?'.$this->buildTwigLikeUrl(array('body' => 'Message Body')).'">admin@admin.com</a></td>',
  1648. 'email',
  1649. 'admin@admin.com',
  1650. array('body' => 'Message Body'),
  1651. ),
  1652. array(
  1653. '<th>Data</th> <td> admin@admin.com</td>',
  1654. 'email',
  1655. 'admin@admin.com',
  1656. array('as_string' => true, 'subject' => 'Main Theme', 'body' => 'Message Body'),
  1657. ),
  1658. array(
  1659. '<th>Data</th> <td> admin@admin.com</td>',
  1660. 'email',
  1661. 'admin@admin.com',
  1662. array('as_string' => true, 'subject' => 'Main Theme'),
  1663. ),
  1664. array(
  1665. '<th>Data</th> <td> admin@admin.com</td>',
  1666. 'email',
  1667. 'admin@admin.com',
  1668. array('as_string' => true, 'body' => 'Message Body'),
  1669. ),
  1670. array(
  1671. '<th>Data</th> <td> <a href="mailto:admin@admin.com">admin@admin.com</a></td>',
  1672. 'email',
  1673. 'admin@admin.com',
  1674. array('as_string' => false),
  1675. ),
  1676. array(
  1677. '<th>Data</th> <td> admin@admin.com</td>',
  1678. 'email',
  1679. 'admin@admin.com',
  1680. array('as_string' => true),
  1681. ),
  1682. array(
  1683. '<th>Data</th> <td><p><strong>Creating a Template for the Field</strong> and form</p> </td>',
  1684. 'html',
  1685. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1686. array(),
  1687. ),
  1688. array(
  1689. '<th>Data</th> <td>Creating a Template for the Field and form </td>',
  1690. 'html',
  1691. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1692. array('strip' => true),
  1693. ),
  1694. array(
  1695. '<th>Data</th> <td> Creating a Template for the Fi... </td>',
  1696. 'html',
  1697. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1698. array('truncate' => true),
  1699. ),
  1700. array(
  1701. '<th>Data</th> <td> Creating a... </td>',
  1702. 'html',
  1703. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1704. array('truncate' => array('length' => 10)),
  1705. ),
  1706. array(
  1707. '<th>Data</th> <td> Creating a Template for the Field... </td>',
  1708. 'html',
  1709. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1710. array('truncate' => array('preserve' => true)),
  1711. ),
  1712. array(
  1713. '<th>Data</th> <td> Creating a Template for the Fi etc. </td>',
  1714. 'html',
  1715. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1716. array('truncate' => array('separator' => ' etc.')),
  1717. ),
  1718. array(
  1719. '<th>Data</th> <td> Creating a Template for[...] </td>',
  1720. 'html',
  1721. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  1722. array(
  1723. 'truncate' => array(
  1724. 'length' => 20,
  1725. 'preserve' => true,
  1726. 'separator' => '[...]',
  1727. ),
  1728. ),
  1729. ),
  1730. // NoValueException
  1731. array('<th>Data</th> <td></td>', 'string', new NoValueException(), array('safe' => false)),
  1732. array('<th>Data</th> <td></td>', 'text', new NoValueException(), array('safe' => false)),
  1733. array('<th>Data</th> <td></td>', 'textarea', new NoValueException(), array('safe' => false)),
  1734. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array()),
  1735. array(
  1736. '<th>Data</th> <td>&nbsp;</td>',
  1737. 'datetime',
  1738. new NoValueException(),
  1739. array('format' => 'd.m.Y H:i:s'),
  1740. ),
  1741. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array()),
  1742. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array('format' => 'd.m.Y')),
  1743. array('<th>Data</th> <td>&nbsp;</td>', 'time', new NoValueException(), array()),
  1744. array('<th>Data</th> <td></td>', 'number', new NoValueException(), array('safe' => false)),
  1745. array('<th>Data</th> <td></td>', 'integer', new NoValueException(), array('safe' => false)),
  1746. array('<th>Data</th> <td> 0 % </td>', 'percent', new NoValueException(), array()),
  1747. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'EUR')),
  1748. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'GBP')),
  1749. array('<th>Data</th> <td> </td>', 'array', new NoValueException(), array('safe' => false)),
  1750. array(
  1751. '<th>Data</th> <td><span class="label label-danger">no</span></td>',
  1752. 'boolean',
  1753. new NoValueException(),
  1754. array(),
  1755. ),
  1756. array(
  1757. '<th>Data</th> <td> </td>',
  1758. 'trans',
  1759. new NoValueException(),
  1760. array('safe' => false, 'catalogue' => 'SonataAdminBundle'),
  1761. ),
  1762. array(
  1763. '<th>Data</th> <td></td>',
  1764. 'choice',
  1765. new NoValueException(),
  1766. array('safe' => false, 'choices' => array()),
  1767. ),
  1768. array(
  1769. '<th>Data</th> <td></td>',
  1770. 'choice',
  1771. new NoValueException(),
  1772. array('safe' => false, 'choices' => array(), 'multiple' => true),
  1773. ),
  1774. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array()),
  1775. array(
  1776. '<th>Data</th> <td>&nbsp;</td>',
  1777. 'url',
  1778. new NoValueException(),
  1779. array('url' => 'http://example.com'),
  1780. ),
  1781. array(
  1782. '<th>Data</th> <td>&nbsp;</td>',
  1783. 'url',
  1784. new NoValueException(),
  1785. array('route' => array('name' => 'sonata_admin_foo')),
  1786. ),
  1787. array(
  1788. <<<'EOT'
  1789. <th>Data</th> <td><div
  1790. class="sonata-readmore"
  1791. data-readmore-height="40"
  1792. data-readmore-more="Read more"
  1793. data-readmore-less="Close">
  1794. A very long string
  1795. </div></td>
  1796. EOT
  1797. ,
  1798. 'text',
  1799. ' A very long string ',
  1800. array(
  1801. 'collapse' => true,
  1802. 'safe' => false,
  1803. ),
  1804. ),
  1805. array(
  1806. <<<'EOT'
  1807. <th>Data</th> <td><div
  1808. class="sonata-readmore"
  1809. data-readmore-height="10"
  1810. data-readmore-more="More"
  1811. data-readmore-less="Less">
  1812. A very long string
  1813. </div></td>
  1814. EOT
  1815. ,
  1816. 'text',
  1817. ' A very long string ',
  1818. array(
  1819. 'collapse' => array(
  1820. 'height' => 10,
  1821. 'more' => 'More',
  1822. 'less' => 'Less',
  1823. ),
  1824. 'safe' => false,
  1825. ),
  1826. ),
  1827. );
  1828. }
  1829. public function testGetValueFromFieldDescription()
  1830. {
  1831. $object = new \stdClass();
  1832. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  1833. $fieldDescription->expects($this->any())
  1834. ->method('getValue')
  1835. ->will($this->returnValue('test123'));
  1836. $this->assertSame('test123', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  1837. }
  1838. public function testGetValueFromFieldDescriptionWithRemoveLoopException()
  1839. {
  1840. $object = $this->getMock('\ArrayAccess');
  1841. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  1842. try {
  1843. $this->assertSame(
  1844. 'anything',
  1845. $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription, array('loop' => true))
  1846. );
  1847. } catch (\RuntimeException $e) {
  1848. $this->assertContains('remove the loop requirement', $e->getMessage());
  1849. return;
  1850. }
  1851. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  1852. }
  1853. public function testGetValueFromFieldDescriptionWithNoValueException()
  1854. {
  1855. $object = new \stdClass();
  1856. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  1857. $fieldDescription->expects($this->any())
  1858. ->method('getValue')
  1859. ->will($this->returnCallback(function () {
  1860. throw new NoValueException();
  1861. }));
  1862. $fieldDescription->expects($this->any())
  1863. ->method('getAssociationAdmin')
  1864. ->will($this->returnValue(null));
  1865. $this->assertSame(null, $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  1866. }
  1867. public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInstance()
  1868. {
  1869. $object = new \stdClass();
  1870. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  1871. $fieldDescription->expects($this->any())
  1872. ->method('getValue')
  1873. ->will($this->returnCallback(function () {
  1874. throw new NoValueException();
  1875. }));
  1876. $fieldDescription->expects($this->any())
  1877. ->method('getAssociationAdmin')
  1878. ->will($this->returnValue($this->admin));
  1879. $this->admin->expects($this->once())
  1880. ->method('getNewInstance')
  1881. ->will($this->returnValue('foo'));
  1882. $this->assertSame('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  1883. }
  1884. public function testOutput()
  1885. {
  1886. $this->fieldDescription->expects($this->any())
  1887. ->method('getTemplate')
  1888. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  1889. $this->fieldDescription->expects($this->any())
  1890. ->method('getFieldName')
  1891. ->will($this->returnValue('fd_name'));
  1892. $this->environment->disableDebug();
  1893. $parameters = array(
  1894. 'admin' => $this->admin,
  1895. 'value' => 'foo',
  1896. 'field_description' => $this->fieldDescription,
  1897. 'object' => $this->object,
  1898. );
  1899. $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
  1900. $this->assertSame(
  1901. '<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
  1902. $this->removeExtraWhitespace($this->twigExtension->output(
  1903. $this->fieldDescription,
  1904. $template,
  1905. $parameters,
  1906. $this->environment
  1907. ))
  1908. );
  1909. $this->environment->enableDebug();
  1910. $this->assertSame(
  1911. $this->removeExtraWhitespace(<<<'EOT'
  1912. <!-- START
  1913. fieldName: fd_name
  1914. template: SonataAdminBundle:CRUD:base_list_field.html.twig
  1915. compiled template: SonataAdminBundle:CRUD:base_list_field.html.twig
  1916. -->
  1917. <td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>
  1918. <!-- END - fieldName: fd_name -->
  1919. EOT
  1920. ),
  1921. $this->removeExtraWhitespace(
  1922. $this->twigExtension->output($this->fieldDescription, $template, $parameters, $this->environment)
  1923. )
  1924. );
  1925. }
  1926. public function testRenderRelationElementNoObject()
  1927. {
  1928. $this->assertSame('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
  1929. }
  1930. public function testRenderRelationElementToString()
  1931. {
  1932. $this->fieldDescription->expects($this->exactly(2))
  1933. ->method('getOption')
  1934. ->will($this->returnCallback(function ($value, $default = null) {
  1935. if ($value == 'associated_property') {
  1936. return $default;
  1937. }
  1938. }));
  1939. $element = new FooToString();
  1940. $this->assertSame('salut', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  1941. }
  1942. /**
  1943. * @group legacy
  1944. */
  1945. public function testDeprecatedRelationElementToString()
  1946. {
  1947. $this->fieldDescription->expects($this->exactly(2))
  1948. ->method('getOption')
  1949. ->will($this->returnCallback(function ($value, $default = null) {
  1950. if ($value == 'associated_tostring') {
  1951. return '__toString';
  1952. }
  1953. }));
  1954. $element = new FooToString();
  1955. $this->assertSame(
  1956. 'salut',
  1957. $this->twigExtension->renderRelationElement($element, $this->fieldDescription)
  1958. );
  1959. }
  1960. /**
  1961. * @group legacy
  1962. */
  1963. public function testRenderRelationElementCustomToString()
  1964. {
  1965. $this->fieldDescription->expects($this->exactly(2))
  1966. ->method('getOption')
  1967. ->will($this->returnCallback(function ($value, $default = null) {
  1968. if ($value == 'associated_property') {
  1969. return $default;
  1970. }
  1971. if ($value == 'associated_tostring') {
  1972. return 'customToString';
  1973. }
  1974. }));
  1975. $element = $this->getMock('stdClass', array('customToString'));
  1976. $element->expects($this->any())
  1977. ->method('customToString')
  1978. ->will($this->returnValue('fooBar'));
  1979. $this->assertSame('fooBar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  1980. }
  1981. /**
  1982. * @group legacy
  1983. */
  1984. public function testRenderRelationElementMethodNotExist()
  1985. {
  1986. $this->fieldDescription->expects($this->exactly(2))
  1987. ->method('getOption')
  1988. ->will($this->returnCallback(function ($value, $default = null) {
  1989. if ($value == 'associated_tostring') {
  1990. return 'nonExistedMethod';
  1991. }
  1992. }));
  1993. $element = new \stdClass();
  1994. try {
  1995. $this->twigExtension->renderRelationElement($element, $this->fieldDescription);
  1996. } catch (\RuntimeException $e) {
  1997. $this->assertContains(
  1998. 'You must define an `associated_property` option or create a `stdClass::__toString',
  1999. $e->getMessage()
  2000. );
  2001. return;
  2002. }
  2003. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  2004. }
  2005. public function testRenderRelationElementWithPropertyPath()
  2006. {
  2007. $this->fieldDescription->expects($this->exactly(1))
  2008. ->method('getOption')
  2009. ->will($this->returnCallback(function ($value, $default = null) {
  2010. if ($value == 'associated_property') {
  2011. return 'foo';
  2012. }
  2013. }));
  2014. $element = new \stdClass();
  2015. $element->foo = 'bar';
  2016. $this->assertSame('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  2017. }
  2018. public function testRenderRelationElementWithClosure()
  2019. {
  2020. $this->fieldDescription->expects($this->exactly(1))
  2021. ->method('getOption')
  2022. ->will($this->returnCallback(function ($value, $default = null) {
  2023. if ($value == 'associated_property') {
  2024. return function ($element) {
  2025. return 'closure '.$element->foo;
  2026. };
  2027. }
  2028. }));
  2029. $element = new \stdClass();
  2030. $element->foo = 'bar';
  2031. $this->assertSame(
  2032. 'closure bar',
  2033. $this->twigExtension->renderRelationElement($element, $this->fieldDescription)
  2034. );
  2035. }
  2036. public function testGetUrlsafeIdentifier()
  2037. {
  2038. $entity = new \stdClass();
  2039. // set admin to pool
  2040. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
  2041. $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service')));
  2042. $this->admin->expects($this->once())
  2043. ->method('getUrlsafeIdentifier')
  2044. ->with($this->equalTo($entity))
  2045. ->will($this->returnValue(1234567));
  2046. $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity));
  2047. }
  2048. public function testGetUrlsafeIdentifier_GivenAdmin_Foo()
  2049. {
  2050. $entity = new \stdClass();
  2051. // set admin to pool
  2052. $this->pool->setAdminServiceIds(array(
  2053. 'sonata_admin_foo_service',
  2054. 'sonata_admin_bar_service',
  2055. ));
  2056. $this->pool->setAdminClasses(array('stdClass' => array(
  2057. 'sonata_admin_foo_service',
  2058. 'sonata_admin_bar_service',
  2059. )));
  2060. $this->admin->expects($this->once())
  2061. ->method('getUrlsafeIdentifier')
  2062. ->with($this->equalTo($entity))
  2063. ->will($this->returnValue(1234567));
  2064. $this->adminBar->expects($this->never())
  2065. ->method('getUrlsafeIdentifier');
  2066. $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->admin));
  2067. }
  2068. public function testGetUrlsafeIdentifier_GivenAdmin_Bar()
  2069. {
  2070. $entity = new \stdClass();
  2071. // set admin to pool
  2072. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
  2073. $this->pool->setAdminClasses(array('stdClass' => array(
  2074. 'sonata_admin_foo_service',
  2075. 'sonata_admin_bar_service',
  2076. )));
  2077. $this->admin->expects($this->never())
  2078. ->method('getUrlsafeIdentifier');
  2079. $this->adminBar->expects($this->once())
  2080. ->method('getUrlsafeIdentifier')
  2081. ->with($this->equalTo($entity))
  2082. ->will($this->returnValue(1234567));
  2083. $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->adminBar));
  2084. }
  2085. public function xEditableChoicesProvider()
  2086. {
  2087. return array(
  2088. 'needs processing' => array(array('Status1' => 'Alias1', 'Status2' => 'Alias2')),
  2089. 'already processed' => array(array(
  2090. array('value' => 'Status1', 'text' => 'Alias1'),
  2091. array('value' => 'Status2', 'text' => 'Alias2'),
  2092. )),
  2093. );
  2094. }
  2095. /**
  2096. * @dataProvider xEditablechoicesProvider
  2097. */
  2098. public function testGetXEditableChoicesIsIdempotent(array $input)
  2099. {
  2100. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  2101. $fieldDescription->expects($this->exactly(2))
  2102. ->method('getOption')
  2103. ->withConsecutive(
  2104. array('choices', array()),
  2105. array('catalogue')
  2106. )
  2107. ->will($this->onConsecutiveCalls(
  2108. $input,
  2109. 'MyCatalogue'
  2110. ));
  2111. $this->assertSame(
  2112. array(
  2113. array('value' => 'Status1', 'text' => 'Alias1'),
  2114. array('value' => 'Status2', 'text' => 'Alias2'),
  2115. ),
  2116. $this->twigExtension->getXEditableChoices($fieldDescription)
  2117. );
  2118. }
  2119. /**
  2120. * This method generates url part for Twig layout. Allows to keep BC for PHP 5.3.
  2121. *
  2122. * Remove this method for next major release only if PHP 5.3 support will be dropped.
  2123. *
  2124. * @param array $url
  2125. *
  2126. * @return string
  2127. */
  2128. private function buildTwigLikeUrl($url)
  2129. {
  2130. if (defined('PHP_QUERY_RFC3986')) {
  2131. // add htmlspecialchars because twig add it auto
  2132. return htmlspecialchars(http_build_query($url, '', '&', PHP_QUERY_RFC3986));
  2133. }
  2134. return htmlspecialchars(http_build_query($url, '', '&'));
  2135. }
  2136. private function removeExtraWhitespace($string)
  2137. {
  2138. return trim(preg_replace(
  2139. '/\s+/',
  2140. ' ',
  2141. $string
  2142. ));
  2143. }
  2144. }