MenuTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. require_once dirname(__FILE__) . '/TestAbstract.php';
  3. require_once 'Zend/View/Helper/Navigation/Menu.php';
  4. /**
  5. * Tests Zend_View_Helper_Navigation_Menu
  6. *
  7. */
  8. class Zend_View_Helper_Navigation_MenuTest
  9. extends Zend_View_Helper_Navigation_TestAbstract
  10. {
  11. /**
  12. * Class name for view helper to test
  13. *
  14. * @var string
  15. */
  16. protected $_helperName = 'Zend_View_Helper_Navigation_Menu';
  17. /**
  18. * View helper
  19. *
  20. * @var Zend_View_Helper_Navigation_Menu
  21. */
  22. protected $_helper;
  23. public function testHelperEntryPointWithoutAnyParams()
  24. {
  25. $returned = $this->_helper->menu();
  26. $this->assertEquals($this->_helper, $returned);
  27. $this->assertEquals($this->_nav1, $returned->getContainer());
  28. }
  29. public function testHelperEntryPointWithContainerParam()
  30. {
  31. $returned = $this->_helper->menu($this->_nav2);
  32. $this->assertEquals($this->_helper, $returned);
  33. $this->assertEquals($this->_nav2, $returned->getContainer());
  34. }
  35. public function testNullingOutContainerInHelper()
  36. {
  37. $this->_helper->setContainer();
  38. $this->assertEquals(0, count($this->_helper->getContainer()));
  39. }
  40. public function testAutoloadingContainerFromRegistry()
  41. {
  42. $oldReg = null;
  43. if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
  44. $oldReg = Zend_Registry::get(self::REGISTRY_KEY);
  45. }
  46. Zend_Registry::set(self::REGISTRY_KEY, $this->_nav1);
  47. $this->_helper->setContainer(null);
  48. $expected = $this->_getExpected('menu/default1.html');
  49. $actual = $this->_helper->render();
  50. Zend_Registry::set(self::REGISTRY_KEY, $oldReg);
  51. $this->assertEquals($expected, $actual);
  52. }
  53. public function testSetIndentAndOverrideInRenderMenu()
  54. {
  55. $this->_helper->setIndent(8);
  56. $expected = array(
  57. 'indent4' => $this->_getExpected('menu/indent4.html'),
  58. 'indent8' => $this->_getExpected('menu/indent8.html')
  59. );
  60. $renderOptions = array(
  61. 'indent' => 4
  62. );
  63. $actual = array(
  64. 'indent4' => rtrim($this->_helper->renderMenu(null, $renderOptions), PHP_EOL),
  65. 'indent8' => rtrim($this->_helper->renderMenu(), PHP_EOL)
  66. );
  67. $this->assertEquals($expected, $actual);
  68. }
  69. public function testRenderSuppliedContainerWithoutInterfering()
  70. {
  71. $rendered1 = $this->_getExpected('menu/default1.html');
  72. $rendered2 = $this->_getExpected('menu/default2.html');
  73. $expected = array(
  74. 'registered' => $rendered1,
  75. 'supplied' => $rendered2,
  76. 'registered_again' => $rendered1
  77. );
  78. $actual = array(
  79. 'registered' => $this->_helper->render(),
  80. 'supplied' => $this->_helper->render($this->_nav2),
  81. 'registered_again' => $this->_helper->render()
  82. );
  83. $this->assertEquals($expected, $actual);
  84. }
  85. public function testUseAclRoleAsString()
  86. {
  87. $acl = $this->_getAcl();
  88. $this->_helper->setAcl($acl['acl']);
  89. $this->_helper->setRole('member');
  90. $expected = $this->_getExpected('menu/acl_string.html');
  91. $this->assertEquals($expected, $this->_helper->render());
  92. }
  93. public function testFilterOutPagesBasedOnAcl()
  94. {
  95. $acl = $this->_getAcl();
  96. $this->_helper->setAcl($acl['acl']);
  97. $this->_helper->setRole($acl['role']);
  98. $expected = $this->_getExpected('menu/acl.html');
  99. $actual = $this->_helper->render();
  100. $this->assertEquals($expected, $actual);
  101. }
  102. public function testDisablingAcl()
  103. {
  104. $acl = $this->_getAcl();
  105. $this->_helper->setAcl($acl['acl']);
  106. $this->_helper->setRole($acl['role']);
  107. $this->_helper->setUseAcl(false);
  108. $expected = $this->_getExpected('menu/default1.html');
  109. $actual = $this->_helper->render();
  110. $this->assertEquals($expected, $actual);
  111. }
  112. public function testUseAnAclRoleInstanceFromAclObject()
  113. {
  114. $acl = $this->_getAcl();
  115. $this->_helper->setAcl($acl['acl']);
  116. $this->_helper->setRole($acl['acl']->getRole('member'));
  117. $expected = $this->_getExpected('menu/acl_role_interface.html');
  118. $this->assertEquals($expected, $this->_helper->render());
  119. }
  120. public function testUseConstructedAclRolesNotFromAclObject()
  121. {
  122. $acl = $this->_getAcl();
  123. $this->_helper->setAcl($acl['acl']);
  124. $this->_helper->setRole(new Zend_Acl_Role('member'));
  125. $expected = $this->_getExpected('menu/acl_role_interface.html');
  126. $this->assertEquals($expected, $this->_helper->render());
  127. }
  128. public function testSetUlCssClass()
  129. {
  130. $this->_helper->setUlClass('My_Nav');
  131. $expected = $this->_getExpected('menu/css.html');
  132. $this->assertEquals($expected, $this->_helper->render($this->_nav2));
  133. }
  134. public function testTranslationUsingZendTranslate()
  135. {
  136. $translator = $this->_getTranslator();
  137. $this->_helper->setTranslator($translator);
  138. $expected = $this->_getExpected('menu/translated.html');
  139. $this->assertEquals($expected, $this->_helper->render());
  140. }
  141. public function testTranslationUsingZendTranslateAdapter()
  142. {
  143. $translator = $this->_getTranslator();
  144. $this->_helper->setTranslator($translator->getAdapter());
  145. $expected = $this->_getExpected('menu/translated.html');
  146. $this->assertEquals($expected, $this->_helper->render());
  147. }
  148. public function testTranslationUsingTranslatorFromRegistry()
  149. {
  150. $oldReg = Zend_Registry::isRegistered('Zend_Translate')
  151. ? Zend_Registry::get('Zend_Translate')
  152. : null;
  153. $translator = $this->_getTranslator();
  154. Zend_Registry::set('Zend_Translate', $translator);
  155. $expected = $this->_getExpected('menu/translated.html');
  156. $actual = $this->_helper->render();
  157. Zend_Registry::set('Zend_Translate', $oldReg);
  158. $this->assertEquals($expected, $actual);
  159. }
  160. public function testDisablingTranslation()
  161. {
  162. $translator = $this->_getTranslator();
  163. $this->_helper->setTranslator($translator);
  164. $this->_helper->setUseTranslator(false);
  165. $expected = $this->_getExpected('menu/default1.html');
  166. $this->assertEquals($expected, $this->_helper->render());
  167. }
  168. public function testRenderingPartial()
  169. {
  170. $this->_helper->setPartial('menu.phtml');
  171. $expected = $this->_getExpected('menu/partial.html');
  172. $actual = $this->_helper->render();
  173. $this->assertEquals($expected, $actual);
  174. }
  175. public function testRenderingPartialBySpecifyingAnArrayAsPartial()
  176. {
  177. $this->_helper->setPartial(array('menu.phtml', 'default'));
  178. $expected = $this->_getExpected('menu/partial.html');
  179. $actual = $this->_helper->render();
  180. $this->assertEquals($expected, $actual);
  181. }
  182. public function testRenderingPartialShouldFailOnInvalidPartialArray()
  183. {
  184. $this->_helper->setPartial(array('menu.phtml'));
  185. try {
  186. $this->_helper->render();
  187. $this->fail('invalid $partial should throw Zend_View_Exception');
  188. } catch (Zend_View_Exception $e) {
  189. }
  190. }
  191. public function testSetMaxDepth()
  192. {
  193. $this->_helper->setMaxDepth(1);
  194. $expected = $this->_getExpected('menu/maxdepth.html');
  195. $actual = $this->_helper->renderMenu();
  196. $this->assertEquals($expected, $actual);
  197. }
  198. public function testSetMinDepth()
  199. {
  200. $this->_helper->setMinDepth(1);
  201. $expected = $this->_getExpected('menu/mindepth.html');
  202. $actual = $this->_helper->renderMenu();
  203. $this->assertEquals($expected, $actual);
  204. }
  205. public function testSetBothDepts()
  206. {
  207. $this->_helper->setMinDepth(1)->setMaxDepth(2);
  208. $expected = $this->_getExpected('menu/bothdepts.html');
  209. $actual = $this->_helper->renderMenu();
  210. $this->assertEquals($expected, $actual);
  211. }
  212. public function testSetOnlyActiveBranch()
  213. {
  214. $this->_helper->setOnlyActiveBranch(true);
  215. $expected = $this->_getExpected('menu/onlyactivebranch.html');
  216. $actual = $this->_helper->renderMenu();
  217. $this->assertEquals($expected, $actual);
  218. }
  219. public function testSetRenderParents()
  220. {
  221. $this->_helper->setOnlyActiveBranch(true)->setRenderParents(false);
  222. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  223. $actual = $this->_helper->renderMenu();
  224. $this->assertEquals($expected, $actual);
  225. }
  226. public function testSetOnlyActiveBranchAndMinDepth()
  227. {
  228. $this->_helper->setOnlyActiveBranch()->setMinDepth(1);
  229. $expected = $this->_getExpected('menu/onlyactivebranch_mindepth.html');
  230. $actual = $this->_helper->renderMenu();
  231. $this->assertEquals($expected, $actual);
  232. }
  233. public function testOnlyActiveBranchAndMaxDepth()
  234. {
  235. $this->_helper->setOnlyActiveBranch()->setMaxDepth(2);
  236. $expected = $this->_getExpected('menu/onlyactivebranch_maxdepth.html');
  237. $actual = $this->_helper->renderMenu();
  238. $this->assertEquals($expected, $actual);
  239. }
  240. public function testOnlyActiveBranchAndBothDepthsSpecified()
  241. {
  242. $this->_helper->setOnlyActiveBranch()->setMinDepth(1)->setMaxDepth(2);
  243. $expected = $this->_getExpected('menu/onlyactivebranch_bothdepts.html');
  244. $actual = $this->_helper->renderMenu();
  245. $this->assertEquals($expected, $actual);
  246. }
  247. public function testOnlyActiveBranchNoParentsAndBothDepthsSpecified()
  248. {
  249. $this->_helper->setOnlyActiveBranch()
  250. ->setMinDepth(1)
  251. ->setMaxDepth(2)
  252. ->setRenderParents(false);
  253. $expected = $this->_getExpected('menu/onlyactivebranch_np_bd.html');
  254. $actual = $this->_helper->renderMenu();
  255. $this->assertEquals($expected, $actual);
  256. }
  257. private function _setActive($label)
  258. {
  259. $container = $this->_helper->getContainer();
  260. foreach ($container->findAllByActive(true) as $page) {
  261. $page->setActive(false);
  262. }
  263. if ($p = $container->findOneByLabel($label)) {
  264. $p->setActive(true);
  265. }
  266. }
  267. public function testOnlyActiveBranchNoParentsActiveOneBelowMinDepth()
  268. {
  269. $this->_setActive('Page 2');
  270. $this->_helper->setOnlyActiveBranch()
  271. ->setMinDepth(1)
  272. ->setMaxDepth(1)
  273. ->setRenderParents(false);
  274. $expected = $this->_getExpected('menu/onlyactivebranch_np_bd2.html');
  275. $actual = $this->_helper->renderMenu();
  276. $this->assertEquals($expected, $actual);
  277. }
  278. public function testRenderSubMenuShouldOverrideOptions()
  279. {
  280. $this->_helper->setOnlyActiveBranch(false)
  281. ->setMinDepth(1)
  282. ->setMaxDepth(2)
  283. ->setRenderParents(true);
  284. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  285. $actual = $this->_helper->renderSubMenu();
  286. $this->assertEquals($expected, $actual);
  287. }
  288. public function testOptionMaxDepth()
  289. {
  290. $options = array(
  291. 'maxDepth' => 1
  292. );
  293. $expected = $this->_getExpected('menu/maxdepth.html');
  294. $actual = $this->_helper->renderMenu(null, $options);
  295. $this->assertEquals($expected, $actual);
  296. }
  297. public function testOptionMinDepth()
  298. {
  299. $options = array(
  300. 'minDepth' => 1
  301. );
  302. $expected = $this->_getExpected('menu/mindepth.html');
  303. $actual = $this->_helper->renderMenu(null, $options);
  304. $this->assertEquals($expected, $actual);
  305. }
  306. public function testOptionBothDepts()
  307. {
  308. $options = array(
  309. 'minDepth' => 1,
  310. 'maxDepth' => 2
  311. );
  312. $expected = $this->_getExpected('menu/bothdepts.html');
  313. $actual = $this->_helper->renderMenu(null, $options);
  314. $this->assertEquals($expected, $actual);
  315. }
  316. public function testOptionOnlyActiveBranch()
  317. {
  318. $options = array(
  319. 'onlyActiveBranch' => true
  320. );
  321. $expected = $this->_getExpected('menu/onlyactivebranch.html');
  322. $actual = $this->_helper->renderMenu(null, $options);
  323. $this->assertEquals($expected, $actual);
  324. }
  325. public function testOptionOnlyActiveBranchNoParents()
  326. {
  327. $options = array(
  328. 'onlyActiveBranch' => true,
  329. 'renderParents' => false
  330. );
  331. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  332. $actual = $this->_helper->renderMenu(null, $options);
  333. $this->assertEquals($expected, $actual);
  334. }
  335. public function testOptionOnlyActiveBranchAndMinDepth()
  336. {
  337. $options = array(
  338. 'minDepth' => 1,
  339. 'onlyActiveBranch' => true
  340. );
  341. $expected = $this->_getExpected('menu/onlyactivebranch_mindepth.html');
  342. $actual = $this->_helper->renderMenu(null, $options);
  343. $this->assertEquals($expected, $actual);
  344. }
  345. public function testOptionOnlyActiveBranchAndMaxDepth()
  346. {
  347. $options = array(
  348. 'maxDepth' => 2,
  349. 'onlyActiveBranch' => true
  350. );
  351. $expected = $this->_getExpected('menu/onlyactivebranch_maxdepth.html');
  352. $actual = $this->_helper->renderMenu(null, $options);
  353. $this->assertEquals($expected, $actual);
  354. }
  355. public function testOptionOnlyActiveBranchAndBothDepthsSpecified()
  356. {
  357. $options = array(
  358. 'minDepth' => 1,
  359. 'maxDepth' => 2,
  360. 'onlyActiveBranch' => true
  361. );
  362. $expected = $this->_getExpected('menu/onlyactivebranch_bothdepts.html');
  363. $actual = $this->_helper->renderMenu(null, $options);
  364. $this->assertEquals($expected, $actual);
  365. }
  366. public function testOptionOnlyActiveBranchNoParentsAndBothDepthsSpecified()
  367. {
  368. $options = array(
  369. 'minDepth' => 2,
  370. 'maxDepth' => 2,
  371. 'onlyActiveBranch' => true,
  372. 'renderParents' => false
  373. );
  374. $expected = $this->_getExpected('menu/onlyactivebranch_np_bd.html');
  375. $actual = $this->_helper->renderMenu(null, $options);
  376. $this->assertEquals($expected, $actual);
  377. }
  378. }