Skip to content

Commit

Permalink
Merge pull request #11853 from nextcloud/bugfix/noid/fix-pcm-expiration
Browse files Browse the repository at this point in the history
fix(federation): Fix expiration of cached messages
  • Loading branch information
nickvergessen authored Mar 19, 2024
2 parents 52a49ad + f63c8a2 commit 6a60046
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Model/ProxyCacheMessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
117 changes: 117 additions & 0 deletions tests/php/Service/ProxyCacheMessageServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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());
}
}
}

0 comments on commit 6a60046

Please sign in to comment.