. */ namespace SP\Tests\Modules\Cli\Commands; use DI\DependencyException; use DI\NotFoundException; use SP\Core\Exceptions\FileNotFoundException; use SP\Modules\Cli\Commands\Crypt\UpdateMasterPasswordCommand; use SP\Tests\DatabaseTrait; use SP\Tests\Modules\Cli\CliTestCase; use SP\Tests\Services\Account\AccountCryptServiceTest; use function SP\Tests\recreateDir; /** * */ class UpdateMasterPasswordCommandTest extends CliTestCase { use DatabaseTrait; /** * @var string */ protected static string $currentConfig; /** * @var string[] */ protected static array $commandInputData = [ '--currentMasterPassword' => AccountCryptServiceTest::CURRENT_MASTERPASS, '--masterPassword' => AccountCryptServiceTest::NEW_MASTERPASS ]; /** * @throws DependencyException * @throws NotFoundException */ public function testUpdateAborted(): void { $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Master password update aborted', $output); } /** * @throws DependencyException * @throws FileNotFoundException * @throws NotFoundException */ public function testUpdateIsSuccessful(): void { $inputData = array_merge( self::$commandInputData, [ '--update' => null ] ); $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class, $inputData ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Master password updated', $output); // Recreate cache directory to avoid unwanted behavior recreateDir(CACHE_PATH); } /** * @throws DependencyException * @throws NotFoundException */ public function testUpdateFromEnvironmentVarIsAbort(): void { $this->setEnvironmentVariables(); $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class, null, false ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Master password update aborted', $output); } private function setEnvironmentVariables(): void { putenv(sprintf('%s=%s', UpdateMasterPasswordCommand::$envVarsMapping['currentMasterPassword'], AccountCryptServiceTest::CURRENT_MASTERPASS) ); putenv(sprintf('%s=%s', UpdateMasterPasswordCommand::$envVarsMapping['masterPassword'], AccountCryptServiceTest::NEW_MASTERPASS) ); } /** * @throws DependencyException * @throws NotFoundException */ public function testUpdateFromEnvironmentVarBlankCurrentMasterPassword(): void { putenv(sprintf('%s=', UpdateMasterPasswordCommand::$envVarsMapping['masterPassword']) ); $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class, null, false ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Master password cannot be blank', $output); } /** * @throws DependencyException * @throws NotFoundException */ public function testUpdateFromEnvironmentVarBlankMasterPassword(): void { putenv(sprintf('%s=', UpdateMasterPasswordCommand::$envVarsMapping['currentMasterPassword']) ); $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class, null, false ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Master password cannot be blank', $output); } /** * @throws DependencyException * @throws NotFoundException */ public function testUpdateFromEnvironmentVarIsSuccessful(): void { putenv(sprintf('%s=true', UpdateMasterPasswordCommand::$envVarsMapping['update']) ); $this->setEnvironmentVariables(); $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class, null, false ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Master password updated', $output); } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ public function testSameMasterPassword(): void { $inputData = [ '--currentMasterPassword' => AccountCryptServiceTest::CURRENT_MASTERPASS, '--masterPassword' => AccountCryptServiceTest::CURRENT_MASTERPASS, '--update' => null ]; $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class, $inputData ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Passwords are the same', $output); } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ public function testWrongMasterPassword(): void { $inputData = [ '--currentMasterPassword' => uniqid('', true), '--masterPassword' => AccountCryptServiceTest::NEW_MASTERPASS, '--update' => null ]; $commandTester = $this->executeCommandTest( UpdateMasterPasswordCommand::class, $inputData ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('The current master password does not match', $output); } protected function setUp(): void { $this->unsetEnvironmentVariables(); $this->setupDatabase(); self::loadFixtures(); parent::setUp(); } private function unsetEnvironmentVariables(): void { foreach (UpdateMasterPasswordCommand::$envVarsMapping as $envVar) { putenv($envVar); } } }