public function testGetChildrenShouldReturnTheCurrentPage() { $container = new Navigation\Navigation(); $page = Page\Page::factory(array('type' => 'uri')); $container->addPage($page); $this->assertEquals($page, $container->getChildren()); }
/** * Determines whether a page should be accepted by ACL when iterating * * Rules: * - If helper has no ACL, page is accepted * - If page has a resource or privilege defined, page is accepted * if the ACL allows access to it using the helper's role * - If page has no resource or privilege, page is accepted * * @param \Zend\Navigation\Page\Page $page page to check * @return bool whether page is accepted by ACL */ protected function _acceptAcl(Page\Page $page) { if (!($acl = $this->getAcl())) { // no acl registered means don't use acl return true; } $role = $this->getRole(); $resource = $page->getResource(); $privilege = $page->getPrivilege(); if ($resource || $privilege) { // determine using helper role and page resource/privilege return $acl->isAllowed($role, $resource, $privilege); } return true; }
/** * Returns an array representation of the page * * @return array associative array containing all page properties */ public function toArray() { return array_merge(parent::toArray(), array('action' => $this->getAction(), 'controller' => $this->getController(), 'module' => $this->getModule(), 'params' => $this->getParams(), 'route' => $this->getRoute(), 'reset_params' => $this->getResetParams())); }
public function testAclFiltersAwayPagesFromPageProperty() { $acl = new Acl\Acl(); $acl->addRole(new Role\GenericRole('member')); $acl->addRole(new Role\GenericRole('admin')); $acl->add(new Resource\GenericResource('protected')); $acl->allow('admin', 'protected'); $this->_helper->setAcl($acl); $this->_helper->setRole($acl->getRole('member')); $samplePage = Page\Page::factory(array('label' => 'An example page', 'uri' => 'http://www.example.com/', 'resource' => 'protected')); $active = $this->_helper->findOneByLabel('Home'); $expected = array('alternate' => false, 'stylesheet' => false, 'start' => false, 'next' => 'Page 1', 'prev' => false, 'contents' => false, 'index' => false, 'glossary' => false, 'copyright' => false, 'chapter' => 'array(4)', 'section' => false, 'subsection' => false, 'appendix' => false, 'help' => false, 'bookmark' => false); $actual = array(); foreach ($expected as $type => $discard) { $active->addRel($type, $samplePage); $found = $this->_helper->findRelation($active, 'rel', $type); if (null === $found) { $actual[$type] = false; } elseif (is_array($found)) { $actual[$type] = 'array(' . count($found) . ')'; } else { $actual[$type] = $found->getLabel(); } } $this->assertEquals($expected, $actual); }
/** * Checks if the container has the given page * * @param \Zend\Navigation\Page\Page $page page to look for * @param bool $recursive [optional] whether to search * recursively. Default is false. * @return bool whether page is in container */ public function hasPage(Page\Page $page, $recursive = false) { if (array_key_exists($page->hashCode(), $this->_index)) { return true; } elseif ($recursive) { foreach ($this->_pages as $childPage) { if ($childPage->hasPage($page, true)) { return true; } } } return false; }
public function testToArrayMethod() { $options = array('label' => 'foo', 'uri' => '#', 'id' => 'my-id', 'class' => 'my-class', 'title' => 'my-title', 'target' => 'my-target', 'rel' => array(), 'rev' => array(), 'order' => 100, 'active' => true, 'visible' => false, 'resource' => 'joker', 'privilege' => null, 'foo' => 'bar', 'meaning' => 42, 'pages' => array(array('label' => 'foo.bar', 'uri' => '#'), array('label' => 'foo.baz', 'uri' => '#'))); $page = Page\Page::factory($options); $toArray = $page->toArray(); // tweak options to what we expect toArray() to contain $options['type'] = 'Zend\\Navigation\\Page\\Uri'; // calculate diff between toArray() and $options $diff = array_diff_assoc($toArray, $options); // should be no diff $this->assertEquals(array(), $diff); // $toArray should have 2 sub pages $this->assertEquals(2, count($toArray['pages'])); // tweak options to what we expect sub page 1 to be $options['label'] = 'foo.bar'; $options['order'] = null; $options['id'] = null; $options['class'] = null; $options['title'] = null; $options['target'] = null; $options['resource'] = null; $options['active'] = false; $options['visible'] = true; unset($options['foo']); unset($options['meaning']); // assert that there is no diff from what we expect $subPageOneDiff = array_diff_assoc($toArray['pages'][0], $options); $this->assertEquals(array(), $subPageOneDiff); // tweak options to what we expect sub page 2 to be $options['label'] = 'foo.baz'; // assert that there is no diff from what we expect $subPageTwoDiff = array_diff_assoc($toArray['pages'][1], $options); $this->assertEquals(array(), $subPageTwoDiff); }
/** * Removes the given page from the container * * @param \Zend\Navigation\Page\Page|int $page page to remove, either a page * instance or a specific page order * @return bool whether the removal was * successful */ public function removePage($page) { if ($page instanceof Page\Page) { $hash = $page->hashCode(); } elseif (is_int($page)) { $this->_sort(); if (!($hash = array_search($page, $this->_index))) { return false; } } else { return false; } if (isset($this->_pages[$hash])) { unset($this->_pages[$hash]); unset($this->_index[$hash]); $this->_dirtyIndex = true; return true; } return false; }
public function testShouldFailIfUnableToDetermineType() { try { $page = Page\Page::factory(array('label' => 'My Invalid Page')); } catch (Navigation\Exception $e) { return; } $this->fail('An exception has not been thrown for invalid page type'); }
/** * Renders the given $page as a link element, with $attrib = $relation * * @param \Zend\Navigation\Page\Page $page the page to render the link for * @param string $attrib the attribute to use for $type, * either 'rel' or 'rev' * @param string $relation relation type, muse be one of; * alternate, appendix, bookmark, * chapter, contents, copyright, * glossary, help, home, index, next, * prev, section, start, stylesheet, * subsection * @return string rendered link element * @throws \Zend\View\Exception if $attrib is invalid */ public function renderLink(Page\Page $page, $attrib, $relation) { if (!in_array($attrib, array('rel', 'rev'))) { $e = new View\Exception(sprintf('Invalid relation attribute "%s", must be "rel" or "rev"', $attrib)); $e->setView($this->view); throw $e; } if (!($href = $page->getHref())) { return ''; } // TODO: add more attribs // http://www.w3.org/TR/html401/struct/links.html#h-12.2 $attribs = array($attrib => $relation, 'href' => $href, 'title' => $page->getLabel()); return '<link' . $this->_htmlAttribs($attribs) . $this->getClosingBracket(); }
/** * Returns an escaped absolute URL for the given page * * @param \Zend\Navigation\Page\Page $page page to get URL from * @return string */ public function url(Navigation\Page\Page $page) { $href = $page->getHref(); if (!isset($href[0])) { // no href return ''; } elseif ($href[0] == '/') { // href is relative to root; use serverUrl helper $url = $this->getServerUrl() . $href; } elseif (preg_match('/^[a-z]+:/im', (string) $href)) { // scheme is given in href; assume absolute URL already $url = (string) $href; } else { // href is relative to current document; use url helpers $url = $this->getServerUrl() . rtrim($this->view->url(), '/') . '/' . $href; } return $this->_xmlEscape($url); }
/** * Returns an HTML string containing an 'a' element for the given page if * the page's href is not empty, and a 'span' element if it is empty * * Overrides {@link Zend_View_Helper_Navigation_Abstract::htmlify()}. * * @param \Zend\Navigation\Page\Page $page page to generate HTML for * @return string HTML string for the given page */ public function htmlify(Navigation\Page\Page $page) { // get label and title for translating $label = $page->getLabel(); $title = $page->getTitle(); // translate label and title? if ($this->getUseTranslator() && ($t = $this->getTranslator())) { if (is_string($label) && !empty($label)) { $label = $t->translate($label); } if (is_string($title) && !empty($title)) { $title = $t->translate($title); } } // get attribs for element $attribs = array('id' => $page->getId(), 'title' => $title, 'class' => $page->getClass()); // does page have a href? if ($href = $page->getHref()) { $element = 'a'; $attribs['href'] = $href; $attribs['target'] = $page->getTarget(); } else { $element = 'span'; } return '<' . $element . $this->_htmlAttribs($attribs) . '>' . $this->view->escape($label) . '</' . $element . '>'; }