RefreshWebcalJobTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 OCP\ILogger;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Test\TestCase;
  36. class RefreshWebcalJobTest extends TestCase {
  37. /** @var RefreshWebcalService | MockObject */
  38. private $refreshWebcalService;
  39. /** @var IConfig | MockObject */
  40. private $config;
  41. /** @var ILogger | MockObject */
  42. private $logger;
  43. /** @var ITimeFactory | MockObject */
  44. private $timeFactory;
  45. /** @var IJobList | MockObject */
  46. private $jobList;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->refreshWebcalService = $this->createMock(RefreshWebcalService::class);
  50. $this->config = $this->createMock(IConfig::class);
  51. $this->logger = $this->createMock(ILogger::class);
  52. $this->timeFactory = $this->createMock(ITimeFactory::class);
  53. $this->jobList = $this->createMock(IJobList::class);
  54. }
  55. /**
  56. *
  57. * @param int $lastRun
  58. * @param int $time
  59. * @param bool $process
  60. *
  61. * @dataProvider runDataProvider
  62. */
  63. public function testRun(int $lastRun, int $time, bool $process): void {
  64. $backgroundJob = new RefreshWebcalJob($this->refreshWebcalService, $this->config, $this->logger, $this->timeFactory);
  65. $backgroundJob->setId(42);
  66. $backgroundJob->setArgument([
  67. 'principaluri' => 'principals/users/testuser',
  68. 'uri' => 'sub123',
  69. ]);
  70. $backgroundJob->setLastRun($lastRun);
  71. $this->refreshWebcalService->expects($this->once())
  72. ->method('getSubscription')
  73. ->with('principals/users/testuser', 'sub123')
  74. ->willReturn([
  75. 'id' => '99',
  76. 'uri' => 'sub456',
  77. '{http://apple.com/ns/ical/}refreshrate' => 'P1D',
  78. '{http://calendarserver.org/ns/}subscribed-strip-todos' => '1',
  79. '{http://calendarserver.org/ns/}subscribed-strip-alarms' => '1',
  80. '{http://calendarserver.org/ns/}subscribed-strip-attachments' => '1',
  81. 'source' => 'webcal://foo.bar/bla'
  82. ]);
  83. $this->config->expects($this->once())
  84. ->method('getAppValue')
  85. ->with('dav', 'calendarSubscriptionRefreshRate', 'P1W')
  86. ->willReturn('P1W');
  87. $this->timeFactory->method('getTime')
  88. ->willReturn($time);
  89. if ($process) {
  90. $this->refreshWebcalService->expects($this->once())
  91. ->method('refreshSubscription')
  92. ->with('principals/users/testuser', 'sub123');
  93. } else {
  94. $this->refreshWebcalService->expects($this->never())
  95. ->method('refreshSubscription')
  96. ->with('principals/users/testuser', 'sub123');
  97. }
  98. $backgroundJob->execute($this->jobList, $this->logger);
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function runDataProvider():array {
  104. return [
  105. [0, 100000, true],
  106. [100000, 100000, false]
  107. ];
  108. }
  109. }