Fixes ZF-7341: Zend_View_Helper_Menu::_renderDeepestMenu() now has a special case...
[zend.git] / tests / Zend / View / Helper / Navigation / MenuTest.php
blob3d3f7d22e81a2610ca03eb33e2aad01f73d8d253
1 <?php
2 require_once dirname(__FILE__) . '/TestAbstract.php';
3 require_once 'Zend/View/Helper/Navigation/Menu.php';
5 /**
6 * Tests Zend_View_Helper_Navigation_Menu
8 */
9 class Zend_View_Helper_Navigation_MenuTest
10 extends Zend_View_Helper_Navigation_TestAbstract
12 /**
13 * Class name for view helper to test
15 * @var string
17 protected $_helperName = 'Zend_View_Helper_Navigation_Menu';
19 /**
20 * View helper
22 * @var Zend_View_Helper_Navigation_Menu
24 protected $_helper;
26 public function testHelperEntryPointWithoutAnyParams()
28 $returned = $this->_helper->menu();
29 $this->assertEquals($this->_helper, $returned);
30 $this->assertEquals($this->_nav1, $returned->getContainer());
33 public function testHelperEntryPointWithContainerParam()
35 $returned = $this->_helper->menu($this->_nav2);
36 $this->assertEquals($this->_helper, $returned);
37 $this->assertEquals($this->_nav2, $returned->getContainer());
40 public function testNullingOutContainerInHelper()
42 $this->_helper->setContainer();
43 $this->assertEquals(0, count($this->_helper->getContainer()));
46 public function testAutoloadingContainerFromRegistry()
48 $oldReg = null;
49 if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
50 $oldReg = Zend_Registry::get(self::REGISTRY_KEY);
52 Zend_Registry::set(self::REGISTRY_KEY, $this->_nav1);
54 $this->_helper->setContainer(null);
56 $expected = $this->_getExpected('menu/default1.html');
57 $actual = $this->_helper->render();
59 Zend_Registry::set(self::REGISTRY_KEY, $oldReg);
61 $this->assertEquals($expected, $actual);
64 public function testSetIndentAndOverrideInRenderMenu()
66 $this->_helper->setIndent(8);
68 $expected = array(
69 'indent4' => $this->_getExpected('menu/indent4.html'),
70 'indent8' => $this->_getExpected('menu/indent8.html')
73 $renderOptions = array(
74 'indent' => 4
77 $actual = array(
78 'indent4' => rtrim($this->_helper->renderMenu(null, $renderOptions), PHP_EOL),
79 'indent8' => rtrim($this->_helper->renderMenu(), PHP_EOL)
82 $this->assertEquals($expected, $actual);
85 public function testRenderSuppliedContainerWithoutInterfering()
87 $rendered1 = $this->_getExpected('menu/default1.html');
88 $rendered2 = $this->_getExpected('menu/default2.html');
89 $expected = array(
90 'registered' => $rendered1,
91 'supplied' => $rendered2,
92 'registered_again' => $rendered1
95 $actual = array(
96 'registered' => $this->_helper->render(),
97 'supplied' => $this->_helper->render($this->_nav2),
98 'registered_again' => $this->_helper->render()
101 $this->assertEquals($expected, $actual);
104 public function testUseAclRoleAsString()
106 $acl = $this->_getAcl();
107 $this->_helper->setAcl($acl['acl']);
108 $this->_helper->setRole('member');
110 $expected = $this->_getExpected('menu/acl_string.html');
111 $this->assertEquals($expected, $this->_helper->render());
114 public function testFilterOutPagesBasedOnAcl()
116 $acl = $this->_getAcl();
117 $this->_helper->setAcl($acl['acl']);
118 $this->_helper->setRole($acl['role']);
120 $expected = $this->_getExpected('menu/acl.html');
121 $actual = $this->_helper->render();
123 $this->assertEquals($expected, $actual);
126 public function testDisablingAcl()
128 $acl = $this->_getAcl();
129 $this->_helper->setAcl($acl['acl']);
130 $this->_helper->setRole($acl['role']);
131 $this->_helper->setUseAcl(false);
133 $expected = $this->_getExpected('menu/default1.html');
134 $actual = $this->_helper->render();
136 $this->assertEquals($expected, $actual);
139 public function testUseAnAclRoleInstanceFromAclObject()
141 $acl = $this->_getAcl();
142 $this->_helper->setAcl($acl['acl']);
143 $this->_helper->setRole($acl['acl']->getRole('member'));
145 $expected = $this->_getExpected('menu/acl_role_interface.html');
146 $this->assertEquals($expected, $this->_helper->render());
149 public function testUseConstructedAclRolesNotFromAclObject()
151 $acl = $this->_getAcl();
152 $this->_helper->setAcl($acl['acl']);
153 $this->_helper->setRole(new Zend_Acl_Role('member'));
155 $expected = $this->_getExpected('menu/acl_role_interface.html');
156 $this->assertEquals($expected, $this->_helper->render());
159 public function testSetUlCssClass()
161 $this->_helper->setUlClass('My_Nav');
162 $expected = $this->_getExpected('menu/css.html');
163 $this->assertEquals($expected, $this->_helper->render($this->_nav2));
166 public function testTranslationUsingZendTranslate()
168 $translator = $this->_getTranslator();
169 $this->_helper->setTranslator($translator);
171 $expected = $this->_getExpected('menu/translated.html');
172 $this->assertEquals($expected, $this->_helper->render());
175 public function testTranslationUsingZendTranslateAdapter()
177 $translator = $this->_getTranslator();
178 $this->_helper->setTranslator($translator->getAdapter());
180 $expected = $this->_getExpected('menu/translated.html');
181 $this->assertEquals($expected, $this->_helper->render());
184 public function testTranslationUsingTranslatorFromRegistry()
186 $oldReg = Zend_Registry::isRegistered('Zend_Translate')
187 ? Zend_Registry::get('Zend_Translate')
188 : null;
190 $translator = $this->_getTranslator();
191 Zend_Registry::set('Zend_Translate', $translator);
193 $expected = $this->_getExpected('menu/translated.html');
194 $actual = $this->_helper->render();
196 Zend_Registry::set('Zend_Translate', $oldReg);
198 $this->assertEquals($expected, $actual);
202 public function testDisablingTranslation()
204 $translator = $this->_getTranslator();
205 $this->_helper->setTranslator($translator);
206 $this->_helper->setUseTranslator(false);
208 $expected = $this->_getExpected('menu/default1.html');
209 $this->assertEquals($expected, $this->_helper->render());
212 public function testRenderingPartial()
214 $this->_helper->setPartial('menu.phtml');
216 $expected = $this->_getExpected('menu/partial.html');
217 $actual = $this->_helper->render();
219 $this->assertEquals($expected, $actual);
222 public function testRenderingPartialBySpecifyingAnArrayAsPartial()
224 $this->_helper->setPartial(array('menu.phtml', 'default'));
226 $expected = $this->_getExpected('menu/partial.html');
227 $actual = $this->_helper->render();
229 $this->assertEquals($expected, $actual);
232 public function testRenderingPartialShouldFailOnInvalidPartialArray()
234 $this->_helper->setPartial(array('menu.phtml'));
236 try {
237 $this->_helper->render();
238 $this->fail('invalid $partial should throw Zend_View_Exception');
239 } catch (Zend_View_Exception $e) {
251 public function testSetMaxDepth()
253 $this->_helper->setMaxDepth(1);
255 $expected = $this->_getExpected('menu/maxdepth.html');
256 $actual = $this->_helper->renderMenu();
258 $this->assertEquals($expected, $actual);
261 public function testSetMinDepth()
263 $this->_helper->setMinDepth(1);
265 $expected = $this->_getExpected('menu/mindepth.html');
266 $actual = $this->_helper->renderMenu();
268 $this->assertEquals($expected, $actual);
271 public function testSetBothDepts()
273 $this->_helper->setMinDepth(1)->setMaxDepth(2);
275 $expected = $this->_getExpected('menu/bothdepts.html');
276 $actual = $this->_helper->renderMenu();
278 $this->assertEquals($expected, $actual);
281 public function testSetOnlyActiveBranch()
283 $this->_helper->setOnlyActiveBranch(true);
285 $expected = $this->_getExpected('menu/onlyactivebranch.html');
286 $actual = $this->_helper->renderMenu();
288 $this->assertEquals($expected, $actual);
291 public function testSetRenderParents()
293 $this->_helper->setOnlyActiveBranch(true)->setRenderParents(false);
295 $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
296 $actual = $this->_helper->renderMenu();
298 $this->assertEquals($expected, $actual);
301 public function testSetOnlyActiveBranchAndMinDepth()
303 $this->_helper->setOnlyActiveBranch()->setMinDepth(1);
305 $expected = $this->_getExpected('menu/onlyactivebranch_mindepth.html');
306 $actual = $this->_helper->renderMenu();
308 $this->assertEquals($expected, $actual);
311 public function testOnlyActiveBranchAndMaxDepth()
313 $this->_helper->setOnlyActiveBranch()->setMaxDepth(2);
315 $expected = $this->_getExpected('menu/onlyactivebranch_maxdepth.html');
316 $actual = $this->_helper->renderMenu();
318 $this->assertEquals($expected, $actual);
321 public function testOnlyActiveBranchAndBothDepthsSpecified()
323 $this->_helper->setOnlyActiveBranch()->setMinDepth(1)->setMaxDepth(2);
325 $expected = $this->_getExpected('menu/onlyactivebranch_bothdepts.html');
326 $actual = $this->_helper->renderMenu();
328 $this->assertEquals($expected, $actual);
331 public function testOnlyActiveBranchNoParentsAndBothDepthsSpecified()
333 $this->_helper->setOnlyActiveBranch()
334 ->setMinDepth(1)
335 ->setMaxDepth(2)
336 ->setRenderParents(false);
338 $expected = $this->_getExpected('menu/onlyactivebranch_np_bd.html');
339 $actual = $this->_helper->renderMenu();
341 $this->assertEquals($expected, $actual);
344 private function _setActive($label)
346 $container = $this->_helper->getContainer();
348 foreach ($container->findAllByActive(true) as $page) {
349 $page->setActive(false);
352 if ($p = $container->findOneByLabel($label)) {
353 $p->setActive(true);
357 public function testOnlyActiveBranchNoParentsActiveOneBelowMinDepth()
359 $this->_setActive('Page 2');
361 $this->_helper->setOnlyActiveBranch()
362 ->setMinDepth(1)
363 ->setMaxDepth(1)
364 ->setRenderParents(false);
366 $expected = $this->_getExpected('menu/onlyactivebranch_np_bd2.html');
367 $actual = $this->_helper->renderMenu();
369 $this->assertEquals($expected, $actual);
372 public function testRenderSubMenuShouldOverrideOptions()
374 $this->_helper->setOnlyActiveBranch(false)
375 ->setMinDepth(1)
376 ->setMaxDepth(2)
377 ->setRenderParents(true);
379 $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
380 $actual = $this->_helper->renderSubMenu();
382 $this->assertEquals($expected, $actual);
385 public function testOptionMaxDepth()
387 $options = array(
388 'maxDepth' => 1
391 $expected = $this->_getExpected('menu/maxdepth.html');
392 $actual = $this->_helper->renderMenu(null, $options);
394 $this->assertEquals($expected, $actual);
397 public function testOptionMinDepth()
399 $options = array(
400 'minDepth' => 1
403 $expected = $this->_getExpected('menu/mindepth.html');
404 $actual = $this->_helper->renderMenu(null, $options);
406 $this->assertEquals($expected, $actual);
409 public function testOptionBothDepts()
411 $options = array(
412 'minDepth' => 1,
413 'maxDepth' => 2
416 $expected = $this->_getExpected('menu/bothdepts.html');
417 $actual = $this->_helper->renderMenu(null, $options);
419 $this->assertEquals($expected, $actual);
422 public function testOptionOnlyActiveBranch()
424 $options = array(
425 'onlyActiveBranch' => true
428 $expected = $this->_getExpected('menu/onlyactivebranch.html');
429 $actual = $this->_helper->renderMenu(null, $options);
431 $this->assertEquals($expected, $actual);
434 public function testOptionOnlyActiveBranchNoParents()
436 $options = array(
437 'onlyActiveBranch' => true,
438 'renderParents' => false
441 $expected = $this->_getExpected('menu/onlyactivebranch_noparents.html');
442 $actual = $this->_helper->renderMenu(null, $options);
444 $this->assertEquals($expected, $actual);
447 public function testOptionOnlyActiveBranchAndMinDepth()
449 $options = array(
450 'minDepth' => 1,
451 'onlyActiveBranch' => true
454 $expected = $this->_getExpected('menu/onlyactivebranch_mindepth.html');
455 $actual = $this->_helper->renderMenu(null, $options);
457 $this->assertEquals($expected, $actual);
460 public function testOptionOnlyActiveBranchAndMaxDepth()
462 $options = array(
463 'maxDepth' => 2,
464 'onlyActiveBranch' => true
467 $expected = $this->_getExpected('menu/onlyactivebranch_maxdepth.html');
468 $actual = $this->_helper->renderMenu(null, $options);
470 $this->assertEquals($expected, $actual);
473 public function testOptionOnlyActiveBranchAndBothDepthsSpecified()
475 $options = array(
476 'minDepth' => 1,
477 'maxDepth' => 2,
478 'onlyActiveBranch' => true
481 $expected = $this->_getExpected('menu/onlyactivebranch_bothdepts.html');
482 $actual = $this->_helper->renderMenu(null, $options);
484 $this->assertEquals($expected, $actual);
487 public function testOptionOnlyActiveBranchNoParentsAndBothDepthsSpecified()
489 $options = array(
490 'minDepth' => 2,
491 'maxDepth' => 2,
492 'onlyActiveBranch' => true,
493 'renderParents' => false
496 $expected = $this->_getExpected('menu/onlyactivebranch_np_bd.html');
497 $actual = $this->_helper->renderMenu(null, $options);
499 $this->assertEquals($expected, $actual);