Skip to content

Commit

Permalink
telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed May 16, 2024
1 parent 998cdf8 commit c95cd5b
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/phpunit/src/Commands/TelemetryCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Commands;

use Acquia\Cli\Command\App\LinkCommand;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Self\TelemetryCommand;
use Acquia\Cli\Helpers\DataStoreContract;
use Acquia\Cli\Tests\CommandTestBase;
use Symfony\Component\Filesystem\Path;

/**
* @property \Acquia\Cli\Command\Self\TelemetryCommand $command
*/
class TelemetryCommandTest extends CommandTestBase {

protected string $legacyAcliConfigFilepath;

public function setUp(): void {
parent::setUp();
$this->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);
}

}
29 changes: 29 additions & 0 deletions tests/phpunit/src/Commands/TelemetryDisableCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Commands;

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Self\TelemetryDisableCommand;
use Acquia\Cli\Tests\CommandTestBase;

/**
* @property \Acquia\Cli\Command\Self\TelemetryDisableCommand $command
*/
class TelemetryDisableCommandTest extends CommandTestBase {

protected function createCommand(): CommandBase {
return $this->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']);
}

}
31 changes: 31 additions & 0 deletions tests/phpunit/src/Commands/TelemetryEnableCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Commands;

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Self\TelemetryEnableCommand;
use Acquia\Cli\Tests\CommandTestBase;

/**
* @property \Acquia\Cli\Command\Self\TelemetryEnableCommand $command
*/
class TelemetryEnableCommandTest extends CommandTestBase {

/**b
*/
protected function createCommand(): CommandBase {
return $this->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']);
}

}

0 comments on commit c95cd5b

Please sign in to comment.