From c95cd5b73624a40fe8843cdbeb4f236b60ffa0d7 Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Thu, 16 May 2024 16:45:05 -0700 Subject: [PATCH] telemetry --- .../src/Commands/TelemetryCommandTest.php | 104 ++++++++++++++++++ .../Commands/TelemetryDisableCommandTest.php | 29 +++++ .../Commands/TelemetryEnableCommandTest.php | 31 ++++++ 3 files changed, 164 insertions(+) create mode 100644 tests/phpunit/src/Commands/TelemetryCommandTest.php create mode 100644 tests/phpunit/src/Commands/TelemetryDisableCommandTest.php create mode 100644 tests/phpunit/src/Commands/TelemetryEnableCommandTest.php diff --git a/tests/phpunit/src/Commands/TelemetryCommandTest.php b/tests/phpunit/src/Commands/TelemetryCommandTest.php new file mode 100644 index 000000000..b7ac1897a --- /dev/null +++ b/tests/phpunit/src/Commands/TelemetryCommandTest.php @@ -0,0 +1,104 @@ +legacyAcliConfigFilepath = Path::join($this->dataDir, 'acquia-cli.json'); + $this->fs->remove($this->legacyAcliConfigFilepath); + } + + /**b + */ + protected function createCommand(): CommandBase { + return $this->injectCommand(TelemetryCommand::class); + } + + /** + * @group brokenProphecy + */ + public function testTelemetryCommand(): void { + $this->mockRequest('getAccount'); + $this->executeCommand(); + $output = $this->getDisplay(); + $this->assertStringContainsString('Telemetry has been enabled.', $output); + $this->executeCommand(); + $output = $this->getDisplay(); + $this->assertStringContainsString('Telemetry has been disabled.', $output); + } + + /** + * @return string[][] + */ + public function providerTestTelemetryPrompt(): array { + return [ + [ + // Would you like to share anonymous performance usage and data? + ['n'], + 'Ok, no data will be collected and shared with us.', + ], + ]; + } + + /** + * Tests telemetry prompt. + * + * @dataProvider providerTestTelemetryPrompt + * @param $message + */ + public function testTelemetryPrompt(array $inputs, mixed $message): void { + $this->cloudConfig = [DataStoreContract::SEND_TELEMETRY => NULL]; + $this->createMockConfigFiles(); + $this->createMockAcliConfigFile('a47ac10b-58cc-4372-a567-0e02b2c3d470'); + $this->createDataStores(); + $this->mockApplicationRequest(); + $this->command = $this->injectCommand(LinkCommand::class); + $this->executeCommand([], $inputs); + $output = $this->getDisplay(); + + $this->assertStringContainsString('Would you like to share anonymous performance usage and data?', $output); + $this->assertStringContainsString($message, $output); + } + + /** + * Opted out by default. + */ + public function testAmplitudeDisabled(): void { + $this->cloudConfig = [DataStoreContract::SEND_TELEMETRY => FALSE]; + $this->createMockConfigFiles(); + $this->executeCommand(); + + $this->assertEquals(0, $this->getStatusCode()); + + } + + public function testMigrateLegacyTelemetryPreference(): void { + $this->cloudConfig = [DataStoreContract::SEND_TELEMETRY => NULL]; + $this->createMockConfigFiles(); + $this->fs->remove($this->legacyAcliConfigFilepath); + $legacyAcliConfig = ['send_telemetry' => FALSE]; + $contents = json_encode($legacyAcliConfig); + $this->fs->dumpFile($this->legacyAcliConfigFilepath, $contents); + $this->executeCommand(); + + $this->assertEquals(0, $this->getStatusCode()); + $this->fs->remove($this->legacyAcliConfigFilepath); + } + +} diff --git a/tests/phpunit/src/Commands/TelemetryDisableCommandTest.php b/tests/phpunit/src/Commands/TelemetryDisableCommandTest.php new file mode 100644 index 000000000..25647d72d --- /dev/null +++ b/tests/phpunit/src/Commands/TelemetryDisableCommandTest.php @@ -0,0 +1,29 @@ +injectCommand(TelemetryDisableCommand::class); + } + + public function testTelemetryDisableCommand(): void { + $this->executeCommand(); + $output = $this->getDisplay(); + $this->assertStringContainsString('Telemetry has been disabled.', $output); + + $settings = json_decode(file_get_contents($this->cloudConfigFilepath), TRUE); + $this->assertFalse($settings['send_telemetry']); + } + +} diff --git a/tests/phpunit/src/Commands/TelemetryEnableCommandTest.php b/tests/phpunit/src/Commands/TelemetryEnableCommandTest.php new file mode 100644 index 000000000..5cfc79953 --- /dev/null +++ b/tests/phpunit/src/Commands/TelemetryEnableCommandTest.php @@ -0,0 +1,31 @@ +injectCommand(TelemetryEnableCommand::class); + } + + public function testTelemetryEnableCommand(): void { + $this->executeCommand(); + $output = $this->getDisplay(); + $this->assertStringContainsString('Telemetry has been enabled.', $output); + + $settings = json_decode(file_get_contents($this->cloudConfigFilepath), TRUE); + $this->assertTrue($settings['send_telemetry']); + } + +}