Ejemplo n.º 1
0
 public function testGetChildrenShouldReturnTheCurrentPage()
 {
     $container = new Navigation\Navigation();
     $page = Page\Page::factory(array('type' => 'uri'));
     $container->addPage($page);
     $this->assertEquals($page, $container->getChildren());
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
 /**
  * Adds a page to the container
  *
  * This method will inject the container as the given page's parent by
  * calling {@link Zend_Navigation_Page::setParent()}.
  *
  * @param  Zend_Navigation_Page|array|\Zend\Config\Config $page  page to add
  * @return \Zend\Navigation\Container                     fluent interface,
  *                                                       returns self
  * @throws \Zend\Navigation\Exception                     if page is invalid
  */
 public function addPage($page)
 {
     if ($page === $this) {
         throw new Exception('A page cannot have itself as a parent');
     }
     if (is_array($page) || $page instanceof Config\Config) {
         $page = Page\Page::factory($page);
     } elseif (!$page instanceof Page\Page) {
         throw new Exception('Invalid argument: $page must be an instance of ' . 'Zend_Navigation_Page or Zend_Config, or an array');
     }
     $hash = $page->hashCode();
     if (array_key_exists($hash, $this->_index)) {
         // page is already in container
         return $this;
     }
     // adds page to container and sets dirty flag
     $this->_pages[$hash] = $page;
     $this->_index[$hash] = $page->getOrder();
     $this->_dirtyIndex = true;
     // inject self as page parent
     $page->setParent($this);
     return $this;
 }
Ejemplo n.º 5
0
 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');
 }
Ejemplo n.º 6
0
Archivo: Links.php Proyecto: stunti/zf2
 /**
  * Converts a $mixed value to an array of pages
  *
  * @param  mixed $mixed                     mixed value to get page(s) from
  * @param  bool  $recursive                 whether $value should be looped
  *                                          if it is an array or a config
  * @return \Zend\Navigation\Page\Page|array|null  empty if unable to convert
  */
 protected function _convertToPages($mixed, $recursive = true)
 {
     if (is_object($mixed)) {
         if ($mixed instanceof Page\Page) {
             // value is a page instance; return directly
             return $mixed;
         } elseif ($mixed instanceof Navigation\Container) {
             // value is a container; return pages in it
             $pages = array();
             foreach ($mixed as $page) {
                 $pages[] = $page;
             }
             return $pages;
         } elseif ($mixed instanceof \Zend\Config\Config) {
             // convert config object to array and extract
             return $this->_convertToPages($mixed->toArray(), $recursive);
         }
     } elseif (is_string($mixed)) {
         // value is a string; make an URI page
         return Page\Page::factory(array('type' => 'uri', 'uri' => $mixed));
     } elseif (is_array($mixed) && !empty($mixed)) {
         if ($recursive && is_numeric(key($mixed))) {
             // first key is numeric; assume several pages
             $pages = array();
             foreach ($mixed as $value) {
                 if ($value = $this->_convertToPages($value, false)) {
                     $pages[] = $value;
                 }
             }
             return $pages;
         } else {
             // pass array to factory directly
             try {
                 $page = Page\Page::factory($mixed);
                 return $page;
             } catch (\Exception $e) {
             }
         }
     }
     // nothing found
     return null;
 }