From ad5135add2012da1e67e3ae8ff4e6516d405553a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 4 Aug 2024 07:17:58 +0200 Subject: [PATCH] test(IT): Test account delete view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Controllers/Account/DeleteController.php | 21 ++-- lib/SP/Domain/Account/Services/Account.php | 6 +- .../Account/CreateControllerTest.php | 1 - .../Account/DeleteControllerTest.php | 95 +++++++++++++++++++ 4 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php diff --git a/app/modules/web/Controllers/Account/DeleteController.php b/app/modules/web/Controllers/Account/DeleteController.php index 6534fb86..07e1b286 100644 --- a/app/modules/web/Controllers/Account/DeleteController.php +++ b/app/modules/web/Controllers/Account/DeleteController.php @@ -28,35 +28,34 @@ use Exception; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\UI\ThemeIcons; +use SP\Domain\Account\Dtos\AccountEnrichedDto; use SP\Domain\Account\Ports\AccountService; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Modules\Web\Controllers\Helpers\Account\AccountHelper; use SP\Modules\Web\Util\ErrorUtil; use SP\Mvc\Controller\WebControllerHelper; +use function SP\__; +use function SP\processException; + /** * Class DeleteController */ final class DeleteController extends AccountControllerBase { - private AccountHelper $accountHelper; - private ThemeIcons $icons; - private AccountService $accountService; + private readonly ThemeIcons $icons; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - AccountHelper $accountHelper, - AccountService $accountService + Application $application, + WebControllerHelper $webControllerHelper, + private readonly AccountHelper $accountHelper, + private readonly AccountService $accountService ) { parent::__construct( $application, $webControllerHelper ); - $this->accountHelper = $accountHelper; - $this->accountService = $accountService; - $this->icons = $this->theme->getIcons(); } @@ -68,7 +67,7 @@ final class DeleteController extends AccountControllerBase public function deleteAction(?int $id = null): void { try { - $accountEnrichedDto = $this->accountService->getByIdEnriched($id); + $accountEnrichedDto = new AccountEnrichedDto($this->accountService->getByIdEnriched($id)); $accountEnrichedDto = $this->accountService->withUsers($accountEnrichedDto); $accountEnrichedDto = $this->accountService->withUserGroups($accountEnrichedDto); $accountEnrichedDto = $this->accountService->withTags($accountEnrichedDto); diff --git a/lib/SP/Domain/Account/Services/Account.php b/lib/SP/Domain/Account/Services/Account.php index 2a9db894..e23eb6c8 100644 --- a/lib/SP/Domain/Account/Services/Account.php +++ b/lib/SP/Domain/Account/Services/Account.php @@ -203,14 +203,14 @@ final class Account extends Service implements AccountService $userCanChangePermissions = AccountAcl::getShowPermission($userData, $userProfile); foreach ($accountUpdateBulkDto->getAccountUpdateDto() as $accountId => $accountUpdateDto) { + $changeOwner = false; + $changeUserGroup = false; + if ($userCanChangePermissions) { $account = $this->getById($accountId); $changeOwner = $this->userCanChangeOwner($userData, $userProfile, $account); $changeUserGroup = $this->userCanChangeGroup($userData, $userProfile, $account); - } else { - $changeOwner = false; - $changeUserGroup = false; } $this->addHistory($accountId); diff --git a/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php index 15a7a70b..253c466b 100644 --- a/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php +++ b/tests/SP/Modules/Web/Controllers/Account/CreateControllerTest.php @@ -77,5 +77,4 @@ class CreateControllerTest extends IntegrationTestCase { return new ProfileData(['accAdd' => true,]); } - } diff --git a/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php b/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php new file mode 100644 index 00000000..f7452828 --- /dev/null +++ b/tests/SP/Modules/Web/Controllers/Account/DeleteControllerTest.php @@ -0,0 +1,95 @@ +. + */ + +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 SP\Domain\Account\Models\AccountView; +use SP\Domain\Core\Exceptions\InvalidClassException; +use SP\Domain\User\Models\ProfileData; +use SP\Infrastructure\Database\QueryData; +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 DeleteControllerTest + */ +#[Group('integration')] +class DeleteControllerTest extends IntegrationTestCase +{ + + /** + * @throws NotFoundExceptionInterface + * @throws Exception + * @throws FileException + * @throws InvalidClassException + * @throws ContainerExceptionInterface + */ + public function testDeleteAction() + { + $definitions = $this->getModuleDefinitions(); + $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler( + static function (string $output) { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath( + '//div[@class="data-container"]//form[@name="frmaccount" and @data-action-route="account/saveDelete"]|//div[@class="item-actions"]//button' + )->extract(['id']); + + return !empty($output) && count($filter) === 3; + } + ); + + $container = $this->buildContainer( + $definitions, + $this->buildRequest('get', 'index.php', ['r' => 'account/delete']) + ); + + $this->runApp($container); + } + + protected function getUserProfile(): ProfileData + { + return new ProfileData(['accDelete' => true]); + } + + protected function getDatabaseReturn(): callable + { + return function (QueryData $queryData): QueryResult { + if ($queryData->getMapClassName() === AccountView::class) { + return new QueryResult([AccountDataGenerator::factory()->buildAccountDataView()]); + } + + return new QueryResult(); + }; + } +}