mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-03 15:14:08 +01:00
235 lines
7.3 KiB
PHP
235 lines
7.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
/*
|
|
* 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/>.
|
|
*/
|
|
|
|
namespace SP\Tests\Infrastructure\Security\Repositories;
|
|
|
|
use Aura\SqlQuery\Common\DeleteInterface;
|
|
use Aura\SqlQuery\Common\InsertInterface;
|
|
use Aura\SqlQuery\Common\SelectInterface;
|
|
use Aura\SqlQuery\Common\UpdateInterface;
|
|
use Aura\SqlQuery\QueryFactory;
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use PHPUnit\Framework\Constraint\Callback;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use SP\Domain\Core\Dtos\ItemSearchDto;
|
|
use SP\Domain\Core\Exceptions\ConstraintException;
|
|
use SP\Domain\Core\Exceptions\QueryException;
|
|
use SP\Domain\Database\Ports\DatabaseInterface;
|
|
use SP\Domain\Security\Models\Track as TrackModel;
|
|
use SP\Infrastructure\Database\QueryData;
|
|
use SP\Infrastructure\Database\QueryResult;
|
|
use SP\Infrastructure\Security\Repositories\Track;
|
|
use SP\Tests\Generators\TrackGenerator;
|
|
use SP\Tests\UnitaryTestCase;
|
|
|
|
/**
|
|
* Class TrackTest
|
|
*/
|
|
#[Group('unitary')]
|
|
class TrackTest extends UnitaryTestCase
|
|
{
|
|
|
|
private DatabaseInterface|MockObject $database;
|
|
private Track $track;
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testClear()
|
|
{
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
|
|
return count($query->getBindValues()) === 0
|
|
&& is_a($query, DeleteInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database->expects(self::once())->method('runQuery')->with($callback);
|
|
|
|
$this->track->clear();
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testUnlock()
|
|
{
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 1
|
|
&& $params['id'] === 100
|
|
&& is_a($query, UpdateInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::exactly(1))
|
|
->method('runQuery')
|
|
->with($callbackCreate)
|
|
->willReturn(new QueryResult(null, 1));
|
|
|
|
$out = $this->track->unlock(100);
|
|
|
|
$this->assertEquals(1, $out);
|
|
}
|
|
|
|
public function testGetTracksForClientFromTime()
|
|
{
|
|
$track = TrackGenerator::factory()->buildTrack();
|
|
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) use ($track) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 4
|
|
&& $params['time'] == $track->getTime()
|
|
&& $params['ipv4'] == $track->getIpv4()
|
|
&& $params['ipv6'] == $track->getIpv6()
|
|
&& $params['source'] == $track->getSource()
|
|
&& $arg->getMapClassName() === TrackModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback);
|
|
|
|
$this->track->getTracksForClientFromTime($track);
|
|
}
|
|
|
|
public function testSearch()
|
|
{
|
|
$item = new ItemSearchDto(self::$faker->name);
|
|
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) use ($item) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
$searchStringLike = '%' . $item->getSeachString() . '%';
|
|
|
|
return count($params) === 2
|
|
&& $params['time'] === 1000
|
|
&& $params['source'] === $searchStringLike
|
|
&& $arg->getMapClassName() === TrackModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback, true);
|
|
|
|
$this->track->search($item, 1000);
|
|
}
|
|
|
|
public function testSearchWithEmptyString()
|
|
{
|
|
$item = new ItemSearchDto();
|
|
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) use ($item) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 1
|
|
&& $params['time'] === 1000
|
|
&& $arg->getMapClassName() === TrackModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback, true);
|
|
|
|
$this->track->search($item, 1000);
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testAdd()
|
|
{
|
|
$track = TrackGenerator::factory()->buildTrack();
|
|
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) use ($track) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 5
|
|
&& $params['userId'] === $track->getUserId()
|
|
&& $params['source'] === $track->getSource()
|
|
&& $params['timeUnlock'] === $track->getTimeUnlock()
|
|
&& $params['ipv4'] === $track->getIpv4()
|
|
&& $params['ipv6'] === $track->getIpv6()
|
|
&& is_a($query, InsertInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::exactly(1))
|
|
->method('runQuery')
|
|
->with($callbackCreate)
|
|
->willReturn(new QueryResult([]));
|
|
|
|
$this->track->add($track);
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->database = $this->createMock(DatabaseInterface::class);
|
|
$queryFactory = new QueryFactory('mysql');
|
|
|
|
$this->track = new Track(
|
|
$this->database,
|
|
$this->context,
|
|
$this->application->getEventDispatcher(),
|
|
$queryFactory,
|
|
);
|
|
}
|
|
}
|