SonataAdminExtensionTest.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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\Twig\Extension\SonataAdminExtension;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  14. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  15. use Symfony\Component\Translation\Translator;
  16. use Symfony\Component\Translation\MessageSelector;
  17. use Symfony\Component\Translation\Loader\XliffFileLoader;
  18. use Symfony\Component\Routing\Generator\UrlGenerator;
  19. use Symfony\Component\Routing\Loader\XmlFileLoader;
  20. use Symfony\Component\Config\FileLocator;
  21. use Symfony\Bridge\Twig\Extension\RoutingExtension;
  22. use Symfony\Component\Routing\RequestContext;
  23. use Sonata\AdminBundle\Exception\NoValueException;
  24. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  25. use Sonata\AdminBundle\Admin\AdminInterface;
  26. use Psr\Log\LoggerInterface;
  27. /**
  28. * Test for SonataAdminExtension
  29. *
  30. * @author Andrej Hudec <pulzarraider@gmail.com>
  31. */
  32. class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * @var SonataAdminExtension
  36. */
  37. private $twigExtension;
  38. /**
  39. * @var Twig_Environment
  40. */
  41. private $environment;
  42. /**
  43. * @var AdminInterface
  44. */
  45. private $admin;
  46. /**
  47. * @var AdminInterface
  48. */
  49. private $adminBar;
  50. /**
  51. * @var FieldDescriptionInterface
  52. */
  53. private $fieldDescription;
  54. /**
  55. * @var \stdClass
  56. */
  57. private $object;
  58. /**
  59. * @var Pool
  60. */
  61. private $pool;
  62. /**
  63. * @var LoggerInterface
  64. */
  65. private $logger;
  66. public function setUp()
  67. {
  68. date_default_timezone_set('Europe/London');
  69. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  70. $this->pool = new Pool($container, '', '');
  71. $this->logger = $this->getMock('Psr\Log\LoggerInterface');
  72. $this->twigExtension = new SonataAdminExtension($this->pool, $this->logger);
  73. $loader = new StubFilesystemLoader(array(
  74. __DIR__.'/../../../Resources/views/CRUD',
  75. ));
  76. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
  77. $this->environment->addExtension($this->twigExtension);
  78. // translation extension
  79. $translator = new Translator('en', new MessageSelector());
  80. $translator->addLoader('xlf', new XliffFileLoader());
  81. $translator->addResource('xlf', __DIR__.'/../../../Resources/translations/SonataAdminBundle.en.xliff', 'en', 'SonataAdminBundle');
  82. $this->environment->addExtension(new TranslationExtension($translator));
  83. // routing extension
  84. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../../Resources/config/routing')));
  85. $routeCollection = $xmlFileLoader->load('sonata_admin.xml');
  86. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../Fixtures/Resources/config/routing')));
  87. $testRouteCollection = $xmlFileLoader->load('routing.xml');
  88. $routeCollection->addCollection($testRouteCollection);
  89. $requestContext = new RequestContext();
  90. $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
  91. $this->environment->addExtension(new RoutingExtension($urlGenerator));
  92. $this->twigExtension->initRuntime($this->environment);
  93. // initialize object
  94. $this->object = new \stdClass();
  95. // initialize admin
  96. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  97. $this->admin->expects($this->any())
  98. ->method('isGranted')
  99. ->will($this->returnValue(true));
  100. $this->admin->expects($this->any())
  101. ->method('getCode')
  102. ->will($this->returnValue('xyz'));
  103. $this->admin->expects($this->any())
  104. ->method('id')
  105. ->with($this->equalTo($this->object))
  106. ->will($this->returnValue(12345));
  107. $this->admin->expects($this->any())
  108. ->method('getNormalizedIdentifier')
  109. ->with($this->equalTo($this->object))
  110. ->will($this->returnValue(12345));
  111. $this->admin->expects($this->any())
  112. ->method('trans')
  113. ->will($this->returnCallback(function ($id) {
  114. return $id;
  115. }));
  116. $this->adminBar = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  117. $this->adminBar->expects($this->any())
  118. ->method('isGranted')
  119. ->will($this->returnValue(true));
  120. $this->adminBar->expects($this->any())
  121. ->method('getNormalizedIdentifier')
  122. ->with($this->equalTo($this->object))
  123. ->will($this->returnValue(12345));
  124. // for php5.3 BC
  125. $admin = $this->admin;
  126. $adminBar = $this->adminBar;
  127. $container->expects($this->any())
  128. ->method('get')
  129. ->will($this->returnCallback(function ($id) use ($admin, $adminBar) {
  130. if ($id == 'sonata_admin_foo_service') {
  131. return $admin;
  132. } elseif ($id == 'sonata_admin_bar_service') {
  133. return $adminBar;
  134. }
  135. return null;
  136. }));
  137. // initialize field description
  138. $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  139. $this->fieldDescription->expects($this->any())
  140. ->method('getName')
  141. ->will($this->returnValue('fd_name'));
  142. $this->fieldDescription->expects($this->any())
  143. ->method('getAdmin')
  144. ->will($this->returnValue($this->admin));
  145. $this->fieldDescription->expects($this->any())
  146. ->method('getLabel')
  147. ->will($this->returnValue('Data'));
  148. }
  149. /**
  150. * @dataProvider getRenderListElementTests
  151. */
  152. public function testRenderListElement($expected, $type, $value, array $options)
  153. {
  154. $this->admin->expects($this->any())
  155. ->method('getTemplate')
  156. ->with($this->equalTo('base_list_field'))
  157. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  158. $this->fieldDescription->expects($this->any())
  159. ->method('getValue')
  160. ->will($this->returnValue($value));
  161. $this->fieldDescription->expects($this->any())
  162. ->method('getType')
  163. ->will($this->returnValue($type));
  164. $this->fieldDescription->expects($this->any())
  165. ->method('getOptions')
  166. ->will($this->returnValue($options));
  167. $this->fieldDescription->expects($this->any())
  168. ->method('getTemplate')
  169. ->will($this->returnCallback(function () use ($type) {
  170. switch ($type) {
  171. case 'string':
  172. return 'SonataAdminBundle:CRUD:list_string.html.twig';
  173. case 'boolean':
  174. return 'SonataAdminBundle:CRUD:list_boolean.html.twig';
  175. case 'datetime':
  176. return 'SonataAdminBundle:CRUD:list_datetime.html.twig';
  177. case 'date':
  178. return 'SonataAdminBundle:CRUD:list_date.html.twig';
  179. case 'time':
  180. return 'SonataAdminBundle:CRUD:list_time.html.twig';
  181. case 'currency':
  182. return 'SonataAdminBundle:CRUD:list_currency.html.twig';
  183. case 'percent':
  184. return 'SonataAdminBundle:CRUD:list_percent.html.twig';
  185. case 'choice':
  186. return 'SonataAdminBundle:CRUD:list_choice.html.twig';
  187. case 'array':
  188. return 'SonataAdminBundle:CRUD:list_array.html.twig';
  189. case 'trans':
  190. return 'SonataAdminBundle:CRUD:list_trans.html.twig';
  191. case 'url':
  192. return 'SonataAdminBundle:CRUD:list_url.html.twig';
  193. case 'nonexistent':
  194. // template doesn`t exist
  195. return 'SonataAdminBundle:CRUD:list_nonexistent_template.html.twig';
  196. default:
  197. return false;
  198. }
  199. }));
  200. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($this->object, $this->fieldDescription))));
  201. }
  202. public function getRenderListElementTests()
  203. {
  204. return array(
  205. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>', 'string', 'Example', array()),
  206. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> </td>', 'string', null, array()),
  207. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> Example </td>', 'nonexistent', 'Example', array()),
  208. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> </td>', 'nonexistent', null, array()),
  209. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>', 'text', 'Example', array()),
  210. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> </td>', 'text', null, array()),
  211. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example', array()),
  212. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>', 'textarea', null, array()),
  213. 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()),
  214. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array()),
  215. 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')),
  216. 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')),
  217. 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()),
  218. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array()),
  219. 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')),
  220. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array('format'=>'d.m.Y')),
  221. 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()),
  222. array('<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> &nbsp; </td>', 'time', null, array()),
  223. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>', 'number', 10.746135, array()),
  224. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> </td>', 'number', null, array()),
  225. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>', 'integer', 5678, array()),
  226. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> </td>', 'integer', null, array()),
  227. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>', 'percent', 10.746135, array()),
  228. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 0 % </td>', 'percent', null, array()),
  229. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  230. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'EUR')),
  231. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  232. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'GBP')),
  233. 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()),
  234. array('<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> </td>', 'array', null, array()),
  235. 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)),
  236. 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)),
  237. 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)),
  238. 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)),
  239. 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)),
  240. 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)),
  241. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>', 'trans', 'action_delete', array('catalogue'=>'SonataAdminBundle')),
  242. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> </td>', 'trans', null, array('catalogue'=>'SonataAdminBundle')),
  243. array('<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>', 'choice', 'Status1', array()),
  244. array('<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>', 'choice', array('Status1'), array('choices'=>array(), 'multiple'=>true)),
  245. 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'))),
  246. 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'))),
  247. 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'))),
  248. 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'))),
  249. 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)),
  250. 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'=>' | ')),
  251. 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)),
  252. 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)),
  253. 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)),
  254. 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)),
  255. 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)),
  256. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>', 'url', null, array()),
  257. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>', 'url', null, array('url'=>'http://example.com')),
  258. 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'))),
  259. 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()),
  260. 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()),
  261. 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)),
  262. 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)),
  263. 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)),
  264. 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)),
  265. 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')),
  266. 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')),
  267. 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'))),
  268. 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))),
  269. 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)),
  270. 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)),
  271. 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')))),
  272. 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')))),
  273. 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'))),
  274. 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'))),
  275. );
  276. }
  277. public function testRenderListElementNonExistentTemplate()
  278. {
  279. $this->admin->expects($this->once())
  280. ->method('getTemplate')
  281. ->with($this->equalTo('base_list_field'))
  282. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  283. $this->fieldDescription->expects($this->once())
  284. ->method('getValue')
  285. ->will($this->returnValue('Foo'));
  286. $this->fieldDescription->expects($this->once())
  287. ->method('getFieldName')
  288. ->will($this->returnValue('Foo_name'));
  289. $this->fieldDescription->expects($this->exactly(2))
  290. ->method('getType')
  291. ->will($this->returnValue('nonexistent'));
  292. $this->fieldDescription->expects($this->once())
  293. ->method('getTemplate')
  294. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  295. $this->logger->expects($this->once())
  296. ->method('warning')
  297. ->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')));
  298. $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
  299. }
  300. /**
  301. * @expectedException Twig_Error_Loader
  302. * @expectedExceptionMessage Unable to find template "base_list_nonexistent_field.html.twig"
  303. */
  304. public function testRenderListElementErrorLoadingTemplate()
  305. {
  306. $this->admin->expects($this->once())
  307. ->method('getTemplate')
  308. ->with($this->equalTo('base_list_field'))
  309. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_nonexistent_field.html.twig'));
  310. $this->fieldDescription->expects($this->once())
  311. ->method('getTemplate')
  312. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  313. $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
  314. }
  315. /**
  316. * @dataProvider getRenderViewElementTests
  317. */
  318. public function testRenderViewElement($expected, $type, $value, array $options)
  319. {
  320. $this->admin->expects($this->any())
  321. ->method('getTemplate')
  322. ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
  323. $this->fieldDescription->expects($this->any())
  324. ->method('getValue')
  325. ->will($this->returnCallback(function () use ($value) {
  326. if ($value instanceof NoValueException) {
  327. throw $value;
  328. }
  329. return $value;
  330. }));
  331. $this->fieldDescription->expects($this->any())
  332. ->method('getType')
  333. ->will($this->returnValue($type));
  334. $this->fieldDescription->expects($this->any())
  335. ->method('getOptions')
  336. ->will($this->returnValue($options));
  337. $this->fieldDescription->expects($this->any())
  338. ->method('getTemplate')
  339. ->will($this->returnCallback(function () use ($type) {
  340. switch ($type) {
  341. case 'boolean':
  342. return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
  343. case 'datetime':
  344. return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
  345. case 'date':
  346. return 'SonataAdminBundle:CRUD:show_date.html.twig';
  347. case 'time':
  348. return 'SonataAdminBundle:CRUD:show_time.html.twig';
  349. case 'currency':
  350. return 'SonataAdminBundle:CRUD:show_currency.html.twig';
  351. case 'percent':
  352. return 'SonataAdminBundle:CRUD:show_percent.html.twig';
  353. case 'choice':
  354. return 'SonataAdminBundle:CRUD:show_choice.html.twig';
  355. case 'array':
  356. return 'SonataAdminBundle:CRUD:show_array.html.twig';
  357. case 'trans':
  358. return 'SonataAdminBundle:CRUD:show_trans.html.twig';
  359. case 'url':
  360. return 'SonataAdminBundle:CRUD:show_url.html.twig';
  361. default:
  362. return false;
  363. }
  364. }));
  365. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderViewElement($this->fieldDescription, $this->object))));
  366. }
  367. public function getRenderViewElementTests()
  368. {
  369. return array(
  370. array('<th>Data</th> <td>Example</td>', 'string', 'Example', array('safe' => false)),
  371. array('<th>Data</th> <td>Example</td>', 'text', 'Example', array('safe' => false)),
  372. array('<th>Data</th> <td>Example</td>', 'textarea', 'Example', array('safe' => false)),
  373. 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()),
  374. 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')),
  375. array('<th>Data</th> <td>December 24, 2013</td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  376. 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')),
  377. array('<th>Data</th> <td>10:11:12</td>', 'time', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  378. array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135, array('safe' => false)),
  379. array('<th>Data</th> <td>5678</td>', 'integer', 5678, array('safe' => false)),
  380. array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135, array()),
  381. array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  382. array('<th>Data</th> <td> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  383. array('<th>Data</th> <td> [1 => First] [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second'), array('safe' => false)),
  384. array('<th>Data</th> <td><i class="icon-ok-circle"></i>yes</td>', 'boolean', true, array()),
  385. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', false, array()),
  386. array('<th>Data</th> <td> Delete </td>', 'trans', 'action_delete', array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  387. array('<th>Data</th> <td>Status1</td>', 'choice', 'Status1', array('safe'=>false)),
  388. array('<th>Data</th> <td>Alias1</td>', 'choice', 'Status1', array('safe'=>false, 'choices'=>array('Status1'=>'Alias1', 'Status2'=>'Alias2', 'Status3'=>'Alias3'))),
  389. array('<th>Data</th> <td>NoValidKeyInChoices</td>', 'choice', 'NoValidKeyInChoices', array('safe'=>false, 'choices'=>array('Status1'=>'Alias1', 'Status2'=>'Alias2', 'Status3'=>'Alias3'))),
  390. array('<th>Data</th> <td>Delete</td>', 'choice', 'Foo', array('safe'=>false, 'catalogue'=>'SonataAdminBundle', 'choices'=>array('Foo'=>'action_delete', 'Status2'=>'Alias2', 'Status3'=>'Alias3'))),
  391. array('<th>Data</th> <td>NoValidKeyInChoices</td>', 'choice', array('NoValidKeyInChoices'), array('safe'=>false, 'choices'=>array('Status1'=>'Alias1', 'Status2'=>'Alias2', 'Status3'=>'Alias3'), 'multiple'=>true,)),
  392. 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,)),
  393. 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,)),
  394. 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'=>' | ')),
  395. 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,)),
  396. 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,)),
  397. 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,)),
  398. array('<th>Data</th> <td><a href="http://example.com">http://example.com</a></td>', 'url', 'http://example.com', array('safe'=>false)),
  399. array('<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>', 'url', 'https://example.com', array('safe'=>false)),
  400. array('<th>Data</th> <td><a href="http://example.com">example.com</a></td>', 'url', 'http://example.com', array('safe'=>false, 'hide_protocol'=>true)),
  401. array('<th>Data</th> <td><a href="https://example.com">example.com</a></td>', 'url', 'https://example.com', array('safe'=>false, 'hide_protocol'=>true)),
  402. 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)),
  403. 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)),
  404. array('<th>Data</th> <td><a href="http://example.com">Foo</a></td>', 'url', 'Foo', array('safe'=>false, 'url'=>'http://example.com')),
  405. 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')),
  406. 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')),
  407. array('<th>Data</th> <td><a href="/foo">Foo</a></td>', 'url', 'Foo', array('safe'=>false, 'route'=>array('name'=>'sonata_admin_foo'))),
  408. 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))),
  409. 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)),
  410. 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)),
  411. 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')))),
  412. 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')))),
  413. 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'))),
  414. 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'))),
  415. // NoValueException
  416. array('<th>Data</th> <td></td>', 'string', new NoValueException(), array('safe' => false)),
  417. array('<th>Data</th> <td></td>', 'text', new NoValueException(), array('safe' => false)),
  418. array('<th>Data</th> <td></td>', 'textarea', new NoValueException(), array('safe' => false)),
  419. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array()),
  420. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array('format'=>'d.m.Y H:i:s')),
  421. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array()),
  422. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array('format'=>'d.m.Y')),
  423. array('<th>Data</th> <td>&nbsp;</td>', 'time', new NoValueException(), array()),
  424. array('<th>Data</th> <td></td>', 'number', new NoValueException(), array('safe' => false)),
  425. array('<th>Data</th> <td></td>', 'integer', new NoValueException(), array('safe' => false)),
  426. array('<th>Data</th> <td> 0 % </td>', 'percent', new NoValueException(), array()),
  427. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'EUR')),
  428. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'GBP')),
  429. array('<th>Data</th> <td> </td>', 'array', new NoValueException(), array('safe' => false)),
  430. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', new NoValueException(), array()),
  431. array('<th>Data</th> <td> </td>', 'trans', new NoValueException(), array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  432. array('<th>Data</th> <td></td>', 'choice', new NoValueException(), array('safe'=>false, 'choices'=>array())),
  433. array('<th>Data</th> <td></td>', 'choice', new NoValueException(), array('safe'=>false, 'choices'=>array(), 'multiple'=>true)),
  434. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array()),
  435. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array('url'=>'http://example.com')),
  436. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array('route'=>array('name'=>'sonata_admin_foo'))),
  437. );
  438. }
  439. public function testGetValueFromFieldDescription()
  440. {
  441. $object = new \stdClass();
  442. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  443. $fieldDescription->expects($this->any())
  444. ->method('getValue')
  445. ->will($this->returnValue('test123'));
  446. $this->assertEquals('test123', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  447. }
  448. public function testGetValueFromFieldDescriptionWithRemoveLoopException()
  449. {
  450. $object = $this->getMock('\ArrayAccess');
  451. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  452. try {
  453. $this->assertEquals('anything', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription, array('loop'=>true)));
  454. } catch (\RuntimeException $e) {
  455. $this->assertContains('remove the loop requirement', $e->getMessage());
  456. return;
  457. }
  458. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  459. }
  460. public function testGetValueFromFieldDescriptionWithNoValueException()
  461. {
  462. $object = new \stdClass();
  463. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  464. $fieldDescription->expects($this->any())
  465. ->method('getValue')
  466. ->will($this->returnCallback(function () {
  467. throw new NoValueException();
  468. }));
  469. $fieldDescription->expects($this->any())
  470. ->method('getAssociationAdmin')
  471. ->will($this->returnValue(null));
  472. $this->assertEquals(null, $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  473. }
  474. public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInstance()
  475. {
  476. $object = new \stdClass();
  477. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  478. $fieldDescription->expects($this->any())
  479. ->method('getValue')
  480. ->will($this->returnCallback(function () {
  481. throw new NoValueException();
  482. }));
  483. $fieldDescription->expects($this->any())
  484. ->method('getAssociationAdmin')
  485. ->will($this->returnValue($this->admin));
  486. $this->admin->expects($this->once())
  487. ->method('getNewInstance')
  488. ->will($this->returnValue('foo'));
  489. $this->assertEquals('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  490. }
  491. public function testOutput()
  492. {
  493. $this->fieldDescription->expects($this->any())
  494. ->method('getTemplate')
  495. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  496. $this->fieldDescription->expects($this->any())
  497. ->method('getFieldName')
  498. ->will($this->returnValue('fd_name'));
  499. $this->environment->disableDebug();
  500. $parameters = array(
  501. 'admin' => $this->admin,
  502. 'value' => 'foo',
  503. 'field_description' => $this->fieldDescription,
  504. 'object' => $this->object,
  505. );
  506. $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
  507. $this->assertEquals('<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
  508. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  509. $this->environment->enableDebug();
  510. $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 -->',
  511. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  512. }
  513. public function testRenderRelationElementNoObject()
  514. {
  515. $this->assertEquals('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
  516. }
  517. public function testRenderRelationElementToString()
  518. {
  519. $this->fieldDescription->expects($this->exactly(2))
  520. ->method('getOption')
  521. ->will($this->returnCallback(function ($value, $default = null) {
  522. if ($value == 'associated_property') {
  523. return $default;
  524. }
  525. if ($value == 'associated_tostring') {
  526. return '__toString';
  527. }
  528. }));
  529. $element = $this->getMock('stdClass', array('__toString'));
  530. $element->expects($this->any())
  531. ->method('__toString')
  532. ->will($this->returnValue('bar'));
  533. $this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  534. }
  535. public function testRenderRelationElementCustomToString()
  536. {
  537. $this->fieldDescription->expects($this->exactly(2))
  538. ->method('getOption')
  539. ->will($this->returnCallback(function ($value, $default = null) {
  540. if ($value == 'associated_property') {
  541. return $default;
  542. }
  543. if ($value == 'associated_tostring') {
  544. return 'customToString';
  545. }
  546. }));
  547. $element = $this->getMock('stdClass', array('customToString'));
  548. $element->expects($this->any())
  549. ->method('customToString')
  550. ->will($this->returnValue('fooBar'));
  551. $this->assertEquals('fooBar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  552. }
  553. public function testRenderRelationElementMethodNotExist()
  554. {
  555. $this->fieldDescription->expects($this->exactly(2))
  556. ->method('getOption')
  557. ->will($this->returnCallback(function ($value, $default = null) {
  558. if ($value == 'associated_tostring') {
  559. return 'nonExistedMethod';
  560. }
  561. }));
  562. $element = new \stdClass();
  563. try {
  564. $this->twigExtension->renderRelationElement($element, $this->fieldDescription);
  565. } catch (\RuntimeException $e) {
  566. $this->assertContains('You must define an `associated_property` option or create a `stdClass::__toString', $e->getMessage());
  567. return;
  568. }
  569. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  570. }
  571. public function testRenderRelationElementWithPropertyPath()
  572. {
  573. $this->fieldDescription->expects($this->exactly(1))
  574. ->method('getOption')
  575. ->will($this->returnCallback(function ($value, $default = null) {
  576. if ($value == 'associated_property') {
  577. return 'foo';
  578. }
  579. }));
  580. $element = new \stdClass();
  581. $element->foo = "bar";
  582. $this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  583. }
  584. public function testGetUrlsafeIdentifier()
  585. {
  586. $entity = new \stdClass();
  587. // set admin to pool
  588. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
  589. $this->pool->setAdminClasses(array('stdClass'=> array('sonata_admin_foo_service')));
  590. $this->admin->expects($this->once())
  591. ->method('getUrlsafeIdentifier')
  592. ->with($this->equalTo($entity))
  593. ->will($this->returnValue(1234567));
  594. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($entity));
  595. }
  596. public function testGetUrlsafeIdentifier_GivenAdmin_Foo()
  597. {
  598. $entity = new \stdClass();
  599. // set admin to pool
  600. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
  601. $this->pool->setAdminClasses(array('stdClass'=> array('sonata_admin_foo_service', 'sonata_admin_bar_service')));
  602. $this->admin->expects($this->once())
  603. ->method('getUrlsafeIdentifier')
  604. ->with($this->equalTo($entity))
  605. ->will($this->returnValue(1234567));
  606. $this->adminBar->expects($this->never())
  607. ->method('getUrlsafeIdentifier');
  608. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->admin));
  609. }
  610. public function testGetUrlsafeIdentifier_GivenAdmin_Bar()
  611. {
  612. $entity = new \stdClass();
  613. // set admin to pool
  614. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
  615. $this->pool->setAdminClasses(array('stdClass'=> array('sonata_admin_foo_service', 'sonata_admin_bar_service')));
  616. $this->admin->expects($this->never())
  617. ->method('getUrlsafeIdentifier');
  618. $this->adminBar->expects($this->once())
  619. ->method('getUrlsafeIdentifier')
  620. ->with($this->equalTo($entity))
  621. ->will($this->returnValue(1234567));
  622. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->adminBar));
  623. }
  624. }