From 3307e922699ffbe38cf656d958f12499aa851a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Wed, 7 Aug 2024 06:46:53 +0200 Subject: [PATCH] test(IT): Test account edit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Account/SaveEditController.php | 32 ++--- app/modules/web/Forms/AccountForm.php | 1 - .../Account/SaveEditControllerTest.php | 123 ++++++++++++++++++ 3 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php diff --git a/app/modules/web/Controllers/Account/SaveEditController.php b/app/modules/web/Controllers/Account/SaveEditController.php index 9701e07f..355632b8 100644 --- a/app/modules/web/Controllers/Account/SaveEditController.php +++ b/app/modules/web/Controllers/Account/SaveEditController.php @@ -24,16 +24,17 @@ namespace SP\Modules\Web\Controllers\Account; - use Exception; -use JsonException; -use SP\Core\Acl\Acl; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Core\Acl\AclActionsInterface; +use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Core\Exceptions\ValidationException; use SP\Domain\Http\Dtos\JsonMessage; +use function SP\__u; +use function SP\processException; + /** * Class SaveEditController */ @@ -42,29 +43,28 @@ final class SaveEditController extends AccountSaveBase /** * Saves edit action * - * @param int $id Account's ID + * @param int $id Account's ID * - * @return bool - * @throws JsonException + * @return bool|null + * @throws SPException */ public function saveEditAction(int $id): ?bool { try { $this->accountForm->validateFor(AclActionsInterface::ACCOUNT_EDIT, $id); - $itemData = $this->accountForm->getItemData(); + $this->accountService->update($id, $this->accountForm->getItemData()); - $this->accountService->update($itemData); - - $accountDetails = $this->accountService->getByIdEnriched($id)->getAccountVData(); + $accountDetails = $this->accountService->getByIdEnriched($id); $this->eventDispatcher->notify( 'edit.account', new Event( - $this, EventMessage::factory() - ->addDescription(__u('Account updated')) - ->addDetail(__u('Account'), $accountDetails->getName()) - ->addDetail(__u('Client'), $accountDetails->getClientName()) + $this, + EventMessage::factory() + ->addDescription(__u('Account updated')) + ->addDetail(__u('Account'), $accountDetails->getName()) + ->addDetail(__u('Client'), $accountDetails->getClientName()) ) ); @@ -77,8 +77,8 @@ final class SaveEditController extends AccountSaveBase return $this->returnJsonResponseData( [ - 'itemId' => $id, - 'nextAction' => Acl::getActionRoute(AclActionsInterface::ACCOUNT_VIEW), + 'itemId' => $id, + 'nextAction' => $this->acl->getRouteFor(AclActionsInterface::ACCOUNT_VIEW), ], JsonMessage::JSON_SUCCESS, __u('Account updated') diff --git a/app/modules/web/Forms/AccountForm.php b/app/modules/web/Forms/AccountForm.php index 47cce674..a0139681 100644 --- a/app/modules/web/Forms/AccountForm.php +++ b/app/modules/web/Forms/AccountForm.php @@ -149,7 +149,6 @@ final class AccountForm extends FormBase implements FormInterface ); } else { $accountDto = new AccountUpdateDto( - $this->itemId, $name, $login, $clientId, diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php new file mode 100644 index 00000000..5865670b --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/Account/SaveEditControllerTest.php @@ -0,0 +1,123 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Modules\Web\Controllers\Account; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use ReflectionClass; +use SP\Domain\Config\Ports\ConfigService; +use SP\Domain\Core\Exceptions\InvalidClassException; +use SP\Infrastructure\Database\QueryData; +use SP\Infrastructure\Database\QueryResult; +use SP\Infrastructure\File\FileException; +use SP\Tests\Generators\AccountDataGenerator; +use SP\Tests\IntegrationTestCase; + +/** + * Class SaveEditControllerTest + */ +#[Group('integration')] +class SaveEditControllerTest extends IntegrationTestCase +{ + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + * @throws InvalidClassException + * @throws FileException + */ + public function testSaveEditAction() + { + $configService = self::createStub(ConfigService::class); + $configService->method('getByParam')->willReturnArgument(0); + + $definitions = $this->getModuleDefinitions(); + $definitions[ConfigService::class] = $configService; + + $account = AccountDataGenerator::factory()->buildAccount(); + + $paramsPost = [ + 'name' => $account->getName(), + 'login' => $account->getLogin(), + 'client_id' => $account->getClientId(), + 'category_id' => $account->getCategoryId(), + 'password' => $account->getPass(), + 'password_repeat' => $account->getPass(), + 'owner_id' => $account->getUserId(), + 'notes' => $account->getNotes(), + 'private_enabled' => $account->getIsPrivate(), + 'private_group_enabled' => $account->getIsPrivateGroup(), + 'password_date_expire_unix' => $account->getPassDate(), + 'parent_account_id' => $account->getParentId(), + 'main_usergroup_id' => $account->getUserGroupId(), + 'other_users_view_update' => 1, + 'other_users_view' => [1, 2, 3], + 'other_users_edit_update' => 1, + 'other_users_edit' => [4, 5, 6], + 'other_usergroups_view_update' => 1, + 'other_usergroups_view' => [8, 9, 10], + 'other_usergroups_edit_update' => 1, + 'other_usergroups_edit' => [11, 12, 13], + 'tags_update' => 1, + 'tags' => [15, 16, 17], + ]; + + $accountId = self::$faker->randomNumber(3); + + $container = $this->buildContainer( + $definitions, + $this->buildRequest( + 'post', + 'index.php', + ['r' => 'account/saveEdit/' . $accountId], + $paramsPost + ) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account updated","data":{"itemId":' . $accountId . + ',"nextAction":"3"},"messages":[]}' + ); + } + + protected function getDatabaseReturn(): callable + { + return function (QueryData $queryData): QueryResult { + if (!empty($queryData->getMapClassName())) { + $reflection = new ReflectionClass($queryData->getMapClassName()); + return new QueryResult([$reflection->newInstance()], 1, 100); + } + + return new QueryResult([], 1, 100); + }; + } +}