2 require_once dirname(__FILE__
) . '/TestAbstract.php';
3 require_once 'Zend/View/Helper/Navigation/Menu.php';
6 * Tests Zend_View_Helper_Navigation_Menu
9 class Zend_View_Helper_Navigation_MenuTest
10 extends Zend_View_Helper_Navigation_TestAbstract
13 * Class name for view helper to test
17 protected $_helperName = 'Zend_View_Helper_Navigation_Menu';
22 * @var Zend_View_Helper_Navigation_Menu
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()
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);
69 'indent4' => $this->_getExpected('menu/indent4.html'),
70 'indent8' => $this->_getExpected('menu/indent8.html')
73 $renderOptions = 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');
90 'registered' => $rendered1,
91 'supplied' => $rendered2,
92 'registered_again' => $rendered1
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')
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'));
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()
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)) {
357 public function testOnlyActiveBranchNoParentsActiveOneBelowMinDepth()
359 $this->_setActive('Page 2');
361 $this->_helper
->setOnlyActiveBranch()
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)
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()
391 $expected = $this->_getExpected('menu/maxdepth.html');
392 $actual = $this->_helper
->renderMenu(null, $options);
394 $this->assertEquals($expected, $actual);
397 public function testOptionMinDepth()
403 $expected = $this->_getExpected('menu/mindepth.html');
404 $actual = $this->_helper
->renderMenu(null, $options);
406 $this->assertEquals($expected, $actual);
409 public function testOptionBothDepts()
416 $expected = $this->_getExpected('menu/bothdepts.html');
417 $actual = $this->_helper
->renderMenu(null, $options);
419 $this->assertEquals($expected, $actual);
422 public function testOptionOnlyActiveBranch()
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()
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()
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()
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()
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()
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);