From e513a7ff410174223e6217cb3d73b2c5c934056a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Wed, 14 Aug 2024 17:16:25 +0200 Subject: [PATCH] test(IT): Test account history view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Account/ViewHistoryController.php | 31 +-- .../Helpers/Account/AccountHelper.php | 20 +- .../Helpers/Account/AccountHistoryHelper.php | 91 ++++--- .../views/account/account-history.inc | 4 +- .../material-blue/views/account/details.inc | 4 +- lib/BaseFunctions.php | 2 + lib/SP/Domain/Account/Dtos/AccountAclDto.php | 1 + .../Account/Dtos/AccountHistoryViewDto.php | 121 +++++++++ lib/SP/Domain/Account/Dtos/AccountViewDto.php | 231 ++++++++++++++++++ lib/SP/Domain/Account/Models/AccountView.php | 10 - .../Account/Services/AccountHistory.php | 2 +- lib/SP/Domain/Common/Dtos/Dto.php | 25 ++ lib/SP/Domain/Common/Models/Model.php | 1 + tests/SP/Generators/AccountDataGenerator.php | 51 ++-- .../Account/ViewHistoryControllerTest.php | 102 ++++++++ 15 files changed, 590 insertions(+), 106 deletions(-) create mode 100644 lib/SP/Domain/Account/Dtos/AccountHistoryViewDto.php create mode 100644 lib/SP/Domain/Account/Dtos/AccountViewDto.php create mode 100644 tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php diff --git a/app/modules/web/Controllers/Account/ViewHistoryController.php b/app/modules/web/Controllers/Account/ViewHistoryController.php index 6416ff88..39d5c720 100644 --- a/app/modules/web/Controllers/Account/ViewHistoryController.php +++ b/app/modules/web/Controllers/Account/ViewHistoryController.php @@ -27,46 +27,49 @@ namespace SP\Modules\Web\Controllers\Account; use Exception; use SP\Core\Application; use SP\Core\Events\Event; +use SP\Domain\Account\Dtos\AccountHistoryViewDto; use SP\Domain\Account\Ports\AccountHistoryService; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Modules\Web\Controllers\Helpers\Account\AccountHistoryHelper; use SP\Modules\Web\Util\ErrorUtil; use SP\Mvc\Controller\WebControllerHelper; +use function SP\__; +use function SP\processException; + /** * ViewHistoryController */ final class ViewHistoryController extends AccountControllerBase { - private AccountHistoryService $accountHistoryService; - private AccountHistoryHelper $accountHistoryHelper; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - AccountHistoryService $accountHistoryService, - AccountHistoryHelper $accountHistoryHelper + Application $application, + WebControllerHelper $webControllerHelper, + private readonly AccountHistoryService $accountHistoryService, + private readonly AccountHistoryHelper $accountHistoryHelper ) { parent::__construct( $application, $webControllerHelper ); - - $this->accountHistoryService = $accountHistoryService; - $this->accountHistoryHelper = $accountHistoryHelper; } /** * Obtener los datos para mostrar el interface para ver cuenta en fecha concreta * - * @param int $id Account's ID + * @param int $id Account's ID */ public function viewHistoryAction(int $id): void { try { - $accountHistoryData = $this->accountHistoryService->getById($id); + $this->accountHistoryHelper->initializeFor(AclActionsInterface::ACCOUNT_HISTORY_VIEW); - $this->accountHistoryHelper->setView($accountHistoryData, AclActionsInterface::ACCOUNT_HISTORY_VIEW); + $accountHistoryViewDto = AccountHistoryViewDto::fromArray( + $this->accountHistoryService->getById($id)->toArray(includeOuter: true) + ); + + $this->accountHistoryHelper->setViewForAccount($accountHistoryViewDto); $this->view->addTemplate('account-history'); @@ -74,8 +77,8 @@ final class ViewHistoryController extends AccountControllerBase 'title', [ 'class' => 'titleNormal', - 'name' => __('Account Details'), - 'icon' => 'access_time', + 'name' => __('Account Details'), + 'icon' => 'access_time', ] ); diff --git a/app/modules/web/Controllers/Helpers/Account/AccountHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountHelper.php index 1f40b02b..cbf2ab6f 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountHelper.php @@ -107,7 +107,7 @@ final class AccountHelper extends AccountHelperBase /** * Sets account's view variables * - * @param AccountEnrichedDto $accountDetailsResponse + * @param AccountEnrichedDto $accountEnrichedDto * @throws AccountPermissionException * @throws ConstraintException * @throws QueryException @@ -115,16 +115,16 @@ final class AccountHelper extends AccountHelperBase * @throws ServiceException * @throws UnauthorizedActionException */ - public function setViewForAccount(AccountEnrichedDto $accountDetailsResponse): void + public function setViewForAccount(AccountEnrichedDto $accountEnrichedDto): void { if (!$this->actionGranted) { throw new UnauthorizedActionException(); } - $this->accountId = $accountDetailsResponse->getAccountView()->getId(); - $this->accountPermission = $this->checkAccess($accountDetailsResponse); + $this->accountId = $accountEnrichedDto->getAccountView()->getId(); + $this->accountPermission = $this->checkAccess($accountEnrichedDto); - $accountData = $accountDetailsResponse->getAccountView(); + $accountData = $accountEnrichedDto->getAccountView(); $accountActionsDto = new AccountActionsDto($this->accountId, null, $accountData->getParentId()); @@ -134,28 +134,28 @@ final class AccountHelper extends AccountHelperBase $usersView = SelectItemAdapter::getIdFromArrayOfObjects( array_filter( - $accountDetailsResponse->getUsers(), + $accountEnrichedDto->getUsers(), static fn($value) => (int)$value->isEdit === 0 ) ); $usersEdit = SelectItemAdapter::getIdFromArrayOfObjects( array_filter( - $accountDetailsResponse->getUsers(), + $accountEnrichedDto->getUsers(), static fn($value) => (int)$value->isEdit === 1 ) ); $userGroupsView = SelectItemAdapter::getIdFromArrayOfObjects( array_filter( - $accountDetailsResponse->getUserGroups(), + $accountEnrichedDto->getUserGroups(), static fn($value) => (int)$value->isEdit === 0 ) ); $userGroupsEdit = SelectItemAdapter::getIdFromArrayOfObjects( array_filter( - $accountDetailsResponse->getUserGroups(), + $accountEnrichedDto->getUserGroups(), static fn($value) => (int)$value->isEdit === 1 ) ); @@ -172,7 +172,7 @@ final class AccountHelper extends AccountHelperBase $this->view->assign( 'tags', $selectTags->getItemsFromModelSelected( - SelectItemAdapter::getIdFromArrayOfObjects($accountDetailsResponse->getTags()) + SelectItemAdapter::getIdFromArrayOfObjects($accountEnrichedDto->getTags()) ) ); $this->view->assign( diff --git a/app/modules/web/Controllers/Helpers/Account/AccountHistoryHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountHistoryHelper.php index 46fc2c0e..087e34fe 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountHistoryHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountHistoryHelper.php @@ -27,6 +27,7 @@ namespace SP\Modules\Web\Controllers\Helpers\Account; use SP\Core\Application; use SP\Domain\Account\Adapters\AccountPermission; use SP\Domain\Account\Dtos\AccountAclDto; +use SP\Domain\Account\Dtos\AccountHistoryViewDto; use SP\Domain\Account\Models\AccountHistory; use SP\Domain\Account\Ports\AccountAclService; use SP\Domain\Account\Ports\AccountHistoryService; @@ -37,6 +38,7 @@ use SP\Domain\Client\Ports\ClientService; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Acl\AccountPermissionException; use SP\Domain\Core\Acl\AclInterface; +use SP\Domain\Core\Acl\UnauthorizedActionException; use SP\Domain\Core\Acl\UnauthorizedPageException; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; @@ -50,34 +52,31 @@ use SP\Mvc\View\TemplateInterface; /** * Class AccountHistoryHelper - * - * @package SP\Modules\Web\Controllers\Helpers */ final class AccountHistoryHelper extends AccountHelperBase { - private ?int $accountId = null; - private ?AccountPermission $accountAcl = null; + private ?int $accountId = null; + private ?AccountPermission $accountPermission = null; public function __construct( - Application $application, - TemplateInterface $template, - RequestService $request, - AclInterface $acl, - AccountActionsHelper $accountActionsHelper, - MasterPassService $masterPassService, - private AccountHistoryService $accountHistoryService, - private AccountAclService $accountAclService, - private CategoryService $categoryService, - private ClientService $clientService, - private AccountToUserService $accountToUserService, - private AccountToUserGroupService $accountToUserGroupService + Application $application, + TemplateInterface $template, + RequestService $request, + AclInterface $acl, + AccountActionsHelper $accountActionsHelper, + MasterPassService $masterPassService, + private readonly AccountHistoryService $accountHistoryService, + private readonly AccountAclService $accountAclService, + private readonly CategoryService $categoryService, + private readonly ClientService $clientService, + private readonly AccountToUserService $accountToUserService, + private readonly AccountToUserGroupService $accountToUserGroupService ) { parent::__construct($application, $template, $request, $acl, $accountActionsHelper, $masterPassService); } /** - * @param AccountHistory $accountHistoryData - * @param int $actionId + * @param AccountHistory $accountHistoryViewDto * * @throws AccountPermissionException * @throws UnauthorizedPageException @@ -88,18 +87,20 @@ final class AccountHistoryHelper extends AccountHelperBase * @throws UpdatedMasterPassException * @throws NoSuchItemException */ - public function setView(AccountHistory $accountHistoryData, int $actionId): void + public function setViewForAccount(AccountHistoryViewDto $accountHistoryViewDto): void { - $this->actionId = $actionId; - $this->accountId = $accountHistoryData->getAccountId(); + if (!$this->actionGranted) { + throw new UnauthorizedActionException(); + } - $this->initializeFor($actionId); - $this->checkAccess($accountHistoryData); + $this->accountId = $accountHistoryViewDto->getAccountId(); + + $this->checkAccess($accountHistoryViewDto); $this->view->assign('isView', true); $this->view->assign('accountIsHistory', true); - $this->view->assign('accountData', $accountHistoryData); - $this->view->assign('accountAcl', $this->accountAcl); + $this->view->assign('accountData', $accountHistoryViewDto); + $this->view->assign('accountAcl', $this->accountPermission); $this->view->assign('actionId', $this->actionId); $this->view->assign('accountId', $this->accountId); @@ -107,67 +108,65 @@ final class AccountHistoryHelper extends AccountHelperBase 'historyData', SelectItemAdapter::factory( self::mapHistoryForDateSelect($this->accountHistoryService->getHistoryForAccount($this->accountId)) - ) - ->getItemsFromArraySelected([$accountHistoryData->getId()]) + )->getItemsFromArraySelected([$accountHistoryViewDto->getId()]) ); - $this->view->assign('accountPassDate', date('Y-m-d H:i:s', $accountHistoryData->getPassDate())); + $this->view->assign('accountPassDate', date('Y-m-d H:i:s', $accountHistoryViewDto->getPassDate())); $this->view->assign( 'accountPassDateChange', - date('Y-m-d', $accountHistoryData->getPassDateChange() ?: 0) + date('Y-m-d', $accountHistoryViewDto->getPassDateChange() ?: 0) ); $this->view->assign( 'categories', SelectItemAdapter::factory($this->categoryService->getAll()) - ->getItemsFromModelSelected([$accountHistoryData->getCategoryId()]) + ->getItemsFromModelSelected([$accountHistoryViewDto->getCategoryId()]) ); $this->view->assign( 'clients', SelectItemAdapter::factory($this->clientService->getAll()) - ->getItemsFromModelSelected([$accountHistoryData->getClientId()]) + ->getItemsFromModelSelected([$accountHistoryViewDto->getClientId()]) ); $this->view->assign( 'isModified', - strtotime($accountHistoryData->getDateEdit()) !== false + strtotime($accountHistoryViewDto->getDateEdit()) !== false ); - $accountActionsDto = new AccountActionsDto( - $this->accountId, - $accountHistoryData->getId(), - 0 - ); + $accountActionsDto = new AccountActionsDto($this->accountId, $accountHistoryViewDto->getId(), 0); $this->view->assign( 'accountActions', - $this->accountActionsHelper->getActionsForAccount($this->accountAcl, $accountActionsDto) + $this->accountActionsHelper->getActionsForAccount($this->accountPermission, $accountActionsDto) ); $this->view->assign( 'accountActionsMenu', - $this->accountActionsHelper->getActionsGrouppedForAccount($this->accountAcl, $accountActionsDto) + $this->accountActionsHelper->getActionsGrouppedForAccount($this->accountPermission, $accountActionsDto) ); } /** * Comprobar si el usuario dispone de acceso al módulo * - * @param AccountHistory $accountHistoryData + * @param AccountHistoryViewDto $accountHistoryViewDto * * @throws AccountPermissionException * @throws ConstraintException * @throws QueryException * @throws SPException */ - protected function checkAccess(AccountHistory $accountHistoryData): void + protected function checkAccess(AccountHistoryViewDto $accountHistoryViewDto): void { - $acccountAclDto = AccountAclDto::makeFromAccountHistory( - $accountHistoryData, + $acccountAclDto = new AccountAclDto( + $this->accountId, + $accountHistoryViewDto->getUserId(), $this->accountToUserService->getUsersByAccountId($this->accountId), - $this->accountToUserGroupService->getUserGroupsByAccountId($this->accountId) + $accountHistoryViewDto->getUserGroupId(), + $this->accountToUserGroupService->getUserGroupsByAccountId($this->accountId), + $accountHistoryViewDto->getDateEdit() ); - $this->accountAcl = $this->accountAclService->getAcl($this->actionId, $acccountAclDto, true); + $this->accountPermission = $this->accountAclService->getAcl($this->actionId, $acccountAclDto, true); - if ($this->accountAcl->checkAccountAccess($this->actionId) === false) { + if ($this->accountPermission->checkAccountAccess($this->actionId) === false) { throw new AccountPermissionException(SPException::INFO); } } diff --git a/app/modules/web/themes/material-blue/views/account/account-history.inc b/app/modules/web/themes/material-blue/views/account/account-history.inc index 4f9d16e6..d8a65480 100644 --- a/app/modules/web/themes/material-blue/views/account/account-history.inc +++ b/app/modules/web/themes/material-blue/views/account/account-history.inc @@ -24,7 +24,7 @@ use SP\Domain\Account\Adapters\AccountPermission; -use SP\Domain\Account\Models\AccountView; +use SP\Domain\Account\Dtos\AccountHistoryViewDto; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\UI\ThemeIconsInterface; use SP\Mvc\View\Components\SelectItem; @@ -34,7 +34,7 @@ use function SP\__; /** * @var callable $_getvar * @var ThemeIconsInterface $icons - * @var AccountView $accountView + * @var AccountHistoryViewDto $accountView * @var AccountPermission $accountAcl * @var ConfigDataInterface $configData */ diff --git a/app/modules/web/themes/material-blue/views/account/details.inc b/app/modules/web/themes/material-blue/views/account/details.inc index 2d33a442..b6a6391a 100644 --- a/app/modules/web/themes/material-blue/views/account/details.inc +++ b/app/modules/web/themes/material-blue/views/account/details.inc @@ -24,7 +24,7 @@ use SP\Domain\Account\Adapters\AccountPermission; -use SP\Domain\Account\Models\AccountView; +use SP\Domain\Account\Dtos\AccountViewDto; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\UI\ThemeIconsInterface; @@ -33,7 +33,7 @@ use function SP\__; /** * @var ThemeIconsInterface $icons * @var callable $_getvar - * @var AccountView $accountView + * @var AccountViewDto $accountView * @var AccountPermission $accountAcl * @var ConfigDataInterface $configData */ diff --git a/lib/BaseFunctions.php b/lib/BaseFunctions.php index d6aae3a2..d25def8b 100644 --- a/lib/BaseFunctions.php +++ b/lib/BaseFunctions.php @@ -29,6 +29,8 @@ use SP\Domain\Core\Exceptions\SPException; use SP\Infrastructure\File\FileSystem; use Throwable; +use const APP_PATH; + /** * [type] [caller] data */ diff --git a/lib/SP/Domain/Account/Dtos/AccountAclDto.php b/lib/SP/Domain/Account/Dtos/AccountAclDto.php index da5d03d3..8015e21b 100644 --- a/lib/SP/Domain/Account/Dtos/AccountAclDto.php +++ b/lib/SP/Domain/Account/Dtos/AccountAclDto.php @@ -1,4 +1,5 @@ . + */ + +declare(strict_types=1); + +namespace SP\Domain\Account\Dtos; + +/** + * Class AccountHistoryViewDto + */ +final class AccountHistoryViewDto extends AccountViewDto +{ + public function __construct( + int $id, + string $name, + string $login, + int $clientId, + int $categoryId, + string $pass, + int $userId, + string $userName, + string $key, + string $url, + string $notes, + int $userEditId, + string $userEditName, + string $userEditLogin, + bool $isPrivate, + bool $isPrivateGroup, + int $userGroupId, + string $userGroupName, + bool $otherUserEdit, + bool $otherUserGroupEdit, + int $countView, + int $countDecrypt, + int $dateAdd, + protected int $accountId, + protected int $isModify, + protected int $isDeleted, + ?int $dateEdit = null, + ?int $passDate = null, + ?int $passDateChange = null, + ?int $parentId = null, + ?array $usersView = null, + ?array $usersEdit = null, + ?array $userGroupsView = null, + ?array $userGroupsEdit = null, + ?array $tags = null + ) { + parent::__construct( + $id, + $name, + $login, + $clientId, + $categoryId, + $pass, + $userId, + $userName, + $key, + $url, + $notes, + $userEditId, + $userEditName, + $userEditLogin, + $isPrivate, + $isPrivateGroup, + $userGroupId, + $userGroupName, + $otherUserEdit, + $otherUserGroupEdit, + $countView, + $countDecrypt, + $dateAdd, + $dateEdit, + $passDate, + $passDateChange, + $parentId, + $usersView, + $usersEdit, + $userGroupsView, + $userGroupsEdit, + $tags + ); + } + + public function getAccountId(): int + { + return $this->accountId; + } + + public function getIsModify(): int + { + return $this->isModify; + } + + public function getIsDeleted(): int + { + return $this->isDeleted; + } +} diff --git a/lib/SP/Domain/Account/Dtos/AccountViewDto.php b/lib/SP/Domain/Account/Dtos/AccountViewDto.php new file mode 100644 index 00000000..57d496df --- /dev/null +++ b/lib/SP/Domain/Account/Dtos/AccountViewDto.php @@ -0,0 +1,231 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Domain\Account\Dtos; + +use SP\Domain\Common\Dtos\Dto; + +/** + * Class AccountViewDto + */ +class AccountViewDto extends Dto +{ + public function __construct( + protected int $id, + protected string $name, + protected string $login, + protected int $clientId, + protected int $categoryId, + protected string $pass, + protected int $userId, + protected string $userName, + protected string $key, + protected string $url, + protected string $notes, + protected int $userEditId, + protected string $userEditName, + protected string $userEditLogin, + protected bool $isPrivate, + protected bool $isPrivateGroup, + protected int $userGroupId, + protected string $userGroupName, + protected bool $otherUserEdit, + protected bool $otherUserGroupEdit, + protected int $countView, + protected int $countDecrypt, + protected int $dateAdd, + protected ?int $dateEdit = null, + protected ?int $passDate = null, + protected ?int $passDateChange = null, + protected ?int $parentId = null, + protected ?array $usersView = null, + protected ?array $usersEdit = null, + protected ?array $userGroupsView = null, + protected ?array $userGroupsEdit = null, + protected ?array $tags = null + ) { + } + + public function getPassDate(): ?int + { + return $this->passDate; + } + + public function getId(): int + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function getLogin(): string + { + return $this->login; + } + + public function getClientId(): int + { + return $this->clientId; + } + + public function getCategoryId(): int + { + return $this->categoryId; + } + + public function getPass(): string + { + return $this->pass; + } + + public function getUserId(): int + { + return $this->userId; + } + + public function getUserName(): string + { + return $this->userName; + } + + public function getKey(): string + { + return $this->key; + } + + public function getUrl(): string + { + return $this->url; + } + + public function getNotes(): string + { + return $this->notes; + } + + public function getUserEditId(): int + { + return $this->userEditId; + } + + public function getUserEditName(): string + { + return $this->userEditName; + } + + public function getUserEditLogin(): string + { + return $this->userEditLogin; + } + + public function isPrivate(): bool + { + return $this->isPrivate; + } + + public function isPrivateGroup(): bool + { + return $this->isPrivateGroup; + } + + public function getUserGroupId(): int + { + return $this->userGroupId; + } + + public function getUserGroupName(): string + { + return $this->userGroupName; + } + + public function isOtherUserEdit(): bool + { + return $this->otherUserEdit; + } + + public function isOtherUserGroupEdit(): bool + { + return $this->otherUserGroupEdit; + } + + public function getPassDateChange(): ?int + { + return $this->passDateChange; + } + + public function getParentId(): ?int + { + return $this->parentId; + } + + public function getUsersView(): ?array + { + return $this->usersView; + } + + public function getUsersEdit(): ?array + { + return $this->usersEdit; + } + + public function getUserGroupsView(): ?array + { + return $this->userGroupsView; + } + + public function getUserGroupsEdit(): ?array + { + return $this->userGroupsEdit; + } + + public function getTags(): ?array + { + return $this->tags; + } + + public function getCountView(): int + { + return $this->countView; + } + + public function getCountDecrypt(): int + { + return $this->countDecrypt; + } + + public function getDateAdd(): int + { + return $this->dateAdd; + } + + public function getDateEdit(): ?int + { + return $this->dateEdit; + } +} diff --git a/lib/SP/Domain/Account/Models/AccountView.php b/lib/SP/Domain/Account/Models/AccountView.php index def7b8eb..854ffa25 100644 --- a/lib/SP/Domain/Account/Models/AccountView.php +++ b/lib/SP/Domain/Account/Models/AccountView.php @@ -170,16 +170,6 @@ final class AccountView extends Model return $this->otherUserGroupEdit; } - public function getPass(): ?string - { - return $this->pass; - } - - public function getKey(): ?string - { - return $this->key; - } - public function getId(): ?int { return $this->id; diff --git a/lib/SP/Domain/Account/Services/AccountHistory.php b/lib/SP/Domain/Account/Services/AccountHistory.php index 2c94a145..934b462c 100644 --- a/lib/SP/Domain/Account/Services/AccountHistory.php +++ b/lib/SP/Domain/Account/Services/AccountHistory.php @@ -69,7 +69,7 @@ final class AccountHistory extends Service implements AccountHistoryService throw new NoSuchItemException(__u('Error while retrieving account\'s data')); } - return AccountHistoryModel::buildFromSimpleModel($results->getData()); + return $results->getData(AccountHistoryModel::class); } /** diff --git a/lib/SP/Domain/Common/Dtos/Dto.php b/lib/SP/Domain/Common/Dtos/Dto.php index e18a04d4..1381db54 100644 --- a/lib/SP/Domain/Common/Dtos/Dto.php +++ b/lib/SP/Domain/Common/Dtos/Dto.php @@ -1,4 +1,5 @@ getConstructor()->getParameters() as $parameter) { + $parameters[] = $properties[$parameter->getName()] ?? null; + } + + return $reflectionClass->newInstanceArgs($parameters); + } catch (ReflectionException $e) { + processException($e); + } + + return new static(); + } + /** * Expose any property. This allows to get any property from dynamic calls. * diff --git a/lib/SP/Domain/Common/Models/Model.php b/lib/SP/Domain/Common/Models/Model.php index 94ec82b7..88f673ce 100644 --- a/lib/SP/Domain/Common/Models/Model.php +++ b/lib/SP/Domain/Common/Models/Model.php @@ -142,6 +142,7 @@ abstract class Model implements JsonSerializable, ArrayAccess * * @return string * @throws JsonException + * @throws SPException */ public function __toString() { diff --git a/tests/SP/Generators/AccountDataGenerator.php b/tests/SP/Generators/AccountDataGenerator.php index 5e0bbde8..f2cfe74b 100644 --- a/tests/SP/Generators/AccountDataGenerator.php +++ b/tests/SP/Generators/AccountDataGenerator.php @@ -31,10 +31,10 @@ use SP\Domain\Account\Dtos\AccountEnrichedDto; use SP\Domain\Account\Dtos\AccountHistoryDto; use SP\Domain\Account\Dtos\AccountUpdateDto; use SP\Domain\Account\Models\Account; +use SP\Domain\Account\Models\AccountHistory; use SP\Domain\Account\Models\AccountSearchView; use SP\Domain\Account\Models\AccountView; use SP\Domain\Common\Models\Item; -use SP\Domain\Common\Models\Simple; /** * Class AccountDataGenerator @@ -125,27 +125,36 @@ final class AccountDataGenerator extends DataGenerator return new Account($this->getAccountProperties()); } - public function buildAccountHistoryData(): Simple + public function buildAccountHistoryData(): AccountHistory { - return new Simple([ - 'id' => $this->faker->randomNumber(3), - 'accountId' => $this->faker->randomNumber(3), - 'name' => $this->faker->name(), - 'login' => $this->faker->userName(), - 'url' => $this->faker->url(), - 'notes' => $this->faker->text(), - 'userEditId' => $this->faker->randomNumber(3), - 'passDateChange' => $this->faker->unixTime(), - 'clientId' => $this->faker->randomNumber(3), - 'categoryId' => $this->faker->randomNumber(3), - 'isPrivate' => $this->faker->numberBetween(0, 1), - 'isPrivateGroup' => $this->faker->numberBetween(0, 1), - 'parentId' => $this->faker->randomNumber(3), - 'userId' => $this->faker->randomNumber(3), - 'userGroupId' => $this->faker->randomNumber(3), - 'key' => $this->faker->text(), - 'pass' => $this->faker->text(), - ]); + return new AccountHistory([ + 'id' => $this->faker->randomNumber(3), + 'accountId' => $this->faker->randomNumber(3), + 'name' => $this->faker->name(), + 'login' => $this->faker->userName(), + 'url' => $this->faker->url(), + 'notes' => $this->faker->text(), + 'userEditId' => $this->faker->randomNumber(3), + 'passDateChange' => $this->faker->unixTime(), + 'passDate' => $this->faker->unixTime(), + 'clientId' => $this->faker->randomNumber(3), + 'categoryId' => $this->faker->randomNumber(3), + 'isPrivate' => $this->faker->numberBetween(0, 1), + 'isPrivateGroup' => $this->faker->numberBetween(0, 1), + 'parentId' => $this->faker->randomNumber(3), + 'userId' => $this->faker->randomNumber(3), + 'userGroupId' => $this->faker->randomNumber(3), + 'key' => $this->faker->text(), + 'pass' => $this->faker->text(), + 'dateEdit' => $this->faker->unixTime(), + 'dateAdd' => $this->faker->unixTime(), + 'isModify' => $this->faker->numberBetween(0, 1), + 'isDeleted' => $this->faker->numberBetween(0, 1), + 'otherUserGroupEdit' => $this->faker->numberBetween(0, 1), + 'otherUserEdit' => $this->faker->numberBetween(0, 1), + 'countView' => $this->faker->randomNumber(3), + 'countDecrypt' => $this->faker->randomNumber(3) + ]); } public function buildAccountUpdateDto(): AccountUpdateDto diff --git a/tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php new file mode 100644 index 00000000..537380dd --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/Account/ViewHistoryControllerTest.php @@ -0,0 +1,102 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Modules\Web\Controllers\Account; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use SP\Domain\Account\Models\AccountHistory; +use SP\Domain\Common\Models\Item; +use SP\Domain\Core\Exceptions\InvalidClassException; +use SP\Infrastructure\Database\QueryResult; +use SP\Infrastructure\File\FileException; +use SP\Mvc\View\OutputHandlerInterface; +use SP\Tests\Generators\AccountDataGenerator; +use SP\Tests\IntegrationTestCase; +use Symfony\Component\DomCrawler\Crawler; + +/** + * Class ViewHistoryControllerTest + */ +#[Group('integration')] +class ViewHistoryControllerTest extends IntegrationTestCase +{ + + /** + * @throws NotFoundExceptionInterface + * @throws Exception + * @throws InvalidClassException + * @throws FileException + * @throws ContainerExceptionInterface + */ + public function testViewHistoryAction() + { + $accountHistory = AccountDataGenerator::factory() + ->buildAccountHistoryData() + ->mutate([ + 'userName' => self::$faker->userName(), + 'userGroupName' => self::$faker->userName(), + 'userEditName' => self::$faker->userName(), + 'userEditLogin' => self::$faker->userName(), + ]); + $this->addDatabaseResolver( + AccountHistory::class, + new QueryResult([$accountHistory]) + ); + + $this->addDatabaseResolver( + Item::class, + new QueryResult( + [ + new Item( + ['id' => self::$faker->randomNumber(3), 'name' => self::$faker->colorName()] + ) + ] + ) + ); + $definitions = $this->getModuleDefinitions(); + $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + assert(!empty($output)); + assert(count($filter) === 2); + + $this->assertTrue(true); + }); + + $container = $this->buildContainer( + $definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/viewHistory/id/' . self::$faker->randomNumber(3)]) + ); + + $this->runApp($container); + } +}