public function testBuild()
 {
     $type = 'history';
     $userId = 1;
     $user = $this->getMockBuilder('stdClass')->setMethods(['getId'])->getMock();
     $user->expects($this->once())->method('getId')->will($this->returnValue($userId));
     $token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $token->expects($this->once())->method('getUser')->will($this->returnValue($user));
     $this->tokenStorage->expects($this->once())->method('getToken')->will($this->returnValue($token));
     $item = $this->getMock('Oro\\Bundle\\NavigationBundle\\Entity\\NavigationItemInterface');
     $this->factory->expects($this->once())->method('createItem')->with($type, [])->will($this->returnValue($item));
     $repository = $this->getMockBuilder('Oro\\Bundle\\NavigationBundle\\Entity\\Repository\\HistoryItemRepository')->disableOriginalConstructor()->getMock();
     $items = [['id' => 1, 'title' => 'test1', 'url' => '/'], ['id' => 2, 'title' => 'test2', 'url' => '/home']];
     $repository->expects($this->once())->method('getNavigationItems')->with($userId, $type)->will($this->returnValue($items));
     $this->em->expects($this->once())->method('getRepository')->with(get_class($item))->will($this->returnValue($repository));
     $menu = $this->getMockBuilder('Knp\\Menu\\MenuItem')->disableOriginalConstructor()->getMock();
     $childMock = $this->getMock('Knp\\Menu\\ItemInterface');
     $childMock2 = clone $childMock;
     $children = [$childMock, $childMock2];
     $matcher = $this->getMock('\\Knp\\Menu\\Matcher\\Matcher');
     $matcher->expects($this->once())->method('isCurrent')->will($this->returnValue(true));
     $this->builder->setMatcher($matcher);
     $menu->expects($this->exactly(2))->method('addChild');
     $menu->expects($this->once())->method('setExtra')->with('type', $type);
     $menu->expects($this->once())->method('getChildren')->will($this->returnValue($children));
     $menu->expects($this->once())->method('removeChild');
     $n = rand(1, 10);
     $configMock = $this->getMockBuilder('Oro\\Bundle\\ConfigBundle\\Config\\UserConfigManager')->disableOriginalConstructor()->getMock();
     $configMock->expects($this->once())->method('get')->with($this->equalTo('oro_navigation.maxItems'))->will($this->returnValue($n));
     $this->manipulator->expects($this->once())->method('slice')->with($menu, 0, $n);
     $this->builder->setOptions($configMock);
     $this->builder->build($menu, [], $type);
 }
 /**
  * Process onReslonse event, updates user history information
  *
  * @param  FilterResponseEvent $event
  * @return bool|void
  */
 public function onResponse(FilterResponseEvent $event)
 {
     if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
         // Do not do anything
         return;
     }
     $request = $event->getRequest();
     $response = $event->getResponse();
     // do not process requests other than in html format
     // with 200 OK status using GET method and not _internal and _wdt
     if (!$this->matchRequest($response, $request)) {
         return false;
     }
     $postArray = ['url' => $request->getRequestUri(), 'user' => $this->user];
     /** @var $historyItem  NavigationHistoryItem */
     $historyItem = $this->em->getRepository('Oro\\Bundle\\NavigationBundle\\Entity\\NavigationHistoryItem')->findOneBy($postArray);
     if (!$historyItem) {
         /** @var $historyItem \Oro\Bundle\NavigationBundle\Entity\NavigationItemInterface */
         $historyItem = $this->navItemFactory->createItem(NavigationHistoryItem::NAVIGATION_HISTORY_ITEM_TYPE, $postArray);
     }
     $historyItem->setTitle($this->titleService->getSerialized());
     // force update
     $historyItem->doUpdate();
     $this->em->persist($historyItem);
     $this->em->flush($historyItem);
     return true;
 }
 /**
  * Returns an existing history item, finding it by URL and user,
  * or creates and returns a new one.
  *
  * @param UserInterface $user
  * @param string        $url
  *
  * @return NavigationItemInterface
  */
 protected function findOrCreate(UserInterface $user, $url)
 {
     $historyItem = $this->repository->findOneBy(['user' => $user, 'url' => $url]);
     if (null === $historyItem) {
         $historyItem = $this->itemFactory->createItem(NavigationHistoryItem::NAVIGATION_HISTORY_ITEM_TYPE, ['user' => $user, 'url' => $url]);
     }
     return $historyItem;
 }
 public function testNewItem()
 {
     $user = new User();
     $user->setEmail('*****@*****.**');
     $this->factory->expects($this->once())->method('createItem')->will($this->returnValue($this->item));
     $repository = $this->getDefaultRepositoryMock(null);
     $em = $this->getEntityManager($repository);
     $listener = $this->getListener($this->factory, $this->tokenStorage, $em);
     $response = $this->getResponse();
     $listener->onResponse($this->getEventMock($this->getRequest(), $response));
 }
 /**
  * Modify menu by adding, removing or editing items.
  *
  * @param \Knp\Menu\ItemInterface $menu
  * @param array                   $options
  * @param string|null             $alias
  */
 public function build(ItemInterface $menu, array $options = [], $alias = null)
 {
     $user = $this->tokenStorage->getToken()->getUser();
     $menu->setExtra('type', $alias);
     if (is_object($user)) {
         /** @var $entity NavigationItemInterface */
         $entity = $this->factory->createItem($alias, []);
         /** @var $repo NavigationRepositoryInterface */
         $repo = $this->em->getRepository(get_class($entity));
         $items = $repo->getNavigationItems($user->getId(), $alias, $options);
         foreach ($items as $item) {
             $menu->addChild($alias . '_item_' . $item['id'], ['extras' => $item, 'uri' => $item['url'], 'label' => $item['title']]);
         }
     }
 }
 /**
  * Modify menu by adding, removing or editing items.
  *
  * @param \Knp\Menu\ItemInterface $menu
  * @param array                   $options
  * @param string|null             $alias
  */
 public function build(ItemInterface $menu, array $options = array(), $alias = null)
 {
     $user = $this->securityContext->getToken() ? $this->securityContext->getToken()->getUser() : null;
     $menu->setExtra('type', $alias);
     if (is_object($user)) {
         $currentOrganization = $this->securityContext->getToken()->getOrganizationContext();
         /** @var $entity NavigationItemInterface */
         $entity = $this->factory->createItem($alias, array());
         /** @var $repo NavigationRepositoryInterface */
         $repo = $this->em->getRepository(ClassUtils::getClass($entity));
         $items = $repo->getNavigationItems($user->getId(), $currentOrganization, $alias, $options);
         foreach ($items as $item) {
             $menu->addChild($alias . '_item_' . $item['id'], array('extras' => $item, 'uri' => $item['url'], 'label' => $item['title']));
         }
     }
 }
Example #7
0
 public function loadUsersTags()
 {
     $params = array('account' => array("url" => "/account", "title_rendered" => "Accounts - Customers", "title" => "{\"template\":\"Accounts - Customers\",\"short_template\":\"Accounts\",\"params\":[]}", "position" => 0, "type" => "pinbar", "display_type" => "list", "maximized" => false, "remove" => false), 'contact' => array("url" => "/contact", "title_rendered" => "Contacts - Customers", "title" => "{\"template\":\"Contacts - Customers\",\"short_template\":\"Contacts\",\"params\":[]}", "position" => 1, "type" => "pinbar", "display_type" => "list", "maximized" => false, "remove" => false), 'leads' => array("url" => "/lead", "title_rendered" => "Leads - Sales", "title" => "{\"template\":\"Leads - Sales\",\"short_template\":\"Leads\",\"params\":[]}", "position" => 2, "type" => "pinbar", "display_type" => "list", "maximized" => false, "remove" => false), 'opportunities' => array("url" => "/opportunity", "title_rendered" => "Opportunities - Sales", "title" => "{\"template\":\"Opportunities - Sales\",\"short_template\":\"Opportunities\",\"params\":[]}", "position" => 3, "type" => "pinbar", "display_type" => "list", "maximized" => false, "remove" => false));
     $organization = $this->getReference('default_organization');
     foreach ($this->users as $user) {
         $securityContext = $this->container->get('security.context');
         $token = new UsernamePasswordOrganizationToken($user, $user->getUsername(), 'main', $this->organization);
         $securityContext->setToken($token);
         foreach ($params as $param) {
             $param['user'] = $user;
             $pinTab = $this->navigationFactory->createItem($param['type'], $param);
             $pinTab->getItem()->setOrganization($organization);
             $this->persist($this->em, $pinTab);
         }
     }
     $this->flush($this->em);
 }
 public function testBuild()
 {
     $type = 'favorite';
     $userId = 1;
     $user = $this->getMockBuilder('stdClass')->setMethods(array('getId'))->getMock();
     $user->expects($this->once($userId))->method('getId')->will($this->returnValue(1));
     $token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $token->expects($this->once())->method('getUser')->will($this->returnValue($user));
     $this->securityContext->expects($this->once())->method('getToken')->will($this->returnValue($token));
     $item = $this->getMock('Oro\\Bundle\\NavigationBundle\\Entity\\NavigationItemInterface');
     $this->factory->expects($this->once())->method('createItem')->with($type, array())->will($this->returnValue($item));
     $repository = $this->getMockBuilder('Oro\\Bundle\\NavigationBundle\\Entity\\Repository\\NavigationItemRepository')->disableOriginalConstructor()->getMock();
     $items = array(array('id' => 1, 'title' => 'test1', 'url' => '/', 'type' => $type), array('id' => 2, 'title' => 'test2', 'url' => '/home', 'type' => $type));
     $repository->expects($this->once())->method('getNavigationItems')->with($userId, $type)->will($this->returnValue($items));
     $this->em->expects($this->once())->method('getRepository')->with(get_class($item))->will($this->returnValue($repository));
     $menu = $this->getMockBuilder('Knp\\Menu\\ItemInterface')->getMock();
     $menu->expects($this->exactly(2))->method('addChild');
     $menu->expects($this->once())->method('setExtra')->with('type', $type);
     $this->builder->build($menu, array(), $type);
 }
 public function testBuild()
 {
     $type = 'mostviewed';
     $maxItems = 20;
     $userId = 1;
     $user = $this->getMockBuilder('stdClass')->setMethods(array('getId'))->getMock();
     $user->expects($this->once())->method('getId')->will($this->returnValue($userId));
     $token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $token->expects($this->once())->method('getUser')->will($this->returnValue($user));
     $this->securityContext->expects($this->once())->method('getToken')->will($this->returnValue($token));
     $item = $this->getMock('Oro\\Bundle\\NavigationBundle\\Entity\\NavigationItemInterface');
     $this->factory->expects($this->once())->method('createItem')->with($type, array())->will($this->returnValue($item));
     $repository = $this->getMockBuilder('Oro\\Bundle\\NavigationBundle\\Entity\\Repository\\HistoryItemRepository')->disableOriginalConstructor()->getMock();
     $repository->expects($this->once())->method('getNavigationItems')->with($userId, $type, array('maxItems' => $maxItems, 'orderBy' => array(array('field' => NavigationHistoryItem::NAVIGATION_HISTORY_COLUMN_VISIT_COUNT))))->will($this->returnValue(array()));
     $this->em->expects($this->once())->method('getRepository')->with(get_class($item))->will($this->returnValue($repository));
     $configMock = $this->getMockBuilder('Oro\\Bundle\\ConfigBundle\\Config\\UserConfigManager')->disableOriginalConstructor()->getMock();
     $configMock->expects($this->once())->method('get')->with($this->equalTo('oro_navigation.maxItems'))->will($this->returnValue($maxItems));
     $menu = $this->getMockBuilder('Knp\\Menu\\ItemInterface')->getMock();
     $this->builder->setOptions($configMock);
     $this->builder->build($menu, array(), $type);
 }