public function testApplyPriceListLimitationsNotApplied()
 {
     /** @var \PHPUnit_Framework_MockObject_MockObject|QueryBuilder $qb */
     $qb = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     $this->priceListTreeHandler->expects($this->never())->method($this->anything());
     $this->modifier->applyPriceListLimitations($qb);
 }
 /**
  * @dataProvider applyPriceListLimitationsDataProvider
  * @param string|null $currency
  * @param array $expectedProductSku
  */
 public function testApplyPriceListLimitations($currency, array $expectedProductSku)
 {
     /** @var PriceList $priceList */
     $priceList = $this->getReference('price_list_2');
     $user = new AccountUser();
     $token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $token->expects($this->any())->method('getUser')->will($this->returnValue($user));
     /** @var \PHPUnit_Framework_MockObject_MockObject|TokenStorageInterface $tokenStorage */
     $tokenStorage = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\Storage\\TokenStorageInterface');
     $tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
     /** @var \PHPUnit_Framework_MockObject_MockObject|PriceListTreeHandler $priceListTreeHandler */
     $priceListTreeHandler = $this->getMockBuilder('OroB2B\\Bundle\\PricingBundle\\Model\\PriceListTreeHandler')->disableOriginalConstructor()->getMock();
     $priceListTreeHandler->expects($this->once())->method('getPriceList')->with($user)->will($this->returnValue($priceList));
     $modifier = new FrontendProductListModifier($tokenStorage, $priceListTreeHandler);
     /** @var EntityManager $em */
     $em = $this->getContainer()->get('doctrine')->getManager();
     $qb = $em->createQueryBuilder()->select('p')->from('OroB2BProductBundle:Product', 'p')->orderBy('p.sku');
     $modifier->applyPriceListLimitations($qb, $currency);
     /** @var Product[] $result */
     $result = $qb->getQuery()->getResult();
     $this->assertCount(count($expectedProductSku), $result);
     $sku = array_map(function (Product $product) {
         return $product->getSku();
     }, $result);
     $this->assertEquals($expectedProductSku, $sku);
 }
 /**
  * {@inheritdoc}
  */
 protected function searchEntities($search, $firstResult, $maxResults)
 {
     $currency = null;
     if ($this->request) {
         $currency = $this->request->get('currency');
     }
     $queryBuilder = $this->entityRepository->createQueryBuilder('p');
     $queryBuilder->innerJoin('p.names', 'pn', 'WITH', $queryBuilder->expr()->isNull('pn.locale'))->where($queryBuilder->expr()->like('LOWER(p.sku)', ':search'))->orWhere($queryBuilder->expr()->like('LOWER(pn.string)', ':search'))->setParameter('search', '%' . strtolower($search) . '%')->setFirstResult($firstResult)->setMaxResults($maxResults);
     $this->productListModifier->applyPriceListLimitations($queryBuilder, $currency);
     $query = $this->aclHelper->apply($queryBuilder);
     return $query->getResult();
 }
 /**
  * {@inheritdoc}
  */
 public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
 {
     if (!$this->isApplicable($config) || !$datasource instanceof OrmDatasource) {
         return;
     }
     if ($this->request) {
         $currency = $this->request->get(self::CURRENCY_KEY, null);
     } else {
         $currency = null;
     }
     /** @var OrmDatasource $datasource */
     $qb = $datasource->getQueryBuilder();
     $this->productListModifier->applyPriceListLimitations($qb, $currency);
     $this->applied = true;
 }
 /**
  * @dataProvider visitDatasourceDataProvider
  * @param string $currency
  */
 public function testVisitDatasource($currency)
 {
     if ($currency) {
         /** @var \PHPUnit_Framework_MockObject_MockObject|Request $request */
         $request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request');
         $this->extension->setRequest($request);
         $request->expects($this->once())->method('get')->with(ProductSelectionGridExtension::CURRENCY_KEY)->willReturn($currency);
     }
     $gridName = 'products-select-grid-frontend';
     $token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $token->expects($this->any())->method('getUser')->will($this->returnValue(new AccountUser()));
     /** @var \PHPUnit_Framework_MockObject_MockObject|DatagridConfiguration $config */
     $config = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datagrid\\Common\\DatagridConfiguration')->disableOriginalConstructor()->getMock();
     $config->expects($this->once())->method('getName')->will($this->returnValue($gridName));
     $this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
     $qb = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     /** @var \PHPUnit_Framework_MockObject_MockObject|OrmDatasource $dataSource */
     $dataSource = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\OrmDatasource')->disableOriginalConstructor()->getMock();
     $dataSource->expects($this->once())->method('getQueryBuilder')->will($this->returnValue($qb, $currency));
     $this->productListModifier->expects($this->once())->method('applyPriceListLimitations')->with($qb);
     $this->extension->visitDatasource($config, $dataSource);
     // Check that limits are applied only once
     $this->extension->visitDatasource($config, $dataSource);
 }