SonataAdminExtensionTest.php 55 KB

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