. */ declare(strict_types=1); namespace SP\Tests\Modules\Web\Controllers\ConfigEncryption; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\MockObject\Exception; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Config\Ports\ConfigFileService; use SP\Domain\Core\Ports\AppLockHandler; use SP\Domain\User\Dtos\UserDto; use SP\Tests\InjectConfigParam; use SP\Tests\InjectVault; use SP\Tests\IntegrationTestCase; /** * Class RefreshControllerTest */ #[Group('integration')] #[InjectVault] class ConfigEncryptionTest extends IntegrationTestCase { /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ #[Test] public function refresh() { $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'configEncryption/refresh']) ); $this->expectOutputString('{"status":"OK","description":"Master password hash updated","data":null}'); IntegrationTestCase::runApp($container); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ #[Test] #[InjectConfigParam(['masterPwd' => '$2y$10$6imglyA01feP1AnEmlAgUeQPa7vysHeKQLz0MDA1Zf7iG6ep.7PaC'])] #[TestWith([false], 'Change accounts encryption')] #[TestWith([true], 'Change only hash')] public function save(bool $noAccountChange) { $data = [ 'current_masterpass' => 'a_password', 'new_masterpass' => 'a_new_password', 'new_masterpass_repeat' => 'a_new_password', 'confirm_masterpass_change' => 'true', 'no_account_change' => $noAccountChange, 'isAjax' => 1 ]; $configData = $this->getConfigData(); $configData['isMaintenance'] = true; $appLockHandler = self::createStub(AppLockHandler::class); $appLockHandler ->method('getLock') ->willReturn(100); $configData = self::createConfiguredStub(ConfigDataInterface::class, $configData); $configFileService = self::createStub(ConfigFileService::class); $configFileService ->method('getConfigData') ->willReturn($configData); $definitions = [ ConfigFileService::class => $configFileService, AppLockHandler::class => $appLockHandler, ]; $container = $this->buildContainer( IntegrationTestCase::buildRequest( 'post', 'index.php', ['r' => 'configEncryption/save'], $data ), $definitions ); if ($noAccountChange) { $this->expectOutputString( '{"status":"OK","description":"Master password updated","data":["No accounts updated, only hash","Please, restart the session to update it"]}' ); } else { $this->expectOutputString( '{"status":"OK","description":"Master password updated","data":"Please, restart the session to update it"}' ); } IntegrationTestCase::runApp($container); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ #[Test] public function saveTemp() { $data = [ 'temporary_masterpass_maxtime' => self::$faker->randomNumber(4), 'temporary_masterpass_group' => self::$faker->randomNumber(3), 'temporary_masterpass_email' => self::$faker->email() ]; $container = $this->buildContainer( IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'configEncryption/saveTemp']), $data ); $this->expectOutputString('{"status":"OK","description":"Temporary password generated","data":null}'); IntegrationTestCase::runApp($container); } protected function getUserDataDto(): UserDto { return parent::getUserDataDto()->mutate(['id' => 100]); } }