From 88d7906888f473ba2bf29864c6c3381be2a8d783 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Fri, 24 May 2024 11:35:32 -0400 Subject: [PATCH 1/4] Normalize ah_env telemetry. --- src/Helpers/TelemetryHelper.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Helpers/TelemetryHelper.php b/src/Helpers/TelemetryHelper.php index 7fd8b44ae..8fed9c0f6 100644 --- a/src/Helpers/TelemetryHelper.php +++ b/src/Helpers/TelemetryHelper.php @@ -107,6 +107,29 @@ public function initializeAmplitude(): void { } } + private function normalizeAhEnv(): string { + $ah_env = AcquiaDrupalEnvironmentDetector::getAhEnv(); + if (AcquiaDrupalEnvironmentDetector::isAcsfEnv()) { + return 'acsf'; + } + if (AcquiaDrupalEnvironmentDetector::isAhProdEnv($ah_env)) { + return 'prod'; + } + if (AcquiaDrupalEnvironmentDetector::isAhStageEnv($ah_env)) { + return 'stage'; + } + if (AcquiaDrupalEnvironmentDetector::isAhDevEnv($ah_env)) { + return 'dev'; + } + if (AcquiaDrupalEnvironmentDetector::isAhOdeEnv($ah_env)) { + return 'ode'; + } + if (AcquiaDrupalEnvironmentDetector::isAhIdeEnv($ah_env)) { + return 'ide'; + } + return AcquiaDrupalEnvironmentDetector::getAhEnv(); + } + /** * Get telemetry user data. * @@ -115,7 +138,7 @@ public function initializeAmplitude(): void { private function getTelemetryUserData(): array { $data = [ 'ah_app_uuid' => getenv('AH_APPLICATION_UUID'), - 'ah_env' => AcquiaDrupalEnvironmentDetector::getAhEnv(), + 'ah_env' => $this->normalizeAhEnv(), 'ah_group' => AcquiaDrupalEnvironmentDetector::getAhGroup(), 'ah_non_production' => getenv('AH_NON_PRODUCTION'), 'ah_realm' => getenv('AH_REALM'), From 584eb3d73a42aa3c5e5d60b5f63d35d13ffaa37a Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Fri, 24 May 2024 11:50:38 -0400 Subject: [PATCH 2/4] Add test. --- src/Helpers/TelemetryHelper.php | 10 +++------- .../phpunit/src/Misc/TelemetryHelperTest.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Helpers/TelemetryHelper.php b/src/Helpers/TelemetryHelper.php index 8fed9c0f6..8512f86b3 100644 --- a/src/Helpers/TelemetryHelper.php +++ b/src/Helpers/TelemetryHelper.php @@ -107,11 +107,7 @@ public function initializeAmplitude(): void { } } - private function normalizeAhEnv(): string { - $ah_env = AcquiaDrupalEnvironmentDetector::getAhEnv(); - if (AcquiaDrupalEnvironmentDetector::isAcsfEnv()) { - return 'acsf'; - } + public static function normalizeAhEnv($ah_env): string { if (AcquiaDrupalEnvironmentDetector::isAhProdEnv($ah_env)) { return 'prod'; } @@ -127,7 +123,7 @@ private function normalizeAhEnv(): string { if (AcquiaDrupalEnvironmentDetector::isAhIdeEnv($ah_env)) { return 'ide'; } - return AcquiaDrupalEnvironmentDetector::getAhEnv(); + return $ah_env; } /** @@ -138,7 +134,7 @@ private function normalizeAhEnv(): string { private function getTelemetryUserData(): array { $data = [ 'ah_app_uuid' => getenv('AH_APPLICATION_UUID'), - 'ah_env' => $this->normalizeAhEnv(), + 'ah_env' => $this->normalizeAhEnv(AcquiaDrupalEnvironmentDetector::getAhEnv()), 'ah_group' => AcquiaDrupalEnvironmentDetector::getAhGroup(), 'ah_non_production' => getenv('AH_NON_PRODUCTION'), 'ah_realm' => getenv('AH_REALM'), diff --git a/tests/phpunit/src/Misc/TelemetryHelperTest.php b/tests/phpunit/src/Misc/TelemetryHelperTest.php index c4baa34fe..14c473fba 100644 --- a/tests/phpunit/src/Misc/TelemetryHelperTest.php +++ b/tests/phpunit/src/Misc/TelemetryHelperTest.php @@ -67,5 +67,24 @@ public function testGetEnvironmentProviderWithoutAnyEnvSet(): void { // Expect null since no provider environment variables are set. $this->assertNull(TelemetryHelper::getEnvironmentProvider()); } + public function providerTestAhEnvNormalization(): array { + return [ + ['prod', 'prod'], + ['01live', 'prod'], + ['stage', 'stage'], + ['stg', 'stage'], + ['dev1', 'dev'], + ['ode1', 'ode'], + ['ide', 'ide'], + ]; + } + + /** + * @dataProvider providerTestAhEnvNormalization + */ + public function testAhEnvNormalization($ah_env, $expected) { + $normalized_ah_env = TelemetryHelper::normalizeAhEnv($ah_env); + $this->assertEquals($expected, $normalized_ah_env); + } } From a914a026007e901ac9f37fb9d905b345b10bc5de Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Fri, 24 May 2024 11:55:56 -0400 Subject: [PATCH 3/4] PHPCS. --- src/Helpers/TelemetryHelper.php | 8 +++++++- tests/phpunit/src/Misc/TelemetryHelperTest.php | 11 ++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Helpers/TelemetryHelper.php b/src/Helpers/TelemetryHelper.php index 8512f86b3..141479c3d 100644 --- a/src/Helpers/TelemetryHelper.php +++ b/src/Helpers/TelemetryHelper.php @@ -107,7 +107,13 @@ public function initializeAmplitude(): void { } } - public static function normalizeAhEnv($ah_env): string { + /** + * @param string $ah_env + * Environment name from AH_ENV. + * @return string + * Normalized environment name. + */ + public static function normalizeAhEnv(string $ah_env): string { if (AcquiaDrupalEnvironmentDetector::isAhProdEnv($ah_env)) { return 'prod'; } diff --git a/tests/phpunit/src/Misc/TelemetryHelperTest.php b/tests/phpunit/src/Misc/TelemetryHelperTest.php index 14c473fba..611a9ca1a 100644 --- a/tests/phpunit/src/Misc/TelemetryHelperTest.php +++ b/tests/phpunit/src/Misc/TelemetryHelperTest.php @@ -67,6 +67,11 @@ public function testGetEnvironmentProviderWithoutAnyEnvSet(): void { // Expect null since no provider environment variables are set. $this->assertNull(TelemetryHelper::getEnvironmentProvider()); } + + /** + * @return mixed[] + * The data provider. + */ public function providerTestAhEnvNormalization(): array { return [ ['prod', 'prod'], @@ -81,8 +86,12 @@ public function providerTestAhEnvNormalization(): array { /** * @dataProvider providerTestAhEnvNormalization + * @param string $ah_env + * The Acquia hosting environment. + * @param string $expected + * The expected normalized environment. */ - public function testAhEnvNormalization($ah_env, $expected) { + public function testAhEnvNormalization(string $ah_env, string $expected): void { $normalized_ah_env = TelemetryHelper::normalizeAhEnv($ah_env); $this->assertEquals($expected, $normalized_ah_env); } From d5b33a8319778ec661855c7a11257265f201da1c Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Fri, 24 May 2024 11:59:05 -0400 Subject: [PATCH 4/4] Mutants --- tests/phpunit/src/Misc/TelemetryHelperTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/src/Misc/TelemetryHelperTest.php b/tests/phpunit/src/Misc/TelemetryHelperTest.php index 611a9ca1a..0c3058a84 100644 --- a/tests/phpunit/src/Misc/TelemetryHelperTest.php +++ b/tests/phpunit/src/Misc/TelemetryHelperTest.php @@ -81,6 +81,7 @@ public function providerTestAhEnvNormalization(): array { ['dev1', 'dev'], ['ode1', 'ode'], ['ide', 'ide'], + ['fake', 'fake'], ]; }