SonataAdminExtensionTest.php 54 KB

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