SonataAdminExtensionTest.php 50 KB

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