RefreshWebcalJobTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\BackgroundJob;
  28. use OCA\DAV\BackgroundJob\RefreshWebcalJob;
  29. use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\BackgroundJob\IJobList;
  32. use OCP\IConfig;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Psr\Log\LoggerInterface;
  35. use Test\TestCase;
  36. class RefreshWebcalJobTest extends TestCase {
  37. /** @var RefreshWebcalService | MockObject */
  38. private $refreshWebcalService;
  39. /** @var IConfig | MockObject */
  40. private $config;
  41. private LoggerInterface $logger;
  42. /** @var ITimeFactory | MockObject */
  43. private $timeFactory;
  44. /** @var IJobList | MockObject */
  45. private $jobList;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->refreshWebcalService = $this->createMock(RefreshWebcalService::class);
  49. $this->config = $this->createMock(IConfig::class);
  50. $this->logger = $this->createMock(LoggerInterface::class);
  51. $this->timeFactory = $this->createMock(ITimeFactory::class);
  52. $this->jobList = $this->createMock(IJobList::class);
  53. }
  54. /**
  55. *
  56. * @param int $lastRun
  57. * @param int $time
  58. * @param bool $process
  59. *
  60. * @dataProvider runDataProvider
  61. */
  62. public function testRun(int $lastRun, int $time, bool $process): void {
  63. $backgroundJob = new RefreshWebcalJob($this->refreshWebcalService, $this->config, $this->logger, $this->timeFactory);
  64. $backgroundJob->setId(42);
  65. $backgroundJob->setArgument([
  66. 'principaluri' => 'principals/users/testuser',
  67. 'uri' => 'sub123',
  68. ]);
  69. $backgroundJob->setLastRun($lastRun);
  70. $this->refreshWebcalService->expects($this->once())
  71. ->method('getSubscription')
  72. ->with('principals/users/testuser', 'sub123')
  73. ->willReturn([
  74. 'id' => '99',
  75. 'uri' => 'sub456',
  76. '{http://apple.com/ns/ical/}refreshrate' => 'P1D',
  77. '{http://calendarserver.org/ns/}subscribed-strip-todos' => '1',
  78. '{http://calendarserver.org/ns/}subscribed-strip-alarms' => '1',
  79. '{http://calendarserver.org/ns/}subscribed-strip-attachments' => '1',
  80. 'source' => 'webcal://foo.bar/bla'
  81. ]);
  82. $this->config->expects($this->once())
  83. ->method('getAppValue')
  84. ->with('dav', 'calendarSubscriptionRefreshRate', 'P1W')
  85. ->willReturn('P1W');
  86. $this->timeFactory->method('getTime')
  87. ->willReturn($time);
  88. if ($process) {
  89. $this->refreshWebcalService->expects($this->once())
  90. ->method('refreshSubscription')
  91. ->with('principals/users/testuser', 'sub123');
  92. } else {
  93. $this->refreshWebcalService->expects($this->never())
  94. ->method('refreshSubscription')
  95. ->with('principals/users/testuser', 'sub123');
  96. }
  97. $backgroundJob->start($this->jobList);
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function runDataProvider():array {
  103. return [
  104. [0, 100000, true],
  105. [100000, 100000, false]
  106. ];
  107. }
  108. }