Skip to content

Commit

Permalink
Normalize ah_env telemetry. (#1742)
Browse files Browse the repository at this point in the history
* Normalize ah_env telemetry.

* Add test.

* PHPCS.

* Mutants
  • Loading branch information
grasmash authored May 24, 2024
1 parent 12e1dc5 commit ddaf5d7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Helpers/TelemetryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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'),
Expand Down
29 changes: 29 additions & 0 deletions tests/phpunit/src/Misc/TelemetryHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit ddaf5d7

Please sign in to comment.