diff --git a/src/Helpers/TelemetryHelper.php b/src/Helpers/TelemetryHelper.php index 7fd8b44ae..141479c3d 100644 --- a/src/Helpers/TelemetryHelper.php +++ b/src/Helpers/TelemetryHelper.php @@ -107,6 +107,31 @@ public function initializeAmplitude(): void { } } + /** + * @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'; + } + 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 $ah_env; + } + /** * Get telemetry user data. * @@ -115,7 +140,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(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..0c3058a84 100644 --- a/tests/phpunit/src/Misc/TelemetryHelperTest.php +++ b/tests/phpunit/src/Misc/TelemetryHelperTest.php @@ -68,4 +68,33 @@ public function testGetEnvironmentProviderWithoutAnyEnvSet(): void { $this->assertNull(TelemetryHelper::getEnvironmentProvider()); } + /** + * @return mixed[] + * The data provider. + */ + public function providerTestAhEnvNormalization(): array { + return [ + ['prod', 'prod'], + ['01live', 'prod'], + ['stage', 'stage'], + ['stg', 'stage'], + ['dev1', 'dev'], + ['ode1', 'ode'], + ['ide', 'ide'], + ['fake', 'fake'], + ]; + } + + /** + * @dataProvider providerTestAhEnvNormalization + * @param string $ah_env + * The Acquia hosting environment. + * @param string $expected + * The expected normalized environment. + */ + public function testAhEnvNormalization(string $ah_env, string $expected): void { + $normalized_ah_env = TelemetryHelper::normalizeAhEnv($ah_env); + $this->assertEquals($expected, $normalized_ah_env); + } + }