ConfigurationTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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;
  11. use Sonata\AdminBundle\DependencyInjection\Configuration;
  12. use Symfony\Component\Config\Definition\Processor;
  13. class ConfigurationTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testOptions()
  16. {
  17. $config = $this->process(array());
  18. $this->assertTrue($config['options']['html5_validate']);
  19. $this->assertNull($config['options']['pager_links']);
  20. $this->assertTrue($config['options']['confirm_exit']);
  21. $this->assertTrue($config['options']['use_icheck']);
  22. }
  23. public function testOptionsWithInvalidFormat()
  24. {
  25. $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
  26. $config = $this->process(array(array(
  27. 'options' => array(
  28. 'html5_validate' => '1',
  29. ),
  30. )));
  31. }
  32. public function testCustomTemplatesPerAdmin()
  33. {
  34. $config = $this->process(array(array(
  35. 'admin_services' => array(
  36. 'my_admin_id' => array(
  37. 'templates' => array(
  38. 'form' => array('form.twig.html', 'form_extra.twig.html'),
  39. 'view' => array('user_block' => 'SonataAdminBundle:mycustomtemplate.html.twig'),
  40. 'filter' => array(),
  41. ),
  42. ),
  43. ),
  44. )));
  45. $this->assertSame('SonataAdminBundle:mycustomtemplate.html.twig', $config['admin_services']['my_admin_id']['templates']['view']['user_block']);
  46. }
  47. public function testAdminServicesDefault()
  48. {
  49. $config = $this->process(array(array(
  50. 'admin_services' => array('my_admin_id' => array()),
  51. )));
  52. $this->assertSame(array(
  53. 'model_manager' => null,
  54. 'form_contractor' => null,
  55. 'show_builder' => null,
  56. 'list_builder' => null,
  57. 'datagrid_builder' => null,
  58. 'translator' => null,
  59. 'configuration_pool' => null,
  60. 'route_generator' => null,
  61. 'validator' => null,
  62. 'security_handler' => null,
  63. 'label' => null,
  64. 'menu_factory' => null,
  65. 'route_builder' => null,
  66. 'label_translator_strategy' => null,
  67. 'pager_type' => null,
  68. 'templates' => array(
  69. 'form' => array(),
  70. 'filter' => array(),
  71. 'view' => array(),
  72. ),
  73. ), $config['admin_services']['my_admin_id']);
  74. }
  75. public function testDashboardWithoutRoles()
  76. {
  77. $config = $this->process(array());
  78. $this->assertEmpty($config['dashboard']['blocks'][0]['roles']);
  79. }
  80. public function testDashboardWithRoles()
  81. {
  82. $config = $this->process(array(array(
  83. 'dashboard' => array(
  84. 'blocks' => array(array(
  85. 'roles' => array('ROLE_ADMIN'),
  86. 'type' => 'my.type',
  87. )),
  88. ),
  89. )));
  90. $this->assertSame($config['dashboard']['blocks'][0]['roles'], array('ROLE_ADMIN'));
  91. }
  92. public function testDashboardGroups()
  93. {
  94. $config = $this->process(array(array(
  95. 'dashboard' => array(
  96. 'groups' => array(
  97. 'bar' => array(
  98. 'label' => 'foo',
  99. 'icon' => '<i class="fa fa-edit"></i>',
  100. 'items' => array(
  101. 'item1',
  102. 'item2',
  103. array(
  104. 'label' => 'fooLabel',
  105. 'route' => 'fooRoute',
  106. 'route_params' => array('bar' => 'foo'),
  107. 'route_absolute' => true,
  108. ),
  109. array(
  110. 'label' => 'barLabel',
  111. 'route' => 'barRoute',
  112. ),
  113. ),
  114. ),
  115. ),
  116. ),
  117. )));
  118. $this->assertCount(4, $config['dashboard']['groups']['bar']['items']);
  119. $this->assertSame(
  120. $config['dashboard']['groups']['bar']['items'][0],
  121. array(
  122. 'admin' => 'item1',
  123. 'label' => '',
  124. 'route' => '',
  125. 'route_params' => array(),
  126. 'route_absolute' => true,
  127. )
  128. );
  129. $this->assertSame(
  130. $config['dashboard']['groups']['bar']['items'][1],
  131. array(
  132. 'admin' => 'item2',
  133. 'label' => '',
  134. 'route' => '',
  135. 'route_params' => array(),
  136. 'route_absolute' => true,
  137. )
  138. );
  139. $this->assertSame(
  140. $config['dashboard']['groups']['bar']['items'][2],
  141. array(
  142. 'label' => 'fooLabel',
  143. 'route' => 'fooRoute',
  144. 'route_params' => array('bar' => 'foo'),
  145. 'route_absolute' => true,
  146. 'admin' => '',
  147. )
  148. );
  149. $this->assertSame(
  150. $config['dashboard']['groups']['bar']['items'][3],
  151. array(
  152. 'label' => 'barLabel',
  153. 'route' => 'barRoute',
  154. 'route_params' => array(),
  155. 'admin' => '',
  156. 'route_absolute' => true,
  157. )
  158. );
  159. }
  160. public function testDashboardGroupsWithBadItemsParams()
  161. {
  162. $this->setExpectedException('\InvalidArgumentException', 'Expected either parameters "route" and "label" for array items');
  163. $config = $this->process(array(array(
  164. 'dashboard' => array(
  165. 'groups' => array(
  166. 'bar' => array(
  167. 'label' => 'foo',
  168. 'icon' => '<i class="fa fa-edit"></i>',
  169. 'items' => array(
  170. 'item1',
  171. 'item2',
  172. array(
  173. 'route' => 'fooRoute',
  174. ),
  175. ),
  176. ),
  177. ),
  178. ),
  179. )));
  180. }
  181. /**
  182. * Processes an array of configurations and returns a compiled version.
  183. *
  184. * @param array $configs An array of raw configurations
  185. *
  186. * @return array A normalized array
  187. */
  188. protected function process($configs)
  189. {
  190. $processor = new Processor();
  191. return $processor->processConfiguration(new Configuration(), $configs);
  192. }
  193. }