mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-13 03:46:58 +01:00
* [ADD] Unit testing. Work in progress
* [MOD] Code refactoring
This commit is contained in:
@@ -244,7 +244,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf
|
||||
|
||||
$groupData = $form->getItemData();
|
||||
|
||||
$id = $this->userGroupService->create($groupData, $groupData->getUsers());
|
||||
$id = $this->userGroupService->create($groupData);
|
||||
|
||||
$this->addCustomFieldsForItem(Acl::GROUP, $id, $this->request);
|
||||
|
||||
|
||||
@@ -40,11 +40,11 @@ class UserGroupData extends DataModelBase implements DataModelInterface
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name = '';
|
||||
public $name;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description = '';
|
||||
public $description;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
||||
@@ -94,7 +94,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
*
|
||||
* @param $id int
|
||||
*
|
||||
* @return array
|
||||
* @return QueryResult
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
@@ -117,7 +117,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
$queryData->setQuery($query);
|
||||
$queryData->addParams(array_fill(0, 4, (int)$id));
|
||||
|
||||
return $this->db->doSelect($queryData)->getDataAsArray();
|
||||
return $this->db->doSelect($queryData);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +125,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
*
|
||||
* @param $id int
|
||||
*
|
||||
* @return array
|
||||
* @return QueryResult
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
@@ -152,7 +152,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
$queryData->setQuery($query);
|
||||
$queryData->addParams([(int)$id, (int)$id]);
|
||||
|
||||
return $this->db->doSelect($queryData)->getDataAsArray();
|
||||
return $this->db->doSelect($queryData);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,7 +160,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return UserGroupData
|
||||
* @return QueryResult
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
@@ -171,7 +171,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
$queryData->setQuery('SELECT id, `name`, description FROM UserGroup WHERE id = ? LIMIT 1');
|
||||
$queryData->addParam($id);
|
||||
|
||||
return $this->db->doSelect($queryData)->getData();
|
||||
return $this->db->doSelect($queryData);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,7 +179,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return UserGroupData
|
||||
* @return QueryResult
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
@@ -190,13 +190,13 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
$queryData->setQuery('SELECT id, `name`, description FROM UserGroup WHERE name = ? LIMIT 1');
|
||||
$queryData->addParam($name);
|
||||
|
||||
return $this->db->doSelect($queryData)->getData();
|
||||
return $this->db->doSelect($queryData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the items
|
||||
*
|
||||
* @return UserGroupData[]
|
||||
* @return QueryResult
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
@@ -206,7 +206,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface
|
||||
$queryData->setMapClassName(UserGroupData::class);
|
||||
$queryData->setQuery('SELECT id, `name`, description FROM UserGroup ORDER BY name');
|
||||
|
||||
return $this->db->doSelect($queryData)->getDataAsArray();
|
||||
return $this->db->doSelect($queryData);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace SP\Services\UserGroup;
|
||||
use SP\Core\Exceptions\SPException;
|
||||
use SP\DataModel\ItemSearchData;
|
||||
use SP\DataModel\UserGroupData;
|
||||
use SP\Repositories\NoSuchItemException;
|
||||
use SP\Repositories\UserGroup\UserGroupRepository;
|
||||
use SP\Services\Service;
|
||||
use SP\Services\ServiceException;
|
||||
@@ -69,10 +70,17 @@ class UserGroupService extends Service
|
||||
* @return UserGroupData
|
||||
* @throws \SP\Core\Exceptions\ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
* @throws NoSuchItemException
|
||||
*/
|
||||
public function getById($id)
|
||||
{
|
||||
return $this->userGroupRepository->getById($id);
|
||||
$result = $this->userGroupRepository->getById($id);
|
||||
|
||||
if ($result->getNumRows() === 0) {
|
||||
throw new NoSuchItemException(__u('Grupo no encontrado'), NoSuchItemException::INFO);
|
||||
}
|
||||
|
||||
return $result->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,7 +92,7 @@ class UserGroupService extends Service
|
||||
public function delete($id)
|
||||
{
|
||||
if ($this->userGroupRepository->delete($id) === 0) {
|
||||
throw new ServiceException(__u('Grupo no encontrado'), ServiceException::INFO);
|
||||
throw new NoSuchItemException(__u('Grupo no encontrado'), NoSuchItemException::INFO);
|
||||
}
|
||||
|
||||
return $this;
|
||||
@@ -109,35 +117,35 @@ class UserGroupService extends Service
|
||||
|
||||
/**
|
||||
* @param UserGroupData $itemData
|
||||
* @param array $users
|
||||
*
|
||||
* @return int
|
||||
* @throws SPException
|
||||
* @throws \SP\Core\Exceptions\ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
* @throws ServiceException
|
||||
*/
|
||||
public function create($itemData, array $users = [])
|
||||
public function create($itemData)
|
||||
{
|
||||
$userGroupId = $this->userGroupRepository->create($itemData);
|
||||
return $this->transactionAware(function () use($itemData) {
|
||||
$id = $this->userGroupRepository->create($itemData);
|
||||
|
||||
if (count($users) > 0) {
|
||||
$this->userToUserGroupService->add($userGroupId, $users);
|
||||
}
|
||||
if (count($itemData->getUsers()) > 0) {
|
||||
$this->userToUserGroupService->add($id, $itemData->getUsers());
|
||||
}
|
||||
|
||||
return $userGroupId;
|
||||
return $id;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserGroupData $itemData
|
||||
*
|
||||
* @throws SPException
|
||||
* @throws \SP\Core\Exceptions\ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
* @throws ServiceException
|
||||
*/
|
||||
public function update($itemData)
|
||||
{
|
||||
$this->userGroupRepository->update($itemData);
|
||||
$this->userToUserGroupService->update($itemData->getId(), $itemData->getUsers());
|
||||
$this->transactionAware(function () use ($itemData) {
|
||||
$this->userGroupRepository->update($itemData);
|
||||
|
||||
$this->userToUserGroupService->update($itemData->getId(), $itemData->getUsers());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,7 +157,7 @@ class UserGroupService extends Service
|
||||
*/
|
||||
public function getAllBasic()
|
||||
{
|
||||
return $this->userGroupRepository->getAll();
|
||||
return $this->userGroupRepository->getAll()->getDataAsArray();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,12 +166,19 @@ class UserGroupService extends Service
|
||||
* @param string $name
|
||||
*
|
||||
* @return UserGroupData
|
||||
* @throws NoSuchItemException
|
||||
* @throws \SP\Core\Exceptions\ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function getByName($name)
|
||||
{
|
||||
return $this->userGroupRepository->getByName($name);
|
||||
$result = $this->userGroupRepository->getByName($name);
|
||||
|
||||
if ($result->getNumRows() === 0) {
|
||||
throw new NoSuchItemException(__u('Grupo no encontrado'), NoSuchItemException::INFO);
|
||||
}
|
||||
|
||||
return $result->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,7 +192,7 @@ class UserGroupService extends Service
|
||||
*/
|
||||
public function getUsage($id)
|
||||
{
|
||||
return $this->userGroupRepository->getUsage($id);
|
||||
return $this->userGroupRepository->getUsage($id)->getDataAsArray();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,7 +206,7 @@ class UserGroupService extends Service
|
||||
*/
|
||||
public function getUsageByUsers($id)
|
||||
{
|
||||
return $this->userGroupRepository->getUsageByUsers($id);
|
||||
return $this->userGroupRepository->getUsageByUsers($id)->getDataAsArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,7 +58,7 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
{
|
||||
$dic = setupContext();
|
||||
|
||||
self::$dataset = 'syspass.xml';
|
||||
self::$dataset = 'syspass_userGroup.xml';
|
||||
|
||||
// Datos de conexión a la BBDD
|
||||
self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
|
||||
@@ -75,9 +75,11 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testGetUsageByUsers()
|
||||
{
|
||||
$this->assertCount(2, self::$repository->getUsageByUsers(1));
|
||||
$this->assertCount(5, self::$repository->getUsageByUsers(2));
|
||||
$this->assertCount(0, self::$repository->getUsageByUsers(3));
|
||||
$this->assertEquals(2, self::$repository->getUsageByUsers(1)->getNumRows());
|
||||
|
||||
$this->assertEquals(5, self::$repository->getUsageByUsers(2)->getNumRows());
|
||||
|
||||
$this->assertEquals(0, self::$repository->getUsageByUsers(3)->getNumRows());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +91,9 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
public function testCheckInUse()
|
||||
{
|
||||
$this->assertTrue(self::$repository->checkInUse(1));
|
||||
|
||||
$this->assertTrue(self::$repository->checkInUse(2));
|
||||
|
||||
$this->assertFalse(self::$repository->checkInUse(5));
|
||||
}
|
||||
|
||||
@@ -101,29 +105,52 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testGetByName()
|
||||
{
|
||||
$group = self::$repository->getByName('Demo');
|
||||
$result = self::$repository->getByName('Demo');
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $group);
|
||||
$this->assertEquals('Demo', $group->getName());
|
||||
$this->assertEmpty($group->getDescription());
|
||||
$this->assertEquals(1, $result->getNumRows());
|
||||
|
||||
$this->assertNull(self::$repository->getByName('Prueba'));
|
||||
/** @var UserGroupData $data */
|
||||
$data = $result->getData();
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data);
|
||||
$this->assertEquals('Demo', $data->getName());
|
||||
$this->assertEmpty($data->getDescription());
|
||||
|
||||
$this->assertEquals(0, self::$repository->getByName('Prueba')->getNumRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* Comprobar la eliminación de grupos en lote
|
||||
*
|
||||
* @throws \SP\Core\Exceptions\ConstraintException
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
public function testDeleteByIdBatch()
|
||||
{
|
||||
$this->assertEquals(2, self::$repository->deleteByIdBatch([4, 5]));
|
||||
|
||||
$this->assertEquals(3, $this->conn->getRowCount('UserGroup'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
public function testDeleteByIdBatchUsed()
|
||||
{
|
||||
// Se lanza excepción en caso de restricción relacional
|
||||
$this->expectException(ConstraintException::class);
|
||||
|
||||
$result = self::$repository->deleteByIdBatch([1, 2, 3]);
|
||||
self::$repository->deleteByIdBatch([1, 2]);
|
||||
}
|
||||
|
||||
$this->assertEquals(1, $result);
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
*/
|
||||
public function testDeleteByIdBatchUnknown()
|
||||
{
|
||||
$this->assertEquals(2, self::$repository->deleteByIdBatch([4, 5, 10]));
|
||||
|
||||
$this->assertEquals(3, $this->conn->getRowCount('UserGroup'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,18 +162,18 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
$userGroupData = new UserGroupData();
|
||||
$userGroupData->setId(2);
|
||||
$userGroupData->setName('Grupo demo');
|
||||
$userGroupData->setDescription('Grupo para usuarios demo');
|
||||
$data = new UserGroupData();
|
||||
$data->setId(2);
|
||||
$data->setName('Grupo demo');
|
||||
$data->setDescription('Grupo para usuarios demo');
|
||||
|
||||
$this->assertEquals(1, self::$repository->update($userGroupData));
|
||||
$this->assertEquals(1, self::$repository->update($data));
|
||||
|
||||
$this->expectException(DuplicatedItemException::class);
|
||||
|
||||
$userGroupData->setName('Admins');
|
||||
$data->setName('Admins');
|
||||
|
||||
self::$repository->update($userGroupData);
|
||||
self::$repository->update($data);
|
||||
|
||||
}
|
||||
|
||||
@@ -158,13 +185,18 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testGetById()
|
||||
{
|
||||
$group = self::$repository->getById(2);
|
||||
$result = self::$repository->getById(2);
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $group);
|
||||
$this->assertEquals('Demo', $group->getName());
|
||||
$this->assertEmpty($group->getDescription());
|
||||
$this->assertEquals(1, $result->getNumRows());
|
||||
|
||||
$this->assertNull(self::$repository->getById(4));
|
||||
/** @var UserGroupData $data */
|
||||
$data = $result->getData();
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data);
|
||||
$this->assertEquals('Demo', $data->getName());
|
||||
$this->assertEmpty($data->getDescription());
|
||||
|
||||
$this->assertEquals(0, self::$repository->getById(10)->getNumRows());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,17 +208,30 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$userGroupData = new UserGroupData();
|
||||
$userGroupData->setName('Grupo Prueba');
|
||||
$userGroupData->setDescription('Grupo de prueba para usuarios');
|
||||
$data = new UserGroupData();
|
||||
$data->setId(6);
|
||||
$data->setName('Grupo Prueba');
|
||||
$data->setDescription('Grupo de prueba para usuarios');
|
||||
|
||||
$this->assertEquals(4, self::$repository->create($userGroupData));
|
||||
$this->assertEquals($data->getId(), self::$repository->create($data));
|
||||
|
||||
$this->assertEquals($data, self::$repository->getById($data->getId())->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws QueryException
|
||||
* @throws \SP\Core\Exceptions\SPException
|
||||
*/
|
||||
public function testCreateDuplicated()
|
||||
{
|
||||
$data = new UserGroupData();
|
||||
$data->setName('Admins');
|
||||
$data->setDescription('Group for demo users');
|
||||
|
||||
$this->expectException(DuplicatedItemException::class);
|
||||
|
||||
$userGroupData->setName('Admins');
|
||||
|
||||
self::$repository->create($userGroupData);
|
||||
self::$repository->create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,13 +242,22 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testGetAll()
|
||||
{
|
||||
$groups = self::$repository->getAll();
|
||||
$result = self::$repository->getAll();
|
||||
|
||||
$this->assertCount(3, $groups);
|
||||
$this->assertInstanceOf(UserGroupData::class, $groups[0]);
|
||||
$this->assertEquals('Admins', $groups[0]->getName());
|
||||
$this->assertInstanceOf(UserGroupData::class, $groups[1]);
|
||||
$this->assertEquals('Demo', $groups[1]->getName());
|
||||
$this->assertEquals(5, $result->getNumRows());
|
||||
|
||||
/** @var UserGroupData[] $data */
|
||||
$data = $result->getDataAsArray();
|
||||
|
||||
$this->assertCount(5, $data);
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data[0]);
|
||||
$this->assertEquals('Admins', $data[0]->getName());
|
||||
$this->assertEquals('sysPass Admins', $data[0]->getDescription());
|
||||
$this->assertInstanceOf(UserGroupData::class, $data[1]);
|
||||
|
||||
$this->assertEquals('Demo', $data[1]->getName());
|
||||
$this->assertEmpty($data[1]->getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,15 +267,23 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testDelete()
|
||||
{
|
||||
$result = self::$repository->delete(3);
|
||||
$this->assertEquals(1, self::$repository->delete(3));
|
||||
|
||||
$this->assertEquals(1, $result);
|
||||
$this->assertEquals(2, $this->conn->getRowCount('UserGroup'));
|
||||
$this->assertEquals(4, $this->conn->getRowCount('UserGroup'));
|
||||
|
||||
$this->assertEquals(0, self::$repository->delete(10));
|
||||
}
|
||||
|
||||
/**
|
||||
* Comprobar la eliminación de grupos
|
||||
*
|
||||
* @throws \SP\Core\Exceptions\SPException
|
||||
*/
|
||||
public function testDeleteUsed()
|
||||
{
|
||||
$this->expectException(ConstraintException::class);
|
||||
|
||||
self::$repository->delete(1);
|
||||
self::$repository->delete(2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,8 +294,9 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testGetUsage()
|
||||
{
|
||||
$this->assertCount(7, self::$repository->getUsage(2));
|
||||
$this->assertCount(0, self::$repository->getUsage(3));
|
||||
$this->assertEquals(7, self::$repository->getUsage(2)->getNumRows());
|
||||
|
||||
$this->assertEquals(0, self::$repository->getUsage(3)->getNumRows());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,13 +307,17 @@ class UserGroupRepositoryTestCase extends DatabaseTestCase
|
||||
*/
|
||||
public function testGetByIdBatch()
|
||||
{
|
||||
$groups = self::$repository->getByIdBatch([1, 2, 5]);
|
||||
$data = self::$repository->getByIdBatch([1, 2, 10]);
|
||||
|
||||
$this->assertCount(2, $groups);
|
||||
$this->assertInstanceOf(UserGroupData::class, $groups[0]);
|
||||
$this->assertEquals(1, $groups[0]->getId());
|
||||
$this->assertEquals('Admins', $groups[0]->getName());
|
||||
$this->assertInstanceOf(UserGroupData::class, $groups[1]);
|
||||
$this->assertCount(2, $data);
|
||||
$this->assertInstanceOf(UserGroupData::class, $data[0]);
|
||||
$this->assertEquals(1, $data[0]->getId());
|
||||
$this->assertEquals('Admins', $data[0]->getName());
|
||||
$this->assertEquals('sysPass Admins', $data[0]->getDescription());
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data[1]);
|
||||
$this->assertEquals('Demo', $data[1]->getName());
|
||||
$this->assertEmpty($data[1]->getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -53,11 +53,6 @@ class UserServiceTest extends DatabaseTestCase
|
||||
*/
|
||||
private static $configData;
|
||||
|
||||
/**
|
||||
* @var \Closure
|
||||
*/
|
||||
private static $getUserLoginResponse;
|
||||
|
||||
/**
|
||||
* @var UserService
|
||||
*/
|
||||
@@ -82,10 +77,6 @@ class UserServiceTest extends DatabaseTestCase
|
||||
self::$service = $dic->get(UserService::class);
|
||||
|
||||
self::$configData = $dic->get(ConfigData::class);
|
||||
|
||||
self::$getUserLoginResponse = function ($login) use ($dic) {
|
||||
return UserService::mapUserLoginResponse($dic->get(UserService::class)->getByLogin($login));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
316
test/SP/Services/UserGroup/UserGroupServiceTest.php
Normal file
316
test/SP/Services/UserGroup/UserGroupServiceTest.php
Normal file
@@ -0,0 +1,316 @@
|
||||
<?php
|
||||
/**
|
||||
* sysPass
|
||||
*
|
||||
* @author nuxsmin
|
||||
* @link https://syspass.org
|
||||
* @copyright 2012-2018, 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\SP\Services\UserGroup;
|
||||
|
||||
use SP\Core\Exceptions\ConstraintException;
|
||||
use SP\DataModel\ItemSearchData;
|
||||
use SP\DataModel\UserGroupData;
|
||||
use SP\Repositories\DuplicatedItemException;
|
||||
use SP\Repositories\NoSuchItemException;
|
||||
use SP\Services\ServiceException;
|
||||
use SP\Services\UserGroup\UserGroupService;
|
||||
use SP\Storage\Database\DatabaseConnectionData;
|
||||
use SP\Test\DatabaseTestCase;
|
||||
use function SP\Test\setupContext;
|
||||
|
||||
/**
|
||||
* Class UserGroupServiceTest
|
||||
*
|
||||
* @package SP\Tests\SP\Services\UserGroup
|
||||
*/
|
||||
class UserGroupServiceTest extends DatabaseTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var UserGroupService
|
||||
*/
|
||||
private static $service;
|
||||
|
||||
/**
|
||||
* @throws \DI\NotFoundException
|
||||
* @throws \SP\Core\Context\ContextException
|
||||
* @throws \DI\DependencyException
|
||||
* @throws \SP\Core\Exceptions\SPException
|
||||
*/
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
$dic = setupContext();
|
||||
|
||||
self::$dataset = 'syspass_userGroup.xml';
|
||||
|
||||
// Datos de conexión a la BBDD
|
||||
self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class);
|
||||
|
||||
// Inicializar el servicio
|
||||
self::$service = $dic->get(UserGroupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \SP\Core\Exceptions\ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function testGetAllBasic()
|
||||
{
|
||||
$data = self::$service->getAllBasic();
|
||||
|
||||
$this->assertCount(5, $data);
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data[0]);
|
||||
$this->assertEquals('Admins', $data[0]->getName());
|
||||
$this->assertEquals('sysPass Admins', $data[0]->getDescription());
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data[1]);
|
||||
$this->assertEquals('Demo', $data[1]->getName());
|
||||
$this->assertEmpty($data[1]->getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \SP\Core\Exceptions\SPException
|
||||
*/
|
||||
public function testDelete()
|
||||
{
|
||||
self::$service->delete(3);
|
||||
|
||||
$this->assertEquals(4, $this->conn->getRowCount('UserGroup'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \SP\Core\Exceptions\SPException
|
||||
*/
|
||||
public function testDeleteUsed()
|
||||
{
|
||||
$this->expectException(ConstraintException::class);
|
||||
|
||||
self::$service->delete(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \SP\Core\Exceptions\SPException
|
||||
*/
|
||||
public function testDeleteUnknown()
|
||||
{
|
||||
$this->expectException(NoSuchItemException::class);
|
||||
|
||||
self::$service->delete(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
* @throws \SP\Services\ServiceException
|
||||
*/
|
||||
public function testDeleteByIdBatch()
|
||||
{
|
||||
$this->assertEquals(2, self::$service->deleteByIdBatch([4, 5]));
|
||||
|
||||
$this->assertEquals(3, $this->conn->getRowCount('UserGroup'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
* @throws \SP\Services\ServiceException
|
||||
*/
|
||||
public function testDeleteByIdBatchUsed()
|
||||
{
|
||||
// Se lanza excepción en caso de restricción relacional
|
||||
$this->expectException(ConstraintException::class);
|
||||
|
||||
self::$service->deleteByIdBatch([1, 2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
* @throws \SP\Services\ServiceException
|
||||
*/
|
||||
public function testDeleteByIdBatchUnknown()
|
||||
{
|
||||
$this->expectException(ServiceException::class);
|
||||
|
||||
self::$service->deleteByIdBatch([4, 5, 10]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
* @throws \SP\Core\Exceptions\SPException
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
$data = new UserGroupData();
|
||||
$data->setId(2);
|
||||
$data->setName('Test group');
|
||||
$data->setDescription('Group for demo users');
|
||||
|
||||
self::$service->update($data);
|
||||
|
||||
$this->assertEquals($data, self::$service->getById(2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ServiceException
|
||||
*/
|
||||
public function testUpdateDuplicated()
|
||||
{
|
||||
$data = new UserGroupData();
|
||||
$data->setId(2);
|
||||
$data->setName('Admins');
|
||||
|
||||
$this->expectException(DuplicatedItemException::class);
|
||||
|
||||
self::$service->update($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function testGetUsage()
|
||||
{
|
||||
$this->assertCount(7, self::$service->getUsage(2));
|
||||
|
||||
$this->assertCount(0, self::$service->getUsage(3));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws NoSuchItemException
|
||||
* @throws ServiceException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$data = new UserGroupData();
|
||||
$data->setId(6);
|
||||
$data->setName('Test group');
|
||||
$data->setDescription('Group for demo users');
|
||||
|
||||
$this->assertEquals($data->getId(), self::$service->create($data));
|
||||
|
||||
$this->assertEquals($data, self::$service->getById($data->getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ServiceException
|
||||
*/
|
||||
public function testCreateDuplicated()
|
||||
{
|
||||
$data = new UserGroupData();
|
||||
$data->setName('Admins');
|
||||
$data->setDescription('Group for demo users');
|
||||
|
||||
$this->expectException(DuplicatedItemException::class);
|
||||
|
||||
self::$service->create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function testSearch()
|
||||
{
|
||||
$itemSearchData = new ItemSearchData();
|
||||
$itemSearchData->setLimitCount(10);
|
||||
$itemSearchData->setSeachString('Demo');
|
||||
|
||||
$result = self::$service->search($itemSearchData);
|
||||
$data = $result->getDataAsArray();
|
||||
|
||||
$this->assertEquals(1, $result->getNumRows());
|
||||
$this->assertCount(1, $data);
|
||||
$this->assertInstanceOf(UserGroupData::class, $data[0]);
|
||||
$this->assertEquals(2, $data[0]->id);
|
||||
$this->assertEquals('Demo', $data[0]->name);
|
||||
$this->assertEmpty($data[0]->description);
|
||||
|
||||
$itemSearchData = new ItemSearchData();
|
||||
$itemSearchData->setLimitCount(10);
|
||||
$itemSearchData->setSeachString('test');
|
||||
|
||||
$result = self::$service->search($itemSearchData);
|
||||
|
||||
$this->assertEquals(2, $result->getNumRows());
|
||||
|
||||
$itemSearchData = new ItemSearchData();
|
||||
$itemSearchData->setLimitCount(10);
|
||||
$itemSearchData->setSeachString('aa');
|
||||
|
||||
$result = self::$service->search($itemSearchData);
|
||||
|
||||
$this->assertEquals(0, $result->getNumRows());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws NoSuchItemException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function testGetByName()
|
||||
{
|
||||
$data = self::$service->getByName('Demo');
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data);
|
||||
$this->assertEquals('Demo', $data->getName());
|
||||
$this->assertEmpty($data->getDescription());
|
||||
|
||||
$this->expectException(NoSuchItemException::class);
|
||||
|
||||
self::$service->getByName('Test');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws NoSuchItemException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function testGetById()
|
||||
{
|
||||
$data = self::$service->getById(2);
|
||||
|
||||
$this->assertInstanceOf(UserGroupData::class, $data);
|
||||
$this->assertEquals('Demo', $data->getName());
|
||||
$this->assertEmpty($data->getDescription());
|
||||
|
||||
|
||||
$this->expectException(NoSuchItemException::class);
|
||||
|
||||
self::$service->getById(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ConstraintException
|
||||
* @throws \SP\Core\Exceptions\QueryException
|
||||
*/
|
||||
public function testGetUsageByUsers()
|
||||
{
|
||||
$this->assertCount(2, self::$service->getUsageByUsers(1));
|
||||
|
||||
$this->assertCount(5, self::$service->getUsageByUsers(2));
|
||||
|
||||
$this->assertCount(0, self::$service->getUsageByUsers(3));
|
||||
}
|
||||
}
|
||||
86
test/res/datasets/syspass_userGroup.xml
Normal file
86
test/res/datasets/syspass_userGroup.xml
Normal file
@@ -0,0 +1,86 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
~ sysPass
|
||||
~
|
||||
~ @author nuxsmin
|
||||
~ @link https://syspass.org
|
||||
~ @copyright 2012-2018, 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/>.
|
||||
-->
|
||||
|
||||
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<database name="syspass">
|
||||
<table_data name="UserProfile">
|
||||
<row>
|
||||
<field name="id">1</field>
|
||||
<field name="name">Admin</field>
|
||||
<field name="profile" xsi:type="xs:hexBinary">4F3A32343A2253505C446174614D6F64656C5C50726F66696C6544617461223A32393A7B733A31303A22002A0061636356696577223B623A303B733A31343A22002A006163635669657750617373223B623A303B733A31373A22002A0061636356696577486973746F7279223B623A303B733A31303A22002A0061636345646974223B623A303B733A31343A22002A006163634564697450617373223B623A303B733A393A22002A00616363416464223B623A303B733A31323A22002A0061636344656C657465223B623A303B733A31313A22002A0061636346696C6573223B623A303B733A31333A22002A0061636350726976617465223B623A313B733A31383A22002A006163635072697661746547726F7570223B623A313B733A31363A22002A006163635065726D697373696F6E223B623A303B733A31373A22002A006163635075626C69634C696E6B73223B623A303B733A31383A22002A00616363476C6F62616C536561726368223B623A303B733A31363A22002A00636F6E66696747656E6572616C223B623A303B733A31393A22002A00636F6E666967456E6372797074696F6E223B623A303B733A31353A22002A00636F6E6669674261636B7570223B623A303B733A31353A22002A00636F6E666967496D706F7274223B623A303B733A31313A22002A006D676D5573657273223B623A303B733A31323A22002A006D676D47726F757073223B623A303B733A31343A22002A006D676D50726F66696C6573223B623A303B733A31363A22002A006D676D43617465676F72696573223B623A303B733A31353A22002A006D676D437573746F6D657273223B623A303B733A31353A22002A006D676D417069546F6B656E73223B623A303B733A31373A22002A006D676D5075626C69634C696E6B73223B623A303B733A31343A22002A006D676D4163636F756E7473223B623A303B733A31303A22002A006D676D54616773223B623A303B733A31313A22002A006D676D46696C6573223B623A303B733A363A22002A0065766C223B623A303B733A31383A22002A006D676D437573746F6D4669656C6473223B623A303B7D</field>
|
||||
</row>
|
||||
<row>
|
||||
<field name="id">2</field>
|
||||
<field name="name">Demo</field>
|
||||
<field name="profile" xsi:type="xs:hexBinary">4F3A32343A2253505C446174614D6F64656C5C50726F66696C6544617461223A32393A7B733A31303A22002A0061636356696577223B623A313B733A31343A22002A006163635669657750617373223B623A313B733A31373A22002A0061636356696577486973746F7279223B623A313B733A31303A22002A0061636345646974223B623A313B733A31343A22002A006163634564697450617373223B623A313B733A393A22002A00616363416464223B623A313B733A31323A22002A0061636344656C657465223B623A313B733A31313A22002A0061636346696C6573223B623A303B733A31333A22002A0061636350726976617465223B623A303B733A31383A22002A006163635072697661746547726F7570223B623A303B733A31363A22002A006163635065726D697373696F6E223B623A303B733A31373A22002A006163635075626C69634C696E6B73223B623A303B733A31383A22002A00616363476C6F62616C536561726368223B623A303B733A31363A22002A00636F6E66696747656E6572616C223B623A303B733A31393A22002A00636F6E666967456E6372797074696F6E223B623A303B733A31353A22002A00636F6E6669674261636B7570223B623A303B733A31353A22002A00636F6E666967496D706F7274223B623A303B733A31313A22002A006D676D5573657273223B623A303B733A31323A22002A006D676D47726F757073223B623A303B733A31343A22002A006D676D50726F66696C6573223B623A303B733A31363A22002A006D676D43617465676F72696573223B623A303B733A31353A22002A006D676D437573746F6D657273223B623A303B733A31353A22002A006D676D417069546F6B656E73223B623A303B733A31373A22002A006D676D5075626C69634C696E6B73223B623A303B733A31343A22002A006D676D4163636F756E7473223B623A303B733A31303A22002A006D676D54616773223B623A303B733A31313A22002A006D676D46696C6573223B623A303B733A363A22002A0065766C223B623A303B733A31383A22002A006D676D437573746F6D4669656C6473223B623A303B7D</field>
|
||||
</row>
|
||||
<row>
|
||||
<field name="id">3</field>
|
||||
<field name="name">Usuarios</field>
|
||||
<field name="profile" xsi:type="xs:hexBinary">4F3A32343A2253505C446174614D6F64656C5C50726F66696C6544617461223A32393A7B733A31303A22002A0061636356696577223B623A313B733A31343A22002A006163635669657750617373223B623A313B733A31373A22002A0061636356696577486973746F7279223B623A313B733A31303A22002A0061636345646974223B623A313B733A31343A22002A006163634564697450617373223B623A313B733A393A22002A00616363416464223B623A313B733A31323A22002A0061636344656C657465223B623A313B733A31313A22002A0061636346696C6573223B623A303B733A31333A22002A0061636350726976617465223B623A303B733A31383A22002A006163635072697661746547726F7570223B623A303B733A31363A22002A006163635065726D697373696F6E223B623A303B733A31373A22002A006163635075626C69634C696E6B73223B623A303B733A31383A22002A00616363476C6F62616C536561726368223B623A303B733A31363A22002A00636F6E66696747656E6572616C223B623A303B733A31393A22002A00636F6E666967456E6372797074696F6E223B623A303B733A31353A22002A00636F6E6669674261636B7570223B623A303B733A31353A22002A00636F6E666967496D706F7274223B623A303B733A31313A22002A006D676D5573657273223B623A303B733A31323A22002A006D676D47726F757073223B623A303B733A31343A22002A006D676D50726F66696C6573223B623A303B733A31363A22002A006D676D43617465676F72696573223B623A303B733A31353A22002A006D676D437573746F6D657273223B623A303B733A31353A22002A006D676D417069546F6B656E73223B623A303B733A31373A22002A006D676D5075626C69634C696E6B73223B623A303B733A31343A22002A006D676D4163636F756E7473223B623A303B733A31303A22002A006D676D54616773223B623A303B733A31313A22002A006D676D46696C6573223B623A303B733A363A22002A0065766C223B623A303B733A31383A22002A006D676D437573746F6D4669656C6473223B623A303B7D</field>
|
||||
</row>
|
||||
</table_data>
|
||||
<table_data name="UserGroup">
|
||||
<row>
|
||||
<field name="id">1</field>
|
||||
<field name="name">Admins</field>
|
||||
<field name="description">sysPass Admins</field>
|
||||
</row>
|
||||
<row>
|
||||
<field name="id">2</field>
|
||||
<field name="name">Demo</field>
|
||||
<field name="description"/>
|
||||
</row>
|
||||
<row>
|
||||
<field name="id">3</field>
|
||||
<field name="name">Usuarios</field>
|
||||
<field name="description">Grupo Usuarios</field>
|
||||
</row>
|
||||
<row>
|
||||
<field name="id">4</field>
|
||||
<field name="name">Test A</field>
|
||||
<field name="description" />
|
||||
</row>
|
||||
<row>
|
||||
<field name="id">5</field>
|
||||
<field name="name">Test B</field>
|
||||
<field name="description" />
|
||||
</row>
|
||||
</table_data>
|
||||
<table_data name="UserToUserGroup">
|
||||
<row>
|
||||
<field name="userId">1</field>
|
||||
<field name="userGroupId">2</field>
|
||||
</row>
|
||||
<row>
|
||||
<field name="userId">3</field>
|
||||
<field name="userGroupId">2</field>
|
||||
</row>
|
||||
<row>
|
||||
<field name="userId">2</field>
|
||||
<field name="userGroupId">1</field>
|
||||
</row>
|
||||
</table_data>
|
||||
</database>
|
||||
</mysqldump>
|
||||
Reference in New Issue
Block a user