RefreshWebcalJobTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 OCA\DAV\BackgroundJob\RefreshWebcalJob;
  9. use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\BackgroundJob\IJobList;
  12. use OCP\IConfig;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Psr\Log\LoggerInterface;
  15. use Test\TestCase;
  16. class RefreshWebcalJobTest extends TestCase {
  17. /** @var RefreshWebcalService | MockObject */
  18. private $refreshWebcalService;
  19. /** @var IConfig | MockObject */
  20. private $config;
  21. private LoggerInterface $logger;
  22. /** @var ITimeFactory | MockObject */
  23. private $timeFactory;
  24. /** @var IJobList | MockObject */
  25. private $jobList;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->refreshWebcalService = $this->createMock(RefreshWebcalService::class);
  29. $this->config = $this->createMock(IConfig::class);
  30. $this->logger = $this->createMock(LoggerInterface::class);
  31. $this->timeFactory = $this->createMock(ITimeFactory::class);
  32. $this->jobList = $this->createMock(IJobList::class);
  33. }
  34. /**
  35. *
  36. * @param int $lastRun
  37. * @param int $time
  38. * @param bool $process
  39. *
  40. * @dataProvider runDataProvider
  41. */
  42. public function testRun(int $lastRun, int $time, bool $process): void {
  43. $backgroundJob = new RefreshWebcalJob($this->refreshWebcalService, $this->config, $this->logger, $this->timeFactory);
  44. $backgroundJob->setId(42);
  45. $backgroundJob->setArgument([
  46. 'principaluri' => 'principals/users/testuser',
  47. 'uri' => 'sub123',
  48. ]);
  49. $backgroundJob->setLastRun($lastRun);
  50. $this->refreshWebcalService->expects($this->once())
  51. ->method('getSubscription')
  52. ->with('principals/users/testuser', 'sub123')
  53. ->willReturn([
  54. 'id' => '99',
  55. 'uri' => 'sub456',
  56. '{http://apple.com/ns/ical/}refreshrate' => 'P1D',
  57. '{http://calendarserver.org/ns/}subscribed-strip-todos' => '1',
  58. '{http://calendarserver.org/ns/}subscribed-strip-alarms' => '1',
  59. '{http://calendarserver.org/ns/}subscribed-strip-attachments' => '1',
  60. 'source' => 'webcal://foo.bar/bla'
  61. ]);
  62. $this->config->expects($this->once())
  63. ->method('getAppValue')
  64. ->with('dav', 'calendarSubscriptionRefreshRate', 'P1W')
  65. ->willReturn('P1W');
  66. $this->timeFactory->method('getTime')
  67. ->willReturn($time);
  68. if ($process) {
  69. $this->refreshWebcalService->expects($this->once())
  70. ->method('refreshSubscription')
  71. ->with('principals/users/testuser', 'sub123');
  72. } else {
  73. $this->refreshWebcalService->expects($this->never())
  74. ->method('refreshSubscription')
  75. ->with('principals/users/testuser', 'sub123');
  76. }
  77. $backgroundJob->start($this->jobList);
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function runDataProvider():array {
  83. return [
  84. [0, 100000, true],
  85. [100000, 100000, false]
  86. ];
  87. }
  88. }