diff --git a/src/Helpers/TelemetryHelper.php b/src/Helpers/TelemetryHelper.php index ca921fa2b..7fd8b44ae 100644 --- a/src/Helpers/TelemetryHelper.php +++ b/src/Helpers/TelemetryHelper.php @@ -120,6 +120,7 @@ private function getTelemetryUserData(): array { 'ah_non_production' => getenv('AH_NON_PRODUCTION'), 'ah_realm' => getenv('AH_REALM'), 'CI' => getenv('CI'), + 'env_provider' => $this->getEnvironmentProvider(), 'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, ]; try { @@ -134,6 +135,20 @@ private function getTelemetryUserData(): array { return $data; } + public static function getEnvironmentProvider(): ?string { + $providers = self::getProviders(); + + // Check for environment variables. + foreach ($providers as $provider => $vars) { + foreach ($vars as $var) { + if (getenv($var) !== FALSE) + return $provider; + } + } + + return NULL; + } + private function getUserId(): ?string { $user = $this->getUserData(); if ($user && isset($user['uuid'])) { @@ -180,4 +195,40 @@ private function getDefaultUserData(): array { ]; } + /** + * @infection-ignore-all + * Skipping infection testing for this because, it most cases, we expect that when a row from this array is changed + * it won't affect the return value. + * @return array + * An array of providers and their associated environment variables. + */ + public static function getProviders(): array { + // Define the environment variables associated with each provider. + // phpcs:ignore SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder + return [ + 'lando' => ['LANDO'], + 'ddev' => ['IS_DDEV_PROJECT'], + // Check Lando and DDEV first because the hijack AH_SITE_ENVIRONMENT. + 'acquia' => ['AH_SITE_ENVIRONMENT'], + 'bamboo' => ['BAMBOO_BUILDNUMBER'], + 'beanstalk' => ['BEANSTALK_ENVIRONMENT'], + 'bitbucket' => ['BITBUCKET_BUILD_NUMBER'], + 'bitrise' => ['BITRISE_IO'], + 'buddy' => ['BUDDY_WORKSPACE_ID'], + 'circleci' => ['CIRCLECI'], + 'codebuild' => ['CODEBUILD_BUILD_ID'], + 'docksal' => ['DOCKSAL_VERSION'], + 'drone' => ['DRONE'], + 'github' => ['GITHUB_ACTIONS'], + 'gitlab' => ['GITLAB_CI'], + 'heroku' => ['HEROKU_TEST_RUN_ID'], + 'jenkins' => ['JENKINS_URL'], + 'pantheon' => ['PANTHEON_ENVIRONMENT'], + 'pipelines' => ['PIPELINE_ENV'], + 'platformsh' => ['PLATFORM_ENVIRONMENT'], + 'teamcity' => ['TEAMCITY_VERSION'], + 'travis' => ['TRAVIS'], + ]; + } + } diff --git a/tests/phpunit/src/Misc/TelemetryHelperTest.php b/tests/phpunit/src/Misc/TelemetryHelperTest.php new file mode 100644 index 000000000..c4baa34fe --- /dev/null +++ b/tests/phpunit/src/Misc/TelemetryHelperTest.php @@ -0,0 +1,71 @@ +providerTestEnvironmentProvider() as $args) { + $envVars = array_merge($envVars, $args[1]); + } + + TestBase::unsetEnvVars($envVars); + } + + public function unsetGitHubEnvVars(): void { + $providers = TelemetryHelper::getProviders(); + + // Since we actually run our own tests on GitHub, getEnvironmentProvider() will return 'github' unless we unset it. + $github_env_vars = []; + foreach ($providers['github'] as $var) { + $github_env_vars[$var] = self::ENV_VAR_DEFAULT_VALUE; + } + TestBase::unsetEnvVars($github_env_vars); + } + + /** + * @return array + */ + public function providerTestEnvironmentProvider(): array { + $providersList = TelemetryHelper::getProviders(); + $providersArray = []; + foreach ($providersList as $provider => $envVars) { + $env_vars_with_values = []; + foreach ($envVars as $var_name) { + $env_vars_with_values[$var_name] = self::ENV_VAR_DEFAULT_VALUE; + } + $providersArray[] = [$provider, $env_vars_with_values]; + } + + return $providersArray; + } + + /** + * @dataProvider providerTestEnvironmentProvider() + */ + public function testEnvironmentProvider(string $provider, array $envVars): void { + $this->unsetGitHubEnvVars(); + TestBase::setEnvVars($envVars); + $this->assertEquals($provider, TelemetryHelper::getEnvironmentProvider()); + } + + /** + * Test the getEnvironmentProvider method when no environment provider is detected. + */ + public function testGetEnvironmentProviderWithoutAnyEnvSet(): void { + $this->unsetGitHubEnvVars(); + + // Expect null since no provider environment variables are set. + $this->assertNull(TelemetryHelper::getEnvironmentProvider()); + } + +}