MenuBuilderTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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\Twig\Extension;
  11. use Knp\Menu\MenuFactory;
  12. use Sonata\AdminBundle\Menu\MenuBuilder;
  13. class MenuBuilderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $pool;
  16. private $provider;
  17. private $factory;
  18. private $eventDispatcher;
  19. private $builder;
  20. protected function setUp()
  21. {
  22. $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')->disableOriginalConstructor()->getMock();
  23. $this->provider = $this->getMock('Knp\Menu\Provider\MenuProviderInterface');
  24. $this->factory = new MenuFactory();
  25. $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  26. $this->builder = new MenuBuilder($this->pool, $this->factory, $this->provider, $this->eventDispatcher);
  27. }
  28. public function testGetKnpMenu()
  29. {
  30. $adminGroups = array(
  31. "bar" => array(
  32. "label" => "foo",
  33. "icon" => '<i class="fa fa-edit"></i>',
  34. "label_catalogue" => 'SonataAdminBundle',
  35. "items" => array(
  36. array(
  37. "admin" => "",
  38. "label" => "fooLabel",
  39. "route" => "FooRoute",
  40. "route_params" => array("foo" => "bar"),
  41. ),
  42. ),
  43. "item_adds" => array(),
  44. "roles" => array(),
  45. ),
  46. );
  47. $this->preparePool($adminGroups);
  48. $menu = $this->builder->createSidebarMenu();
  49. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  50. $this->assertArrayHasKey('bar', $menu->getChildren());
  51. foreach ($menu->getChildren() as $key => $child) {
  52. $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
  53. $this->assertEquals("bar", $child->getName());
  54. $this->assertEquals($adminGroups["bar"]["label"], $child->getLabel());
  55. // menu items
  56. $children = $child->getChildren();
  57. $this->assertCount(1, $children);
  58. $this->assertArrayHasKey('fooLabel', $children);
  59. $this->assertInstanceOf('Knp\Menu\MenuItem', $child['fooLabel']);
  60. $this->assertEquals('fooLabel', $child['fooLabel']->getLabel());
  61. }
  62. }
  63. public function testGetKnpMenuWithAdmin()
  64. {
  65. $adminGroups = array(
  66. 'bar' => array(
  67. 'label' => 'foo',
  68. 'icon' => '<i class="fa fa-edit"></i>',
  69. 'label_catalogue' => 'SonataAdminBundle',
  70. 'items' => array(
  71. array(
  72. 'admin' => 'sonata_admin_foo_service',
  73. 'label' => 'fooLabel',
  74. ),
  75. ),
  76. 'item_adds' => array(),
  77. 'roles' => array(),
  78. ),
  79. );
  80. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  81. $admin->expects($this->once())
  82. ->method('hasRoute')
  83. ->with($this->equalTo('list'))
  84. ->will($this->returnValue(true))
  85. ;
  86. $admin->expects($this->any())
  87. ->method('isGranted')
  88. ->with($this->equalTo('LIST'))
  89. ->will($this->returnValue(true))
  90. ;
  91. $admin->expects($this->once())
  92. ->method('getLabel')
  93. ->will($this->returnValue('foo_admin_label'))
  94. ;
  95. $admin->expects($this->once())
  96. ->method('generateMenuUrl')
  97. ->will($this->returnValue(array()))
  98. ;
  99. $this->preparePool($adminGroups, $admin);
  100. $menu = $this->builder->createSidebarMenu();
  101. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  102. $this->assertArrayHasKey('bar', $menu->getChildren());
  103. foreach ($menu->getChildren() as $key => $child) {
  104. $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
  105. $this->assertEquals('bar', $child->getName());
  106. $this->assertEquals($adminGroups['bar']['label'], $child->getLabel());
  107. // menu items
  108. $children = $child->getChildren();
  109. $this->assertCount(1, $children);
  110. $this->assertArrayHasKey('foo_admin_label', $children);
  111. $this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo_admin_label']);
  112. $this->assertEquals('foo_admin_label', $child['foo_admin_label']->getLabel());
  113. }
  114. }
  115. public function testGetKnpMenuWithNoListRoute()
  116. {
  117. $adminGroups = array(
  118. 'bar' => array(
  119. 'label' => 'foo',
  120. 'icon' => '<i class="fa fa-edit"></i>',
  121. 'label_catalogue' => 'SonataAdminBundle',
  122. 'items' => array(
  123. array(
  124. 'admin' => 'sonata_admin_foo_service',
  125. 'label' => 'fooLabel',
  126. ),
  127. ),
  128. 'item_adds' => array(),
  129. 'roles' => array(),
  130. ),
  131. );
  132. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  133. $admin->expects($this->once())
  134. ->method('hasRoute')
  135. ->with($this->equalTo('list'))
  136. ->will($this->returnValue(false))
  137. ;
  138. $this->preparePool($adminGroups, $admin);
  139. $menu = $this->builder->createSidebarMenu();
  140. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  141. $this->assertArrayNotHasKey('bar', $menu->getChildren());
  142. $this->assertCount(0, $menu->getChildren());
  143. }
  144. public function testGetKnpMenuWithNotGrantedList()
  145. {
  146. $adminGroups = array(
  147. 'bar' => array(
  148. 'label' => 'foo',
  149. 'icon' => '<i class="fa fa-edit"></i>',
  150. 'label_catalogue' => 'SonataAdminBundle',
  151. 'items' => array(
  152. array(
  153. 'admin' => 'sonata_admin_foo_service',
  154. 'label' => 'fooLabel',
  155. ),
  156. ),
  157. 'item_adds' => array(),
  158. 'roles' => array(),
  159. ),
  160. );
  161. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  162. $admin->expects($this->once())
  163. ->method('hasRoute')
  164. ->with($this->equalTo('list'))
  165. ->will($this->returnValue(true))
  166. ;
  167. $admin->expects($this->any())
  168. ->method('isGranted')
  169. ->with($this->equalTo('LIST'))
  170. ->will($this->returnValue(false))
  171. ;
  172. $this->preparePool($adminGroups, $admin);
  173. $menu = $this->builder->createSidebarMenu();
  174. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  175. $this->assertArrayNotHasKey('bar', $menu->getChildren());
  176. $this->assertCount(0, $menu->getChildren());
  177. }
  178. public function testGetKnpMenuWithProvider()
  179. {
  180. $adminGroups = array(
  181. "bar" => array(
  182. "provider" => 'my_menu',
  183. "label_catalogue" => '',
  184. "icon" => '<i class="fa fa-edit"></i>',
  185. "roles" => array(),
  186. ),
  187. );
  188. $this->provider
  189. ->expects($this->once())
  190. ->method('get')
  191. ->with('my_menu')
  192. ->will($this->returnValue($this->factory->createItem('bar')->addChild('foo')->getParent()))
  193. ;
  194. $this->preparePool($adminGroups);
  195. $menu = $this->builder->createSidebarMenu();
  196. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  197. $this->assertArrayHasKey('bar', $menu->getChildren());
  198. foreach ($menu->getChildren() as $key => $child) {
  199. $this->assertInstanceOf('Knp\Menu\MenuItem', $child);
  200. $this->assertEquals("bar", $child->getName());
  201. $this->assertEquals("bar", $child->getLabel());
  202. // menu items
  203. $children = $child->getChildren();
  204. $this->assertCount(1, $children);
  205. $this->assertArrayHasKey('foo', $children);
  206. $this->assertInstanceOf('Knp\Menu\MenuItem', $child['foo']);
  207. $this->assertEquals('foo', $child['foo']->getLabel());
  208. }
  209. }
  210. public function testGetKnpMenuAndDispatchEvent()
  211. {
  212. $adminGroups = array(
  213. "bar" => array(
  214. "label" => "foo",
  215. "icon" => '<i class="fa fa-edit"></i>',
  216. "label_catalogue" => 'SonataAdminBundle',
  217. "items" => array(),
  218. "item_adds" => array(),
  219. "roles" => array(),
  220. ),
  221. );
  222. $this->preparePool($adminGroups);
  223. $this->eventDispatcher
  224. ->expects($this->once())
  225. ->method('dispatch')
  226. ->with($this->equalTo('sonata.admin.event.configure.menu.sidebar'), $this->isInstanceOf('Sonata\AdminBundle\Event\ConfigureMenuEvent'))
  227. ;
  228. $this->builder->createSidebarMenu();
  229. }
  230. private function preparePool($adminGroups, $admin = null)
  231. {
  232. $this->pool->expects($this->once())
  233. ->method('getAdminGroups')
  234. ->will($this->returnValue($adminGroups))
  235. ;
  236. if (null !== $admin) {
  237. $this->pool->expects($this->once())
  238. ->method('getInstance')
  239. ->with($this->equalTo('sonata_admin_foo_service'))
  240. ->will($this->returnValue($admin))
  241. ;
  242. }
  243. }
  244. }