From 154e71de74792cb539dcbe24de0d492ecb8007b2 Mon Sep 17 00:00:00 2001 From: akemidx Date: Tue, 7 Nov 2023 17:43:51 -0500 Subject: [PATCH 1/6] shifting wrap around logic back into 0-265 --- app/Helpers/Helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index c2ccda6c84ee..98f40de5b89e 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -349,7 +349,7 @@ public static function defaultChartColors($index = 0) $total_colors = count($colors); if ($index >= $total_colors) { - $index = $index - $total_colors; + $index = $index - $total_colors - 1; } return $colors[$index]; From 495521b70ebb5e4fbdcc3905a059fadcdfd08e20 Mon Sep 17 00:00:00 2001 From: akemidx Date: Wed, 8 Nov 2023 12:51:51 -0500 Subject: [PATCH 2/6] adjusting for less than 0 possibility --- app/Helpers/Helper.php | 4 ++++ tests/Feature/DefaultColorKeyTest.php | 21 +++++++++++++++++++++ tests/Feature/Reports/CustomReportTest.php | 1 + 3 files changed, 26 insertions(+) create mode 100644 tests/Feature/DefaultColorKeyTest.php diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 98f40de5b89e..cd790ff40fa7 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -350,6 +350,10 @@ public static function defaultChartColors($index = 0) if ($index >= $total_colors) { $index = $index - $total_colors - 1; + + if($index < 0) { + $index = 0; + } } return $colors[$index]; diff --git a/tests/Feature/DefaultColorKeyTest.php b/tests/Feature/DefaultColorKeyTest.php new file mode 100644 index 000000000000..fdabd0db145b --- /dev/null +++ b/tests/Feature/DefaultColorKeyTest.php @@ -0,0 +1,21 @@ +hasAssets(1)->count(255)->create(); + + $this->defaultChartColors($index); + + $this->assertArrayHasKey('index', ($index)[0]); + + + } +} diff --git a/tests/Feature/Reports/CustomReportTest.php b/tests/Feature/Reports/CustomReportTest.php index a1a269a4ab3d..dd3199212e93 100644 --- a/tests/Feature/Reports/CustomReportTest.php +++ b/tests/Feature/Reports/CustomReportTest.php @@ -11,6 +11,7 @@ use Tests\Support\InteractsWithSettings; use Tests\TestCase; + class CustomReportTest extends TestCase { use InteractsWithSettings; From 9cc89911f7213b34ba78c1e7bb4dc6107fe86dff Mon Sep 17 00:00:00 2001 From: akemidx Date: Wed, 8 Nov 2023 13:29:32 -0500 Subject: [PATCH 3/6] possibilitieeeeeeeeessss --- app/Helpers/Helper.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index cd790ff40fa7..0fcee3157501 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -354,6 +354,9 @@ public static function defaultChartColors($index = 0) if($index < 0) { $index = 0; } + elseif($index > 255) { + $index = 255; + } } return $colors[$index]; From 2051ac785d92dfcf2e7f7246771d102e5ee4fb8f Mon Sep 17 00:00:00 2001 From: akemidx Date: Wed, 8 Nov 2023 13:44:46 -0500 Subject: [PATCH 4/6] adding in error logging and code comments --- app/Helpers/Helper.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 0fcee3157501..3c5a0e9f113c 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -349,13 +349,18 @@ public static function defaultChartColors($index = 0) $total_colors = count($colors); if ($index >= $total_colors) { + + \Log::error('Status label count is '.$index.' and exceeds the allowed count of 256.'); + //patch fix for array key overflow (color count starts at 1, array starts at 0) $index = $index - $total_colors - 1; + //constraints to keep result in 0-265 range. This should never be needed, but if something happens + //to create this many status labels and it DOES happen, this will keep it from failing at least. if($index < 0) { $index = 0; } - elseif($index > 255) { - $index = 255; + elseif($index > 265) { + $index = 265; } } From 4382adce85352516b09f957020ae5a7e3781cb62 Mon Sep 17 00:00:00 2001 From: akemidx Date: Wed, 8 Nov 2023 15:29:29 -0500 Subject: [PATCH 5/6] typo in error log message --- app/Helpers/Helper.php | 2 +- tests/Feature/DefaultColorKeyTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 3c5a0e9f113c..603945763782 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -350,7 +350,7 @@ public static function defaultChartColors($index = 0) if ($index >= $total_colors) { - \Log::error('Status label count is '.$index.' and exceeds the allowed count of 256.'); + \Log::error('Status label count is '.$index.' and exceeds the allowed count of 266.'); //patch fix for array key overflow (color count starts at 1, array starts at 0) $index = $index - $total_colors - 1; diff --git a/tests/Feature/DefaultColorKeyTest.php b/tests/Feature/DefaultColorKeyTest.php index fdabd0db145b..3fab03b7f6a5 100644 --- a/tests/Feature/DefaultColorKeyTest.php +++ b/tests/Feature/DefaultColorKeyTest.php @@ -2,8 +2,8 @@ namespace Tests\Feature; -use Tests\TestCase; use App\Models\Statuslabel; +use Tests\TestCase; use App\Models\Asset; class DefaultColorKeyTest extends TestCase From 49136a4d67c9d0434ae51c3820b294f24590a864 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 8 Nov 2023 12:35:42 -0800 Subject: [PATCH 6/6] Add tests for color helper --- app/Helpers/Helper.php | 12 ++++++++---- tests/Feature/DefaultColorKeyTest.php | 21 --------------------- tests/Unit/Helpers/HelperTest.php | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 25 deletions(-) delete mode 100644 tests/Feature/DefaultColorKeyTest.php create mode 100644 tests/Unit/Helpers/HelperTest.php diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 603945763782..800a2491d406 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -73,10 +73,14 @@ public static function formatCurrencyOutput($cost) * * @author [A. Gianotto] [] * @since [v3.3] - * @return array + * @return string */ - public static function defaultChartColors($index = 0) + public static function defaultChartColors(int $index = 0) { + if ($index < 0) { + $index = 0; + } + $colors = [ '#008941', '#FF4A46', @@ -359,8 +363,8 @@ public static function defaultChartColors($index = 0) if($index < 0) { $index = 0; } - elseif($index > 265) { - $index = 265; + elseif($index >($total_colors - 1)) { + $index = $total_colors - 1; } } diff --git a/tests/Feature/DefaultColorKeyTest.php b/tests/Feature/DefaultColorKeyTest.php deleted file mode 100644 index 3fab03b7f6a5..000000000000 --- a/tests/Feature/DefaultColorKeyTest.php +++ /dev/null @@ -1,21 +0,0 @@ -hasAssets(1)->count(255)->create(); - - $this->defaultChartColors($index); - - $this->assertArrayHasKey('index', ($index)[0]); - - - } -} diff --git a/tests/Unit/Helpers/HelperTest.php b/tests/Unit/Helpers/HelperTest.php new file mode 100644 index 000000000000..0b5fba986c04 --- /dev/null +++ b/tests/Unit/Helpers/HelperTest.php @@ -0,0 +1,19 @@ +assertIsString(Helper::defaultChartColors(1000)); + } + + public function testDefaultChartColorsMethodHandlesNegativeNumbers() + { + $this->assertIsString(Helper::defaultChartColors(-1)); + } +}