From 2299b9c8e6023aea7caa7286f8917c3462957113 Mon Sep 17 00:00:00 2001 From: Jonathan Foucher <jfoucher@gmail.com> Date: Thu, 8 Sep 2022 10:34:43 +0200 Subject: [PATCH] Stop checking dates to prevent failing tests --- .../Controllers/Api/GraphDataController.php | 2 - app/Http/Controllers/PdfController.php | 13 ++-- app/Repository/GraphDataRepository.php | 65 ++++++++++--------- routes/web.php | 7 +- tests/Feature/EvaluationsControllerTest.php | 4 +- tests/Feature/GraphDataControllerTest.php | 5 +- 6 files changed, 48 insertions(+), 48 deletions(-) diff --git a/app/Http/Controllers/Api/GraphDataController.php b/app/Http/Controllers/Api/GraphDataController.php index 88a3cfa6..bccc8b40 100644 --- a/app/Http/Controllers/Api/GraphDataController.php +++ b/app/Http/Controllers/Api/GraphDataController.php @@ -35,8 +35,6 @@ public function measures(int $id): Collection|array|\Illuminate\Support\Collecti return $this->repository->getMeasuresDataForEvaluation($evaluation); } - - /** * @param int $id Evaluation id */ diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php index cd195912..59d74d8c 100644 --- a/app/Http/Controllers/PdfController.php +++ b/app/Http/Controllers/PdfController.php @@ -68,12 +68,10 @@ public function ActionPlanPdf() unlink(storage_path('app/action_plan/tmp.pdf')); } - - // foreach measure, get all levels that should be added $measureDocs = collect($actionSorted)->map(function (MeasureLevel $ml) { $files = []; - for ($i = $ml->actual_level +1; $i <= $ml->expected_level; $i++) { + for ($i = $ml->actual_level + 1; $i <= $ml->expected_level; ++$i) { if (null !== $ml->measure->{'level' . $i . '_file'}) { $files[] = str_replace('/storage/', storage_path('app/public/'), $ml->measure->{'level' . $i . '_file'}); } @@ -187,11 +185,10 @@ public function DossierCyberSecuritePdf(GraphDataRepository $repository, $organi $dangerLevels = DangerLevel::orderBy('value', 'asc')->get(); $attackData = $repository->getAttackDataForEvaluation($evaluation); - $attackColors = $attackData['labels']->map(function($l, $i) use ($attackData) { - - $color = GraphDataRepository::hslToRgb($i/count($attackData['labels']), 1, 0.7); - //return $color; - return '#' . sprintf("%x%x%x", $color[0], $color[1], $color[2]); + $attackColors = $attackData['labels']->map(function ($l, $i) use ($attackData) { + $color = GraphDataRepository::hslToRgb($i / count($attackData['labels']), 1, 0.7); + // return $color; + return '#' . sprintf('%x%x%x', $color[0], $color[1], $color[2]); }); $data = [ diff --git a/app/Repository/GraphDataRepository.php b/app/Repository/GraphDataRepository.php index 2e3e5c96..13847ab4 100644 --- a/app/Repository/GraphDataRepository.php +++ b/app/Repository/GraphDataRepository.php @@ -159,7 +159,6 @@ public function getFutureRisksDataForEvaluation(Evaluation $evaluation): array ]; } - public function getAttackDataForEvaluation(Evaluation $evaluation): array { $scenarios = Scenario::with('measures')->get(); @@ -198,19 +197,17 @@ public function getMaturityDataForEvaluation(Evaluation $evaluation): array ]; } - public function getBestMeasuresForEvaluation(Evaluation $evaluation): array { $measureLevels = collect($evaluation->measureLevels); // For each danger, add all points of all measures that apply to the danger scenarios - $scenarios = Scenario::all(); $measures = Measure::all(); - Log::info('Calcul des meilleures mesures pour évaluation ' . $evaluation->id); + Log::info('____ Calcul des meilleures mesures pour l\'évaluation ' . $evaluation->id . ' ____'); - $bestMeasures = array_map(function($i) {return 0;}, array_flip($measures->pluck('id')->toArray())); + $bestMeasures = array_map(function ($i) {return 0; }, array_flip($measures->pluck('id')->toArray())); /** * @var DangerLevelEvaluation $dangerLevel */ @@ -229,18 +226,15 @@ public function getBestMeasuresForEvaluation(Evaluation $evaluation): array // Pour chaque scenarios nous regardons les mesures qui l'affectent foreach ($scenario->measures as $measure) { - Log::info('- Mesure ' . $measure->short_name. ' -'); - if ($measure->fundamental) { - Log::info('Mesure fondamentale'); - } + Log::info('- Mesure ' . $measure->short_name . ($measure->fundamental ? '(fondamentale) -' : ' -')); /** - * @var MeasureLevel $selectedLevel + * @var MeasureLevel|null $selectedLevel */ // Nous regardons quel niveau a été choisit à l'étape 2 pour cette mesure. $selectedLevel = $measureLevels->first(function (MeasureLevel $level) use ($measure) { return $measure->id === $level->measure_id; }); - Log::info('Niveau de la mesure : ' . $selectedLevel->actual_level); + Log::info('Niveau actuel de la mesure : ' . $selectedLevel->actual_level); if (!$selectedLevel) { // Si aucun niveau n'a été choisi pour cette mesure, nous lui mettons le niveau 0 @@ -256,15 +250,15 @@ public function getBestMeasuresForEvaluation(Evaluation $evaluation): array Log::info('Valeur du niveau actuel : ' . $measure->{'level' . $selectedLevel->actual_level . '_value'}); // La valeur du niveau juste au dessus du niveau actuel choisit pour cette mesure (avec un maximum de niveau 3) // (les mesures fondamentales sont déja multipliées par deux en base de données) - $l2 = $measure->{'level' . (min($selectedLevel->actual_level + 1, 3)) . '_value'}; - Log::info('Valeur du niveau actuel + 1 : ' . $measure->{'level' . (min($selectedLevel->actual_level + 1, 3)) . '_value'}); + $l2 = $measure->{'level' . min($selectedLevel->actual_level + 1, 3) . '_value'}; + Log::info('Valeur du niveau actuel + 1 : ' . $measure->{'level' . min($selectedLevel->actual_level + 1, 3) . '_value'}); // La différence entre la valeur du niveau N + 1 et du niveau N (progression potentielle) // est pondérée par la perception du danger // puis ajouté au score pour cette mesure $bestMeasures[$measure->id] += ($l2 - $l1) * $dangerLevel->level->value; - Log::info('Valeur actuelle de cette mesure : ' . $bestMeasures[$measure->id]); + Log::info('Valeur courante de la mesure ' . $measure->id . ' (' . $measure->short_name . ') : ' . $bestMeasures[$measure->id]); } } } @@ -296,7 +290,7 @@ private function getExpositionPointsForScenario(Scenario $scenario, $measureLeve if (!$selectedLevel) { return $acc; } - //fundamental measures count double + // fundamental measures count double return $acc + $measure->{'level' . $selectedLevel->actual_level . '_value'}; }, 0); @@ -319,7 +313,7 @@ private function getFutureExpositionPointsForScenario(Scenario $scenario, $measu return $measure->id === $level->measure_id; }); - //fundamental measures count double + // fundamental measures count double $val = $measure->{'level' . $selectedLevel->expected_level . '_value'}; if (!$val) { $val = $measure->{'level' . $selectedLevel->actual_level . '_value'}; @@ -368,7 +362,7 @@ public function getActionTerritoryGraphData(): array // Get the number of planned actions in the current evaluation return $acc + $currentEval->measureLevels->filter(function (MeasureLevel $ml) { - return $ml->expected_level; + return null !== $ml->expected_level && 0 !== $ml->expected_level; })->count(); } @@ -501,28 +495,41 @@ public function getOrganizationData(int|null $territory_id = null) ]; } - public static function hue2rgb($p, $q, $t){ - if($t < 0) $t += 1; - if($t > 1) $t -= 1; - if($t < 1/6) return $p + ($q - $p) * 6 * $t; - if($t < 1/2) return $q; - if($t < 2/3) return $p + ($q - $p) * (2/3 - $t) * 6; + public static function hue2rgb($p, $q, $t) + { + if ($t < 0) { + ++$t; + } + if ($t > 1) { + --$t; + } + if ($t < 1 / 6) { + return $p + ($q - $p) * 6 * $t; + } + if ($t < 1 / 2) { + return $q; + } + if ($t < 2 / 3) { + return $p + ($q - $p) * (2 / 3 - $t) * 6; + } + return $p; } - public static function hslToRgb($h, $s, $l){ - if($s == 0){ + public static function hslToRgb($h, $s, $l) + { + if (0 == $s) { $r = $l; $g = $l; $b = $l; // achromatic - }else{ + } else { $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; $p = 2 * $l - $q; - $r = self::hue2rgb($p, $q, $h + 1/3); + $r = self::hue2rgb($p, $q, $h + 1 / 3); $g = self::hue2rgb($p, $q, $h); - $b = self::hue2rgb($p, $q, $h - 1/3); + $b = self::hue2rgb($p, $q, $h - 1 / 3); } - return array(round($r * 255), round($g * 255), round($b * 255)); + return [round($r * 255), round($g * 255), round($b * 255)]; } } diff --git a/routes/web.php b/routes/web.php index 1e4d1eb4..e2c83086 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,11 +1,12 @@ <?php +use App\Http\Controllers\Auth\LoginController; +use App\Http\Controllers\Auth\LogoutController; use App\Http\Controllers\PdfController; use App\Http\Controllers\UsersController; -use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Auth; -use App\Http\Controllers\Auth\LoginController; -use App\Http\Controllers\Auth\LogoutController; +use Illuminate\Support\Facades\Route; + /* /* |-------------------------------------------------------------------------- diff --git a/tests/Feature/EvaluationsControllerTest.php b/tests/Feature/EvaluationsControllerTest.php index 4f6d3942..542287aa 100644 --- a/tests/Feature/EvaluationsControllerTest.php +++ b/tests/Feature/EvaluationsControllerTest.php @@ -234,8 +234,8 @@ public function testPostNonDraftEvaluationWithDataShouldMoveToStep2() 'updated_by' => $user->firstname . ' ' . $user->lastname, 'organization_id' => $user->organization_id, 'organization' => $user->organization->toArray(), - 'created_at' => Carbon::now()->setMicroseconds(0)->format('Y-m-d\TH:i:s.u\Z'), - 'updated_at' => Carbon::now()->setMicroseconds(0)->format('Y-m-d\TH:i:s.u\Z'), + 'created_at' => $data['created_at'], + 'updated_at' => $data['updated_at'], 'danger_levels' => $dangersLevelsEval->toArray(), 'measure_levels' => [], 'maturity_levels' => [], diff --git a/tests/Feature/GraphDataControllerTest.php b/tests/Feature/GraphDataControllerTest.php index b279e25d..abf30a1c 100644 --- a/tests/Feature/GraphDataControllerTest.php +++ b/tests/Feature/GraphDataControllerTest.php @@ -263,8 +263,6 @@ public function testCheckEvaluationMaturityGraphData() $this->assertEquals($expected, $data); } - - public function testEvaluationBestMeasures() { /** @@ -279,14 +277,13 @@ public function testEvaluationBestMeasures() // Set all measure levels to known quantity foreach ($evaluation->measureLevels as $k => $measureLevel) { - $measureLevel->actual_level = $k%4; + $measureLevel->actual_level = $k % 4; $measureLevel->save(); } // Set all dangers levels to known quantity $dangerLevel = DangerLevel::where('value', 25)->first(); foreach ($evaluation->dangerLevels as $k => $dangerLevelEval) { - $dangerLevelEval->danger_level_id = $dangerLevel->id; $dangerLevelEval->save(); } -- GitLab