Exemple #1
0
 public function testShouldFailIfUnableToDetermineType()
 {
     try {
         $page = AbstractPage::factory(array('label' => 'My Invalid Page'));
     } catch (Navigation\Exception\InvalidArgumentException $e) {
         return;
     }
     $this->fail('An exception has not been thrown for invalid page type');
 }
 /**
  * Get the feed list and the posts of the feed we are looking at now
  *
  * @return void
  */
 public function indexAction()
 {
     $viewData = array();
     $flashMessenger = $this->flashMessenger();
     $username = $this->params()->fromRoute('username');
     $currentFeedId = $this->params()->fromRoute('feed_id');
     $userData = ApiClient::getUser($username);
     if ($userData !== FALSE) {
         $hydrator = new ClassMethods();
         $user = $hydrator->hydrate($userData, new User());
     } else {
         $this->getResponse()->setStatusCode(404);
         return;
     }
     $subscribeForm = new SubscribeForm();
     $unsubscribeForm = new UnsubscribeForm();
     $subscribeForm->setAttribute('action', $this->url()->fromRoute('feeds-subscribe', array('username' => $username)));
     $unsubscribeForm->setAttribute('action', $this->url()->fromRoute('feeds-unsubscribe', array('username' => $username)));
     $hydrator = new ClassMethods();
     $response = ApiClient::getFeeds($username);
     $feeds = array();
     foreach ($response as $r) {
         $feeds[$r['id']] = $hydrator->hydrate($r, new Feed());
     }
     if ($currentFeedId === null && !empty($feeds)) {
         $currentFeedId = reset($feeds)->getId();
     }
     $feedsMenu = new Navigation();
     $router = $this->getEvent()->getRouter();
     $routeMatch = $this->getEvent()->getRouteMatch()->setParam('feed_id', $currentFeedId);
     foreach ($feeds as $f) {
         $feedsMenu->addPage(AbstractPage::factory(array('title' => $f->getTitle(), 'icon' => $f->getIcon(), 'route' => 'feeds', 'routeMatch' => $routeMatch, 'router' => $router, 'params' => array('username' => $username, 'feed_id' => $f->getId()))));
     }
     $currentFeed = $currentFeedId != null ? $feeds[$currentFeedId] : null;
     if ($currentFeed != null) {
         $paginator = new Paginator(new ArrayAdapter($currentFeed->getArticles()));
         $paginator->setItemCountPerPage(5);
         $paginator->setCurrentPageNumber($this->params()->fromRoute('page'));
         $viewData['paginator'] = $paginator;
         $viewData['feedId'] = $currentFeedId;
     }
     $unsubscribeForm->get('feed_id')->setValue($currentFeedId);
     $viewData['subscribeForm'] = $subscribeForm;
     $viewData['unsubscribeForm'] = $unsubscribeForm;
     $viewData['username'] = $username;
     $viewData['feedsMenu'] = $feedsMenu;
     $viewData['user'] = $user;
     $viewData['paginator'] = $paginator;
     $viewData['feedId'] = $currentFeedId;
     $viewData['feed'] = $currentFeed;
     $this->layout()->username = $username;
     if ($flashMessenger->hasMessages()) {
         $viewData['flashMessages'] = $flashMessenger->getMessages();
     }
     return $viewData;
 }
Exemple #3
0
 public function testAclFiltersAwayPagesFromPageProperty()
 {
     $acl = new Acl\Acl();
     $acl->addRole(new Role\GenericRole('member'));
     $acl->addRole(new Role\GenericRole('admin'));
     $acl->addResource(new Resource\GenericResource('protected'));
     $acl->allow('admin', 'protected');
     $this->_helper->setAcl($acl);
     $this->_helper->setRole($acl->getRole('member'));
     $samplePage = AbstractPage::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);
 }
Exemple #4
0
 public function testGetChildrenShouldReturnTheCurrentPage()
 {
     $container = new Navigation\Navigation();
     $page = Page\AbstractPage::factory(array('type' => 'uri'));
     $container->addPage($page);
     $this->assertEquals($page, $container->getChildren());
 }
 /**
  * Adds a page to the container
  *
  * This method will inject the container as the given page's parent by
  * calling {@link Page\AbstractPage::setParent()}.
  *
  * @param  Page\AbstractPage|array|Traversable $page  page to add
  * @return self fluent interface, returns self
  * @throws Exception\InvalidArgumentException if page is invalid
  */
 public function addPage($page)
 {
     if ($page === $this) {
         throw new Exception\InvalidArgumentException('A page cannot have itself as a parent');
     }
     if (!$page instanceof Page\AbstractPage) {
         if (!is_array($page) && !$page instanceof Traversable) {
             throw new Exception\InvalidArgumentException('Invalid argument: $page must be an instance of ' . 'Zend\\Navigation\\Page\\AbstractPage or Traversable, or an array');
         }
         $page = Page\AbstractPage::factory($page);
     }
     $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;
 }
Exemple #6
0
 public function testToArrayMethod()
 {
     $options = array('label' => 'foo', 'uri' => 'http://www.example.com/foo.html', 'fragment' => 'bar', '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' => 'http://www.example.com/foo.html'), array('label' => 'foo.baz', 'uri' => 'http://www.example.com/foo.html')));
     $page = AbstractPage::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['fragment'] = null;
     $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);
 }
Exemple #7
0
 public function testToArrayMethod()
 {
     $options = array('label' => 'foo', 'uri' => 'http://www.example.com/foo.html', 'fragment' => 'bar', '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('type' => 'Zend\\Navigation\\Page\\Uri', 'label' => 'foo.bar', 'fragment' => null, 'id' => null, 'class' => null, 'title' => null, 'target' => null, 'rel' => array(), 'rev' => array(), 'order' => null, 'resource' => null, 'privilege' => null, 'active' => null, 'visible' => 1, 'pages' => array(), 'uri' => 'http://www.example.com/foo.html'), array('label' => 'foo.baz', 'type' => 'Zend\\Navigation\\Page\\Uri', 'label' => 'foo.bar', 'fragment' => null, 'id' => null, 'class' => null, 'title' => null, 'target' => null, 'rel' => array(), 'rev' => array(), 'order' => null, 'resource' => null, 'privilege' => null, 'active' => null, 'visible' => 1, 'pages' => array(), 'uri' => 'http://www.example.com/foo.html')));
     $page = AbstractPage::factory($options);
     $toArray = $page->toArray();
     // tweak options to what we expect toArray() to contain
     $options['type'] = 'Zend\\Navigation\\Page\\Uri';
     ksort($options);
     ksort($toArray);
     $this->assertEquals($options, $toArray);
 }
Exemple #8
0
 /**
  * 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 AbstractPage|array|null
  */
 protected function convertToPages($mixed, $recursive = true)
 {
     if ($mixed instanceof AbstractPage) {
         // value is a page instance; return directly
         return $mixed;
     } elseif ($mixed instanceof AbstractContainer) {
         // value is a container; return pages in it
         $pages = array();
         foreach ($mixed as $page) {
             $pages[] = $page;
         }
         return $pages;
     } elseif ($mixed instanceof Traversable) {
         $mixed = ArrayUtils::iteratorToArray($mixed);
     } elseif (is_string($mixed)) {
         // value is a string; make an URI page
         return AbstractPage::factory(array('type' => 'uri', 'uri' => $mixed));
     }
     if (is_array($mixed) && !empty($mixed)) {
         if ($recursive && is_numeric(key($mixed))) {
             // first key is numeric; assume several pages
             $pages = array();
             foreach ($mixed as $value) {
                 $value = $this->convertToPages($value, false);
                 if ($value) {
                     $pages[] = $value;
                 }
             }
             return $pages;
         } else {
             // pass array to factory directly
             try {
                 $page = AbstractPage::factory($mixed);
                 return $page;
             } catch (\Exception $e) {
             }
         }
     }
     // nothing found
     return null;
 }
Exemple #9
0
 public function testSetObjectPermission()
 {
     $page = AbstractPage::factory(array('type' => 'uri'));
     $permission = new \stdClass();
     $permission->name = 'my_permission';
     $page->setPermission($permission);
     $this->assertInstanceOf('stdClass', $page->getPermission());
     $this->assertEquals('my_permission', $page->getPermission()->name);
 }
Exemple #10
0
 public function testMenu()
 {
     $this->_authService->method('hasIdentity')->willReturn(true);
     $this->_authService->method('getIdentity')->willReturn('identity');
     $this->_view->plugin('navigation')->menu()->setTranslator(null);
     $menu = \Zend\Navigation\Page\AbstractPage::factory(array('type' => 'uri', 'pages' => array(array('label' => 'main', 'uri' => 'mainUri', 'active' => true, 'pages' => array(array('label' => 'sub', 'uri' => 'subUri', 'active' => true))))));
     $html = $this->_view->render('layout', array('menu' => $menu));
     $document = new \Zend\Dom\Document($html);
     $this->assertCount(1, Query::execute('/html/body/div[@id="menu"]/ul[@class="navigation"]/li/a[@href="mainUri"]', $document));
     $this->assertCount(1, Query::execute('/html/body/div[@id="menu"]/ul[@class="navigation navigation_sub"]/li/a[@href="subUri"]', $document));
     $this->assertCount(1, Query::execute("/html/body/div[@id='menu']/div[@id='logout']/a[@href='/console/login/logout/'][text()='\nAbmelden\n']", $document));
 }