1
0

PruneOutdatedSyncTokensJobTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\BackgroundJob;
  8. use InvalidArgumentException;
  9. use OCA\DAV\AppInfo\Application;
  10. use OCA\DAV\BackgroundJob\PruneOutdatedSyncTokensJob;
  11. use OCA\DAV\CalDAV\CalDavBackend;
  12. use OCA\DAV\CardDAV\CardDavBackend;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\IConfig;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. class PruneOutdatedSyncTokensJobTest extends TestCase {
  19. /** @var ITimeFactory | MockObject */
  20. private $timeFactory;
  21. /** @var CalDavBackend | MockObject */
  22. private $calDavBackend;
  23. /** @var CardDavBackend | MockObject */
  24. private $cardDavBackend;
  25. /** @var IConfig|MockObject */
  26. private $config;
  27. /** @var LoggerInterface|MockObject*/
  28. private $logger;
  29. private PruneOutdatedSyncTokensJob $backgroundJob;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->timeFactory = $this->createMock(ITimeFactory::class);
  33. $this->calDavBackend = $this->createMock(CalDavBackend::class);
  34. $this->cardDavBackend = $this->createMock(CardDavBackend::class);
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->logger = $this->createMock(LoggerInterface::class);
  37. $this->backgroundJob = new PruneOutdatedSyncTokensJob($this->timeFactory, $this->calDavBackend, $this->cardDavBackend, $this->config, $this->logger);
  38. }
  39. /**
  40. * @dataProvider dataForTestRun
  41. */
  42. public function testRun(string $configToKeep, string $configRetentionDays, int $actualLimit, int $retentionDays, int $deletedCalendarSyncTokens, int $deletedAddressBookSyncTokens): void {
  43. $this->config->expects($this->exactly(2))
  44. ->method('getAppValue')
  45. ->with(Application::APP_ID, self::anything(), self::anything())
  46. ->willReturnCallback(function ($app, $key) use ($configToKeep, $configRetentionDays) {
  47. switch ($key) {
  48. case 'totalNumberOfSyncTokensToKeep':
  49. return $configToKeep;
  50. case 'syncTokensRetentionDays':
  51. return $configRetentionDays;
  52. default:
  53. throw new InvalidArgumentException();
  54. }
  55. });
  56. $this->calDavBackend->expects($this->once())
  57. ->method('pruneOutdatedSyncTokens')
  58. ->with($actualLimit)
  59. ->willReturn($deletedCalendarSyncTokens);
  60. $this->cardDavBackend->expects($this->once())
  61. ->method('pruneOutdatedSyncTokens')
  62. ->with($actualLimit, $retentionDays)
  63. ->willReturn($deletedAddressBookSyncTokens);
  64. $this->logger->expects($this->once())
  65. ->method('info')
  66. ->with('Pruned {calendarSyncTokensNumber} calendar sync tokens and {addressBooksSyncTokensNumber} address book sync tokens', [
  67. 'calendarSyncTokensNumber' => $deletedCalendarSyncTokens,
  68. 'addressBooksSyncTokensNumber' => $deletedAddressBookSyncTokens
  69. ]);
  70. $this->backgroundJob->run(null);
  71. }
  72. public function dataForTestRun(): array {
  73. return [
  74. ['100', '2', 100, 7 * 24 * 3600, 2, 3],
  75. ['100', '14', 100, 14 * 24 * 3600, 2, 3],
  76. ['0', '60', 1, 60 * 24 * 3600, 0, 0]
  77. ];
  78. }
  79. }