ConfigurationTest.php 7.2 KB

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