ConfigurationTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. ),
  108. array(
  109. 'label' => 'barLabel',
  110. 'route' => 'barRoute',
  111. ),
  112. ),
  113. ),
  114. ),
  115. ),
  116. )));
  117. $this->assertCount(4, $config['dashboard']['groups']['bar']['items']);
  118. $this->assertSame(
  119. $config['dashboard']['groups']['bar']['items'][0],
  120. array(
  121. 'admin' => 'item1',
  122. 'label' => '',
  123. 'route' => '',
  124. 'route_params' => array(),
  125. )
  126. );
  127. $this->assertSame(
  128. $config['dashboard']['groups']['bar']['items'][1],
  129. array(
  130. 'admin' => 'item2',
  131. 'label' => '',
  132. 'route' => '',
  133. 'route_params' => array(),
  134. )
  135. );
  136. $this->assertSame(
  137. $config['dashboard']['groups']['bar']['items'][2],
  138. array(
  139. 'label' => 'fooLabel',
  140. 'route' => 'fooRoute',
  141. 'route_params' => array('bar' => 'foo'),
  142. 'admin' => '',
  143. )
  144. );
  145. $this->assertSame(
  146. $config['dashboard']['groups']['bar']['items'][3],
  147. array(
  148. 'label' => 'barLabel',
  149. 'route' => 'barRoute',
  150. 'route_params' => array(),
  151. 'admin' => '',
  152. )
  153. );
  154. }
  155. public function testDashboardGroupsWithBadItemsParams()
  156. {
  157. $this->setExpectedException('\InvalidArgumentException', 'Expected either parameters "route" and "label" for array items');
  158. $config = $this->process(array(array(
  159. 'dashboard' => array(
  160. 'groups' => array(
  161. 'bar' => array(
  162. 'label' => 'foo',
  163. 'icon' => '<i class="fa fa-edit"></i>',
  164. 'items' => array(
  165. 'item1',
  166. 'item2',
  167. array(
  168. 'route' => 'fooRoute',
  169. ),
  170. ),
  171. ),
  172. ),
  173. ),
  174. )));
  175. }
  176. /**
  177. * Processes an array of configurations and returns a compiled version.
  178. *
  179. * @param array $configs An array of raw configurations
  180. *
  181. * @return array A normalized array
  182. */
  183. protected function process($configs)
  184. {
  185. $processor = new Processor();
  186. return $processor->processConfiguration(new Configuration(), $configs);
  187. }
  188. }