GroupMenuProviderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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\Menu\Provider;
  11. use Knp\Menu\FactoryInterface;
  12. use Knp\Menu\MenuFactory;
  13. use Knp\Menu\Provider\MenuProviderInterface;
  14. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  15. use Sonata\AdminBundle\Admin\AdminInterface;
  16. use Sonata\AdminBundle\Admin\Pool;
  17. use Sonata\AdminBundle\Menu\Provider\GroupMenuProvider;
  18. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  19. class GroupMenuProviderTest extends PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @var MockObject|Pool
  23. */
  24. private $pool;
  25. /**
  26. * @var MockObject|MenuProviderInterface
  27. */
  28. private $provider;
  29. /**
  30. * @var MockObject|FactoryInterface
  31. */
  32. private $factory;
  33. /**
  34. * @var MockObject
  35. */
  36. private $checker;
  37. protected function setUp()
  38. {
  39. $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')->disableOriginalConstructor()->getMock();
  40. $this->checker = $this
  41. ->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')
  42. ->setMethods(array('isGranted'))
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->factory = new MenuFactory();
  46. $this->provider = new GroupMenuProvider($this->factory, $this->pool, $this->checker);
  47. }
  48. public function testGroupMenuProviderName()
  49. {
  50. $this->assertTrue($this->provider->has('sonata_group_menu'));
  51. }
  52. /**
  53. * NEXT_MAJOR: Remove this test.
  54. *
  55. * @param array $adminGroups
  56. *
  57. * @group legacy
  58. * @dataProvider getAdminGroups
  59. */
  60. public function testGroupMenuProviderWithoutChecker(array $adminGroups)
  61. {
  62. $provider = new GroupMenuProvider($this->factory, $this->pool);
  63. $this->pool->expects($this->once())
  64. ->method('getInstance')
  65. ->with($this->equalTo('sonata_admin_foo_service'))
  66. ->will($this->returnValue($this->getAdminMock()));
  67. $menu = $provider->get(
  68. 'providerFoo',
  69. array(
  70. 'name' => 'foo',
  71. 'group' => $adminGroups,
  72. )
  73. );
  74. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  75. $this->assertSame('foo', $menu->getName());
  76. $children = $menu->getChildren();
  77. $this->assertCount(2, $children);
  78. $this->assertArrayHasKey('foo_admin_label', $children);
  79. $this->assertArrayHasKey('route_label', $children);
  80. $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
  81. $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
  82. }
  83. /**
  84. * NEXT_MAJOR: Remove this test when bumping requirements to >=Symfony 2.6.
  85. *
  86. * @group legacy
  87. */
  88. public function testGroupMenuProviderThrowsExceptionWithInvalidArgument()
  89. {
  90. $this->expectException('InvalidArgumentException');
  91. new GroupMenuProvider($this->factory, $this->pool, 'foo');
  92. }
  93. /**
  94. * @param array $adminGroups
  95. *
  96. * @dataProvider getAdminGroups
  97. */
  98. public function testGetMenuProviderWithCheckerGrantedGroupRoles(array $adminGroups)
  99. {
  100. $this->pool->expects($this->once())
  101. ->method('getInstance')
  102. ->with($this->equalTo('sonata_admin_foo_service'))
  103. ->will($this->returnValue($this->getAdminMock()));
  104. $this->checker->expects($this->any())
  105. ->method('isGranted')
  106. ->willReturn(false);
  107. $menu = $this->provider->get(
  108. 'providerFoo',
  109. array(
  110. 'name' => 'foo',
  111. 'group' => $adminGroups,
  112. )
  113. );
  114. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  115. $this->assertSame('foo', $menu->getName());
  116. $children = $menu->getChildren();
  117. $this->assertCount(1, $children);
  118. $this->assertArrayHasKey('foo_admin_label', $children);
  119. $this->assertArrayNotHasKey('route_label', $children);
  120. $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
  121. $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
  122. }
  123. /**
  124. * @param array $adminGroups
  125. *
  126. * @dataProvider getAdminGroups
  127. */
  128. public function testGetMenuProviderWithAdmin(array $adminGroups)
  129. {
  130. $this->pool->expects($this->once())
  131. ->method('getInstance')
  132. ->with($this->equalTo('sonata_admin_foo_service'))
  133. ->will($this->returnValue($this->getAdminMock()));
  134. $this->checker->expects($this->any())
  135. ->method('isGranted')
  136. ->willReturn(true);
  137. $menu = $this->provider->get(
  138. 'providerFoo',
  139. array(
  140. 'name' => 'foo',
  141. 'group' => $adminGroups,
  142. )
  143. );
  144. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  145. $this->assertSame('foo', $menu->getName());
  146. $children = $menu->getChildren();
  147. $this->assertCount(2, $children);
  148. $this->assertArrayHasKey('foo_admin_label', $children);
  149. $this->assertArrayHasKey('route_label', $children);
  150. $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
  151. $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
  152. }
  153. /**
  154. * @param array $adminGroups
  155. *
  156. * @dataProvider getAdminGroups
  157. */
  158. public function testGetKnpMenuWithListRoute(array $adminGroups)
  159. {
  160. $this->pool->expects($this->once())
  161. ->method('getInstance')
  162. ->with($this->equalTo('sonata_admin_foo_service'))
  163. ->will($this->returnValue($this->getAdminMock(false)));
  164. $this->checker->expects($this->any())
  165. ->method('isGranted')
  166. ->willReturn(true);
  167. $menu = $this->provider->get(
  168. 'providerFoo',
  169. array(
  170. 'name' => 'foo',
  171. 'group' => $adminGroups,
  172. )
  173. );
  174. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  175. $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
  176. $this->assertArrayHasKey('route_label', $menu->getChildren());
  177. $this->assertCount(1, $menu->getChildren());
  178. }
  179. /**
  180. * @param array $adminGroups
  181. *
  182. * @dataProvider getAdminGroups
  183. */
  184. public function testGetKnpMenuWithGrantedList(array $adminGroups)
  185. {
  186. $this->pool->expects($this->once())
  187. ->method('getInstance')
  188. ->with($this->equalTo('sonata_admin_foo_service'))
  189. ->will($this->returnValue($this->getAdminMock(true, false)));
  190. $this->checker->expects($this->any())
  191. ->method('isGranted')
  192. ->willReturn(true);
  193. $menu = $this->provider->get(
  194. 'providerFoo',
  195. array(
  196. 'name' => 'foo',
  197. 'group' => $adminGroups,
  198. )
  199. );
  200. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  201. $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
  202. $this->assertArrayHasKey('route_label', $menu->getChildren());
  203. $this->assertCount(1, $menu->getChildren());
  204. }
  205. /**
  206. * @param array $adminGroupsOnTopOption
  207. *
  208. * @dataProvider getAdminGroupsWithOnTopOption
  209. */
  210. public function testGetMenuProviderOnTopOptions(array $adminGroupsOnTopOption)
  211. {
  212. $this->pool->expects($this->once())
  213. ->method('getInstance')
  214. ->with($this->equalTo('sonata_admin_foo_service'))
  215. ->will($this->returnValue($this->getAdminMock(true, false)));
  216. $menu = $this->provider->get(
  217. 'providerFoo',
  218. array(
  219. 'name' => 'foo',
  220. 'group' => $adminGroupsOnTopOption,
  221. )
  222. );
  223. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  224. $this->assertCount(0, $menu->getChildren());
  225. }
  226. /**
  227. * @param array $adminGroups
  228. *
  229. * @dataProvider getAdminGroups
  230. */
  231. public function testGetMenuProviderKeepOpenOption(array $adminGroups)
  232. {
  233. $this->pool->expects($this->once())
  234. ->method('getInstance')
  235. ->with($this->equalTo('sonata_admin_foo_service'))
  236. ->will($this->returnValue($this->getAdminMock()));
  237. $this->checker->expects($this->any())
  238. ->method('isGranted')
  239. ->willReturn(true);
  240. $adminGroups['keep_open'] = true;
  241. $menu = $this->provider->get(
  242. 'providerFoo',
  243. array(
  244. 'name' => 'foo',
  245. 'group' => $adminGroups,
  246. )
  247. );
  248. $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
  249. $this->assertSame('keep-open', $menu->getAttribute('class'));
  250. $this->assertTrue($menu->getExtra('keep_open'));
  251. }
  252. /**
  253. * @return array
  254. */
  255. public function getAdminGroups()
  256. {
  257. return array(
  258. array(
  259. 'bar' => array(
  260. 'label' => 'foo',
  261. 'icon' => '<i class="fa fa-edit"></i>',
  262. 'label_catalogue' => 'SonataAdminBundle',
  263. 'items' => array(
  264. array(
  265. 'admin' => 'sonata_admin_foo_service',
  266. 'label' => 'fooLabel',
  267. 'route_absolute' => true,
  268. ),
  269. array(
  270. 'admin' => '',
  271. 'label' => 'route_label',
  272. 'route' => 'FooRoute',
  273. 'route_params' => array('foo' => 'bar'),
  274. 'route_absolute' => true,
  275. 'roles' => array(),
  276. ),
  277. ),
  278. 'item_adds' => array(),
  279. 'roles' => array('foo'),
  280. ),
  281. ),
  282. );
  283. }
  284. /**
  285. * @return array
  286. */
  287. public function getAdminGroupsWithOnTopOption()
  288. {
  289. return array(
  290. array(
  291. 'foo' => array(
  292. 'label' => 'foo_on_top',
  293. 'icon' => '<i class="fa fa-edit"></i>',
  294. 'label_catalogue' => 'SonataAdminBundle',
  295. 'on_top' => true,
  296. 'items' => array(
  297. array(
  298. 'admin' => 'sonata_admin_foo_service',
  299. 'label' => 'fooLabel',
  300. 'route_absolute' => true,
  301. 'route_params' => array(),
  302. ),
  303. ),
  304. 'item_adds' => array(),
  305. 'roles' => array(),
  306. ),
  307. ),
  308. );
  309. }
  310. /**
  311. * @param bool $hasRoute
  312. * @param bool $isGranted
  313. *
  314. * @return MockObject|AdminInterface
  315. */
  316. private function getAdminMock($hasRoute = true, $isGranted = true)
  317. {
  318. $admin = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
  319. $admin->expects($this->once())
  320. ->method('hasRoute')
  321. ->with($this->equalTo('list'))
  322. ->will($this->returnValue($hasRoute));
  323. $admin->expects($this->any())
  324. ->method('hasAccess')
  325. ->with($this->equalTo('list'))
  326. ->will($this->returnValue($isGranted));
  327. $admin->expects($this->any())
  328. ->method('getLabel')
  329. ->will($this->returnValue('foo_admin_label'));
  330. $admin->expects($this->any())
  331. ->method('generateMenuUrl')
  332. ->will($this->returnValue(array()));
  333. return $admin;
  334. }
  335. }