MenuTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once dirname(__FILE__) . '/TestAbstract.php';
  23. require_once 'Zend/View/Helper/Navigation/Menu.php';
  24. /**
  25. * Tests Zend_View_Helper_Navigation_Menu
  26. *
  27. * @category Zend
  28. * @package Zend_View
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_View
  33. * @group Zend_View_Helper
  34. */
  35. class Zend_View_Helper_Navigation_MenuTest
  36. extends Zend_View_Helper_Navigation_TestAbstract
  37. {
  38. /**
  39. * Class name for view helper to test
  40. *
  41. * @var string
  42. */
  43. protected $_helperName = 'Zend_View_Helper_Navigation_Menu';
  44. /**
  45. * View helper
  46. *
  47. * @var Zend_View_Helper_Navigation_Menu
  48. */
  49. protected $_helper;
  50. public function testHelperEntryPointWithoutAnyParams()
  51. {
  52. $returned = $this->_helper->menu();
  53. $this->assertEquals($this->_helper, $returned);
  54. $this->assertEquals($this->_nav1, $returned->getContainer());
  55. }
  56. public function testHelperEntryPointWithContainerParam()
  57. {
  58. $returned = $this->_helper->menu($this->_nav2);
  59. $this->assertEquals($this->_helper, $returned);
  60. $this->assertEquals($this->_nav2, $returned->getContainer());
  61. }
  62. public function testNullingOutContainerInHelper()
  63. {
  64. $this->_helper->setContainer();
  65. $this->assertEquals(0, count($this->_helper->getContainer()));
  66. }
  67. public function testAutoloadingContainerFromRegistry()
  68. {
  69. $oldReg = null;
  70. if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
  71. $oldReg = Zend_Registry::get(self::REGISTRY_KEY);
  72. }
  73. Zend_Registry::set(self::REGISTRY_KEY, $this->_nav1);
  74. $this->_helper->setContainer(null);
  75. $expected = $this->_getExpected('menu/default1.html');
  76. $actual = $this->_helper->render();
  77. Zend_Registry::set(self::REGISTRY_KEY, $oldReg);
  78. $this->assertEquals($expected, $actual);
  79. }
  80. public function testSetIndentAndOverrideInRenderMenu()
  81. {
  82. $this->_helper->setIndent(8);
  83. $expected = array(
  84. 'indent4' => $this->_getExpected('menu/indent4.html'),
  85. 'indent8' => $this->_getExpected('menu/indent8.html')
  86. );
  87. $renderOptions = array(
  88. 'indent' => 4
  89. );
  90. $actual = array(
  91. 'indent4' => rtrim($this->_helper->renderMenu(null, $renderOptions), PHP_EOL),
  92. 'indent8' => rtrim($this->_helper->renderMenu(), PHP_EOL)
  93. );
  94. $this->assertEquals($expected, $actual);
  95. }
  96. public function testRenderSuppliedContainerWithoutInterfering()
  97. {
  98. $rendered1 = $this->_getExpected('menu/default1.html');
  99. $rendered2 = $this->_getExpected('menu/default2.html');
  100. $expected = array(
  101. 'registered' => $rendered1,
  102. 'supplied' => $rendered2,
  103. 'registered_again' => $rendered1
  104. );
  105. $actual = array(
  106. 'registered' => $this->_helper->render(),
  107. 'supplied' => $this->_helper->render($this->_nav2),
  108. 'registered_again' => $this->_helper->render()
  109. );
  110. $this->assertEquals($expected, $actual);
  111. }
  112. public function testUseAclRoleAsString()
  113. {
  114. $acl = $this->_getAcl();
  115. $this->_helper->setAcl($acl['acl']);
  116. $this->_helper->setRole('member');
  117. $expected = $this->_getExpected('menu/acl_string.html');
  118. $this->assertEquals($expected, $this->_helper->render());
  119. }
  120. public function testFilterOutPagesBasedOnAcl()
  121. {
  122. $acl = $this->_getAcl();
  123. $this->_helper->setAcl($acl['acl']);
  124. $this->_helper->setRole($acl['role']);
  125. $expected = $this->_getExpected('menu/acl.html');
  126. $actual = $this->_helper->render();
  127. $this->assertEquals($expected, $actual);
  128. }
  129. public function testDisablingAcl()
  130. {
  131. $acl = $this->_getAcl();
  132. $this->_helper->setAcl($acl['acl']);
  133. $this->_helper->setRole($acl['role']);
  134. $this->_helper->setUseAcl(false);
  135. $expected = $this->_getExpected('menu/default1.html');
  136. $actual = $this->_helper->render();
  137. $this->assertEquals($expected, $actual);
  138. }
  139. public function testUseAnAclRoleInstanceFromAclObject()
  140. {
  141. $acl = $this->_getAcl();
  142. $this->_helper->setAcl($acl['acl']);
  143. $this->_helper->setRole($acl['acl']->getRole('member'));
  144. $expected = $this->_getExpected('menu/acl_role_interface.html');
  145. $this->assertEquals($expected, $this->_helper->render());
  146. }
  147. public function testUseConstructedAclRolesNotFromAclObject()
  148. {
  149. $acl = $this->_getAcl();
  150. $this->_helper->setAcl($acl['acl']);
  151. $this->_helper->setRole(new Zend_Acl_Role('member'));
  152. $expected = $this->_getExpected('menu/acl_role_interface.html');
  153. $this->assertEquals($expected, $this->_helper->render());
  154. }
  155. public function testSetUlCssClass()
  156. {
  157. $this->_helper->setUlClass('My_Nav');
  158. $expected = $this->_getExpected('menu/css.html');
  159. $this->assertEquals($expected, $this->_helper->render($this->_nav2));
  160. }
  161. public function testTranslationUsingZendTranslate()
  162. {
  163. $translator = $this->_getTranslator();
  164. $this->_helper->setTranslator($translator);
  165. $expected = $this->_getExpected('menu/translated.html');
  166. $this->assertEquals($expected, $this->_helper->render());
  167. }
  168. public function testTranslationUsingZendTranslateAdapter()
  169. {
  170. $translator = $this->_getTranslator();
  171. $this->_helper->setTranslator($translator->getAdapter());
  172. $expected = $this->_getExpected('menu/translated.html');
  173. $this->assertEquals($expected, $this->_helper->render());
  174. }
  175. public function testTranslationUsingTranslatorFromRegistry()
  176. {
  177. $oldReg = Zend_Registry::isRegistered('Zend_Translate')
  178. ? Zend_Registry::get('Zend_Translate')
  179. : null;
  180. $translator = $this->_getTranslator();
  181. Zend_Registry::set('Zend_Translate', $translator);
  182. $expected = $this->_getExpected('menu/translated.html');
  183. $actual = $this->_helper->render();
  184. Zend_Registry::set('Zend_Translate', $oldReg);
  185. $this->assertEquals($expected, $actual);
  186. }
  187. public function testDisablingTranslation()
  188. {
  189. $translator = $this->_getTranslator();
  190. $this->_helper->setTranslator($translator);
  191. $this->_helper->setUseTranslator(false);
  192. $expected = $this->_getExpected('menu/default1.html');
  193. $this->assertEquals($expected, $this->_helper->render());
  194. }
  195. public function testRenderingPartial()
  196. {
  197. $this->_helper->setPartial('menu.phtml');
  198. $expected = $this->_getExpected('menu/partial.html');
  199. $actual = $this->_helper->render();
  200. $this->assertEquals($expected, $actual);
  201. }
  202. public function testRenderingPartialBySpecifyingAnArrayAsPartial()
  203. {
  204. $this->_helper->setPartial(array('menu.phtml', 'default'));
  205. $expected = $this->_getExpected('menu/partial.html');
  206. $actual = $this->_helper->render();
  207. $this->assertEquals($expected, $actual);
  208. }
  209. public function testRenderingPartialShouldFailOnInvalidPartialArray()
  210. {
  211. $this->_helper->setPartial(array('menu.phtml'));
  212. try {
  213. $this->_helper->render();
  214. $this->fail('invalid $partial should throw Zend_View_Exception');
  215. } catch (Zend_View_Exception $e) {
  216. }
  217. }
  218. public function testSetMaxDepth()
  219. {
  220. $this->_helper->setMaxDepth(1);
  221. $expected = $this->_getExpected('menu/maxdepth.html');
  222. $actual = $this->_helper->renderMenu();
  223. $this->assertEquals($expected, $actual);
  224. }
  225. public function testSetMinDepth()
  226. {
  227. $this->_helper->setMinDepth(1);
  228. $expected = $this->_getExpected('menu/mindepth.html');
  229. $actual = $this->_helper->renderMenu();
  230. $this->assertEquals($expected, $actual);
  231. }
  232. public function testSetBothDepts()
  233. {
  234. $this->_helper->setMinDepth(1)->setMaxDepth(2);
  235. $expected = $this->_getExpected('menu/bothdepts.html');
  236. $actual = $this->_helper->renderMenu();
  237. $this->assertEquals($expected, $actual);
  238. }
  239. public function testSetOnlyActiveBranch()
  240. {
  241. $this->_helper->setOnlyActiveBranch(true);
  242. $expected = $this->_getExpected('menu/onlyactivebranch.html');
  243. $actual = $this->_helper->renderMenu();
  244. $this->assertEquals($expected, $actual);
  245. }
  246. public function testSetRenderParents()
  247. {
  248. $this->_helper->setOnlyActiveBranch(true)->setRenderParents(false);
  249. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  250. $actual = $this->_helper->renderMenu();
  251. $this->assertEquals($expected, $actual);
  252. }
  253. public function testSetOnlyActiveBranchAndMinDepth()
  254. {
  255. $this->_helper->setOnlyActiveBranch()->setMinDepth(1);
  256. $expected = $this->_getExpected('menu/onlyactivebranch_mindepth.html');
  257. $actual = $this->_helper->renderMenu();
  258. $this->assertEquals($expected, $actual);
  259. }
  260. public function testOnlyActiveBranchAndMaxDepth()
  261. {
  262. $this->_helper->setOnlyActiveBranch()->setMaxDepth(2);
  263. $expected = $this->_getExpected('menu/onlyactivebranch_maxdepth.html');
  264. $actual = $this->_helper->renderMenu();
  265. $this->assertEquals($expected, $actual);
  266. }
  267. public function testOnlyActiveBranchAndBothDepthsSpecified()
  268. {
  269. $this->_helper->setOnlyActiveBranch()->setMinDepth(1)->setMaxDepth(2);
  270. $expected = $this->_getExpected('menu/onlyactivebranch_bothdepts.html');
  271. $actual = $this->_helper->renderMenu();
  272. $this->assertEquals($expected, $actual);
  273. }
  274. public function testOnlyActiveBranchNoParentsAndBothDepthsSpecified()
  275. {
  276. $this->_helper->setOnlyActiveBranch()
  277. ->setMinDepth(1)
  278. ->setMaxDepth(2)
  279. ->setRenderParents(false);
  280. $expected = $this->_getExpected('menu/onlyactivebranch_np_bd.html');
  281. $actual = $this->_helper->renderMenu();
  282. $this->assertEquals($expected, $actual);
  283. }
  284. private function _setActive($label)
  285. {
  286. $container = $this->_helper->getContainer();
  287. foreach ($container->findAllByActive(true) as $page) {
  288. $page->setActive(false);
  289. }
  290. if ($p = $container->findOneByLabel($label)) {
  291. $p->setActive(true);
  292. }
  293. }
  294. public function testOnlyActiveBranchNoParentsActiveOneBelowMinDepth()
  295. {
  296. $this->_setActive('Page 2');
  297. $this->_helper->setOnlyActiveBranch()
  298. ->setMinDepth(1)
  299. ->setMaxDepth(1)
  300. ->setRenderParents(false);
  301. $expected = $this->_getExpected('menu/onlyactivebranch_np_bd2.html');
  302. $actual = $this->_helper->renderMenu();
  303. $this->assertEquals($expected, $actual);
  304. }
  305. public function testRenderSubMenuShouldOverrideOptions()
  306. {
  307. $this->_helper->setOnlyActiveBranch(false)
  308. ->setMinDepth(1)
  309. ->setMaxDepth(2)
  310. ->setRenderParents(true);
  311. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  312. $actual = $this->_helper->renderSubMenu();
  313. $this->assertEquals($expected, $actual);
  314. }
  315. public function testOptionMaxDepth()
  316. {
  317. $options = array(
  318. 'maxDepth' => 1
  319. );
  320. $expected = $this->_getExpected('menu/maxdepth.html');
  321. $actual = $this->_helper->renderMenu(null, $options);
  322. $this->assertEquals($expected, $actual);
  323. }
  324. public function testOptionMinDepth()
  325. {
  326. $options = array(
  327. 'minDepth' => 1
  328. );
  329. $expected = $this->_getExpected('menu/mindepth.html');
  330. $actual = $this->_helper->renderMenu(null, $options);
  331. $this->assertEquals($expected, $actual);
  332. }
  333. public function testOptionBothDepts()
  334. {
  335. $options = array(
  336. 'minDepth' => 1,
  337. 'maxDepth' => 2
  338. );
  339. $expected = $this->_getExpected('menu/bothdepts.html');
  340. $actual = $this->_helper->renderMenu(null, $options);
  341. $this->assertEquals($expected, $actual);
  342. }
  343. public function testOptionOnlyActiveBranch()
  344. {
  345. $options = array(
  346. 'onlyActiveBranch' => true
  347. );
  348. $expected = $this->_getExpected('menu/onlyactivebranch.html');
  349. $actual = $this->_helper->renderMenu(null, $options);
  350. $this->assertEquals($expected, $actual);
  351. }
  352. public function testOptionOnlyActiveBranchNoParents()
  353. {
  354. $options = array(
  355. 'onlyActiveBranch' => true,
  356. 'renderParents' => false
  357. );
  358. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  359. $actual = $this->_helper->renderMenu(null, $options);
  360. $this->assertEquals($expected, $actual);
  361. }
  362. public function testOptionOnlyActiveBranchAndMinDepth()
  363. {
  364. $options = array(
  365. 'minDepth' => 1,
  366. 'onlyActiveBranch' => true
  367. );
  368. $expected = $this->_getExpected('menu/onlyactivebranch_mindepth.html');
  369. $actual = $this->_helper->renderMenu(null, $options);
  370. $this->assertEquals($expected, $actual);
  371. }
  372. public function testOptionOnlyActiveBranchAndMaxDepth()
  373. {
  374. $options = array(
  375. 'maxDepth' => 2,
  376. 'onlyActiveBranch' => true
  377. );
  378. $expected = $this->_getExpected('menu/onlyactivebranch_maxdepth.html');
  379. $actual = $this->_helper->renderMenu(null, $options);
  380. $this->assertEquals($expected, $actual);
  381. }
  382. public function testOptionOnlyActiveBranchAndBothDepthsSpecified()
  383. {
  384. $options = array(
  385. 'minDepth' => 1,
  386. 'maxDepth' => 2,
  387. 'onlyActiveBranch' => true
  388. );
  389. $expected = $this->_getExpected('menu/onlyactivebranch_bothdepts.html');
  390. $actual = $this->_helper->renderMenu(null, $options);
  391. $this->assertEquals($expected, $actual);
  392. }
  393. public function testOptionOnlyActiveBranchNoParentsAndBothDepthsSpecified()
  394. {
  395. $options = array(
  396. 'minDepth' => 2,
  397. 'maxDepth' => 2,
  398. 'onlyActiveBranch' => true,
  399. 'renderParents' => false
  400. );
  401. $expected = $this->_getExpected('menu/onlyactivebranch_np_bd.html');
  402. $actual = $this->_helper->renderMenu(null, $options);
  403. $this->assertEquals($expected, $actual);
  404. }
  405. /**
  406. * @group ZF-9746
  407. */
  408. public function testRenderingWithAccesskey()
  409. {
  410. $this->_nav3->findOneBy('id', 'home')->setAccesskey('H');
  411. $this->_nav3->findOneBy('uri', 'contact')->setAccesskey('c');
  412. $this->_nav3->findOneBy('id', 'imprint')->setAccesskey('i');
  413. $expected = $this->_getExpected('menu/accesskey.html');
  414. $this->assertEquals($expected, $this->_helper->render($this->_nav3));
  415. }
  416. /**
  417. * @group ZF-6941
  418. */
  419. public function testExpandSiblingNodesOfActiveBranch()
  420. {
  421. $this->_helper->setExpandSiblingNodesOfActiveBranch(true);
  422. $expected = $this->_getExpected('menu/expandbranch.html');
  423. $actual = $this->_helper->renderMenu();
  424. $this->assertEquals($expected, $actual);
  425. }
  426. /**
  427. * @group ZF-6941
  428. */
  429. public function testExpandSiblingNodesOfActiveBranchWhenShowingOnlyActiveBranch()
  430. {
  431. $this->_helper->setExpandSiblingNodesOfActiveBranch(true)->setOnlyActiveBranch(true);
  432. $expected = $this->_getExpected('menu/expandbranch_onlyactivebranch.html');
  433. $actual = $this->_helper->renderMenu();
  434. $this->assertEquals($expected, $actual);
  435. }
  436. /**
  437. * @group ZF-11876
  438. */
  439. public function testRenderingWithCustomHtmlAttribs()
  440. {
  441. $this->_nav3->findOneBy('id', 'home')->setCustomHtmlAttrib('rel', 'nofollow');
  442. $this->_nav3->findOneBy('uri', 'contact')->setCustomHtmlAttribs(
  443. array(
  444. 'rel' => 'nofollow',
  445. 'style' => 'font-weight: bold;',
  446. )
  447. );
  448. $this->_nav3->findOneBy('id', 'imprint')->setCustomHtmlAttrib('rel', 'nofollow');
  449. $expected = $this->_getExpected('menu/customhtmlattribs.html');
  450. $this->assertEquals($expected, $this->_helper->render($this->_nav3));
  451. }
  452. }