mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-03 07:04:07 +01:00
512 lines
16 KiB
PHP
512 lines
16 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\Plugin\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 SP\Domain\Common\Models\Simple;
|
|
use SP\Domain\Core\Dtos\ItemSearchDto;
|
|
use SP\Domain\Core\Exceptions\ConstraintException;
|
|
use SP\Domain\Core\Exceptions\QueryException;
|
|
use SP\Domain\Core\Exceptions\SPException;
|
|
use SP\Domain\Database\Ports\DatabaseInterface;
|
|
use SP\Domain\Plugin\Models\Plugin as PluginModel;
|
|
use SP\Infrastructure\Database\QueryData;
|
|
use SP\Infrastructure\Database\QueryResult;
|
|
use SP\Infrastructure\Plugin\Repositories\Plugin;
|
|
use SP\Tests\Generators\PluginGenerator;
|
|
use SP\Tests\UnitaryTestCase;
|
|
|
|
/**
|
|
* Class PluginTest
|
|
*/
|
|
#[Group('unitary')]
|
|
class PluginTest extends UnitaryTestCase
|
|
{
|
|
|
|
private Plugin $plugin;
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testToggleAvailableByName()
|
|
{
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 3
|
|
&& $params['available'] === true
|
|
&& $params['enabled'] === 0
|
|
&& $params['name'] === 'test_name'
|
|
&& 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->plugin->toggleAvailableByName('test_name', true);
|
|
|
|
$this->assertEquals(1, $out);
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testToggleEnabledByName()
|
|
{
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 2
|
|
&& $params['enabled'] === true
|
|
&& $params['name'] === 'test_name'
|
|
&& 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->plugin->toggleEnabledByName('test_name', true);
|
|
|
|
$this->assertEquals(1, $out);
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testUpdate()
|
|
{
|
|
$plugin = PluginGenerator::factory()->buildPlugin();
|
|
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) use ($plugin) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 5
|
|
&& $params['id'] === $plugin->getId()
|
|
&& $params['name'] === $plugin->getName()
|
|
&& $params['data'] === $plugin->getData()
|
|
&& $params['enabled'] === $plugin->getEnabled()
|
|
&& $params['versionLevel'] === $plugin->getVersionLevel()
|
|
&& 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->plugin->update($plugin);
|
|
|
|
$this->assertEquals(1, $out);
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testToggleEnabled()
|
|
{
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 2
|
|
&& $params['enabled'] === true
|
|
&& $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->plugin->toggleEnabled(100, true);
|
|
|
|
$this->assertEquals(1, $out);
|
|
}
|
|
|
|
public function testGetEnabled()
|
|
{
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 0
|
|
&& $arg->getMapClassName() === PluginModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback);
|
|
|
|
$this->plugin->getEnabled();
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws SPException
|
|
* @throws QueryException
|
|
*/
|
|
public function testDeleteByIdBatch()
|
|
{
|
|
$ids = [self::$faker->randomNumber(), self::$faker->randomNumber(), self::$faker->randomNumber()];
|
|
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) use ($ids) {
|
|
$query = $arg->getQuery();
|
|
$values = $query->getBindValues();
|
|
|
|
return count($values) === 3
|
|
&& array_shift($values) === array_shift($ids)
|
|
&& array_shift($values) === array_shift($ids)
|
|
&& array_shift($values) === array_shift($ids)
|
|
&& $arg->getMapClassName() === Simple::class
|
|
&& is_a($query, DeleteInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback);
|
|
|
|
$this->plugin->deleteByIdBatch($ids);
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testToggleAvailable()
|
|
{
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 3
|
|
&& $params['available'] === true
|
|
&& $params['enabled'] === 0
|
|
&& $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->plugin->toggleAvailable(100, true);
|
|
|
|
$this->assertEquals(1, $out);
|
|
}
|
|
|
|
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) === 1
|
|
&& $params['name'] === $searchStringLike
|
|
&& $arg->getMapClassName() === PluginModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback, true);
|
|
|
|
$this->plugin->search($item);
|
|
}
|
|
|
|
public function testSearchWithEmptyString()
|
|
{
|
|
$item = new ItemSearchDto();
|
|
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) use ($item) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
$searchStringLike = '%' . $item->getSeachString() . '%';
|
|
|
|
return count($params) === 0
|
|
&& $arg->getMapClassName() === PluginModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback, true);
|
|
|
|
$this->plugin->search($item);
|
|
}
|
|
|
|
public function testGetAll()
|
|
{
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
|
|
return $arg->getMapClassName() === PluginModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback);
|
|
|
|
$this->plugin->getAll();
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testResetById()
|
|
{
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 2
|
|
&& $params['data'] === null
|
|
&& $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->plugin->resetById(100);
|
|
|
|
$this->assertEquals(1, $out);
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testCreate()
|
|
{
|
|
$plugin = PluginGenerator::factory()->buildPlugin();
|
|
|
|
$callbackCreate = new Callback(
|
|
static function (QueryData $arg) use ($plugin) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 4
|
|
&& $params['name'] === $plugin->getName()
|
|
&& $params['data'] === $plugin->getData()
|
|
&& $params['enabled'] === $plugin->getEnabled()
|
|
&& $params['versionLevel'] === $plugin->getVersionLevel()
|
|
&& is_a($query, InsertInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::exactly(1))
|
|
->method('runQuery')
|
|
->with($callbackCreate)
|
|
->willReturn(new QueryResult([]));
|
|
|
|
$this->plugin->create($plugin);
|
|
}
|
|
|
|
public function testGetByName()
|
|
{
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 1
|
|
&& $params['name'] === 'test_name'
|
|
&& $arg->getMapClassName() === PluginModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback);
|
|
|
|
$this->plugin->getByName('test_name');
|
|
}
|
|
|
|
public function testGetByIdBatch()
|
|
{
|
|
$ids = [self::$faker->randomNumber(), self::$faker->randomNumber(), self::$faker->randomNumber()];
|
|
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) use ($ids) {
|
|
$query = $arg->getQuery();
|
|
$values = $query->getBindValues();
|
|
|
|
return count($values) === 3
|
|
&& array_shift($values) === array_shift($ids)
|
|
&& array_shift($values) === array_shift($ids)
|
|
&& array_shift($values) === array_shift($ids)
|
|
&& $arg->getMapClassName() === PluginModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback);
|
|
|
|
$this->plugin->getByIdBatch($ids);
|
|
}
|
|
|
|
public function testGetById()
|
|
{
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) {
|
|
$query = $arg->getQuery();
|
|
$params = $query->getBindValues();
|
|
|
|
return count($params) === 1
|
|
&& $params['id'] === 100
|
|
&& $arg->getMapClassName() === PluginModel::class
|
|
&& is_a($query, SelectInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database
|
|
->expects(self::once())
|
|
->method('runQuery')
|
|
->with($callback);
|
|
|
|
$this->plugin->getById(100);
|
|
}
|
|
|
|
/**
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function testDelete()
|
|
{
|
|
$id = self::$faker->randomNumber();
|
|
|
|
$callback = new Callback(
|
|
static function (QueryData $arg) use ($id) {
|
|
$query = $arg->getQuery();
|
|
|
|
return $query->getBindValues()['id'] === $id
|
|
&& is_a($query, DeleteInterface::class)
|
|
&& !empty($query->getStatement());
|
|
}
|
|
);
|
|
|
|
$this->database->expects(self::once())->method('runQuery')->with($callback);
|
|
|
|
$this->plugin->delete($id);
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->database = $this->createMock(DatabaseInterface::class);
|
|
$queryFactory = new QueryFactory('mysql');
|
|
|
|
$this->plugin = new Plugin(
|
|
$this->database,
|
|
$this->context,
|
|
$this->application->getEventDispatcher(),
|
|
$queryFactory,
|
|
);
|
|
}
|
|
}
|