test(IT): Test account search

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-08-12 19:09:08 +02:00
parent 8e762f85de
commit fd666c9d3c
8 changed files with 135 additions and 29 deletions

View File

@@ -24,14 +24,16 @@
namespace SP\Modules\Web\Controllers\Account;
use Exception;
use SP\Core\Application;
use SP\Core\Events\Event;
use SP\Domain\Core\Exceptions\SPException;
use SP\Modules\Web\Controllers\Helpers\Account\AccountSearchHelper;
use SP\Modules\Web\Controllers\Traits\JsonTrait;
use SP\Mvc\Controller\WebControllerHelper;
use function SP\processException;
/**
* SearchController
*/
@@ -39,24 +41,17 @@ final class SearchController extends AccountControllerBase
{
use JsonTrait;
private AccountSearchHelper $accountSearchHelper;
public function __construct(
Application $application,
WebControllerHelper $webControllerHelper,
AccountSearchHelper $accountSearchHelper
Application $application,
WebControllerHelper $webControllerHelper,
private readonly AccountSearchHelper $accountSearchHelper
) {
parent::__construct(
$application,
$webControllerHelper
);
$this->accountSearchHelper = $accountSearchHelper;
parent::__construct($application, $webControllerHelper);
}
/**
* @return bool
* @throws \JsonException
* @return bool|null
* @throws SPException
*/
public function searchAction(): ?bool
{

View File

@@ -24,8 +24,6 @@
namespace SP\Modules\Web\Controllers\Helpers\Account;
use DI\DependencyException;
use DI\NotFoundException;
use SP\Core\Application;
use SP\Domain\Account\Adapters\AccountSearchItem;
use SP\Domain\Account\Dtos\AccountSearchFilterDto;
@@ -191,8 +189,6 @@ final class AccountSearchHelper extends HelperBase
/**
* Obtener los resultados de una búsqueda
*
* @throws DependencyException
* @throws NotFoundException
* @throws ConstraintException
* @throws QueryException
* @throws SPException

View File

@@ -1,11 +1,13 @@
<?php
/**
* @var SP\Html\DataGrid\DataGridInterface $data
* @var \SP\Mvc\View\TemplateInterface $this
* @var TemplateInterface $this
* @var callable $_getvar
*/
use SP\Mvc\View\Template;
use SP\Mvc\View\TemplateInterface;
use function SP\__;
if (!isset($data)) {
$data = $_getvar('data');
@@ -73,4 +75,4 @@ $pager = $data->getPager();
class="mdl-tooltip mdl-tooltip--top"><?php echo $pager->getIconLast()->getTitle(); ?></span>
<?php endif; ?>
</div>
</div>
</div>

View File

@@ -26,6 +26,7 @@ declare(strict_types=1);
namespace SP\Domain\User\Dtos;
use SP\Domain\Common\Dtos\Dto;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\User\Models\User;
use SP\Domain\User\Models\UserPreferences;
@@ -33,14 +34,14 @@ use SP\Domain\User\Models\UserPreferences;
/**
* Class UserDataDto
*/
final readonly class UserDataDto
final class UserDataDto extends Dto
{
private ?UserPreferences $preferences;
protected ?UserPreferences $preferences;
/**
* @throws SPException
*/
public function __construct(private ?User $user = null)
public function __construct(private readonly ?User $user = null)
{
$this->preferences = $this->user?->hydrate(UserPreferences::class);
}

View File

@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* sysPass
@@ -25,7 +26,6 @@ declare(strict_types=1);
namespace SP\Html\DataGrid;
use SP\Domain\Core\Exceptions\SPException;
use SP\Html\Assets\IconInterface;
use SP\Infrastructure\Database\QueryResult;
@@ -94,7 +94,7 @@ abstract class DataGridDataBase implements DataGridDataInterface
}
/**
* @throws SPException
* @param QueryResult $queryResult
*/
public function setData(QueryResult $queryResult): void
{

View File

@@ -137,6 +137,13 @@ class QueryResult
*/
public function mutateWithCallback(callable $callable): QueryResult
{
return new self(SplFixedArray::fromArray(array_map($callable, $this->data->toArray())));
$queryResult = new self(
SplFixedArray::fromArray(array_map($callable, $this->data->toArray())),
$this->affectedNumRows,
$this->lastId
);
$queryResult->totalNumRows = $this->totalNumRows;
return $queryResult;
}
}

View File

@@ -0,0 +1,103 @@
<?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\AccountSearchView;
use SP\Domain\Core\Exceptions\InvalidClassException;
use SP\Domain\User\Dtos\UserDataDto;
use SP\Infrastructure\Database\QueryResult;
use SP\Infrastructure\File\FileException;
use SP\Mvc\View\OutputHandlerInterface;
use SP\Tests\Generators\AccountDataGenerator;
use SP\Tests\Generators\UserDataGenerator;
use SP\Tests\IntegrationTestCase;
use Symfony\Component\DomCrawler\Crawler;
/**
* Class SearchControllerTest
*/
#[Group('integration')]
class SearchControllerTest extends IntegrationTestCase
{
/**
* @throws NotFoundExceptionInterface
* @throws Exception
* @throws InvalidClassException
* @throws FileException
* @throws ContainerExceptionInterface
*/
public function testSearchAction()
{
$accountSearchView = AccountDataGenerator::factory()->buildAccountSearchView();
$this->addDatabaseResolver(
AccountSearchView::class,
QueryResult::withTotalNumRows([$accountSearchView], 1)
);
$definitions = $this->getModuleDefinitions();
$definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void {
$crawler = new Crawler($output);
$filter = $crawler->filterXPath(
'//div[@id="res-content"]/div'
)->extract(['id']);
assert(!empty($output));
assert(count($filter) === 4);
$this->assertTrue(true);
});
$container = $this->buildContainer(
$definitions,
$this->buildRequest(
'post',
'index.php',
['r' => 'account/search'],
['search' => $accountSearchView->getName()]
)
);
$this->expectOutputRegex(
'/\{"status":0,"description":null,"data":\{"html":".*"\},"messages":\[\]\}/'
);
$this->runApp($container);
}
protected function getUserDataDto(): UserDataDto
{
$userPreferences = UserDataGenerator::factory()->buildUserPreferencesData()->mutate(['topNavbar' => true]);
return parent::getUserDataDto()->set('preferences', $userPreferences);
}
}

View File

@@ -46,8 +46,10 @@ final readonly class OutputHandlerStub implements OutputHandlerInterface
ob_start();
$callback();
($this->outputChecker)(ob_get_clean());
$out = ob_get_clean();
return '';
($this->outputChecker)($out);
return $out;
}
}