From d56bc9dad13de94afbe0b0f072857ce59af97cfb Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Mon, 16 Jul 2018 00:53:19 +0200 Subject: [PATCH] * [ADD] Unit testing. Work in progress * [MOD] Code refactoring --- .../web/Controllers/Traits/ItemTrait.php | 21 +- .../CustomField/CustomFieldDefRepository.php | 33 ++- .../CustomField/CustomFieldRepository.php | 2 +- .../CustomField/CustomFieldCryptService.php | 33 +-- .../CustomField/CustomFieldDefService.php | 85 +++--- .../CustomField/CustomFieldService.php | 18 +- .../CustomFieldDefRepositoryTest.php | 15 +- .../CustomFieldRepositoryTest.php | 23 +- .../CustomFieldCryptServiceTest.php | 89 +++++++ .../CustomField/CustomFieldDefServiceTest.php | 250 ++++++++++++++++++ tests/res/datasets/syspass_customField.xml | 66 +++++ 11 files changed, 531 insertions(+), 104 deletions(-) create mode 100644 tests/Services/CustomField/CustomFieldCryptServiceTest.php create mode 100644 tests/Services/CustomField/CustomFieldDefServiceTest.php create mode 100644 tests/res/datasets/syspass_customField.xml diff --git a/app/modules/web/Controllers/Traits/ItemTrait.php b/app/modules/web/Controllers/Traits/ItemTrait.php index acdfd115..6aa13622 100644 --- a/app/modules/web/Controllers/Traits/ItemTrait.php +++ b/app/modules/web/Controllers/Traits/ItemTrait.php @@ -26,7 +26,6 @@ namespace SP\Modules\Web\Controllers\Traits; use Defuse\Crypto\Exception\CryptoException; use SP\Bootstrap; -use SP\Core\Context\SessionContext; use SP\Core\Exceptions\SPException; use SP\DataModel\CustomFieldData; use SP\DataModel\ItemSearchData; @@ -44,14 +43,15 @@ trait ItemTrait /** * Obtener la lista de campos personalizados y sus valores * - * @param int $moduleId - * @param int $itemId - * @param SessionContext $sessionContext + * @param int $moduleId + * @param int $itemId + * * @return array - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException */ - protected function getCustomFieldsForItem($moduleId, $itemId, SessionContext $sessionContext) + protected function getCustomFieldsForItem($moduleId, $itemId) { $customFieldService = Bootstrap::getContainer()->get(CustomFieldService::class); $customFields = []; @@ -75,7 +75,7 @@ trait ItemTrait && $item->key !== null ) { $customField->isValueEncrypted = true; - $customField->value = CustomFieldService::decryptData($item->data, $item->key, $sessionContext); + $customField->value = $customFieldService->decryptData($item->data, $item->key); } else { $customField->isValueEncrypted = false; $customField->value = $item->data; @@ -129,6 +129,7 @@ trait ItemTrait * * @param int $moduleId * @param int|int[] $itemId + * * @throws SPException * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface @@ -168,7 +169,9 @@ trait ItemTrait $customFieldData->setDefinitionId($id); $customFieldData->setData($value); - $customFieldService->update($customFieldData); + if ($customFieldService->update($customFieldData) !== 1) { + throw new SPException(__u('Error al actualizar los datos del campo personalizado')); + } } } catch (CryptoException $e) { throw new SPException(__u('Error interno'), SPException::ERROR); diff --git a/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php b/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php index bac4696a..c818de38 100644 --- a/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php +++ b/lib/SP/Repositories/CustomField/CustomFieldDefRepository.php @@ -32,6 +32,7 @@ use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; use SP\Storage\Database\QueryData; +use SP\Storage\Database\QueryResult; /** * Class CustomFieldDefRepository @@ -87,9 +88,20 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter */ public function update($itemData) { + if ($this->customFieldDefCollection->exists($itemData->getId())) { + $this->customFieldDefCollection->remove($itemData->getId()); + } + $query = /** @lang SQL */ 'UPDATE CustomFieldDefinition - SET `name` = ?, moduleId = ?, required = ?, `help` = ?, showInList = ?, typeId = ?, isEncrypted = ?, field = NULL + SET `name` = ?, + moduleId = ?, + required = ?, + `help` = ?, + showInList = ?, + typeId = ?, + isEncrypted = ?, + field = NULL WHERE id = ? LIMIT 1'; $queryData = new QueryData(); @@ -126,7 +138,14 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter } $query = /** @lang SQL */ - 'SELECT id, `name`, moduleId, required, `help`, showInList, typeId, isEncrypted + 'SELECT id, + `name`, + moduleId, + required, + `help`, + showInList, + typeId, + isEncrypted FROM CustomFieldDefinition WHERE id = ? LIMIT 1'; @@ -149,7 +168,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter /** * Returns all the items * - * @return CustomFieldDefinitionData[] + * @return \SP\Storage\Database\QueryResult * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -164,7 +183,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->setMapClassName(CustomFieldDefinitionData::class); $queryData->setQuery($query); - return $this->db->doSelect($queryData)->getDataAsArray(); + return $this->db->doSelect($queryData); } /** @@ -172,14 +191,14 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter * * @param array $ids * - * @return CustomFieldDefinitionData[] + * @return QueryResult * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function getByIdBatch(array $ids) { if (empty($ids)) { - return []; + return new QueryResult(); } $query = /** @lang SQL */ @@ -193,7 +212,7 @@ class CustomFieldDefRepository extends Repository implements RepositoryItemInter $queryData->setQuery($query); $queryData->setParams($ids); - return $this->db->doSelect($queryData)->getDataAsArray(); + return $this->db->doSelect($queryData); } /** diff --git a/lib/SP/Repositories/CustomField/CustomFieldRepository.php b/lib/SP/Repositories/CustomField/CustomFieldRepository.php index be1a288a..531dda46 100644 --- a/lib/SP/Repositories/CustomField/CustomFieldRepository.php +++ b/lib/SP/Repositories/CustomField/CustomFieldRepository.php @@ -300,7 +300,7 @@ class CustomFieldRepository extends Repository implements RepositoryItemInterfac { $queryData = new QueryData(); $queryData->setMapClassName(CustomFieldData::class); - $queryData->setQuery('SELECT * FROM CustomFieldData WHERE `key` IS NOT NULL'); + $queryData->setQuery('SELECT * FROM CustomFieldData WHERE `key` IS NOT NULL ORDER BY definitionId'); return $this->db->doSelect($queryData); } diff --git a/lib/SP/Services/CustomField/CustomFieldCryptService.php b/lib/SP/Services/CustomField/CustomFieldCryptService.php index 128274a8..2cb6ecac 100644 --- a/lib/SP/Services/CustomField/CustomFieldCryptService.php +++ b/lib/SP/Services/CustomField/CustomFieldCryptService.php @@ -27,7 +27,6 @@ namespace SP\Services\CustomField; defined('APP_ROOT') || die(); use SP\Core\Crypt\Crypt; -use SP\Core\Crypt\OldCrypt; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\DataModel\CustomFieldData; @@ -52,36 +51,11 @@ class CustomFieldCryptService extends Service */ protected $request; - /** - * Actualizar los datos encriptados con una nueva clave - * - * @param UpdateMasterPassRequest $request - * @throws ServiceException - */ - public function updateMasterPasswordOld(UpdateMasterPassRequest $request) - { - $this->request = $request; - - try { - $this->processUpdateMasterPassword(function (CustomFieldData $customFieldData) { - return OldCrypt::getDecrypt($customFieldData->getData(), $customFieldData->getKey(), $this->request->getCurrentMasterPass()); - }); - } catch (ServiceException $e) { - throw $e; - } catch (\Exception $e) { - $this->eventDispatcher->notifyEvent('exception', new Event($e)); - - throw new ServiceException( - __u('Errores al actualizar datos de campos personalizados'), - ServiceException::ERROR, - null, - $e->getCode(), - $e); - } - } - /** * @param callable $decryptor + * + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ protected function processUpdateMasterPassword(callable $decryptor) { @@ -173,6 +147,5 @@ class CustomFieldCryptService extends Service protected function initialize() { $this->customFieldService = $this->dic->get(CustomFieldService::class); - } } \ No newline at end of file diff --git a/lib/SP/Services/CustomField/CustomFieldDefService.php b/lib/SP/Services/CustomField/CustomFieldDefService.php index b0f2d6df..792db71e 100644 --- a/lib/SP/Services/CustomField/CustomFieldDefService.php +++ b/lib/SP/Services/CustomField/CustomFieldDefService.php @@ -28,6 +28,7 @@ use SP\Core\Acl\ActionsInterface; use SP\DataModel\CustomFieldDefinitionData; use SP\DataModel\ItemSearchData; use SP\Repositories\CustomField\CustomFieldDefRepository; +use SP\Repositories\NoSuchItemException; use SP\Services\Service; use SP\Services\ServiceException; use SP\Services\ServiceItemTrait; @@ -45,6 +46,10 @@ class CustomFieldDefService extends Service * @var CustomFieldDefRepository */ protected $customFieldDefRepository; + /** + * @var CustomFieldService + */ + protected $customFieldService; /** * @param $id @@ -88,34 +93,22 @@ class CustomFieldDefService extends Service return $this->customFieldDefRepository->search($itemSearchData); } - /** - * @param $id - * - * @return \SP\DataModel\CustomFieldDefinitionData - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Repositories\NoSuchItemException - */ - public function getById($id) - { - return $this->customFieldDefRepository->getById($id); - } - /** * @param $id * * @return CustomFieldDefService * @throws ServiceException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Core\Exceptions\SPException */ public function delete($id) { - if ($this->dic->get(CustomFieldService::class)->deleteCustomFieldDefinitionData($id) === 0 - || $this->customFieldDefRepository->delete($id) === 0) { - throw new ServiceException(__u('Campo no encontrado'), ServiceException::INFO); - } + $this->transactionAware(function () use ($id) { + $this->dic->get(CustomFieldService::class) + ->deleteCustomFieldDefinitionData($id); + + if ($this->customFieldDefRepository->delete($id) === 0) { + throw new NoSuchItemException(__u('Campo no encontrado'), NoSuchItemException::INFO); + } + }); return $this; } @@ -125,22 +118,18 @@ class CustomFieldDefService extends Service * * @param array $ids * - * @return int * @throws ServiceException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - * @throws \SP\Core\Exceptions\SPException */ public function deleteByIdBatch(array $ids) { - $numIds = count($ids); + $this->transactionAware(function () use ($ids) { + $this->dic->get(CustomFieldService::class) + ->deleteCustomFieldDefinitionDataBatch($ids); - if ($this->dic->get(CustomFieldService::class)->deleteCustomFieldDefinitionDataBatch($ids) !== $numIds - || ($count = $this->customFieldDefRepository->deleteByIdBatch($ids)) !== $numIds) { - throw new ServiceException(__u('Error al eliminar los campos'), ServiceException::WARNING); - } - - return $count; + if ($this->customFieldDefRepository->deleteByIdBatch($ids) !== count($ids)) { + throw new ServiceException(__u('Error al eliminar los campos'), ServiceException::WARNING); + } + }); } /** @@ -156,15 +145,39 @@ class CustomFieldDefService extends Service } /** - * @param $itemData + * @param CustomFieldDefinitionData $itemData * * @return mixed - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException + * @throws ServiceException */ public function update(CustomFieldDefinitionData $itemData) { - return $this->customFieldDefRepository->update($itemData); + return $this->transactionAware(function () use ($itemData) { + $customFieldDefinitionData = $this->getById($itemData->getId()); + + // Delete the data used by the items using the previous definition + if ($customFieldDefinitionData->getModuleId() !== $itemData->moduleId) { + $this->dic->get(CustomFieldService::class) + ->deleteCustomFieldDefinitionData($customFieldDefinitionData->getId()); + } + + if ($this->customFieldDefRepository->update($itemData) !== 1) { + throw new ServiceException(__u('Error al actualizar el campo personalizado')); + } + }); + } + + /** + * @param $id + * + * @return \SP\DataModel\CustomFieldDefinitionData + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Repositories\NoSuchItemException + */ + public function getById($id) + { + return $this->customFieldDefRepository->getById($id); } /** @@ -176,7 +189,7 @@ class CustomFieldDefService extends Service */ public function getAllBasic() { - return $this->customFieldDefRepository->getAll(); + return $this->customFieldDefRepository->getAll()->getDataAsArray(); } /** diff --git a/lib/SP/Services/CustomField/CustomFieldService.php b/lib/SP/Services/CustomField/CustomFieldService.php index 4e476f45..6cfbcf6c 100644 --- a/lib/SP/Services/CustomField/CustomFieldService.php +++ b/lib/SP/Services/CustomField/CustomFieldService.php @@ -25,9 +25,7 @@ namespace SP\Services\CustomField; use Defuse\Crypto\Exception\CryptoException; -use SP\Core\Context\SessionContext; use SP\Core\Crypt\Crypt; -use SP\Core\Crypt\Session as CryptSession; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\CustomFieldData; @@ -66,17 +64,17 @@ class CustomFieldService extends Service /** * Desencriptar y formatear los datos del campo * - * @param string $data - * @param string $key - * @param SessionContext $sessionContext + * @param string $data + * @param string $key * * @return string * @throws CryptoException + * @throws ServiceException */ - public static function decryptData($data, $key, SessionContext $sessionContext) + public function decryptData($data, $key) { - if (!empty($data) && empty($key)) { - return self::formatValue(Crypt::decrypt($data, $key, CryptSession::getSessionKey($sessionContext))); + if (!empty($data) && !empty($key)) { + return self::formatValue(Crypt::decrypt($data, $key, $this->getMasterKeyFromContext())); } return ''; @@ -105,6 +103,8 @@ class CustomFieldService extends Service * @param $itemId * * @return array + * @throws QueryException + * @throws \SP\Core\Exceptions\ConstraintException */ public function getForModuleById($moduleId, $itemId) { @@ -196,7 +196,7 @@ class CustomFieldService extends Service */ protected function setSecureData(CustomFieldData $customFieldData, $key = null) { - $key = $key ?: CryptSession::getSessionKey($this->context); + $key = $key ?: $this->getMasterKeyFromContext(); $securedKey = Crypt::makeSecuredKey($key); if (strlen($securedKey) > 1000) { diff --git a/tests/Repositories/CustomFieldDefRepositoryTest.php b/tests/Repositories/CustomFieldDefRepositoryTest.php index 9f2a356d..4310df3f 100644 --- a/tests/Repositories/CustomFieldDefRepositoryTest.php +++ b/tests/Repositories/CustomFieldDefRepositoryTest.php @@ -141,7 +141,11 @@ class CustomFieldDefRepositoryTest extends DatabaseTestCase */ public function testGetByIdBatch() { - $data = self::$repository->getByIdBatch([1, 2]); + $result = self::$repository->getByIdBatch([1, 2]); + + $this->assertEquals(2, $result->getNumRows()); + + $data = $result->getDataAsArray(); $this->assertCount(2, $data); $this->assertInstanceOf(CustomFieldDefinitionData::class, $data[0]); @@ -170,9 +174,9 @@ class CustomFieldDefRepositoryTest extends DatabaseTestCase $this->assertEquals($expected, $data[1]); - $this->assertCount(0, self::$repository->getByIdBatch([10])); + $this->assertEquals(0, self::$repository->getByIdBatch([10])->getNumRows()); - $this->assertCount(0, self::$repository->getByIdBatch([])); + $this->assertEquals(0, self::$repository->getByIdBatch([])->getNumRows()); } /** @@ -205,7 +209,10 @@ class CustomFieldDefRepositoryTest extends DatabaseTestCase { self::$repository->resetCollection(); - $data = self::$repository->getAll(); + $result = self::$repository->getAll(); + $this->assertEquals(3, $result->getNumRows()); + + $data = $result->getDataAsArray(); $this->assertCount(3, $data); diff --git a/tests/Repositories/CustomFieldRepositoryTest.php b/tests/Repositories/CustomFieldRepositoryTest.php index f5035bb8..41299c48 100644 --- a/tests/Repositories/CustomFieldRepositoryTest.php +++ b/tests/Repositories/CustomFieldRepositoryTest.php @@ -131,12 +131,16 @@ class CustomFieldRepositoryTest extends DatabaseTestCase public function testGetAllEncrypted() { $result = self::$repository->getAllEncrypted(); + $this->assertEquals(1, $result->getNumRows()); - $this->assertCount(1, $result); - $this->assertInstanceOf(CustomFieldData::class, $result[0]); - $this->assertEquals(1, $result[0]->getItemId()); - $this->assertEquals(ActionsInterface::ACCOUNT, $result[0]->getModuleId()); - $this->assertEquals(1, $result[0]->getItemId()); + /** @var CustomFieldData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertCount(1, $data); + $this->assertInstanceOf(CustomFieldData::class, $data[0]); + $this->assertEquals(1, $data[0]->getItemId()); + $this->assertEquals(ActionsInterface::ACCOUNT, $data[0]->getModuleId()); + $this->assertEquals(1, $data[0]->getItemId()); } /** @@ -159,10 +163,13 @@ class CustomFieldRepositoryTest extends DatabaseTestCase public function testGetAll() { $result = self::$repository->getAll(); + $this->assertEquals(2, $result->getNumRows()); - $this->assertCount(2, $result); - $this->assertInstanceOf(CustomFieldData::class, $result[0]); - $this->assertInstanceOf(CustomFieldData::class, $result[1]); + $data = $result->getDataAsArray(); + + $this->assertCount(2, $data); + $this->assertInstanceOf(CustomFieldData::class, $data[0]); + $this->assertInstanceOf(CustomFieldData::class, $data[1]); } /** diff --git a/tests/Services/CustomField/CustomFieldCryptServiceTest.php b/tests/Services/CustomField/CustomFieldCryptServiceTest.php new file mode 100644 index 00000000..ef37cfbb --- /dev/null +++ b/tests/Services/CustomField/CustomFieldCryptServiceTest.php @@ -0,0 +1,89 @@ +. + */ + +namespace SP\Tests\Services\CustomField; + +use SP\Core\Crypt\Crypt; +use SP\Services\Crypt\UpdateMasterPassRequest; +use SP\Services\CustomField\CustomFieldCryptService; +use SP\Services\CustomField\CustomFieldService; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use SP\Tests\Services\Account\AccountCryptServiceTest; +use function SP\Tests\setupContext; + +/** + * Class CustomFieldCryptServiceTest + * + * @package SP\Tests\Services\CustomField + */ +class CustomFieldCryptServiceTest extends DatabaseTestCase +{ + /** + * @var CustomFieldService + */ + private static $customFieldService; + /** + * @var CustomFieldCryptService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + self::$dataset = 'syspass_accountCrypt.xml'; + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el repositorio + self::$service = $dic->get(CustomFieldCryptService::class); + self::$customFieldService = $dic->get(CustomFieldService::class); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + * @throws \Defuse\Crypto\Exception\CryptoException + */ + public function testUpdateMasterPassword() + { + $request = new UpdateMasterPassRequest(AccountCryptServiceTest::CURRENT_MASTERPASS, AccountCryptServiceTest::NEW_MASTERPASS, AccountCryptServiceTest::CURRENT_HASH); + + self::$service->updateMasterPassword($request); + + $result = self::$customFieldService->getAllEncrypted(); + + $data = Crypt::decrypt($result[0]->getData(), $result[0]->getKey(), AccountCryptServiceTest::NEW_MASTERPASS); + + $this->assertEquals('1234', $data); + } +} diff --git a/tests/Services/CustomField/CustomFieldDefServiceTest.php b/tests/Services/CustomField/CustomFieldDefServiceTest.php new file mode 100644 index 00000000..0a34cd27 --- /dev/null +++ b/tests/Services/CustomField/CustomFieldDefServiceTest.php @@ -0,0 +1,250 @@ +. + */ + +namespace SP\Tests\Services\CustomField; + +use SP\Core\Acl\ActionsInterface; +use SP\Core\Exceptions\ConstraintException; +use SP\DataModel\CustomFieldDefinitionData; +use SP\DataModel\ItemSearchData; +use SP\Repositories\NoSuchItemException; +use SP\Services\CustomField\CustomFieldDefService; +use SP\Services\ServiceException; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Tests\DatabaseTestCase; +use function SP\Tests\setupContext; + +/** + * Class CustomFieldDefServiceTest + * + * @package SP\Tests\Services\CustomField + */ +class CustomFieldDefServiceTest extends DatabaseTestCase +{ + /** + * @var CustomFieldDefService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + self::$dataset = 'syspass_customField.xml'; + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el repositorio + self::$service = $dic->get(CustomFieldDefService::class); + } + + /** + * @throws \SP\Services\ServiceException + */ + public function testDelete() + { + self::$service->delete(3); + + $this->expectException(NoSuchItemException::class); + + self::$service->delete(10); + + $this->expectException(ConstraintException::class); + + self::$service->delete(1); + + $this->assertEquals(2, $this->conn->getRowCount('CustomFieldDefinition')); + $this->assertEquals(3, $this->conn->getRowCount('CustomFieldData')); + } + + /** + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetAllBasic() + { + $data = self::$service->getAllBasic(); + + $this->assertCount(3, $data); + + $expected = new CustomFieldDefinitionData(); + $expected->setId(1); + $expected->setName('Prueba'); + $expected->setIsEncrypted(1); + $expected->setHelp('Ayuda'); + $expected->setModuleId(ActionsInterface::ACCOUNT); + $expected->setRequired(true); + $expected->setTypeId(1); + $expected->setShowInList(0); + + $this->assertEquals($expected, $data[0]); + + $expected = new CustomFieldDefinitionData(); + $expected->setId(2); + $expected->setName('RSA'); + $expected->setIsEncrypted(0); + $expected->setModuleId(ActionsInterface::CATEGORY); + $expected->setRequired(false); + $expected->setTypeId(2); + $expected->setShowInList(0); + + $this->assertEquals($expected, $data[1]); + } + + /** + * @throws \SP\Services\ServiceException + */ + public function testDeleteByIdBatch() + { + self::$service->deleteByIdBatch([3]); + + self::$service->deleteByIdBatch([]); + + $this->expectException(ServiceException::class); + + self::$service->deleteByIdBatch([3, 4]); + + $this->expectException(ConstraintException::class); + + self::$service->deleteByIdBatch([1, 2]); + + $this->assertEquals(2, $this->conn->getRowCount('CustomFieldDefinition')); + $this->assertEquals(3, $this->conn->getRowCount('CustomFieldData')); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testCreate() + { + $data = new CustomFieldDefinitionData(); + $data->setId(4); + $data->setName('Phone'); + $data->setIsEncrypted(0); + $data->setHelp('Telefono'); + $data->setModuleId(ActionsInterface::ACCOUNT); + $data->setRequired(true); + $data->setTypeId(6); + $data->setShowInList(0); + + $this->assertEquals(4, self::$service->create($data)); + + $this->assertEquals(4, $this->conn->getRowCount('CustomFieldDefinition')); + + $this->assertEquals($data, self::$service->getById(4)); + + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetById() + { + $expected = new CustomFieldDefinitionData(); + $expected->setId(1); + $expected->setName('Prueba'); + $expected->setIsEncrypted(1); + $expected->setHelp('Ayuda'); + $expected->setModuleId(ActionsInterface::ACCOUNT); + $expected->setRequired(true); + $expected->setTypeId(1); + $expected->setShowInList(0); + + $this->assertEquals($expected, self::$service->getById(1)); + + $this->expectException(NoSuchItemException::class); + + $this->assertEquals($expected, self::$service->getById(10)); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + */ + public function testUpdate() + { + $data = new CustomFieldDefinitionData(); + $data->setId(1); + $data->setName('PIN'); + $data->setIsEncrypted(0); + $data->setHelp('Pin code'); + $data->setModuleId(ActionsInterface::CLIENT); + $data->setRequired(false); + $data->setTypeId(2); + $data->setShowInList(1); + + self::$service->update($data); + + $dataUpdated = self::$service->getById(1); + + $this->assertEquals($data, $dataUpdated); + + $this->assertEquals(1, $this->conn->getRowCount('CustomFieldData')); + + $data->setTypeId(100); + + $this->expectException(ConstraintException::class); + + $this->assertEquals(1, self::$service->update($data)); + } + + /** + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testSearch() + { + $itemSearchData = new ItemSearchData(); + $itemSearchData->setSeachString('RSA'); + $itemSearchData->setLimitCount(10); + + $result = self::$service->search($itemSearchData); + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertEquals(1, $result->getTotalNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(CustomFieldDefinitionData::class, $data[0]); + $this->assertEquals(2, $data[0]->id); + $this->assertEquals('password', $data[0]->typeName); + + $itemSearchData = new ItemSearchData(); + $itemSearchData->setSeachString('test'); + $itemSearchData->setLimitCount(10); + + $this->assertEquals(0, self::$service->search($itemSearchData)->getNumRows()); + } +} diff --git a/tests/res/datasets/syspass_customField.xml b/tests/res/datasets/syspass_customField.xml new file mode 100644 index 00000000..b25d8f26 --- /dev/null +++ b/tests/res/datasets/syspass_customField.xml @@ -0,0 +1,66 @@ + + + + + + 1 + Prueba + 10 + + 1 + Ayuda + 0 + 1 + 1 + + + 2 + RSA + 61 + + 0 + + 0 + 2 + 0 + + + 3 + SSL + 61 + + 0 + + 0 + 10 + 1 + + + + + 1 + 10 + 1 + 1 + 6465663530323030633262353536613539613465333330646461323833363730363462623861326463336630643963386565333935366134326631326135326261323035633036663063313933313263626465353630396562303133356364613461353738636534616263323436343235613739343338663461393231353433623437633062386134363566336466663131373061613162663532356466646434383165613664333763303537396132 + 6465663130303030646566353032303061356237393366343238663337393936356539393836656663363632396332613462336662323431666131343731326332333138323465376632366639313863383663653164636330393838333735343463326237316232383361663135633731363438326630303863313135326563623238383939313939346139376165613836623432613534333166383261343734343565636336376137643462633266396263343065653162333236343030373163333334386338626331613632323165613534346433396630636537343538356561653432376266373131633864366237336166316561613237623630643863626631643531666636366133366562636364353232643538633734653664626363613534646334366662303739626631653537626530646231643363316464313264303139633665663437633366353431303231633233376639303066333633323838613864346464393463323637306365313239393864626237396235333262623266383330323164663062656631326138363664646132343132653338333535636137646465613364336663366535303532346634653961313435366466313034626238376433633532353837643036613162383066613361613064643330633866356239373338663930336535653432653362363333333739333863 + + + 2 + 61 + 1 + 2 + 4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73656374657475722061646970697363696E6720656C69742E205365642075742074696E636964756E74206C6F72656D2E205072616573656E74207665686963756C6120766F6C7574706174206C696265726F2073697420616D65742066617563696275732E2041656E65616E20766974616520617263752071756973206C616375732070756C76696E617220737573636970697420617420656765742074656C6C75732E204E616D2066656C6973207475727069732C207363656C657269737175652075742066656C69732061632C2074696E636964756E74206D6178696D7573206D692E205365642076756C7075746174652076697461652073656D20616320736F6C6C696369747564696E2E20446F6E6563206672696E67696C6C61206C6F626F7274697320657820657520706F72747469746F722E20457469616D2073656D70657220736F6C6C696369747564696E207665686963756C612E20467573636520766172697573206E756C6C61206D657475732C206E65632076697665727261206E696268206469676E697373696D2069642E + + + + 3 + 10 + 2 + 1 + 646566353032303064656134373466643830666664303264336662323864396566393731653237643161333834353933323463376461353236343466316430316638326335316536623332366533383066353463623235653235613738656637393639643066363465623331313961313731613536353632383439303539633937616561636362653539393030373861393264623262336330363331326534613034363634306435646366643965 + 6465663130303030646566353032303036373464616463613665303139656365643164306230663630643430343635636531373031663039333263653334343563336338383736653362333531373631613939346135306231373962303566343035373936376334613732623165303637646133623137663339623831383339663230613038663139623363356631316531636436323062336564383636626432353433383633326337653365666530393666613736666164336461623832393934646335653037313338623936353761333464373435373733653861323761323036346333313461346335343465376364363333666434353961363935373632663035666466333330323032636430623864316633333835386330313663336431336566376663653866376562633263393938316365616562663737323162346136303137653864336533653762613635623334373031626238306262383565613735356238653166616235316666313839383662663666393861376231323263313934363034333064316262363539346233643439333664303932323066303938333332376437303732653332376462626339323833636134663830666164303063643730326138623031373337366438326138343636643936333330653762646232633637666330373531363434626166623031346564306563353436 + + + +