ConfigurationTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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. $processor = new Processor();
  18. $config = $processor->processConfiguration(new Configuration(), 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 testOptionsWithInvalidFormat()
  25. {
  26. $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
  27. $processor = new Processor();
  28. $config = $processor->processConfiguration(new Configuration(), array(array(
  29. 'options' => array(
  30. 'html5_validate' => '1'
  31. )
  32. )));
  33. }
  34. public function testCustomTemplatesPerAdmin()
  35. {
  36. $processor = new Processor();
  37. $config = $processor->processConfiguration(new Configuration(), array(array(
  38. 'admin_services' => array(
  39. 'my_admin_id' => array(
  40. 'templates' => array(
  41. 'form' => array('form.twig.html', 'form_extra.twig.html'),
  42. 'view' => array('user_block' => 'SonataAdminBundle:mycustomtemplate.html.twig'),
  43. 'filter' => array()
  44. )
  45. )
  46. )
  47. )));
  48. $this->assertEquals('SonataAdminBundle:mycustomtemplate.html.twig', $config['admin_services']['my_admin_id']['templates']['view']['user_block']);
  49. }
  50. public function testAdminServicesDefault()
  51. {
  52. $processor = new Processor();
  53. $config = $processor->processConfiguration(new Configuration(), array(array(
  54. 'admin_services' => array('my_admin_id' => array())
  55. )));
  56. $this->assertEquals(array(
  57. 'model_manager' => null,
  58. 'form_contractor' => null,
  59. 'show_builder' => null,
  60. 'list_builder' => null,
  61. 'datagrid_builder' => null,
  62. 'translator' => null,
  63. 'configuration_pool' => null,
  64. 'validator' => null,
  65. 'security_handler' => null,
  66. 'label' => null,
  67. 'templates' => array(),
  68. 'route_generator' => 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. $processor = new Processor();
  83. $config = $processor->processConfiguration(new Configuration(), array());
  84. $this->assertEmpty($config['dashboard']["blocks"][0]["roles"]);
  85. }
  86. public function testDashboardWithRoles()
  87. {
  88. $processor = new Processor();
  89. $config = $processor->processConfiguration(new Configuration(), array(array(
  90. "dashboard" => array(
  91. "blocks" => array(array(
  92. "roles" => array("ROLE_ADMIN"),
  93. "type" => "my.type"
  94. ))
  95. )
  96. )));
  97. $this->assertEquals($config['dashboard']["blocks"][0]["roles"], array("ROLE_ADMIN"));
  98. }
  99. public function testDashboardGroups()
  100. {
  101. $processor = new Processor();
  102. $config = $processor->processConfiguration(new Configuration(), array(array(
  103. "dashboard" => array(
  104. "groups" => array(
  105. "bar" => array(
  106. "label" => "foo",
  107. "icon" => '<i class="fa fa-edit"></i>',
  108. "items" => array(
  109. "item1",
  110. "item2",
  111. array(
  112. "label" => "fooLabel",
  113. "route" => "fooRoute",
  114. "route_params" => array("bar" => "foo")
  115. ),
  116. array(
  117. "label" => "barLabel",
  118. "route" => "barRoute",
  119. )
  120. )
  121. )
  122. )
  123. )
  124. )));
  125. $this->assertCount(4, $config['dashboard']["groups"]["bar"]["items"]);
  126. $this->assertEquals(
  127. $config['dashboard']["groups"]["bar"]["items"][0],
  128. array(
  129. "admin" => "item1",
  130. "route" => "",
  131. "route_params" => array(),
  132. "label" => "",
  133. )
  134. );
  135. $this->assertEquals(
  136. $config['dashboard']["groups"]["bar"]["items"][1],
  137. array(
  138. "admin" => "item2",
  139. "route" => "",
  140. "route_params" => array(),
  141. "label" => "",
  142. )
  143. );
  144. $this->assertEquals(
  145. $config['dashboard']["groups"]["bar"]["items"][2],
  146. array(
  147. "admin" => "",
  148. "route" => "fooRoute",
  149. "route_params" => array("bar" => "foo"),
  150. "label" => "fooLabel",
  151. )
  152. );
  153. $this->assertEquals(
  154. $config['dashboard']["groups"]["bar"]["items"][3],
  155. array(
  156. "admin" => "",
  157. "route" => "barRoute",
  158. "route_params" => array(),
  159. "label" => "barLabel",
  160. )
  161. );
  162. }
  163. public function testDashboardGroupsWithBadItemsParams()
  164. {
  165. $this->setExpectedException('\InvalidArgumentException', 'Expected either parameters "route" and "label" for array items');
  166. $processor = new Processor();
  167. $config = $processor->processConfiguration(new Configuration(), array(array(
  168. "dashboard" => array(
  169. "groups" => array(
  170. "bar" => array(
  171. "label" => "foo",
  172. "icon" => '<i class="fa fa-edit"></i>',
  173. "items" => array(
  174. "item1",
  175. "item2",
  176. array(
  177. "route" => "fooRoute"
  178. )
  179. )
  180. )
  181. )
  182. )
  183. )));
  184. }
  185. }