diff --git a/lib/SP/Repositories/Notification/NotificationRepository.php b/lib/SP/Repositories/Notification/NotificationRepository.php index 1a293e87..4452f8ac 100644 --- a/lib/SP/Repositories/Notification/NotificationRepository.php +++ b/lib/SP/Repositories/Notification/NotificationRepository.php @@ -486,7 +486,7 @@ class NotificationRepository extends Repository implements RepositoryItemInterfa sticky, onlyAdmin FROM Notification - WHERE (userId = ? OR userId IS NULL OR sticky = 1) + WHERE (userId = ? OR (userId IS NULL AND sticky = 1)) AND onlyAdmin = 0 ORDER BY `date` DESC '; diff --git a/lib/SP/Services/Notification/NotificationService.php b/lib/SP/Services/Notification/NotificationService.php index a36a22ba..dad44e26 100644 --- a/lib/SP/Services/Notification/NotificationService.php +++ b/lib/SP/Services/Notification/NotificationService.php @@ -26,6 +26,7 @@ namespace SP\Services\Notification; use SP\DataModel\ItemSearchData; use SP\DataModel\NotificationData; +use SP\Repositories\NoSuchItemException; use SP\Repositories\Notification\NotificationRepository; use SP\Services\Service; use SP\Services\ServiceException; @@ -91,14 +92,14 @@ class NotificationService extends Service * @param $id * * @return NotificationService - * @throws ServiceException + * @throws NoSuchItemException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function delete($id) { if ($this->notificationRepository->delete($id) === 0) { - throw new ServiceException(__u('Notificación no encontrada'), ServiceException::INFO); + throw new NoSuchItemException(__u('Notificación no encontrada'), NoSuchItemException::INFO); } return $this; @@ -110,14 +111,14 @@ class NotificationService extends Service * @param $id * * @return NotificationService - * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException */ public function deleteAdmin($id) { if ($this->notificationRepository->deleteAdmin($id) === 0) { - throw new ServiceException(__u('Notificación no encontrada'), ServiceException::INFO); + throw new NoSuchItemException(__u('Notificación no encontrada'), NoSuchItemException::INFO); } return $this; @@ -169,10 +170,17 @@ class NotificationService extends Service * @return NotificationData * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException */ public function getById($id) { - return $this->notificationRepository->getById($id)->getData(); + $result = $this->notificationRepository->getById($id); + + if ($result->getNumRows() === 0) { + throw new NoSuchItemException(__u('Notificación no encontrada'), NoSuchItemException::INFO); + } + + return $result->getData(); } /** @@ -192,13 +200,15 @@ class NotificationService extends Service * * @param $id * - * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException */ public function setCheckedById($id) { - return $this->notificationRepository->setCheckedById($id); + if ($this->notificationRepository->setCheckedById($id) === 0) { + throw new NoSuchItemException(__u('Notificación no encontrada'), NoSuchItemException::INFO); + } } /** diff --git a/test/Services/Notification/NotificationServiceTest.php b/test/Services/Notification/NotificationServiceTest.php new file mode 100644 index 00000000..d3289aba --- /dev/null +++ b/test/Services/Notification/NotificationServiceTest.php @@ -0,0 +1,433 @@ +. + */ + +namespace SP\Tests\Services\Notification; + +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Messages\NotificationMessage; +use SP\DataModel\ItemSearchData; +use SP\DataModel\NotificationData; +use SP\Repositories\NoSuchItemException; +use SP\Services\Notification\NotificationService; +use SP\Services\ServiceException; +use SP\Storage\Database\DatabaseConnectionData; +use SP\Test\DatabaseTestCase; +use function SP\Test\setupContext; + +/** + * Class NotificationServiceTest + * + * @package SP\Tests\Services\Notification + */ +class NotificationServiceTest extends DatabaseTestCase +{ + /** + * @var NotificationService + */ + private static $service; + + /** + * @throws \DI\NotFoundException + * @throws \SP\Core\Context\ContextException + * @throws \DI\DependencyException + */ + public static function setUpBeforeClass() + { + $dic = setupContext(); + + self::$dataset = 'syspass_notification.xml'; + + // Datos de conexión a la BBDD + self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); + + // Inicializar el repositorio + self::$service = $dic->get(NotificationService::class); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetAll() + { + $data = self::$service->getAll(); + + $this->assertCount(3, $data); + $this->assertEquals(1, $data[0]->getId()); + $this->assertEquals(2, $data[1]->getId()); + $this->assertEquals(3, $data[2]->getId()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testSearchForUserId() + { + $itemSearchData = new ItemSearchData(); + $itemSearchData->setLimitCount(10); + $itemSearchData->setSeachString('Test'); + + $result = self::$service->searchForUserId($itemSearchData, 2); + /** @var NotificationData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertEquals(2, $data[0]->getId()); + + $itemSearchData->setSeachString('Accounts'); + + $result = self::$service->searchForUserId($itemSearchData, 2); + /** @var NotificationData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertEquals(2, $result->getNumRows()); + $this->assertCount(2, $data); + $this->assertEquals(2, $data[0]->getId()); + $this->assertEquals(1, $data[1]->getId()); + + $itemSearchData->setSeachString('Admins'); + + $result = self::$service->searchForUserId($itemSearchData, 2); + + $this->assertEquals(0, $result->getNumRows()); + + $itemSearchData->setSeachString('Global'); + + $result = self::$service->searchForUserId($itemSearchData, 2); + /** @var NotificationData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertEquals(2, $data[0]->getId()); + + $itemSearchData->setSeachString(''); + + $result = self::$service->searchForUserId($itemSearchData, 2); + /** @var NotificationData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertEquals(2, $result->getNumRows()); + $this->assertCount(2, $data); + $this->assertEquals(2, $data[0]->getId()); + $this->assertEquals(1, $data[1]->getId()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException + */ + public function testGetAllActiveForUserId() + { + $data = self::$service->getAllActiveForUserId(2); + + $this->assertCount(2, $data); + $this->assertEquals(2, $data[0]->getId()); + $this->assertEquals(1, $data[1]->getId()); + + $data = self::$service->getAllActiveForUserId(3); + + $this->assertCount(1, $data); + $this->assertEquals(2, $data[0]->getId()); + + self::$service->setCheckedById(1); + + $data = self::$service->getAllActiveForUserId(2); + + $this->assertCount(1, $data); + $this->assertEquals(2, $data[0]->getId()); + + self::$service->setCheckedById(2); + + $this->assertCount(0, self::$service->getAllActiveForUserId(2)); + $this->assertCount(0, self::$service->getAllActiveForUserId(3)); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testSetCheckedById() + { + self::$service->setCheckedById(1); + + $this->assertTrue(true); + + $this->expectException(NoSuchItemException::class); + + $this->assertEquals(0, self::$service->setCheckedById(4)); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetForUserIdByDate() + { + $data = self::$service->getForUserIdByDate('Accounts', 2); + + $this->assertCount(1, $data); + $this->assertEquals(1, $data[0]->getId()); + + $this->assertCount(0, self::$service->getForUserIdByDate('Accounts', 3)); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException + */ + public function testCreate() + { + $data = new NotificationData(); + $data->setId(4); + $data->setUserId(2); + $data->setDate(time()); + $data->setType('Test'); + $data->setComponent('Config'); + $data->setDescription(NotificationMessage::factory()->setTitle('Prueba')->setDescription(['blablabla'])); + $data->setChecked(0); + $data->setOnlyAdmin(1); + $data->setSticky(1); + + $this->assertEquals(4, self::$service->create($data)); + + $this->assertEquals($data, self::$service->getById(4)); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetByIdBatch() + { + $this->assertCount(0, self::$service->getByIdBatch([])); + + $data = self::$service->getByIdBatch([1, 2, 3, 4]); + + $this->assertCount(3, $data); + $this->assertInstanceOf(NotificationData::class, $data[0]); + $this->assertEquals(1, $data[0]->getId()); + $this->assertEquals('Prueba', $data[0]->getType()); + $this->assertEquals('Accounts', $data[0]->getComponent()); + $this->assertEquals('Notificación de prueba', trim($data[0]->getDescription())); + $this->assertEquals(1529145158, $data[0]->getDate()); + $this->assertEquals(0, $data[0]->isChecked()); + $this->assertEquals(0, $data[0]->isOnlyAdmin()); + $this->assertEquals(0, $data[0]->isSticky()); + $this->assertEquals(2, $data[0]->getUserId()); + + $this->assertEquals(2, $data[1]->getId()); + $this->assertEquals(3, $data[2]->getId()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testSearch() + { + $itemSearchData = new ItemSearchData(); + $itemSearchData->setLimitCount(10); + $itemSearchData->setSeachString('Test'); + + $result = self::$service->search($itemSearchData); + + $this->assertEquals(2, $result->getNumRows()); + + $itemSearchData->setSeachString('Global'); + + $result = self::$service->search($itemSearchData); + /** @var NotificationData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertEquals(1, $result->getNumRows()); + $this->assertCount(1, $data); + $this->assertInstanceOf(NotificationData::class, $data[0]); + $this->assertEquals(2, $data[0]->getId()); + $this->assertEquals('Global', $data[0]->getType()); + + $itemSearchData->setSeachString(''); + + $result = self::$service->search($itemSearchData); + + $this->assertEquals(3, $result->getNumRows()); + $this->assertCount(3, $result->getDataAsArray()); + + $itemSearchData->setSeachString('Accounts'); + + $result = self::$service->search($itemSearchData); + /** @var NotificationData[] $data */ + $data = $result->getDataAsArray(); + + $this->assertEquals(3, $result->getNumRows()); + $this->assertCount(3, $data); + $this->assertEquals(1529145313, $data[0]->getDate()); + $this->assertEquals('Accounts', $data[0]->getComponent()); + $this->assertEquals(1529145296, $data[1]->getDate()); + $this->assertEquals('Accounts', $data[1]->getComponent()); + $this->assertEquals(1529145158, $data[2]->getDate()); + $this->assertEquals('Accounts', $data[2]->getComponent()); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws NoSuchItemException + */ + public function testGetById() + { + $data = self::$service->getById(3); + + $this->assertInstanceOf(NotificationData::class, $data); + $this->assertEquals(3, $data->getId()); + + $this->expectException(NoSuchItemException::class); + + self::$service->getById(4); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + */ + public function testDeleteAdminBatch() + { + $this->assertEquals(3, self::$service->deleteAdminBatch([1, 2, 3])); + + $this->assertEquals(0, self::$service->deleteAdminBatch([])); + + $this->assertEquals(0, $this->conn->getRowCount('Notification')); + + $this->expectException(ServiceException::class); + + $this->assertEquals(2, self::$service->deleteAdminBatch([4])); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDeleteAdmin() + { + $countBefore = $this->conn->getRowCount('Notification'); + + self::$service->deleteAdmin(3); + + $this->assertEquals($countBefore - 1, $this->conn->getRowCount('Notification')); + + $this->expectException(NoSuchItemException::class); + + $this->assertEquals(1, self::$service->deleteAdmin(4)); + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testGetAllForUserId() + { + $data = self::$service->getAllForUserId(2); + + $this->assertCount(2, $data); + $this->assertEquals(2, $data[0]->getId()); + $this->assertEquals(1, $data[1]->getId()); + + $this->assertCount(1, self::$service->getAllForUserId(3)); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testUpdate() + { + $data = new NotificationData(); + $data->setId(3); + $data->setUserId(2); + $data->setDate(time()); + $data->setType('Test'); + $data->setComponent('Config'); + $data->setDescription(NotificationMessage::factory()->setTitle('Prueba')->setDescription(['blablabla'])); + $data->setChecked(0); + $data->setOnlyAdmin(1); + $data->setSticky(1); + + $this->assertEquals(1, self::$service->update($data)); + + $this->assertEquals($data, self::$service->getById(3)); + + $data->setId(4); + + $this->assertEquals(0, self::$service->update($data)); + + $data = new NotificationData(); + $data->setId(1); + + $this->expectException(ConstraintException::class); + + $this->assertEquals(0, self::$service->update($data)); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws \SP\Core\Exceptions\QueryException + */ + public function testDelete() + { + self::$service->delete(3); + + $this->assertEquals(2, $this->conn->getRowCount('Notification')); + + $this->expectException(NoSuchItemException::class); + + $this->assertEquals(0, self::$service->delete(4)); + } + + /** + * @throws ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Services\ServiceException + */ + public function testDeleteByIdBatch() + { + $this->assertEquals(2, self::$service->deleteByIdBatch([1, 3])); + + $this->assertEquals(0, self::$service->deleteByIdBatch([])); + + $this->assertEquals(1, $this->conn->getRowCount('Notification')); + + $this->expectException(ServiceException::class); + + $this->assertEquals(2, self::$service->deleteByIdBatch([2])); + } +} diff --git a/test/res/datasets/syspass_notification.xml b/test/res/datasets/syspass_notification.xml new file mode 100644 index 00000000..60b6dea3 --- /dev/null +++ b/test/res/datasets/syspass_notification.xml @@ -0,0 +1,46 @@ + + + + + + 1 + Prueba + Accounts + + Notificación de prueba + + 1529145158 + 0 + 2 + 0 + 0 + + + 2 + Global + Accounts + + test + + 1529145296 + 0 + + 1 + 0 + + + 3 + Admins + Accounts + + test + + 1529145313 + 0 + + 0 + 1 + + + +