ConfigurationTest.php 7.1 KB

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