SonataAdminExtensionTest.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Twig\Extension;
  11. use Psr\Log\LoggerInterface;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. use Sonata\AdminBundle\Exception\NoValueException;
  16. use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
  17. use Symfony\Bridge\Twig\Extension\RoutingExtension;
  18. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  19. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  20. use Symfony\Component\Config\FileLocator;
  21. use Symfony\Component\Routing\Generator\UrlGenerator;
  22. use Symfony\Component\Routing\Loader\XmlFileLoader;
  23. use Symfony\Component\Routing\RequestContext;
  24. use Symfony\Component\Translation\Loader\XliffFileLoader;
  25. use Symfony\Component\Translation\MessageSelector;
  26. use Symfony\Component\Translation\Translator;
  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->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
  72. $this->pool->setAdminClasses(array('fooClass' => array('sonata_admin_foo_service')));
  73. $this->logger = $this->getMock('Psr\Log\LoggerInterface');
  74. $this->twigExtension = new SonataAdminExtension($this->pool, $this->logger);
  75. $loader = new StubFilesystemLoader(array(
  76. __DIR__.'/../../../Resources/views/CRUD',
  77. ));
  78. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0));
  79. $this->environment->addExtension($this->twigExtension);
  80. // translation extension
  81. $translator = new Translator('en', new MessageSelector());
  82. $translator->addLoader('xlf', new XliffFileLoader());
  83. $translator->addResource('xlf', __DIR__.'/../../../Resources/translations/SonataAdminBundle.en.xliff', 'en', 'SonataAdminBundle');
  84. $this->environment->addExtension(new TranslationExtension($translator));
  85. // routing extension
  86. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../../Resources/config/routing')));
  87. $routeCollection = $xmlFileLoader->load('sonata_admin.xml');
  88. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../Fixtures/Resources/config/routing')));
  89. $testRouteCollection = $xmlFileLoader->load('routing.xml');
  90. $routeCollection->addCollection($testRouteCollection);
  91. $requestContext = new RequestContext();
  92. $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
  93. $this->environment->addExtension(new RoutingExtension($urlGenerator));
  94. $this->environment->addExtension(new \Twig_Extensions_Extension_Text());
  95. $this->twigExtension->initRuntime($this->environment);
  96. // initialize object
  97. $this->object = new \stdClass();
  98. // initialize admin
  99. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  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;
  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('isGranted')
  156. ->will($this->returnValue(true));
  157. $this->admin->expects($this->any())
  158. ->method('getTemplate')
  159. ->with($this->equalTo('base_list_field'))
  160. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  161. $this->fieldDescription->expects($this->any())
  162. ->method('getValue')
  163. ->will($this->returnValue($value));
  164. $this->fieldDescription->expects($this->any())
  165. ->method('getType')
  166. ->will($this->returnValue($type));
  167. $this->fieldDescription->expects($this->any())
  168. ->method('getOptions')
  169. ->will($this->returnValue($options));
  170. $this->fieldDescription->expects($this->any())
  171. ->method('getTemplate')
  172. ->will($this->returnCallback(function () use ($type) {
  173. switch ($type) {
  174. case 'string':
  175. return 'SonataAdminBundle:CRUD:list_string.html.twig';
  176. case 'boolean':
  177. return 'SonataAdminBundle:CRUD:list_boolean.html.twig';
  178. case 'datetime':
  179. return 'SonataAdminBundle:CRUD:list_datetime.html.twig';
  180. case 'date':
  181. return 'SonataAdminBundle:CRUD:list_date.html.twig';
  182. case 'time':
  183. return 'SonataAdminBundle:CRUD:list_time.html.twig';
  184. case 'currency':
  185. return 'SonataAdminBundle:CRUD:list_currency.html.twig';
  186. case 'percent':
  187. return 'SonataAdminBundle:CRUD:list_percent.html.twig';
  188. case 'choice':
  189. return 'SonataAdminBundle:CRUD:list_choice.html.twig';
  190. case 'array':
  191. return 'SonataAdminBundle:CRUD:list_array.html.twig';
  192. case 'trans':
  193. return 'SonataAdminBundle:CRUD:list_trans.html.twig';
  194. case 'url':
  195. return 'SonataAdminBundle:CRUD:list_url.html.twig';
  196. case 'html':
  197. return 'SonataAdminBundle:CRUD:list_html.html.twig';
  198. case 'nonexistent':
  199. // template doesn`t exist
  200. return 'SonataAdminBundle:CRUD:list_nonexistent_template.html.twig';
  201. default:
  202. return false;
  203. }
  204. }));
  205. $this->assertSame($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($this->object, $this->fieldDescription))));
  206. }
  207. public function getRenderListElementTests()
  208. {
  209. return array(
  210. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>', 'string', 'Example', array()),
  211. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> </td>', 'string', null, array()),
  212. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> Example </td>', 'nonexistent', 'Example', array()),
  213. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> </td>', 'nonexistent', null, array()),
  214. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>', 'text', 'Example', array()),
  215. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> </td>', 'text', null, array()),
  216. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example', array()),
  217. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>', 'textarea', null, array()),
  218. 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()),
  219. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array()),
  220. 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')),
  221. 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')),
  222. 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()),
  223. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array()),
  224. 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')),
  225. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array('format' => 'd.m.Y')),
  226. 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()),
  227. array('<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> &nbsp; </td>', 'time', null, array()),
  228. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>', 'number', 10.746135, array()),
  229. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> </td>', 'number', null, array()),
  230. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>', 'integer', 5678, array()),
  231. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> </td>', 'integer', null, array()),
  232. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>', 'percent', 10.746135, array()),
  233. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 0 % </td>', 'percent', null, array()),
  234. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  235. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'EUR')),
  236. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  237. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'GBP')),
  238. 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()),
  239. array('<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> </td>', 'array', null, array()),
  240. 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)),
  241. 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)),
  242. 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)),
  243. 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)),
  244. 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)),
  245. 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)),
  246. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>', 'trans', 'action_delete', array('catalogue' => 'SonataAdminBundle')),
  247. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> </td>', 'trans', null, array('catalogue' => 'SonataAdminBundle')),
  248. array('<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>', 'choice', 'Status1', array()),
  249. array('<td class="sonata-ba-list-field sonata-ba-list-field-choice" objectId="12345"> Status1 </td>', 'choice', array('Status1'), array('choices' => array(), 'multiple' => true)),
  250. 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'))),
  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'))),
  252. 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'))),
  253. 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'))),
  254. 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)),
  255. 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' => ' | ')),
  256. 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)),
  257. 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)),
  258. 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)),
  259. 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)),
  260. 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)),
  261. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>', 'url', null, array()),
  262. array('<td class="sonata-ba-list-field sonata-ba-list-field-url" objectId="12345"> &nbsp; </td>', 'url', null, array('url' => 'http://example.com')),
  263. 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'))),
  264. 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()),
  265. 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()),
  266. 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)),
  267. 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)),
  268. 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)),
  269. 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)),
  270. 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')),
  271. 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')),
  272. 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'))),
  273. 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))),
  274. 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)),
  275. 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)),
  276. 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')))),
  277. 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')))),
  278. 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'))),
  279. 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'))),
  280. array(
  281. '<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>',
  282. 'html',
  283. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  284. array(),
  285. ),
  286. array(
  287. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Field and form </td>',
  288. 'html',
  289. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  290. array('strip' => true),
  291. ),
  292. array(
  293. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Fi... </td>',
  294. 'html',
  295. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  296. array('truncate' => true),
  297. ),
  298. array(
  299. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a... </td>',
  300. 'html',
  301. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  302. array('truncate' => array('length' => 10)),
  303. ),
  304. array(
  305. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Field... </td>',
  306. 'html',
  307. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  308. array('truncate' => array('preserve' => true)),
  309. ),
  310. array(
  311. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for the Fi etc. </td>',
  312. 'html',
  313. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  314. array('truncate' => array('separator' => ' etc.')),
  315. ),
  316. array(
  317. '<td class="sonata-ba-list-field sonata-ba-list-field-html" objectId="12345"> Creating a Template for[...] </td>',
  318. 'html',
  319. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  320. array(
  321. 'truncate' => array(
  322. 'length' => 20,
  323. 'preserve' => true,
  324. 'separator' => '[...]',
  325. ),
  326. ),
  327. ),
  328. );
  329. }
  330. public function testRenderListElementNonExistentTemplate()
  331. {
  332. $this->admin->expects($this->once())
  333. ->method('getTemplate')
  334. ->with($this->equalTo('base_list_field'))
  335. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  336. $this->fieldDescription->expects($this->once())
  337. ->method('getValue')
  338. ->will($this->returnValue('Foo'));
  339. $this->fieldDescription->expects($this->once())
  340. ->method('getFieldName')
  341. ->will($this->returnValue('Foo_name'));
  342. $this->fieldDescription->expects($this->exactly(2))
  343. ->method('getType')
  344. ->will($this->returnValue('nonexistent'));
  345. $this->fieldDescription->expects($this->once())
  346. ->method('getTemplate')
  347. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  348. $this->logger->expects($this->once())
  349. ->method('warning')
  350. ->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')));
  351. $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
  352. }
  353. /**
  354. * @expectedException Twig_Error_Loader
  355. * @expectedExceptionMessage Unable to find template "base_list_nonexistent_field.html.twig"
  356. */
  357. public function testRenderListElementErrorLoadingTemplate()
  358. {
  359. $this->admin->expects($this->once())
  360. ->method('getTemplate')
  361. ->with($this->equalTo('base_list_field'))
  362. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_nonexistent_field.html.twig'));
  363. $this->fieldDescription->expects($this->once())
  364. ->method('getTemplate')
  365. ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
  366. $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
  367. }
  368. /**
  369. * @dataProvider getRenderViewElementTests
  370. */
  371. public function testRenderViewElement($expected, $type, $value, array $options)
  372. {
  373. $this->admin->expects($this->any())
  374. ->method('getTemplate')
  375. ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
  376. $this->fieldDescription->expects($this->any())
  377. ->method('getValue')
  378. ->will($this->returnCallback(function () use ($value) {
  379. if ($value instanceof NoValueException) {
  380. throw $value;
  381. }
  382. return $value;
  383. }));
  384. $this->fieldDescription->expects($this->any())
  385. ->method('getType')
  386. ->will($this->returnValue($type));
  387. $this->fieldDescription->expects($this->any())
  388. ->method('getOptions')
  389. ->will($this->returnValue($options));
  390. $this->fieldDescription->expects($this->any())
  391. ->method('getTemplate')
  392. ->will($this->returnCallback(function () use ($type) {
  393. switch ($type) {
  394. case 'boolean':
  395. return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
  396. case 'datetime':
  397. return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
  398. case 'date':
  399. return 'SonataAdminBundle:CRUD:show_date.html.twig';
  400. case 'time':
  401. return 'SonataAdminBundle:CRUD:show_time.html.twig';
  402. case 'currency':
  403. return 'SonataAdminBundle:CRUD:show_currency.html.twig';
  404. case 'percent':
  405. return 'SonataAdminBundle:CRUD:show_percent.html.twig';
  406. case 'choice':
  407. return 'SonataAdminBundle:CRUD:show_choice.html.twig';
  408. case 'array':
  409. return 'SonataAdminBundle:CRUD:show_array.html.twig';
  410. case 'trans':
  411. return 'SonataAdminBundle:CRUD:show_trans.html.twig';
  412. case 'url':
  413. return 'SonataAdminBundle:CRUD:show_url.html.twig';
  414. case 'html':
  415. return 'SonataAdminBundle:CRUD:show_html.html.twig';
  416. default:
  417. return false;
  418. }
  419. }));
  420. $this->assertSame($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderViewElement($this->fieldDescription, $this->object))));
  421. }
  422. public function getRenderViewElementTests()
  423. {
  424. return array(
  425. array('<th>Data</th> <td>Example</td>', 'string', 'Example', array('safe' => false)),
  426. array('<th>Data</th> <td>Example</td>', 'text', 'Example', array('safe' => false)),
  427. array('<th>Data</th> <td>Example</td>', 'textarea', 'Example', array('safe' => false)),
  428. 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()),
  429. 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')),
  430. array('<th>Data</th> <td>December 24, 2013</td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  431. 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')),
  432. array('<th>Data</th> <td>10:11:12</td>', 'time', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  433. array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135, array('safe' => false)),
  434. array('<th>Data</th> <td>5678</td>', 'integer', 5678, array('safe' => false)),
  435. array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135, array()),
  436. array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  437. array('<th>Data</th> <td> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  438. array('<th>Data</th> <td> [1 => First] <br> [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second'), array('safe' => false)),
  439. array('<th>Data</th> <td> [1 => First] [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second'), array('safe' => false, 'inline' => true)),
  440. array('<th>Data</th> <td><i class="fa fa-check-circle-o"></i>yes</td>', 'boolean', true, array()),
  441. array('<th>Data</th> <td><i class="fa fa-ban"></i>no</td>', 'boolean', false, array()),
  442. array('<th>Data</th> <td> Delete </td>', 'trans', 'action_delete', array('safe' => false, 'catalogue' => 'SonataAdminBundle')),
  443. array('<th>Data</th> <td>Status1</td>', 'choice', 'Status1', array('safe' => false)),
  444. array('<th>Data</th> <td>Alias1</td>', 'choice', 'Status1', array('safe' => false, 'choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3'))),
  445. array('<th>Data</th> <td>NoValidKeyInChoices</td>', 'choice', 'NoValidKeyInChoices', array('safe' => false, 'choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3'))),
  446. array('<th>Data</th> <td>Delete</td>', 'choice', 'Foo', array('safe' => false, 'catalogue' => 'SonataAdminBundle', 'choices' => array('Foo' => 'action_delete', 'Status2' => 'Alias2', 'Status3' => 'Alias3'))),
  447. array('<th>Data</th> <td>NoValidKeyInChoices</td>', 'choice', array('NoValidKeyInChoices'), array('safe' => false, 'choices' => array('Status1' => 'Alias1', 'Status2' => 'Alias2', 'Status3' => 'Alias3'), 'multiple' => true)),
  448. 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)),
  449. 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)),
  450. 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' => ' | ')),
  451. 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)),
  452. 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)),
  453. 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)),
  454. array('<th>Data</th> <td><a href="http://example.com">http://example.com</a></td>', 'url', 'http://example.com', array('safe' => false)),
  455. array('<th>Data</th> <td><a href="https://example.com">https://example.com</a></td>', 'url', 'https://example.com', array('safe' => false)),
  456. array('<th>Data</th> <td><a href="http://example.com">example.com</a></td>', 'url', 'http://example.com', array('safe' => false, 'hide_protocol' => true)),
  457. array('<th>Data</th> <td><a href="https://example.com">example.com</a></td>', 'url', 'https://example.com', array('safe' => false, 'hide_protocol' => true)),
  458. 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)),
  459. 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)),
  460. array('<th>Data</th> <td><a href="http://example.com">Foo</a></td>', 'url', 'Foo', array('safe' => false, 'url' => 'http://example.com')),
  461. 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')),
  462. 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')),
  463. array('<th>Data</th> <td><a href="/foo">Foo</a></td>', 'url', 'Foo', array('safe' => false, 'route' => array('name' => 'sonata_admin_foo'))),
  464. 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))),
  465. 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)),
  466. 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)),
  467. 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')))),
  468. 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')))),
  469. 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'))),
  470. 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'))),
  471. array(
  472. '<th>Data</th> <td><p><strong>Creating a Template for the Field</strong> and form</p> </td>',
  473. 'html',
  474. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  475. array(),
  476. ),
  477. array(
  478. '<th>Data</th> <td>Creating a Template for the Field and form </td>',
  479. 'html',
  480. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  481. array('strip' => true),
  482. ),
  483. array(
  484. '<th>Data</th> <td> Creating a Template for the Fi... </td>',
  485. 'html',
  486. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  487. array('truncate' => true),
  488. ),
  489. array(
  490. '<th>Data</th> <td> Creating a... </td>',
  491. 'html',
  492. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  493. array('truncate' => array('length' => 10)),
  494. ),
  495. array(
  496. '<th>Data</th> <td> Creating a Template for the Field... </td>',
  497. 'html',
  498. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  499. array('truncate' => array('preserve' => true)),
  500. ),
  501. array(
  502. '<th>Data</th> <td> Creating a Template for the Fi etc. </td>',
  503. 'html',
  504. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  505. array('truncate' => array('separator' => ' etc.')),
  506. ),
  507. array(
  508. '<th>Data</th> <td> Creating a Template for[...] </td>',
  509. 'html',
  510. '<p><strong>Creating a Template for the Field</strong> and form</p>',
  511. array(
  512. 'truncate' => array(
  513. 'length' => 20,
  514. 'preserve' => true,
  515. 'separator' => '[...]',
  516. ),
  517. ),
  518. ),
  519. // NoValueException
  520. array('<th>Data</th> <td></td>', 'string', new NoValueException(), array('safe' => false)),
  521. array('<th>Data</th> <td></td>', 'text', new NoValueException(), array('safe' => false)),
  522. array('<th>Data</th> <td></td>', 'textarea', new NoValueException(), array('safe' => false)),
  523. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array()),
  524. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array('format' => 'd.m.Y H:i:s')),
  525. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array()),
  526. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array('format' => 'd.m.Y')),
  527. array('<th>Data</th> <td>&nbsp;</td>', 'time', new NoValueException(), array()),
  528. array('<th>Data</th> <td></td>', 'number', new NoValueException(), array('safe' => false)),
  529. array('<th>Data</th> <td></td>', 'integer', new NoValueException(), array('safe' => false)),
  530. array('<th>Data</th> <td> 0 % </td>', 'percent', new NoValueException(), array()),
  531. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'EUR')),
  532. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'GBP')),
  533. array('<th>Data</th> <td> </td>', 'array', new NoValueException(), array('safe' => false)),
  534. array('<th>Data</th> <td><i class="fa fa-ban"></i>no</td>', 'boolean', new NoValueException(), array()),
  535. array('<th>Data</th> <td> </td>', 'trans', new NoValueException(), array('safe' => false, 'catalogue' => 'SonataAdminBundle')),
  536. array('<th>Data</th> <td></td>', 'choice', new NoValueException(), array('safe' => false, 'choices' => array())),
  537. array('<th>Data</th> <td></td>', 'choice', new NoValueException(), array('safe' => false, 'choices' => array(), 'multiple' => true)),
  538. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array()),
  539. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array('url' => 'http://example.com')),
  540. array('<th>Data</th> <td>&nbsp;</td>', 'url', new NoValueException(), array('route' => array('name' => 'sonata_admin_foo'))),
  541. );
  542. }
  543. public function testGetValueFromFieldDescription()
  544. {
  545. $object = new \stdClass();
  546. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  547. $fieldDescription->expects($this->any())
  548. ->method('getValue')
  549. ->will($this->returnValue('test123'));
  550. $this->assertSame('test123', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  551. }
  552. public function testGetValueFromFieldDescriptionWithRemoveLoopException()
  553. {
  554. $object = $this->getMock('\ArrayAccess');
  555. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  556. try {
  557. $this->assertSame('anything', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription, array('loop' => true)));
  558. } catch (\RuntimeException $e) {
  559. $this->assertContains('remove the loop requirement', $e->getMessage());
  560. return;
  561. }
  562. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  563. }
  564. public function testGetValueFromFieldDescriptionWithNoValueException()
  565. {
  566. $object = new \stdClass();
  567. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  568. $fieldDescription->expects($this->any())
  569. ->method('getValue')
  570. ->will($this->returnCallback(function () {
  571. throw new NoValueException();
  572. }));
  573. $fieldDescription->expects($this->any())
  574. ->method('getAssociationAdmin')
  575. ->will($this->returnValue(null));
  576. $this->assertSame(null, $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  577. }
  578. public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInstance()
  579. {
  580. $object = new \stdClass();
  581. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  582. $fieldDescription->expects($this->any())
  583. ->method('getValue')
  584. ->will($this->returnCallback(function () {
  585. throw new NoValueException();
  586. }));
  587. $fieldDescription->expects($this->any())
  588. ->method('getAssociationAdmin')
  589. ->will($this->returnValue($this->admin));
  590. $this->admin->expects($this->once())
  591. ->method('getNewInstance')
  592. ->will($this->returnValue('foo'));
  593. $this->assertSame('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  594. }
  595. public function testOutput()
  596. {
  597. $this->fieldDescription->expects($this->any())
  598. ->method('getTemplate')
  599. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  600. $this->fieldDescription->expects($this->any())
  601. ->method('getFieldName')
  602. ->will($this->returnValue('fd_name'));
  603. $this->environment->disableDebug();
  604. $parameters = array(
  605. 'admin' => $this->admin,
  606. 'value' => 'foo',
  607. 'field_description' => $this->fieldDescription,
  608. 'object' => $this->object,
  609. );
  610. $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
  611. $this->assertSame('<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
  612. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  613. $this->environment->enableDebug();
  614. $this->assertSame('<!-- 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 -->',
  615. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  616. }
  617. public function testRenderRelationElementNoObject()
  618. {
  619. $this->assertSame('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
  620. }
  621. public function testRenderRelationElementToString()
  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 '__toString';
  631. }
  632. }));
  633. $element = $this->getMock('stdClass', array('__toString'));
  634. $element->expects($this->any())
  635. ->method('__toString')
  636. ->will($this->returnValue('bar'));
  637. $this->assertSame('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  638. }
  639. public function testRenderRelationElementCustomToString()
  640. {
  641. $this->fieldDescription->expects($this->exactly(2))
  642. ->method('getOption')
  643. ->will($this->returnCallback(function ($value, $default = null) {
  644. if ($value == 'associated_property') {
  645. return $default;
  646. }
  647. if ($value == 'associated_tostring') {
  648. return 'customToString';
  649. }
  650. }));
  651. $element = $this->getMock('stdClass', array('customToString'));
  652. $element->expects($this->any())
  653. ->method('customToString')
  654. ->will($this->returnValue('fooBar'));
  655. $this->assertSame('fooBar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  656. }
  657. public function testRenderRelationElementMethodNotExist()
  658. {
  659. $this->fieldDescription->expects($this->exactly(2))
  660. ->method('getOption')
  661. ->will($this->returnCallback(function ($value, $default = null) {
  662. if ($value == 'associated_tostring') {
  663. return 'nonExistedMethod';
  664. }
  665. }));
  666. $element = new \stdClass();
  667. try {
  668. $this->twigExtension->renderRelationElement($element, $this->fieldDescription);
  669. } catch (\RuntimeException $e) {
  670. $this->assertContains('You must define an `associated_property` option or create a `stdClass::__toString', $e->getMessage());
  671. return;
  672. }
  673. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  674. }
  675. public function testRenderRelationElementWithPropertyPath()
  676. {
  677. $this->fieldDescription->expects($this->exactly(1))
  678. ->method('getOption')
  679. ->will($this->returnCallback(function ($value, $default = null) {
  680. if ($value == 'associated_property') {
  681. return 'foo';
  682. }
  683. }));
  684. $element = new \stdClass();
  685. $element->foo = 'bar';
  686. $this->assertSame('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  687. }
  688. public function testRenderRelationElementWithClosure()
  689. {
  690. $this->fieldDescription->expects($this->exactly(1))
  691. ->method('getOption')
  692. ->will($this->returnCallback(function ($value, $default = null) {
  693. if ($value == 'associated_property') {
  694. return function ($element) {
  695. return 'closure '.$element->foo;
  696. };
  697. }
  698. }));
  699. $element = new \stdClass();
  700. $element->foo = 'bar';
  701. $this->assertSame('closure bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  702. }
  703. public function testGetUrlsafeIdentifier()
  704. {
  705. $entity = new \stdClass();
  706. // set admin to pool
  707. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
  708. $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service')));
  709. $this->admin->expects($this->once())
  710. ->method('getUrlsafeIdentifier')
  711. ->with($this->equalTo($entity))
  712. ->will($this->returnValue(1234567));
  713. $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity));
  714. }
  715. public function testGetUrlsafeIdentifier_GivenAdmin_Foo()
  716. {
  717. $entity = new \stdClass();
  718. // set admin to pool
  719. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
  720. $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service', 'sonata_admin_bar_service')));
  721. $this->admin->expects($this->once())
  722. ->method('getUrlsafeIdentifier')
  723. ->with($this->equalTo($entity))
  724. ->will($this->returnValue(1234567));
  725. $this->adminBar->expects($this->never())
  726. ->method('getUrlsafeIdentifier');
  727. $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->admin));
  728. }
  729. public function testGetUrlsafeIdentifier_GivenAdmin_Bar()
  730. {
  731. $entity = new \stdClass();
  732. // set admin to pool
  733. $this->pool->setAdminServiceIds(array('sonata_admin_foo_service', 'sonata_admin_bar_service'));
  734. $this->pool->setAdminClasses(array('stdClass' => array('sonata_admin_foo_service', 'sonata_admin_bar_service')));
  735. $this->admin->expects($this->never())
  736. ->method('getUrlsafeIdentifier');
  737. $this->adminBar->expects($this->once())
  738. ->method('getUrlsafeIdentifier')
  739. ->with($this->equalTo($entity))
  740. ->will($this->returnValue(1234567));
  741. $this->assertSame(1234567, $this->twigExtension->getUrlsafeIdentifier($entity, $this->adminBar));
  742. }
  743. }