SonataAdminExtensionTest.php 41 KB

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