From 7b24cded97d9dbcb83fbeedd6ee588aba1c9e8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Thu, 29 Aug 2024 18:59:58 +0200 Subject: [PATCH] test(IT): Test Config Account use cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../ConfigAccount/SaveController.php | 48 +++++------ .../ConfigBackup/XmlExportController.php | 26 ++---- .../web/Controllers/Traits/ConfigTrait.php | 18 ++-- .../ConfigAccountControllerTest.php | 82 +++++++++++++++++++ 4 files changed, 114 insertions(+), 60 deletions(-) create mode 100644 tests/SP/Modules/Web/Controllers/ConfigAccount/ConfigAccountControllerTest.php diff --git a/app/modules/web/Controllers/ConfigAccount/SaveController.php b/app/modules/web/Controllers/ConfigAccount/SaveController.php index 081ec118..e585045b 100644 --- a/app/modules/web/Controllers/ConfigAccount/SaveController.php +++ b/app/modules/web/Controllers/ConfigAccount/SaveController.php @@ -24,21 +24,23 @@ namespace SP\Modules\Web\Controllers\ConfigAccount; -use JsonException; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; +use SP\Domain\Common\Attributes\Action; +use SP\Domain\Common\Dtos\ActionResponse; +use SP\Domain\Common\Enums\ResponseType; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\Acl\AclActionsInterface; -use SP\Domain\Core\Acl\UnauthorizedPageException; use SP\Domain\Core\Exceptions\SessionTimeout; +use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Core\Exceptions\ValidationException; use SP\Modules\Web\Controllers\SimpleControllerBase; use SP\Modules\Web\Controllers\Traits\ConfigTrait; +use function SP\__u; + /** - * Class ConfigAccountController - * - * @package SP\Modules\Web\Controllers + * Class SaveController */ final class SaveController extends SimpleControllerBase { @@ -47,31 +49,25 @@ final class SaveController extends SimpleControllerBase private const MAX_FILES_SIZE = 16384; /** - * @return bool - * @throws JsonException + * @return ActionResponse + * @throws ValidationException + * @throws SPException */ - public function saveAction(): bool + #[Action(ResponseType::JSON)] + public function saveAction(): ActionResponse { $configData = $this->config->getConfigData(); $eventMessage = EventMessage::build(); - try { - $this->handleAccountsConfig($configData); - $this->handleFilesConfig($configData, $eventMessage); - $this->handlePublicLinksConfig($configData, $eventMessage); - } catch (ValidationException $e) { - $this->eventDispatcher->notify('exception', new Event($e)); - - $this->returnJsonResponseException($e); - } + $this->handleAccountsConfig($configData); + $this->handleFilesConfig($configData, $eventMessage); + $this->handlePublicLinksConfig($configData, $eventMessage); return $this->saveConfig( $configData, $this->config, - function () use ($eventMessage) { - $this->eventDispatcher->notify('save.config.account', new Event($this, $eventMessage)); - } + fn() => $this->eventDispatcher->notify('save.config.account', new Event($this, $eventMessage)) ); } @@ -152,18 +148,12 @@ final class SaveController extends SimpleControllerBase /** * @return void - * @throws JsonException + * @throws SPException * @throws SessionTimeout */ protected function initialize(): void { - try { - $this->checks(); - $this->checkAccess(AclActionsInterface::CONFIG_ACCOUNT); - } catch (UnauthorizedPageException $e) { - $this->eventDispatcher->notify('exception', new Event($e)); - - $this->returnJsonResponseException($e); - } + $this->checks(); + $this->checkAccess(AclActionsInterface::CONFIG_ACCOUNT); } } diff --git a/app/modules/web/Controllers/ConfigBackup/XmlExportController.php b/app/modules/web/Controllers/ConfigBackup/XmlExportController.php index b3c59ea0..5534f881 100644 --- a/app/modules/web/Controllers/ConfigBackup/XmlExportController.php +++ b/app/modules/web/Controllers/ConfigBackup/XmlExportController.php @@ -54,6 +54,11 @@ final class XmlExportController extends SimpleControllerBase { use JsonTrait; + /** + * @throws SessionTimeout + * @throws SPException + * @throws UnauthorizedPageException + */ public function __construct( Application $application, SimpleControllerHelper $simpleControllerHelper, @@ -62,6 +67,9 @@ final class XmlExportController extends SimpleControllerBase private readonly PathsContext $pathsContext ) { parent::__construct($application, $simpleControllerHelper); + + $this->checks(); + $this->checkAccess(AclActionsInterface::CONFIG_BACKUP); } /** @@ -132,22 +140,4 @@ final class XmlExportController extends SimpleControllerBase return $this->returnJsonResponseException($e); } } - - /** - * initialize - * - * @throws SPException - * @throws SessionTimeout - */ - protected function initialize(): void - { - try { - $this->checks(); - $this->checkAccess(AclActionsInterface::CONFIG_BACKUP); - } catch (UnauthorizedPageException $e) { - $this->eventDispatcher->notify('exception', new Event($e)); - - $this->returnJsonResponseException($e); - } - } } diff --git a/app/modules/web/Controllers/Traits/ConfigTrait.php b/app/modules/web/Controllers/Traits/ConfigTrait.php index 53f2a067..832d39d5 100644 --- a/app/modules/web/Controllers/Traits/ConfigTrait.php +++ b/app/modules/web/Controllers/Traits/ConfigTrait.php @@ -25,11 +25,10 @@ namespace SP\Modules\Web\Controllers\Traits; use Exception; -use JsonException; +use SP\Domain\Common\Dtos\ActionResponse; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Config\Ports\ConfigFileService; use SP\Domain\Core\Exceptions\SPException; -use SP\Domain\Http\Dtos\JsonMessage; use function SP\__u; use function SP\processException; @@ -41,22 +40,19 @@ use function SP\processException; */ trait ConfigTrait { - use JsonTrait; - /** * Guardar la configuración * - * @throws JsonException * @throws SPException */ protected function saveConfig( ConfigDataInterface $configData, ConfigFileService $config, callable $onSuccess = null - ): bool { + ): ActionResponse { try { if ($configData->isDemoEnabled()) { - return $this->returnJsonResponse(JsonMessage::JSON_WARNING, __u('Ey, this is a DEMO!!')); + return ActionResponse::warning(__u('Ey, this is a DEMO!!')); } $config->save($configData); @@ -65,15 +61,11 @@ trait ConfigTrait $onSuccess(); } - return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('Configuration updated')); + return ActionResponse::ok(__u('Configuration updated')); } catch (Exception $e) { processException($e); - return $this->returnJsonResponse( - JsonMessage::JSON_ERROR, - __u('Error while saving the configuration'), - [$e] - ); + return ActionResponse::error(__u('Error while saving the configuration')); } } } diff --git a/tests/SP/Modules/Web/Controllers/ConfigAccount/ConfigAccountControllerTest.php b/tests/SP/Modules/Web/Controllers/ConfigAccount/ConfigAccountControllerTest.php new file mode 100644 index 00000000..dcad8015 --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/ConfigAccount/ConfigAccountControllerTest.php @@ -0,0 +1,82 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Modules\Web\Controllers\ConfigAccount; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\Exception; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use SP\Domain\Core\Exceptions\InvalidClassException; +use SP\Infrastructure\File\FileException; +use SP\Tests\IntegrationTestCase; + +/** + * Class ConfigAccountControllerTest + */ +#[Group('integration')] +class ConfigAccountControllerTest extends IntegrationTestCase +{ + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + * @throws InvalidClassException + * @throws FileException + */ + #[Test] + public function save() + { + $data = [ + 'publiclinks_enabled' => true, + 'publiclinks_image_enabled' => self::$faker->boolean(), + 'publiclinks_maxtime' => self::$faker->randomNumber(4), + 'publiclinks_maxviews' => self::$faker->randomNumber(4), + 'files_enabled' => true, + 'files_allowed_size' => self::$faker->randomNumber(3), + 'files_allowed_mimetypes' => [self::$faker->mimeType(), self::$faker->mimeType()], + 'account_globalsearch_enabled' => self::$faker->boolean(), + 'account_passtoimage_enabled' => self::$faker->boolean(), + 'account_link_enabled' => self::$faker->boolean(), + 'account_fullgroup_access_enabled' => self::$faker->boolean(), + 'account_count' => self::$faker->randomNumber(3), + 'account_resultsascards_enabled' => self::$faker->boolean(), + 'account_expire_enabled' => self::$faker->boolean(), + 'account_expire_time' => self::$faker->randomNumber(8), + ]; + + $container = $this->buildContainer( + $this->getModuleDefinitions(), + $this->buildRequest('post', 'index.php', ['r' => 'configAccount/save'], $data) + ); + + $this->runApp($container); + + $this->expectOutputString('{"status":"OK","description":"Configuration updated","data":null}'); + } +}