/**
  * {@inheritdoc}
  */
 public function handle(MassActionHandlerArgs $args)
 {
     $massAction = $args->getMassAction();
     $entityName = $this->actionRepository->getEntityName($args->getDatagrid());
     if (!$this->isMassActionEnabled($entityName)) {
         return $this->getResponse($massAction, 0, 'Action not configured or not allowed');
     }
     $massAction->getOptions()->offsetSet('entityName', $entityName);
     $entitiesCount = $this->actionRepository->batchUpdate($massAction, $args->getResults(), $args->getData());
     return $this->getResponse($massAction, $entitiesCount);
 }
 public function testGetEntityName()
 {
     /** @var DatagridInterface|\PHPUnit_Framework_MockObject_MockObject $datagridMock */
     $datagridMock = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datagrid\\DatagridInterface')->disableOriginalConstructor()->getMock();
     $datagridMock->expects($this->once())->method('getDatasource')->will($this->returnValue(null));
     $resultEntityName = $this->actionRepo->getEntityName($datagridMock);
     $this->assertNull($resultEntityName, 'Failed asserting that datasource configured');
     // assert datasource configured
     $datagridMock = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datagrid\\DatagridInterface')->disableOriginalConstructor()->getMock();
     $datasourceMock = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\OrmDatasource')->disableOriginalConstructor()->getMock();
     $datagridMock->expects($this->once())->method('getDatasource')->will($this->returnValue($datasourceMock));
     $qbMock = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     $datasourceMock->expects($this->once())->method('getQueryBuilder')->willReturn($qbMock);
     $qbMock->expects($this->once())->method('getRootEntities')->willReturn(['Test\\Entity', 'Test\\AnotherEntity']);
     $resultEntityName = $this->actionRepo->getEntityName($datagridMock);
     $this->assertEquals('Test\\Entity', $resultEntityName, 'Failed asserting that entity name was found');
 }
 public function testHandleUpdateNotAllowed()
 {
     $massActionMock = $this->getMock(MassActionInterface::class);
     $datagridMock = $this->getMock('Oro\\Bundle\\DataGridBundle\\Datagrid\\DatagridInterface');
     $iteratorMock = $this->getMock('Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\IterableResultInterface');
     $entityName = 'Test\\EntityName';
     $hasConfig = true;
     $isEnabled = true;
     $isGranted = false;
     $data = [];
     $this->actionRepoMock->expects($this->once())->method('getEntityName')->with($datagridMock)->will($this->returnValue($entityName));
     $this->configMock->expects($this->once())->method('hasConfig')->with($entityName)->will($this->returnValue($hasConfig));
     $this->configMock->expects($this->once())->method('getConfig')->with($entityName)->will($this->returnValue(new Config(new EntityConfigId('extend', $entityName), ['update_mass_action_enabled' => $isEnabled])));
     $this->securityMock->expects($this->once())->method('isGranted')->with('EDIT', 'entity:' . $entityName)->will($this->returnValue($isGranted));
     $this->actionRepoMock->expects($this->never())->method('batchUpdate');
     $this->transMock->expects($this->once())->method('trans')->will($this->returnValue(uniqid()));
     $options = ActionConfiguration::create(['success_message' => '', 'error_message' => '']);
     $massActionMock->expects($this->any())->method('getOptions')->will($this->returnValue($options));
     $this->transMock->expects($this->once())->method('trans')->with('', ['%error%' => 'Action not configured or not allowed']);
     $this->loggerMock->expects($this->once())->method('debug');
     $actionResponse = $this->handler->handle(new MassActionHandlerArgs($massActionMock, $datagridMock, $iteratorMock, $data));
     $this->assertFalse($actionResponse->isSuccessful());
 }