diff --git a/app/modules/web/Controllers/ConfigManagerController.php b/app/modules/web/Controllers/ConfigManagerController.php index bde940aa..3fcae8b9 100644 --- a/app/modules/web/Controllers/ConfigManagerController.php +++ b/app/modules/web/Controllers/ConfigManagerController.php @@ -229,7 +229,7 @@ class ConfigManagerController extends ControllerBase $template->setBase('config'); $template->addTemplate('encryption'); - $numAccounts = $this->dic->get(AccountService::class)->getTotalNumAccounts()->num; + $numAccounts = $this->dic->get(AccountService::class)->getTotalNumAccounts(); $template->assign('numAccounts', $numAccounts); if ($numAccounts > 500) { diff --git a/lib/SP/Core/Context/ContextBase.php b/lib/SP/Core/Context/ContextBase.php index c9e5cc4b..1d3b6458 100644 --- a/lib/SP/Core/Context/ContextBase.php +++ b/lib/SP/Core/Context/ContextBase.php @@ -40,9 +40,60 @@ abstract class ContextBase implements ContextInterface * @var ContextCollection */ private $context; + /** + * @var ContextCollection + */ + private $trasient; + + /** + * ContextBase constructor. + * + * @param ContextCollection $trasient + */ + public function __construct(ContextCollection $trasient) + { + $this->trasient = new ContextCollection(); + } + + /** + * Sets an arbitrary key in the trasient collection. + * This key is not bound to any known method or type + * + * @param string $key + * @param mixed $value + * + * @return mixed + * @throws ContextException + */ + public function setTrasientKey(string $key, $value) + { + // If the key starts with "_" it's a protected key, thus cannot be overwritten + if (strpos($key, '_') === 0 && $this->trasient->exists($key)) { + throw new ContextException(__u('No es posible cambiar el valor de la clave')); + } + + $this->trasient->set($key, $value); + + return $value; + } + + /** + * Gets an arbitrary key from the trasient collection. + * This key is not bound to any known method or type + * + * @param string $key + * @param mixed $default + * + * @return mixed + */ + public function getTrasientKey(string $key, $default = null) + { + return is_numeric($default) ? (int)$this->trasient->get($key, $default) : $this->trasient->get($key, $default); + } /** * @param $context + * * @throws ContextException */ final protected function setContextReference(&$context) @@ -65,6 +116,7 @@ abstract class ContextBase implements ContextInterface /** * @param ContextCollection $contextCollection + * * @throws ContextException */ final protected function setContext(ContextCollection $contextCollection) @@ -81,35 +133,15 @@ abstract class ContextBase implements ContextInterface * * @param string $key * @param mixed $default - * @return mixed - * @throws ContextException - */ - protected function getContextKey($key, $default = null) - { - $this->checkContext(); - - if (isset($this->context[$key])) { - return is_numeric($default) ? (int)$this->context[$key] : $this->context[$key]; - } - - return $default; - } - - /** - * Establecer una variable de contexto * - * @param string $key El nombre de la variable - * @param mixed $value El valor de la variable * @return mixed * @throws ContextException */ - protected function setContextKey($key, $value) + protected function getContextKey(string $key, $default = null) { $this->checkContext(); - $this->context[$key] = $value; - - return $value; + return is_numeric($default) ? (int)$this->context->get($key, $default) : $this->context->get($key, $default); } /** @@ -121,4 +153,22 @@ abstract class ContextBase implements ContextInterface throw new ContextException(__u('Contexto no inicializado')); } } + + /** + * Establecer una variable de contexto + * + * @param string $key El nombre de la variable + * @param mixed $value El valor de la variable + * + * @return mixed + * @throws ContextException + */ + protected function setContextKey(string $key, $value) + { + $this->checkContext(); + + $this->context->set($key, $value); + + return $value; + } } \ No newline at end of file diff --git a/lib/SP/Core/Context/ContextInterface.php b/lib/SP/Core/Context/ContextInterface.php index 1f03d51b..92603750 100644 --- a/lib/SP/Core/Context/ContextInterface.php +++ b/lib/SP/Core/Context/ContextInterface.php @@ -159,4 +159,27 @@ interface ContextInterface * @return AccountCache[]|null */ public function getAccountsCache(); + + /** + * Sets an arbitrary key in the trasient collection. + * This key is not bound to any known method or type + * + * @param string $key + * @param mixed $value + * + * @return mixed + * @throws ContextException + */ + public function setTrasientKey(string $key, $value); + + /** + * Gets an arbitrary key from the trasient collection. + * This key is not bound to any known method or type + * + * @param string $key + * @param mixed $default + * + * @return mixed + */ + public function getTrasientKey(string $key, $default = null); } \ No newline at end of file diff --git a/lib/SP/Core/Context/SessionContext.php b/lib/SP/Core/Context/SessionContext.php index 00c6e86b..851ad7f4 100644 --- a/lib/SP/Core/Context/SessionContext.php +++ b/lib/SP/Core/Context/SessionContext.php @@ -91,7 +91,7 @@ class SessionContext extends ContextBase * * @return mixed */ - protected function getContextKey($key, $default = null) + protected function getContextKey(string $key, $default = null) { try { return parent::getContextKey($key, $default); @@ -120,7 +120,7 @@ class SessionContext extends ContextBase * * @return mixed */ - protected function setContextKey($key, $value) + protected function setContextKey(string $key, $value) { try { if (self::$isLocked) { diff --git a/lib/SP/Core/Context/StatelessContext.php b/lib/SP/Core/Context/StatelessContext.php index f653dbe6..cb38a2dd 100644 --- a/lib/SP/Core/Context/StatelessContext.php +++ b/lib/SP/Core/Context/StatelessContext.php @@ -53,7 +53,7 @@ class StatelessContext extends ContextBase * * @return mixed */ - protected function setContextKey($key, $value) + protected function setContextKey(string $key, $value) { try { parent::setContextKey($key, $value); @@ -94,7 +94,7 @@ class StatelessContext extends ContextBase * * @return mixed */ - protected function getContextKey($key, $default = null) + protected function getContextKey(string $key, $default = null) { try { return parent::getContextKey($key, $default); diff --git a/lib/SP/Repositories/Account/AccountRepository.php b/lib/SP/Repositories/Account/AccountRepository.php index af8a435d..3ceb0c6c 100644 --- a/lib/SP/Repositories/Account/AccountRepository.php +++ b/lib/SP/Repositories/Account/AccountRepository.php @@ -80,7 +80,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface /** * @param $id * - * @return AccountPassData + * @return QueryResult * @throws ConstraintException * @throws QueryException */ @@ -97,7 +97,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->setWhere($queryFilter->getFilters()); $queryData->setParams($queryFilter->getParams()); - return $this->db->doSelect($queryData)->getData(); + return $this->db->doSelect($queryData); } /** @@ -557,8 +557,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface * * @param $id * - * @return AccountExtData - * @throws NoSuchItemException + * @return QueryResult * @throws QueryException * @throws ConstraintException */ @@ -584,13 +583,7 @@ class AccountRepository extends Repository implements RepositoryItemInterface $queryData->addParam($id); $queryData->setOnErrorMessage(__u('No se pudieron obtener los datos de la cuenta')); - $result = $this->db->doSelect($queryData, true); - - if ($result->getNumRows() === 0) { - throw new NoSuchItemException(__u('La cuenta no existe')); - } - - return $result->getData(); + return $this->db->doSelect($queryData, true); } /** diff --git a/lib/SP/Repositories/Account/AccountToTagRepository.php b/lib/SP/Repositories/Account/AccountToTagRepository.php index ab27737b..b3b515ed 100644 --- a/lib/SP/Repositories/Account/AccountToTagRepository.php +++ b/lib/SP/Repositories/Account/AccountToTagRepository.php @@ -44,7 +44,7 @@ class AccountToTagRepository extends Repository * * @param int $id * - * @return ItemData[] + * @return \SP\Storage\Database\QueryResult * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -62,7 +62,7 @@ class AccountToTagRepository extends Repository $queryData->addParam($id); $queryData->setMapClassName(ItemData::class); - return $this->db->doSelect($queryData)->getDataAsArray(); + return $this->db->doSelect($queryData); } /** diff --git a/lib/SP/Repositories/Account/AccountToUserGroupRepository.php b/lib/SP/Repositories/Account/AccountToUserGroupRepository.php index 0515dcb2..6c501322 100644 --- a/lib/SP/Repositories/Account/AccountToUserGroupRepository.php +++ b/lib/SP/Repositories/Account/AccountToUserGroupRepository.php @@ -44,7 +44,7 @@ class AccountToUserGroupRepository extends Repository * * @param int $id con el Id de la cuenta * - * @return ItemData[] + * @return \SP\Storage\Database\QueryResult * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -62,7 +62,7 @@ class AccountToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setMapClassName(ItemData::class); - return $this->db->doSelect($queryData)->getDataAsArray(); + return $this->db->doSelect($queryData); } /** @@ -70,7 +70,7 @@ class AccountToUserGroupRepository extends Repository * * @param $id * - * @return ItemData[] + * @return \SP\Storage\Database\QueryResult * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -88,7 +88,7 @@ class AccountToUserGroupRepository extends Repository $queryData->addParam($id); $queryData->setMapClassName(ItemData::class); - return $this->db->doSelect($queryData)->getDataAsArray(); + return $this->db->doSelect($queryData); } /** @@ -125,7 +125,7 @@ class AccountToUserGroupRepository extends Repository /** * @param $id int * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -168,7 +168,7 @@ class AccountToUserGroupRepository extends Repository /** * @param AccountRequest $accountRequest * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -182,7 +182,7 @@ class AccountToUserGroupRepository extends Repository /** * @param $id int * - * @return bool + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -199,7 +199,7 @@ class AccountToUserGroupRepository extends Repository /** * @param AccountRequest $accountRequest * - * @return int Last ID inserted + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ diff --git a/lib/SP/Repositories/Account/AccountToUserRepository.php b/lib/SP/Repositories/Account/AccountToUserRepository.php index cdd195c4..edd6dc69 100644 --- a/lib/SP/Repositories/Account/AccountToUserRepository.php +++ b/lib/SP/Repositories/Account/AccountToUserRepository.php @@ -175,6 +175,8 @@ class AccountToUserRepository extends Repository * @return ItemData[] con los id de usuarios de la cuenta * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException + * + * @todo change returned type */ public function getUsersByAccountId($id) { diff --git a/lib/SP/Services/Account/AccountHistoryService.php b/lib/SP/Services/Account/AccountHistoryService.php index aed43037..7eb42554 100644 --- a/lib/SP/Services/Account/AccountHistoryService.php +++ b/lib/SP/Services/Account/AccountHistoryService.php @@ -138,7 +138,7 @@ class AccountHistoryService extends Service */ public function getUserGroupsByAccountId($id) { - return $this->accountToUserGroupRepository->getUserGroupsByAccountId($id); + return $this->accountToUserGroupRepository->getUserGroupsByAccountId($id)->getDataAsArray(); } /** diff --git a/lib/SP/Services/Account/AccountSearchService.php b/lib/SP/Services/Account/AccountSearchService.php index 8794d386..11fa7d4f 100644 --- a/lib/SP/Services/Account/AccountSearchService.php +++ b/lib/SP/Services/Account/AccountSearchService.php @@ -173,7 +173,7 @@ class AccountSearchService extends Service $accountsSearchItem->setUserGroups($cache->getUserGroups()); } - $accountsSearchItem->setTags($this->accountToTagRepository->getTagsByAccountId($accountSearchData->getId())); + $accountsSearchItem->setTags($this->accountToTagRepository->getTagsByAccountId($accountSearchData->getId())->getDataAsArray()); $accountsSearchItem->setTextMaxLength($maxTextLength); $accountsSearchItem->setColor($this->pickAccountColor($accountSearchData->getClientId())); $accountsSearchItem->setLink($accountLinkEnabled); @@ -342,7 +342,7 @@ class AccountSearchService extends Service $cache[$accountId] = new AccountCache( $accountId, $this->accountToUserRepository->getUsersByAccountId($accountId), - $this->accountToUserGroupRepository->getUserGroupsByAccountId($accountId)); + $this->accountToUserGroupRepository->getUserGroupsByAccountId($accountId)->getDataAsArray()); if ($hasCache) { $this->context->setAccountsCache($cache); diff --git a/lib/SP/Services/Account/AccountService.php b/lib/SP/Services/Account/AccountService.php index 1c857c7f..90cf1f67 100644 --- a/lib/SP/Services/Account/AccountService.php +++ b/lib/SP/Services/Account/AccountService.php @@ -28,6 +28,7 @@ use Defuse\Crypto\Exception\CryptoException; use SP\Account\AccountRequest; use SP\Account\AccountSearchFilter; use SP\Account\AccountUtil; +use SP\Core\Context\SessionContext; use SP\Core\Crypt\Crypt; use SP\Core\Crypt\Session as CryptSession; use SP\Core\Exceptions\QueryException; @@ -42,6 +43,7 @@ use SP\Repositories\Account\AccountRepository; use SP\Repositories\Account\AccountToTagRepository; use SP\Repositories\Account\AccountToUserGroupRepository; use SP\Repositories\Account\AccountToUserRepository; +use SP\Repositories\NoSuchItemException; use SP\Services\Config\ConfigService; use SP\Services\Service; use SP\Services\ServiceException; @@ -74,18 +76,6 @@ class AccountService extends Service implements AccountServiceInterface */ protected $accountToTagRepository; - /** - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface - */ - public function initialize() - { - $this->accountRepository = $this->dic->get(AccountRepository::class); - $this->accountToUserRepository = $this->dic->get(AccountToUserRepository::class); - $this->accountToUserGroupRepository = $this->dic->get(AccountToUserGroupRepository::class); - $this->accountToTagRepository = $this->dic->get(AccountToTagRepository::class); - } - /** * @param int $id * @@ -122,7 +112,7 @@ class AccountService extends Service implements AccountServiceInterface */ public function withUserGroupsById(AccountDetailsResponse $accountDetailsResponse) { - $accountDetailsResponse->setUserGroups($this->accountToUserGroupRepository->getUserGroupsByAccountId($accountDetailsResponse->getId())); + $accountDetailsResponse->setUserGroups($this->accountToUserGroupRepository->getUserGroupsByAccountId($accountDetailsResponse->getId())->getDataAsArray()); return $this; } @@ -136,7 +126,7 @@ class AccountService extends Service implements AccountServiceInterface */ public function withTagsById(AccountDetailsResponse $accountDetailsResponse) { - $accountDetailsResponse->setTags($this->accountToTagRepository->getTagsByAccountId($accountDetailsResponse->getId())); + $accountDetailsResponse->setTags($this->accountToTagRepository->getTagsByAccountId($accountDetailsResponse->getId())->getDataAsArray()); return $this; } @@ -171,10 +161,17 @@ class AccountService extends Service implements AccountServiceInterface * @return \SP\DataModel\AccountPassData * @throws QueryException * @throws \SP\Core\Exceptions\ConstraintException + * @throws NoSuchItemException */ public function getPasswordForId($id) { - return $this->accountRepository->getPasswordForId($id); + $result = $this->accountRepository->getPasswordForId($id); + + if ($result->getNumRows() === 0) { + throw new NoSuchItemException(__u('Cuenta no encontrada')); + } + + return $result->getData(); } /** @@ -216,18 +213,28 @@ class AccountService extends Service implements AccountServiceInterface public function getPasswordEncrypted($pass, $masterPass = null) { try { - $masterPass = $masterPass ?: CryptSession::getSessionKey($this->context); + if ($masterPass === null) { + if ($this->context instanceof SessionContext) { + $masterPass = CryptSession::getSessionKey($this->context); + } else { + $masterPass = $this->context->getTrasientKey('_masterpass'); + } + } + + if (empty($masterPass)) { + throw new ServiceException(__u('Clave maestra no establecida')); + } $out['key'] = Crypt::makeSecuredKey($masterPass); $out['pass'] = Crypt::encrypt($pass, $out['key'], $masterPass); - if (strlen($pass) > 1000 || strlen($out['key']) > 1000) { - throw new ServiceException(__u('Error interno'), SPException::ERROR); + if (strlen($out['pass']) > 1000 || strlen($out['key']) > 1000) { + throw new ServiceException(__u('Error interno')); } return $out; } catch (CryptoException $e) { - throw new ServiceException(__u('Error interno'), SPException::ERROR); + throw new ServiceException(__u('Error interno')); } } @@ -253,7 +260,7 @@ class AccountService extends Service implements AccountServiceInterface } if (is_array($accountRequest->usersEdit) && !empty($accountRequest->usersEdit)) { - $this->accountToUserRepository->add($accountRequest); + $this->accountToUserRepository->addEdit($accountRequest); } } @@ -363,7 +370,7 @@ class AccountService extends Service implements AccountServiceInterface } } } catch (SPException $e) { - debugLog($e->getMessage()); + processException($e); } } @@ -392,17 +399,19 @@ class AccountService extends Service implements AccountServiceInterface } /** + * Updates an already encrypted password data from a master password changing action + * * @param AccountPasswordRequest $accountRequest * - * @throws SPException + * @return bool * @throws \SP\Core\Exceptions\ConstraintException + * @throws QueryException */ public function updatePasswordMasterPass(AccountPasswordRequest $accountRequest) { - $this->accountRepository->updatePassword($accountRequest); + return $this->accountRepository->updatePassword($accountRequest); } - /** * @param $historyId * @param $accountId @@ -424,13 +433,14 @@ class AccountService extends Service implements AccountServiceInterface * @param $id * * @return AccountService - * @throws SPException - * @throws ServiceException + * @throws NoSuchItemException + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function delete($id) { if ($this->accountRepository->delete($id) === 0) { - throw new ServiceException(__u('Cuenta no encontrada'), ServiceException::INFO); + throw new NoSuchItemException(__u('Cuenta no encontrada')); } return $this; @@ -446,7 +456,7 @@ class AccountService extends Service implements AccountServiceInterface public function deleteByIdBatch(array $ids) { if ($this->accountRepository->deleteByIdBatch($ids) === 0) { - throw new ServiceException(__u('Error al eliminar las cuentas'), ServiceException::WARNING); + throw new ServiceException(__u('Error al eliminar las cuentas')); } return $this; @@ -531,7 +541,7 @@ class AccountService extends Service implements AccountServiceInterface */ public function getTotalNumAccounts() { - return $this->accountRepository->getTotalNumAccounts(); + return $this->accountRepository->getTotalNumAccounts()->num; } /** @@ -546,7 +556,13 @@ class AccountService extends Service implements AccountServiceInterface */ public function getDataForLink($id) { - return $this->accountRepository->getDataForLink($id); + $result = $this->accountRepository->getDataForLink($id); + + if ($result->getNumRows() === 0) { + throw new NoSuchItemException(__u('La cuenta no existe')); + } + + return $result->getData(); } /** @@ -575,4 +591,16 @@ class AccountService extends Service implements AccountServiceInterface { return $this->accountRepository->getByFilter($accountSearchFilter); } + + /** + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + */ + protected function initialize() + { + $this->accountRepository = $this->dic->get(AccountRepository::class); + $this->accountToUserRepository = $this->dic->get(AccountToUserRepository::class); + $this->accountToUserGroupRepository = $this->dic->get(AccountToUserGroupRepository::class); + $this->accountToTagRepository = $this->dic->get(AccountToTagRepository::class); + } } \ No newline at end of file diff --git a/lib/SP/Services/Account/AccountToTagService.php b/lib/SP/Services/Account/AccountToTagService.php index eeb31ab2..436b60df 100644 --- a/lib/SP/Services/Account/AccountToTagService.php +++ b/lib/SP/Services/Account/AccountToTagService.php @@ -49,7 +49,7 @@ class AccountToTagService extends Service */ public function getTagsByAccountId($id) { - return $this->accountToTagRepository->getTagsByAccountId($id); + return $this->accountToTagRepository->getTagsByAccountId($id)->getDataAsArray(); } /** diff --git a/lib/SP/Services/Api/ApiService.php b/lib/SP/Services/Api/ApiService.php index 2410364c..fe58cb05 100644 --- a/lib/SP/Services/Api/ApiService.php +++ b/lib/SP/Services/Api/ApiService.php @@ -64,15 +64,12 @@ class ApiService extends Service * @var AuthTokenData */ protected $authTokenData; - /** - * @var string - */ - protected $masterPass; /** * Sets up API * * @param $actionId + * * @throws ServiceException * @throws \Exception */ @@ -97,7 +94,7 @@ class ApiService extends Service if ($actionId === ActionsInterface::ACCOUNT_VIEW_PASS || $actionId === ActionsInterface::ACCOUNT_CREATE ) { - $this->masterPass = $this->getMasterPassFromVault(); + $this->context->setTrasientKey('_masterpass', $this->getMasterPassFromVault()); } } @@ -124,8 +121,9 @@ class ApiService extends Service * Devolver el valor de un parámetro * * @param string $param - * @param bool $required Si es requerido - * @param mixed $default Valor por defecto + * @param bool $required Si es requerido + * @param mixed $default Valor por defecto + * * @return int|string * @throws ServiceException */ @@ -149,6 +147,7 @@ class ApiService extends Service * Devuelve la ayuda para una acción * * @param string $action + * * @return array */ public function getHelp($action) @@ -329,8 +328,9 @@ class ApiService extends Service /** * @param string $param - * @param bool $required - * @param null $default + * @param bool $required + * @param null $default + * * @return int|string * @throws ServiceException */ @@ -341,8 +341,9 @@ class ApiService extends Service /** * @param string $param - * @param bool $required - * @param null $default + * @param bool $required + * @param null $default + * * @return int|string * @throws ServiceException */ @@ -353,8 +354,9 @@ class ApiService extends Service /** * @param string $param - * @param bool $required - * @param null $default + * @param bool $required + * @param null $default + * * @return int|string * @throws ServiceException */ @@ -365,8 +367,9 @@ class ApiService extends Service /** * @param string $param - * @param bool $required - * @param null $default + * @param bool $required + * @param null $default + * * @return int|string * @throws ServiceException */ @@ -380,11 +383,12 @@ class ApiService extends Service */ public function getMasterPass() { - return $this->masterPass; + return $this->context->getTrasientKey('_masterpass'); } /** * @param ApiRequest $apiRequest + * * @return ApiService */ public function setApiRequest(ApiRequest $apiRequest) diff --git a/tests/DatabaseTestCase.php b/tests/DatabaseTestCase.php index 53342696..126098e7 100644 --- a/tests/DatabaseTestCase.php +++ b/tests/DatabaseTestCase.php @@ -46,6 +46,10 @@ abstract class DatabaseTestCase extends TestCase * @var DatabaseConnectionData */ protected static $databaseConnectionData; + /** + * @var string + */ + protected static $dataset = 'syspass.xml'; /** * @var \PDO */ @@ -81,6 +85,6 @@ abstract class DatabaseTestCase extends TestCase */ protected function getDataSet() { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass.xml'); + return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . self::$dataset); } } \ No newline at end of file diff --git a/tests/Repositories/AccountFileRepositoryTest.php b/tests/Repositories/AccountFileRepositoryTest.php index 6d123b6b..cb3c0075 100644 --- a/tests/Repositories/AccountFileRepositoryTest.php +++ b/tests/Repositories/AccountFileRepositoryTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Repositories; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\DataModel\FileData; use SP\DataModel\FileExtData; use SP\DataModel\ItemSearchData; @@ -54,6 +53,8 @@ class AccountFileRepositoryTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountFile.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -302,14 +303,4 @@ class AccountFileRepositoryTest extends DatabaseTestCase $result = self::$repository->getByIdBatch([]); $this->assertEquals(0, $result->getNumRows()); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountFile.xml'); - } } diff --git a/tests/Repositories/AccountHistoryRepositoryTest.php b/tests/Repositories/AccountHistoryRepositoryTest.php index c7b1e682..52fa9ece 100644 --- a/tests/Repositories/AccountHistoryRepositoryTest.php +++ b/tests/Repositories/AccountHistoryRepositoryTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Repositories; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\DataModel\AccountHistoryData; use SP\DataModel\Dto\AccountHistoryCreateDto; use SP\DataModel\ItemSearchData; @@ -56,6 +55,8 @@ class AccountHistoryRepositoryTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountHistory.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -237,14 +238,4 @@ class AccountHistoryRepositoryTest extends DatabaseTestCase $request->id = 10; $this->assertEquals(0, self::$repository->updatePassword($request)); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountHistory.xml'); - } } diff --git a/tests/Repositories/AccountRepositoryTest.php b/tests/Repositories/AccountRepositoryTest.php index 4d841861..6a5533ae 100644 --- a/tests/Repositories/AccountRepositoryTest.php +++ b/tests/Repositories/AccountRepositoryTest.php @@ -63,6 +63,8 @@ class AccountRepositoryTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_account.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -120,14 +122,14 @@ class AccountRepositoryTest extends DatabaseTestCase // Comprobar que la modificación de la clave es correcta $this->assertEquals(1, self::$repository->editPassword($accountRequest)); - $accountPassData = self::$repository->getPasswordForId(2); + $accountPassData = self::$repository->getPasswordForId(2)->getData(); $clearPassword = Crypt::decrypt($accountPassData->pass, $accountPassData->key, self::SECURE_KEY_PASSWORD); // Comprobar que la clave obtenida es igual a la encriptada anteriormente $this->assertEquals('1234', $clearPassword); - // Comprobar que se devuelve un array vacío - $this->assertNull(self::$repository->getPasswordForId(10)); + // Comprobar que no devuelve resultados + $this->assertEquals(0, self::$repository->getPasswordForId(10)->getNumRows()); } /** @@ -311,7 +313,7 @@ class AccountRepositoryTest extends DatabaseTestCase // Comprobar que la modificación de la clave es correcta $this->assertTrue(self::$repository->updatePassword($accountRequest)); - $accountPassData = self::$repository->getPasswordForId(2); + $accountPassData = self::$repository->getPasswordForId(2)->getData(); $clearPassword = Crypt::decrypt($accountPassData->pass, $accountPassData->key, self::SECURE_KEY_PASSWORD); // Comprobar que la clave obtenida es igual a la encriptada anteriormente diff --git a/tests/Repositories/AccountToFavoriteRepositoryTest.php b/tests/Repositories/AccountToFavoriteRepositoryTest.php index aa767406..0653bf1b 100644 --- a/tests/Repositories/AccountToFavoriteRepositoryTest.php +++ b/tests/Repositories/AccountToFavoriteRepositoryTest.php @@ -25,7 +25,6 @@ namespace SP\Tests\Repositories; use DI\DependencyException; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Exceptions\ConstraintException; use SP\Repositories\Account\AccountToFavoriteRepository; use SP\Storage\Database\DatabaseConnectionData; @@ -53,6 +52,8 @@ class AccountToFavoriteRepositoryTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountFavorite.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -105,14 +106,4 @@ class AccountToFavoriteRepositoryTest extends DatabaseTestCase $this->assertEquals(1, self::$repository->delete(1, 3)); $this->assertEquals(0, self::$repository->delete(10, 1)); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountFavorite.xml'); - } } diff --git a/tests/Repositories/AccountToTagRepositoryTest.php b/tests/Repositories/AccountToTagRepositoryTest.php index 1b4b8794..1c2d92cc 100644 --- a/tests/Repositories/AccountToTagRepositoryTest.php +++ b/tests/Repositories/AccountToTagRepositoryTest.php @@ -71,8 +71,8 @@ class AccountToTagRepositoryTest extends DatabaseTestCase */ public function testGetTagsByAccountId() { - $this->assertCount(1, self::$repository->getTagsByAccountId(1)); - $this->assertCount(0, self::$repository->getTagsByAccountId(10)); + $this->assertEquals(1, self::$repository->getTagsByAccountId(1)->getNumRows()); + $this->assertEquals(0, self::$repository->getTagsByAccountId(10)->getNumRows()); } /** @@ -90,12 +90,13 @@ class AccountToTagRepositoryTest extends DatabaseTestCase $this->assertEquals(2, self::$repository->add($accountRequest)); - $tags = self::$repository->getTagsByAccountId($accountRequest->id); + $result = self::$repository->getTagsByAccountId($accountRequest->id); + $data = $result->getDataAsArray(); - $this->assertCount(3, $tags); - $this->assertInstanceOf(ItemData::class, $tags[0]); - $this->assertInstanceOf(ItemData::class, $tags[1]); - $this->assertInstanceOf(ItemData::class, $tags[2]); + $this->assertCount(3, $result); + $this->assertInstanceOf(ItemData::class, $data[0]); + $this->assertInstanceOf(ItemData::class, $data[1]); + $this->assertInstanceOf(ItemData::class, $data[2]); $this->expectException(ConstraintException::class); @@ -119,7 +120,7 @@ class AccountToTagRepositoryTest extends DatabaseTestCase { $this->assertEquals(1, self::$repository->deleteByAccountId(1)); - $this->assertCount(0, self::$repository->getTagsByAccountId(1)); + $this->assertEquals(0, self::$repository->getTagsByAccountId(1)->getNumRows()); $this->assertEquals(0, self::$repository->deleteByAccountId(10)); } @@ -139,10 +140,11 @@ class AccountToTagRepositoryTest extends DatabaseTestCase self::$repository->update($accountRequest); - $tags = self::$repository->getTagsByAccountId($accountRequest->id); + $result = self::$repository->getTagsByAccountId($accountRequest->id); + $data = $result->getDataAsArray(); - $this->assertCount(2, $tags); - $this->assertInstanceOf(ItemData::class, $tags[0]); - $this->assertInstanceOf(ItemData::class, $tags[1]); + $this->assertEquals(2, $result); + $this->assertInstanceOf(ItemData::class, $data[0]); + $this->assertInstanceOf(ItemData::class, $data[1]); } } diff --git a/tests/Repositories/AccountToUserGroupRepositoryTest.php b/tests/Repositories/AccountToUserGroupRepositoryTest.php index dcb3dc6e..9a2afe26 100644 --- a/tests/Repositories/AccountToUserGroupRepositoryTest.php +++ b/tests/Repositories/AccountToUserGroupRepositoryTest.php @@ -71,43 +71,43 @@ class AccountToUserGroupRepositoryTest extends DatabaseTestCase */ public function testGetUserGroupsByAccountId() { - $userGroups = self::$repository->getUserGroupsByAccountId(1); + $result = self::$repository->getUserGroupsByAccountId(1); + $data = $result->getDataAsArray(); - $this->assertCount(1, $userGroups); - $this->assertInstanceOf(ItemData::class, $userGroups[0]); + $this->assertEquals(1, $result->getNumRows()); + $this->assertInstanceOf(ItemData::class, $data[0]); - $userGroupsView = array_filter($userGroups, function ($user) { + $userGroupsView = array_filter($data, function ($user) { return (int)$user->isEdit === 0; }); $this->assertCount(0, $userGroupsView); - $userGroupsEdit = array_filter($userGroups, function ($user) { + $userGroupsEdit = array_filter($data, function ($user) { return (int)$user->isEdit === 1; }); $this->assertCount(1, $userGroupsEdit); - $userGroups = self::$repository->getUserGroupsByAccountId(2); + $result = self::$repository->getUserGroupsByAccountId(2); + $data = $result->getDataAsArray(); - $this->assertCount(1, $userGroups); - $this->assertInstanceOf(ItemData::class, $userGroups[0]); + $this->assertEquals(1, $result->getNumRows()); + $this->assertInstanceOf(ItemData::class, $data[0]); - $userGroupsView = array_filter($userGroups, function ($user) { + $userGroupsView = array_filter($data, function ($user) { return (int)$user->isEdit === 0; }); $this->assertCount(1, $userGroupsView); - $userGroupsEdit = array_filter($userGroups, function ($user) { + $userGroupsEdit = array_filter($data, function ($user) { return (int)$user->isEdit === 1; }); $this->assertCount(0, $userGroupsEdit); - $userGroups = self::$repository->getUserGroupsByAccountId(3); - - $this->assertCount(0, $userGroups); + $this->assertEquals(0, self::$repository->getUserGroupsByAccountId(3)->getNumRows()); } /** @@ -125,15 +125,16 @@ class AccountToUserGroupRepositoryTest extends DatabaseTestCase $this->assertEquals(3, self::$repository->update($accountRequest)); - $userGroups = self::$repository->getUserGroupsByAccountId($accountRequest->id); + $result = self::$repository->getUserGroupsByAccountId($accountRequest->id); + $data = $result->getDataAsArray(); - $this->assertCount(3, $userGroups); - $this->assertInstanceOf(ItemData::class, $userGroups[0]); - $this->assertEquals(0, (int)$userGroups[0]->isEdit); - $this->assertInstanceOf(ItemData::class, $userGroups[1]); - $this->assertEquals(0, (int)$userGroups[1]->isEdit); - $this->assertInstanceOf(ItemData::class, $userGroups[2]); - $this->assertEquals(0, (int)$userGroups[2]->isEdit); + $this->assertEquals(3, $result->getNumRows()); + $this->assertInstanceOf(ItemData::class, $data[0]); + $this->assertEquals(0, (int)$data[0]->isEdit); + $this->assertInstanceOf(ItemData::class, $data[1]); + $this->assertEquals(0, (int)$data[1]->isEdit); + $this->assertInstanceOf(ItemData::class, $data[2]); + $this->assertEquals(0, (int)$data[2]->isEdit); $this->expectException(ConstraintException::class); @@ -162,13 +163,14 @@ class AccountToUserGroupRepositoryTest extends DatabaseTestCase $this->assertEquals(3, self::$repository->updateEdit($accountRequest)); - $userGroups = self::$repository->getUserGroupsByAccountId($accountRequest->id); + $result = self::$repository->getUserGroupsByAccountId($accountRequest->id); + $data = $result->getDataAsArray(); - $this->assertCount(2, $userGroups); - $this->assertInstanceOf(ItemData::class, $userGroups[0]); - $this->assertEquals(1, (int)$userGroups[0]->isEdit); - $this->assertInstanceOf(ItemData::class, $userGroups[1]); - $this->assertEquals(1, (int)$userGroups[1]->isEdit); + $this->assertEquals(2, $result->getNumRows()); + $this->assertInstanceOf(ItemData::class, $data[0]); + $this->assertEquals(1, (int)$data[0]->isEdit); + $this->assertInstanceOf(ItemData::class, $data[1]); + $this->assertEquals(1, (int)$data[1]->isEdit); $this->expectException(ConstraintException::class); @@ -194,7 +196,7 @@ class AccountToUserGroupRepositoryTest extends DatabaseTestCase public function testDeleteByAccountId() { $this->assertEquals(1, self::$repository->deleteByAccountId(1)); - $this->assertCount(0, self::$repository->getUserGroupsByAccountId(1)); + $this->assertEquals(0, self::$repository->getUserGroupsByAccountId(1)->getNumRows()); $this->assertEquals(0, self::$repository->deleteByAccountId(10)); @@ -216,12 +218,13 @@ class AccountToUserGroupRepositoryTest extends DatabaseTestCase self::$repository->addEdit($accountRequest); - $userGroups = self::$repository->getUserGroupsByAccountId($accountRequest->id); + $result = self::$repository->getUserGroupsByAccountId($accountRequest->id); + $data = $result->getDataAsArray(); - $this->assertCount(3, $userGroups); - $this->assertInstanceOf(ItemData::class, $userGroups[0]); - $this->assertInstanceOf(ItemData::class, $userGroups[1]); - $this->assertInstanceOf(ItemData::class, $userGroups[2]); + $this->assertEquals(3, $result->getNumRows()); + $this->assertInstanceOf(ItemData::class, $data[0]); + $this->assertInstanceOf(ItemData::class, $data[1]); + $this->assertInstanceOf(ItemData::class, $data[2]); $this->expectException(ConstraintException::class); @@ -283,7 +286,7 @@ class AccountToUserGroupRepositoryTest extends DatabaseTestCase public function testDeleteEditByAccountId() { $this->assertEquals(1, self::$repository->deleteEditByAccountId(1)); - $this->assertCount(0, self::$repository->getUserGroupsByAccountId(1)); + $this->assertEquals(0, self::$repository->getUserGroupsByAccountId(1)->getNumRows()); $this->assertEquals(0, self::$repository->deleteEditByAccountId(10)); @@ -298,17 +301,11 @@ class AccountToUserGroupRepositoryTest extends DatabaseTestCase */ public function testGetUserGroupsByUserGroupId() { - $userGroups = self::$repository->getUserGroupsByUserGroupId(2); + $this->assertCount(2, self::$repository->getUserGroupsByUserGroupId(2)->getNumRows()); - $this->assertCount(2, $userGroups); + $this->assertCount(0, self::$repository->getUserGroupsByUserGroupId(3)->getNumRows()); - $userGroups = self::$repository->getUserGroupsByUserGroupId(3); - - $this->assertCount(0, $userGroups); - - $userGroups = self::$repository->getUserGroupsByUserGroupId(10); - - $this->assertCount(0, $userGroups); + $this->assertCount(0, self::$repository->getUserGroupsByUserGroupId(10)->getNumRows()); } /** diff --git a/tests/Repositories/EventlogRepositoryTest.php b/tests/Repositories/EventlogRepositoryTest.php index d9e6debd..eb2207d1 100644 --- a/tests/Repositories/EventlogRepositoryTest.php +++ b/tests/Repositories/EventlogRepositoryTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Repositories; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Exceptions\ConstraintException; use SP\DataModel\EventlogData; use SP\DataModel\ItemSearchData; @@ -56,6 +55,8 @@ class EventlogRepositoryTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_eventlog.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -150,14 +151,4 @@ class EventlogRepositoryTest extends DatabaseTestCase self::$repository->create(new EventlogData()); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_eventlog.xml'); - } } diff --git a/tests/Repositories/PluginRepositoryTest.php b/tests/Repositories/PluginRepositoryTest.php index 3ed5b074..84333bf2 100644 --- a/tests/Repositories/PluginRepositoryTest.php +++ b/tests/Repositories/PluginRepositoryTest.php @@ -25,7 +25,6 @@ namespace SP\Tests\Repositories; use DI\DependencyException; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Exceptions\ConstraintException; use SP\DataModel\ItemData; use SP\DataModel\ItemSearchData; @@ -56,6 +55,8 @@ class PluginRepositoryTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_plugin.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -361,14 +362,4 @@ class PluginRepositoryTest extends DatabaseTestCase $this->assertEquals(0, $result->getNumRows()); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_plugin.xml'); - } } diff --git a/tests/Repositories/TrackRepositoryTest.php b/tests/Repositories/TrackRepositoryTest.php index 756f2f77..2f19e6f7 100644 --- a/tests/Repositories/TrackRepositoryTest.php +++ b/tests/Repositories/TrackRepositoryTest.php @@ -25,7 +25,6 @@ namespace SP\Tests\Repositories; use DI\DependencyException; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\DataModel\TrackData; use SP\Repositories\Track\TrackRepository; use SP\Repositories\Track\TrackRequest; @@ -54,6 +53,8 @@ class TrackRepositoryTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_track.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -174,14 +175,4 @@ class TrackRepositoryTest extends DatabaseTestCase $this->assertEquals(0, $result->getNumRows()); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_track.xml'); - } } diff --git a/tests/Services/AccountAclServiceTest.php b/tests/Services/AccountAclServiceTest.php index 482003cf..0872a8ed 100644 --- a/tests/Services/AccountAclServiceTest.php +++ b/tests/Services/AccountAclServiceTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Services; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Account\AccountAcl; use SP\Core\Acl\Acl; use SP\Core\Context\ContextInterface; @@ -87,6 +86,8 @@ class AccountAclServiceTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountAcl.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -781,14 +782,4 @@ class AccountAclServiceTest extends DatabaseTestCase $this->assertNull($service->getAclFromCache(1, 10)); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountAcl.xml'); - } } diff --git a/tests/Services/AccountCryptServiceTest.php b/tests/Services/AccountCryptServiceTest.php index 3bbb6376..ce97d4f6 100644 --- a/tests/Services/AccountCryptServiceTest.php +++ b/tests/Services/AccountCryptServiceTest.php @@ -25,7 +25,6 @@ namespace SP\Tests\Services; use Defuse\Crypto\Exception\CryptoException; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Crypt\Crypt; use SP\Services\Account\AccountCryptService; use SP\Services\Account\AccountService; @@ -63,6 +62,8 @@ class AccountCryptServiceTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountCrypt.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -145,14 +146,4 @@ class AccountCryptServiceTest extends DatabaseTestCase $this->assertEquals($request->getHash(), $account->getMPassHash()); $this->assertEquals('-{?^··\mjCcreateMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountCrypt.xml'); - } } diff --git a/tests/Services/AccountFileServiceTest.php b/tests/Services/AccountFileServiceTest.php index e477327b..fbef6195 100644 --- a/tests/Services/AccountFileServiceTest.php +++ b/tests/Services/AccountFileServiceTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Services; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Exceptions\InvalidImageException; use SP\DataModel\FileData; use SP\DataModel\FileExtData; @@ -57,6 +56,8 @@ class AccountFileServiceTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountFile.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -322,14 +323,4 @@ class AccountFileServiceTest extends DatabaseTestCase self::$service->deleteByIdBatch([10]); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountFile.xml'); - } } diff --git a/tests/Services/AccountHistoryServiceTest.php b/tests/Services/AccountHistoryServiceTest.php index b59273c7..227658dd 100644 --- a/tests/Services/AccountHistoryServiceTest.php +++ b/tests/Services/AccountHistoryServiceTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Services; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\DataModel\AccountHistoryData; use SP\DataModel\Dto\AccountHistoryCreateDto; use SP\DataModel\ItemSearchData; @@ -58,6 +57,8 @@ class AccountHistoryServiceTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountHistory.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -237,14 +238,4 @@ class AccountHistoryServiceTest extends DatabaseTestCase $this->assertEquals(pack('H*', '6465663130303030646566353032303032636635623034396437656539356531653838663166613438643061616132663133613163663766346238316165663837326134373665316461653661353865316666626438346130383166303062633138646136373265653935643234626564336565303063333262646262303433336633356534323263616337613238363532336233313666316137333462616337343839346631333632643863376430373861373862396135633064396239653061353537626562666336636566623766363166376330393734356461623536373762303436313865343936383434663932666364303634316330303935636239363938336361336631363161623134663339643536636233653938333833613062396464356365383736333334376364363933313563306436343362623937366139383831376632346431303364316533353133306262393862353034353262346334663934663162323531383632356530653331346438343430323362666334306264616265376437386238663632326535353338636537663431626261616461613138646333333662623762636565333030656565333734616537356365303131363731323239383132383964346634383661376635303136303835336138663335653366393230383632386162373332343335633037656432616234'), $data[0]->key); $this->assertEquals(pack('H*', '24327924313024787473754E325055766753482F306D7266426C73624F4163745667436A596371447143364C3354395172614E785A43345258475961'), $data[0]->mPassHash); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountHistory.xml'); - } } diff --git a/tests/Services/AccountSearchServiceTest.php b/tests/Services/AccountSearchServiceTest.php index 16137825..4bc5d427 100644 --- a/tests/Services/AccountSearchServiceTest.php +++ b/tests/Services/AccountSearchServiceTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Services; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Account\AccountSearchFilter; use SP\Account\AccountSearchItem; use SP\Core\Context\ContextInterface; @@ -62,6 +61,8 @@ class AccountSearchServiceTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountSearch.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -490,14 +491,4 @@ class AccountSearchServiceTest extends DatabaseTestCase $this->checkTags([1, 3], [2], QueryCondition::CONDITION_OR); $this->checkTags([2]); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountSearch.xml'); - } } diff --git a/tests/Services/AccountServiceTest.php b/tests/Services/AccountServiceTest.php new file mode 100644 index 00000000..7b46be22 --- /dev/null +++ b/tests/Services/AccountServiceTest.php @@ -0,0 +1,293 @@ +. + */ + +namespace SP\Tests\Services; + +use SP\Account\AccountRequest; +use SP\Core\Crypt\Crypt; +use SP\Repositories\NoSuchItemException; +use SP\Services\Account\AccountPasswordRequest; +use SP\Services\Account\AccountService; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; + +/** + * Class AccountServiceTest + * + * @package SP\Tests\Services + */ +class AccountServiceTest extends DatabaseTestCase +{ + const SECURE_KEY_PASSWORD = '12345678900'; + /** + * @var AccountService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + self::$dataset = 'syspass_account.xml'; + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el servicio + self::$service = $dic->get(AccountService::class); + } + + /** + * @covers \SP\Services\Account\AccountService::withTagsById() + * @covers \SP\Services\Account\AccountService::withUsersById() + * @covers \SP\Services\Account\AccountService::withUserGroupsById() + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + * @throws \Defuse\Crypto\Exception\CryptoException + */ + public function testCreate() + { + $accountRequest = new AccountRequest(); + $accountRequest->name = 'Prueba 2'; + $accountRequest->login = 'admin'; + $accountRequest->url = 'http://syspass.org'; + $accountRequest->notes = 'notas'; + $accountRequest->userEditId = 1; + $accountRequest->passDateChange = time() + 3600; + $accountRequest->clientId = 1; + $accountRequest->categoryId = 1; + $accountRequest->isPrivate = 0; + $accountRequest->isPrivateGroup = 0; + $accountRequest->parentId = 0; + $accountRequest->userId = 1; + $accountRequest->userGroupId = 2; + $accountRequest->pass = '1234abc'; + $accountRequest->tags = [2, 3]; + $accountRequest->usersView = [2, 4]; + $accountRequest->usersEdit = [3, 4]; + $accountRequest->userGroupsView = [2, 3]; + $accountRequest->userGroupsEdit = [2]; + + $this->assertEquals(3, self::$service->create($accountRequest)); + + $result = self::$service->getById(3); + + self::$service->withTagsById($result); + self::$service->withUsersById($result); + self::$service->withUserGroupsById($result); + + $data = $result->getAccountVData(); + + $this->assertEquals(3, $result->getId()); + $this->assertEquals($accountRequest->name, $data->getName()); + $this->assertEquals($accountRequest->login, $data->getLogin()); + $this->assertEquals($accountRequest->url, $data->getUrl()); + $this->assertEquals($accountRequest->notes, $data->getNotes()); + $this->assertEquals($accountRequest->userId, $data->getUserId()); + $this->assertEquals($accountRequest->userGroupId, $data->getUserGroupId()); + $this->assertEquals($accountRequest->userEditId, $data->getUserEditId()); + $this->assertEquals($accountRequest->passDateChange, $data->getPassDateChange()); + $this->assertEquals($accountRequest->clientId, $data->getClientId()); + $this->assertEquals($accountRequest->categoryId, $data->getCategoryId()); + $this->assertEquals($accountRequest->isPrivate, $data->getIsPrivate()); + $this->assertEquals($accountRequest->isPrivateGroup, $data->getIsPrivateGroup()); + $this->assertEquals($accountRequest->parentId, $data->getParentId()); + + $tags = $result->getTags(); + + $this->assertEquals(3, $tags[0]->getId()); + $this->assertEquals(2, $tags[1]->getId()); + + $users = $result->getUsers(); + + $this->assertEquals(2, $users[0]->getId()); + $this->assertEquals(0, (int)$users[0]->isEdit); + $this->assertEquals(3, $users[1]->getId()); + $this->assertEquals(1, (int)$users[1]->isEdit); + $this->assertEquals(4, $users[2]->getId()); + $this->assertEquals(1, (int)$users[2]->isEdit); + + $groups = $result->getUserGroups(); + + $this->assertEquals(2, $groups[0]->getId()); + $this->assertEquals(1, (int)$groups[0]->isEdit); + $this->assertEquals(3, $groups[1]->getId()); + $this->assertEquals(0, (int)$groups[1]->isEdit); + + $data = self::$service->getPasswordForId(3); + + $this->assertEquals('1234abc', Crypt::decrypt($data->getPass(), $data->getKey(), self::SECURE_KEY_PASSWORD)); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Repositories\NoSuchItemException + */ + public function testDelete() + { + // Comprobar registros iniciales + $this->assertEquals(2, $this->conn->getRowCount('Account')); + + // Eliminar registros y comprobar el total de registros + self::$service->delete(1); + self::$service->delete(2); + + $this->assertEquals(0, $this->conn->getRowCount('Account')); + + $this->expectException(NoSuchItemException::class); + + // Eliminar un registro no existente + $this->assertEquals(0, self::$service->delete(100)); + } + + /** + * @throws \Defuse\Crypto\Exception\CryptoException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException + */ + public function testUpdatePasswordMasterPass() + { + $accountRequest = new AccountPasswordRequest(); + $accountRequest->id = 2; + $accountRequest->key = Crypt::makeSecuredKey(self::SECURE_KEY_PASSWORD); + $accountRequest->pass = Crypt::encrypt('1234', $accountRequest->key, self::SECURE_KEY_PASSWORD); + + // Comprobar que la modificación de la clave es correcta + $this->assertTrue(self::$service->updatePasswordMasterPass($accountRequest)); + + $data = self::$service->getPasswordForId(2); + $clearPassword = Crypt::decrypt($data->pass, $data->key, self::SECURE_KEY_PASSWORD); + + // Comprobar que la clave obtenida es igual a la encriptada anteriormente + $this->assertEquals('1234', $clearPassword); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetTotalNumAccounts() + { + $this->assertEquals(2, self::$service->getTotalNumAccounts()); + } + + /** + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetDataForLink() + { + self::$service->getDataForLink(1); + } + + public function testGetAccountsPassData() + { + + } + + public function testEditRestore() + { + + } + + public function testGetLinked() + { + + } + + public function testGetPasswordEncrypted() + { + + } + + public function testEditPassword() + { + + } + + public function testGetPasswordForId() + { + + } + + public function testUpdate() + { + + } + + public function testGetForUser() + { + + } + + public function testGetById() + { + + } + + public function testDeleteByIdBatch() + { + + } + + public function testGetByFilter() + { + + } + + public function testSearch() + { + + } + + public function testIncrementDecryptCounter() + { + + } + + public function testIncrementViewCounter() + { + + } + + public function testGetPasswordHistoryForId() + { + + } + + public function testGetAllBasic() + { + + } +} diff --git a/tests/Services/AccountFavoriteServiceTest.php b/tests/Services/AccountToFavoriteServiceTest.php similarity index 88% rename from tests/Services/AccountFavoriteServiceTest.php rename to tests/Services/AccountToFavoriteServiceTest.php index afa39645..d8128b50 100644 --- a/tests/Services/AccountFavoriteServiceTest.php +++ b/tests/Services/AccountToFavoriteServiceTest.php @@ -24,7 +24,6 @@ namespace SP\Tests\Services; -use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Exceptions\ConstraintException; use SP\Services\Account\AccountToFavoriteService; use SP\Storage\Database\DatabaseConnectionData; @@ -36,7 +35,7 @@ use function SP\Tests\setupContext; * * @package SP\Tests\Services */ -class AccountFavoriteServiceTest extends DatabaseTestCase +class AccountToFavoriteServiceTest extends DatabaseTestCase { /** * @var AccountToFavoriteService @@ -52,6 +51,8 @@ class AccountFavoriteServiceTest extends DatabaseTestCase { $dic = setupContext(); + self::$dataset = 'syspass_accountFavorite.xml'; + // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); @@ -100,14 +101,4 @@ class AccountFavoriteServiceTest extends DatabaseTestCase self::$service->add(1, 3); } - - /** - * Returns the test dataset. - * - * @return IDataSet - */ - protected function getDataSet() - { - return $this->createMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountFavorite.xml'); - } } diff --git a/tests/Services/AccountToTagServiceTest.php b/tests/Services/AccountToTagServiceTest.php new file mode 100644 index 00000000..8a02ec47 --- /dev/null +++ b/tests/Services/AccountToTagServiceTest.php @@ -0,0 +1,69 @@ +. + */ + +namespace SP\Tests\Services; + +use SP\Services\Account\AccountToTagService; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; + +/** + * Class AccountToTagServiceTest + * + * @package SP\Tests\Services + */ +class AccountToTagServiceTest extends DatabaseTestCase +{ + /** + * @var AccountToTagService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el servicio + self::$service = $dic->get(AccountToTagService::class); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetTagsByAccountId() + { + $this->assertCount(1, self::$service->getTagsByAccountId(1)); + $this->assertCount(0, self::$service->getTagsByAccountId(10)); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 96d481a8..cc8e4d3f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -81,6 +81,7 @@ function setupContext() $context = $dic->get(ContextInterface::class); $context->initialize(); $context->setConfig(new ConfigData()); + $context->setTrasientKey('_masterpass', '12345678900'); $userData = new UserLoginResponse(); $userData->setId(1); diff --git a/tests/res/datasets/syspass_account.xml b/tests/res/datasets/syspass_account.xml new file mode 100644 index 00000000..cdc2e156 --- /dev/null +++ b/tests/res/datasets/syspass_account.xml @@ -0,0 +1,89 @@ + + + + + + 1 + 1 + 1 + 1 + 1 + Google + 1 + admin + http://google.com + 6465663530323030656135663361636362366237656462653536343938666234313231616635323237363539663162346532383963386361346565323732656530636238333632316436393736353665373631393435623033353236616164333730336662306531333535626437333638653033666137623565633364306365323634663863643436393436633365353234316534373338376130393133663935303736396364613365313234643432306636393834386434613262316231306138 + 6465663130303030646566353032303065646434636466636231333437613739616166313734343462343839626362643364353664376664356562373233363235653130316261666432323539343633336664626639326630613135373461653562613562323535353230393236353237623863633534313862653363376361376536366139356366353366356162663031623064343236613234336162643533643837643239636633643165326532663732626664396433366133653061343534656664373134633661366237616338363966636263366435303166613964316338386365623264303861333438626633656638653135356538633865353838623938636465653061306463313835646636366535393138393831653366303464323139386236383738333539616563653034376434643637663835313235636661313237633138373865643530616630393434613934616363356265316130323566623065633362663831613933626365366365343734336164363562656638353131343466343332323837356438323339303236656363613866643862376330396563356465373233666466313636656166386336356539666537353436333535333664393766383366316366663931396530386339373730636166633136376661656364306366656262323931666334343831333238333662366432 + aaaa + 341 + 35 + 2018-03-25 09:54:07 + 2018-04-02 21:38:25 + 0 + 0 + 0 + 0 + 1522341709 + 0 + 0 + + + 2 + 1 + 1 + 1 + 2 + Apple + 2 + admin + http://apple.com + 6465663530323030656135663361636362366237656462653536343938666234313231616635323237363539663162346532383963386361346565323732656530636238333632316436393736353665373631393435623033353236616164333730336662306531333535626437333638653033666137623565633364306365323634663863643436393436633365353234316534373338376130393133663935303736396364613365313234643432306636393834386434613262316231306138 + 6465663130303030646566353032303065646434636466636231333437613739616166313734343462343839626362643364353664376664356562373233363235653130316261666432323539343633336664626639326630613135373461653562613562323535353230393236353237623863633534313862653363376361376536366139356366353366356162663031623064343236613234336162643533643837643239636633643165326532663732626664396433366133653061343534656664373134633661366237616338363966636263366435303166613964316338386365623264303861333438626633656638653135356538633865353838623938636465653061306463313835646636366535393138393831653366303464323139386236383738333539616563653034376434643637663835313235636661313237633138373865643530616630393434613934616363356265316130323566623065633362663831613933626365366365343734336164363562656638353131343466343332323837356438323339303236656363613866643862376330396563356465373233666466313636656166386336356539666537353436333535333664393766383366316366663931396530386339373730636166633136376661656364306366656262323931666334343831333238333662366432 + bbbb + 341 + 35 + 2018-03-25 09:54:07 + 2018-04-02 21:38:25 + 0 + 0 + 0 + 0 + 1522341709 + 0 + 0 + + + + + + + 1 + 1 + + + + + 1 + 3 + 1 + + + 2 + 2 + 0 + + + + + 1 + 2 + 1 + + + 2 + 2 + 0 + + + +