diff --git a/lib/Model/ProxyCacheMessageMapper.php b/lib/Model/ProxyCacheMessageMapper.php index f4fba2b240d..f7006a13ef1 100644 --- a/lib/Model/ProxyCacheMessageMapper.php +++ b/lib/Model/ProxyCacheMessageMapper.php @@ -75,8 +75,8 @@ public function findByRemote(string $remoteServerUrl, string $remoteToken, int $ public function deleteExpiredMessages(\DateTimeInterface $dateTime): int { $query = $this->db->getQueryBuilder(); $query->delete($this->getTableName()) - ->where($query->expr()->isNotNull('expire_datetime')) - ->andWhere($query->expr()->lte('expire_datetime', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE))); + ->where($query->expr()->isNotNull('expiration_datetime')) + ->andWhere($query->expr()->lte('expiration_datetime', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE))); return $query->executeStatement(); } diff --git a/tests/php/Service/ProxyCacheMessageServiceTest.php b/tests/php/Service/ProxyCacheMessageServiceTest.php new file mode 100644 index 00000000000..f6612c9c2fd --- /dev/null +++ b/tests/php/Service/ProxyCacheMessageServiceTest.php @@ -0,0 +1,117 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Talk\Tests\php\Service; + +use OCA\Talk\Model\ProxyCacheMessage; +use OCA\Talk\Model\ProxyCacheMessageMapper; +use OCA\Talk\Service\ProxyCacheMessageService; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IDBConnection; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use Test\TestCase; + +/** + * @group DB + */ +class ProxyCacheMessageServiceTest extends TestCase { + protected ?ProxyCacheMessageMapper $mapper = null; + protected LoggerInterface|MockObject $logger; + protected ITimeFactory|MockObject $timeFactory; + protected ?ProxyCacheMessageService $service = null; + + + public function setUp(): void { + parent::setUp(); + + $this->mapper = new ProxyCacheMessageMapper(\OCP\Server::get(IDBConnection::class)); + $this->logger = $this->createMock(LoggerInterface::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + + $this->service = new ProxyCacheMessageService( + $this->mapper, + $this->logger, + $this->timeFactory, + ); + $this->clearMessages(); + } + + public function tearDown(): void { + $this->clearMessages(); + + parent::tearDown(); + } + + protected function clearMessages(): void { + $query = \OCP\Server::get(IDBConnection::class)->getQueryBuilder(); + $query->delete('talk_proxy_messages') + ->where($query->expr()->eq('remote_server_url', $query->createNamedParameter('phpunittests'))); + $query->executeStatement(); + } + + public function dataDeleteExpiredMessages(): array { + return [ + [1234, 12345, true], + [1234567, 12345, false], + [null, 12345, false], + ]; + } + + /** + * @dataProvider dataDeleteExpiredMessages + */ + public function testDeleteExpiredMessages(?int $messageTime, int $currentTime, bool $expired): void { + $m1 = new ProxyCacheMessage(); + $m1->setLocalToken('local_token'); + $m1->setRemoteServerUrl('phpunittests'); + $m1->setRemoteToken('remote_token'); + $m1->setRemoteMessageId(12345); + $m1->setActorType('actor_type'); + $m1->setActorId('actor_id'); + $m1->setMessageType('message_type'); + if ($messageTime === null) { + $m1->setExpirationDatetime($messageTime); + } else { + $m1->setExpirationDatetime(new \DateTime('@' . $messageTime)); + } + $this->mapper->insert($m1); + + $this->mapper->findById($m1->getId()); + + $this->timeFactory->method('getDateTime') + ->willReturn(new \DateTime('@' . $currentTime)); + $this->service->deleteExpiredMessages(); + + if ($expired) { + $this->expectException(DoesNotExistException::class); + } + $actual = $this->mapper->findById($m1->getId()); + if (!$expired) { + $this->assertEquals($m1->getId(), $actual->getId()); + } + } +}