BreadcrumbsBuilderTest.php 19 KB

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