mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-31 01:08:26 +00:00 
			
		
		
		
	Fix tests on ExportManager
This commit is contained in:
		| @@ -456,11 +456,11 @@ class ExportManager | ||||
|      * | ||||
|      * | ||||
|      */ | ||||
|     public function isGrantedForElement(ExportElementInterface $element, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export = null, ?array $centers = null): bool | ||||
|     public function isGrantedForElement(ExportInterface|DirectExportInterface|ModifierInterface $element, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export = null, ?array $centers = null): bool | ||||
|     { | ||||
|         if ($element instanceof ExportInterface || $element instanceof DirectExportInterface) { | ||||
|             $role = $element->requiredRole(); | ||||
|         } elseif ($element instanceof ModifierInterface) { | ||||
|         } else { | ||||
|             if (null === $element->addRole()) { | ||||
|                 if (null === $export) { | ||||
|                     throw new LogicException('The export should not be null: as the ' | ||||
| @@ -471,13 +471,10 @@ class ExportManager | ||||
|             } else { | ||||
|                 $role = $element->addRole(); | ||||
|             } | ||||
|         } else { | ||||
|             throw new LogicException('The element is not an ModifiersInterface or ' | ||||
|                     . 'an ExportInterface.'); | ||||
|         } | ||||
|  | ||||
|         if (null === $centers || [] !== $centers) { | ||||
|             // we want to try if at least one center is reachabler | ||||
|         if (null === $centers || [] === $centers) { | ||||
|             // we want to try if at least one center is reachable | ||||
|             return [] !== $this->authorizationHelper->getReachableCenters( | ||||
|                 $this->user, | ||||
|                 $role | ||||
| @@ -542,6 +539,11 @@ class ExportManager | ||||
|         $aggregators = $this->retrieveUsedAggregators($data); | ||||
|  | ||||
|         foreach ($aggregators as $alias => $aggregator) { | ||||
|             if ($this->isGrantedForElement($aggregator, $export, $center) === false) { | ||||
|                 throw new UnauthorizedHttpException('You are not authorized to ' | ||||
|                     . 'use the aggregator' . $aggregator->getTitle()); | ||||
|             } | ||||
|  | ||||
|             $formData = $data[$alias]; | ||||
|             $aggregator->alterQuery($qb, $formData['form']); | ||||
|         } | ||||
|   | ||||
| @@ -450,7 +450,11 @@ class SpreadSheetFormatter implements FormatterInterface | ||||
|     { | ||||
|         $title = $this->translator->trans($this->export->getTitle()); | ||||
|  | ||||
|         return substr($title, 0, 30) . '…'; | ||||
|         if (30 < strlen($title)) { | ||||
|             return substr($title, 0, 30) . '…'; | ||||
|         } | ||||
|  | ||||
|         return $title; | ||||
|     } | ||||
|  | ||||
|     protected function initializeCache($key) | ||||
|   | ||||
| @@ -11,11 +11,14 @@ declare(strict_types=1); | ||||
|  | ||||
| namespace Chill\MainBundle\Tests\Export; | ||||
|  | ||||
| use Chill\MainBundle\Entity\Center; | ||||
| use Chill\MainBundle\Entity\User; | ||||
| use Chill\MainBundle\Export\AggregatorInterface; | ||||
| use Chill\MainBundle\Export\ExportInterface; | ||||
| use Chill\MainBundle\Export\ExportManager; | ||||
| use Chill\MainBundle\Export\FilterInterface; | ||||
| use Chill\MainBundle\Form\Type\Export\ExportType; | ||||
| use Chill\MainBundle\Repository\UserRepositoryInterface; | ||||
| use Chill\MainBundle\Security\Authorization\AuthorizationHelper; | ||||
| use Chill\MainBundle\Test\PrepareCenterTrait; | ||||
| use Chill\MainBundle\Test\PrepareScopeTrait; | ||||
| @@ -27,12 +30,14 @@ use Prophecy\Prophet; | ||||
| use Psr\Log\LoggerInterface; | ||||
| use RuntimeException; | ||||
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||||
| use Symfony\Component\Form\FormBuilderInterface; | ||||
| use Symfony\Component\HttpFoundation\Response; | ||||
| use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||||
| use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||||
| use Symfony\Component\Security\Core\Role\Role; | ||||
| use Symfony\Component\Security\Core\User\UserInterface; | ||||
| use Symfony\Contracts\Translation\TranslatorInterface; | ||||
| use function count; | ||||
|  | ||||
| /** | ||||
| @@ -65,8 +70,7 @@ final class ExportManagerTest extends KernelTestCase | ||||
|  | ||||
|     public function testAggregatorsApplyingOn() | ||||
|     { | ||||
|         $center = $this->prepareCenter(100, 'center'); | ||||
|         $centers = [$center]; | ||||
|         $centers = [$center = new Center()]; | ||||
|         $user = $this->prepareUser([]); | ||||
|  | ||||
|         $authorizationChecker = $this->prophet->prophesize(); | ||||
| @@ -74,121 +78,102 @@ final class ExportManagerTest extends KernelTestCase | ||||
|         $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center) | ||||
|             ->willReturn(true); | ||||
|  | ||||
|         $exportManager = $this->createExportManager( | ||||
|             null, | ||||
|             null, | ||||
|             $authorizationChecker->reveal(), | ||||
|             null, | ||||
|             $user | ||||
|         ); | ||||
|         $exports = []; | ||||
|         $aggregators = []; | ||||
|         $filters = []; | ||||
|  | ||||
|         $exportFooBar = $this->prophet->prophesize(); | ||||
|         $exportFooBar->willImplement(ExportInterface::class); | ||||
|         $exportFooBar->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|         $exportFooBar->supportsModifiers()->willReturn(['foo', 'bar']); | ||||
|         $exports[] = $exportFooBar = new DummyExport('CHILL_STAT_DUMMY', ['foo', 'bar']); | ||||
|  | ||||
|         $aggregatorBar = $this->prophet->prophesize(); | ||||
|         $aggregatorBar->willImplement(AggregatorInterface::class); | ||||
|         $aggregatorBar->applyOn()->willReturn('bar'); | ||||
|         $aggregatorBar->addRole()->willReturn(null); | ||||
|         $exportManager->addAggregator($aggregatorBar->reveal(), 'bar'); | ||||
|         $aggregators['bar'] = $aggregatorBar->reveal(); | ||||
|  | ||||
|         $exportBar = $this->prophet->prophesize(); | ||||
|         $exportBar->willImplement(ExportInterface::class); | ||||
|         $exportBar->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|         $exportBar->supportsModifiers()->willReturn(['bar']); | ||||
|         $exports[] = $exportBar = new DummyExport('CHILL_STAT_DUMMY', ['bar']); | ||||
|  | ||||
|         $aggregatorFoo = $this->prophet->prophesize(); | ||||
|         $aggregatorFoo->willImplement(AggregatorInterface::class); | ||||
|         $aggregatorFoo->applyOn()->willReturn('foo'); | ||||
|         $aggregatorFoo->addRole()->willReturn(null); | ||||
|         $exportManager->addAggregator($aggregatorFoo->reveal(), 'foo'); | ||||
|         $aggregators['foo'] = $aggregatorFoo->reveal(); | ||||
|  | ||||
|         $exportFoo = $this->prophet->prophesize(); | ||||
|         $exportFoo->willImplement(ExportInterface::class); | ||||
|         $exportFoo->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|         $exportFoo->supportsModifiers()->willReturn(['foo']); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportFoo->reveal(), $centers)); | ||||
|         $this->assertEquals(1, count($obtained)); | ||||
|         $this->assertContains('foo', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportBar->reveal(), $centers)); | ||||
|         $this->assertEquals(1, count($obtained)); | ||||
|         $this->assertContains('bar', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportFooBar->reveal(), $centers)); | ||||
|         $this->assertEquals(2, count($obtained)); | ||||
|         $this->assertContains('bar', array_keys($obtained)); | ||||
|         $this->assertContains('foo', array_keys($obtained)); | ||||
|  | ||||
|         // test with empty centers | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportFooBar->reveal(), [])); | ||||
|         $this->assertEquals(0, count($obtained)); | ||||
|     } | ||||
|  | ||||
|     public function testFiltersApplyingOn() | ||||
|     { | ||||
|         $center = $this->prepareCenter(100, 'center'); | ||||
|         $centers = [$center]; | ||||
|         $user = $this->prepareUser([]); | ||||
|  | ||||
|         $authorizationChecker = $this->prophet->prophesize(); | ||||
|         $authorizationChecker->willImplement(AuthorizationCheckerInterface::class); | ||||
|         $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center) | ||||
|             ->willReturn(true); | ||||
|         $exports[] = $exportFoo = new DummyExport('CHILL_STAT_DUMMY', ['foo']); | ||||
|  | ||||
|         $exportManager = $this->createExportManager( | ||||
|             null, | ||||
|             null, | ||||
|             $authorizationChecker->reveal(), | ||||
|             null, | ||||
|             $user | ||||
|             $user, | ||||
|             $exports, | ||||
|             $aggregators, | ||||
|             $filters | ||||
|         ); | ||||
|  | ||||
|         $exportFooBar = $this->prophet->prophesize(); | ||||
|         $exportFooBar->willImplement(ExportInterface::class); | ||||
|         $exportFooBar->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|         $exportFooBar->supportsModifiers()->willReturn(['foo', 'bar']); | ||||
|  | ||||
|         $filterBar = $this->prophet->prophesize(); | ||||
|         $filterBar->willImplement(FilterInterface::class); | ||||
|         $filterBar->applyOn()->willReturn('bar'); | ||||
|         $filterBar->addRole()->willReturn(null); | ||||
|         $filterBar->describeAction(Argument::cetera())->willReturn('string'); | ||||
|         $exportManager->addFilter($filterBar->reveal(), 'bar'); | ||||
|  | ||||
|         $exportBar = $this->prophet->prophesize(); | ||||
|         $exportBar->willImplement(ExportInterface::class); | ||||
|         $exportBar->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|         $exportBar->supportsModifiers()->willReturn(['bar']); | ||||
|  | ||||
|         $filterFoo = $this->prophet->prophesize(); | ||||
|         $filterFoo->willImplement(FilterInterface::class); | ||||
|         $filterFoo->applyOn()->willReturn('foo'); | ||||
|         $filterFoo->addRole()->willReturn(null); | ||||
|         $filterFoo->describeAction(Argument::cetera())->willReturn('string'); | ||||
|         $exportManager->addFilter($filterFoo->reveal(), 'foo'); | ||||
|  | ||||
|         $exportFoo = $this->prophet->prophesize(); | ||||
|         $exportFoo->willImplement(ExportInterface::class); | ||||
|         $exportFoo->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|         $exportFoo->supportsModifiers()->willReturn(['foo']); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportFoo->reveal(), $centers)); | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportFoo, $centers)); | ||||
|         $this->assertEquals(1, count($obtained)); | ||||
|         $this->assertContains('foo', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportBar->reveal(), $centers)); | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportBar, $centers)); | ||||
|         $this->assertEquals(1, count($obtained)); | ||||
|         $this->assertContains('bar', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportFooBar->reveal(), $centers)); | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportFooBar, $centers)); | ||||
|         $this->assertEquals(2, count($obtained)); | ||||
|         $this->assertContains('bar', array_keys($obtained)); | ||||
|         $this->assertContains('foo', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportFooBar->reveal(), [])); | ||||
|         // test with empty centers | ||||
|         $obtained = iterator_to_array($exportManager->getAggregatorsApplyingOn($exportFooBar, [])); | ||||
|         $this->assertEquals(0, count($obtained)); | ||||
|     } | ||||
|  | ||||
|     public function testFiltersApplyingOn() | ||||
|     { | ||||
|         $centers = [$center = new Center()]; | ||||
|         $user = $this->prepareUser([]); | ||||
|  | ||||
|         $authorizationChecker = $this->prophet->prophesize(); | ||||
|         $authorizationChecker->willImplement(AuthorizationCheckerInterface::class); | ||||
|         $authorizationChecker->isGranted(Argument::type('string'), $center) | ||||
|             ->willReturn(true); | ||||
|  | ||||
|         $filters = []; | ||||
|         $exports = []; | ||||
|  | ||||
|         $exports['fooBar'] = $exportFooBar = new DummyExport('CHILL_STAT_DUMMY', ['foo', 'bar']); | ||||
|         $filters['bar'] = $filterBar = new DummyFilterWithApplying(null, 'bar'); | ||||
|         $exports['bar'] = $exportBar = new DummyExport('CHILL_STAT_DUMMY', ['bar']); | ||||
|         $filters['foo'] = $filterFoo = new DummyFilterWithApplying(null, 'foo'); | ||||
|         $exports['foo'] = $exportFoo = new DummyExport('CHILL_STAT_DUMMY', ['foo']); | ||||
|  | ||||
|         $exportManager = $this->createExportManager( | ||||
|             null, | ||||
|             null, | ||||
|             $authorizationChecker->reveal(), | ||||
|             null, | ||||
|             $user, | ||||
|             $exports, | ||||
|             [], | ||||
|             $filters | ||||
|         ); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportFoo, $centers)); | ||||
|         $this->assertEquals(1, count($obtained)); | ||||
|         $this->assertContains('foo', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportBar, $centers)); | ||||
|         $this->assertEquals(1, count($obtained)); | ||||
|         $this->assertContains('bar', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportFooBar, $centers)); | ||||
|         $this->assertEquals(2, count($obtained)); | ||||
|         $this->assertContains('bar', array_keys($obtained)); | ||||
|         $this->assertContains('foo', array_keys($obtained)); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFiltersApplyingOn($exportFooBar, [])); | ||||
|         $this->assertEquals(0, count($obtained)); | ||||
|     } | ||||
|  | ||||
| @@ -224,18 +209,14 @@ final class ExportManagerTest extends KernelTestCase | ||||
|         $authorizationChecker->willImplement(AuthorizationCheckerInterface::class); | ||||
|         $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center) | ||||
|             ->willReturn(true); | ||||
|         $exports = []; | ||||
|         $filters = []; | ||||
|         $aggregators = []; | ||||
|  | ||||
|         $exportManager = $this->createExportManager( | ||||
|             null, | ||||
|             null, | ||||
|             $authorizationChecker->reveal(), | ||||
|             null, | ||||
|             $user | ||||
|         ); | ||||
|         $em = self::$container->get(EntityManagerInterface::class); | ||||
|  | ||||
|         $export = $this->prophet->prophesize(); | ||||
|         $export->willImplement(ExportInterface::class); | ||||
|         $em = self::$container->get('doctrine.orm.entity_manager'); | ||||
|         $export->initiateQuery( | ||||
|             Argument::is(['foo']), | ||||
|             Argument::Type('array'), | ||||
| @@ -245,11 +226,15 @@ final class ExportManagerTest extends KernelTestCase | ||||
|                 $qb = $em->createQueryBuilder(); | ||||
|  | ||||
|                 return $qb->addSelect('COUNT(user.id) as export') | ||||
|                     ->from('ChillMainBundle:User', 'user'); | ||||
|                     ->from(User::class, 'user'); | ||||
|             }); | ||||
|         //$export->initiateQuery()->shouldBeCalled(); | ||||
|         $export->initiateQuery( | ||||
|             Argument::is(['foo']), | ||||
|             Argument::Type('array'), | ||||
|             Argument::is(['a' => 'b']) | ||||
|         )->shouldBeCalled(); | ||||
|         $export->supportsModifiers()->willReturn(['foo']); | ||||
|         $export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|         $export->requiredRole()->willReturn('CHILL_STAT_DUMMY'); | ||||
|         $export->getResult(Argument::Type(QueryBuilder::class), Argument::Type('array'))->willReturn([ | ||||
|             [ | ||||
|                 'aggregator' => 'cat a', | ||||
| @@ -280,25 +265,28 @@ final class ExportManagerTest extends KernelTestCase | ||||
|  | ||||
|         $export->getQueryKeys(Argument::Type('array'))->willReturn(['export']); | ||||
|         $export->getTitle()->willReturn('dummy title'); | ||||
|         $exportManager->addExport($export->reveal(), 'dummy'); | ||||
|         $exports['dummy'] = $export->reveal(); | ||||
|  | ||||
|         $filter = $this->prophet->prophesize(); | ||||
|         $filter->willImplement(FilterInterface::class); | ||||
|         //$filter->alterQuery()->shouldBeCalled(); | ||||
|         $filter->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) | ||||
|             ->willReturn(null); | ||||
|         $filter->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) | ||||
|             ->shouldBeCalled(); | ||||
|         $filter->addRole()->shouldBeCalled(); | ||||
|         //$filter->addRole()->shouldBeCalled(); | ||||
|         $filter->addRole()->willReturn(null); | ||||
|         $filter->applyOn()->willReturn('foo'); | ||||
|         $filter->describeAction(Argument::cetera())->willReturn('filtered string'); | ||||
|         $exportManager->addFilter($filter->reveal(), 'filter_foo'); | ||||
|         $filters['filter_foo'] = $filter->reveal(); | ||||
|  | ||||
|         $aggregator = $this->prophet->prophesize(); | ||||
|         $aggregator->willImplement(AggregatorInterface::class); | ||||
|         $aggregator->addRole()->willReturn(null); | ||||
|         $aggregator->applyOn()->willReturn('foo'); | ||||
|         $aggregator->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) | ||||
|             ->willReturn(null); | ||||
|         //$aggregator->alterQuery()->shouldBeCalled(); | ||||
|         $aggregator->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) | ||||
|             ->shouldBeCalled(); | ||||
|         $aggregator->getQueryKeys(Argument::Type('array'))->willReturn(['aggregator']); | ||||
|         $aggregator->getLabels( | ||||
|             Argument::is('aggregator'), | ||||
| @@ -312,17 +300,28 @@ final class ExportManagerTest extends KernelTestCase | ||||
|                 default => throw new RuntimeException(sprintf('This value (%s) is not valid', $value)), | ||||
|             }); | ||||
|         $aggregator->addRole()->willReturn(null); | ||||
|         //$aggregator->addRole()->shouldBeCalled(); | ||||
|         $exportManager->addAggregator($aggregator->reveal(), 'aggregator_foo'); | ||||
|         $aggregator->addRole()->shouldBeCalled(); | ||||
|         $aggregators['aggregator_foo'] = $aggregator->reveal(); | ||||
|  | ||||
|         $exportManager = $this->createExportManager( | ||||
|             null, | ||||
|             null, | ||||
|             $authorizationChecker->reveal(), | ||||
|             null, | ||||
|             $user, | ||||
|             $exports, | ||||
|             $aggregators, | ||||
|             $filters | ||||
|         ); | ||||
|  | ||||
|         //add formatter interface | ||||
|         $formatter = new \Chill\MainBundle\Export\Formatter\SpreadSheetFormatter( | ||||
|             self::$container->get('translator'), | ||||
|             self::$container->get(TranslatorInterface::class), | ||||
|             $exportManager | ||||
|         ); | ||||
|  | ||||
|         $exportManager->addFormatter($formatter, 'spreadsheet'); | ||||
|  | ||||
|         //ob_start(); | ||||
|         $response = $exportManager->generate( | ||||
|             'dummy', | ||||
|             [$center], | ||||
| @@ -353,14 +352,14 @@ final class ExportManagerTest extends KernelTestCase | ||||
|                 ], | ||||
|             ] | ||||
|         ); | ||||
|         //$content = ob_get_clean(); | ||||
|  | ||||
|         $this->assertInstanceOf(Response::class, $response); | ||||
|         $expected = <<<'EOT' | ||||
|             "dummy title","" | ||||
|             "","" | ||||
|             "filtered string","" | ||||
|             "foo_header","_header" | ||||
|             "label cat a","" | ||||
|             "foo_header","export" | ||||
|             "label cat a","0" | ||||
|             "label cat b","1" | ||||
|  | ||||
|             EOT; | ||||
| @@ -368,162 +367,6 @@ final class ExportManagerTest extends KernelTestCase | ||||
|         $this->assertEquals($expected, $response->getContent()); | ||||
|     } | ||||
|  | ||||
|     public function testGetAggregator() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create a filter and add it to ExportManager | ||||
|         $agg = $this->prophet->prophesize(); | ||||
|         $agg->willImplement(\Chill\MainBundle\Export\AggregatorInterface::class); | ||||
|         $exportManager->addAggregator($agg->reveal(), 'dummy'); | ||||
|  | ||||
|         $obtained = $exportManager->getAggregator('dummy'); | ||||
|  | ||||
|         $this->assertInstanceof(\Chill\MainBundle\Export\AggregatorInterface::class, $obtained); | ||||
|     } | ||||
|  | ||||
|     public function testGetAggregatorNonExistant() | ||||
|     { | ||||
|         $this->expectException(RuntimeException::class); | ||||
|  | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         $exportManager->getAggregator('non existing'); | ||||
|     } | ||||
|  | ||||
|     public function testGetAggregators() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create three filters and add them to ExportManager | ||||
|         $aggFoo = $this->prophet->prophesize(); | ||||
|         $aggFoo->willImplement(\Chill\MainBundle\Export\AggregatorInterface::class); | ||||
|         $aggBar = $this->prophet->prophesize(); | ||||
|         $aggBar->willImplement(\Chill\MainBundle\Export\AggregatorInterface::class); | ||||
|         $aggFooBar = $this->prophet->prophesize(); | ||||
|         $aggFooBar->willImplement(\Chill\MainBundle\Export\AggregatorInterface::class); | ||||
|         $exportManager->addAggregator($aggFoo->reveal(), 'foo'); | ||||
|         $exportManager->addAggregator($aggBar->reveal(), 'bar'); | ||||
|         $exportManager->addAggregator($aggFooBar->reveal(), 'foobar'); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getAggregators(['foo', 'bar'])); | ||||
|  | ||||
|         $this->assertContains($aggBar->reveal(), $obtained); | ||||
|         $this->assertContains($aggFoo->reveal(), $obtained); | ||||
|         $this->assertNotContains($aggFooBar->reveal(), $obtained); | ||||
|     } | ||||
|  | ||||
|     public function testGetExistingExportsTypes() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create an export and add it to ExportManager | ||||
|         $export = $this->prophet->prophesize(); | ||||
|         $export->willImplement(ExportInterface::class); | ||||
|         $export->getType()->willReturn('my_type'); | ||||
|         $exportManager->addExport($export->reveal(), 'dummy'); | ||||
|  | ||||
|         $this->assertContains('my_type', $exportManager->getExistingExportsTypes()); | ||||
|     } | ||||
|  | ||||
|     public function testGetExport() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create an export and add it to ExportManager | ||||
|         $export = $this->prophet->prophesize(); | ||||
|         $export->willImplement(ExportInterface::class); | ||||
|         $exportManager->addExport($export->reveal(), 'dummy'); | ||||
|  | ||||
|         $obtained = $exportManager->getExport('dummy'); | ||||
|  | ||||
|         $this->assertInstanceof(ExportInterface::class, $obtained); | ||||
|     } | ||||
|  | ||||
|     public function testGetExportNonExistant() | ||||
|     { | ||||
|         $this->expectException(RuntimeException::class); | ||||
|  | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         $exportManager->getExport('non existing'); | ||||
|     } | ||||
|  | ||||
|     public function testGetExportsWithoutGranting() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create an export and add it to ExportManager | ||||
|         $export = $this->prophet->prophesize(); | ||||
|         $export->willImplement(ExportInterface::class); | ||||
|         $exportManager->addExport($export->reveal(), 'dummy'); | ||||
|  | ||||
|         $exports = iterator_to_array($exportManager->getExports(false)); | ||||
|  | ||||
|         $this->assertGreaterThan(0, count($exports)); | ||||
|         $this->assertContains($export->reveal(), $exports); | ||||
|         $this->assertContains('dummy', array_keys($exports)); | ||||
|     } | ||||
|  | ||||
|     public function testGetFilter() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create a filter and add it to ExportManager | ||||
|         $filter = $this->prophet->prophesize(); | ||||
|         $filter->willImplement(\Chill\MainBundle\Export\FilterInterface::class); | ||||
|         $exportManager->addFilter($filter->reveal(), 'dummy'); | ||||
|  | ||||
|         $obtained = $exportManager->getFilter('dummy'); | ||||
|  | ||||
|         $this->assertInstanceof(\Chill\MainBundle\Export\FilterInterface::class, $obtained); | ||||
|     } | ||||
|  | ||||
|     public function testGetFilterNonExistant() | ||||
|     { | ||||
|         $this->expectException(RuntimeException::class); | ||||
|  | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         $exportManager->getFilter('non existing'); | ||||
|     } | ||||
|  | ||||
|     public function testGetFilters() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create three filters and add them to ExportManager | ||||
|         $filterFoo = $this->prophet->prophesize(); | ||||
|         $filterFoo->willImplement(\Chill\MainBundle\Export\FilterInterface::class); | ||||
|         $filterBar = $this->prophet->prophesize(); | ||||
|         $filterBar->willImplement(\Chill\MainBundle\Export\FilterInterface::class); | ||||
|         $filterFooBar = $this->prophet->prophesize(); | ||||
|         $filterFooBar->willImplement(\Chill\MainBundle\Export\FilterInterface::class); | ||||
|         $exportManager->addFilter($filterFoo->reveal(), 'foo'); | ||||
|         $exportManager->addFilter($filterBar->reveal(), 'bar'); | ||||
|         $exportManager->addFilter($filterFooBar->reveal(), 'foobar'); | ||||
|  | ||||
|         $obtained = iterator_to_array($exportManager->getFilters(['foo', 'bar'])); | ||||
|  | ||||
|         $this->assertContains($filterBar->reveal(), $obtained); | ||||
|         $this->assertContains($filterFoo->reveal(), $obtained); | ||||
|         $this->assertNotContains($filterFooBar->reveal(), $obtained); | ||||
|     } | ||||
|  | ||||
|     public function testGetFormatter() | ||||
|     { | ||||
|         $exportManager = $this->createExportManager(); | ||||
|  | ||||
|         //create a formatter | ||||
|         $formatter = $this->prophet->prophesize(); | ||||
|         $formatter->willImplement(\Chill\MainBundle\Export\FormatterInterface::class); | ||||
|         $exportManager->addFormatter($formatter->reveal(), 'dummy'); | ||||
|  | ||||
|         $obtained = $exportManager->getFormatter('dummy'); | ||||
|  | ||||
|         $this->assertInstanceOf(\Chill\MainBundle\Export\FormatterInterface::class, $obtained); | ||||
|     } | ||||
|  | ||||
|     public function testIsGrantedForElementWithExportAndUserIsGranted() | ||||
|     { | ||||
|         $center = $this->prepareCenter(100, 'center A'); | ||||
| @@ -534,18 +377,19 @@ final class ExportManagerTest extends KernelTestCase | ||||
|         $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center) | ||||
|             ->willReturn(true); | ||||
|  | ||||
|         $export = $this->prophet->prophesize(); | ||||
|         $export->willImplement(ExportInterface::class); | ||||
|         $export->requiredRole()->willReturn('CHILL_STAT_DUMMY'); | ||||
|  | ||||
|         $exportManager = $this->createExportManager( | ||||
|             null, | ||||
|             null, | ||||
|             $authorizationChecker->reveal(), | ||||
|             null, | ||||
|             $user | ||||
|             $user, | ||||
|             ['dummy_export' => $export->reveal()], | ||||
|         ); | ||||
|  | ||||
|         $export = $this->prophet->prophesize(); | ||||
|         $export->willImplement(ExportInterface::class); | ||||
|         $export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY')); | ||||
|  | ||||
|         $result = $exportManager->isGrantedForElement($export->reveal(), null, [$center]); | ||||
|  | ||||
|         $this->assertTrue($result); | ||||
| @@ -661,12 +505,14 @@ final class ExportManagerTest extends KernelTestCase | ||||
|         ?EntityManagerInterface $em = null, | ||||
|         ?AuthorizationCheckerInterface $authorizationChecker = null, | ||||
|         ?AuthorizationHelper $authorizationHelper = null, | ||||
|         ?UserInterface $user = null | ||||
|         ?UserInterface $user = null, | ||||
|         array $exports = [], | ||||
|         array $aggregators = [], | ||||
|         array $filters = [], | ||||
|     ): ExportManager { | ||||
|         $localUser = $user ?? self::$container->get( | ||||
|             'doctrine.orm.entity_manager' | ||||
|             UserRepositoryInterface::class | ||||
|         ) | ||||
|             ->getRepository(\Chill\MainBundle\Entity\User::class) | ||||
|             ->findOneBy(['username' => 'center a_social']); | ||||
|         $token = new UsernamePasswordToken($localUser, 'password', 'provider'); | ||||
|         $tokenStorage = new TokenStorage(); | ||||
| @@ -677,9 +523,116 @@ final class ExportManagerTest extends KernelTestCase | ||||
|             $authorizationChecker ?? self::$container->get('security.authorization_checker'), | ||||
|             $authorizationHelper ?? self::$container->get('chill.main.security.authorization.helper'), | ||||
|             $tokenStorage, | ||||
|             [], | ||||
|             [], | ||||
|             [] | ||||
|             $exports, | ||||
|             $aggregators, | ||||
|             $filters | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|  | ||||
| class DummyFilterWithApplying implements FilterInterface | ||||
| { | ||||
|     public function __construct( | ||||
|         private ?string $role, | ||||
|         private string $applyOn | ||||
|     ) { | ||||
|     } | ||||
|  | ||||
|     public function getTitle() | ||||
|     { | ||||
|         return 'dummy'; | ||||
|     } | ||||
|     public function buildForm(FormBuilderInterface $builder) | ||||
|     { | ||||
|     } | ||||
|     public function getFormDefaultData(): array | ||||
|     { | ||||
|         return []; | ||||
|     } | ||||
|     public function describeAction($data, $format = 'string') | ||||
|     { | ||||
|         return ['dummy filter', []]; | ||||
|     } | ||||
|     public function addRole(): ?string | ||||
|     { | ||||
|         return $this->role; | ||||
|     } | ||||
|     public function alterQuery(QueryBuilder $qb, $data) | ||||
|     { | ||||
|     } | ||||
|     public function applyOn() | ||||
|     { | ||||
|         return $this->applyOn; | ||||
|     } | ||||
| } | ||||
|  | ||||
| class DummyExport implements ExportInterface | ||||
| { | ||||
|     public function __construct( | ||||
|         private string $role, | ||||
|         /** | ||||
|          * @var array<string> | ||||
|          */ | ||||
|         private array $supportedModifiers, | ||||
|     ) { | ||||
|     } | ||||
|  | ||||
|     public function getTitle() | ||||
|     { | ||||
|         return 'dummy'; | ||||
|     } | ||||
|  | ||||
|     public function buildForm(FormBuilderInterface $builder) | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     public function getFormDefaultData(): array | ||||
|     { | ||||
|         return []; | ||||
|     } | ||||
|  | ||||
|     public function getAllowedFormattersTypes() | ||||
|     { | ||||
|         return ['test']; | ||||
|     } | ||||
|  | ||||
|     public function getDescription() | ||||
|     { | ||||
|         return 'dummy export'; | ||||
|     } | ||||
|  | ||||
|     public function getLabels($key, array $values, mixed $data) | ||||
|     { | ||||
|         return []; | ||||
|     } | ||||
|  | ||||
|     public function getQueryKeys($data) | ||||
|     { | ||||
|         return []; | ||||
|     } | ||||
|  | ||||
|     public function getResult($query, $data) | ||||
|     { | ||||
|         return []; | ||||
|     } | ||||
|  | ||||
|     public function getType() | ||||
|     { | ||||
|         return 'dummy'; | ||||
|     } | ||||
|  | ||||
|     public function initiateQuery(array $requiredModifiers, array $acl, array $data = []) | ||||
|     { | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     public function requiredRole(): string | ||||
|     { | ||||
|         return $this->role; | ||||
|     } | ||||
|  | ||||
|     public function supportsModifiers() | ||||
|     { | ||||
|         return $this->supportedModifiers; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user