GenerateBirthdayCalendarBackgroundJobTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\GenerateBirthdayCalendarBackgroundJob;
  29. use OCA\DAV\CalDAV\BirthdayService;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\IConfig;
  32. use PHPUnit\Framework\MockObject\MockObject;
  33. use Test\TestCase;
  34. class GenerateBirthdayCalendarBackgroundJobTest extends TestCase {
  35. /** @var ITimeFactory|MockObject */
  36. private $time;
  37. /** @var BirthdayService | MockObject */
  38. private $birthdayService;
  39. /** @var IConfig | MockObject */
  40. private $config;
  41. /** @var \OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob */
  42. private $backgroundJob;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->time = $this->createMock(ITimeFactory::class);
  46. $this->birthdayService = $this->createMock(BirthdayService::class);
  47. $this->config = $this->createMock(IConfig::class);
  48. $this->backgroundJob = new GenerateBirthdayCalendarBackgroundJob(
  49. $this->time,
  50. $this->birthdayService,
  51. $this->config,
  52. );
  53. }
  54. public function testRun(): void {
  55. $this->config->expects($this->once())
  56. ->method('getAppValue')
  57. ->with('dav', 'generateBirthdayCalendar', 'yes')
  58. ->willReturn('yes');
  59. $this->config->expects($this->once())
  60. ->method('getUserValue')
  61. ->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
  62. ->willReturn('yes');
  63. $this->birthdayService->expects($this->never())
  64. ->method('resetForUser')
  65. ->with('user123');
  66. $this->birthdayService->expects($this->once())
  67. ->method('syncUser')
  68. ->with('user123');
  69. $this->backgroundJob->run(['userId' => 'user123']);
  70. }
  71. public function testRunAndReset(): void {
  72. $this->config->expects($this->once())
  73. ->method('getAppValue')
  74. ->with('dav', 'generateBirthdayCalendar', 'yes')
  75. ->willReturn('yes');
  76. $this->config->expects($this->once())
  77. ->method('getUserValue')
  78. ->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
  79. ->willReturn('yes');
  80. $this->birthdayService->expects($this->once())
  81. ->method('resetForUser')
  82. ->with('user123');
  83. $this->birthdayService->expects($this->once())
  84. ->method('syncUser')
  85. ->with('user123');
  86. $this->backgroundJob->run(['userId' => 'user123', 'purgeBeforeGenerating' => true]);
  87. }
  88. public function testRunGloballyDisabled(): void {
  89. $this->config->expects($this->once())
  90. ->method('getAppValue')
  91. ->with('dav', 'generateBirthdayCalendar', 'yes')
  92. ->willReturn('no');
  93. $this->config->expects($this->never())
  94. ->method('getUserValue');
  95. $this->birthdayService->expects($this->never())
  96. ->method('syncUser');
  97. $this->backgroundJob->run(['userId' => 'user123']);
  98. }
  99. public function testRunUserDisabled(): void {
  100. $this->config->expects($this->once())
  101. ->method('getAppValue')
  102. ->with('dav', 'generateBirthdayCalendar', 'yes')
  103. ->willReturn('yes');
  104. $this->config->expects($this->once())
  105. ->method('getUserValue')
  106. ->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
  107. ->willReturn('no');
  108. $this->birthdayService->expects($this->never())
  109. ->method('syncUser');
  110. $this->backgroundJob->run(['userId' => 'user123']);
  111. }
  112. }