. */ namespace SP\Test\Repositories; use DI\DependencyException; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Messages\NotificationMessage; use SP\DataModel\ItemSearchData; use SP\DataModel\NotificationData; use SP\Repositories\Notification\NotificationRepository; use SP\Storage\Database\DatabaseConnectionData; use SP\Test\DatabaseTestCase; use function SP\Test\setupContext; /** * Class NotificationRepositoryTest * * @package SP\Tests\Repositories */ class NotificationRepositoryTest extends DatabaseTestCase { /** * @var NotificationRepository */ private static $repository; /** * @throws DependencyException * @throws \DI\NotFoundException * @throws \SP\Core\Context\ContextException */ public static function setUpBeforeClass() { $dic = setupContext(); self::$dataset = 'syspass.xml'; // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); // Inicializar el repositorio self::$repository = $dic->get(NotificationRepository::class); } /** * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function testDeleteAdmin() { $countBefore = $this->conn->getRowCount('Notification'); $this->assertEquals(1, self::$repository->deleteAdmin(3)); $this->assertEquals($countBefore - 1, $this->conn->getRowCount('Notification')); } /** * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function testDeleteAdminBatch() { $this->assertEquals(3, self::$repository->deleteAdminBatch([1, 2, 3, 5])); $this->assertEquals(0, $this->conn->getRowCount('Notification')); } /** * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function testGetByIdBatch() { $this->assertEquals(0, self::$repository->getByIdBatch([])->getNumRows()); $result = self::$repository->getByIdBatch([1, 2, 3, 4]); /** @var NotificationData[] $data */ $data = $result->getDataAsArray(); $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 ConstraintException * @throws QueryException */ public function testGetById() { $result = self::$repository->getById(3); /** @var NotificationData $data */ $data = $result->getData(); $this->assertEquals(1, $result->getNumRows()); $this->assertInstanceOf(NotificationData::class, $data); $this->assertEquals(3, $data->getId()); $result = self::$repository->getById(4); $this->assertEquals(0, $result->getNumRows()); } /** * @depends testGetById * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ 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::$repository->create($data)->getLastId()); $this->assertEquals($data, self::$repository->getById(4)->getData()); } /** * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function testSearch() { $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('Test'); $result = self::$repository->search($itemSearchData); $this->assertEquals(2, $result->getNumRows()); $itemSearchData->setSeachString('Global'); $result = self::$repository->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::$repository->search($itemSearchData); $this->assertEquals(3, $result->getNumRows()); $this->assertCount(3, $result->getDataAsArray()); $itemSearchData->setSeachString('Accounts'); $result = self::$repository->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 */ public function testSetCheckedById() { $this->assertEquals(1, self::$repository->setCheckedById(1)); $this->assertEquals(0, self::$repository->setCheckedById(4)); } /** * @depends testGetById * @throws \SP\Core\Exceptions\ConstraintException * @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::$repository->update($data)); $this->assertEquals($data, self::$repository->getById(3)->getData()); $data->setId(4); $this->assertEquals(0, self::$repository->update($data)); $data = new NotificationData(); $data->setId(1); $this->expectException(ConstraintException::class); $this->assertEquals(0, self::$repository->update($data)); } /** * @throws ConstraintException * @throws QueryException */ public function testSearchForUserId() { $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('Test'); $result = self::$repository->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::$repository->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::$repository->searchForUserId($itemSearchData, 2); $this->assertEquals(0, $result->getNumRows()); $itemSearchData->setSeachString('Global'); $result = self::$repository->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::$repository->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 ConstraintException * @throws QueryException */ public function testGetAll() { $result = self::$repository->getAll(); /** @var NotificationData[] $data */ $data = $result->getDataAsArray(); $this->assertEquals(3, $result->getNumRows()); $this->assertCount(3, $data); $this->assertEquals(1, $data[0]->getId()); $this->assertEquals(2, $data[1]->getId()); $this->assertEquals(3, $data[2]->getId()); } /** * @throws ConstraintException * @throws QueryException */ public function testGetForUserIdByDate() { $result = self::$repository->getForUserIdByDate('Accounts', 2); /** @var NotificationData[] $data */ $data = $result->getDataAsArray(); $this->assertEquals(1, $result->getNumRows()); $this->assertCount(1, $data); $this->assertEquals(1, $data[0]->getId()); $result = self::$repository->getForUserIdByDate('Accounts', 3); $this->assertEquals(0, $result->getNumRows()); } /** * @throws ConstraintException * @throws QueryException */ public function testDelete() { $this->assertEquals(1, self::$repository->delete(3)); $this->assertEquals(0, self::$repository->delete(4)); } /** * @throws ConstraintException * @throws QueryException */ public function testGetAllForUserId() { $result = self::$repository->getAllForUserId(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()); $result = self::$repository->getAllForUserId(3); $this->assertEquals(1, $result->getNumRows()); } /** * @throws ConstraintException * @throws QueryException */ public function testDeleteByIdBatch() { $this->assertEquals(2, self::$repository->deleteByIdBatch([1, 2, 3, 4])); $this->assertEquals(0, self::$repository->deleteByIdBatch([])); $this->assertEquals(1, $this->conn->getRowCount('Notification')); } /** * @depends testSetCheckedById * * @throws ConstraintException * @throws QueryException */ public function testGetAllActiveForUserId() { $result = self::$repository->getAllActiveForUserId(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()); $result = self::$repository->getAllActiveForUserId(3); /** @var NotificationData[] $data */ $data = $result->getDataAsArray(); $this->assertEquals(1, $result->getNumRows()); $this->assertCount(1, $data); $this->assertEquals(2, $data[0]->getId()); self::$repository->setCheckedById(1); $result = self::$repository->getAllActiveForUserId(2); /** @var NotificationData[] $data */ $data = $result->getDataAsArray(); $this->assertEquals(1, $result->getNumRows()); $this->assertCount(1, $data); $this->assertEquals(2, $data[0]->getId()); self::$repository->setCheckedById(2); $this->assertEquals(0, self::$repository->getAllActiveForUserId(2)->getNumRows()); $this->assertEquals(0, self::$repository->getAllActiveForUserId(3)->getNumRows()); } }