test(IT): Test Client use cases

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-08-29 18:38:59 +02:00
parent 04edbc3b9f
commit e2d67fef14
13 changed files with 574 additions and 310 deletions

View File

@@ -26,7 +26,9 @@ namespace SP\Modules\Web\Controllers\Client;
use SP\Core\Application;
use SP\Domain\Auth\Services\AuthException;
use SP\Domain\Client\Ports\ClientService;
use SP\Domain\Core\Exceptions\SessionTimeout;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
use SP\Modules\Web\Controllers\ControllerBase;
use SP\Modules\Web\Forms\ClientForm;
@@ -37,22 +39,22 @@ use SP\Mvc\Controller\WebControllerHelper;
*/
abstract class ClientSaveBase extends ControllerBase
{
protected ClientService $clientService;
protected CustomFieldDataService $customFieldService;
protected ClientForm $form;
protected readonly ClientForm $form;
/**
* @throws AuthException
* @throws SessionTimeout
*/
public function __construct(
Application $application,
WebControllerHelper $webControllerHelper,
ClientService $clientService,
CustomFieldDataService $customFieldService
Application $application,
WebControllerHelper $webControllerHelper,
protected readonly ClientService $clientService,
protected readonly CustomFieldDataService $customFieldService
) {
parent::__construct($application, $webControllerHelper);
$this->checkLoggedIn();
$this->clientService = $clientService;
$this->customFieldService = $customFieldService;
$this->form = new ClientForm($application, $this->request);
}
}

View File

@@ -24,15 +24,15 @@
namespace SP\Modules\Web\Controllers\Client;
use SP\Core\Acl\Acl;
use SP\Core\Application;
use SP\Domain\Auth\Services\AuthException;
use SP\Domain\Client\Models\Client;
use SP\Domain\Client\Ports\ClientService;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SessionTimeout;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
@@ -47,27 +47,25 @@ abstract class ClientViewBase extends ControllerBase
{
use ItemTrait;
private ClientService $clientService;
private CustomFieldDataService $customFieldService;
/**
* @throws AuthException
* @throws SessionTimeout
*/
public function __construct(
Application $application,
WebControllerHelper $webControllerHelper,
ClientService $clientService,
CustomFieldDataService $customFieldService
Application $application,
WebControllerHelper $webControllerHelper,
private readonly ClientService $clientService,
private readonly CustomFieldDataService $customFieldService
) {
parent::__construct($application, $webControllerHelper);
$this->checkLoggedIn();
$this->clientService = $clientService;
$this->customFieldService = $customFieldService;
}
/**
* Sets view data for displaying client's data
*
* @param int|null $clientId
* @param int|null $clientId
*
* @throws ConstraintException
* @throws NoSuchItemException
@@ -75,7 +73,7 @@ abstract class ClientViewBase extends ControllerBase
* @throws SPException
* @throws ServiceException
*/
protected function setViewData(?int $clientId = null): void
protected function setViewData(?int $clientId = null, bool $readonly = true): void
{
$this->view->addTemplate('client', 'itemshow');
@@ -87,10 +85,12 @@ abstract class ClientViewBase extends ControllerBase
$this->view->assign(
'nextAction',
Acl::getActionRoute(AclActionsInterface::ITEMS_MANAGE)
$this->acl->getRouteFor(AclActionsInterface::ITEMS_MANAGE)
);
if ($this->view->isView === true) {
$this->view->assign('isView', $readonly);
if ($readonly) {
$this->view->assign('disabled', 'disabled');
$this->view->assign('readonly', 'readonly');
} else {

View File

@@ -24,53 +24,48 @@
namespace SP\Modules\Web\Controllers\Client;
use Exception;
use JsonException;
use SP\Core\Events\Event;
use SP\Domain\Common\Attributes\Action;
use SP\Domain\Common\Dtos\ActionResponse;
use SP\Domain\Common\Enums\ResponseType;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Http\Dtos\JsonMessage;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use function SP\__;
use function SP\__u;
/**
* Class CreateController
*/
final class CreateController extends ClientViewBase
{
use JsonTrait;
/**
* @return bool
* @throws JsonException
* @return ActionResponse
* @throws ServiceException
* @throws ConstraintException
* @throws QueryException
* @throws SPException
* @throws NoSuchItemException
*/
public function createAction(): bool
#[Action(ResponseType::JSON)]
public function createAction(): ActionResponse
{
try {
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_CREATE)) {
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->assign('header', __('New Client'));
$this->view->assign('isView', false);
$this->view->assign('route', 'client/saveCreate');
$this->setViewData();
$this->eventDispatcher->notify('show.client.create', new Event($this));
return $this->returnJsonResponseData(['html' => $this->render()]);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notify(
'exception',
new Event($e)
);
return $this->returnJsonResponseException($e);
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_CREATE)) {
return ActionResponse::error(__u('You don\'t have permission to do this operation'));
}
$this->view->assign('header', __('New Client'));
$this->view->assign('isView', false);
$this->view->assign('route', 'client/saveCreate');
$this->setViewData();
$this->eventDispatcher->notify('show.client.create', new Event($this));
return ActionResponse::ok('', ['html' => $this->render()]);
}
}

View File

@@ -24,16 +24,21 @@
namespace SP\Modules\Web\Controllers\Client;
use Exception;
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\Common\Services\ServiceException;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Http\Dtos\JsonMessage;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use SP\Mvc\Controller\ItemTrait;
use function SP\__u;
/**
* Class DeleteController
*/
@@ -45,60 +50,48 @@ final class DeleteController extends ClientSaveBase
/**
* Delete action
*
* @param int|null $id
* @param int|null $id
*
* @return bool
* @throws JsonException
* @return ActionResponse
* @throws ServiceException
* @throws ConstraintException
* @throws QueryException
* @throws NoSuchItemException
*/
public function deleteAction(?int $id = null): bool
#[Action(ResponseType::JSON)]
public function deleteAction(?int $id = null): ActionResponse
{
try {
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_DELETE)) {
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_DELETE)) {
return ActionResponse::error(__u('You don\'t have permission to do this operation'));
}
if ($id === null) {
$this->clientService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
$this->deleteCustomFieldsForItem(AclActionsInterface::CLIENT, $id, $this->customFieldService);
$this->eventDispatcher->notify(
'delete.client.selection',
new Event(
$this,
EventMessage::build()->addDescription(__u('Clients deleted'))
)
);
return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('Clients deleted'));
}
$this->clientService->delete($id);
$this->deleteCustomFieldsForItem(AclActionsInterface::CLIENT, $id, $this->customFieldService);
if ($id === null) {
$ids = $this->getItemsIdFromRequest($this->request);
$this->clientService->deleteByIdBatch($ids);
$this->deleteCustomFieldsForItem(AclActionsInterface::CLIENT, $ids, $this->customFieldService);
$this->eventDispatcher->notify(
'delete.client',
'delete.client.selection',
new Event(
$this,
EventMessage::build()
->addDescription(__u('Client deleted'))
->addDetail(__u('Client'), $id)
EventMessage::build()->addDescription(__u('Clients deleted'))
)
);
return $this->returnJsonResponse(
JsonMessage::JSON_SUCCESS,
__u('Client deleted')
);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notify('exception', new Event($e));
return $this->returnJsonResponseException($e);
return ActionResponse::ok(__u('Clients deleted'));
}
$this->clientService->delete($id);
$this->deleteCustomFieldsForItem(AclActionsInterface::CLIENT, $id, $this->customFieldService);
$this->eventDispatcher->notify(
'delete.client',
new Event(
$this,
EventMessage::build(__u('Client deleted'))->addDetail(__u('Client'), $id)
)
);
return ActionResponse::ok(__u('Client deleted'));
}
}

View File

@@ -24,14 +24,21 @@
namespace SP\Modules\Web\Controllers\Client;
use Exception;
use JsonException;
use SP\Core\Events\Event;
use SP\Domain\Common\Attributes\Action;
use SP\Domain\Common\Dtos\ActionResponse;
use SP\Domain\Common\Enums\ResponseType;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Http\Dtos\JsonMessage;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use function SP\__;
use function SP\__u;
/**
* Class EditController
*/
@@ -42,36 +49,30 @@ final class EditController extends ClientViewBase
/**
* Edit action
*
* @param int $id
* @param int $id
*
* @return bool
* @throws JsonException
* @return ActionResponse
* @throws ServiceException
* @throws ConstraintException
* @throws QueryException
* @throws SPException
* @throws NoSuchItemException
*/
public function editAction(int $id): bool
#[Action(ResponseType::JSON)]
public function editAction(int $id): ActionResponse
{
try {
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_EDIT)) {
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->assign('header', __('Edit Client'));
$this->view->assign('isView', false);
$this->view->assign('route', 'client/saveEdit/'.$id);
$this->setViewData($id);
$this->eventDispatcher->notify('show.client.edit', new Event($this));
return $this->returnJsonResponseData(['html' => $this->render()]);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notify('exception', new Event($e));
return $this->returnJsonResponseException($e);
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_EDIT)) {
return ActionResponse::error(__u('You don\'t have permission to do this operation'));
}
$this->view->assign('header', __('Edit Client'));
$this->view->assign('isView', false);
$this->view->assign('route', 'client/saveEdit/' . $id);
$this->setViewData($id, false);
$this->eventDispatcher->notify('show.client.edit', new Event($this));
return ActionResponse::ok('', ['html' => $this->render()]);
}
}

View File

@@ -25,16 +25,21 @@
namespace SP\Modules\Web\Controllers\Client;
use Exception;
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\Common\Services\ServiceException;
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 SP\Infrastructure\Common\Repositories\DuplicatedItemException;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use SP\Mvc\Controller\ItemTrait;
use function SP\__u;
/**
* Class SaveCreateController
*/
@@ -44,51 +49,40 @@ final class SaveCreateController extends ClientSaveBase
use JsonTrait;
/**
* @return bool
* @throws JsonException
* @return ActionResponse
* @throws ValidationException
* @throws ServiceException
* @throws SPException
* @throws DuplicatedItemException
*/
public function saveCreateAction(): bool
#[Action(ResponseType::JSON)]
public function saveCreateAction(): ActionResponse
{
try {
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_CREATE)) {
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->form->validateFor(AclActionsInterface::CLIENT_CREATE);
$itemData = $this->form->getItemData();
$id = $this->clientService->create($itemData);
$this->eventDispatcher->notify(
'create.client',
new Event(
$this,
EventMessage::build()
->addDescription(__u('Client added'))
->addDetail(__u('Client'), $itemData->getName())
)
);
$this->addCustomFieldsForItem(
AclActionsInterface::CLIENT,
$id,
$this->request,
$this->customFieldService
);
return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('Client added'));
} catch (ValidationException $e) {
return $this->returnJsonResponseException($e);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notify('exception', new Event($e));
return $this->returnJsonResponseException($e);
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_CREATE)) {
return ActionResponse::error(__u('You don\'t have permission to do this operation'));
}
$this->form->validateFor(AclActionsInterface::CLIENT_CREATE);
$itemData = $this->form->getItemData();
$id = $this->clientService->create($itemData);
$this->eventDispatcher->notify(
'create.client',
new Event(
$this,
EventMessage::build(__u('Client added'))->addDetail(__u('Client'), $itemData->getName())
)
);
$this->addCustomFieldsForItem(
AclActionsInterface::CLIENT,
$id,
$this->request,
$this->customFieldService
);
return ActionResponse::ok(__u('Client added'));
}
}

View File

@@ -25,16 +25,22 @@
namespace SP\Modules\Web\Controllers\Client;
use Exception;
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\Common\Services\ServiceException;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Core\Exceptions\ValidationException;
use SP\Domain\Http\Dtos\JsonMessage;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use SP\Mvc\Controller\ItemTrait;
use function SP\__u;
/**
* Class SaveEditController
*/
@@ -46,49 +52,41 @@ final class SaveEditController extends ClientSaveBase
/**
* Saves edit action
*
* @param int $id
* @param int $id
*
* @return bool
* @throws JsonException
* @return ActionResponse
* @throws ValidationException
* @throws ServiceException
* @throws ConstraintException
* @throws QueryException
* @throws SPException
*/
public function saveEditAction(int $id): bool
#[Action(ResponseType::JSON)]
public function saveEditAction(int $id): ActionResponse
{
try {
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_EDIT)) {
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->form->validateFor(AclActionsInterface::CLIENT_EDIT, $id);
$this->clientService->update($this->form->getItemData());
$this->eventDispatcher->notify(
'edit.client',
new Event(
$this,
EventMessage::build()->addDescription(__u('Client updated'))->addDetail(__u('Client'), $id)
)
);
$this->updateCustomFieldsForItem(
AclActionsInterface::CLIENT,
$id,
$this->request,
$this->customFieldService
);
return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('Client updated'));
} catch (ValidationException $e) {
return $this->returnJsonResponseException($e);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notify('exception', new Event($e));
return $this->returnJsonResponseException($e);
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_EDIT)) {
return ActionResponse::error(__u('You don\'t have permission to do this operation'));
}
$this->form->validateFor(AclActionsInterface::CLIENT_EDIT, $id);
$this->clientService->update($this->form->getItemData());
$this->eventDispatcher->notify(
'edit.client',
new Event(
$this,
EventMessage::build()->addDescription(__u('Client updated'))->addDetail(__u('Client'), $id)
)
);
$this->updateCustomFieldsForItem(
AclActionsInterface::CLIENT,
$id,
$this->request,
$this->customFieldService
);
return ActionResponse::ok(__u('Client updated'));
}
}

View File

@@ -25,76 +25,49 @@
namespace SP\Modules\Web\Controllers\Client;
use JsonException;
use SP\Core\Application;
use SP\Domain\Auth\Services\AuthException;
use SP\Domain\Client\Ports\ClientService;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SessionTimeout;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Http\Dtos\JsonMessage;
use SP\Html\DataGrid\DataGridInterface;
use SP\Modules\Web\Controllers\ControllerBase;
use SP\Modules\Web\Controllers\Helpers\Grid\ClientGrid;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use SP\Modules\Web\Controllers\SearchGridControllerBase;
use SP\Mvc\Controller\ItemTrait;
use SP\Mvc\Controller\WebControllerHelper;
/**
* Class SearchController
*/
final class SearchController extends ControllerBase
final class SearchController extends SearchGridControllerBase
{
use ItemTrait;
use JsonTrait;
private ClientService $clientService;
private ClientGrid $clientGrid;
/**
* @throws AuthException
* @throws SessionTimeout
*/
public function __construct(
Application $application,
WebControllerHelper $webControllerHelper,
ClientService $clientService,
ClientGrid $clientGrid
Application $application,
WebControllerHelper $webControllerHelper,
private readonly ClientService $clientService,
private readonly ClientGrid $clientGrid
) {
parent::__construct($application, $webControllerHelper);
$this->checkLoggedIn();
$this->clientService = $clientService;
$this->clientGrid = $clientGrid;
}
/**
* Search action
*
* @return bool
* @throws ConstraintException
* @throws QueryException
* @throws SPException
* @throws JsonException
*/
public function searchAction(): bool
{
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_SEARCH)) {
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->addTemplate('datagrid-table', 'grid');
$this->view->assign('index', $this->request->analyzeInt('activetab', 0));
$this->view->assign('data', $this->getSearchGrid());
return $this->returnJsonResponseData(['html' => $this->render()]);
}
/**
* getSearchGrid
*
* @return DataGridInterface
* @throws ConstraintException
* @throws QueryException
* @throws SPException
*/
protected function getSearchGrid(): DataGridInterface
{
@@ -105,4 +78,9 @@ final class SearchController extends ControllerBase
$itemSearchData
);
}
protected function getAclAction(): int
{
return AclActionsInterface::CLIENT_SEARCH;
}
}

View File

@@ -24,13 +24,21 @@
namespace SP\Modules\Web\Controllers\Client;
use Exception;
use JsonException;
use SP\Core\Events\Event;
use SP\Domain\Common\Attributes\Action;
use SP\Domain\Common\Dtos\ActionResponse;
use SP\Domain\Common\Enums\ResponseType;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Http\Dtos\JsonMessage;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use function SP\__;
use function SP\__u;
/**
* Class ViewController
*
@@ -43,35 +51,29 @@ final class ViewController extends ClientViewBase
/**
* View action
*
* @param int $id
* @param int $id
*
* @return bool
* @throws JsonException
* @return ActionResponse
* @throws ServiceException
* @throws ConstraintException
* @throws QueryException
* @throws SPException
* @throws NoSuchItemException
*/
public function viewAction(int $id): bool
#[Action(ResponseType::JSON)]
public function viewAction(int $id): ActionResponse
{
try {
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_VIEW)) {
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('You don\'t have permission to do this operation')
);
}
$this->view->assign('header', __('View Client'));
$this->view->assign('isView', true);
$this->setViewData($id);
$this->eventDispatcher->notify('show.client', new Event($this));
return $this->returnJsonResponseData(['html' => $this->render()]);
} catch (Exception $e) {
processException($e);
$this->eventDispatcher->notify('exception', new Event($e));
return $this->returnJsonResponseException($e);
if (!$this->acl->checkUserAccess(AclActionsInterface::CLIENT_VIEW)) {
return ActionResponse::error(__u('You don\'t have permission to do this operation'));
}
$this->view->assign('header', __('View Client'));
$this->view->assign('isView', true);
$this->setViewData($id);
$this->eventDispatcher->notify('show.client', new Event($this));
return ActionResponse::ok('', ['html' => $this->render()]);
}
}