RefreshWebcalJobTest.php 3.7 KB

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