Merge branch '179-remove-filter-by-center-in-exports' into 'master'

Resolve "Export: permettre de ne pas filtrer les résultats par centre"

Closes #179

See merge request Chill-Projet/chill-bundles!599
This commit is contained in:
2023-10-24 13:18:03 +00:00
48 changed files with 1168 additions and 412 deletions

View File

@@ -27,6 +27,7 @@ use Chill\MainBundle\Security\Authorization\SavedExportVoter;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
@@ -46,6 +47,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
*/
class ExportController extends AbstractController
{
private readonly bool $filterStatsByCenters;
public function __construct(
private readonly ChillRedis $redis,
private readonly ExportManager $exportManager,
@@ -56,8 +59,11 @@ class ExportController extends AbstractController
private readonly EntityManagerInterface $entityManager,
private readonly ExportFormHelper $exportFormHelper,
private readonly SavedExportRepositoryInterface $savedExportRepository,
private readonly Security $security
) {}
private readonly Security $security,
ParameterBagInterface $parameterBag,
) {
$this->filterStatsByCenters = $parameterBag->get('chill_main')['acl']['filter_stats_by_center'];
}
/**
* @\Symfony\Component\Routing\Annotation\Route(path="/{_locale}/exports/download/{alias}", name="chill_main_export_download", methods={"GET"})
@@ -484,9 +490,13 @@ class ExportController extends AbstractController
$alias = $rawData['alias'];
$formCenters = $this->createCreateFormExport($alias, 'generate_centers', [], $savedExport);
$formCenters->submit($rawData['centers']);
$dataCenters = $formCenters->getData();
if ($this->filterStatsByCenters) {
$formCenters = $this->createCreateFormExport($alias, 'generate_centers', [], $savedExport);
$formCenters->submit($rawData['centers']);
$dataCenters = $formCenters->getData();
} else {
$dataCenters = ['centers' => []];
}
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters, $savedExport);
$formExport->submit($rawData['export']);
@@ -513,6 +523,14 @@ class ExportController extends AbstractController
*/
private function selectCentersStep(Request $request, DirectExportInterface|ExportInterface $export, $alias, SavedExport $savedExport = null)
{
if (!$this->filterStatsByCenters) {
return $this->redirectToRoute('chill_main_export_new', [
'step' => $this->getNextStep('centers', $export),
'alias' => $alias,
'from_saved' => $request->get('from_saved', ''),
]);
}
/** @var \Chill\MainBundle\Export\ExportManager $exportManager */
$exportManager = $this->exportManager;

View File

@@ -116,8 +116,12 @@ class Configuration implements ConfigurationInterface
->booleanNode('form_show_centers')
->defaultTrue()
->end()
->booleanNode('filter_stats_by_center')
->defaultTrue()
->info("if set to false, the exports won't take into account the center of the people")
->end()
->end()
->end() // end of 'acl'
->booleanNode('access_global_history')
->defaultTrue()
->end()

View File

@@ -11,11 +11,15 @@ declare(strict_types=1);
namespace Chill\MainBundle\Test\Export;
use Chill\MainBundle\Export\DirectExportInterface;
use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NativeQuery;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
/**
* This class provide a set of tests for exports.
@@ -94,7 +98,7 @@ abstract class AbstractExportTest extends WebTestCase
/**
* Create an instance of the report to test.
*
* @return \Chill\MainBundle\Export\ExportInterface an instance of the export to test
* @return ExportInterface|DirectExportInterface|iterable<ExportInterface>|iterable<DirectExportInterface> an instance of the export to test
*/
abstract public function getExport();
@@ -116,19 +120,40 @@ abstract class AbstractExportTest extends WebTestCase
*/
abstract public function getModifiersCombination();
protected function getParameters(bool $filterStatsByCenter): ParameterBagInterface
{
return new ParameterBag(['chill_main' => ['acl' => ['filter_stats_by_center' => $filterStatsByCenter]]]);
}
/**
* wrap the results of @see{self::getExports()}, which may be an iterable or an export into an iterble.
*/
private function getExports(): iterable
{
$exports = $this->getExport();
if (is_iterable($exports)) {
return $exports;
}
return [$exports];
}
/**
* Test the formatters type are string.
*/
public function testGetAllowedFormattersType()
{
$formattersTypes = $this->getExport()->getAllowedFormattersTypes();
foreach ($this->getExports() as $export) {
$formattersTypes = $export->getAllowedFormattersTypes();
$this->assertContainsOnly(
'string',
$formattersTypes,
true,
'Test that the method `getAllowedFormattersTypes` returns an array of string'
);
$this->assertContainsOnly(
'string',
$formattersTypes,
true,
'Test that the method `getAllowedFormattersTypes` returns an array of string'
);
}
}
/**
@@ -136,17 +161,17 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetDescription()
{
$export = $this->getExport();
$this->assertIsString(
$export->getDescription(),
'Assert that the `getDescription` method return a string'
);
$this->assertNotEmpty(
$export->getDescription(),
'Assert that the `getDescription` method does not return an empty '
.'string.'
);
foreach ($this->getExports() as $export) {
$this->assertIsString(
$export->getDescription(),
'Assert that the `getDescription` method return a string'
);
$this->assertNotEmpty(
$export->getDescription(),
'Assert that the `getDescription` method does not return an empty '
.'string.'
);
}
}
/**
@@ -156,19 +181,21 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetQueryKeys(array $data)
{
$queryKeys = $this->getExport()->getQueryKeys($data);
foreach ($this->getExports() as $export) {
$queryKeys = $export->getQueryKeys($data);
$this->assertContainsOnly(
'string',
$queryKeys,
true,
'test that the query keys returned by `getQueryKeys` are only strings'
);
$this->assertGreaterThanOrEqual(
1,
\count($queryKeys),
'test that there are at least one query key returned'
);
$this->assertContainsOnly(
'string',
$queryKeys,
true,
'test that the query keys returned by `getQueryKeys` are only strings'
);
$this->assertGreaterThanOrEqual(
1,
\count($queryKeys),
'test that there are at least one query key returned'
);
}
}
/**
@@ -187,61 +214,70 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetResultsAndLabels($modifiers, $acl, array $data)
{
// it is more convenient to group the `getResult` and `getLabels` test
// due to the fact that testing both methods use the same tools.
foreach ($this->getExports() as $export) {
// it is more convenient to group the `getResult` and `getLabels` test
// due to the fact that testing both methods use the same tools.
$queryKeys = $this->getExport()->getQueryKeys($data);
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
$queryKeys = $export->getQueryKeys($data);
$query = $export->initiateQuery($modifiers, $acl, $data);
// limit the result for the query for performance reason (only for QueryBuilder,
// not possible in NativeQuery)
if ($query instanceof QueryBuilder) {
$query->setMaxResults(1);
}
// limit the result for the query for performance reason (only for QueryBuilder,
// not possible in NativeQuery)
if ($query instanceof QueryBuilder) {
$query->setMaxResults(1);
}
$results = $this->getExport()->getResult($query, $data);
$results = $export->getResult($query, $data);
$this->assertIsArray(
$results,
'assert that the returned result is an array'
);
if (0 === \count($results)) {
$this->markTestIncomplete('The result is empty. We cannot process tests '
.'on results');
}
// testing the result
$result = $results[0];
$this->assertTrue(
is_iterable($result),
'test that each row in the result is traversable or an array'
);
foreach ($result as $key => $value) {
$this->assertContains(
$key,
$queryKeys,
'test that each key is present in `getQueryKeys`'
$this->assertIsArray(
$results,
'assert that the returned result is an array'
);
$closure = $this->getExport()->getLabels($key, [$value], $data);
if (0 === \count($results)) {
$this->markTestIncomplete('The result is empty. We cannot process tests '
.'on results');
}
// testing the result
$result = $results[0];
$this->assertTrue(
\is_callable($closure, false),
'test that the `getLabels` for key is a callable'
is_iterable($result),
'test that each row in the result is traversable or an array'
);
$this->assertTrue(
// conditions
\is_string((string) \call_user_func($closure, '_header'))
&& !empty(\call_user_func($closure, '_header'))
&& '_header' !== \call_user_func($closure, '_header'),
// message
sprintf('Test that the callable return by `getLabels` for key %s '
.'can provide an header', $key)
);
$i = 0;
foreach ($result as $key => $value) {
$this->assertContains(
$key,
$queryKeys,
'test that each key is present in `getQueryKeys`'
);
$closure = $export->getLabels($key, [$value], $data);
$this->assertTrue(
\is_callable($closure, false),
'test that the `getLabels` for key is a callable'
);
$this->assertTrue(
// conditions
\is_string((string) \call_user_func($closure, '_header'))
&& !empty(\call_user_func($closure, '_header'))
&& '_header' !== \call_user_func($closure, '_header'),
// message
sprintf('Test that the callable return by `getLabels` for key %s '
.'can provide an header', $key)
);
++$i;
if ($i > 15) {
// do not iterate on each result
break;
}
}
}
}
@@ -250,14 +286,14 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetType()
{
$export = $this->getExport();
$this->assertIsString(
$export->getType(),
'Assert that the `getType` method return a string'
);
$this->assertNotEmpty($export->getType(), 'Assert that the `getType` method'
.' does not return an empty string.');
foreach ($this->getExports() as $export) {
$this->assertIsString(
$export->getType(),
'Assert that the `getType` method return a string'
);
$this->assertNotEmpty($export->getType(), 'Assert that the `getType` method'
.' does not return an empty string.');
}
}
/**
@@ -272,34 +308,36 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testInitiateQuery(mixed $modifiers, mixed $acl, mixed $data)
{
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
foreach ($this->getExports() as $export) {
$query = $export->initiateQuery($modifiers, $acl, $data);
$this->assertTrue(
$query instanceof QueryBuilder || $query instanceof NativeQuery,
sprintf(
'Assert that the returned query is an instance of %s or %s',
QueryBuilder::class,
Query::class
)
);
if ($query instanceof QueryBuilder) {
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('select')),
"assert there is at least one 'select' part"
$this->assertTrue(
$query instanceof QueryBuilder || $query instanceof NativeQuery,
sprintf(
'Assert that the returned query is an instance of %s or %s',
QueryBuilder::class,
NativeQuery::class
)
);
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('from')),
"assert there is at least one 'from' part"
);
} elseif ($query instanceof NativeQuery) {
$this->assertNotEmpty(
$query->getSQL(),
'check that the SQL query is not empty'
);
if ($query instanceof QueryBuilder) {
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('select')),
"assert there is at least one 'select' part"
);
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('from')),
"assert there is at least one 'from' part"
);
} elseif ($query instanceof NativeQuery) {
$this->assertNotEmpty(
$query->getSQL(),
'check that the SQL query is not empty'
);
}
}
}
@@ -308,9 +346,11 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testRequiredRole()
{
$role = $this->getExport()->requiredRole();
foreach ($this->getExports() as $export) {
$role = $export->requiredRole();
self::assertIsString($role);
self::assertIsString($role);
}
}
/**
@@ -323,22 +363,23 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testSupportsModifier(mixed $modifiers, mixed $acl, mixed $data)
{
$export = $this->getExport();
$query = $export->initiateQuery($modifiers, $acl, $data);
foreach ($this->getExports() as $export) {
$query = $export->initiateQuery($modifiers, $acl, $data);
if ($query instanceof QueryBuilder) {
$this->assertContainsOnly(
'string',
$export->supportsModifiers(),
true,
'Test that the `supportsModifiers` method returns only strings'
);
} elseif ($query instanceof NativeQuery) {
$this->assertTrue(
null === $export->supportsModifiers()
|| 0 === \count($export->supportsModifiers()),
'Test that the `supportsModifier` methods returns null or an empty array'
);
if ($query instanceof QueryBuilder) {
$this->assertContainsOnly(
'string',
$export->supportsModifiers(),
true,
'Test that the `supportsModifiers` method returns only strings'
);
} elseif ($query instanceof NativeQuery) {
$this->assertTrue(
null === $export->supportsModifiers()
|| 0 === \count($export->supportsModifiers()),
'Test that the `supportsModifier` methods returns null or an empty array'
);
}
}
}
}