SonataAdminExtensionTest.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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 Sonata\AdminBundle\Admin\Pool;
  12. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Sonata\AdminBundle\Exception\NoValueException;
  15. use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
  16. use Symfony\Bridge\Twig\Extension\RoutingExtension;
  17. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  18. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  19. use Symfony\Component\Config\FileLocator;
  20. use Symfony\Component\Routing\Generator\UrlGenerator;
  21. use Symfony\Component\Routing\Loader\XmlFileLoader;
  22. use Symfony\Component\Routing\RequestContext;
  23. use Symfony\Component\Routing\RouterInterface;
  24. use Symfony\Component\Translation\Translator;
  25. use Symfony\Component\Translation\MessageSelector;
  26. use Symfony\Component\Translation\Loader\XliffFileLoader;
  27. use Psr\Log\LoggerInterface;
  28. /**
  29. * Test for SonataAdminExtension
  30. *
  31. * @author Andrej Hudec <pulzarraider@gmail.com>
  32. */
  33. class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * @var SonataAdminExtension
  37. */
  38. private $twigExtension;
  39. /**
  40. * @var Twig_Environment
  41. */
  42. private $environment;
  43. /**
  44. * @var AdminInterface
  45. */
  46. private $admin;
  47. /**
  48. * @var AdminInterface
  49. */
  50. private $adminBar;
  51. /**
  52. * @var FieldDescriptionInterface
  53. */
  54. private $fieldDescription;
  55. /**
  56. * @var \stdClass
  57. */
  58. private $object;
  59. /**
  60. * @var Pool
  61. */
  62. private $pool;
  63. /**
  64. * @var Router
  65. */
  66. private $router;
  67. /**
  68. * @var LoggerInterface
  69. */
  70. private $logger;
  71. public function setUp()
  72. {
  73. date_default_timezone_set('Europe/London');
  74. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  75. $this->pool = new Pool($container, '', '');
  76. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
  77. $this->pool->setAdminClasses(array('fooClass' => array('sonata_admin_foo_service')));
  78. $this->router = $this->getMock('Symfony\Component\Routing\RouterInterface');
  79. $this->logger = $this->getMock('Psr\Log\LoggerInterface');
  80. $this->twigExtension = new SonataAdminExtension($this->pool, $this->router, $this->logger);
  81. $loader = new StubFilesystemLoader(array(
  82. __DIR__.'/../../../Resources/views/CRUD',
  83. ));
  84. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
  85. $this->environment->addExtension($this->twigExtension);
  86. // translation extension
  87. $translator = new Translator('en', new MessageSelector());
  88. $translator->addLoader('xlf', new XliffFileLoader());
  89. $translator->addResource('xlf', __DIR__.'/../../../Resources/translations/SonataAdminBundle.en.xliff', 'en', 'SonataAdminBundle');
  90. $this->environment->addExtension(new TranslationExtension($translator));
  91. // routing extension
  92. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../../Resources/config/routing')));
  93. $routeCollection = $xmlFileLoader->load('sonata_admin.xml');
  94. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../Fixtures/Resources/config/routing')));
  95. $testRouteCollection = $xmlFileLoader->load('routing.xml');
  96. $routeCollection->addCollection($testRouteCollection);
  97. $requestContext = new RequestContext();
  98. $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
  99. $this->environment->addExtension(new RoutingExtension($urlGenerator));
  100. $this->environment->addExtension(new \Twig_Extensions_Extension_Text());
  101. $this->twigExtension->initRuntime($this->environment);
  102. // initialize object
  103. $this->object = new \stdClass();
  104. // initialize admin
  105. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  106. $this->admin->expects($this->any())
  107. ->method('getCode')
  108. ->will($this->returnValue('xyz'));
  109. $this->admin->expects($this->any())
  110. ->method('id')
  111. ->with($this->equalTo($this->object))
  112. ->will($this->returnValue(12345));
  113. $this->admin->expects($this->any())
  114. ->method('getNormalizedIdentifier')
  115. ->with($this->equalTo($this->object))
  116. ->will($this->returnValue(12345));
  117. $this->admin->expects($this->any())
  118. ->method('trans')
  119. ->will($this->returnCallback(function ($id) {
  120. return $id;
  121. }));
  122. $this->adminBar = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  123. $this->adminBar->expects($this->any())
  124. ->method('isGranted')
  125. ->will($this->returnValue(true));
  126. $this->adminBar->expects($this->any())
  127. ->method('getNormalizedIdentifier')
  128. ->with($this->equalTo($this->object))
  129. ->will($this->returnValue(12345));
  130. // for php5.3 BC
  131. $admin = $this->admin;
  132. $adminBar = $this->adminBar;
  133. $container->expects($this->any())
  134. ->method('get')
  135. ->will($this->returnCallback(function ($id) use ($admin, $adminBar) {
  136. if ($id == 'sonata_admin_foo_service') {
  137. return $admin;
  138. } elseif ($id == 'sonata_admin_bar_service') {
  139. return $adminBar;
  140. }
  141. return;
  142. }));
  143. // initialize field description
  144. $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  145. $this->fieldDescription->expects($this->any())
  146. ->method('getName')
  147. ->will($this->returnValue('fd_name'));
  148. $this->fieldDescription->expects($this->any())
  149. ->method('getAdmin')
  150. ->will($this->returnValue($this->admin));
  151. $this->fieldDescription->expects($this->any())
  152. ->method('getLabel')
  153. ->will($this->returnValue('Data'));
  154. }
  155. /**
  156. * @dataProvider getRenderListElementTests
  157. */
  158. public function testRenderListElement($expected, $type, $value, array $options)
  159. {
  160. $this->admin->expects($this->any())
  161. ->method('isGranted')
  162. ->will($this->returnValue(true));
  163. $this->admin->expects($this->any())
  164. ->method('getTemplate')
  165. ->with($this->equalTo('base_list_field'))
  166. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  167. $this->fieldDescription->expects($this->any())
  168. ->method('getValue')
  169. ->will($this->returnValue($value));
  170. $this->fieldDescription->expects($this->any())
  171. ->method('getType')
  172. ->will($this->returnValue($type));
  173. $this->fieldDescription->expects($this->any())
  174. ->method('getOptions')
  175. ->will($this->returnValue($options));
  176. $this->fieldDescription->expects($this->any())
  177. ->method('getTemplate')
  178. ->will($this->returnCallback(function () use ($type) {
  179. switch ($type) {
  180. case 'string':
  181. return 'SonataAdminBundle:CRUD:list_string.html.twig';
  182. case 'boolean':
  183. return 'SonataAdminBundle:CRUD:list_boolean.html.twig';
  184. case 'datetime':
  185. return 'SonataAdminBundle:CRUD:list_datetime.html.twig';
  186. case 'date':
  187. return 'SonataAdminBundle:CRUD:list_date.html.twig';
  188. case 'time':
  189. return 'SonataAdminBundle:CRUD:list_time.html.twig';
  190. case 'currency':
  191. return 'SonataAdminBundle:CRUD:list_currency.html.twig';
  192. case 'percent':
  193. return 'SonataAdminBundle:CRUD:list_percent.html.twig';
  194. case 'choice':
  195. return 'SonataAdminBundle:CRUD:list_choice.html.twig';
  196. case 'array':
  197. return 'SonataAdminBundle:CRUD:list_array.html.twig';
  198. case 'trans':
  199. return 'SonataAdminBundle:CRUD:list_trans.html.twig';
  200. case 'url':
  201. return 'SonataAdminBundle:CRUD:list_url.html.twig';
  202. case 'html':
  203. return 'SonataAdminBundle:CRUD:list_html.html.twig';
  204. case 'nonexistent':
  205. // template doesn`t exist
  206. return 'SonataAdminBundle:CRUD:list_nonexistent_template.html.twig';
  207. default:
  208. return false;
  209. }
  210. }));
  211. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($this->object, $this->fieldDescription))));
  212. }
  213. public function getRenderListElementTests()
  214. {
  215. return array(
  216. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>', 'string', 'Example', array()),
  217. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> </td>', 'string', null, array()),
  218. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> Example </td>', 'nonexistent', 'Example', array()),
  219. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> </td>', 'nonexistent', null, array()),
  220. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>', 'text', 'Example', array()),
  221. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> </td>', 'text', null, array()),
  222. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example', array()),
  223. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>', 'textarea', null, array()),
  224. 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()),
  225. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array()),
  226. 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')),
  227. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array('format' => 'd.m.Y H:i:s')),
  228. 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()),
  229. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array()),
  230. 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')),
  231. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array('format' => 'd.m.Y')),
  232. 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()),
  233. array('<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> &nbsp; </td>', 'time', null, array()),
  234. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>', 'number', 10.746135, array()),
  235. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> </td>', 'number', null, array()),
  236. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>', 'integer', 5678, array()),
  237. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> </td>', 'integer', null, array()),
  238. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>', 'percent', 10.746135, array()),
  239. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 0 % </td>', 'percent', null, array()),
  240. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  241. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'EUR')),
  242. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  243. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'GBP')),
  244. 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()),
  245. array('<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> </td>', 'array', null, array()),
  246. 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)),
  247. 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)),
  248. 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)),
  249. array('<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&amp;field=fd_name&amp;objectId=12345&amp;code=xyz" data-source="[{value: 0, text: \'no\'},{value: 1, text: \'yes\'}]" > <span class="label label-success">yes</span> </span> </td>', 'boolean', true, array('editable' => true)),
  250. array('<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&amp;field=fd_name&amp;objectId=12345&amp;code=xyz" data-source="[{value: 0, text: \'no\'},{value: 1, text: \'yes\'}]" > <span class="label label-danger">no</span> </span> </td>', 'boolean', false, array('editable' => true)),
  251. array('<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&amp;field=fd_name&amp;objectId=12345&amp;code=xyz" data-source="[{value: 0, text: \'no\'},{value: 1, text: \'yes\'}]" > <span class="label label-danger">no</span> </span> </td>', 'boolean', null, array('editable' => true)),
  252. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>', 'trans', 'action_delete', array('catalogue' => 'SonataAdminBundle')),
  253. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> </td>', 'trans', null, array('catalogue' => 'SonataAdminBundle')),
  254. array('<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>', 'choice', 'Status1', array()),
  255. array('<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>', 'choice', array('Status1'), array('choices' => array(), 'multiple' => true)),
  256. 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'))),
  257. 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'))),
  258. 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'))),
  259. 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'))),
  260. 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)),
  261. 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' => ' | ')),
  262. 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)),
  263. 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)),
  264. 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)),
  265. 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)),
  266. array('<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> &lt;b&gt;Alias1&lt;/b&gt;, &lt;b&gt;Alias3&lt;/b&gt; </td>', 'choice', array('Status1', 'Status3'), array('choices' => array('Status1' => '<b>Alias1</b>', 'Status2' => '<b>Alias2</b>', 'Status3' => '<b>Alias3</b>'), 'multiple' => true)),
  267. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>', 'url', null, array()),
  268. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>', 'url', null, array('url' => 'http://example.com')),
  269. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>', 'url', null, array('route' => array('name' => 'sonata_admin_foo'))),
  270. 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()),
  271. 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()),
  272. 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)),
  273. 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)),
  274. 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)),
  275. 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)),
  276. 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')),
  277. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> <a href="http://example.com">&lt;b&gt;Foo&lt;/b&gt;</a> </td>', 'url', '<b>Foo</b>', array('url' => 'http://example.com')),
  278. 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'))),
  279. 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))),
  280. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> <a href="/foo">foo/bar?a=b&amp;c=123456789</a> </td>', 'url', 'http://foo/bar?a=b&c=123456789', array('route' => array('name' => 'sonata_admin_foo'), 'hide_protocol' => true)),
  281. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> <a href="http://localhost/foo">foo/bar?a=b&amp;c=123456789</a> </td>', 'url', 'http://foo/bar?a=b&c=123456789', array('route' => array('name' => 'sonata_admin_foo', 'absolute' => true), 'hide_protocol' => true)),
  282. 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')))),
  283. 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')))),
  284. 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'))),
  285. 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'))),
  286. array(
  287. '<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>',
  288. 'html',
  289. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  290. array(),
  291. ),
  292. array(
  293. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Field and form </td>',
  294. 'html',
  295. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  296. array('strip' => true),
  297. ),
  298. array(
  299. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Fi... </td>',
  300. 'html',
  301. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  302. array('truncate' => true),
  303. ),
  304. array(
  305. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a... </td>',
  306. 'html',
  307. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  308. array('truncate' => array('length' => 10)),
  309. ),
  310. array(
  311. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Field... </td>',
  312. 'html',
  313. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  314. array('truncate' => array('preserve' => true)),
  315. ),
  316. array(
  317. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Fi etc. </td>',
  318. 'html',
  319. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  320. array('truncate' => array('separator' => ' etc.')),
  321. ),
  322. array(
  323. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for[...] </td>',
  324. 'html',
  325. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  326. array(
  327. 'truncate' => array(
  328. 'length' => 20,
  329. 'preserve' => true,
  330. 'separator' => '[...]',
  331. ),
  332. ),
  333. ),
  334. );
  335. }
  336. public function testRenderListElementNonExistentTemplate()
  337. {
  338. $this->admin->expects($this->once())
  339. ->method('getTemplate')
  340. ->with($this->equalTo('base_list_field'))
  341. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  342. $this->fieldDescription->expects($this->once())
  343. ->method('getValue')
  344. ->will($this->returnValue('Foo'));
  345. $this->fieldDescription->expects($this->once())
  346. ->method('getFieldName')
  347. ->will($this->returnValue('Foo_name'));
  348. $this->fieldDescription->expects($this->exactly(2))
  349. ->method('getType')
  350. ->will($this->returnValue('nonexistent'));
  351. $this->fieldDescription->expects($this->once())
  352. ->method('getTemplate')
  353. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  354. $this->logger->expects($this->once())
  355. ->method('warning')
  356. ->with(($this->stringStartsWith('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: "Unable to find template "list_nonexistent_template.html.twig')));
  357. $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
  358. }
  359. /**
  360. * @expectedException Twig_Error_Loader
  361. * @expectedExceptionMessage Unable to find template "base_list_nonexistent_field.html.twig"
  362. */
  363. public function testRenderListElementErrorLoadingTemplate()
  364. {
  365. $this->admin->expects($this->once())
  366. ->method('getTemplate')
  367. ->with($this->equalTo('base_list_field'))
  368. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_nonexistent_field.html.twig'));
  369. $this->fieldDescription->expects($this->once())
  370. ->method('getTemplate')
  371. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  372. $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
  373. }
  374. /**
  375. * @dataProvider getRenderViewElementTests
  376. */
  377. public function testRenderViewElement($expected, $type, $value, array $options)
  378. {
  379. $this->admin->expects($this->any())
  380. ->method('getTemplate')
  381. ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
  382. $this->fieldDescription->expects($this->any())
  383. ->method('getValue')
  384. ->will($this->returnCallback(function () use ($value) {
  385. if ($value instanceof NoValueException) {
  386. throw $value;
  387. }
  388. return $value;
  389. }));
  390. $this->fieldDescription->expects($this->any())
  391. ->method('getType')
  392. ->will($this->returnValue($type));
  393. $this->fieldDescription->expects($this->any())
  394. ->method('getOptions')
  395. ->will($this->returnValue($options));
  396. $this->fieldDescription->expects($this->any())
  397. ->method('getTemplate')
  398. ->will($this->returnCallback(function () use ($type) {
  399. switch ($type) {
  400. case 'boolean':
  401. return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
  402. case 'datetime':
  403. return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
  404. case 'date':
  405. return 'SonataAdminBundle:CRUD:show_date.html.twig';
  406. case 'time':
  407. return 'SonataAdminBundle:CRUD:show_time.html.twig';
  408. case 'currency':
  409. return 'SonataAdminBundle:CRUD:show_currency.html.twig';
  410. case 'percent':
  411. return 'SonataAdminBundle:CRUD:show_percent.html.twig';
  412. case 'choice':
  413. return 'SonataAdminBundle:CRUD:show_choice.html.twig';
  414. case 'array':
  415. return 'SonataAdminBundle:CRUD:show_array.html.twig';
  416. case 'trans':
  417. return 'SonataAdminBundle:CRUD:show_trans.html.twig';
  418. case 'url':
  419. return 'SonataAdminBundle:CRUD:show_url.html.twig';
  420. case 'html':
  421. return 'SonataAdminBundle:CRUD:show_html.html.twig';
  422. default:
  423. return false;
  424. }
  425. }));
  426. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderViewElement($this->fieldDescription, $this->object))));
  427. }
  428. public function getRenderViewElementTests()
  429. {
  430. return array(
  431. array('<th>Data</th> <td>Example</td>', 'string', 'Example', array('safe' => false)),
  432. array('<th>Data</th> <td>Example</td>', 'text', 'Example', array('safe' => false)),
  433. array('<th>Data</th> <td>Example</td>', 'textarea', 'Example', array('safe' => false)),
  434. 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()),
  435. 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')),
  436. array('<th>Data</th> <td>December 24, 2013</td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  437. 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')),
  438. array('<th>Data</th> <td>10:11:12</td>', 'time', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  439. array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135, array('safe' => false)),
  440. array('<th>Data</th> <td>5678</td>', 'integer', 5678, array('safe' => false)),
  441. array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135, array()),
  442. array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  443. array('<th>Data</th> <td> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  444. array('<th>Data</th> <td> [1 => First] <br> [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second'), array('safe' => false)),
  445. array('<th>Data</th> <td> [1 => First] [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second'), array('safe' => false, 'inline' => true)),
  446. array('<th>Data</th> <td><i class="icon-ok-circle"></i>yes</td>', 'boolean', true, array()),
  447. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', false, array()),
  448. array('<th>Data</th> <td> Delete </td>', 'trans', 'action_delete', array('safe' => false, 'catalogue' => 'SonataAdminBundle')),
  449. array('<th>Data</th> <td>Status1</td>', 'choice', 'Status1', array('safe' => false)),
  450. array('<th>Data</th> <td>Alias1</td>', 'choice', 'Status1', array('safe' => false, 'choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3'))),
  451. array('<th>Data</th> <td>NoValidKeyInChoices</td>', 'choice', 'NoValidKeyInChoices', array('safe' => false, 'choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3'))),
  452. array('<th>Data</th> <td>Delete</td>', 'choice', 'Foo', array('safe' => false, 'catalogue' => 'SonataAdminBundle', 'choices' => array('Foo' => 'action_delete', 'Status2' => 'Alias2', 'Status3' => 'Alias3'))),
  453. array('<th>Data</th> <td>NoValidKeyInChoices</td>', 'choice', array('NoValidKeyInChoices'), array('safe' => false, 'choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3'), 'multiple' => true)),
  454. 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)),
  455. 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)),
  456. 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' => ' | ')),
  457. 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)),
  458. 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)),
  459. array('<th>Data</th> <td>&lt;b&gt;Alias1&lt;/b&gt;, &lt;b&gt;Alias3&lt;/b&gt;</td>', 'choice', array('Status1', 'Status3'), array('safe' => false, 'choices' => array('Status1' => '<b>Alias1</b>', 'Status2' => '<b>Alias2</b>', 'Status3' => '<b>Alias3</b>'), 'multiple' => true)),
  460. array('<th>Data</th> <td><a href="http://example.com">http://example.com</a></td>', 'url', 'http://example.com', array('safe' => false)),
  461. array('<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>', 'url', 'https://example.com', array('safe' => false)),
  462. array('<th>Data</th> <td><a href="http://example.com">example.com</a></td>', 'url', 'http://example.com', array('safe' => false, 'hide_protocol' => true)),
  463. array('<th>Data</th> <td><a href="https://example.com">example.com</a></td>', 'url', 'https://example.com', array('safe' => false, 'hide_protocol' => true)),
  464. 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)),
  465. 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)),
  466. array('<th>Data</th> <td><a href="http://example.com">Foo</a></td>', 'url', 'Foo', array('safe' => false, 'url' => 'http://example.com')),
  467. array('<th>Data</th> <td><a href="http://example.com">&lt;b&gt;Foo&lt;/b&gt;</a></td>', 'url', '<b>Foo</b>', array('safe' => false, 'url' => 'http://example.com')),
  468. 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')),
  469. array('<th>Data</th> <td><a href="/foo">Foo</a></td>', 'url', 'Foo', array('safe' => false, 'route' => array('name' => 'sonata_admin_foo'))),
  470. 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))),
  471. array('<th>Data</th> <td><a href="/foo">foo/bar?a=b&amp;c=123456789</a></td>', 'url', 'http://foo/bar?a=b&c=123456789', array('safe' => false, 'route' => array('name' => 'sonata_admin_foo'), 'hide_protocol' => true)),
  472. array('<th>Data</th> <td><a href="http://localhost/foo">foo/bar?a=b&amp;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)),
  473. 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')))),
  474. 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')))),
  475. 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'))),
  476. 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'))),
  477. array(
  478. '<th>Data</th> <td><p><strong>Creating a Template for the Field</strong> and form</p> </td>',
  479. 'html',
  480. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  481. array(),
  482. ),
  483. array(
  484. '<th>Data</th> <td>Creating a Template for the Field and form </td>',
  485. 'html',
  486. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  487. array('strip' => true),
  488. ),
  489. array(
  490. '<th>Data</th> <td> Creating a Template for the Fi... </td>',
  491. 'html',
  492. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  493. array('truncate' => true),
  494. ),
  495. array(
  496. '<th>Data</th> <td> Creating a... </td>',
  497. 'html',
  498. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  499. array('truncate' => array('length' => 10)),
  500. ),
  501. array(
  502. '<th>Data</th> <td> Creating a Template for the Field... </td>',
  503. 'html',
  504. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  505. array('truncate' => array('preserve' => true)),
  506. ),
  507. array(
  508. '<th>Data</th> <td> Creating a Template for the Fi etc. </td>',
  509. 'html',
  510. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  511. array('truncate' => array('separator' => ' etc.')),
  512. ),
  513. array(
  514. '<th>Data</th> <td> Creating a Template for[...] </td>',
  515. 'html',
  516. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  517. array(
  518. 'truncate' => array(
  519. 'length' => 20,
  520. 'preserve' => true,
  521. 'separator' => '[...]',
  522. ),
  523. ),
  524. ),
  525. // NoValueException
  526. array('<th>Data</th> <td></td>', 'string', new NoValueException(), array('safe' => false)),
  527. array('<th>Data</th> <td></td>', 'text', new NoValueException(), array('safe' => false)),
  528. array('<th>Data</th> <td></td>', 'textarea', new NoValueException(), array('safe' => false)),
  529. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array()),
  530. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array('format' => 'd.m.Y H:i:s')),
  531. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array()),
  532. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array('format' => 'd.m.Y')),
  533. array('<th>Data</th> <td>&nbsp;</td>', 'time', new NoValueException(), array()),
  534. array('<th>Data</th> <td></td>', 'number', new NoValueException(), array('safe' => false)),
  535. array('<th>Data</th> <td></td>', 'integer', new NoValueException(), array('safe' => false)),
  536. array('<th>Data</th> <td> 0 % </td>', 'percent', new NoValueException(), array()),
  537. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'EUR')),
  538. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'GBP')),
  539. array('<th>Data</th> <td> </td>', 'array', new NoValueException(), array('safe' => false)),
  540. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', new NoValueException(), array()),
  541. array('<th>Data</th> <td> </td>', 'trans', new NoValueException(), array('safe' => false, 'catalogue' => 'SonataAdminBundle')),
  542. array('<th>Data</th> <td></td>', 'choice', new NoValueException(), array('safe' => false, 'choices' => array())),
  543. array('<th>Data</th> <td></td>', 'choice', new NoValueException(), array('safe' => false, 'choices' => array(), 'multiple' => true)),
  544. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array()),
  545. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array('url' => 'http://example.com')),
  546. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array('route' => array('name' => 'sonata_admin_foo'))),
  547. );
  548. }
  549. public function testGetValueFromFieldDescription()
  550. {
  551. $object = new \stdClass();
  552. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  553. $fieldDescription->expects($this->any())
  554. ->method('getValue')
  555. ->will($this->returnValue('test123'));
  556. $this->assertEquals('test123', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  557. }
  558. public function testGetValueFromFieldDescriptionWithRemoveLoopException()
  559. {
  560. $object = $this->getMock('\ArrayAccess');
  561. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  562. try {
  563. $this->assertEquals('anything', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription, array('loop' => true)));
  564. } catch (\RuntimeException $e) {
  565. $this->assertContains('remove the loop requirement', $e->getMessage());
  566. return;
  567. }
  568. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  569. }
  570. public function testGetValueFromFieldDescriptionWithNoValueException()
  571. {
  572. $object = new \stdClass();
  573. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  574. $fieldDescription->expects($this->any())
  575. ->method('getValue')
  576. ->will($this->returnCallback(function () {
  577. throw new NoValueException();
  578. }));
  579. $fieldDescription->expects($this->any())
  580. ->method('getAssociationAdmin')
  581. ->will($this->returnValue(null));
  582. $this->assertEquals(null, $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  583. }
  584. public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInstance()
  585. {
  586. $object = new \stdClass();
  587. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  588. $fieldDescription->expects($this->any())
  589. ->method('getValue')
  590. ->will($this->returnCallback(function () {
  591. throw new NoValueException();
  592. }));
  593. $fieldDescription->expects($this->any())
  594. ->method('getAssociationAdmin')
  595. ->will($this->returnValue($this->admin));
  596. $this->admin->expects($this->once())
  597. ->method('getNewInstance')
  598. ->will($this->returnValue('foo'));
  599. $this->assertEquals('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  600. }
  601. public function testOutput()
  602. {
  603. $this->fieldDescription->expects($this->any())
  604. ->method('getTemplate')
  605. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  606. $this->fieldDescription->expects($this->any())
  607. ->method('getFieldName')
  608. ->will($this->returnValue('fd_name'));
  609. $this->environment->disableDebug();
  610. $parameters = array(
  611. 'admin' => $this->admin,
  612. 'value' => 'foo',
  613. 'field_description' => $this->fieldDescription,
  614. 'object' => $this->object,
  615. );
  616. $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
  617. $this->assertEquals('<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
  618. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  619. $this->environment->enableDebug();
  620. $this->assertEquals('<!-- 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 -->',
  621. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  622. }
  623. public function testRenderRelationElementNoObject()
  624. {
  625. $this->assertEquals('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
  626. }
  627. public function testRenderRelationElementToString()
  628. {
  629. $this->fieldDescription->expects($this->exactly(2))
  630. ->method('getOption')
  631. ->will($this->returnCallback(function ($value, $default = null) {
  632. if ($value == 'associated_property') {
  633. return $default;
  634. }
  635. if ($value == 'associated_tostring') {
  636. return '__toString';
  637. }
  638. }));
  639. $element = $this->getMock('stdClass', array('__toString'));
  640. $element->expects($this->any())
  641. ->method('__toString')
  642. ->will($this->returnValue('bar'));
  643. $this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  644. }
  645. public function testRenderRelationElementCustomToString()
  646. {
  647. $this->fieldDescription->expects($this->exactly(2))
  648. ->method('getOption')
  649. ->will($this->returnCallback(function ($value, $default = null) {
  650. if ($value == 'associated_property') {
  651. return $default;
  652. }
  653. if ($value == 'associated_tostring') {
  654. return 'customToString';
  655. }
  656. }));
  657. $element = $this->getMock('stdClass', array('customToString'));
  658. $element->expects($this->any())
  659. ->method('customToString')
  660. ->will($this->returnValue('fooBar'));
  661. $this->assertEquals('fooBar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  662. }
  663. public function testRenderRelationElementMethodNotExist()
  664. {
  665. $this->fieldDescription->expects($this->exactly(2))
  666. ->method('getOption')
  667. ->will($this->returnCallback(function ($value, $default = null) {
  668. if ($value == 'associated_tostring') {
  669. return 'nonExistedMethod';
  670. }
  671. }));
  672. $element = new \stdClass();
  673. try {
  674. $this->twigExtension->renderRelationElement($element, $this->fieldDescription);
  675. } catch (\RuntimeException $e) {
  676. $this->assertContains('You must define an `associated_property` option or create a `stdClass::__toString', $e->getMessage());
  677. return;
  678. }
  679. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  680. }
  681. public function testRenderRelationElementWithPropertyPath()
  682. {
  683. $this->fieldDescription->expects($this->exactly(1))
  684. ->method('getOption')
  685. ->will($this->returnCallback(function ($value, $default = null) {
  686. if ($value == 'associated_property') {
  687. return 'foo';
  688. }
  689. }));
  690. $element = new \stdClass();
  691. $element->foo = "bar";
  692. $this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  693. }
  694. public function testRenderRelationElementWithClosure()
  695. {
  696. $this->fieldDescription->expects($this->exactly(1))
  697. ->method('getOption')
  698. ->will($this->returnCallback(function ($value, $default = null) {
  699. if ($value == 'associated_property') {
  700. return function ($element) {
  701. return 'closure '.$element->foo;
  702. };
  703. }
  704. }));
  705. $element = new \stdClass();
  706. $element->foo = "bar";
  707. $this->assertEquals('closure bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  708. }
  709. public function testGetUrlsafeIdentifier()
  710. {
  711. $entity = new \stdClass();
  712. // set admin to pool
  713. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
  714. $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service')));
  715. $this->admin->expects($this->once())
  716. ->method('getUrlsafeIdentifier')
  717. ->with($this->equalTo($entity))
  718. ->will($this->returnValue(1234567));
  719. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($entity));
  720. }
  721. public function testGetUrlsafeIdentifier_GivenAdmin_Foo()
  722. {
  723. $entity = new \stdClass();
  724. // set admin to pool
  725. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
  726. $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service', 'sonata_admin_bar_service')));
  727. $this->admin->expects($this->once())
  728. ->method('getUrlsafeIdentifier')
  729. ->with($this->equalTo($entity))
  730. ->will($this->returnValue(1234567));
  731. $this->adminBar->expects($this->never())
  732. ->method('getUrlsafeIdentifier');
  733. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->admin));
  734. }
  735. public function testGetUrlsafeIdentifier_GivenAdmin_Bar()
  736. {
  737. $entity = new \stdClass();
  738. // set admin to pool
  739. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
  740. $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service', 'sonata_admin_bar_service')));
  741. $this->admin->expects($this->never())
  742. ->method('getUrlsafeIdentifier');
  743. $this->adminBar->expects($this->once())
  744. ->method('getUrlsafeIdentifier')
  745. ->with($this->equalTo($entity))
  746. ->will($this->returnValue(1234567));
  747. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->adminBar));
  748. }
  749. public function testGetKnpMenu()
  750. {
  751. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  752. $adminGroups = array(
  753. "bar" => array(
  754. "label" => "foo",
  755. "icon" => '<i class="fa fa-edit"></i>',
  756. "label_catalogue" => 'SonataAdminBundle',
  757. "items" => array(
  758. array(
  759. "admin" => "",
  760. "label" => "fooLabel",
  761. "route" => "FooRoute",
  762. "route_params" => array("foo" => "bar"),
  763. ),
  764. ),
  765. "item_adds" => array(),
  766. "roles" => array(),
  767. ),
  768. );
  769. $this->pool->setAdminGroups($adminGroups);
  770. $menu = $this->twigExtension->getKnpMenu($request);
  771. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  772. $this->assertArrayHasKey('bar', $menu->getChildren());
  773. foreach ($menu->getChildren() as $key => $child) {
  774. $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
  775. $this->assertEquals("bar", $child->getName());
  776. $this->assertEquals($adminGroups["bar"]["label"], $child->getLabel());
  777. // menu items
  778. $children = $child->getChildren();
  779. $this->assertCount(1, $children);
  780. $this->assertArrayHasKey('fooLabel', $children);
  781. $this->assertInstanceOf('Knp\Menu\MenuItem', $child['fooLabel']);
  782. $this->assertEquals('fooLabel', $child['fooLabel']->getLabel());
  783. }
  784. }
  785. public function testGetKnpMenuWithAdmin()
  786. {
  787. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  788. $adminGroups = array(
  789. 'bar' => array(
  790. 'label' => 'foo',
  791. 'icon' => '<i class="fa fa-edit"></i>',
  792. 'label_catalogue' => 'SonataAdminBundle',
  793. 'items' => array(
  794. array(
  795. 'admin' => 'sonata_admin_foo_service',
  796. 'label' => 'fooLabel',
  797. ),
  798. ),
  799. 'item_adds' => array(),
  800. 'roles' => array(),
  801. ),
  802. );
  803. $this->pool->setAdminGroups($adminGroups);
  804. $this->admin->expects($this->once())
  805. ->method('hasRoute')
  806. ->with($this->equalTo('list'))
  807. ->will($this->returnValue(true));
  808. $this->admin->expects($this->any())
  809. ->method('isGranted')
  810. ->with($this->equalTo('LIST'))
  811. ->will($this->returnValue(true));
  812. $this->admin->expects($this->once())
  813. ->method('getLabel')
  814. ->will($this->returnValue('foo_admin_label'));
  815. $menu = $this->twigExtension->getKnpMenu($request);
  816. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  817. $this->assertArrayHasKey('bar', $menu->getChildren());
  818. foreach ($menu->getChildren() as $key => $child) {
  819. $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
  820. $this->assertEquals('bar', $child->getName());
  821. $this->assertEquals($adminGroups['bar']['label'], $child->getLabel());
  822. // menu items
  823. $children = $child->getChildren();
  824. $this->assertCount(1, $children);
  825. $this->assertArrayHasKey('foo_admin_label', $children);
  826. $this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo_admin_label']);
  827. $this->assertEquals('foo_admin_label', $child['foo_admin_label']->getLabel());
  828. }
  829. }
  830. public function testGetKnpMenuWithNoListRoute()
  831. {
  832. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  833. $adminGroups = array(
  834. 'bar' => array(
  835. 'label' => 'foo',
  836. 'icon' => '<i class="fa fa-edit"></i>',
  837. 'label_catalogue' => 'SonataAdminBundle',
  838. 'items' => array(
  839. array(
  840. 'admin' => 'sonata_admin_foo_service',
  841. 'label' => 'fooLabel',
  842. ),
  843. ),
  844. 'item_adds' => array(),
  845. 'roles' => array(),
  846. ),
  847. );
  848. $this->pool->setAdminGroups($adminGroups);
  849. $this->admin->expects($this->once())
  850. ->method('hasRoute')
  851. ->with($this->equalTo('list'))
  852. ->will($this->returnValue(false));
  853. $menu = $this->twigExtension->getKnpMenu($request);
  854. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  855. $this->assertArrayNotHasKey('bar', $menu->getChildren());
  856. $this->assertCount(0, $menu->getChildren());
  857. }
  858. public function testGetKnpMenuWithNotGrantedList()
  859. {
  860. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  861. $adminGroups = array(
  862. 'bar' => array(
  863. 'label' => 'foo',
  864. 'icon' => '<i class="fa fa-edit"></i>',
  865. 'label_catalogue' => 'SonataAdminBundle',
  866. 'items' => array(
  867. array(
  868. 'admin' => 'sonata_admin_foo_service',
  869. 'label' => 'fooLabel',
  870. ),
  871. ),
  872. 'item_adds' => array(),
  873. 'roles' => array(),
  874. ),
  875. );
  876. $this->pool->setAdminGroups($adminGroups);
  877. $this->admin->expects($this->once())
  878. ->method('hasRoute')
  879. ->with($this->equalTo('list'))
  880. ->will($this->returnValue(true));
  881. $this->admin->expects($this->any())
  882. ->method('isGranted')
  883. ->with($this->equalTo('LIST'))
  884. ->will($this->returnValue(false));
  885. $menu = $this->twigExtension->getKnpMenu($request);
  886. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  887. $this->assertArrayNotHasKey('bar', $menu->getChildren());
  888. $this->assertCount(0, $menu->getChildren());
  889. }
  890. }