Merge branch 'feature/stats' of https://github.com/monarc-project/MonarcAppFO into feature/stats

pull/316/head
Cédric Bonhomme 2020-09-02 14:06:08 +02:00
commit d5b240733e
2 changed files with 86 additions and 26 deletions

View File

@ -232,6 +232,7 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
array_merge([ array_merge([
'anrs' => [$anr1->getUuid(), $anr2->getUuid(), $anr3->getUuid()], 'anrs' => [$anr1->getUuid(), $anr2->getUuid(), $anr3->getUuid()],
'type' => StatsDataObject::TYPE_COMPLIANCE, 'type' => StatsDataObject::TYPE_COMPLIANCE,
'get_last' => 0,
], $defaultDates), ], $defaultDates),
$queryParams $queryParams
); );
@ -241,23 +242,8 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
'type' => StatsDataObject::TYPE_RISK, 'type' => StatsDataObject::TYPE_RISK,
'anr' => '1232-31abcd-213efgh-123klmp', 'anr' => '1232-31abcd-213efgh-123klmp',
'data' => [ 'data' => [
'total' => [ 'risks' => [],
[ 'total' => [],
'label' => 'Low risks',
'value' => 50,
],
[
'label' => 'Medium risks',
'value' => 30,
],
[
'label' => 'High risks',
'value' => 10,
],
],
], ],
]) ])
]); ]);
@ -265,13 +251,14 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
$stats = $this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_RISK, 'anrs' => [1, 3, 7]]); $stats = $this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_RISK, 'anrs' => [1, 3, 7]]);
$this->assertEquals($this->getStatsResponse($stats), $statsResponse); $this->assertEquals($this->getStatsResponse($stats), $this->getStatsResponse());
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams); parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
$this->assertEquals( $this->assertEquals(
array_merge([ array_merge([
'anrs' => [$anr1->getUuid(), $anr3->getUuid()], 'anrs' => [$anr1->getUuid(), $anr3->getUuid()],
'type' => StatsDataObject::TYPE_RISK, 'type' => StatsDataObject::TYPE_RISK,
'get_last' => 0,
], $defaultDates), ], $defaultDates),
$queryParams $queryParams
); );
@ -307,21 +294,25 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams); parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
$this->assertEquals(array_merge([ $this->assertEquals(array_merge([
'anrs' => $anrUuids, 'anrs' => $anrUuids,
'type' => StatsDataObject::TYPE_RISK 'type' => StatsDataObject::TYPE_RISK,
'get_last' => 0,
], $defaultDates), $queryParams); ], $defaultDates), $queryParams);
$this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_VULNERABILITY]); $this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_VULNERABILITY]);
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams); parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
$this->assertEquals(array_merge(['type' => StatsDataObject::TYPE_VULNERABILITY], $defaultDates), $queryParams); $this->assertEquals(
array_merge(['type' => StatsDataObject::TYPE_VULNERABILITY], $defaultDates + ['get_last' => 0]),
$queryParams
);
} }
public function testItCanSendDifferentParamsToGetTheStats() public function testItCanSendDifferentParamsToGetTheStats()
{ {
$user = $this->createMock(User::class); $user = $this->createMock(User::class);
$user->expects($this->exactly(2))->method('hasRole')->with(UserRole::USER_ROLE_CEO)->willReturn(true); $user->expects($this->exactly(3))->method('hasRole')->with(UserRole::USER_ROLE_CEO)->willReturn(true);
$user->expects($this->never())->method('getUserAnrs'); $user->expects($this->never())->method('getUserAnrs');
$this->connectedUserService->expects($this->exactly(2))->method('getConnectedUser')->willReturn($user); $this->connectedUserService->expects($this->exactly(3))->method('getConnectedUser')->willReturn($user);
/** @var AnrTable $anrTable */ /** @var AnrTable $anrTable */
$anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class); $anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class);
@ -335,6 +326,7 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
'dateTo' => (new DateTime())->format('Y-m-d'), 'dateTo' => (new DateTime())->format('Y-m-d'),
]; ];
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse())); $this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse())); $this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
@ -348,7 +340,8 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
$this->assertEquals(array_merge([ $this->assertEquals(array_merge([
'anrs' => $anrUuids, 'anrs' => $anrUuids,
'aggregation_period' => 'month', 'aggregation_period' => 'month',
'type' => StatsDataObject::TYPE_COMPLIANCE 'type' => StatsDataObject::TYPE_COMPLIANCE,
'get_last' => 0,
], [ ], [
'date_from' => $datesRange['dateFrom'], 'date_from' => $datesRange['dateFrom'],
'date_to' => $datesRange['dateTo'], 'date_to' => $datesRange['dateTo'],
@ -362,11 +355,23 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams); parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
$this->assertEquals(array_merge([ $this->assertEquals(array_merge([
'aggregation_period' => 'week', 'aggregation_period' => 'week',
'get_last' => 0,
'type' => StatsDataObject::TYPE_VULNERABILITY 'type' => StatsDataObject::TYPE_VULNERABILITY
], [ ], [
'date_from' => $datesRange['dateFrom'], 'date_from' => $datesRange['dateFrom'],
'date_to' => $datesRange['dateTo'], 'date_to' => $datesRange['dateTo'],
]), $queryParams); ]), $queryParams);
$this->statsAnrService->getStats(array_merge([
'getLast' => true,
'type' => StatsDataObject::TYPE_CARTOGRAPHY,
], $datesRange));
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
$this->assertEquals(array_merge([
'get_last' => 1,
'type' => StatsDataObject::TYPE_CARTOGRAPHY
]), $queryParams);
} }
public function testItFetchesStatsForDefaultPeriodIfFromAndToDatesAreNotPassed() public function testItFetchesStatsForDefaultPeriodIfFromAndToDatesAreNotPassed()
@ -392,6 +397,7 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
'date_from' => $defaultDates['dateFrom'], 'date_from' => $defaultDates['dateFrom'],
'date_to' => $defaultDates['dateTo'], 'date_to' => $defaultDates['dateTo'],
'type' => StatsDataObject::TYPE_THREAT, 'type' => StatsDataObject::TYPE_THREAT,
'get_last' => 0,
], $queryParams); ], $queryParams);
$this->statsAnrService->getStats([ $this->statsAnrService->getStats([
@ -404,6 +410,7 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
'date_from' => (new DateTime())->modify('-6 months')->format('Y-m-d'), 'date_from' => (new DateTime())->modify('-6 months')->format('Y-m-d'),
'date_to' => $defaultDates['dateTo'], 'date_to' => $defaultDates['dateTo'],
'type' => StatsDataObject::TYPE_THREAT, 'type' => StatsDataObject::TYPE_THREAT,
'get_last' => 0,
], $queryParams); ], $queryParams);
$dateTo = (new DateTimeImmutable())->modify('-6 months'); $dateTo = (new DateTimeImmutable())->modify('-6 months');
@ -413,7 +420,8 @@ class StatsAnrServiceTest extends AbstractIntegrationTestCase
$this->assertEquals([ $this->assertEquals([
'type' => StatsDataObject::TYPE_THREAT, 'type' => StatsDataObject::TYPE_THREAT,
'date_from' => $dateTo->modify('-' . StatsAnrService::DEFAULT_STATS_DATES_RANGE)->format('Y-m-d'), 'date_from' => $dateTo->modify('-' . StatsAnrService::DEFAULT_STATS_DATES_RANGE)->format('Y-m-d'),
'date_to' => $dateTo->format('Y-m-d') 'date_to' => $dateTo->format('Y-m-d'),
'get_last' => 0,
], $queryParams); ], $queryParams);
} }

View File

@ -78,7 +78,8 @@ class GetStatsQueryParamsValidatorTest extends AbstractUnitTestCase
'dateTo' => '2020-06-01', 'dateTo' => '2020-06-01',
'anrs' => [], 'anrs' => [],
'type' => StatsDataObject::TYPE_RISK, 'type' => StatsDataObject::TYPE_RISK,
'aggregationPeriod' => null 'aggregationPeriod' => null,
'getLast' => false,
], ],
$this->getStatsQueryParamsValidator->getValidData() $this->getStatsQueryParamsValidator->getValidData()
); );
@ -96,7 +97,8 @@ class GetStatsQueryParamsValidatorTest extends AbstractUnitTestCase
'dateTo' => '2019-12-01', 'dateTo' => '2019-12-01',
'anrs' => [], 'anrs' => [],
'type' => StatsDataObject::TYPE_RISK, 'type' => StatsDataObject::TYPE_RISK,
'aggregationPeriod' => null 'aggregationPeriod' => null,
'getLast' => false,
], ],
$this->getStatsQueryParamsValidator->getValidData() $this->getStatsQueryParamsValidator->getValidData()
); );
@ -169,4 +171,54 @@ class GetStatsQueryParamsValidatorTest extends AbstractUnitTestCase
'aggregationType' => 'week', 'aggregationType' => 'week',
])); ]));
} }
public function testGetLastIsSetToFalseWhenNotPassed()
{
static::assertTrue($this->getStatsQueryParamsValidator->isValid([
'type' => StatsDataObject::TYPE_RISK,
]));
static::assertEquals(
[
'type' => StatsDataObject::TYPE_RISK,
'getLast' => false,
'anrs' => [],
'dateFrom' => null,
'dateTo' => null,
'aggregationPeriod' => null,
],
$this->getStatsQueryParamsValidator->getValidData()
);
static::assertTrue($this->getStatsQueryParamsValidator->isValid([
'type' => StatsDataObject::TYPE_RISK,
'getLast' => true,
]));
static::assertEquals(
[
'type' => StatsDataObject::TYPE_RISK,
'getLast' => true,
'anrs' => [],
'dateFrom' => null,
'dateTo' => null,
'aggregationPeriod' => null,
],
$this->getStatsQueryParamsValidator->getValidData()
);
static::assertTrue($this->getStatsQueryParamsValidator->isValid([
'type' => StatsDataObject::TYPE_RISK,
'getLast' => 'the value is converted to boolean.',
]));
static::assertEquals(
[
'type' => StatsDataObject::TYPE_RISK,
'getLast' => true,
'anrs' => [],
'dateFrom' => null,
'dateTo' => null,
'aggregationPeriod' => null,
],
$this->getStatsQueryParamsValidator->getValidData()
);
}
} }