2020-07-02 15:24:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MonarcAppFo\Tests\Integration\Service;
|
|
|
|
|
|
|
|
use DateTime;
|
2020-07-03 15:00:32 +02:00
|
|
|
use DateTimeImmutable;
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2020-07-02 15:24:51 +02:00
|
|
|
use GuzzleHttp\Handler\MockHandler;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Laminas\ServiceManager\ServiceManager;
|
2020-07-13 14:37:04 +02:00
|
|
|
use LogicException;
|
2020-07-03 15:00:32 +02:00
|
|
|
use Monarc\Core\Service\ConnectedUserService;
|
|
|
|
use Monarc\FrontOffice\Model\Entity\Anr;
|
|
|
|
use Monarc\FrontOffice\Model\Entity\UserAnr;
|
|
|
|
use Monarc\FrontOffice\Model\Entity\UserRole;
|
|
|
|
use Monarc\FrontOffice\Exception\AccessForbiddenException;
|
|
|
|
use Monarc\FrontOffice\Exception\UserNotAuthorizedException;
|
|
|
|
use Monarc\FrontOffice\Model\Entity\User;
|
2020-07-02 15:24:51 +02:00
|
|
|
use Monarc\FrontOffice\Model\Table\AnrTable;
|
2020-11-24 10:34:26 +01:00
|
|
|
use Monarc\FrontOffice\Model\Table\InstanceRiskOpTable;
|
|
|
|
use Monarc\FrontOffice\Model\Table\InstanceRiskTable;
|
|
|
|
use Monarc\FrontOffice\Model\Table\ReferentialTable;
|
|
|
|
use Monarc\FrontOffice\Model\Table\ScaleTable;
|
2020-07-02 15:24:51 +02:00
|
|
|
use Monarc\FrontOffice\Model\Table\SettingTable;
|
2020-11-24 10:34:26 +01:00
|
|
|
use Monarc\FrontOffice\Model\Table\SnapshotTable;
|
|
|
|
use Monarc\FrontOffice\Model\Table\SoaTable;
|
|
|
|
use Monarc\FrontOffice\Model\Table\UserTable;
|
2020-07-02 15:24:51 +02:00
|
|
|
use Monarc\FrontOffice\Stats\DataObject\StatsDataObject;
|
|
|
|
use Monarc\FrontOffice\Stats\Exception\StatsAlreadyCollectedException;
|
|
|
|
use Monarc\FrontOffice\Stats\Provider\StatsApiProvider;
|
|
|
|
use Monarc\FrontOffice\Stats\Service\StatsAnrService;
|
|
|
|
use MonarcAppFo\Tests\Integration\AbstractIntegrationTestCase;
|
2020-07-03 15:00:32 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-07-02 15:24:51 +02:00
|
|
|
|
|
|
|
class StatsAnrServiceTest extends AbstractIntegrationTestCase
|
|
|
|
{
|
|
|
|
/** @var MockHandler */
|
2020-07-03 15:00:32 +02:00
|
|
|
private $statsApiMockHandler;
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-14 11:23:52 +02:00
|
|
|
/** @var string */
|
|
|
|
private $currentDate;
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
/** @var StatsAnrService */
|
|
|
|
private $statsAnrService;
|
|
|
|
|
|
|
|
/** @var ConnectedUserService|MockObject */
|
|
|
|
private $connectedUserService;
|
|
|
|
|
2020-07-02 15:24:51 +02:00
|
|
|
public static function setUpBeforeClass(): void
|
|
|
|
{
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
|
|
|
static::createMyPrintTestData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-07-14 11:23:52 +02:00
|
|
|
$this->currentDate = (new DateTime())->format('Y-m-d');
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class);
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function configureServiceManager(ServiceManager $serviceManager)
|
|
|
|
{
|
|
|
|
$serviceManager->setAllowOverride(true);
|
|
|
|
|
2020-11-24 10:34:26 +01:00
|
|
|
$serviceLocator = $this->getApplicationServiceLocator();
|
|
|
|
$config = ['statsApi' => ['baseUrl' => 'http://url.com', 'apiKey' => 'token12345']];
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler = new MockHandler();
|
2020-11-24 10:34:26 +01:00
|
|
|
$statsApiProvider = new StatsApiProvider($config, $this->statsApiMockHandler);
|
2020-07-02 15:24:51 +02:00
|
|
|
$serviceManager->setService(StatsApiProvider::class, $statsApiProvider);
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->connectedUserService = $this->createMock(ConnectedUserService::class);
|
|
|
|
$serviceManager->setService(ConnectedUserService::class, $this->connectedUserService);
|
|
|
|
|
2020-11-24 10:34:26 +01:00
|
|
|
$statsAnrService = new StatsAnrService(
|
|
|
|
$serviceLocator->get(AnrTable::class),
|
|
|
|
$serviceLocator->get(ScaleTable::class),
|
|
|
|
$serviceLocator->get(InstanceRiskTable::class),
|
|
|
|
$serviceLocator->get(InstanceRiskOpTable::class),
|
|
|
|
$serviceLocator->get(ReferentialTable::class),
|
|
|
|
$serviceLocator->get(SoaTable::class),
|
|
|
|
$serviceLocator->get(StatsApiProvider::class),
|
|
|
|
$this->connectedUserService,
|
|
|
|
$serviceLocator->get(UserTable::class),
|
|
|
|
$serviceLocator->get(SnapshotTable::class),
|
|
|
|
$serviceLocator->get(SettingTable::class),
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
$serviceManager->setService(StatsAnrService::class, $statsAnrService);
|
|
|
|
|
2020-07-02 15:24:51 +02:00
|
|
|
$serviceManager->setAllowOverride(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItThrowsTheErrorWhenTheTheStatsAlreadyGeneratedForToday()
|
|
|
|
{
|
|
|
|
$this->expectException(StatsAlreadyCollectedException::class);
|
|
|
|
$this->expectExceptionMessage('The stats is already collected for today.');
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse([
|
|
|
|
new StatsDataObject([
|
2020-07-02 15:24:51 +02:00
|
|
|
'type' => StatsDataObject::TYPE_RISK,
|
2020-07-03 15:00:32 +02:00
|
|
|
'anr' => '1232-31abcd-213efgh-123klmp',
|
2020-07-02 15:24:51 +02:00
|
|
|
'data' => [
|
2020-07-03 15:00:32 +02:00
|
|
|
'total' => [
|
2020-07-02 15:24:51 +02:00
|
|
|
[
|
|
|
|
'label' => 'Low risks',
|
|
|
|
'value' => 50,
|
|
|
|
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'label' => 'Medium risks',
|
|
|
|
'value' => 30,
|
|
|
|
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'label' => 'High risks',
|
|
|
|
'value' => 10,
|
|
|
|
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2020-07-03 15:00:32 +02:00
|
|
|
])
|
2020-07-02 15:24:51 +02:00
|
|
|
])));
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsAnrService->collectStats();
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItDoesNotSendTheStatsWhenTheDataIsEmpty()
|
|
|
|
{
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsAnrService->collectStats([99, 78]);
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->assertEquals('GET', $this->statsApiMockHandler->getLastRequest()->getMethod());
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCanGenerateTheStatsForAllTheAnrs()
|
|
|
|
{
|
|
|
|
/** @var AnrTable $anrTable */
|
|
|
|
$anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class);
|
|
|
|
$anrs = $anrTable->findAll();
|
|
|
|
$anrUuids = [];
|
|
|
|
foreach ($anrs as $anr) {
|
|
|
|
$anrUuids[] = $anr->getUuid();
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
|
|
|
$this->statsApiMockHandler->append(new Response(201, [], '{"status": "ok"}'));
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsAnrService->collectStats();
|
2020-07-02 15:24:51 +02:00
|
|
|
|
|
|
|
$this->assertJsonStringEqualsJsonString(
|
|
|
|
$this->getExpectedStatsDataJson($anrUuids),
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler->getLastRequest()->getBody()->getContents()
|
2020-07-02 15:24:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItGenerateTheStatsOnlyForPassedAnrs()
|
|
|
|
{
|
|
|
|
$anrIdsToGenerateTheStats = [1, 2, 3];
|
|
|
|
|
|
|
|
/** @var AnrTable $anrTable */
|
|
|
|
$anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class);
|
|
|
|
$anrs = $anrTable->findByIds($anrIdsToGenerateTheStats);
|
|
|
|
$anrUuids = [];
|
|
|
|
foreach ($anrs as $num => $anr) {
|
|
|
|
$anrUuids[] = $anr->getUuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(\count($anrIdsToGenerateTheStats), $anrUuids);
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
|
|
|
$this->statsApiMockHandler->append(new Response(201, [], '{"status": "ok"}'));
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsAnrService->collectStats($anrIdsToGenerateTheStats);
|
2020-07-02 15:24:51 +02:00
|
|
|
|
|
|
|
$this->assertJsonStringEqualsJsonString(
|
|
|
|
$this->getExpectedStatsDataJson($anrUuids),
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler->getLastRequest()->getBody()->getContents()
|
2020-07-02 15:24:51 +02:00
|
|
|
);
|
|
|
|
}
|
2020-07-03 15:00:32 +02:00
|
|
|
|
2020-07-02 15:24:51 +02:00
|
|
|
public function testItThrowsTheExceptionIfUserIsNotLoggedIn()
|
|
|
|
{
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->expectException(UserNotAuthorizedException::class);
|
|
|
|
$this->expectExceptionMessage('User not authorized.');
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsAnrService->getStats([]);
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItThrowsTheExceptionIfUserDoesNotHaveTheRightsToGetTheStats()
|
|
|
|
{
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->expectException(AccessForbiddenException::class);
|
|
|
|
$this->expectExceptionMessage('User does not have an access to the action.');
|
|
|
|
|
|
|
|
$user = $this->createMock(User::class);
|
|
|
|
$user->expects($this->once())->method('hasRole')->with(UserRole::USER_ROLE_CEO)->willReturn(false);
|
|
|
|
$user->method('getUserAnrs')->willReturn(new ArrayCollection());
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-13 14:37:04 +02:00
|
|
|
$this->connectedUserService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getConnectedUser')
|
|
|
|
->willReturn($user);
|
|
|
|
|
2020-11-24 10:34:26 +01:00
|
|
|
$this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_CARTOGRAPHY, 'anrs' => []]);
|
2020-07-13 14:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItThrowsLogicExceptionIfTypeIsNotPassed()
|
|
|
|
{
|
|
|
|
$this->expectException(LogicException::class);
|
|
|
|
$this->expectExceptionMessage('Filter parameter \'type\' is mandatory to get the stats.');
|
|
|
|
|
|
|
|
$user = $this->createMock(User::class);
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->connectedUserService
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getConnectedUser')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$this->statsAnrService->getStats([]);
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItAddsToTheFilterAllowedForTheUserAnrUuids()
|
|
|
|
{
|
2020-07-03 15:00:32 +02:00
|
|
|
$user = $this->createMock(User::class);
|
|
|
|
$user->expects($this->exactly(2))->method('hasRole')->with(UserRole::USER_ROLE_CEO)->willReturn(false);
|
|
|
|
/** @var Anr $anr1 */
|
|
|
|
$anr1 = (new Anr())->setId(1);
|
|
|
|
/** @var Anr $anr2 */
|
|
|
|
$anr2 = (new Anr())->setId(2);
|
|
|
|
/** @var Anr $anr3 */
|
|
|
|
$anr3 = (new Anr())->setId(3);
|
|
|
|
$user->method('getUserAnrs')->willReturn(new ArrayCollection([
|
|
|
|
(new UserAnr())->setAnr($anr1->generateAndSetUuid()),
|
|
|
|
(new UserAnr())->setAnr($anr2->generateAndSetUuid()),
|
|
|
|
(new UserAnr())->setAnr($anr3->generateAndSetUuid()),
|
|
|
|
]));
|
|
|
|
$this->connectedUserService->expects($this->exactly(2))->method('getConnectedUser')->willReturn($user);
|
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
|
|
|
|
2020-07-13 14:37:04 +02:00
|
|
|
$stats = $this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_COMPLIANCE]);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
|
|
|
$this->assertEmpty($stats);
|
|
|
|
|
|
|
|
$defaultDates = [
|
2020-07-13 14:37:04 +02:00
|
|
|
'date_from' => (new DateTime())->modify('-' . StatsAnrService::DEFAULT_STATS_DATES_RANGE)->format('Y-m-d'),
|
|
|
|
'date_to' => (new DateTime())->format('Y-m-d'),
|
2020-07-03 15:00:32 +02:00
|
|
|
];
|
|
|
|
$queryParams = [];
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
|
|
|
$this->assertEquals(
|
2020-07-13 14:37:04 +02:00
|
|
|
array_merge([
|
|
|
|
'anrs' => [$anr1->getUuid(), $anr2->getUuid(), $anr3->getUuid()],
|
|
|
|
'type' => StatsDataObject::TYPE_COMPLIANCE,
|
2020-08-05 17:00:38 +02:00
|
|
|
'get_last' => 0,
|
2020-07-13 14:37:04 +02:00
|
|
|
], $defaultDates),
|
2020-07-03 15:00:32 +02:00
|
|
|
$queryParams
|
|
|
|
);
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$statsResponse = $this->getStatsResponse([
|
|
|
|
new StatsDataObject([
|
|
|
|
'type' => StatsDataObject::TYPE_RISK,
|
|
|
|
'anr' => '1232-31abcd-213efgh-123klmp',
|
|
|
|
'data' => [
|
2020-08-05 17:00:38 +02:00
|
|
|
'risks' => [],
|
|
|
|
'total' => [],
|
2020-07-03 15:00:32 +02:00
|
|
|
],
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $statsResponse));
|
|
|
|
|
2020-07-13 14:37:04 +02:00
|
|
|
$stats = $this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_RISK, 'anrs' => [1, 3, 7]]);
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-08-05 17:00:38 +02:00
|
|
|
$this->assertEquals($this->getStatsResponse($stats), $this->getStatsResponse());
|
2020-07-03 15:00:32 +02:00
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
|
|
|
$this->assertEquals(
|
2020-07-13 14:37:04 +02:00
|
|
|
array_merge([
|
|
|
|
'anrs' => [$anr1->getUuid(), $anr3->getUuid()],
|
|
|
|
'type' => StatsDataObject::TYPE_RISK,
|
2020-08-05 17:00:38 +02:00
|
|
|
'get_last' => 0,
|
2020-07-13 14:37:04 +02:00
|
|
|
], $defaultDates),
|
2020-07-03 15:00:32 +02:00
|
|
|
$queryParams
|
|
|
|
);
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
public function testItAllowsToFilterBySpecificAnrsForCeoRoleOrGetItWithoutLimitations()
|
2020-07-02 15:24:51 +02:00
|
|
|
{
|
2020-07-03 15:00:32 +02:00
|
|
|
$user = $this->createMock(User::class);
|
|
|
|
$user->expects($this->exactly(2))->method('hasRole')->with(UserRole::USER_ROLE_CEO)->willReturn(true);
|
|
|
|
$user->expects($this->never())->method('getUserAnrs');
|
|
|
|
$this->connectedUserService->expects($this->exactly(2))->method('getConnectedUser')->willReturn($user);
|
|
|
|
|
|
|
|
/** @var AnrTable $anrTable */
|
|
|
|
$anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class);
|
|
|
|
$anrUuids = [];
|
|
|
|
foreach ($anrTable->findByIds([1, 2, 3]) as $anr) {
|
|
|
|
$anrUuids[] = $anr->getUuid();
|
|
|
|
}
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
$defaultDates = [
|
2020-07-13 14:37:04 +02:00
|
|
|
'date_from' => (new DateTime())->modify('-' . StatsAnrService::DEFAULT_STATS_DATES_RANGE)->format('Y-m-d'),
|
|
|
|
'date_to' => (new DateTime())->format('Y-m-d'),
|
2020-07-03 15:00:32 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
|
|
|
|
|
|
|
$this->statsAnrService->getStats([
|
2020-07-13 14:37:04 +02:00
|
|
|
'type' => StatsDataObject::TYPE_RISK,
|
2020-07-03 15:00:32 +02:00
|
|
|
'anrs' => [1, 2, 3, 99] // anr ID = 99 is not in thew db.
|
|
|
|
]);
|
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
2020-07-13 14:37:04 +02:00
|
|
|
$this->assertEquals(array_merge([
|
|
|
|
'anrs' => $anrUuids,
|
2020-08-05 17:00:38 +02:00
|
|
|
'type' => StatsDataObject::TYPE_RISK,
|
|
|
|
'get_last' => 0,
|
2020-07-13 14:37:04 +02:00
|
|
|
], $defaultDates), $queryParams);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
2020-07-13 14:37:04 +02:00
|
|
|
$this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_VULNERABILITY]);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
2020-08-05 17:00:38 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array_merge(['type' => StatsDataObject::TYPE_VULNERABILITY], $defaultDates + ['get_last' => 0]),
|
|
|
|
$queryParams
|
|
|
|
);
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
public function testItCanSendDifferentParamsToGetTheStats()
|
2020-07-02 15:24:51 +02:00
|
|
|
{
|
2020-07-03 15:00:32 +02:00
|
|
|
$user = $this->createMock(User::class);
|
2020-08-05 17:00:38 +02:00
|
|
|
$user->expects($this->exactly(3))->method('hasRole')->with(UserRole::USER_ROLE_CEO)->willReturn(true);
|
2020-07-03 15:00:32 +02:00
|
|
|
$user->expects($this->never())->method('getUserAnrs');
|
2020-08-05 17:00:38 +02:00
|
|
|
$this->connectedUserService->expects($this->exactly(3))->method('getConnectedUser')->willReturn($user);
|
2020-07-02 15:24:51 +02:00
|
|
|
|
2020-07-03 15:00:32 +02:00
|
|
|
/** @var AnrTable $anrTable */
|
|
|
|
$anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class);
|
|
|
|
$anrUuids = [];
|
|
|
|
foreach ($anrTable->findByIds([1, 2, 3, 4]) as $anr) {
|
|
|
|
$anrUuids[] = $anr->getUuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
$datesRange = [
|
|
|
|
'dateFrom' => (new DateTime())->modify('-1 year')->format('Y-m-d'),
|
|
|
|
'dateTo' => (new DateTime())->format('Y-m-d'),
|
|
|
|
];
|
|
|
|
|
2020-08-05 17:00:38 +02:00
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
2020-07-03 15:00:32 +02:00
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
|
|
|
$this->statsApiMockHandler->append(new Response(200, [], $this->getStatsResponse()));
|
|
|
|
|
|
|
|
$this->statsAnrService->getStats(array_merge([
|
|
|
|
'anrs' => [1, 2, 3, 4],
|
|
|
|
'aggregationPeriod' => 'month',
|
|
|
|
'type' => StatsDataObject::TYPE_COMPLIANCE
|
|
|
|
], $datesRange));
|
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
|
|
|
$this->assertEquals(array_merge([
|
|
|
|
'anrs' => $anrUuids,
|
2020-07-13 14:37:04 +02:00
|
|
|
'aggregation_period' => 'month',
|
2020-08-05 17:00:38 +02:00
|
|
|
'type' => StatsDataObject::TYPE_COMPLIANCE,
|
|
|
|
'get_last' => 0,
|
2020-07-13 14:37:04 +02:00
|
|
|
], [
|
|
|
|
'date_from' => $datesRange['dateFrom'],
|
|
|
|
'date_to' => $datesRange['dateTo'],
|
|
|
|
]), $queryParams);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
|
|
|
$this->statsAnrService->getStats(array_merge([
|
|
|
|
'aggregationPeriod' => 'week',
|
|
|
|
'type' => StatsDataObject::TYPE_VULNERABILITY
|
|
|
|
], $datesRange));
|
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
|
|
|
$this->assertEquals(array_merge([
|
2020-07-13 14:37:04 +02:00
|
|
|
'aggregation_period' => 'week',
|
2020-08-05 17:00:38 +02:00
|
|
|
'get_last' => 0,
|
2020-07-03 15:00:32 +02:00
|
|
|
'type' => StatsDataObject::TYPE_VULNERABILITY
|
2020-07-13 14:37:04 +02:00
|
|
|
], [
|
|
|
|
'date_from' => $datesRange['dateFrom'],
|
|
|
|
'date_to' => $datesRange['dateTo'],
|
|
|
|
]), $queryParams);
|
2020-08-05 17:00:38 +02:00
|
|
|
|
|
|
|
$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);
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItFetchesStatsForDefaultPeriodIfFromAndToDatesAreNotPassed()
|
|
|
|
{
|
2020-07-03 15:00:32 +02:00
|
|
|
$user = $this->createMock(User::class);
|
|
|
|
$user->expects($this->exactly(3))->method('hasRole')->with(UserRole::USER_ROLE_CEO)->willReturn(true);
|
|
|
|
$user->expects($this->never())->method('getUserAnrs');
|
|
|
|
$this->connectedUserService->expects($this->exactly(3))->method('getConnectedUser')->willReturn($user);
|
|
|
|
|
|
|
|
$defaultDates = [
|
|
|
|
'dateFrom' => (new DateTime())->modify('-' . StatsAnrService::DEFAULT_STATS_DATES_RANGE)->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()));
|
|
|
|
|
2020-07-13 14:37:04 +02:00
|
|
|
$this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_THREAT]);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
2020-07-13 14:37:04 +02:00
|
|
|
$this->assertEquals([
|
|
|
|
'date_from' => $defaultDates['dateFrom'],
|
|
|
|
'date_to' => $defaultDates['dateTo'],
|
|
|
|
'type' => StatsDataObject::TYPE_THREAT,
|
2020-08-05 17:00:38 +02:00
|
|
|
'get_last' => 0,
|
2020-07-13 14:37:04 +02:00
|
|
|
], $queryParams);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
2020-07-13 14:37:04 +02:00
|
|
|
$this->statsAnrService->getStats([
|
|
|
|
'type' => StatsDataObject::TYPE_THREAT,
|
|
|
|
'dateFrom' => (new DateTime())->modify('-6 months')->format('Y-m-d')
|
|
|
|
]);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
|
|
|
$this->assertEquals([
|
2020-07-13 14:37:04 +02:00
|
|
|
'date_from' => (new DateTime())->modify('-6 months')->format('Y-m-d'),
|
|
|
|
'date_to' => $defaultDates['dateTo'],
|
|
|
|
'type' => StatsDataObject::TYPE_THREAT,
|
2020-08-05 17:00:38 +02:00
|
|
|
'get_last' => 0,
|
2020-07-03 15:00:32 +02:00
|
|
|
], $queryParams);
|
|
|
|
|
|
|
|
$dateTo = (new DateTimeImmutable())->modify('-6 months');
|
2020-07-13 14:37:04 +02:00
|
|
|
$this->statsAnrService->getStats(['type' => StatsDataObject::TYPE_THREAT, 'dateTo' => $dateTo->format('Y-m-d')]);
|
2020-07-03 15:00:32 +02:00
|
|
|
|
|
|
|
parse_str($this->statsApiMockHandler->getLastRequest()->getUri()->getQuery(), $queryParams);
|
|
|
|
$this->assertEquals([
|
2020-07-13 14:37:04 +02:00
|
|
|
'type' => StatsDataObject::TYPE_THREAT,
|
|
|
|
'date_from' => $dateTo->modify('-' . StatsAnrService::DEFAULT_STATS_DATES_RANGE)->format('Y-m-d'),
|
2020-08-05 17:00:38 +02:00
|
|
|
'date_to' => $dateTo->format('Y-m-d'),
|
|
|
|
'get_last' => 0,
|
2020-07-03 15:00:32 +02:00
|
|
|
], $queryParams);
|
2020-07-02 15:24:51 +02:00
|
|
|
}
|
2020-07-03 15:00:32 +02:00
|
|
|
|
2020-07-02 15:24:51 +02:00
|
|
|
private function getStatsResponse(array $results = []): string
|
|
|
|
{
|
|
|
|
return json_encode([
|
|
|
|
'metadata' => [
|
2020-07-13 14:37:04 +02:00
|
|
|
'count' => \count($results),
|
|
|
|
'offset' => 0,
|
|
|
|
'limit' => 0,
|
2020-07-02 15:24:51 +02:00
|
|
|
],
|
|
|
|
'data' => $results,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getExpectedStatsDataJson(array $anrUuids): string
|
|
|
|
{
|
|
|
|
$statsData = json_decode(
|
|
|
|
file_get_contents($this->testPath . '/data/expected_stats_data_for_my_print.json'),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedStats = [];
|
|
|
|
foreach ($anrUuids as $num => $anrUuid) {
|
|
|
|
foreach ($statsData as $data) {
|
|
|
|
$data['anr'] = $anrUuid;
|
2020-07-14 11:23:52 +02:00
|
|
|
$data['date'] = $this->currentDate;
|
2020-07-02 15:24:51 +02:00
|
|
|
$expectedStats[] = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_encode($expectedStats);
|
|
|
|
}
|
|
|
|
}
|