test(IT): Test account delete view

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-08-04 07:17:58 +02:00
parent 630aa0d4b3
commit ad5135add2
4 changed files with 108 additions and 15 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -77,5 +77,4 @@ class CreateControllerTest extends IntegrationTestCase
{
return new ProfileData(['accAdd' => true,]);
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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();
};
}
}