ConfigurationTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. )
  133. );
  134. $this->assertSame(
  135. $config['dashboard']['groups']['bar']['items'][1],
  136. array(
  137. 'admin' => 'item2',
  138. 'label' => '',
  139. 'route' => '',
  140. 'route_params' => array(),
  141. 'route_absolute' => true,
  142. )
  143. );
  144. $this->assertSame(
  145. $config['dashboard']['groups']['bar']['items'][2],
  146. array(
  147. 'label' => 'fooLabel',
  148. 'route' => 'fooRoute',
  149. 'route_params' => array('bar' => 'foo'),
  150. 'route_absolute' => true,
  151. 'admin' => '',
  152. )
  153. );
  154. $this->assertSame(
  155. $config['dashboard']['groups']['bar']['items'][3],
  156. array(
  157. 'label' => 'barLabel',
  158. 'route' => 'barRoute',
  159. 'route_params' => array(),
  160. 'admin' => '',
  161. 'route_absolute' => true,
  162. )
  163. );
  164. }
  165. public function testDashboardGroupsWithBadItemsParams()
  166. {
  167. $this->setExpectedException('\InvalidArgumentException', 'Expected either parameters "route" and "label" for array items');
  168. $config = $this->process(array(array(
  169. 'dashboard' => array(
  170. 'groups' => array(
  171. 'bar' => array(
  172. 'label' => 'foo',
  173. 'icon' => '<i class="fa fa-edit"></i>',
  174. 'items' => array(
  175. 'item1',
  176. 'item2',
  177. array(
  178. 'route' => 'fooRoute',
  179. ),
  180. ),
  181. ),
  182. ),
  183. ),
  184. )));
  185. }
  186. /**
  187. * Processes an array of configurations and returns a compiled version.
  188. *
  189. * @param array $configs An array of raw configurations
  190. *
  191. * @return array A normalized array
  192. */
  193. protected function process($configs)
  194. {
  195. $processor = new Processor();
  196. return $processor->processConfiguration(new Configuration(), $configs);
  197. }
  198. }