SonataAdminExtensionTest.php 50 KB

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