diff --git a/app/modules/web/Controllers/Account/SaveCreateController.php b/app/modules/web/Controllers/Account/SaveCreateController.php index d940f52a..a6731338 100644 --- a/app/modules/web/Controllers/Account/SaveCreateController.php +++ b/app/modules/web/Controllers/Account/SaveCreateController.php @@ -25,22 +25,24 @@ 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 SaveCreateController */ final class SaveCreateController extends AccountSaveBase { /** - * @return bool - * @throws JsonException + * @return bool|null + * @throws SPException */ public function saveCreateAction(): ?bool { @@ -48,16 +50,16 @@ final class SaveCreateController extends AccountSaveBase $this->accountForm->validateFor(AclActionsInterface::ACCOUNT_CREATE); $accountId = $this->accountService->create($this->accountForm->getItemData()); - - $accountDetails = $this->accountService->getByIdEnriched($accountId)->getAccountVData(); + $accountView = $this->accountService->getByIdEnriched($accountId); $this->eventDispatcher->notify( 'create.account', new Event( - $this, EventMessage::factory() - ->addDescription(__u('Account created')) - ->addDetail(__u('Account'), $accountDetails->getName()) - ->addDetail(__u('Client'), $accountDetails->getClientName()) + $this, + EventMessage::factory() + ->addDescription(__u('Account created')) + ->addDetail(__u('Account'), $accountView->getName()) + ->addDetail(__u('Client'), $accountView->getClientName()) ) ); @@ -70,8 +72,8 @@ final class SaveCreateController extends AccountSaveBase return $this->returnJsonResponseData( [ - 'itemId' => $accountId, - 'nextAction' => Acl::getActionRoute(AclActionsInterface::ACCOUNT_EDIT), + 'itemId' => $accountId, + 'nextAction' => $this->acl->getRouteFor(AclActionsInterface::ACCOUNT_EDIT), ], JsonMessage::JSON_SUCCESS, __u('Account created') diff --git a/tests/SP/IntegrationTestCase.php b/tests/SP/IntegrationTestCase.php index 82c24cc0..032faf70 100644 --- a/tests/SP/IntegrationTestCase.php +++ b/tests/SP/IntegrationTestCase.php @@ -110,6 +110,7 @@ abstract class IntegrationTestCase extends TestCase $acl = self::createMock(AclInterface::class); $acl->method('checkUserAccess')->willReturn(true); + $acl->method('getRouteFor')->willReturnCallback(static fn(int $actionId) => (string)$actionId); $accountAcl = self::createStub(AccountAclService::class); $accountAcl->method('getAcl') diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php index d3b1e0c4..d95a9815 100644 --- a/tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/Account/SaveCopyControllerTest.php @@ -101,7 +101,7 @@ class SaveCopyControllerTest extends IntegrationTestCase $this->runApp($container); $this->expectOutputString( - '{"status":0,"description":"Account created","data":{"itemId":100,"nextAction":""},"messages":[]}' + '{"status":0,"description":"Account created","data":{"itemId":100,"nextAction":"5"},"messages":[]}' ); } diff --git a/tests/SP/Modules/Web/Controllers/Account/SaveCreateControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/SaveCreateControllerTest.php new file mode 100644 index 00000000..9ea85acf --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/Account/SaveCreateControllerTest.php @@ -0,0 +1,131 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Modules\Web\Controllers\Account; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\Stub; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use ReflectionClass; +use SP\Domain\Core\Context\SessionContext; +use SP\Domain\Core\Crypt\CryptInterface; +use SP\Domain\Core\Crypt\VaultInterface; +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 SaveCreateControllerTest + */ +#[Group('integration')] +class SaveCreateControllerTest extends IntegrationTestCase +{ + + /** + * @throws ContainerExceptionInterface + * @throws Exception + * @throws NotFoundExceptionInterface + * @throws InvalidClassException + * @throws FileException + */ + public function testSaveCreateAction() + { + $crypt = $this->createStub(CryptInterface::class); + $crypt->method('decrypt')->willReturn('some_data'); + $crypt->method('encrypt')->willReturn('some_data'); + + $definitions = $this->getModuleDefinitions(); + $definitions[CryptInterface::class] = $crypt; + + $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], + ]; + + $container = $this->buildContainer( + $definitions, + $this->buildRequest('post', 'index.php', ['r' => 'account/saveCreate'], $paramsPost) + ); + + $this->runApp($container); + + $this->expectOutputString( + '{"status":0,"description":"Account created","data":{"itemId":100,"nextAction":"5"},"messages":[]}' + ); + } + + protected function getDatabaseReturn(): callable + { + return function (QueryData $queryData): QueryResult { + if (!empty($queryData->getMapClassName())) { + $reflection = new ReflectionClass($queryData->getMapClassName()); + return new QueryResult([$reflection->newInstance()], 0, 100); + } + + return new QueryResult([], 0, 100); + }; + } + + protected function getContext(): SessionContext|Stub + { + $vault = self::createStub(VaultInterface::class); + $vault->method('getData') + ->willReturn('some_data'); + + $context = parent::getContext(); + $context->method('getVault')->willReturn($vault); + + return $context; + } +}