BreadcrumbsBuilderTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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\Admin;
  11. use Sonata\AdminBundle\Admin\BreadcrumbsBuilder;
  12. use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
  13. use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
  14. use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\DummySubject;
  15. use Symfony\Component\HttpFoundation\Request;
  16. /**
  17. * This test class contains unit and integration tests. Maybe it could be
  18. * separated into two classes.
  19. *
  20. * @author Grégoire Paris <postmaster@greg0ire.fr>
  21. */
  22. class BreadcrumbsBuilderTest extends \PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @group legacy
  26. */
  27. public function testGetBreadcrumbs()
  28. {
  29. $class = 'Application\Sonata\NewsBundle\Entity\Post';
  30. $baseControllerName = 'SonataNewsBundle:PostAdmin';
  31. $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
  32. $commentAdmin = new CommentAdmin(
  33. 'sonata.post.admin.comment',
  34. 'Application\Sonata\NewsBundle\Entity\Comment',
  35. 'SonataNewsBundle:CommentAdmin'
  36. );
  37. $subCommentAdmin = new CommentAdmin(
  38. 'sonata.post.admin.comment',
  39. 'Application\Sonata\NewsBundle\Entity\Comment',
  40. 'SonataNewsBundle:CommentAdmin'
  41. );
  42. $admin->addChild($commentAdmin);
  43. $admin->setRequest(new Request(array('id' => 42)));
  44. $commentAdmin->setRequest(new Request());
  45. $commentAdmin->initialize();
  46. $admin->initialize();
  47. $commentAdmin->setCurrentChild($subCommentAdmin);
  48. $menuFactory = $this->getMock('Knp\Menu\FactoryInterface');
  49. $menu = $this->getMock('Knp\Menu\ItemInterface');
  50. $translatorStrategy = $this->getMock(
  51. 'Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface'
  52. );
  53. $routeGenerator = $this->getMock(
  54. 'Sonata\AdminBundle\Route\RouteGeneratorInterface'
  55. );
  56. $modelManager = $this->getMock(
  57. 'Sonata\AdminBundle\Model\ModelManagerInterface'
  58. );
  59. $admin->setMenuFactory($menuFactory);
  60. $admin->setLabelTranslatorStrategy($translatorStrategy);
  61. $admin->setRouteGenerator($routeGenerator);
  62. $admin->setModelManager($modelManager);
  63. $commentAdmin->setLabelTranslatorStrategy($translatorStrategy);
  64. $commentAdmin->setRouteGenerator($routeGenerator);
  65. $modelManager->expects($this->exactly(1))
  66. ->method('find')
  67. ->with('Application\Sonata\NewsBundle\Entity\Post', 42)
  68. ->will($this->returnValue(new DummySubject()));
  69. $menuFactory->expects($this->exactly(5))
  70. ->method('createItem')
  71. ->with('root')
  72. ->will($this->returnValue($menu));
  73. $menu->expects($this->once())
  74. ->method('setUri')
  75. ->with($this->identicalTo(false));
  76. $menu->expects($this->exactly(5))
  77. ->method('getParent')
  78. ->will($this->returnValue(false));
  79. $routeGenerator->expects($this->exactly(5))
  80. ->method('generate')
  81. ->with('sonata_admin_dashboard')
  82. ->will($this->returnValue('http://somehost.com'));
  83. $translatorStrategy->expects($this->exactly(18))
  84. ->method('getLabel')
  85. ->withConsecutive(
  86. array('dashboard'),
  87. array('Post_list'),
  88. array('Comment_list'),
  89. array('Comment_repost'),
  90. array('dashboard'),
  91. array('Post_list'),
  92. array('Comment_list'),
  93. array('Comment_flag'),
  94. array('dashboard'),
  95. array('Post_list'),
  96. array('Comment_list'),
  97. array('Comment_edit'),
  98. array('dashboard'),
  99. array('Post_list'),
  100. array('Comment_list'),
  101. array('dashboard'),
  102. array('Post_list'),
  103. array('Comment_list')
  104. )
  105. ->will($this->onConsecutiveCalls(
  106. 'someLabel',
  107. 'someOtherLabel',
  108. 'someInterestingLabel',
  109. 'someFancyLabel',
  110. 'someCoolLabel',
  111. 'someTipTopLabel',
  112. 'someFunkyLabel',
  113. 'someAwesomeLabel',
  114. 'someLikeableLabel',
  115. 'someMildlyInterestingLabel',
  116. 'someWTFLabel',
  117. 'someBadLabel',
  118. 'someBoringLabel',
  119. 'someLongLabel',
  120. 'someEndlessLabel',
  121. 'someAlmostThereLabel',
  122. 'someOriginalLabel',
  123. 'someOkayishLabel'
  124. ));
  125. $menu->expects($this->exactly(24))
  126. ->method('addChild')
  127. ->withConsecutive(
  128. array('someLabel'),
  129. array('someOtherLabel'),
  130. array('dummy subject representation'),
  131. array('someInterestingLabel'),
  132. array('someFancyLabel'),
  133. array('someCoolLabel'),
  134. array('someTipTopLabel'),
  135. array('dummy subject representation'),
  136. array('someFunkyLabel'),
  137. array('someAwesomeLabel'),
  138. array('someLikeableLabel'),
  139. array('someMildlyInterestingLabel'),
  140. array('dummy subject representation'),
  141. array('someWTFLabel'),
  142. array('someBadLabel'),
  143. array('someBoringLabel'),
  144. array('someLongLabel'),
  145. array('dummy subject representation'),
  146. array('someEndlessLabel'),
  147. array('someAlmostThereLabel'),
  148. array('someOriginalLabel'),
  149. array('dummy subject representation'),
  150. array('someOkayishLabel'),
  151. array('dummy subject representation')
  152. )
  153. ->will($this->returnValue($menu));
  154. $admin->getBreadcrumbs('repost');
  155. $admin->setSubject(new DummySubject());
  156. $admin->getBreadcrumbs('flag');
  157. $commentAdmin->getBreadcrumbs('edit');
  158. $commentAdmin->getBreadcrumbs('list');
  159. $commentAdmin->setSubject(new DummySubject());
  160. $commentAdmin->getBreadcrumbs('reply');
  161. }
  162. /**
  163. * @group legacy
  164. */
  165. public function testGetBreadcrumbsWithNoCurrentAdmin()
  166. {
  167. $class = 'Application\Sonata\NewsBundle\Entity\Post';
  168. $baseControllerName = 'SonataNewsBundle:PostAdmin';
  169. $admin = new PostAdmin('sonata.post.admin.post', $class, $baseControllerName);
  170. $commentAdmin = new CommentAdmin(
  171. 'sonata.post.admin.comment',
  172. 'Application\Sonata\NewsBundle\Entity\Comment',
  173. 'SonataNewsBundle:CommentAdmin'
  174. );
  175. $admin->addChild($commentAdmin);
  176. $admin->setRequest(new Request(array('id' => 42)));
  177. $commentAdmin->setRequest(new Request());
  178. $commentAdmin->initialize();
  179. $admin->initialize();
  180. $menuFactory = $this->getMock('Knp\Menu\FactoryInterface');
  181. $menu = $this->getMock('Knp\Menu\ItemInterface');
  182. $translatorStrategy = $this->getMock(
  183. 'Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface'
  184. );
  185. $routeGenerator = $this->getMock(
  186. 'Sonata\AdminBundle\Route\RouteGeneratorInterface'
  187. );
  188. $admin->setMenuFactory($menuFactory);
  189. $admin->setLabelTranslatorStrategy($translatorStrategy);
  190. $admin->setRouteGenerator($routeGenerator);
  191. $menuFactory->expects($this->any())
  192. ->method('createItem')
  193. ->with('root')
  194. ->will($this->returnValue($menu));
  195. $translatorStrategy->expects($this->any())
  196. ->method('getLabel')
  197. ->withConsecutive(
  198. array('dashboard'),
  199. array('Post_list'),
  200. array('Post_repost'),
  201. array('dashboard'),
  202. array('Post_list')
  203. )
  204. ->will($this->onConsecutiveCalls(
  205. 'someLabel',
  206. 'someOtherLabel',
  207. 'someInterestingLabel',
  208. 'someFancyLabel',
  209. 'someCoolLabel'
  210. ));
  211. $menu->expects($this->any())
  212. ->method('addChild')
  213. ->withConsecutive(
  214. array('someLabel'),
  215. array('someOtherLabel'),
  216. array('someInterestingLabel'),
  217. array('someFancyLabel'),
  218. array('someCoolLabel'),
  219. array('dummy subject representation')
  220. )
  221. ->will($this->returnValue($menu));
  222. $admin->getBreadcrumbs('repost');
  223. $admin->setSubject(new DummySubject());
  224. $flagBreadcrumb = $admin->getBreadcrumbs('flag');
  225. $this->assertSame($flagBreadcrumb, $admin->getBreadcrumbs('flag'));
  226. }
  227. public function testUnitChildGetBreadCrumbs()
  228. {
  229. $menu = $this->prophesize('Knp\Menu\ItemInterface');
  230. $menu->getParent()->willReturn(null);
  231. $dashboardMenu = $this->prophesize('Knp\Menu\ItemInterface');
  232. $dashboardMenu->getParent()->willReturn($menu);
  233. $adminListMenu = $this->prophesize('Knp\Menu\ItemInterface');
  234. $adminListMenu->getParent()->willReturn($dashboardMenu);
  235. $adminSubjectMenu = $this->prophesize('Knp\Menu\ItemInterface');
  236. $adminSubjectMenu->getParent()->willReturn($adminListMenu);
  237. $childMenu = $this->prophesize('Knp\Menu\ItemInterface');
  238. $childMenu->getParent()->willReturn($adminSubjectMenu);
  239. $leafMenu = $this->prophesize('Knp\Menu\ItemInterface');
  240. $leafMenu->getParent()->willReturn($childMenu);
  241. $action = 'my_action';
  242. $breadcrumbsBuilder = new BreadcrumbsBuilder();
  243. $admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
  244. $admin->isChild()->willReturn(false);
  245. $menuFactory = $this->prophesize('Knp\Menu\MenuFactory');
  246. $menuFactory->createItem('root')->willReturn($menu);
  247. $admin->getMenuFactory()->willReturn($menuFactory);
  248. $labelTranslatorStrategy = $this->prophesize(
  249. 'Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface'
  250. );
  251. $labelTranslatorStrategy->getLabel(
  252. 'dashboard',
  253. 'breadcrumb',
  254. 'link'
  255. )->willReturn('Dashboard');
  256. $admin->trans('Dashboard', array(), 'SonataAdminBundle')->willReturn(
  257. 'Tableau de bord'
  258. );
  259. $routeGenerator = $this->prophesize('Sonata\AdminBundle\Route\RouteGeneratorInterface');
  260. $routeGenerator->generate('sonata_admin_dashboard')->willReturn('/dashboard');
  261. $admin->getRouteGenerator()->willReturn($routeGenerator->reveal());
  262. $menu->addChild('Tableau de bord', array('uri' => '/dashboard'))->willReturn(
  263. $dashboardMenu->reveal()
  264. );
  265. $labelTranslatorStrategy->getLabel(
  266. 'my_class_name_list',
  267. 'breadcrumb',
  268. 'link'
  269. )->willReturn('My class');
  270. $labelTranslatorStrategy->getLabel(
  271. 'my_child_class_name_list',
  272. 'breadcrumb',
  273. 'link'
  274. )->willReturn('My child class');
  275. $childAdmin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
  276. $childAdmin->isChild()->willReturn(true);
  277. $childAdmin->getParent()->willReturn($admin->reveal());
  278. $childAdmin->getLabelTranslatorStrategy()
  279. ->shouldBeCalled()
  280. ->willReturn($labelTranslatorStrategy->reveal());
  281. $childAdmin->getClassnameLabel()->willReturn('my_child_class_name');
  282. $childAdmin->hasRoute('list')->willReturn(true);
  283. $childAdmin->isGranted('LIST')->willReturn(true);
  284. $childAdmin->generateUrl('list')->willReturn('/myadmin/my-object/mychildadmin/list');
  285. $childAdmin->trans('My child class', array(), null)->willReturn('Ma classe fille');
  286. $childAdmin->getCurrentChildAdmin()->willReturn(null);
  287. $childAdmin->hasSubject()->willReturn(true);
  288. $childAdmin->getSubject()->willReturn('my subject');
  289. $childAdmin->toString('my subject')->willReturn('My subject');
  290. $admin->trans('My class', array(), null)->willReturn('Ma classe');
  291. $admin->hasRoute('list')->willReturn(true);
  292. $admin->isGranted('LIST')->willReturn(true);
  293. $admin->generateUrl('list')->willReturn('/myadmin/list');
  294. $admin->getCurrentChildAdmin()->willReturn($childAdmin->reveal());
  295. $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
  296. $request->get('slug')->willReturn('my-object');
  297. $admin->getIdParameter()->willReturn('slug');
  298. $admin->hasRoute('edit')->willReturn(true);
  299. $admin->isGranted('EDIT')->willReturn(true);
  300. $admin->generateUrl('edit', array('id' => 'my-object'))->willReturn('/myadmin/my-object');
  301. $admin->getRequest()->willReturn($request->reveal());
  302. $admin->hasSubject()->willReturn(true);
  303. $admin->getSubject()->willReturn('my subject');
  304. $admin->toString('my subject')->willReturn('My subject');
  305. $admin->getLabelTranslatorStrategy()->willReturn(
  306. $labelTranslatorStrategy->reveal()
  307. );
  308. $admin->getClassnameLabel()->willReturn('my_class_name');
  309. $dashboardMenu->addChild('Ma classe', array(
  310. 'uri' => '/myadmin/list',
  311. ))->shouldBeCalled()->willReturn($adminListMenu->reveal());
  312. $adminListMenu->addChild('My subject', array(
  313. 'uri' => '/myadmin/my-object',
  314. ))->shouldBeCalled()->willReturn($adminSubjectMenu->reveal());
  315. $adminSubjectMenu->addChild('Ma classe fille', array(
  316. 'uri' => '/myadmin/my-object/mychildadmin/list',
  317. ))->shouldBeCalled()->willReturn($childMenu->reveal());
  318. $childMenu->addChild('My subject')
  319. ->shouldBeCalled()->willReturn($leafMenu->reveal());
  320. $breadcrumbs = $breadcrumbsBuilder->getBreadcrumbs($childAdmin->reveal(), $action);
  321. $this->assertCount(5, $breadcrumbs);
  322. }
  323. public function actionProvider()
  324. {
  325. return array(
  326. array('my_action'),
  327. array('list'),
  328. array('edit'),
  329. array('create'),
  330. );
  331. }
  332. /**
  333. * @dataProvider actionProvider
  334. */
  335. public function testUnitBuildBreadcrumbs($action)
  336. {
  337. $breadcrumbsBuilder = new BreadcrumbsBuilder();
  338. $menu = $this->prophesize('Knp\Menu\ItemInterface');
  339. $menuFactory = $this->prophesize('Knp\Menu\MenuFactory');
  340. $menuFactory->createItem('root')->willReturn($menu);
  341. $admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
  342. $admin->getMenuFactory()->willReturn($menuFactory);
  343. $labelTranslatorStrategy = $this->prophesize(
  344. 'Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface'
  345. );
  346. $labelTranslatorStrategy->getLabel(
  347. 'dashboard',
  348. 'breadcrumb',
  349. 'link'
  350. )->willReturn('Dashboard');
  351. $admin->trans('Dashboard', array(), 'SonataAdminBundle')->willReturn(
  352. 'Tableau de bord'
  353. );
  354. $routeGenerator = $this->prophesize('Sonata\AdminBundle\Route\RouteGeneratorInterface');
  355. $routeGenerator->generate('sonata_admin_dashboard')->willReturn('/dashboard');
  356. $admin->getRouteGenerator()->willReturn($routeGenerator->reveal());
  357. $menu->addChild('Tableau de bord', array('uri' => '/dashboard'))->willReturn(
  358. $menu->reveal()
  359. );
  360. $labelTranslatorStrategy->getLabel(
  361. 'my_class_name_list',
  362. 'breadcrumb',
  363. 'link'
  364. )->willReturn('My class');
  365. $labelTranslatorStrategy->getLabel(
  366. 'my_child_class_name_list',
  367. 'breadcrumb',
  368. 'link'
  369. )->willReturn('My child class');
  370. $labelTranslatorStrategy->getLabel(
  371. 'my_child_class_name_my_action',
  372. 'breadcrumb',
  373. 'link'
  374. )->willReturn('My action');
  375. if ($action == 'create') {
  376. $labelTranslatorStrategy->getLabel(
  377. 'my_class_name_create',
  378. 'breadcrumb',
  379. 'link'
  380. )->willReturn('create my object');
  381. $admin->trans('create my object', array(), null)->willReturn('Créer mon objet')->shouldBeCalled();
  382. $menu->addChild('Créer mon objet', array())->willReturn($menu);
  383. }
  384. $childAdmin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
  385. $childAdmin->getLabelTranslatorStrategy()->willReturn($labelTranslatorStrategy->reveal());
  386. $childAdmin->getClassnameLabel()->willReturn('my_child_class_name');
  387. $childAdmin->hasRoute('list')->willReturn(false);
  388. $childAdmin->trans('My child class', array(), null)->willReturn('Ma classe fille');
  389. $childAdmin->trans('My action', array(), null)->willReturn('Mon action');
  390. $childAdmin->getCurrentChildAdmin()->willReturn(null);
  391. $childAdmin->hasSubject()->willReturn(false);
  392. $admin->trans('My class', array(), null)->willReturn('Ma classe');
  393. $admin->hasRoute('list')->willReturn(true);
  394. $admin->isGranted('LIST')->willReturn(true);
  395. $admin->generateUrl('list')->willReturn('/myadmin/list');
  396. $admin->getCurrentChildAdmin()->willReturn(
  397. $action == 'my_action' ? $childAdmin->reveal() : false
  398. );
  399. if ($action == 'list') {
  400. $admin->isChild()->willReturn(true);
  401. $menu->setUri(false)->shouldBeCalled();
  402. }
  403. $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
  404. $request->get('slug')->willReturn('my-object');
  405. $admin->getIdParameter()->willReturn('slug');
  406. $admin->hasRoute('edit')->willReturn(false);
  407. $admin->getRequest()->willReturn($request->reveal());
  408. $admin->hasSubject()->willReturn(true);
  409. $admin->getSubject()->willReturn('my subject');
  410. $admin->toString('my subject')->willReturn('My subject');
  411. $admin->getLabelTranslatorStrategy()->willReturn(
  412. $labelTranslatorStrategy->reveal()
  413. );
  414. $admin->getClassnameLabel()->willReturn('my_class_name');
  415. $menu->addChild('Ma classe', array(
  416. 'uri' => '/myadmin/list',
  417. ))->willReturn($menu->reveal());
  418. $menu->addChild('My subject')->willReturn($menu);
  419. $menu->addChild('My subject', array('uri' => null))->willReturn($menu);
  420. $menu->addChild('Ma classe fille', array('uri' => null))->willReturn($menu);
  421. $menu->addChild('Mon action', array())->willReturn($menu);
  422. $breadcrumbsBuilder->buildBreadCrumbs($admin->reveal(), $action);
  423. }
  424. }