2
0

MenuTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. public function testRenderSubMenuShouldOverrideOptions()
  258. {
  259. $this->_helper->setOnlyActiveBranch(false)
  260. ->setMinDepth(1)
  261. ->setMaxDepth(2)
  262. ->setRenderParents(true);
  263. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  264. $actual = $this->_helper->renderSubMenu();
  265. $this->assertEquals($expected, $actual);
  266. }
  267. public function testOptionMaxDepth()
  268. {
  269. $options = array(
  270. 'maxDepth' => 1
  271. );
  272. $expected = $this->_getExpected('menu/maxdepth.html');
  273. $actual = $this->_helper->renderMenu(null, $options);
  274. $this->assertEquals($expected, $actual);
  275. }
  276. public function testOptionMinDepth()
  277. {
  278. $options = array(
  279. 'minDepth' => 1
  280. );
  281. $expected = $this->_getExpected('menu/mindepth.html');
  282. $actual = $this->_helper->renderMenu(null, $options);
  283. $this->assertEquals($expected, $actual);
  284. }
  285. public function testOptionBothDepts()
  286. {
  287. $options = array(
  288. 'minDepth' => 1,
  289. 'maxDepth' => 2
  290. );
  291. $expected = $this->_getExpected('menu/bothdepts.html');
  292. $actual = $this->_helper->renderMenu(null, $options);
  293. $this->assertEquals($expected, $actual);
  294. }
  295. public function testOptionOnlyActiveBranch()
  296. {
  297. $options = array(
  298. 'onlyActiveBranch' => true
  299. );
  300. $expected = $this->_getExpected('menu/onlyactivebranch.html');
  301. $actual = $this->_helper->renderMenu(null, $options);
  302. $this->assertEquals($expected, $actual);
  303. }
  304. public function testOptionOnlyActiveBranchNoParents()
  305. {
  306. $options = array(
  307. 'onlyActiveBranch' => true,
  308. 'renderParents' => false
  309. );
  310. $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
  311. $actual = $this->_helper->renderMenu(null, $options);
  312. $this->assertEquals($expected, $actual);
  313. }
  314. public function testOptionOnlyActiveBranchAndMinDepth()
  315. {
  316. $options = array(
  317. 'minDepth' => 1,
  318. 'onlyActiveBranch' => true
  319. );
  320. $expected = $this->_getExpected('menu/onlyactivebranch_mindepth.html');
  321. $actual = $this->_helper->renderMenu(null, $options);
  322. $this->assertEquals($expected, $actual);
  323. }
  324. public function testOptionOnlyActiveBranchAndMaxDepth()
  325. {
  326. $options = array(
  327. 'maxDepth' => 2,
  328. 'onlyActiveBranch' => true
  329. );
  330. $expected = $this->_getExpected('menu/onlyactivebranch_maxdepth.html');
  331. $actual = $this->_helper->renderMenu(null, $options);
  332. $this->assertEquals($expected, $actual);
  333. }
  334. public function testOptionOnlyActiveBranchAndBothDepthsSpecified()
  335. {
  336. $options = array(
  337. 'minDepth' => 1,
  338. 'maxDepth' => 2,
  339. 'onlyActiveBranch' => true
  340. );
  341. $expected = $this->_getExpected('menu/onlyactivebranch_bothdepts.html');
  342. $actual = $this->_helper->renderMenu(null, $options);
  343. $this->assertEquals($expected, $actual);
  344. }
  345. public function testOptionOnlyActiveBranchNoParentsAndBothDepthsSpecified()
  346. {
  347. $options = array(
  348. 'minDepth' => 2,
  349. 'maxDepth' => 2,
  350. 'onlyActiveBranch' => true,
  351. 'renderParents' => false
  352. );
  353. $expected = $this->_getExpected('menu/onlyactivebranch_np_bd.html');
  354. $actual = $this->_helper->renderMenu(null, $options);
  355. $this->assertEquals($expected, $actual);
  356. }
  357. }